fork from simple sample of mqtt

Dependencies:   azure_c_shared_utility azure_umqtt_c iothub_client iothub_mqtt_transport serializer wolfSSL

Fork of simplesample_mqtt by Azure IoT

Committer:
lrh
Date:
Tue Feb 07 07:56:36 2017 +0000
Revision:
32:27328e208674
test1

Who changed what in which revision?

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