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