2019-03-09 00:53:11 +01:00
|
|
|
/*
|
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
///--> IMPORTANT LICENSE NOTE for timesync option 1 in this file <--///
|
2019-03-09 00:53:11 +01:00
|
|
|
|
|
|
|
PLEASE NOTE: There is a patent filed for the time sync algorithm used in the
|
2019-03-09 00:54:34 +01:00
|
|
|
code of this file. The shown implementation example is covered by the
|
2019-03-09 00:53:11 +01:00
|
|
|
repository's licencse, but you may not be eligible to deploy the applied
|
2019-03-10 17:35:57 +01:00
|
|
|
algorithm in applications without granted license by the patent holder.
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
You may use timesync option 2 if you do not want or cannot accept this.
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
*/
|
2019-03-09 00:53:11 +01:00
|
|
|
|
|
|
|
#include "timesync.h"
|
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
#if (TIME_SYNC_LORASERVER) && (TIME_SYNC_LORAWAN) && (HAS_LORA)
|
|
|
|
#error Duplicate timesync method selected. You must select either LORASERVER or LORAWAN timesync.
|
|
|
|
#endif
|
|
|
|
|
2019-03-09 00:53:11 +01:00
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
2020-03-05 01:30:07 +01:00
|
|
|
static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO);
|
|
|
|
#define WRAP(v, top) (v++ > top ? 0 : v)
|
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
// timesync option 1: use external timeserver (for LoRAWAN < 1.0.3)
|
|
|
|
|
|
|
|
#if (TIME_SYNC_LORASERVER) && (HAS_LORA)
|
2019-04-07 16:13:04 +02:00
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
static TaskHandle_t timeSyncReqTask = NULL;
|
|
|
|
static bool timeSyncPending = false;
|
2020-03-03 16:38:29 +01:00
|
|
|
static uint8_t sample_idx = 0;
|
2020-03-03 12:54:42 +01:00
|
|
|
static uint32_t timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0};
|
2019-03-09 00:53:11 +01:00
|
|
|
|
|
|
|
// send time request message
|
2020-03-04 21:54:26 +01:00
|
|
|
void send_timesync_req(void) {
|
2019-04-13 13:59:30 +02:00
|
|
|
// if a timesync handshake is pending then exit
|
|
|
|
if (timeSyncPending)
|
2019-03-09 00:53:11 +01:00
|
|
|
return;
|
2019-04-07 16:13:04 +02:00
|
|
|
// else unblock timesync task
|
|
|
|
else {
|
2019-03-16 21:01:43 +01:00
|
|
|
ESP_LOGI(TAG, "[%0.3f] Timeserver sync request started", millis() / 1000.0);
|
2019-04-07 16:13:04 +02:00
|
|
|
xTaskNotifyGive(timeSyncReqTask);
|
2019-03-09 00:53:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// task for sending time sync requests
|
2020-03-04 21:54:26 +01:00
|
|
|
void IRAM_ATTR process_timesync_req(void *taskparameter) {
|
2019-03-09 20:40:21 +01:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
uint32_t rcv_seq_no = TIMEREQUEST_FINISH, time_offset_ms;
|
|
|
|
|
|
|
|
// this task is an endless loop, waiting in blocked mode, until it is
|
|
|
|
// unblocked by send_timesync_req(). It then waits to be notified from
|
|
|
|
// recv_timesync_ans(), which is called from RX callback in lorawan.cpp, each
|
|
|
|
// time a timestamp from timeserver arrived.
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2020-03-03 12:54:42 +01:00
|
|
|
// --- asnychronous part: generate and collect timestamps from gateway ---
|
2019-04-07 16:13:04 +02:00
|
|
|
|
2020-03-03 12:54:42 +01:00
|
|
|
while (1) {
|
2019-04-07 16:13:04 +02:00
|
|
|
|
|
|
|
// wait for kickoff
|
2019-04-07 14:06:47 +02:00
|
|
|
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
2019-04-13 13:59:30 +02:00
|
|
|
timeSyncPending = true;
|
2020-03-03 16:38:29 +01:00
|
|
|
time_offset_ms = 0;
|
2019-04-13 13:59:30 +02:00
|
|
|
|
|
|
|
// wait until we are joined if we are not
|
|
|
|
while (!LMIC.devaddr) {
|
2020-03-03 16:38:29 +01:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(5000));
|
2019-07-24 12:37:02 +02:00
|
|
|
}
|
2019-03-09 20:40:21 +01:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
// trigger and collect timestamp samples
|
|
|
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
|
|
|
// send timesync request to timeserver
|
2019-04-07 16:13:04 +02:00
|
|
|
payload.reset();
|
|
|
|
payload.addByte(time_sync_seqNo);
|
|
|
|
SendPayload(TIMEPORT, prio_high);
|
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
// wait until recv_timesync_ans() signals a timestamp was received
|
|
|
|
while (rcv_seq_no != time_sync_seqNo) {
|
|
|
|
if (xTaskNotifyWait(0x00, ULONG_MAX, &rcv_seq_no,
|
2019-04-13 13:59:30 +02:00
|
|
|
pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) ==
|
|
|
|
pdFALSE) {
|
|
|
|
ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: timeout",
|
|
|
|
millis() / 1000.0);
|
|
|
|
goto finish; // no valid sequence received before timeout
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 16:13:04 +02:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
// calculate time diff from collected timestamps
|
|
|
|
time_offset_ms += timesync_timestamp[sample_idx][timesync_rx] -
|
|
|
|
timesync_timestamp[sample_idx][timesync_tx];
|
|
|
|
|
|
|
|
// increment and maybe wrap around seqNo, keeping it in time port range
|
2020-03-05 01:30:07 +01:00
|
|
|
WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO);
|
2019-04-13 13:59:30 +02:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
// increment index for timestamp array
|
|
|
|
sample_idx++;
|
|
|
|
|
|
|
|
// if last cycle, send finish char for closing timesync handshake,
|
|
|
|
// else wait until time has come for next cycle
|
|
|
|
if (i < TIME_SYNC_SAMPLES - 1) { // wait for next cycle
|
2019-04-13 13:59:30 +02:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
|
2020-03-03 16:38:29 +01:00
|
|
|
} else { // finish timesync handshake
|
2019-04-13 13:59:30 +02:00
|
|
|
payload.reset();
|
2020-03-03 16:38:29 +01:00
|
|
|
payload.addByte(TIMEREQUEST_FINISH);
|
2019-04-13 13:59:30 +02:00
|
|
|
SendPayload(RCMDPORT, prio_high);
|
2020-03-03 16:38:29 +01:00
|
|
|
// open a receive window to get last time_sync_answer instantly
|
2019-08-30 18:55:08 +02:00
|
|
|
LMIC_sendAlive();
|
2019-03-16 21:01:43 +01:00
|
|
|
}
|
2020-03-03 12:54:42 +01:00
|
|
|
|
2019-04-07 21:54:19 +02:00
|
|
|
} // end of for loop to collect timestamp samples
|
2019-04-07 16:13:04 +02:00
|
|
|
|
2020-03-03 12:54:42 +01:00
|
|
|
// --- time critial part: evaluate timestamps and calculate time ---
|
|
|
|
|
2019-07-24 20:09:36 +02:00
|
|
|
// mask application irq to ensure accurate timing
|
2019-07-23 20:43:10 +02:00
|
|
|
mask_user_IRQ();
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
// average time offset over the summed up difference
|
|
|
|
// + add msec from recent gateway time, found with last sample_idx
|
|
|
|
// + apply a compensation constant TIME_SYNC_FIXUP for processing time
|
2020-03-03 12:54:42 +01:00
|
|
|
time_offset_ms /= TIME_SYNC_SAMPLES;
|
2020-03-03 16:38:29 +01:00
|
|
|
time_offset_ms +=
|
|
|
|
timesync_timestamp[sample_idx - 1][gwtime_msec] + TIME_SYNC_FIXUP;
|
2019-03-23 15:12:11 +01:00
|
|
|
|
2020-03-03 12:54:42 +01:00
|
|
|
// calculate absolute time in UTC epoch: take latest time received from
|
|
|
|
// gateway, convert to whole seconds, round to ceil, add fraction seconds
|
2020-03-03 16:38:29 +01:00
|
|
|
setMyTime(timesync_timestamp[sample_idx - 1][gwtime_sec] +
|
|
|
|
time_offset_ms / 1000,
|
2020-03-03 12:54:42 +01:00
|
|
|
time_offset_ms % 1000, _lora);
|
2019-03-27 20:38:00 +01:00
|
|
|
|
2019-07-24 20:09:36 +02:00
|
|
|
// end of time critical section: release app irq lock
|
2019-07-29 14:43:37 +02:00
|
|
|
unmask_user_IRQ();
|
2019-04-07 14:06:47 +02:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
finish:
|
|
|
|
timeSyncPending = false;
|
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
} // infinite while(1)
|
2019-03-09 00:53:11 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 12:54:42 +01:00
|
|
|
// called from lorawan.cpp
|
|
|
|
void store_timestamp(uint32_t timestamp, timesync_t timestamp_type) {
|
2019-03-12 23:50:02 +01:00
|
|
|
|
2020-03-03 20:16:36 +01:00
|
|
|
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: timestamp(t%d)=%d", millis() / 1000.0,
|
|
|
|
time_sync_seqNo, sample_idx, timestamp_type, timestamp);
|
2019-03-17 15:04:11 +01:00
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
timesync_timestamp[sample_idx][timestamp_type] = timestamp;
|
2019-03-16 21:01:43 +01:00
|
|
|
}
|
|
|
|
|
2019-10-05 13:15:45 +02:00
|
|
|
// process timeserver timestamp answer, called by myRxCallback() in lorawan.cpp
|
2019-10-04 15:31:31 +02:00
|
|
|
int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) {
|
2019-10-05 13:15:45 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
parse 7 byte timesync_answer:
|
|
|
|
|
|
|
|
byte meaning
|
|
|
|
1 sequence number (taken from node's time_sync_req)
|
|
|
|
2 timezone in 15 minutes steps
|
|
|
|
3..6 current second (from epoch time 1970)
|
|
|
|
7 1/250ths fractions of current second
|
|
|
|
*/
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-04-06 16:43:12 +02:00
|
|
|
// if no timesync handshake is pending then exit
|
2019-04-07 16:13:04 +02:00
|
|
|
if (!timeSyncPending)
|
2019-03-16 21:01:43 +01:00
|
|
|
return 0; // failure
|
|
|
|
|
2020-03-03 16:38:29 +01:00
|
|
|
// extract 1 byte timerequest sequence number from payload
|
2019-10-05 13:15:45 +02:00
|
|
|
uint8_t seq_no = buf[0];
|
|
|
|
buf++;
|
|
|
|
|
2019-03-19 00:02:35 +01:00
|
|
|
// if no time is available or spurious buffer then exit
|
|
|
|
if (buf_len != TIME_SYNC_FRAME_LENGTH) {
|
2019-10-05 13:15:45 +02:00
|
|
|
if (seq_no == 0xff)
|
2019-03-19 00:02:35 +01:00
|
|
|
ESP_LOGI(TAG, "[%0.3f] Timeserver error: no confident time available",
|
|
|
|
millis() / 1000.0);
|
|
|
|
else
|
|
|
|
ESP_LOGW(TAG, "[%0.3f] Timeserver error: spurious data received",
|
|
|
|
millis() / 1000.0);
|
|
|
|
return 0; // failure
|
|
|
|
}
|
|
|
|
|
|
|
|
else { // we received a probably valid time frame
|
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
// pointers to 4 bytes msb order
|
2019-07-24 20:09:36 +02:00
|
|
|
uint32_t timestamp_sec, *timestamp_ptr;
|
2019-07-24 13:52:24 +02:00
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
// extract 1 byte containing timezone offset
|
|
|
|
// one step being 15min * 60sec = 900sec
|
|
|
|
uint32_t timestamp_tzsec = buf[0] * 900; // timezone offset in secs
|
2019-09-03 16:28:11 +02:00
|
|
|
buf++;
|
2019-07-24 13:52:24 +02:00
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
// extract 4 bytes containing gateway time in UTC seconds since unix
|
|
|
|
// epoch and convert it to uint32_t, octet order is big endian
|
2019-08-29 09:32:55 +02:00
|
|
|
timestamp_ptr = (uint32_t *)buf;
|
2020-03-04 21:54:26 +01:00
|
|
|
// swap byte order from msb to lsb, note: this is a platform dependent hack
|
2019-07-24 20:09:36 +02:00
|
|
|
timestamp_sec = __builtin_bswap32(*timestamp_ptr);
|
2019-10-05 13:15:45 +02:00
|
|
|
buf += 4;
|
2020-03-04 21:54:26 +01:00
|
|
|
|
|
|
|
// extract 1 byte containing fractional seconds in 2^-8 second steps
|
|
|
|
// one step being 1/250th sec * 1000 = 4msec
|
|
|
|
uint16_t timestamp_msec = buf[0] * 4;
|
2020-03-03 12:54:42 +01:00
|
|
|
// calculate absolute time received from gateway
|
|
|
|
time_t t = timestamp_sec + timestamp_msec / 1000;
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2020-03-01 23:49:52 +01:00
|
|
|
// we guess timepoint is recent if it is newer than code compile date
|
2020-03-03 12:54:42 +01:00
|
|
|
if (timeIsValid(t)) {
|
2020-03-03 16:38:29 +01:00
|
|
|
ESP_LOGD(TAG, "[%0.3f] Timesync request seq#%d rcvd at %0.3f",
|
|
|
|
millis() / 1000.0, seq_no, osticks2ms(os_getTime()) / 1000.0);
|
2020-03-03 12:54:42 +01:00
|
|
|
|
|
|
|
// store time received from gateway
|
|
|
|
store_timestamp(timestamp_sec, gwtime_sec);
|
|
|
|
store_timestamp(timestamp_msec, gwtime_msec);
|
2020-03-04 21:54:26 +01:00
|
|
|
store_timestamp(timestamp_tzsec, gwtime_tzsec);
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-03-19 00:02:35 +01:00
|
|
|
// inform processing task
|
2019-04-07 16:13:04 +02:00
|
|
|
xTaskNotify(timeSyncReqTask, seq_no, eSetBits);
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-03-19 00:02:35 +01:00
|
|
|
return 1; // success
|
|
|
|
} else {
|
|
|
|
ESP_LOGW(TAG, "[%0.3f] Timeserver error: outdated time received",
|
|
|
|
millis() / 1000.0);
|
|
|
|
return 0; // failure
|
|
|
|
}
|
|
|
|
}
|
2019-03-09 15:25:44 +01:00
|
|
|
}
|
|
|
|
|
2019-08-04 15:17:50 +02:00
|
|
|
// create task for timeserver handshake processing, called from main.cpp
|
2019-04-07 16:13:04 +02:00
|
|
|
void timesync_init() {
|
|
|
|
xTaskCreatePinnedToCore(process_timesync_req, // task function
|
|
|
|
"timesync_req", // name of task
|
|
|
|
2048, // stack size of task
|
|
|
|
(void *)1, // task parameter
|
2019-04-07 21:54:19 +02:00
|
|
|
3, // priority of the task
|
2019-04-07 16:13:04 +02:00
|
|
|
&timeSyncReqTask, // task handle
|
|
|
|
1); // CPU core
|
|
|
|
}
|
|
|
|
|
2019-05-07 18:20:59 +02:00
|
|
|
#endif
|
2020-03-04 21:54:26 +01:00
|
|
|
|
|
|
|
// timesync option 2: use LoRAWAN network time (requires LoRAWAN >= 1.0.3)
|
|
|
|
|
|
|
|
#if (TIME_SYNC_LORAWAN) && (HAS_LORA)
|
|
|
|
|
|
|
|
// send time request message
|
|
|
|
void send_timesync_req(void) {
|
2020-03-05 01:30:07 +01:00
|
|
|
LMIC_requestNetworkTime(process_timesync_req, &time_sync_seqNo);
|
2020-03-04 21:54:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 01:30:07 +01:00
|
|
|
void IRAM_ATTR process_timesync_req(void *pUserData, int flagSuccess) {
|
|
|
|
// Explicit conversion from void* to uint8_t* to avoid compiler errors
|
|
|
|
uint8_t *seqNo = (uint8_t *)pUserData;
|
|
|
|
|
|
|
|
// mask application irq to ensure accurate timing
|
|
|
|
mask_user_IRQ();
|
2020-03-04 21:54:26 +01:00
|
|
|
|
|
|
|
// A struct that will be populated by LMIC_getNetworkTimeReference.
|
|
|
|
// It contains the following fields:
|
|
|
|
// - tLocal: the value returned by os_GetTime() when the time
|
|
|
|
// request was sent to the gateway, and
|
|
|
|
// - tNetwork: the seconds between the GPS epoch and the time
|
|
|
|
// the gateway received the time request
|
2020-03-04 23:39:28 +01:00
|
|
|
lmic_time_reference_t lmicTime;
|
2020-03-04 21:54:26 +01:00
|
|
|
|
2020-03-05 01:30:07 +01:00
|
|
|
uint32_t networkTimeSec;
|
|
|
|
uint16_t requestDelaymSec;
|
|
|
|
|
|
|
|
if ((flagSuccess != 1) || (time_sync_seqNo != *seqNo)) {
|
2020-03-04 21:54:26 +01:00
|
|
|
ESP_LOGW(TAG, "LoRaWAN network did not answer time request");
|
2020-03-05 01:30:07 +01:00
|
|
|
goto Finish;
|
2020-03-04 21:54:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate lmic_time_reference
|
2020-03-04 23:39:28 +01:00
|
|
|
flagSuccess = LMIC_getNetworkTimeReference(&lmicTime);
|
2020-03-04 21:54:26 +01:00
|
|
|
if (flagSuccess != 1) {
|
|
|
|
ESP_LOGW(TAG, "LoRaWAN time request failed");
|
2020-03-05 01:30:07 +01:00
|
|
|
goto Finish;
|
2020-03-04 21:54:26 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 01:30:07 +01:00
|
|
|
// Calculate UTCTime, considering the difference between GPS and UTC time
|
|
|
|
networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF;
|
2020-03-04 21:54:26 +01:00
|
|
|
// Add delay between the instant the time was transmitted and the current time
|
2020-03-05 01:30:07 +01:00
|
|
|
requestDelaymSec = osticks2ms(os_getTime() - lmicTime.tLocal);
|
2020-03-04 21:54:26 +01:00
|
|
|
// Update system time with time read from the network
|
2020-03-04 23:39:28 +01:00
|
|
|
setMyTime(networkTimeSec, requestDelaymSec, _lora);
|
2020-03-04 21:54:26 +01:00
|
|
|
|
2020-03-05 01:30:07 +01:00
|
|
|
Finish:
|
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
// end of time critical section: release app irq lock
|
|
|
|
unmask_user_IRQ();
|
|
|
|
|
2020-03-05 01:30:07 +01:00
|
|
|
// increment and maybe wrap around seqNo, keeping it in time port range
|
|
|
|
WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO);
|
|
|
|
|
2020-03-04 21:54:26 +01:00
|
|
|
} // user_request_network_time_callback
|
2020-03-05 01:30:07 +01:00
|
|
|
#endif // TIME_SYNC_LORAWAN
|