Fork of my MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:45:51 2017 +0000
Revision:
0:f1d3878b8dd9
Initial commit

Who changed what in which revision?

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