diff --git a/examples/build_system/cmake/import_lib/main/CMakeLists.txt b/examples/build_system/cmake/import_lib/main/CMakeLists.txt index 3477a4a46d..6e9d7d94ca 100644 --- a/examples/build_system/cmake/import_lib/main/CMakeLists.txt +++ b/examples/build_system/cmake/import_lib/main/CMakeLists.txt @@ -1,4 +1,7 @@ -idf_component_register(SRCS "main.cpp" +idf_component_register(SRCS "import_lib_example_main.cpp" INCLUDE_DIRS "." - EMBED_TXTFILES sample.xml PRIV_REQUIRES tinyxml2 fatfs) + +# Create a FAT filesystem image from the contents of data/ subdirectory, +# The image will be flashed into the 'storage' partition when 'idf.py flash' is used. +fatfs_create_spiflash_image(storage data FLASH_IN_PROJECT) diff --git a/examples/build_system/cmake/import_lib/main/sample.xml b/examples/build_system/cmake/import_lib/main/data/sample.xml similarity index 100% rename from examples/build_system/cmake/import_lib/main/sample.xml rename to examples/build_system/cmake/import_lib/main/data/sample.xml diff --git a/examples/build_system/cmake/import_lib/main/import_lib_example_main.cpp b/examples/build_system/cmake/import_lib/main/import_lib_example_main.cpp new file mode 100644 index 0000000000..069296cb03 --- /dev/null +++ b/examples/build_system/cmake/import_lib/main/import_lib_example_main.cpp @@ -0,0 +1,49 @@ +/* + * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD + * + * SPDX-License-Identifier: Unlicense OR CC0-1.0 + */ +#include "esp_err.h" +#include "esp_log.h" +#include "esp_vfs_fat.h" +#include "tinyxml2.h" + +static const char *TAG = "example"; + + +extern "C" void app_main(void) +{ + ESP_LOGI(TAG, "Initializing the filesystem"); + esp_vfs_fat_mount_config_t mount_config = {}; + mount_config.max_files = 1; + + wl_handle_t wl_handle = WL_INVALID_HANDLE; + esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl("/spiflash", "storage", &mount_config, &wl_handle); + if (err != ESP_OK) { + ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err)); + return; + } + + // Load the XML file from the filesystem and parse it using tinyxml2 + ESP_LOGI(TAG, "Reading XML file"); + tinyxml2::XMLDocument data; + data.LoadFile("/spiflash/sample.xml"); + + tinyxml2::XMLPrinter printer; + data.Print(&printer); + + ESP_LOGI(TAG, "Read XML data:\n%s", printer.CStr()); + + const char* to_data = data.FirstChildElement("note")->FirstChildElement("to")->GetText(); + const char* from_data = data.FirstChildElement("note")->FirstChildElement("from")->GetText(); + const char* heading_data = data.FirstChildElement("note")->FirstChildElement("heading")->GetText(); + const char* body_data = data.FirstChildElement("note")->FirstChildElement("body")->GetText(); + + ESP_LOGI(TAG, "Parsed XML data:\n\nTo: %s\nFrom: %s\nHeading: %s\nBody: %s", + to_data, from_data, heading_data, body_data); + + // Clean up + esp_vfs_fat_spiflash_unmount_rw_wl("/spiflash", wl_handle); + + ESP_LOGI(TAG, "Example end"); +} diff --git a/examples/build_system/cmake/import_lib/main/main.cpp b/examples/build_system/cmake/import_lib/main/main.cpp deleted file mode 100644 index 970b2b5e15..0000000000 --- a/examples/build_system/cmake/import_lib/main/main.cpp +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include "esp_err.h" -#include "esp_log.h" -#include "esp_vfs_fat.h" -#include "tinyxml2.h" - -using namespace tinyxml2; - -static const char *TAG = "example"; - -// Handle of the wear levelling library instance -static wl_handle_t s_wl_handle = WL_INVALID_HANDLE; - -// Mount path for the partition -const char *base_path = "/spiflash"; - -extern "C" void app_main(void) -{ - // Do example setup - ESP_LOGI(TAG, "Setting up..."); - esp_vfs_fat_mount_config_t mount_config; - mount_config.max_files = 4; - mount_config.format_if_mount_failed = true; - mount_config.allocation_unit_size = CONFIG_WL_SECTOR_SIZE; - - esp_err_t err = esp_vfs_fat_spiflash_mount_rw_wl(base_path, "storage", &mount_config, &s_wl_handle); - if (err != ESP_OK) { - ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err)); - return; - } - - // The sample XML is embedded binary data. Create a file first containing the embedded - // so it can be accessed. - ESP_LOGI(TAG, "Copying sample XML to filesystem..."); - - extern const char data_start[] asm("_binary_sample_xml_start"); - extern const char data_end[] asm("_binary_sample_xml_end"); - FILE *f = fopen("/spiflash/sample.xml", "wb"); - - if (f == NULL) { - ESP_LOGE(TAG, "Failed to open file for writing"); - return; - } - fwrite(data_start, sizeof(char), data_end - data_start + 1, f); - fclose(f); - - // Now that the file is created, load it using tinyxml2 and parse - ESP_LOGI(TAG, "Reading XML file"); - - XMLDocument data; - data.LoadFile("/spiflash/sample.xml"); - - XMLPrinter printer; - data.Print(&printer); - - ESP_LOGI(TAG, "Read XML data:\n%s", printer.CStr()); - - const char* to_data = data.FirstChildElement("note")->FirstChildElement("to")->GetText(); - const char* from_data = data.FirstChildElement("note")->FirstChildElement("from")->GetText(); - const char* heading_data = data.FirstChildElement("note")->FirstChildElement("heading")->GetText(); - const char* body_data = data.FirstChildElement("note")->FirstChildElement("body")->GetText(); - - ESP_LOGI(TAG, "Parsed XML data:\n\nTo: %s\nFrom: %s\nHeading: %s\nBody: %s", - to_data, from_data, heading_data, body_data); - - ESP_LOGI(TAG, "Example end"); -} diff --git a/tools/ci/check_copyright_ignore.txt b/tools/ci/check_copyright_ignore.txt index b2adaca858..d3faf1a6d7 100644 --- a/tools/ci/check_copyright_ignore.txt +++ b/tools/ci/check_copyright_ignore.txt @@ -1733,7 +1733,6 @@ examples/bluetooth/nimble/bleprph_wifi_coex/main/bleprph.h examples/bluetooth/nimble/bleprph_wifi_coex/main/gatt_svr.c examples/bluetooth/nimble/bleprph_wifi_coex/main/main.c examples/build_system/cmake/component_manager/main/component_manager.c -examples/build_system/cmake/import_lib/main/main.cpp examples/build_system/cmake/import_prebuilt/main/main.c examples/build_system/cmake/import_prebuilt/prebuilt/components/prebuilt/prebuilt.c examples/build_system/cmake/import_prebuilt/prebuilt/components/prebuilt/prebuilt.h