NTP

Dependents:   DISCO-F746NG_Ethernet gather_sensor_data final DISCO-F746NG_Ethernet

Committer:
DieterGraef
Date:
Sun Jun 19 16:24:06 2016 +0000
Revision:
0:584a18640e84
NTPClient

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