Dependents: TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more
EthernetNetIf.cpp
00001 #pragma diag_remark 1464 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 #include "EthernetNetIf.h" 00025 00026 #include "netif/etharp.h" 00027 #include "lwip/dhcp.h " 00028 #include "lwip/dns.h" 00029 #include "lwip/igmp.h" 00030 00031 #include "drv/eth/eth_drv.h" 00032 #include "mbed.h" 00033 00034 //#define __DEBUG 00035 #include "dbg/dbg.h" 00036 00037 #include "netCfg.h" 00038 #if NET_ETH 00039 00040 EthernetNetIf::EthernetNetIf(const char* hostname) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), 00041 m_netmask(255,255,255,255), m_gateway(), m_hostname(hostname) 00042 { 00043 //m_hostname = NULL; 00044 m_pNetIf = new netif; 00045 m_useDhcp = true; 00046 m_pDhcp = new dhcp; 00047 m_setup = false; 00048 } 00049 00050 EthernetNetIf::EthernetNetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), m_hostname(NULL) //W/o DHCP 00051 { 00052 m_hostname = NULL; 00053 m_netmask = netmask; 00054 m_gateway = gateway; 00055 m_ip = ip; 00056 m_pNetIf = new netif; 00057 dns_setserver(0, &dns.getStruct()); 00058 m_useDhcp = false; 00059 m_setup = false; 00060 } 00061 00062 EthernetNetIf::~EthernetNetIf() 00063 { 00064 if(m_pNetIf) 00065 { 00066 igmp_stop(m_pNetIf); //Stop IGMP processing 00067 netif_set_down(m_pNetIf); 00068 netif_remove(m_pNetIf); 00069 delete m_pNetIf; 00070 eth_free(); 00071 } 00072 00073 if (m_pDhcp) 00074 delete m_pDhcp; 00075 } 00076 00077 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/) 00078 { 00079 if (m_setup) 00080 { 00081 igmp_stop(m_pNetIf); 00082 netif_set_down(m_pNetIf); 00083 netif_remove(m_pNetIf); 00084 delete m_pNetIf; 00085 eth_free(); 00086 m_pNetIf = new netif; 00087 } 00088 00089 LwipNetIf::init(); 00090 //m_ethArpTicker.attach_us(ðarp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h 00091 m_ethArpTimer.start(); 00092 if(m_useDhcp) 00093 { 00094 //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h 00095 //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h 00096 m_dhcpCoarseTimer.start(); 00097 m_dhcpFineTimer.start(); 00098 } 00099 m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6 00100 eth_address((char *)m_pNetIf->hwaddr); 00101 00102 DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", 00103 m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2], 00104 m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]); 00105 00106 m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input); 00107 m_pNetIf->hostname = (char *)m_hostname; 00108 netif_set_default(m_pNetIf); 00109 00110 //DBG("\r\nStarting DHCP.\r\n"); 00111 00112 if(m_useDhcp) 00113 { 00114 dhcp_set_struct(m_pNetIf, m_pDhcp); 00115 dhcp_start(m_pNetIf); 00116 DBG("DHCP Started, waiting for IP...\n"); 00117 } 00118 else 00119 { 00120 netif_set_up(m_pNetIf); 00121 } 00122 00123 Timer timeout; 00124 timeout.start(); 00125 while( !netif_is_up(m_pNetIf) ) //Wait until device is up 00126 { 00127 if(m_useDhcp) 00128 { 00129 if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS) 00130 { 00131 m_dhcpFineTimer.reset(); 00132 dhcp_fine_tmr(); 00133 } 00134 if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS) 00135 { 00136 m_dhcpCoarseTimer.reset(); 00137 dhcp_coarse_tmr(); 00138 } 00139 } 00140 poll(); 00141 if( timeout.read_ms() > timeout_ms ) 00142 { 00143 //Abort 00144 if(m_useDhcp) 00145 dhcp_stop(m_pNetIf); 00146 else 00147 netif_set_down(m_pNetIf); 00148 DBG("\r\nTimeout.\r\n"); 00149 m_setup = true; 00150 return ETH_TIMEOUT; 00151 } 00152 } 00153 00154 #if LWIP_IGMP 00155 igmp_start(m_pNetIf); //Start IGMP processing 00156 #endif 00157 00158 m_ip = IpAddr(&(m_pNetIf->ip_addr)); 00159 00160 DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]); 00161 00162 m_setup = true; 00163 return ETH_OK; 00164 } 00165 00166 void EthernetNetIf::poll() 00167 { 00168 if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL) 00169 { 00170 m_ethArpTimer.reset(); 00171 etharp_tmr(); 00172 } 00173 #if LWIP_IGMP 00174 if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL) 00175 { 00176 m_igmpTimer.reset(); 00177 igmp_tmr(); 00178 } 00179 #endif 00180 LwipNetIf::poll(); 00181 eth_poll(); 00182 } 00183 00184 const char* EthernetNetIf::getHwAddr() const { 00185 return (char*)m_pNetIf->hwaddr; 00186 } 00187 00188 const char* EthernetNetIf::getHostname() const { 00189 return m_hostname; 00190 } 00191 00192 #endif
Generated on Tue Jul 12 2022 16:54:37 by 1.7.2