Add setting for SNTP server

This commit is contained in:
Camden Dixie O'Brien 2023-05-15 23:21:47 +01:00
parent 7d84b95ad5
commit 62405149ad
3 changed files with 51 additions and 0 deletions

View File

@ -23,6 +23,7 @@ typedef enum {
SSID, SSID,
PSK, PSK,
TIMEZONE, TIMEZONE,
SNTP_SERVER,
ITEM_COUNT, ITEM_COUNT,
} ItemIndex; } ItemIndex;
@ -53,6 +54,10 @@ static Item state[ITEM_COUNT] = {
.id = "timezone", .id = "timezone",
.default_value = CONFIG_DEFAULT_TIMEZONE, .default_value = CONFIG_DEFAULT_TIMEZONE,
}, },
[SNTP_SERVER] = {
.id = "sntp-server",
.default_value = CONFIG_DEFAULT_SNTP_SERVER,
},
}; };
static bool load(ItemIndex item) static bool load(ItemIndex item)
@ -281,3 +286,18 @@ void settings_add_timezone_callback(SettingsCallback callback)
{ {
add_callback(TIMEZONE, 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);
}

View File

@ -136,4 +136,32 @@ size_t settings_get_timezone(char *buffer, size_t buffer_size);
*/ */
void settings_add_timezone_callback(SettingsCallback callback); 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 #endif

View File

@ -11,6 +11,9 @@ menu "Bedside clock settings"
config DEFAULT_TIMEZONE config DEFAULT_TIMEZONE
string "Default timezone" string "Default timezone"
default "Europe/London" default "Europe/London"
config DEFAULT_SNTP_SERVER
string "Default SNTP server domain"
default "pool.ntp.org"
config WIFI_MAX_RETRIES config WIFI_MAX_RETRIES
int "Maximum number of times to retry connecting to WiFi network" int "Maximum number of times to retry connecting to WiFi network"
default 10 default 10