Move all firmware files to subdirectory
This commit is contained in:
5
firmware/components/time/CMakeLists.txt
Normal file
5
firmware/components/time/CMakeLists.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "time_manager.c" "time_sntp.c" "time_storage.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES console_wrapper esp_timer fatal lwip nvs_flash settings
|
||||
)
|
||||
244
firmware/components/time/time_manager.c
Normal file
244
firmware/components/time/time_manager.c
Normal file
@@ -0,0 +1,244 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
*/
|
||||
|
||||
#include "time_manager.h"
|
||||
|
||||
#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 <string.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
#define TAG "Time"
|
||||
|
||||
#define TM_YEAR_OFFSET 1900
|
||||
#define TM_MONTH_OFFSET 1
|
||||
|
||||
#define UPDATE_PERIOD_US 1000000UL
|
||||
#define MAX_CALLBACKS 8
|
||||
|
||||
static TimeCallback callbacks[MAX_CALLBACKS];
|
||||
static unsigned callback_count;
|
||||
|
||||
static void handle_timezone_update(const char *timezone)
|
||||
{
|
||||
setenv("TZ", timezone, 1);
|
||||
tzset();
|
||||
time_sntp_restart();
|
||||
}
|
||||
|
||||
static void run_callbacks(void *arg)
|
||||
{
|
||||
const Time time = get_time();
|
||||
for (unsigned i = 0; i < callback_count; ++i)
|
||||
callbacks[i](time);
|
||||
}
|
||||
|
||||
static int time_command_func(int argc, char **argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
const Time time = get_time();
|
||||
printf("%02u:%02u:%02u\n", time.hour, time.minute, time.second);
|
||||
return 0;
|
||||
} else if (argc == 2) {
|
||||
if (strcmp(argv[1], "store") == 0) {
|
||||
time_storage_save();
|
||||
return 0;
|
||||
} else {
|
||||
Time time;
|
||||
const int result = sscanf(
|
||||
argv[1], "%02u:%02u:%02u", &time.hour, &time.minute,
|
||||
&time.second);
|
||||
if (result < 2 || time.hour > 23 || time.minute > 59
|
||||
|| time.second > 59) {
|
||||
printf("Invalid time\n");
|
||||
return 1;
|
||||
}
|
||||
set_time(time);
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
printf("Invalid number of arguments\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
static int date_command_func(int argc, char **argv)
|
||||
{
|
||||
if (argc == 1) {
|
||||
const Date date = get_date();
|
||||
printf("%04u-%02u-%02u\n", date.year, date.month, date.day);
|
||||
return 0;
|
||||
} else if (argc == 2) {
|
||||
if (strcmp(argv[1], "week-day") == 0) {
|
||||
const WeekDay day = get_week_day();
|
||||
printf("%s\n", week_day_name(day));
|
||||
return 0;
|
||||
}
|
||||
Date date;
|
||||
const int result = sscanf(
|
||||
argv[1], "%04u-%02u-%02u", &date.year, &date.month, &date.day);
|
||||
if (result != 3 || date.month == 0 || date.month > 12
|
||||
|| date.day == 0 || date.day > 31) {
|
||||
printf("Invalid date\n");
|
||||
return 1;
|
||||
}
|
||||
set_date(date);
|
||||
return 0;
|
||||
} else {
|
||||
printf("Invalid number of arguments\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
esp_timer_handle_t update_timer;
|
||||
const esp_timer_create_args_t update_timer_config = {
|
||||
.callback = &run_callbacks,
|
||||
.arg = NULL,
|
||||
.name = "time updates",
|
||||
};
|
||||
error = esp_timer_create(&update_timer_config, &update_timer);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Error creating update timer: %04x", error);
|
||||
FATAL();
|
||||
}
|
||||
error = esp_timer_start_periodic(update_timer, UPDATE_PERIOD_US);
|
||||
if (error != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Error starting update timer: %04x", error);
|
||||
FATAL();
|
||||
}
|
||||
|
||||
console_register(
|
||||
"time", "Get, set or store the time",
|
||||
"time [hh:mm:ss] 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)
|
||||
{
|
||||
if (callback_count >= MAX_CALLBACKS) {
|
||||
ESP_LOGE(TAG, "Max number of time callbacks exceeded");
|
||||
return;
|
||||
}
|
||||
callbacks[callback_count] = callback;
|
||||
++callback_count;
|
||||
}
|
||||
|
||||
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,
|
||||
.second = timeinfo.tm_sec,
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
timeinfo.tm_sec = time.second;
|
||||
|
||||
tv.tv_sec = mktime(&timeinfo);
|
||||
settimeofday(&tv, NULL);
|
||||
}
|
||||
|
||||
Date get_date(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
struct tm timeinfo;
|
||||
(void)localtime_r(&tv.tv_sec, &timeinfo);
|
||||
|
||||
return (Date) {
|
||||
.year = timeinfo.tm_year + TM_YEAR_OFFSET,
|
||||
.month = timeinfo.tm_mon + TM_MONTH_OFFSET,
|
||||
.day = timeinfo.tm_mday,
|
||||
};
|
||||
}
|
||||
|
||||
void set_date(Date date)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
struct tm timeinfo;
|
||||
(void)localtime_r(&tv.tv_sec, &timeinfo);
|
||||
|
||||
timeinfo.tm_year = date.year - TM_YEAR_OFFSET;
|
||||
timeinfo.tm_mon = date.month - TM_MONTH_OFFSET;
|
||||
timeinfo.tm_mday = date.day;
|
||||
|
||||
tv.tv_sec = mktime(&timeinfo);
|
||||
settimeofday(&tv, NULL);
|
||||
}
|
||||
|
||||
WeekDay get_week_day(void)
|
||||
{
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
struct tm timeinfo;
|
||||
(void)localtime_r(&tv.tv_sec, &timeinfo);
|
||||
if (timeinfo.tm_wday == 0)
|
||||
return WEEK_DAY_SUNDAY;
|
||||
else
|
||||
return (WeekDay)(timeinfo.tm_wday - 1);
|
||||
}
|
||||
|
||||
const char *week_day_name(WeekDay day)
|
||||
{
|
||||
switch (day) {
|
||||
case WEEK_DAY_MONDAY:
|
||||
return "Monday";
|
||||
case WEEK_DAY_TUESDAY:
|
||||
return "Tuesday";
|
||||
case WEEK_DAY_WEDNESDAY:
|
||||
return "Wednesday";
|
||||
case WEEK_DAY_THURSDAY:
|
||||
return "Thursday";
|
||||
case WEEK_DAY_FRIDAY:
|
||||
return "Friday";
|
||||
case WEEK_DAY_SATURDAY:
|
||||
return "Saturday";
|
||||
case WEEK_DAY_SUNDAY:
|
||||
return "Sunday";
|
||||
|
||||
default:
|
||||
ESP_LOGE(
|
||||
TAG, "Invalid day of the week passed to %s(): %u", __func__,
|
||||
(unsigned)day);
|
||||
return "INVALID";
|
||||
}
|
||||
}
|
||||
53
firmware/components/time/time_manager.h
Normal file
53
firmware/components/time/time_manager.h
Normal file
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
*/
|
||||
|
||||
#ifndef TIME_MANAGER_H
|
||||
#define TIME_MANAGER_H
|
||||
|
||||
#include "time_types.h"
|
||||
|
||||
typedef void (*TimeCallback)(Time now);
|
||||
|
||||
/**
|
||||
* Initialize the time module.
|
||||
*/
|
||||
void time_manager_init(void);
|
||||
|
||||
/**
|
||||
* Add a callback to be regularly invoked with time updates.
|
||||
*/
|
||||
void add_time_callback(TimeCallback callback);
|
||||
|
||||
/**
|
||||
* Get the current time.
|
||||
*/
|
||||
Time get_time(void);
|
||||
|
||||
/**
|
||||
* Set the time.
|
||||
*/
|
||||
void set_time(Time time);
|
||||
|
||||
/**
|
||||
* Get the current date.
|
||||
*/
|
||||
Date get_date(void);
|
||||
|
||||
/**
|
||||
* Set the date.
|
||||
*/
|
||||
void set_date(Date date);
|
||||
|
||||
/**
|
||||
* Get which day of the week it is.
|
||||
*/
|
||||
WeekDay get_week_day(void);
|
||||
|
||||
/**
|
||||
* Get the name of a day of the week.
|
||||
*/
|
||||
const char *week_day_name(WeekDay day);
|
||||
|
||||
#endif
|
||||
98
firmware/components/time/time_sntp.c
Normal file
98
firmware/components/time/time_sntp.c
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
*/
|
||||
|
||||
#include "time_sntp.h"
|
||||
|
||||
#include "console.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_sntp.h"
|
||||
|
||||
#define TAG "Time SNTP"
|
||||
|
||||
static void handle_sntp_server_update(const char *sntp_server)
|
||||
{
|
||||
esp_sntp_setservername(0, sntp_server);
|
||||
time_sntp_restart();
|
||||
}
|
||||
|
||||
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 command_func(int argc, char **argv)
|
||||
{
|
||||
if (argc == 2) {
|
||||
if (strcmp(argv[1], "status") == 0) {
|
||||
if (esp_sntp_enabled()) {
|
||||
const sntp_sync_status_t status = sntp_get_sync_status();
|
||||
printf("%s\n", sync_status_description(status));
|
||||
} else {
|
||||
printf("Disabled\n");
|
||||
}
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "restart") == 0) {
|
||||
time_sntp_restart();
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "stop") == 0) {
|
||||
sntp_stop();
|
||||
return 0;
|
||||
} else if (strcmp(argv[1], "ip") == 0) {
|
||||
const ip_addr_t *ip = esp_sntp_getserver(0);
|
||||
printf("%s\n", ipaddr_ntoa(ip));
|
||||
return 0;
|
||||
} else {
|
||||
printf("Unrecognised subcommand\n");
|
||||
return 1;
|
||||
}
|
||||
} else {
|
||||
printf("Invalid number of arguments\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
void time_sntp_init(void)
|
||||
{
|
||||
char sntp_server[SETTINGS_MAX_VALUE_SIZE];
|
||||
(void)settings_get_sntp_server(sntp_server, SETTINGS_MAX_VALUE_SIZE);
|
||||
settings_add_sntp_server_callback(&handle_sntp_server_update);
|
||||
|
||||
esp_sntp_setoperatingmode(ESP_SNTP_OPMODE_POLL);
|
||||
esp_sntp_setservername(0, sntp_server);
|
||||
esp_sntp_init();
|
||||
|
||||
sntp_set_time_sync_notification_cb(sntp_sync_callback);
|
||||
|
||||
console_register(
|
||||
"sntp", "Manage SNTP", "sntp <status|restart|stop|ip>",
|
||||
command_func);
|
||||
}
|
||||
|
||||
void time_sntp_restart(void)
|
||||
{
|
||||
if (!sntp_restart())
|
||||
ESP_LOGW(TAG, "SNTP restart requested, but SNTP is not running");
|
||||
}
|
||||
19
firmware/components/time/time_sntp.h
Normal file
19
firmware/components/time/time_sntp.h
Normal file
@@ -0,0 +1,19 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
*/
|
||||
|
||||
#ifndef TIME_SNTP_H
|
||||
#define TIME_SNTP_H
|
||||
|
||||
/**
|
||||
* Initialize SNTP time synchronisation.
|
||||
*/
|
||||
void time_sntp_init(void);
|
||||
|
||||
/**
|
||||
* Restart SNTP
|
||||
*/
|
||||
void time_sntp_restart(void);
|
||||
|
||||
#endif
|
||||
73
firmware/components/time/time_storage.c
Normal file
73
firmware/components/time/time_storage.c
Normal 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, ×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);
|
||||
}
|
||||
20
firmware/components/time/time_storage.h
Normal file
20
firmware/components/time/time_storage.h
Normal 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
|
||||
32
firmware/components/time/time_types.h
Normal file
32
firmware/components/time/time_types.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Copyright (c) Camden Dixie O'Brien
|
||||
*/
|
||||
|
||||
#ifndef TIME_TYPES_H
|
||||
#define TIME_TYPES_H
|
||||
|
||||
typedef struct {
|
||||
unsigned hour;
|
||||
unsigned minute;
|
||||
unsigned second;
|
||||
} Time;
|
||||
|
||||
typedef struct {
|
||||
unsigned year;
|
||||
unsigned month;
|
||||
unsigned day;
|
||||
} Date;
|
||||
|
||||
typedef enum {
|
||||
WEEK_DAY_MONDAY,
|
||||
WEEK_DAY_TUESDAY,
|
||||
WEEK_DAY_WEDNESDAY,
|
||||
WEEK_DAY_THURSDAY,
|
||||
WEEK_DAY_FRIDAY,
|
||||
WEEK_DAY_SATURDAY,
|
||||
WEEK_DAY_SUNDAY,
|
||||
WEEK_DAY_COUNT,
|
||||
} WeekDay;
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user