diff options
| author | Filip Wandzio <contact@philw.dev> | 2025-10-14 22:18:55 +0200 |
|---|---|---|
| committer | Filip Wandzio <contact@philw.dev> | 2025-10-14 22:18:55 +0200 |
| commit | 4807e1cfe11dbedd7656e534e3995edb575129a6 (patch) | |
| tree | 48afc894f2b8c0ec1ad82ca433a77e4b247fa774 /firmware/src/main.c | |
| parent | 01713bbe20d2cf5aafbe5eb32721d3e4fc5823d8 (diff) | |
| download | e1-4807e1cfe11dbedd7656e534e3995edb575129a6.tar.gz e1-4807e1cfe11dbedd7656e534e3995edb575129a6.zip | |
Optimize mqtt rtt analysis method
Diffstat (limited to 'firmware/src/main.c')
| -rw-r--r-- | firmware/src/main.c | 108 |
1 files changed, 86 insertions, 22 deletions
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 @@ | |||
| 7 | #include "mqtt.h" | 7 | #include "mqtt.h" |
| 8 | #include "wifi.h" | 8 | #include "wifi.h" |
| 9 | 9 | ||
| 10 | #include <stdio.h> | ||
| 10 | static const char *TAG = "app"; | 11 | static const char *TAG = "app"; |
| 11 | 12 | ||
| 12 | /** | 13 | /** |
| 13 | * @brief Initialize NVS flash storage, handling full or incompatible pages. | 14 | * @brief Initialize NVS (Non-Volatile Storage) flash storage, handling full or |
| 15 | * incompatible pages. | ||
| 14 | * | 16 | * |
| 15 | * @return esp_err_t ESP_OK on success, error code otherwise. | 17 | * This function checks the NVS flash storage to ensure that there is enough |
| 18 | * space and that the version of NVS is compatible. If the partition is full or | ||
| 19 | * incompatible, it will erase the NVS partition and attempt initialization | ||
| 20 | * again. | ||
| 21 | * | ||
| 22 | * @return esp_err_t | ||
| 23 | * - ESP_OK: Initialization successful. | ||
| 24 | * - Other error codes if initialization or erasure fails. | ||
| 16 | */ | 25 | */ |
| 17 | static esp_err_t nvs_flash_init_check(void) | 26 | static esp_err_t nvs_flash_init_check(void) |
| 18 | { | 27 | { |
| 19 | esp_err_t ret = nvs_flash_init(); | 28 | esp_err_t ret = nvs_flash_init(); |
| 29 | |||
| 20 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES || | 30 | if (ret == ESP_ERR_NVS_NO_FREE_PAGES || |
| 21 | ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { | 31 | ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { |
| 22 | ESP_LOGW(TAG, | 32 | ESP_LOGW(TAG, |
| 23 | "NVS partition was full or incompatible, erasing..."); | 33 | "NVS partition is full or incompatible, erasing..."); |
| 24 | ESP_ERROR_CHECK(nvs_flash_erase()); | 34 | ret = nvs_flash_erase(); |
| 35 | |||
| 36 | if (ret != ESP_OK) { | ||
| 37 | ESP_LOGE(TAG, "Failed to erase NVS partition (%s)", | ||
| 38 | esp_err_to_name(ret)); | ||
| 39 | return ret; | ||
| 40 | } | ||
| 25 | ret = nvs_flash_init(); | 41 | ret = nvs_flash_init(); |
| 26 | } | 42 | } |
| 43 | |||
| 27 | if (ret != ESP_OK) | 44 | if (ret != ESP_OK) |
| 28 | ESP_LOGE(TAG, "Failed to initialize NVS (%s)", | 45 | ESP_LOGE(TAG, "Failed to initialize NVS (%s)", |
| 29 | esp_err_to_name(ret)); | 46 | esp_err_to_name(ret)); |
| 47 | |||
| 30 | return ret; | 48 | return ret; |
| 31 | } | 49 | } |
| 32 | 50 | ||
| 33 | /** | 51 | /** |
| 34 | * @brief Application entry point. | 52 | * @brief General helper function to initialize a given component and log |
| 53 | * errors. | ||
| 35 | * | 54 | * |
| 36 | * Initializes NVS, network interface, event loop, | 55 | * This function takes an initialization function and a component name, attempts |
| 37 | * WiFi station, and MQTT client with proper error handling and logging. | 56 | * to initialize the component, and logs an error if initialization fails. |
| 57 | * | ||
| 58 | * @param init_func A function pointer to the initialization function of the | ||
| 59 | * component. | ||
| 60 | * @param component_name A string describing the name of the component being | ||
| 61 | * initialized. | ||
| 62 | * | ||
| 63 | * @return esp_err_t | ||
| 64 | * - ESP_OK: If initialization is successful. | ||
| 65 | * - Other error codes if initialization fails. | ||
| 38 | */ | 66 | */ |
| 39 | void app_main(void) | 67 | static esp_err_t init_component(esp_err_t (*init_func)(void), |
| 68 | const char *component_name) | ||
| 40 | { | 69 | { |
| 41 | esp_err_t ret = nvs_flash_init_check(); | 70 | esp_err_t ret = init_func(); |
| 42 | if (ret != ESP_OK) { | ||
| 43 | ESP_LOGE(TAG, "NVS init failed, aborting app start"); | ||
| 44 | return; | ||
| 45 | } | ||
| 46 | 71 | ||
| 47 | ret = esp_netif_init(); | ||
| 48 | if (ret != ESP_OK) { | 72 | if (ret != ESP_OK) { |
| 49 | ESP_LOGE(TAG, "Failed to initialize network interface (%s)", | 73 | ESP_LOGE(TAG, "Failed to initialize %s (%s)", component_name, |
| 50 | esp_err_to_name(ret)); | 74 | esp_err_to_name(ret)); |
| 51 | return; | 75 | return ret; |
| 52 | } | 76 | } |
| 53 | 77 | ||
| 54 | ret = esp_event_loop_create_default(); | 78 | return ESP_OK; |
| 55 | if (ret != ESP_OK) { | 79 | } |
| 56 | ESP_LOGE(TAG, "Failed to create event loop (%s)", | 80 | |
| 57 | esp_err_to_name(ret)); | 81 | /** |
| 82 | * @brief Initializes all the essential components for the application. | ||
| 83 | * | ||
| 84 | * This function initializes the NVS storage, network interface, and event loop | ||
| 85 | * in sequence. If any of these components fail to initialize, the function | ||
| 86 | * returns an error, and the application will stop early. | ||
| 87 | * | ||
| 88 | * @return esp_err_t | ||
| 89 | * - ESP_OK: If all components are successfully initialized. | ||
| 90 | * - ESP_FAIL: If any component fails to initialize. | ||
| 91 | */ | ||
| 92 | esp_err_t init_app_components(void) | ||
| 93 | { | ||
| 94 | if (init_component(nvs_flash_init_check, "NVS") != ESP_OK) { | ||
| 95 | ESP_LOGE(TAG, "NVS initialization failed, aborting app start."); | ||
| 96 | return ESP_FAIL; | ||
| 97 | } | ||
| 98 | |||
| 99 | if (init_component(esp_netif_init, "network interface") != ESP_OK) | ||
| 100 | return ESP_FAIL; | ||
| 101 | |||
| 102 | if (init_component(esp_event_loop_create_default, "event loop") != | ||
| 103 | ESP_OK) | ||
| 104 | return ESP_FAIL; | ||
| 105 | |||
| 106 | return ESP_OK; | ||
| 107 | } | ||
| 108 | |||
| 109 | /** | ||
| 110 | * @brief Main entry point of the application. | ||
| 111 | * | ||
| 112 | * This function is responsible for initializing the core components of the | ||
| 113 | * application, including the NVS storage, network interface, and event loop. If | ||
| 114 | * any initialization step fails, the application will log the error and stop. | ||
| 115 | * | ||
| 116 | * After initialization, this function will configure the WiFi station and start | ||
| 117 | * the MQTT client. | ||
| 118 | */ | ||
| 119 | |||
| 120 | void app_main(void) | ||
| 121 | { | ||
| 122 | if (init_app_components() != ESP_OK) { | ||
| 123 | ESP_LOGE(TAG, "App initialization failed, aborting."); | ||
| 58 | return; | 124 | return; |
| 59 | } | 125 | } |
| 60 | 126 | ||
| @@ -63,6 +129,4 @@ void app_main(void) | |||
| 63 | 129 | ||
| 64 | ESP_LOGI(TAG, "Starting MQTT client"); | 130 | ESP_LOGI(TAG, "Starting MQTT client"); |
| 65 | mqtt_app_start(); | 131 | mqtt_app_start(); |
| 66 | |||
| 67 | ESP_LOGI(TAG, "Application setup completed"); | ||
| 68 | } | 132 | } |
