Copy of NetServicesMin with the HTTP Client library. Includes modification for HTTP servers which send the HTTP status line in its own packet.

Committer:
andrewbonney
Date:
Thu May 26 10:02:40 2011 +0000
Revision:
0:18dd877d2c77

        

Who changed what in which revision?

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