Rename config module to settings

This commit is contained in:
Camden Dixie O'Brien 2023-05-15 17:39:29 +01:00
parent cee2e368dc
commit 3ab6c20e58
7 changed files with 56 additions and 53 deletions

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "config.c" SRCS "settings.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES fatal nvs_flash REQUIRES fatal nvs_flash
) )

View File

@ -3,7 +3,7 @@
* Copyright (c) Camden Dixie O'Brien * Copyright (c) Camden Dixie O'Brien
*/ */
#include "config.h" #include "settings.h"
#include "fatal.h" #include "fatal.h"
@ -12,9 +12,9 @@
#include <stdbool.h> #include <stdbool.h>
#include <string.h> #include <string.h>
#define TAG "Config" #define TAG "Settings"
#define NAMESPACE "config" #define NAMESPACE "settings"
#define MAX_CALLBACKS 8 #define MAX_CALLBACKS 8
typedef enum { typedef enum {
@ -28,9 +28,9 @@ typedef enum {
typedef struct { typedef struct {
const char *id; const char *id;
const char *default_value; const char *default_value;
char value[CONFIG_MAX_VALUE_SIZE]; char value[SETTINGS_MAX_VALUE_SIZE];
struct { struct {
ConfigCallback funcs[MAX_CALLBACKS]; SettingsCallback funcs[MAX_CALLBACKS];
unsigned count; unsigned count;
} callbacks; } callbacks;
} Item; } Item;
@ -67,9 +67,12 @@ static bool load(ItemIndex item)
return false; 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); 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( ESP_LOGE(
TAG, "Error loading %s from storage: %04x", state[item].id, TAG, "Error loading %s from storage: %04x", state[item].id,
error); error);
@ -120,11 +123,11 @@ static void set(ItemIndex item, const char *value)
} }
size_t len = strlen(value); size_t len = strlen(value);
if (len >= CONFIG_MAX_VALUE_SIZE) { if (len >= SETTINGS_MAX_VALUE_SIZE) {
ESP_LOGW( ESP_LOGW(
TAG, "%s value \"%s\" exceeds maximum size; truncated", TAG, "%s value \"%s\" exceeds maximum size; truncated",
state[item].id, value); state[item].id, value);
len = CONFIG_MAX_VALUE_SIZE - 1; len = SETTINGS_MAX_VALUE_SIZE - 1;
} }
memcpy(state[item].value, value, len); memcpy(state[item].value, value, len);
@ -145,7 +148,7 @@ static size_t get(ItemIndex item, char *buffer, size_t buffer_size)
return len; return len;
} }
static void add_callback(ItemIndex item, ConfigCallback callback) static void add_callback(ItemIndex item, SettingsCallback callback)
{ {
if (callback == NULL) { if (callback == NULL) {
ESP_LOGW( ESP_LOGW(
@ -162,7 +165,7 @@ static void add_callback(ItemIndex item, ConfigCallback callback)
} }
} }
void config_init() void settings_init()
{ {
esp_err_t error; 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); 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); return get(HOSTNAME, buffer, buffer_size);
} }
void config_add_hostname_callback(ConfigCallback callback) void settings_add_hostname_callback(SettingsCallback callback)
{ {
add_callback(HOSTNAME, callback); add_callback(HOSTNAME, callback);
} }
void config_set_ssid(const char *ssid) void settings_set_ssid(const char *ssid)
{ {
set(SSID, 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); return get(SSID, buffer, buffer_size);
} }
void config_add_ssid_callback(ConfigCallback callback) void settings_add_ssid_callback(SettingsCallback callback)
{ {
add_callback(SSID, callback); add_callback(SSID, callback);
} }
void config_set_psk(const char *psk) void settings_set_psk(const char *psk)
{ {
set(PSK, 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); return get(PSK, buffer, buffer_size);
} }
void config_add_psk_callback(ConfigCallback callback) void settings_add_psk_callback(SettingsCallback callback)
{ {
add_callback(PSK, callback); add_callback(PSK, callback);
} }
void config_set_timezone(const char *timezone) void settings_set_timezone(const char *timezone)
{ {
set(TIMEZONE, 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); return get(TIMEZONE, buffer, buffer_size);
} }
void config_add_timezone_callback(ConfigCallback callback) void settings_add_timezone_callback(SettingsCallback callback)
{ {
add_callback(TIMEZONE, callback); add_callback(TIMEZONE, callback);
} }

