Small Testprogram to have WebAccess via Webserver to a 433Mhz tranmitter to control remotly some devices from remote, with TFTP, NTP and RMF. This could be a base to develop applications.

Dependencies:   ChaNFSSD TFTPServer RMFWeb

Dependents:   RMFWeb

Committer:
ED7418
Date:
Mon Jun 16 07:40:08 2014 +0000
Revision:
1:809b59c7a800
Parent:
0:51f1ef89ec7b
mbed-lib and other libs are a based on a project, published in a Elektor-book "ARM-microkontroller Part II".

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ED7418 0:51f1ef89ec7b 1
ED7418 0:51f1ef89ec7b 2 /*
ED7418 0:51f1ef89ec7b 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
ED7418 0:51f1ef89ec7b 4
ED7418 0:51f1ef89ec7b 5 Permission is hereby granted, free of charge, to any person obtaining a copy
ED7418 0:51f1ef89ec7b 6 of this software and associated documentation files (the "Software"), to deal
ED7418 0:51f1ef89ec7b 7 in the Software without restriction, including without limitation the rights
ED7418 0:51f1ef89ec7b 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
ED7418 0:51f1ef89ec7b 9 copies of the Software, and to permit persons to whom the Software is
ED7418 0:51f1ef89ec7b 10 furnished to do so, subject to the following conditions:
ED7418 0:51f1ef89ec7b 11
ED7418 0:51f1ef89ec7b 12 The above copyright notice and this permission notice shall be included in
ED7418 0:51f1ef89ec7b 13 all copies or substantial portions of the Software.
ED7418 0:51f1ef89ec7b 14
ED7418 0:51f1ef89ec7b 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
ED7418 0:51f1ef89ec7b 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
ED7418 0:51f1ef89ec7b 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
ED7418 0:51f1ef89ec7b 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
ED7418 0:51f1ef89ec7b 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
ED7418 0:51f1ef89ec7b 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
ED7418 0:51f1ef89ec7b 21 THE SOFTWARE.
ED7418 0:51f1ef89ec7b 22 */
ED7418 0:51f1ef89ec7b 23
ED7418 0:51f1ef89ec7b 24 #include "lwipNetTcpSocket.h"
ED7418 0:51f1ef89ec7b 25 #include "lwip/tcp.h"
ED7418 0:51f1ef89ec7b 26
ED7418 0:51f1ef89ec7b 27 //#define __DEBUG
ED7418 0:51f1ef89ec7b 28 #include "dbg/dbg.h"
ED7418 0:51f1ef89ec7b 29
ED7418 0:51f1ef89ec7b 30 #include "netCfg.h"
ED7418 0:51f1ef89ec7b 31 #if NET_LWIP_STACK
ED7418 0:51f1ef89ec7b 32
ED7418 0:51f1ef89ec7b 33 LwipNetTcpSocket::LwipNetTcpSocket(tcp_pcb* pPcb /*= NULL*/) : NetTcpSocket(), m_pPcb(pPcb), m_lpInNetTcpSocket(), //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
ED7418 0:51f1ef89ec7b 34 m_pReadPbuf(NULL)
ED7418 0:51f1ef89ec7b 35 {
ED7418 0:51f1ef89ec7b 36 DBG("New NetTcpSocket %p\n", (void*)this);
ED7418 0:51f1ef89ec7b 37 if(!m_pPcb)
ED7418 0:51f1ef89ec7b 38 {
ED7418 0:51f1ef89ec7b 39 m_pPcb = tcp_new();
ED7418 0:51f1ef89ec7b 40 DBG("Creating new PCB %p\n", m_pPcb);
ED7418 0:51f1ef89ec7b 41 }
ED7418 0:51f1ef89ec7b 42 if(m_pPcb)
ED7418 0:51f1ef89ec7b 43 {
ED7418 0:51f1ef89ec7b 44 //Setup callbacks
ED7418 0:51f1ef89ec7b 45 tcp_arg( (tcp_pcb*) m_pPcb, (void*) this ); //this will be passed to each static callback
ED7418 0:51f1ef89ec7b 46
ED7418 0:51f1ef89ec7b 47 tcp_recv( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sRecvCb );
ED7418 0:51f1ef89ec7b 48 tcp_sent((tcp_pcb*) m_pPcb, LwipNetTcpSocket::sSentCb );
ED7418 0:51f1ef89ec7b 49 tcp_err( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sErrCb );
ED7418 0:51f1ef89ec7b 50 //Connected callback is defined in connect()
ED7418 0:51f1ef89ec7b 51 //Accept callback is defined in listen()
ED7418 0:51f1ef89ec7b 52 DBG("NetTcpSocket created.\n");
ED7418 0:51f1ef89ec7b 53 }
ED7418 0:51f1ef89ec7b 54 }
ED7418 0:51f1ef89ec7b 55
ED7418 0:51f1ef89ec7b 56 LwipNetTcpSocket::~LwipNetTcpSocket()
ED7418 0:51f1ef89ec7b 57 {
ED7418 0:51f1ef89ec7b 58 /* if(m_pPcb)
ED7418 0:51f1ef89ec7b 59 tcp_close( (tcp_pcb*) m_pPcb); //Disconnect & free pcb*/
ED7418 0:51f1ef89ec7b 60 close();
ED7418 0:51f1ef89ec7b 61 }
ED7418 0:51f1ef89ec7b 62
ED7418 0:51f1ef89ec7b 63 NetTcpSocketErr LwipNetTcpSocket::bind(const Host& me)
ED7418 0:51f1ef89ec7b 64 {
ED7418 0:51f1ef89ec7b 65 if(!m_pPcb)
ED7418 0:51f1ef89ec7b 66 return NETTCPSOCKET_MEM; //NetTcpSocket was not properly initialised, should destroy it & retry
ED7418 0:51f1ef89ec7b 67
ED7418 0:51f1ef89ec7b 68 err_t err = tcp_bind( (tcp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
ED7418 0:51f1ef89ec7b 69 if(err)
ED7418 0:51f1ef89ec7b 70 return NETTCPSOCKET_INUSE;
ED7418 0:51f1ef89ec7b 71
ED7418 0:51f1ef89ec7b 72 return NETTCPSOCKET_OK;
ED7418 0:51f1ef89ec7b 73 }
ED7418 0:51f1ef89ec7b 74
ED7418 0:51f1ef89ec7b 75 NetTcpSocketErr LwipNetTcpSocket::listen()
ED7418 0:51f1ef89ec7b 76 {
ED7418 0:51f1ef89ec7b 77 if(!m_pPcb)
ED7418 0:51f1ef89ec7b 78 return NETTCPSOCKET_MEM; //NetTcpSocket was not properly initialised, should destroy it & retry
ED7418 0:51f1ef89ec7b 79 /*
ED7418 0:51f1ef89ec7b 80 From doc/rawapi.txt :
ED7418 0:51f1ef89ec7b 81
ED7418 0:51f1ef89ec7b 82 The tcp_listen() function returns a new connection identifier, and
ED7418 0:51f1ef89ec7b 83 the one passed as an argument to the function will be
ED7418 0:51f1ef89ec7b 84 deallocated. The reason for this behavior is that less memory is
ED7418 0:51f1ef89ec7b 85 needed for a connection that is listening, so tcp_listen() will
ED7418 0:51f1ef89ec7b 86 reclaim the memory needed for the original connection and allocate a
ED7418 0:51f1ef89ec7b 87 new smaller memory block for the listening connection.
ED7418 0:51f1ef89ec7b 88 */
ED7418 0:51f1ef89ec7b 89
ED7418 0:51f1ef89ec7b 90 // tcp_pcb* pNewPcb = tcp_listen(m_pPcb);
ED7418 0:51f1ef89ec7b 91 tcp_pcb* pNewPcb = tcp_listen_with_backlog((tcp_pcb*)m_pPcb, 5);
ED7418 0:51f1ef89ec7b 92 if( !pNewPcb ) //Not enough memory to create the listening pcb
ED7418 0:51f1ef89ec7b 93 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 94
ED7418 0:51f1ef89ec7b 95 m_pPcb = pNewPcb;
ED7418 0:51f1ef89ec7b 96
ED7418 0:51f1ef89ec7b 97 tcp_accept( (tcp_pcb*) m_pPcb, LwipNetTcpSocket::sAcceptCb );
ED7418 0:51f1ef89ec7b 98
ED7418 0:51f1ef89ec7b 99 return NETTCPSOCKET_OK;
ED7418 0:51f1ef89ec7b 100 }
ED7418 0:51f1ef89ec7b 101
ED7418 0:51f1ef89ec7b 102 NetTcpSocketErr LwipNetTcpSocket::connect(const Host& host)
ED7418 0:51f1ef89ec7b 103 {
ED7418 0:51f1ef89ec7b 104 if(!m_pPcb)
ED7418 0:51f1ef89ec7b 105 return NETTCPSOCKET_MEM; //NetTcpSocket was not properly initialised, should destroy it & retry
ED7418 0:51f1ef89ec7b 106
ED7418 0:51f1ef89ec7b 107 ip_addr_t ip = host.getIp().getStruct();
ED7418 0:51f1ef89ec7b 108 err_t err = tcp_connect( (tcp_pcb*) m_pPcb, &ip, host.getPort(), LwipNetTcpSocket::sConnectedCb );
ED7418 0:51f1ef89ec7b 109
ED7418 0:51f1ef89ec7b 110 if(err)
ED7418 0:51f1ef89ec7b 111 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 112
ED7418 0:51f1ef89ec7b 113 return NETTCPSOCKET_OK;
ED7418 0:51f1ef89ec7b 114 }
ED7418 0:51f1ef89ec7b 115
ED7418 0:51f1ef89ec7b 116 NetTcpSocketErr LwipNetTcpSocket::accept(Host* pClient, NetTcpSocket** ppNewNetTcpSocket)
ED7418 0:51f1ef89ec7b 117 {
ED7418 0:51f1ef89ec7b 118 if( !m_pPcb ) //Pcb doesn't exist (anymore)
ED7418 0:51f1ef89ec7b 119 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 120 //Dequeue a connection
ED7418 0:51f1ef89ec7b 121 //if( m_lpInPcb.empty() )
ED7418 0:51f1ef89ec7b 122 if( m_lpInNetTcpSocket.empty() )
ED7418 0:51f1ef89ec7b 123 return NETTCPSOCKET_EMPTY;
ED7418 0:51f1ef89ec7b 124
ED7418 0:51f1ef89ec7b 125 tcp_accepted( ((tcp_pcb*) m_pPcb) ); //Should fire proper events //WARN: m_pPcb is the GOOD param here (and not pInPcb)
ED7418 0:51f1ef89ec7b 126
ED7418 0:51f1ef89ec7b 127 /* tcp_pcb* pInPcb = m_lpInPcb.front();
ED7418 0:51f1ef89ec7b 128 m_lpInPcb.pop();*/
ED7418 0:51f1ef89ec7b 129
ED7418 0:51f1ef89ec7b 130 if( (m_lpInNetTcpSocket.front()) == NULL )
ED7418 0:51f1ef89ec7b 131 {
ED7418 0:51f1ef89ec7b 132 m_lpInNetTcpSocket.pop();
ED7418 0:51f1ef89ec7b 133 return NETTCPSOCKET_RST;
ED7418 0:51f1ef89ec7b 134 }
ED7418 0:51f1ef89ec7b 135
ED7418 0:51f1ef89ec7b 136 if( (m_lpInNetTcpSocket.front())->m_closed )
ED7418 0:51f1ef89ec7b 137 {
ED7418 0:51f1ef89ec7b 138 Net::releaseTcpSocket(m_lpInNetTcpSocket.front());
ED7418 0:51f1ef89ec7b 139 m_lpInNetTcpSocket.pop();
ED7418 0:51f1ef89ec7b 140 return NETTCPSOCKET_RST;
ED7418 0:51f1ef89ec7b 141 }
ED7418 0:51f1ef89ec7b 142
ED7418 0:51f1ef89ec7b 143 ip_addr_t* ip = (ip_addr_t*) &( (m_lpInNetTcpSocket.front()->m_pPcb)->remote_ip);
ED7418 0:51f1ef89ec7b 144
ED7418 0:51f1ef89ec7b 145 *ppNewNetTcpSocket = m_lpInNetTcpSocket.front();
ED7418 0:51f1ef89ec7b 146 *pClient = Host(
ED7418 0:51f1ef89ec7b 147 IpAddr(
ED7418 0:51f1ef89ec7b 148 ip
ED7418 0:51f1ef89ec7b 149 ),
ED7418 0:51f1ef89ec7b 150 m_lpInNetTcpSocket.front()->m_pPcb->remote_port
ED7418 0:51f1ef89ec7b 151 );
ED7418 0:51f1ef89ec7b 152 m_lpInNetTcpSocket.pop();
ED7418 0:51f1ef89ec7b 153 // *pClient = Host( IpAddr(pInPcb->remote_ip), pInPcb->remote_port );
ED7418 0:51f1ef89ec7b 154
ED7418 0:51f1ef89ec7b 155 //Return a new socket
ED7418 0:51f1ef89ec7b 156 // *ppNewNetTcpSocket = (NetTcpSocket*) new LwipNetTcpSocket(pInPcb);
ED7418 0:51f1ef89ec7b 157
ED7418 0:51f1ef89ec7b 158 //tcp_accepted( ((tcp_pcb*) m_pPcb) ); //Should fire proper events //WARN: m_pPcb is the GOOD param here (and not pInPcb)
ED7418 0:51f1ef89ec7b 159
ED7418 0:51f1ef89ec7b 160 /* if(*ppNewNetTcpSocket == NULL)
ED7418 0:51f1ef89ec7b 161 {
ED7418 0:51f1ef89ec7b 162 DBG("Not enough mem, socket dropped in LwipNetTcpSocket::accept.\n");
ED7418 0:51f1ef89ec7b 163 tcp_abort(pInPcb);
ED7418 0:51f1ef89ec7b 164 }*/
ED7418 0:51f1ef89ec7b 165
ED7418 0:51f1ef89ec7b 166 return NETTCPSOCKET_OK;
ED7418 0:51f1ef89ec7b 167 }
ED7418 0:51f1ef89ec7b 168
ED7418 0:51f1ef89ec7b 169 #define MAX(a,b) (((a)>(b))?(a):(b))
ED7418 0:51f1ef89ec7b 170 #define MIN(a,b) (((a)<(b))?(a):(b))
ED7418 0:51f1ef89ec7b 171
ED7418 0:51f1ef89ec7b 172 int /*if < 0 : NetTcpSocketErr*/ LwipNetTcpSocket::send(const char* buf, int len)
ED7418 0:51f1ef89ec7b 173 {
ED7418 0:51f1ef89ec7b 174 if( !m_pPcb ) //Pcb doesn't exist (anymore)
ED7418 0:51f1ef89ec7b 175 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 176 int outLen = MIN( len, tcp_sndbuf( (tcp_pcb*) m_pPcb) );
ED7418 0:51f1ef89ec7b 177 //tcp_sndbuf() returns the number of bytes available in the output queue, so never go above it
ED7418 0:51f1ef89ec7b 178 err_t err = tcp_write( (tcp_pcb*) m_pPcb, (void*) buf, outLen, TCP_WRITE_FLAG_COPY );
ED7418 0:51f1ef89ec7b 179 //Flags are TCP_WRITE_FLAG_COPY & TCP_WRITE_FLAG_MORE (see tcp_out.c) :
ED7418 0:51f1ef89ec7b 180 //If TCP_WRITE_FLAG_MORE is not set ask client to push buffered data to app
ED7418 0:51f1ef89ec7b 181 if(err)
ED7418 0:51f1ef89ec7b 182 {
ED7418 0:51f1ef89ec7b 183 switch( err )
ED7418 0:51f1ef89ec7b 184 {
ED7418 0:51f1ef89ec7b 185 case ERR_CONN:
ED7418 0:51f1ef89ec7b 186 return (int) NETTCPSOCKET_SETUP; //Not connected properly
ED7418 0:51f1ef89ec7b 187 case ERR_ARG:
ED7418 0:51f1ef89ec7b 188 return (int) NETTCPSOCKET_SETUP; //Wrong args ! (like buf pointing to NULL)
ED7418 0:51f1ef89ec7b 189 case ERR_MEM:
ED7418 0:51f1ef89ec7b 190 default:
ED7418 0:51f1ef89ec7b 191 return (int) NETTCPSOCKET_MEM; //Not enough memory
ED7418 0:51f1ef89ec7b 192 }
ED7418 0:51f1ef89ec7b 193 }
ED7418 0:51f1ef89ec7b 194 return outLen;
ED7418 0:51f1ef89ec7b 195 }
ED7418 0:51f1ef89ec7b 196
ED7418 0:51f1ef89ec7b 197 int /*if < 0 : NetTcpSocketErr*/ LwipNetTcpSocket::recv(char* buf, int len)
ED7418 0:51f1ef89ec7b 198 {
ED7418 0:51f1ef89ec7b 199 if( !m_pPcb ) //Pcb doesn't exist (anymore)
ED7418 0:51f1ef89ec7b 200 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 201 int inLen = 0;
ED7418 0:51f1ef89ec7b 202 int cpyLen = 0;
ED7418 0:51f1ef89ec7b 203
ED7418 0:51f1ef89ec7b 204 static int rmgLen = 0;
ED7418 0:51f1ef89ec7b 205 //Contains the remaining len in this pbuf
ED7418 0:51f1ef89ec7b 206
ED7418 0:51f1ef89ec7b 207 if( !m_pReadPbuf )
ED7418 0:51f1ef89ec7b 208 {
ED7418 0:51f1ef89ec7b 209 rmgLen = 0;
ED7418 0:51f1ef89ec7b 210 return 0;
ED7418 0:51f1ef89ec7b 211 }
ED7418 0:51f1ef89ec7b 212
ED7418 0:51f1ef89ec7b 213 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
ED7418 0:51f1ef89ec7b 214 {
ED7418 0:51f1ef89ec7b 215 rmgLen = m_pReadPbuf->len;
ED7418 0:51f1ef89ec7b 216 }
ED7418 0:51f1ef89ec7b 217
ED7418 0:51f1ef89ec7b 218 while ( inLen < len )
ED7418 0:51f1ef89ec7b 219 {
ED7418 0:51f1ef89ec7b 220 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
ED7418 0:51f1ef89ec7b 221 memcpy((void*)buf, (void*)((char*)(m_pReadPbuf->payload) + (m_pReadPbuf->len - rmgLen)), cpyLen);
ED7418 0:51f1ef89ec7b 222 inLen += cpyLen;
ED7418 0:51f1ef89ec7b 223 buf += cpyLen;
ED7418 0:51f1ef89ec7b 224
ED7418 0:51f1ef89ec7b 225 rmgLen = rmgLen - cpyLen; //Update rmgLen
ED7418 0:51f1ef89ec7b 226
ED7418 0:51f1ef89ec7b 227 if( rmgLen > 0 )
ED7418 0:51f1ef89ec7b 228 {
ED7418 0:51f1ef89ec7b 229 //We did not read this pbuf completely, so let's save it's pos & return
ED7418 0:51f1ef89ec7b 230 break;
ED7418 0:51f1ef89ec7b 231 }
ED7418 0:51f1ef89ec7b 232
ED7418 0:51f1ef89ec7b 233 if(m_pReadPbuf->next)
ED7418 0:51f1ef89ec7b 234 {
ED7418 0:51f1ef89ec7b 235 pbuf* pNextPBuf = m_pReadPbuf->next;
ED7418 0:51f1ef89ec7b 236 m_pReadPbuf->next = NULL; //So that it is not freed as well
ED7418 0:51f1ef89ec7b 237 //We get the reference to pNextPBuf from m_pReadPbuf
ED7418 0:51f1ef89ec7b 238 pbuf_free((pbuf*)m_pReadPbuf);
ED7418 0:51f1ef89ec7b 239 m_pReadPbuf = pNextPBuf;
ED7418 0:51f1ef89ec7b 240 rmgLen = m_pReadPbuf->len;
ED7418 0:51f1ef89ec7b 241 }
ED7418 0:51f1ef89ec7b 242 else
ED7418 0:51f1ef89ec7b 243 {
ED7418 0:51f1ef89ec7b 244 pbuf_free((pbuf*)m_pReadPbuf);
ED7418 0:51f1ef89ec7b 245 m_pReadPbuf = NULL;
ED7418 0:51f1ef89ec7b 246 rmgLen = 0;
ED7418 0:51f1ef89ec7b 247 break; //No more data to read
ED7418 0:51f1ef89ec7b 248 }
ED7418 0:51f1ef89ec7b 249
ED7418 0:51f1ef89ec7b 250 }
ED7418 0:51f1ef89ec7b 251
ED7418 0:51f1ef89ec7b 252 //tcp_recved(m_pPcb, inLen); //Acknowledge the reception
ED7418 0:51f1ef89ec7b 253
ED7418 0:51f1ef89ec7b 254 return inLen;
ED7418 0:51f1ef89ec7b 255 }
ED7418 0:51f1ef89ec7b 256
ED7418 0:51f1ef89ec7b 257 NetTcpSocketErr LwipNetTcpSocket::close()
ED7418 0:51f1ef89ec7b 258 {
ED7418 0:51f1ef89ec7b 259 //DBG("LwipNetTcpSocket::close() : Closing...\n");
ED7418 0:51f1ef89ec7b 260
ED7418 0:51f1ef89ec7b 261 if(m_closed)
ED7418 0:51f1ef89ec7b 262 return NETTCPSOCKET_OK; //Already being closed
ED7418 0:51f1ef89ec7b 263 m_closed = true;
ED7418 0:51f1ef89ec7b 264
ED7418 0:51f1ef89ec7b 265 if( !m_pPcb ) //Pcb doesn't exist (anymore)
ED7418 0:51f1ef89ec7b 266 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 267
ED7418 0:51f1ef89ec7b 268 //Cleanup incoming data
ED7418 0:51f1ef89ec7b 269 cleanUp();
ED7418 0:51f1ef89ec7b 270
ED7418 0:51f1ef89ec7b 271 if( !!tcp_close( (tcp_pcb*) m_pPcb) )
ED7418 0:51f1ef89ec7b 272 {
ED7418 0:51f1ef89ec7b 273 DBG("LwipNetTcpSocket::close() could not close properly, abort.\n");
ED7418 0:51f1ef89ec7b 274 tcp_abort( (tcp_pcb*) m_pPcb);
ED7418 0:51f1ef89ec7b 275 m_pPcb = NULL;
ED7418 0:51f1ef89ec7b 276 return NETTCPSOCKET_MEM;
ED7418 0:51f1ef89ec7b 277 }
ED7418 0:51f1ef89ec7b 278
ED7418 0:51f1ef89ec7b 279 DBG("LwipNetTcpSocket::close() : connection closed successfully.\n");
ED7418 0:51f1ef89ec7b 280
ED7418 0:51f1ef89ec7b 281 m_pPcb = NULL;
ED7418 0:51f1ef89ec7b 282 return NETTCPSOCKET_OK;
ED7418 0:51f1ef89ec7b 283 }
ED7418 0:51f1ef89ec7b 284
ED7418 0:51f1ef89ec7b 285 NetTcpSocketErr LwipNetTcpSocket::poll()
ED7418 0:51f1ef89ec7b 286 {
ED7418 0:51f1ef89ec7b 287 NetTcpSocket::flushEvents();
ED7418 0:51f1ef89ec7b 288 return NETTCPSOCKET_OK;
ED7418 0:51f1ef89ec7b 289 }
ED7418 0:51f1ef89ec7b 290
ED7418 0:51f1ef89ec7b 291 // Callbacks events
ED7418 0:51f1ef89ec7b 292
ED7418 0:51f1ef89ec7b 293 err_t LwipNetTcpSocket::acceptCb(struct tcp_pcb *newpcb, err_t err)
ED7418 0:51f1ef89ec7b 294 {
ED7418 0:51f1ef89ec7b 295 if(err)
ED7418 0:51f1ef89ec7b 296 {
ED7418 0:51f1ef89ec7b 297 DBG("Error %d in LwipNetTcpSocket::acceptCb.\n", err);
ED7418 0:51f1ef89ec7b 298 return err;
ED7418 0:51f1ef89ec7b 299 }
ED7418 0:51f1ef89ec7b 300 //FIXME: MEM Errs
ED7418 0:51f1ef89ec7b 301 //m_lpInPcb.push(newpcb); //Add connection to the queue
ED7418 0:51f1ef89ec7b 302 LwipNetTcpSocket* pNewNetTcpSocket = new LwipNetTcpSocket(newpcb);
ED7418 0:51f1ef89ec7b 303
ED7418 0:51f1ef89ec7b 304 if(pNewNetTcpSocket == NULL)
ED7418 0:51f1ef89ec7b 305 {
ED7418 0:51f1ef89ec7b 306 DBG("Not enough mem, socket dropped in LwipNetTcpSocket::acceptCb.\n");
ED7418 0:51f1ef89ec7b 307 tcp_abort(newpcb);
ED7418 0:51f1ef89ec7b 308 return ERR_ABRT;
ED7418 0:51f1ef89ec7b 309 }
ED7418 0:51f1ef89ec7b 310
ED7418 0:51f1ef89ec7b 311 pNewNetTcpSocket->m_refs++;
ED7418 0:51f1ef89ec7b 312 m_lpInNetTcpSocket.push( pNewNetTcpSocket );
ED7418 0:51f1ef89ec7b 313
ED7418 0:51f1ef89ec7b 314 // tcp_accepted(newpcb);
ED7418 0:51f1ef89ec7b 315 // tcp_accepted( m_pPcb ); //Should fire proper events //WARN: m_pPcb is the GOOD param here (and not pInPcb)
ED7418 0:51f1ef89ec7b 316 queueEvent(NETTCPSOCKET_ACCEPT);
ED7418 0:51f1ef89ec7b 317 return ERR_OK;
ED7418 0:51f1ef89ec7b 318 }
ED7418 0:51f1ef89ec7b 319
ED7418 0:51f1ef89ec7b 320 err_t LwipNetTcpSocket::connectedCb(struct tcp_pcb *tpcb, err_t err)
ED7418 0:51f1ef89ec7b 321 {
ED7418 0:51f1ef89ec7b 322 queueEvent(NETTCPSOCKET_CONNECTED);
ED7418 0:51f1ef89ec7b 323 return ERR_OK;
ED7418 0:51f1ef89ec7b 324 }
ED7418 0:51f1ef89ec7b 325
ED7418 0:51f1ef89ec7b 326 void LwipNetTcpSocket::errCb(err_t err)
ED7418 0:51f1ef89ec7b 327 {
ED7418 0:51f1ef89ec7b 328 DBG("NetTcpSocket %p - Error %d in LwipNetTcpSocket::errCb.\n", (void*)this, err);
ED7418 0:51f1ef89ec7b 329 //WARN: At this point, m_pPcb has been freed by lwIP
ED7418 0:51f1ef89ec7b 330 m_pPcb = NULL;
ED7418 0:51f1ef89ec7b 331 //These errors are fatal, discard all events queued before so that the errors are handled first
ED7418 0:51f1ef89ec7b 332 discardEvents();
ED7418 0:51f1ef89ec7b 333 m_closed = true;
ED7418 0:51f1ef89ec7b 334 cleanUp();
ED7418 0:51f1ef89ec7b 335 if( err == ERR_ABRT)
ED7418 0:51f1ef89ec7b 336 queueEvent(NETTCPSOCKET_CONABRT);
ED7418 0:51f1ef89ec7b 337 else //if( err == ERR_RST)
ED7418 0:51f1ef89ec7b 338 queueEvent(NETTCPSOCKET_CONRST);
ED7418 0:51f1ef89ec7b 339 }
ED7418 0:51f1ef89ec7b 340
ED7418 0:51f1ef89ec7b 341 err_t LwipNetTcpSocket::sentCb(tcp_pcb* tpcb, u16_t len)
ED7418 0:51f1ef89ec7b 342 {
ED7418 0:51f1ef89ec7b 343 // DBG("%d bytes ACKed by host.\n", len);
ED7418 0:51f1ef89ec7b 344 queueEvent(NETTCPSOCKET_WRITEABLE);
ED7418 0:51f1ef89ec7b 345 return ERR_OK;
ED7418 0:51f1ef89ec7b 346 }
ED7418 0:51f1ef89ec7b 347
ED7418 0:51f1ef89ec7b 348 err_t LwipNetTcpSocket::recvCb(tcp_pcb* tpcb, pbuf *p, err_t err)
ED7418 0:51f1ef89ec7b 349 {
ED7418 0:51f1ef89ec7b 350 //Store pbuf ptr
ED7418 0:51f1ef89ec7b 351 // DBG("Receive CB with err = %d & len = %d.\n", err, p->tot_len);
ED7418 0:51f1ef89ec7b 352 // tcp_recved( (tcp_pcb*) m_pPcb, p->tot_len); //Acknowledge the reception
ED7418 0:51f1ef89ec7b 353
ED7418 0:51f1ef89ec7b 354 if(err)
ED7418 0:51f1ef89ec7b 355 {
ED7418 0:51f1ef89ec7b 356 queueEvent(NETTCPSOCKET_ERROR);
ED7418 0:51f1ef89ec7b 357 return ERR_OK; //FIXME: More robust error handling there
ED7418 0:51f1ef89ec7b 358 }
ED7418 0:51f1ef89ec7b 359 else if(!p)
ED7418 0:51f1ef89ec7b 360 {
ED7418 0:51f1ef89ec7b 361 DBG("NetTcpSocket %p - Connection closed by remote host (LwipNetTcpSocket::recvCb).\n", (void*)this);
ED7418 0:51f1ef89ec7b 362 //Buf is NULL, that means that the connection has been closed by remote host
ED7418 0:51f1ef89ec7b 363
ED7418 0:51f1ef89ec7b 364 //FIX: 27/05/2010: We do not want to deallocate the socket while some data might still be readable
ED7418 0:51f1ef89ec7b 365 //REMOVED: close();
ED7418 0:51f1ef89ec7b 366
ED7418 0:51f1ef89ec7b 367 //However we do not want to close the socket yet
ED7418 0:51f1ef89ec7b 368
ED7418 0:51f1ef89ec7b 369 queueEvent(NETTCPSOCKET_DISCONNECTED);
ED7418 0:51f1ef89ec7b 370 return ERR_OK;
ED7418 0:51f1ef89ec7b 371 }
ED7418 0:51f1ef89ec7b 372
ED7418 0:51f1ef89ec7b 373 //We asserted that p is a valid pointer
ED7418 0:51f1ef89ec7b 374
ED7418 0:51f1ef89ec7b 375 //New data processing
ED7418 0:51f1ef89ec7b 376 tcp_recved( tpcb, p->tot_len); //Acknowledge the reception
ED7418 0:51f1ef89ec7b 377 if(!m_pReadPbuf)
ED7418 0:51f1ef89ec7b 378 {
ED7418 0:51f1ef89ec7b 379 m_pReadPbuf = p;
ED7418 0:51f1ef89ec7b 380 queueEvent(NETTCPSOCKET_READABLE);
ED7418 0:51f1ef89ec7b 381 }
ED7418 0:51f1ef89ec7b 382 else
ED7418 0:51f1ef89ec7b 383 {
ED7418 0:51f1ef89ec7b 384 pbuf_cat((pbuf*)m_pReadPbuf, p); //m_pReadPbuf is not empty, tail p to it and drop our ref
ED7418 0:51f1ef89ec7b 385 //No need to queue an event in that case since the read buf has not been processed yet
ED7418 0:51f1ef89ec7b 386 }
ED7418 0:51f1ef89ec7b 387 return ERR_OK;
ED7418 0:51f1ef89ec7b 388 }
ED7418 0:51f1ef89ec7b 389
ED7418 0:51f1ef89ec7b 390
ED7418 0:51f1ef89ec7b 391 void LwipNetTcpSocket::cleanUp() //Flush input buffer
ED7418 0:51f1ef89ec7b 392 {
ED7418 0:51f1ef89ec7b 393 //Ensure that further error won't be followed to this inst (which can be destroyed)
ED7418 0:51f1ef89ec7b 394 if( m_pPcb )
ED7418 0:51f1ef89ec7b 395 {
ED7418 0:51f1ef89ec7b 396 tcp_arg( (tcp_pcb*) m_pPcb, (void*) NULL );
ED7418 0:51f1ef89ec7b 397 tcp_recv( (tcp_pcb*) m_pPcb, NULL );
ED7418 0:51f1ef89ec7b 398 tcp_sent((tcp_pcb*) m_pPcb, NULL );
ED7418 0:51f1ef89ec7b 399 tcp_err( (tcp_pcb*) m_pPcb, NULL );
ED7418 0:51f1ef89ec7b 400 }
ED7418 0:51f1ef89ec7b 401
ED7418 0:51f1ef89ec7b 402 if( m_pReadPbuf )
ED7418 0:51f1ef89ec7b 403 {
ED7418 0:51f1ef89ec7b 404 DBG("Deallocating unread data.\n");
ED7418 0:51f1ef89ec7b 405 pbuf_free((pbuf*)m_pReadPbuf); //Free all unread data
ED7418 0:51f1ef89ec7b 406 m_pReadPbuf = NULL;
ED7418 0:51f1ef89ec7b 407 recv(NULL,0); //Update recv ptr position
ED7418 0:51f1ef89ec7b 408 }
ED7418 0:51f1ef89ec7b 409 }
ED7418 0:51f1ef89ec7b 410
ED7418 0:51f1ef89ec7b 411 // Static callbacks from LwIp
ED7418 0:51f1ef89ec7b 412
ED7418 0:51f1ef89ec7b 413 err_t LwipNetTcpSocket::sAcceptCb(void *arg, struct tcp_pcb *newpcb, err_t err)
ED7418 0:51f1ef89ec7b 414 {
ED7418 0:51f1ef89ec7b 415 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
ED7418 0:51f1ef89ec7b 416 return pMe->acceptCb( newpcb, err );
ED7418 0:51f1ef89ec7b 417 }
ED7418 0:51f1ef89ec7b 418
ED7418 0:51f1ef89ec7b 419 err_t LwipNetTcpSocket::sConnectedCb(void *arg, struct tcp_pcb *tpcb, err_t err)
ED7418 0:51f1ef89ec7b 420 {
ED7418 0:51f1ef89ec7b 421 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
ED7418 0:51f1ef89ec7b 422 return pMe->connectedCb( tpcb, err );
ED7418 0:51f1ef89ec7b 423 }
ED7418 0:51f1ef89ec7b 424
ED7418 0:51f1ef89ec7b 425 void LwipNetTcpSocket::sErrCb(void *arg, err_t err)
ED7418 0:51f1ef89ec7b 426 {
ED7418 0:51f1ef89ec7b 427 if( !arg )
ED7418 0:51f1ef89ec7b 428 {
ED7418 0:51f1ef89ec7b 429 DBG("NetTcpSocket - Error %d in LwipNetTcpSocket::sErrCb.\n", err);
ED7418 0:51f1ef89ec7b 430 return; //The socket has been destroyed, discard error
ED7418 0:51f1ef89ec7b 431 }
ED7418 0:51f1ef89ec7b 432 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
ED7418 0:51f1ef89ec7b 433 return pMe->errCb( err );
ED7418 0:51f1ef89ec7b 434 }
ED7418 0:51f1ef89ec7b 435
ED7418 0:51f1ef89ec7b 436 err_t LwipNetTcpSocket::sSentCb(void *arg, struct tcp_pcb *tpcb, u16_t len)
ED7418 0:51f1ef89ec7b 437 {
ED7418 0:51f1ef89ec7b 438 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
ED7418 0:51f1ef89ec7b 439 return pMe->sentCb( tpcb, len );
ED7418 0:51f1ef89ec7b 440 }
ED7418 0:51f1ef89ec7b 441
ED7418 0:51f1ef89ec7b 442 err_t LwipNetTcpSocket::sRecvCb(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
ED7418 0:51f1ef89ec7b 443 {
ED7418 0:51f1ef89ec7b 444 if( tpcb->flags & TF_RXCLOSED )
ED7418 0:51f1ef89ec7b 445 {
ED7418 0:51f1ef89ec7b 446 //The Pcb is in a closing state
ED7418 0:51f1ef89ec7b 447 //Discard that data here since we might have destroyed the corresponding socket object
ED7418 0:51f1ef89ec7b 448 tcp_recved( tpcb, p->tot_len);
ED7418 0:51f1ef89ec7b 449 pbuf_free( p );
ED7418 0:51f1ef89ec7b 450 return ERR_OK;
ED7418 0:51f1ef89ec7b 451 }
ED7418 0:51f1ef89ec7b 452 LwipNetTcpSocket* pMe = (LwipNetTcpSocket*) arg;
ED7418 0:51f1ef89ec7b 453 return pMe->recvCb( tpcb, p, err );
ED7418 0:51f1ef89ec7b 454 }
ED7418 0:51f1ef89ec7b 455
ED7418 0:51f1ef89ec7b 456 #endif