-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathespntp.cpp
300 lines (243 loc) · 6.74 KB
/
espntp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
//////////////////////////////////////////////////////////////////////
// espntp.cpp -- ESP8266 NTP Code (under POSIX as a test)
// Date: Wed Nov 18 22:08:19 2015 (C) Warren W. Gay VE3WWG
///////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <time.h>
#include <poll.h>
#include <fcntl.h>
#include <termios.h>
#include <assert.h>
#include "esp8266.hpp"
static void writeb(char b);
static char readb();
static bool rpoll();
static void idle();
static ESP8266 esp(writeb,readb,rpoll,idle);
static int fd = -1;
static bool opt_verbose = false;
static int opt_baudrate = 115200;
static const char *opt_device = "/dev/cu.usbserial-A50285BI";
//////////////////////////////////////////////////////////////////////
// Write byte callback
//////////////////////////////////////////////////////////////////////
static void
writeb(char b) {
int rc;
do {
rc = write(fd,&b,1);
} while ( rc == -1 && errno == EINTR );
assert(rc==1);
}
//////////////////////////////////////////////////////////////////////
// Read byte callback
//////////////////////////////////////////////////////////////////////
static char
readb() {
char b;
int rc;
do {
rc = read(fd,&b,1);
} while ( rc == -1 && errno == EINTR );
assert(rc==1);
return b;
}
//////////////////////////////////////////////////////////////////////
// Poll for a readable byte
//////////////////////////////////////////////////////////////////////
static bool
rpoll() {
struct pollfd p;
int rc;
p.fd = fd;
p.events = POLLIN;
p.revents = 0;
do {
rc = poll(&p,1,0);
} while ( rc == -1 && errno == EINTR );
return rc == 1;
}
//////////////////////////////////////////////////////////////////////
// Idle processing
//////////////////////////////////////////////////////////////////////
static void
idle() {
usleep(100);
}
//////////////////////////////////////////////////////////////////////
// UDP Receiving
//////////////////////////////////////////////////////////////////////
static uint32_t rxbuf[12]; // NTP receiving buffer
static char *rxp = 0; // rx_cb byte pointer within rxbuf
static unsigned rx = 0; // Index for rxp[rx]
static bool rx_done = false; // End of datagram seen
static void
rx_cb(int s,int ch) {
if ( ch == -1 ) {
rx_done = true;
} else {
if ( rx < sizeof rxbuf )
rxp[rx++] = ch;
}
}
//////////////////////////////////////////////////////////////////////
// Query NTP time server
//////////////////////////////////////////////////////////////////////
static time_t
ntp_time(const char *hostname) {
static const uint64_t ntp_offset = ((uint64_t(365)*70)+17)*24*60*60;
static const unsigned char reqmsg[48] = {010,0,0,0,0,0,0,0,0};
static const short port = 123; // NTP
uint32_t ntp_time = 0;
int s, rc;
// Get a socket
s = esp.udp_socket(hostname,port,rx_cb);
if ( s < 0 )
return 0; // No socket
// Initialize RX
rxp = (char *)rxbuf;
rx = 0;
rx_done = false;
// Write request datagram
rc = esp.write(s,(const char *)reqmsg,sizeof reqmsg);
assert(rc == sizeof reqmsg);
// Wait for the response
{
time_t t0 = time(0); // This is valid only for POSIX systems
while ( !rx_done && time(0) - t0 < 5 )
esp.receive();
esp.close(s);
if ( !rx_done )
return 0; // No response
}
ntp_time = ntohl(rxbuf[10]);
// Convert to Unix epoch time:
time_t uxtime = uint64_t(ntp_time) - ntp_offset;
// Simple UTC time calculation:
unsigned hour = uxtime % 86400ul / 3600ul;
unsigned min = uxtime % 3600ul / 60ul;
unsigned secs = uxtime % 60;
printf("%02d:%02d:%02d UTC from %s\n",hour,min,secs,hostname);
// Compute system time difference
time_t td;
td = time(0);
long sdiff = int64_t(td) - int64_t(uxtime);
printf("%+ld seconds off: %s\n",long(sdiff),ctime(&uxtime));
return uxtime;
}
static void
usage(const char *cmd) {
const char *cp = strrchr(cmd,'/');
if ( cp )
cmd = cp + 1;
fprintf(stderr,
"Usage: %s [-b baudrate] [-d /dev/usbserial] [-v] [-h] [ntpserver1...]\n"
"where options include:\n"
"\t-b baudrate\tSerial baud rate (115200)\n"
"\t-d device\tSerial device pathname\n"
"\t-v\t\tVerbose output mode\n"
"\t-h\t\tThis help info.\n",
cmd);
exit(0);
}
//////////////////////////////////////////////////////////////////////
// Provide the name(s) of time servers on the command line
//////////////////////////////////////////////////////////////////////
int
main(int argc,char **argv) {
static const char options[] = ":b:d:vh";
termios ios, svios;
int rc, optch, er = 0;
//////////////////////////////////////////////////////////////
// Parse command line options
//////////////////////////////////////////////////////////////
while ( (optch = getopt(argc,argv,options)) != -1 ) {
switch ( optch ) {
case 'b':
opt_baudrate = atoi(optarg);
break;
case 'd':
opt_device = optarg;
break;
case 'v':
opt_verbose = true;
break;
case 'h':
usage(argv[0]);
break;
case ':':
fprintf(stderr,"Missing argument for -%c\n",optopt);
++er;
break;
default:
fprintf(stderr,"Invalid option -%c\n",optopt);
++er;
}
}
if ( er > 0 ) {
fprintf(stderr,"Use option -h for more information.\n");
exit(1); // Command line option error(s)
}
if ( opt_baudrate < 300 || opt_baudrate > 115200 ) {
fprintf(stderr,"Invalid baud rate -b %d\n",opt_baudrate);
exit(2);
}
//////////////////////////////////////////////////////////////
// Open serial device
//////////////////////////////////////////////////////////////
fd = open(opt_device,O_RDWR);
if ( fd == -1 ) {
fprintf(stderr,"%s: Opening serial device %s for r/w\n",
strerror(errno),
opt_device);
exit(3);
}
//////////////////////////////////////////////////////////////
// Setup device for raw I/O
//////////////////////////////////////////////////////////////
rc = tcgetattr(fd,&ios);
svios = ios;
assert(!rc);
cfmakeraw(&ios);
cfsetspeed(&ios,opt_baudrate);
ios.c_cflag |= CRTSCTS; // Hardware flow control on
rc = tcsetattr(fd,TCSADRAIN,&ios);
if ( rc == -1 ) {
fprintf(stderr,"%s: setting raw device %s to baud_rate %d\n",
strerror(errno),
opt_device,
opt_baudrate);
exit(2);
}
//////////////////////////////////////////////////////////////
// Start execution
//////////////////////////////////////////////////////////////
if ( !esp.start() ) {
fprintf(stderr,"Unable to start ESP8266\n");
exit(3);
}
esp.receive();
if ( optind < argc ) {
for ( ; optind < argc; ++optind ) {
while ( !ntp_time(argv[optind]) ) {
sleep(2);
printf("Retrying %s\n",argv[optind]);
}
}
} else {
const char *srv = "time.nrc.ca";
while ( !ntp_time(srv) ) {
sleep(2);
printf("Retrying %s\n",srv);
}
}
rc = tcsetattr(fd,TCSADRAIN,&svios);
close(fd);
return 0;
}
// End espntp.cpp