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