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