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