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