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 /** \file
hendrikvincent 0:05ccbd4f84f1 25 NTP Client header file
hendrikvincent 0:05ccbd4f84f1 26 */
hendrikvincent 0:05ccbd4f84f1 27
hendrikvincent 0:05ccbd4f84f1 28 #ifndef NTP_CLIENT_H
hendrikvincent 0:05ccbd4f84f1 29 #define NTP_CLIENT_H
hendrikvincent 0:05ccbd4f84f1 30
hendrikvincent 0:05ccbd4f84f1 31 #include "core/net.h"
hendrikvincent 0:05ccbd4f84f1 32 #include "core/netservice.h"
hendrikvincent 0:05ccbd4f84f1 33 #include "api/UDPSocket.h"
hendrikvincent 0:05ccbd4f84f1 34 #include "api/DNSRequest.h"
hendrikvincent 0:05ccbd4f84f1 35 #include "mbed.h"
hendrikvincent 0:05ccbd4f84f1 36
hendrikvincent 0:05ccbd4f84f1 37 ///NTP Client results
hendrikvincent 0:05ccbd4f84f1 38 enum NTPResult
hendrikvincent 0:05ccbd4f84f1 39 {
hendrikvincent 0:05ccbd4f84f1 40 NTP_OK, ///<Success
hendrikvincent 0:05ccbd4f84f1 41 NTP_PROCESSING, ///<Processing
hendrikvincent 0:05ccbd4f84f1 42 NTP_PRTCL, ///<Protocol error
hendrikvincent 0:05ccbd4f84f1 43 NTP_TIMEOUT, ///<Connection timeout
hendrikvincent 0:05ccbd4f84f1 44 NTP_DNS ///<Could not resolve DNS hostname
hendrikvincent 0:05ccbd4f84f1 45 };
hendrikvincent 0:05ccbd4f84f1 46
hendrikvincent 0:05ccbd4f84f1 47 ///A NTP Client
hendrikvincent 0:05ccbd4f84f1 48 /**
hendrikvincent 0:05ccbd4f84f1 49 The NTP client is a simple UDP client that will update the mbed's RTC
hendrikvincent 0:05ccbd4f84f1 50 */
hendrikvincent 0:05ccbd4f84f1 51 class NTPClient : protected NetService
hendrikvincent 0:05ccbd4f84f1 52 {
hendrikvincent 0:05ccbd4f84f1 53 public:
hendrikvincent 0:05ccbd4f84f1 54 /**
hendrikvincent 0:05ccbd4f84f1 55 Instantiates the NTP client
hendrikvincent 0:05ccbd4f84f1 56 */
hendrikvincent 0:05ccbd4f84f1 57 NTPClient();
hendrikvincent 0:05ccbd4f84f1 58 virtual ~NTPClient();
hendrikvincent 0:05ccbd4f84f1 59
hendrikvincent 0:05ccbd4f84f1 60 //High level setup functions
hendrikvincent 0:05ccbd4f84f1 61
hendrikvincent 0:05ccbd4f84f1 62 ///Gets current time (blocking)
hendrikvincent 0:05ccbd4f84f1 63 /**
hendrikvincent 0:05ccbd4f84f1 64 Updates the time using the server host
hendrikvincent 0:05ccbd4f84f1 65 Blocks until completion
hendrikvincent 0:05ccbd4f84f1 66 @param host : NTP server
hendrikvincent 0:05ccbd4f84f1 67 */
hendrikvincent 0:05ccbd4f84f1 68 NTPResult setTime(const Host& host); //Blocking
hendrikvincent 0:05ccbd4f84f1 69
hendrikvincent 0:05ccbd4f84f1 70 ///Gets current time (non-blocking)
hendrikvincent 0:05ccbd4f84f1 71 /**
hendrikvincent 0:05ccbd4f84f1 72 Updates the time using the server host
hendrikvincent 0:05ccbd4f84f1 73 The function returns immediately and calls the callback on completion or error
hendrikvincent 0:05ccbd4f84f1 74 @param host : NTP server
hendrikvincent 0:05ccbd4f84f1 75 @param pMethod : callback function
hendrikvincent 0:05ccbd4f84f1 76 */
hendrikvincent 0:05ccbd4f84f1 77 NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking
hendrikvincent 0:05ccbd4f84f1 78
hendrikvincent 0:05ccbd4f84f1 79 ///Gets current time (non-blocking)
hendrikvincent 0:05ccbd4f84f1 80 /**
hendrikvincent 0:05ccbd4f84f1 81 Updates the time
hendrikvincent 0:05ccbd4f84f1 82 @param host : NTP server
hendrikvincent 0:05ccbd4f84f1 83 @param pItem : instance of class on which to execute the callback method
hendrikvincent 0:05ccbd4f84f1 84 @param pMethod : callback method
hendrikvincent 0:05ccbd4f84f1 85 The function returns immediately and calls the callback on completion or error
hendrikvincent 0:05ccbd4f84f1 86 */
hendrikvincent 0:05ccbd4f84f1 87 template<class T>
hendrikvincent 0:05ccbd4f84f1 88 NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
hendrikvincent 0:05ccbd4f84f1 89 {
hendrikvincent 0:05ccbd4f84f1 90 setOnResult(pItem, pMethod);
hendrikvincent 0:05ccbd4f84f1 91 doSetTime(host);
hendrikvincent 0:05ccbd4f84f1 92 return NTP_PROCESSING;
hendrikvincent 0:05ccbd4f84f1 93 }
hendrikvincent 0:05ccbd4f84f1 94
hendrikvincent 0:05ccbd4f84f1 95 ///Gets current time (non-blocking)
hendrikvincent 0:05ccbd4f84f1 96 /**
hendrikvincent 0:05ccbd4f84f1 97 Updates the time using the server host
hendrikvincent 0:05ccbd4f84f1 98 The function returns immediately and calls the previously set callback on completion or error
hendrikvincent 0:05ccbd4f84f1 99 @param host : NTP server
hendrikvincent 0:05ccbd4f84f1 100 */
hendrikvincent 0:05ccbd4f84f1 101 void doSetTime(const Host& host);
hendrikvincent 0:05ccbd4f84f1 102
hendrikvincent 0:05ccbd4f84f1 103 ///Setups the result callback
hendrikvincent 0:05ccbd4f84f1 104 /**
hendrikvincent 0:05ccbd4f84f1 105 @param pMethod : callback function
hendrikvincent 0:05ccbd4f84f1 106 */
hendrikvincent 0:05ccbd4f84f1 107 void setOnResult( void (*pMethod)(NTPResult) );
hendrikvincent 0:05ccbd4f84f1 108
hendrikvincent 0:05ccbd4f84f1 109 ///Setups the result callback
hendrikvincent 0:05ccbd4f84f1 110 /**
hendrikvincent 0:05ccbd4f84f1 111 @param pItem : instance of class on which to execute the callback method
hendrikvincent 0:05ccbd4f84f1 112 @param pMethod : callback method
hendrikvincent 0:05ccbd4f84f1 113 */
hendrikvincent 0:05ccbd4f84f1 114 class CDummy;
hendrikvincent 0:05ccbd4f84f1 115 template<class T>
hendrikvincent 0:05ccbd4f84f1 116 void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) )
hendrikvincent 0:05ccbd4f84f1 117 {
hendrikvincent 0:05ccbd4f84f1 118 m_pCbItem = (CDummy*) pItem;
hendrikvincent 0:05ccbd4f84f1 119 m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod;
hendrikvincent 0:05ccbd4f84f1 120 }
hendrikvincent 0:05ccbd4f84f1 121
hendrikvincent 0:05ccbd4f84f1 122 void close();
hendrikvincent 0:05ccbd4f84f1 123
hendrikvincent 0:05ccbd4f84f1 124 protected:
hendrikvincent 0:05ccbd4f84f1 125 virtual void poll(); //Called by NetServices
hendrikvincent 0:05ccbd4f84f1 126
hendrikvincent 0:05ccbd4f84f1 127 private:
hendrikvincent 0:05ccbd4f84f1 128 void init();
hendrikvincent 0:05ccbd4f84f1 129 void open();
hendrikvincent 0:05ccbd4f84f1 130
hendrikvincent 0:05ccbd4f84f1 131 __packed struct NTPPacket //See RFC 4330 for Simple NTP
hendrikvincent 0:05ccbd4f84f1 132 {
hendrikvincent 0:05ccbd4f84f1 133 //WARN: We are in LE! Network is BE!
hendrikvincent 0:05ccbd4f84f1 134 //LSb first
hendrikvincent 0:05ccbd4f84f1 135 unsigned mode : 3;
hendrikvincent 0:05ccbd4f84f1 136 unsigned vn : 3;
hendrikvincent 0:05ccbd4f84f1 137 unsigned li : 2;
hendrikvincent 0:05ccbd4f84f1 138
hendrikvincent 0:05ccbd4f84f1 139 uint8_t stratum;
hendrikvincent 0:05ccbd4f84f1 140 uint8_t poll;
hendrikvincent 0:05ccbd4f84f1 141 uint8_t precision;
hendrikvincent 0:05ccbd4f84f1 142 //32 bits header
hendrikvincent 0:05ccbd4f84f1 143
hendrikvincent 0:05ccbd4f84f1 144 uint32_t rootDelay;
hendrikvincent 0:05ccbd4f84f1 145 uint32_t rootDispersion;
hendrikvincent 0:05ccbd4f84f1 146 uint32_t refId;
hendrikvincent 0:05ccbd4f84f1 147
hendrikvincent 0:05ccbd4f84f1 148 uint32_t refTm_s;
hendrikvincent 0:05ccbd4f84f1 149 uint32_t refTm_f;
hendrikvincent 0:05ccbd4f84f1 150 uint32_t origTm_s;
hendrikvincent 0:05ccbd4f84f1 151 uint32_t origTm_f;
hendrikvincent 0:05ccbd4f84f1 152 uint32_t rxTm_s;
hendrikvincent 0:05ccbd4f84f1 153 uint32_t rxTm_f;
hendrikvincent 0:05ccbd4f84f1 154 uint32_t txTm_s;
hendrikvincent 0:05ccbd4f84f1 155 uint32_t txTm_f;
hendrikvincent 0:05ccbd4f84f1 156 };
hendrikvincent 0:05ccbd4f84f1 157
hendrikvincent 0:05ccbd4f84f1 158 void process(); //Main state-machine
hendrikvincent 0:05ccbd4f84f1 159
hendrikvincent 0:05ccbd4f84f1 160 void setTimeout(int ms);
hendrikvincent 0:05ccbd4f84f1 161 void resetTimeout();
hendrikvincent 0:05ccbd4f84f1 162
hendrikvincent 0:05ccbd4f84f1 163 void onTimeout(); //Connection has timed out
hendrikvincent 0:05ccbd4f84f1 164 void onDNSReply(DNSReply r);
hendrikvincent 0:05ccbd4f84f1 165 void onUDPSocketEvent(UDPSocketEvent e);
hendrikvincent 0:05ccbd4f84f1 166 void onResult(NTPResult r); //Called when exchange completed or on failure
hendrikvincent 0:05ccbd4f84f1 167
hendrikvincent 0:05ccbd4f84f1 168 NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
hendrikvincent 0:05ccbd4f84f1 169
hendrikvincent 0:05ccbd4f84f1 170 UDPSocket* m_pUDPSocket;
hendrikvincent 0:05ccbd4f84f1 171
hendrikvincent 0:05ccbd4f84f1 172 enum NTPStep
hendrikvincent 0:05ccbd4f84f1 173 {
hendrikvincent 0:05ccbd4f84f1 174 NTP_PING,
hendrikvincent 0:05ccbd4f84f1 175 NTP_PONG
hendrikvincent 0:05ccbd4f84f1 176 };
hendrikvincent 0:05ccbd4f84f1 177
hendrikvincent 0:05ccbd4f84f1 178 NTPStep m_state;
hendrikvincent 0:05ccbd4f84f1 179
hendrikvincent 0:05ccbd4f84f1 180 NTPPacket m_pkt;
hendrikvincent 0:05ccbd4f84f1 181
hendrikvincent 0:05ccbd4f84f1 182 CDummy* m_pCbItem;
hendrikvincent 0:05ccbd4f84f1 183 void (CDummy::*m_pCbMeth)(NTPResult);
hendrikvincent 0:05ccbd4f84f1 184
hendrikvincent 0:05ccbd4f84f1 185 void (*m_pCb)(NTPResult);
hendrikvincent 0:05ccbd4f84f1 186
hendrikvincent 0:05ccbd4f84f1 187 Timer m_watchdog;
hendrikvincent 0:05ccbd4f84f1 188 int m_timeout;
hendrikvincent 0:05ccbd4f84f1 189
hendrikvincent 0:05ccbd4f84f1 190 bool m_closed;
hendrikvincent 0:05ccbd4f84f1 191
hendrikvincent 0:05ccbd4f84f1 192 Host m_host;
hendrikvincent 0:05ccbd4f84f1 193
hendrikvincent 0:05ccbd4f84f1 194 DNSRequest* m_pDnsReq;
hendrikvincent 0:05ccbd4f84f1 195
hendrikvincent 0:05ccbd4f84f1 196 NTPResult m_blockingResult; //Result if blocking mode
hendrikvincent 0:05ccbd4f84f1 197
hendrikvincent 0:05ccbd4f84f1 198 };
hendrikvincent 0:05ccbd4f84f1 199
hendrikvincent 0:05ccbd4f84f1 200 #endif