Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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