Block thread execution waiting for UDP message

Hi, I have been working with Photon for a couple of month now. The project consists on several photons on a network, exchanging info via UDP messages. The info basically is the mac address, and some data that we are getting from an external sensor (HMC5883L), all the Photons send their info and receive the info from the other ones.

We have several threads running, basically we have one thread sending the mac address of the photon every a given amount of time, one thread sending the direction, and two more threads receiving the mac address and the direction.

Here is an example of a sender thread:

Thread *compassDirectionSenderThread;
os_thread_return_t compassDirectionSender(void* param){
    DataStatus *dataStatus = (DataStatus *)param;
    String myId = AddressManager::getIp();
    while (true) {
        UDP sendUdp;
        sendUdp.begin(groupPortCD+1);

        CompassDirectionWithTimeStamp oldData = dataStatus->getDataFrom(myId);
        int direction = oldData.direction.compassDirection + 1;
        CompassDirection dataStruct = { myId, direction };

        String dataToSend = DataCompassDirection::toString(dataStruct);
        char buffer[dataLength];
        dataToSend.toCharArray(buffer, dataLength);

        IPAddress remoteIP(groupIpCD);
        int port = groupPortCD;
        sendUdp.beginPacket(remoteIP, port);
        sendUdp.write(reinterpret_cast<const unsigned char*>(buffer), dataLength);
        sendUdp.endPacket();

        delay(publishTime*1000);
    }

}

And here is an example of a receiver thread:

Thread *compassDirectionReceiverThread;
os_thread_return_t compassDirectionReceiver(void* param){
    DataStatus *dataStatus = (DataStatus *)param;
    UDP receiveUdp;

    receiveUdp.begin(groupPortCD);
    receiveUdp.joinMulticast(groupIpCD);

    for(;;) {
        char data[dataLength];
        if (receiveUdp.parsePacket() > 0){
            receiveUdp.read(data, dataLength);
            String receivedData = String(data).substring(0, dataLength);
            dataStatus->setData(DataCompassDirection::toCompassDirection(receivedData));
        }
    }
}

I have two main troubles, the first one is that I couldn’t find a way to block the thread execution, until there is some dara received in the UDP socket, so the receiver thread, is in a busy wait, that continuously checks if parsePacket() > 0, while ideally, I want to block the thread execution until some new info is received. Is there a way to achive this?

The other concern, is that randomly, one of the receiver threads, does not receive anything else. As all the info is sent in multicast packets, I run a program that monitor multicast packets, and all of them are sent, but the Photon are not receiving them. Am I doing something wrong? Because some times everything works fine, but if maybe I shut down a photon, and after a while I turn on that same photon, other photons stop receiving the packets.

Looking in UDP implementation in github (https://github.com/spark/firmware/blob/master/hal/src/photon/socket_hal.cpp) I found that it’s using WICED library to handle sockets, and in the receive method, it’s doing something like:

sock_result_t socket_receivefrom(sock_handle_t sd, void* buffer, socklen_t bufLen, uint32_t flags, sockaddr_t* addr, socklen_t* addrsize) {
    ...
    // UDP receive timeout changed to 0 sec so as not to block
    if ((result=wiced_udp_receive(udp(socket), &packet, WICED_NO_WAIT))==WICED_SUCCESS) 

Is there a way to change this configuration?

Hello,

Let me ping someone that might be able to help, @tylercpeacock are you able to assist?

Kyle