From 4807e1cfe11dbedd7656e534e3995edb575129a6 Mon Sep 17 00:00:00 2001 From: Filip Wandzio Date: Tue, 14 Oct 2025 22:18:55 +0200 Subject: Optimize mqtt rtt analysis method --- firmware/src/main.c | 108 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 86 insertions(+), 22 deletions(-) (limited to 'firmware/src/main.c') diff --git a/firmware/src/main.c b/firmware/src/main.c index 790959d..3f186f8 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -7,54 +7,120 @@ #include "mqtt.h" #include "wifi.h" +#include static const char *TAG = "app"; /** - * @brief Initialize NVS flash storage, handling full or incompatible pages. + * @brief Initialize NVS (Non-Volatile Storage) flash storage, handling full or + * incompatible pages. * - * @return esp_err_t ESP_OK on success, error code otherwise. + * This function checks the NVS flash storage to ensure that there is enough + * space and that the version of NVS is compatible. If the partition is full or + * incompatible, it will erase the NVS partition and attempt initialization + * again. + * + * @return esp_err_t + * - ESP_OK: Initialization successful. + * - Other error codes if initialization or erasure fails. */ static esp_err_t nvs_flash_init_check(void) { esp_err_t ret = nvs_flash_init(); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_LOGW(TAG, - "NVS partition was full or incompatible, erasing..."); - ESP_ERROR_CHECK(nvs_flash_erase()); + "NVS partition is full or incompatible, erasing..."); + ret = nvs_flash_erase(); + + if (ret != ESP_OK) { + ESP_LOGE(TAG, "Failed to erase NVS partition (%s)", + esp_err_to_name(ret)); + return ret; + } ret = nvs_flash_init(); } + if (ret != ESP_OK) ESP_LOGE(TAG, "Failed to initialize NVS (%s)", esp_err_to_name(ret)); + return ret; } /** - * @brief Application entry point. + * @brief General helper function to initialize a given component and log + * errors. * - * Initializes NVS, network interface, event loop, - * WiFi station, and MQTT client with proper error handling and logging. + * This function takes an initialization function and a component name, attempts + * to initialize the component, and logs an error if initialization fails. + * + * @param init_func A function pointer to the initialization function of the + * component. + * @param component_name A string describing the name of the component being + * initialized. + * + * @return esp_err_t + * - ESP_OK: If initialization is successful. + * - Other error codes if initialization fails. */ -void app_main(void) +static esp_err_t init_component(esp_err_t (*init_func)(void), + const char *component_name) { - esp_err_t ret = nvs_flash_init_check(); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "NVS init failed, aborting app start"); - return; - } + esp_err_t ret = init_func(); - ret = esp_netif_init(); if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to initialize network interface (%s)", + ESP_LOGE(TAG, "Failed to initialize %s (%s)", component_name, esp_err_to_name(ret)); - return; + return ret; } - ret = esp_event_loop_create_default(); - if (ret != ESP_OK) { - ESP_LOGE(TAG, "Failed to create event loop (%s)", - esp_err_to_name(ret)); + return ESP_OK; +} + +/** + * @brief Initializes all the essential components for the application. + * + * This function initializes the NVS storage, network interface, and event loop + * in sequence. If any of these components fail to initialize, the function + * returns an error, and the application will stop early. + * + * @return esp_err_t + * - ESP_OK: If all components are successfully initialized. + * - ESP_FAIL: If any component fails to initialize. + */ +esp_err_t init_app_components(void) +{ + if (init_component(nvs_flash_init_check, "NVS") != ESP_OK) { + ESP_LOGE(TAG, "NVS initialization failed, aborting app start."); + return ESP_FAIL; + } + + if (init_component(esp_netif_init, "network interface") != ESP_OK) + return ESP_FAIL; + + if (init_component(esp_event_loop_create_default, "event loop") != + ESP_OK) + return ESP_FAIL; + + return ESP_OK; +} + +/** + * @brief Main entry point of the application. + * + * This function is responsible for initializing the core components of the + * application, including the NVS storage, network interface, and event loop. If + * any initialization step fails, the application will log the error and stop. + * + * After initialization, this function will configure the WiFi station and start + * the MQTT client. + */ + +void app_main(void) +{ + if (init_app_components() != ESP_OK) { + ESP_LOGE(TAG, "App initialization failed, aborting."); return; } @@ -63,6 +129,4 @@ void app_main(void) ESP_LOGI(TAG, "Starting MQTT client"); mqtt_app_start(); - - ESP_LOGI(TAG, "Application setup completed"); } -- cgit v1.2.3