Skip to content

Commit

Permalink
[SL-UP] Provision: Dynamic buffer allocation (#158)
Browse files Browse the repository at this point in the history
Co-authored-by: lpbeliveau-silabs <[email protected]>
  • Loading branch information
rcasallas-silabs and lpbeliveau-silabs authored Dec 10, 2024
1 parent 53cd9dd commit 750432a
Show file tree
Hide file tree
Showing 5 changed files with 1,130 additions and 22 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
36 changes: 20 additions & 16 deletions examples/platform/silabs/provision/ProvisionStorageDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,29 @@ 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. The "max" argument is used to ensure
* that the padding won't exceed the limits of the buffer.
*/
CHIP_ERROR WritePage(uint32_t addr, const uint8_t * data, size_t size, size_t max)
{
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 the input data is smaller than the 32-bit size, pad the buffer with "0xff"
if(size_32 != size)
{
uint8_t * p = (uint8_t *) data;
VerifyOrReturnError(size_32 <= max, CHIP_ERROR_BUFFER_TOO_SMALL);
memset(p + size, 0xff, size_32 - size);
size = size_32;
}
return chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, data, size);
}

CHIP_ERROR WriteFile(Storage & store, SilabsConfig::Key offset_key, SilabsConfig::Key size_key, const ByteSpan & value)
Expand All @@ -89,17 +104,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.GetBufferSize()));

// Store file offset
ReturnErrorOnFailure(SilabsConfig::WriteConfigValue(offset_key, (uint32_t) sCredentialsOffset));
Expand All @@ -119,7 +124,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 750432a

Please sign in to comment.