Seperate SNTP stuff into its own module

This commit is contained in:
Camden Dixie O'Brien 2023-05-17 18:08:55 +01:00
parent 89eb99b80f
commit 6ec3cae4b3
4 changed files with 120 additions and 75 deletions

View File

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

View File

@ -7,14 +7,16 @@
#include "console.h"
#include "settings.h"
#include "time_sntp.h"
#include "esp_log.h"
#include "esp_sntp.h"
#include "esp_timer.h"
#include "fatal.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include <time.h>
#include <string.h>
#include <sys/time.h>
#define TAG "Time"
@ -34,40 +36,7 @@ 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";
}
time_sntp_restart();
}
static int store_time(void)
@ -154,32 +123,6 @@ static int date_command_func(int argc, char **argv)
}
}
static int sntp_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) {
return sntp_restart() ? 0 : 1;
} else if (strcmp(argv[1], "stop") == 0) {
sntp_stop();
return 0;
} else {
printf("Unrecognised subcommand\n");
return 1;
}
} else {
printf("Invalid number of arguments\n");
return 1;
}
}
static void time_saver_func(void *arg)
{
(void)arg;
@ -194,29 +137,19 @@ void time_manager_init(void)
{
callback_count = 0;
time_sntp_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);
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_SMOOTH);
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 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);
console_register(
"sntp", "Manage SNTP", "sntp <status|restart|stop>",
sntp_command_func);
// Attempt to load time from storage
esp_err_t error;

View File

@ -0,0 +1,93 @@
/*
* 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 {
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_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
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();
console_register(
"sntp", "Manage SNTP", "sntp <status|restart|stop>", command_func);
}
void time_sntp_restart(void)
{
if (!sntp_restart())
ESP_LOGW(TAG, "SNTP restart requested, but SNTP is not running");
}

View 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