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
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 "PPPNetIf.h"
andrewbonney 0:ec559500a63f 25 #include "mbed.h"
andrewbonney 0:ec559500a63f 26 #include "ppp/ppp.h"
andrewbonney 0:ec559500a63f 27 #include "lwip/init.h"
andrewbonney 0:ec559500a63f 28 #include "lwip/sio.h"
andrewbonney 0:ec559500a63f 29
andrewbonney 0:ec559500a63f 30 #define __DEBUG
andrewbonney 0:ec559500a63f 31 #include "dbg/dbg.h"
andrewbonney 0:ec559500a63f 32
andrewbonney 0:ec559500a63f 33 #include "netCfg.h"
andrewbonney 0:ec559500a63f 34 #if NET_PPP
andrewbonney 0:ec559500a63f 35
andrewbonney 0:ec559500a63f 36 #define PPP_TIMEOUT 60000
andrewbonney 0:ec559500a63f 37
andrewbonney 0:ec559500a63f 38 #define BUF_SIZE 256
andrewbonney 0:ec559500a63f 39
andrewbonney 0:ec559500a63f 40 PPPNetIf::PPPNetIf(GPRSModem* pIf) : LwipNetIf(), m_pIf(pIf),/* m_open(false),*/ m_connected(false), m_status(PPP_DISCONNECTED), m_fd(0) //, m_id(0)
andrewbonney 0:ec559500a63f 41 {
andrewbonney 0:ec559500a63f 42 //FIXME: Check static refcount
andrewbonney 0:ec559500a63f 43 m_buf = new uint8_t[BUF_SIZE];
andrewbonney 0:ec559500a63f 44 }
andrewbonney 0:ec559500a63f 45
andrewbonney 0:ec559500a63f 46 PPPNetIf::~PPPNetIf()
andrewbonney 0:ec559500a63f 47 {
andrewbonney 0:ec559500a63f 48 delete[] m_buf;
andrewbonney 0:ec559500a63f 49 }
andrewbonney 0:ec559500a63f 50
andrewbonney 0:ec559500a63f 51 #if 0
andrewbonney 0:ec559500a63f 52 PPPErr PPPNetIf::open(Serial* pSerial)
andrewbonney 0:ec559500a63f 53 {
andrewbonney 0:ec559500a63f 54 GPRSErr err = m_pIf->open(pSerial);
andrewbonney 0:ec559500a63f 55 if(err)
andrewbonney 0:ec559500a63f 56 return PPP_MODEM;
andrewbonney 0:ec559500a63f 57 m_open = true;
andrewbonney 0:ec559500a63f 58 #if 0
andrewbonney 0:ec559500a63f 59 m_id = sioMgr::registerSerialIf(this);
andrewbonney 0:ec559500a63f 60 if(!m_id)
andrewbonney 0:ec559500a63f 61 {
andrewbonney 0:ec559500a63f 62 close();
andrewbonney 0:ec559500a63f 63 return PPP_CLOSED;
andrewbonney 0:ec559500a63f 64 }
andrewbonney 0:ec559500a63f 65 #endif
andrewbonney 0:ec559500a63f 66 return PPP_OK;
andrewbonney 0:ec559500a63f 67 }
andrewbonney 0:ec559500a63f 68 #endif
andrewbonney 0:ec559500a63f 69
andrewbonney 0:ec559500a63f 70
andrewbonney 0:ec559500a63f 71 PPPErr PPPNetIf::GPRSConnect(const char* apn, const char* userId, const char* password) //Connect using GPRS
andrewbonney 0:ec559500a63f 72 {
andrewbonney 0:ec559500a63f 73 LwipNetIf::init();
andrewbonney 0:ec559500a63f 74 pppInit();
andrewbonney 0:ec559500a63f 75 //TODO: Tell ATIf that we get ownership of the serial port
andrewbonney 0:ec559500a63f 76
andrewbonney 0:ec559500a63f 77 GPRSErr gprsErr;
andrewbonney 0:ec559500a63f 78 gprsErr = m_pIf->connect(apn);
andrewbonney 0:ec559500a63f 79 if(gprsErr)
andrewbonney 0:ec559500a63f 80 return PPP_NETWORK;
andrewbonney 0:ec559500a63f 81
andrewbonney 0:ec559500a63f 82 DBG("PPPNetIf: If Connected.\n");
andrewbonney 0:ec559500a63f 83
andrewbonney 0:ec559500a63f 84 if( userId == NULL )
andrewbonney 0:ec559500a63f 85 pppSetAuth(PPPAUTHTYPE_NONE, NULL, NULL);
andrewbonney 0:ec559500a63f 86 else
andrewbonney 0:ec559500a63f 87 pppSetAuth(PPPAUTHTYPE_PAP, userId, password); //TODO: Allow CHAP as well
andrewbonney 0:ec559500a63f 88
andrewbonney 0:ec559500a63f 89 DBG("PPPNetIf: Set Auth.\n");
andrewbonney 0:ec559500a63f 90
andrewbonney 0:ec559500a63f 91 //wait(1.);
andrewbonney 0:ec559500a63f 92
andrewbonney 0:ec559500a63f 93 //m_pIf->flushBuffer(); //Flush buffer before passing serial port to PPP
andrewbonney 0:ec559500a63f 94
andrewbonney 0:ec559500a63f 95 m_status = PPP_CONNECTING;
andrewbonney 0:ec559500a63f 96 DBG("m_pIf = %p\n", m_pIf);
andrewbonney 0:ec559500a63f 97 int res = pppOverSerialOpen((void*)m_pIf, sPppCallback, (void*)this);
andrewbonney 0:ec559500a63f 98 DBG("PPP connected\n");
andrewbonney 0:ec559500a63f 99 if(res<0)
andrewbonney 0:ec559500a63f 100 {
andrewbonney 0:ec559500a63f 101 disconnect();
andrewbonney 0:ec559500a63f 102 return PPP_PROTOCOL;
andrewbonney 0:ec559500a63f 103 }
andrewbonney 0:ec559500a63f 104
andrewbonney 0:ec559500a63f 105 DBG("PPPNetIf: PPP Started with res = %d.\n", res);
andrewbonney 0:ec559500a63f 106
andrewbonney 0:ec559500a63f 107 m_fd = res;
andrewbonney 0:ec559500a63f 108 m_connected = true;
andrewbonney 0:ec559500a63f 109 Timer t;
andrewbonney 0:ec559500a63f 110 t.start();
andrewbonney 0:ec559500a63f 111 while( m_status == PPP_CONNECTING ) //Wait for callback
andrewbonney 0:ec559500a63f 112 {
andrewbonney 0:ec559500a63f 113 poll();
andrewbonney 0:ec559500a63f 114 if(t.read_ms()>PPP_TIMEOUT)
andrewbonney 0:ec559500a63f 115 {
andrewbonney 0:ec559500a63f 116 DBG("PPPNetIf: Timeout.\n");
andrewbonney 0:ec559500a63f 117 disconnect();
andrewbonney 0:ec559500a63f 118 return PPP_PROTOCOL;
andrewbonney 0:ec559500a63f 119 }
andrewbonney 0:ec559500a63f 120 }
andrewbonney 0:ec559500a63f 121
andrewbonney 0:ec559500a63f 122 DBG("PPPNetIf: Callback returned.\n");
andrewbonney 0:ec559500a63f 123
andrewbonney 0:ec559500a63f 124 if( m_status == PPP_DISCONNECTED )
andrewbonney 0:ec559500a63f 125 {
andrewbonney 0:ec559500a63f 126 disconnect();
andrewbonney 0:ec559500a63f 127 return PPP_PROTOCOL;
andrewbonney 0:ec559500a63f 128 }
andrewbonney 0:ec559500a63f 129
andrewbonney 0:ec559500a63f 130 return PPP_OK;
andrewbonney 0:ec559500a63f 131
andrewbonney 0:ec559500a63f 132 }
andrewbonney 0:ec559500a63f 133
andrewbonney 0:ec559500a63f 134 PPPErr PPPNetIf::ATConnect(const char* number) //Connect using a "classic" voice modem or GSM
andrewbonney 0:ec559500a63f 135 {
andrewbonney 0:ec559500a63f 136 //TODO: IMPL
andrewbonney 0:ec559500a63f 137 return PPP_MODEM;
andrewbonney 0:ec559500a63f 138 }
andrewbonney 0:ec559500a63f 139
andrewbonney 0:ec559500a63f 140 PPPErr PPPNetIf::disconnect()
andrewbonney 0:ec559500a63f 141 {
andrewbonney 0:ec559500a63f 142 if(m_fd)
andrewbonney 0:ec559500a63f 143 pppClose(m_fd); //0 if ok, else should gen a WARN
andrewbonney 0:ec559500a63f 144 m_connected = false;
andrewbonney 0:ec559500a63f 145
andrewbonney 0:ec559500a63f 146 m_pIf->flushBuffer();
andrewbonney 0:ec559500a63f 147 m_pIf->printf("+++\r\n");
andrewbonney 0:ec559500a63f 148 wait(.5);
andrewbonney 0:ec559500a63f 149 m_pIf->flushBuffer();
andrewbonney 0:ec559500a63f 150
andrewbonney 0:ec559500a63f 151 GPRSErr gprsErr;
andrewbonney 0:ec559500a63f 152 gprsErr = m_pIf->disconnect();
andrewbonney 0:ec559500a63f 153 if(gprsErr)
andrewbonney 0:ec559500a63f 154 return PPP_NETWORK;
andrewbonney 0:ec559500a63f 155
andrewbonney 0:ec559500a63f 156 return PPP_OK;
andrewbonney 0:ec559500a63f 157 }
andrewbonney 0:ec559500a63f 158
andrewbonney 0:ec559500a63f 159 #if 0
andrewbonney 0:ec559500a63f 160 PPPErr PPPNetIf::close()
andrewbonney 0:ec559500a63f 161 {
andrewbonney 0:ec559500a63f 162 GPRSErr err = m_pIf->close();
andrewbonney 0:ec559500a63f 163 if(err)
andrewbonney 0:ec559500a63f 164 return PPP_MODEM;
andrewbonney 0:ec559500a63f 165 m_open = false;
andrewbonney 0:ec559500a63f 166 return PPP_OK;
andrewbonney 0:ec559500a63f 167 }
andrewbonney 0:ec559500a63f 168 #endif
andrewbonney 0:ec559500a63f 169
andrewbonney 0:ec559500a63f 170
andrewbonney 0:ec559500a63f 171 #if 0
andrewbonney 0:ec559500a63f 172 //We have to use :
andrewbonney 0:ec559500a63f 173
andrewbonney 0:ec559500a63f 174 /** Pass received raw characters to PPPoS to be decoded. This function is
andrewbonney 0:ec559500a63f 175 * thread-safe and can be called from a dedicated RX-thread or from a main-loop.
andrewbonney 0:ec559500a63f 176 *
andrewbonney 0:ec559500a63f 177 * @param pd PPP descriptor index, returned by pppOpen()
andrewbonney 0:ec559500a63f 178 * @param data received data
andrewbonney 0:ec559500a63f 179 * @param len length of received data
andrewbonney 0:ec559500a63f 180 */
andrewbonney 0:ec559500a63f 181 void
andrewbonney 0:ec559500a63f 182 pppos_input(int pd, u_char* data, int len)
andrewbonney 0:ec559500a63f 183 {
andrewbonney 0:ec559500a63f 184 pppInProc(&pppControl[pd].rx, data, len);
andrewbonney 0:ec559500a63f 185 }
andrewbonney 0:ec559500a63f 186 #endif
andrewbonney 0:ec559500a63f 187
andrewbonney 0:ec559500a63f 188 void PPPNetIf::poll()
andrewbonney 0:ec559500a63f 189 {
andrewbonney 0:ec559500a63f 190 if(!m_connected)
andrewbonney 0:ec559500a63f 191 return;
andrewbonney 0:ec559500a63f 192 LwipNetIf::poll();
andrewbonney 0:ec559500a63f 193 //static u8_t buf[128];
andrewbonney 0:ec559500a63f 194 int len;
andrewbonney 0:ec559500a63f 195 do
andrewbonney 0:ec559500a63f 196 {
andrewbonney 0:ec559500a63f 197 len = sio_tryread((sio_fd_t) m_pIf, m_buf, BUF_SIZE);
andrewbonney 0:ec559500a63f 198 if(len > 0)
andrewbonney 0:ec559500a63f 199 pppos_input(m_fd, m_buf, len);
andrewbonney 0:ec559500a63f 200 } while(len>0);
andrewbonney 0:ec559500a63f 201 }
andrewbonney 0:ec559500a63f 202
andrewbonney 0:ec559500a63f 203 //Link Callback
andrewbonney 0:ec559500a63f 204 void PPPNetIf::pppCallback(int errCode, void *arg)
andrewbonney 0:ec559500a63f 205 {
andrewbonney 0:ec559500a63f 206 switch ( errCode )
andrewbonney 0:ec559500a63f 207 {
andrewbonney 0:ec559500a63f 208 //No error
andrewbonney 0:ec559500a63f 209 case PPPERR_NONE:
andrewbonney 0:ec559500a63f 210 {
andrewbonney 0:ec559500a63f 211 struct ppp_addrs* addrs = (struct ppp_addrs*) arg;
andrewbonney 0:ec559500a63f 212 m_ip = IpAddr(&(addrs->our_ipaddr)); //Set IP
andrewbonney 0:ec559500a63f 213 }
andrewbonney 0:ec559500a63f 214 m_status = PPP_CONNECTED;
andrewbonney 0:ec559500a63f 215 break;
andrewbonney 0:ec559500a63f 216 default:
andrewbonney 0:ec559500a63f 217 //Disconnected
andrewbonney 0:ec559500a63f 218 DBG("PPPNetIf: Callback errCode = %d.\n", errCode);
andrewbonney 0:ec559500a63f 219 m_status = PPP_DISCONNECTED;
andrewbonney 0:ec559500a63f 220 break;
andrewbonney 0:ec559500a63f 221 }
andrewbonney 0:ec559500a63f 222 }
andrewbonney 0:ec559500a63f 223
andrewbonney 0:ec559500a63f 224 #endif