Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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