reworked lora appserver timesync logic (part 1)
This commit is contained in:
parent
0f2e48f39c
commit
1516d71018
@ -11,12 +11,18 @@
|
||||
#define TIME_SYNC_FIXUP 4 // calibration to fixup processing time [milliseconds]
|
||||
#define TIMEREQUEST_MAX_SEQNO 0xf0 // threshold for wrap around seqno
|
||||
|
||||
enum timesync_t {
|
||||
timesync_tx,
|
||||
timesync_rx,
|
||||
gwtime_sec,
|
||||
gwtime_msec,
|
||||
no_of_timestamps
|
||||
};
|
||||
|
||||
void timesync_init(void);
|
||||
void send_timesync_req(void);
|
||||
|
||||
int recv_timesync_ans(const uint8_t buf[], uint8_t buf_len);
|
||||
|
||||
void process_timesync_req(void *taskparameter);
|
||||
void store_time_sync_req(uint32_t t_millisec);
|
||||
void store_timestamp(uint32_t timestamp, timesync_t timestamp_type);
|
||||
|
||||
#endif
|
||||
|
108
src/timesync.cpp
108
src/timesync.cpp
@ -16,19 +16,11 @@ algorithm in applications without granted license by the patent holder.
|
||||
// Local logging tag
|
||||
static const char TAG[] = __FILE__;
|
||||
|
||||
using namespace std::chrono;
|
||||
|
||||
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;
|
||||
|
||||
TaskHandle_t timeSyncReqTask = NULL;
|
||||
|
||||
static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO);
|
||||
static bool timeSyncPending = false;
|
||||
static myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES];
|
||||
static myClock_timepoint time_sync_rx[TIME_SYNC_SAMPLES];
|
||||
static uint32_t timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0};
|
||||
|
||||
// send time request message
|
||||
void send_timesync_req() {
|
||||
@ -46,18 +38,13 @@ void send_timesync_req() {
|
||||
// task for sending time sync requests
|
||||
void process_timesync_req(void *taskparameter) {
|
||||
|
||||
uint8_t k;
|
||||
uint16_t time_to_set_fraction_msec;
|
||||
uint32_t seq_no = 0, time_to_set;
|
||||
auto time_offset_ms = myClock_msecTick::zero();
|
||||
uint8_t i;
|
||||
uint32_t seq_no, time_offset_ms;
|
||||
|
||||
// --- asnychronous part: generate and collect timestamps from gateway ---
|
||||
|
||||
while (1) {
|
||||
|
||||
// reset all timestamps before next sync run
|
||||
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
|
||||
ulTaskNotifyTake(pdFALSE, portMAX_DELAY);
|
||||
timeSyncPending = true;
|
||||
@ -67,8 +54,8 @@ void process_timesync_req(void *taskparameter) {
|
||||
vTaskDelay(pdMS_TO_TICKS(3000));
|
||||
}
|
||||
|
||||
// collect timestamp samples
|
||||
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
||||
// generate and collect timestamp samples
|
||||
for (i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
||||
// send sync request to server
|
||||
payload.reset();
|
||||
payload.addByte(time_sync_seqNo);
|
||||
@ -85,13 +72,6 @@ void process_timesync_req(void *taskparameter) {
|
||||
}
|
||||
}
|
||||
|
||||
// process answer
|
||||
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
|
||||
time_sync_seqNo++;
|
||||
if (time_sync_seqNo > TIMEREQUEST_MAX_SEQNO) {
|
||||
@ -109,32 +89,36 @@ void process_timesync_req(void *taskparameter) {
|
||||
// ...send a alive open a receive window for last time_sync_answer
|
||||
LMIC_sendAlive();
|
||||
}
|
||||
|
||||
} // end of for loop to collect timestamp samples
|
||||
|
||||
// --- time critial part: evaluate timestamps and calculate time ---
|
||||
|
||||
// mask application irq to ensure accurate timing
|
||||
mask_user_IRQ();
|
||||
|
||||
// average time offset over all collected diffs
|
||||
time_offset_ms = 0;
|
||||
|
||||
for (i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
||||
// calculate time diff from collected timestamps and apply
|
||||
// a compensation constant TIME_SYNC_FIXUP for processing time
|
||||
|
||||
time_offset_ms += timesync_timestamp[i][timesync_rx] -
|
||||
timesync_timestamp[i][timesync_tx] + TIME_SYNC_FIXUP;
|
||||
}
|
||||
|
||||
// decrement i for subscription to latest element in timestamp array
|
||||
i--;
|
||||
|
||||
// average time offset over all collected diffs and add gateway time msec
|
||||
time_offset_ms /= TIME_SYNC_SAMPLES;
|
||||
time_offset_ms += timesync_timestamp[i][gwtime_msec];
|
||||
|
||||
// --------- do we need this? ---------
|
||||
// calculate time offset with millisecond precision using LMIC's time base,
|
||||
// since we use LMIC's ostime_t txEnd as tx timestamp.
|
||||
//
|
||||
// time_offset_ms += milliseconds(osticks2ms(os_getTime())) -
|
||||
// milliseconds(millis());
|
||||
// --------- not sure -----------------
|
||||
// calculate absolute time in UTC epoch: take latest time received from
|
||||
// gateway, convert to whole seconds, round to ceil, add fraction seconds
|
||||
|
||||
// Apply calibration const to compensate processing time.
|
||||
time_offset_ms += milliseconds(TIME_SYNC_FIXUP);
|
||||
|
||||
// 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);
|
||||
|
||||
setMyTime(time_to_set, time_to_set_fraction_msec, _lora);
|
||||
setMyTime(timesync_timestamp[i][gwtime_sec] + time_offset_ms / 1000,
|
||||
time_offset_ms % 1000, _lora);
|
||||
|
||||
finish:
|
||||
// end of time critical section: release app irq lock
|
||||
@ -144,19 +128,14 @@ void process_timesync_req(void *taskparameter) {
|
||||
} // infinite while(1)
|
||||
}
|
||||
|
||||
// called from lorawan.cpp after time_sync_req was sent
|
||||
void store_time_sync_req(uint32_t timestamp) {
|
||||
// called from lorawan.cpp
|
||||
void store_timestamp(uint32_t timestamp, timesync_t timestamp_type) {
|
||||
|
||||
// if no timesync handshake is pending then exit
|
||||
if (!timeSyncPending)
|
||||
return;
|
||||
uint8_t seq = time_sync_seqNo % TIME_SYNC_SAMPLES;
|
||||
timesync_timestamp[seq][timestamp_type] = timestamp;
|
||||
|
||||
uint8_t k = time_sync_seqNo % TIME_SYNC_SAMPLES;
|
||||
time_sync_tx[k] += milliseconds(timestamp);
|
||||
|
||||
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);
|
||||
ESP_LOGD(TAG, "[%0.3f] Timesync seq#%d: timestamp(%d) %d stored",
|
||||
millis() / 1000.0, seq, timestamp_type, timestamp);
|
||||
}
|
||||
|
||||
// process timeserver timestamp answer, called by myRxCallback() in lorawan.cpp
|
||||
@ -211,15 +190,18 @@ int recv_timesync_ans(const uint8_t buf[], const uint8_t buf_len) {
|
||||
// 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];
|
||||
|
||||
// construct the timepoint when message was seen on gateway
|
||||
time_sync_rx[k] += seconds(timestamp_sec) + milliseconds(timestamp_msec);
|
||||
// calculate absolute time received from gateway
|
||||
time_t t = timestamp_sec + timestamp_msec / 1000;
|
||||
|
||||
// we guess timepoint is recent if it is newer than code compile date
|
||||
if (timeIsValid(myClock::to_time_t(time_sync_rx[k]))) {
|
||||
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);
|
||||
if (timeIsValid(t)) {
|
||||
ESP_LOGD(TAG, "[%0.3f] Timesync request #%d of %d rcvd at %0.3f",
|
||||
millis() / 1000.0, k + 1, TIME_SYNC_SAMPLES,
|
||||
osticks2ms(os_getTime()) / 1000.0);
|
||||
|
||||
// store time received from gateway
|
||||
store_timestamp(timestamp_sec, gwtime_sec);
|
||||
store_timestamp(timestamp_msec, gwtime_msec);
|
||||
|
||||
// inform processing task
|
||||
xTaskNotify(timeSyncReqTask, seq_no, eSetBits);
|
||||
|
Loading…
Reference in New Issue
Block a user