Skip to content

Commit

Permalink
[ESP32] Make partition name configurable for chip namespaces (#17233)
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhamdp authored and pull[bot] committed Feb 19, 2024
1 parent 13ae496 commit 1078056
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 16 deletions.
21 changes: 21 additions & 0 deletions config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -780,4 +780,25 @@ menu "CHIP Device Layer"
The gn target of the external platform.
endmenu

menu "Matter Manufacturing Options"
config CHIP_FACTORY_NAMESPACE_PARTITION_LABEL
string "chip-factory namespace partition label"
default "nvs"
help
Label of the partition to store key-values in the "chip-factory" namespace.

config CHIP_CONFIG_NAMESPACE_PARTITION_LABEL
string "chip-config namespace partition label"
default "nvs"
help
Label of the partition to store key-values in the "chip-config" namespace.

config CHIP_COUNTERS_NAMESPACE_PARTITION_LABEL
string "chip-counters namespace partition label"
default "nvs"
help
Label of the partition to store key-values in the "chip-counters" namespace.

endmenu

endmenu
3 changes: 3 additions & 0 deletions src/platform/ESP32/CHIPDevicePlatformConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,6 @@
#define CHIP_DEVICE_CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE CONFIG_EVENT_LOGGING_INFO_BUFFER_SIZE
#define CHIP_DEVICE_CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE CONFIG_EVENT_LOGGING_DEBUG_BUFFER_SIZE
#define CHIP_DEVICE_CONFIG_LOG_PROVISIONING_HASH CONFIG_LOG_PROVISIONING_HASH
#define CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION_LABEL
#define CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION_LABEL
#define CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION_LABEL
9 changes: 9 additions & 0 deletions src/platform/ESP32/ConfigurationManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ CHIP_ERROR ConfigurationManagerImpl::Init()
CHIP_ERROR err;
uint32_t rebootCount;

// Initialize the nvs partitions,
// nvs_flash_init_partition() will initialize the partition only if it is not already initialized.
esp_err_t esp_err = nvs_flash_init_partition(CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION);
SuccessOrExit(MapConfigError(esp_err));
esp_err = nvs_flash_init_partition(CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION);
SuccessOrExit(MapConfigError(esp_err));
esp_err = nvs_flash_init_partition(CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION);
SuccessOrExit(MapConfigError(esp_err));

// Force initialization of NVS namespaces if they doesn't already exist.
err = ESP32Config::EnsureNamespace(ESP32Config::kConfigNamespace_ChipFactory);
SuccessOrExit(err);
Expand Down
46 changes: 32 additions & 14 deletions src/platform/ESP32/ESP32Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,30 @@ const ESP32Config::Key ESP32Config::kCounterKey_TotalOperationalHours = { kConfi
// Prefix used for NVS keys that contain Chip group encryption keys.
const char ESP32Config::kGroupKeyNamePrefix[] = "gk-";

const char * ESP32Config::GetPartitionLabelByNamespace(const char * ns)
{
if (strcmp(ns, kConfigNamespace_ChipFactory) == 0)
{
return CHIP_DEVICE_CONFIG_CHIP_FACTORY_NAMESPACE_PARTITION;
}
else if (strcmp(ns, kConfigNamespace_ChipConfig) == 0)
{
return CHIP_DEVICE_CONFIG_CHIP_CONFIG_NAMESPACE_PARTITION;
}
else if (strcmp(ns, kConfigNamespace_ChipCounters))
{
return CHIP_DEVICE_CONFIG_CHIP_COUNTERS_NAMESPACE_PARTITION;
}

return NVS_DEFAULT_PART_NAME;
}

CHIP_ERROR ESP32Config::ReadConfigValue(Key key, bool & val)
{
ScopedNvsHandle handle;
uint32_t intVal;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace)));

esp_err_t err = nvs_get_u32(handle, key.Name, &intVal);
if (err == ESP_ERR_NVS_NOT_FOUND)
Expand All @@ -108,7 +126,7 @@ CHIP_ERROR ESP32Config::ReadConfigValue(Key key, uint32_t & val)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace)));

esp_err_t err = nvs_get_u32(handle, key.Name, &val);
if (err == ESP_ERR_NVS_NOT_FOUND)
Expand All @@ -124,7 +142,7 @@ CHIP_ERROR ESP32Config::ReadConfigValue(Key key, uint64_t & val)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace)));

// Special case the MfrDeviceId value, optionally allowing it to be read as a blob containing
// a 64-bit big-endian integer, instead of a u64 value.
Expand Down Expand Up @@ -161,7 +179,7 @@ CHIP_ERROR ESP32Config::ReadConfigValueStr(Key key, char * buf, size_t bufSize,
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace)));

