Dirk-Willem van Gulik (NXP/mbed) / Mbed 2 deprecated Bonjour

Dependencies:   mbed

Committer:
dirkx
Date:
Sat Aug 14 15:54:31 2010 +0000
Revision:
5:8e53abda9900
Parent:
0:355018f44c9f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dirkx 0:355018f44c9f 1
dirkx 0:355018f44c9f 2 /*
dirkx 0:355018f44c9f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
dirkx 0:355018f44c9f 4
dirkx 0:355018f44c9f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
dirkx 0:355018f44c9f 6 of this software and associated documentation files (the "Software"), to deal
dirkx 0:355018f44c9f 7 in the Software without restriction, including without limitation the rights
dirkx 0:355018f44c9f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
dirkx 0:355018f44c9f 9 copies of the Software, and to permit persons to whom the Software is
dirkx 0:355018f44c9f 10 furnished to do so, subject to the following conditions:
dirkx 0:355018f44c9f 11
dirkx 0:355018f44c9f 12 The above copyright notice and this permission notice shall be included in
dirkx 0:355018f44c9f 13 all copies or substantial portions of the Software.
dirkx 0:355018f44c9f 14
dirkx 0:355018f44c9f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
dirkx 0:355018f44c9f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
dirkx 0:355018f44c9f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
dirkx 0:355018f44c9f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
dirkx 0:355018f44c9f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
dirkx 0:355018f44c9f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
dirkx 0:355018f44c9f 21 THE SOFTWARE.
dirkx 0:355018f44c9f 22 */
dirkx 0:355018f44c9f 23
dirkx 0:355018f44c9f 24 #include "SerialBuf.h"
dirkx 0:355018f44c9f 25 #include "mbed.h"
dirkx 0:355018f44c9f 26
dirkx 0:355018f44c9f 27 //#define __DEBUG
dirkx 0:355018f44c9f 28 #include "dbg/dbg.h"
dirkx 0:355018f44c9f 29
dirkx 0:355018f44c9f 30 #include "netCfg.h"
dirkx 0:355018f44c9f 31 #if NET_GPRS
dirkx 0:355018f44c9f 32
dirkx 0:355018f44c9f 33 #if NET_USB_SERIAL
dirkx 0:355018f44c9f 34 #define m_pStream( a ) (m_pSerial?m_pSerial->a:m_pUsbSerial->a)
dirkx 0:355018f44c9f 35 #else
dirkx 0:355018f44c9f 36 #define m_pStream( a ) (m_pSerial->a)
dirkx 0:355018f44c9f 37 #endif
dirkx 0:355018f44c9f 38
dirkx 0:355018f44c9f 39 SerialBuf::SerialBuf(int len) : m_trmt(false), m_pSerial(NULL), m_intCanReadData(true), m_readMode(false) //Buffer length
dirkx 0:355018f44c9f 40 #if NET_USB_SERIAL
dirkx 0:355018f44c9f 41 , m_pUsbSerial(NULL)
dirkx 0:355018f44c9f 42 #endif
dirkx 0:355018f44c9f 43 {
dirkx 0:355018f44c9f 44 m_buf = new char[len];
dirkx 0:355018f44c9f 45 m_bufLen=len;
dirkx 0:355018f44c9f 46 m_pRead=m_buf;
dirkx 0:355018f44c9f 47 m_pReadStart=m_buf;
dirkx 0:355018f44c9f 48 m_pReadByInt=m_buf;
dirkx 0:355018f44c9f 49 m_pWrite=m_buf;
dirkx 0:355018f44c9f 50 }
dirkx 0:355018f44c9f 51
dirkx 0:355018f44c9f 52 SerialBuf::~SerialBuf()
dirkx 0:355018f44c9f 53 {
dirkx 0:355018f44c9f 54 delete[] m_buf;
dirkx 0:355018f44c9f 55 }
dirkx 0:355018f44c9f 56
dirkx 0:355018f44c9f 57 void SerialBuf::attach(Serial* pSerial)
dirkx 0:355018f44c9f 58 {
dirkx 0:355018f44c9f 59 m_pSerial = pSerial;
dirkx 0:355018f44c9f 60 m_pSerial->attach<SerialBuf>(this, &SerialBuf::onSerialInterrupt);
dirkx 0:355018f44c9f 61 // onSerialInterrupt(); //Flush hw buffer into current buf
dirkx 0:355018f44c9f 62 }
dirkx 0:355018f44c9f 63
dirkx 0:355018f44c9f 64 #if NET_USB_SERIAL
dirkx 0:355018f44c9f 65 void SerialBuf::attach(UsbSerial* pUsbSerial)
dirkx 0:355018f44c9f 66 {
dirkx 0:355018f44c9f 67 m_pUsbSerial = pUsbSerial;
dirkx 0:355018f44c9f 68 //m_pUsbSerial->attach<SerialBuf>(this, &SerialBuf::onSerialInterrupt);
dirkx 0:355018f44c9f 69 m_usbTick.attach_us<SerialBuf>(this, &SerialBuf::onSerialInterrupt, 10000);
dirkx 0:355018f44c9f 70 }
dirkx 0:355018f44c9f 71 #endif
dirkx 0:355018f44c9f 72
dirkx 0:355018f44c9f 73 void SerialBuf::detach()
dirkx 0:355018f44c9f 74 {
dirkx 0:355018f44c9f 75 if(m_pSerial)
dirkx 0:355018f44c9f 76 {
dirkx 0:355018f44c9f 77 m_pSerial->attach(0);
dirkx 0:355018f44c9f 78 m_pSerial = NULL;
dirkx 0:355018f44c9f 79 }
dirkx 0:355018f44c9f 80 #if NET_USB_SERIAL
dirkx 0:355018f44c9f 81 else if(m_pUsbSerial)
dirkx 0:355018f44c9f 82 {
dirkx 0:355018f44c9f 83 m_usbTick.detach();
dirkx 0:355018f44c9f 84 m_pUsbSerial = NULL;
dirkx 0:355018f44c9f 85 }
dirkx 0:355018f44c9f 86 #endif
dirkx 0:355018f44c9f 87 }
dirkx 0:355018f44c9f 88
dirkx 0:355018f44c9f 89
dirkx 0:355018f44c9f 90 void SerialBuf::onSerialInterrupt() //Callback from m_pSerial
dirkx 0:355018f44c9f 91 {
dirkx 0:355018f44c9f 92 //DBG("\r\n[INT]");
dirkx 0:355018f44c9f 93 // Timer tmr;
dirkx 0:355018f44c9f 94 // tmr.start();
dirkx 0:355018f44c9f 95 // static volatile bool incompleteData = true;
dirkx 0:355018f44c9f 96 // static volatile char* pLastIntReadPos = m_buf;
dirkx 0:355018f44c9f 97 if(!(m_pStream(readable())))
dirkx 0:355018f44c9f 98 {
dirkx 0:355018f44c9f 99 return;
dirkx 0:355018f44c9f 100 }
dirkx 0:355018f44c9f 101 int len=0;
dirkx 0:355018f44c9f 102 do
dirkx 0:355018f44c9f 103 {
dirkx 0:355018f44c9f 104 if(room()>0)
dirkx 0:355018f44c9f 105 {
dirkx 0:355018f44c9f 106 len++;
dirkx 0:355018f44c9f 107 #ifdef __DEBUGVERBOSE
dirkx 0:355018f44c9f 108 char c = m_pStream(getc());
dirkx 0:355018f44c9f 109 DBG("\r\n[%c]",c);
dirkx 0:355018f44c9f 110 put(c);
dirkx 0:355018f44c9f 111 #else
dirkx 0:355018f44c9f 112 put(m_pStream(getc()));
dirkx 0:355018f44c9f 113 #endif
dirkx 0:355018f44c9f 114 }
dirkx 0:355018f44c9f 115 else
dirkx 0:355018f44c9f 116 {
dirkx 0:355018f44c9f 117 DBG("\r\nWARN: SerialBuf Overrun");
dirkx 0:355018f44c9f 118 m_pStream(getc());
dirkx 0:355018f44c9f 119 }
dirkx 0:355018f44c9f 120 } while(m_pStream(readable()));
dirkx 0:355018f44c9f 121 // DBG("\r\n[/INT]=*%d*\r\n w len=*%d*",tmr.read_us(),len);
dirkx 0:355018f44c9f 122
dirkx 0:355018f44c9f 123 if( m_intCanReadData )
dirkx 0:355018f44c9f 124 {
dirkx 0:355018f44c9f 125 volatile bool u_readMode = m_readMode; //Save User context on interrupt
dirkx 0:355018f44c9f 126 volatile bool handled = onRead();
dirkx 0:355018f44c9f 127 if(handled)
dirkx 0:355018f44c9f 128 {
dirkx 0:355018f44c9f 129 m_pReadByInt = m_pRead;
dirkx 0:355018f44c9f 130 if(m_pRead==m_pReadStart)
dirkx 0:355018f44c9f 131 {
dirkx 0:355018f44c9f 132 //Data has been processed
dirkx 0:355018f44c9f 133 }
dirkx 0:355018f44c9f 134 else
dirkx 0:355018f44c9f 135 {
dirkx 0:355018f44c9f 136 m_pRead = m_pReadStart;
dirkx 0:355018f44c9f 137 m_intCanReadData = false;
dirkx 0:355018f44c9f 138 //Data has to be processed in user space
dirkx 0:355018f44c9f 139 }
dirkx 0:355018f44c9f 140 }
dirkx 0:355018f44c9f 141 setReadMode( u_readMode );
dirkx 0:355018f44c9f 142 }
dirkx 0:355018f44c9f 143
dirkx 0:355018f44c9f 144
dirkx 0:355018f44c9f 145 #if 0
dirkx 0:355018f44c9f 146 if( incompleteData || ( pLastIntReadPos != m_pRead ) )
dirkx 0:355018f44c9f 147 {
dirkx 0:355018f44c9f 148 bool u_readMode = m_readMode; //Save User context on interrupt
dirkx 0:355018f44c9f 149 incompleteData = onRead();
dirkx 0:355018f44c9f 150 if(!incompleteData)
dirkx 0:355018f44c9f 151 m_pRead = m_pReadStart;
dirkx 0:355018f44c9f 152 pLastIntReadPos = m_pRead;
dirkx 0:355018f44c9f 153
dirkx 0:355018f44c9f 154 }
dirkx 0:355018f44c9f 155 #endif
dirkx 0:355018f44c9f 156
dirkx 0:355018f44c9f 157 }
dirkx 0:355018f44c9f 158
dirkx 0:355018f44c9f 159 char SerialBuf::getc()
dirkx 0:355018f44c9f 160 {
dirkx 0:355018f44c9f 161 // DBG("\r\n\[GETC]");
dirkx 0:355018f44c9f 162 #if 0
dirkx 0:355018f44c9f 163 if(m_trmt) //Was transmitting
dirkx 0:355018f44c9f 164 {
dirkx 0:355018f44c9f 165 DBG("\r\n<\r\n");
dirkx 0:355018f44c9f 166 m_trmt=false;
dirkx 0:355018f44c9f 167 }
dirkx 0:355018f44c9f 168 #endif
dirkx 0:355018f44c9f 169 char c;
dirkx 0:355018f44c9f 170 c = get();
dirkx 0:355018f44c9f 171 DBG("%c", c);
dirkx 0:355018f44c9f 172 // DBG("\r\n[/GETC]");
dirkx 0:355018f44c9f 173 return c;
dirkx 0:355018f44c9f 174 }
dirkx 0:355018f44c9f 175
dirkx 0:355018f44c9f 176 void SerialBuf::putc(char c)
dirkx 0:355018f44c9f 177 {
dirkx 0:355018f44c9f 178 #if 0
dirkx 0:355018f44c9f 179 if(!m_trmt) //Was receiving
dirkx 0:355018f44c9f 180 {
dirkx 0:355018f44c9f 181 DBG("\r\n>\r\n");
dirkx 0:355018f44c9f 182 m_trmt=true;
dirkx 0:355018f44c9f 183 }
dirkx 0:355018f44c9f 184 #endif
dirkx 0:355018f44c9f 185 // m_pSerial->writeable();
dirkx 0:355018f44c9f 186 // while(!m_pSerial->writeable() /*|| m_pSerial->readable()*/)
dirkx 0:355018f44c9f 187 // {
dirkx 0:355018f44c9f 188 // wait_us(100);
dirkx 0:355018f44c9f 189 // DBG("\r\nWait...\r\n");
dirkx 0:355018f44c9f 190 // }
dirkx 0:355018f44c9f 191 /*
dirkx 0:355018f44c9f 192 NVIC_DisableIRQ(UART1_IRQn);
dirkx 0:355018f44c9f 193 NVIC_DisableIRQ(UART2_IRQn);
dirkx 0:355018f44c9f 194 NVIC_DisableIRQ(UART3_IRQn);
dirkx 0:355018f44c9f 195 */
dirkx 0:355018f44c9f 196 // onSerialInterrupt();
dirkx 0:355018f44c9f 197 m_pStream(putc(c));
dirkx 0:355018f44c9f 198 /* NVIC_EnableIRQ(UART1_IRQn);
dirkx 0:355018f44c9f 199 NVIC_EnableIRQ(UART2_IRQn);
dirkx 0:355018f44c9f 200 NVIC_EnableIRQ(UART3_IRQn);
dirkx 0:355018f44c9f 201 */
dirkx 0:355018f44c9f 202 DBG("%c", c);
dirkx 0:355018f44c9f 203 }
dirkx 0:355018f44c9f 204
dirkx 0:355018f44c9f 205 /* Buffer Stuff */
dirkx 0:355018f44c9f 206
dirkx 0:355018f44c9f 207 bool SerialBuf::readable()
dirkx 0:355018f44c9f 208 {
dirkx 0:355018f44c9f 209 // onSerialInterrupt(); //Flush hw buffer into current buf
dirkx 0:355018f44c9f 210 // DBG("\r\nLen=%d",len());
dirkx 0:355018f44c9f 211 return (len()>0);
dirkx 0:355018f44c9f 212 }
dirkx 0:355018f44c9f 213
dirkx 0:355018f44c9f 214 bool SerialBuf::writeable()
dirkx 0:355018f44c9f 215 {
dirkx 0:355018f44c9f 216 return m_pStream(writeable());
dirkx 0:355018f44c9f 217 }
dirkx 0:355018f44c9f 218
dirkx 0:355018f44c9f 219 void SerialBuf::setReadMode(bool readMode) //If true, keeps chars in buf when read, false by default
dirkx 0:355018f44c9f 220 {
dirkx 0:355018f44c9f 221 if(m_readMode == true && readMode == false)
dirkx 0:355018f44c9f 222 {
dirkx 0:355018f44c9f 223 //Trash bytes that have been read
dirkx 0:355018f44c9f 224 flushRead();
dirkx 0:355018f44c9f 225 }
dirkx 0:355018f44c9f 226 m_readMode=readMode;
dirkx 0:355018f44c9f 227 }
dirkx 0:355018f44c9f 228
dirkx 0:355018f44c9f 229 void SerialBuf::flushRead() //Delete chars that have been read (only useful with readMode = true)
dirkx 0:355018f44c9f 230 {
dirkx 0:355018f44c9f 231 m_pReadStart = m_pRead;
dirkx 0:355018f44c9f 232 }
dirkx 0:355018f44c9f 233
dirkx 0:355018f44c9f 234 void SerialBuf::resetRead() //Go back to initial read position (only useful with readMode = true)
dirkx 0:355018f44c9f 235 {
dirkx 0:355018f44c9f 236 m_pRead = m_pReadStart;
dirkx 0:355018f44c9f 237 }
dirkx 0:355018f44c9f 238
dirkx 0:355018f44c9f 239 bool SerialBuf::onRead()
dirkx 0:355018f44c9f 240 {
dirkx 0:355018f44c9f 241 return false;
dirkx 0:355018f44c9f 242 //Nothing here
dirkx 0:355018f44c9f 243 }
dirkx 0:355018f44c9f 244
dirkx 0:355018f44c9f 245 char SerialBuf::get() //Get a char from buf
dirkx 0:355018f44c9f 246 {
dirkx 0:355018f44c9f 247 //WARN: Must call len() before
dirkx 0:355018f44c9f 248 char c = *m_pRead;
dirkx 0:355018f44c9f 249 m_pRead++;
dirkx 0:355018f44c9f 250
dirkx 0:355018f44c9f 251 if(m_pRead>=m_buf+m_bufLen)
dirkx 0:355018f44c9f 252 m_pRead=m_buf;
dirkx 0:355018f44c9f 253
dirkx 0:355018f44c9f 254 if(!m_readMode) //If readmode=false, trash this char
dirkx 0:355018f44c9f 255 m_pReadStart=m_pRead;
dirkx 0:355018f44c9f 256
dirkx 0:355018f44c9f 257 if(m_pRead==m_pReadByInt) //Next message can be processed by interrupt
dirkx 0:355018f44c9f 258 m_intCanReadData = true;
dirkx 0:355018f44c9f 259 /* else if(m_intCanReadData) //Increment this address
dirkx 0:355018f44c9f 260 m_pReadByInt=m_pRead;*/
dirkx 0:355018f44c9f 261 return c;
dirkx 0:355018f44c9f 262 }
dirkx 0:355018f44c9f 263
dirkx 0:355018f44c9f 264 void SerialBuf::put(char c) //Put a char in buf
dirkx 0:355018f44c9f 265 {
dirkx 0:355018f44c9f 266 //WARN: Must call room() before
dirkx 0:355018f44c9f 267 *m_pWrite = c;
dirkx 0:355018f44c9f 268 m_pWrite++;
dirkx 0:355018f44c9f 269 if(m_pWrite>=m_buf+m_bufLen)
dirkx 0:355018f44c9f 270 m_pWrite=m_buf;
dirkx 0:355018f44c9f 271 }
dirkx 0:355018f44c9f 272
dirkx 0:355018f44c9f 273 int SerialBuf::room() //Return room available in buf
dirkx 0:355018f44c9f 274 {
dirkx 0:355018f44c9f 275 //return m_bufLen - len() - 1; //-1 is to avoid loop
dirkx 0:355018f44c9f 276 if ( m_pReadStart > m_pWrite )
dirkx 0:355018f44c9f 277 return ( m_pReadStart - m_pWrite - 1 );
dirkx 0:355018f44c9f 278 else
dirkx 0:355018f44c9f 279 return m_bufLen - ( m_pWrite - m_pReadStart ) - 1;
dirkx 0:355018f44c9f 280 }
dirkx 0:355018f44c9f 281
dirkx 0:355018f44c9f 282 int SerialBuf::len() //Return chars length in buf
dirkx 0:355018f44c9f 283 {
dirkx 0:355018f44c9f 284 if ( m_pWrite >= m_pRead )
dirkx 0:355018f44c9f 285 return ( m_pWrite - m_pRead );
dirkx 0:355018f44c9f 286 else
dirkx 0:355018f44c9f 287 return m_bufLen - ( m_pRead - m_pWrite ); // = ( ( m_buf + m_bufLen) - m_pRead ) + ( m_pWrite - m_buf )
dirkx 0:355018f44c9f 288 }
dirkx 0:355018f44c9f 289
dirkx 0:355018f44c9f 290 #endif