Skip to content

Commit

Permalink
Code review.
Browse files Browse the repository at this point in the history
  • Loading branch information
rcasallas-silabs committed Dec 6, 2024
1 parent 93718d4 commit fa07ac1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
20 changes: 17 additions & 3 deletions src/platform/silabs/provision/ProvisionEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <lib/core/CHIPError.h>
#include <stddef.h>
#include <stdint.h>
#include <cstdlib>

namespace chip {
namespace DeviceLayer {
Expand All @@ -29,6 +28,15 @@ namespace Provision {
namespace Encoding {

/*
Generic buffer used to hold incoming and outgoing data.
The "in" pointer marks the next address to be written.
The "out" pointer marks the next address to be read.
When "out" reaches "in", all incoming data has been read.
"Size" is the total amount of bytes written, including the part already read.
"Left" is the number of bytes available for reading.
"Offset" is the number of bytes read.
"Spare" is the number of bytes available for writing.
"Limit" it the total number of bytes allocated (size + spare).
begin out in end
|---------------v---------------v----------------|
|.....offset....|......left.....|.....spare......|
Expand All @@ -45,7 +53,7 @@ struct Buffer
Finish();
if(nullptr == ptr)
{
ptr = static_cast<uint8_t *>(malloc(size));
ptr = new uint8_t[size];
allocated = true;
}
this->begin = ptr;
Expand All @@ -57,16 +65,22 @@ struct Buffer
{
if(this->begin && allocated)
{
free(this->begin);
delete[] this->begin;
}
this->begin = this->end = this->in = this->out = nullptr;
}

/** Reset the pointers to initial position. Zero write, zero read. */
void Clear() { this->in = this->out = this->begin; }
/** @return Total size allocated for the buffer. */
size_t Limit() { return (this->end > this->begin) ? (this->end - this->begin) : 0; }
/** @return Number of bytes written. */
size_t Size() { return (this->in > this->begin) ? (this->in - this->begin) : 0; }
/** @return Number of bytes read. */
size_t Offset() { return (this->out > this->begin) ? (this->out - this->begin) : 0; }
/** @return Number of bytes available for reading. */
size_t Left() { return this->Size() - this->Offset(); }
/** @return Number of bytes available for writing. */
size_t Spare() { return this->Limit() - this->Size(); }

CHIP_ERROR Add(uint8_t in);
Expand Down
4 changes: 2 additions & 2 deletions src/platform/silabs/provision/ProvisionStorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ struct Storage : public GenericStorage,
static constexpr size_t kTotalPayloadDataSize = kTotalPayloadDataSizeInBits / 8;

public:
friend class Manager;
friend class Manager;
friend class Protocol1;
friend class Command;
friend class CsrCommand;
Expand Down Expand Up @@ -236,7 +236,7 @@ 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; }
void SetBufferSize(size_t size) { mBufferSize = size > 0 ? size : kArgumentSizeMax; }
size_t GetBufferSize() { return mBufferSize; }

private:
Expand Down

0 comments on commit fa07ac1

Please sign in to comment.