]> git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/tests/net/protocols/NTPClient_HelloWorld/NTPClient/NTPClient.cpp
remove experimental return, cleanup slash_question key
[tmk_keyboard.git] / tmk_core / tool / mbed / mbed-sdk / libraries / tests / net / protocols / NTPClient_HelloWorld / NTPClient / NTPClient.cpp
1 /* NTPClient.cpp */
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 //Debug is disabled by default
21 #if 0
22 //Enable debug
23 #define __DEBUG__
24 #include <cstdio>
25 #define DBG(x, ...) std::printf("[NTPClient : DBG]"x"\r\n", ##__VA_ARGS__);
26 #define WARN(x, ...) std::printf("[NTPClient : WARN]"x"\r\n", ##__VA_ARGS__);
27 #define ERR(x, ...) std::printf("[NTPClient : ERR]"x"\r\n", ##__VA_ARGS__);
28
29 #else
30 //Disable debug
31 #define DBG(x, ...)
32 #define WARN(x, ...)
33 #define ERR(x, ...)
34
35 #endif
36
37 #include "NTPClient.h"
38
39 #include "UDPSocket.h"
40
41 #include "mbed.h" //time() and set_time()
42
43 #define NTP_PORT 123
44 #define NTP_CLIENT_PORT 0 //Random port
45 #define NTP_TIMESTAMP_DELTA 2208988800ull //Diff btw a UNIX timestamp (Starting Jan, 1st 1970) and a NTP timestamp (Starting Jan, 1st 1900)
46
47 NTPClient::NTPClient() : m_sock()
48 {
49
50
51 }
52
53 NTPResult NTPClient::setTime(const char* host, uint16_t port, uint32_t timeout)
54 {
55 #ifdef __DEBUG__
56 time_t ctTime;
57 ctTime = time(NULL);
58 DBG("Time is set to (UTC): %s", ctime(&ctTime));
59 #endif
60
61 //Create & bind socket
62 DBG("Binding socket");
63 m_sock.bind(0); //Bind to a random port
64
65 m_sock.set_blocking(false, timeout); //Set not blocking
66
67 struct NTPPacket pkt;
68
69 //Now ping the server and wait for response
70 DBG("Ping");
71 //Prepare NTP Packet:
72 pkt.li = 0; //Leap Indicator : No warning
73 pkt.vn = 4; //Version Number : 4
74 pkt.mode = 3; //Client mode
75 pkt.stratum = 0; //Not relevant here
76 pkt.poll = 0; //Not significant as well
77 pkt.precision = 0; //Neither this one is
78
79 pkt.rootDelay = 0; //Or this one
80 pkt.rootDispersion = 0; //Or that one
81 pkt.refId = 0; //...
82
83 pkt.refTm_s = 0;
84 pkt.origTm_s = 0;
85 pkt.rxTm_s = 0;
86 pkt.txTm_s = htonl( NTP_TIMESTAMP_DELTA + time(NULL) ); //WARN: We are in LE format, network byte order is BE
87
88 pkt.refTm_f = pkt.origTm_f = pkt.rxTm_f = pkt.txTm_f = 0;
89
90 Endpoint outEndpoint;
91
92 if( outEndpoint.set_address(host, port) < 0)
93 {
94 m_sock.close();
95 return NTP_DNS;
96 }
97
98 //Set timeout, non-blocking and wait using select
99 int ret = m_sock.sendTo( outEndpoint, (char*)&pkt, sizeof(NTPPacket) );
100 if (ret < 0 )
101 {
102 ERR("Could not send packet");
103 m_sock.close();
104 return NTP_CONN;
105 }
106
107 //Read response
108 Endpoint inEndpoint;
109
110 DBG("Pong");
111 do
112 {
113 ret = m_sock.receiveFrom( inEndpoint, (char*)&pkt, sizeof(NTPPacket) ); //FIXME need a DNS Resolver to actually compare the incoming address with the DNS name
114 if(ret < 0)
115 {
116 ERR("Could not receive packet");
117 m_sock.close();
118 return NTP_CONN;
119 }
120 } while( strcmp(outEndpoint.get_address(), inEndpoint.get_address()) != 0 );
121
122 if(ret < sizeof(NTPPacket)) //TODO: Accept chunks
123 {
124 ERR("Receive packet size does not match");
125 m_sock.close();
126 return NTP_PRTCL;
127 }
128
129 if( pkt.stratum == 0) //Kiss of death message : Not good !
130 {
131 ERR("Kissed to death!");
132 m_sock.close();
133 return NTP_PRTCL;
134 }
135
136 //Correct Endianness
137 pkt.refTm_s = ntohl( pkt.refTm_s );
138 pkt.refTm_f = ntohl( pkt.refTm_f );
139 pkt.origTm_s = ntohl( pkt.origTm_s );
140 pkt.origTm_f = ntohl( pkt.origTm_f );
141 pkt.rxTm_s = ntohl( pkt.rxTm_s );
142 pkt.rxTm_f = ntohl( pkt.rxTm_f );
143 pkt.txTm_s = ntohl( pkt.txTm_s );
144 pkt.txTm_f = ntohl( pkt.txTm_f );
145
146 //Compute offset, see RFC 4330 p.13
147 uint32_t destTm_s = (NTP_TIMESTAMP_DELTA + time(NULL));
148 int64_t offset = ( (int64_t)( pkt.rxTm_s - pkt.origTm_s ) + (int64_t) ( pkt.txTm_s - destTm_s ) ) / 2; //Avoid overflow
149 DBG("Sent @%ul", pkt.txTm_s);
150 DBG("Offset: %lld", offset);
151 //Set time accordingly
152 set_time( time(NULL) + offset );
153
154 #ifdef __DEBUG__
155 ctTime = time(NULL);
156 DBG("Time is now (UTC): %s", ctime(&ctTime));
157 #endif
158
159 m_sock.close();
160
161 return NTP_OK;
162 }
163
Imprint / Impressum