Leest de waarde van een sensor binnen een maakt deze beschikbaar via internet

Dependencies:   NTPClient_NetServices mbed

Committer:
hendrikvincent
Date:
Mon Dec 02 09:01:23 2013 +0000
Revision:
0:05ccbd4f84f1
eerste programma;

Who changed what in which revision?

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