diff --git a/components/settings/settings.c b/components/settings/settings.c index 9799de5..6cb8e2d 100644 --- a/components/settings/settings.c +++ b/components/settings/settings.c @@ -23,6 +23,7 @@ typedef enum { SSID, PSK, TIMEZONE, + SNTP_SERVER, ITEM_COUNT, } ItemIndex; @@ -53,6 +54,10 @@ static Item state[ITEM_COUNT] = { .id = "timezone", .default_value = CONFIG_DEFAULT_TIMEZONE, }, + [SNTP_SERVER] = { + .id = "sntp-server", + .default_value = CONFIG_DEFAULT_SNTP_SERVER, + }, }; static bool load(ItemIndex item) @@ -281,3 +286,18 @@ void settings_add_timezone_callback(SettingsCallback callback) { add_callback(TIMEZONE, callback); } + +void settings_set_sntp_server(const char *sntp_server) +{ + set(SNTP_SERVER, sntp_server); +} + +size_t settings_get_sntp_server(char *buffer, size_t buffer_size) +{ + return get(SNTP_SERVER, buffer, buffer_size); +} + +void settings_add_sntp_server_callback(SettingsCallback callback) +{ + add_callback(SNTP_SERVER, callback); +} diff --git a/components/settings/settings.h b/components/settings/settings.h index 72fc834..0fedb40 100644 --- a/components/settings/settings.h +++ b/components/settings/settings.h @@ -136,4 +136,32 @@ size_t settings_get_timezone(char *buffer, size_t buffer_size); */ void settings_add_timezone_callback(SettingsCallback callback); +/** + * Set the SNTP server URL. + * + * The argument should be a null-terminated string, containing a valid + * domain name for an SNTP server. If the maximum length is exceeded, + * the value will still be used, but will be truncated. + */ +void settings_set_sntp_server(const char *sntp_server); + +/** + * Write the SNTP server URL into the given buffer. + * + * The length of the SNTP server domain is returned. If the value's + * size exceeds the size of the buffer, nothing will be written to the + * buffer but the length is still returned. + */ +size_t settings_get_sntp_server(char *buffer, size_t buffer_size); + +/** + * Add a callback for SNTP server URL updates. + * + * The function specified in the argument will be invoked whenever the + * SNTP server is updated, with the new value as its argument. The + * lifetime of the passed argument will be static, but the value may + * be modified once the callback returns. + */ +void settings_add_sntp_server_callback(SettingsCallback callback); + #endif diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index b5b54b8..4ef5ccf 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -11,6 +11,9 @@ menu "Bedside clock settings" config DEFAULT_TIMEZONE string "Default timezone" default "Europe/London" + config DEFAULT_SNTP_SERVER + string "Default SNTP server domain" + default "pool.ntp.org" config WIFI_MAX_RETRIES int "Maximum number of times to retry connecting to WiFi network" default 10