EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Tue Jul 27 15:59:42 2010 +0000
Revision:
5:dd63a1e02b1b
Parent:
4:fd826cad83c0

        

Who changed what in which revision?

UserRevisionLine numberNew 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 5:dd63a1e02b1b 25
donatien 0:632c9925f013 26 #include "netif/etharp.h"
donatien 0:632c9925f013 27 #include "lwip/dhcp.h"
donatien 0:632c9925f013 28 #include "lwip/dns.h"
donatien 5:dd63a1e02b1b 29 #include "lwip/igmp.h"
donatien 5:dd63a1e02b1b 30
donatien 0:632c9925f013 31 #include "drv/eth/eth_drv.h"
donatien 0:632c9925f013 32 #include "mbed.h"
donatien 0:632c9925f013 33
donatien 0:632c9925f013 34 #define __DEBUG
donatien 0:632c9925f013 35 #include "dbg/dbg.h"
donatien 0:632c9925f013 36
donatien 0:632c9925f013 37 #include "netCfg.h"
donatien 0:632c9925f013 38 #if NET_ETH
donatien 0:632c9925f013 39
donatien 5:dd63a1e02b1b 40 EthernetNetIf::EthernetNetIf() : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL),
donatien 0:632c9925f013 41 m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL)
donatien 0:632c9925f013 42 {
donatien 0:632c9925f013 43 m_hostname = NULL;
donatien 0:632c9925f013 44 m_pNetIf = new netif;
donatien 0:632c9925f013 45 m_useDhcp = true;
donatien 0:632c9925f013 46 }
donatien 0:632c9925f013 47
donatien 5:dd63a1e02b1b 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
donatien 0:632c9925f013 49 {
donatien 0:632c9925f013 50 m_hostname = NULL;
donatien 0:632c9925f013 51 m_netmask = netmask;
donatien 0:632c9925f013 52 m_gateway = gateway;
donatien 0:632c9925f013 53 m_ip = ip;
donatien 0:632c9925f013 54 m_pNetIf = new netif;
donatien 0:632c9925f013 55 dns_setserver(0, &dns.getStruct());
donatien 0:632c9925f013 56 m_useDhcp = false;
donatien 0:632c9925f013 57 }
donatien 0:632c9925f013 58
donatien 0:632c9925f013 59 EthernetNetIf::~EthernetNetIf()
donatien 0:632c9925f013 60 {
donatien 0:632c9925f013 61 if(m_pNetIf)
donatien 0:632c9925f013 62 {
donatien 5:dd63a1e02b1b 63 igmp_stop(m_pNetIf); //Stop IGMP processing
donatien 0:632c9925f013 64 netif_set_down(m_pNetIf);
donatien 0:632c9925f013 65 netif_remove(m_pNetIf);
donatien 0:632c9925f013 66 delete m_pNetIf;
donatien 0:632c9925f013 67 eth_free();
donatien 0:632c9925f013 68 }
donatien 0:632c9925f013 69 }
donatien 0:632c9925f013 70
donatien 0:632c9925f013 71 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/)
donatien 0:632c9925f013 72 {
donatien 0:632c9925f013 73 LwipNetIf::init();
donatien 2:a4f97773c90f 74 //m_ethArpTicker.attach_us(&etharp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h
donatien 2:a4f97773c90f 75 m_ethArpTimer.start();
donatien 0:632c9925f013 76 if(m_useDhcp)
donatien 0:632c9925f013 77 {
donatien 2:a4f97773c90f 78 //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
donatien 2:a4f97773c90f 79 //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
donatien 2:a4f97773c90f 80 m_dhcpCoarseTimer.start();
donatien 2:a4f97773c90f 81 m_dhcpFineTimer.start();
donatien 0:632c9925f013 82 }
donatien 0:632c9925f013 83 m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6
donatien 0:632c9925f013 84 eth_address((char *)m_pNetIf->hwaddr);
donatien 0:632c9925f013 85
donatien 4:fd826cad83c0 86 DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n",
donatien 0:632c9925f013 87 m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
donatien 0:632c9925f013 88 m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
donatien 2:a4f97773c90f 89
donatien 0:632c9925f013 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);
donatien 0:632c9925f013 91 //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now
donatien 0:632c9925f013 92 netif_set_default(m_pNetIf);
donatien 0:632c9925f013 93
donatien 0:632c9925f013 94 //DBG("\r\nStarting DHCP.\r\n");
donatien 0:632c9925f013 95
donatien 0:632c9925f013 96 if(m_useDhcp)
donatien 0:632c9925f013 97 {
donatien 0:632c9925f013 98 dhcp_start(m_pNetIf);
donatien 4:fd826cad83c0 99 DBG("DHCP Started, waiting for IP...\n");
donatien 0:632c9925f013 100 }
donatien 0:632c9925f013 101 else
donatien 0:632c9925f013 102 {
donatien 0:632c9925f013 103 netif_set_up(m_pNetIf);
donatien 0:632c9925f013 104 }
donatien 0:632c9925f013 105
donatien 0:632c9925f013 106 Timer timeout;
donatien 0:632c9925f013 107 timeout.start();
donatien 0:632c9925f013 108 while( !netif_is_up(m_pNetIf) ) //Wait until device is up
donatien 0:632c9925f013 109 {
donatien 2:a4f97773c90f 110 if(m_useDhcp)
donatien 2:a4f97773c90f 111 {
donatien 2:a4f97773c90f 112 if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
donatien 2:a4f97773c90f 113 {
donatien 2:a4f97773c90f 114 m_dhcpFineTimer.reset();
donatien 2:a4f97773c90f 115 dhcp_fine_tmr();
donatien 2:a4f97773c90f 116 }
donatien 2:a4f97773c90f 117 if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
donatien 2:a4f97773c90f 118 {
donatien 2:a4f97773c90f 119 m_dhcpCoarseTimer.reset();
donatien 2:a4f97773c90f 120 dhcp_coarse_tmr();
donatien 2:a4f97773c90f 121 }
donatien 2:a4f97773c90f 122 }
donatien 0:632c9925f013 123 poll();
donatien 0:632c9925f013 124 if( timeout.read_ms() > timeout_ms )
donatien 0:632c9925f013 125 {
donatien 0:632c9925f013 126 //Abort
donatien 0:632c9925f013 127 if(m_useDhcp)
donatien 0:632c9925f013 128 dhcp_stop(m_pNetIf);
donatien 0:632c9925f013 129 else
donatien 0:632c9925f013 130 netif_set_down(m_pNetIf);
donatien 0:632c9925f013 131 DBG("\r\nTimeout.\r\n");
donatien 0:632c9925f013 132 return ETH_TIMEOUT;
donatien 0:632c9925f013 133 }
donatien 0:632c9925f013 134 }
donatien 0:632c9925f013 135
donatien 5:dd63a1e02b1b 136 #if LWIP_IGMP
donatien 5:dd63a1e02b1b 137 igmp_start(m_pNetIf); //Start IGMP processing
donatien 5:dd63a1e02b1b 138 #endif
donatien 5:dd63a1e02b1b 139
donatien 0:632c9925f013 140 m_ip = IpAddr(&(m_pNetIf->ip_addr));
donatien 0:632c9925f013 141
donatien 4:fd826cad83c0 142 DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
donatien 0:632c9925f013 143
donatien 0:632c9925f013 144 return ETH_OK;
donatien 0:632c9925f013 145 }
donatien 0:632c9925f013 146
donatien 0:632c9925f013 147 void EthernetNetIf::poll()
donatien 0:632c9925f013 148 {
donatien 2:a4f97773c90f 149 if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL)
donatien 2:a4f97773c90f 150 {
donatien 2:a4f97773c90f 151 m_ethArpTimer.reset();
donatien 2:a4f97773c90f 152 etharp_tmr();
donatien 2:a4f97773c90f 153 }
donatien 5:dd63a1e02b1b 154 #if LWIP_IGMP
donatien 5:dd63a1e02b1b 155 if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL)
donatien 5:dd63a1e02b1b 156 {
donatien 5:dd63a1e02b1b 157 m_igmpTimer.reset();
donatien 5:dd63a1e02b1b 158 igmp_tmr();
donatien 5:dd63a1e02b1b 159 }
donatien 5:dd63a1e02b1b 160 #endif
donatien 0:632c9925f013 161 LwipNetIf::poll();
donatien 0:632c9925f013 162 eth_poll();
donatien 0:632c9925f013 163 }
donatien 0:632c9925f013 164
donatien 0:632c9925f013 165 #endif