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