SNMP agent attached to SPI slave

Dependencies:   mbed

Committer:
lorcansmith
Date:
Mon Aug 13 15:07:40 2012 +0000
Revision:
0:2a53a4c3238c
v1.1 release includes ioAlarm traps

Who changed what in which revision?

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