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