Dependents:   TimeZoneDemo EthernetJackTestCode MMEx_Challenge ntp_mem ... more

Committer:
segundo
Date:
Fri Nov 19 21:01:51 2010 +0000
Revision:
3:5a6792c147c0
Parent:
0:ac1725ba162c
Child:
4:966a0265edfc

        

Who changed what in which revision?

UserRevisionLine numberNew 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 3:5a6792c147c0 46 m_setup = false;
segundo 0:ac1725ba162c 47 }
segundo 0:ac1725ba162c 48
segundo 0:ac1725ba162c 49 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 50 {
segundo 0:ac1725ba162c 51 m_hostname = NULL;
segundo 0:ac1725ba162c 52 m_netmask = netmask;
segundo 0:ac1725ba162c 53 m_gateway = gateway;
segundo 0:ac1725ba162c 54 m_ip = ip;
segundo 0:ac1725ba162c 55 m_pNetIf = new netif;
segundo 0:ac1725ba162c 56 dns_setserver(0, &dns.getStruct());
segundo 0:ac1725ba162c 57 m_useDhcp = false;
segundo 3:5a6792c147c0 58 m_setup = false;
segundo 0:ac1725ba162c 59 }
segundo 0:ac1725ba162c 60
segundo 0:ac1725ba162c 61 EthernetNetIf::~EthernetNetIf()
segundo 0:ac1725ba162c 62 {
segundo 0:ac1725ba162c 63 if(m_pNetIf)
segundo 0:ac1725ba162c 64 {
segundo 0:ac1725ba162c 65 igmp_stop(m_pNetIf); //Stop IGMP processing
segundo 0:ac1725ba162c 66 netif_set_down(m_pNetIf);
segundo 0:ac1725ba162c 67 netif_remove(m_pNetIf);
segundo 0:ac1725ba162c 68 delete m_pNetIf;
segundo 0:ac1725ba162c 69 eth_free();
segundo 0:ac1725ba162c 70 }
segundo 0:ac1725ba162c 71 }
segundo 0:ac1725ba162c 72
segundo 0:ac1725ba162c 73 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/)
segundo 0:ac1725ba162c 74 {
segundo 3:5a6792c147c0 75 if (m_setup)
segundo 3:5a6792c147c0 76 {
segundo 3:5a6792c147c0 77 igmp_stop(m_pNetIf);
segundo 3:5a6792c147c0 78 netif_set_down(m_pNetIf);
segundo 3:5a6792c147c0 79 netif_remove(m_pNetIf);
segundo 3:5a6792c147c0 80 delete m_pNetIf;
segundo 3:5a6792c147c0 81 eth_free();
segundo 3:5a6792c147c0 82 m_pNetIf = new netif;
segundo 3:5a6792c147c0 83 }
segundo 3:5a6792c147c0 84
segundo 0:ac1725ba162c 85 LwipNetIf::init();
segundo 0:ac1725ba162c 86 //m_ethArpTicker.attach_us(&etharp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h
segundo 0:ac1725ba162c 87 m_ethArpTimer.start();
segundo 0:ac1725ba162c 88 if(m_useDhcp)
segundo 0:ac1725ba162c 89 {
segundo 0:ac1725ba162c 90 //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
segundo 0:ac1725ba162c 91 //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
segundo 0:ac1725ba162c 92 m_dhcpCoarseTimer.start();
segundo 0:ac1725ba162c 93 m_dhcpFineTimer.start();
segundo 0:ac1725ba162c 94 }
segundo 0:ac1725ba162c 95 m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6
segundo 0:ac1725ba162c 96 eth_address((char *)m_pNetIf->hwaddr);
segundo 0:ac1725ba162c 97
segundo 0:ac1725ba162c 98 DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n",
segundo 0:ac1725ba162c 99 m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
segundo 0:ac1725ba162c 100 m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
segundo 0:ac1725ba162c 101
segundo 0:ac1725ba162c 102 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 103 m_pNetIf->hostname = (char *)m_hostname;
segundo 0:ac1725ba162c 104 netif_set_default(m_pNetIf);
segundo 0:ac1725ba162c 105
segundo 0:ac1725ba162c 106 //DBG("\r\nStarting DHCP.\r\n");
segundo 0:ac1725ba162c 107
segundo 0:ac1725ba162c 108 if(m_useDhcp)
segundo 0:ac1725ba162c 109 {
segundo 0:ac1725ba162c 110 dhcp_start(m_pNetIf);
segundo 0:ac1725ba162c 111 DBG("DHCP Started, waiting for IP...\n");
segundo 0:ac1725ba162c 112 }
segundo 0:ac1725ba162c 113 else
segundo 0:ac1725ba162c 114 {
segundo 0:ac1725ba162c 115 netif_set_up(m_pNetIf);
segundo 0:ac1725ba162c 116 }
segundo 0:ac1725ba162c 117
segundo 0:ac1725ba162c 118 Timer timeout;
segundo 0:ac1725ba162c 119 timeout.start();
segundo 0:ac1725ba162c 120 while( !netif_is_up(m_pNetIf) ) //Wait until device is up
segundo 0:ac1725ba162c 121 {
segundo 0:ac1725ba162c 122 if(m_useDhcp)
segundo 0:ac1725ba162c 123 {
segundo 0:ac1725ba162c 124 if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
segundo 0:ac1725ba162c 125 {
segundo 0:ac1725ba162c 126 m_dhcpFineTimer.reset();
segundo 0:ac1725ba162c 127 dhcp_fine_tmr();
segundo 0:ac1725ba162c 128 }
segundo 0:ac1725ba162c 129 if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
segundo 0:ac1725ba162c 130 {
segundo 0:ac1725ba162c 131 m_dhcpCoarseTimer.reset();
segundo 0:ac1725ba162c 132 dhcp_coarse_tmr();
segundo 0:ac1725ba162c 133 }
segundo 0:ac1725ba162c 134 }
segundo 0:ac1725ba162c 135 poll();
segundo 0:ac1725ba162c 136 if( timeout.read_ms() > timeout_ms )
segundo 0:ac1725ba162c 137 {
segundo 0:ac1725ba162c 138 //Abort
segundo 0:ac1725ba162c 139 if(m_useDhcp)
segundo 0:ac1725ba162c 140 dhcp_stop(m_pNetIf);
segundo 0:ac1725ba162c 141 else
segundo 0:ac1725ba162c 142 netif_set_down(m_pNetIf);
segundo 0:ac1725ba162c 143 DBG("\r\nTimeout.\r\n");
segundo 3:5a6792c147c0 144 m_setup = true;
segundo 0:ac1725ba162c 145 return ETH_TIMEOUT;
segundo 0:ac1725ba162c 146 }
segundo 0:ac1725ba162c 147 }
segundo 0:ac1725ba162c 148
segundo 0:ac1725ba162c 149 #if LWIP_IGMP
segundo 0:ac1725ba162c 150 igmp_start(m_pNetIf); //Start IGMP processing
segundo 0:ac1725ba162c 151 #endif
segundo 0:ac1725ba162c 152
segundo 0:ac1725ba162c 153 m_ip = IpAddr(&(m_pNetIf->ip_addr));
segundo 0:ac1725ba162c 154
segundo 0:ac1725ba162c 155 DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
segundo 0:ac1725ba162c 156
segundo 3:5a6792c147c0 157 m_setup = true;
segundo 0:ac1725ba162c 158 return ETH_OK;
segundo 0:ac1725ba162c 159 }
segundo 0:ac1725ba162c 160
segundo 0:ac1725ba162c 161 void EthernetNetIf::poll()
segundo 0:ac1725ba162c 162 {
segundo 0:ac1725ba162c 163 if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL)
segundo 0:ac1725ba162c 164 {
segundo 0:ac1725ba162c 165 m_ethArpTimer.reset();
segundo 0:ac1725ba162c 166 etharp_tmr();
segundo 0:ac1725ba162c 167 }
segundo 0:ac1725ba162c 168 #if LWIP_IGMP
segundo 0:ac1725ba162c 169 if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL)
segundo 0:ac1725ba162c 170 {
segundo 0:ac1725ba162c 171 m_igmpTimer.reset();
segundo 0:ac1725ba162c 172 igmp_tmr();
segundo 0:ac1725ba162c 173 }
segundo 0:ac1725ba162c 174 #endif
segundo 0:ac1725ba162c 175 LwipNetIf::poll();
segundo 0:ac1725ba162c 176 eth_poll();
segundo 0:ac1725ba162c 177 }
segundo 0:ac1725ba162c 178
segundo 3:5a6792c147c0 179 const char* EthernetNetIf::getHwAddr() const {
segundo 3:5a6792c147c0 180 return (char*)m_pNetIf->hwaddr;
segundo 3:5a6792c147c0 181 }
segundo 3:5a6792c147c0 182
segundo 0:ac1725ba162c 183 #endif