outLen = bufSize;
esp_err_t err = nvs_get_str(handle, key.Name, buf, &outLen);
Expand All @@ -185,7 +203,7 @@ CHIP_ERROR ESP32Config::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSiz
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READONLY, GetPartitionLabelByNamespace(key.Namespace)));

outLen = bufSize;
esp_err_t err = nvs_get_blob(handle, key.Name, buf, &outLen);
Expand All @@ -207,7 +225,7 @@ CHIP_ERROR ESP32Config::WriteConfigValue(Key key, bool val)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace)));
ReturnMappedErrorOnFailure(nvs_set_u32(handle, key.Name, val ? 1 : 0));

// Commit the value to the persistent store.
Expand All @@ -221,7 +239,7 @@ CHIP_ERROR ESP32Config::WriteConfigValue(Key key, uint32_t val)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace)));
ReturnMappedErrorOnFailure(nvs_set_u32(handle, key.Name, val));

// Commit the value to the persistent store.
Expand All @@ -235,7 +253,7 @@ CHIP_ERROR ESP32Config::WriteConfigValue(Key key, uint64_t val)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace)));
ReturnMappedErrorOnFailure(nvs_set_u64(handle, key.Name, val));

// Commit the value to the persistent store.
Expand All @@ -251,7 +269,7 @@ CHIP_ERROR ESP32Config::WriteConfigValueStr(Key key, const char * str)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace)));
ReturnMappedErrorOnFailure(nvs_set_str(handle, key.Name, str));

// Commit the value to the persistent store.
Expand Down Expand Up @@ -283,7 +301,7 @@ CHIP_ERROR ESP32Config::WriteConfigValueBin(Key key, const uint8_t * data, size_

if (data != NULL)
{
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace)));
ReturnMappedErrorOnFailure(nvs_set_blob(handle, key.Name, data, dataLen));

// Commit the value to the persistent store.
Expand All @@ -300,7 +318,7 @@ CHIP_ERROR ESP32Config::ClearConfigValue(Key key)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(key.Namespace, NVS_READWRITE, GetPartitionLabelByNamespace(key.Namespace)));

esp_err_t err = nvs_erase_key(handle, key.Name);
if (err == ESP_ERR_NVS_NOT_FOUND)
Expand Down Expand Up @@ -337,14 +355,14 @@ CHIP_ERROR ESP32Config::EnsureNamespace(const char * ns)
{
ScopedNvsHandle handle;

CHIP_ERROR err = handle.Open(ns, NVS_READONLY);
CHIP_ERROR err = handle.Open(ns, NVS_READONLY, GetPartitionLabelByNamespace(ns));
if (err == CHIP_NO_ERROR)
{
return CHIP_NO_ERROR;
}
if (err == ESP32Utils::MapError(ESP_ERR_NVS_NOT_FOUND))
{
ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE, GetPartitionLabelByNamespace(ns)));
ReturnMappedErrorOnFailure(nvs_commit(handle));
return CHIP_NO_ERROR;
}
Expand All @@ -355,7 +373,7 @@ CHIP_ERROR ESP32Config::ClearNamespace(const char * ns)
{
ScopedNvsHandle handle;

ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE));
ReturnErrorOnFailure(handle.Open(ns, NVS_READWRITE, GetPartitionLabelByNamespace(ns)));
ReturnMappedErrorOnFailure(nvs_erase_all(handle));
ReturnMappedErrorOnFailure(nvs_commit(handle));
return CHIP_NO_ERROR;
Expand Down
3 changes: 3 additions & 0 deletions src/platform/ESP32/ESP32Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ class ESP32Config
static CHIP_ERROR ClearNamespace(const char * ns);

static void RunConfigUnitTest(void);

private:
static const char * GetPartitionLabelByNamespace(const char * ns);
};

struct ESP32Config::Key
Expand Down
4 changes: 2 additions & 2 deletions src/platform/ESP32/ScopedNvsHandle.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ class ScopedNvsHandle
public:
ScopedNvsHandle() : mIsOpen(false) {}
~ScopedNvsHandle() { Close(); }
CHIP_ERROR Open(const char * name, nvs_open_mode_t open_mode)
CHIP_ERROR Open(const char * name, nvs_open_mode_t open_mode, const char * partition_label = NVS_DEFAULT_PART_NAME)
{
esp_err_t err = nvs_open(name, open_mode, &mHandle);
esp_err_t err = nvs_open_from_partition(partition_label, name, open_mode, &mHandle);
if (err == ESP_OK)
{
mIsOpen = true;
Expand Down

0 comments on commit 1078056

Please sign in to comment.