-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_clock.c
469 lines (397 loc) · 14.6 KB
/
app_clock.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* app_clock.c
*
* Implementation of LoRaWAN Application Layer Clock Synchronization v1.0.0 Specification
* https://lora-alliance.org/resource-hub/lorawanr-application-layer-clock-synchronization-specification-v100
*
* LoRaWAN® Application Layer Clock Synchronization Specification, authored by the FUOTA Working Group of the
* LoRa Alliance® Technical Committee, proposes an application layer messaging package running over LoRaWAN®
* to synchronize the real-time clock of an end-device to the network’s GPS clock with second accuracy.
*
* This package is useful for end-devices which do not have access to other accurate time source.
* An end-device using LoRaWAN 1.1 or above SHOULD use DeviceTimeReq MAC command instead of this package.
* ClassB end-devices have a more efficient way of synchronizing their clock, the classB network beacon. They
* SHOULD NOT use this package and directly use the beacon time information.
* End-devices with an accurate external clock source (e.g.: GPS) SHOULD use that clock source instead.
*
* Remark: Since GPS clock sources can be jammed or spoofed, this package can be used for secure time distribution.
* https://wiki.eclipse.org/images/3/3a/Eclipse-IoTDay2020Grenoble-friedt.pdf
*/
/**
* @ingroup pkg_lorawan_app_clock
* @{
*
* @file
* @brief Implementation of Implementation of LoRaWAN Application Layer Clock Synchronization v1.0.0 Specification.
*
* @author Didier Donsez <[email protected]>
*
* @}
*/
#define ENABLE_DEBUG (1)
#include "debug.h"
#include "app_clock.h"
#include "xtimer.h"
#include <time.h>
#include "net/loramac.h"
#include "semtech_loramac.h"
#include "loramac_utils.h"
#include "periph_conf.h"
#if MODULE_PERIPH_RTC == 1
#include "periph/rtc.h"
#endif
#define DEFAULT_TM {0,0,0,1,0,121,0,0,0}
// 1972 and 1976 have 366 days (DELTA_EPOCH_GPS is 315964800 seconds)
// GPS Epoch consists of a count of weeks and seconds of the week since 0 hours (midnight) Sunday 6 January 1980
#define DELTA_EPOCH_GPS ((365*8 + 366*2 + 5)*(24*60*60))
// The end-device responds by sending up to NbTransmissions AppTimeReq messages
// with the AnsRequired bit set to 0.
// The end-device stops re-transmissions of the AppTimeReq if a valid AppTimeAns is received.
// If the NbTransmissions field is 0, the command SHALL be silently discarded.
// The delay between consecutive transmissions of the AppTimeReq is application specific.
// TODO static unsigned int NbTransmissions = 0;
// TokenReq is a 4 bits counter initially set to 0. TokenReq is incremented (modulo 16) each time the end-device receives and processes successfully an AppTimeAns message.
static unsigned int TokenReq = 0;
// If the AnsRequired bit is set to 1 the end-device expects an answer whether its clock is well
// synchronized or not. If this bit is set to 0, this signals to the AS that it only needs to answer if
// the end-device clock is de-synchronized.
// TODO static unsigned int AnsRequired = 1;
// Period encodes the periodicity of the AppTimeReq transmissions. The actual periodicity in
// seconds is 128.2𝑃𝑒𝑟𝑖𝑜𝑑 ±𝑟𝑎𝑛𝑑(30) where 𝑟𝑎𝑛𝑑(30) is a random integer in the +/-30sec
// range varying with each transmission.
static bool isPeriodDefined = false;
static unsigned int Period = 0;
#define sent_buffer_SIZE ((1 + sizeof(APP_CLOCK_PackageVersionAns_t)) + (1 + sizeof(APP_CLOCK_DeviceAppTimePeriodicityAns_t)) + (1 + sizeof(APP_CLOCK_AppTimeReq_t)))
static uint8_t sent_buffer[sent_buffer_SIZE];
static uint32_t sent_buffer_cursor = 0;
static uint32_t sent_buffer_device_time_pos = 0;
static time_t lastTimeCorrection = 0; // 01/01/1970
/*
* print a tm struct
*/
#define TM_YEAR_OFFSET (1900)
/**
* Print the time
*
* @param label the label prefixing the time
* @param time the time
*/
static void print_time(const char *label, const struct tm *time) {
DEBUG("%s %04d-%02d-%02d %02d:%02d:%02d\n", label,
time->tm_year + TM_YEAR_OFFSET, time->tm_mon + 1, time->tm_mday,
time->tm_hour, time->tm_min, time->tm_sec);
}
/**
* Print the RTC time
*/
void app_clock_print_rtc(void) {
/* read RTC */
struct tm current_time = DEFAULT_TM;
#if MODULE_PERIPH_RTC == 1
rtc_get_time(¤t_time);
#endif
print_time("[clock] Current RTC time : ", ¤t_time);
struct tm lastTimeCorrectionTime = *localtime(&lastTimeCorrection);
if (lastTimeCorrection == 0) {
DEBUG("[clock] Last correction : never\n");
} else {
print_time("[clock] Last correction : ", &lastTimeCorrectionTime);
}
}
/**
* Get the RTC time in seconds since 1/1/1980 (GPS time)
*/
static unsigned int getTimeSinceEpoch(void) {
struct tm current_time = DEFAULT_TM;
// Read the RTC current time
#if MODULE_PERIPH_RTC == 1
rtc_get_time(¤t_time);
#endif
print_time("[clock] Current time: ", ¤t_time);
time_t timeSinceEpoch = mktime(¤t_time);
// substract number of seconds between 6/1/1980 and 1/1/1970
timeSinceEpoch -= DELTA_EPOCH_GPS;
return timeSinceEpoch;
}
/**
* Correct the RTC time
*
* @param timeCorrection the correction to apply to the RTC
*/
static void correct_rtc(int timeCorrection) {
struct tm current_time = DEFAULT_TM;
// Read the RTC current time
#if MODULE_PERIPH_RTC == 1
rtc_get_time(¤t_time);
#endif
print_time("[clock] Current time : ", ¤t_time);
time_t timeSinceEpoch = mktime(¤t_time);
// Apply correction
timeSinceEpoch += timeCorrection;
DEBUG("[clock] Time Correction : %d\n", timeCorrection);
current_time = *localtime(&timeSinceEpoch);
#if MODULE_PERIPH_RTC == 1
rtc_set_time(¤t_time);
#endif
lastTimeCorrection = mktime(¤t_time);
print_time("[clock] RTC time fixed : ", ¤t_time);
}
/**
* Set the RTC time
*
* @param timeToSet the time in seconds since 6/1/1980 (GPS start time)
*/
static void set_rtc(unsigned int timeToSet) {
struct tm current_time = DEFAULT_TM;
// Read the RTC current time
#if MODULE_PERIPH_RTC == 1
rtc_get_time(¤t_time);
#endif
print_time("[clock] Current time : ", ¤t_time);
time_t _TimeToSet = timeToSet + DELTA_EPOCH_GPS;
current_time = *localtime(&_TimeToSet);
#if MODULE_PERIPH_RTC == 1
rtc_set_time(¤t_time);
#endif
lastTimeCorrection = mktime(¤t_time);
print_time("[clock] RTC time fixed : ", ¤t_time);
}
int8_t app_clock_process_downlink(semtech_loramac_t *loramac) {
DEBUG("[clock] app_clock_process_downlink\n");
uint32_t len = loramac->rx_data.payload_len;
uint32_t idx = 0;
uint8_t *payload = (uint8_t*) loramac->rx_data.payload;
int8_t error = APP_CLOCK_OK;
sent_buffer_cursor = 0;
bool contains_APP_CLOCK_CID_PackageVersionReq = false;
bool contains_APP_CLOCK_CID_DeviceAppTimePeriodicityReq = false;
bool contains_APP_CLOCK_CID_AppTimeAns = false;
bool contains_APP_CLOCK_CID_ForceDeviceResyncReq = false;
#ifdef EXPERIMENTAL
bool contains_X_APP_CLOCK_CID_AppTimeSetReq = false;
#endif
while (idx < len && (error == APP_CLOCK_OK )) {
uint8_t cid = payload[idx];
switch (cid) {
case APP_CLOCK_CID_PackageVersionReq :
DEBUG("[clock] APP_CLOCK_CID_PackageVersionReq\n")
;
if (contains_APP_CLOCK_CID_PackageVersionReq) {
error = APP_CLOCK_CID_ALREADY_PROCESS;
DEBUG("[clock] APP_CLOCK_CID_PackageVersionReq, error=%d\n", error);
break;
}
contains_APP_CLOCK_CID_PackageVersionReq = true;
if (idx + 1 + 0 <= len) {
sent_buffer[sent_buffer_cursor] =
APP_CLOCK_CID_PackageVersionAns;
APP_CLOCK_PackageVersionAns_t *pva =
(APP_CLOCK_PackageVersionAns_t*) (sent_buffer
+ (1 + sent_buffer_cursor));
pva->PackageIdentifier = 1;
pva->PackageVersion = 1;
sent_buffer_cursor +=
(1 + sizeof(APP_CLOCK_PackageVersionAns_t));
idx += 1;
} else {
error = APP_CLOCK_ERROR_OVERFLOW;
DEBUG("[clock] APP_CLOCK_CID_PackageVersionReq, error=%d\n", error);
}
break;
case APP_CLOCK_CID_DeviceAppTimePeriodicityReq :
DEBUG("[clock] APP_CLOCK_CID_DeviceAppTimePeriodicityReq\n")
;
if (contains_APP_CLOCK_CID_DeviceAppTimePeriodicityReq) {
error = APP_CLOCK_CID_ALREADY_PROCESS;
DEBUG("[clock] APP_CLOCK_CID_DeviceAppTimePeriodicityReq, error=%d\n",
error);
break;
}
contains_APP_CLOCK_CID_DeviceAppTimePeriodicityReq = true;
if (idx + 1 + sizeof(APP_CLOCK_DeviceAppTimePeriodicityReq_t)
<= len) {
APP_CLOCK_DeviceAppTimePeriodicityReq_t *datpr =
(APP_CLOCK_DeviceAppTimePeriodicityReq_t*) (payload
+ (idx + 1));
isPeriodDefined = true;
Period = datpr->Period;
sent_buffer[sent_buffer_cursor] =
APP_CLOCK_CID_DeviceAppTimePeriodicityAns;
APP_CLOCK_DeviceAppTimePeriodicityAns_t *datpa =
(APP_CLOCK_DeviceAppTimePeriodicityAns_t*) (sent_buffer
+ (1 + sent_buffer_cursor));
sent_buffer_device_time_pos = 1 + sent_buffer_cursor;
datpa->NotSupported = 0; // The endpoint is not supporting periodicity currently
datpa->Time = getTimeSinceEpoch();
sent_buffer_cursor += (1
+ sizeof(APP_CLOCK_DeviceAppTimePeriodicityAns_t));
idx += (1 + sizeof(APP_CLOCK_DeviceAppTimePeriodicityReq_t));
} else {
error = APP_CLOCK_ERROR_OVERFLOW;
DEBUG("[clock] APP_CLOCK_CID_DeviceAppTimePeriodicityReq, error=%d\n",
error);
}
break;
case APP_CLOCK_CID_AppTimeAns :
DEBUG("[clock] APP_CLOCK_CID_AppTimeAns\n")
;
if (contains_APP_CLOCK_CID_AppTimeAns) {
error = APP_CLOCK_CID_ALREADY_PROCESS;
DEBUG("[clock] APP_CLOCK_CID_AppTimeAns, error=%d\n", error);
break;
}
contains_APP_CLOCK_CID_AppTimeAns = true;
if (idx + 1 + sizeof(APP_CLOCK_AppTimeAns_t) <= len) {
APP_CLOCK_AppTimeAns_t *ata = (APP_CLOCK_AppTimeAns_t*) (payload
+ (idx + 1));
unsigned int TokenAns = ata->TokenAns;
if (TokenAns != TokenReq) {
error = APP_CLOCK_BAD_TOKEN;
DEBUG("[clock] APP_CLOCK_CID_AppTimeAns, error=%d\n", error);
break;
}
correct_rtc(ata->TimeCorrection);
// increment TokenReq
TokenReq++;
TokenReq %= 16;
idx += (1 + sizeof(APP_CLOCK_AppTimeAns_t));
} else {
error = APP_CLOCK_ERROR_OVERFLOW;
DEBUG("[clock] APP_CLOCK_CID_AppTimeAns, error=%d\n", error);
}
break;
case APP_CLOCK_CID_ForceDeviceResyncReq :
DEBUG("[clock] APP_CLOCK_CID_ForceDeviceResyncReq\n")
;
if (contains_APP_CLOCK_CID_ForceDeviceResyncReq) {
error = APP_CLOCK_CID_ALREADY_PROCESS;
DEBUG("[clock] APP_CLOCK_CID_ForceDeviceResyncReq, error=%d\n", error);
break;
}
contains_APP_CLOCK_CID_ForceDeviceResyncReq = true;
if (idx + 1 + sizeof(APP_CLOCK_ForceDeviceResyncReq_t) <= len) {
APP_CLOCK_ForceDeviceResyncReq_t *fdrr =
(APP_CLOCK_ForceDeviceResyncReq_t*) (payload + (idx + 1));
unsigned int NbTransmissions = fdrr->NbTransmissions;
(void) NbTransmissions;
// TODO
idx += (1 + sizeof(APP_CLOCK_ForceDeviceResyncReq_t));
error = APP_CLOCK_NOT_IMPLEMENTED;
DEBUG("[clock] APP_CLOCK_CID_ForceDeviceResyncReq, error=%d\n", error);
} else {
error = APP_CLOCK_ERROR_OVERFLOW;
DEBUG("[clock] APP_CLOCK_CID_ForceDeviceResyncReq, error=%d\n", error);
}
break;
#ifdef EXPERIMENTAL
case X_APP_CLOCK_CID_AppTimeSetReq :
DEBUG("[clock] X_APP_CLOCK_CID_AppTimeSetReq\n")
;
if (contains_X_APP_CLOCK_CID_AppTimeSetReq) {
error = APP_CLOCK_CID_ALREADY_PROCESS;
DEBUG("[clock] X_APP_CLOCK_CID_AppTimeSetReq, error=%d\n", error);
break;
}
contains_X_APP_CLOCK_CID_AppTimeSetReq = true;
if (idx + 1 + sizeof(X_APP_CLOCK_AppTimeSetReq_t) <= len) {
X_APP_CLOCK_AppTimeSetReq_t *atsr =
(X_APP_CLOCK_AppTimeSetReq_t*) (payload + (idx + 1));
set_rtc(atsr->TimeToSet);
idx += (1 + sizeof(X_APP_CLOCK_AppTimeSetReq_t));
} else {
error = APP_CLOCK_ERROR_OVERFLOW;
DEBUG("[clock] X_APP_CLOCK_CID_AppTimeSetReq, error=%d\n", error);
}
break;
#endif
default:
error = APP_CLOCK_UNKNOWN_CID;
DEBUG("[clock] APP_CLOCK : Unknown CID, error=%d\n", error)
;
break;
}
}
DEBUG("[clock] sent_buffer:");
printf_ba(sent_buffer, sent_buffer_cursor);
DEBUG("\n");
if (error == APP_CLOCK_OK) {
error = app_clock_send_buffer(loramac);
} else {
sent_buffer_cursor = 0;
}
// TODO if NbTransmissions > 0, send an APP_CLOCK_CID_AppTimeReq
return error;
}
int8_t app_clock_send_app_time_req(semtech_loramac_t *loramac) {
DEBUG("[clock] app_clock_send_app_time_req\n");
uint8_t payload[1 + sizeof(APP_CLOCK_AppTimeReq_t)];
payload[0] = APP_CLOCK_CID_AppTimeReq;
APP_CLOCK_AppTimeReq_t *atr = (APP_CLOCK_AppTimeReq_t*) (payload + 1);
atr->TokenReq = TokenReq;
atr->AnsRequired = 1;
atr->DeviceTime = getTimeSinceEpoch();
// save the current fPort and set the APP_CLOCK_PORT
uint8_t current_fPort = semtech_loramac_get_tx_port(loramac);
semtech_loramac_set_tx_port(loramac, APP_CLOCK_PORT);
/* send the LoRaWAN message */
// TODO thread can be stuck into the semtech_loramac_send
uint8_t ret = semtech_loramac_send(loramac, payload,
1 + sizeof(APP_CLOCK_AppTimeReq_t));
int8_t error;
if (ret != SEMTECH_LORAMAC_TX_DONE) {
DEBUG("[clock] Cannot send buffer : ret code: %d (%s)\n", ret,
loramac_utils_err_message(ret));
if (ret == SEMTECH_LORAMAC_TX_SCHEDULE
|| ret == SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED) {
error = APP_CLOCK_TX_RETRY_LATER;
} else {
error = APP_CLOCK_TX_KO;
// reset the buffer
sent_buffer_cursor = 0;
}
} else {
error = APP_CLOCK_OK;
}
// restore the current fPort
semtech_loramac_set_tx_port(loramac, current_fPort);
return error;
}
int8_t app_clock_send_buffer(semtech_loramac_t *loramac) {
DEBUG("[clock] app_clock_send_buffer\n");
int8_t error = APP_CLOCK_OK;
if (sent_buffer_cursor != 0) {
// save the current fPort and set the APP_CLOCK_PORT
uint8_t current_fPort = semtech_loramac_get_tx_port(loramac);
semtech_loramac_set_tx_port(loramac, APP_CLOCK_PORT);
if (sent_buffer_device_time_pos != 0) {
APP_CLOCK_DeviceAppTimePeriodicityAns_t *datpa =
(APP_CLOCK_DeviceAppTimePeriodicityAns_t*) (sent_buffer
+ (1 + sent_buffer_device_time_pos));
datpa->Time = getTimeSinceEpoch();
}
/* send the LoRaWAN message */
uint8_t ret = semtech_loramac_send(loramac, sent_buffer,
sent_buffer_cursor);
if (ret != SEMTECH_LORAMAC_TX_DONE) {
DEBUG("[clock] Cannot send buffer : ret code: %d (%s)\n", ret,
loramac_utils_err_message(ret));
if (ret == SEMTECH_LORAMAC_TX_SCHEDULE
|| ret == SEMTECH_LORAMAC_DUTYCYCLE_RESTRICTED) {
error = APP_CLOCK_TX_RETRY_LATER;
} else {
error = APP_CLOCK_TX_KO;
// reset the buffer
sent_buffer_cursor = 0;
}
} else {
// reset the buffer
sent_buffer_cursor = 0;
}
// restore the current fPort
semtech_loramac_set_tx_port(loramac, current_fPort);
}
return error;
}
bool app_clock_is_pending_buffer(void) {
return sent_buffer_cursor != 0;
}