The extracted NTP client from Segundos NetServices library, for use with the [[http://mbed.org/users/hlipka/libraries/NetServicesMin|NetServicesMin]] library. The only fixed bug is the memory leak / OOM problem. Needs the [[http://mbed.org/users/hlipka/libraries/DNSResolver|DNSResolver]] library as well.
Dependents: SPIVFDclock LPC1768_AppBoard_Internet_LCD_Clock
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 /** \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 "mbed.h" 00035 00036 ///NTP Client results 00037 enum NTPResult 00038 { 00039 NTP_OK, ///<Success 00040 NTP_PROCESSING, ///<Processing 00041 NTP_PRTCL, ///<Protocol error 00042 NTP_TIMEOUT, ///<Connection timeout 00043 NTP_DNS ///<Could not resolve DNS hostname 00044 }; 00045 00046 __packed struct NTPPacket //See RFC 4330 for Simple NTP 00047 { 00048 //WARN: We are in LE! Network is BE! 00049 //LSb first 00050 unsigned mode : 3; 00051 unsigned vn : 3; 00052 unsigned li : 2; 00053 00054 uint8_t stratum; 00055 uint8_t poll; 00056 uint8_t precision; 00057 //32 bits header 00058 00059 uint32_t rootDelay; 00060 uint32_t rootDispersion; 00061 uint32_t refId; 00062 00063 uint32_t refTm_s; 00064 uint32_t refTm_f; 00065 uint32_t origTm_s; 00066 uint32_t origTm_f; 00067 uint32_t rxTm_s; 00068 uint32_t rxTm_f; 00069 uint32_t txTm_s; 00070 uint32_t txTm_f; 00071 }; 00072 00073 /** 00074 The NTP client is a simple UDP client that will update the mbed's RTC 00075 */ 00076 class NTPClient 00077 { 00078 public: 00079 /** 00080 Instantiates the NTP client 00081 */ 00082 NTPClient(); 00083 ~NTPClient(); 00084 00085 /** 00086 Updates the time using the server host, blocks until completion 00087 @param host : NTP server 00088 */ 00089 NTPResult setTime(const Host& host); //Blocking 00090 00091 void close(); 00092 00093 private: 00094 void init(); 00095 void open(); 00096 00097 /** 00098 Updates the time using the server host 00099 The function returns immediately and calls the previously set callback on completion or error 00100 @param host : NTP server 00101 */ 00102 void doSetTime(const Host& host); 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 onUDPSocketEvent(UDPSocketEvent e); 00111 void onResult(NTPResult r); //Called when exchange completed or on failure 00112 00113 NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available 00114 00115 UDPSocket* _pUDPSocket; 00116 00117 00118 00119 enum NTPStep 00120 { 00121 NTP_PING, 00122 NTP_PONG 00123 }; 00124 00125 NTPStep _state; 00126 00127 NTPPacket _pkt; 00128 00129 Timer *_watchdog; 00130 int _timeout; 00131 00132 bool _closed; 00133 00134 Host _host; 00135 00136 NTPResult _blockingResult; //Result if blocking mode 00137 00138 }; 00139 00140 #endif
Generated on Wed Jul 13 2022 20:04:13 by 1.7.2