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

Committer:
RodColeman
Date:
Thu Sep 08 10:41:36 2011 +0000
Revision:
0:8f5825f330b0
setDataLen hacked to 180bytes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
RodColeman 0:8f5825f330b0 1
RodColeman 0:8f5825f330b0 2 /*
RodColeman 0:8f5825f330b0 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
RodColeman 0:8f5825f330b0 4
RodColeman 0:8f5825f330b0 5 Permission is hereby granted, free of charge, to any person obtaining a copy
RodColeman 0:8f5825f330b0 6 of this software and associated documentation files (the "Software"), to deal
RodColeman 0:8f5825f330b0 7 in the Software without restriction, including without limitation the rights
RodColeman 0:8f5825f330b0 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
RodColeman 0:8f5825f330b0 9 copies of the Software, and to permit persons to whom the Software is
RodColeman 0:8f5825f330b0 10 furnished to do so, subject to the following conditions:
RodColeman 0:8f5825f330b0 11
RodColeman 0:8f5825f330b0 12 The above copyright notice and this permission notice shall be included in
RodColeman 0:8f5825f330b0 13 all copies or substantial portions of the Software.
RodColeman 0:8f5825f330b0 14
RodColeman 0:8f5825f330b0 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
RodColeman 0:8f5825f330b0 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
RodColeman 0:8f5825f330b0 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
RodColeman 0:8f5825f330b0 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
RodColeman 0:8f5825f330b0 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
RodColeman 0:8f5825f330b0 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
RodColeman 0:8f5825f330b0 21 THE SOFTWARE.
RodColeman 0:8f5825f330b0 22 */
RodColeman 0:8f5825f330b0 23
RodColeman 0:8f5825f330b0 24 #include "SerialBuf.h"
RodColeman 0:8f5825f330b0 25 #include "mbed.h"
RodColeman 0:8f5825f330b0 26
RodColeman 0:8f5825f330b0 27 //#define __DEBUG
RodColeman 0:8f5825f330b0 28 #include "dbg/dbg.h"
RodColeman 0:8f5825f330b0 29
RodColeman 0:8f5825f330b0 30 #include "netCfg.h"
RodColeman 0:8f5825f330b0 31 #if NET_GPRS
RodColeman 0:8f5825f330b0 32
RodColeman 0:8f5825f330b0 33 #if NET_USB_SERIAL
RodColeman 0:8f5825f330b0 34 #define m_pStream( a ) (m_pSerial?m_pSerial->a:m_pUsbSerial->a)
RodColeman 0:8f5825f330b0 35 #else
RodColeman 0:8f5825f330b0 36 #define m_pStream( a ) (m_pSerial->a)
RodColeman 0:8f5825f330b0 37 #endif
RodColeman 0:8f5825f330b0 38
RodColeman 0:8f5825f330b0 39 //Circular buf
RodColeman 0:8f5825f330b0 40
RodColeman 0:8f5825f330b0 41 SerialCircularBuf::SerialCircularBuf(int len) : m_readMode(false)
RodColeman 0:8f5825f330b0 42 {
RodColeman 0:8f5825f330b0 43 m_buf = new char[len];
RodColeman 0:8f5825f330b0 44 m_len = len;
RodColeman 0:8f5825f330b0 45 m_pReadStart = m_pRead = m_buf;
RodColeman 0:8f5825f330b0 46 m_pWrite = m_buf;
RodColeman 0:8f5825f330b0 47 }
RodColeman 0:8f5825f330b0 48
RodColeman 0:8f5825f330b0 49 SerialCircularBuf::~SerialCircularBuf()
RodColeman 0:8f5825f330b0 50 {
RodColeman 0:8f5825f330b0 51 if(m_buf)
RodColeman 0:8f5825f330b0 52 delete[] m_buf;
RodColeman 0:8f5825f330b0 53 }
RodColeman 0:8f5825f330b0 54
RodColeman 0:8f5825f330b0 55 int SerialCircularBuf::room() //Return room available in buf
RodColeman 0:8f5825f330b0 56 {
RodColeman 0:8f5825f330b0 57 //return m_len - len() - 1; //-1 is to avoid loop
RodColeman 0:8f5825f330b0 58 if ( m_pReadStart > m_pWrite )
RodColeman 0:8f5825f330b0 59 return ( m_pReadStart - m_pWrite - 1 );
RodColeman 0:8f5825f330b0 60 else
RodColeman 0:8f5825f330b0 61 return m_len - ( m_pWrite - m_pReadStart ) - 1;
RodColeman 0:8f5825f330b0 62 }
RodColeman 0:8f5825f330b0 63
RodColeman 0:8f5825f330b0 64 int SerialCircularBuf::len() //Return chars length in buf
RodColeman 0:8f5825f330b0 65 {
RodColeman 0:8f5825f330b0 66 if ( m_pWrite >= m_pRead )
RodColeman 0:8f5825f330b0 67 return ( m_pWrite - m_pRead );
RodColeman 0:8f5825f330b0 68 else
RodColeman 0:8f5825f330b0 69 return m_len - ( m_pRead - m_pWrite ); // = ( ( m_buf + m_len) - m_pRead ) + ( m_pWrite - m_buf )
RodColeman 0:8f5825f330b0 70 }
RodColeman 0:8f5825f330b0 71
RodColeman 0:8f5825f330b0 72 void SerialCircularBuf::write(char c)
RodColeman 0:8f5825f330b0 73 {
RodColeman 0:8f5825f330b0 74 #if 0
RodColeman 0:8f5825f330b0 75 if(!room())
RodColeman 0:8f5825f330b0 76 return;
RodColeman 0:8f5825f330b0 77 #endif
RodColeman 0:8f5825f330b0 78 //WARN: Must call room() before
RodColeman 0:8f5825f330b0 79 *m_pWrite = c;
RodColeman 0:8f5825f330b0 80 m_pWrite++;
RodColeman 0:8f5825f330b0 81 if(m_pWrite>=m_buf+m_len)
RodColeman 0:8f5825f330b0 82 m_pWrite=m_buf;
RodColeman 0:8f5825f330b0 83 }
RodColeman 0:8f5825f330b0 84 char SerialCircularBuf::read()
RodColeman 0:8f5825f330b0 85 {
RodColeman 0:8f5825f330b0 86 #if 0
RodColeman 0:8f5825f330b0 87 if(!len())
RodColeman 0:8f5825f330b0 88 return 0;
RodColeman 0:8f5825f330b0 89 #endif
RodColeman 0:8f5825f330b0 90 //WARN: Must call len() before
RodColeman 0:8f5825f330b0 91 char c = *m_pRead;
RodColeman 0:8f5825f330b0 92 m_pRead++;
RodColeman 0:8f5825f330b0 93
RodColeman 0:8f5825f330b0 94 if(m_pRead>=m_buf+m_len)
RodColeman 0:8f5825f330b0 95 m_pRead=m_buf;
RodColeman 0:8f5825f330b0 96
RodColeman 0:8f5825f330b0 97 if(!m_readMode) //If readmode=false, trash this char
RodColeman 0:8f5825f330b0 98 m_pReadStart=m_pRead;
RodColeman 0:8f5825f330b0 99
RodColeman 0:8f5825f330b0 100 return c;
RodColeman 0:8f5825f330b0 101 }
RodColeman 0:8f5825f330b0 102
RodColeman 0:8f5825f330b0 103 void SerialCircularBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
RodColeman 0:8f5825f330b0 104 {
RodColeman 0:8f5825f330b0 105 if(m_readMode == true && readMode == false)
RodColeman 0:8f5825f330b0 106 {
RodColeman 0:8f5825f330b0 107 //Trash bytes that have been read
RodColeman 0:8f5825f330b0 108 flushRead();
RodColeman 0:8f5825f330b0 109 }
RodColeman 0:8f5825f330b0 110 m_readMode = readMode;
RodColeman 0:8f5825f330b0 111 }
RodColeman 0:8f5825f330b0 112
RodColeman 0:8f5825f330b0 113 void SerialCircularBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true)
RodColeman 0:8f5825f330b0 114 {
RodColeman 0:8f5825f330b0 115 m_pReadStart = m_pRead;
RodColeman 0:8f5825f330b0 116 }
RodColeman 0:8f5825f330b0 117
RodColeman 0:8f5825f330b0 118 void SerialCircularBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true)
RodColeman 0:8f5825f330b0 119 {
RodColeman 0:8f5825f330b0 120 m_pRead = m_pReadStart;
RodColeman 0:8f5825f330b0 121 }
RodColeman 0:8f5825f330b0 122
RodColeman 0:8f5825f330b0 123 //SerialBuf
RodColeman 0:8f5825f330b0 124
RodColeman 0:8f5825f330b0 125 SerialBuf::SerialBuf(int len) : m_rxBuf(len), m_txBuf(len), m_pSerial(NULL) //Buffer length
RodColeman 0:8f5825f330b0 126 #if NET_USB_SERIAL
RodColeman 0:8f5825f330b0 127 , m_pUsbSerial(NULL)
RodColeman 0:8f5825f330b0 128 #endif
RodColeman 0:8f5825f330b0 129 {
RodColeman 0:8f5825f330b0 130 DBG("New Serial buf@%p\n", this);
RodColeman 0:8f5825f330b0 131 }
RodColeman 0:8f5825f330b0 132
RodColeman 0:8f5825f330b0 133 SerialBuf::~SerialBuf()
RodColeman 0:8f5825f330b0 134 {
RodColeman 0:8f5825f330b0 135
RodColeman 0:8f5825f330b0 136 }
RodColeman 0:8f5825f330b0 137
RodColeman 0:8f5825f330b0 138 void SerialBuf::attach(Serial* pSerial)
RodColeman 0:8f5825f330b0 139 {
RodColeman 0:8f5825f330b0 140 DBG("Serial buf@%p in attach\n", this);
RodColeman 0:8f5825f330b0 141 m_pSerial = pSerial;
RodColeman 0:8f5825f330b0 142 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, Serial::RxIrq);
RodColeman 0:8f5825f330b0 143 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, Serial::TxIrq);
RodColeman 0:8f5825f330b0 144 onRxInterrupt(); //Read data
RodColeman 0:8f5825f330b0 145 }
RodColeman 0:8f5825f330b0 146
RodColeman 0:8f5825f330b0 147 void SerialBuf::detach()
RodColeman 0:8f5825f330b0 148 {
RodColeman 0:8f5825f330b0 149 if(m_pSerial)
RodColeman 0:8f5825f330b0 150 {
RodColeman 0:8f5825f330b0 151 m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::RxIrq);
RodColeman 0:8f5825f330b0 152 m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::TxIrq);
RodColeman 0:8f5825f330b0 153 m_pSerial = NULL;
RodColeman 0:8f5825f330b0 154 }
RodColeman 0:8f5825f330b0 155 #if NET_USB_SERIAL
RodColeman 0:8f5825f330b0 156 else if(m_pUsbSerial)
RodColeman 0:8f5825f330b0 157 {
RodColeman 0:8f5825f330b0 158 m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::RxIrq);
RodColeman 0:8f5825f330b0 159 m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::TxIrq);
RodColeman 0:8f5825f330b0 160 m_pUsbSerial = NULL;
RodColeman 0:8f5825f330b0 161 }
RodColeman 0:8f5825f330b0 162 #endif
RodColeman 0:8f5825f330b0 163 }
RodColeman 0:8f5825f330b0 164
RodColeman 0:8f5825f330b0 165 #if NET_USB_SERIAL
RodColeman 0:8f5825f330b0 166 void SerialBuf::attach(UsbSerial* pUsbSerial)
RodColeman 0:8f5825f330b0 167 {
RodColeman 0:8f5825f330b0 168 m_pUsbSerial = pUsbSerial;
RodColeman 0:8f5825f330b0 169 m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, UsbSerial::RxIrq);
RodColeman 0:8f5825f330b0 170 m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, UsbSerial::TxIrq);
RodColeman 0:8f5825f330b0 171 onRxInterrupt(); //Read data
RodColeman 0:8f5825f330b0 172 }
RodColeman 0:8f5825f330b0 173 #endif
RodColeman 0:8f5825f330b0 174
RodColeman 0:8f5825f330b0 175 char SerialBuf::getc()
RodColeman 0:8f5825f330b0 176 {
RodColeman 0:8f5825f330b0 177 while(!readable());
RodColeman 0:8f5825f330b0 178 char c = m_rxBuf.read();
RodColeman 0:8f5825f330b0 179 return c;
RodColeman 0:8f5825f330b0 180 }
RodColeman 0:8f5825f330b0 181
RodColeman 0:8f5825f330b0 182 void SerialBuf::putc(char c)
RodColeman 0:8f5825f330b0 183 {
RodColeman 0:8f5825f330b0 184 while(!writeable());
RodColeman 0:8f5825f330b0 185 m_txBuf.write(c);
RodColeman 0:8f5825f330b0 186 onTxInterrupt();
RodColeman 0:8f5825f330b0 187 }
RodColeman 0:8f5825f330b0 188
RodColeman 0:8f5825f330b0 189 bool SerialBuf::readable()
RodColeman 0:8f5825f330b0 190 {
RodColeman 0:8f5825f330b0 191 if( !m_rxBuf.len() ) //Fill buf if possible
RodColeman 0:8f5825f330b0 192 onRxInterrupt();
RodColeman 0:8f5825f330b0 193 return (m_rxBuf.len() > 0);
RodColeman 0:8f5825f330b0 194 }
RodColeman 0:8f5825f330b0 195
RodColeman 0:8f5825f330b0 196 bool SerialBuf::writeable()
RodColeman 0:8f5825f330b0 197 {
RodColeman 0:8f5825f330b0 198 if( !m_txBuf.room() ) //Free buf is possible
RodColeman 0:8f5825f330b0 199 onTxInterrupt();
RodColeman 0:8f5825f330b0 200 return (m_txBuf.room() > 0);
RodColeman 0:8f5825f330b0 201 }
RodColeman 0:8f5825f330b0 202
RodColeman 0:8f5825f330b0 203 void SerialBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
RodColeman 0:8f5825f330b0 204 {
RodColeman 0:8f5825f330b0 205 m_rxBuf.setReadMode(readMode);
RodColeman 0:8f5825f330b0 206 }
RodColeman 0:8f5825f330b0 207
RodColeman 0:8f5825f330b0 208 void SerialBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true)
RodColeman 0:8f5825f330b0 209 {
RodColeman 0:8f5825f330b0 210 m_rxBuf.flushRead();
RodColeman 0:8f5825f330b0 211 }
RodColeman 0:8f5825f330b0 212
RodColeman 0:8f5825f330b0 213 void SerialBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true)
RodColeman 0:8f5825f330b0 214 {
RodColeman 0:8f5825f330b0 215 m_rxBuf.resetRead();
RodColeman 0:8f5825f330b0 216 }
RodColeman 0:8f5825f330b0 217
RodColeman 0:8f5825f330b0 218 void SerialBuf::onRxInterrupt() //Callback from m_pSerial
RodColeman 0:8f5825f330b0 219 {
RodColeman 0:8f5825f330b0 220 while( m_rxBuf.room() && m_pStream(readable()) )
RodColeman 0:8f5825f330b0 221 {
RodColeman 0:8f5825f330b0 222 m_rxBuf.write(m_pStream(getc()));
RodColeman 0:8f5825f330b0 223 }
RodColeman 0:8f5825f330b0 224 }
RodColeman 0:8f5825f330b0 225
RodColeman 0:8f5825f330b0 226 void SerialBuf::onTxInterrupt() //Callback from m_pSerial
RodColeman 0:8f5825f330b0 227 {
RodColeman 0:8f5825f330b0 228 while( m_txBuf.len() && m_pStream(writeable()) )
RodColeman 0:8f5825f330b0 229 {
RodColeman 0:8f5825f330b0 230 m_pStream(putc(m_txBuf.read()));
RodColeman 0:8f5825f330b0 231 }
RodColeman 0:8f5825f330b0 232 }
RodColeman 0:8f5825f330b0 233
RodColeman 0:8f5825f330b0 234
RodColeman 0:8f5825f330b0 235 #endif