Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

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