lwIP: Blocking on queues and sockets

hermarcel wrote on Monday, August 05, 2013:

After asking some questions on this forum, I felt it was time to contribute a little.

I am running lwIP on FreeRTOS and was building an application that should block on a combination of FreeRTOS queues and lwIP sockets. After realizing that the lwIP socket-api is based on FreeRTOS queues, I came up with the following.

xQueueHandle lwip_GetAcceptQ(int s)
// Add these functions to the end of lwIP's sockets.c
{
	// Get FreeRTOS handle of accept queue
    xQueueHandle xQueue = NULL;
	struct lwip_sock *sock;
    
	sock = tryget_socket(s);
	if((sock != NULL) && (sock->conn != NULL))
    {
		xQueue = sock->conn->acceptmbox;
	}
    
    return(xQueue);
}
xQueueHandle lwip_GetRecvQ(int s)
{
	// Get FreeRTOS handle of receive queue
    xQueueHandle xQueue = NULL;
	struct lwip_sock *sock;
    
	sock = tryget_socket(s);
	if((sock != NULL) && (sock->conn != NULL))
    {
		xQueue = sock->conn->recvmbox;
	}
    
    return(xQueue);
}
int lwip_DataInRecvQ(int s)
{
	// Return 1 (TRUE)  if there is data in the socket receive pbuf(s),
    //        0 (FALSE) otherwise
    int iReturn = 0;
	struct lwip_sock *sock;
    
	sock = tryget_socket(s);
	if((sock != NULL) && (sock->conn != NULL))
	{
		iReturn = (sock->lastdata != NULL);
	}
    
    return(iReturn);
}

These functions return the queue handles that are (exclusively) used by lwIP  for the given socket. Having the queue-id, one can simply add it to a queue set.

One note: use the lwip_DataInRecvQ(s) function to check is there is data available in the local socket buffers. This data must be read before attempting to block on the socket receive queue.

hermarcel wrote on Monday, August 05, 2013:

(tidied up a little. Is there no preview on the forum? Or an edit button?)

// Add these functions to the end of lwIP's sockets.c
xQueueHandle lwip_GetAcceptQ(int s)
{
    // Get FreeRTOS handle of accept queue
    xQueueHandle xQueue = NULL;
    struct lwip_sock *sock;
    
    sock = tryget_socket(s);
    if((sock != NULL) && (sock->conn != NULL))
    {
        xQueue = sock->conn->acceptmbox;
    }
    
    return(xQueue);
}
xQueueHandle lwip_GetRecvQ(int s)
{
    // Get FreeRTOS handle of receive queue
    xQueueHandle xQueue = NULL;
    struct lwip_sock *sock;
    
    sock = tryget_socket(s);
    if((sock != NULL) && (sock->conn != NULL))
    {
        xQueue = sock->conn->recvmbox;
    }
    
    return(xQueue);
}
int lwip_DataInRecvQ(int s)
{
    // Return 1 (TRUE)  if there is data in the socket receive pbuf(s),
    //        0 (FALSE) otherwise
    int iReturn = 0;
    struct lwip_sock *sock;
    
    sock = tryget_socket(s);
    if((sock != NULL) && (sock->conn != NULL))
    {
        iReturn = (sock->lastdata != NULL);
    }
    
    return(iReturn);
}

rtel wrote on Monday, August 05, 2013:

Thanks for the input.

Unfortunately there is no edit button!

Regards.

hermarcel wrote on Tuesday, August 06, 2013:

Thanks for the input.

Well, it was a long time ago since I wrote the pic18/wizC port. Now I’m all into PIC32.