diff --git a/components/config/CMakeLists.txt b/components/settings/CMakeLists.txt similarity index 78% rename from components/config/CMakeLists.txt rename to components/settings/CMakeLists.txt index 5010f11..0ea4c8d 100644 --- a/components/config/CMakeLists.txt +++ b/components/settings/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( - SRCS "config.c" + SRCS "settings.c" INCLUDE_DIRS "." REQUIRES fatal nvs_flash ) diff --git a/components/config/config.c b/components/settings/settings.c similarity index 78% rename from components/config/config.c rename to components/settings/settings.c index eb4e8a9..c48a843 100644 --- a/components/config/config.c +++ b/components/settings/settings.c @@ -3,7 +3,7 @@ * Copyright (c) Camden Dixie O'Brien */ -#include "config.h" +#include "settings.h" #include "fatal.h" @@ -12,9 +12,9 @@ #include #include -#define TAG "Config" +#define TAG "Settings" -#define NAMESPACE "config" +#define NAMESPACE "settings" #define MAX_CALLBACKS 8 typedef enum { @@ -28,9 +28,9 @@ typedef enum { typedef struct { const char *id; const char *default_value; - char value[CONFIG_MAX_VALUE_SIZE]; + char value[SETTINGS_MAX_VALUE_SIZE]; struct { - ConfigCallback funcs[MAX_CALLBACKS]; + SettingsCallback funcs[MAX_CALLBACKS]; unsigned count; } callbacks; } Item; @@ -67,9 +67,12 @@ static bool load(ItemIndex item) return false; } - size_t size = CONFIG_MAX_VALUE_SIZE; + size_t size = SETTINGS_MAX_VALUE_SIZE; error = nvs_get_str(handle, state[item].id, state[item].value, &size); - if (error != ESP_OK) { + if (error == ESP_ERR_NVS_NOT_FOUND) { + nvs_close(handle); + return false; + } else if (error != ESP_OK) { ESP_LOGE( TAG, "Error loading %s from storage: %04x", state[item].id, error); @@ -120,11 +123,11 @@ static void set(ItemIndex item, const char *value) } size_t len = strlen(value); - if (len >= CONFIG_MAX_VALUE_SIZE) { + if (len >= SETTINGS_MAX_VALUE_SIZE) { ESP_LOGW( TAG, "%s value \"%s\" exceeds maximum size; truncated", state[item].id, value); - len = CONFIG_MAX_VALUE_SIZE - 1; + len = SETTINGS_MAX_VALUE_SIZE - 1; } memcpy(state[item].value, value, len); @@ -145,7 +148,7 @@ static size_t get(ItemIndex item, char *buffer, size_t buffer_size) return len; } -static void add_callback(ItemIndex item, ConfigCallback callback) +static void add_callback(ItemIndex item, SettingsCallback callback) { if (callback == NULL) { ESP_LOGW( @@ -162,7 +165,7 @@ static void add_callback(ItemIndex item, ConfigCallback callback) } } -void config_init() +void settings_init() { esp_err_t error; @@ -186,62 +189,62 @@ void config_init() } } -void config_set_hostname(const char *hostname) +void settings_set_hostname(const char *hostname) { set(HOSTNAME, hostname); } -size_t config_get_hostname(char *buffer, size_t buffer_size) +size_t settings_get_hostname(char *buffer, size_t buffer_size) { return get(HOSTNAME, buffer, buffer_size); } -void config_add_hostname_callback(ConfigCallback callback) +void settings_add_hostname_callback(SettingsCallback callback) { add_callback(HOSTNAME, callback); } -void config_set_ssid(const char *ssid) +void settings_set_ssid(const char *ssid) { set(SSID, ssid); } -size_t config_get_ssid(char *buffer, size_t buffer_size) +size_t settings_get_ssid(char *buffer, size_t buffer_size) { return get(SSID, buffer, buffer_size); } -void config_add_ssid_callback(ConfigCallback callback) +void settings_add_ssid_callback(SettingsCallback callback) { add_callback(SSID, callback); } -void config_set_psk(const char *psk) +void settings_set_psk(const char *psk) { set(PSK, psk); } -size_t config_get_psk(char *buffer, size_t buffer_size) +size_t settings_get_psk(char *buffer, size_t buffer_size) { return get(PSK, buffer, buffer_size); } -void config_add_psk_callback(ConfigCallback callback) +void settings_add_psk_callback(SettingsCallback callback) { add_callback(PSK, callback); } -void config_set_timezone(const char *timezone) +void settings_set_timezone(const char *timezone) { set(TIMEZONE, timezone); } -size_t config_get_timezone(char *buffer, size_t buffer_size) +size_t settings_get_timezone(char *buffer, size_t buffer_size) { return get(TIMEZONE, buffer, buffer_size); } -void config_add_timezone_callback(ConfigCallback callback) +void settings_add_timezone_callback(SettingsCallback callback) { add_callback(TIMEZONE, callback); } diff --git a/components/config/config.h b/components/settings/settings.h similarity index 75% rename from components/config/config.h rename to components/settings/settings.h index df10a97..72fc834 100644 --- a/components/config/config.h +++ b/components/settings/settings.h @@ -3,25 +3,25 @@ * Copyright (c) Camden Dixie O'Brien */ -#ifndef CONFIG_H -#define CONFIG_H +#ifndef SETTINGS_H +#define SETTINGS_H #include -#define CONFIG_MAX_VALUE_SIZE 32U +#define SETTINGS_MAX_VALUE_SIZE 32U /** - * Callback type for config updates + * Callback type for settings updates */ -typedef void (*ConfigCallback)(const char *value); +typedef void (*SettingsCallback)(const char *value); /** - * Initialize the configuration module. + * Initialize the settings module. * - * If there is a saved configuration, it will be loaded. Otherwise, - * the default configuration will be loaded and saved. + * If there are saved settings, they will be loaded. Otherwise, + * the default settings will be loaded and saved. */ -void config_init(void); +void settings_init(void); /** * Set the device's hostname. @@ -30,7 +30,7 @@ void config_init(void); * length is exceeded, the value will still be used, but will be * truncated. */ -void config_set_hostname(const char *hostname); +void settings_set_hostname(const char *hostname); /** * Write the device's hostname into the given buffer. @@ -39,7 +39,7 @@ void config_set_hostname(const char *hostname); * the size of the buffer, nothing will be written to the buffer but * the length is still returned. */ -size_t config_get_hostname(char *buffer, size_t buffer_size); +size_t settings_get_hostname(char *buffer, size_t buffer_size); /** * Add a callback for hostname updates. @@ -49,7 +49,7 @@ size_t config_get_hostname(char *buffer, size_t buffer_size); * lifetime of the passed argument will be static, but the value may * be modified once the callback returns. */ -void config_add_hostname_callback(ConfigCallback callback); +void settings_add_hostname_callback(SettingsCallback callback); /** * Set the SSID of the WiFi network. @@ -58,7 +58,7 @@ void config_add_hostname_callback(ConfigCallback callback); * length is exceeded, the value will still be used, but will be * truncated. */ -void config_set_ssid(const char *ssid); +void settings_set_ssid(const char *ssid); /** * Write the SSID of the WiFi network into the given buffer. @@ -67,7 +67,7 @@ void config_set_ssid(const char *ssid); * the size of the buffer, nothing will be written to the buffer but * the length is still returned. */ -size_t config_get_ssid(char *buffer, size_t buffer_size); +size_t settings_get_ssid(char *buffer, size_t buffer_size); /** * Add a callback for SSID updates. @@ -77,7 +77,7 @@ size_t config_get_ssid(char *buffer, size_t buffer_size); * lifetime of the passed argument will be static, but the value may * be modified once the callback returns. */ -void config_add_ssid_callback(ConfigCallback callback); +void settings_add_ssid_callback(SettingsCallback callback); /** * Set the PSK for the WiFi network. @@ -86,7 +86,7 @@ void config_add_ssid_callback(ConfigCallback callback); * length is exceeded, the value will still be used, but will be * truncated. */ -void config_set_psk(const char *psk); +void settings_set_psk(const char *psk); /** * Write the PSK for the WiFi network into the given buffer. @@ -95,7 +95,7 @@ void config_set_psk(const char *psk); * the size of the buffer, nothing will be written to the buffer but * the length is still returned. */ -size_t config_get_psk(char *buffer, size_t buffer_size); +size_t settings_get_psk(char *buffer, size_t buffer_size); /** * Add a callback for PSK updates. @@ -105,7 +105,7 @@ size_t config_get_psk(char *buffer, size_t buffer_size); * the passed argument will be static, but the value may be modified * once the callback returns. */ -void config_add_psk_callback(ConfigCallback callback); +void settings_add_psk_callback(SettingsCallback callback); /** * Set the timezone. @@ -115,7 +115,7 @@ void config_add_psk_callback(ConfigCallback callback); * length is exceeded, the value will still be used, but will be * truncated. */ -void config_set_timezone(const char *psk); +void settings_set_timezone(const char *psk); /** * Write the timezone into the given buffer. @@ -124,7 +124,7 @@ void config_set_timezone(const char *psk); * 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); +size_t settings_get_timezone(char *buffer, size_t buffer_size); /** * Add a callback for timezone updates. @@ -134,6 +134,6 @@ size_t config_get_timezone(char *buffer, size_t buffer_size); * 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); +void settings_add_timezone_callback(SettingsCallback callback); #endif diff --git a/components/time/CMakeLists.txt b/components/time/CMakeLists.txt index 130f44d..a05a4b0 100644 --- a/components/time/CMakeLists.txt +++ b/components/time/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "time_manager.c" INCLUDE_DIRS "." - REQUIRES config + REQUIRES settings ) diff --git a/components/time/time_manager.c b/components/time/time_manager.c index b8cf755..b3ffc00 100644 --- a/components/time/time_manager.c +++ b/components/time/time_manager.c @@ -5,7 +5,7 @@ #include "time_manager.h" -#include "config.h" +#include "settings.h" #include @@ -17,11 +17,11 @@ static void handle_timezone_update(const char *timezone) void time_manager_init(void) { - char timezone[CONFIG_MAX_VALUE_SIZE]; - (void)config_get_timezone(timezone, CONFIG_MAX_VALUE_SIZE); + char timezone[SETTINGS_MAX_VALUE_SIZE]; + (void)settings_get_timezone(timezone, sizeof(timezone)); handle_timezone_update(timezone); - config_add_timezone_callback(&handle_timezone_update); + settings_add_timezone_callback(&handle_timezone_update); } Time get_time(void) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f983e56..46a97e0 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,5 +1,5 @@ idf_component_register( SRCS "main.c" INCLUDE_DIRS "." - REQUIRES config display + REQUIRES display settings ) diff --git a/main/main.c b/main/main.c index c10eddf..c8e9df8 100644 --- a/main/main.c +++ b/main/main.c @@ -3,11 +3,11 @@ * Copyright (c) Camden Dixie O'Brien */ -#include "config.h" #include "display.h" +#include "settings.h" void app_main(void) { - config_init(); + settings_init(); display_init(); }