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