cancel
Showing results for 
Search instead for 
Did you mean: 

LWIP STM32F7 UDP Rx Callback Data is not equal to Data Length

Simon477
Associate

Hello together, 
I'm using a STM32F769-DISCO Board with STM Cube IDE.

LWIP is activated.
No DHCP, static IP.
Most of the LWIP is default only disabled the DHCP and TCP parts.

First, sending UDP packages is working fine. 

My Issue is:
that the receiving udp callback function delivers a udp data length of 22 (p->len = 22). But I only receive 2 Bytes of data in the Rx-buffer (p->payload). The 2 Bytes are filled correctly with data. The rest of the data array is 0.  

 

Does someone have any advise regarding LWIP UDP receiving?

I tried allocating (pbuf_alloc) the buffer and using pbuf_take() , but I did not see any difference. 
I think there could be some issues with the buffer address, but I'm honestly at a loss here...

 

Here the udp init and the receive callback function in "lwip.c":

 

 

 

uint8_t testbuffer[100] = { 0 };

err_t udpServer_init(void)
{
    err_t err = ERR_OK;
    ip4_addr_t destIPAddr;
    ip4_addr_t myIPAddr;

    upcb = udp_new();
    IP4_ADDR(&myIPAddr, UDP_SERVER_IP_1, UDP_SERVER_IP_2, UDP_SERVER_IP_3, UDP_SERVER_IP_4); //stm ip
    err = udp_bind(upcb, &myIPAddr, 52605);

    if(upcb == NULL)
    {
	return ERR_MEM;
    }
    // Load the static IP of the destination address
    IP4_ADDR(&destIPAddr, UDP_CLIENT_IP_1, UDP_CLIENT_IP_2, UDP_CLIENT_IP_3, UDP_CLIENT_IP_4);

    // Connect to the other port
    err = udp_connect(upcb, &destIPAddr, UDP_CLIENT_PORT);

    if(err == ERR_OK)
    {
	//send msg to server
	ZKW_eFLAT_query_status();
	// Set the receive function
	udp_recv(upcb, udp_receive_callback, NULL);
    }
    else
    {
	udp_remove(upcb);
    }
    return err;
}

void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port)
{
    uint32_t len = 0;
    uint8_t idata[100] = { 0 };

    len = p->len;

    if(len == 22)
    {
	strncpy(testbuffer, (char*) p->payload, p->len);

	/* allocate pbuf from pool*/
	if(p != NULL)
	{
	    /* copy data to pbuf */
	    for(uint8_t i = 0; i <= 100; i++)
	    {
		idata[i] = testbuffer[i];
	    }
	    /* free pbuf */
	    pbuf_free(p);
	}
    }
}

 

 

 

Thank you very much

1 ACCEPTED SOLUTION

Accepted Solutions
STea
ST Employee

Hello @Simon477 ,

Make sure that the buffer size is sufficient to hold the incoming data. If the buffer is too small, it might not be able to store the entire packet.
Here is an example of the receive callback:

void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
    struct pbuf *q;
    uint8_t buffer[256]; // Adjust the size as needed
    uint16_t len = 0;

    // Iterate through the pbuf chain
    for (q = p; q != NULL; q = q->next) {
        memcpy(buffer + len, q->payload, q->len);
        len += q->len;
    }

    // Process the received data
    process_received_data(buffer, len);

    // Free the pbuf
    pbuf_free(p);
}

also make sure that the generated packets you are trying to send don't have a minimum size and this might cause the extra zeroes received check for this using Wireshark maybe. 
Regards 

 

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

2 REPLIES 2
STea
ST Employee

Hello @Simon477 ,

Make sure that the buffer size is sufficient to hold the incoming data. If the buffer is too small, it might not be able to store the entire packet.
Here is an example of the receive callback:

void udp_receive_callback(void *arg, struct udp_pcb *upcb, struct pbuf *p, const ip_addr_t *addr, u16_t port) {
    struct pbuf *q;
    uint8_t buffer[256]; // Adjust the size as needed
    uint16_t len = 0;

    // Iterate through the pbuf chain
    for (q = p; q != NULL; q = q->next) {
        memcpy(buffer + len, q->payload, q->len);
        len += q->len;
    }

    // Process the received data
    process_received_data(buffer, len);

    // Free the pbuf
    pbuf_free(p);
}

also make sure that the generated packets you are trying to send don't have a minimum size and this might cause the extra zeroes received check for this using Wireshark maybe. 
Regards 

 

In order to give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Simon477
Associate

Thank you,
the main issue was that the strncpy function did not work. memcpy was the solution in combination with p->payload.
BR