Used in Live Traffic Update Nokia LCD Display Project
Fork of NetServices by
if/eth/EthernetNetIf.cpp@4:966a0265edfc, 2010-11-21 (annotated)
- Committer:
- segundo
- Date:
- Sun Nov 21 17:13:44 2010 +0000
- Revision:
- 4:966a0265edfc
- Parent:
- 3:5a6792c147c0
- Child:
- 7:4e2468d7d5cb
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
segundo | 0:ac1725ba162c | 1 | #pragma diag_remark 1464 |
segundo | 0:ac1725ba162c | 2 | /* |
segundo | 0:ac1725ba162c | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
segundo | 0:ac1725ba162c | 4 | |
segundo | 0:ac1725ba162c | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
segundo | 0:ac1725ba162c | 6 | of this software and associated documentation files (the "Software"), to deal |
segundo | 0:ac1725ba162c | 7 | in the Software without restriction, including without limitation the rights |
segundo | 0:ac1725ba162c | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
segundo | 0:ac1725ba162c | 9 | copies of the Software, and to permit persons to whom the Software is |
segundo | 0:ac1725ba162c | 10 | furnished to do so, subject to the following conditions: |
segundo | 0:ac1725ba162c | 11 | |
segundo | 0:ac1725ba162c | 12 | The above copyright notice and this permission notice shall be included in |
segundo | 0:ac1725ba162c | 13 | all copies or substantial portions of the Software. |
segundo | 0:ac1725ba162c | 14 | |
segundo | 0:ac1725ba162c | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
segundo | 0:ac1725ba162c | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
segundo | 0:ac1725ba162c | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
segundo | 0:ac1725ba162c | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
segundo | 0:ac1725ba162c | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
segundo | 0:ac1725ba162c | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
segundo | 0:ac1725ba162c | 21 | THE SOFTWARE. |
segundo | 0:ac1725ba162c | 22 | */ |
segundo | 0:ac1725ba162c | 23 | |
segundo | 0:ac1725ba162c | 24 | #include "EthernetNetIf.h" |
segundo | 0:ac1725ba162c | 25 | |
segundo | 0:ac1725ba162c | 26 | #include "netif/etharp.h" |
segundo | 0:ac1725ba162c | 27 | #include "lwip/dhcp.h" |
segundo | 0:ac1725ba162c | 28 | #include "lwip/dns.h" |
segundo | 0:ac1725ba162c | 29 | #include "lwip/igmp.h" |
segundo | 0:ac1725ba162c | 30 | |
segundo | 0:ac1725ba162c | 31 | #include "drv/eth/eth_drv.h" |
segundo | 0:ac1725ba162c | 32 | #include "mbed.h" |
segundo | 0:ac1725ba162c | 33 | |
segundo | 0:ac1725ba162c | 34 | //#define __DEBUG |
segundo | 0:ac1725ba162c | 35 | #include "dbg/dbg.h" |
segundo | 0:ac1725ba162c | 36 | |
segundo | 0:ac1725ba162c | 37 | #include "netCfg.h" |
segundo | 0:ac1725ba162c | 38 | #if NET_ETH |
segundo | 0:ac1725ba162c | 39 | |
segundo | 0:ac1725ba162c | 40 | EthernetNetIf::EthernetNetIf(const char* hostname) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), |
segundo | 0:ac1725ba162c | 41 | m_netmask(255,255,255,255), m_gateway(), m_hostname(hostname) |
segundo | 0:ac1725ba162c | 42 | { |
segundo | 0:ac1725ba162c | 43 | //m_hostname = NULL; |
segundo | 0:ac1725ba162c | 44 | m_pNetIf = new netif; |
segundo | 0:ac1725ba162c | 45 | m_useDhcp = true; |
segundo | 4:966a0265edfc | 46 | m_pDhcp = new dhcp; |
segundo | 3:5a6792c147c0 | 47 | m_setup = false; |
segundo | 0:ac1725ba162c | 48 | } |
segundo | 0:ac1725ba162c | 49 | |
segundo | 0:ac1725ba162c | 50 | 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 |
segundo | 0:ac1725ba162c | 51 | { |
segundo | 0:ac1725ba162c | 52 | m_hostname = NULL; |
segundo | 0:ac1725ba162c | 53 | m_netmask = netmask; |
segundo | 0:ac1725ba162c | 54 | m_gateway = gateway; |
segundo | 0:ac1725ba162c | 55 | m_ip = ip; |
segundo | 0:ac1725ba162c | 56 | m_pNetIf = new netif; |
segundo | 0:ac1725ba162c | 57 | dns_setserver(0, &dns.getStruct()); |
segundo | 0:ac1725ba162c | 58 | m_useDhcp = false; |
segundo | 3:5a6792c147c0 | 59 | m_setup = false; |
segundo | 0:ac1725ba162c | 60 | } |
segundo | 0:ac1725ba162c | 61 | |
segundo | 0:ac1725ba162c | 62 | EthernetNetIf::~EthernetNetIf() |
segundo | 0:ac1725ba162c | 63 | { |
segundo | 0:ac1725ba162c | 64 | if(m_pNetIf) |
segundo | 0:ac1725ba162c | 65 | { |
segundo | 0:ac1725ba162c | 66 | igmp_stop(m_pNetIf); //Stop IGMP processing |
segundo | 0:ac1725ba162c | 67 | netif_set_down(m_pNetIf); |
segundo | 0:ac1725ba162c | 68 | netif_remove(m_pNetIf); |
segundo | 0:ac1725ba162c | 69 | delete m_pNetIf; |
segundo | 0:ac1725ba162c | 70 | eth_free(); |
segundo | 0:ac1725ba162c | 71 | } |
segundo | 4:966a0265edfc | 72 | |
segundo | 4:966a0265edfc | 73 | if (m_pDhcp) |
segundo | 4:966a0265edfc | 74 | delete m_pDhcp; |
segundo | 0:ac1725ba162c | 75 | } |
segundo | 0:ac1725ba162c | 76 | |
segundo | 0:ac1725ba162c | 77 | EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/) |
segundo | 0:ac1725ba162c | 78 | { |
segundo | 3:5a6792c147c0 | 79 | if (m_setup) |
segundo | 3:5a6792c147c0 | 80 | { |
segundo | 3:5a6792c147c0 | 81 | igmp_stop(m_pNetIf); |
segundo | 3:5a6792c147c0 | 82 | netif_set_down(m_pNetIf); |
segundo | 3:5a6792c147c0 | 83 | netif_remove(m_pNetIf); |
segundo | 3:5a6792c147c0 | 84 | delete m_pNetIf; |
segundo | 3:5a6792c147c0 | 85 | eth_free(); |
segundo | 3:5a6792c147c0 | 86 | m_pNetIf = new netif; |
segundo | 3:5a6792c147c0 | 87 | } |
segundo | 3:5a6792c147c0 | 88 | |
segundo | 0:ac1725ba162c | 89 | LwipNetIf::init(); |
segundo | 0:ac1725ba162c | 90 | //m_ethArpTicker.attach_us(ðarp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h |
segundo | 0:ac1725ba162c | 91 | m_ethArpTimer.start(); |
segundo | 0:ac1725ba162c | 92 | if(m_useDhcp) |
segundo | 0:ac1725ba162c | 93 | { |
segundo | 0:ac1725ba162c | 94 | //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h |
segundo | 0:ac1725ba162c | 95 | //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h |
segundo | 0:ac1725ba162c | 96 | m_dhcpCoarseTimer.start(); |
segundo | 0:ac1725ba162c | 97 | m_dhcpFineTimer.start(); |
segundo | 0:ac1725ba162c | 98 | } |
segundo | 0:ac1725ba162c | 99 | m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6 |
segundo | 0:ac1725ba162c | 100 | eth_address((char *)m_pNetIf->hwaddr); |
segundo | 0:ac1725ba162c | 101 | |
segundo | 0:ac1725ba162c | 102 | DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", |
segundo | 0:ac1725ba162c | 103 | m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2], |
segundo | 0:ac1725ba162c | 104 | m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]); |
segundo | 0:ac1725ba162c | 105 | |
segundo | 0:ac1725ba162c | 106 | m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input); |
segundo | 0:ac1725ba162c | 107 | m_pNetIf->hostname = (char *)m_hostname; |
segundo | 0:ac1725ba162c | 108 | netif_set_default(m_pNetIf); |
segundo | 0:ac1725ba162c | 109 | |
segundo | 0:ac1725ba162c | 110 | //DBG("\r\nStarting DHCP.\r\n"); |
segundo | 0:ac1725ba162c | 111 | |
segundo | 0:ac1725ba162c | 112 | if(m_useDhcp) |
segundo | 0:ac1725ba162c | 113 | { |
segundo | 4:966a0265edfc | 114 | dhcp_set_struct(m_pNetIf, m_pDhcp); |
segundo | 0:ac1725ba162c | 115 | dhcp_start(m_pNetIf); |
segundo | 0:ac1725ba162c | 116 | DBG("DHCP Started, waiting for IP...\n"); |
segundo | 0:ac1725ba162c | 117 | } |
segundo | 0:ac1725ba162c | 118 | else |
segundo | 0:ac1725ba162c | 119 | { |
segundo | 0:ac1725ba162c | 120 | netif_set_up(m_pNetIf); |
segundo | 0:ac1725ba162c | 121 | } |
segundo | 0:ac1725ba162c | 122 | |
segundo | 0:ac1725ba162c | 123 | Timer timeout; |
segundo | 0:ac1725ba162c | 124 | timeout.start(); |
segundo | 0:ac1725ba162c | 125 | while( !netif_is_up(m_pNetIf) ) //Wait until device is up |
segundo | 0:ac1725ba162c | 126 | { |
segundo | 0:ac1725ba162c | 127 | if(m_useDhcp) |
segundo | 0:ac1725ba162c | 128 | { |
segundo | 0:ac1725ba162c | 129 | if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS) |
segundo | 0:ac1725ba162c | 130 | { |
segundo | 0:ac1725ba162c | 131 | m_dhcpFineTimer.reset(); |
segundo | 0:ac1725ba162c | 132 | dhcp_fine_tmr(); |
segundo | 0:ac1725ba162c | 133 | } |
segundo | 0:ac1725ba162c | 134 | if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS) |
segundo | 0:ac1725ba162c | 135 | { |
segundo | 0:ac1725ba162c | 136 | m_dhcpCoarseTimer.reset(); |
segundo | 0:ac1725ba162c | 137 | dhcp_coarse_tmr(); |
segundo | 0:ac1725ba162c | 138 | } |
segundo | 0:ac1725ba162c | 139 | } |
segundo | 0:ac1725ba162c | 140 | poll(); |
segundo | 0:ac1725ba162c | 141 | if( timeout.read_ms() > timeout_ms ) |
segundo | 0:ac1725ba162c | 142 | { |
segundo | 0:ac1725ba162c | 143 | //Abort |
segundo | 0:ac1725ba162c | 144 | if(m_useDhcp) |
segundo | 0:ac1725ba162c | 145 | dhcp_stop(m_pNetIf); |
segundo | 0:ac1725ba162c | 146 | else |
segundo | 0:ac1725ba162c | 147 | netif_set_down(m_pNetIf); |
segundo | 0:ac1725ba162c | 148 | DBG("\r\nTimeout.\r\n"); |
segundo | 3:5a6792c147c0 | 149 | m_setup = true; |
segundo | 0:ac1725ba162c | 150 | return ETH_TIMEOUT; |
segundo | 0:ac1725ba162c | 151 | } |
segundo | 0:ac1725ba162c | 152 | } |
segundo | 0:ac1725ba162c | 153 | |
segundo | 0:ac1725ba162c | 154 | #if LWIP_IGMP |
segundo | 0:ac1725ba162c | 155 | igmp_start(m_pNetIf); //Start IGMP processing |
segundo | 0:ac1725ba162c | 156 | #endif |
segundo | 0:ac1725ba162c | 157 | |
segundo | 0:ac1725ba162c | 158 | m_ip = IpAddr(&(m_pNetIf->ip_addr)); |
segundo | 0:ac1725ba162c | 159 | |
segundo | 0:ac1725ba162c | 160 | DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]); |
segundo | 0:ac1725ba162c | 161 | |
segundo | 3:5a6792c147c0 | 162 | m_setup = true; |
segundo | 0:ac1725ba162c | 163 | return ETH_OK; |
segundo | 0:ac1725ba162c | 164 | } |
segundo | 0:ac1725ba162c | 165 | |
segundo | 0:ac1725ba162c | 166 | void EthernetNetIf::poll() |
segundo | 0:ac1725ba162c | 167 | { |
segundo | 0:ac1725ba162c | 168 | if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL) |
segundo | 0:ac1725ba162c | 169 | { |
segundo | 0:ac1725ba162c | 170 | m_ethArpTimer.reset(); |
segundo | 0:ac1725ba162c | 171 | etharp_tmr(); |
segundo | 0:ac1725ba162c | 172 | } |
segundo | 0:ac1725ba162c | 173 | #if LWIP_IGMP |
segundo | 0:ac1725ba162c | 174 | if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL) |
segundo | 0:ac1725ba162c | 175 | { |
segundo | 0:ac1725ba162c | 176 | m_igmpTimer.reset(); |
segundo | 0:ac1725ba162c | 177 | igmp_tmr(); |
segundo | 0:ac1725ba162c | 178 | } |
segundo | 0:ac1725ba162c | 179 | #endif |
segundo | 0:ac1725ba162c | 180 | LwipNetIf::poll(); |
segundo | 0:ac1725ba162c | 181 | eth_poll(); |
segundo | 0:ac1725ba162c | 182 | } |
segundo | 0:ac1725ba162c | 183 | |
segundo | 3:5a6792c147c0 | 184 | const char* EthernetNetIf::getHwAddr() const { |
segundo | 3:5a6792c147c0 | 185 | return (char*)m_pNetIf->hwaddr; |
segundo | 3:5a6792c147c0 | 186 | } |
segundo | 3:5a6792c147c0 | 187 | |
segundo | 0:ac1725ba162c | 188 | #endif |