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 "lwipNetUdpSocket.h"
iva2k 0:e614f7875b60 25 #include "lwip/udp.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_LWIP_STACK
iva2k 0:e614f7875b60 32
iva2k 0:e614f7875b60 33 LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : m_pPcb(pPcb), m_lInPkt() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
iva2k 0:e614f7875b60 34 {
iva2k 1:3ee499525aa5 35 DBG("New NetUdpSocket %p\r\n", (void*)this);
iva2k 0:e614f7875b60 36 if(!m_pPcb)
iva2k 0:e614f7875b60 37 m_pPcb = udp_new();
iva2k 0:e614f7875b60 38 if(m_pPcb)
iva2k 0:e614f7875b60 39 {
iva2k 0:e614f7875b60 40 //Setup callback
iva2k 0:e614f7875b60 41 // udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
iva2k 0:e614f7875b60 42 }
iva2k 0:e614f7875b60 43 }
iva2k 0:e614f7875b60 44
iva2k 0:e614f7875b60 45 LwipNetUdpSocket::~LwipNetUdpSocket()
iva2k 0:e614f7875b60 46 {
iva2k 0:e614f7875b60 47 close();
iva2k 0:e614f7875b60 48 }
iva2k 0:e614f7875b60 49
iva2k 0:e614f7875b60 50 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
iva2k 0:e614f7875b60 51 {
iva2k 0:e614f7875b60 52 if(!m_pPcb)
iva2k 0:e614f7875b60 53 return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
iva2k 0:e614f7875b60 54
iva2k 0:e614f7875b60 55 /* err_t err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
iva2k 0:e614f7875b60 56 if(err)
iva2k 0:e614f7875b60 57 return NETUDPSOCKET_INUSE;*/
iva2k 0:e614f7875b60 58
iva2k 0:e614f7875b60 59 //Setup callback
iva2k 0:e614f7875b60 60 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
iva2k 0:e614f7875b60 61
iva2k 0:e614f7875b60 62 return NETUDPSOCKET_OK;
iva2k 0:e614f7875b60 63 }
iva2k 0:e614f7875b60 64
iva2k 0:e614f7875b60 65 #define MAX(a,b) ((a>b)?a:b)
iva2k 0:e614f7875b60 66 #define MIN(a,b) ((a<b)?a:b)
iva2k 0:e614f7875b60 67
iva2k 0:e614f7875b60 68 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost)
iva2k 0:e614f7875b60 69 {
iva2k 0:e614f7875b60 70 if( !m_pPcb ) //Pcb doesn't exist (anymore)
iva2k 0:e614f7875b60 71 return NETUDPSOCKET_MEM;
iva2k 0:e614f7875b60 72 pbuf* p = pbuf_alloc(PBUF_RAW, len, PBUF_RAM);
iva2k 0:e614f7875b60 73 if( !p )
iva2k 0:e614f7875b60 74 return NETUDPSOCKET_MEM;
iva2k 0:e614f7875b60 75 memcpy (p->payload, (void*)buf, len);
iva2k 0:e614f7875b60 76 //udp_connect( (udp_pcb*) m_pPcb, &(pHost->getIp().getStruct()), pHost->getPort() );
iva2k 0:e614f7875b60 77 //err_t err = udp_send( (udp_pcb*) m_pPcb, p);
iva2k 0:e614f7875b60 78 err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() );
iva2k 0:e614f7875b60 79 pbuf_free( p );
iva2k 0:e614f7875b60 80 if(err)
iva2k 0:e614f7875b60 81 return NETUDPSOCKET_SETUP; //Connection problem
iva2k 1:3ee499525aa5 82 DBG("%d bytes sent in UDP Socket.\r\n", len);
iva2k 0:e614f7875b60 83 return len;
iva2k 0:e614f7875b60 84 }
iva2k 0:e614f7875b60 85
iva2k 0:e614f7875b60 86 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost)
iva2k 0:e614f7875b60 87 {
iva2k 0:e614f7875b60 88 if( !m_pPcb ) //Pcb doesn't exist (anymore)
iva2k 0:e614f7875b60 89 return NETUDPSOCKET_MEM;
iva2k 0:e614f7875b60 90 int inLen = 0;
iva2k 0:e614f7875b60 91 int cpyLen = 0;
iva2k 0:e614f7875b60 92
iva2k 0:e614f7875b60 93 static int rmgLen = 0;
iva2k 0:e614f7875b60 94 //Contains the remaining len in this pbuf
iva2k 0:e614f7875b60 95
iva2k 0:e614f7875b60 96 if( m_lInPkt.empty() )
iva2k 0:e614f7875b60 97 return 0;
iva2k 0:e614f7875b60 98
iva2k 0:e614f7875b60 99 pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf;
iva2k 0:e614f7875b60 100
iva2k 0:e614f7875b60 101 if(pHost)
iva2k 0:e614f7875b60 102 *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port );
iva2k 0:e614f7875b60 103
iva2k 0:e614f7875b60 104 if( !pBuf )
iva2k 0:e614f7875b60 105 {
iva2k 0:e614f7875b60 106 rmgLen = 0;
iva2k 0:e614f7875b60 107 return 0;
iva2k 0:e614f7875b60 108 }
iva2k 0:e614f7875b60 109
iva2k 0:e614f7875b60 110 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
iva2k 0:e614f7875b60 111 {
iva2k 0:e614f7875b60 112 rmgLen = pBuf->len;
iva2k 0:e614f7875b60 113 }
iva2k 0:e614f7875b60 114
iva2k 0:e614f7875b60 115 while ( inLen < len )
iva2k 0:e614f7875b60 116 {
iva2k 0:e614f7875b60 117 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
iva2k 0:e614f7875b60 118 memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen);
iva2k 0:e614f7875b60 119 inLen += cpyLen;
iva2k 0:e614f7875b60 120 buf += cpyLen;
iva2k 0:e614f7875b60 121
iva2k 0:e614f7875b60 122 rmgLen = rmgLen - cpyLen; //Update rmgLen
iva2k 0:e614f7875b60 123
iva2k 0:e614f7875b60 124 if( rmgLen > 0 )
iva2k 0:e614f7875b60 125 {
iva2k 0:e614f7875b60 126 //We did not read this pbuf completely, so let's save it's pos & return
iva2k 0:e614f7875b60 127 break;
iva2k 0:e614f7875b60 128 }
iva2k 0:e614f7875b60 129
iva2k 0:e614f7875b60 130 if(pBuf->next)
iva2k 0:e614f7875b60 131 {
iva2k 0:e614f7875b60 132 pbuf* pNextPBuf = pBuf->next;
iva2k 0:e614f7875b60 133 pBuf->next = NULL; //So that it is not freed as well
iva2k 0:e614f7875b60 134 //We get the reference to pNextPBuf from m_pReadPbuf
iva2k 0:e614f7875b60 135 pbuf_free((pbuf*)pBuf);
iva2k 0:e614f7875b60 136 pBuf = pNextPBuf;
iva2k 0:e614f7875b60 137 rmgLen = pBuf->len;
iva2k 0:e614f7875b60 138 }
iva2k 0:e614f7875b60 139 else
iva2k 0:e614f7875b60 140 {
iva2k 0:e614f7875b60 141 pbuf_free((pbuf*)pBuf);
iva2k 0:e614f7875b60 142 pBuf = NULL;
iva2k 0:e614f7875b60 143 rmgLen = 0;
iva2k 0:e614f7875b60 144 m_lInPkt.pop_front();
iva2k 0:e614f7875b60 145 break; //No more data to read
iva2k 0:e614f7875b60 146 }
iva2k 0:e614f7875b60 147 }
iva2k 0:e614f7875b60 148
iva2k 0:e614f7875b60 149 return inLen;
iva2k 0:e614f7875b60 150 }
iva2k 0:e614f7875b60 151
iva2k 0:e614f7875b60 152 NetUdpSocketErr LwipNetUdpSocket::close()
iva2k 0:e614f7875b60 153 {
iva2k 1:3ee499525aa5 154 //DBG("LwipNetUdpSocket::close() : Closing...\r\n");
iva2k 0:e614f7875b60 155
iva2k 0:e614f7875b60 156 if(m_closed)
iva2k 0:e614f7875b60 157 return NETUDPSOCKET_OK; //Already being closed
iva2k 0:e614f7875b60 158 m_closed = true;
iva2k 0:e614f7875b60 159
iva2k 0:e614f7875b60 160 if( !m_pPcb ) //Pcb doesn't exist (anymore)
iva2k 0:e614f7875b60 161 return NETUDPSOCKET_MEM;
iva2k 0:e614f7875b60 162
iva2k 0:e614f7875b60 163 //Cleanup incoming data
iva2k 0:e614f7875b60 164 cleanUp();
iva2k 0:e614f7875b60 165
iva2k 0:e614f7875b60 166 udp_remove( (udp_pcb*) m_pPcb);
iva2k 0:e614f7875b60 167
iva2k 0:e614f7875b60 168 m_pPcb = NULL;
iva2k 0:e614f7875b60 169 return NETUDPSOCKET_OK;
iva2k 0:e614f7875b60 170 }
iva2k 0:e614f7875b60 171
iva2k 0:e614f7875b60 172 NetUdpSocketErr LwipNetUdpSocket::poll()
iva2k 0:e614f7875b60 173 {
iva2k 0:e614f7875b60 174 NetUdpSocket::flushEvents();
iva2k 0:e614f7875b60 175 return NETUDPSOCKET_OK;
iva2k 0:e614f7875b60 176 }
iva2k 0:e614f7875b60 177
iva2k 0:e614f7875b60 178 // Callbacks events
iva2k 0:e614f7875b60 179
iva2k 0:e614f7875b60 180 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
iva2k 0:e614f7875b60 181 {
iva2k 1:3ee499525aa5 182 DBG(" Packet of length %d arrived in UDP Socket.\r\n", p->tot_len);
iva2k 0:e614f7875b60 183 list<InPacket>::iterator it;
iva2k 0:e614f7875b60 184 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
iva2k 0:e614f7875b60 185 {
iva2k 0:e614f7875b60 186 if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) )
iva2k 0:e614f7875b60 187 {
iva2k 0:e614f7875b60 188 //Let's tail this packet to the previous one
iva2k 0:e614f7875b60 189 pbuf_cat((pbuf*)(*it).pBuf, p);
iva2k 0:e614f7875b60 190 //No need to queue an event in that case since the read buf has not been processed yet
iva2k 0:e614f7875b60 191 return;
iva2k 0:e614f7875b60 192 }
iva2k 0:e614f7875b60 193 }
iva2k 0:e614f7875b60 194
iva2k 0:e614f7875b60 195 //New host, add a packet to the queue
iva2k 0:e614f7875b60 196 InPacket pkt;
iva2k 0:e614f7875b60 197 pkt.pBuf = p;
iva2k 0:e614f7875b60 198 pkt.addr = *addr;
iva2k 0:e614f7875b60 199 pkt.port = port;
iva2k 0:e614f7875b60 200 m_lInPkt.push_back(pkt);
iva2k 0:e614f7875b60 201
iva2k 0:e614f7875b60 202 queueEvent(NETUDPSOCKET_READABLE);
iva2k 0:e614f7875b60 203 }
iva2k 0:e614f7875b60 204
iva2k 0:e614f7875b60 205 void LwipNetUdpSocket::cleanUp() //Flush input buffer
iva2k 0:e614f7875b60 206 {
iva2k 0:e614f7875b60 207 list<InPacket>::iterator it;
iva2k 0:e614f7875b60 208 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
iva2k 0:e614f7875b60 209 {
iva2k 0:e614f7875b60 210 //Free buf
iva2k 0:e614f7875b60 211 pbuf_free((pbuf*)(*it).pBuf);
iva2k 0:e614f7875b60 212 }
iva2k 0:e614f7875b60 213 recvfrom(NULL, 0, NULL);
iva2k 0:e614f7875b60 214 m_lInPkt.clear();
iva2k 0:e614f7875b60 215 }
iva2k 0:e614f7875b60 216
iva2k 0:e614f7875b60 217 // Static callback from LwIp
iva2k 0:e614f7875b60 218
iva2k 0:e614f7875b60 219 void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
iva2k 0:e614f7875b60 220 {
iva2k 0:e614f7875b60 221 LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg;
iva2k 0:e614f7875b60 222 return pMe->recvCb( pcb, p, addr, port );
iva2k 0:e614f7875b60 223 }
iva2k 0:e614f7875b60 224
iva2k 0:e614f7875b60 225 #endif