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