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