From 3b6ecf7b36ab7742f216e5fb59a37b0b05d2cd3f Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Sun, 12 Mar 2023 00:31:55 +0100 Subject: [PATCH] reset.cpp/.h code sanitizations --- include/reset.h | 2 +- src/reset.cpp | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/reset.h b/include/reset.h index f16c1c39..05d18ab3 100644 --- a/include/reset.h +++ b/include/reset.h @@ -15,7 +15,7 @@ void reset_rtc_vars(void); void do_reset(bool warmstart); void do_after_reset(void); void enter_deepsleep(uint32_t wakeup_sec, const gpio_num_t wakeup_gpio); -unsigned long long uptime(void); +uint64_t uptime(void); enum runmode_t { RUNMODE_POWERCYCLE, diff --git a/src/reset.cpp b/src/reset.cpp index da405e61..e889506b 100644 --- a/src/reset.cpp +++ b/src/reset.cpp @@ -5,14 +5,14 @@ // Conversion factor for micro seconds to seconds #define uS_TO_S_FACTOR 1000000ULL -// RTC_NOINIT_ATTR -> keep value after a software restart or system crash +// RTC_NOINIT_ATTR -> keeps value after a software restart or system crash RTC_NOINIT_ATTR runmode_t RTC_runmode; RTC_NOINIT_ATTR uint32_t RTC_restarts; -// RTC_DATA_ATTR -> keep values after a wakeup from sleep -RTC_DATA_ATTR struct timeval RTC_sleep_start_time; -RTC_DATA_ATTR unsigned long long RTC_millis = 0; +// RTC_DATA_ATTR -> keeps value after a wakeup from sleep +RTC_DATA_ATTR struct timeval sleep_start_time; +RTC_DATA_ATTR int64_t RTC_millis = 0; -timeval sleep_stop_time; +struct timeval sleep_stop_time; void reset_rtc_vars(void) { RTC_runmode = RUNMODE_POWERCYCLE; @@ -36,7 +36,7 @@ void do_reset(bool warmstart) { void do_after_reset(void) { struct timeval sleep_stop_time; - uint64_t sleep_time_ms; + int64_t sleep_time_ms; // read (and initialize on first run) runtime settings from NVRAM loadConfig(); @@ -62,9 +62,11 @@ void do_after_reset(void) { case RESET_REASON_CORE_DEEP_SLEEP: // calculate time spent in deep sleep gettimeofday(&sleep_stop_time, NULL); - sleep_time_ms = - (sleep_stop_time.tv_sec - RTC_sleep_start_time.tv_sec) * 1000 + - (sleep_stop_time.tv_usec - RTC_sleep_start_time.tv_usec) / 1000; + sleep_time_ms = ((int64_t)sleep_stop_time.tv_sec * 1000000L + + (int64_t)sleep_stop_time.tv_usec - + (int64_t)sleep_start_time.tv_sec * 1000000L - + (int64_t)sleep_start_time.tv_usec) / + 1000LL; RTC_millis += sleep_time_ms; // increment system monotonic time ESP_LOGI(TAG, "Time spent in deep sleep: %llu ms", sleep_time_ms); // do we have a valid time? -> set global variable @@ -180,8 +182,8 @@ void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) { } // time stamp sleep start time and save system monotonic time. Deep sleep. - gettimeofday(&RTC_sleep_start_time, NULL); - RTC_millis += esp_timer_get_time() / 1000; + gettimeofday(&sleep_start_time, NULL); + RTC_millis += esp_timer_get_time() / 1000LL; ESP_LOGI(TAG, "Going to sleep, good bye."); // flush & close sd card, if we have @@ -192,6 +194,6 @@ void enter_deepsleep(uint32_t wakeup_sec, gpio_num_t wakeup_gpio) { esp_deep_sleep_start(); } -unsigned long long uptime() { - return (RTC_millis + esp_timer_get_time() / 1000); +uint64_t uptime() { + return (uint64_t)(RTC_millis + esp_timer_get_time() / 1000LL); } \ No newline at end of file