NTP Client for the "old" NetServices libraries

Dependents:   NTPClientExample server1 server2 RFID2Twitter ... more

Committer:
donatien
Date:
Thu Aug 05 15:16:59 2010 +0000
Revision:
4:7c3f1199256a
Parent:
1:1f436e56925f

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 0:de37f60b4d0f 1
donatien 0:de37f60b4d0f 2 /*
donatien 0:de37f60b4d0f 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
donatien 0:de37f60b4d0f 4
donatien 0:de37f60b4d0f 5 Permission is hereby granted, free of charge, to any person obtaining a copy
donatien 0:de37f60b4d0f 6 of this software and associated documentation files (the "Software"), to deal
donatien 0:de37f60b4d0f 7 in the Software without restriction, including without limitation the rights
donatien 0:de37f60b4d0f 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
donatien 0:de37f60b4d0f 9 copies of the Software, and to permit persons to whom the Software is
donatien 0:de37f60b4d0f 10 furnished to do so, subject to the following conditions:
donatien 0:de37f60b4d0f 11
donatien 0:de37f60b4d0f 12 The above copyright notice and this permission notice shall be included in
donatien 0:de37f60b4d0f 13 all copies or substantial portions of the Software.
donatien 0:de37f60b4d0f 14
donatien 0:de37f60b4d0f 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
donatien 0:de37f60b4d0f 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
donatien 0:de37f60b4d0f 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
donatien 0:de37f60b4d0f 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
donatien 0:de37f60b4d0f 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
donatien 0:de37f60b4d0f 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
donatien 0:de37f60b4d0f 21 THE SOFTWARE.
donatien 0:de37f60b4d0f 22 */
donatien 0:de37f60b4d0f 23
donatien 4:7c3f1199256a 24 /** \file
donatien 4:7c3f1199256a 25 NTP Client header file
donatien 4:7c3f1199256a 26 */
donatien 4:7c3f1199256a 27
donatien 0:de37f60b4d0f 28 #ifndef NTP_CLIENT_H
donatien 0:de37f60b4d0f 29 #define NTP_CLIENT_H
donatien 0:de37f60b4d0f 30
donatien 4:7c3f1199256a 31 #include "core/net.h"
donatien 4:7c3f1199256a 32 #include "core/netservice.h"
donatien 0:de37f60b4d0f 33 #include "api/UDPSocket.h"
donatien 0:de37f60b4d0f 34 #include "api/DNSRequest.h"
donatien 0:de37f60b4d0f 35 #include "mbed.h"
donatien 0:de37f60b4d0f 36
donatien 4:7c3f1199256a 37 ///NTP Client results
donatien 0:de37f60b4d0f 38 enum NTPResult
donatien 0:de37f60b4d0f 39 {
donatien 4:7c3f1199256a 40 NTP_OK, ///<Success
donatien 4:7c3f1199256a 41 NTP_PROCESSING, ///<Processing
donatien 4:7c3f1199256a 42 NTP_PRTCL, ///<Protocol error
donatien 4:7c3f1199256a 43 NTP_TIMEOUT, ///<Connection timeout
donatien 4:7c3f1199256a 44 NTP_DNS ///<Could not resolve DNS hostname
donatien 0:de37f60b4d0f 45 };
donatien 0:de37f60b4d0f 46
donatien 4:7c3f1199256a 47 ///A NTP Client
donatien 4:7c3f1199256a 48 /**
donatien 4:7c3f1199256a 49 The NTP client is a simple UDP client that will update the mbed's RTC
donatien 4:7c3f1199256a 50 */
donatien 1:1f436e56925f 51 class NTPClient : protected NetService
donatien 0:de37f60b4d0f 52 {
donatien 0:de37f60b4d0f 53 public:
donatien 4:7c3f1199256a 54 /**
donatien 4:7c3f1199256a 55 Instantiates the NTP client
donatien 4:7c3f1199256a 56 */
donatien 0:de37f60b4d0f 57 NTPClient();
donatien 0:de37f60b4d0f 58 virtual ~NTPClient();
donatien 0:de37f60b4d0f 59
donatien 0:de37f60b4d0f 60 //High level setup functions
donatien 4:7c3f1199256a 61
donatien 4:7c3f1199256a 62 ///Gets current time (blocking)
donatien 4:7c3f1199256a 63 /**
donatien 4:7c3f1199256a 64 Updates the time using the server host
donatien 4:7c3f1199256a 65 Blocks until completion
donatien 4:7c3f1199256a 66 @param host : NTP server
donatien 4:7c3f1199256a 67 */
donatien 0:de37f60b4d0f 68 NTPResult setTime(const Host& host); //Blocking
donatien 4:7c3f1199256a 69
donatien 4:7c3f1199256a 70 ///Gets current time (non-blocking)
donatien 4:7c3f1199256a 71 /**
donatien 4:7c3f1199256a 72 Updates the time using the server host
donatien 4:7c3f1199256a 73 The function returns immediately and calls the callback on completion or error
donatien 4:7c3f1199256a 74 @param host : NTP server
donatien 4:7c3f1199256a 75 @param pMethod : callback function
donatien 4:7c3f1199256a 76 */
donatien 0:de37f60b4d0f 77 NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking
donatien 4:7c3f1199256a 78
donatien 4:7c3f1199256a 79 ///Gets current time (non-blocking)
donatien 4:7c3f1199256a 80 /**
donatien 4:7c3f1199256a 81 Updates the time
donatien 4:7c3f1199256a 82 @param host : NTP server
donatien 4:7c3f1199256a 83 @param pItem : instance of class on which to execute the callback method
donatien 4:7c3f1199256a 84 @param pMethod : callback method
donatien 4:7c3f1199256a 85 The function returns immediately and calls the callback on completion or error
donatien 4:7c3f1199256a 86 */
donatien 0:de37f60b4d0f 87 template<class T>
donatien 0:de37f60b4d0f 88 NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
donatien 0:de37f60b4d0f 89 {
donatien 0:de37f60b4d0f 90 setOnResult(pItem, pMethod);
donatien 0:de37f60b4d0f 91 doSetTime(host);
donatien 0:de37f60b4d0f 92 return NTP_PROCESSING;
donatien 0:de37f60b4d0f 93 }
donatien 0:de37f60b4d0f 94
donatien 4:7c3f1199256a 95 ///Gets current time (non-blocking)
donatien 4:7c3f1199256a 96 /**
donatien 4:7c3f1199256a 97 Updates the time using the server host
donatien 4:7c3f1199256a 98 The function returns immediately and calls the previously set callback on completion or error
donatien 4:7c3f1199256a 99 @param host : NTP server
donatien 4:7c3f1199256a 100 */
donatien 0:de37f60b4d0f 101 void doSetTime(const Host& host);
donatien 0:de37f60b4d0f 102
donatien 4:7c3f1199256a 103 ///Setups the result callback
donatien 4:7c3f1199256a 104 /**
donatien 4:7c3f1199256a 105 @param pMethod : callback function
donatien 4:7c3f1199256a 106 */
donatien 0:de37f60b4d0f 107 void setOnResult( void (*pMethod)(NTPResult) );
donatien 4:7c3f1199256a 108
donatien 4:7c3f1199256a 109 ///Setups the result callback
donatien 4:7c3f1199256a 110 /**
donatien 4:7c3f1199256a 111 @param pItem : instance of class on which to execute the callback method
donatien 4:7c3f1199256a 112 @param pMethod : callback method
donatien 4:7c3f1199256a 113 */
donatien 0:de37f60b4d0f 114 class CDummy;
donatien 0:de37f60b4d0f 115 template<class T>
donatien 0:de37f60b4d0f 116 void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) )
donatien 0:de37f60b4d0f 117 {
donatien 0:de37f60b4d0f 118 m_pCbItem = (CDummy*) pItem;
donatien 0:de37f60b4d0f 119 m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod;
donatien 0:de37f60b4d0f 120 }
donatien 0:de37f60b4d0f 121
donatien 0:de37f60b4d0f 122 void close();
donatien 0:de37f60b4d0f 123
donatien 1:1f436e56925f 124 protected:
donatien 1:1f436e56925f 125 virtual void poll(); //Called by NetServices
donatien 1:1f436e56925f 126
donatien 0:de37f60b4d0f 127 private:
donatien 1:1f436e56925f 128 void init();
donatien 1:1f436e56925f 129 void open();
donatien 1:1f436e56925f 130
donatien 0:de37f60b4d0f 131 __packed struct NTPPacket //See RFC 4330 for Simple NTP
donatien 0:de37f60b4d0f 132 {
donatien 0:de37f60b4d0f 133 //WARN: We are in LE! Network is BE!
donatien 0:de37f60b4d0f 134 //LSb first
donatien 0:de37f60b4d0f 135 unsigned mode : 3;
donatien 0:de37f60b4d0f 136 unsigned vn : 3;
donatien 0:de37f60b4d0f 137 unsigned li : 2;
donatien 0:de37f60b4d0f 138
donatien 0:de37f60b4d0f 139 uint8_t stratum;
donatien 0:de37f60b4d0f 140 uint8_t poll;
donatien 0:de37f60b4d0f 141 uint8_t precision;
donatien 0:de37f60b4d0f 142 //32 bits header
donatien 0:de37f60b4d0f 143
donatien 0:de37f60b4d0f 144 uint32_t rootDelay;
donatien 0:de37f60b4d0f 145 uint32_t rootDispersion;
donatien 0:de37f60b4d0f 146 uint32_t refId;
donatien 0:de37f60b4d0f 147
donatien 0:de37f60b4d0f 148 uint32_t refTm_s;
donatien 0:de37f60b4d0f 149 uint32_t refTm_f;
donatien 0:de37f60b4d0f 150 uint32_t origTm_s;
donatien 0:de37f60b4d0f 151 uint32_t origTm_f;
donatien 0:de37f60b4d0f 152 uint32_t rxTm_s;
donatien 0:de37f60b4d0f 153 uint32_t rxTm_f;
donatien 0:de37f60b4d0f 154 uint32_t txTm_s;
donatien 0:de37f60b4d0f 155 uint32_t txTm_f;
donatien 0:de37f60b4d0f 156 };
donatien 0:de37f60b4d0f 157
donatien 0:de37f60b4d0f 158 void process(); //Main state-machine
donatien 0:de37f60b4d0f 159
donatien 0:de37f60b4d0f 160 void setTimeout(int ms);
donatien 0:de37f60b4d0f 161 void resetTimeout();
donatien 0:de37f60b4d0f 162
donatien 0:de37f60b4d0f 163 void onTimeout(); //Connection has timed out
donatien 0:de37f60b4d0f 164 void onDNSReply(DNSReply r);
donatien 0:de37f60b4d0f 165 void onUDPSocketEvent(UDPSocketEvent e);
donatien 0:de37f60b4d0f 166 void onResult(NTPResult r); //Called when exchange completed or on failure
donatien 0:de37f60b4d0f 167
donatien 0:de37f60b4d0f 168 NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
donatien 0:de37f60b4d0f 169
donatien 0:de37f60b4d0f 170 UDPSocket* m_pUDPSocket;
donatien 0:de37f60b4d0f 171
donatien 0:de37f60b4d0f 172 enum NTPStep
donatien 0:de37f60b4d0f 173 {
donatien 0:de37f60b4d0f 174 NTP_PING,
donatien 0:de37f60b4d0f 175 NTP_PONG
donatien 0:de37f60b4d0f 176 };
donatien 0:de37f60b4d0f 177
donatien 0:de37f60b4d0f 178 NTPStep m_state;
donatien 0:de37f60b4d0f 179
donatien 1:1f436e56925f 180 NTPPacket m_pkt;
donatien 1:1f436e56925f 181
donatien 0:de37f60b4d0f 182 CDummy* m_pCbItem;
donatien 0:de37f60b4d0f 183 void (CDummy::*m_pCbMeth)(NTPResult);
donatien 0:de37f60b4d0f 184
donatien 0:de37f60b4d0f 185 void (*m_pCb)(NTPResult);
donatien 0:de37f60b4d0f 186
donatien 1:1f436e56925f 187 Timer m_watchdog;
donatien 0:de37f60b4d0f 188 int m_timeout;
donatien 0:de37f60b4d0f 189
donatien 0:de37f60b4d0f 190 bool m_closed;
donatien 0:de37f60b4d0f 191
donatien 0:de37f60b4d0f 192 Host m_host;
donatien 0:de37f60b4d0f 193
donatien 0:de37f60b4d0f 194 DNSRequest* m_pDnsReq;
donatien 0:de37f60b4d0f 195
donatien 0:de37f60b4d0f 196 NTPResult m_blockingResult; //Result if blocking mode
donatien 0:de37f60b4d0f 197
donatien 0:de37f60b4d0f 198 };
donatien 0:de37f60b4d0f 199
donatien 0:de37f60b4d0f 200 #endif