modded version Dirk-Willem van Gulik's Bonjour/Zerconf library http://mbed.org/users/dirkx/code/Bonjour/

Dependents:   OSCtoCVConverter

Fork of Bonjour by Dirk-Willem van Gulik (NXP/mbed)

Committer:
casiotone401
Date:
Thu Oct 16 14:13:21 2014 +0000
Revision:
8:275256b5d807
Parent:
0:355018f44c9f
minor change

Who changed what in which revision?

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