Skip to content

Commit

Permalink
Merge pull request #565 from cyberman54/development
Browse files Browse the repository at this point in the history
timesync.cpp: minor sanitizations
  • Loading branch information
cyberman54 authored Mar 4, 2020
2 parents d2c120b + fbfb03e commit b2eafa0
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/timesync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,9 @@ void timesync_init() {

#if (TIME_SYNC_LORAWAN) && (HAS_LORA)

static time_t networkUTCTime;

// send time request message
void send_timesync_req(void) {
LMIC_requestNetworkTime(process_timesync_req, &networkUTCTime);
LMIC_requestNetworkTime(process_timesync_req, NULL);
}

void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) {
Expand All @@ -258,15 +256,15 @@ void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) {
// request was sent to the gateway, and
// - tNetwork: the seconds between the GPS epoch and the time
// the gateway received the time request
lmic_time_reference_t lmicTimeReference;
lmic_time_reference_t lmicTime;

if (flagSuccess != 1) {
ESP_LOGW(TAG, "LoRaWAN network did not answer time request");
return;
}

// Populate lmic_time_reference
flagSuccess = LMIC_getNetworkTimeReference(&lmicTimeReference);
flagSuccess = LMIC_getNetworkTimeReference(&lmicTime);
if (flagSuccess != 1) {
ESP_LOGW(TAG, "LoRaWAN time request failed");
return;
Expand All @@ -276,13 +274,13 @@ void IRAM_ATTR process_timesync_req(void *pVoidUserUTCTime, int flagSuccess) {
mask_user_IRQ();

// Update networkUTCTime, considering the difference between GPS and UTC time
*pUserUTCTime = lmicTimeReference.tNetwork + GPS_UTC_DIFF;
uint32_t networkTimeSec = lmicTime.tNetwork + GPS_UTC_DIFF;
// Add delay between the instant the time was transmitted and the current time
uint16_t requestDelaymSec =
osticks2ms(os_getTime() - lmicTimeReference.tLocal);
osticks2ms(os_getTime() - lmicTime.tLocal);

// Update system time with time read from the network
setMyTime(*pUserUTCTime, requestDelaymSec, _lora);
setMyTime(networkTimeSec, requestDelaymSec, _lora);

// end of time critical section: release app irq lock
unmask_user_IRQ();
Expand Down

0 comments on commit b2eafa0

Please sign in to comment.