Dependents: SimpleLCDClock readCard2Twitter_http AnalogClock_StepperMotor_NTP ServoCamV1
NtpClient.h
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 #ifndef SMTP_CLIENT_H 00025 #define SMTP_CLIENT_H 00026 00027 #include "if/net/net.h" 00028 #include "api/UdpSocket.h" 00029 #include "api/DnsRequest.h" 00030 #include "mbed.h" 00031 00032 #define NTP_PORT 123 00033 #define NTP_REQUEST_TIMEOUT 15000 00034 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900) 00035 00036 enum NtpResult 00037 { 00038 NTP_OK, 00039 NTP_PROCESSING, 00040 NTP_PRTCL, //Protocol error 00041 NTP_TIMEOUT, //Connection timeout 00042 NTP_DNS //Could not resolve DNS Addr 00043 }; 00044 00045 class NtpClient 00046 { 00047 public: 00048 NtpClient(); 00049 virtual ~NtpClient(); 00050 00051 //High level setup functions 00052 NtpResult setTime(const Host& host); //Blocking 00053 NtpResult setTime(const Host& host, void (*pMethod)(NtpResult)); //Non blocking 00054 template<class T> 00055 NtpResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NtpResult)) //Non blocking 00056 { 00057 setOnResult(pItem, pMethod); 00058 doSetTime(host); 00059 return NTP_PROCESSING; 00060 } 00061 00062 void doSetTime(const Host& host); 00063 00064 void setOnResult( void (*pMethod)(NtpResult) ); 00065 class CDummy; 00066 template<class T> 00067 void setOnResult( T* pItem, void (T::*pMethod)(NtpResult) ) 00068 { 00069 m_pCbItem = (CDummy*) pItem; 00070 m_pCbMeth = (void (CDummy::*)(NtpResult)) pMethod; 00071 } 00072 00073 void init(); 00074 void close(); 00075 00076 private: 00077 __packed struct NtpPacket //See RFC 4330 for Simple NTP 00078 { 00079 //WARN: We are in LE! Network is BE! 00080 //LSb first 00081 unsigned mode : 3; 00082 unsigned vn : 3; 00083 unsigned li : 2; 00084 00085 uint8_t stratum; 00086 uint8_t poll; 00087 uint8_t precision; 00088 //32 bits header 00089 00090 uint32_t rootDelay; 00091 uint32_t rootDispersion; 00092 uint32_t refId; 00093 00094 uint32_t refTm_s; 00095 uint32_t refTm_f; 00096 uint32_t origTm_s; 00097 uint32_t origTm_f; 00098 uint32_t rxTm_s; 00099 uint32_t rxTm_f; 00100 uint32_t txTm_s; 00101 uint32_t txTm_f; 00102 }; 00103 00104 void process(); //Main state-machine 00105 00106 void setTimeout(int ms); 00107 void resetTimeout(); 00108 00109 void onTimeout(); //Connection has timed out 00110 void onDnsReply(DnsReply r); 00111 void onUdpSocketEvent(UdpSocketEvent e); 00112 void onResult(NtpResult r); //Called when exchange completed or on failure 00113 00114 NtpResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available 00115 00116 UdpSocket* m_pUdpSocket; 00117 00118 enum NtpStep 00119 { 00120 NTP_PING, 00121 NTP_PONG 00122 }; 00123 00124 NtpStep m_state; 00125 00126 CDummy* m_pCbItem; 00127 void (CDummy::*m_pCbMeth)(NtpResult); 00128 00129 void (*m_pCb)(NtpResult); 00130 00131 Timeout m_watchdog; 00132 int m_timeout; 00133 00134 bool m_closed; 00135 00136 Host m_host; 00137 00138 DnsRequest* m_pDnsReq; 00139 00140 NtpResult m_blockingResult; //Result if blocking mode 00141 00142 }; 00143 00144 #endif
Generated on Tue Jul 12 2022 22:47:25 by 1.7.2