Hello all,
I have a UDP TX socket and a UDP RX socket. I am using a semaphore to trigger a UDP TX since I have control over when to transmit a packet in my code. My question is how to do the same for the UDP receive task instead of block the task using TaskSleep(1);
My code for the 2 sockets is as follows:
void udp_rx_task_Fxn()
{
SOCKET sUDP = INVALID_SOCKET; // Listen socket.
struct sockaddr_in sin1; // Listen socket address structure
int recvCnt;
int tmp, k;
// Allocate the file environment for this task
fdOpenSession( TaskSelf() );
// Create the listening socket
sUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( sUDP == INVALID_SOCKET )
goto leave;
// sUDP socket: set Port = 5001, IP destination address = ANY
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_len = sizeof( sin1 );
sin1.sin_port = htons(5001);
// Bind sUDP socket
if ( bind( sUDP, (PSA) &sin1, sizeof(sin1) ) < 0 )
{
printf("sUDP: %d",fdError());
goto leave;
}
sin1.sin_addr.s_addr = inet_addr(UnicastAddr); // PC address is the destination
sin1.sin_port = htons(8); // port (8) is where we want to send data
tmp = sizeof(sin1);
for(;;)
{
//DM to DSP
//Check for new incoming packets from Host
recvCnt = recvfrom(sUDP, &ReceptMessage, sizeof(ReceptMessage), MSG_DONTWAIT, (PSA)&sin1, &tmp);
if(recvCnt > 0)
{
switch (ReceptMessage.MsgId)
{
case DM_HEARTBEAT_MSG:
...
}
}
TaskSleep(1); //??????????????????? how to use a different method to block a task
}
leave:
// We only get here on a fatal error - close the sockets
if( sUDP != INVALID_SOCKET )
fdClose( sUDP );
printf("Fatal Error\n");
// This task is killed by the system - here, we block
TaskBlock( TaskSelf() );
}
void udp_tx_task_Fxn()
{
SOCKET sUDP = INVALID_SOCKET; // send socket.
struct sockaddr_in sout1; // send socket address structure
int sentCnt;
int i;
// Allocate the file environment for this task
fdOpenSession( TaskSelf() );
// Create the sending socket
sUDP = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if( sUDP == INVALID_SOCKET )
goto leave;
// sUDP socket: set Port = 5001, IP destination address = ANY
bzero( &sout1, sizeof(struct sockaddr_in) );
sout1.sin_family = AF_INET;
sout1.sin_len = sizeof( sout1 );
sout1.sin_addr.s_addr = inet_addr(UnicastAddr); // PC address is the destination
sout1.sin_port = htons(8); // port (8) is where we want to send data
// Bind sUDP socket
if ( bind( sUDP, (PSA) &sout1, sizeof(sout1) ) < 0 )
{
printf("sUDP: %d",fdError());
goto leave;
}
for(;;)
{
ClearDspToDmMsg(&SendDsp2DMMessage);
//wait for semDsp2Dm semaphore to be posted
Semaphore_pend(semDsp2DM, BIOS_WAIT_FOREVER);
if(SDsp2DMReadyMessagesbits.DSP_HEARTBEAT_MSGbit)
{
...
}
}
leave:
// We only get here on a fatal error - close the sockets
if( sUDP != INVALID_SOCKET )
fdClose( sUDP );
printf("Fatal Error\n");
// This task is killed by the system - here, we block
TaskBlock( TaskSelf() );
}