Modified version of NetServices. Fixes an issue where connections failed should the HTTP response status line be received in a packet on its own prior to any further headers. Changes are made to the HTTPClient.cpp file's readHeaders method.

Committer:
andrewbonney
Date:
Fri Apr 08 14:39:41 2011 +0000
Revision:
0:ec559500a63f

        

Who changed what in which revision?

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