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 /*
iva2k 0:e614f7875b60 25
iva2k 0:e614f7875b60 26 This is a wrapper around Serial if for lwIP (acts as a serial driver)
iva2k 0:e614f7875b60 27
iva2k 0:e614f7875b60 28 See sio.h for functions to be implemented
iva2k 0:e614f7875b60 29
iva2k 0:e614f7875b60 30 sio_fd_t is a void* defined type, we use it as a SerialBuf ptr
iva2k 0:e614f7875b60 31
iva2k 0:e614f7875b60 32
iva2k 0:e614f7875b60 33
iva2k 0:e614f7875b60 34 */
iva2k 0:e614f7875b60 35
iva2k 0:e614f7875b60 36
iva2k 0:e614f7875b60 37 #include "netCfg.h"
iva2k 0:e614f7875b60 38 #if NET_PPP
iva2k 0:e614f7875b60 39
iva2k 0:e614f7875b60 40
iva2k 0:e614f7875b60 41 #define MAX_SERIAL_PORTS 8
iva2k 0:e614f7875b60 42
iva2k 0:e614f7875b60 43 #include "lwip/sio.h"
iva2k 0:e614f7875b60 44 #include "mbed.h"
iva2k 0:e614f7875b60 45 #include "sioMgr.h"
iva2k 0:e614f7875b60 46
iva2k 0:e614f7875b60 47 //extern "C" {
iva2k 0:e614f7875b60 48
iva2k 0:e614f7875b60 49 /**
iva2k 0:e614f7875b60 50 * Opens a serial device for communication.
iva2k 0:e614f7875b60 51 *
iva2k 0:e614f7875b60 52 * @param devnum device number
iva2k 0:e614f7875b60 53 * @return handle to serial device if successful, NULL otherwise
iva2k 0:e614f7875b60 54 */
iva2k 0:e614f7875b60 55 sio_fd_t sio_open(u8_t devnum)
iva2k 0:e614f7875b60 56 {
iva2k 0:e614f7875b60 57 SerialBuf* pIf = SioMgr::getIf(devnum);
iva2k 0:e614f7875b60 58 if(pIf == NULL)
iva2k 0:e614f7875b60 59 return NULL;
iva2k 0:e614f7875b60 60
iva2k 0:e614f7875b60 61 //Got a SerialBuf* object
iva2k 0:e614f7875b60 62 //WARN: It HAS to be initialised (instanciated + attached to a Serial obj)
iva2k 0:e614f7875b60 63
iva2k 0:e614f7875b60 64 return (sio_fd_t*) pIf;
iva2k 0:e614f7875b60 65 }
iva2k 0:e614f7875b60 66
iva2k 0:e614f7875b60 67 /**
iva2k 0:e614f7875b60 68 * Sends a single character to the serial device.
iva2k 0:e614f7875b60 69 *
iva2k 0:e614f7875b60 70 * @param c character to send
iva2k 0:e614f7875b60 71 * @param fd serial device handle
iva2k 0:e614f7875b60 72 *
iva2k 0:e614f7875b60 73 * @note This function will block until the character can be sent.
iva2k 0:e614f7875b60 74 */
iva2k 0:e614f7875b60 75 void sio_send(u8_t c, sio_fd_t fd)
iva2k 0:e614f7875b60 76 {
iva2k 0:e614f7875b60 77 SerialBuf* pIf = (SerialBuf*) fd;
iva2k 0:e614f7875b60 78 //while(!pIf->writeable());
iva2k 0:e614f7875b60 79 pIf->putc( (char) c );
iva2k 0:e614f7875b60 80 }
iva2k 0:e614f7875b60 81
iva2k 0:e614f7875b60 82 /**
iva2k 0:e614f7875b60 83 * Receives a single character from the serial device.
iva2k 0:e614f7875b60 84 *
iva2k 0:e614f7875b60 85 * @param fd serial device handle
iva2k 0:e614f7875b60 86 *
iva2k 0:e614f7875b60 87 * @note This function will block until a character is received.
iva2k 0:e614f7875b60 88 */
iva2k 0:e614f7875b60 89 u8_t sio_recv(sio_fd_t fd)
iva2k 0:e614f7875b60 90 {
iva2k 0:e614f7875b60 91 SerialBuf* pIf = (SerialBuf*) fd;
iva2k 0:e614f7875b60 92 pIf->setReadMode(false);
iva2k 0:e614f7875b60 93 while(!pIf->readable());
iva2k 0:e614f7875b60 94 return (u8_t) pIf->getc();
iva2k 0:e614f7875b60 95 }
iva2k 0:e614f7875b60 96
iva2k 0:e614f7875b60 97 /**
iva2k 0:e614f7875b60 98 * Reads from the serial device.
iva2k 0:e614f7875b60 99 *
iva2k 0:e614f7875b60 100 * @param fd serial device handle
iva2k 0:e614f7875b60 101 * @param data pointer to data buffer for receiving
iva2k 0:e614f7875b60 102 * @param len maximum length (in bytes) of data to receive
iva2k 0:e614f7875b60 103 * @return number of bytes actually received - may be 0 if aborted by sio_read_abort
iva2k 0:e614f7875b60 104 *
iva2k 0:e614f7875b60 105 * @note This function will block until data can be received. The blocking
iva2k 0:e614f7875b60 106 * can be cancelled by calling sio_read_abort().
iva2k 0:e614f7875b60 107 */
iva2k 0:e614f7875b60 108 static volatile bool m_abort = false;
iva2k 0:e614f7875b60 109 u32_t sio_read(sio_fd_t fd, u8_t *data, u32_t len)
iva2k 0:e614f7875b60 110 {
iva2k 0:e614f7875b60 111 u32_t recvd = 0; //bytes received
iva2k 0:e614f7875b60 112 SerialBuf* pIf = (SerialBuf*) fd;
iva2k 0:e614f7875b60 113 pIf->setReadMode(false);
iva2k 0:e614f7875b60 114 while(!m_abort && len)
iva2k 0:e614f7875b60 115 {
iva2k 0:e614f7875b60 116 while(!pIf->readable());
iva2k 0:e614f7875b60 117 *data = (u8_t) pIf->getc();
iva2k 0:e614f7875b60 118 data++;
iva2k 0:e614f7875b60 119 len--;
iva2k 0:e614f7875b60 120 recvd++;
iva2k 0:e614f7875b60 121 }
iva2k 0:e614f7875b60 122 m_abort = false;
iva2k 0:e614f7875b60 123 return recvd;
iva2k 0:e614f7875b60 124 }
iva2k 0:e614f7875b60 125
iva2k 0:e614f7875b60 126 /**
iva2k 0:e614f7875b60 127 * Tries to read from the serial device. Same as sio_read but returns
iva2k 0:e614f7875b60 128 * immediately if no data is available and never blocks.
iva2k 0:e614f7875b60 129 *
iva2k 0:e614f7875b60 130 * @param fd serial device handle
iva2k 0:e614f7875b60 131 * @param data pointer to data buffer for receiving
iva2k 0:e614f7875b60 132 * @param len maximum length (in bytes) of data to receive
iva2k 0:e614f7875b60 133 * @return number of bytes actually received
iva2k 0:e614f7875b60 134 */
iva2k 0:e614f7875b60 135 u32_t sio_tryread(sio_fd_t fd, u8_t *data, u32_t len)
iva2k 0:e614f7875b60 136 {
iva2k 0:e614f7875b60 137 u32_t recvd = 0; //bytes received
iva2k 0:e614f7875b60 138 SerialBuf* pIf = (SerialBuf*) fd;
iva2k 0:e614f7875b60 139 pIf->setReadMode(false);
iva2k 0:e614f7875b60 140 while(len)
iva2k 0:e614f7875b60 141 {
iva2k 0:e614f7875b60 142 /* if(!pIf->readable())
iva2k 0:e614f7875b60 143 {
iva2k 0:e614f7875b60 144 wait_ms(4);
iva2k 0:e614f7875b60 145 }*/
iva2k 0:e614f7875b60 146 if(!pIf->readable())
iva2k 0:e614f7875b60 147 {
iva2k 0:e614f7875b60 148 return recvd;
iva2k 0:e614f7875b60 149 }
iva2k 0:e614f7875b60 150 *data = (u8_t) pIf->getc();
iva2k 0:e614f7875b60 151 data++;
iva2k 0:e614f7875b60 152 len--;
iva2k 0:e614f7875b60 153 recvd++;
iva2k 0:e614f7875b60 154 }
iva2k 0:e614f7875b60 155 return recvd;
iva2k 0:e614f7875b60 156 }
iva2k 0:e614f7875b60 157
iva2k 0:e614f7875b60 158 /**
iva2k 0:e614f7875b60 159 * Writes to the serial device.
iva2k 0:e614f7875b60 160 *
iva2k 0:e614f7875b60 161 * @param fd serial device handle
iva2k 0:e614f7875b60 162 * @param data pointer to data to send
iva2k 0:e614f7875b60 163 * @param len length (in bytes) of data to send
iva2k 0:e614f7875b60 164 * @return number of bytes actually sent
iva2k 0:e614f7875b60 165 *
iva2k 0:e614f7875b60 166 * @note This function will block until all data can be sent.
iva2k 0:e614f7875b60 167 */
iva2k 0:e614f7875b60 168 u32_t sio_write(sio_fd_t fd, u8_t *data, u32_t len)
iva2k 0:e614f7875b60 169 {
iva2k 0:e614f7875b60 170 u32_t sent = 0; //bytes sent
iva2k 0:e614f7875b60 171 SerialBuf* pIf = (SerialBuf*) fd;
iva2k 0:e614f7875b60 172 while(len)
iva2k 0:e614f7875b60 173 {
iva2k 0:e614f7875b60 174 //while(!pIf->writeable());
iva2k 0:e614f7875b60 175 pIf->putc(*data);
iva2k 0:e614f7875b60 176 data++;
iva2k 0:e614f7875b60 177 len--;
iva2k 0:e614f7875b60 178 sent++;
iva2k 0:e614f7875b60 179 }
iva2k 0:e614f7875b60 180 return sent; //Well, this is bound to be len if no interrupt mechanism
iva2k 0:e614f7875b60 181 }
iva2k 0:e614f7875b60 182
iva2k 0:e614f7875b60 183 /**
iva2k 0:e614f7875b60 184 * Aborts a blocking sio_read() call.
iva2k 0:e614f7875b60 185 *
iva2k 0:e614f7875b60 186 * @param fd serial device handle
iva2k 0:e614f7875b60 187 */
iva2k 0:e614f7875b60 188 void sio_read_abort(sio_fd_t fd)
iva2k 0:e614f7875b60 189 {
iva2k 0:e614f7875b60 190 m_abort = true;
iva2k 0:e614f7875b60 191 }
iva2k 0:e614f7875b60 192
iva2k 0:e614f7875b60 193 //}
iva2k 0:e614f7875b60 194
iva2k 0:e614f7875b60 195 #endif
iva2k 0:e614f7875b60 196