Darran Shepherd
/
AutoIpNetStack
Net stack with AutoIP enabled
Embed:
(wiki syntax)
Show/hide line numbers
EthernetNetIf.cpp
00001 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 #include "netif/etharp.h" 00026 #include "lwip/dhcp.h " 00027 #include "ipv4/lwip/autoip.h" 00028 #include "lwip/dns.h" 00029 #include "drv/eth/eth_drv.h" 00030 #include "mbed.h" 00031 00032 #define __DEBUG 00033 #include "dbg/dbg.h" 00034 00035 #include "netCfg.h" 00036 #if NET_ETH 00037 00038 EthernetNetIf::EthernetNetIf() : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_autoIpTimer(), m_pNetIf(NULL), 00039 m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL) 00040 { 00041 m_hostname = NULL; 00042 m_pNetIf = new netif; 00043 m_useDhcp = true; 00044 } 00045 00046 EthernetNetIf::EthernetNetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_autoIpTimer(), m_pNetIf(NULL), m_hostname(NULL) //W/o DHCP 00047 { 00048 m_hostname = NULL; 00049 m_netmask = netmask; 00050 m_gateway = gateway; 00051 m_ip = ip; 00052 m_pNetIf = new netif; 00053 dns_setserver(0, &dns.getStruct()); 00054 m_useDhcp = false; 00055 } 00056 00057 EthernetNetIf::~EthernetNetIf() 00058 { 00059 if(m_pNetIf) 00060 { 00061 netif_set_down(m_pNetIf); 00062 netif_remove(m_pNetIf); 00063 delete m_pNetIf; 00064 eth_free(); 00065 } 00066 } 00067 00068 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/) 00069 { 00070 LwipNetIf::init(); 00071 //m_ethArpTicker.attach_us(ðarp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h 00072 m_ethArpTimer.start(); 00073 if(m_useDhcp) 00074 { 00075 //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h 00076 //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h 00077 m_dhcpCoarseTimer.start(); 00078 m_dhcpFineTimer.start(); 00079 m_autoIpTimer.start(); 00080 } 00081 m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6 00082 eth_address((char *)m_pNetIf->hwaddr); 00083 00084 DBG("\r\nHW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\r\n", 00085 m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2], 00086 m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]); 00087 00088 m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input); 00089 //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now 00090 netif_set_default(m_pNetIf); 00091 00092 //DBG("\r\nStarting DHCP.\r\n"); 00093 00094 if(m_useDhcp) 00095 { 00096 dhcp_start(m_pNetIf); 00097 DBG("\r\nDHCP Started, waiting for IP...\r\n"); 00098 } 00099 else 00100 { 00101 netif_set_up(m_pNetIf); 00102 } 00103 00104 Timer timeout; 00105 timeout.start(); 00106 while( !netif_is_up(m_pNetIf) ) //Wait until device is up 00107 { 00108 if(m_useDhcp) 00109 { 00110 if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS) 00111 { 00112 m_dhcpFineTimer.reset(); 00113 dhcp_fine_tmr(); 00114 } 00115 if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS) 00116 { 00117 m_dhcpCoarseTimer.reset(); 00118 dhcp_coarse_tmr(); 00119 } 00120 if(m_autoIpTimer.read_ms()>=AUTOIP_TMR_INTERVAL) 00121 { 00122 m_autoIpTimer.reset(); 00123 autoip_tmr(); 00124 } 00125 } 00126 poll(); 00127 if( timeout.read_ms() > timeout_ms ) 00128 { 00129 //Abort 00130 if(m_useDhcp) 00131 dhcp_stop(m_pNetIf); 00132 else 00133 netif_set_down(m_pNetIf); 00134 DBG("\r\nTimeout.\r\n"); 00135 return ETH_TIMEOUT; 00136 } 00137 } 00138 00139 m_ip = IpAddr(&(m_pNetIf->ip_addr)); 00140 00141 DBG("\r\nConnected, IP : %d.%d.%d.%d\r\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]); 00142 00143 return ETH_OK; 00144 } 00145 00146 void EthernetNetIf::poll() 00147 { 00148 if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL) 00149 { 00150 m_ethArpTimer.reset(); 00151 etharp_tmr(); 00152 } 00153 LwipNetIf::poll(); 00154 eth_poll(); 00155 } 00156 00157 #endif
Generated on Tue Jul 12 2022 15:37:03 by 1.7.2