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