Skip to content

Commit d0b23a9

Browse files
rcasallas-silabsmkardous-silabs
authored andcommitted
[Silabs] Provision: Dynamic buffer allocation.
1 parent 9fb1053 commit d0b23a9

File tree

5 files changed

+343
-455
lines changed

5 files changed

+343
-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

+20-16
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,29 @@ 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. The "max" argument is used to ensure
81+
* that the padding won't exceed the limits of the buffer.
82+
*/
83+
CHIP_ERROR WritePage(uint32_t addr, const uint8_t * data, size_t size, size_t max)
7984
{
80-
return (n % multiple) > 0 ? n + (multiple - n % multiple) : n;
85+
// The flash driver fails if the size is not a multiple of 4 (32-bits)
86+
size_t size_32 = RoundNearest(size, 4);
87+
// If the input data is smaller than the 32-bit size, pad the buffer with "0xff"
88+
if (size_32 != size)
89+
{
90+
uint8_t * p = (uint8_t *) data;
91+
VerifyOrReturnError(size_32 <= max, CHIP_ERROR_BUFFER_TOO_SMALL);
92+
memset(p + size, 0xff, size_32 - size);
93+
size = size_32;
94+
}
95+
return chip::DeviceLayer::Silabs::GetPlatform().FlashWritePage(addr, data, size);
8196
}
8297

8398
CHIP_ERROR WriteFile(Storage & store, SilabsConfig::Key offset_key, SilabsConfig::Key size_key, const ByteSpan & value)
@@ -89,17 +104,7 @@ CHIP_ERROR WriteFile(Storage & store, SilabsConfig::Key offset_key, SilabsConfig
89104
ReturnErrorOnFailure(ErasePage(base_addr));
90105
}
91106

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));
107+
ReturnErrorOnFailure(WritePage(base_addr + sCredentialsOffset, value.data(), value.size(), store.GetBufferSize()));
103108

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

0 commit comments

Comments
 (0)