Skip to content

Commit

Permalink
bugfix reagard leap seconds in network time sync
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberman54 committed Jul 6, 2021
1 parent f876edf commit 335d646
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
1 change: 1 addition & 0 deletions include/timesync.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define TIME_SYNC_MAX_SEQNO 0xfe // threshold for wrap around time_sync_seqNo
#define TIME_SYNC_END_FLAG (TIME_SYNC_MAX_SEQNO + 1) // end of handshake marker
#define GPS_UTC_DIFF 315964800 // seconds diff between gps and utc epoch
#define LEAP_SECS_SINCE_GPSEPOCH 18 // state of 2021

enum timesync_t {
timesync_tx,
Expand Down
2 changes: 1 addition & 1 deletion src/gpsread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ time_t get_gpstime(uint16_t *msec) {
t = makeTime(tm);

ESP_LOGD(TAG, "GPS date/time: %s",
UTC.dateTime(t, "d.M Y H:i:s T").c_str());
UTC.dateTime(t, "d.M Y H:i:s.v T").c_str());

// add protocol delay with millisecond precision
t += delay_ms / 1000 - 1; // whole seconds
Expand Down
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ void setup() {
// set time zone to user value from paxcounter.conf
#ifdef TIME_SYNC_TIMEZONE
myTZ.setPosix(TIME_SYNC_TIMEZONE);
ESP_LOGD(TAG, "Timezone set to %s", myTZ.getPosix().c_str());
#endif

// hash 6 byte device MAC to 4 byte clientID
Expand Down
10 changes: 6 additions & 4 deletions src/timesync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {
time_offset_ms %= 1000;

ESP_LOGD(TAG, "LORA date/time: %s",
myTZ.dateTime(time_offset_sec, "d.M Y H:i:s T").c_str());
myTZ.dateTime(time_offset_sec, "d.M Y H:i:s.v T").c_str());

setMyTime(time_offset_sec, time_offset_ms, _lora);

Expand All @@ -168,7 +168,7 @@ void IRAM_ATTR timesync_processReq(void *taskparameter) {

// store incoming timestamps
void timesync_store(uint32_t timestamp, timesync_t timestamp_type) {
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", _seconds(), time_sync_seqNo,
ESP_LOGD(TAG, "[%0.3f] seq#%d[%d]: t%d=%d", _seconds(), time_sync_seqNo - 1,
sample_idx, timestamp_type, timestamp);
timesync_timestamp[sample_idx][timestamp_type] = timestamp;
}
Expand Down Expand Up @@ -239,14 +239,16 @@ void IRAM_ATTR timesync_serverAnswer(void *pUserData, int flag) {
}

// Populate lmic_time_reference
if ((LMIC_getNetworkTimeReference(&lmicTime)) != 1) {
flag = LMIC_getNetworkTimeReference(&lmicTime);
if (flag != 1) {
ESP_LOGW(TAG, "[%0.3f] Network time request failed", _seconds());
goto Exit;
}

// Calculate UTCTime, considering the difference between GPS and UTC time
// epoch, and the leap seconds
timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF;
timestamp_sec = lmicTime.tNetwork + GPS_UTC_DIFF - LEAP_SECS_SINCE_GPSEPOCH;
ESP_LOGD(TAG, "lmicTime.tNetwork = %d", timestamp_sec);

// Add the delay between the instant the time was transmitted and
// the current time
Expand Down

0 comments on commit 335d646

Please sign in to comment.