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 "rpc.h"
andrewbonney 0:ec559500a63f 25
andrewbonney 0:ec559500a63f 26 #include "UsbSerial.h"
andrewbonney 0:ec559500a63f 27
andrewbonney 0:ec559500a63f 28 //#define __DEBUG
andrewbonney 0:ec559500a63f 29 #include "dbg/dbg.h"
andrewbonney 0:ec559500a63f 30
andrewbonney 0:ec559500a63f 31 #include "netCfg.h"
andrewbonney 0:ec559500a63f 32 #if NET_USB_SERIAL
andrewbonney 0:ec559500a63f 33
andrewbonney 0:ec559500a63f 34 namespace mbed {
andrewbonney 0:ec559500a63f 35
andrewbonney 0:ec559500a63f 36 #define BUF_LEN 64
andrewbonney 0:ec559500a63f 37 #define FLUSH_TMOUT 100000 //US
andrewbonney 0:ec559500a63f 38
andrewbonney 0:ec559500a63f 39 UsbSerial::UsbSerial(UsbDevice* pDevice, int epIn, int epOut, const char* name /*= NULL*/) : Stream(name), m_epIn(pDevice, epIn, true, USB_BULK, BUF_LEN), m_epOut(pDevice, epOut, false, USB_BULK, BUF_LEN),
andrewbonney 0:ec559500a63f 40 m_pInCbItem(NULL), m_pInCbMeth(NULL), m_pOutCbItem(NULL), m_pOutCbMeth(NULL)
andrewbonney 0:ec559500a63f 41 {
andrewbonney 0:ec559500a63f 42 m_inBufEven = new char[BUF_LEN];
andrewbonney 0:ec559500a63f 43 m_inBufOdd = new char[BUF_LEN];
andrewbonney 0:ec559500a63f 44 m_pInBufPos = m_inBufUsr = m_inBufEven;
andrewbonney 0:ec559500a63f 45 m_inBufTrmt = m_inBufOdd;
andrewbonney 0:ec559500a63f 46
andrewbonney 0:ec559500a63f 47 m_outBufEven = new char[BUF_LEN];
andrewbonney 0:ec559500a63f 48 m_outBufOdd = new char[BUF_LEN];
andrewbonney 0:ec559500a63f 49 m_pOutBufPos = m_outBufUsr = m_outBufEven;
andrewbonney 0:ec559500a63f 50 m_outBufTrmt = m_outBufOdd;
andrewbonney 0:ec559500a63f 51
andrewbonney 0:ec559500a63f 52 m_inBufLen = m_outBufLen = 0;
andrewbonney 0:ec559500a63f 53
andrewbonney 0:ec559500a63f 54 DBG("Starting RX'ing on in ep\n");
andrewbonney 0:ec559500a63f 55
andrewbonney 0:ec559500a63f 56 m_timeout = false;
andrewbonney 0:ec559500a63f 57
andrewbonney 0:ec559500a63f 58 m_epIn.setOnCompletion(this, &UsbSerial::onEpInTransfer);
andrewbonney 0:ec559500a63f 59 m_epOut.setOnCompletion(this, &UsbSerial::onEpOutTransfer);
andrewbonney 0:ec559500a63f 60
andrewbonney 0:ec559500a63f 61 startRx();
andrewbonney 0:ec559500a63f 62 }
andrewbonney 0:ec559500a63f 63
andrewbonney 0:ec559500a63f 64 UsbSerial::~UsbSerial()
andrewbonney 0:ec559500a63f 65 {
andrewbonney 0:ec559500a63f 66 delete[] m_inBufEven;
andrewbonney 0:ec559500a63f 67 delete[] m_inBufOdd;
andrewbonney 0:ec559500a63f 68 delete[] m_outBufEven;
andrewbonney 0:ec559500a63f 69 delete[] m_outBufOdd;
andrewbonney 0:ec559500a63f 70 }
andrewbonney 0:ec559500a63f 71
andrewbonney 0:ec559500a63f 72 void UsbSerial::baud(int baudrate) {
andrewbonney 0:ec559500a63f 73 //
andrewbonney 0:ec559500a63f 74 }
andrewbonney 0:ec559500a63f 75
andrewbonney 0:ec559500a63f 76 void UsbSerial::format(int bits, int parity, int stop) {
andrewbonney 0:ec559500a63f 77 //
andrewbonney 0:ec559500a63f 78 }
andrewbonney 0:ec559500a63f 79
andrewbonney 0:ec559500a63f 80 #if 0 //For doc only
andrewbonney 0:ec559500a63f 81 template <class T>
andrewbonney 0:ec559500a63f 82 void attach(T* pCbItem, void (T::*pCbMeth)())
andrewbonney 0:ec559500a63f 83 {
andrewbonney 0:ec559500a63f 84 m_pCbItem = (CDummy*) pCbItem;
andrewbonney 0:ec559500a63f 85 m_pCbMeth = (void (CDummy::*)()) pCbMeth;
andrewbonney 0:ec559500a63f 86 }
andrewbonney 0:ec559500a63f 87 #endif
andrewbonney 0:ec559500a63f 88
andrewbonney 0:ec559500a63f 89 int UsbSerial::_getc() {
andrewbonney 0:ec559500a63f 90 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 91 NVIC_DisableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 92 char c;
andrewbonney 0:ec559500a63f 93 c = *m_pInBufPos;
andrewbonney 0:ec559500a63f 94 m_pInBufPos++;
andrewbonney 0:ec559500a63f 95 NVIC_EnableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 96 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 97 return c;
andrewbonney 0:ec559500a63f 98 }
andrewbonney 0:ec559500a63f 99
andrewbonney 0:ec559500a63f 100 int UsbSerial::_putc(int c) {
andrewbonney 0:ec559500a63f 101 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 102 NVIC_DisableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 103 if( (m_pOutBufPos - m_outBufUsr) < BUF_LEN )
andrewbonney 0:ec559500a63f 104 {
andrewbonney 0:ec559500a63f 105 *m_pOutBufPos = (char) c;
andrewbonney 0:ec559500a63f 106 m_pOutBufPos++;
andrewbonney 0:ec559500a63f 107 }
andrewbonney 0:ec559500a63f 108 else
andrewbonney 0:ec559500a63f 109 {
andrewbonney 0:ec559500a63f 110 DBG("NO WAY!!!\n");
andrewbonney 0:ec559500a63f 111 }
andrewbonney 0:ec559500a63f 112 #if 1
andrewbonney 0:ec559500a63f 113 if( (m_pOutBufPos - m_outBufUsr) >= BUF_LEN ) //Must flush
andrewbonney 0:ec559500a63f 114 {
andrewbonney 0:ec559500a63f 115 if(m_timeout)
andrewbonney 0:ec559500a63f 116 m_txTimeout.detach();
andrewbonney 0:ec559500a63f 117 startTx();
andrewbonney 0:ec559500a63f 118 }
andrewbonney 0:ec559500a63f 119 else
andrewbonney 0:ec559500a63f 120 {
andrewbonney 0:ec559500a63f 121 /*if(m_timeout)
andrewbonney 0:ec559500a63f 122 m_txTimeout.detach();
andrewbonney 0:ec559500a63f 123 m_timeout = true;
andrewbonney 0:ec559500a63f 124 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);*/
andrewbonney 0:ec559500a63f 125 if(!m_timeout)
andrewbonney 0:ec559500a63f 126 {
andrewbonney 0:ec559500a63f 127 m_timeout = true;
andrewbonney 0:ec559500a63f 128 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
andrewbonney 0:ec559500a63f 129 }
andrewbonney 0:ec559500a63f 130 }
andrewbonney 0:ec559500a63f 131 #endif
andrewbonney 0:ec559500a63f 132 //startTx();
andrewbonney 0:ec559500a63f 133 NVIC_EnableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 134 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 135 return c;
andrewbonney 0:ec559500a63f 136 }
andrewbonney 0:ec559500a63f 137
andrewbonney 0:ec559500a63f 138 int UsbSerial::readable() {
andrewbonney 0:ec559500a63f 139 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 140 NVIC_DisableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 141 int res;
andrewbonney 0:ec559500a63f 142 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
andrewbonney 0:ec559500a63f 143 {
andrewbonney 0:ec559500a63f 144 //DBG("\r\nREADABLE\r\n");
andrewbonney 0:ec559500a63f 145 res = true;
andrewbonney 0:ec559500a63f 146 }
andrewbonney 0:ec559500a63f 147 else
andrewbonney 0:ec559500a63f 148 {
andrewbonney 0:ec559500a63f 149 //DBG("\r\nNOT READABLE\r\n");
andrewbonney 0:ec559500a63f 150 startRx(); //Try to swap packets & start another transmission
andrewbonney 0:ec559500a63f 151 res = ((m_pInBufPos - m_inBufUsr) < m_inBufLen )?true:false;
andrewbonney 0:ec559500a63f 152 }
andrewbonney 0:ec559500a63f 153 NVIC_EnableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 154 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 155 return (bool)res;
andrewbonney 0:ec559500a63f 156 }
andrewbonney 0:ec559500a63f 157
andrewbonney 0:ec559500a63f 158 int UsbSerial::writeable() {
andrewbonney 0:ec559500a63f 159 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 160 NVIC_DisableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 161 // DBG("\r\nWRITEABLE???\r\n");
andrewbonney 0:ec559500a63f 162 int res = (bool)( (m_pOutBufPos - m_outBufUsr) < BUF_LEN);
andrewbonney 0:ec559500a63f 163 NVIC_EnableIRQ(USB_IRQn);
andrewbonney 0:ec559500a63f 164 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
andrewbonney 0:ec559500a63f 165 return res;
andrewbonney 0:ec559500a63f 166 }
andrewbonney 0:ec559500a63f 167
andrewbonney 0:ec559500a63f 168 void UsbSerial::onReadable()
andrewbonney 0:ec559500a63f 169 {
andrewbonney 0:ec559500a63f 170 if(m_pInCbItem && m_pInCbMeth)
andrewbonney 0:ec559500a63f 171 (m_pInCbItem->*m_pInCbMeth)();
andrewbonney 0:ec559500a63f 172 }
andrewbonney 0:ec559500a63f 173
andrewbonney 0:ec559500a63f 174 void UsbSerial::onWriteable()
andrewbonney 0:ec559500a63f 175 {
andrewbonney 0:ec559500a63f 176 if(m_pOutCbItem && m_pOutCbMeth)
andrewbonney 0:ec559500a63f 177 (m_pOutCbItem->*m_pOutCbMeth)();
andrewbonney 0:ec559500a63f 178 }
andrewbonney 0:ec559500a63f 179
andrewbonney 0:ec559500a63f 180 void UsbSerial::onEpInTransfer()
andrewbonney 0:ec559500a63f 181 {
andrewbonney 0:ec559500a63f 182 int len = m_epIn.status();
andrewbonney 0:ec559500a63f 183 DBG("RX transfer completed w len=%d\n",len);
andrewbonney 0:ec559500a63f 184 startRx();
andrewbonney 0:ec559500a63f 185 if(len > 0)
andrewbonney 0:ec559500a63f 186 onReadable();
andrewbonney 0:ec559500a63f 187 }
andrewbonney 0:ec559500a63f 188
andrewbonney 0:ec559500a63f 189 void UsbSerial::onEpOutTransfer()
andrewbonney 0:ec559500a63f 190 {
andrewbonney 0:ec559500a63f 191 int len = m_epOut.status();
andrewbonney 0:ec559500a63f 192 DBG("TX transfer completed w len=%d\n",len);
andrewbonney 0:ec559500a63f 193 if(m_timeout)
andrewbonney 0:ec559500a63f 194 m_txTimeout.detach();
andrewbonney 0:ec559500a63f 195 startTx();
andrewbonney 0:ec559500a63f 196 if(len > 0)
andrewbonney 0:ec559500a63f 197 onWriteable();
andrewbonney 0:ec559500a63f 198 }
andrewbonney 0:ec559500a63f 199
andrewbonney 0:ec559500a63f 200 void UsbSerial::startTx()
andrewbonney 0:ec559500a63f 201 {
andrewbonney 0:ec559500a63f 202
andrewbonney 0:ec559500a63f 203 DBG("Transfer>\n");
andrewbonney 0:ec559500a63f 204
andrewbonney 0:ec559500a63f 205 m_timeout = false;
andrewbonney 0:ec559500a63f 206
andrewbonney 0:ec559500a63f 207 // m_txTimeout.detach();
andrewbonney 0:ec559500a63f 208
andrewbonney 0:ec559500a63f 209 if(!(m_pOutBufPos - m_outBufUsr))
andrewbonney 0:ec559500a63f 210 {
andrewbonney 0:ec559500a63f 211 DBG("?!?!?\n");
andrewbonney 0:ec559500a63f 212 return;
andrewbonney 0:ec559500a63f 213 }
andrewbonney 0:ec559500a63f 214
andrewbonney 0:ec559500a63f 215 if( m_epOut.status() == USBERR_PROCESSING )
andrewbonney 0:ec559500a63f 216 {
andrewbonney 0:ec559500a63f 217 //Wait & retry
andrewbonney 0:ec559500a63f 218 //m_timeout = true;
andrewbonney 0:ec559500a63f 219 //m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
andrewbonney 0:ec559500a63f 220 DBG("Ep is busy...\n");
andrewbonney 0:ec559500a63f 221 return;
andrewbonney 0:ec559500a63f 222 }
andrewbonney 0:ec559500a63f 223
andrewbonney 0:ec559500a63f 224 if( m_epOut.status() < 0 )
andrewbonney 0:ec559500a63f 225 {
andrewbonney 0:ec559500a63f 226 DBG("Tx trying again...\n");
andrewbonney 0:ec559500a63f 227 m_epOut.transfer((volatile uint8_t*)m_outBufTrmt, m_outBufLen);
andrewbonney 0:ec559500a63f 228 return;
andrewbonney 0:ec559500a63f 229 }
andrewbonney 0:ec559500a63f 230
andrewbonney 0:ec559500a63f 231 m_outBufLen = m_pOutBufPos - m_outBufUsr;
andrewbonney 0:ec559500a63f 232
andrewbonney 0:ec559500a63f 233 //Swap buffers
andrewbonney 0:ec559500a63f 234 volatile char* swapBuf = m_outBufUsr;
andrewbonney 0:ec559500a63f 235 m_outBufUsr = m_outBufTrmt;
andrewbonney 0:ec559500a63f 236 m_outBufTrmt = swapBuf;
andrewbonney 0:ec559500a63f 237
andrewbonney 0:ec559500a63f 238 m_epOut.transfer((volatile uint8_t*)m_outBufTrmt, m_outBufLen);
andrewbonney 0:ec559500a63f 239
andrewbonney 0:ec559500a63f 240 m_pOutBufPos = m_outBufUsr;
andrewbonney 0:ec559500a63f 241
andrewbonney 0:ec559500a63f 242 }
andrewbonney 0:ec559500a63f 243
andrewbonney 0:ec559500a63f 244 void UsbSerial::startRx()
andrewbonney 0:ec559500a63f 245 {
andrewbonney 0:ec559500a63f 246 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
andrewbonney 0:ec559500a63f 247 {
andrewbonney 0:ec559500a63f 248 //User buf is not empty, cannot swap now...
andrewbonney 0:ec559500a63f 249 return;
andrewbonney 0:ec559500a63f 250 }
andrewbonney 0:ec559500a63f 251 int len = m_epIn.status();
andrewbonney 0:ec559500a63f 252 if( len == USBERR_PROCESSING )
andrewbonney 0:ec559500a63f 253 {
andrewbonney 0:ec559500a63f 254 //Previous transmission not completed
andrewbonney 0:ec559500a63f 255 return;
andrewbonney 0:ec559500a63f 256 }
andrewbonney 0:ec559500a63f 257 if( len < 0 )
andrewbonney 0:ec559500a63f 258 {
andrewbonney 0:ec559500a63f 259 DBG("Rx trying again...\n");
andrewbonney 0:ec559500a63f 260 m_epIn.transfer((volatile uint8_t*)m_inBufTrmt, BUF_LEN); //Start another transmission
andrewbonney 0:ec559500a63f 261 return;
andrewbonney 0:ec559500a63f 262 }
andrewbonney 0:ec559500a63f 263
andrewbonney 0:ec559500a63f 264 m_inBufLen = len;
andrewbonney 0:ec559500a63f 265
andrewbonney 0:ec559500a63f 266 //Swap buffers
andrewbonney 0:ec559500a63f 267 volatile char* swapBuf = m_inBufUsr;
andrewbonney 0:ec559500a63f 268 m_inBufUsr = m_inBufTrmt;
andrewbonney 0:ec559500a63f 269 m_inBufTrmt = swapBuf;
andrewbonney 0:ec559500a63f 270 m_pInBufPos = m_inBufUsr;
andrewbonney 0:ec559500a63f 271
andrewbonney 0:ec559500a63f 272 DBG("Starting new transfer\n");
andrewbonney 0:ec559500a63f 273 m_epIn.transfer((volatile uint8_t*)m_inBufTrmt, BUF_LEN); //Start another transmission
andrewbonney 0:ec559500a63f 274
andrewbonney 0:ec559500a63f 275 }
andrewbonney 0:ec559500a63f 276
andrewbonney 0:ec559500a63f 277 #ifdef MBED_RPC
andrewbonney 0:ec559500a63f 278 const struct rpc_method *UsbSerial::get_rpc_methods() {
andrewbonney 0:ec559500a63f 279 static const rpc_method methods[] = {
andrewbonney 0:ec559500a63f 280 { "readable", rpc_method_caller<int, UsbSerial, &UsbSerial::readable> },
andrewbonney 0:ec559500a63f 281 { "writeable", rpc_method_caller<int, UsbSerial, &UsbSerial::writeable> },
andrewbonney 0:ec559500a63f 282 RPC_METHOD_SUPER(Stream)
andrewbonney 0:ec559500a63f 283 };
andrewbonney 0:ec559500a63f 284 return methods;
andrewbonney 0:ec559500a63f 285 }
andrewbonney 0:ec559500a63f 286
andrewbonney 0:ec559500a63f 287 struct rpc_class *UsbSerial::get_rpc_class() {
andrewbonney 0:ec559500a63f 288 static const rpc_function funcs[] = {
andrewbonney 0:ec559500a63f 289 /*{ "new", rpc_function_caller<const char*, UsbDevice*, int, int, const char*, Base::construct<UsbSerial,UsbDevice*,int,int,const char*> > },*/ //RPC is buggy
andrewbonney 0:ec559500a63f 290 RPC_METHOD_END
andrewbonney 0:ec559500a63f 291 };
andrewbonney 0:ec559500a63f 292 static rpc_class c = { "UsbSerial", funcs, NULL };
andrewbonney 0:ec559500a63f 293 return &c;
andrewbonney 0:ec559500a63f 294 }
andrewbonney 0:ec559500a63f 295 #endif
andrewbonney 0:ec559500a63f 296
andrewbonney 0:ec559500a63f 297 } // namespace mbed
andrewbonney 0:ec559500a63f 298
andrewbonney 0:ec559500a63f 299 #endif
andrewbonney 0:ec559500a63f 300