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