Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Infineon][SVE] Fix CYW30739 KVS read/write methods. #21742

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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