I cannot copy the whole code but for example I have got this structure:
Code: Select all
/**
* Defines a Device
*/
struct DeviceNVS
{
char name[CHAR_NORMAL_ARRAY_SIZE];
SystemElementId systemElementId; // enum
ConnectedDeviceType connectedDeviceType; // enum
SystemPinOrMask pinOrMask; // enum
uint8_t enabled;
};
And I make use of the previous posted save method against that struct like this:
Code: Select all
// Relays
for (int i=0; i<relays.size(); i++)
{
DeviceNVS device = relays.get(i)->toDeviceNVS();
String key(CONFIGURATION_KEY_RELAYS);
key += i;
saveDataToModule(&moduleHandler, key, &device, sizeof(DeviceNVS));
}
Where the key is something like "DEVICE" and the module handler is created on another method like:
Code: Select all
return nvs_open("NVS module name here", openMode, moduleHandler);
The main problem my application has is that it is totally configurable by the user through a web interface. That data is sent over to the ESP32 in JSON format, parsed intro structs and then saved/read to NVS module.
So in theory I can have an indeterminate (limited by NVS size) number of different NVS elements, each one of them different in size. I wonder if I am having some kind of overwriting in keys, as data will not be a fixed configuration but a changing one. Problem only occurs when writing though.
What I am testing right now is to write all possible allowed configurations (as they are hierarchical and I limit the number of children) with fake data to "reserve" the space, and only use it when necessary.
E.g.
Code: Select all
SystemNVS (64bytes) -> 1 main struct
Subsystem_1_NVS (32 bytes) -> 10 max structs (reserve space)
Sybsystem_1_1_NVS (50 bytes) -> 20 max structs (reserve space)
Subsystem_2_NVS (16 bytes) -> 5 max structs (reserve space)
Subsystem_3_NVS (100 bytes) -> 15 max structs( reserve space)
...
and so on.
This way I would lose flexibility of NVS key/values but I can guarantee no keys are overlapped if I delete and recreate them, as they all will have their space pre reserved.
Hope it makes sense.