# Problem with lwip mailbox or FreeRTOS Queue

**URL:** <https://forums.freertos.org/t/problem-with-lwip-mailbox-or-freertos-queue/113>\
**Category:** Kernel\
**Created:** [June 15, 2009, 7:39am UTC](https://forums.freertos.org/t/problem-with-lwip-mailbox-or-freertos-queue/113 "2009-06-15T07:39:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 15, 2009, 7:39am UTC](https://forums.freertos.org/t/problem-with-lwip-mailbox-or-freertos-queue/113/1 "2009-06-15T07:39:45Z")

</div>

**[jesperv](https://sourceforge.net/u/jesperv/)** wrote on [Monday, June 15, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/0d1ed3a2/#68d6):

I’m implementing lwip version 1.3.0 on the newly added FreeRTOS port for AVR32 AP7000 but i’m having a problem with a mailbox in lwip or a queue in FreeRTOS.

I have a printf in netcom accept just after the creation of the newconn pointer and after the sys\_arch\_mbox\_fetch and they prints: "newconn 0x2af6" and "mbox fetch: 0x10013540 conn: 0x2af6 recmbox: 0xf01f0046"

In accept\_function i have a printf just before the data is added to the mailbox and looks like this: "mailbox: 0x10013540 conn 0x10002600 recvmbox: 0x100136b0"

Why isn’t the data properly sent through the mailbox or queue?

Here’s the code from lwip:

/\*\*  
\* Accept a new connection on a TCP listening netconn.  
\*  
\* @param conn the TCP listen netconn  
\* @return the newly accepted netconn or NULL on timeout  
\*/  
struct netconn \*  
netconn\_accept(struct netconn \*conn)  
{  
&nbsp; struct netconn \*newconn;

&nbsp; printf("newconn %p\n", newconn);

&nbsp; LWIP\_ERROR("netconn\_accept: invalid conn",&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (conn != NULL),&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NULL;);  
&nbsp; LWIP\_ERROR("netconn\_accept: invalid acceptmbox", (conn-\>acceptmbox != SYS\_MBOX\_NULL), return NULL;);

#if LWIP\_SO\_RCVTIMEO  
&nbsp; if (sys\_arch\_mbox\_fetch(conn-\>acceptmbox, (void \*)&newconn, conn-\>recv\_timeout) == SYS\_ARCH\_TIMEOUT) {  
&nbsp;&nbsp;&nbsp; newconn = NULL;  
&nbsp; } else  
#else

&nbsp; sys\_arch\_mbox\_fetch(conn-\>acceptmbox, (void \*)&newconn, 0);  
&nbsp; printf("mbox fetch: %p\ conn: %p recmbox: %p\n",conn-\>acceptmbox, newconn, newconn-\>recvmbox);  
#endif /\* LWIP\_SO\_RCVTIMEO\*/  
&nbsp; {  
&nbsp;&nbsp;&nbsp; /\* Register event with callback \*/  
&nbsp;&nbsp;&nbsp; API\_EVENT(conn, NETCONN\_EVT\_RCVMINUS, 0);

#if TCP\_LISTEN\_BACKLOG  
&nbsp;&nbsp;&nbsp; if (newconn != NULL) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; /\* Let the stack know that we have accepted the connection. \*/  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; struct api\_msg msg;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg.function = do\_recv;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg.msg.conn = conn;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TCPIP\_APIMSG(&msg);  
&nbsp;&nbsp;&nbsp; }  
#endif /\* TCP\_LISTEN\_BACKLOG \*/  
&nbsp; }  
&nbsp; return newconn;  
}

/\*\*  
\* Accept callback function for TCP netconns.  
\* Allocates a new netconn and posts that to conn-\>acceptmbox.  
\*  
\* @see tcp.h (struct tcp\_pcb\_listen.accept) for parameters and return value  
\*/  
static err\_t  
accept\_function(void \*arg, struct tcp\_pcb \*newpcb, err\_t err)  
{  
&nbsp; struct netconn \*newconn;  
&nbsp; struct netconn \*conn;

#if API\_MSG\_DEBUG  
#if TCP\_DEBUG  
&nbsp; tcp\_debug\_print\_state(newpcb-\>state);  
#endif /\* TCP\_DEBUG \*/  
#endif /\* API\_MSG\_DEBUG \*/  
&nbsp; conn = (struct netconn \*)arg;

&nbsp; LWIP\_ERROR("accept\_function: invalid conn-\>acceptmbox",  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; conn-\>acceptmbox != SYS\_MBOX\_NULL, return ERR\_VAL;);

&nbsp; /\* We have to set the callback here even though  
&nbsp;&nbsp; \* the new socket is unknown. conn-\>socket is marked as -1. \*/  
&nbsp; newconn = netconn\_alloc(conn-\>type, conn-\>callback);  
&nbsp; if (newconn == NULL) {  
&nbsp;&nbsp;&nbsp; return ERR\_MEM;  
&nbsp; }  
&nbsp; newconn-\>pcb.tcp = newpcb;  
&nbsp; setup\_tcp(newconn);  
&nbsp; newconn-\>err = err;  
&nbsp; /\* Register event with callback \*/  
&nbsp; API\_EVENT(conn, NETCONN\_EVT\_RCVPLUS, 0);

&nbsp; printf("mailbox: %p conn %p recvmbox: %p\n", conn-\>acceptmbox, newconn, newconn-\>recvmbox);  
&nbsp; if (sys\_mbox\_trypost(conn-\>acceptmbox, newconn) != ERR\_OK) {  
&nbsp;&nbsp;&nbsp; /\* When returning != ERR\_OK, the connection is aborted in tcp\_process(),  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; so do nothing here! \*/  
&nbsp;&nbsp;&nbsp; newconn-\>pcb.tcp = NULL;  
&nbsp;&nbsp;&nbsp; netconn\_free(newconn);  
&nbsp;&nbsp;&nbsp; return ERR\_MEM;  
&nbsp; }  
&nbsp; return ERR\_OK;  
}

/\*  
&nbsp; Blocks the thread until a message arrives in the mailbox, but does  
&nbsp; not block the thread longer than "timeout" milliseconds (similar to  
&nbsp; the sys\_arch\_sem\_wait() function). The "msg" argument is a result  
&nbsp; parameter that is set by the function (i.e., by doing "\*msg =  
&nbsp; ptr"). The "msg" parameter maybe NULL to indicate that the message  
&nbsp; should be dropped.

&nbsp; The return values are the same as for the sys\_arch\_sem\_wait() function:  
&nbsp; Number of milliseconds spent waiting or SYS\_ARCH\_TIMEOUT if there was a  
&nbsp; timeout.

&nbsp; Note that a function with a similar name, sys\_mbox\_fetch(), is  
&nbsp; implemented by lwIP.  
\*/  
u32\_t sys\_arch\_mbox\_fetch(sys\_mbox\_t mbox, void \*\*msg, u32\_t timeout)  
{  
void \*dummyptr;  
portTickType StartTime, EndTime, Elapsed;

&nbsp;&nbsp;&nbsp; StartTime = xTaskGetTickCount();

&nbsp;&nbsp;&nbsp; if( msg == NULL )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msg = &dummyptr;  
&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; if(&nbsp;&nbsp;&nbsp; timeout != 0 )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(pdTRUE == xQueueReceive( mbox, &(\*msg), timeout ) )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EndTime = xTaskGetTickCount();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Elapsed = EndTime - StartTime;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( Elapsed == 0 )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Elapsed = 1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ( Elapsed );  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else // timed out blocking for message  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \*msg = NULL;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return SYS\_ARCH\_TIMEOUT;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; else // block forever for a message.  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while( pdTRUE != xQueueReceive( mbox, &(\*msg), portMAX\_DELAY ) ) // time is arbitrary  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EndTime = xTaskGetTickCount();  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Elapsed = EndTime - StartTime;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if( Elapsed == 0 )  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Elapsed = 1;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ( Elapsed ); // return time blocked TBD test&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; }  
}

/\*-----------------------------------------------------------------------------------\*/  
//&nbsp;&nbsp; Try immediately posting the "msg" to the mailbox.  
err\_t  
sys\_mbox\_trypost(sys\_mbox\_t mbox, void \*data)  
{&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp; if ( xQueueSend( mbox, &data, ( portTickType ) 1 ) == pdPASS )  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ERR\_OK;  
&nbsp;&nbsp;&nbsp; }  
&nbsp;&nbsp;&nbsp; else  
&nbsp;&nbsp;&nbsp; {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return ERR\_MEM;  
&nbsp;&nbsp;&nbsp; }  
}

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 15, 2009, 1:11pm UTC](https://forums.freertos.org/t/problem-with-lwip-mailbox-or-freertos-queue/113/2 "2009-06-15T13:11:48Z")

</div>

**[jesperv](https://sourceforge.net/u/jesperv/)** wrote on [Monday, June 15, 2009](https://sourceforge.net/p/freertos/discussion/382005/thread/0d1ed3a2/#68d6/4a34):

Nevermind. I found the problem.  
When a new mailbox is created i had changed the size from sizeof( void \* )&nbsp; to a dynamic size. This is not a good idea when queues in FreeRTOS use void pointers.
