ZG2100 Network interface source

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ZG2100NetIf.cpp Source File

ZG2100NetIf.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 "ZG2100NetIf.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/zg2100/zg_com.h"
00032 #include "drv/zg2100/zg_drv.h"
00033 #include "drv/zg2100/zg_if.h"
00034 #include "drv/zg2100/zg_net.h"
00035 
00036 #define __DEBUG
00037 #include "dbg/dbg.h"
00038 
00039 #include "netCfg.h"
00040 #if NET_ZG2100
00041 
00042 ZG2100NetIf::ZG2100NetIf( PinName mosi, PinName miso, PinName sclk, PinName cs, PinName intp, PinName nrst ) : 
00043 LwipNetIf(), m_spi(mosi,miso,sclk), m_cs(cs), m_intp(intp), m_nrst(nrst),
00044 m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL)
00045 {
00046   m_hostname = NULL;
00047   m_pNetIf = new netif;
00048   m_useDhcp = true;
00049 }
00050 
00051 ZG2100NetIf::ZG2100NetIf( PinName mosi, PinName miso, PinName sclk, PinName cs, PinName intp, PinName nrst,
00052 IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns ) : 
00053 LwipNetIf(), m_spi(mosi,miso,sclk), m_cs(cs), m_intp(intp), m_nrst(nrst),
00054 m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL), m_hostname(NULL) //W/o DHCP
00055 {
00056   m_hostname = NULL;
00057   m_netmask = netmask;
00058   m_gateway = gateway;
00059   m_ip = ip;
00060   m_pNetIf = new netif;
00061   dns_setserver(0, &dns.getStruct());
00062   m_useDhcp = false;
00063 }
00064 
00065 ZG2100NetIf::~ZG2100NetIf()
00066 {
00067   if(m_pNetIf)
00068   {
00069     netif_set_down(m_pNetIf);
00070     netif_remove(m_pNetIf);
00071     delete m_pNetIf;
00072   }
00073 }
00074 
00075 void ZG2100NetIf::init()
00076 {
00077   LwipNetIf::init();
00078   
00079   zg_com_init(&m_spi, &m_cs, &m_intp, &m_nrst);
00080   zg_drv_init();
00081   
00082   zg_mgmt_get_param(ZG_FIFO_MGMT_PARM_MACAD);
00083   
00084   waitReady();
00085   
00086   zg_mgmt_get_param(ZG_FIFO_MGMT_PARM_SYSV);
00087   
00088   waitReady();
00089   
00090   zg_mgmt_get_param(ZG_FIFO_MGMT_PARM_REGION);
00091   
00092   waitReady();
00093   
00094   m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, zg_net_init, ip_input);//ethernet_input);// ip_input);
00095   //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now
00096   netif_set_default(m_pNetIf);
00097   
00098   //DBG("\r\nStarting DHCP.\r\n");
00099   
00100   DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", 
00101   m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
00102   m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
00103    
00104 }
00105 
00106 void ZG2100NetIf::setSsid(const char* ssid)
00107 {
00108   zg_set_ssid(ssid);
00109 }
00110 
00111 void ZG2100NetIf::setWepKey(const byte* key, int len)
00112 {
00113   zg_set_wep_key(key, len);
00114   waitReady();
00115 }
00116 
00117 void ZG2100NetIf::setWpaPass(const char* pass)
00118 {
00119   zg_set_wpa_pass(pass);
00120   waitReady();
00121 }
00122 
00123 void ZG2100NetIf::setPskKey(const byte* key, int len)
00124 {
00125   zg_set_psk_key(key, len);
00126   waitReady();
00127 }
00128 
00129 ZG2100Err ZG2100NetIf::connect(ZG_BSS_TYPE type, ZG_SECURITY security)
00130 {
00131   zg_connect(type, security);
00132   waitReady();
00133   return zg_get_connection_result();
00134 }
00135 
00136 void ZG2100NetIf::disconnect()
00137 {
00138   zg_disconnect();
00139   waitReady();
00140 }
00141   
00142 ZG2100Err ZG2100NetIf::setup(int timeout_ms /*= 15000*/)
00143 {
00144   //m_ethArpTicker.attach_us(&etharp_tmr,  ARP_TMR_INTERVAL  * 1000); // = 5s in etharp.h
00145   m_ethArpTimer.start();
00146   if(m_useDhcp)
00147   {
00148     //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
00149     //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
00150     m_dhcpCoarseTimer.start();
00151     m_dhcpFineTimer.start();
00152   }
00153    
00154   if(m_useDhcp)
00155   {
00156     dhcp_start(m_pNetIf);
00157     DBG("DHCP Started, waiting for IP...\n");
00158   }
00159   else
00160   {
00161     netif_set_up(m_pNetIf);
00162   }
00163   
00164   Timer timeout;
00165   timeout.start();
00166   while( !netif_is_up(m_pNetIf) ) //Wait until device is up
00167   {
00168     if(m_useDhcp)
00169     {
00170       if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
00171       {
00172         m_dhcpFineTimer.reset();
00173         dhcp_fine_tmr();
00174       }
00175       if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
00176       {
00177         m_dhcpCoarseTimer.reset();
00178         dhcp_coarse_tmr();
00179       }
00180     }
00181     poll();
00182     if( timeout.read_ms() > timeout_ms )
00183     {
00184       //Abort
00185       if(m_useDhcp)
00186         dhcp_stop(m_pNetIf);
00187       else
00188         netif_set_down(m_pNetIf);
00189       DBG("Timeout.\n");
00190       return ZG_TIMEOUT;
00191     }
00192   }
00193   
00194   #if LWIP_IGMP
00195   igmp_start(m_pNetIf); //Start IGMP processing
00196   #endif
00197   
00198   m_ip = IpAddr(&(m_pNetIf->ip_addr));   
00199     
00200   DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
00201   
00202   return ZG_OK;
00203 }
00204 
00205 void ZG2100NetIf::poll()
00206 {
00207   if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL)
00208   {
00209     m_ethArpTimer.reset();
00210     etharp_tmr();
00211   }
00212   #if LWIP_IGMP
00213   if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL)
00214   {
00215     m_igmpTimer.reset();
00216     igmp_tmr();
00217   }
00218   #endif
00219   LwipNetIf::poll();
00220   zg_process();
00221 }
00222 
00223 void ZG2100NetIf::waitReady()
00224 {
00225   while( zg_mgmt_is_busy() )
00226   {
00227     zg_process();
00228   }
00229 }
00230 
00231 #endif