diff --git a/components/time/CMakeLists.txt b/components/time/CMakeLists.txt index a13b8c8..628bf35 100644 --- a/components/time/CMakeLists.txt +++ b/components/time/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "time_manager.c" "time_sntp.c" + SRCS "time_manager.c" "time_sntp.c" "time_storage.c" INCLUDE_DIRS "." REQUIRES console_wrapper esp_timer fatal lwip nvs_flash settings ) diff --git a/components/time/time_manager.c b/components/time/time_manager.c index fc74cb8..ea218d1 100644 --- a/components/time/time_manager.c +++ b/components/time/time_manager.c @@ -8,21 +8,16 @@ #include "console.h" #include "settings.h" #include "time_sntp.h" +#include "time_storage.h" #include "esp_log.h" #include "esp_timer.h" #include "fatal.h" -#include "freertos/FreeRTOS.h" -#include "freertos/task.h" -#include "nvs_flash.h" #include #include #define TAG "Time" -#define NVS_NAMESPACE "time" -#define TIMESTAMP_KEY "timestamp" - #define TM_YEAR_OFFSET 1900 #define TM_MONTH_OFFSET 1 @@ -39,29 +34,6 @@ static void handle_timezone_update(const char *timezone) time_sntp_restart(); } -static int store_time(void) -{ - esp_err_t error; - - nvs_handle_t nvs; - error = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs); - if (error != ESP_OK) { - ESP_LOGE(TAG, "Error opening NVS: %04x", error); - return 1; - } - - struct timeval tv; - gettimeofday(&tv, NULL); - error = nvs_set_u64(nvs, TIMESTAMP_KEY, tv.tv_sec); - if (error == ESP_OK) - ESP_LOGI(TAG, "Time stored"); - else - ESP_LOGE(TAG, "Error storing time: %04x", error); - - nvs_close(nvs); - return error == ESP_OK ? 0 : 1; -} - static void run_callbacks(void *arg) { const Time time = get_time(); @@ -77,7 +49,8 @@ static int time_command_func(int argc, char **argv) return 0; } else if (argc == 2) { if (strcmp(argv[1], "store") == 0) { - return store_time(); + time_storage_save(); + return 0; } else { Time time; const int result @@ -123,60 +96,20 @@ static int date_command_func(int argc, char **argv) } } -static void time_saver_func(void *arg) -{ - (void)arg; - const TickType_t delay = CONFIG_TIME_SAVE_PERIOD_MS / portTICK_PERIOD_MS; - while (1) { - (void)store_time(); - vTaskDelay(delay); - } -} - void time_manager_init(void) { + esp_err_t error; + callback_count = 0; time_sntp_init(); + time_storage_init(); char timezone[SETTINGS_MAX_VALUE_SIZE]; (void)settings_get_timezone(timezone, SETTINGS_MAX_VALUE_SIZE); handle_timezone_update(timezone); settings_add_timezone_callback(&handle_timezone_update); - console_register( - "time", "Get, set or store the time", "time [HH:MM] OR time store", - time_command_func); - console_register( - "date", "Get or set the date, or get the day of the week", - "date [yyyy-mm-dd] OR date week-day", date_command_func); - - // Attempt to load time from storage - esp_err_t error; - nvs_handle_t nvs; - error = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs); - if (error == ESP_OK) { - uint64_t timestamp; - error = nvs_get_u64(nvs, TIMESTAMP_KEY, ×tamp); - if (error == ESP_OK) { - struct timeval tv = { .tv_sec = (time_t)timestamp }; - settimeofday(&tv, NULL); - } else { - if (error != ESP_ERR_NVS_NOT_FOUND) - ESP_LOGE(TAG, "Error getting stored time: %04x", error); - } - nvs_close(nvs); - } else { - if (error != ESP_ERR_NVS_NOT_FOUND) - ESP_LOGE(TAG, "Error opening NVS: %04x", error); - } - - // Start task to save time to storage - (void)xTaskCreate( - &time_saver_func, "time saver", CONFIG_DEFAULT_TASK_STACK, NULL, 1, - NULL); - - // Create and start timer esp_timer_handle_t update_timer; const esp_timer_create_args_t update_timer_config = { .callback = &run_callbacks, @@ -193,6 +126,13 @@ void time_manager_init(void) ESP_LOGE(TAG, "Error starting update timer: %04x", error); FATAL(); } + + console_register( + "time", "Get, set or store the time", "time [HH:MM] OR time store", + time_command_func); + console_register( + "date", "Get or set the date, or get the day of the week", + "date [yyyy-mm-dd] OR date week-day", date_command_func); } void add_time_callback(TimeCallback callback) diff --git a/components/time/time_storage.c b/components/time/time_storage.c new file mode 100644 index 0000000..3502dc3 --- /dev/null +++ b/components/time/time_storage.c @@ -0,0 +1,73 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#include "time_storage.h" + +#include "esp_log.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "nvs_flash.h" +#include + +#define TAG "Time storage" + +#define NVS_NAMESPACE "time" +#define TIMESTAMP_KEY "timestamp" + +static void time_saver_func(void *arg) +{ + (void)arg; + const TickType_t delay = CONFIG_TIME_SAVE_PERIOD_MS / portTICK_PERIOD_MS; + while (1) { + time_storage_save(); + vTaskDelay(delay); + } +} + +void time_storage_init() +{ + esp_err_t error; + nvs_handle_t nvs; + error = nvs_open(NVS_NAMESPACE, NVS_READONLY, &nvs); + if (error == ESP_OK) { + uint64_t timestamp; + error = nvs_get_u64(nvs, TIMESTAMP_KEY, ×tamp); + if (error == ESP_OK) { + struct timeval tv = { .tv_sec = (time_t)timestamp }; + settimeofday(&tv, NULL); + } else { + if (error != ESP_ERR_NVS_NOT_FOUND) + ESP_LOGE(TAG, "Error getting stored time: %04x", error); + } + nvs_close(nvs); + } else { + if (error != ESP_ERR_NVS_NOT_FOUND) + ESP_LOGE(TAG, "Error opening NVS: %04x", error); + } + + (void)xTaskCreate( + &time_saver_func, "time saver", CONFIG_DEFAULT_TASK_STACK, NULL, 1, + NULL); +} + +void time_storage_save() +{ + esp_err_t error; + + nvs_handle_t nvs; + error = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &nvs); + if (error != ESP_OK) { + ESP_LOGE(TAG, "Error opening NVS: %04x", error); + return; + } + + struct timeval tv; + gettimeofday(&tv, NULL); + error = nvs_set_u64(nvs, TIMESTAMP_KEY, tv.tv_sec); + if (error != ESP_OK) + ESP_LOGE(TAG, "Error storing time: %04x", error); + + nvs_close(nvs); +} diff --git a/components/time/time_storage.h b/components/time/time_storage.h new file mode 100644 index 0000000..7d786fc --- /dev/null +++ b/components/time/time_storage.h @@ -0,0 +1,20 @@ +/* + * SPDX-License-Identifier: AGPL-3.0-only + * Copyright (c) Camden Dixie O'Brien + */ + +#ifndef TIME_STORAGE_H +#define TIME_STORAGE_H + +/** + * Load the system time from persistent storage and set up a regular + * task to save the system time + */ +void time_storage_init(void); + +/** + * Save the system time in persistent storage. + */ +void time_storage_save(void); + +#endif