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.

Dependencies:   mbed

Committer:
iva2k
Date:
Mon Jun 14 03:24:33 2010 +0000
Revision:
1:3ee499525aa5
Parent:
0:e614f7875b60

        

Who changed what in which revision?

UserRevisionLine numberNew 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 "UsbSerial.h"
iva2k 0:e614f7875b60 25 #include "usbserialif.h"
iva2k 0:e614f7875b60 26 #include "rpc.h"
iva2k 0:e614f7875b60 27
iva2k 0:e614f7875b60 28 #include "netCfg.h"
iva2k 0:e614f7875b60 29 #if NET_USB_SERIAL
iva2k 0:e614f7875b60 30
iva2k 0:e614f7875b60 31 namespace mbed {
iva2k 0:e614f7875b60 32
iva2k 0:e614f7875b60 33 #define BUF_LEN 64
iva2k 0:e614f7875b60 34 #define FLUSH_TMOUT 10000 //US
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36 UsbSerial::UsbSerial(int usbDev, int usbIf, const char *name /*= NULL*/) : Stream(name), m_txTimeout() {
iva2k 0:e614f7875b60 37 m_inBufEven = new char[BUF_LEN];
iva2k 0:e614f7875b60 38 m_inBufOdd = new char[BUF_LEN];
iva2k 0:e614f7875b60 39 m_pInBufPos = m_inBufUsr = m_inBufEven;
iva2k 0:e614f7875b60 40 m_inBufTrmt = m_inBufOdd;
iva2k 0:e614f7875b60 41
iva2k 0:e614f7875b60 42 m_outBufEven = new char[BUF_LEN];
iva2k 0:e614f7875b60 43 m_outBufOdd = new char[BUF_LEN];
iva2k 0:e614f7875b60 44 m_pOutBufPos = m_outBufUsr = m_outBufEven;
iva2k 0:e614f7875b60 45 m_outBufTrmt = m_outBufOdd;
iva2k 0:e614f7875b60 46
iva2k 0:e614f7875b60 47 m_inBufLen = m_outBufLen = 0;
iva2k 0:e614f7875b60 48
iva2k 0:e614f7875b60 49 // startRx();
iva2k 0:e614f7875b60 50 }
iva2k 0:e614f7875b60 51
iva2k 0:e614f7875b60 52 UsbSerial::~UsbSerial()
iva2k 0:e614f7875b60 53 {
iva2k 0:e614f7875b60 54 delete[] m_inBufEven;
iva2k 0:e614f7875b60 55 delete[] m_inBufOdd;
iva2k 0:e614f7875b60 56 delete[] m_outBufEven;
iva2k 0:e614f7875b60 57 delete[] m_outBufOdd;
iva2k 0:e614f7875b60 58 }
iva2k 0:e614f7875b60 59
iva2k 0:e614f7875b60 60 void UsbSerial::baud(int baudrate) {
iva2k 0:e614f7875b60 61 //
iva2k 0:e614f7875b60 62 }
iva2k 0:e614f7875b60 63
iva2k 0:e614f7875b60 64 void UsbSerial::format(int bits, int parity, int stop) {
iva2k 0:e614f7875b60 65 //
iva2k 0:e614f7875b60 66 }
iva2k 0:e614f7875b60 67
iva2k 0:e614f7875b60 68 int UsbSerial::_getc() {
iva2k 0:e614f7875b60 69 char c;
iva2k 0:e614f7875b60 70 c = *m_pInBufPos;
iva2k 0:e614f7875b60 71 m_pInBufPos++;
iva2k 0:e614f7875b60 72 return c;
iva2k 0:e614f7875b60 73 }
iva2k 0:e614f7875b60 74
iva2k 0:e614f7875b60 75 int UsbSerial::_putc(int c) {
iva2k 0:e614f7875b60 76 m_txTimeout.detach(); //Do not want to be interrupted, accessing shared data here...
iva2k 0:e614f7875b60 77 *m_pOutBufPos = (char) c;
iva2k 0:e614f7875b60 78 m_pOutBufPos++;
iva2k 0:e614f7875b60 79 if( (m_pOutBufPos - m_outBufUsr) == BUF_LEN) //Must flush
iva2k 0:e614f7875b60 80 {
iva2k 0:e614f7875b60 81 startTx();
iva2k 0:e614f7875b60 82 }
iva2k 0:e614f7875b60 83 else
iva2k 0:e614f7875b60 84 {
iva2k 0:e614f7875b60 85 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
iva2k 0:e614f7875b60 86 }
iva2k 0:e614f7875b60 87 return c;
iva2k 0:e614f7875b60 88 }
iva2k 0:e614f7875b60 89
iva2k 0:e614f7875b60 90 int UsbSerial::readable() {
iva2k 0:e614f7875b60 91 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
iva2k 0:e614f7875b60 92 {
iva2k 0:e614f7875b60 93 // printf("\r\nREADABLE\r\n");
iva2k 0:e614f7875b60 94 return true;
iva2k 0:e614f7875b60 95 }
iva2k 0:e614f7875b60 96 else
iva2k 0:e614f7875b60 97 {
iva2k 0:e614f7875b60 98 // printf("\r\nNOT READABLE\r\n");
iva2k 0:e614f7875b60 99 startRx(); //Try to swap packets & start another transmission
iva2k 0:e614f7875b60 100 return ((m_pInBufPos - m_inBufUsr) < m_inBufLen )?true:false;
iva2k 0:e614f7875b60 101 }
iva2k 0:e614f7875b60 102 }
iva2k 0:e614f7875b60 103
iva2k 0:e614f7875b60 104 int UsbSerial::writeable() {
iva2k 0:e614f7875b60 105 // printf("\r\nWRITEABLE???\r\n");
iva2k 0:e614f7875b60 106 return (bool)( (m_pOutBufPos - m_outBufUsr) < BUF_LEN);
iva2k 0:e614f7875b60 107 }
iva2k 0:e614f7875b60 108
iva2k 0:e614f7875b60 109
iva2k 0:e614f7875b60 110 void UsbSerial::startTx()
iva2k 0:e614f7875b60 111 {
iva2k 0:e614f7875b60 112 if( SerialTransmitted() < 0 )
iva2k 0:e614f7875b60 113 {
iva2k 0:e614f7875b60 114 //Wait & retry
iva2k 0:e614f7875b60 115 m_txTimeout.attach_us(this, &UsbSerial::startTx, FLUSH_TMOUT);
iva2k 0:e614f7875b60 116 return;
iva2k 0:e614f7875b60 117 }
iva2k 0:e614f7875b60 118
iva2k 0:e614f7875b60 119 m_outBufLen = m_pOutBufPos - m_outBufUsr;
iva2k 0:e614f7875b60 120
iva2k 0:e614f7875b60 121 //Swap buffers
iva2k 0:e614f7875b60 122 volatile char* swapBuf = m_outBufUsr;
iva2k 0:e614f7875b60 123 m_outBufUsr = m_outBufTrmt;
iva2k 0:e614f7875b60 124 m_outBufTrmt = swapBuf;
iva2k 0:e614f7875b60 125
iva2k 0:e614f7875b60 126 SerialTx((volatile USB_INT08U*)m_outBufTrmt, m_outBufLen);
iva2k 0:e614f7875b60 127
iva2k 0:e614f7875b60 128 m_pOutBufPos = m_outBufUsr;
iva2k 0:e614f7875b60 129
iva2k 0:e614f7875b60 130 }
iva2k 0:e614f7875b60 131
iva2k 0:e614f7875b60 132 void UsbSerial::startRx()
iva2k 0:e614f7875b60 133 {
iva2k 0:e614f7875b60 134 if( (m_pInBufPos - m_inBufUsr) < m_inBufLen )
iva2k 0:e614f7875b60 135 {
iva2k 0:e614f7875b60 136 //User buf is not empty, cannot swap now...
iva2k 0:e614f7875b60 137 return;
iva2k 0:e614f7875b60 138 }
iva2k 0:e614f7875b60 139 int len = SerialReceived();
iva2k 0:e614f7875b60 140 if( len < 0 )
iva2k 0:e614f7875b60 141 {
iva2k 0:e614f7875b60 142 //Previous transmission not completed
iva2k 0:e614f7875b60 143 return;
iva2k 0:e614f7875b60 144 }
iva2k 0:e614f7875b60 145
iva2k 0:e614f7875b60 146 m_inBufLen = len;
iva2k 0:e614f7875b60 147
iva2k 0:e614f7875b60 148 //Swap buffers
iva2k 0:e614f7875b60 149 volatile char* swapBuf = m_inBufUsr;
iva2k 0:e614f7875b60 150 m_inBufUsr = m_inBufTrmt;
iva2k 0:e614f7875b60 151 m_inBufTrmt = swapBuf;
iva2k 0:e614f7875b60 152
iva2k 0:e614f7875b60 153 SerialRx((volatile USB_INT08U*)m_inBufTrmt, BUF_LEN); //Start another transmission
iva2k 0:e614f7875b60 154
iva2k 0:e614f7875b60 155 m_pInBufPos = m_inBufUsr;
iva2k 0:e614f7875b60 156
iva2k 0:e614f7875b60 157 }
iva2k 0:e614f7875b60 158
iva2k 0:e614f7875b60 159
iva2k 0:e614f7875b60 160
iva2k 0:e614f7875b60 161 const struct rpc_method *UsbSerial::get_rpc_methods() {
iva2k 0:e614f7875b60 162 static const rpc_method methods[] = {
iva2k 0:e614f7875b60 163 { "baud", rpc_method_caller<UsbSerial, int, &UsbSerial::baud> },
iva2k 0:e614f7875b60 164 { "format", rpc_method_caller<UsbSerial, int, int, int, &UsbSerial::format> },
iva2k 0:e614f7875b60 165 { "readable", rpc_method_caller<int, UsbSerial, &UsbSerial::readable> },
iva2k 0:e614f7875b60 166 { "writeable", rpc_method_caller<int, UsbSerial, &UsbSerial::writeable> },
iva2k 0:e614f7875b60 167 RPC_METHOD_SUPER(Stream)
iva2k 0:e614f7875b60 168 };
iva2k 0:e614f7875b60 169 return methods;
iva2k 0:e614f7875b60 170 }
iva2k 0:e614f7875b60 171
iva2k 0:e614f7875b60 172 struct rpc_class *UsbSerial::get_rpc_class() {
iva2k 0:e614f7875b60 173 static const rpc_function funcs[] = {
iva2k 0:e614f7875b60 174 { "new", rpc_function_caller<const char*, int, int, const char*, Base::construct<UsbSerial,int,int,const char*> > },
iva2k 0:e614f7875b60 175 RPC_METHOD_END
iva2k 0:e614f7875b60 176 };
iva2k 0:e614f7875b60 177 static rpc_class c = { "UsbSerial", funcs, NULL };
iva2k 0:e614f7875b60 178 return &c;
iva2k 0:e614f7875b60 179 }
iva2k 0:e614f7875b60 180
iva2k 0:e614f7875b60 181 } // namespace mbed
iva2k 0:e614f7875b60 182
iva2k 0:e614f7875b60 183 #endif
iva2k 0:e614f7875b60 184