diff --git a/src/platform/silabs/provision/ProvisionEncoder.h b/src/platform/silabs/provision/ProvisionEncoder.h index c83313d955..e8e4132f18 100644 --- a/src/platform/silabs/provision/ProvisionEncoder.h +++ b/src/platform/silabs/provision/ProvisionEncoder.h @@ -20,7 +20,6 @@ #include #include #include -#include namespace chip { namespace DeviceLayer { @@ -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......| @@ -45,7 +53,7 @@ struct Buffer Finish(); if(nullptr == ptr) { - ptr = static_cast(malloc(size)); + ptr = new uint8_t[size]; allocated = true; } this->begin = ptr; @@ -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); diff --git a/src/platform/silabs/provision/ProvisionStorage.h b/src/platform/silabs/provision/ProvisionStorage.h index c4a148ee59..f169ee771f 100644 --- a/src/platform/silabs/provision/ProvisionStorage.h +++ b/src/platform/silabs/provision/ProvisionStorage.h @@ -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; @@ -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: