Atsushi Hattori
/
20210627_Logger
hattori&ide
Embed:
(wiki syntax)
Show/hide line numbers
NTPClient.h
Go to the documentation of this file.
00001 00002 /* 00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) 00004 00005 Permission is hereby granted, free of charge, to any person obtaining a copy 00006 of this software and associated documentation files (the "Software"), to deal 00007 in the Software without restriction, including without limitation the rights 00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00009 copies of the Software, and to permit persons to whom the Software is 00010 furnished to do so, subject to the following conditions: 00011 00012 The above copyright notice and this permission notice shall be included in 00013 all copies or substantial portions of the Software. 00014 00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00021 THE SOFTWARE. 00022 */ 00023 00024 /** \file 00025 NTP Client header file 00026 */ 00027 00028 #ifndef NTP_CLIENT_H 00029 #define NTP_CLIENT_H 00030 00031 #include "core/net.h" 00032 #include "core/netservice.h" 00033 #include "api/UDPSocket.h" 00034 #include "api/DNSRequest.h" 00035 #include "mbed.h" 00036 00037 ///NTP Client results 00038 enum NTPResult 00039 { 00040 NTP_OK, ///<Success 00041 NTP_PROCESSING, ///<Processing 00042 NTP_PRTCL, ///<Protocol error 00043 NTP_TIMEOUT, ///<Connection timeout 00044 NTP_DNS ///<Could not resolve DNS hostname 00045 }; 00046 00047 ///A NTP Client 00048 /** 00049 The NTP client is a simple UDP client that will update the mbed's RTC 00050 */ 00051 class NTPClient : protected NetService 00052 { 00053 public: 00054 /** 00055 Instantiates the NTP client 00056 */ 00057 NTPClient(); 00058 virtual ~NTPClient(); 00059 00060 //High level setup functions 00061 00062 ///Gets current time (blocking) 00063 /** 00064 Updates the time using the server host 00065 Blocks until completion 00066 @param host : NTP server 00067 */ 00068 NTPResult setTime(const Host& host); //Blocking 00069 00070 ///Gets current time (non-blocking) 00071 /** 00072 Updates the time using the server host 00073 The function returns immediately and calls the callback on completion or error 00074 @param host : NTP server 00075 @param pMethod : callback function 00076 */ 00077 NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking 00078 00079 ///Gets current time (non-blocking) 00080 /** 00081 Updates the time 00082 @param host : NTP server 00083 @param pItem : instance of class on which to execute the callback method 00084 @param pMethod : callback method 00085 The function returns immediately and calls the callback on completion or error 00086 */ 00087 template<class T> 00088 NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking 00089 { 00090 setOnResult(pItem, pMethod); 00091 doSetTime(host); 00092 return NTP_PROCESSING; 00093 } 00094 00095 ///Gets current time (non-blocking) 00096 /** 00097 Updates the time using the server host 00098 The function returns immediately and calls the previously set callback on completion or error 00099 @param host : NTP server 00100 */ 00101 void doSetTime(const Host& host); 00102 00103 ///Setups the result callback 00104 /** 00105 @param pMethod : callback function 00106 */ 00107 void setOnResult( void (*pMethod)(NTPResult) ); 00108 00109 ///Setups the result callback 00110 /** 00111 @param pItem : instance of class on which to execute the callback method 00112 @param pMethod : callback method 00113 */ 00114 class CDummy; 00115 template<class T> 00116 void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) ) 00117 { 00118 m_pCbItem = (CDummy*) pItem; 00119 m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod; 00120 } 00121 00122 void close(); 00123 00124 protected: 00125 virtual void poll(); //Called by NetServices 00126 00127 private: 00128 void init(); 00129 void open(); 00130 00131 __packed struct NTPPacket //See RFC 4330 for Simple NTP 00132 { 00133 //WARN: We are in LE! Network is BE! 00134 //LSb first 00135 unsigned mode : 3; 00136 unsigned vn : 3; 00137 unsigned li : 2; 00138 00139 uint8_t stratum; 00140 uint8_t poll; 00141 uint8_t precision; 00142 //32 bits header 00143 00144 uint32_t rootDelay; 00145 uint32_t rootDispersion; 00146 uint32_t refId; 00147 00148 uint32_t refTm_s; 00149 uint32_t refTm_f; 00150 uint32_t origTm_s; 00151 uint32_t origTm_f; 00152 uint32_t rxTm_s; 00153 uint32_t rxTm_f; 00154 uint32_t txTm_s; 00155 uint32_t txTm_f; 00156 }; 00157 00158 void process(); //Main state-machine 00159 00160 void setTimeout(int ms); 00161 void resetTimeout(); 00162 00163 void onTimeout(); //Connection has timed out 00164 void onDNSReply(DNSReply r); 00165 void onUDPSocketEvent(UDPSocketEvent e); 00166 void onResult(NTPResult r); //Called when exchange completed or on failure 00167 00168 NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available 00169 00170 UDPSocket* m_pUDPSocket; 00171 00172 enum NTPStep 00173 { 00174 NTP_PING, 00175 NTP_PONG 00176 }; 00177 00178 NTPStep m_state; 00179 00180 NTPPacket m_pkt; 00181 00182 CDummy* m_pCbItem; 00183 void (CDummy::*m_pCbMeth)(NTPResult); 00184 00185 void (*m_pCb)(NTPResult); 00186 00187 Timer m_watchdog; 00188 int m_timeout; 00189 00190 bool m_closed; 00191 00192 Host m_host; 00193 00194 DNSRequest* m_pDnsReq; 00195 00196 NTPResult m_blockingResult; //Result if blocking mode 00197 00198 }; 00199 00200 #endif
Generated on Sun Dec 18 2022 08:16:46 by 1.7.2