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.
drv/serial/buf/SerialBuf.cpp@0:ec559500a63f, 2011-04-08 (annotated)
- Committer:
- andrewbonney
- Date:
- Fri Apr 08 14:39:41 2011 +0000
- Revision:
- 0:ec559500a63f
Who changed what in which revision?
User | Revision | Line number | New 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 "SerialBuf.h" |
andrewbonney | 0:ec559500a63f | 25 | #include "mbed.h" |
andrewbonney | 0:ec559500a63f | 26 | |
andrewbonney | 0:ec559500a63f | 27 | //#define __DEBUG |
andrewbonney | 0:ec559500a63f | 28 | #include "dbg/dbg.h" |
andrewbonney | 0:ec559500a63f | 29 | |
andrewbonney | 0:ec559500a63f | 30 | #include "netCfg.h" |
andrewbonney | 0:ec559500a63f | 31 | #if NET_GPRS |
andrewbonney | 0:ec559500a63f | 32 | |
andrewbonney | 0:ec559500a63f | 33 | #if NET_USB_SERIAL |
andrewbonney | 0:ec559500a63f | 34 | #define m_pStream( a ) (m_pSerial?m_pSerial->a:m_pUsbSerial->a) |
andrewbonney | 0:ec559500a63f | 35 | #else |
andrewbonney | 0:ec559500a63f | 36 | #define m_pStream( a ) (m_pSerial->a) |
andrewbonney | 0:ec559500a63f | 37 | #endif |
andrewbonney | 0:ec559500a63f | 38 | |
andrewbonney | 0:ec559500a63f | 39 | //Circular buf |
andrewbonney | 0:ec559500a63f | 40 | |
andrewbonney | 0:ec559500a63f | 41 | SerialCircularBuf::SerialCircularBuf(int len) : m_readMode(false) |
andrewbonney | 0:ec559500a63f | 42 | { |
andrewbonney | 0:ec559500a63f | 43 | m_buf = new char[len]; |
andrewbonney | 0:ec559500a63f | 44 | m_len = len; |
andrewbonney | 0:ec559500a63f | 45 | m_pReadStart = m_pRead = m_buf; |
andrewbonney | 0:ec559500a63f | 46 | m_pWrite = m_buf; |
andrewbonney | 0:ec559500a63f | 47 | } |
andrewbonney | 0:ec559500a63f | 48 | |
andrewbonney | 0:ec559500a63f | 49 | SerialCircularBuf::~SerialCircularBuf() |
andrewbonney | 0:ec559500a63f | 50 | { |
andrewbonney | 0:ec559500a63f | 51 | if(m_buf) |
andrewbonney | 0:ec559500a63f | 52 | delete[] m_buf; |
andrewbonney | 0:ec559500a63f | 53 | } |
andrewbonney | 0:ec559500a63f | 54 | |
andrewbonney | 0:ec559500a63f | 55 | int SerialCircularBuf::room() //Return room available in buf |
andrewbonney | 0:ec559500a63f | 56 | { |
andrewbonney | 0:ec559500a63f | 57 | //return m_len - len() - 1; //-1 is to avoid loop |
andrewbonney | 0:ec559500a63f | 58 | if ( m_pReadStart > m_pWrite ) |
andrewbonney | 0:ec559500a63f | 59 | return ( m_pReadStart - m_pWrite - 1 ); |
andrewbonney | 0:ec559500a63f | 60 | else |
andrewbonney | 0:ec559500a63f | 61 | return m_len - ( m_pWrite - m_pReadStart ) - 1; |
andrewbonney | 0:ec559500a63f | 62 | } |
andrewbonney | 0:ec559500a63f | 63 | |
andrewbonney | 0:ec559500a63f | 64 | int SerialCircularBuf::len() //Return chars length in buf |
andrewbonney | 0:ec559500a63f | 65 | { |
andrewbonney | 0:ec559500a63f | 66 | if ( m_pWrite >= m_pRead ) |
andrewbonney | 0:ec559500a63f | 67 | return ( m_pWrite - m_pRead ); |
andrewbonney | 0:ec559500a63f | 68 | else |
andrewbonney | 0:ec559500a63f | 69 | return m_len - ( m_pRead - m_pWrite ); // = ( ( m_buf + m_len) - m_pRead ) + ( m_pWrite - m_buf ) |
andrewbonney | 0:ec559500a63f | 70 | } |
andrewbonney | 0:ec559500a63f | 71 | |
andrewbonney | 0:ec559500a63f | 72 | void SerialCircularBuf::write(char c) |
andrewbonney | 0:ec559500a63f | 73 | { |
andrewbonney | 0:ec559500a63f | 74 | #if 0 |
andrewbonney | 0:ec559500a63f | 75 | if(!room()) |
andrewbonney | 0:ec559500a63f | 76 | return; |
andrewbonney | 0:ec559500a63f | 77 | #endif |
andrewbonney | 0:ec559500a63f | 78 | //WARN: Must call room() before |
andrewbonney | 0:ec559500a63f | 79 | *m_pWrite = c; |
andrewbonney | 0:ec559500a63f | 80 | m_pWrite++; |
andrewbonney | 0:ec559500a63f | 81 | if(m_pWrite>=m_buf+m_len) |
andrewbonney | 0:ec559500a63f | 82 | m_pWrite=m_buf; |
andrewbonney | 0:ec559500a63f | 83 | } |
andrewbonney | 0:ec559500a63f | 84 | char SerialCircularBuf::read() |
andrewbonney | 0:ec559500a63f | 85 | { |
andrewbonney | 0:ec559500a63f | 86 | #if 0 |
andrewbonney | 0:ec559500a63f | 87 | if(!len()) |
andrewbonney | 0:ec559500a63f | 88 | return 0; |
andrewbonney | 0:ec559500a63f | 89 | #endif |
andrewbonney | 0:ec559500a63f | 90 | //WARN: Must call len() before |
andrewbonney | 0:ec559500a63f | 91 | char c = *m_pRead; |
andrewbonney | 0:ec559500a63f | 92 | m_pRead++; |
andrewbonney | 0:ec559500a63f | 93 | |
andrewbonney | 0:ec559500a63f | 94 | if(m_pRead>=m_buf+m_len) |
andrewbonney | 0:ec559500a63f | 95 | m_pRead=m_buf; |
andrewbonney | 0:ec559500a63f | 96 | |
andrewbonney | 0:ec559500a63f | 97 | if(!m_readMode) //If readmode=false, trash this char |
andrewbonney | 0:ec559500a63f | 98 | m_pReadStart=m_pRead; |
andrewbonney | 0:ec559500a63f | 99 | |
andrewbonney | 0:ec559500a63f | 100 | return c; |
andrewbonney | 0:ec559500a63f | 101 | } |
andrewbonney | 0:ec559500a63f | 102 | |
andrewbonney | 0:ec559500a63f | 103 | void SerialCircularBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default |
andrewbonney | 0:ec559500a63f | 104 | { |
andrewbonney | 0:ec559500a63f | 105 | if(m_readMode == true && readMode == false) |
andrewbonney | 0:ec559500a63f | 106 | { |
andrewbonney | 0:ec559500a63f | 107 | //Trash bytes that have been read |
andrewbonney | 0:ec559500a63f | 108 | flushRead(); |
andrewbonney | 0:ec559500a63f | 109 | } |
andrewbonney | 0:ec559500a63f | 110 | m_readMode = readMode; |
andrewbonney | 0:ec559500a63f | 111 | } |
andrewbonney | 0:ec559500a63f | 112 | |
andrewbonney | 0:ec559500a63f | 113 | void SerialCircularBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true) |
andrewbonney | 0:ec559500a63f | 114 | { |
andrewbonney | 0:ec559500a63f | 115 | m_pReadStart = m_pRead; |
andrewbonney | 0:ec559500a63f | 116 | } |
andrewbonney | 0:ec559500a63f | 117 | |
andrewbonney | 0:ec559500a63f | 118 | void SerialCircularBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true) |
andrewbonney | 0:ec559500a63f | 119 | { |
andrewbonney | 0:ec559500a63f | 120 | m_pRead = m_pReadStart; |
andrewbonney | 0:ec559500a63f | 121 | } |
andrewbonney | 0:ec559500a63f | 122 | |
andrewbonney | 0:ec559500a63f | 123 | //SerialBuf |
andrewbonney | 0:ec559500a63f | 124 | |
andrewbonney | 0:ec559500a63f | 125 | SerialBuf::SerialBuf(int len) : m_rxBuf(len), m_txBuf(len), m_pSerial(NULL) //Buffer length |
andrewbonney | 0:ec559500a63f | 126 | #if NET_USB_SERIAL |
andrewbonney | 0:ec559500a63f | 127 | , m_pUsbSerial(NULL) |
andrewbonney | 0:ec559500a63f | 128 | #endif |
andrewbonney | 0:ec559500a63f | 129 | { |
andrewbonney | 0:ec559500a63f | 130 | DBG("New Serial buf@%p\n", this); |
andrewbonney | 0:ec559500a63f | 131 | } |
andrewbonney | 0:ec559500a63f | 132 | |
andrewbonney | 0:ec559500a63f | 133 | SerialBuf::~SerialBuf() |
andrewbonney | 0:ec559500a63f | 134 | { |
andrewbonney | 0:ec559500a63f | 135 | |
andrewbonney | 0:ec559500a63f | 136 | } |
andrewbonney | 0:ec559500a63f | 137 | |
andrewbonney | 0:ec559500a63f | 138 | void SerialBuf::attach(Serial* pSerial) |
andrewbonney | 0:ec559500a63f | 139 | { |
andrewbonney | 0:ec559500a63f | 140 | DBG("Serial buf@%p in attach\n", this); |
andrewbonney | 0:ec559500a63f | 141 | m_pSerial = pSerial; |
andrewbonney | 0:ec559500a63f | 142 | m_pSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, Serial::RxIrq); |
andrewbonney | 0:ec559500a63f | 143 | m_pSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, Serial::TxIrq); |
andrewbonney | 0:ec559500a63f | 144 | onRxInterrupt(); //Read data |
andrewbonney | 0:ec559500a63f | 145 | } |
andrewbonney | 0:ec559500a63f | 146 | |
andrewbonney | 0:ec559500a63f | 147 | void SerialBuf::detach() |
andrewbonney | 0:ec559500a63f | 148 | { |
andrewbonney | 0:ec559500a63f | 149 | if(m_pSerial) |
andrewbonney | 0:ec559500a63f | 150 | { |
andrewbonney | 0:ec559500a63f | 151 | m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::RxIrq); |
andrewbonney | 0:ec559500a63f | 152 | m_pSerial->attach<SerialBuf>(NULL, NULL, Serial::TxIrq); |
andrewbonney | 0:ec559500a63f | 153 | m_pSerial = NULL; |
andrewbonney | 0:ec559500a63f | 154 | } |
andrewbonney | 0:ec559500a63f | 155 | #if NET_USB_SERIAL |
andrewbonney | 0:ec559500a63f | 156 | else if(m_pUsbSerial) |
andrewbonney | 0:ec559500a63f | 157 | { |
andrewbonney | 0:ec559500a63f | 158 | m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::RxIrq); |
andrewbonney | 0:ec559500a63f | 159 | m_pUsbSerial->attach<SerialBuf>(NULL, NULL, UsbSerial::TxIrq); |
andrewbonney | 0:ec559500a63f | 160 | m_pUsbSerial = NULL; |
andrewbonney | 0:ec559500a63f | 161 | } |
andrewbonney | 0:ec559500a63f | 162 | #endif |
andrewbonney | 0:ec559500a63f | 163 | } |
andrewbonney | 0:ec559500a63f | 164 | |
andrewbonney | 0:ec559500a63f | 165 | #if NET_USB_SERIAL |
andrewbonney | 0:ec559500a63f | 166 | void SerialBuf::attach(UsbSerial* pUsbSerial) |
andrewbonney | 0:ec559500a63f | 167 | { |
andrewbonney | 0:ec559500a63f | 168 | m_pUsbSerial = pUsbSerial; |
andrewbonney | 0:ec559500a63f | 169 | m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onRxInterrupt, UsbSerial::RxIrq); |
andrewbonney | 0:ec559500a63f | 170 | m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onTxInterrupt, UsbSerial::TxIrq); |
andrewbonney | 0:ec559500a63f | 171 | onRxInterrupt(); //Read data |
andrewbonney | 0:ec559500a63f | 172 | } |
andrewbonney | 0:ec559500a63f | 173 | #endif |
andrewbonney | 0:ec559500a63f | 174 | |
andrewbonney | 0:ec559500a63f | 175 | char SerialBuf::getc() |
andrewbonney | 0:ec559500a63f | 176 | { |
andrewbonney | 0:ec559500a63f | 177 | while(!readable()); |
andrewbonney | 0:ec559500a63f | 178 | char c = m_rxBuf.read(); |
andrewbonney | 0:ec559500a63f | 179 | return c; |
andrewbonney | 0:ec559500a63f | 180 | } |
andrewbonney | 0:ec559500a63f | 181 | |
andrewbonney | 0:ec559500a63f | 182 | void SerialBuf::putc(char c) |
andrewbonney | 0:ec559500a63f | 183 | { |
andrewbonney | 0:ec559500a63f | 184 | while(!writeable()); |
andrewbonney | 0:ec559500a63f | 185 | m_txBuf.write(c); |
andrewbonney | 0:ec559500a63f | 186 | onTxInterrupt(); |
andrewbonney | 0:ec559500a63f | 187 | } |
andrewbonney | 0:ec559500a63f | 188 | |
andrewbonney | 0:ec559500a63f | 189 | bool SerialBuf::readable() |
andrewbonney | 0:ec559500a63f | 190 | { |
andrewbonney | 0:ec559500a63f | 191 | if( !m_rxBuf.len() ) //Fill buf if possible |
andrewbonney | 0:ec559500a63f | 192 | onRxInterrupt(); |
andrewbonney | 0:ec559500a63f | 193 | return (m_rxBuf.len() > 0); |
andrewbonney | 0:ec559500a63f | 194 | } |
andrewbonney | 0:ec559500a63f | 195 | |
andrewbonney | 0:ec559500a63f | 196 | bool SerialBuf::writeable() |
andrewbonney | 0:ec559500a63f | 197 | { |
andrewbonney | 0:ec559500a63f | 198 | if( !m_txBuf.room() ) //Free buf is possible |
andrewbonney | 0:ec559500a63f | 199 | onTxInterrupt(); |
andrewbonney | 0:ec559500a63f | 200 | return (m_txBuf.room() > 0); |
andrewbonney | 0:ec559500a63f | 201 | } |
andrewbonney | 0:ec559500a63f | 202 | |
andrewbonney | 0:ec559500a63f | 203 | void SerialBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default |
andrewbonney | 0:ec559500a63f | 204 | { |
andrewbonney | 0:ec559500a63f | 205 | m_rxBuf.setReadMode(readMode); |
andrewbonney | 0:ec559500a63f | 206 | } |
andrewbonney | 0:ec559500a63f | 207 | |
andrewbonney | 0:ec559500a63f | 208 | void SerialBuf::flushRead() //Delete chars that have been read & return chars len (only useful with readMode = true) |
andrewbonney | 0:ec559500a63f | 209 | { |
andrewbonney | 0:ec559500a63f | 210 | m_rxBuf.flushRead(); |
andrewbonney | 0:ec559500a63f | 211 | } |
andrewbonney | 0:ec559500a63f | 212 | |
andrewbonney | 0:ec559500a63f | 213 | void SerialBuf::resetRead() //Go back to initial read position & return chars len (only useful with readMode = true) |
andrewbonney | 0:ec559500a63f | 214 | { |
andrewbonney | 0:ec559500a63f | 215 | m_rxBuf.resetRead(); |
andrewbonney | 0:ec559500a63f | 216 | } |
andrewbonney | 0:ec559500a63f | 217 | |
andrewbonney | 0:ec559500a63f | 218 | void SerialBuf::onRxInterrupt() //Callback from m_pSerial |
andrewbonney | 0:ec559500a63f | 219 | { |
andrewbonney | 0:ec559500a63f | 220 | while( m_rxBuf.room() && m_pStream(readable()) ) |
andrewbonney | 0:ec559500a63f | 221 | { |
andrewbonney | 0:ec559500a63f | 222 | m_rxBuf.write(m_pStream(getc())); |
andrewbonney | 0:ec559500a63f | 223 | } |
andrewbonney | 0:ec559500a63f | 224 | } |
andrewbonney | 0:ec559500a63f | 225 | |
andrewbonney | 0:ec559500a63f | 226 | void SerialBuf::onTxInterrupt() //Callback from m_pSerial |
andrewbonney | 0:ec559500a63f | 227 | { |
andrewbonney | 0:ec559500a63f | 228 | while( m_txBuf.len() && m_pStream(writeable()) ) |
andrewbonney | 0:ec559500a63f | 229 | { |
andrewbonney | 0:ec559500a63f | 230 | m_pStream(putc(m_txBuf.read())); |
andrewbonney | 0:ec559500a63f | 231 | } |
andrewbonney | 0:ec559500a63f | 232 | } |
andrewbonney | 0:ec559500a63f | 233 | |
andrewbonney | 0:ec559500a63f | 234 | |
andrewbonney | 0:ec559500a63f | 235 | #endif |