Ethernet Interface Networking Library

Dependencies:   LwIPNetworking lwip-eth

Dependents:   CPPSocketsTest

Fork of EthernetNetworkLib by Donatien Garnier

Files at this revision

API Documentation at this revision

Comitter:
donatien
Date:
Wed Jun 20 13:19:13 2012 +0000
Parent:
6:8acfdd9f43f1
Child:
9:beabe4e574be
Commit message:
Blocking connect() & disconnect() functions, recovery of interface state & IP address

Changed in this revision

LwIPNetworking.lib Show annotated file Show diff for this revision Revisions of this file
lwip-eth.lib Show annotated file Show diff for this revision Revisions of this file
main/if/EthernetInterface.cpp Show annotated file Show diff for this revision Revisions of this file
main/if/EthernetInterface.h Show annotated file Show diff for this revision Revisions of this file
main/lwipopts.h Show annotated file Show diff for this revision Revisions of this file
--- a/LwIPNetworking.lib	Fri Jun 15 14:23:03 2012 +0000
+++ b/LwIPNetworking.lib	Wed Jun 20 13:19:13 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/donatien/code/LwIPNetworking/#387f573a6813
+http://mbed.org/users/donatien/code/LwIPNetworking/#9952709b2ee3
--- a/lwip-eth.lib	Fri Jun 15 14:23:03 2012 +0000
+++ b/lwip-eth.lib	Wed Jun 20 13:19:13 2012 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/emilmont/code/lwip-eth/#43ddf192c635
+http://mbed.org/users/emilmont/code/lwip-eth/#0211aeb1b708
--- a/main/if/EthernetInterface.cpp	Fri Jun 15 14:23:03 2012 +0000
+++ b/main/if/EthernetInterface.cpp	Wed Jun 20 13:19:13 2012 +0000
@@ -41,9 +41,10 @@
 #include "mbed.h"
 
 
-EthernetInterface::EthernetInterface() : LwIPInterface(), m_supervisor(&EthernetInterface::phySupervisorCb, osTimerPeriodic, this), m_lpcNetif(), m_useDHCP(false)
+EthernetInterface::EthernetInterface() : LwIPInterface(), m_lpcNetif(), m_netifStatusSphre(1), m_useDHCP(false)
 {
-
+  s_lpcNetifOff = (int32_t)( ((char*)this) - ((char*)&m_lpcNetif) );
+  m_netifStatusSphre.wait(0);
 }
 
 int EthernetInterface::init() //With DHCP
@@ -58,6 +59,8 @@
   memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif));
   netif_add(&m_lpcNetif, NULL, NULL, NULL, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/);
   netif_set_default(&m_lpcNetif);
+  
+  netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb);
 
   return OK;
 }
@@ -70,7 +73,7 @@
   m_useDHCP = false;
 
   DBG("Initializing LwIP");
-  LwIPInterface::init(); //Init LwIP, NOT including PPP
+  LwIPInterface::init(); //Init LwIP
 
   DBG("Static IP assignment");
   inet_aton(ip, &ip_n);
@@ -82,14 +85,15 @@
   memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif));
   netif_add(&m_lpcNetif, &ip_n, &mask_n, &gateway_n, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/);
   netif_set_default(&m_lpcNetif);
+  
+  netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb);
 
   return OK;
 }
 
 int EthernetInterface::connect()
 {
-  m_supervisor.start(250);
-  netif_set_up(&m_lpcNetif);
+  m_netifStatusSphre.wait(0);
 
   DBG("Enable MAC interrupts");
   NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01));
@@ -97,46 +101,49 @@
 
   if(m_useDHCP)
   {
-    dhcp_start(&m_lpcNetif);
+    dhcp_start(&m_lpcNetif); //The DHCP client will set the interface up once the IP/Netmask/Getway/DNS Servers are recovered
   }
+  else
+  {
+    netif_set_up(&m_lpcNetif); //Set interface up
+  }
+  
+  m_netifStatusSphre.wait();
+  
+  DBG("Connected with IP %s", getIPAddress());
 
   return OK;
 }
 
 int EthernetInterface::disconnect()
 {
+  m_netifStatusSphre.wait(0);
+
   if(m_useDHCP)
   {
+    dhcp_release(&m_lpcNetif); //Release the lease & bring interface down
     dhcp_stop(&m_lpcNetif);
   }
-
-  netif_set_down(&m_lpcNetif);
+  else
+  {
+    netif_set_down(&m_lpcNetif);  
+  }
 
   DBG("Disable MAC interrupts");
   NVIC_DisableIRQ(ENET_IRQn);
-
-  m_supervisor.stop();
+  
+  m_netifStatusSphre.wait();
 
   return OK;
 }
 
-/*static*/ void EthernetInterface::phySupervisorCb(void const* ctx)
+/*static*/ void EthernetInterface::netifStatusCb(struct netif *netif)
 {
-  EthernetInterface* pIf = (EthernetInterface*) ctx;
-  /* Call the PHY status update state machine once in a while
-   to keep the link status up-to-date */
-  if (lpc_phy_sts_sm(&pIf->m_lpcNetif) != 0)
-  {
-    /* Set the state of the LED to on if the ethernet link is
-     active or off is disconnected. */
-    if (pIf->m_lpcNetif.flags & NETIF_FLAG_LINK_UP)
-    {
-      pIf->m_linkUp = true;
-    }
-    else
-    {
-      pIf->m_linkUp = false;
-    }
-  }
+    EthernetInterface* pIf = (EthernetInterface*)( ((int32_t)netif) + ((int32_t)s_lpcNetifOff) ); //Bad kludge
+    pIf->setIPAddress(inet_ntoa(netif->ip_addr));
+    pIf->setConnected(netif_is_up(netif)?true:false);
+    pIf->m_netifStatusSphre.wait(0); //Clear if pending
+    pIf->m_netifStatusSphre.release();
 }
 
+/*static*/ int32_t EthernetInterface::s_lpcNetifOff = 0;
\ No newline at end of file
--- a/main/if/EthernetInterface.h	Fri Jun 15 14:23:03 2012 +0000
+++ b/main/if/EthernetInterface.h	Wed Jun 20 13:19:13 2012 +0000
@@ -45,16 +45,17 @@
 
   int init(const char* ip, const char* mask, const char* gateway, const char* dns1, const char* dns2); //No DHCP
 
-  int connect();
-  int disconnect();
+  virtual int connect();
+  virtual int disconnect();
 
 private:
-  static void phySupervisorCb(void const* ctx);
-
-  RtosTimer m_supervisor;
+  static void netifStatusCb(struct netif *netif);
+  
+  static int32_t s_lpcNetifOff; //Offset between m_lpcNetif and this ... this might be quite kludgy but should work!
+  
   struct netif m_lpcNetif;
+  Semaphore m_netifStatusSphre;
   bool m_useDHCP;
-  bool m_linkUp;
 };
 
 #endif /* ETHERNETNETWORK_H_ */
--- a/main/lwipopts.h	Fri Jun 15 14:23:03 2012 +0000
+++ b/main/lwipopts.h	Wed Jun 20 13:19:13 2012 +0000
@@ -65,4 +65,7 @@
 
 #define LWIP_BROADCAST_PING       1
 
+#define LWIP_NETIF_STATUS_CALLBACK 1 //We want to be notified whenever the netif state changes 
+
+
 #endif /* LWIPOPTS_H_ */
\ No newline at end of file