Stripped down version of Segundos NetService library (http://mbed.org/users/segundo/libraries/NetServices ). I have removed all NetServices, and all functions which had been disabled. Use this version when you need only pure TCP or UDP functions - this library compiles faster.

Dependencies:   lwip lwip-sys

Dependents:   christmasLights device_server pop3demo device_server_udp ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetNetIf.cpp Source File

EthernetNetIf.cpp

00001 #pragma diag_remark 1464
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 #include "EthernetNetIf.h"
00025 
00026 #include "netif/etharp.h"
00027 #include "lwip/dhcp.h"
00028 #include "lwip/dns.h"
00029 #include "lwip/igmp.h"
00030 
00031 #include "drv/eth/eth_drv.h"
00032 #include "mbed.h"
00033 
00034 //#define __DEBUG
00035 #include "dbg/dbg.h"
00036 
00037 #include "netCfg.h"
00038 #if NET_ETH
00039 
00040 EthernetNetIf::EthernetNetIf(const char* hostname) : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL),
00041 m_netmask(255,255,255,255), m_gateway(), m_hostname(hostname)
00042 {
00043   //m_hostname = NULL;
00044   m_pNetIf = new netif;
00045   m_useDhcp = true;
00046   m_pDhcp = new dhcp;
00047   m_setup = false;
00048 }
00049 
00050 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
00051 {
00052   m_hostname = NULL;
00053   m_netmask = netmask;
00054   m_gateway = gateway;
00055   m_ip = ip;
00056   m_pNetIf = new netif;
00057   dns_setserver(0, &dns.getStruct());
00058   m_useDhcp = false;
00059   m_setup = false;
00060 }
00061 
00062 EthernetNetIf::~EthernetNetIf()
00063 {
00064   if(m_pNetIf)
00065   {
00066 #if LWIP_IGMP
00067     igmp_stop(m_pNetIf); //Stop IGMP processing
00068 #endif
00069     netif_set_down(m_pNetIf);
00070     netif_remove(m_pNetIf);
00071     delete m_pNetIf;
00072     eth_free();
00073   }
00074   
00075   if (m_pDhcp)
00076     delete m_pDhcp;
00077 }
00078   
00079 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/)
00080 {
00081   if (m_setup)
00082   {
00083 #if LWIP_IGMP
00084     igmp_stop(m_pNetIf);
00085 #endif
00086     netif_set_down(m_pNetIf);
00087     netif_remove(m_pNetIf);
00088     delete m_pNetIf;
00089     eth_free();
00090     m_pNetIf = new netif;
00091   }
00092 
00093   LwipNetIf::init();
00094   //m_ethArpTicker.attach_us(&etharp_tmr,  ARP_TMR_INTERVAL  * 1000); // = 5s in etharp.h
00095   m_ethArpTimer.start();
00096   if(m_useDhcp)
00097   {
00098     //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
00099     //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
00100     m_dhcpCoarseTimer.start();
00101     m_dhcpFineTimer.start();
00102   }
00103   m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6
00104   eth_address((char *)m_pNetIf->hwaddr);
00105   
00106   DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", 
00107   m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
00108   m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
00109 
00110   m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input);
00111   m_pNetIf->hostname = (char *)m_hostname;
00112   netif_set_default(m_pNetIf);
00113   
00114   //DBG("\r\nStarting DHCP.\r\n");
00115   
00116   if(m_useDhcp)
00117   {
00118     dhcp_set_struct(m_pNetIf, m_pDhcp);
00119     dhcp_start(m_pNetIf);
00120     DBG("DHCP Started, waiting for IP...\n");
00121   }
00122   else
00123   {
00124     netif_set_up(m_pNetIf);
00125   }
00126   
00127   Timer timeout;
00128   timeout.start();
00129   while( !netif_is_up(m_pNetIf) ) //Wait until device is up
00130   {
00131     if(m_useDhcp)
00132     {
00133       if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
00134       {
00135         m_dhcpFineTimer.reset();
00136         dhcp_fine_tmr();
00137       }
00138       if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
00139       {
00140         m_dhcpCoarseTimer.reset();
00141         dhcp_coarse_tmr();
00142       }
00143     }
00144     poll();
00145     if( timeout.read_ms() > timeout_ms )
00146     {
00147       //Abort
00148       if(m_useDhcp)
00149         dhcp_stop(m_pNetIf);
00150       else
00151         netif_set_down(m_pNetIf);
00152       DBG("\r\nTimeout.\r\n");
00153       m_setup = true;
00154       return ETH_TIMEOUT;
00155     }
00156   }
00157   
00158   #if LWIP_IGMP
00159   igmp_start(m_pNetIf); //Start IGMP processing
00160   #endif
00161   
00162   m_ip = IpAddr(&(m_pNetIf->ip_addr));
00163    
00164   DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
00165   
00166   m_setup = true;
00167   return ETH_OK;
00168 }
00169 
00170 void EthernetNetIf::poll()
00171 {
00172   if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL)
00173   {
00174     m_ethArpTimer.reset();
00175     etharp_tmr();
00176   }
00177   #if LWIP_IGMP
00178   if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL)
00179   {
00180     m_igmpTimer.reset();
00181     igmp_tmr();
00182   }
00183   #endif
00184   LwipNetIf::poll();
00185   eth_poll();
00186 }
00187 
00188 const char* EthernetNetIf::getHwAddr() const {
00189     return (char*)m_pNetIf->hwaddr;
00190 }
00191 
00192 const char* EthernetNetIf::getHostname() const {
00193     return m_hostname;
00194 }
00195 
00196 #endif