Skip to content

Commit

Permalink
Provision: Dynamic buffer allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasallas-silabs committed Dec 6, 2024
1 parent ac466ef commit 93718d4
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 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
31 changes: 15 additions & 16 deletions examples/platform/silabs/provision/ProvisionStorageDefault.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,26 @@ 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)
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;
size_t size_32 = RoundNearest(size, 4);
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)
{
uint32_t base_addr = 0;
Expand All @@ -89,17 +99,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 +119,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
26 changes: 25 additions & 1 deletion src/platform/silabs/provision/ProvisionEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,47 @@
#include <lib/core/CHIPError.h>
#include <stddef.h>
#include <stdint.h>
#include <cstdlib>

namespace chip {
namespace DeviceLayer {
namespace Silabs {
namespace Provision {
namespace Encoding {

/*
begin out in end
|---------------v---------------v----------------|
|.....offset....|......left.....|.....spare......|
|..............size.............|
|......................limit.....................|
*/

struct Buffer
{
Buffer(uint8_t * ptr, size_t size, bool at_end = false) { Init(ptr, size, at_end); }

void Init(uint8_t * ptr, size_t size, bool at_end = false)
{
Finish();
if(nullptr == ptr)
{
ptr = static_cast<uint8_t *>(malloc(size));
allocated = true;
}
this->begin = ptr;
this->end = ptr + size;
this->in = at_end ? end : begin;
this->out = ptr;
}
void Finish()
{
if(this->begin && allocated)
{
free(this->begin);
}
this->begin = this->end = this->in = this->out = nullptr;
}

void Clear() { this->in = this->out = this->begin; }
size_t Limit() { return (this->end > this->begin) ? (this->end - this->begin) : 0; }
Expand All @@ -62,6 +85,7 @@ struct Buffer
uint8_t * end = nullptr;
uint8_t * in = nullptr;
uint8_t * out = nullptr;
bool allocated = false;
};

//------------------------------------------------------------------------------
Expand Down Expand Up @@ -142,7 +166,7 @@ struct Argument : public Buffer
State_Ready = 5,
};

Argument(uint8_t * ptr, size_t size) : Buffer(ptr, size) { Reset(); }
Argument(uint8_t * ptr = nullptr, size_t size = 0) : Buffer(ptr, size) { Reset(); }

void Reset()
{
Expand Down
11 changes: 6 additions & 5 deletions src/platform/silabs/provision/ProvisionStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ enum ID : uint16_t
kGeneratorFW = 0x0135,
kProductionFW = 0x0136,
kCertToolPath = 0x0137,
kPylinkLib = 0x0138,
kPylinkLib = 0x013a,
kBufferSize = 0x013b,
// Instance Info,
kSerialNumber = 0x0141,
kVendorId = 0x0142,
Expand Down Expand Up @@ -148,7 +149,6 @@ struct Storage : public GenericStorage,
static constexpr size_t kSpake2pSaltB64LengthMax = BASE64_ENCODED_LEN(chip::Crypto::kSpake2p_Max_PBKDF_Salt_Length) + 1;
static constexpr size_t kFirmwareInfoSizeMax = 32;
static constexpr size_t kCertificationSizeMax = 350;
static constexpr size_t kCertificateSizeMax = kArgumentSizeMax;
static constexpr size_t kDeviceAttestationKeySizeMax = 128;
static constexpr size_t kSetupPayloadSizeMax = 32;
static constexpr size_t kCsrLengthMax = 512;
Expand All @@ -160,9 +160,7 @@ struct Storage : public GenericStorage,
static constexpr size_t kTotalPayloadDataSize = kTotalPayloadDataSizeInBits / 8;

public:
static uint8_t aux_buffer[Storage::kArgumentSizeMax];

friend class Manager;
friend class Manager;
friend class Protocol1;
friend class Command;
friend class CsrCommand;
Expand Down Expand Up @@ -238,6 +236,8 @@ struct Storage : public GenericStorage,
CHIP_ERROR SetProvisionRequest(bool value);
CHIP_ERROR GetProvisionRequest(bool & value);
CHIP_ERROR GetTestEventTriggerKey(MutableByteSpan & keySpan);
void SetBufferSize(size_t size) { mBufferSize = size; }
size_t GetBufferSize() { return mBufferSize; }

private:
// Generic Interface
Expand Down Expand Up @@ -291,6 +291,7 @@ struct Storage : public GenericStorage,
uint32_t mRendezvousFlags = 0;
uint32_t mPasscode = 0;
uint32_t mKeyId = 0;
uint32_t mBufferSize = kArgumentSizeMax;
char mCommonName[kCommonNameMax] = { 0 };
CustomStorage mCustom;
};
Expand Down

0 comments on commit 93718d4

Please sign in to comment.