ESP32-PaxCounter/src/timesync.cpp

301 lines
9.9 KiB
C++
Raw Normal View History

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
2020-03-06 19:24:58 +01:00
#if (HAS_LORA)
2019-03-09 00:53:11 +01:00
2020-03-06 19:24:58 +01:00
#if (TIME_SYNC_LORASERVER) && (TIME_SYNC_LORAWAN)
2020-03-04 21:54:26 +01:00
#error Duplicate timesync method selected. You must select either LORASERVER or LORAWAN timesync.
#endif
2020-03-06 19:24:58 +01:00
#include "timesync.h"
2019-03-09 00:53:11 +01:00
#define WRAP(v, top) (v++ > top ? 0 : v)
2020-03-06 19:24:58 +01:00
// Local logging tag
static const char TAG[] = __FILE__;
2019-04-07 16:13:04 +02:00
2020-03-04 21:54:26 +01:00
static bool timeSyncPending = false;
2020-03-06 19:24:58 +01:00
static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO),
sample_idx;
static uint16_t timestamp_msec;
static uint32_t timestamp_sec,
timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps];
2020-03-07 19:41:08 +01:00
static TaskHandle_t timeSyncProcTask = NULL;
2020-03-06 19:24:58 +01:00
// create task for timeserver handshake processing, called from main.cpp
void timesync_init() {
xTaskCreatePinnedToCore(timesync_processReq, // task function
2020-03-07 19:41:08 +01:00
"timesync_proc", // name of task
2020-03-06 19:24:58 +01:00
2048, // stack size of task
(void *)1, // task parameter
3, // priority of the task
2020-03-07 19:41:08 +01:00
&timeSyncProcTask, // task handle
2020-03-06 19:24:58 +01:00
1); // CPU core
}
2019-03-09 00:53:11 +01:00
2020-03-06 19:24:58 +01:00
// kickoff asnychronous timesync handshake
void timesync_sendReq(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;
2020-03-06 19:24:58 +01:00
// else clear array and unblock timesync task
2019-04-07 16:13:04 +02:00
else {
2020-03-06 19:24:58 +01:00
ESP_LOGI(TAG, "[%0.3f] Timeserver sync request seqNo#%d started",
millis() / 1000.0, time_sync_seqNo);
sample_idx = 0;
2020-03-07 19:41:08 +01:00
xTaskNotifyGive(timeSyncProcTask);
2019-03-09 00:53:11 +01:00
}
}
2020-03-06 19:24:58 +01:00
// task for processing time sync request
void IRAM_ATTR timesync_processReq(void *taskparameter) {
2019-03-09 20:40:21 +01:00
2020-03-07 19:41:08 +01:00
uint32_t seqNo = TIMEREQUEST_END, time_offset_ms;
// this task is an endless loop, waiting in blocked mode, until it is
2020-03-06 19:24:58 +01:00
// unblocked by timesync_sendReq(). It then waits to be notified from
2020-03-07 19:41:08 +01:00
// timesync_serverAnswer(), which is called from LMIC each time a timestamp
// from the timesource via LORAWAN arrived.
2019-03-16 21:01:43 +01:00
// --- asnychronous part: generate and collect timestamps from gateway ---
2019-04-07 16:13:04 +02: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;
time_offset_ms = 0;
2019-04-13 13:59:30 +02:00
// wait until we are joined if we are not
while (!LMIC.devaddr) {
vTaskDelay(pdMS_TO_TICKS(5000));
2019-07-24 12:37:02 +02:00
}
2019-03-09 20:40:21 +01:00
2020-03-06 19:24:58 +01:00
// clear timestamp array
timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0};
// trigger and collect samples in timestamp array
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
2020-03-06 19:24:58 +01:00
// send timesync request to timeserver or networkserver
#if (TIME_SYNC_LORASERVER)
// timesync option 1: use external timeserver (for LoRAWAN < 1.0.3)
2020-03-07 19:41:08 +01:00
// ask timeserver
2019-04-07 16:13:04 +02:00
payload.reset();
payload.addByte(time_sync_seqNo);
SendPayload(TIMEPORT, prio_high);
2020-03-06 19:24:58 +01:00
#elif (TIME_SYNC_LORAWAN)
// timesync option 2: use LoRAWAN network time (requires LoRAWAN >= 1.0.3)
2020-03-07 19:41:08 +01:00
// ask networkserver
LMIC_requestNetworkTime(timesync_serverAnswer, &time_sync_seqNo);
2020-03-06 19:24:58 +01:00
#endif
2019-04-07 16:13:04 +02:00
2020-03-07 19:41:08 +01:00
// open a receive window to immediately get the answer (Class A device)
LMIC_sendAlive();
2020-03-06 19:24:58 +01:00
// wait until a timestamp was received
2020-03-07 19:41:08 +01:00
if (xTaskNotifyWait(0x00, ULONG_MAX, &seqNo,
pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) {
ESP_LOGW(TAG, "[%0.3f] Timesync handshake error: timeout",
millis() / 1000.0);
goto Fail; // no valid sequence received before timeout
}
// check if we are in handshake with server
if (seqNo != time_sync_seqNo) {
ESP_LOGW(TAG, "[%0.3f] Timesync handshake aborted", millis() / 1000.0);
goto Fail;
2019-04-13 13:59:30 +02:00
}
2019-04-07 16:13:04 +02:00
2020-03-07 19:41:08 +01:00
// calculate time diff with received timestamp
time_offset_ms += timesync_timestamp[sample_idx][timesync_rx] -
timesync_timestamp[sample_idx][timesync_tx];
2020-03-07 19:41:08 +01:00
// increment and wrap around seqNo, keeping it in time port range
WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO);
// increment index for timestamp array
sample_idx++;
2020-03-06 19:24:58 +01:00
// if last cycle, finish after, else pause until next cycle
2020-03-07 19:41:08 +01:00
if (i < TIME_SYNC_SAMPLES - 1)
2019-04-13 13:59:30 +02:00
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
2019-04-07 21:54:19 +02:00
} // end of for loop to collect timestamp samples
2019-04-07 16:13:04 +02:00
// --- time critial part: evaluate timestamps and calculate time ---
// 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
// 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
time_offset_ms /= TIME_SYNC_SAMPLES;
time_offset_ms +=
timesync_timestamp[sample_idx - 1][gwtime_msec] + TIME_SYNC_FIXUP;
// calculate absolute time in UTC epoch: take latest time received from
// gateway, convert to whole seconds, round to ceil, add fraction seconds
setMyTime(timesync_timestamp[sample_idx - 1][gwtime_sec] +
time_offset_ms / 1000,
time_offset_ms % 1000, _lora);
2019-03-27 20:38:00 +01:00
2020-03-07 19:41:08 +01:00
// send timerequest end char to show timesync was successful
payload.reset();
payload.addByte(TIMEREQUEST_END);
SendPayload(RCMDPORT, prio_high);
goto Finish;
Fail:
// set retry timer
timesyncer.attach(TIME_SYNC_INTERVAL_RETRY * 60, timeSync);
Finish:
// end of time critical section: release app irq lock
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
}
2020-03-06 19:24:58 +01:00
// store incoming timestamps
void timesync_storeReq(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
timesync_timestamp[sample_idx][timestamp_type] = timestamp;
2019-03-16 21:01:43 +01:00
}
2020-03-07 19:41:08 +01:00
// callback function to receive network time server answer
void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
// if no timesync handshake is pending then exit
if (!timeSyncPending)
return;
// mask application irq to ensure accurate timing
mask_user_IRQ();
int rc = 0;
uint32_t timestamp_sec;
uint16_t timestamp_msec;
2020-03-06 19:24:58 +01:00
#if (TIME_SYNC_LORASERVER)
2020-03-07 19:41:08 +01:00
// store LMIC time when we received the timesync answer
timesync_storeReq(osticks2ms(os_getTime()), timesync_rx);
// pUserData: contains pointer to payload buffer
// flag: length of buffer
2019-10-05 13:15:45 +02:00
/*
2020-03-07 19:41:08 +01:00
parse 6 byte timesync_answer:
2019-10-05 13:15:45 +02:00
2020-03-07 19:41:08 +01:00
byte meaning
1 sequence number (taken from node's time_sync_req)
2..5 current second (from epoch time 1970)
6 1/250ths fractions of current second
*/
2019-03-16 21:01:43 +01:00
2020-03-07 19:41:08 +01:00
// Explicit conversion from void* to uint8_t* to avoid compiler errors
uint8_t *p = (uint8_t *)pUserData;
// Get payload buffer from pUserData
uint8_t *buf = p;
2019-03-16 21:01:43 +01:00
// extract 1 byte timerequest sequence number from payload
2020-03-06 19:24:58 +01:00
uint8_t seqNo = buf[0];
2019-10-05 13:15:45 +02:00
buf++;
2019-03-19 00:02:35 +01:00
// if no time is available or spurious buffer then exit
2020-03-07 19:41:08 +01:00
if (flag != TIME_SYNC_FRAME_LENGTH) {
if (seqNo == TIMEREQUEST_END)
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);
2020-03-07 19:41:08 +01:00
goto Exit; // failure
2019-03-19 00:02:35 +01:00
}
2020-03-07 19:41:08 +01:00
// pointer to 4 bytes msb order
uint32_t *timestamp_ptr;
// extract 4 bytes containing gateway time in UTC seconds since unix
// epoch and convert it to uint32_t, octet order is big endian
timestamp_ptr = (uint32_t *)buf;
// swap byte order from msb to lsb, note: this is a platform dependent hack
timestamp_sec = __builtin_bswap32(*timestamp_ptr);
buf += 4;
// extract 1 byte containing fractional seconds in 2^-8 second steps
// one step being 1/250th sec * 1000 = 4msec
timestamp_msec = buf[0] * 4;
2020-03-07 19:41:08 +01:00
goto Finish;
2019-03-09 15:25:44 +01:00
2020-03-06 19:24:58 +01:00
#elif (TIME_SYNC_LORAWAN)
2020-03-04 21:54:26 +01:00
2020-03-07 19:41:08 +01:00
// pUserData: contains pointer to SeqNo
// flagSuccess: indicates if we got a recent time from the network
2020-03-07 19:41:08 +01:00
// Explicit conversion from void* to uint8_t* to avoid compiler errors
uint8_t *p = (uint8_t *)pUserData;
// Get seqNo from pUserData
uint8_t seqNo = *p;
if (flag != 1) {
ESP_LOGW(TAG, "[%0.3f] Network did not answer time request",
millis() / 1000.0);
goto Exit;
}
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-06 19:24:58 +01:00
// Populate lmic_time_reference
if ((LMIC_getNetworkTimeReference(&lmicTime)) != 1) {
2020-03-07 19:41:08 +01:00
ESP_LOGW(TAG, "[%0.3f] Network time request failed", millis() / 1000.0);
goto Exit;
}
2020-03-04 21:54:26 +01:00
2020-03-07 19:41:08 +01:00
// Calculate UTCTime, considering the difference between GPS and UTC time
timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF;
// Add delay between the instant the time was transmitted and the current time
timestamp_msec = osticks2ms(os_getTime() - lmicTime.tLocal);
goto Finish;
2020-03-07 19:41:08 +01:00
#endif // (TIME_SYNC_LORAWAN)
Finish:
// check if calucalted time is recent
if (timeIsValid(timestamp_sec)) {
// store time received from gateway
timesync_storeReq(timestamp_sec, gwtime_sec);
timesync_storeReq(timestamp_msec, gwtime_msec);
// success
rc = 1;
} else {
ESP_LOGW(TAG, "[%0.3f] Timeserver error: outdated time received",
millis() / 1000.0);
}
2020-03-04 21:54:26 +01:00
2020-03-07 19:41:08 +01:00
Exit:
// end of time critical section: release app irq lock
unmask_user_IRQ();
// inform processing task
xTaskNotify(timeSyncProcTask, rc ? seqNo : TIMEREQUEST_END, eSetBits);
2020-03-06 19:24:58 +01:00
}
#endif // HAS_LORA