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 5d3d9cb commit 65d9911
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
19 changes: 17 additions & 2 deletions src/platform/silabs/provision/ProvisionEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,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 +54,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 +66,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
2 changes: 1 addition & 1 deletion src/platform/silabs/provision/ProvisionStorage.h
Original file line number Diff line number Diff line change
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 65d9911

Please sign in to comment.