SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lorcansmith 0:2a53a4c3238c 1
lorcansmith 0:2a53a4c3238c 2 /*
lorcansmith 0:2a53a4c3238c 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
lorcansmith 0:2a53a4c3238c 4
lorcansmith 0:2a53a4c3238c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
lorcansmith 0:2a53a4c3238c 6 of this software and associated documentation files (the "Software"), to deal
lorcansmith 0:2a53a4c3238c 7 in the Software without restriction, including without limitation the rights
lorcansmith 0:2a53a4c3238c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
lorcansmith 0:2a53a4c3238c 9 copies of the Software, and to permit persons to whom the Software is
lorcansmith 0:2a53a4c3238c 10 furnished to do so, subject to the following conditions:
lorcansmith 0:2a53a4c3238c 11
lorcansmith 0:2a53a4c3238c 12 The above copyright notice and this permission notice shall be included in
lorcansmith 0:2a53a4c3238c 13 all copies or substantial portions of the Software.
lorcansmith 0:2a53a4c3238c 14
lorcansmith 0:2a53a4c3238c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
lorcansmith 0:2a53a4c3238c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
lorcansmith 0:2a53a4c3238c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
lorcansmith 0:2a53a4c3238c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
lorcansmith 0:2a53a4c3238c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lorcansmith 0:2a53a4c3238c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
lorcansmith 0:2a53a4c3238c 21 THE SOFTWARE.
lorcansmith 0:2a53a4c3238c 22 */
lorcansmith 0:2a53a4c3238c 23
lorcansmith 0:2a53a4c3238c 24 #include "EthernetNetIf.h"
lorcansmith 0:2a53a4c3238c 25
lorcansmith 0:2a53a4c3238c 26 #include "netif/etharp.h"
lorcansmith 0:2a53a4c3238c 27 #include "lwip/dhcp.h"
lorcansmith 0:2a53a4c3238c 28 #include "lwip/dns.h"
lorcansmith 0:2a53a4c3238c 29 #include "lwip/igmp.h"
lorcansmith 0:2a53a4c3238c 30
lorcansmith 0:2a53a4c3238c 31 #include "drv/eth/eth_drv.h"
lorcansmith 0:2a53a4c3238c 32 #include "mbed.h"
lorcansmith 0:2a53a4c3238c 33
lorcansmith 0:2a53a4c3238c 34 #define __DEBUG
lorcansmith 0:2a53a4c3238c 35 #include "dbg/dbg.h"
lorcansmith 0:2a53a4c3238c 36
lorcansmith 0:2a53a4c3238c 37 #include "netCfg.h"
lorcansmith 0:2a53a4c3238c 38 #if NET_ETH
lorcansmith 0:2a53a4c3238c 39
lorcansmith 0:2a53a4c3238c 40 EthernetNetIf::EthernetNetIf() : LwipNetIf(), m_ethArpTimer(), m_dhcpCoarseTimer(), m_dhcpFineTimer(), m_igmpTimer(), m_pNetIf(NULL),
lorcansmith 0:2a53a4c3238c 41 m_netmask(255,255,255,255), m_gateway(), m_hostname(NULL)
lorcansmith 0:2a53a4c3238c 42 {
lorcansmith 0:2a53a4c3238c 43 m_hostname = NULL;
lorcansmith 0:2a53a4c3238c 44 m_pNetIf = new netif;
lorcansmith 0:2a53a4c3238c 45 m_useDhcp = true;
lorcansmith 0:2a53a4c3238c 46 }
lorcansmith 0:2a53a4c3238c 47
lorcansmith 0:2a53a4c3238c 48 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
lorcansmith 0:2a53a4c3238c 49 {
lorcansmith 0:2a53a4c3238c 50 m_hostname = NULL;
lorcansmith 0:2a53a4c3238c 51 m_netmask = netmask;
lorcansmith 0:2a53a4c3238c 52 m_gateway = gateway;
lorcansmith 0:2a53a4c3238c 53 m_ip = ip;
lorcansmith 0:2a53a4c3238c 54 m_pNetIf = new netif;
lorcansmith 0:2a53a4c3238c 55 dns_setserver(0, &dns.getStruct());
lorcansmith 0:2a53a4c3238c 56 m_useDhcp = false;
lorcansmith 0:2a53a4c3238c 57 }
lorcansmith 0:2a53a4c3238c 58
lorcansmith 0:2a53a4c3238c 59 EthernetNetIf::~EthernetNetIf()
lorcansmith 0:2a53a4c3238c 60 {
lorcansmith 0:2a53a4c3238c 61 if(m_pNetIf)
lorcansmith 0:2a53a4c3238c 62 {
lorcansmith 0:2a53a4c3238c 63 igmp_stop(m_pNetIf); //Stop IGMP processing
lorcansmith 0:2a53a4c3238c 64 netif_set_down(m_pNetIf);
lorcansmith 0:2a53a4c3238c 65 netif_remove(m_pNetIf);
lorcansmith 0:2a53a4c3238c 66 delete m_pNetIf;
lorcansmith 0:2a53a4c3238c 67 eth_free();
lorcansmith 0:2a53a4c3238c 68 }
lorcansmith 0:2a53a4c3238c 69 }
lorcansmith 0:2a53a4c3238c 70
lorcansmith 0:2a53a4c3238c 71 EthernetErr EthernetNetIf::setup(int timeout_ms /*= 15000*/)
lorcansmith 0:2a53a4c3238c 72 {
lorcansmith 0:2a53a4c3238c 73 LwipNetIf::init();
lorcansmith 0:2a53a4c3238c 74 //m_ethArpTicker.attach_us(&etharp_tmr, ARP_TMR_INTERVAL * 1000); // = 5s in etharp.h
lorcansmith 0:2a53a4c3238c 75 m_ethArpTimer.start();
lorcansmith 0:2a53a4c3238c 76 if(m_useDhcp)
lorcansmith 0:2a53a4c3238c 77 {
lorcansmith 0:2a53a4c3238c 78 //m_dhcpCoarseTicker.attach(&dhcp_coarse_tmr, DHCP_COARSE_TIMER_SECS); // = 60s in dhcp.h
lorcansmith 0:2a53a4c3238c 79 //m_dhcpFineTicker.attach_us(&dhcp_fine_tmr, DHCP_FINE_TIMER_MSECS * 1000); // = 500ms in dhcp.h
lorcansmith 0:2a53a4c3238c 80 m_dhcpCoarseTimer.start();
lorcansmith 0:2a53a4c3238c 81 m_dhcpFineTimer.start();
lorcansmith 0:2a53a4c3238c 82 }
lorcansmith 0:2a53a4c3238c 83 m_pNetIf->hwaddr_len = ETHARP_HWADDR_LEN; //6
lorcansmith 0:2a53a4c3238c 84 eth_address((char *)m_pNetIf->hwaddr);
lorcansmith 0:2a53a4c3238c 85
lorcansmith 0:2a53a4c3238c 86 DBG("HW Addr is : %02x:%02x:%02x:%02x:%02x:%02x.\n",
lorcansmith 0:2a53a4c3238c 87 m_pNetIf->hwaddr[0], m_pNetIf->hwaddr[1], m_pNetIf->hwaddr[2],
lorcansmith 0:2a53a4c3238c 88 m_pNetIf->hwaddr[3], m_pNetIf->hwaddr[4], m_pNetIf->hwaddr[5]);
lorcansmith 0:2a53a4c3238c 89
lorcansmith 0:2a53a4c3238c 90 m_pNetIf = netif_add(m_pNetIf, &(m_ip.getStruct()), &(m_netmask.getStruct()), &(m_gateway.getStruct()), NULL, eth_init, ip_input);//ethernet_input);// ip_input);
lorcansmith 0:2a53a4c3238c 91 //m_pNetIf->hostname = "mbedDG";//(char *)m_hostname; //Not used for now
lorcansmith 0:2a53a4c3238c 92 netif_set_default(m_pNetIf);
lorcansmith 0:2a53a4c3238c 93
lorcansmith 0:2a53a4c3238c 94 //DBG("\r\nStarting DHCP.\r\n");
lorcansmith 0:2a53a4c3238c 95
lorcansmith 0:2a53a4c3238c 96 if(m_useDhcp)
lorcansmith 0:2a53a4c3238c 97 {
lorcansmith 0:2a53a4c3238c 98 dhcp_start(m_pNetIf);
lorcansmith 0:2a53a4c3238c 99 DBG("DHCP Started, waiting for IP...\n");
lorcansmith 0:2a53a4c3238c 100 }
lorcansmith 0:2a53a4c3238c 101 else
lorcansmith 0:2a53a4c3238c 102 {
lorcansmith 0:2a53a4c3238c 103 netif_set_up(m_pNetIf);
lorcansmith 0:2a53a4c3238c 104 }
lorcansmith 0:2a53a4c3238c 105
lorcansmith 0:2a53a4c3238c 106 Timer timeout;
lorcansmith 0:2a53a4c3238c 107 timeout.start();
lorcansmith 0:2a53a4c3238c 108 while( !netif_is_up(m_pNetIf) ) //Wait until device is up
lorcansmith 0:2a53a4c3238c 109 {
lorcansmith 0:2a53a4c3238c 110 if(m_useDhcp)
lorcansmith 0:2a53a4c3238c 111 {
lorcansmith 0:2a53a4c3238c 112 if(m_dhcpFineTimer.read_ms()>=DHCP_FINE_TIMER_MSECS)
lorcansmith 0:2a53a4c3238c 113 {
lorcansmith 0:2a53a4c3238c 114 m_dhcpFineTimer.reset();
lorcansmith 0:2a53a4c3238c 115 dhcp_fine_tmr();
lorcansmith 0:2a53a4c3238c 116 }
lorcansmith 0:2a53a4c3238c 117 if(m_dhcpCoarseTimer.read()>=DHCP_COARSE_TIMER_SECS)
lorcansmith 0:2a53a4c3238c 118 {
lorcansmith 0:2a53a4c3238c 119 m_dhcpCoarseTimer.reset();
lorcansmith 0:2a53a4c3238c 120 dhcp_coarse_tmr();
lorcansmith 0:2a53a4c3238c 121 }
lorcansmith 0:2a53a4c3238c 122 }
lorcansmith 0:2a53a4c3238c 123 poll();
lorcansmith 0:2a53a4c3238c 124 if( timeout.read_ms() > timeout_ms )
lorcansmith 0:2a53a4c3238c 125 {
lorcansmith 0:2a53a4c3238c 126 //Abort
lorcansmith 0:2a53a4c3238c 127 if(m_useDhcp)
lorcansmith 0:2a53a4c3238c 128 dhcp_stop(m_pNetIf);
lorcansmith 0:2a53a4c3238c 129 else
lorcansmith 0:2a53a4c3238c 130 netif_set_down(m_pNetIf);
lorcansmith 0:2a53a4c3238c 131 DBG("\r\nTimeout.\r\n");
lorcansmith 0:2a53a4c3238c 132 return ETH_TIMEOUT;
lorcansmith 0:2a53a4c3238c 133 }
lorcansmith 0:2a53a4c3238c 134 }
lorcansmith 0:2a53a4c3238c 135
lorcansmith 0:2a53a4c3238c 136 #if LWIP_IGMP
lorcansmith 0:2a53a4c3238c 137 igmp_start(m_pNetIf); //Start IGMP processing
lorcansmith 0:2a53a4c3238c 138 #endif
lorcansmith 0:2a53a4c3238c 139
lorcansmith 0:2a53a4c3238c 140 m_ip = IpAddr(&(m_pNetIf->ip_addr));
lorcansmith 0:2a53a4c3238c 141
lorcansmith 0:2a53a4c3238c 142 DBG("Connected, IP : %d.%d.%d.%d\n", m_ip[0], m_ip[1], m_ip[2], m_ip[3]);
lorcansmith 0:2a53a4c3238c 143
lorcansmith 0:2a53a4c3238c 144 return ETH_OK;
lorcansmith 0:2a53a4c3238c 145 }
lorcansmith 0:2a53a4c3238c 146
lorcansmith 0:2a53a4c3238c 147 void EthernetNetIf::poll()
lorcansmith 0:2a53a4c3238c 148 {
lorcansmith 0:2a53a4c3238c 149 if(m_ethArpTimer.read_ms()>=ARP_TMR_INTERVAL)
lorcansmith 0:2a53a4c3238c 150 {
lorcansmith 0:2a53a4c3238c 151 m_ethArpTimer.reset();
lorcansmith 0:2a53a4c3238c 152 etharp_tmr();
lorcansmith 0:2a53a4c3238c 153 }
lorcansmith 0:2a53a4c3238c 154 #if LWIP_IGMP
lorcansmith 0:2a53a4c3238c 155 if(m_igmpTimer.read_ms()>=IGMP_TMR_INTERVAL)
lorcansmith 0:2a53a4c3238c 156 {
lorcansmith 0:2a53a4c3238c 157 m_igmpTimer.reset();
lorcansmith 0:2a53a4c3238c 158 igmp_tmr();
lorcansmith 0:2a53a4c3238c 159 }
lorcansmith 0:2a53a4c3238c 160 #endif
lorcansmith 0:2a53a4c3238c 161 LwipNetIf::poll();
lorcansmith 0:2a53a4c3238c 162 eth_poll();
lorcansmith 0:2a53a4c3238c 163 }
lorcansmith 0:2a53a4c3238c 164
lorcansmith 0:2a53a4c3238c 165 #endif