diff --git a/src/app/encoder.cpp b/src/app/encoder.cpp index b06f4dfe544638..bcfa9a9b2a377e 100644 --- a/src/app/encoder.cpp +++ b/src/app/encoder.cpp @@ -77,7 +77,7 @@ uint16_t encodeApsFrame(uint8_t * buffer, uint16_t buf_length, EmberApsFrame * a #define COMMAND_HEADER(name, clusterId) \ const char * kName = name; \ \ - PacketBufferWriter buf(kMaxBufferSize); \ + PacketBufferWriter buf(System::PacketBufferHandle::New(kMaxBufferSize)); \ if (buf.IsNull()) \ { \ ChipLogError(Zcl, "Could not allocate packet buffer while trying to encode %s command", kName); \ diff --git a/src/app/zap-templates/templates/chip/encoder-src.zapt b/src/app/zap-templates/templates/chip/encoder-src.zapt index fb267fd8265d34..69c50cb03cc001 100644 --- a/src/app/zap-templates/templates/chip/encoder-src.zapt +++ b/src/app/zap-templates/templates/chip/encoder-src.zapt @@ -61,7 +61,7 @@ uint16_t encodeApsFrame(uint8_t * buffer, uint16_t buf_length, EmberApsFrame * a #define COMMAND_HEADER(name, clusterId) \ const char * kName = name; \ \ - PacketBufferWriter buf(kMaxBufferSize); \ + PacketBufferWriter buf(System::PacketBufferHandle::New(kMaxBufferSize)); \ if (buf.IsNull()) \ { \ ChipLogError(Zcl, "Could not allocate packet buffer while trying to encode %s command", kName); \ diff --git a/src/protocols/bdx/BUILD.gn b/src/protocols/bdx/BUILD.gn index e8ca3f14da8ae7..b1786f61bf5884 100644 --- a/src/protocols/bdx/BUILD.gn +++ b/src/protocols/bdx/BUILD.gn @@ -30,6 +30,6 @@ static_library("bdx") { "${chip_root}/src/lib/core", "${chip_root}/src/lib/support", "${chip_root}/src/system", - "${chip_root}/src/transport/raw", + "${chip_root}/src/transport", ] } diff --git a/src/protocols/bdx/BdxTransferSession.cpp b/src/protocols/bdx/BdxTransferSession.cpp index c9499b07d20582..cea58bfbea2284 100644 --- a/src/protocols/bdx/BdxTransferSession.cpp +++ b/src/protocols/bdx/BdxTransferSession.cpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace { constexpr uint8_t kBdxVersion = 0; ///< The version of this implementation of the BDX spec @@ -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), msgDataSize); if (bbuf.IsNull()) { return CHIP_ERROR_NO_MEMORY; @@ -36,7 +37,6 @@ CHIP_ERROR WriteToPacketBuffer(const ::chip::bdx::BdxMessage & msgStruct, ::chip { return CHIP_ERROR_NO_MEMORY; } - return CHIP_NO_ERROR; } @@ -848,7 +848,7 @@ void TransferSession::PrepareStatusReport(StatusCode code) { mStatusReportData.StatusCode = code; - Encoding::LittleEndian::PacketBufferWriter bbuf(kStatusReportMinSize); + Encoding::LittleEndian::PacketBufferWriter bbuf(chip::MessagePacketBuffer::New(kStatusReportMinSize), kStatusReportMinSize); VerifyOrReturn(!bbuf.IsNull()); bbuf.Put16(static_cast(Protocols::Common::StatusCode::Failure)); diff --git a/src/protocols/bdx/tests/TestBdxMessages.cpp b/src/protocols/bdx/tests/TestBdxMessages.cpp index 8f8e7d9e58bebf..bd618afaad2d7f 100644 --- a/src/protocols/bdx/tests/TestBdxMessages.cpp +++ b/src/protocols/bdx/tests/TestBdxMessages.cpp @@ -22,7 +22,7 @@ void TestHelperWrittenAndParsedMatch(nlTestSuite * inSuite, void * inContext, Ms CHIP_ERROR err = CHIP_NO_ERROR; size_t msgSize = testMsg.MessageSize(); - Encoding::LittleEndian::PacketBufferWriter bbuf(msgSize); + Encoding::LittleEndian::PacketBufferWriter bbuf(System::PacketBufferHandle::New(msgSize)); NL_TEST_ASSERT(inSuite, !bbuf.IsNull()); testMsg.WriteToBuffer(bbuf); diff --git a/src/system/SystemPacketBuffer.cpp b/src/system/SystemPacketBuffer.cpp index 99f6b0378136ee..e02c3aa371bd4f 100644 --- a/src/system/SystemPacketBuffer.cpp +++ b/src/system/SystemPacketBuffer.cpp @@ -619,16 +619,6 @@ PacketBufferHandle PacketBufferHandle::CloneData(uint16_t aAdditionalSize, uint1 namespace Encoding { -void PacketBufferWriterUtil::Initialize(BufferWriter & aBufferWriter, System::PacketBufferHandle & aPacket, size_t aAvailableSize, - uint16_t aReservedSize) -{ - aPacket = System::PacketBufferHandle::New(aAvailableSize, aReservedSize); - if (!aPacket.IsNull()) - { - aBufferWriter = Encoding::BufferWriter(aPacket->Start(), aAvailableSize); - } -} - System::PacketBufferHandle PacketBufferWriterUtil::Finalize(BufferWriter & aBufferWriter, System::PacketBufferHandle & aPacket) { if (!aPacket.IsNull() && aBufferWriter.Fit()) diff --git a/src/system/SystemPacketBuffer.h b/src/system/SystemPacketBuffer.h index 2c07ea03e9e767..4847e11ef82da6 100644 --- a/src/system/SystemPacketBuffer.h +++ b/src/system/SystemPacketBuffer.h @@ -36,6 +36,7 @@ #include #include +#include #if CHIP_SYSTEM_CONFIG_USE_LWIP #include @@ -60,7 +61,6 @@ class PacketBufferTest; #undef CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHT_SIZE // True if RightSize() has a nontrivial implementation #undef CHIP_SYSTEM_PACKETBUFFER_HAS_CHECK // True if Check() has a nontrivial implementation - #if CHIP_SYSTEM_CONFIG_USE_LWIP #if LWIP_PBUF_FROM_CUSTOM_POOLS #define CHIP_SYSTEM_PACKETBUFFER_STORE CHIP_SYSTEM_PACKETBUFFER_STORE_LWIP_CUSTOM @@ -697,8 +697,6 @@ class PacketBufferWriterUtil private: template friend class PacketBufferWriterBase; - static void Initialize(BufferWriter & aBufferWriter, System::PacketBufferHandle & aPacket, size_t aAvailableSize, - uint16_t aReservedSize); static System::PacketBufferHandle Finalize(BufferWriter & aBufferWriter, System::PacketBufferHandle & aPacket); }; @@ -721,19 +719,26 @@ class PacketBufferWriterBase : public Writer { public: /** - * Constructs a BufferWriter that writes into a newly allocated packet buffer. + * Constructs a BufferWriter that writes into a packet buffer, using all avaiable space. * - * If no memory is available, or if the size requested is too large, then \c IsNull() will be true. - * Otherwise, it is guaranteed that the BufferWriter length is \a aAvailableSize. (The underlying packet - * buffer may be larger.) + * @param[in] aPacket A handle to PacketBuffer, to be used as backing store for the BufferWriter. + */ + PacketBufferWriterBase(System::PacketBufferHandle && aPacket) : + Writer(aPacket->Start() + aPacket->DataLength(), aPacket->AvailableDataLength()) + { + mPacket = std::move(aPacket); + } + + /** + * Constructs a BufferWriter that writes into a packet buffer, using no more than the requested space. * - * @param[in] aAvailableSize Length bound of the BufferWriter. - * @param[in] aReservedSize Reserved packet buffer space for protocol headers; see \c PacketBufferHandle::New(). + * @param[in] aPacket A handle to PacketBuffer, to be used as backing store for the BufferWriter. + * @param[in] aSize Maximum number of octects to write into the packet buffer. */ - PacketBufferWriterBase(size_t aAvailableSize, uint16_t aReservedSize = CHIP_SYSTEM_CONFIG_HEADER_RESERVE_SIZE) : - Writer(nullptr, 0) + PacketBufferWriterBase(System::PacketBufferHandle && aPacket, size_t aSize) : + Writer(aPacket->Start() + aPacket->DataLength(), chip::min(aSize, static_cast(aPacket->AvailableDataLength()))) { - PacketBufferWriterUtil::Initialize(*this, mPacket, aAvailableSize, aReservedSize); + mPacket = std::move(aPacket); } /** diff --git a/src/system/tests/TestSystemPacketBuffer.cpp b/src/system/tests/TestSystemPacketBuffer.cpp index 74f64a8e4d885f..2dbe1ecbb804bb 100644 --- a/src/system/tests/TestSystemPacketBuffer.cpp +++ b/src/system/tests/TestSystemPacketBuffer.cpp @@ -1715,8 +1715,8 @@ void PacketBufferTest::CheckPacketBufferWriter(nlTestSuite * inSuite, void * inC const char kPayload[] = "Hello, world!"; - PacketBufferWriter yay(sizeof(kPayload)); - PacketBufferWriter nay(sizeof(kPayload) - 2); + PacketBufferWriter yay(PacketBufferHandle::New(sizeof(kPayload))); + PacketBufferWriter nay(PacketBufferHandle::New(sizeof(kPayload)), sizeof(kPayload) - 2); NL_TEST_ASSERT(inSuite, !yay.IsNull()); NL_TEST_ASSERT(inSuite, !nay.IsNull()); diff --git a/src/transport/NetworkProvisioning.cpp b/src/transport/NetworkProvisioning.cpp index 0f31cb374c27f7..d201d80751d4d5 100644 --- a/src/transport/NetworkProvisioning.cpp +++ b/src/transport/NetworkProvisioning.cpp @@ -230,7 +230,7 @@ CHIP_ERROR NetworkProvisioning::SendNetworkCredentials(const char * ssid, const const size_t bufferSize = EncodedStringSize(ssid) + EncodedStringSize(passwd); VerifyOrExit(CanCastTo(bufferSize), err = CHIP_ERROR_INVALID_ARGUMENT); { - Encoding::LittleEndian::PacketBufferWriter bbuf(bufferSize + MessagePacketBuffer::kMaxFooterSize); + Encoding::LittleEndian::PacketBufferWriter bbuf(MessagePacketBuffer::New(bufferSize), bufferSize); ChipLogProgress(NetworkProvisioning, "Sending Network Creds. Delegate %p\n", mDelegate); VerifyOrExit(mDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE); @@ -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 + MessagePacketBuffer::kMaxFooterSize); + Encoding::LittleEndian::PacketBufferWriter bbuf(MessagePacketBuffer::New(credentialSize), credentialSize); ChipLogProgress(NetworkProvisioning, "Sending Thread Credentials"); VerifyOrExit(mDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE); diff --git a/src/transport/PASESession.cpp b/src/transport/PASESession.cpp index b1c35f222b80dc..aea0e9cce5b6d9 100644 --- a/src/transport/PASESession.cpp +++ b/src/transport/PASESession.cpp @@ -635,7 +635,7 @@ CHIP_ERROR PASESession::HandleMsg1_and_SendMsg2(const PacketHeader & header, con data_len = static_cast(Y_len + verifier_len); { - Encoding::PacketBufferWriter bbuf(data_len); + Encoding::PacketBufferWriter bbuf(System::PacketBufferHandle::New(data_len)); VerifyOrExit(!bbuf.IsNull(), err = CHIP_SYSTEM_ERROR_NO_MEMORY); bbuf.Put(&Y[0], Y_len); bbuf.Put(verifier, verifier_len); @@ -691,7 +691,7 @@ CHIP_ERROR PASESession::HandleMsg2_and_SendMsg3(const PacketHeader & header, con } { - Encoding::PacketBufferWriter bbuf(verifier_len); + Encoding::PacketBufferWriter bbuf(System::PacketBufferHandle::New(verifier_len)); VerifyOrExit(!bbuf.IsNull(), err = CHIP_SYSTEM_ERROR_NO_MEMORY); bbuf.Put(verifier, verifier_len);