This short program illustrates how to use the DS130x_I2C library. My objective is to share the same RTC with Microchip 18F MCU.

Dependencies:   mbed DebugLibrary

Committer:
Yann
Date:
Fri Feb 11 10:17:20 2011 +0000
Revision:
1:995212d326ca
Parent:
0:f30e2135b0db
V0.0.0.2

Who changed what in which revision?

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