transmit UDP from RX callback connection?

stickben
Posts: 12
Joined: Tue Jun 23, 2015 12:28 pm

transmit UDP from RX callback connection?

Postby stickben » Fri Aug 14, 2015 9:09 pm

Hello,

I am trying to send a udp packet in response to a udp packet that I am receiving. Here is my code in the RX callback. I am not getting any errors, but I dont ever see the packets with my sniffer. Am I doing this right?

Code: Select all

void RxCallback(void *arg, char *data, unsigned short len)
{
   uint32 txlen;
   uint8 *pResponse = (uint8 *)os_malloc(512);
   struct espconn *pConn = (struct espconn *)arg;
 
   ets_uart_printf("\r\n\r\n\r\n************************************************************\r\nRXCallback got data length = %d\r\n", len);
   DebugData(data, len);
   ets_uart_printf("***** RX UDP Local port is %d local IP is %d.%d.%d.%d \r\n remote port is %d remote IP is %d.%d.%d.%d\r\n",
                                                                                       pConn->proto.udp->local_port,
                                                                              pConn->proto.udp->local_ip[0],
                                                                                                                       pConn->proto.udp->local_ip[1],
                                                                              pConn->proto.udp->local_ip[2],
                                                                              pConn->proto.udp->local_ip[3],
                                                                                pConn->proto.udp->remote_port,
                                                                                pConn->proto.udp->remote_ip[0],
                                                                                                                             pConn->proto.udp->remote_ip[1],
                                                                               pConn->proto.udp->remote_ip[2],
                                                                               pConn->proto.udp->remote_ip[3]);
   txlen = BuildAnswer(data, pResponse);
   ets_uart_printf("Response length = %d\r\n", txlen);
   PrintHeader(pResponse);
   DebugData(pResponse, txlen);

   if(espconn_send(pConn, pResponse, txlen) != 0)
   {
      ets_uart_printf(" UDP fail send\r\n");

   }
   os_free(pResponse);
   }

User avatar
kolban
Posts: 131
Joined: Tue Jun 16, 2015 1:09 pm
Location: Fort Worth, Texas, USA

Re: transmit UDP from RX callback connection?

Postby kolban » Fri Aug 14, 2015 9:49 pm

First thing I notice in your code is that you malloc() a buffer for response, populate that buffer and then execute a send to transmit the data ... that may be just fine ... I don't know. However, the thing that might be a problems is that immediately after sending the data, you free() the buffer. What is important to note that everything here is Asynch. That means that when you call send, it does NOT block until the send completes. Instead, think of it as having queued a request to send sometime in the future. As such, the buffer you passed in can not be deleted until after the send completes as indicated by the send callback function. That *may* be part of the problem.

stickben
Posts: 12
Joined: Tue Jun 23, 2015 12:28 pm

Re: transmit UDP from RX callback connection?

Postby stickben » Sat Aug 15, 2015 3:27 am

Ya I thought of that, I removed the free and no change.

eriksl
Posts: 159
Joined: Fri May 22, 2015 6:22 pm

Re: transmit UDP from RX callback connection?

Postby eriksl » Sat Aug 15, 2015 3:14 pm

Don't use malloc/free (not in this way anyway), your heap will get fragmented in no time -> out of memory -> crash. Just define a static struct or array (the static is essential, otherwise the stack will grow over the heap at some time), inside the function.

Globally define a static bool that keeps track if the buffer has been sent already. If not, don't try to send a new one. Define a callback function for packet sent and clear the flag there.

Also, and that's I think common error #1, don't use endless loops anywhere. Every function that is called from the SDK code should return fairly quickly. If you work isn't done at that point, just return and use posts to have your background work function called frequently. This is NO pre-emptive multitasking, if you don't return, the lwip doesn't get a chance to do it's work.

Who is online

Users browsing this forum: No registered users and 28 guests