UDP on mBed

This page will have code snippets and links to more information on using UDP (via the LWIP library) on mBed. I am investigating this while making an Open Sound Control implementation for mBed.

http://en.wikipedia.org/wiki/User_Datagram_Protocol

I import the 'core' folder of LWIP instead of the precompiled version so I can modify settings, options, debug flags, etc:

http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk/Core

 

If not using TCP or needing HTTP server you should use the NetServer example class provided in the cookbook which handles DHCP and initialization of the network interface (netif).

After the NetServer is set up you need to create the UDP PCB interface and bind it to the port it will listen on. You can specify what IP to listen from or bind to any using IP_ADDR_ANY.

Next you attach the callback to be run when a packet is received.

In the While() loop you must poll the NetServer as fast as possible so it can process any received packets. If you do not poll the server you will never receive packets (your callbacks will not be called).

In addition to sending packets to a specific IP, you can broadcast to any IP using  IP_ADDR_BROADCAST which is 255.255.255.255

 

 

Some code snippets (incomplete and without all includes):

 

 

/* Callback to echo the packet that was received, on a different port) */

static void udp_test_recv(void *arg, struct udp_pcb *upcb, struct pbuf *p, struct ip_addr *addr, u16_t port)
{
printf("Received UDP Packet on port %d\r\n",port);

LWIP_UNUSED_ARG(arg);
got_udp = 1;
/* if packet is valid */
if (p != NULL) {
//Try to print out what we received
printf("UDP Packet Received! Payload:\r\n");
printf("-- %s --\r\n",static_cast<char *>(p->payload));

err_t code = udp_sendto(upcb, p, IP_ADDR_BROADCAST, 5555); //send it back to port 5555

printf("Echo'd packet, result code is %d\r\n",code);



/* free the pbuf */
pbuf_free(p);
got_udp=0;
sent_udp=0;
}
}

 

int main() {
/*Initialize NetServer which gets us our DHCP address and
gets the network interface ready
*/
NetServer *net = NetServer::ready();


//Initialize UDP
struct udp_pcb *pcb;
pcb = udp_new();

struct pbuf *p;
char msg[]="testing mBedUDP";

if (pcb != NULL) {
/* we have to be allowed to send broadcast packets! */
pcb->so_options |= SOF_BROADCAST;
udp_bind(pcb, IP_ADDR_ANY, 4444); //Receive from any IP address, on the specified port
udp_recv(pcb,udp_test_recv, NULL);
}else{
printf("Could not make UDP pcb\r\n");
}


net->waitUntilReady(); //this will print IP


//Send a startup packet
printf("UDP Tester started...\r\n");

while(1) {
net->poll();

if(button){
printf("Button pressed, toggled send_test to = %d\r\n",send_test);
send_test = !send_test;
}

if(send_test){
//broadcast something
p = pbuf_alloc(PBUF_TRANSPORT,sizeof(msg),PBUF_RAM);
memcpy (p->payload, msg, sizeof(msg));
err_t res = udp_sendto(pcb, p, IP_ADDR_BROADCAST, 5555); //broadcast test msg
pbuf_free(p); //De-allocate packet buffer
printf("Sent test message to port 1234, result is %d Waiting...\r\n",res);
wait(2);

}
}
}


0 comments

You need to log in to post a comment