Skip to content

Commit

Permalink
[Infineon][SVE] Fix CYW30739 KVS read/write methods. (#21742)
Browse files Browse the repository at this point in the history
* Write methods should copy data to RAM before calling the platform API
  which doesn't support writing data from the flash.
* Read methods should return CHIP_ERROR_BUFFER_TOO_SMALL if the
  specified entry exists but the given buffer size is zero.
  • Loading branch information
hsusid authored and pull[bot] committed Aug 25, 2023
1 parent ca9aeab commit 67ccfa5
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
29 changes: 23 additions & 6 deletions src/platform/CYW30739/CYW30739Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
/* this file behaves like a config.h, comes first */
#include <platform/internal/CHIPDeviceLayerInternal.h>

#include <hal/wiced_hal_eflash.h>
#include <platform/CYW30739/CYW30739Config.h>
#include <platform_nvram.h>

Expand Down Expand Up @@ -78,12 +79,7 @@ template CHIP_ERROR CYW30739Config::ReadConfigValue(Key key, uint64_t & val);
template <typename T>
CHIP_ERROR CYW30739Config::WriteConfigValue(Key key, T val)
{
wiced_result_t result;
uint16_t write_count = wiced_hal_write_nvram(PLATFORM_NVRAM_VSID_MATTER_BASE + key, sizeof(val), (uint8_t *) &val, &result);
if (result == WICED_SUCCESS && write_count == sizeof(val))
return CHIP_NO_ERROR;
else
return CHIP_DEVICE_ERROR_CONFIG_NOT_FOUND;
return WriteConfigValueBin(key, &val, sizeof(val));
}

template CHIP_ERROR CYW30739Config::WriteConfigValue(Key key, bool val);
Expand Down Expand Up @@ -111,6 +107,22 @@ CHIP_ERROR CYW30739Config::WriteConfigValueBin(Key key, const void * data, size_
if (dataLen == 0)
return CHIP_NO_ERROR;

if (IsDataFromFlash(data))
{
/*
* Copy data to RAM before calling the platform API
* which doesn't support writing data from the flash.
*/
using namespace chip::Platform;
std::unique_ptr<void, decltype(&MemoryFree)> buffer(MemoryAlloc(dataLen), &MemoryFree);
if (!buffer)
{
return CHIP_ERROR_NO_MEMORY;
}
memcpy(buffer.get(), data, dataLen);
return WriteConfigValueBin(key, buffer.get(), dataLen);
}

wiced_result_t result;
const uint16_t write_count = wiced_hal_write_nvram(PLATFORM_NVRAM_VSID_MATTER_BASE + key, dataLen, (uint8_t *) data, &result);
if (result == WICED_SUCCESS && write_count == dataLen)
Expand Down Expand Up @@ -147,6 +159,11 @@ CHIP_ERROR CYW30739Config::FactoryResetConfig(void)

void CYW30739Config::RunConfigUnitTest(void) {}

bool CYW30739Config::IsDataFromFlash(const void * data)
{
return reinterpret_cast<void *>(FLASH_BASE_ADDRESS) <= data && data < reinterpret_cast<void *>(FLASH_BASE_ADDRESS + FLASH_SIZE);
}

} // namespace Internal
} // namespace DeviceLayer
} // namespace chip
3 changes: 3 additions & 0 deletions src/platform/CYW30739/CYW30739Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ class CYW30739Config
static bool ConfigValueExists(Key key);
static CHIP_ERROR FactoryResetConfig(void);
static void RunConfigUnitTest(void);

private:
static bool IsDataFromFlash(const void * data);
};

} // namespace Internal
Expand Down
1 change: 1 addition & 0 deletions src/platform/CYW30739/KeyValueStoreManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ CHIP_ERROR KeyValueStoreManagerImpl::_Get(const char * key, void * value, size_t

entry = FindEntry(key);
VerifyOrExit(entry != nullptr, err = CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND);
VerifyOrExit(value_size != 0, err = CHIP_ERROR_BUFFER_TOO_SMALL);

size_t byte_count;
err = CYW30739Config::ReadConfigValueBin(entry->GetValueConfigKey(), value, value_size, byte_count);
Expand Down

0 comments on commit 67ccfa5

Please sign in to comment.