Skip to content

Commit

Permalink
[ESP32] Return the required length if buf is NULL when reading (#21413)
Browse files Browse the repository at this point in the history
string/blob from NVS
  • Loading branch information
shubhamdp authored Jul 29, 2022
1 parent 40ef264 commit 1e3b289
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
27 changes: 18 additions & 9 deletions src/platform/ESP32/ESP32Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,24 @@ CHIP_ERROR ESP32Config::ReadConfigValueStr(Key key, char * buf, size_t bufSize,

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

outLen = bufSize;
outLen = bufSize;

// If buf is null, nvs_get_str() sets the outLen to required length to fit the string
esp_err_t err = nvs_get_str(handle, key.Name, buf, &outLen);
if (err == ESP_ERR_NVS_NOT_FOUND)
{
outLen = 0;
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
if (err == ESP_ERR_NVS_INVALID_LENGTH && buf != NULL)
if (buf != NULL)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
if (err == ESP_ERR_NVS_INVALID_LENGTH)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
ReturnErrorCodeIf(buf[outLen - 1] != 0, CHIP_ERROR_INVALID_STRING_LENGTH);
ReturnMappedErrorOnFailure(err);
}
ReturnErrorCodeIf(buf[outLen - 1] != 0, CHIP_ERROR_INVALID_STRING_LENGTH);
ReturnMappedErrorOnFailure(err);

outLen -= 1; // Don't count trailing nul.

Expand All @@ -210,18 +215,22 @@ CHIP_ERROR ESP32Config::ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSiz

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

outLen = bufSize;
outLen = bufSize;
// If buf is null, nvs_get_blob() sets the outLen to required length to fit the blob
esp_err_t err = nvs_get_blob(handle, key.Name, buf, &outLen);
if (err == ESP_ERR_NVS_NOT_FOUND)
{
outLen = 0;
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
}
else if (err == ESP_ERR_NVS_INVALID_LENGTH && buf != NULL)
if (buf != NULL)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
if (err == ESP_ERR_NVS_INVALID_LENGTH)
{
return CHIP_ERROR_BUFFER_TOO_SMALL;
}
ReturnMappedErrorOnFailure(err);
}
ReturnMappedErrorOnFailure(err);

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 @@ -92,8 +92,11 @@ class ESP32Config
static CHIP_ERROR ReadConfigValue(Key key, bool & val);
static CHIP_ERROR ReadConfigValue(Key key, uint32_t & val);
static CHIP_ERROR ReadConfigValue(Key key, uint64_t & val);

// If buf is NULL then outLen is set to the required length to fit the string/blob
static CHIP_ERROR ReadConfigValueStr(Key key, char * buf, size_t bufSize, size_t & outLen);
static CHIP_ERROR ReadConfigValueBin(Key key, uint8_t * buf, size_t bufSize, size_t & outLen);

static CHIP_ERROR WriteConfigValue(Key key, bool val);
static CHIP_ERROR WriteConfigValue(Key key, uint32_t val);
static CHIP_ERROR WriteConfigValue(Key key, uint64_t val);
Expand Down

0 comments on commit 1e3b289

Please sign in to comment.