UDP protocol

02 Nov 2009 . Edited: 02 Nov 2009

Question : I need to use the UDP protocol . Is it possible with the mbed?
 
There is an implementation of lwip for mbed, which some classes have been built on that use TCP/IP. However, there are no classes using UDP.
 
The short answer is yes, UDP is possible. If you want to build on lwip there is an example of the TCP/IP implementation as a guide :
 
http://mbed.org/projects/cookbook/wiki/EMAC/purehttpc
 
There are also some lwip examples of UDP in use :
 
http://cvs.savannah.gnu.org/viewvc/contrib/apps/netbios/?root=lwip

04 Nov 2009

 

pierre doussay wrote:

Hi Rolf

httpclient has the some behaviour for me, it works then fails after a while

Today , I tried to UDP functions and lwip API functions

For UDP, I have to import "http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk/Core" .

But for API... I saw api.h and api_msg.h, but cpp can't  be compiled

Here is a doc "http://www.sics.se/~adam/lwip/doc/lwip.pdf" where you can see on page 29

int main() {
    struct netconn *conn;
    struct netbuf *buf;
    struct ip_addr addr;
    char *data;
    char text[] = "A static text";
    int i;
    
    /* create a new connection */
    conn = netconn_new(NETCONN_UDP);
    
    /* set up the IP address of the remote host */
    addr.addr = htonl(0x0a000001);
    
    /* connect the connection to the remote host */
    netconn_connect(conn, &addr, 7000);
    
    /* create a new netbuf */
    buf = netbuf_new();
    data = netbuf_alloc(buf, 10);
    
    /* create some arbitrary data */
    for (i = 0; i < 10; i++) {
        data[i] = i;
    }
    /* send the arbitrary data */
    netconn_send(conn, buf);
    
    /* reference the text into the netbuf */
    netbuf_ref(buf, text, sizeof(text));
    /* send the text */
    
    netconn_send(conn, buf);
    /* deallocate connection and netbuf */
    
    netconn_delete(conn);
    netconn_delete(buf);
}

If you had a new core, can you check that

Regards

 

Hi Pierre,

I answer you here because it is not HTTPServer related. And this topic might fit better.

If you want to use UDP with the lwIP stack you have to import the Core, that’s right.

If you have a look around the in the lwIP library you will see that there is no API.h provided. Like Adam Dunkels said, in his description on pages 15-16, chapter 11, to use the application program interface (API) the system has to provide a multi thread environment. What mbed does not provide by itself. So I decided the easiest way to provide HTTP connectivity is to use the internal TCP API.

If you want to proceed in the same manner, you can use the functions provided by udp.h. The netbios example posted by Chris uses this API so it might be a good example.

By the way, what is your project about?

 

Cheers

Rolf

02 Mar 2010

Has anyone put together some sample code of UDP on mBed? I have been digging through the Lwip library code and netbios example from the contributions repo to see how the udp methods and callbacks work but am unsure what needs to be running in the main while() loop to listen for incoming UDP packets.

I'll be working on something with this in the next few days but am still confused on how to use Lwip with UDP only (no TCP) but also with DHCP if possible.

Some hints would be great!

05 Mar 2010

I got some basic UDP send/receive going and posted some scratch notes and code snippets at http://mbed.org/users/pehrhovey/notebook/udp-on-mbed/

Basically the missing part is that you use the NetServer class which sets up the net interface, does DHCP, etc. Then you poll the server in the main loop which will execute the receive callback when a packet is found.

13 Mar 2010

Pehr,

Did you ever receive any help on getting UDP running on the mbed? I am trying to do the same, unsuccessfully. I have includes in my file like:

#include "mbed.h"
#include "lwipopts.h"
#include "lwip/opt.h"
#include "lwip/stats.h"
#include "lwip/sys.h"

then some initialization code I found in the Trunk path, but the compiler cannot resolve the includes. Do you have any ideas here or have a code sample I could look at or start from?

This online compiler does not let you set the include path so I don't know how to get to the includes.

Thanks,

Russ

14 Mar 2010

Hey Russ,

I ended up figuring it out by myself, the trick i needed was to use the NetServer class in the lwip library to handle the network interface / DHCP stuff. See my post above for a link to a notebook with some code snippets.

As for your includes, make sure you imported the lwip 'core' folder. I import the code itself, not the precompiled library so that I can see all the files and make sure things are enabled in lwipopts.

I published a hacked together UDP testing program at http://mbed.org/users/pehrhovey/programs/UDPtest/60b1u/

Its not quality code but it demonstrates some basic UDP send/receive. I think this project also imported the HTTP server / client but I am not using those, just plain UDP.

19 Mar 2010

Pehr,

I looked over your code. It looks pretty simple. I will try to mimic this. Were you using 2 mbed's to run this or an mbed and a PC?

I had managed to get the lwip compiled over the weekend by importing the lwip core folder as you mention above. I then tried to turn on ICMP to enable "PING" in the lwip core but could not get the mbed to respond to the PC on the network. Do you know how to enable the mbed lwip facilities to allow it to respond to pings?

Thanks,

Russ