Create system_utils component

Somewhat of a 'misc' component; handles early initialization, checking
for first boot and rebooting.
This commit is contained in:
Camden Dixie O'Brien 2023-05-19 16:05:27 +01:00
parent 9c9f027bb8
commit 685e0950bf
8 changed files with 120 additions and 18 deletions

View File

@ -1,5 +1,5 @@
idf_component_register(
SRCS "console.c"
INCLUDE_DIRS "."
REQUIRES console esp_system
REQUIRES console system_utils
)

View File

@ -5,9 +5,10 @@
#include "console.h"
#include "system_utils.h"
#include "esp_console.h"
#include "esp_log.h"
#include "esp_system.h"
#define TAG "Console"
@ -17,7 +18,7 @@ static int reboot_command_func(int argc, char **argv)
{
(void)argc;
(void)argv;
(void)esp_restart();
reboot();
return 0;
}

View File

@ -201,20 +201,6 @@ static int command_func(int argc, char **argv)
void settings_init()
{
esp_err_t error;
error = nvs_flash_init();
if (error == ESP_ERR_NVS_NO_FREE_PAGES
|| error == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGI(TAG, "NVS partition full or outdated; erasing");
(void)nvs_flash_erase();
error = nvs_flash_init();
}
if (error != ESP_OK) {
ESP_LOGE(TAG, "Error initializing NVS store: %04x", error);
FATAL();
}
for (ItemIndex item = (ItemIndex)0; item < ITEM_COUNT; ++item) {
if (!load(item)) {
set(item, state[item].default_value);

View File

@ -0,0 +1,5 @@
idf_component_register(
SRCS "system_utils.c"
INCLUDE_DIRS "."
REQUIRES esp_system fatal nvs_flash time
)

View File

@ -0,0 +1,79 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) Camden Dixie O'Brien
*/
#include "system_utils.h"
#include "fatal.h"
#include "time_storage.h"
#include "esp_log.h"
#include "esp_system.h"
#include "nvs_flash.h"
#define TAG "System utils"
#define NVS_NAMESPACE "system-utils"
#define BOOTED_FLAG_KEY "booted"
static bool first_boot;
static void test_first_boot(void)
{
esp_err_t error;
nvs_handle_t handle;
error = nvs_open(NVS_NAMESPACE, NVS_READWRITE, &handle);
if (error != ESP_OK) {
ESP_LOGE(TAG, "Error opening NVS store namespace: %02x", error);
first_boot = false;
return;
}
uint8_t tmp;
error = nvs_get_u8(handle, BOOTED_FLAG_KEY, &tmp);
if (error == ESP_OK) {
first_boot = false;
} else if (error == ESP_ERR_NVS_NOT_FOUND) {
ESP_LOGI(TAG, "First boot of system");
first_boot = true;
error = nvs_set_u8(handle, BOOTED_FLAG_KEY, 1);
if (error != ESP_OK)
ESP_LOGE(TAG, "Error setting booted flag: %02x", error);
} else {
ESP_LOGE(TAG, "Error getting booted flag: %02x", error);
first_boot = false;
}
nvs_close(handle);
}
void early_init()
{
esp_err_t error = nvs_flash_init();
if (error == ESP_ERR_NVS_NO_FREE_PAGES
|| error == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_LOGI(TAG, "NVS partition full or outdated; erasing");
(void)nvs_flash_erase();
error = nvs_flash_init();
}
if (error != ESP_OK) {
ESP_LOGE(TAG, "Error initializing NVS store: %04x", error);
FATAL();
}
test_first_boot();
}
bool is_first_boot()
{
return first_boot;
}
void reboot()
{
ESP_LOGI(TAG, "Rebooting system");
time_storage_save();
esp_restart();
}

View File

@ -0,0 +1,27 @@
/*
* SPDX-License-Identifier: AGPL-3.0-only
* Copyright (c) Camden Dixie O'Brien
*/
#ifndef SYSTEM_UTILS_H
#define SYSTEM_UTILS_H
#include <stdbool.h>
/**
* Perform system initialization that must occur before any other
* components are initialized.
*/
void early_init(void);
/**
* Return whether or not this is the first boot of the system.
*/
bool is_first_boot(void);
/**
* Reboot the system, storing the current time beforehand.
*/
void reboot(void);
#endif

View File

@ -1,5 +1,6 @@
idf_component_register(
SRCS "main.c"
INCLUDE_DIRS "."
REQUIRES buttons console_wrapper display settings sound time wifi
REQUIRES
buttons console_wrapper display settings sound system_utils time wifi
)

View File

@ -9,11 +9,14 @@
#include "display.h"
#include "settings.h"
#include "sound.h"
#include "system_utils.h"
#include "time_manager.h"
#include "wifi.h"
void app_main(void)
{
early_init();
console_init();
settings_init();
wifi_init();