Netservices modded to read fragmented HTTP respsonse/payload from special purpose server - 180 bytes only

Committer:
RodColeman
Date:
Thu Sep 08 10:48:09 2011 +0000
Revision:
0:850eacf3e945
revised fixed length to 178 bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:850eacf3e945 1
RodColeman 0:850eacf3e945 2 /*
RodColeman 0:850eacf3e945 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
RodColeman 0:850eacf3e945 4
RodColeman 0:850eacf3e945 5 Permission is hereby granted, free of charge, to any person obtaining a copy
RodColeman 0:850eacf3e945 6 of this software and associated documentation files (the "Software"), to deal
RodColeman 0:850eacf3e945 7 in the Software without restriction, including without limitation the rights
RodColeman 0:850eacf3e945 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RodColeman 0:850eacf3e945 9 copies of the Software, and to permit persons to whom the Software is
RodColeman 0:850eacf3e945 10 furnished to do so, subject to the following conditions:
RodColeman 0:850eacf3e945 11
RodColeman 0:850eacf3e945 12 The above copyright notice and this permission notice shall be included in
RodColeman 0:850eacf3e945 13 all copies or substantial portions of the Software.
RodColeman 0:850eacf3e945 14
RodColeman 0:850eacf3e945 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RodColeman 0:850eacf3e945 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RodColeman 0:850eacf3e945 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RodColeman 0:850eacf3e945 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RodColeman 0:850eacf3e945 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RodColeman 0:850eacf3e945 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RodColeman 0:850eacf3e945 21 THE SOFTWARE.
RodColeman 0:850eacf3e945 22 */
RodColeman 0:850eacf3e945 23
RodColeman 0:850eacf3e945 24 #include "rpc.h"
RodColeman 0:850eacf3e945 25
RodColeman 0:850eacf3e945 26 #include "UsbSerial.h"
RodColeman 0:850eacf3e945 27
RodColeman 0:850eacf3e945 28 //#define __DEBUG
RodColeman 0:850eacf3e945 29 #include "dbg/dbg.h"
RodColeman 0:850eacf3e945 30
RodColeman 0:850eacf3e945 31 #include "netCfg.h"
RodColeman 0:850eacf3e945 32 #if NET_USB_SERIAL
RodColeman 0:850eacf3e945 33
RodColeman 0:850eacf3e945 34 namespace mbed {
RodColeman 0:850eacf3e945 35
RodColeman 0:850eacf3e945 36 #define BUF_LEN 64
RodColeman 0:850eacf3e945 37 #define FLUSH_TMOUT 100000 //US
RodColeman 0:850eacf3e945 38
RodColeman 0:850eacf3e945 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),
RodColeman 0:850eacf3e945 40 m_pInCbItem(NULL), m_pInCbMeth(NULL), m_pOutCbItem(NULL), m_pOutCbMeth(NULL)
RodColeman 0:850eacf3e945 41 {
RodColeman 0:850eacf3e945 42 m_inBufEven = new char[BUF_LEN];
RodColeman 0:850eacf3e945 43 m_inBufOdd = new char[BUF_LEN];
RodColeman 0:850eacf3e945 44 m_pInBufPos = m_inBufUsr = m_inBufEven;
RodColeman 0:850eacf3e945 45 m_inBufTrmt = m_inBufOdd;
RodColeman 0:850eacf3e945 46
RodColeman 0:850eacf3e945 47 m_outBufEven = new char[BUF_LEN];
RodColeman 0:850eacf3e945 48 m_outBufOdd = new char[BUF_LEN];
RodColeman 0:850eacf3e945 49 m_pOutBufPos = m_outBufUsr = m_outBufEven;
RodColeman 0:850eacf3e945 50 m_outBufTrmt = m_outBufOdd;
RodColeman 0:850eacf3e945 51
RodColeman 0:850eacf3e945 52 m_inBufLen = m_outBufLen = 0;
RodColeman 0:850eacf3e945 53
RodColeman 0:850eacf3e945 54 DBG("Starting RX'ing on in ep\n");
RodColeman 0:850eacf3e945 55
RodColeman 0:850eacf3e945 56 m_timeout = false;
RodColeman 0:850eacf3e945 57
RodColeman 0:850eacf3e945 58 m_epIn.setOnCompletion(this, &UsbSerial::onEpInTransfer);
RodColeman 0:850eacf3e945 59 m_epOut.setOnCompletion(this, &UsbSerial::onEpOutTransfer);
RodColeman 0:850eacf3e945 60
RodColeman 0:850eacf3e945 61 startRx();
RodColeman 0:850eacf3e945 62 }
RodColeman 0:850eacf3e945 63
RodColeman 0:850eacf3e945 64 UsbSerial::~UsbSerial()
RodColeman 0:850eacf3e945 65 {
RodColeman 0:850eacf3e945 66 delete[] m_inBufEven;
RodColeman 0:850eacf3e945 67 delete[] m_inBufOdd;
RodColeman 0:850eacf3e945 68 delete[] m_outBufEven;
RodColeman 0:850eacf3e945 69 delete[] m_outBufOdd;
RodColeman 0:850eacf3e945 70 }
RodColeman 0:850eacf3e945 71
RodColeman 0:850eacf3e945 72 void UsbSerial::baud(int baudrate) {
RodColeman 0:850eacf3e945 73 //
RodColeman 0:850eacf3e945 74 }
RodColeman 0:850eacf3e945 75
RodColeman 0:850eacf3e945 76 void UsbSerial::format(int bits, int parity, int stop) {
RodColeman 0:850eacf3e945 77 //
RodColeman 0:850eacf3e945 78 }
RodColeman 0:850eacf3e945 79
RodColeman 0:850eacf3e945 80 #if 0 //For doc only
RodColeman 0:850eacf3e945 81 template <class T>
RodColeman 0:850eacf3e945 82 void attach(T* pCbItem, void (T::*pCbMeth)())
RodColeman 0:850eacf3e945 83 {
RodColeman 0:850eacf3e945 84 m_pCbItem = (CDummy*) pCbItem;
RodColeman 0:850eacf3e945 85 m_pCbMeth = (void (CDummy::*)()) pCbMeth;
RodColeman 0:850eacf3e945 86 }
RodColeman 0:850eacf3e945 87 #endif
RodColeman 0:850eacf3e945 88
RodColeman 0:850eacf3e945 89 int UsbSerial::_getc() {
RodColeman 0:850eacf3e945 90 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 91 NVIC_DisableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 92 char c;
RodColeman 0:850eacf3e945 93 c = *m_pInBufPos;
RodColeman 0:850eacf3e945 94 m_pInBufPos++;
RodColeman 0:850eacf3e945 95 NVIC_EnableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 96 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 97 return c;
RodColeman 0:850eacf3e945 98 }
RodColeman 0:850eacf3e945 99
RodColeman 0:850eacf3e945 100 int UsbSerial::_putc(int c) {
RodColeman 0:850eacf3e945 101 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 102 NVIC_DisableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 103 if( (m_pOutBufPos - m_outBufUsr) < BUF_LEN )
RodColeman 0:850eacf3e945 104 {
RodColeman 0:850eacf3e945 105 *m_pOutBufPos = (char) c;
RodColeman 0:850eacf3e945 106 m_pOutBufPos++;
RodColeman 0:850eacf3e945 107 }
RodColeman 0:850eacf3e945 108 else
RodColeman 0:850eacf3e945 109 {
RodColeman 0:850eacf3e945 110 DBG("NO WAY!!!\n");
RodColeman 0:850eacf3e945 111 }
RodColeman 0:850eacf3e945 112 #if 1
RodColeman 0:850eacf3e945 113 if( (m_pOutBufPos - m_outBufUsr) >= BUF_LEN ) //Must flush
RodColeman 0:850eacf3e945 114 {
RodColeman 0:850eacf3e945 115 if(m_timeout)
RodColeman 0:850eacf3e945 116 m_txTimeout.detach();
RodColeman 0:850eacf3e945 117 startTx();
RodColeman 0:850eacf3e945 118 }
RodColeman 0:850eacf3e945 119 else
RodColeman 0:850eacf3e945 120 {
RodColeman 0:850eacf3e945 121 /*if(m_timeout)
RodColeman 0:850eacf3e945 122 m_txTimeout.detach();
RodColeman 0:850eacf3e945 123 m_timeout = true;
RodColeman 0:850eacf3e945 124 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);*/
RodColeman 0:850eacf3e945 125 if(!m_timeout)
RodColeman 0:850eacf3e945 126 {
RodColeman 0:850eacf3e945 127 m_timeout = true;
RodColeman 0:850eacf3e945 128 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
RodColeman 0:850eacf3e945 129 }
RodColeman 0:850eacf3e945 130 }
RodColeman 0:850eacf3e945 131 #endif
RodColeman 0:850eacf3e945 132 //startTx();
RodColeman 0:850eacf3e945 133 NVIC_EnableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 134 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 135 return c;
RodColeman 0:850eacf3e945 136 }
RodColeman 0:850eacf3e945 137
RodColeman 0:850eacf3e945 138 int UsbSerial::readable() {
RodColeman 0:850eacf3e945 139 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 140 NVIC_DisableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 141 int res;
RodColeman 0:850eacf3e945 142 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
RodColeman 0:850eacf3e945 143 {
RodColeman 0:850eacf3e945 144 //DBG("\r\nREADABLE\r\n");
RodColeman 0:850eacf3e945 145 res = true;
RodColeman 0:850eacf3e945 146 }
RodColeman 0:850eacf3e945 147 else
RodColeman 0:850eacf3e945 148 {
RodColeman 0:850eacf3e945 149 //DBG("\r\nNOT READABLE\r\n");
RodColeman 0:850eacf3e945 150 startRx(); //Try to swap packets & start another transmission
RodColeman 0:850eacf3e945 151 res = ((m_pInBufPos - m_inBufUsr) < m_inBufLen )?true:false;
RodColeman 0:850eacf3e945 152 }
RodColeman 0:850eacf3e945 153 NVIC_EnableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 154 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 155 return (bool)res;
RodColeman 0:850eacf3e945 156 }
RodColeman 0:850eacf3e945 157
RodColeman 0:850eacf3e945 158 int UsbSerial::writeable() {
RodColeman 0:850eacf3e945 159 NVIC_DisableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 160 NVIC_DisableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 161 // DBG("\r\nWRITEABLE???\r\n");
RodColeman 0:850eacf3e945 162 int res = (bool)( (m_pOutBufPos - m_outBufUsr) < BUF_LEN);
RodColeman 0:850eacf3e945 163 NVIC_EnableIRQ(USB_IRQn);
RodColeman 0:850eacf3e945 164 NVIC_EnableIRQ(US_TICKER_TIMER_IRQn);
RodColeman 0:850eacf3e945 165 return res;
RodColeman 0:850eacf3e945 166 }
RodColeman 0:850eacf3e945 167
RodColeman 0:850eacf3e945 168 void UsbSerial::onReadable()
RodColeman 0:850eacf3e945 169 {
RodColeman 0:850eacf3e945 170 if(m_pInCbItem && m_pInCbMeth)
RodColeman 0:850eacf3e945 171 (m_pInCbItem->*m_pInCbMeth)();
RodColeman 0:850eacf3e945 172 }
RodColeman 0:850eacf3e945 173
RodColeman 0:850eacf3e945 174 void UsbSerial::onWriteable()
RodColeman 0:850eacf3e945 175 {
RodColeman 0:850eacf3e945 176 if(m_pOutCbItem && m_pOutCbMeth)
RodColeman 0:850eacf3e945 177 (m_pOutCbItem->*m_pOutCbMeth)();
RodColeman 0:850eacf3e945 178 }
RodColeman 0:850eacf3e945 179
RodColeman 0:850eacf3e945 180 void UsbSerial::onEpInTransfer()
RodColeman 0:850eacf3e945 181 {
RodColeman 0:850eacf3e945 182 int len = m_epIn.status();
RodColeman 0:850eacf3e945 183 DBG("RX transfer completed w len=%d\n",len);
RodColeman 0:850eacf3e945 184 startRx();
RodColeman 0:850eacf3e945 185 if(len > 0)
RodColeman 0:850eacf3e945 186 onReadable();
RodColeman 0:850eacf3e945 187 }
RodColeman 0:850eacf3e945 188
RodColeman 0:850eacf3e945 189 void UsbSerial::onEpOutTransfer()
RodColeman 0:850eacf3e945 190 {
RodColeman 0:850eacf3e945 191 int len = m_epOut.status();
RodColeman 0:850eacf3e945 192 DBG("TX transfer completed w len=%d\n",len);
RodColeman 0:850eacf3e945 193 if(m_timeout)
RodColeman 0:850eacf3e945 194 m_txTimeout.detach();
RodColeman 0:850eacf3e945 195 startTx();
RodColeman 0:850eacf3e945 196 if(len > 0)
RodColeman 0:850eacf3e945 197 onWriteable();
RodColeman 0:850eacf3e945 198 }
RodColeman 0:850eacf3e945 199
RodColeman 0:850eacf3e945 200 void UsbSerial::startTx()
RodColeman 0:850eacf3e945 201 {
RodColeman 0:850eacf3e945 202
RodColeman 0:850eacf3e945 203 DBG("Transfer>\n");
RodColeman 0:850eacf3e945 204
RodColeman 0:850eacf3e945 205 m_timeout = false;
RodColeman 0:850eacf3e945 206
RodColeman 0:850eacf3e945 207 // m_txTimeout.detach();
RodColeman 0:850eacf3e945 208
RodColeman 0:850eacf3e945 209 if(!(m_pOutBufPos - m_outBufUsr))
RodColeman 0:850eacf3e945 210 {
RodColeman 0:850eacf3e945 211 DBG("?!?!?\n");
RodColeman 0:850eacf3e945 212 return;
RodColeman 0:850eacf3e945 213 }
RodColeman 0:850eacf3e945 214
RodColeman 0:850eacf3e945 215 if( m_epOut.status() == USBERR_PROCESSING )
RodColeman 0:850eacf3e945 216 {
RodColeman 0:850eacf3e945 217 //Wait & retry
RodColeman 0:850eacf3e945 218 //m_timeout = true;
RodColeman 0:850eacf3e945 219 //m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
RodColeman 0:850eacf3e945 220 DBG("Ep is busy...\n");
RodColeman 0:850eacf3e945 221 return;
RodColeman 0:850eacf3e945 222 }
RodColeman 0:850eacf3e945 223
RodColeman 0:850eacf3e945 224 if( m_epOut.status() < 0 )
RodColeman 0:850eacf3e945 225 {
RodColeman 0:850eacf3e945 226 DBG("Tx trying again...\n");
RodColeman 0:850eacf3e945 227 m_epOut.transfer((volatile uint8_t*)m_outBufTrmt, m_outBufLen);
RodColeman 0:850eacf3e945 228 return;
RodColeman 0:850eacf3e945 229 }
RodColeman 0:850eacf3e945 230
RodColeman 0:850eacf3e945 231 m_outBufLen = m_pOutBufPos - m_outBufUsr;
RodColeman 0:850eacf3e945 232
RodColeman 0:850eacf3e945 233 //Swap buffers
RodColeman 0:850eacf3e945 234 volatile char* swapBuf = m_outBufUsr;
RodColeman 0:850eacf3e945 235 m_outBufUsr = m_outBufTrmt;
RodColeman 0:850eacf3e945 236 m_outBufTrmt = swapBuf;
RodColeman 0:850eacf3e945 237
RodColeman 0:850eacf3e945 238 m_epOut.transfer((volatile uint8_t*)m_outBufTrmt, m_outBufLen);
RodColeman 0:850eacf3e945 239
RodColeman 0:850eacf3e945 240 m_pOutBufPos = m_outBufUsr;
RodColeman 0:850eacf3e945 241
RodColeman 0:850eacf3e945 242 }
RodColeman 0:850eacf3e945 243
RodColeman 0:850eacf3e945 244 void UsbSerial::startRx()
RodColeman 0:850eacf3e945 245 {
RodColeman 0:850eacf3e945 246 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
RodColeman 0:850eacf3e945 247 {
RodColeman 0:850eacf3e945 248 //User buf is not empty, cannot swap now...
RodColeman 0:850eacf3e945 249 return;
RodColeman 0:850eacf3e945 250 }
RodColeman 0:850eacf3e945 251 int len = m_epIn.status();
RodColeman 0:850eacf3e945 252 if( len == USBERR_PROCESSING )
RodColeman 0:850eacf3e945 253 {
RodColeman 0:850eacf3e945 254 //Previous transmission not completed
RodColeman 0:850eacf3e945 255 return;
RodColeman 0:850eacf3e945 256 }
RodColeman 0:850eacf3e945 257 if( len < 0 )
RodColeman 0:850eacf3e945 258 {
RodColeman 0:850eacf3e945 259 DBG("Rx trying again...\n");
RodColeman 0:850eacf3e945 260 m_epIn.transfer((volatile uint8_t*)m_inBufTrmt, BUF_LEN); //Start another transmission
RodColeman 0:850eacf3e945 261 return;
RodColeman 0:850eacf3e945 262 }
RodColeman 0:850eacf3e945 263
RodColeman 0:850eacf3e945 264 m_inBufLen = len;
RodColeman 0:850eacf3e945 265
RodColeman 0:850eacf3e945 266 //Swap buffers
RodColeman 0:850eacf3e945 267 volatile char* swapBuf = m_inBufUsr;
RodColeman 0:850eacf3e945 268 m_inBufUsr = m_inBufTrmt;
RodColeman 0:850eacf3e945 269 m_inBufTrmt = swapBuf;
RodColeman 0:850eacf3e945 270 m_pInBufPos = m_inBufUsr;
RodColeman 0:850eacf3e945 271
RodColeman 0:850eacf3e945 272 DBG("Starting new transfer\n");
RodColeman 0:850eacf3e945 273 m_epIn.transfer((volatile uint8_t*)m_inBufTrmt, BUF_LEN); //Start another transmission
RodColeman 0:850eacf3e945 274
RodColeman 0:850eacf3e945 275 }
RodColeman 0:850eacf3e945 276
RodColeman 0:850eacf3e945 277 #ifdef MBED_RPC
RodColeman 0:850eacf3e945 278 const struct rpc_method *UsbSerial::get_rpc_methods() {
RodColeman 0:850eacf3e945 279 static const rpc_method methods[] = {
RodColeman 0:850eacf3e945 280 { "readable", rpc_method_caller<int, UsbSerial, &UsbSerial::readable> },
RodColeman 0:850eacf3e945 281 { "writeable", rpc_method_caller<int, UsbSerial, &UsbSerial::writeable> },
RodColeman 0:850eacf3e945 282 RPC_METHOD_SUPER(Stream)
RodColeman 0:850eacf3e945 283 };
RodColeman 0:850eacf3e945 284 return methods;
RodColeman 0:850eacf3e945 285 }
RodColeman 0:850eacf3e945 286
RodColeman 0:850eacf3e945 287 struct rpc_class *UsbSerial::get_rpc_class() {
RodColeman 0:850eacf3e945 288 static const rpc_function funcs[] = {
RodColeman 0:850eacf3e945 289 /*{ "new", rpc_function_caller<const char*, UsbDevice*, int, int, const char*, Base::construct<UsbSerial,UsbDevice*,int,int,const char*> > },*/ //RPC is buggy
RodColeman 0:850eacf3e945 290 RPC_METHOD_END
RodColeman 0:850eacf3e945 291 };
RodColeman 0:850eacf3e945 292 static rpc_class c = { "UsbSerial", funcs, NULL };
RodColeman 0:850eacf3e945 293 return &c;
RodColeman 0:850eacf3e945 294 }
RodColeman 0:850eacf3e945 295 #endif
RodColeman 0:850eacf3e945 296
RodColeman 0:850eacf3e945 297 } // namespace mbed
RodColeman 0:850eacf3e945 298
RodColeman 0:850eacf3e945 299 #endif
RodColeman 0:850eacf3e945 300