Skip to content

Commit

Permalink
[Silabs] Provision: Dynamic buffer allocation. (#36964)
Browse files Browse the repository at this point in the history
* [Silabs] Provision: Dynamic buffer allocation.

* Code review.
  • Loading branch information
rcasallas-silabs authored Jan 15, 2025
1 parent 083803c commit 6ffcd19
Show file tree
Hide file tree
Showing 5 changed files with 349 additions and 455 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ProvisionStorage.h"
#include <ProvisionStorage.h>
#include <algorithm>
#include <lib/support/CodeUtils.h>
#include <string.h>
Expand Down
42 changes: 26 additions & 16 deletions examples/platform/silabs/provision/ProvisionStorageDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,35 @@ CHIP_ERROR ErasePage(uint32_t addr)
return chip::DeviceLayer::Silabs::GetPlatform().FlashErasePage(addr);
}

CHIP_ERROR WritePage(uint32_t addr, const uint8_t * data, size_t size)
size_t RoundNearest(size_t n, size_t multiple)
{
return chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, data, size);
return (n % multiple) > 0 ? n + (multiple - n % multiple) : n;
}

size_t RoundNearest(size_t n, size_t multiple)
/**
* Writes "size" bytes to the flash page. The data is padded with 0xff
* up to the nearest 32-bit boundary.
*/
CHIP_ERROR WritePage(uint32_t addr, const uint8_t * data, size_t size)
{
return (n % multiple) > 0 ? n + (multiple - n % multiple) : n;
// The flash driver fails if the size is not a multiple of 4 (32-bits)
size_t size_32 = RoundNearest(size, 4);
if (size_32 == size)
{
// The given data is already aligned to 32-bit
return chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, data, size);
}
else
{
// Create a temporary buffer, and pad it with "0xff"
uint8_t * p = static_cast<uint8_t *>(Platform::MemoryAlloc(size_32));
VerifyOrReturnError(p != nullptr, CHIP_ERROR_INTERNAL);
memcpy(p, data, size);
memset(p + size, 0xff, size_32 - size);
CHIP_ERROR err = chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, p, size_32);
Platform::MemoryFree(p);
return err;
}
}

CHIP_ERROR WriteFile(Storage & store, SilabsConfig::Key offset_key, SilabsConfig::Key size_key, const ByteSpan & value)
Expand All @@ -89,17 +110,7 @@ CHIP_ERROR WriteFile(Storage & store, SilabsConfig::Key offset_key, SilabsConfig
ReturnErrorOnFailure(ErasePage(base_addr));
}

memcpy(Storage::aux_buffer, value.data(), value.size());
if (value.size() < Storage::kArgumentSizeMax)
{
memset(Storage::aux_buffer + value.size(), 0xff, Storage::kArgumentSizeMax - value.size());
}

ChipLogProgress(DeviceLayer, "WriteFile, addr:0x%06x+%03u, size:%u", (unsigned) base_addr, (unsigned) sCredentialsOffset,
(unsigned) value.size());
// ChipLogByteSpan(DeviceLayer, ByteSpan(value.data(), value.size() < kDebugLength ? value.size() : kDebugLength));

ReturnErrorOnFailure(WritePage(base_addr + sCredentialsOffset, Storage::aux_buffer, Storage::kArgumentSizeMax));
ReturnErrorOnFailure(WritePage(base_addr + sCredentialsOffset, value.data(), value.size()));

// Store file offset
ReturnErrorOnFailure(SilabsConfig::WriteConfigValue(offset_key, (uint32_t) sCredentialsOffset));
Expand All @@ -119,7 +130,6 @@ CHIP_ERROR ReadFileByOffset(Storage & store, const char * description, uint32_t
ByteSpan span(address, size);
ChipLogProgress(DeviceLayer, "%s, addr:0x%06x+%03u, size:%u", description, (unsigned) base_addr, (unsigned) offset,
(unsigned) size);
// ChipLogByteSpan(DeviceLayer, ByteSpan(span.data(), span.size() < kDebugLength ? span.size() : kDebugLength));
return CopySpanToMutableSpan(span, value);
}

Expand Down
Loading

0 comments on commit 6ffcd19

Please sign in to comment.