This is a fork of the NTPClient Library to work with the mbed-os 5 way of doing networking

Fork of NTPClient by Dieter Graef

Committer:
jhd25
Date:
Sun Feb 25 11:13:37 2018 +0000
Revision:
2:eda6db3ada54
Parent:
1:52e0f030d00e
Changed to stdint.h from cstdint

Who changed what in which revision?

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