Ethernet Interface Networking Library
Dependencies: LwIPNetworking lwip-eth
Fork of EthernetNetworkLib by
EthernetInterface.cpp
00001 /* Ethernetnetwork.cpp */ 00002 /* Copyright (C) 2012 mbed.org, MIT License 00003 * 00004 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00005 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00006 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00007 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00008 * furnished to do so, subject to the following conditions: 00009 * 00010 * The above copyright notice and this permission notice shall be included in all copies or 00011 * substantial portions of the Software. 00012 * 00013 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00014 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00015 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00016 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00017 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00018 */ 00019 00020 #define __DEBUG__ 4 00021 #ifndef __MODULE__ 00022 #define __MODULE__ "EthernetInterface.cpp" 00023 #endif 00024 00025 #include "core/fwk.h" 00026 00027 #include "EthernetInterface.h" 00028 00029 #include "lwip/inet.h" 00030 #include "lwip/netif.h" 00031 #include "netif/etharp.h" 00032 #include "lwip/dhcp.h" 00033 #include "arch/lpc17_emac.h" 00034 #include "lpc_phy.h" /* For the PHY monitor support */ 00035 #include "lwip/tcpip.h" 00036 00037 #include "mbed.h" 00038 00039 00040 EthernetInterface::EthernetInterface() : LwIPInterface(), m_lpcNetif(), m_netifStatusSphre(1), m_useDHCP(false) 00041 { 00042 s_lpcNetifOff = (int32_t)( ((char*)this) - ((char*)&m_lpcNetif) ); 00043 m_netifStatusSphre.wait(0); 00044 } 00045 00046 int EthernetInterface::init() //With DHCP 00047 { 00048 m_useDHCP = true; 00049 00050 DBG("Initializing LwIP"); 00051 LwIPInterface::init(); //Init LwIP, NOT including PPP 00052 00053 DBG("DHCP IP assignment"); 00054 00055 memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif)); 00056 netif_add(&m_lpcNetif, NULL, NULL, NULL, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/); 00057 netif_set_default(&m_lpcNetif); 00058 00059 netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb); 00060 00061 return OK; 00062 } 00063 00064 int EthernetInterface::init(const char* ip, const char* mask, const char* gateway, 00065 const char* dns1, const char* dns2) //No DHCP 00066 { 00067 ip_addr_t ip_n, mask_n, gateway_n, dns1_n, dns2_n; 00068 00069 m_useDHCP = false; 00070 00071 DBG("Initializing LwIP"); 00072 LwIPInterface::init(); //Init LwIP 00073 00074 DBG("Static IP assignment"); 00075 inet_aton(ip, &ip_n); 00076 inet_aton(mask, &mask_n); 00077 inet_aton(gateway, &gateway_n); 00078 inet_aton(dns1, &dns1_n); 00079 inet_aton(dns2, &dns2_n); 00080 00081 memset((void*)&m_lpcNetif, 0, sizeof(m_lpcNetif)); 00082 netif_add(&m_lpcNetif, &ip_n, &mask_n, &gateway_n, NULL, lpc_enetif_init, ethernet_input/*tcpip_input???*/); 00083 netif_set_default(&m_lpcNetif); 00084 00085 netif_set_status_callback(&m_lpcNetif, &EthernetInterface::netifStatusCb); 00086 00087 return OK; 00088 } 00089 00090 int EthernetInterface::connect() 00091 { 00092 m_netifStatusSphre.wait(0); 00093 00094 DBG("Enable MAC interrupts"); 00095 NVIC_SetPriority(ENET_IRQn, ((0x01 << 3) | 0x01)); 00096 NVIC_EnableIRQ(ENET_IRQn); 00097 00098 if(m_useDHCP) 00099 { 00100 dhcp_start(&m_lpcNetif); //The DHCP client will set the interface up once the IP/Netmask/Getway/DNS Servers are recovered 00101 } 00102 else 00103 { 00104 netif_set_up(&m_lpcNetif); //Set interface up 00105 } 00106 00107 m_netifStatusSphre.wait(); 00108 00109 DBG("Connected with IP %s", getIPAddress()); 00110 00111 return OK; 00112 } 00113 00114 int EthernetInterface::disconnect() 00115 { 00116 m_netifStatusSphre.wait(0); 00117 00118 if(m_useDHCP) 00119 { 00120 dhcp_release(&m_lpcNetif); //Release the lease & bring interface down 00121 dhcp_stop(&m_lpcNetif); 00122 } 00123 else 00124 { 00125 netif_set_down(&m_lpcNetif); 00126 } 00127 00128 DBG("Disable MAC interrupts"); 00129 NVIC_DisableIRQ(ENET_IRQn); 00130 00131 m_netifStatusSphre.wait(); 00132 00133 return OK; 00134 } 00135 00136 /*static*/ void EthernetInterface::netifStatusCb(struct netif *netif) 00137 { 00138 EthernetInterface* pIf = (EthernetInterface*)( ((int32_t)netif) + ((int32_t)s_lpcNetifOff) ); //Bad kludge 00139 pIf->setIPAddress(inet_ntoa(netif->ip_addr)); 00140 pIf->setConnected(netif_is_up(netif)?true:false); 00141 pIf->m_netifStatusSphre.wait(0); //Clear if pending 00142 pIf->m_netifStatusSphre.release(); 00143 } 00144 00145 /*static*/ int32_t EthernetInterface::s_lpcNetifOff = 0;
Generated on Sat Jul 16 2022 06:56:09 by 1.7.2