-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Revise PacketBufferWriter interface #4874
Conversation
#### Problem The `PacketBufferWriter` constructor that takes a size and allocates the `PacketBuffer` internally is awkward for some cases, and useless for the case of an existing `PacketBufferHandle`. #### Summary of Changes - `PacketBufferWriter()` takes ownership of an existing `PacketBufferHandle` (same as `PacketBufferTLVWriter`). - Added a `Finalize()` overload that matches that of `PacketBufferTLVWriter`.
src/system/SystemPacketBuffer.h
Outdated
@@ -730,10 +730,9 @@ class PacketBufferWriterBase : public Writer | |||
* @param[in] aAvailableSize Length bound of the BufferWriter. | |||
* @param[in] aReservedSize Reserved packet buffer space for protocol headers; see \c PacketBufferHandle::New(). | |||
*/ | |||
PacketBufferWriterBase(size_t aAvailableSize, uint16_t aReservedSize = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) : | |||
Writer(nullptr, 0) | |||
PacketBufferWriterBase(System::PacketBufferHandle && aPacket) : Writer(aPacket->Start(), aPacket->AvailableDataLength()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this still needs a length (possibly optional; checking the cases), for the case that the pool-backed buffer is actually larger than requested.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with a followup for the MessagePacketBuffer bits, so that we don't combine behavior changes with refactoring in a single PR. Up to you.
But we do need to sort out the way we get the pointer+length out of the buffer.
@@ -25,19 +25,13 @@ constexpr size_t kStatusReportMinSize = 2 + 4 + 2; ///< 16 bits for GeneralCode, | |||
CHIP_ERROR WriteToPacketBuffer(const ::chip::bdx::BdxMessage & msgStruct, ::chip::System::PacketBufferHandle & msgBuf) | |||
{ | |||
size_t msgDataSize = msgStruct.MessageSize(); | |||
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(msgDataSize); | |||
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(::chip::System::PacketBufferHandle::New(msgDataSize)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be a MessagePacketBuffer eventually, but a followup for that is OK.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll add them here so they don't get overlooked.
@@ -848,21 +842,21 @@ void TransferSession::PrepareStatusReport(StatusCode code) | |||
{ | |||
mStatusReportData.StatusCode = code; | |||
|
|||
Encoding::LittleEndian::PacketBufferWriter bbuf(kStatusReportMinSize); | |||
Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(kStatusReportMinSize)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again, I expect MessagePacketBuffer here.
src/system/SystemPacketBuffer.h
Outdated
@@ -730,10 +730,9 @@ class PacketBufferWriterBase : public Writer | |||
* @param[in] aAvailableSize Length bound of the BufferWriter. | |||
* @param[in] aReservedSize Reserved packet buffer space for protocol headers; see \c PacketBufferHandle::New(). | |||
*/ | |||
PacketBufferWriterBase(size_t aAvailableSize, uint16_t aReservedSize = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) : | |||
Writer(nullptr, 0) | |||
PacketBufferWriterBase(System::PacketBufferHandle && aPacket) : Writer(aPacket->Start(), aPacket->AvailableDataLength()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That combination of args doesn't really make sense if there's data in the buffer already.
We want to either use aPacket->Start()
with aPacket->MaxDataLength()
, or aPacket->Start() + aPacket->DataLength()
with aPacket->AvailableDataLength()
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct, I started off intended the former, but I think the latter is more useful.
src/system/SystemPacketBuffer.h
Outdated
@@ -757,6 +756,22 @@ class PacketBufferWriterBase : public Writer | |||
*/ | |||
System::PacketBufferHandle Finalize() { return PacketBufferWriterUtil::Finalize(*this, mPacket); } | |||
|
|||
/** | |||
* Finish the writing to the buffer and release ownership of the underlying PacketBuffer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
* Finish the writing to the buffer and release ownership of the underlying PacketBuffer. | |
* Finish writing to the buffer and release ownership of the underlying PacketBuffer. |
src/system/SystemPacketBuffer.h
Outdated
/** | ||
* Finish the writing to the buffer and release ownership of the underlying PacketBuffer. | ||
* | ||
* This signature matches `PacketBufferTLVWriter::Finalize()`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's really not clear how one decides between the two signatures in practice... It might be better to only have one signature; if we want to match PacketBufferTLVWriter
, let's just do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The original signature is more useful in practice; almost all Writer users immediately pass the buffer somewhere else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, but why add the new one then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consistency with PacketBufferTLVWriter
seemed like a good idea at the time.
@@ -264,7 +264,7 @@ CHIP_ERROR NetworkProvisioning::SendThreadCredentials(const DeviceLayer::Interna | |||
4; // threadData.ThreadChannel, threadData.FieldPresent.ThreadExtendedPANId, | |||
// threadData.FieldPresent.ThreadMeshPrefix, threadData.FieldPresent.ThreadPSKc | |||
/* clang-format on */ | |||
Encoding::LittleEndian::PacketBufferWriter bbuf(credentialSize); | |||
Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(credentialSize)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a MessagePacketBuffer
too.
Size increase report for "esp32-example-build" from 3c2238a
Full report output
|
Size increase report for "nrfconnect-example-build" from 3c2238a
Full report output
|
@@ -25,7 +26,7 @@ constexpr size_t kStatusReportMinSize = 2 + 4 + 2; ///< 16 bits for GeneralCode, | |||
CHIP_ERROR WriteToPacketBuffer(const ::chip::bdx::BdxMessage & msgStruct, ::chip::System::PacketBufferHandle & msgBuf) | |||
{ | |||
size_t msgDataSize = msgStruct.MessageSize(); | |||
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(msgDataSize); | |||
::chip::Encoding::LittleEndian::PacketBufferWriter bbuf(chip::MessagePacketBuffer::New(msgDataSize)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this also pass msgDataSize
as second arg? Or document why we don't care if this overallocates?
Problem
The
PacketBufferWriter
constructor that takes a size and allocates thePacketBuffer
internally is awkward for some cases, and useless for thecase of an existing
PacketBufferHandle
.Summary of Changes
PacketBufferWriter()
takes ownership of an existingPacketBufferHandle
(same as
PacketBufferTLVWriter
).Finalize()
overload that matches that ofPacketBufferTLVWriter
.