Fork of NetServicesMin with some warnings removed

Dependencies:   lwip-sys lwip

Fork of NetServicesMin by Hendrik Lipka

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   ip_addr_t dip(dns.getStruct());
00058   dns_setserver(0, &dip);
00059   m_useDhcp = false;
00060   m_setup = false;
00061 }
00062 
00063 EthernetNetIf::~EthernetNetIf()
00064 {
00065   if(m_pNetIf)
00066   {
00067 #if LWIP_IGMP
00068     igmp_stop(m_pNetIf); //Stop IGMP processing
00069 #endif
00070     netif_set_down(m_pNetIf);
00071     netif_remove(m_pNetIf);
00072     delete m_pNetIf;
00073     eth_free();
00074   }
00075   
00076   if (m_pDhcp)
00077     delete m_pDhcp;
00078 }
00079   
00080 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/)
00081 {
00082   if (m_setup)
00083   {
00084 #if LWIP_IGMP
00085     igmp_stop(m_pNetIf);
00086 #endif
00087     netif_set_down(m_pNetIf);
00088     netif_remove(m_pNetIf);
00089     delete m_pNetIf;
00090     eth_free();
00091     m_pNetIf = new netif;
00092   }
00093 
00094   LwipNetIf::init();
00095   //m_ethArpTicker.attach_us(&etharp_tmr,  ARP_TMR_INTERVAL  * 1000); // = 5s in etharp.h
00096   m_ethArpTimer.start();
00097   if(m_useDhcp)
00098   {
00099     //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
00100     //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
00101     m_dhcpCoarseTimer.start();
00102     m_dhcpFineTimer.start();
00103   }
00104   m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6
00105   eth_address((char *)m_pNetIf->hwaddr);
00106   
00107   DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", 
00108   m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
00109   m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
00110 
00111   ip_addr_t mip(m_ip.getStruct()),
00112             mnm(m_netmask.getStruct()),
00113             mgt((m_gateway.getStruct()));
00114   m_pNetIf = netif_add(m_pNetIf, &mip, &mnm, &mgt, NULL, eth_init, ip_input);//ethernet_input);// ip_input);
00115   m_pNetIf->hostname = (char *)m_hostname;
00116   netif_set_default(m_pNetIf);
00117   
00118   //DBG("\r\nStarting DHCP.\r\n");
00119   
00120   if(m_useDhcp)
00121   {
00122     dhcp_set_struct(m_pNetIf, m_pDhcp);
00123     dhcp_start(m_pNetIf);
00124     DBG("DHCP Started, waiting for IP...\n");
00125   }
00126   else
00127   {
00128     netif_set_up(m_pNetIf);
00129   }
00130   
00131   Timer timeout;
00132   timeout.start();
00133   while( !netif_is_up(m_pNetIf) ) //Wait until device is up
00134   {
00135     if(m_useDhcp)
00136     {
00137       if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
00138       {
00139         m_dhcpFineTimer.reset();
00140         dhcp_fine_tmr();
00141       }
00142       if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
00143       {
00144         m_dhcpCoarseTimer.reset();
00145         dhcp_coarse_tmr();
00146       }
00147     }
00148     poll();
00149     if( timeout.read_ms() > timeout_ms )
00150     {
00151       //Abort
00152       if(m_useDhcp)
00153         dhcp_stop(m_pNetIf);
00154       else
00155         netif_set_down(m_pNetIf);
00156       DBG("\r\nTimeout.\r\n");
00157       m_setup = true;
00158       return ETH_TIMEOUT;
00159     }
00160   }
00161   
00162   #if LWIP_IGMP
00163   igmp_start(m_pNetIf); //Start IGMP processing
00164   #endif
00165   
00166   m_ip = IpAddr(&(m_pNetIf->ip_addr));
00167    
00168   DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
00169   
00170   m_setup = true;
00171   return ETH_OK;
00172 }
00173 
00174 void EthernetNetIf::poll()
00175 {
00176   if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL)
00177   {
00178     m_ethArpTimer.reset();
00179     etharp_tmr();
00180   }
00181   #if LWIP_IGMP
00182   if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL)
00183   {
00184     m_igmpTimer.reset();
00185     igmp_tmr();
00186   }
00187   #endif
00188   LwipNetIf::poll();
00189   eth_poll();
00190 }
00191 
00192 const char* EthernetNetIf::getHwAddr() const {
00193     return (char*)m_pNetIf->hwaddr;
00194 }
00195 
00196 const char* EthernetNetIf::getHostname() const {
00197     return m_hostname;
00198 }
00199 
00200 #endif