Ilya I / Mbed 2 deprecated iva2k_NetHttpServerTcpSockets

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers EthernetNetIf.cpp Source File

EthernetNetIf.cpp

00001 
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 #include "netif/etharp.h"
00026 #include "lwip/dhcp.h "
00027 #include "lwip/dns.h"
00028 #include "drv/eth/eth_drv.h"
00029 #include "mbed.h"
00030 
00031 #define __DEBUG
00032 #include "dbg/dbg.h"
00033 
00034 #include "netCfg.h"
00035 #if NET_ETH
00036 
00037 EthernetNetIf::EthernetNetIf() : LwipNetIf(), m_ethArpTicker(), m_dhcpCoarseTicker(), m_dhcpFineTicker(), m_pNetIf(NULL),
00038 m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL)
00039 {
00040   m_hostname = NULL;
00041   m_pNetIf = new netif;
00042   m_useDhcp = true;
00043 }
00044 
00045 EthernetNetIf::EthernetNetIf(IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns) : LwipNetIf(), m_ethArpTicker(), m_dhcpCoarseTicker(), m_dhcpFineTicker(), m_pNetIf(NULL), m_hostname(NULL) //W/o DHCP
00046 {
00047   m_hostname = NULL;
00048   m_netmask = netmask;
00049   m_gateway = gateway;
00050   m_ip = ip;
00051   m_pNetIf = new netif;
00052   dns_setserver(0, &dns.getStruct());
00053   m_useDhcp = false;
00054 }
00055 
00056 EthernetNetIf::~EthernetNetIf()
00057 {
00058   if(m_pNetIf)
00059   {
00060     netif_set_down(m_pNetIf);
00061     netif_remove(m_pNetIf);
00062     delete m_pNetIf;
00063     eth_free();
00064   }
00065 }
00066   
00067 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/)
00068 {
00069   LwipNetIf::init();
00070   m_ethArpTicker.attach_us(&etharp_tmr,  ARP_TMR_INTERVAL  * 1000); // = 5s in etharp.h
00071   if(m_useDhcp)
00072   {
00073     m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
00074     m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
00075   }
00076   m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6
00077   eth_address((char *)m_pNetIf->hwaddr);
00078   
00079   DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\r\n", 
00080   m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
00081   m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
00082   DBG("In Setup.\r\n");
00083   
00084   m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input);
00085   //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now
00086   netif_set_default(m_pNetIf);
00087   
00088   //DBG("Starting DHCP.\r\n");
00089   
00090   if(m_useDhcp)
00091   {
00092     dhcp_start(m_pNetIf);
00093     DBG("DHCP Started, waiting for IP...\r\n");
00094   }
00095   else
00096   {
00097     netif_set_up(m_pNetIf);
00098   }
00099   
00100   Timer timeout;
00101   timeout.start();
00102   while( !netif_is_up(m_pNetIf) ) //Wait until device is up
00103   {
00104     poll();
00105     if( timeout.read_ms() > timeout_ms )
00106     {
00107       //Abort
00108       if(m_useDhcp)
00109         dhcp_stop(m_pNetIf);
00110       else
00111         netif_set_down(m_pNetIf);
00112       DBG("Timeout.\r\n");
00113       return ETH_TIMEOUT;
00114     }
00115   }
00116   
00117   m_ip = IpAddr(&(m_pNetIf->ip_addr));
00118    
00119   DBG("Connected, IP : %d.%d.%d.%d\r\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
00120   
00121   return ETH_OK;
00122 }
00123 
00124 void EthernetNetIf::poll()
00125 {
00126   LwipNetIf::poll();
00127   eth_poll();
00128 }
00129 
00130 #endif