NTP client to get UTC time from the internet time servers

Dependents:   NTPClient_HelloWorld

Fork of NTPClient by ST Expansion SW Team

Committer:
mapellil
Date:
Tue Jan 03 13:28:59 2017 +0000
Revision:
7:2ac816b0f4ab
Parent:
6:510bffa8b3d9
fixed warning

Who changed what in which revision?

UserRevisionLine numberNew 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
donatien 2:9a64a50df235 27 #include <cstdint>
donatien 2:9a64a50df235 28
donatien 2:9a64a50df235 29 using std::uint8_t;
donatien 2:9a64a50df235 30 using std::uint16_t;
donatien 2:9a64a50df235 31 using std::uint32_t;
donatien 2:9a64a50df235 32
donatien 2:9a64a50df235 33 #include "UDPSocket.h"
donatien 2:9a64a50df235 34
donatien 0:04a82df0f587 35 #define NTP_DEFAULT_PORT 123
donatien 0:04a82df0f587 36 #define NTP_DEFAULT_TIMEOUT 4000
donatien 0:04a82df0f587 37
donatien 2:9a64a50df235 38 ///NTP client results
donatien 2:9a64a50df235 39 enum NTPResult
donatien 2:9a64a50df235 40 {
donatien 2:9a64a50df235 41 NTP_DNS, ///<Could not resolve name
donatien 2:9a64a50df235 42 NTP_PRTCL, ///<Protocol error
donatien 2:9a64a50df235 43 NTP_TIMEOUT, ///<Connection timeout
donatien 2:9a64a50df235 44 NTP_CONN, ///<Connection error
donatien 2:9a64a50df235 45 NTP_OK = 0, ///<Success
donatien 2:9a64a50df235 46 };
donatien 2:9a64a50df235 47
donatien 0:04a82df0f587 48 /** NTP Client to update the mbed's RTC using a remote time server
donatien 0:04a82df0f587 49 *
donatien 0:04a82df0f587 50 */
donatien 0:04a82df0f587 51 class NTPClient
donatien 0:04a82df0f587 52 {
donatien 0:04a82df0f587 53 public:
donatien 0:04a82df0f587 54 /**
donatien 0:04a82df0f587 55 Instantiate the NTP client
donatien 0:04a82df0f587 56 */
mapellil 5:fe36718063a5 57 // NTPClient();
mapellil 7:2ac816b0f4ab 58 NTPClient(NetworkStack & _m_intf);
donatien 0:04a82df0f587 59 /**Get current time (blocking)
donatien 0:04a82df0f587 60 Update the time using the server host
donatien 0:04a82df0f587 61 Blocks until completion
donatien 0:04a82df0f587 62 @param host NTP server IPv4 address or hostname (will be resolved via DNS)
donatien 0:04a82df0f587 63 @param port port to use; defaults to 123
donatien 0:04a82df0f587 64 @param timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
donatien 2:9a64a50df235 65 @return 0 on success, NTP error code (<0) on failure
donatien 0:04a82df0f587 66 */
mapellil 6:510bffa8b3d9 67 NTPResult setTime(const char* host, uint16_t port = NTP_DEFAULT_PORT, uint32_t timeout = NTP_DEFAULT_TIMEOUT);
donatien 0:04a82df0f587 68
donatien 0:04a82df0f587 69 private:
donatien 0:04a82df0f587 70 struct NTPPacket //See RFC 4330 for Simple NTP
donatien 0:04a82df0f587 71 {
donatien 0:04a82df0f587 72 //WARN: We are in LE! Network is BE!
donatien 0:04a82df0f587 73 //LSb first
donatien 0:04a82df0f587 74 unsigned mode : 3;
donatien 0:04a82df0f587 75 unsigned vn : 3;
donatien 0:04a82df0f587 76 unsigned li : 2;
donatien 0:04a82df0f587 77
donatien 0:04a82df0f587 78 uint8_t stratum;
donatien 0:04a82df0f587 79 uint8_t poll;
donatien 0:04a82df0f587 80 uint8_t precision;
donatien 0:04a82df0f587 81 //32 bits header
donatien 0:04a82df0f587 82
donatien 0:04a82df0f587 83 uint32_t rootDelay;
donatien 0:04a82df0f587 84 uint32_t rootDispersion;
donatien 0:04a82df0f587 85 uint32_t refId;
donatien 0:04a82df0f587 86
donatien 0:04a82df0f587 87 uint32_t refTm_s;
donatien 0:04a82df0f587 88 uint32_t refTm_f;
donatien 0:04a82df0f587 89 uint32_t origTm_s;
donatien 0:04a82df0f587 90 uint32_t origTm_f;
donatien 0:04a82df0f587 91 uint32_t rxTm_s;
donatien 0:04a82df0f587 92 uint32_t rxTm_f;
donatien 0:04a82df0f587 93 uint32_t txTm_s;
donatien 0:04a82df0f587 94 uint32_t txTm_f;
donatien 0:04a82df0f587 95 } __attribute__ ((packed));
donatien 2:9a64a50df235 96
mapellil 5:fe36718063a5 97 NetworkStack & m_intf; // WiFi interface
donatien 2:9a64a50df235 98 UDPSocket m_sock;
donatien 0:04a82df0f587 99 };
donatien 0:04a82df0f587 100
donatien 0:04a82df0f587 101
donatien 0:04a82df0f587 102 #endif /* NTPCLIENT_H_ */