NTP Client for the "old" NetServices libraries
Dependents: NTPClientExample server1 server2 RFID2Twitter ... more
LPC1768/services/ntp/NTPClient.h@0:de37f60b4d0f, 2010-06-11 (annotated)
- Committer:
- donatien
- Date:
- Fri Jun 11 16:27:11 2010 +0000
- Revision:
- 0:de37f60b4d0f
- Child:
- 1:1f436e56925f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:de37f60b4d0f | 1 | |
donatien | 0:de37f60b4d0f | 2 | /* |
donatien | 0:de37f60b4d0f | 3 | Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com) |
donatien | 0:de37f60b4d0f | 4 | |
donatien | 0:de37f60b4d0f | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy |
donatien | 0:de37f60b4d0f | 6 | of this software and associated documentation files (the "Software"), to deal |
donatien | 0:de37f60b4d0f | 7 | in the Software without restriction, including without limitation the rights |
donatien | 0:de37f60b4d0f | 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
donatien | 0:de37f60b4d0f | 9 | copies of the Software, and to permit persons to whom the Software is |
donatien | 0:de37f60b4d0f | 10 | furnished to do so, subject to the following conditions: |
donatien | 0:de37f60b4d0f | 11 | |
donatien | 0:de37f60b4d0f | 12 | The above copyright notice and this permission notice shall be included in |
donatien | 0:de37f60b4d0f | 13 | all copies or substantial portions of the Software. |
donatien | 0:de37f60b4d0f | 14 | |
donatien | 0:de37f60b4d0f | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
donatien | 0:de37f60b4d0f | 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
donatien | 0:de37f60b4d0f | 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
donatien | 0:de37f60b4d0f | 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
donatien | 0:de37f60b4d0f | 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:de37f60b4d0f | 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
donatien | 0:de37f60b4d0f | 21 | THE SOFTWARE. |
donatien | 0:de37f60b4d0f | 22 | */ |
donatien | 0:de37f60b4d0f | 23 | |
donatien | 0:de37f60b4d0f | 24 | #ifndef NTP_CLIENT_H |
donatien | 0:de37f60b4d0f | 25 | #define NTP_CLIENT_H |
donatien | 0:de37f60b4d0f | 26 | |
donatien | 0:de37f60b4d0f | 27 | #include "if/net/net.h" |
donatien | 0:de37f60b4d0f | 28 | #include "api/UDPSocket.h" |
donatien | 0:de37f60b4d0f | 29 | #include "api/DNSRequest.h" |
donatien | 0:de37f60b4d0f | 30 | #include "mbed.h" |
donatien | 0:de37f60b4d0f | 31 | |
donatien | 0:de37f60b4d0f | 32 | enum NTPResult |
donatien | 0:de37f60b4d0f | 33 | { |
donatien | 0:de37f60b4d0f | 34 | NTP_OK, |
donatien | 0:de37f60b4d0f | 35 | NTP_PROCESSING, |
donatien | 0:de37f60b4d0f | 36 | NTP_PRTCL, //Protocol error |
donatien | 0:de37f60b4d0f | 37 | NTP_TIMEOUT, //Connection timeout |
donatien | 0:de37f60b4d0f | 38 | NTP_DNS //Could not resolve DNS Addr |
donatien | 0:de37f60b4d0f | 39 | }; |
donatien | 0:de37f60b4d0f | 40 | |
donatien | 0:de37f60b4d0f | 41 | class NTPClient |
donatien | 0:de37f60b4d0f | 42 | { |
donatien | 0:de37f60b4d0f | 43 | public: |
donatien | 0:de37f60b4d0f | 44 | NTPClient(); |
donatien | 0:de37f60b4d0f | 45 | virtual ~NTPClient(); |
donatien | 0:de37f60b4d0f | 46 | |
donatien | 0:de37f60b4d0f | 47 | //High level setup functions |
donatien | 0:de37f60b4d0f | 48 | NTPResult setTime(const Host& host); //Blocking |
donatien | 0:de37f60b4d0f | 49 | NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking |
donatien | 0:de37f60b4d0f | 50 | template<class T> |
donatien | 0:de37f60b4d0f | 51 | NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking |
donatien | 0:de37f60b4d0f | 52 | { |
donatien | 0:de37f60b4d0f | 53 | setOnResult(pItem, pMethod); |
donatien | 0:de37f60b4d0f | 54 | doSetTime(host); |
donatien | 0:de37f60b4d0f | 55 | return NTP_PROCESSING; |
donatien | 0:de37f60b4d0f | 56 | } |
donatien | 0:de37f60b4d0f | 57 | |
donatien | 0:de37f60b4d0f | 58 | void doSetTime(const Host& host); |
donatien | 0:de37f60b4d0f | 59 | |
donatien | 0:de37f60b4d0f | 60 | void setOnResult( void (*pMethod)(NTPResult) ); |
donatien | 0:de37f60b4d0f | 61 | class CDummy; |
donatien | 0:de37f60b4d0f | 62 | template<class T> |
donatien | 0:de37f60b4d0f | 63 | void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) ) |
donatien | 0:de37f60b4d0f | 64 | { |
donatien | 0:de37f60b4d0f | 65 | m_pCbItem = (CDummy*) pItem; |
donatien | 0:de37f60b4d0f | 66 | m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod; |
donatien | 0:de37f60b4d0f | 67 | } |
donatien | 0:de37f60b4d0f | 68 | |
donatien | 0:de37f60b4d0f | 69 | void init(); |
donatien | 0:de37f60b4d0f | 70 | void close(); |
donatien | 0:de37f60b4d0f | 71 | |
donatien | 0:de37f60b4d0f | 72 | private: |
donatien | 0:de37f60b4d0f | 73 | __packed struct NTPPacket //See RFC 4330 for Simple NTP |
donatien | 0:de37f60b4d0f | 74 | { |
donatien | 0:de37f60b4d0f | 75 | //WARN: We are in LE! Network is BE! |
donatien | 0:de37f60b4d0f | 76 | //LSb first |
donatien | 0:de37f60b4d0f | 77 | unsigned mode : 3; |
donatien | 0:de37f60b4d0f | 78 | unsigned vn : 3; |
donatien | 0:de37f60b4d0f | 79 | unsigned li : 2; |
donatien | 0:de37f60b4d0f | 80 | |
donatien | 0:de37f60b4d0f | 81 | uint8_t stratum; |
donatien | 0:de37f60b4d0f | 82 | uint8_t poll; |
donatien | 0:de37f60b4d0f | 83 | uint8_t precision; |
donatien | 0:de37f60b4d0f | 84 | //32 bits header |
donatien | 0:de37f60b4d0f | 85 | |
donatien | 0:de37f60b4d0f | 86 | uint32_t rootDelay; |
donatien | 0:de37f60b4d0f | 87 | uint32_t rootDispersion; |
donatien | 0:de37f60b4d0f | 88 | uint32_t refId; |
donatien | 0:de37f60b4d0f | 89 | |
donatien | 0:de37f60b4d0f | 90 | uint32_t refTm_s; |
donatien | 0:de37f60b4d0f | 91 | uint32_t refTm_f; |
donatien | 0:de37f60b4d0f | 92 | uint32_t origTm_s; |
donatien | 0:de37f60b4d0f | 93 | uint32_t origTm_f; |
donatien | 0:de37f60b4d0f | 94 | uint32_t rxTm_s; |
donatien | 0:de37f60b4d0f | 95 | uint32_t rxTm_f; |
donatien | 0:de37f60b4d0f | 96 | uint32_t txTm_s; |
donatien | 0:de37f60b4d0f | 97 | uint32_t txTm_f; |
donatien | 0:de37f60b4d0f | 98 | }; |
donatien | 0:de37f60b4d0f | 99 | |
donatien | 0:de37f60b4d0f | 100 | void process(); //Main state-machine |
donatien | 0:de37f60b4d0f | 101 | |
donatien | 0:de37f60b4d0f | 102 | void setTimeout(int ms); |
donatien | 0:de37f60b4d0f | 103 | void resetTimeout(); |
donatien | 0:de37f60b4d0f | 104 | |
donatien | 0:de37f60b4d0f | 105 | void onTimeout(); //Connection has timed out |
donatien | 0:de37f60b4d0f | 106 | void onDNSReply(DNSReply r); |
donatien | 0:de37f60b4d0f | 107 | void onUDPSocketEvent(UDPSocketEvent e); |
donatien | 0:de37f60b4d0f | 108 | void onResult(NTPResult r); //Called when exchange completed or on failure |
donatien | 0:de37f60b4d0f | 109 | |
donatien | 0:de37f60b4d0f | 110 | NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available |
donatien | 0:de37f60b4d0f | 111 | |
donatien | 0:de37f60b4d0f | 112 | UDPSocket* m_pUDPSocket; |
donatien | 0:de37f60b4d0f | 113 | |
donatien | 0:de37f60b4d0f | 114 | enum NTPStep |
donatien | 0:de37f60b4d0f | 115 | { |
donatien | 0:de37f60b4d0f | 116 | NTP_PING, |
donatien | 0:de37f60b4d0f | 117 | NTP_PONG |
donatien | 0:de37f60b4d0f | 118 | }; |
donatien | 0:de37f60b4d0f | 119 | |
donatien | 0:de37f60b4d0f | 120 | NTPStep m_state; |
donatien | 0:de37f60b4d0f | 121 | |
donatien | 0:de37f60b4d0f | 122 | CDummy* m_pCbItem; |
donatien | 0:de37f60b4d0f | 123 | void (CDummy::*m_pCbMeth)(NTPResult); |
donatien | 0:de37f60b4d0f | 124 | |
donatien | 0:de37f60b4d0f | 125 | void (*m_pCb)(NTPResult); |
donatien | 0:de37f60b4d0f | 126 | |
donatien | 0:de37f60b4d0f | 127 | Timeout m_watchdog; |
donatien | 0:de37f60b4d0f | 128 | int m_timeout; |
donatien | 0:de37f60b4d0f | 129 | |
donatien | 0:de37f60b4d0f | 130 | bool m_closed; |
donatien | 0:de37f60b4d0f | 131 | |
donatien | 0:de37f60b4d0f | 132 | Host m_host; |
donatien | 0:de37f60b4d0f | 133 | |
donatien | 0:de37f60b4d0f | 134 | DNSRequest* m_pDnsReq; |
donatien | 0:de37f60b4d0f | 135 | |
donatien | 0:de37f60b4d0f | 136 | NTPResult m_blockingResult; //Result if blocking mode |
donatien | 0:de37f60b4d0f | 137 | |
donatien | 0:de37f60b4d0f | 138 | }; |
donatien | 0:de37f60b4d0f | 139 | |
donatien | 0:de37f60b4d0f | 140 | #endif |