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