EthernetNetIf Compatibility.
Dependents: XBeeWiFi_SPI_example
Fork of NetServicesSource by
if/lwip/lwipNetUdpSocket.cpp@5:dd63a1e02b1b, 2010-07-27 (annotated)
- Committer:
- donatien
- Date:
- Tue Jul 27 15:59:42 2010 +0000
- Revision:
- 5:dd63a1e02b1b
- Parent:
- 2:a4f97773c90f
- Child:
- 9:c79fa4034f5b
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 | 0:632c9925f013 | 24 | #include "lwipNetUdpSocket.h" |
donatien | 0:632c9925f013 | 25 | #include "lwip/udp.h" |
donatien | 5:dd63a1e02b1b | 26 | #include "lwip/igmp.h" |
donatien | 0:632c9925f013 | 27 | |
donatien | 0:632c9925f013 | 28 | //#define __DEBUG |
donatien | 0:632c9925f013 | 29 | #include "dbg/dbg.h" |
donatien | 0:632c9925f013 | 30 | |
donatien | 0:632c9925f013 | 31 | #include "netCfg.h" |
donatien | 0:632c9925f013 | 32 | #if NET_LWIP_STACK |
donatien | 0:632c9925f013 | 33 | |
donatien | 5:dd63a1e02b1b | 34 | 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 | 35 | { |
donatien | 5:dd63a1e02b1b | 36 | DBG("New LwipNetUdpSocket %p (pPCb=%p)\n", (void*)this, (void*) pPcb); |
donatien | 0:632c9925f013 | 37 | if(!m_pPcb) |
donatien | 0:632c9925f013 | 38 | m_pPcb = udp_new(); |
donatien | 2:a4f97773c90f | 39 | if(m_pPcb) |
donatien | 0:632c9925f013 | 40 | { |
donatien | 0:632c9925f013 | 41 | //Setup callback |
donatien | 2:a4f97773c90f | 42 | udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this ); |
donatien | 2:a4f97773c90f | 43 | } |
donatien | 0:632c9925f013 | 44 | } |
donatien | 0:632c9925f013 | 45 | |
donatien | 0:632c9925f013 | 46 | LwipNetUdpSocket::~LwipNetUdpSocket() |
donatien | 0:632c9925f013 | 47 | { |
donatien | 0:632c9925f013 | 48 | close(); |
donatien | 0:632c9925f013 | 49 | } |
donatien | 0:632c9925f013 | 50 | |
donatien | 0:632c9925f013 | 51 | NetUdpSocketErr LwipNetUdpSocket::bind(const Host& me) |
donatien | 0:632c9925f013 | 52 | { |
donatien | 5:dd63a1e02b1b | 53 | err_t err; |
donatien | 5:dd63a1e02b1b | 54 | |
donatien | 0:632c9925f013 | 55 | if(!m_pPcb) |
donatien | 0:632c9925f013 | 56 | return NETUDPSOCKET_MEM; //NetUdpSocket was not properly initialised, should destroy it & retry |
donatien | 5:dd63a1e02b1b | 57 | |
donatien | 5:dd63a1e02b1b | 58 | #if LWIP_IGMP //Multicast support enabled |
donatien | 5:dd63a1e02b1b | 59 | if(me.getIp().isMulticast()) |
donatien | 5:dd63a1e02b1b | 60 | { |
donatien | 5:dd63a1e02b1b | 61 | DBG("This is a multicast addr, joining multicast group\n"); |
donatien | 5:dd63a1e02b1b | 62 | m_multicastGroup = me.getIp(); |
donatien | 5:dd63a1e02b1b | 63 | err = igmp_joingroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct())); |
donatien | 5:dd63a1e02b1b | 64 | if(err) |
donatien | 5:dd63a1e02b1b | 65 | return NETUDPSOCKET_IF; //Could not find or create group |
donatien | 5:dd63a1e02b1b | 66 | } |
donatien | 5:dd63a1e02b1b | 67 | #endif |
donatien | 2:a4f97773c90f | 68 | |
donatien | 5:dd63a1e02b1b | 69 | 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 | 70 | if(err) |
donatien | 1:abb442332fa8 | 71 | return NETUDPSOCKET_INUSE; |
donatien | 2:a4f97773c90f | 72 | |
donatien | 0:632c9925f013 | 73 | //Setup callback |
donatien | 0:632c9925f013 | 74 | udp_recv( (udp_pcb*) m_pPcb, LwipNetUdpSocket::sRecvCb, (void*) this ); |
donatien | 0:632c9925f013 | 75 | |
donatien | 0:632c9925f013 | 76 | return NETUDPSOCKET_OK; |
donatien | 0:632c9925f013 | 77 | } |
donatien | 0:632c9925f013 | 78 | |
donatien | 0:632c9925f013 | 79 | #define MAX(a,b) ((a>b)?a:b) |
donatien | 0:632c9925f013 | 80 | #define MIN(a,b) ((a<b)?a:b) |
donatien | 0:632c9925f013 | 81 | |
donatien | 0:632c9925f013 | 82 | int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::sendto(const char* buf, int len, Host* pHost) |
donatien | 0:632c9925f013 | 83 | { |
donatien | 0:632c9925f013 | 84 | if( !m_pPcb ) //Pcb doesn't exist (anymore) |
donatien | 0:632c9925f013 | 85 | return NETUDPSOCKET_MEM; |
donatien | 2:a4f97773c90f | 86 | pbuf* p = pbuf_alloc(PBUF_TRANSPORT, len, PBUF_POOL); |
donatien | 0:632c9925f013 | 87 | if( !p ) |
donatien | 0:632c9925f013 | 88 | return NETUDPSOCKET_MEM; |
donatien | 2:a4f97773c90f | 89 | char* pBuf = (char*) buf; |
donatien | 2:a4f97773c90f | 90 | pbuf* q = p; |
donatien | 2:a4f97773c90f | 91 | do |
donatien | 2:a4f97773c90f | 92 | { |
donatien | 2:a4f97773c90f | 93 | memcpy (q->payload, (void*)pBuf, q->len); |
donatien | 2:a4f97773c90f | 94 | pBuf += q->len; |
donatien | 2:a4f97773c90f | 95 | q = q->next; |
donatien | 2:a4f97773c90f | 96 | } while(q != NULL); |
donatien | 2:a4f97773c90f | 97 | |
donatien | 0:632c9925f013 | 98 | err_t err = udp_sendto( (udp_pcb*) m_pPcb, p, &(pHost->getIp().getStruct()), pHost->getPort() ); |
donatien | 0:632c9925f013 | 99 | pbuf_free( p ); |
donatien | 0:632c9925f013 | 100 | if(err) |
donatien | 0:632c9925f013 | 101 | return NETUDPSOCKET_SETUP; //Connection problem |
donatien | 5:dd63a1e02b1b | 102 | DBG("%d bytes sent in UDP Socket.\n", len); |
donatien | 0:632c9925f013 | 103 | return len; |
donatien | 0:632c9925f013 | 104 | } |
donatien | 0:632c9925f013 | 105 | |
donatien | 0:632c9925f013 | 106 | int /*if < 0 : NetUdpSocketErr*/ LwipNetUdpSocket::recvfrom(char* buf, int len, Host* pHost) |
donatien | 0:632c9925f013 | 107 | { |
donatien | 0:632c9925f013 | 108 | if( !m_pPcb ) //Pcb doesn't exist (anymore) |
donatien | 0:632c9925f013 | 109 | return NETUDPSOCKET_MEM; |
donatien | 0:632c9925f013 | 110 | int inLen = 0; |
donatien | 0:632c9925f013 | 111 | int cpyLen = 0; |
donatien | 0:632c9925f013 | 112 | |
donatien | 0:632c9925f013 | 113 | static int rmgLen = 0; |
donatien | 0:632c9925f013 | 114 | //Contains the remaining len in this pbuf |
donatien | 0:632c9925f013 | 115 | |
donatien | 0:632c9925f013 | 116 | if( m_lInPkt.empty() ) |
donatien | 0:632c9925f013 | 117 | return 0; |
donatien | 0:632c9925f013 | 118 | |
donatien | 0:632c9925f013 | 119 | pbuf* pBuf = (pbuf*) m_lInPkt.front().pBuf; |
donatien | 0:632c9925f013 | 120 | |
donatien | 0:632c9925f013 | 121 | if(pHost) |
donatien | 0:632c9925f013 | 122 | *pHost = Host( IpAddr(&m_lInPkt.front().addr), m_lInPkt.front().port ); |
donatien | 0:632c9925f013 | 123 | |
donatien | 0:632c9925f013 | 124 | if( !pBuf ) |
donatien | 0:632c9925f013 | 125 | { |
donatien | 0:632c9925f013 | 126 | rmgLen = 0; |
donatien | 0:632c9925f013 | 127 | return 0; |
donatien | 0:632c9925f013 | 128 | } |
donatien | 0:632c9925f013 | 129 | |
donatien | 0:632c9925f013 | 130 | if ( !rmgLen ) //We did not know m_pReadPbuf->len last time we called this fn |
donatien | 0:632c9925f013 | 131 | { |
donatien | 0:632c9925f013 | 132 | rmgLen = pBuf->len; |
donatien | 0:632c9925f013 | 133 | } |
donatien | 0:632c9925f013 | 134 | |
donatien | 0:632c9925f013 | 135 | while ( inLen < len ) |
donatien | 0:632c9925f013 | 136 | { |
donatien | 0:632c9925f013 | 137 | cpyLen = MIN( (len - inLen), rmgLen ); //Remaining len to copy, remaining len in THIS pbuf |
donatien | 0:632c9925f013 | 138 | memcpy((void*)buf, (void*)((char*)(pBuf->payload) + (pBuf->len - rmgLen)), cpyLen); |
donatien | 0:632c9925f013 | 139 | inLen += cpyLen; |
donatien | 0:632c9925f013 | 140 | buf += cpyLen; |
donatien | 0:632c9925f013 | 141 | |
donatien | 0:632c9925f013 | 142 | rmgLen = rmgLen - cpyLen; //Update rmgLen |
donatien | 0:632c9925f013 | 143 | |
donatien | 0:632c9925f013 | 144 | if( rmgLen > 0 ) |
donatien | 0:632c9925f013 | 145 | { |
donatien | 0:632c9925f013 | 146 | //We did not read this pbuf completely, so let's save it's pos & return |
donatien | 0:632c9925f013 | 147 | break; |
donatien | 0:632c9925f013 | 148 | } |
donatien | 0:632c9925f013 | 149 | |
donatien | 0:632c9925f013 | 150 | if(pBuf->next) |
donatien | 0:632c9925f013 | 151 | { |
donatien | 0:632c9925f013 | 152 | pbuf* pNextPBuf = pBuf->next; |
donatien | 0:632c9925f013 | 153 | pBuf->next = NULL; //So that it is not freed as well |
donatien | 0:632c9925f013 | 154 | //We get the reference to pNextPBuf from m_pReadPbuf |
donatien | 0:632c9925f013 | 155 | pbuf_free((pbuf*)pBuf); |
donatien | 0:632c9925f013 | 156 | pBuf = pNextPBuf; |
donatien | 0:632c9925f013 | 157 | rmgLen = pBuf->len; |
donatien | 0:632c9925f013 | 158 | } |
donatien | 0:632c9925f013 | 159 | else |
donatien | 0:632c9925f013 | 160 | { |
donatien | 0:632c9925f013 | 161 | pbuf_free((pbuf*)pBuf); |
donatien | 0:632c9925f013 | 162 | pBuf = NULL; |
donatien | 0:632c9925f013 | 163 | rmgLen = 0; |
donatien | 0:632c9925f013 | 164 | m_lInPkt.pop_front(); |
donatien | 0:632c9925f013 | 165 | break; //No more data to read |
donatien | 0:632c9925f013 | 166 | } |
donatien | 0:632c9925f013 | 167 | } |
donatien | 0:632c9925f013 | 168 | |
donatien | 0:632c9925f013 | 169 | return inLen; |
donatien | 0:632c9925f013 | 170 | } |
donatien | 0:632c9925f013 | 171 | |
donatien | 0:632c9925f013 | 172 | NetUdpSocketErr LwipNetUdpSocket::close() |
donatien | 0:632c9925f013 | 173 | { |
donatien | 5:dd63a1e02b1b | 174 | DBG("LwipNetUdpSocket::close() : Closing...\n"); |
donatien | 0:632c9925f013 | 175 | |
donatien | 0:632c9925f013 | 176 | if(m_closed) |
donatien | 0:632c9925f013 | 177 | return NETUDPSOCKET_OK; //Already being closed |
donatien | 0:632c9925f013 | 178 | m_closed = true; |
donatien | 0:632c9925f013 | 179 | |
donatien | 0:632c9925f013 | 180 | if( !m_pPcb ) //Pcb doesn't exist (anymore) |
donatien | 0:632c9925f013 | 181 | return NETUDPSOCKET_MEM; |
donatien | 0:632c9925f013 | 182 | |
donatien | 5:dd63a1e02b1b | 183 | DBG("LwipNetUdpSocket::close() : Cleanup...\n"); |
donatien | 2:a4f97773c90f | 184 | |
donatien | 0:632c9925f013 | 185 | //Cleanup incoming data |
donatien | 0:632c9925f013 | 186 | cleanUp(); |
donatien | 5:dd63a1e02b1b | 187 | |
donatien | 5:dd63a1e02b1b | 188 | |
donatien | 5:dd63a1e02b1b | 189 | DBG("LwipNetUdpSocket::close() : removing m_pPcb...\n"); |
donatien | 0:632c9925f013 | 190 | udp_remove( (udp_pcb*) m_pPcb); |
donatien | 2:a4f97773c90f | 191 | |
donatien | 0:632c9925f013 | 192 | m_pPcb = NULL; |
donatien | 0:632c9925f013 | 193 | return NETUDPSOCKET_OK; |
donatien | 0:632c9925f013 | 194 | } |
donatien | 0:632c9925f013 | 195 | |
donatien | 0:632c9925f013 | 196 | NetUdpSocketErr LwipNetUdpSocket::poll() |
donatien | 0:632c9925f013 | 197 | { |
donatien | 0:632c9925f013 | 198 | NetUdpSocket::flushEvents(); |
donatien | 0:632c9925f013 | 199 | return NETUDPSOCKET_OK; |
donatien | 0:632c9925f013 | 200 | } |
donatien | 0:632c9925f013 | 201 | |
donatien | 0:632c9925f013 | 202 | // Callbacks events |
donatien | 0:632c9925f013 | 203 | |
donatien | 0:632c9925f013 | 204 | void LwipNetUdpSocket::recvCb(udp_pcb* pcb, struct pbuf* p, ip_addr_t* addr, u16_t port) |
donatien | 0:632c9925f013 | 205 | { |
donatien | 5:dd63a1e02b1b | 206 | DBG(" Packet of length %d arrived in UDP Socket.\n", p->tot_len); |
donatien | 0:632c9925f013 | 207 | list<InPacket>::iterator it; |
donatien | 0:632c9925f013 | 208 | for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ ) |
donatien | 0:632c9925f013 | 209 | { |
donatien | 0:632c9925f013 | 210 | if( ip_addr_cmp((&((*it).addr)), addr) && ((*it).port == port) ) |
donatien | 0:632c9925f013 | 211 | { |
donatien | 0:632c9925f013 | 212 | //Let's tail this packet to the previous one |
donatien | 2:a4f97773c90f | 213 | pbuf_cat((pbuf*)((*it).pBuf), p); |
donatien | 0:632c9925f013 | 214 | //No need to queue an event in that case since the read buf has not been processed yet |
donatien | 0:632c9925f013 | 215 | return; |
donatien | 0:632c9925f013 | 216 | } |
donatien | 0:632c9925f013 | 217 | } |
donatien | 0:632c9925f013 | 218 | |
donatien | 0:632c9925f013 | 219 | //New host, add a packet to the queue |
donatien | 0:632c9925f013 | 220 | InPacket pkt; |
donatien | 0:632c9925f013 | 221 | pkt.pBuf = p; |
donatien | 0:632c9925f013 | 222 | pkt.addr = *addr; |
donatien | 0:632c9925f013 | 223 | pkt.port = port; |
donatien | 0:632c9925f013 | 224 | m_lInPkt.push_back(pkt); |
donatien | 0:632c9925f013 | 225 | |
donatien | 0:632c9925f013 | 226 | queueEvent(NETUDPSOCKET_READABLE); |
donatien | 0:632c9925f013 | 227 | } |
donatien | 0:632c9925f013 | 228 | |
donatien | 0:632c9925f013 | 229 | void LwipNetUdpSocket::cleanUp() //Flush input buffer |
donatien | 0:632c9925f013 | 230 | { |
donatien | 2:a4f97773c90f | 231 | //Ensure that further error won't be followed to this inst (which can be destroyed) |
donatien | 2:a4f97773c90f | 232 | if( m_pPcb ) |
donatien | 2:a4f97773c90f | 233 | { |
donatien | 2:a4f97773c90f | 234 | udp_recv( (udp_pcb*) m_pPcb, NULL, (void*) NULL ); |
donatien | 2:a4f97773c90f | 235 | } |
donatien | 2:a4f97773c90f | 236 | |
donatien | 5:dd63a1e02b1b | 237 | //Leaving multicast group(Ok because LwIP has a refscount for multicast group) |
donatien | 5:dd63a1e02b1b | 238 | #if LWIP_IGMP //Multicast support enabled |
donatien | 5:dd63a1e02b1b | 239 | if(m_multicastGroup.isMulticast()) |
donatien | 5:dd63a1e02b1b | 240 | { |
donatien | 5:dd63a1e02b1b | 241 | igmp_leavegroup(IP_ADDR_ANY, &(m_multicastGroup.getStruct())); |
donatien | 5:dd63a1e02b1b | 242 | m_multicastGroup = IpAddr(); |
donatien | 5:dd63a1e02b1b | 243 | } |
donatien | 5:dd63a1e02b1b | 244 | #endif |
donatien | 5:dd63a1e02b1b | 245 | |
donatien | 0:632c9925f013 | 246 | list<InPacket>::iterator it; |
donatien | 0:632c9925f013 | 247 | for ( it = m_lInPkt.begin(); it != m_lInPkt.end(); it++ ) |
donatien | 0:632c9925f013 | 248 | { |
donatien | 0:632c9925f013 | 249 | //Free buf |
donatien | 2:a4f97773c90f | 250 | pbuf_free((pbuf*)((*it).pBuf)); |
donatien | 0:632c9925f013 | 251 | } |
donatien | 0:632c9925f013 | 252 | m_lInPkt.clear(); |
donatien | 0:632c9925f013 | 253 | } |
donatien | 0:632c9925f013 | 254 | |
donatien | 0:632c9925f013 | 255 | // Static callback from LwIp |
donatien | 0:632c9925f013 | 256 | |
donatien | 0:632c9925f013 | 257 | void LwipNetUdpSocket::sRecvCb(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t port) |
donatien | 0:632c9925f013 | 258 | { |
donatien | 0:632c9925f013 | 259 | LwipNetUdpSocket* pMe = (LwipNetUdpSocket*) arg; |
donatien | 0:632c9925f013 | 260 | return pMe->recvCb( pcb, p, addr, port ); |
donatien | 0:632c9925f013 | 261 | } |
donatien | 0:632c9925f013 | 262 | |
donatien | 0:632c9925f013 | 263 | #endif |