Create system_utils component
Somewhat of a 'misc' component; handles early initialization, checking for first boot and rebooting.
This commit is contained in:
parent
9c9f027bb8
commit
685e0950bf
@ -1,5 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "console.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES console esp_system
|
||||
REQUIRES console system_utils
|
||||
)
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
|
5
components/system_utils/CMakeLists.txt
Normal file
5
components/system_utils/CMakeLists.txt
Normal file
@ -0,0 +1,5 @@
|
||||
idf_component_register(
|
||||
SRCS "system_utils.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES esp_system fatal nvs_flash time
|
||||
)
|
79
components/system_utils/system_utils.c
Normal file
79
components/system_utils/system_utils.c
Normal 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();
|
||||
}
|
27
components/system_utils/system_utils.h
Normal file
27
components/system_utils/system_utils.h
Normal 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
|
@ -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
|
||||
)
|
||||
|
@ -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();
|
||||
|
Loading…
x
Reference in New Issue
Block a user