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
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef TIME_SYNC_TIMESERVER
|
|
|
|
|
|
|
|
#include "timesync.h"
|
|
|
|
|
2019-03-14 00:05:19 +01:00
|
|
|
using namespace std::chrono;
|
|
|
|
|
2019-03-09 00:53:11 +01:00
|
|
|
// Local logging tag
|
|
|
|
static const char TAG[] = __FILE__;
|
|
|
|
|
2019-03-09 20:40:21 +01:00
|
|
|
TaskHandle_t timeSyncReqTask;
|
2019-03-12 23:50:02 +01:00
|
|
|
|
2019-03-11 01:05:41 +01:00
|
|
|
static uint8_t time_sync_seqNo = 0;
|
2019-03-12 23:50:02 +01:00
|
|
|
static bool lora_time_sync_pending = false;
|
|
|
|
|
|
|
|
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-03-12 23:50:02 +01:00
|
|
|
myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES];
|
|
|
|
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
|
|
|
|
|
|
|
// if a timesync handshake is pending then exit
|
2019-03-12 23:50:02 +01:00
|
|
|
if (lora_time_sync_pending) {
|
2019-03-09 22:08:57 +01:00
|
|
|
ESP_LOGI(TAG, "Timeserver sync request already pending");
|
2019-03-09 00:53:11 +01:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
ESP_LOGI(TAG, "Timeserver sync request started");
|
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
lora_time_sync_pending = true;
|
2019-03-09 20:40:21 +01:00
|
|
|
|
2019-03-09 00:53:11 +01:00
|
|
|
// clear timestamp array
|
2019-03-12 23:50:02 +01:00
|
|
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
|
|
|
time_sync_tx[i] = time_sync_rx[i] = myClock_timepoint(); // set to epoch
|
2019-03-09 00:53:11 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 20:40:21 +01:00
|
|
|
// kick off temporary task for timeserver handshake processing
|
2019-03-09 00:53:11 +01:00
|
|
|
if (!timeSyncReqTask)
|
2019-03-12 23:50:02 +01:00
|
|
|
xTaskCreatePinnedToCore(process_timesync_req, // task function
|
|
|
|
"timesync_req", // name of task
|
|
|
|
2048, // stack size of task
|
|
|
|
(void *)1, // task parameter
|
|
|
|
0, // priority of the task
|
|
|
|
&timeSyncReqTask, // task handle
|
|
|
|
1); // CPU core
|
2019-03-09 00:53:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
// process timeserver timestamp answer, called from lorawan.cpp
|
2019-03-14 19:52:10 +01:00
|
|
|
int recv_timesync_ans(uint8_t buf[], uint8_t buf_len) {
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
// if no timesync handshake is pending or spurious buffer then exit
|
|
|
|
if ((!lora_time_sync_pending) || (buf_len != TIME_SYNC_FRAME_LENGTH))
|
2019-03-14 19:52:10 +01:00
|
|
|
return 0; // failure
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2019-03-11 01:05:41 +01:00
|
|
|
uint8_t seq_no = buf[0], k = seq_no % TIME_SYNC_SAMPLES;
|
2019-03-12 23:50:02 +01:00
|
|
|
uint16_t timestamp_msec = 4 * buf[5]; // convert 1/250th sec fractions to ms
|
|
|
|
uint32_t timestamp_sec = 0, tmp_sec = 0;
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2019-03-11 01:05:41 +01:00
|
|
|
for (uint8_t i = 1; i <= 4; i++) {
|
2019-03-12 23:50:02 +01:00
|
|
|
timestamp_sec = (tmp_sec <<= 8) |= buf[i];
|
2019-03-11 01:05:41 +01:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:52:10 +01:00
|
|
|
if (timestamp_sec + timestamp_msec) // field validation: timestamp not 0 ?
|
|
|
|
time_sync_rx[k] += seconds(timestamp_sec) + milliseconds(timestamp_msec);
|
|
|
|
else
|
|
|
|
return 0; // failure
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2019-03-13 20:20:19 +01:00
|
|
|
ESP_LOGD(TAG, "Timesync request #%d rcvd at %d", seq_no,
|
2019-03-12 23:50:02 +01:00
|
|
|
myClock::to_time_t(time_sync_rx[k]));
|
2019-03-09 00:53:11 +01:00
|
|
|
|
|
|
|
// inform processing task
|
2019-03-09 20:40:21 +01:00
|
|
|
if (timeSyncReqTask)
|
|
|
|
xTaskNotify(timeSyncReqTask, seq_no, eSetBits);
|
2019-03-14 19:52:10 +01:00
|
|
|
|
|
|
|
return 1; // success
|
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-03-12 23:50:02 +01:00
|
|
|
uint8_t k = 0, i = 0;
|
|
|
|
uint32_t seq_no = 0;
|
2019-03-14 00:05:19 +01:00
|
|
|
// milliseconds time_offset(0);
|
2019-03-12 23:50:02 +01:00
|
|
|
auto time_offset = myClock_msecTick::zero();
|
2019-03-14 19:52:10 +01:00
|
|
|
int time_offset_msec;
|
|
|
|
time_t time_to_set;
|
2019-03-09 20:40:21 +01:00
|
|
|
|
2019-03-09 00:53:11 +01:00
|
|
|
// enqueue timestamp samples in lora sendqueue
|
2019-03-12 23:50:02 +01:00
|
|
|
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
2019-03-09 20:40:21 +01:00
|
|
|
|
|
|
|
// wrap around seqNo 0 .. 254
|
|
|
|
time_sync_seqNo = (time_sync_seqNo >= 255) ? 0 : time_sync_seqNo + 1;
|
|
|
|
|
|
|
|
// send sync request to server
|
2019-03-09 00:53:11 +01:00
|
|
|
payload.reset();
|
2019-03-09 22:08:57 +01:00
|
|
|
payload.addByte(time_sync_seqNo);
|
2019-03-09 00:53:11 +01:00
|
|
|
SendPayload(TIMEPORT, prio_high);
|
|
|
|
|
2019-03-09 20:40:21 +01:00
|
|
|
// process answer
|
2019-03-09 15:25:44 +01:00
|
|
|
if ((xTaskNotifyWait(0x00, ULONG_MAX, &seq_no,
|
2019-03-12 23:50:02 +01:00
|
|
|
pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) ||
|
2019-03-09 20:40:21 +01:00
|
|
|
(seq_no != time_sync_seqNo)) {
|
2019-03-12 23:50:02 +01:00
|
|
|
|
2019-03-10 17:35:57 +01:00
|
|
|
ESP_LOGW(TAG, "Timeserver handshake failed");
|
2019-03-09 20:40:21 +01:00
|
|
|
goto finish;
|
|
|
|
} // no valid sequence received before timeout
|
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
else { // calculate time diff from collected timestamps
|
2019-03-11 01:05:41 +01:00
|
|
|
k = seq_no % TIME_SYNC_SAMPLES;
|
|
|
|
|
2019-03-14 00:05:19 +01:00
|
|
|
auto t_tx = time_point_cast<milliseconds>(
|
2019-03-13 20:20:19 +01:00
|
|
|
time_sync_tx[k]); // timepoint when node TX_completed
|
2019-03-14 00:05:19 +01:00
|
|
|
auto t_rx = time_point_cast<milliseconds>(
|
2019-03-12 23:50:02 +01:00
|
|
|
time_sync_rx[k]); // timepoint when message was seen on gateway
|
2019-03-11 01:05:41 +01:00
|
|
|
|
2019-03-13 20:20:19 +01:00
|
|
|
time_offset += t_rx - t_tx; // cumulate timepoint diffs
|
2019-03-11 01:05:41 +01:00
|
|
|
|
2019-03-14 19:52:10 +01:00
|
|
|
ESP_LOGD(TAG, "time_offset: %lldms", time_offset.count());
|
|
|
|
|
2019-03-13 20:20:19 +01:00
|
|
|
if (i < TIME_SYNC_SAMPLES - 1) // wait until next cycle
|
2019-03-12 23:50:02 +01:00
|
|
|
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
|
2019-03-09 15:25:44 +01:00
|
|
|
}
|
2019-03-10 17:35:57 +01:00
|
|
|
} // for
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
// calculate time offset from collected diffs and set time if necessary
|
2019-03-14 00:05:19 +01:00
|
|
|
ESP_LOGD(TAG, "Avg time diff: %lldms", time_offset.count());
|
2019-03-14 19:52:10 +01:00
|
|
|
time_offset /= TIME_SYNC_SAMPLES;
|
2019-03-15 22:48:09 +01:00
|
|
|
// 1sec wait for top of second
|
|
|
|
time_to_set = now() + time_offset.count() / 1000 + 1;
|
2019-03-14 19:52:10 +01:00
|
|
|
ESP_LOGD(TAG, "Calculated UTC epoch time: %d", time_to_set);
|
2019-03-09 15:25:44 +01:00
|
|
|
|
2019-03-14 19:52:10 +01:00
|
|
|
// adjust system time
|
|
|
|
if (timeIsValid(time_to_set)) {
|
2019-03-09 15:25:44 +01:00
|
|
|
|
2019-03-14 19:52:10 +01:00
|
|
|
if (abs(time_offset.count()) >=
|
|
|
|
TIME_SYNC_TRIGGER) { // milliseconds threshold
|
2019-03-14 00:05:19 +01:00
|
|
|
|
|
|
|
// wait until top of second
|
2019-03-14 19:52:10 +01:00
|
|
|
time_offset_msec = abs(time_offset.count()) % 1000;
|
2019-03-14 00:05:19 +01:00
|
|
|
ESP_LOGD(TAG, "waiting %dms", 1000 - time_offset_msec);
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(1000 - time_offset_msec));
|
|
|
|
|
|
|
|
// sync timer pps to top of second
|
2019-03-16 00:38:41 +01:00
|
|
|
if (ppsIRQ) {
|
|
|
|
timerRestart(ppsIRQ); // reset pps timer
|
|
|
|
CLOCKIRQ(); // fire clock pps interrupt
|
|
|
|
}
|
2019-03-14 00:05:19 +01:00
|
|
|
|
2019-03-16 00:38:41 +01:00
|
|
|
setTime(time_to_set);
|
2019-03-09 15:25:44 +01:00
|
|
|
timeSource = _lora;
|
|
|
|
timesyncer.attach(TIME_SYNC_INTERVAL * 60,
|
|
|
|
timeSync); // set to regular repeat
|
2019-03-16 00:38:41 +01:00
|
|
|
ESP_LOGI(TAG, "Timesync finished, time adjusted by %lld ms",
|
|
|
|
time_offset.count());
|
2019-03-09 00:53:11 +01:00
|
|
|
} else
|
2019-03-15 22:48:09 +01:00
|
|
|
ESP_LOGI(TAG, "Timesync finished, time not adjusted, is up to date");
|
2019-03-09 15:25:44 +01:00
|
|
|
} else
|
2019-03-14 19:52:10 +01:00
|
|
|
ESP_LOGW(TAG, "Invalid time received from timeserver");
|
2019-03-09 15:25:44 +01:00
|
|
|
|
|
|
|
finish:
|
2019-03-09 00:53:11 +01:00
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
lora_time_sync_pending = false;
|
2019-03-09 20:40:21 +01:00
|
|
|
timeSyncReqTask = NULL;
|
2019-03-09 00:53:11 +01:00
|
|
|
vTaskDelete(NULL); // end task
|
|
|
|
}
|
|
|
|
|
2019-03-10 17:35:57 +01:00
|
|
|
// called from lorawan.cpp after time_sync_req was sent
|
2019-03-14 19:52:10 +01:00
|
|
|
void store_time_sync_req(time_t t_millisec) {
|
2019-03-12 23:50:02 +01:00
|
|
|
|
2019-03-09 20:40:21 +01:00
|
|
|
uint8_t k = time_sync_seqNo % TIME_SYNC_SAMPLES;
|
2019-03-14 19:52:10 +01:00
|
|
|
time_sync_tx[k] += milliseconds(t_millisec);
|
2019-03-09 15:25:44 +01:00
|
|
|
|
2019-03-12 23:50:02 +01:00
|
|
|
ESP_LOGD(TAG, "Timesync request #%d sent at %d", time_sync_seqNo,
|
|
|
|
myClock::to_time_t(time_sync_tx[k]));
|
2019-03-09 15:25:44 +01:00
|
|
|
}
|
|
|
|
|
2019-03-09 00:53:11 +01:00
|
|
|
#endif
|