Hi,
I am using FreeRTOS with FreeRTOS-TCP/IP stack on my TI ARM Cortex M4 controller i.e. TM4C129ENCPDT. I have a project that is communicating via UDP protocol and is doing good.
My firmware can communicate to a remotely connected client and serve the client request.
For example if my remote client send a UDP packet to switch on LED it will switch on and reply to the client as well.
However we need some change to it such that we want a remotely connected device send a broadcast UDP packet and then the controller extract the command from the broadcast packet and performs the requested task.
As I said my controller works fine if I send a direct udp packet to the controller IP address. However it will not reply if I send a broadcast udp packet.
Here is my code;
void UDPCommunicationTask( uint16_t usStackSize, uint32_t ulPort, UBaseType_t uxPriority )
{
xTaskCreate( prvUDPCommandInterpreterTask, "UDP COMM", usStackSize, ( void * ) ulPort, uxPriority, NULL );
}
/*-----------------------------------------------------------*/
void prvUDPCommandInterpreterTask( void *pvParameters )
{
int32_t lBytes, lByte;
char cRxedChar, cInputIndex = 0;
char datasize;
static unsigned char cOutputString[ cmdMAX_OUTPUT_SIZE ], cLocalBuffer[ cmdSOCKET_INPUT_BUFFER_SIZE ];
static char cLastInputString[ cmdMAX_INPUT_SIZE ], cInputString[ cmdMAX_INPUT_SIZE ];
BaseType_t xMoreDataToFollow;
struct freertos_sockaddr xClient;
memset( cInputString, 0x00, cmdMAX_INPUT_SIZE );
/* Attempt to open the socket. The port number is passed in the task
parameter. The strange casting is to remove compiler warnings on 32-bit
machines. */
xSocket = prvOpenUDPServerSocket( ( uint16_t ) ( ( uint32_t ) pvParameters ) & 0xffffUL );
uint8_t counter1,counter2,counter3,counter4;
counter1 = 0;
counter2 = 0;
counter3 = 0;
counter4 = 0;
if( xSocket != FREERTOS_INVALID_SOCKET )
{
for( ;; )
{
/* Wait for incoming data on the opened socket. */
lBytes = FreeRTOS_recvfrom( xSocket, ( void * ) cLocalBuffer, sizeof( cLocalBuffer ), 0, &xClient, &xClientAddressLength );
if (lBytes > 0)
{
LED0_HIGH;
udp_hex_mode = 1;
if(udp_hex_mode == 1)
{
memset( cOutputString, 0x00, cmdMAX_OUTPUT_SIZE );
hex_mode_cmd_interpretor(cLocalBuffer, cOutputString);
FreeRTOS_sendto( xSocket, cOutputString, strlen( ( const char * ) cOutputString ), 0, &xClient, xClientAddressLength );
}
LED0_LOW;
}
Here is how I create the socket;
static Socket_t prvOpenUDPServerSocket( uint16_t usPort )
{
struct freertos_sockaddr xServer;
Socket_t xServerSocket = FREERTOS_INVALID_SOCKET;
TickType_t xSendTimeOut = 0;
xServerSocket = FreeRTOS_socket( FREERTOS_AF_INET, FREERTOS_SOCK_DGRAM, FREERTOS_IPPROTO_UDP );
if( xServerSocket != FREERTOS_INVALID_SOCKET)
{
/* Set to non-blocking sends with a timeout of zero as the socket might
also be used for debug prints which should not block. */
FreeRTOS_setsockopt( xServerSocket, 0, FREERTOS_SO_SNDTIMEO, &xSendTimeOut, sizeof( xSendTimeOut ) );
/* Zero out the server structure. */
memset( ( void * ) &xServer, 0x00, sizeof( xServer ) );
/* Set family and port. */
xServer.sin_port = FreeRTOS_htons( usPort );
/* Bind the address to the socket. */
if( FreeRTOS_bind( xServerSocket, &xServer, sizeof( xServer ) ) == -1 )
{
FreeRTOS_closesocket( xServerSocket );
xServerSocket = FREERTOS_INVALID_SOCKET;
}
}
return xServerSocket;
}
Thank you all