GAURAV KUMAR

Saturday, February 16, 2013

multicast in lwip using LM3S6965 micro-controller


Hi all,

recently I was working on sending multicast packets to multiple LM3S6965 micro-controllers. I was using lwip library and my experience with lwip is not much.
So, naturally I googled for some examples to get the start. But unfortunately I could not find any complete working example. So, I did a bit of trial and error.
After struggling a while, I was able to send and receive multicast packets. So, I thought I would write a working example for this. I did this using raw sockets and without RTOS.

A few things to do first:-
1. enable igmp in lwipopts.h file by adding this line :-
#define LWIP_IGMP 1
2. enable udp options and disable NETCONN and SOCKET options from lwipopts.h file
#define LWIP_UDP 1
#define LWIP_NETCONN 0
#define LWIP_SOCKET 0

3.if you are using DHCP options then set netif flags in dhcp.c file.
Replace line: netif->flags |= NETIF_FLAG_DHCP;
With line: netif->flags = NETIF_FLAG_DHCP | NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_LINK_UP | NETIF_FLAG_IGMP;

Now you are all set. Write the code to start receiving and send a sample packet.


void UDP_Multicast_init(void)
{
struct ip_addr ipgroup,test_ip;
struct udp_pcb *g_udppcb;
char msg[] = "gaurav";
struct pbuf* p;
p = pbuf_alloc(PBUF_TRANSPORT,sizeof(msg),PBUF_RAM);
memcpy (p->payload, msg, sizeof(msg));

IP4_ADDR(&ipgroup, 238, 0, 0, 3 ); //MultiCasting Ipaddress.
#if LWIP_IGMP
iret = igmp_joingroup(IP_ADDR_ANY,(struct ip_addr *)(&ipgroup));
UARTprintf("ret of igmp_joingroup: %d \n\r",iret);
#endif
g_udppcb =( struct udp_pcb*) udp_new();
udp_bind(g_udppcb, IP_ADDR_ANY, LOCAL_PORT); //to receive multicast
udp_recv(g_udppcb, NET_UDP_rec,NULL);// (void *)0); //NET_UDP_rec is the callback function that will be called every time you receive multicast
udp_sendto(g_udppcb,p,&ipgroup,LOCAL_PORT); //send a multicast packet
}

Note that you don't need udp_connect to send or receive multicast.

It worked for me. I use lwip 1.3.2

4 comments:

  1. Thanks, that just saved me reading a lot of code!
    Also I noticed the DNS code does use udp_connect with the comment that it makes receiving faster, so although it is not required it might be a good idea.

    ReplyDelete
  2. Hi,
    I have an already implemented uip server on TI microcontroller. I dont think uip server example supports post method. Please can you tell me how to implement this method?

    ReplyDelete
  3. thank you very much, i was wandering about this issue..

    ReplyDelete
  4. thanks sir, what will be the ip address client to use to connect to server then?? multicasting ip address or ?

    ReplyDelete