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
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 "UDPSocket.h"
hendrikvincent 0:05ccbd4f84f1 25 #include "if/net/netudpsocket.h"
hendrikvincent 0:05ccbd4f84f1 26
hendrikvincent 0:05ccbd4f84f1 27 UDPSocket::UDPSocket() : m_pNetUdpSocket(NULL), m_pCbItem(NULL), m_pCbMeth(NULL), m_pCb(NULL)
hendrikvincent 0:05ccbd4f84f1 28 {
hendrikvincent 0:05ccbd4f84f1 29
hendrikvincent 0:05ccbd4f84f1 30 }
hendrikvincent 0:05ccbd4f84f1 31
hendrikvincent 0:05ccbd4f84f1 32 UDPSocket::~UDPSocket() //close()
hendrikvincent 0:05ccbd4f84f1 33 {
hendrikvincent 0:05ccbd4f84f1 34 close();
hendrikvincent 0:05ccbd4f84f1 35 }
hendrikvincent 0:05ccbd4f84f1 36
hendrikvincent 0:05ccbd4f84f1 37 UDPSocketErr UDPSocket::bind(const Host& me)
hendrikvincent 0:05ccbd4f84f1 38 {
hendrikvincent 0:05ccbd4f84f1 39 UDPSocketErr udpSocketErr = checkInst();
hendrikvincent 0:05ccbd4f84f1 40 if(udpSocketErr)
hendrikvincent 0:05ccbd4f84f1 41 return udpSocketErr;
hendrikvincent 0:05ccbd4f84f1 42 return (UDPSocketErr) m_pNetUdpSocket->bind(me);
hendrikvincent 0:05ccbd4f84f1 43 }
hendrikvincent 0:05ccbd4f84f1 44
hendrikvincent 0:05ccbd4f84f1 45 int /*if < 0 : UDPSocketErr*/ UDPSocket::sendto(const char* buf, int len, Host* pHost)
hendrikvincent 0:05ccbd4f84f1 46 {
hendrikvincent 0:05ccbd4f84f1 47 UDPSocketErr udpSocketErr = checkInst();
hendrikvincent 0:05ccbd4f84f1 48 if(udpSocketErr)
hendrikvincent 0:05ccbd4f84f1 49 return udpSocketErr;
hendrikvincent 0:05ccbd4f84f1 50 return m_pNetUdpSocket->sendto(buf, len, pHost);
hendrikvincent 0:05ccbd4f84f1 51 }
hendrikvincent 0:05ccbd4f84f1 52
hendrikvincent 0:05ccbd4f84f1 53 int /*if < 0 : UDPSocketErr*/ UDPSocket::recvfrom(char* buf, int len, Host* pHost)
hendrikvincent 0:05ccbd4f84f1 54 {
hendrikvincent 0:05ccbd4f84f1 55 UDPSocketErr udpSocketErr = checkInst();
hendrikvincent 0:05ccbd4f84f1 56 if(udpSocketErr)
hendrikvincent 0:05ccbd4f84f1 57 return udpSocketErr;
hendrikvincent 0:05ccbd4f84f1 58 return m_pNetUdpSocket->recvfrom(buf, len, pHost);
hendrikvincent 0:05ccbd4f84f1 59 }
hendrikvincent 0:05ccbd4f84f1 60
hendrikvincent 0:05ccbd4f84f1 61 UDPSocketErr UDPSocket::close()
hendrikvincent 0:05ccbd4f84f1 62 {
hendrikvincent 0:05ccbd4f84f1 63 if(!m_pNetUdpSocket)
hendrikvincent 0:05ccbd4f84f1 64 return UDPSOCKET_SETUP;
hendrikvincent 0:05ccbd4f84f1 65 m_pNetUdpSocket->resetOnEvent();
hendrikvincent 0:05ccbd4f84f1 66 UDPSocketErr udpSocketErr = (UDPSocketErr) m_pNetUdpSocket->close(); //Close (can already be closed)
hendrikvincent 0:05ccbd4f84f1 67 Net::releaseUdpSocket(m_pNetUdpSocket); //And release it so it can be freed when properly removed
hendrikvincent 0:05ccbd4f84f1 68 m_pNetUdpSocket = NULL;
hendrikvincent 0:05ccbd4f84f1 69 return udpSocketErr;
hendrikvincent 0:05ccbd4f84f1 70 }
hendrikvincent 0:05ccbd4f84f1 71
hendrikvincent 0:05ccbd4f84f1 72 //Callbacks
hendrikvincent 0:05ccbd4f84f1 73 void UDPSocket::setOnEvent( void (*pMethod)(UDPSocketEvent) )
hendrikvincent 0:05ccbd4f84f1 74 {
hendrikvincent 0:05ccbd4f84f1 75 m_pCb = pMethod;
hendrikvincent 0:05ccbd4f84f1 76 }
hendrikvincent 0:05ccbd4f84f1 77
hendrikvincent 0:05ccbd4f84f1 78 #if 0 //For info only
hendrikvincent 0:05ccbd4f84f1 79 template<class T>
hendrikvincent 0:05ccbd4f84f1 80 void UDPSocket::setOnEvent( T* pItem, void (T::*pMethod)(UDPSocketEvent) )
hendrikvincent 0:05ccbd4f84f1 81 {
hendrikvincent 0:05ccbd4f84f1 82 m_pCbItem = (CDummy*) pItem;
hendrikvincent 0:05ccbd4f84f1 83 m_pCbMeth = (void (CDummy::*)(UDPSocketEvent)) pMethod;
hendrikvincent 0:05ccbd4f84f1 84 }
hendrikvincent 0:05ccbd4f84f1 85 #endif
hendrikvincent 0:05ccbd4f84f1 86
hendrikvincent 0:05ccbd4f84f1 87 void UDPSocket::resetOnEvent() //Disable callback
hendrikvincent 0:05ccbd4f84f1 88 {
hendrikvincent 0:05ccbd4f84f1 89 m_pCb = NULL;
hendrikvincent 0:05ccbd4f84f1 90 m_pCbItem = NULL;
hendrikvincent 0:05ccbd4f84f1 91 m_pCbMeth = NULL;
hendrikvincent 0:05ccbd4f84f1 92 }
hendrikvincent 0:05ccbd4f84f1 93
hendrikvincent 0:05ccbd4f84f1 94 void UDPSocket::onNetUdpSocketEvent(NetUdpSocketEvent e)
hendrikvincent 0:05ccbd4f84f1 95 {
hendrikvincent 0:05ccbd4f84f1 96 if(m_pCbItem && m_pCbMeth)
hendrikvincent 0:05ccbd4f84f1 97 (m_pCbItem->*m_pCbMeth)((UDPSocketEvent) e);
hendrikvincent 0:05ccbd4f84f1 98 else if(m_pCb)
hendrikvincent 0:05ccbd4f84f1 99 m_pCb((UDPSocketEvent) e);
hendrikvincent 0:05ccbd4f84f1 100 }
hendrikvincent 0:05ccbd4f84f1 101
hendrikvincent 0:05ccbd4f84f1 102 UDPSocketErr UDPSocket::checkInst()
hendrikvincent 0:05ccbd4f84f1 103 {
hendrikvincent 0:05ccbd4f84f1 104 if(!m_pNetUdpSocket)
hendrikvincent 0:05ccbd4f84f1 105 {
hendrikvincent 0:05ccbd4f84f1 106 m_pNetUdpSocket = Net::udpSocket();
hendrikvincent 0:05ccbd4f84f1 107 if(!m_pNetUdpSocket)
hendrikvincent 0:05ccbd4f84f1 108 {
hendrikvincent 0:05ccbd4f84f1 109 return UDPSOCKET_IF; //Interface did not return a socket (usually because a default interface does not exist)
hendrikvincent 0:05ccbd4f84f1 110 }
hendrikvincent 0:05ccbd4f84f1 111 m_pNetUdpSocket->setOnEvent(this, &UDPSocket::onNetUdpSocketEvent);
hendrikvincent 0:05ccbd4f84f1 112 }
hendrikvincent 0:05ccbd4f84f1 113 return UDPSOCKET_OK;
hendrikvincent 0:05ccbd4f84f1 114 }