Skip to content

Commit 6ffcd19

Browse files
[Silabs] Provision: Dynamic buffer allocation. (#36964)
* [Silabs] Provision: Dynamic buffer allocation. * Code review.
1 parent 083803c commit 6ffcd19

File tree

5 files changed

+349
-455
lines changed

5 files changed

+349
-455
lines changed

examples/platform/silabs/provision/ProvisionStorageCustom.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#include "ProvisionStorage.h"
17+
#include <ProvisionStorage.h>
1818
#include <algorithm>
1919
#include <lib/support/CodeUtils.h>
2020
#include <string.h>

examples/platform/silabs/provision/ProvisionStorageDefault.cpp

+26-16
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,35 @@ CHIP_ERROR ErasePage(uint32_t addr)
7070
return chip::DeviceLayer::Silabs::GetPlatform().FlashErasePage(addr);
7171
}
7272

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

78-
size_t RoundNearest(size_t n, size_t multiple)
78+
/**
79+
* Writes "size" bytes to the flash page. The data is padded with 0xff
80+
* up to the nearest 32-bit boundary.
81+
*/
82+
CHIP_ERROR WritePage(uint32_t addr, const uint8_t * data, size_t size)
7983
{
80-
return (n % multiple) > 0 ? n + (multiple - n % multiple) : n;
84+
// The flash driver fails if the size is not a multiple of 4 (32-bits)
85+
size_t size_32 = RoundNearest(size, 4);
86+
if (size_32 == size)
87+
{
88+
// The given data is already aligned to 32-bit
89+
return chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, data, size);
90+
}
91+
else
92+
{
93+
// Create a temporary buffer, and pad it with "0xff"
94+
uint8_t * p = static_cast<uint8_t *>(Platform::MemoryAlloc(size_32));
95+
VerifyOrReturnError(p != nullptr, CHIP_ERROR_INTERNAL);
96+
memcpy(p, data, size);
97+
memset(p + size, 0xff, size_32 - size);
98+
CHIP_ERROR err = chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, p, size_32);
99+
Platform::MemoryFree(p);
100+
return err;
101+
}
81102
}
82103

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

92-
memcpy(Storage::aux_buffer, value.data(), value.size());
93-
if (value.size() < Storage::kArgumentSizeMax)
94-
{
95-
memset(Storage::aux_buffer + value.size(), 0xff, Storage::kArgumentSizeMax - value.size());
96-
}
97-
98-
ChipLogProgress(DeviceLayer, "WriteFile, addr:0x%06x+%03u, size:%u", (unsigned) base_addr, (unsigned) sCredentialsOffset,
99-
(unsigned) value.size());
100-
// ChipLogByteSpan(DeviceLayer, ByteSpan(value.data(), value.size() < kDebugLength ? value.size() : kDebugLength));
101-
102-
ReturnErrorOnFailure(WritePage(base_addr + sCredentialsOffset, Storage::aux_buffer, Storage::kArgumentSizeMax));
113+
ReturnErrorOnFailure(WritePage(base_addr + sCredentialsOffset, value.data(), value.size()));
103114

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

0 commit comments

Comments
 (0)