forked from bootc/PolyController
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimesync.c
301 lines (245 loc) · 7.05 KB
/
timesync.c
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
/*
* This file is part of the PolyController firmware source code.
* Copyright (C) 2011 Chris Boot.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include <contiki-net.h>
#include <sys/stimer.h>
#include <stdio.h>
#include <stdlib.h>
#include <init.h>
#include <alloca.h>
#include <avr/pgmspace.h>
#include "timesync.h"
#include <time.h>
#include <sntp.h>
#include <resolv_helper.h>
#if CONFIG_DRIVERS_DS1307
#include "drivers/ds1307.h"
#endif
#include "drivers/wallclock.h"
#include "apps/network.h"
#if CONFIG_APPS_SYSLOG
#include "apps/syslog.h"
#endif
const char sntp_server_name[] PROGMEM = "ntp.polycontroller.co.uk";
PROCESS(timesync_process, "TimeSync");
INIT_PROCESS(timesync_process);
timesync_status_t timesync_status;
process_event_t timesync_event;
static struct resolv_helper_status res;
static struct etimer tmr_periodic;
static struct stimer tmr_resync;
static int init(void) {
#if CONFIG_DRIVERS_DS1307
int err;
struct tm tm;
wallclock_time_t new;
char date[32];
#endif
timesync_event = process_alloc_event();
timesync_status.running = 0;
timesync_status.synchronised = 0;
#if CONFIG_DRIVERS_DS1307
// Set up DS1307 RTC
ds1307_clock_start();
ds1307_ctl_set(DS1307_OUT_SQW_32768HZ);
#endif
// Start the wallclock timer
wallclock_init();
#if CONFIG_DRIVERS_DS1307
// Get the time from the RTC
err = ds1307_clock_get(&tm);
if (err) {
return err;
}
// Set up the new time
new.sec = mktime(&tm);
new.frac = 0x7ff; // mid-way through second
// Set the system time from the RTC
wallclock_set(&new);
// Print date retrieved from RTC
strftime_P(date, sizeof(date), PSTR("%c"), &tm);
printf_P(PSTR("Date from RTC: %s\n"), date);
#endif
// Copy the host name into the resolv helper structure
strncpy_P(res.name, sntp_server_name, sizeof(res.name));
// Launch the lookup
resolv_helper_lookup(&res);
return 0;
}
static void sntp_lookup_sync(void) {
if (res.state == RESOLV_HELPER_STATE_DONE) {
sntp_sync(res.ipaddr);
timesync_status.sync_pending = 0;
return; // no poll
}
else if (res.state == RESOLV_HELPER_STATE_EXPIRED) {
// Refresh the expired lookup
resolv_helper_lookup(&res);
}
else if (res.state == RESOLV_HELPER_STATE_ERROR) {
syslog_P(LOG_DAEMON | LOG_ERR,
PSTR("SNTP host lookup error: %s"),
res.name);
return; // no poll
}
process_poll(×ync_process);
}
PROCESS_THREAD(timesync_process, ev, data) {
PROCESS_BEGIN();
// Set things up
init();
while (1) {
PROCESS_WAIT_EVENT();
// Call the resolver
resolv_helper_appcall(&res, ev, data);
if (ev == PROCESS_EVENT_POLL) {
sntp_lookup_sync();
}
else if (ev == tcpip_event) {
if (timesync_status.running) {
sntp_appcall(ev, data);
}
}
else if (ev == net_event) {
if (net_status.configured && !timesync_status.running) {
timesync_status.running = 1;
timesync_status.sync_pending = 1;
timesync_status.synchronised = 0;
etimer_set(&tmr_periodic, CLOCK_SECOND);
stimer_set(&tmr_resync, SNTP_RESYNC_INTERVAL);
process_post(PROCESS_BROADCAST, timesync_event,
×ync_status);
syslog_P(LOG_DAEMON | LOG_INFO, PSTR("Starting"));
process_poll(×ync_process);
}
else if (!net_status.configured && timesync_status.running) {
timesync_status.running = 0;
timesync_status.sync_pending = 0;
timesync_status.synchronised = 0;
etimer_stop(&tmr_periodic);
process_post(PROCESS_BROADCAST, timesync_event,
×ync_status);
syslog_P(LOG_DAEMON | LOG_INFO, PSTR("Stopped"));
}
}
else if (ev == PROCESS_EVENT_TIMER) {
if (data == &tmr_periodic && etimer_expired(&tmr_periodic)) {
etimer_reset(&tmr_periodic);
if (timesync_status.running && stimer_expired(&tmr_resync)) {
stimer_reset(&tmr_resync);
timesync_status.sync_pending = 1;
process_poll(×ync_process);
}
}
else if (timesync_status.running) {
sntp_appcall(ev, data);
}
}
else if (ev == PROCESS_EVENT_EXIT) {
timesync_status.running = 0;
timesync_status.sync_pending = 0;
timesync_status.synchronised = 0;
#if CONFIG_DRIVERS_DS1307
// Disable the DS1307 clock output to save power
ds1307_ctl_set(DS1307_OUT_LOW);
#endif
process_exit(×ync_process);
LOADER_UNLOAD();
}
}
PROCESS_END();
}
void timesync_schedule_resync(void) {
// Hack the timer
if (timesync_status.running) {
tmr_resync.start = clock_seconds() - tmr_resync.interval;
}
}
int timesync_set_time(const wallclock_time_t *time) {
#if CONFIG_DRIVERS_DS1307
int err;
struct tm tm;
uint32_t cur;
#endif
wallclock_time_t oldtime;
int32_t diffms;
// Get the current wallclock time
wallclock_get(&oldtime);
// Update the wallclock
wallclock_set(time);
// Work out the time difference
diffms = ((int32_t)time->sec - (int32_t)oldtime.sec) * 1000;
diffms += (((int32_t)time->frac - (int32_t)oldtime.frac) * 1000) >> 12;
// Tell folks about the change
process_post(PROCESS_BROADCAST, timesync_event, ×ync_status);
syslog_P(LOG_DAEMON | LOG_INFO, PSTR("Clock adjusted by %ldms"),
diffms);
#if CONFIG_DRIVERS_DS1307
// Get the current RTC time
err = ds1307_clock_get(&tm);
if (err) {
return err;
}
cur = mktime(&tm);
// If the RTC is 3 or more seconds out, change the RTC
if (abs(time->sec - cur) >= 3) {
gmtime(time->sec, &tm);
err = ds1307_clock_set(&tm);
if (err) {
return err;
}
// Make sure the clock is running
ds1307_clock_start();
ds1307_ctl_set(DS1307_OUT_SQW_32768HZ);
syslog_P(LOG_DAEMON | LOG_INFO,
PSTR("RTC adjusted by %ds"), time->sec - cur);
}
#endif
return 0;
}
void sntp_synced(const struct sntp_hdr *message) {
if (!message) {
timesync_status.synchronised = 0;
process_post(PROCESS_BROADCAST, timesync_event, ×ync_status);
syslog_P(LOG_DAEMON | LOG_WARNING, PSTR("SNTP timed out"));
return;
}
// Sanity check message
if (message->VN == 0 ||
message->Stratum == 0 ||
uip_ntohl(message->TxTimestamp[0]) == 0)
{
timesync_status.synchronised = 0;
process_post(PROCESS_BROADCAST, timesync_event, ×ync_status);
syslog_P(LOG_DAEMON | LOG_WARNING, PSTR("Invalid SNTP message"));
return;
}
// Set the new wallclock time
wallclock_time_t new = {
.sec = NTP_TO_UNIX(uip_ntohl(message->TxTimestamp[0])),
.frac = uip_ntohl(message->TxTimestamp[1]) >> 20, // 32 to 12 bit fixed
};
// Set our status flags
timesync_status.synchronised = 1;
// Update the clock
timesync_set_time(&new);
}
uint32_t sntp_seconds(void) {
return UNIX_TO_NTP(wallclock_seconds());
}