I have a problem getting this to work. Server only recieves half of the data being sent. Whats wrong

Dependencies:   mbed

Committer:
tax
Date:
Tue Mar 29 13:20:15 2011 +0000
Revision:
0:66300c77c6e9

        

Who changed what in which revision?

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