EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Revision:
5:dd63a1e02b1b
Parent:
2:a4f97773c90f
Child:
9:c79fa4034f5b
--- a/if/lwip/lwipNetUdpSocket.cpp	Fri Jul 09 14:46:47 2010 +0000
+++ b/if/lwip/lwipNetUdpSocket.cpp	Tue Jul 27 15:59:42 2010 +0000
@@ -23,6 +23,7 @@
 
 #include "lwipNetUdpSocket.h"
 #include "lwip/udp.h"
+#include "lwip/igmp.h"
 
 //#define __DEBUG
 #include "dbg/dbg.h"
@@ -30,9 +31,9 @@
 #include "netCfg.h"
 #if NET_LWIP_STACK
 
-LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
+LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt(), m_multicastGroup() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
 {
-  DBG("\r\nNew LwipNetUdpSocket %p (pPCb=%p)\r\n", (void*)this, (void*) pPcb);
+  DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb);
   if(!m_pPcb)
     m_pPcb = udp_new();
   if(m_pPcb)
@@ -49,10 +50,23 @@
   
 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
 {
+  err_t err;
+  
   if(!m_pPcb)
     return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
+   
+  #if LWIP_IGMP //Multicast support enabled
+  if(me.getIp().isMulticast())
+  {
+    DBG("This is a multicast addr, joining multicast group\n");
+    m_multicastGroup = me.getIp();
+    err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
+    if(err)
+      return NETUDPSOCKET_IF; //Could not find or create group
+  }
+  #endif
 
-  err_t err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
+  err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
   if(err)
     return NETUDPSOCKET_INUSE;
 
@@ -85,7 +99,7 @@
   pbuf_free( p );
   if(err)
     return NETUDPSOCKET_SETUP; //Connection problem
-  DBG("\r\n%d bytes sent in UDP Socket.\r\n", len);
+  DBG("%d bytes sent in UDP Socket.\n", len);
   return len;
 }
 
@@ -157,7 +171,7 @@
 
 NetUdpSocketErr LwipNetUdpSocket::close()
 {
-  DBG("\r\nLwipNetUdpSocket::close() : Closing...\r\n");
+  DBG("LwipNetUdpSocket::close() : Closing...\n");
 
   if(m_closed)
     return NETUDPSOCKET_OK; //Already being closed
@@ -166,12 +180,13 @@
   if( !m_pPcb ) //Pcb doesn't exist (anymore)
     return NETUDPSOCKET_MEM;
     
-  DBG("\r\nLwipNetUdpSocket::close() : Cleanup...\r\n");
+  DBG("LwipNetUdpSocket::close() : Cleanup...\n");
     
   //Cleanup incoming data
   cleanUp();
- 
-  DBG("\r\nLwipNetUdpSocket::close() : removing m_pPcb...\r\n");
+  
+
+  DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n");
   udp_remove( (udp_pcb*) m_pPcb);
     
   m_pPcb = NULL;
@@ -188,7 +203,7 @@
 
 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
 {
-  DBG("\r\n Packet of length %d arrived in UDP Socket.\r\n", p->tot_len);
+  DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len);
   list<InPacket>::iterator it;
   for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
   {
@@ -219,6 +234,15 @@
     udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
   }
   
+  //Leaving multicast group(Ok because LwIP has a refscount for multicast group)
+  #if LWIP_IGMP //Multicast support enabled
+  if(m_multicastGroup.isMulticast())
+  {
+    igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
+    m_multicastGroup = IpAddr();
+  }
+  #endif
+  
   list<InPacket>::iterator it;
   for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
   {