Mbed_demos ALU
Dependencies: Socket lwip-eth lwip-sys lwip
Fork of EthernetInterface by
Revision 14:cec293071eed, committed 2012-07-23
- Comitter:
- emilmont
- Date:
- Mon Jul 23 11:53:01 2012 +0000
- Parent:
- 13:0063a5bac04f
- Child:
- 15:fd9597f1b81b
- Commit message:
- tidyup
Changed in this revision
--- a/EthernetInterface.cpp Mon Jul 16 14:47:36 2012 +0000 +++ b/EthernetInterface.cpp Mon Jul 23 11:53:01 2012 +0000 @@ -16,14 +16,6 @@ * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - -#define __DEBUG__ 0 -#ifndef __MODULE__ -#define __MODULE__ "EthernetInterface.cpp" -#endif - -#include "core/fwk.h" - #include "EthernetInterface.h" #include "lwip/inet.h" @@ -31,115 +23,83 @@ #include "netif/etharp.h" #include "lwip/dhcp.h" #include "arch/lpc17_emac.h" -#include "lpc_phy.h" /* For the PHY monitor support */ +#include "lpc_phy.h" #include "lwip/tcpip.h" #include "mbed.h" +/* TCP/IP and Network Interface Initialisation */ +static struct netif lpcNetif; -EthernetInterface::EthernetInterface() : LwIPInterface(), m_lpcNetif(), m_netifStatusSphre(1), m_useDHCP(false) -{ - s_lpcNetifOff = (int32_t)( ((char*)this) - ((char*)&m_lpcNetif) ); - m_netifStatusSphre.wait(0); -} +static Semaphore tcpip_inited(0); +static Semaphore netif_inited(0); -int EthernetInterface::init() //With DHCP -{ - m_useDHCP = true; +static char ip_addr[16]; +static bool connected; +static bool use_dhcp = false; - DBG("Initializing LwIP"); - LwIPInterface::init(); //Init LwIP, NOT including PPP - - DBG("DHCP IP assignment"); - - 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; +static void tcpip_init_done(void *arg) { + tcpip_inited.release(); +} +static void netif_status_callback(struct netif *netif) { + strcpy(ip_addr, inet_ntoa(netif->ip_addr)); + connected = netif_is_up(netif) ? true : false; + netif_inited.release(); +} +static void init_netif(ip_addr_t *ipaddr, ip_addr_t *netmask, ip_addr_t *gw) { + tcpip_init(tcpip_init_done, NULL); + tcpip_inited.wait(); + + memset((void*) &lpcNetif, 0, sizeof(lpcNetif)); + netif_add(&lpcNetif, ipaddr, netmask, gw, NULL, lpc_enetif_init, ethernet_input); + netif_set_default(&lpcNetif); + netif_set_status_callback(&lpcNetif, netif_status_callback); } -int EthernetInterface::init(const char* ip, const char* mask, const char* gateway, - const char* dns1, const char* dns2) //No DHCP -{ - ip_addr_t ip_n, mask_n, gateway_n, dns1_n, dns2_n; - - m_useDHCP = false; - - DBG("Initializing LwIP"); - LwIPInterface::init(); //Init LwIP - - DBG("Static IP assignment"); - inet_aton(ip, &ip_n); - inet_aton(mask, &mask_n); - inet_aton(gateway, &gateway_n); - inet_aton(dns1, &dns1_n); - inet_aton(dns2, &dns2_n); - - 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::init() { + use_dhcp = true; + init_netif(NULL, NULL, NULL); + return OK; } -int EthernetInterface::connect() -{ - m_netifStatusSphre.wait(0); - - DBG("Enable MAC interrupts"); - NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); - NVIC_EnableIRQ(ENET_IRQn); - - if(m_useDHCP) - { - 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::init(const char* ip, const char* mask, const char* gateway) { + use_dhcp = false; + ip_addr_t ip_n, mask_n, gateway_n; + inet_aton(ip, &ip_n); + inet_aton(mask, &mask_n); + inet_aton(gateway, &gateway_n); + init_netif(&ip_n, &mask_n, &gateway_n); + 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); - } - else - { - netif_set_down(&m_lpcNetif); - } - - DBG("Disable MAC interrupts"); - NVIC_DisableIRQ(ENET_IRQn); - - m_netifStatusSphre.wait(); - - return OK; +int EthernetInterface::connect() { + NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); + NVIC_EnableIRQ(ENET_IRQn); + + if (use_dhcp) { + dhcp_start(&lpcNetif); + } else { + netif_set_up(&lpcNetif); + } + + netif_inited.wait(); + + return OK; } -/*static*/ void EthernetInterface::netifStatusCb(struct netif *netif) -{ - 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(); +int EthernetInterface::disconnect() { + if (use_dhcp) { + dhcp_release(&lpcNetif); + dhcp_stop(&lpcNetif); + } else { + netif_set_down(&lpcNetif); + } + + NVIC_DisableIRQ(ENET_IRQn); + + return OK; } -/*static*/ int32_t EthernetInterface::s_lpcNetifOff = 0; \ No newline at end of file +char* EthernetInterface::getIPAddress() { + return (connected) ? (ip_addr) : (NULL); +}
--- a/EthernetInterface.h Mon Jul 16 14:47:36 2012 +0000 +++ b/EthernetInterface.h Mon Jul 23 11:53:01 2012 +0000 @@ -20,62 +20,42 @@ #ifndef ETHERNETINTERFACE_H_ #define ETHERNETINTERFACE_H_ -#include "core/fwk.h" - #include "rtos.h" - -#include "LwIPInterface.h" - #include "lwip/netif.h" /** Interface using Ethernet to connect to an IP-based network * */ -class EthernetInterface : public LwIPInterface -{ +class EthernetInterface { public: - /** Instantiate the Ethernet interface. - * - */ - EthernetInterface(); - /** Initialize the interface with DHCP. * Initialize the interface and configure it to use DHCP (no connection at this point). * \return 0 on success, a negative number on failure */ - int init(); //With DHCP + static int init(); //With DHCP /** Initialize the interface with a static IP address. * Initialize the interface and configure it with the following static configuration (no connection at this point). * \param ip the IP address to use * \param mask the IP address mask * \param gateway the gateway to use - * \param dns1 the primary DNS server - * \param dns2 the secondary DNS server * \return 0 on success, a negative number on failure */ - int init(const char* ip, const char* mask, const char* gateway, const char* dns1, const char* dns2); //No DHCP + static int init(const char* ip, const char* mask, const char* gateway); /** Connect * Bring the interface up, start DHCP if needed. * \return 0 on success, a negative number on failure */ - virtual int connect(); + static int connect(); /** Disconnect * Bring the interface down * \return 0 on success, a negative number on failure */ - virtual int disconnect(); - -private: - static void netifStatusCb(struct netif *netif); + static int disconnect(); - 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; + static char* getIPAddress(); }; //As a helper, include TCPSocket.h & UDPSocket.h here
--- a/LwIPNetworking.lib Mon Jul 16 14:47:36 2012 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/users/mbed_official/code/LwIPNetworking/#21bc40957627
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Socket.lib Mon Jul 23 11:53:01 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/Socket/#b227d242f3c7
--- a/lwip-eth.lib Mon Jul 16 14:47:36 2012 +0000 +++ b/lwip-eth.lib Mon Jul 23 11:53:01 2012 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/lwip-eth/#5208926bd863 +http://mbed.org/users/mbed_official/code/lwip-eth/#dd8b8f5b449a
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwip-sys.lib Mon Jul 23 11:53:01 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/lwip-sys/#2ace1ff71ae6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwip.lib Mon Jul 23 11:53:01 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/lwip/#00bf89892c76