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