EthernetNetIf Compatibility.

Dependents:   XBeeWiFi_SPI_example

Fork of NetServicesSource by Donatien Garnier

Committer:
donatien
Date:
Tue Jul 27 15:59:42 2010 +0000
Revision:
5:dd63a1e02b1b
Parent:
2:a4f97773c90f

        

Who changed what in which revision?

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