EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
if/eth/EthernetNetIf.cpp@0:632c9925f013, 2010-06-11 (annotated)
- Committer:
- donatien
- Date:
- Fri Jun 11 16:05:15 2010 +0000
- Revision:
- 0:632c9925f013
- Child:
- 2:a4f97773c90f
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 | 0:632c9925f013 | 37 | EthernetNetIf::EthernetNetIf() : LwipNetIf(), m_ethArpTicker(), m_dhcpCoarseTicker(), m_dhcpFineTicker(), 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 | 0:632c9925f013 | 45 | EthernetNetIf::EthernetNetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) : LwipNetIf(), m_ethArpTicker(), m_dhcpCoarseTicker(), m_dhcpFineTicker(), 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 | 0:632c9925f013 | 70 | m_ethArpTicker.attach_us(ðarp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h |
donatien | 0:632c9925f013 | 71 | if(m_useDhcp) |
donatien | 0:632c9925f013 | 72 | { |
donatien | 0:632c9925f013 | 73 | m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h |
donatien | 0:632c9925f013 | 74 | m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h |
donatien | 0:632c9925f013 | 75 | } |
donatien | 0:632c9925f013 | 76 | m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6 |
donatien | 0:632c9925f013 | 77 | eth_address((char *)m_pNetIf->hwaddr); |
donatien | 0:632c9925f013 | 78 | |
donatien | 0:632c9925f013 | 79 | DBG("\r\nHW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\r\n", |
donatien | 0:632c9925f013 | 80 | m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2], |
donatien | 0:632c9925f013 | 81 | m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]); |
donatien | 0:632c9925f013 | 82 | DBG("\r\nIn Setup.\r\n"); |
donatien | 0:632c9925f013 | 83 | |
donatien | 0:632c9925f013 | 84 | 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 | 85 | //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now |
donatien | 0:632c9925f013 | 86 | netif_set_default(m_pNetIf); |
donatien | 0:632c9925f013 | 87 | |
donatien | 0:632c9925f013 | 88 | //DBG("\r\nStarting DHCP.\r\n"); |
donatien | 0:632c9925f013 | 89 | |
donatien | 0:632c9925f013 | 90 | if(m_useDhcp) |
donatien | 0:632c9925f013 | 91 | { |
donatien | 0:632c9925f013 | 92 | dhcp_start(m_pNetIf); |
donatien | 0:632c9925f013 | 93 | DBG("\r\nDHCP Started, waiting for IP...\r\n"); |
donatien | 0:632c9925f013 | 94 | } |
donatien | 0:632c9925f013 | 95 | else |
donatien | 0:632c9925f013 | 96 | { |
donatien | 0:632c9925f013 | 97 | netif_set_up(m_pNetIf); |
donatien | 0:632c9925f013 | 98 | } |
donatien | 0:632c9925f013 | 99 | |
donatien | 0:632c9925f013 | 100 | Timer timeout; |
donatien | 0:632c9925f013 | 101 | timeout.start(); |
donatien | 0:632c9925f013 | 102 | while( !netif_is_up(m_pNetIf) ) //Wait until device is up |
donatien | 0:632c9925f013 | 103 | { |
donatien | 0:632c9925f013 | 104 | poll(); |
donatien | 0:632c9925f013 | 105 | if( timeout.read_ms() > timeout_ms ) |
donatien | 0:632c9925f013 | 106 | { |
donatien | 0:632c9925f013 | 107 | //Abort |
donatien | 0:632c9925f013 | 108 | if(m_useDhcp) |
donatien | 0:632c9925f013 | 109 | dhcp_stop(m_pNetIf); |
donatien | 0:632c9925f013 | 110 | else |
donatien | 0:632c9925f013 | 111 | netif_set_down(m_pNetIf); |
donatien | 0:632c9925f013 | 112 | DBG("\r\nTimeout.\r\n"); |
donatien | 0:632c9925f013 | 113 | return ETH_TIMEOUT; |
donatien | 0:632c9925f013 | 114 | } |
donatien | 0:632c9925f013 | 115 | } |
donatien | 0:632c9925f013 | 116 | |
donatien | 0:632c9925f013 | 117 | m_ip = IpAddr(&(m_pNetIf->ip_addr)); |
donatien | 0:632c9925f013 | 118 | |
donatien | 0:632c9925f013 | 119 | 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 | 120 | |
donatien | 0:632c9925f013 | 121 | return ETH_OK; |
donatien | 0:632c9925f013 | 122 | } |
donatien | 0:632c9925f013 | 123 | |
donatien | 0:632c9925f013 | 124 | void EthernetNetIf::poll() |
donatien | 0:632c9925f013 | 125 | { |
donatien | 0:632c9925f013 | 126 | LwipNetIf::poll(); |
donatien | 0:632c9925f013 | 127 | eth_poll(); |
donatien | 0:632c9925f013 | 128 | } |
donatien | 0:632c9925f013 | 129 | |
donatien | 0:632c9925f013 | 130 | #endif |