Seperate time storage into its own module

This commit is contained in:
Camden Dixie O'Brien 2023-05-17 18:23:58 +01:00
parent 6ec3cae4b3
commit 7926ccc6fa
4 changed files with 107 additions and 74 deletions

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "time_manager.c" "time_sntp.c" SRCS "time_manager.c" "time_sntp.c" "time_storage.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES console_wrapper esp_timer fatal lwip nvs_flash settings REQUIRES console_wrapper esp_timer fatal lwip nvs_flash settings
) )

View File

@ -8,21 +8,16 @@
#include "console.h" #include "console.h"
#include "settings.h" #include "settings.h"
#include "time_sntp.h" #include "time_sntp.h"
#include "time_storage.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_timer.h" #include "esp_timer.h"
#include "fatal.h" #include "fatal.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include <string.h> #include <string.h>
#include <sys/time.h> #include <sys/time.h>
#define TAG "Time" #define TAG "Time"
#define NVS_NAMESPACE "time"
#define TIMESTAMP_KEY "timestamp"
#define TM_YEAR_OFFSET 1900 #define TM_YEAR_OFFSET 1900
#define TM_MONTH_OFFSET 1 #define TM_MONTH_OFFSET 1
@ -39,29 +34,6 @@ static void handle_timezone_update(const char *timezone)
time_sntp_restart(); 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) static void run_callbacks(void *arg)
{ {
const Time time = get_time(); const Time time = get_time();
@ -77,7 +49,8 @@ static int time_command_func(int argc, char **argv)
return 0; return 0;
} else if (argc == 2) { } else if (argc == 2) {
if (strcmp(argv[1], "store") == 0) { if (strcmp(argv[1], "store") == 0) {
return store_time(); time_storage_save();
return 0;
} else { } else {
Time time; Time time;
const int result 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) void time_manager_init(void)
{ {
esp_err_t error;
callback_count = 0; callback_count = 0;
time_sntp_init(); time_sntp_init();
time_storage_init();
char timezone[SETTINGS_MAX_VALUE_SIZE]; char timezone[SETTINGS_MAX_VALUE_SIZE];
(void)settings_get_timezone(timezone, SETTINGS_MAX_VALUE_SIZE); (void)settings_get_timezone(timezone, SETTINGS_MAX_VALUE_SIZE);
handle_timezone_update(timezone); handle_timezone_update(timezone);
settings_add_timezone_callback(&handle_timezone_update); 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, &timestamp);
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; esp_timer_handle_t update_timer;
const esp_timer_create_args_t update_timer_config = { const esp_timer_create_args_t update_timer_config = {
.callback = &run_callbacks, .callback = &run_callbacks,
@ -193,6 +126,13 @@ void time_manager_init(void)
ESP_LOGE(TAG, "Error starting update timer: %04x", error); ESP_LOGE(TAG, "Error starting update timer: %04x", error);
FATAL(); 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) void add_time_callback(TimeCallback callback)

View File

@ -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 <sys/time.h>
#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, &timestamp);
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);
}

View File

@ -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