diff --git a/include/cyclic.h b/include/cyclic.h index 89ccb240..829f9610 100644 --- a/include/cyclic.h +++ b/include/cyclic.h @@ -5,15 +5,12 @@ #include "senddata.h" #include "rcommand.h" #include "spislave.h" -#include "timekeeper.h" #include #ifdef HAS_BME #include "bme680mems.h" #endif - - void doHousekeeping(void); uint64_t uptime(void); void reset_counters(void); diff --git a/include/lorawan.h b/include/lorawan.h index d02e8931..ddc2fc41 100644 --- a/include/lorawan.h +++ b/include/lorawan.h @@ -35,7 +35,4 @@ void lora_queuereset(void); void lora_housekeeping(void); void user_request_network_time_callback(void *pVoidUserUTCTime, int flagSuccess); -time_t set_loratime(time_t t); -time_t get_loratime(void); - #endif \ No newline at end of file diff --git a/include/timekeeper.h b/include/timekeeper.h index 2b36bbf2..198d8987 100644 --- a/include/timekeeper.h +++ b/include/timekeeper.h @@ -18,9 +18,9 @@ extern const char timeSetSymbols[]; void IRAM_ATTR CLOCKIRQ(void); void clock_init(void); void clock_loop(void *pvParameters); -void time_sync(void); +time_t time_sync(void); void timepulse_start(void); -uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller); +time_t syncTime(getExternalTime getTimeFunction, timesource_t const caller); uint8_t timepulse_init(void); uint8_t TimeIsValid(time_t const t); time_t syncProvider_CB(void); diff --git a/src/cyclic.cpp b/src/cyclic.cpp index 021ea8d6..3d170eb1 100644 --- a/src/cyclic.cpp +++ b/src/cyclic.cpp @@ -19,7 +19,6 @@ void doHousekeeping() { spi_housekeeping(); lora_housekeeping(); - time_sync(); // task storage debugging // ESP_LOGD(TAG, "IRQhandler %d bytes left | Taskstate = %d", diff --git a/src/display.cpp b/src/display.cpp index f0c556d6..d5e2b382 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -132,7 +132,7 @@ void refreshtheDisplay() { uint8_t msgWaiting; char timeState, buff[16]; - time_t t; + time_t t = myTZ.toLocal(now()); // note: call now() here *before* locking mutex! // block i2c bus access if (I2C_MUTEX_LOCK()) { @@ -216,8 +216,7 @@ void refreshtheDisplay() { // update LoRa status display (line 6) u8x8.printf("%-16s", display_line6); #else // we want a systime display instead LoRa status - t = myTZ.toLocal(now()); - timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource]; + timeState = TimePulseTick ? ' ' : timeSetSymbols[timeSource]; TimePulseTick = false; u8x8.printf("%02d:%02d:%02d%c %2d.%3s", hour(t), minute(t), second(t), timeState, day(t), printmonth[month(t)]); diff --git a/src/lorawan.cpp b/src/lorawan.cpp index b4067cd9..02503aba 100644 --- a/src/lorawan.cpp +++ b/src/lorawan.cpp @@ -472,19 +472,11 @@ void user_request_network_time_callback(void *pVoidUserUTCTime, *pUserUTCTime += requestDelaySec; // Update system time with time read from the network - set_loratime(*pUserUTCTime); // store time in time sync provider function - if (syncTime(get_loratime, _lora)) + if (TimeIsValid(*pUserUTCTime)) { + setTime(*pUserUTCTime); ESP_LOGI(TAG, "Received recent time from LoRa"); + } else ESP_LOGI(TAG, "Invalid time received from LoRa"); #endif // HAS_LORA -} // user_request_network_time_callback - -time_t set_loratime(time_t t) { - static time_t loratime = 0; // stores time for retrieval - if (t > loratime) - loratime = t; // store time if it is recent - return loratime; -} - -time_t get_loratime(void) { return set_loratime(0); } +} // user_request_network_time_callback \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 27c6fa3c..a2a807ab 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -363,9 +363,8 @@ void setup() { timepulse_start(); #ifdef TIME_SYNC_INTERVAL setSyncInterval(TIME_SYNC_INTERVAL * 60); // controls timeStatus() via Time.h - setSyncProvider(syncProvider_CB); // is called by Time.h + setSyncProvider(time_sync); // is called by Time.h #endif - time_sync(); // sync time // start wifi in monitor mode and start channel rotation timer ESP_LOGI(TAG, "Starting Wifi..."); diff --git a/src/timekeeper.cpp b/src/timekeeper.cpp index 316835ec..712fd09b 100644 --- a/src/timekeeper.cpp +++ b/src/timekeeper.cpp @@ -8,22 +8,23 @@ const char timeSetSymbols[] = {'G', 'R', 'L', '?'}; getExternalTime TimeSourcePtr; // pointer to time source function -void time_sync() { +time_t time_sync() { // check synchonization of systime, called by cyclic.cpp + time_t t = 0; + #ifdef TIME_SYNC_INTERVAL - if (timeStatus() == timeSet) // timeStatus() is flipped in Time.h - return; - #ifdef HAS_GPS - if (syncTime(get_gpstime, _gps)) - return; // attempt sync with GPS time + t = syncTime(get_gpstime, _gps); + if (t) + return t; // attempt sync with GPS time #endif // no GPS -> fallback to RTC time while trying lora sync #ifdef HAS_RTC - if (!syncTime(get_rtctime, _rtc)) // sync with RTC time + t = syncTime(get_rtctime, _rtc); // sync with RTC time + if (!t) ESP_LOGW(TAG, "no confident RTC time"); #endif @@ -33,26 +34,25 @@ void time_sync() { #endif #endif // TIME_SYNC_INTERVAL + + return t; } // time_sync() // sync time on start of next second from GPS or RTC -uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) { +time_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) { - TimeSourcePtr = getTimeFunction; time_t t; - + TimeSourcePtr = getTimeFunction; if (!TimeSourcePtr) goto error; - if ((caller == _gps || caller == _rtc)) // ticking timesource? - xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // then wait on pps + xSemaphoreTake(TimePulse, pdMS_TO_TICKS(1000)); // wait for pps t = TimeSourcePtr(); // get time from given timesource if (TimeIsValid(t)) { if (caller == _gps) // gps time concerns past second t++; - setTime(t); // flips timeStatus() in Time.h timeSource = caller; ESP_LOGD(TAG, "Time source %c set time to %02d:%02d:%02d", timeSetSymbols[timeSource], hour(t), minute(t), second(t)); @@ -62,7 +62,7 @@ uint8_t syncTime(getExternalTime getTimeFunction, timesource_t const caller) { set_rtctime(t); #endif - return 1; // success + return t; // success } error: @@ -72,14 +72,6 @@ error: } // syncTime() - -// callback function called by Time.h in interval set in main.cpp -time_t syncProvider_CB(void) { - timeSource = _unsynced; - return 0; -} - - // helper function to setup a pulse per second for time synchronisation uint8_t timepulse_init() {