timeserver (experimental)
This commit is contained in:
parent
bd8718f23f
commit
edb2c822c8
@ -5,7 +5,7 @@
|
||||
#include <Arduino.h>
|
||||
|
||||
// Time functions
|
||||
#include <Time.h>
|
||||
#include "microTime.h"
|
||||
#include <Timezone.h>
|
||||
#include <RtcDateTime.h>
|
||||
#include <Ticker.h>
|
||||
|
@ -1,24 +1,21 @@
|
||||
#ifndef _TIME_SYNC_TIMESERVER_H
|
||||
#define _TIME_SYNC_TIMESERVER_H
|
||||
|
||||
#include <ctime>
|
||||
#include <chrono>
|
||||
#include "globals.h"
|
||||
#include "timesync.h"
|
||||
#include "timekeeper.h"
|
||||
|
||||
#define TIME_SYNC_SAMPLES 3 // number of time requests for averaging
|
||||
#define TIME_SYNC_CYCLE 30 // seconds between two time requests
|
||||
#define TIME_SYNC_TIMEOUT 120 // timeout seconds waiting for timeserver answer
|
||||
#define TIME_SYNC_TRIGGER 1.0f // time deviation threshold triggering time sync
|
||||
#define TIME_SYNC_CYCLE 20 // seconds between two time requests
|
||||
#define TIME_SYNC_TIMEOUT 120 // timeout seconds waiting for timeserver answer
|
||||
#define TIME_SYNC_TRIGGER 1 // time deviation threshold triggering time sync
|
||||
#define TIME_SYNC_FRAME_LENGTH 0x06 // timeserver answer frame length
|
||||
|
||||
typedef struct {
|
||||
uint32_t seconds;
|
||||
uint8_t fractions; // 1/250ths second = 4 milliseconds resolution
|
||||
} time_sync_message_t;
|
||||
|
||||
void send_Servertime_req(void);
|
||||
void recv_Servertime_ans(uint8_t buf[], uint8_t buf_len);
|
||||
void process_Servertime_sync_req(void *taskparameter);
|
||||
void send_timesync_req(void);
|
||||
void recv_timesync_ans(uint8_t buf[], uint8_t buf_len);
|
||||
void process_timesync_req(void *taskparameter);
|
||||
void store_time_sync_req(time_t secs, uint32_t micros);
|
||||
|
||||
#endif
|
@ -175,7 +175,7 @@ void showLoraKeys(void) {
|
||||
|
||||
void onEvent(ev_t ev) {
|
||||
char buff[24] = "";
|
||||
uint32_t now_micros;
|
||||
uint32_t now_micros = 0;
|
||||
|
||||
switch (ev) {
|
||||
|
||||
@ -238,21 +238,27 @@ void onEvent(ev_t ev) {
|
||||
: PSTR("TX_COMPLETE"));
|
||||
sprintf(display_line6, " "); // clear previous lmic status
|
||||
|
||||
if (LMIC.dataLen) {
|
||||
if (LMIC.dataLen) { // did we receive data -> display info
|
||||
ESP_LOGI(TAG, "Received %d bytes of payload, RSSI -%d SNR %d",
|
||||
LMIC.dataLen, LMIC.rssi, LMIC.snr / 4);
|
||||
sprintf(display_line6, "RSSI -%d SNR %d", LMIC.rssi, LMIC.snr / 4);
|
||||
|
||||
// check if this is a timesync answer, then call timesync processor
|
||||
if (LMIC.txrxFlags & TXRX_PORT) { // FPort -> use to switch
|
||||
switch (LMIC.frame[LMIC.dataBeg - 1]) {
|
||||
#if (TIME_SYNC_TIMESERVER)
|
||||
if ((LMIC.txrxFlags & TXRX_PORT) &&
|
||||
(LMIC.frame[LMIC.dataBeg - 1] == TIMEPORT))
|
||||
recv_Servertime_ans(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
|
||||
case TIMEPORT: // timesync answer -> call timesync processor
|
||||
recv_timesync_ans(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
|
||||
break;
|
||||
#endif
|
||||
// check if this an opcode, then call rcommand interpreter
|
||||
if ((LMIC.txrxFlags & TXRX_PORT) &&
|
||||
(LMIC.frame[LMIC.dataBeg - 1] == RCMDPORT))
|
||||
rcommand(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
|
||||
case RCMDPORT: // opcode -> call rcommand interpreter
|
||||
rcommand(LMIC.frame + LMIC.dataBeg, LMIC.dataLen);
|
||||
break;
|
||||
default: // unknown port -> display info
|
||||
ESP_LOGI(TAG, "Received data on unsupported port #%d",
|
||||
LMIC.frame[LMIC.dataBeg - 1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -45,7 +45,7 @@ time_t timeProvider(void) {
|
||||
|
||||
// kick off asychronous Lora timeserver timesync if we have
|
||||
#if (TIME_SYNC_TIMESERVER)
|
||||
send_Servertime_req();
|
||||
send_timesync_req();
|
||||
// kick off asychronous lora network sync if we have
|
||||
#elif (TIME_SYNC_LORAWAN)
|
||||
LMIC_requestNetworkTime(user_request_network_time_callback, &userUTCTime);
|
||||
@ -116,7 +116,7 @@ void timepulse_start(void) {
|
||||
void IRAM_ATTR CLOCKIRQ(void) {
|
||||
|
||||
BaseType_t xHigherPriorityTaskWoken;
|
||||
SyncToPPS(); // calibrates UTC systime, see Time.h
|
||||
SyncToPPS(); // calibrates UTC systime, see microTime.h
|
||||
xHigherPriorityTaskWoken = pdFALSE;
|
||||
|
||||
if (ClockTask != NULL)
|
||||
@ -149,7 +149,7 @@ time_t compiledUTC(void) {
|
||||
time_t tmConvert(uint16_t YYYY, uint8_t MM, uint8_t DD, uint8_t hh, uint8_t mm,
|
||||
uint8_t ss) {
|
||||
tmElements_t tm;
|
||||
tm.Year = CalendarYrToTm(YYYY); // year offset from 1970 in time.h
|
||||
tm.Year = CalendarYrToTm(YYYY); // year offset from 1970 in microTime.h
|
||||
tm.Month = MM;
|
||||
tm.Day = DD;
|
||||
tm.Hour = hh;
|
||||
|
137
src/timesync.cpp
137
src/timesync.cpp
@ -17,60 +17,67 @@ algorithm in applications without granted license by the patent holder.
|
||||
static const char TAG[] = __FILE__;
|
||||
|
||||
TaskHandle_t timeSyncReqTask;
|
||||
time_sync_message_t time_sync_messages[TIME_SYNC_SAMPLES] = {0},
|
||||
time_sync_answers[TIME_SYNC_SAMPLES] = {0};
|
||||
|
||||
static uint8_t time_sync_seqNo = 0;
|
||||
static bool time_sync_pending = false;
|
||||
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;
|
||||
// 32bit millisec resolution from epoch until year 2038
|
||||
myClock_timepoint time_sync_tx[TIME_SYNC_SAMPLES];
|
||||
myClock_timepoint time_sync_rx[TIME_SYNC_SAMPLES];
|
||||
|
||||
// send time request message
|
||||
void send_Servertime_req() {
|
||||
void send_timesync_req() {
|
||||
|
||||
// if a timesync handshake is pending then exit
|
||||
if (time_sync_pending) {
|
||||
if (lora_time_sync_pending) {
|
||||
ESP_LOGI(TAG, "Timeserver sync request already pending");
|
||||
return;
|
||||
} else {
|
||||
ESP_LOGI(TAG, "Timeserver sync request started");
|
||||
|
||||
time_sync_pending = true;
|
||||
lora_time_sync_pending = true;
|
||||
|
||||
// clear timestamp array
|
||||
for (uint8_t i = 0; i <= TIME_SYNC_SAMPLES + 1; i++) {
|
||||
time_sync_messages[i].seconds = time_sync_answers[i].seconds =
|
||||
time_sync_messages[i].fractions = time_sync_answers[i].fractions = 0;
|
||||
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
||||
time_sync_tx[i] = time_sync_rx[i] = myClock_timepoint(); // set to epoch
|
||||
}
|
||||
|
||||
// kick off temporary task for timeserver handshake processing
|
||||
if (!timeSyncReqTask)
|
||||
xTaskCreatePinnedToCore(process_Servertime_sync_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
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
// process timeserver timestamp response, called from rcommand.cpp
|
||||
void recv_Servertime_ans(uint8_t buf[], uint8_t buf_len) {
|
||||
// process timeserver timestamp answer, called from lorawan.cpp
|
||||
void recv_timesync_ans(uint8_t buf[], uint8_t buf_len) {
|
||||
|
||||
// if no timesync handshake is pending or invalid buffer then exit
|
||||
if ((!time_sync_pending) || (buf_len != TIME_SYNC_FRAME_LENGTH))
|
||||
// if no timesync handshake is pending or spurious buffer then exit
|
||||
if ((!lora_time_sync_pending) || (buf_len != TIME_SYNC_FRAME_LENGTH))
|
||||
return;
|
||||
|
||||
uint8_t seq_no = buf[0], k = seq_no % TIME_SYNC_SAMPLES;
|
||||
uint32_t timestamp_sec = 0;
|
||||
uint16_t timestamp_msec = 4 * buf[5]; // convert 1/250th sec fractions to ms
|
||||
uint32_t timestamp_sec = 0, tmp_sec = 0;
|
||||
|
||||
for (uint8_t i = 1; i <= 4; i++) {
|
||||
time_sync_answers[k].seconds = (timestamp_sec <<= 8) |= buf[i];
|
||||
timestamp_sec = (tmp_sec <<= 8) |= buf[i];
|
||||
}
|
||||
time_sync_answers[k].fractions = buf[5];
|
||||
|
||||
ESP_LOGD(TAG, "Timeserver answer:");
|
||||
time_sync_rx[k] += std::chrono::seconds(timestamp_sec) +
|
||||
std::chrono::milliseconds(timestamp_msec);
|
||||
|
||||
ESP_LOGD(TAG, "ans.sec(%d)=%d / ans.ms(%d)=%d", k,
|
||||
time_sync_answers[k].seconds, k, time_sync_answers[k].fractions);
|
||||
ESP_LOGD(TAG, "Timesync answer #%d rcvd at %d", seq_no,
|
||||
myClock::to_time_t(time_sync_rx[k]));
|
||||
|
||||
// inform processing task
|
||||
if (timeSyncReqTask)
|
||||
@ -78,15 +85,15 @@ void recv_Servertime_ans(uint8_t buf[], uint8_t buf_len) {
|
||||
}
|
||||
|
||||
// task for sending time sync requests
|
||||
void process_Servertime_sync_req(void *taskparameter) {
|
||||
void process_timesync_req(void *taskparameter) {
|
||||
|
||||
time_t t = 0, time_to_set = 0;
|
||||
uint32_t seq_no = 0, k = 0;
|
||||
int time_diff_frac = 0, time_diff_ms = 0;
|
||||
long time_diff_sec = 0, time_offset = 0;
|
||||
time_t time_to_set = 0;
|
||||
uint8_t k = 0, i = 0;
|
||||
uint32_t seq_no = 0;
|
||||
auto time_offset = myClock_msecTick::zero();
|
||||
|
||||
// enqueue timestamp samples in lora sendqueue
|
||||
for (uint8_t i = 1; i <= TIME_SYNC_SAMPLES; i++) {
|
||||
for (uint8_t i = 0; i < TIME_SYNC_SAMPLES; i++) {
|
||||
|
||||
// wrap around seqNo 0 .. 254
|
||||
time_sync_seqNo = (time_sync_seqNo >= 255) ? 0 : time_sync_seqNo + 1;
|
||||
@ -96,55 +103,48 @@ void process_Servertime_sync_req(void *taskparameter) {
|
||||
payload.addByte(time_sync_seqNo);
|
||||
SendPayload(TIMEPORT, prio_high);
|
||||
|
||||
/* -> do we really need this? maybe for SF9 up?
|
||||
// send dummy packet to trigger receive answer
|
||||
payload.reset();
|
||||
payload.addByte(0x99); // flush
|
||||
SendPayload(RCMDPORT, prio_low); // to open receive slot for answer
|
||||
*/
|
||||
|
||||
// process answer
|
||||
if ((xTaskNotifyWait(0x00, ULONG_MAX, &seq_no,
|
||||
TIME_SYNC_TIMEOUT * 1000 / portTICK_PERIOD_MS) ==
|
||||
pdFALSE) ||
|
||||
pdMS_TO_TICKS(TIME_SYNC_TIMEOUT * 1000)) == pdFALSE) ||
|
||||
(seq_no != time_sync_seqNo)) {
|
||||
|
||||
ESP_LOGW(TAG, "Timeserver handshake failed");
|
||||
goto finish;
|
||||
} // no valid sequence received before timeout
|
||||
|
||||
else { // calculate time diff from set of collected timestamps
|
||||
|
||||
else { // calculate time diff from collected timestamps
|
||||
k = seq_no % TIME_SYNC_SAMPLES;
|
||||
|
||||
time_diff_sec +=
|
||||
((time_sync_messages[k].seconds - time_sync_answers[k].seconds) / TIME_SYNC_SAMPLES);
|
||||
auto t_tx = std::chrono::time_point_cast<std::chrono::milliseconds>(
|
||||
time_sync_tx[k]); // timepoint node after TX_completed
|
||||
auto t_rx = std::chrono::time_point_cast<std::chrono::milliseconds>(
|
||||
time_sync_rx[k]); // timepoint when message was seen on gateway
|
||||
|
||||
time_diff_frac +=
|
||||
((time_sync_messages[k].fractions - time_sync_answers[k].fractions) / TIME_SYNC_SAMPLES);
|
||||
time_offset += t_rx - t_tx;
|
||||
|
||||
ESP_LOGD(TAG, "time_diff_sec=%d / time_diff_frac=%d", time_diff_sec,
|
||||
time_diff_frac);
|
||||
if (i < TIME_SYNC_SAMPLES - 1)
|
||||
vTaskDelay(pdMS_TO_TICKS(TIME_SYNC_CYCLE * 1000));
|
||||
}
|
||||
} // for
|
||||
|
||||
// calculate time offset and set time if necessary
|
||||
time_diff_ms = 4 * time_diff_frac;
|
||||
time_offset = (time_diff_sec + (long) (time_diff_ms / 1000));
|
||||
// calculate time offset from collected diffs and set time if necessary
|
||||
time_offset /= TIME_SYNC_SAMPLES;
|
||||
ESP_LOGD(TAG, "Avg time diff: %lldms", time_offset.count());
|
||||
|
||||
ESP_LOGD(TAG, "Timesync time offset=%d", time_offset);
|
||||
if (abs(time_offset.count()) >= TIME_SYNC_TRIGGER) {
|
||||
|
||||
t = now();
|
||||
|
||||
if (labs(time_offset) >= (t + TIME_SYNC_TRIGGER)) {
|
||||
/*
|
||||
// wait until top of second
|
||||
if (time_diff_ms > 0) // clock is fast
|
||||
if (time_offset_ms > 0) // clock is fast
|
||||
vTaskDelay(pdMS_TO_TICKS(time_diff_ms));
|
||||
else if (time_diff_ms < 0) // clock is slow
|
||||
vTaskDelay(pdMS_TO_TICKS(1000 + time_diff_ms));
|
||||
else if (time_offset_ms < 0) // clock is slow
|
||||
vTaskDelay(pdMS_TO_TICKS(1000 + time_offset_ms));
|
||||
|
||||
time_diff_sec++;
|
||||
time_to_set = t - time_t(time_diff_sec);
|
||||
ESP_LOGD(TAG, "Now()=%d, Time to set = %d", t, time_to_set);
|
||||
time_to_set = t - time_t(time_offset_sec + 1);
|
||||
*/
|
||||
|
||||
time_t time_to_set = myClock::to_time_t(myClock::now() + time_offset);
|
||||
ESP_LOGD(TAG, "New UTC epoch time: %d", time_to_set);
|
||||
|
||||
// adjust system time
|
||||
if (timeIsValid(time_to_set)) {
|
||||
@ -161,20 +161,21 @@ void process_Servertime_sync_req(void *taskparameter) {
|
||||
|
||||
finish:
|
||||
|
||||
time_sync_pending = false;
|
||||
lora_time_sync_pending = false;
|
||||
timeSyncReqTask = NULL;
|
||||
vTaskDelete(NULL); // end task
|
||||
}
|
||||
|
||||
// called from lorawan.cpp after time_sync_req was sent
|
||||
void store_time_sync_req(time_t secs, uint32_t micros) {
|
||||
void store_time_sync_req(time_t t_sec, uint32_t t_microsec) {
|
||||
|
||||
uint8_t k = time_sync_seqNo % TIME_SYNC_SAMPLES;
|
||||
|
||||
time_sync_messages[k].seconds = (uint32_t) secs;
|
||||
time_sync_messages[k].fractions = (uint8_t) (micros / 4000); // 4ms resolution
|
||||
time_sync_tx[k] +=
|
||||
std::chrono::seconds(t_sec) + std::chrono::microseconds(t_microsec);
|
||||
|
||||
ESP_LOGD(TAG, "Timeserver request #%d sent at %d.%03d", time_sync_seqNo,
|
||||
time_sync_messages[k].seconds, time_sync_messages[k].fractions);
|
||||
ESP_LOGD(TAG, "Timesync request #%d sent at %d", time_sync_seqNo,
|
||||
myClock::to_time_t(time_sync_tx[k]));
|
||||
}
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue
Block a user