Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 11 16:07:34 2014 +0000
Revision:
5:6878defcfeb9
Parent:
0:f615d151a72c
Project updated belastingslijn

Who changed what in which revision?

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