ZG2100 Network interface source

Revision:
0:b802fc31f1db
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/if/zg2100/zg2100NetIf.cpp	Fri Jul 09 15:37:23 2010 +0000
@@ -0,0 +1,212 @@
+
+/*
+Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
+ 
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+ 
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+ 
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+*/
+
+#include "zg2100NetIf.h"
+#include "netif/etharp.h"
+#include "lwip/dhcp.h"
+#include "lwip/dns.h"
+
+#include "drv/zg2100/zg_com.h"
+#include "drv/zg2100/zg_drv.h"
+#include "drv/zg2100/zg_if.h"
+#include "drv/zg2100/zg_net.h"
+
+#define __DEBUG
+#include "dbg/dbg.h"
+
+#include "netCfg.h"
+#if NET_ZG2100
+
+ZG2100NetIf::ZG2100NetIf( PinName mosi, PinName miso, PinName sclk, PinName cs, PinName intp, PinName nrst ) : 
+LwipNetIf(), m_spi(mosi,miso,sclk), m_cs(cs), m_intp(intp), m_nrst(nrst),
+m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_pNetIf(NULL), m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL)
+{
+  m_hostname = NULL;
+  m_pNetIf = new netif;
+  m_useDhcp = true;
+}
+
+ZG2100NetIf::ZG2100NetIf( PinName mosi, PinName miso, PinName sclk, PinName cs, PinName intp, PinName nrst,
+IpAddr ip, IpAddr netmask, IpAddr gateway, IpAddr dns ) : 
+LwipNetIf(), m_spi(mosi,miso,sclk), m_cs(cs), m_intp(intp), m_nrst(nrst),
+m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_pNetIf(NULL), m_hostname(NULL) //W/o DHCP
+{
+  m_hostname = NULL;
+  m_netmask = netmask;
+  m_gateway = gateway;
+  m_ip = ip;
+  m_pNetIf = new netif;
+  dns_setserver(0, &dns.getStruct());
+  m_useDhcp = false;
+}
+
+ZG2100NetIf::~ZG2100NetIf()
+{
+  if(m_pNetIf)
+  {
+    netif_set_down(m_pNetIf);
+    netif_remove(m_pNetIf);
+    delete m_pNetIf;
+  }
+}
+
+void ZG2100NetIf::init()
+{
+  LwipNetIf::init();
+  
+  zg_com_init(&m_spi, &m_cs, &m_intp, &m_nrst);
+  zg_drv_init();
+  
+  zg_mgmt_get_param(ZG_FIFO_MGMT_PARM_MACAD);
+  
+  waitReady();
+  
+  zg_mgmt_get_param(ZG_FIFO_MGMT_PARM_SYSV);
+  
+  waitReady();
+  
+  zg_mgmt_get_param(ZG_FIFO_MGMT_PARM_REGION);
+  
+  waitReady();
+}
+
+void ZG2100NetIf::setSsid(const char* ssid)
+{
+  zg_set_ssid(ssid);
+}
+
+void ZG2100NetIf::setWepKey(const byte* key, int len)
+{
+  zg_set_wep_key(key, len);
+  waitReady();
+}
+
+void ZG2100NetIf::setWpaPass(const char* pass)
+{
+  zg_set_wpa_pass(pass);
+  waitReady();
+}
+
+void ZG2100NetIf::setPskKey(const byte* key, int len)
+{
+  zg_set_psk_key(key, len);
+  waitReady();
+}
+
+ZG2100Err ZG2100NetIf::connect(ZG_BSS_TYPE type, ZG_SECURITY security)
+{
+  zg_connect(type, security);
+  waitReady();
+  return zg_get_connection_result();
+}
+
+void ZG2100NetIf::disconnect()
+{
+  zg_disconnect();
+  waitReady();
+}
+  
+ZG2100Err ZG2100NetIf::setup(int timeout_ms /*= 15000*/)
+{
+  //m_ethArpTicker.attach_us(&etharp_tmr,  ARP_TMR_INTERVAL  * 1000); // = 5s in etharp.h
+  m_ethArpTimer.start();
+  if(m_useDhcp)
+  {
+    //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
+    //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
+    m_dhcpCoarseTimer.start();
+    m_dhcpFineTimer.start();
+  }
+   
+  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);
+  //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now
+  netif_set_default(m_pNetIf);
+  
+  //DBG("\r\nStarting DHCP.\r\n");
+  
+  DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n", 
+  m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
+  m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
+  
+  if(m_useDhcp)
+  {
+    dhcp_start(m_pNetIf);
+    DBG("DHCP Started, waiting for IP...\n");
+  }
+  else
+  {
+    netif_set_up(m_pNetIf);
+  }
+  
+  Timer timeout;
+  timeout.start();
+  while( !netif_is_up(m_pNetIf) ) //Wait until device is up
+  {
+    if(m_useDhcp)
+    {
+      if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
+      {
+        m_dhcpFineTimer.reset();
+        dhcp_fine_tmr();
+      }
+      if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
+      {
+        m_dhcpCoarseTimer.reset();
+        dhcp_coarse_tmr();
+      }
+    }
+    poll();
+    if( timeout.read_ms() > timeout_ms )
+    {
+      //Abort
+      if(m_useDhcp)
+        dhcp_stop(m_pNetIf);
+      else
+        netif_set_down(m_pNetIf);
+      DBG("Timeout.\n");
+      return ZG_TIMEOUT;
+    }
+  }
+  
+  m_ip = IpAddr(&(m_pNetIf->ip_addr));   
+    
+  DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
+  
+  return ZG_OK;
+}
+
+void ZG2100NetIf::poll()
+{
+  LwipNetIf::poll();
+  zg_process();
+}
+
+void ZG2100NetIf::waitReady()
+{
+  while( zg_mgmt_is_busy() )
+  {
+    zg_process();
+  }
+}
+
+#endif