1 #include <stdio.h>
2 #include <string.h>
3 #include <sys/unistd.h>
4 #include <sys/stat.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include "esp_err.h"
8 #include "esp_log.h"
9 #include "esp_vfs_fat.h"
10 #include "lib/tinyxml2/tinyxml2.h"
11
12 using namespace tinyxml2;
13
14 static const char *TAG = "example";
15
16 // Handle of the wear levelling library instance
17 static wl_handle_t s_wl_handle = WL_INVALID_HANDLE;
18
19 // Mount path for the partition
20 const char *base_path = "/spiflash";
21
app_main(void)22 extern "C" void app_main(void)
23 {
24 // Do example setup
25 ESP_LOGI(TAG, "Setting up...");
26 esp_vfs_fat_mount_config_t mount_config;
27 mount_config.max_files = 4;
28 mount_config.format_if_mount_failed = true;
29 mount_config.allocation_unit_size = CONFIG_WL_SECTOR_SIZE;
30
31 esp_err_t err = esp_vfs_fat_spiflash_mount(base_path, "storage", &mount_config, &s_wl_handle);
32 if (err != ESP_OK) {
33 ESP_LOGE(TAG, "Failed to mount FATFS (%s)", esp_err_to_name(err));
34 return;
35 }
36
37 // The sample XML is embedded binary data. Create a file first containing the embedded
38 // so it can be accessed.
39 ESP_LOGI(TAG, "Copying sample XML to filesystem...");
40
41 extern const char data_start[] asm("_binary_sample_xml_start");
42 extern const char data_end[] asm("_binary_sample_xml_end");
43 FILE *f = fopen("/spiflash/sample.xml", "wb");
44
45 if (f == NULL) {
46 ESP_LOGE(TAG, "Failed to open file for writing");
47 return;
48 }
49 fwrite(data_start, sizeof(char), data_end - data_start + 1, f);
50 fclose(f);
51
52 // Now that the file is created, load it using tinyxml2 and parse
53 ESP_LOGI(TAG, "Reading XML file");
54
55 XMLDocument data;
56 data.LoadFile("/spiflash/sample.xml");
57
58 XMLPrinter printer;
59 data.Print(&printer);
60
61 ESP_LOGI(TAG, "Read XML data:\n%s", printer.CStr());
62
63 const char* to_data = data.FirstChildElement("note")->FirstChildElement("to")->GetText();
64 const char* from_data = data.FirstChildElement("note")->FirstChildElement("from")->GetText();
65 const char* heading_data = data.FirstChildElement("note")->FirstChildElement("heading")->GetText();
66 const char* body_data = data.FirstChildElement("note")->FirstChildElement("body")->GetText();
67
68 ESP_LOGI(TAG, "Parsed XML data:\n\nTo: %s\nFrom: %s\nHeading: %s\nBody: %s",
69 to_data, from_data, heading_data, body_data);
70
71 ESP_LOGI(TAG, "Example end");
72 }
73