From 704d1c9c0bbc90d581f1df2bc330e1a72001ef42 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 26 Jul 2025 10:36:31 +0200 Subject: [PATCH] Added Test of NVS and Partion API --- main/main.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/main/main.c b/main/main.c index ef4d985..7784794 100644 --- a/main/main.c +++ b/main/main.c @@ -1,7 +1,11 @@ #include "client_handler.h" #include "driver/gpio.h" #include "driver/uart.h" +#include "esp_err.h" #include "esp_log.h" +#include "esp_log_buffer.h" +#include "esp_ota_ops.h" +#include "esp_partition.h" #include "esp_phy_init.h" #include "esp_rom_gpio.h" #include "esp_timer.h" @@ -10,6 +14,7 @@ #include "hal/uart_types.h" #include "message_handler.h" #include "message_parser.h" +#include "nvs.h" #include "nvs_flash.h" #include "communication_handler.h" @@ -181,7 +186,70 @@ void app_main(void) { ESP_ERROR_CHECK(esp_now_register_recv_cb(client_receive_callback)); } - init_com(&clientList); + init_com(&clientList, 1); + if (ret < 0) { + ESP_LOGE(TAG, "Could not Init ESP NOW Communication!"); + } + + esp_partition_iterator_t partition_iter = esp_partition_find( + ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, NULL); + + while (partition_iter != NULL) { + const esp_partition_t *part1 = esp_partition_get(partition_iter); + ESP_LOGI(TAG, "Partition: %s, Address: %d, Size %d", part1->label, + part1->address, part1->size); + partition_iter = esp_partition_next(partition_iter); + } + + const esp_partition_t *running = esp_ota_get_running_partition(); + ESP_LOGI(TAG, "OTA: Running Partition: %s", running->label); + uint8_t ota_part_count = esp_ota_get_app_partition_count(); + ESP_LOGI(TAG, "OTA: Got %d OTA Partitions", ota_part_count); + + esp_ota_img_states_t ota_state; + if (esp_ota_get_state_partition(running, &ota_state) == ESP_OK) { + ESP_LOGI(TAG, "OTA: Partition State : %d", ota_state); + if (ota_state == ESP_OTA_IMG_PENDING_VERIFY) { + // run diagnostic function ... + bool diagnostic_is_ok = true; // TODO: a real function that checks if + // everything is running properly + if (diagnostic_is_ok) { + ESP_LOGI( + TAG, + "Diagnostics completed successfully! Continuing execution ..."); + // esp_ota_mark_app_valid_cancel_rollback(); + } else { + ESP_LOGE( + TAG, + "Diagnostics failed! Start rollback to the previous version ..."); + // esp_ota_mark_app_invalid_rollback_and_reboot(); + } + } + } + + const char nvs_part_name[] = "nvs_data"; + const char nvs_namespace_name[] = "saved_clients"; + ret = nvs_flash_init_partition(nvs_part_name); + if (ret == ESP_ERR_NVS_NO_FREE_PAGES || + ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase_partition(nvs_part_name)); + ret = nvs_flash_init_partition(nvs_part_name); + } + ESP_ERROR_CHECK(ret); + nvs_handle_t nt; + ESP_ERROR_CHECK(nvs_open_from_partition(nvs_part_name, nvs_namespace_name, + NVS_READWRITE, &nt)); + + int32_t outval; + ret = nvs_get_i32(nt, "test_entry", &outval); + if (ret == ESP_ERR_NVS_NOT_FOUND) { + ESP_ERROR_CHECK(nvs_set_i32(nt, "test_entry", 6969)); + ESP_ERROR_CHECK(nvs_commit(nt)); + ESP_LOGE(TAG, "Nichts im Flash gefunden hab was dahin geschrieben"); + } else if (ret == ESP_OK) { + ESP_LOGE(TAG, "DAS WAR IM FLASH %d", outval); + } + nvs_close(nt); // Tasks starten basierend auf Master/Client if (isMaster) {