NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

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