How to create chain of pbuf's to be sent? Send data using UDP.

cancel
Showing results for 
Search instead for 
Did you mean: 

How to create chain of pbuf's to be sent? Send data using UDP.

23,865 Views
profprogrammer
Contributor III

How to creat and initsializtion this struct for send data using UDP

struct pbuf *p;

 

I using this code.   

 

struct netif fsl_netif0;  struct udp_pcb * pcb;  ip_addr_t fsl_netif0_ipaddr, fsl_netif0_netmask, fsl_netif0_gw;     #define PORT_M 9000    void eth_lwip_init () {        lwip_init();       IP4_ADDR(&fsl_netif0_ipaddr, 192,168,70,110);    IP4_ADDR(&fsl_netif0_netmask, 255,255,255,0);    IP4_ADDR(&fsl_netif0_gw, 192,168,70,1);    netif_add(&fsl_netif0, &fsl_netif0_ipaddr, &fsl_netif0_netmask, &fsl_netif0_gw, NULL, ethernetif_init, ethernet_input);       netif_set_default(&fsl_netif0);       {     uint32_t dt = 0;     ENET->MSCR = 0x26<<1;     PHY_DRV_Read (0,0,0x1f, &dt);     dt |= (1<<7);     PHY_DRV_Write (0, 0, 0x1f, dt );     OSA_TimeDelay(1000);    }           // get new pcb      pcb = udp_new();      if (pcb == NULL) {          LWIP_DEBUGF(UDP_DEBUG, ("udp_new failed!\n"));          return;      }          //bind to any IP address on port  //IP_ADDR_ANY      if (udp_bind(pcb, &fsl_netif0_ipaddr, PORT_M) != ERR_OK) {          LWIP_DEBUGF(UDP_DEBUG, ("udp_bind failed!\n"));          return;      }         netif_set_up(&fsl_netif0);      // set udp_echo_recv() as callback function      // for received packets      udp_recv(pcb, udp_echo_recv, NULL);           ///  test message      {   uint8_t i;      err_t err_if;         struct pbuf *p;         struct udp_hdr* u_hdr;        // extern struct udp_pcb *pcb;         uint8_t data[8] = {0x55, 0x44, 0x33, 0x22, 0x11, 0x0, 0x0, 0x0 };            p = pbuf_alloc(PBUF_TRANSPORT, 8, PBUF_RAM);            u_hdr = (struct udp_hdr *)p->payload;         u_hdr->src=(uint16_t) fsl_netif0_ipaddr;         u_hdr->dest = PORT_M;         u_hdr->len = sizeof(struct udp_hdr) + sizeof(data);            for (i=0; i<sizeof(data); i++) {      ((char*)u_hdr)[sizeof(struct udp_hdr) + i] = (char)data[i];         }            u_hdr->chksum = inet_chksum( &u_hdr, 8UL);         err_if = udp_send ( pcb, p );      if (err_if != ERR_OK) {      printf ("Error udp_sendto = %d\n", err_if);      }        pbuf_free (p);      }  }

 

How to send data fo udp packets?

Labels (1)
0 Kudos
1 Reply

538 Views
isaacavila
NXP Employee
NXP Employee

Hello,

If you look at KSDK 1.3 examples, you will find the lwip_udpecho_demo_freertos_frdmk64f project that echoes udp data.

In this example, you will notice that netconn_send() function is used to send the data over UDP:

/**

* Send data over a UDP or RAW netconn (that is already connected).

*

* @param conn the UDP or RAW netconn over which to send data

* @param buf a netbuf containing the data to send

* @return ERR_OK if data was sent, any other err_t on error

*/

err_t

netconn_send(struct netconn *conn, struct netbuf *buf)

{

  struct api_msg msg;

  err_t err;

  LWIP_ERROR("netconn_send: invalid conn",  (conn != NULL), return ERR_ARG;);

  LWIP_DEBUGF(API_LIB_DEBUG, ("netconn_send: sending %"U16_F" bytes\n", buf->p->tot_len));

  msg.function = do_send;

  msg.msg.conn = conn;

  msg.msg.msg.b = buf;

  err = TCPIP_APIMSG(&msg);

  NETCONN_SET_SAFE_ERR(conn, err);

  return err;

}

You only need to use conn pointer as NETCONN_UDP:

static void udpecho_thread(void *arg)

{

    static struct netconn *conn;

    static struct netbuf *buf;

    char buffer[100];

    err_t err;

    LWIP_UNUSED_ARG(arg);

    netif_set_up(&fsl_netif0);

    conn = netconn_new(NETCONN_UDP);

    LWIP_ASSERT("con != NULL", conn != NULL);

    netconn_bind(conn, NULL, 7);

    while (1)

    {

        err = netconn_recv(conn, &buf);

        if (err == ERR_OK)

        {

            if(netbuf_copy(buf, buffer, buf->p->tot_len) != buf->p->tot_len)

            {

                LWIP_DEBUGF(UDPECHO_DBG, ("netbuf_copy failed\r\n"));

            }

            else

            {

                buffer[buf->p->tot_len] = '\0';

                err = netconn_send(conn, buf);

                if(err != ERR_OK)

                {

                LWIP_DEBUGF(UDPECHO_DBG, ("netconn_send failed: %d\r\n", (int)err));

                }

                else

                {

                    LWIP_DEBUGF(UDPECHO_DBG, ("got %s\r\n", buffer));

                }

            }

            netbuf_delete(buf);

        }

    }

}

You can also look for udpecho_thread to see how connection is established, receiving and sending data over UDP.

I hope this can help you!

Best Reards,

Isaac

----------------------------------------------------------------------------------------------------------------------------------------

Note: If this post answers your question, please click the Correct Answer button. Thank you!

----------------------------------------------------------------------------------------------------------------------------------------

0 Kudos