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