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