UPGRADE YOUR BROWSER

We have detected your current browser version is not the latest one. Xilinx.com uses the latest web technologies to bring you the best online experience possible. Please upgrade to a Xilinx.com supported browser:Chrome, Firefox, Internet Explorer 11, Safari. Thank you!

cancel
Showing results for 
Search instead for 
Did you mean: 
Scholar golson
Scholar
5,854 Views
Registered: ‎04-07-2008

Are Multiple UDP ports possible in uecho or echo test (XAPP1026)

Here is some code from the echo.c file

 

The function of the application loop is to receive packets constantly (xemacif_input), then

pass them on to lwIP. Before entering this loop, the echo server sets up certain callbacks:

/* create new TCP PCB structure */

pcb = tcp_new();

/* bind to specified @port */

err = tcp_bind(pcb, IP_ADDR_ANY, port);

/* we do not need any arguments to callback functions */

tcp_arg(pcb, NULL);

/* listen for connections */

pcb = tcp_listen(pcb);

/* specify callback to use for incoming connections */

tcp_accept(pcb, accept_callback);

 

 

I have a requirement to listen for 3 ports instead of just one.  I am trying to figure out if it would be possible to do in lwip.

 

The above code binds to one port, does anybody know if I can change the code to listen for one of three ports instead of just one?

 

Thank You,

  Gary Olson

 

0 Kudos
5 Replies
Scholar golson
Scholar
5,834 Views
Registered: ‎04-07-2008

Re: Are Multiple UDP ports possible in uecho or echo test (XAPP1026)

I asked this question to the lwip users group and got a answer from them.  They have a mailing list that you can subscribe to and also ask members questions.  Their info is below:

Send lwip-users mailing list submissions to

      lwip-users@nongnu.org

 

To subscribe or unsubscribe via the World Wide Web, visit

      http://lists.nongnu.org/mailman/listinfo/lwip-users

 

Thanks to Kieran  who showed me what to do.  The functions use pointers so in the function above there is one pcb pointer.  I need to make it an array of 3 pointers so that it can bind

to three ports and call the function if the data from any of these ports comes to the network interface.  Here is my changes that made the uecho.c file handle 3 ports.

 

 

void recv_callback(void *arg, struct udp_pcb *upcb,

struct pbuf *p, struct ip_addr *addr, u16_t port)

{

   int n;

   int portnum;

 

   n = p->tot_len;

   xil_printf(" tot_len = %d \n\r", n);

 

   portnum = upcb->remote_port; 

   xil_printf(" remote_port = %d \n\r", portnum);

   portnum = upcb->local_port;

   xil_printf(" local_port = %d \n\r", portnum);

 

   udp_sendto(upcb, p, addr, port);

   pbuf_free(p);

}

 

int start_application()

{

   int i;

   //struct udp_pcb *pcb; have to redefine here

   struct udp_pcb *pcb[3];

 

   err_t err;

   unsigned port[3] = { 1025, 2025, 3025 };

   for( i = 0; i < 3; i++ )

   {

      /* create new UDP PCB structure */

      pcb[i] = udp_new();

      if (!pcb[i])

      {

         xil_printf("Error creating PCB. Out of Memory\n\r");

         return -1;

      }

      /* bind to echo @port */

      err = udp_bind(pcb[i], IP_ADDR_ANY, port[i]);

      if (err != ERR_OK)

      {

          xil_printf("Unable to bind to port %d: err = %d\n\r", port[i], err);

          return -2;

      }

      udp_recv(pcb[i], recv_callback, NULL);

   } // end of the for loop

   xil_printf( "UDP echo server started @ port %d %d %d \n\r", port[0], port[1], port[2] );

   return 0;

}

 

I am also showing how you can access the port info of the packet.  the local port has the value of the port the packet is addressed to.  I will need to access this port information to send

the packet to the write output circuit of my design.

 

I am also going to submit the TCP changes next.

 

