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