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 /*
andrewbonney 0:ec559500a63f 25
andrewbonney 0:ec559500a63f 26 This is a wrapper around Serial if for lwIP (acts as a serial driver)
andrewbonney 0:ec559500a63f 27
andrewbonney 0:ec559500a63f 28 See sio.h for functions to be implemented
andrewbonney 0:ec559500a63f 29
andrewbonney 0:ec559500a63f 30 sio_fd_t is a void* defined type, we use it as a SerialBuf ptr
andrewbonney 0:ec559500a63f 31
andrewbonney 0:ec559500a63f 32
andrewbonney 0:ec559500a63f 33
andrewbonney 0:ec559500a63f 34 */
andrewbonney 0:ec559500a63f 35
andrewbonney 0:ec559500a63f 36
andrewbonney 0:ec559500a63f 37 #include "netCfg.h"
andrewbonney 0:ec559500a63f 38 #if NET_PPP
andrewbonney 0:ec559500a63f 39
andrewbonney 0:ec559500a63f 40 //#define MAX_SERIAL_PORTS 8
andrewbonney 0:ec559500a63f 41
andrewbonney 0:ec559500a63f 42 #include "lwip/sio.h"
andrewbonney 0:ec559500a63f 43 #include "mbed.h"
andrewbonney 0:ec559500a63f 44 //#include "sioMgr.h"
andrewbonney 0:ec559500a63f 45 #include "drv/serial/buf/SerialBuf.h"
andrewbonney 0:ec559500a63f 46
andrewbonney 0:ec559500a63f 47 //#define __DEBUG
andrewbonney 0:ec559500a63f 48 #include "dbg/dbg.h"
andrewbonney 0:ec559500a63f 49
andrewbonney 0:ec559500a63f 50 //extern "C" {
andrewbonney 0:ec559500a63f 51
andrewbonney 0:ec559500a63f 52 /**
andrewbonney 0:ec559500a63f 53 * Opens a serial device for communication.
andrewbonney 0:ec559500a63f 54 *
andrewbonney 0:ec559500a63f 55 * @param devnum device number
andrewbonney 0:ec559500a63f 56 * @return handle to serial device if successful, NULL otherwise
andrewbonney 0:ec559500a63f 57 */
andrewbonney 0:ec559500a63f 58 sio_fd_t sio_open(u8_t devnum)
andrewbonney 0:ec559500a63f 59 {
andrewbonney 0:ec559500a63f 60 #if 0
andrewbonney 0:ec559500a63f 61 SerialBuf* pIf = SioMgr::getIf(devnum);
andrewbonney 0:ec559500a63f 62 if(pIf == NULL)
andrewbonney 0:ec559500a63f 63 return NULL;
andrewbonney 0:ec559500a63f 64
andrewbonney 0:ec559500a63f 65 //Got a SerialBuf* object
andrewbonney 0:ec559500a63f 66 //WARN: It HAS to be initialised (instanciated + attached to a Serial obj)
andrewbonney 0:ec559500a63f 67
andrewbonney 0:ec559500a63f 68 return (sio_fd_t) pIf;
andrewbonney 0:ec559500a63f 69 #endif
andrewbonney 0:ec559500a63f 70 return NULL;
andrewbonney 0:ec559500a63f 71 }
andrewbonney 0:ec559500a63f 72
andrewbonney 0:ec559500a63f 73 /**
andrewbonney 0:ec559500a63f 74 * Sends a single character to the serial device.
andrewbonney 0:ec559500a63f 75 *
andrewbonney 0:ec559500a63f 76 * @param c character to send
andrewbonney 0:ec559500a63f 77 * @param fd serial device handle
andrewbonney 0:ec559500a63f 78 *
andrewbonney 0:ec559500a63f 79 * @note This function will block until the character can be sent.
andrewbonney 0:ec559500a63f 80 */
andrewbonney 0:ec559500a63f 81 void sio_send(u8_t c, sio_fd_t fd)
andrewbonney 0:ec559500a63f 82 {
andrewbonney 0:ec559500a63f 83 SerialBuf* pIf = (SerialBuf*) fd;
andrewbonney 0:ec559500a63f 84 //while(!pIf->writeable());
andrewbonney 0:ec559500a63f 85 pIf->putc( (char) c );
andrewbonney 0:ec559500a63f 86 }
andrewbonney 0:ec559500a63f 87
andrewbonney 0:ec559500a63f 88 /**
andrewbonney 0:ec559500a63f 89 * Receives a single character from the serial device.
andrewbonney 0:ec559500a63f 90 *
andrewbonney 0:ec559500a63f 91 * @param fd serial device handle
andrewbonney 0:ec559500a63f 92 *
andrewbonney 0:ec559500a63f 93 * @note This function will block until a character is received.
andrewbonney 0:ec559500a63f 94 */
andrewbonney 0:ec559500a63f 95 u8_t sio_recv(sio_fd_t fd)
andrewbonney 0:ec559500a63f 96 {
andrewbonney 0:ec559500a63f 97 SerialBuf* pIf = (SerialBuf*) fd;
andrewbonney 0:ec559500a63f 98 pIf->setReadMode(false);
andrewbonney 0:ec559500a63f 99 while(!pIf->readable());
andrewbonney 0:ec559500a63f 100 return (u8_t) pIf->getc();
andrewbonney 0:ec559500a63f 101 }
andrewbonney 0:ec559500a63f 102
andrewbonney 0:ec559500a63f 103 /**
andrewbonney 0:ec559500a63f 104 * Reads from the serial device.
andrewbonney 0:ec559500a63f 105 *
andrewbonney 0:ec559500a63f 106 * @param fd serial device handle
andrewbonney 0:ec559500a63f 107 * @param data pointer to data buffer for receiving
andrewbonney 0:ec559500a63f 108 * @param len maximum length (in bytes) of data to receive
andrewbonney 0:ec559500a63f 109 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
andrewbonney 0:ec559500a63f 110 *
andrewbonney 0:ec559500a63f 111 * @note This function will block until data can be received. The blocking
andrewbonney 0:ec559500a63f 112 * can be cancelled by calling sio_read_abort().
andrewbonney 0:ec559500a63f 113 */
andrewbonney 0:ec559500a63f 114 static volatile bool m_abort = false;
andrewbonney 0:ec559500a63f 115 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
andrewbonney 0:ec559500a63f 116 {
andrewbonney 0:ec559500a63f 117 u32_t recvd = 0; //bytes received
andrewbonney 0:ec559500a63f 118 SerialBuf* pIf = (SerialBuf*) fd;
andrewbonney 0:ec559500a63f 119 pIf->setReadMode(false);
andrewbonney 0:ec559500a63f 120 while(!m_abort && len)
andrewbonney 0:ec559500a63f 121 {
andrewbonney 0:ec559500a63f 122 while(!pIf->readable());
andrewbonney 0:ec559500a63f 123 *data = (u8_t) pIf->getc();
andrewbonney 0:ec559500a63f 124 data++;
andrewbonney 0:ec559500a63f 125 len--;
andrewbonney 0:ec559500a63f 126 recvd++;
andrewbonney 0:ec559500a63f 127 }
andrewbonney 0:ec559500a63f 128 m_abort = false;
andrewbonney 0:ec559500a63f 129 return recvd;
andrewbonney 0:ec559500a63f 130 }
andrewbonney 0:ec559500a63f 131
andrewbonney 0:ec559500a63f 132 /**
andrewbonney 0:ec559500a63f 133 * Tries to read from the serial device. Same as sio_read but returns
andrewbonney 0:ec559500a63f 134 * immediately if no data is available and never blocks.
andrewbonney 0:ec559500a63f 135 *
andrewbonney 0:ec559500a63f 136 * @param fd serial device handle
andrewbonney 0:ec559500a63f 137 * @param data pointer to data buffer for receiving
andrewbonney 0:ec559500a63f 138 * @param len maximum length (in bytes) of data to receive
andrewbonney 0:ec559500a63f 139 * @return number of bytes actually received
andrewbonney 0:ec559500a63f 140 */
andrewbonney 0:ec559500a63f 141 u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
andrewbonney 0:ec559500a63f 142 {
andrewbonney 0:ec559500a63f 143 u32_t recvd = 0; //bytes received
andrewbonney 0:ec559500a63f 144 SerialBuf* pIf = (SerialBuf*) fd;
andrewbonney 0:ec559500a63f 145 pIf->setReadMode(false);
andrewbonney 0:ec559500a63f 146 while(len)
andrewbonney 0:ec559500a63f 147 {
andrewbonney 0:ec559500a63f 148 /* if(!pIf->readable())
andrewbonney 0:ec559500a63f 149 {
andrewbonney 0:ec559500a63f 150 wait_ms(4);
andrewbonney 0:ec559500a63f 151 }*/
andrewbonney 0:ec559500a63f 152 if(!pIf->readable())
andrewbonney 0:ec559500a63f 153 {
andrewbonney 0:ec559500a63f 154 return recvd;
andrewbonney 0:ec559500a63f 155 }
andrewbonney 0:ec559500a63f 156 *data = (u8_t) pIf->getc();
andrewbonney 0:ec559500a63f 157 data++;
andrewbonney 0:ec559500a63f 158 len--;
andrewbonney 0:ec559500a63f 159 recvd++;
andrewbonney 0:ec559500a63f 160 }
andrewbonney 0:ec559500a63f 161 return recvd;
andrewbonney 0:ec559500a63f 162 }
andrewbonney 0:ec559500a63f 163
andrewbonney 0:ec559500a63f 164 /**
andrewbonney 0:ec559500a63f 165 * Writes to the serial device.
andrewbonney 0:ec559500a63f 166 *
andrewbonney 0:ec559500a63f 167 * @param fd serial device handle
andrewbonney 0:ec559500a63f 168 * @param data pointer to data to send
andrewbonney 0:ec559500a63f 169 * @param len length (in bytes) of data to send
andrewbonney 0:ec559500a63f 170 * @return number of bytes actually sent
andrewbonney 0:ec559500a63f 171 *
andrewbonney 0:ec559500a63f 172 * @note This function will block until all data can be sent.
andrewbonney 0:ec559500a63f 173 */
andrewbonney 0:ec559500a63f 174 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
andrewbonney 0:ec559500a63f 175 {
andrewbonney 0:ec559500a63f 176 u32_t sent = 0; //bytes sent
andrewbonney 0:ec559500a63f 177 SerialBuf* pIf = (SerialBuf*) fd;
andrewbonney 0:ec559500a63f 178 while(len)
andrewbonney 0:ec559500a63f 179 {
andrewbonney 0:ec559500a63f 180 while(!pIf->writeable());
andrewbonney 0:ec559500a63f 181 pIf->putc(*data);
andrewbonney 0:ec559500a63f 182 data++;
andrewbonney 0:ec559500a63f 183 len--;
andrewbonney 0:ec559500a63f 184 sent++;
andrewbonney 0:ec559500a63f 185 }
andrewbonney 0:ec559500a63f 186 return sent; //Well, this is bound to be len if no interrupt mechanism
andrewbonney 0:ec559500a63f 187 }
andrewbonney 0:ec559500a63f 188
andrewbonney 0:ec559500a63f 189 /**
andrewbonney 0:ec559500a63f 190 * Aborts a blocking sio_read() call.
andrewbonney 0:ec559500a63f 191 *
andrewbonney 0:ec559500a63f 192 * @param fd serial device handle
andrewbonney 0:ec559500a63f 193 */
andrewbonney 0:ec559500a63f 194 void sio_read_abort(sio_fd_t fd)
andrewbonney 0:ec559500a63f 195 {
andrewbonney 0:ec559500a63f 196 m_abort = true;
andrewbonney 0:ec559500a63f 197 }
andrewbonney 0:ec559500a63f 198
andrewbonney 0:ec559500a63f 199 //}
andrewbonney 0:ec559500a63f 200
andrewbonney 0:ec559500a63f 201 #endif
andrewbonney 0:ec559500a63f 202