Thank You,

  Gary Olson

 

Message Edited by golson on 08-15-2008 06:56 AM
0 Kudos
Scholar golson
Scholar
5,823 Views
Registered: ‎04-07-2008

Re: Are Multiple UDP ports possible in uecho or echo test (XAPP1026)

Finally I tested TCP echo and made the following changes to the two functions:

 

Slight change in callback to verify that I can find the port number in the received data.

 

 

 

err_t recv_callback(void *arg, struct tcp_pcb *tpcb,
                               struct pbuf *p, err_t err)
{
   int portnum;
   /* do not read the packet if we are not in ESTABLISHED state */
   #if 0
   if (tpcb->state >= 5 && tpcb->state <= 8)

   {
      tcp_close(tpcb);
      if (p)
         pbuf_free(p);
         //xil_printf("Connection (%d) closed\n\r", (int)(arg));
         return;
    }

    else if (tpcb->state > 8)
        return;
   #else
       if (!p)

      {
          tcp_close(tpcb);
          tcp_recv(tpcb, NULL);
          return ERR_OK;
      }
   #endif

   /* indicate that the packet has been received */
   tcp_recved(tpcb, p->len);
   portnum = tpcb->local_port;
   xil_printf(" portnum = %d \n\r", portnum);

   /* echo back the payload */
   /* in this case, we assume that the payload is < TCP_SND_BUF */
   if (tcp_sndbuf(tpcb) > p->len)

   {
      err = tcp_write(tpcb, p->payload, p->len, 1);
   }

   else

      print("no space in tcp_sndbuf\n\r");

 

   /* free the received pbuf */
   pbuf_free(p);

   return ERR_OK;
}

 

 

 


int start_application()
{
   int i;
   //struct tcp_pcb *pcb;
   struct tcp_pcb *pcb[3];
   err_t err;
   unsigned port[3] = {1025, 2025, 3025 };


   /* create new TCP PCB structure */
   for ( i = 0; i < 3; i ++ )
   {
      pcb[i] = tcp_new();
      if (!pcb[i])
      {
         xil_printf("Error creating PCB. Out of Memory\n\r");
         return -1;
      }
 
      /* bind to specified @port */
      err = tcp_bind(pcb[i], IP_ADDR_ANY, port[i]);
      if (err != ERR_OK)
      {
         xil_printf("Unable to bind to port %d: err = %d\n\r", port, err);
         return -2;
      }
      /* we do not need any arguments to callback functions */
      tcp_arg(pcb[i], NULL);
      /* listen for connections */
      pcb[i] = tcp_listen(pcb[i]);
      if (!pcb[i])
      {
         xil_printf("Out of memory while tcp_listen\n\r");
         return -3;
      }
      /* specify callback to use for incoming connections */
      tcp_accept(pcb[i], accept_callback);
   }  // end of for loop
   xil_printf( "tcp echo server started @ port %d  %d  %d \n\r", port[0], port[1], port[2] );

   return 0;
}

 

 

 

Message Edited by golson on 08-15-2008 10:23 AM
0 Kudos
Explorer
Explorer
1,914 Views
Registered: ‎03-09-2017

Re: Are Multiple UDP ports possible in uecho or echo test (XAPP1026)

I tried it by giving base addresses of two ethernet ports in my platform config file but only 1st udp port is receiving data and echoing back .... do i need to have free rtos running? or what changes have to be done in main file of this... at xemac netif ?

0 Kudos
Explorer
Explorer
1,870 Views
Registered: ‎03-09-2017

Re: Are Multiple UDP ports possible in uecho or echo test (XAPP1026)

Can you share the main.c file for this multiple port udp?

0 Kudos
Highlighted
Scholar golson
Scholar
1,856 Views
Registered: ‎04-07-2008

Re: Are Multiple UDP ports possible in uecho or echo test (XAPP1026)

OK,  I found this.

0 Kudos