Donatien Garnier / EthernetInterface

Dependencies:   LwIPNetworking lwip-eth

Dependents:   CPPSocketsTest

Fork of EthernetNetworkLib by Donatien Garnier

Revision:
8:f7aa5ec1e6fe
Parent:
6:8acfdd9f43f1
Child:
10:7336d3d9cf64
--- 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