From 8be7ec1e2840c47c2768e807e33f5b2c416b9048 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Mon, 15 May 2023 17:25:25 +0100 Subject: [PATCH] Add timezone config item --- components/config/config.c | 20 ++++++++++++++++++ components/config/config.h | 43 +++++++++++++++++++++++++++++++------- main/Kconfig.projbuild | 3 +++ 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/components/config/config.c b/components/config/config.c index 9cb9e6e..eb4e8a9 100644 --- a/components/config/config.c +++ b/components/config/config.c @@ -21,6 +21,7 @@ typedef enum { HOSTNAME, SSID, PSK, + TIMEZONE, ITEM_COUNT, } ItemIndex; @@ -47,6 +48,10 @@ static Item state[ITEM_COUNT] = { .id = "psk", .default_value = CONFIG_DEFAULT_PSK, }, + [TIMEZONE] = { + .id = "timezone", + .default_value = CONFIG_DEFAULT_TIMEZONE, + }, }; static bool load(ItemIndex item) @@ -225,3 +230,18 @@ void config_add_psk_callback(ConfigCallback callback) { add_callback(PSK, callback); } + +void config_set_timezone(const char *timezone) +{ + set(TIMEZONE, timezone); +} + +size_t config_get_timezone(char *buffer, size_t buffer_size) +{ + return get(TIMEZONE, buffer, buffer_size); +} + +void config_add_timezone_callback(ConfigCallback callback) +{ + add_callback(TIMEZONE, callback); +} diff --git a/components/config/config.h b/components/config/config.h index 1c38106..df10a97 100644 --- a/components/config/config.h +++ b/components/config/config.h @@ -44,7 +44,7 @@ size_t config_get_hostname(char *buffer, size_t buffer_size); /** * Add a callback for hostname updates. * - * The function specified in the argument will be invoked whenever a + * The function specified in the argument will be invoked whenever * the hostname 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. @@ -72,8 +72,8 @@ size_t config_get_ssid(char *buffer, size_t buffer_size); /** * Add a callback for SSID updates. * - * The function specified in the argument will be invoked whenever a - * the ssid is updated, with the new value as its argument. The + * The function specified in the argument will be invoked whenever + * the SSID 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. */ @@ -100,11 +100,40 @@ size_t config_get_psk(char *buffer, size_t buffer_size); /** * Add a callback for PSK updates. * - * The function specified in the argument will be invoked whenever a - * the psk 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. + * The function specified in the argument will be invoked whenever the + * PSK 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 config_add_psk_callback(ConfigCallback callback); +/** + * Set the timezone. + * + * The argument should be a null-terminated string, containing a + * timezone spec in the format expected by tzset(). If the maximum + * length is exceeded, the value will still be used, but will be + * truncated. + */ +void config_set_timezone(const char *psk); + +/** + * Write the timezone into the given buffer. + * + * The length of the timezone 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 config_get_timezone(char *buffer, size_t buffer_size); + +/** + * Add a callback for timezone updates. + * + * The function specified in the argument will be invoked whenever the + * timezone 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 config_add_timezone_callback(ConfigCallback callback); + #endif diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 68f73d3..3e3f816 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -8,4 +8,7 @@ menu "Bedside clock settings" config DEFAULT_PSK string "PSK for default WiFi network" default "" + config DEFAULT_TIMEZONE + string "Default timezone" + default "Europe/London" endmenu