NetTribute library with debug turned on in FShandler Donatien Garner -> Segundo Equipo -> this version

Committer:
hexley
Date:
Fri Nov 19 01:54:45 2010 +0000
Revision:
0:281d6ff68967

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hexley 0:281d6ff68967 1 #pragma diag_remark 1464
hexley 0:281d6ff68967 2 /*
hexley 0:281d6ff68967 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
hexley 0:281d6ff68967 4
hexley 0:281d6ff68967 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hexley 0:281d6ff68967 6 of this software and associated documentation files (the "Software"), to deal
hexley 0:281d6ff68967 7 in the Software without restriction, including without limitation the rights
hexley 0:281d6ff68967 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hexley 0:281d6ff68967 9 copies of the Software, and to permit persons to whom the Software is
hexley 0:281d6ff68967 10 furnished to do so, subject to the following conditions:
hexley 0:281d6ff68967 11
hexley 0:281d6ff68967 12 The above copyright notice and this permission notice shall be included in
hexley 0:281d6ff68967 13 all copies or substantial portions of the Software.
hexley 0:281d6ff68967 14
hexley 0:281d6ff68967 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hexley 0:281d6ff68967 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hexley 0:281d6ff68967 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hexley 0:281d6ff68967 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hexley 0:281d6ff68967 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hexley 0:281d6ff68967 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hexley 0:281d6ff68967 21 THE SOFTWARE.
hexley 0:281d6ff68967 22 */
hexley 0:281d6ff68967 23
hexley 0:281d6ff68967 24 #include "lwip/ip_addr.h"
hexley 0:281d6ff68967 25 #include "lwipNetUdpSocket.h"
hexley 0:281d6ff68967 26 #include "lwip/udp.h"
hexley 0:281d6ff68967 27 #include "lwip/igmp.h"
hexley 0:281d6ff68967 28
hexley 0:281d6ff68967 29
hexley 0:281d6ff68967 30 //#define __DEBUG
hexley 0:281d6ff68967 31 #include "dbg/dbg.h"
hexley 0:281d6ff68967 32
hexley 0:281d6ff68967 33 #include "netCfg.h"
hexley 0:281d6ff68967 34 #if NET_LWIP_STACK
hexley 0:281d6ff68967 35
hexley 0:281d6ff68967 36 LwipNetUdpSocket::LwipNetUdpSocket(udp_pcb* pPcb /*= NULL*/) : NetUdpSocket(), m_pPcb(pPcb), m_lInPkt(), m_multicastGroup() //Passes a pcb if already created (by an accept req for instance), in that case transfers ownership
hexley 0:281d6ff68967 37 {
hexley 0:281d6ff68967 38 DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb);
hexley 0:281d6ff68967 39 if(!m_pPcb)
hexley 0:281d6ff68967 40 m_pPcb = udp_new();
hexley 0:281d6ff68967 41 if(m_pPcb)
hexley 0:281d6ff68967 42 {
hexley 0:281d6ff68967 43 //Setup callback
hexley 0:281d6ff68967 44 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
hexley 0:281d6ff68967 45 }
hexley 0:281d6ff68967 46 }
hexley 0:281d6ff68967 47
hexley 0:281d6ff68967 48 LwipNetUdpSocket::~LwipNetUdpSocket()
hexley 0:281d6ff68967 49 {
hexley 0:281d6ff68967 50 close();
hexley 0:281d6ff68967 51 }
hexley 0:281d6ff68967 52
hexley 0:281d6ff68967 53 NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me)
hexley 0:281d6ff68967 54 {
hexley 0:281d6ff68967 55 err_t err;
hexley 0:281d6ff68967 56
hexley 0:281d6ff68967 57 if(!m_pPcb)
hexley 0:281d6ff68967 58 return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry
hexley 0:281d6ff68967 59
hexley 0:281d6ff68967 60 #if LWIP_IGMP //Multicast support enabled
hexley 0:281d6ff68967 61 if(me.getIp().isMulticast())
hexley 0:281d6ff68967 62 {
hexley 0:281d6ff68967 63 DBG("This is a multicast addr, joining multicast group\n");
hexley 0:281d6ff68967 64 m_multicastGroup = me.getIp();
hexley 0:281d6ff68967 65 err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
hexley 0:281d6ff68967 66 if(err)
hexley 0:281d6ff68967 67 return NETUDPSOCKET_IF; //Could not find or create group
hexley 0:281d6ff68967 68 }
hexley 0:281d6ff68967 69 #endif
hexley 0:281d6ff68967 70
hexley 0:281d6ff68967 71 err = udp_bind( (udp_pcb*) m_pPcb, IP_ADDR_ANY, me.getPort()); //IP_ADDR_ANY : Bind the connection to all local addresses
hexley 0:281d6ff68967 72 if(err)
hexley 0:281d6ff68967 73 return NETUDPSOCKET_INUSE;
hexley 0:281d6ff68967 74
hexley 0:281d6ff68967 75 //Setup callback
hexley 0:281d6ff68967 76 udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this );
hexley 0:281d6ff68967 77
hexley 0:281d6ff68967 78 return NETUDPSOCKET_OK;
hexley 0:281d6ff68967 79 }
hexley 0:281d6ff68967 80
hexley 0:281d6ff68967 81 #define MAX(a,b) ((a>b)?a:b)
hexley 0:281d6ff68967 82 #define MIN(a,b) ((a<b)?a:b)
hexley 0:281d6ff68967 83
hexley 0:281d6ff68967 84 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost)
hexley 0:281d6ff68967 85 {
hexley 0:281d6ff68967 86 if( !m_pPcb ) //Pcb doesn't exist (anymore)
hexley 0:281d6ff68967 87 return NETUDPSOCKET_MEM;
hexley 0:281d6ff68967 88 pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL);
hexley 0:281d6ff68967 89 if( !p )
hexley 0:281d6ff68967 90 return NETUDPSOCKET_MEM;
hexley 0:281d6ff68967 91 char* pBuf = (char*) buf;
hexley 0:281d6ff68967 92 pbuf* q = p;
hexley 0:281d6ff68967 93 do
hexley 0:281d6ff68967 94 {
hexley 0:281d6ff68967 95 memcpy (q->payload, (void*)pBuf, q->len);
hexley 0:281d6ff68967 96 pBuf += q->len;
hexley 0:281d6ff68967 97 q = q->next;
hexley 0:281d6ff68967 98 } while(q != NULL);
hexley 0:281d6ff68967 99
hexley 0:281d6ff68967 100 err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() );
hexley 0:281d6ff68967 101 pbuf_free( p );
hexley 0:281d6ff68967 102 if(err)
hexley 0:281d6ff68967 103 return NETUDPSOCKET_SETUP; //Connection problem
hexley 0:281d6ff68967 104 DBG("%d bytes sent in UDP Socket.\n", len);
hexley 0:281d6ff68967 105 return len;
hexley 0:281d6ff68967 106 }
hexley 0:281d6ff68967 107
hexley 0:281d6ff68967 108 int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost)
hexley 0:281d6ff68967 109 {
hexley 0:281d6ff68967 110 if( !m_pPcb ) //Pcb doesn't exist (anymore)
hexley 0:281d6ff68967 111 return NETUDPSOCKET_MEM;
hexley 0:281d6ff68967 112 int inLen = 0;
hexley 0:281d6ff68967 113 int cpyLen = 0;
hexley 0:281d6ff68967 114
hexley 0:281d6ff68967 115 static int rmgLen = 0;
hexley 0:281d6ff68967 116 //Contains the remaining len in this pbuf
hexley 0:281d6ff68967 117
hexley 0:281d6ff68967 118 if( m_lInPkt.empty() )
hexley 0:281d6ff68967 119 return 0;
hexley 0:281d6ff68967 120
hexley 0:281d6ff68967 121 pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf;
hexley 0:281d6ff68967 122
hexley 0:281d6ff68967 123 if(pHost)
hexley 0:281d6ff68967 124 *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port );
hexley 0:281d6ff68967 125
hexley 0:281d6ff68967 126 if( !pBuf )
hexley 0:281d6ff68967 127 {
hexley 0:281d6ff68967 128 rmgLen = 0;
hexley 0:281d6ff68967 129 return 0;
hexley 0:281d6ff68967 130 }
hexley 0:281d6ff68967 131
hexley 0:281d6ff68967 132 if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn
hexley 0:281d6ff68967 133 {
hexley 0:281d6ff68967 134 rmgLen = pBuf->len;
hexley 0:281d6ff68967 135 }
hexley 0:281d6ff68967 136
hexley 0:281d6ff68967 137 while ( inLen < len )
hexley 0:281d6ff68967 138 {
hexley 0:281d6ff68967 139 cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf
hexley 0:281d6ff68967 140 memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen);
hexley 0:281d6ff68967 141 inLen += cpyLen;
hexley 0:281d6ff68967 142 buf += cpyLen;
hexley 0:281d6ff68967 143
hexley 0:281d6ff68967 144 rmgLen = rmgLen - cpyLen; //Update rmgLen
hexley 0:281d6ff68967 145
hexley 0:281d6ff68967 146 if( rmgLen > 0 )
hexley 0:281d6ff68967 147 {
hexley 0:281d6ff68967 148 //We did not read this pbuf completely, so let's save it's pos & return
hexley 0:281d6ff68967 149 break;
hexley 0:281d6ff68967 150 }
hexley 0:281d6ff68967 151
hexley 0:281d6ff68967 152 if(pBuf->next)
hexley 0:281d6ff68967 153 {
hexley 0:281d6ff68967 154 pbuf* pNextPBuf = pBuf->next;
hexley 0:281d6ff68967 155 pBuf->next = NULL; //So that it is not freed as well
hexley 0:281d6ff68967 156 //We get the reference to pNextPBuf from m_pReadPbuf
hexley 0:281d6ff68967 157 pbuf_free((pbuf*)pBuf);
hexley 0:281d6ff68967 158 pBuf = pNextPBuf;
hexley 0:281d6ff68967 159 rmgLen = pBuf->len;
hexley 0:281d6ff68967 160 }
hexley 0:281d6ff68967 161 else
hexley 0:281d6ff68967 162 {
hexley 0:281d6ff68967 163 pbuf_free((pbuf*)pBuf);
hexley 0:281d6ff68967 164 pBuf = NULL;
hexley 0:281d6ff68967 165 rmgLen = 0;
hexley 0:281d6ff68967 166 m_lInPkt.pop_front();
hexley 0:281d6ff68967 167 break; //No more data to read
hexley 0:281d6ff68967 168 }
hexley 0:281d6ff68967 169 }
hexley 0:281d6ff68967 170
hexley 0:281d6ff68967 171 return inLen;
hexley 0:281d6ff68967 172 }
hexley 0:281d6ff68967 173
hexley 0:281d6ff68967 174 NetUdpSocketErr LwipNetUdpSocket::close()
hexley 0:281d6ff68967 175 {
hexley 0:281d6ff68967 176 DBG("LwipNetUdpSocket::close() : Closing...\n");
hexley 0:281d6ff68967 177
hexley 0:281d6ff68967 178 if(m_closed)
hexley 0:281d6ff68967 179 return NETUDPSOCKET_OK; //Already being closed
hexley 0:281d6ff68967 180 m_closed = true;
hexley 0:281d6ff68967 181
hexley 0:281d6ff68967 182 if( !m_pPcb ) //Pcb doesn't exist (anymore)
hexley 0:281d6ff68967 183 return NETUDPSOCKET_MEM;
hexley 0:281d6ff68967 184
hexley 0:281d6ff68967 185 DBG("LwipNetUdpSocket::close() : Cleanup...\n");
hexley 0:281d6ff68967 186
hexley 0:281d6ff68967 187 //Cleanup incoming data
hexley 0:281d6ff68967 188 cleanUp();
hexley 0:281d6ff68967 189
hexley 0:281d6ff68967 190
hexley 0:281d6ff68967 191 DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n");
hexley 0:281d6ff68967 192 udp_remove( (udp_pcb*) m_pPcb);
hexley 0:281d6ff68967 193
hexley 0:281d6ff68967 194 m_pPcb = NULL;
hexley 0:281d6ff68967 195 return NETUDPSOCKET_OK;
hexley 0:281d6ff68967 196 }
hexley 0:281d6ff68967 197
hexley 0:281d6ff68967 198 NetUdpSocketErr LwipNetUdpSocket::poll()
hexley 0:281d6ff68967 199 {
hexley 0:281d6ff68967 200 NetUdpSocket::flushEvents();
hexley 0:281d6ff68967 201 return NETUDPSOCKET_OK;
hexley 0:281d6ff68967 202 }
hexley 0:281d6ff68967 203
hexley 0:281d6ff68967 204 // Callbacks events
hexley 0:281d6ff68967 205
hexley 0:281d6ff68967 206 void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port)
hexley 0:281d6ff68967 207 {
hexley 0:281d6ff68967 208 DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len);
hexley 0:281d6ff68967 209 list<InPacket>::iterator it;
hexley 0:281d6ff68967 210 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
hexley 0:281d6ff68967 211 {
hexley 0:281d6ff68967 212 if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) )
hexley 0:281d6ff68967 213 {
hexley 0:281d6ff68967 214 //Let's tail this packet to the previous one
hexley 0:281d6ff68967 215 pbuf_cat((pbuf*)((*it).pBuf), p);
hexley 0:281d6ff68967 216 //No need to queue an event in that case since the read buf has not been processed yet
hexley 0:281d6ff68967 217 return;
hexley 0:281d6ff68967 218 }
hexley 0:281d6ff68967 219 }
hexley 0:281d6ff68967 220
hexley 0:281d6ff68967 221 //New host, add a packet to the queue
hexley 0:281d6ff68967 222 InPacket pkt;
hexley 0:281d6ff68967 223 pkt.pBuf = p;
hexley 0:281d6ff68967 224 pkt.addr = *addr;
hexley 0:281d6ff68967 225 pkt.port = port;
hexley 0:281d6ff68967 226 m_lInPkt.push_back(pkt);
hexley 0:281d6ff68967 227
hexley 0:281d6ff68967 228 queueEvent(NETUDPSOCKET_READABLE);
hexley 0:281d6ff68967 229 }
hexley 0:281d6ff68967 230
hexley 0:281d6ff68967 231 void LwipNetUdpSocket::cleanUp() //Flush input buffer
hexley 0:281d6ff68967 232 {
hexley 0:281d6ff68967 233 //Ensure that further error won't be followed to this inst (which can be destroyed)
hexley 0:281d6ff68967 234 if( m_pPcb )
hexley 0:281d6ff68967 235 {
hexley 0:281d6ff68967 236 udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL );
hexley 0:281d6ff68967 237 }
hexley 0:281d6ff68967 238
hexley 0:281d6ff68967 239 //Leaving multicast group(Ok because LwIP has a refscount for multicast group)
hexley 0:281d6ff68967 240 #if LWIP_IGMP //Multicast support enabled
hexley 0:281d6ff68967 241 if(m_multicastGroup.isMulticast())
hexley 0:281d6ff68967 242 {
hexley 0:281d6ff68967 243 igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct()));
hexley 0:281d6ff68967 244 m_multicastGroup = IpAddr();
hexley 0:281d6ff68967 245 }
hexley 0:281d6ff68967 246 #endif
hexley 0:281d6ff68967 247
hexley 0:281d6ff68967 248 list<InPacket>::iterator it;
hexley 0:281d6ff68967 249 for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ )
hexley 0:281d6ff68967 250 {
hexley 0:281d6ff68967 251 //Free buf
hexley 0:281d6ff68967 252 pbuf_free((pbuf*)((*it).pBuf));
hexley 0:281d6ff68967 253 }
hexley 0:281d6ff68967 254 m_lInPkt.clear();
hexley 0:281d6ff68967 255 }
hexley 0:281d6ff68967 256
hexley 0:281d6ff68967 257 // Static callback from LwIp
hexley 0:281d6ff68967 258
hexley 0:281d6ff68967 259 void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port)
hexley 0:281d6ff68967 260 {
hexley 0:281d6ff68967 261 LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg;
hexley 0:281d6ff68967 262 return pMe->recvCb( pcb, p, addr, port );
hexley 0:281d6ff68967 263 }
hexley 0:281d6ff68967 264
hexley 0:281d6ff68967 265 #endif