Jonathan Caes / InetTest-8d87bc453349

Dependencies:   DS1307 TextLCD mbed

Committer:
JonathanCaes
Date:
Mon Dec 29 13:37:45 2014 +0000
Revision:
2:63849bb30db0
Parent:
0:a16f60a71395
Project

Who changed what in which revision?

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