/* * SPDX-License-Identifier: AGPL-3.0-only * Copyright (c) Camden Dixie O'Brien */ #include "time_manager.h" #include "console.h" #include "settings.h" #include "esp_log.h" #include "esp_sntp.h" #include "freertos/FreeRTOS.h" #include "nvs_flash.h" #include #define TAG "Time" #define NVS_NAMESPACE "time" #define TIMESTAMP_KEY "timestamp" static void handle_timezone_update(const char *timezone) { setenv("TZ", timezone, 1); tzset(); (void)sntp_restart(); } static void handle_sntp_server_update(const char *sntp_server) { esp_sntp_setservername(0, sntp_server); if (!sntp_restart()) { ESP_LOGW( TAG, "Received SNTP server update before SNTP initialization"); } } static void sntp_sync_callback(struct timeval *tv) { const time_t now = tv->tv_sec; struct tm timeinfo; (void)localtime_r(&now, &timeinfo); ESP_LOGI( TAG, "Received SNTP time notification: %02u:%02u", timeinfo.tm_hour, timeinfo.tm_min); } static const char *sync_status_description(sntp_sync_status_t status) { switch (status) { case SNTP_SYNC_STATUS_RESET: return "Reset"; case SNTP_SYNC_STATUS_COMPLETED: return "Completed"; case SNTP_SYNC_STATUS_IN_PROGRESS: return "In progress"; default: return "Invalid"; } } 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, "Stored time"); else ESP_LOGE(TAG, "Error storing time: %04x", error); nvs_close(nvs); return error == ESP_OK ? 0 : 1; } static int command_func(int argc, char **argv) { if (argc == 1) { const Time time = get_time(); printf("%02u:%02u\n", time.hour, time.minute); return 0; } else if (argc == 2) { if (strcmp(argv[1], "sntp-status") == 0) { const sntp_sync_status_t status = sntp_get_sync_status(); printf("%s\n", sync_status_description(status)); return 0; } else if (strcmp(argv[1], "store") == 0) { return store_time(); } else { Time time; const int result = sscanf(argv[1], "%02u:%02u", &time.hour, &time.minute); if (result < 2 || time.hour > 23 || time.minute > 59) { printf("Invalid time\n"); return 1; } set_time(time); return 0; } } else { printf("Invalid number of arguments\n"); return 1; } } 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) { 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); char sntp_server[SETTINGS_MAX_VALUE_SIZE]; (void)settings_get_sntp_server(sntp_server, SETTINGS_MAX_VALUE_SIZE); esp_sntp_set_sync_mode(SNTP_SYNC_MODE_IMMED); esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL); esp_sntp_setservername(0, sntp_server); sntp_set_time_sync_notification_cb(sntp_sync_callback); esp_sntp_init(); settings_add_sntp_server_callback(&handle_sntp_server_update); console_register( "time", "Get / set time and SNTP status", "time OR time OR time sntp-status", 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); } Time get_time(void) { struct timeval tv; gettimeofday(&tv, NULL); struct tm timeinfo; (void)localtime_r(&tv.tv_sec, &timeinfo); return (Time) { .hour = timeinfo.tm_hour, .minute = timeinfo.tm_min }; } void set_time(Time time) { struct timeval tv; gettimeofday(&tv, NULL); struct tm timeinfo; (void)localtime_r(&tv.tv_sec, &timeinfo); timeinfo.tm_hour = time.hour; timeinfo.tm_min = time.minute; tv.tv_sec = mktime(&timeinfo); settimeofday(&tv, NULL); }