2019-03-09 00:53:11 +01:00
|
|
|
/*
|
|
|
|
|
2019-03-09 00:54:34 +01:00
|
|
|
///--> IMPORTANT LICENSE NOTE for 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
|
|
|
|
|
|
|
*/
|
|
|
|
|
2019-04-01 08:10:08 +02:00
|
|
|
#if (TIME_SYNC_LORASERVER) && (HAS_LORA)
|
2019-03-09 00:53:11 +01:00
|
|
|
|
|
|
|
#include "timesync.h"
|
|
|
|
|
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
using namespace std::chrono;
|
2019-03-12 23:50:02 +01:00
|
|
|
|
|
|
|
typedef std::chrono::system_clock myClock;
|
|
|
|
typedef myClock::time_point myClock_timepoint;
|
|
|
|
typedef std::chrono::duration<long long int, std::ratio<1, 1000>>
|
|
|
|
myClock_msecTick;
|
2019-03-13 20:20:19 +01:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
TaskHandle_t timeSyncReqTask = NULL;
|
|
|
|
|
2019-10-05 15:03:15 +02:00
|
|
|
static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO);
|
2019-04-07 16:13:04 +02:00
|
|
|
static bool timeSyncPending = false;
|
|
|
|
static myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES];
|
|
|
|
static myClock_timepoint time_sync_rx[TIME_SYNC_SAMPLES];
|
2019-03-09 00:53:11 +01:00
|
|
|
|
|
|
|
// send time request message
|
2019-03-12 23:50:02 +01:00
|
|
|
void send_timesync_req() {
|
2019-03-09 00:53:11 +01:00
|
|
|
|
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
|
2019-03-12 23:50:02 +01:00
|
|
|
void process_timesync_req(void *taskparameter) {
|
2019-03-09 20:40:21 +01:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
uint8_t k;
|
2019-03-28 14:19:02 +01:00
|
|
|
uint16_t time_to_set_fraction_msec;
|
2019-04-13 13:59:30 +02:00
|
|
|
uint32_t seq_no = 0, time_to_set;
|
2019-03-24 00:15:04 +01:00
|
|
|
auto time_offset_ms = myClock_msecTick::zero();
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
while (1) {
|
|
|
|
|
2019-04-07 21:54:19 +02:00
|
|
|
// reset all timestamps before next sync run
|
2019-04-07 16:13:04 +02:00
|
|
|
time_offset_ms = myClock_msecTick::zero();
|
|
|
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++)
|
|
|
|
time_sync_tx[i] = time_sync_rx[i] = myClock_timepoint();
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// wait until we are joined if we are not
|
|
|
|
while (!LMIC.devaddr) {
|
2019-07-23 21:33:27 +02:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(3000));
|
2019-07-24 12:37:02 +02:00
|
|
|
}
|
2019-03-09 20:40:21 +01:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
// collect timestamp samples
|
|
|
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
|
|
|
// send sync request to server
|
|
|
|
payload.reset();
|
|
|
|
payload.addByte(time_sync_seqNo);
|
|
|
|
SendPayload(TIMEPORT, prio_high);
|
|
|
|
|
2019-04-13 13:59:30 +02:00
|
|
|
// wait for a valid timestamp from recv_timesync_ans()
|
|
|
|
while (seq_no != time_sync_seqNo) {
|
|
|
|
if (xTaskNotifyWait(0x00, ULONG_MAX, &seq_no,
|
|
|
|
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
|
|
|
|
2019-04-07 21:54:19 +02:00
|
|
|
// process answer
|
2019-04-13 13:59:30 +02:00
|
|
|
k = seq_no % TIME_SYNC_SAMPLES;
|
|
|
|
|
|
|
|
// calculate time diff from collected timestamps
|
|
|
|
time_offset_ms += time_point_cast<milliseconds>(time_sync_rx[k]) -
|
|
|
|
time_point_cast<milliseconds>(time_sync_tx[k]);
|
|
|
|
|
|
|
|
// wrap around seqNo, keeping it in time port range
|
2019-08-29 09:32:55 +02:00
|
|
|
time_sync_seqNo++;
|
2019-10-05 13:15:45 +02:00
|
|
|
if (time_sync_seqNo > TIMEREQUEST_MAX_SEQNO) {
|
2019-08-29 09:32:55 +02:00
|
|
|
time_sync_seqNo = 0;
|
|
|
|
}
|
2019-04-13 13:59:30 +02:00
|
|
|
|
|
|
|
if (i < TIME_SYNC_SAMPLES - 1) {
|
|
|
|
// wait until next cycle
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
|
|
|
|
} else { // before sending last time sample...
|
|
|
|
// ...send flush to open a receive window for last time_sync_answer
|
|
|
|
payload.reset();
|
|
|
|
payload.addByte(0x99);
|
|
|
|
SendPayload(RCMDPORT, prio_high);
|
|
|
|
// ...send a alive open a receive window for last time_sync_answer
|
2019-08-30 18:55:08 +02:00
|
|
|
LMIC_sendAlive();
|
2019-03-16 21:01:43 +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
|
|
|
|
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
|
|
|
|
2019-04-07 21:54:19 +02:00
|
|
|
// average time offset over all collected diffs
|
2019-04-07 16:13:04 +02:00
|
|
|
time_offset_ms /= TIME_SYNC_SAMPLES;
|
2019-03-27 20:38:00 +01:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
// calculate time offset with millisecond precision using LMIC's time base,
|
|
|
|
// since we use LMIC's ostime_t txEnd as tx timestamp.
|
2019-04-07 21:54:19 +02:00
|
|
|
// Also apply calibration const to compensate processing time.
|
2019-04-07 16:13:04 +02:00
|
|
|
time_offset_ms +=
|
|
|
|
milliseconds(osticks2ms(os_getTime())) + milliseconds(TIME_SYNC_FIXUP);
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
// calculate absolute time in UTC epoch: convert to whole seconds, round to
|
|
|
|
// ceil, and calculate fraction milliseconds
|
|
|
|
time_to_set = (uint32_t)(time_offset_ms.count() / 1000) + 1;
|
|
|
|
// calculate fraction milliseconds
|
|
|
|
time_to_set_fraction_msec = (uint16_t)(time_offset_ms.count() % 1000);
|
2019-03-23 15:12:11 +01:00
|
|
|
|
2019-08-03 12:27:24 +02:00
|
|
|
setMyTime(time_to_set, time_to_set_fraction_msec, _lora);
|
2019-03-27 20:38:00 +01:00
|
|
|
|
2019-07-22 11:25:05 +02:00
|
|
|
finish:
|
2019-07-24 20:09:36 +02:00
|
|
|
// end of time critical section: release app irq lock
|
2019-04-07 16:13:04 +02:00
|
|
|
timeSyncPending = false;
|
2019-07-29 14:43:37 +02:00
|
|
|
unmask_user_IRQ();
|
2019-04-07 14:06:47 +02:00
|
|
|
|
2019-04-07 16:13:04 +02:00
|
|
|
} // infinite while(1)
|
2019-03-09 00:53:11 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 17:35:57 +01:00
|
|
|
// called from lorawan.cpp after time_sync_req was sent
|
2019-03-25 19:06:54 +01:00
|
|
|
void store_time_sync_req(uint32_t timestamp) {
|
2019-03-12 23:50:02 +01:00
|
|
|
|
2019-04-13 13:59:30 +02:00
|
|
|
// if no timesync handshake is pending then exit
|
|
|
|
if (!timeSyncPending)
|
|
|
|
return;
|
2019-03-17 15:04:11 +01:00
|
|
|
|
2019-04-13 13:59:30 +02:00
|
|
|
uint8_t k = time_sync_seqNo % TIME_SYNC_SAMPLES;
|
|
|
|
time_sync_tx[k] += milliseconds(timestamp);
|
2019-03-09 15:25:44 +01:00
|
|
|
|
2019-04-13 13:59:30 +02:00
|
|
|
ESP_LOGD(TAG, "[%0.3f] Timesync request #%d of %d sent at %d.%03d",
|
|
|
|
millis() / 1000.0, k + 1, TIME_SYNC_SAMPLES, timestamp / 1000,
|
|
|
|
timestamp % 1000);
|
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
|
|
|
|
|
2019-10-05 13:15:45 +02:00
|
|
|
// extract 1 byte timerequest sequence number from buffer
|
|
|
|
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
|
|
|
|
|
2019-04-06 16:43:12 +02:00
|
|
|
uint8_t k = seq_no % TIME_SYNC_SAMPLES;
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-08-18 22:13:42 +02:00
|
|
|
// pointers to 4 bytes containing UTC seconds since unix epoch, msb
|
2019-07-24 20:09:36 +02:00
|
|
|
uint32_t timestamp_sec, *timestamp_ptr;
|
2019-07-24 13:52:24 +02:00
|
|
|
|
2019-10-05 13:15:45 +02:00
|
|
|
// extract 1 byte timezone from buffer (one step being 15min * 60s = 900s)
|
|
|
|
uint32_t timezone_sec = buf[0] * 900;
|
2019-09-03 16:28:11 +02:00
|
|
|
buf++;
|
2019-07-24 13:52:24 +02:00
|
|
|
|
2019-10-05 13:15:45 +02:00
|
|
|
// extract 4 bytes timestamp from buffer
|
|
|
|
// and convert it to uint32_t, octet order is big endian
|
2019-08-29 09:32:55 +02:00
|
|
|
timestamp_ptr = (uint32_t *)buf;
|
2019-07-24 20:09:36 +02:00
|
|
|
// swap byte order from msb to lsb, note: this is platform dependent
|
|
|
|
timestamp_sec = __builtin_bswap32(*timestamp_ptr);
|
2019-10-05 13:15:45 +02:00
|
|
|
buf += 4;
|
|
|
|
// extract 1 byte fractional seconds in 2^-8 second steps
|
|
|
|
// (= 1/250th sec), we convert this to ms
|
|
|
|
uint16_t timestamp_msec = 4 * buf[0];
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-03-19 00:02:35 +01:00
|
|
|
// construct the timepoint when message was seen on gateway
|
2019-10-05 13:15:45 +02:00
|
|
|
time_sync_rx[k] +=
|
2019-10-10 16:11:38 +02:00
|
|
|
seconds(timestamp_sec) + milliseconds(timestamp_msec);
|
2019-03-16 21:01:43 +01:00
|
|
|
|
2019-04-07 21:54:19 +02:00
|
|
|
// we guess timepoint is recent if it newer than code compile date
|
2019-03-19 00:02:35 +01:00
|
|
|
if (timeIsValid(myClock::to_time_t(time_sync_rx[k]))) {
|
2019-04-13 13:59:30 +02:00
|
|
|
ESP_LOGD(TAG, "[%0.3f] Timesync request #%d of %d rcvd at %d.%03d",
|
|
|
|
millis() / 1000.0, k + 1, TIME_SYNC_SAMPLES, timestamp_sec,
|
|
|
|
timestamp_msec);
|
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
|