First step: AutoIP compiled in and working

Dependencies:   mbed

Committer:
darran
Date:
Fri Jun 18 15:54:21 2010 +0000
Revision:
1:4218cacaf696
Parent:
0:55a05330f8cc

        

Who changed what in which revision?

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