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