Library for ethernet
Dependents: VC0706_FTP_Client_Ethernet_MQTT
Fork of WIZnetInterface by
Socket/NTPClient.h@16:c830d2e7e0f5, 2015-06-24 (annotated)
- Committer:
- eunkyoungkim
- Date:
- Wed Jun 24 02:27:46 2015 +0000
- Revision:
- 16:c830d2e7e0f5
Add SNTP Client
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eunkyoungkim | 16:c830d2e7e0f5 | 1 | /* NTPClient.h */ |
eunkyoungkim | 16:c830d2e7e0f5 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
eunkyoungkim | 16:c830d2e7e0f5 | 3 | * |
eunkyoungkim | 16:c830d2e7e0f5 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
eunkyoungkim | 16:c830d2e7e0f5 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
eunkyoungkim | 16:c830d2e7e0f5 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
eunkyoungkim | 16:c830d2e7e0f5 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
eunkyoungkim | 16:c830d2e7e0f5 | 8 | * furnished to do so, subject to the following conditions: |
eunkyoungkim | 16:c830d2e7e0f5 | 9 | * |
eunkyoungkim | 16:c830d2e7e0f5 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
eunkyoungkim | 16:c830d2e7e0f5 | 11 | * substantial portions of the Software. |
eunkyoungkim | 16:c830d2e7e0f5 | 12 | * |
eunkyoungkim | 16:c830d2e7e0f5 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
eunkyoungkim | 16:c830d2e7e0f5 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
eunkyoungkim | 16:c830d2e7e0f5 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
eunkyoungkim | 16:c830d2e7e0f5 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
eunkyoungkim | 16:c830d2e7e0f5 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
eunkyoungkim | 16:c830d2e7e0f5 | 18 | */ |
eunkyoungkim | 16:c830d2e7e0f5 | 19 | |
eunkyoungkim | 16:c830d2e7e0f5 | 20 | /** \file |
eunkyoungkim | 16:c830d2e7e0f5 | 21 | NTP Client header file |
eunkyoungkim | 16:c830d2e7e0f5 | 22 | */ |
eunkyoungkim | 16:c830d2e7e0f5 | 23 | |
eunkyoungkim | 16:c830d2e7e0f5 | 24 | #ifndef NTPCLIENT_H_ |
eunkyoungkim | 16:c830d2e7e0f5 | 25 | #define NTPCLIENT_H_ |
eunkyoungkim | 16:c830d2e7e0f5 | 26 | |
eunkyoungkim | 16:c830d2e7e0f5 | 27 | #include <cstdint> |
eunkyoungkim | 16:c830d2e7e0f5 | 28 | |
eunkyoungkim | 16:c830d2e7e0f5 | 29 | using std::uint8_t; |
eunkyoungkim | 16:c830d2e7e0f5 | 30 | using std::uint16_t; |
eunkyoungkim | 16:c830d2e7e0f5 | 31 | using std::uint32_t; |
eunkyoungkim | 16:c830d2e7e0f5 | 32 | |
eunkyoungkim | 16:c830d2e7e0f5 | 33 | #include "UDPSocket.h" |
eunkyoungkim | 16:c830d2e7e0f5 | 34 | |
eunkyoungkim | 16:c830d2e7e0f5 | 35 | #define NTP_DEFAULT_PORT 123 |
eunkyoungkim | 16:c830d2e7e0f5 | 36 | #define NTP_DEFAULT_TIMEOUT 4000 |
eunkyoungkim | 16:c830d2e7e0f5 | 37 | |
eunkyoungkim | 16:c830d2e7e0f5 | 38 | ///NTP client results |
eunkyoungkim | 16:c830d2e7e0f5 | 39 | enum NTPResult |
eunkyoungkim | 16:c830d2e7e0f5 | 40 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 41 | NTP_DNS, ///<Could not resolve name |
eunkyoungkim | 16:c830d2e7e0f5 | 42 | NTP_PRTCL, ///<Protocol error |
eunkyoungkim | 16:c830d2e7e0f5 | 43 | NTP_TIMEOUT, ///<Connection timeout |
eunkyoungkim | 16:c830d2e7e0f5 | 44 | NTP_CONN, ///<Connection error |
eunkyoungkim | 16:c830d2e7e0f5 | 45 | NTP_OK = 0, ///<Success |
eunkyoungkim | 16:c830d2e7e0f5 | 46 | }; |
eunkyoungkim | 16:c830d2e7e0f5 | 47 | |
eunkyoungkim | 16:c830d2e7e0f5 | 48 | /** NTP Client to update the mbed's RTC using a remote time server |
eunkyoungkim | 16:c830d2e7e0f5 | 49 | * |
eunkyoungkim | 16:c830d2e7e0f5 | 50 | */ |
eunkyoungkim | 16:c830d2e7e0f5 | 51 | class NTPClient |
eunkyoungkim | 16:c830d2e7e0f5 | 52 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 53 | public: |
eunkyoungkim | 16:c830d2e7e0f5 | 54 | /** |
eunkyoungkim | 16:c830d2e7e0f5 | 55 | Instantiate the NTP client |
eunkyoungkim | 16:c830d2e7e0f5 | 56 | */ |
eunkyoungkim | 16:c830d2e7e0f5 | 57 | NTPClient(); |
eunkyoungkim | 16:c830d2e7e0f5 | 58 | |
eunkyoungkim | 16:c830d2e7e0f5 | 59 | /**Get current time (blocking) |
eunkyoungkim | 16:c830d2e7e0f5 | 60 | Update the time using the server host |
eunkyoungkim | 16:c830d2e7e0f5 | 61 | Blocks until completion |
eunkyoungkim | 16:c830d2e7e0f5 | 62 | @param host NTP server IPv4 address or hostname (will be resolved via DNS) |
eunkyoungkim | 16:c830d2e7e0f5 | 63 | @param port port to use; defaults to 123 |
eunkyoungkim | 16:c830d2e7e0f5 | 64 | @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended) |
eunkyoungkim | 16:c830d2e7e0f5 | 65 | @return 0 on success, NTP error code (<0) on failure |
eunkyoungkim | 16:c830d2e7e0f5 | 66 | */ |
eunkyoungkim | 16:c830d2e7e0f5 | 67 | NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT); //Blocking |
eunkyoungkim | 16:c830d2e7e0f5 | 68 | |
eunkyoungkim | 16:c830d2e7e0f5 | 69 | private: |
eunkyoungkim | 16:c830d2e7e0f5 | 70 | struct NTPPacket //See RFC 4330 for Simple NTP |
eunkyoungkim | 16:c830d2e7e0f5 | 71 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 72 | //WARN: We are in LE! Network is BE! |
eunkyoungkim | 16:c830d2e7e0f5 | 73 | //LSb first |
eunkyoungkim | 16:c830d2e7e0f5 | 74 | unsigned mode : 3; |
eunkyoungkim | 16:c830d2e7e0f5 | 75 | unsigned vn : 3; |
eunkyoungkim | 16:c830d2e7e0f5 | 76 | unsigned li : 2; |
eunkyoungkim | 16:c830d2e7e0f5 | 77 | |
eunkyoungkim | 16:c830d2e7e0f5 | 78 | uint8_t stratum; |
eunkyoungkim | 16:c830d2e7e0f5 | 79 | uint8_t poll; |
eunkyoungkim | 16:c830d2e7e0f5 | 80 | uint8_t precision; |
eunkyoungkim | 16:c830d2e7e0f5 | 81 | //32 bits header |
eunkyoungkim | 16:c830d2e7e0f5 | 82 | |
eunkyoungkim | 16:c830d2e7e0f5 | 83 | uint32_t rootDelay; |
eunkyoungkim | 16:c830d2e7e0f5 | 84 | uint32_t rootDispersion; |
eunkyoungkim | 16:c830d2e7e0f5 | 85 | uint32_t refId; |
eunkyoungkim | 16:c830d2e7e0f5 | 86 | |
eunkyoungkim | 16:c830d2e7e0f5 | 87 | uint32_t refTm_s; |
eunkyoungkim | 16:c830d2e7e0f5 | 88 | uint32_t refTm_f; |
eunkyoungkim | 16:c830d2e7e0f5 | 89 | uint32_t origTm_s; |
eunkyoungkim | 16:c830d2e7e0f5 | 90 | uint32_t origTm_f; |
eunkyoungkim | 16:c830d2e7e0f5 | 91 | uint32_t rxTm_s; |
eunkyoungkim | 16:c830d2e7e0f5 | 92 | uint32_t rxTm_f; |
eunkyoungkim | 16:c830d2e7e0f5 | 93 | uint32_t txTm_s; |
eunkyoungkim | 16:c830d2e7e0f5 | 94 | uint32_t txTm_f; |
eunkyoungkim | 16:c830d2e7e0f5 | 95 | } __attribute__ ((packed)); |
eunkyoungkim | 16:c830d2e7e0f5 | 96 | |
eunkyoungkim | 16:c830d2e7e0f5 | 97 | UDPSocket m_sock; |
eunkyoungkim | 16:c830d2e7e0f5 | 98 | |
eunkyoungkim | 16:c830d2e7e0f5 | 99 | }; |
eunkyoungkim | 16:c830d2e7e0f5 | 100 | typedef struct _datetime |
eunkyoungkim | 16:c830d2e7e0f5 | 101 | { |
eunkyoungkim | 16:c830d2e7e0f5 | 102 | uint16_t yy; |
eunkyoungkim | 16:c830d2e7e0f5 | 103 | uint8_t mo; |
eunkyoungkim | 16:c830d2e7e0f5 | 104 | uint8_t dd; |
eunkyoungkim | 16:c830d2e7e0f5 | 105 | uint8_t hh; |
eunkyoungkim | 16:c830d2e7e0f5 | 106 | uint8_t mm; |
eunkyoungkim | 16:c830d2e7e0f5 | 107 | uint8_t ss; |
eunkyoungkim | 16:c830d2e7e0f5 | 108 | } datetime; |
eunkyoungkim | 16:c830d2e7e0f5 | 109 | |
eunkyoungkim | 16:c830d2e7e0f5 | 110 | |
eunkyoungkim | 16:c830d2e7e0f5 | 111 | #endif /* NTPCLIENT_H_ */ |