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:
Sat Jun 12 06:01:50 2010 +0000
Revision:
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 #ifndef ATIF_H
iva2k 0:e614f7875b60 25 #define ATIF_H
iva2k 0:e614f7875b60 26
iva2k 0:e614f7875b60 27 #include "netCfg.h"
iva2k 0:e614f7875b60 28
iva2k 0:e614f7875b60 29 #include "mbed.h"
iva2k 0:e614f7875b60 30 #include "drv/serial/buf/SerialBuf.h"
iva2k 0:e614f7875b60 31 #include <list>
iva2k 0:e614f7875b60 32 using std::list;
iva2k 0:e614f7875b60 33
iva2k 0:e614f7875b60 34 //class Serial; //Pb w forward decl
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36 enum ATErr
iva2k 0:e614f7875b60 37 {
iva2k 0:e614f7875b60 38 __AT_MIN = -0xFFFF,
iva2k 0:e614f7875b60 39 AT_CLOSED,
iva2k 0:e614f7875b60 40 AT_NOANSWER,
iva2k 0:e614f7875b60 41 AT_ERROR,
iva2k 0:e614f7875b60 42 AT_TIMEOUT,
iva2k 0:e614f7875b60 43 AT_BUSY,
iva2k 0:e614f7875b60 44 AT_PARSE,
iva2k 0:e614f7875b60 45 AT_INCOMPLETE,
iva2k 0:e614f7875b60 46 AT_OK = 0
iva2k 0:e614f7875b60 47 };
iva2k 0:e614f7875b60 48
iva2k 0:e614f7875b60 49 class ATIf : public SerialBuf
iva2k 0:e614f7875b60 50 {
iva2k 0:e614f7875b60 51 public:
iva2k 0:e614f7875b60 52
iva2k 0:e614f7875b60 53 ATIf();
iva2k 0:e614f7875b60 54 virtual ~ATIf();
iva2k 0:e614f7875b60 55
iva2k 0:e614f7875b60 56 template<class T>
iva2k 0:e614f7875b60 57 //void attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ); //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn
iva2k 0:e614f7875b60 58 //Linker bug : Must be defined here :(
iva2k 0:e614f7875b60 59 void attachSignal( const char* sigName, T* pItem, bool (T::*pMethod)(ATIf*, bool, bool*) ) //Attach Signal ("Unsollicited response code" in Telit_AT_Reference_Guide.pdf) to an handler fn
iva2k 0:e614f7875b60 60 {
iva2k 0:e614f7875b60 61 ATSigHandler sig(sigName, (ATSigHandler::CDummy*)pItem, (bool (ATSigHandler::CDummy::*)(ATIf*, bool, bool*))pMethod);
iva2k 0:e614f7875b60 62 m_signals.push_back(sig);
iva2k 0:e614f7875b60 63 }
iva2k 0:e614f7875b60 64 void detachSignal( const char* sigName );
iva2k 0:e614f7875b60 65
iva2k 0:e614f7875b60 66 ATErr open(Serial* pSerial); //Deactivate echo, etc
iva2k 0:e614f7875b60 67 #if NET_USB_SERIAL
iva2k 0:e614f7875b60 68 ATErr open(UsbSerial* pUsbSerial); //Deactivate echo, etc
iva2k 0:e614f7875b60 69 #endif
iva2k 0:e614f7875b60 70 ATErr close(); //Release port
iva2k 0:e614f7875b60 71
iva2k 0:e614f7875b60 72 int printf(const char* format, ... );
iva2k 0:e614f7875b60 73 int scanf(const char* format, ... );
iva2k 0:e614f7875b60 74 void setTimeout(int timeout); //used by scanf
iva2k 0:e614f7875b60 75 void setLineMode(bool lineMode); //Switch btw line & raw fns
iva2k 0:e614f7875b60 76 void setSignals(bool signalsEnable);
iva2k 0:e614f7875b60 77 ATErr flushBuffer(); //Discard input buffer
iva2k 0:e614f7875b60 78 ATErr flushLine(int timeout); //Discard input buffer until CRLF is encountered
iva2k 0:e614f7875b60 79
iva2k 0:e614f7875b60 80 protected:
iva2k 0:e614f7875b60 81 virtual bool onRead(); //Inherited from SerialBuf, return true if data is incomplete
iva2k 0:e614f7875b60 82 ATErr rawOpen(Serial* pSerial, int baudrate); //Simple open function for similar non-conforming protocols
iva2k 0:e614f7875b60 83
iva2k 0:e614f7875b60 84 public:
iva2k 0:e614f7875b60 85 /* ATErr command(const char* cmd, char* result, int resultLen, int timeout); */ //Kinda useless
iva2k 0:e614f7875b60 86 ATErr write(const char* cmd, bool lineMode = false);
iva2k 0:e614f7875b60 87 ATErr read(char* result, int resultMaxLen, int timeout, bool lineMode = false, int resultMinLen = 0);
iva2k 0:e614f7875b60 88 bool isOpen();
iva2k 0:e614f7875b60 89 ATErr checkOK(); //Helper fn to quickly check that OK has been returned
iva2k 0:e614f7875b60 90
iva2k 0:e614f7875b60 91 private:
iva2k 0:e614f7875b60 92 int readLine(char* line, int maxLen, int timeout); //Read a single line from serial port
iva2k 0:e614f7875b60 93 int writeLine(const char* line); //Write a single line to serial port
iva2k 0:e614f7875b60 94
iva2k 0:e614f7875b60 95 int readRaw(char* str, int maxLen, int timeout = 0, int minLen = 0); //Read from serial port in buf
iva2k 0:e614f7875b60 96 int writeRaw(const char* str); //Write directly to serial port
iva2k 0:e614f7875b60 97
iva2k 0:e614f7875b60 98 volatile int m_readTimeout;
iva2k 0:e614f7875b60 99 volatile bool m_lineMode;
iva2k 0:e614f7875b60 100 bool m_signalsEnable;
iva2k 0:e614f7875b60 101 bool m_isOpen;
iva2k 0:e614f7875b60 102
iva2k 0:e614f7875b60 103 char* m_tmpBuf;
iva2k 0:e614f7875b60 104
iva2k 0:e614f7875b60 105 class ATSigHandler
iva2k 0:e614f7875b60 106 {
iva2k 0:e614f7875b60 107 class CDummy;
iva2k 0:e614f7875b60 108 public:
iva2k 0:e614f7875b60 109 ATSigHandler(const char* name, CDummy* cbObj, bool (CDummy::*cbMeth)(ATIf* pIf, bool beg, bool* pMoreData)) : m_cbObj(cbObj), m_cbMeth(cbMeth), m_name(name)
iva2k 0:e614f7875b60 110 {}
iva2k 0:e614f7875b60 111 protected:
iva2k 0:e614f7875b60 112 CDummy* m_cbObj;
iva2k 0:e614f7875b60 113 bool (CDummy::*m_cbMeth)(ATIf* pIf, bool beg, bool* pMoreData); //*pMoreData set to true if needs to read next line, beg = true if beginning of new code
iva2k 0:e614f7875b60 114 const char* m_name;
iva2k 0:e614f7875b60 115
iva2k 0:e614f7875b60 116 friend class ATIf;
iva2k 0:e614f7875b60 117 };
iva2k 0:e614f7875b60 118
iva2k 0:e614f7875b60 119 volatile ATSigHandler* m_pCurrentSignal; //Signal that asked more data
iva2k 0:e614f7875b60 120
iva2k 0:e614f7875b60 121 bool handleSignal(); //Returns true if signal has been handled
iva2k 0:e614f7875b60 122 list<ATSigHandler> m_signals;
iva2k 0:e614f7875b60 123
iva2k 0:e614f7875b60 124 };
iva2k 0:e614f7875b60 125
iva2k 0:e614f7875b60 126 #endif