timesync.cpp: minor code beautifications
This commit is contained in:
parent
fbfb03ef81
commit
7ba2186ac1
@ -20,13 +20,15 @@ You may use timesync option 2 if you do not want or cannot accept this.
|
|||||||
// Local logging tag
|
// Local logging tag
|
||||||
static const char TAG[] = __FILE__;
|
static const char TAG[] = __FILE__;
|
||||||
|
|
||||||
|
static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO);
|
||||||
|
#define WRAP(v, top) (v++ > top ? 0 : v)
|
||||||
|
|
||||||
// timesync option 1: use external timeserver (for LoRAWAN < 1.0.3)
|
// timesync option 1: use external timeserver (for LoRAWAN < 1.0.3)
|
||||||
|
|
||||||
#if (TIME_SYNC_LORASERVER) && (HAS_LORA)
|
#if (TIME_SYNC_LORASERVER) && (HAS_LORA)
|
||||||
|
|
||||||
static TaskHandle_t timeSyncReqTask = NULL;
|
static TaskHandle_t timeSyncReqTask = NULL;
|
||||||
static bool timeSyncPending = false;
|
static bool timeSyncPending = false;
|
||||||
static uint8_t time_sync_seqNo = (uint8_t)random(TIMEREQUEST_MAX_SEQNO);
|
|
||||||
static uint8_t sample_idx = 0;
|
static uint8_t sample_idx = 0;
|
||||||
static uint32_t timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0};
|
static uint32_t timesync_timestamp[TIME_SYNC_SAMPLES][no_of_timestamps] = {0};
|
||||||
|
|
||||||
@ -89,10 +91,7 @@ void IRAM_ATTR process_timesync_req(void *taskparameter) {
|
|||||||
timesync_timestamp[sample_idx][timesync_tx];
|
timesync_timestamp[sample_idx][timesync_tx];
|
||||||
|
|
||||||
// increment and maybe wrap around seqNo, keeping it in time port range
|
// increment and maybe wrap around seqNo, keeping it in time port range
|
||||||
time_sync_seqNo++;
|
WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO);
|
||||||
if (time_sync_seqNo > TIMEREQUEST_MAX_SEQNO) {
|
|
||||||
time_sync_seqNo = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
// increment index for timestamp array
|
// increment index for timestamp array
|
||||||
sample_idx++;
|
sample_idx++;
|
||||||
@ -243,12 +242,15 @@ void timesync_init() {
|
|||||||
|
|
||||||
// send time request message
|
// send time request message
|
||||||
void send_timesync_req(void) {
|
void send_timesync_req(void) {
|
||||||
LMIC_requestNetworkTime(process_timesync_req, NULL);
|
LMIC_requestNetworkTime(process_timesync_req, &time_sync_seqNo);
|
||||||
}
|
}
|
||||||
|
|
||||||
void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) {
|
void IRAM_ATTR process_timesync_req(void *pUserData, int flagSuccess) {
|
||||||
// Explicit conversion from void* to uint32_t* to avoid compiler errors
|
// Explicit conversion from void* to uint8_t* to avoid compiler errors
|
||||||
time_t *pUserUTCTime = (time_t *)pVoidUserUTCTime;
|
uint8_t *seqNo = (uint8_t *)pUserData;
|
||||||
|
|
||||||
|
// mask application irq to ensure accurate timing
|
||||||
|
mask_user_IRQ();
|
||||||
|
|
||||||
// A struct that will be populated by LMIC_getNetworkTimeReference.
|
// A struct that will be populated by LMIC_getNetworkTimeReference.
|
||||||
// It contains the following fields:
|
// It contains the following fields:
|
||||||
@ -258,32 +260,35 @@ void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) {
|
|||||||
// the gateway received the time request
|
// the gateway received the time request
|
||||||
lmic_time_reference_t lmicTime;
|
lmic_time_reference_t lmicTime;
|
||||||
|
|
||||||
if (flagSuccess != 1) {
|
uint32_t networkTimeSec;
|
||||||
|
uint16_t requestDelaymSec;
|
||||||
|
|
||||||
|
if ((flagSuccess != 1) || (time_sync_seqNo != *seqNo)) {
|
||||||
ESP_LOGW(TAG, "LoRaWAN network did not answer time request");
|
ESP_LOGW(TAG, "LoRaWAN network did not answer time request");
|
||||||
return;
|
goto Finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Populate lmic_time_reference
|
// Populate lmic_time_reference
|
||||||
flagSuccess = LMIC_getNetworkTimeReference(&lmicTime);
|
flagSuccess = LMIC_getNetworkTimeReference(&lmicTime);
|
||||||
if (flagSuccess != 1) {
|
if (flagSuccess != 1) {
|
||||||
ESP_LOGW(TAG, "LoRaWAN time request failed");
|
ESP_LOGW(TAG, "LoRaWAN time request failed");
|
||||||
return;
|
goto Finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
// mask application irq to ensure accurate timing
|
// Calculate UTCTime, considering the difference between GPS and UTC time
|
||||||
mask_user_IRQ();
|
networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF;
|
||||||
|
|
||||||
// Update networkUTCTime, considering the difference between GPS and UTC time
|
|
||||||
uint32_t networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF;
|
|
||||||
// Add delay between the instant the time was transmitted and the current time
|
// Add delay between the instant the time was transmitted and the current time
|
||||||
uint16_t requestDelaymSec =
|
requestDelaymSec = osticks2ms(os_getTime() - lmicTime.tLocal);
|
||||||
osticks2ms(os_getTime() - lmicTime.tLocal);
|
|
||||||
|
|
||||||
// Update system time with time read from the network
|
// Update system time with time read from the network
|
||||||
setMyTime(networkTimeSec, requestDelaymSec, _lora);
|
setMyTime(networkTimeSec, requestDelaymSec, _lora);
|
||||||
|
|
||||||
|
Finish:
|
||||||
|
|
||||||
// end of time critical section: release app irq lock
|
// end of time critical section: release app irq lock
|
||||||
unmask_user_IRQ();
|
unmask_user_IRQ();
|
||||||
|
|
||||||
|
// increment and maybe wrap around seqNo, keeping it in time port range
|
||||||
|
WRAP(time_sync_seqNo, TIMEREQUEST_MAX_SEQNO);
|
||||||
|
|
||||||
} // user_request_network_time_callback
|
} // user_request_network_time_callback
|
||||||
#endif // TIME_SYNC_LORAWAN
|
#endif // TIME_SYNC_LORAWAN
|
Loading…
Reference in New Issue
Block a user