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