View File

@ -3,25 +3,25 @@
* Copyright (c) Camden Dixie O'Brien * Copyright (c) Camden Dixie O'Brien
*/ */
#ifndef CONFIG_H #ifndef SETTINGS_H
#define CONFIG_H #define SETTINGS_H
#include <stddef.h> #include <stddef.h>
#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, * If there are saved settings, they will be loaded. Otherwise,
* the default configuration will be loaded and saved. * the default settings will be loaded and saved.
*/ */
void config_init(void); void settings_init(void);
/** /**
* Set the device's hostname. * 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 * length is exceeded, the value will still be used, but will be
* truncated. * truncated.
*/ */
void config_set_hostname(const char *hostname); void settings_set_hostname(const char *hostname);
/** /**
* Write the device's hostname into the given buffer. * 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 size of the buffer, nothing will be written to the buffer but
* the length is still returned. * 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. * 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 * lifetime of the passed argument will be static, but the value may
* be modified once the callback returns. * 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. * 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 * length is exceeded, the value will still be used, but will be
* truncated. * 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. * 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 size of the buffer, nothing will be written to the buffer but
* the length is still returned. * 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. * 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 * lifetime of the passed argument will be static, but the value may
* be modified once the callback returns. * 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. * 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 * length is exceeded, the value will still be used, but will be
* truncated. * 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. * 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 size of the buffer, nothing will be written to the buffer but
* the length is still returned. * 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. * 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 * the passed argument will be static, but the value may be modified
* once the callback returns. * once the callback returns.
*/ */
void config_add_psk_callback(ConfigCallback callback); void settings_add_psk_callback(SettingsCallback callback);
/** /**
* Set the timezone. * 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 * length is exceeded, the value will still be used, but will be
* truncated. * truncated.
*/ */
void config_set_timezone(const char *psk); void settings_set_timezone(const char *psk);
/** /**
* Write the timezone into the given buffer. * 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 size of the buffer, nothing will be written to the buffer but
* the length is still returned. * 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. * 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 * lifetime of the passed argument will be static, but the value may
* be modified once the callback returns. * be modified once the callback returns.
*/ */
void config_add_timezone_callback(ConfigCallback callback); void settings_add_timezone_callback(SettingsCallback callback);
#endif #endif

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "time_manager.c" SRCS "time_manager.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES config REQUIRES settings
) )

View File

@ -5,7 +5,7 @@
#include "time_manager.h" #include "time_manager.h"
#include "config.h" #include "settings.h"
#include <time.h> #include <time.h>
@ -17,11 +17,11 @@ static void handle_timezone_update(const char *timezone)
void time_manager_init(void) void time_manager_init(void)
{ {
char timezone[CONFIG_MAX_VALUE_SIZE]; char timezone[SETTINGS_MAX_VALUE_SIZE];
(void)config_get_timezone(timezone, CONFIG_MAX_VALUE_SIZE); (void)settings_get_timezone(timezone, sizeof(timezone));
handle_timezone_update(timezone); handle_timezone_update(timezone);
config_add_timezone_callback(&handle_timezone_update); settings_add_timezone_callback(&handle_timezone_update);
} }
Time get_time(void) Time get_time(void)

View File

@ -1,5 +1,5 @@
idf_component_register( idf_component_register(
SRCS "main.c" SRCS "main.c"
INCLUDE_DIRS "." INCLUDE_DIRS "."
REQUIRES config display REQUIRES display settings
) )

View File

@ -3,11 +3,11 @@
* Copyright (c) Camden Dixie O'Brien * Copyright (c) Camden Dixie O'Brien
*/ */
#include "config.h"
#include "display.h" #include "display.h"
#include "settings.h"
void app_main(void) void app_main(void)
{ {
config_init(); settings_init();
display_init(); display_init();
} }