From 4e976bf2c64f89c2e25875d3895d69169bf7f2d7 Mon Sep 17 00:00:00 2001 From: Zang MingJie Date: Tue, 30 Mar 2021 21:21:24 +0800 Subject: [PATCH] Use ExchangeHandle to track ref count of ExchangeContext --- examples/shell/shell_common/cmd_ping.cpp | 2 +- examples/shell/shell_common/cmd_send.cpp | 18 +-- src/app/Command.cpp | 4 +- src/app/Command.h | 4 +- src/app/CommandHandler.cpp | 4 +- src/app/CommandHandler.h | 2 +- src/app/CommandSender.cpp | 8 +- src/app/CommandSender.h | 4 +- src/app/InteractionModelDelegate.h | 2 +- src/app/InteractionModelEngine.cpp | 26 ++-- src/app/InteractionModelEngine.h | 11 +- src/app/ReadClient.cpp | 12 +- src/app/ReadClient.h | 6 +- src/app/ReadHandler.cpp | 11 +- src/app/ReadHandler.h | 4 +- src/app/server/EchoHandler.cpp | 2 +- src/app/server/Server.cpp | 4 +- src/app/tests/TestCommandInteraction.cpp | 4 +- src/app/tests/TestReadInteraction.cpp | 2 +- src/app/tests/TestReportingEngine.cpp | 6 +- .../tests/integration/chip_im_initiator.cpp | 2 +- src/app/util/DataModelHandler.cpp | 2 +- src/app/util/DataModelHandler.h | 2 +- src/app/util/af-types.h | 7 +- src/app/util/chip-message-send.cpp | 10 +- src/app/util/chip-message-send.h | 2 +- src/app/util/common.h | 2 +- .../util/ember-compatibility-functions.cpp | 4 +- src/app/util/util.cpp | 6 +- src/app/util/util.h | 4 +- src/channel/Channel.cpp | 2 +- src/channel/Channel.h | 3 +- src/channel/ChannelContext.cpp | 6 +- src/channel/ChannelContext.h | 3 +- src/channel/tests/TestChannel.cpp | 6 +- src/controller/CHIPDevice.cpp | 12 +- src/controller/CHIPDevice.h | 4 +- src/controller/CHIPDeviceController.cpp | 10 +- src/controller/CHIPDeviceController.h | 4 +- src/messaging/BUILD.gn | 2 + src/messaging/ExchangeContext.cpp | 38 ++---- src/messaging/ExchangeContext.h | 9 +- src/messaging/ExchangeDelegate.h | 5 +- src/messaging/ExchangeHandle.cpp | 58 +++++++++ src/messaging/ExchangeHandle.h | 62 ++++++++++ src/messaging/ExchangeMessageDispatch.cpp | 25 ++-- src/messaging/ExchangeMessageDispatch.h | 5 +- src/messaging/ExchangeMgr.cpp | 19 +-- src/messaging/ExchangeMgr.h | 2 +- src/messaging/ReliableMessageContext.cpp | 15 +-- src/messaging/ReliableMessageContext.h | 7 +- src/messaging/ReliableMessageMgr.cpp | 113 ++++++++---------- src/messaging/ReliableMessageMgr.h | 22 +--- src/messaging/tests/MessagingContext.cpp | 4 +- src/messaging/tests/MessagingContext.h | 4 +- src/messaging/tests/TestExchangeMgr.cpp | 14 +-- .../tests/TestReliableMessageProtocol.cpp | 35 +++--- src/messaging/tests/echo/echo_requester.cpp | 2 +- src/messaging/tests/echo/echo_responder.cpp | 2 +- src/protocols/echo/Echo.h | 12 +- src/protocols/echo/EchoClient.cpp | 25 ++-- src/protocols/echo/EchoServer.cpp | 4 +- src/protocols/secure_channel/CASEServer.cpp | 8 +- src/protocols/secure_channel/CASEServer.h | 6 +- src/protocols/secure_channel/CASESession.cpp | 20 ++-- src/protocols/secure_channel/CASESession.h | 10 +- .../secure_channel/MessageCounterManager.cpp | 14 +-- .../secure_channel/MessageCounterManager.h | 10 +- src/protocols/secure_channel/PASESession.cpp | 22 ++-- src/protocols/secure_channel/PASESession.h | 8 +- .../SessionEstablishmentExchangeDispatch.cpp | 4 +- .../SessionEstablishmentExchangeDispatch.h | 2 +- .../secure_channel/tests/TestCASESession.cpp | 8 +- .../tests/TestMessageCounterManager.cpp | 8 +- .../secure_channel/tests/TestPASESession.cpp | 12 +- src/transport/SecureSessionMgr.h | 2 + 76 files changed, 449 insertions(+), 396 deletions(-) create mode 100644 src/messaging/ExchangeHandle.cpp create mode 100644 src/messaging/ExchangeHandle.h diff --git a/examples/shell/shell_common/cmd_ping.cpp b/examples/shell/shell_common/cmd_ping.cpp index f8995db411deff..a1d7a0a56381a4 100644 --- a/examples/shell/shell_common/cmd_ping.cpp +++ b/examples/shell/shell_common/cmd_ping.cpp @@ -220,7 +220,7 @@ CHIP_ERROR EstablishSecureSession(streamer_t * stream, Transport::PeerAddress & return err; } -void HandleEchoResponseReceived(Messaging::ExchangeContext * ec, System::PacketBufferHandle && payload) +void HandleEchoResponseReceived(Messaging::ExchangeHandle ec, System::PacketBufferHandle && payload) { uint32_t respTime = System::Timer::GetCurrentEpoch(); uint32_t transitTime = respTime - gPingArguments.GetLastEchoTime(); diff --git a/examples/shell/shell_common/cmd_send.cpp b/examples/shell/shell_common/cmd_send.cpp index 9a6ef3f4a9869d..bd8ff677fe4326 100644 --- a/examples/shell/shell_common/cmd_send.cpp +++ b/examples/shell/shell_common/cmd_send.cpp @@ -40,7 +40,7 @@ using namespace Logging; namespace { -Messaging::ExchangeContext * gExchangeCtx = nullptr; +Messaging::ExchangeHandle gExchangeCtx; class SendArguments { @@ -100,7 +100,7 @@ class SendArguments class MockAppDelegate : public Messaging::ExchangeDelegate { public: - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && buffer) override { uint32_t respTime = System::Timer::GetCurrentEpoch(); @@ -111,16 +111,16 @@ class MockAppDelegate : public Messaging::ExchangeDelegate static_cast(transitTime) / 1000); gExchangeCtx->Close(); - gExchangeCtx = nullptr; + gExchangeCtx.Release(); } - void OnResponseTimeout(Messaging::ExchangeContext * ec) override + void OnResponseTimeout(Messaging::ExchangeHandle ec) override { streamer_t * sout = streamer_get(); streamer_printf(sout, "No response received\n"); gExchangeCtx->Close(); - gExchangeCtx = nullptr; + gExchangeCtx.Release(); } } gMockAppDelegate; @@ -135,15 +135,15 @@ CHIP_ERROR SendMessage(streamer_t * stream) // Discard any existing exchange context. Effectively we can only have one exchange with // a single node at any one time. - if (gExchangeCtx != nullptr) + if (gExchangeCtx.HasValue()) { gExchangeCtx->Abort(); - gExchangeCtx = nullptr; + gExchangeCtx.Release(); } // Create a new exchange context. gExchangeCtx = gExchangeManager.NewContext({ kTestDeviceNodeId, 0, gAdminId }, &gMockAppDelegate); - VerifyOrExit(gExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY); + VerifyOrExit(gExchangeCtx.HasValue(), err = CHIP_ERROR_NO_MEMORY); size = gSendArguments.GetPayloadSize(); VerifyOrExit(size <= kMaxPayloadSize, err = CHIP_ERROR_INVALID_MESSAGE_LENGTH); @@ -177,7 +177,7 @@ CHIP_ERROR SendMessage(streamer_t * stream) if (err != CHIP_NO_ERROR) { gExchangeCtx->Abort(); - gExchangeCtx = nullptr; + gExchangeCtx.Release(); } exit: diff --git a/src/app/Command.cpp b/src/app/Command.cpp index bea7e493370bf3..35560164e64942 100644 --- a/src/app/Command.cpp +++ b/src/app/Command.cpp @@ -216,10 +216,10 @@ CHIP_ERROR Command::AbortExistingExchangeContext() { // Discard any existing exchange context. Effectively we can only have one Echo exchange with // a single node at any one time. - if (mpExchangeCtx != nullptr) + if (mpExchangeCtx.HasValue()) { mpExchangeCtx->Abort(); - mpExchangeCtx = nullptr; + mpExchangeCtx.Release(); } return CHIP_NO_ERROR; diff --git a/src/app/Command.h b/src/app/Command.h index 9b79be051efae5..a00df3824da991 100644 --- a/src/app/Command.h +++ b/src/app/Command.h @@ -109,7 +109,7 @@ class Command * exchange context has been assigned or the context * has been released. */ - Messaging::ExchangeContext * GetExchangeContext() const { return mpExchangeCtx; } + const Messaging::ExchangeHandle GetExchangeContext() const { return mpExchangeCtx; } CHIP_ERROR Reset(); @@ -128,7 +128,7 @@ class Command InvokeCommand::Builder mInvokeCommandBuilder; Messaging::ExchangeManager * mpExchangeMgr = nullptr; - Messaging::ExchangeContext * mpExchangeCtx = nullptr; + Messaging::ExchangeHandle mpExchangeCtx; InteractionModelDelegate * mpDelegate = nullptr; chip::System::PacketBufferHandle mCommandMessageBuf; uint8_t mCommandIndex = 0; diff --git a/src/app/CommandHandler.cpp b/src/app/CommandHandler.cpp index 4913ed423347f1..1738807e954725 100644 --- a/src/app/CommandHandler.cpp +++ b/src/app/CommandHandler.cpp @@ -33,7 +33,7 @@ using GeneralStatusCode = chip::Protocols::SecureChannel::GeneralStatusCode; namespace chip { namespace app { -void CommandHandler::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, +void CommandHandler::OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -62,7 +62,7 @@ CHIP_ERROR CommandHandler::SendCommandResponse() err = FinalizeCommandsMessage(); SuccessOrExit(err); - VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_INCORRECT_STATE); err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::InvokeCommandResponse, std::move(mCommandMessageBuf)); SuccessOrExit(err); diff --git a/src/app/CommandHandler.h b/src/app/CommandHandler.h index a3fe1e8309e30e..865521e74733a0 100644 --- a/src/app/CommandHandler.h +++ b/src/app/CommandHandler.h @@ -42,7 +42,7 @@ namespace app { class CommandHandler : public Command { public: - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload); CHIP_ERROR AddStatusCode(const CommandPathParams * apCommandPathParams, const Protocols::SecureChannel::GeneralStatusCode aGeneralCode, const Protocols::Id aProtocolId, diff --git a/src/app/CommandSender.cpp b/src/app/CommandSender.cpp index 235ce294b1033a..653065bc6199ad 100644 --- a/src/app/CommandSender.cpp +++ b/src/app/CommandSender.cpp @@ -58,7 +58,7 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId { mpExchangeCtx = mpExchangeMgr->NewContext(*secureSession, this); } - VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY); + VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_NO_MEMORY); mpExchangeCtx->SetResponseTimeout(kImMessageTimeoutMsec); err = mpExchangeCtx->SendMessage( @@ -77,7 +77,7 @@ CHIP_ERROR CommandSender::SendCommandRequest(NodeId aNodeId, Transport::AdminId return err; } -void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, +void CommandSender::OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -95,7 +95,7 @@ void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeCon // Close the exchange cleanly so that the ExchangeManager will send an ack for the message we just received. // This needs to be done before the Reset() call, because Reset() aborts mpExchangeCtx if its not null. mpExchangeCtx->Close(); - mpExchangeCtx = nullptr; + mpExchangeCtx.Release(); if (mpDelegate != nullptr) { @@ -112,7 +112,7 @@ void CommandSender::OnMessageReceived(Messaging::ExchangeContext * apExchangeCon Shutdown(); } -void CommandSender::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) +void CommandSender::OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext) { ChipLogProgress(DataManagement, "Time out! failed to receive invoke command response from Exchange: %d", apExchangeContext->GetExchangeId()); diff --git a/src/app/CommandSender.h b/src/app/CommandSender.h index aa0a47e7d2366f..1a18ab9ab3e213 100644 --- a/src/app/CommandSender.h +++ b/src/app/CommandSender.h @@ -58,9 +58,9 @@ class CommandSender : public Command, public Messaging::ExchangeDelegate // ExchangeDelegate interface implementation. Private so people won't // accidentally call it on us when we're not being treated as an actual // ExchangeDelegate. - void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, + void OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) override; - void OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) override; + void OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext) override; CHIP_ERROR ProcessCommandDataElement(CommandDataElement::Parser & aCommandElement) override; }; diff --git a/src/app/InteractionModelDelegate.h b/src/app/InteractionModelDelegate.h index f4f01c284ec3cf..2009ee1ff7a452 100644 --- a/src/app/InteractionModelDelegate.h +++ b/src/app/InteractionModelDelegate.h @@ -54,7 +54,7 @@ class InteractionModelDelegate * * @retval # CHIP_ERROR_NOT_IMPLEMENTED if not implemented */ - virtual CHIP_ERROR EventStreamReceived(const Messaging::ExchangeContext * apExchangeContext, TLV::TLVReader * apEventListReader) + virtual CHIP_ERROR EventStreamReceived(const Messaging::ExchangeHandle apExchangeContext, TLV::TLVReader * apEventListReader) { return CHIP_ERROR_NOT_IMPLEMENTED; } diff --git a/src/app/InteractionModelEngine.cpp b/src/app/InteractionModelEngine.cpp index 3b8515a858e92b..abd5c50f7bae0c 100644 --- a/src/app/InteractionModelEngine.cpp +++ b/src/app/InteractionModelEngine.cpp @@ -139,7 +139,7 @@ CHIP_ERROR InteractionModelEngine::NewReadClient(ReadClient ** const apReadClien return err; } -void InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, +void InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -151,18 +151,12 @@ void InteractionModelEngine::OnUnknownMsgType(Messaging::ExchangeContext * apExc // SuccessOrExit(err); apExchangeContext->Close(); - apExchangeContext = nullptr; + apExchangeContext.Release(); ChipLogFunctError(err); - - // Todo: Fix the below check after the above status report is implemented. - if (nullptr != apExchangeContext) - { - apExchangeContext->Abort(); - } } -void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext, +void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) { @@ -175,7 +169,7 @@ void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeContext * err = commandHandler.Init(mpExchangeMgr, mpDelegate); SuccessOrExit(err); commandHandler.OnMessageReceived(apExchangeContext, aPacketHeader, aPayloadHeader, std::move(aPayload)); - apExchangeContext = nullptr; + apExchangeContext.Release(); break; } } @@ -183,13 +177,13 @@ void InteractionModelEngine::OnInvokeCommandRequest(Messaging::ExchangeContext * exit: ChipLogFunctError(err); - if (nullptr != apExchangeContext) + if (apExchangeContext.HasValue()) { apExchangeContext->Abort(); } } -void InteractionModelEngine::OnReadRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, +void InteractionModelEngine::OnReadRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -204,7 +198,7 @@ void InteractionModelEngine::OnReadRequest(Messaging::ExchangeContext * apExchan SuccessOrExit(err); err = readHandler.OnReadRequest(apExchangeContext, std::move(aPayload)); SuccessOrExit(err); - apExchangeContext = nullptr; + apExchangeContext.Release(); break; } } @@ -212,13 +206,13 @@ void InteractionModelEngine::OnReadRequest(Messaging::ExchangeContext * apExchan exit: ChipLogFunctError(err); - if (nullptr != apExchangeContext) + if (apExchangeContext.HasValue()) { apExchangeContext->Abort(); } } -void InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, +void InteractionModelEngine::OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) { if (aPayloadHeader.HasMessageType(Protocols::InteractionModel::MsgType::InvokeCommandRequest)) @@ -236,7 +230,7 @@ void InteractionModelEngine::OnMessageReceived(Messaging::ExchangeContext * apEx } } -void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeContext * ec) +void InteractionModelEngine::OnResponseTimeout(Messaging::ExchangeHandle ec) { ChipLogProgress(DataManagement, "Time out! failed to receive echo response from Exchange: %d", ec->GetExchangeId()); } diff --git a/src/app/InteractionModelEngine.h b/src/app/InteractionModelEngine.h index c2ff6ea0111a53..a2b22518d62e93 100644 --- a/src/app/InteractionModelEngine.h +++ b/src/app/InteractionModelEngine.h @@ -27,6 +27,7 @@ #include #include +#include #include #include #include @@ -136,19 +137,19 @@ class InteractionModelEngine : public Messaging::ExchangeDelegate private: friend class reporting::Engine; - void OnUnknownMsgType(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, + void OnUnknownMsgType(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload); - void OnInvokeCommandRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, + void OnInvokeCommandRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload); - void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, + void OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload); - void OnResponseTimeout(Messaging::ExchangeContext * ec); + void OnResponseTimeout(Messaging::ExchangeHandle ec); /** * Called when Interaction Model receives a Read Request message. Errors processing * the Read Request are handled entirely within this function. */ - void OnReadRequest(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, + void OnReadRequest(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload); Messaging::ExchangeManager * mpExchangeMgr = nullptr; diff --git a/src/app/ReadClient.cpp b/src/app/ReadClient.cpp index e857eb5ea37396..0ce185ec0054e5 100644 --- a/src/app/ReadClient.cpp +++ b/src/app/ReadClient.cpp @@ -168,7 +168,7 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin } mpExchangeCtx = mpExchangeMgr->NewContext({ aNodeId, 0, aAdminId }, this); - VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_NO_MEMORY); + VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_NO_MEMORY); mpExchangeCtx->SetResponseTimeout(kImMessageTimeoutMsec); err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::ReadRequest, std::move(msgBuf), @@ -187,7 +187,7 @@ CHIP_ERROR ReadClient::SendReadRequest(NodeId aNodeId, Transport::AdminId aAdmin return err; } -void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, +void ReadClient::OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -202,7 +202,7 @@ void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContex // Close the exchange cleanly so that the ExchangeManager will send an ack for the message we just received. mpExchangeCtx->Close(); - mpExchangeCtx = nullptr; + mpExchangeCtx.Release(); MoveToState(ClientState::Initialized); if (mpDelegate != nullptr) @@ -222,10 +222,10 @@ void ReadClient::OnMessageReceived(Messaging::ExchangeContext * apExchangeContex CHIP_ERROR ReadClient::AbortExistingExchangeContext() { - if (mpExchangeCtx != nullptr) + if (mpExchangeCtx.HasValue()) { mpExchangeCtx->Abort(); - mpExchangeCtx = nullptr; + mpExchangeCtx.Release(); } return CHIP_NO_ERROR; @@ -311,7 +311,7 @@ CHIP_ERROR ReadClient::ProcessReportData(System::PacketBufferHandle && aPayload) return err; } -void ReadClient::OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) +void ReadClient::OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext) { ChipLogProgress(DataManagement, "Time out! failed to receive report data from Exchange: %d", apExchangeContext->GetExchangeId()); diff --git a/src/app/ReadClient.h b/src/app/ReadClient.h index dae8a6878101f0..6105dec75f68ec 100644 --- a/src/app/ReadClient.h +++ b/src/app/ReadClient.h @@ -101,9 +101,9 @@ class ReadClient : public Messaging::ExchangeDelegate virtual ~ReadClient() = default; - void OnMessageReceived(Messaging::ExchangeContext * apExchangeContext, const PacketHeader & aPacketHeader, + void OnMessageReceived(Messaging::ExchangeHandle apExchangeContext, const PacketHeader & aPacketHeader, const PayloadHeader & aPayloadHeader, System::PacketBufferHandle && aPayload) override; - void OnResponseTimeout(Messaging::ExchangeContext * apExchangeContext) override; + void OnResponseTimeout(Messaging::ExchangeHandle apExchangeContext) override; /** * Check if current read client is being used @@ -119,7 +119,7 @@ class ReadClient : public Messaging::ExchangeDelegate const char * GetStateStr() const; Messaging::ExchangeManager * mpExchangeMgr = nullptr; - Messaging::ExchangeContext * mpExchangeCtx = nullptr; + Messaging::ExchangeHandle mpExchangeCtx; InteractionModelDelegate * mpDelegate = nullptr; ClientState mState = ClientState::Uninitialized; }; diff --git a/src/app/ReadHandler.cpp b/src/app/ReadHandler.cpp index cef5e6a3d8977e..4d86ecfe0ba4b3 100644 --- a/src/app/ReadHandler.cpp +++ b/src/app/ReadHandler.cpp @@ -34,8 +34,7 @@ CHIP_ERROR ReadHandler::Init(InteractionModelDelegate * apDelegate) CHIP_ERROR err = CHIP_NO_ERROR; // Error if already initialized. VerifyOrExit(apDelegate != nullptr, err = CHIP_ERROR_INCORRECT_STATE); - VerifyOrExit(mpExchangeCtx == nullptr, err = CHIP_ERROR_INCORRECT_STATE); - mpExchangeCtx = nullptr; + VerifyOrExit(!mpExchangeCtx.HasValue(), err = CHIP_ERROR_INCORRECT_STATE); mpDelegate = apDelegate; mSuppressResponse = true; mpAttributeClusterInfoList = nullptr; @@ -62,16 +61,16 @@ void ReadHandler::Shutdown() CHIP_ERROR ReadHandler::AbortExistingExchangeContext() { - if (mpExchangeCtx != nullptr) + if (mpExchangeCtx.HasValue()) { mpExchangeCtx->Abort(); - mpExchangeCtx = nullptr; + mpExchangeCtx.Release(); } return CHIP_NO_ERROR; } -CHIP_ERROR ReadHandler::OnReadRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload) +CHIP_ERROR ReadHandler::OnReadRequest(Messaging::ExchangeHandle apExchangeContext, System::PacketBufferHandle && aPayload) { CHIP_ERROR err = CHIP_NO_ERROR; System::PacketBufferHandle response; @@ -93,7 +92,7 @@ CHIP_ERROR ReadHandler::OnReadRequest(Messaging::ExchangeContext * apExchangeCon CHIP_ERROR ReadHandler::SendReportData(System::PacketBufferHandle && aPayload) { CHIP_ERROR err = CHIP_NO_ERROR; - VerifyOrExit(mpExchangeCtx != nullptr, err = CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mpExchangeCtx.HasValue(), err = CHIP_ERROR_INCORRECT_STATE); err = mpExchangeCtx->SendMessage(Protocols::InteractionModel::MsgType::ReportData, std::move(aPayload)); exit: diff --git a/src/app/ReadHandler.h b/src/app/ReadHandler.h index 8218b228eb6c67..d1683d5e3d3b1c 100644 --- a/src/app/ReadHandler.h +++ b/src/app/ReadHandler.h @@ -83,7 +83,7 @@ class ReadHandler * @retval #CHIP_NO_ERROR On success. * */ - CHIP_ERROR OnReadRequest(Messaging::ExchangeContext * apExchangeContext, System::PacketBufferHandle && aPayload); + CHIP_ERROR OnReadRequest(Messaging::ExchangeHandle apExchangeContext, System::PacketBufferHandle && aPayload); /** * Send ReportData to initiator @@ -130,7 +130,7 @@ class ReadHandler const char * GetStateStr() const; CHIP_ERROR AbortExistingExchangeContext(); - Messaging::ExchangeContext * mpExchangeCtx = nullptr; + Messaging::ExchangeHandle mpExchangeCtx; InteractionModelDelegate * mpDelegate = nullptr; // Don't need the response for report data if true diff --git a/src/app/server/EchoHandler.cpp b/src/app/server/EchoHandler.cpp index 16fa70ee8a7b9b..947bdab015ec25 100644 --- a/src/app/server/EchoHandler.cpp +++ b/src/app/server/EchoHandler.cpp @@ -38,7 +38,7 @@ chip::Protocols::Echo::EchoServer gEchoServer; * that it will free the buffer before returning. * */ -void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload) +void HandleEchoRequestReceived(chip::Messaging::ExchangeHandle ec, chip::System::PacketBufferHandle && payload) { ChipLogProgress(AppServer, "Echo Request, len=%u ... sending response.\n", payload->DataLength()); } diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index 3e6a908f6385c6..6503355d005bf9 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -315,7 +315,7 @@ static CHIP_ERROR OpenPairingWindowUsingVerifier(uint16_t discriminator, PASEVer class ServerCallback : public ExchangeDelegate { public: - void OnMessageReceived(Messaging::ExchangeContext * exchangeContext, const PacketHeader & packetHeader, + void OnMessageReceived(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && buffer) override { // as soon as a client connects, assume it is connected @@ -382,7 +382,7 @@ class ServerCallback : public ExchangeDelegate exchangeContext->Close(); } - void OnResponseTimeout(ExchangeContext * ec) override + void OnResponseTimeout(ExchangeHandle ec) override { ChipLogProgress(AppServer, "Failed to receive response"); if (mDelegate != nullptr) diff --git a/src/app/tests/TestCommandInteraction.cpp b/src/app/tests/TestCommandInteraction.cpp index 5cbb314538d3cb..416992397bb965 100644 --- a/src/app/tests/TestCommandInteraction.cpp +++ b/src/app/tests/TestCommandInteraction.cpp @@ -113,11 +113,11 @@ class TestCommandInteraction class TestExchangeDelegate : public Messaging::ExchangeDelegate { - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override {} - void OnResponseTimeout(Messaging::ExchangeContext * ec) override {} + void OnResponseTimeout(Messaging::ExchangeHandle ec) override {} }; void TestCommandInteraction::GenerateReceivedCommand(nlTestSuite * apSuite, void * apContext, System::PacketBufferHandle & aPayload, diff --git a/src/app/tests/TestReadInteraction.cpp b/src/app/tests/TestReadInteraction.cpp index 5831a2ffc68a55..477c4d8e4c89b0 100644 --- a/src/app/tests/TestReadInteraction.cpp +++ b/src/app/tests/TestReadInteraction.cpp @@ -136,7 +136,7 @@ void TestReadInteraction::TestReadHandler(nlTestSuite * apSuite, void * apContex err = writer.Finalize(&readRequestbuf); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - err = readHandler.OnReadRequest(nullptr, std::move(readRequestbuf)); + err = readHandler.OnReadRequest(Messaging::ExchangeHandle(), std::move(readRequestbuf)); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); } diff --git a/src/app/tests/TestReportingEngine.cpp b/src/app/tests/TestReportingEngine.cpp index 5c0fec489efe01..3ef4b1855d344b 100644 --- a/src/app/tests/TestReportingEngine.cpp +++ b/src/app/tests/TestReportingEngine.cpp @@ -89,11 +89,11 @@ class TestReportingEngine class TestExchangeDelegate : public Messaging::ExchangeDelegate { - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override {} - void OnResponseTimeout(Messaging::ExchangeContext * ec) override {} + void OnResponseTimeout(Messaging::ExchangeHandle ec) override {} }; void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite, void * apContext) @@ -109,7 +109,7 @@ void TestReportingEngine::TestBuildAndSendSingleReportData(nlTestSuite * apSuite err = InteractionModelEngine::GetInstance()->Init(&gExchangeManager, nullptr); NL_TEST_ASSERT(apSuite, err == CHIP_NO_ERROR); - Messaging::ExchangeContext * exchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr); + Messaging::ExchangeHandle exchangeCtx = gExchangeManager.NewContext({ 0, 0, 0 }, nullptr); TestExchangeDelegate delegate; exchangeCtx->SetDelegate(&delegate); diff --git a/src/app/tests/integration/chip_im_initiator.cpp b/src/app/tests/integration/chip_im_initiator.cpp index 5b9fffc3f3d24e..406997b1edef70 100644 --- a/src/app/tests/integration/chip_im_initiator.cpp +++ b/src/app/tests/integration/chip_im_initiator.cpp @@ -244,7 +244,7 @@ void HandleReadComplete() class MockInteractionModelApp : public chip::app::InteractionModelDelegate { public: - CHIP_ERROR EventStreamReceived(const chip::Messaging::ExchangeContext * apExchangeContext, + CHIP_ERROR EventStreamReceived(const chip::Messaging::ExchangeHandle apExchangeContext, chip::TLV::TLVReader * apEventListReader) override { return CHIP_NO_ERROR; diff --git a/src/app/util/DataModelHandler.cpp b/src/app/util/DataModelHandler.cpp index d9202fdc2aff42..be318a32f6288d 100644 --- a/src/app/util/DataModelHandler.cpp +++ b/src/app/util/DataModelHandler.cpp @@ -65,7 +65,7 @@ void InitDataModelHandler(chip::Messaging::ExchangeManager * exchangeManager) #endif } -void HandleDataModelMessage(Messaging::ExchangeContext * exchange, System::PacketBufferHandle && buffer) +void HandleDataModelMessage(Messaging::ExchangeHandle exchange, System::PacketBufferHandle && buffer) { #ifdef USE_ZAP_CONFIG EmberApsFrame frame; diff --git a/src/app/util/DataModelHandler.h b/src/app/util/DataModelHandler.h index 99cf71873cf8b7..5d25c504e59ca5 100644 --- a/src/app/util/DataModelHandler.h +++ b/src/app/util/DataModelHandler.h @@ -43,4 +43,4 @@ void InitDataModelHandler(chip::Messaging::ExchangeManager * exchangeMgr); * that it will free the buffer before returning. * */ -void HandleDataModelMessage(chip::Messaging::ExchangeContext * exchange, chip::System::PacketBufferHandle && buffer); +void HandleDataModelMessage(chip::Messaging::ExchangeHandle exchange, chip::System::PacketBufferHandle && buffer); diff --git a/src/app/util/af-types.h b/src/app/util/af-types.h index fc349785f714a1..01c2a81386e6a2 100644 --- a/src/app/util/af-types.h +++ b/src/app/util/af-types.h @@ -435,6 +435,7 @@ typedef struct */ struct EmberAfClusterCommand { + EmberAfClusterCommand() : source() {} chip::NodeId SourceNodeId() const { return source->GetSecureSession().GetPeerNodeId(); } /** @@ -442,7 +443,7 @@ struct EmberAfClusterCommand */ EmberApsFrame * apsFrame; EmberIncomingMessageType type; - chip::Messaging::ExchangeContext * source; + chip::Messaging::ExchangeHandle source; uint8_t * buffer; uint16_t bufLen; bool clusterSpecific; @@ -1305,8 +1306,8 @@ class MessageSendDestination struct VariantViaExchange { static constexpr const std::size_t VariantId = 8; - explicit VariantViaExchange(Messaging::ExchangeContext * exchangeContext) : mExchangeContext(exchangeContext) {} - Messaging::ExchangeContext * mExchangeContext; + explicit VariantViaExchange(Messaging::ExchangeHandle exchangeContext) : mExchangeContext(exchangeContext) {} + ExchangeHandle mExchangeContext; }; MessageSendDestination(MessageSendDestination & that) = default; diff --git a/src/app/util/chip-message-send.cpp b/src/app/util/chip-message-send.cpp index ff43a94491dad5..298c23f8244682 100644 --- a/src/app/util/chip-message-send.cpp +++ b/src/app/util/chip-message-send.cpp @@ -43,16 +43,16 @@ namespace chip { // Delete this class when Device::SendMessage() is obsoleted. class DeviceExchangeDelegate : public Messaging::ExchangeDelegate { - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override {} - void OnResponseTimeout(Messaging::ExchangeContext * ec) override {} + void OnResponseTimeout(Messaging::ExchangeHandle ec) override {} }; extern Messaging::ExchangeManager * ExchangeManager(); } // namespace chip -EmberStatus chipSendUnicast(Messaging::ExchangeContext * exchange, EmberApsFrame * apsFrame, uint16_t messageLength, +EmberStatus chipSendUnicast(Messaging::ExchangeHandle exchange, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, Messaging::SendFlags sendFlags) { uint16_t frameSize = encodeApsFrame(nullptr, 0, apsFrame); @@ -108,8 +108,8 @@ EmberStatus chipSendUnicast(NodeId destination, EmberApsFrame * apsFrame, uint16 return EMBER_DELIVERY_FAILED; } - Messaging::ExchangeContext * exchange = exchangeMgr->NewContext({ destination, Transport::kAnyKeyId, 0 }, nullptr); - if (exchange == nullptr) + Messaging::ExchangeHandle exchange = exchangeMgr->NewContext({ destination, Transport::kAnyKeyId, 0 }, nullptr); + if (!exchange.HasValue()) { return EMBER_DELIVERY_FAILED; } diff --git a/src/app/util/chip-message-send.h b/src/app/util/chip-message-send.h index ca429785eb360b..255ddbaa7e7e3d 100644 --- a/src/app/util/chip-message-send.h +++ b/src/app/util/chip-message-send.h @@ -41,7 +41,7 @@ * @param[in] message The message to send after the APS frame. * @param[in] sendFlags The SendFlags needed, if any. */ -EmberStatus chipSendUnicast(chip::Messaging::ExchangeContext * exchange, EmberApsFrame * apsFrame, uint16_t messageLength, +EmberStatus chipSendUnicast(chip::Messaging::ExchangeHandle exchange, EmberApsFrame * apsFrame, uint16_t messageLength, uint8_t * message, chip::Messaging::SendFlags sendFlags = chip::Messaging::SendFlags()); /** diff --git a/src/app/util/common.h b/src/app/util/common.h index 05cef174755586..146a5f0eb5d5d6 100644 --- a/src/app/util/common.h +++ b/src/app/util/common.h @@ -54,4 +54,4 @@ extern EmberApsFrame emberAfResponseApsFrame; extern uint8_t appResponseData[EMBER_AF_RESPONSE_BUFFER_LEN]; extern uint16_t appResponseLength; -extern chip::Messaging::ExchangeContext * emberAfResponseDestination; +extern chip::Messaging::ExchangeHandle emberAfResponseDestination; diff --git a/src/app/util/ember-compatibility-functions.cpp b/src/app/util/ember-compatibility-functions.cpp index f55c66fe0b5978..5b3ca085e1ca5c 100644 --- a/src/app/util/ember-compatibility-functions.cpp +++ b/src/app/util/ember-compatibility-functions.cpp @@ -41,12 +41,12 @@ Command * currentCommandObject; void SetupEmberAfObjects(Command * command, ClusterId clusterId, CommandId commandId, EndpointId endpointId) { - Messaging::ExchangeContext * commandExchangeCtx = command->GetExchangeContext(); + Messaging::ExchangeHandle commandExchangeCtx = command->GetExchangeContext(); imCompatibilityEmberApsFrame.clusterId = clusterId; imCompatibilityEmberApsFrame.destinationEndpoint = endpointId; imCompatibilityEmberApsFrame.sourceEndpoint = 1; // source endpoint is fixed to 1 for now. - imCompatibilityEmberApsFrame.sequence = (commandExchangeCtx != nullptr ? commandExchangeCtx->GetExchangeId() & 0xFF : 0); + imCompatibilityEmberApsFrame.sequence = (commandExchangeCtx.HasValue() ? commandExchangeCtx->GetExchangeId() & 0xFF : 0); imCompatibilityEmberAfCluster.commandId = commandId; imCompatibilityEmberAfCluster.apsFrame = &imCompatibilityEmberApsFrame; diff --git a/src/app/util/util.cpp b/src/app/util/util.cpp index d15334bd6c7d80..356b9c6aa522df 100644 --- a/src/app/util/util.cpp +++ b/src/app/util/util.cpp @@ -493,7 +493,7 @@ static bool dispatchZclMessage(EmberAfClusterCommand * cmd) } bool emberAfProcessMessageIntoZclCmd(EmberApsFrame * apsFrame, EmberIncomingMessageType type, uint8_t * message, - uint16_t messageLength, Messaging::ExchangeContext * exchange, InterPanHeader * interPanHeader, + uint16_t messageLength, Messaging::ExchangeHandle exchange, InterPanHeader * interPanHeader, EmberAfClusterCommand * returnCmd) { uint8_t minLength = @@ -539,7 +539,7 @@ bool emberAfProcessMessageIntoZclCmd(EmberApsFrame * apsFrame, EmberIncomingMess // a single call to process global and cluster-specific messages and callbacks. bool emberAfProcessMessage(EmberApsFrame * apsFrame, EmberIncomingMessageType type, uint8_t * message, uint16_t msgLen, - Messaging::ExchangeContext * exchange, InterPanHeader * interPanHeader) + Messaging::ExchangeHandle exchange, InterPanHeader * interPanHeader) { bool msgHandled = false; // reset/reinitialize curCmd @@ -703,7 +703,7 @@ void emAfApplyDisableDefaultResponse(uint8_t * frame_control) } } -static bool isBroadcastDestination(Messaging::ExchangeContext * responseDestination) +static bool isBroadcastDestination(Messaging::ExchangeHandle responseDestination) { // TODO: Will need to actually figure out how to test for this! return false; diff --git a/src/app/util/util.h b/src/app/util/util.h index 20ab0f3cb081e8..422356cf561656 100644 --- a/src/app/util/util.h +++ b/src/app/util/util.h @@ -152,10 +152,10 @@ void emberAfDecodeAndPrintCluster(chip::ClusterId cluster); void emberAfDecodeAndPrintClusterWithMfgCode(chip::ClusterId cluster, uint16_t mfgCode); bool emberAfProcessMessage(EmberApsFrame * apsFrame, EmberIncomingMessageType type, uint8_t * message, uint16_t msgLen, - chip::Messaging::ExchangeContext * source, InterPanHeader * interPanHeader); + chip::Messaging::ExchangeHandle source, InterPanHeader * interPanHeader); bool emberAfProcessMessageIntoZclCmd(EmberApsFrame * apsFrame, EmberIncomingMessageType type, uint8_t * message, - uint16_t messageLength, chip::Messaging::ExchangeContext * source, + uint16_t messageLength, chip::Messaging::ExchangeHandle source, InterPanHeader * interPanHeader, EmberAfClusterCommand * returnCmd); /** diff --git a/src/channel/Channel.cpp b/src/channel/Channel.cpp index 151b10ab78ed74..7e7118a347c6d8 100644 --- a/src/channel/Channel.cpp +++ b/src/channel/Channel.cpp @@ -30,7 +30,7 @@ ChannelState ChannelHandle::GetState() const return mAssociation->mChannelContext->GetState(); } -ExchangeContext * ChannelHandle::NewExchange(ExchangeDelegate * delegate) +ExchangeHandle ChannelHandle::NewExchange(ExchangeDelegate * delegate) { assert(mAssociation != nullptr); return mAssociation->mChannelContext->NewExchange(delegate); diff --git a/src/channel/Channel.h b/src/channel/Channel.h index 2d6821ec1ef711..d311159575376c 100644 --- a/src/channel/Channel.h +++ b/src/channel/Channel.h @@ -45,6 +45,7 @@ #include #include +#include #include namespace chip { @@ -187,7 +188,7 @@ class ChannelHandle * * @pre GetState() == ChannelState::kReady */ - ExchangeContext * NewExchange(ExchangeDelegate * delegate); + ExchangeHandle NewExchange(ExchangeDelegate * delegate); void Release(); diff --git a/src/channel/ChannelContext.cpp b/src/channel/ChannelContext.cpp index 97798f604b72c7..33e5c98458bc09 100644 --- a/src/channel/ChannelContext.cpp +++ b/src/channel/ChannelContext.cpp @@ -35,7 +35,7 @@ void ChannelContext::Start(const ChannelBuilder & builder) EnterPreparingState(builder); } -ExchangeContext * ChannelContext::NewExchange(ExchangeDelegate * delegate) +ExchangeHandle ChannelContext::NewExchange(ExchangeDelegate * delegate) { assert(GetState() == ChannelState::kReady); return mExchangeManager->NewContext(GetReadyVars().mSession, delegate); @@ -258,8 +258,8 @@ void ChannelContext::EnterCasePairingState() auto & prepare = GetPrepareVars(); prepare.mCasePairingSession = Platform::New(); - ExchangeContext * ctxt = mExchangeManager->NewContext(SecureSessionHandle(), prepare.mCasePairingSession); - VerifyOrReturn(ctxt != nullptr); + ExchangeHandle ctxt = mExchangeManager->NewContext(SecureSessionHandle(), prepare.mCasePairingSession); + VerifyOrReturn(ctxt.HasValue()); // TODO: currently only supports IP/UDP paring Transport::PeerAddress addr; diff --git a/src/channel/ChannelContext.h b/src/channel/ChannelContext.h index 393c1cbad2c97a..d99aece95a27a9 100644 --- a/src/channel/ChannelContext.h +++ b/src/channel/ChannelContext.h @@ -28,6 +28,7 @@ #include #include #include +#include #include #include #include @@ -90,7 +91,7 @@ class ChannelContext : public ReferenceCounted gTransportMgr; class MockAppDelegate : public ExchangeDelegate { public: - void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && buffer) override { IsOnMessageReceivedCalled = true; } - void OnResponseTimeout(ExchangeContext * ec) override {} + void OnResponseTimeout(ExchangeHandle ec) override {} bool IsOnMessageReceivedCalled = false; }; @@ -118,7 +118,7 @@ void CheckExchangeChannels(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, channelHandle.GetState() == ChannelState::kReady); MockAppDelegate mockAppDelegate; - ExchangeContext * ec1 = channelHandle.NewExchange(&mockAppDelegate); + ExchangeHandle ec1 = channelHandle.NewExchange(&mockAppDelegate); // send a malicious packet ec1->SendMessage(0x0001, 0x0002, System::PacketBufferHandle::New(System::PacketBuffer::kMaxSize)); diff --git a/src/controller/CHIPDevice.cpp b/src/controller/CHIPDevice.cpp index 41bd381dd6ca14..105bb47b939783 100644 --- a/src/controller/CHIPDevice.cpp +++ b/src/controller/CHIPDevice.cpp @@ -66,8 +66,8 @@ CHIP_ERROR Device::SendMessage(Protocols::Id protocolId, uint8_t msgType, System ReturnErrorOnFailure(LoadSecureSessionParametersIfNeeded(loadedSecureSession)); - Messaging::ExchangeContext * exchange = mExchangeMgr->NewContext(mSecureSession, nullptr); - VerifyOrReturnError(exchange != nullptr, CHIP_ERROR_NO_MEMORY); + Messaging::ExchangeHandle exchange = mExchangeMgr->NewContext(mSecureSession, nullptr); + VerifyOrReturnError(exchange.HasValue(), CHIP_ERROR_NO_MEMORY); if (!loadedSecureSession) { @@ -270,7 +270,7 @@ void Device::OnConnectionExpired(SecureSessionHandle session) mSecureSession = SecureSessionHandle{}; } -void Device::OnMessageReceived(Messaging::ExchangeContext * exchange, const PacketHeader & header, +void Device::OnMessageReceived(Messaging::ExchangeHandle exchange, const PacketHeader & header, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf) { if (mState == ConnectionState::SecureConnected) @@ -287,7 +287,7 @@ void Device::OnMessageReceived(Messaging::ExchangeContext * exchange, const Pack exchange->Close(); } -void Device::OnResponseTimeout(Messaging::ExchangeContext * ec) +void Device::OnResponseTimeout(Messaging::ExchangeHandle ec) { ec->Close(); } @@ -403,8 +403,8 @@ bool Device::GetAddress(Inet::IPAddress & addr, uint16_t & port) const CHIP_ERROR Device::EstablishCASESession() { - Messaging::ExchangeContext * exchange = mExchangeMgr->NewContext(SecureSessionHandle(), &mCASESession); - VerifyOrReturnError(exchange != nullptr, CHIP_ERROR_INTERNAL); + Messaging::ExchangeHandle exchange = mExchangeMgr->NewContext(SecureSessionHandle(), &mCASESession); + VerifyOrReturnError(exchange.HasValue(), CHIP_ERROR_INTERNAL); ReturnErrorOnFailure(mCASESession.MessageDispatch().Init(mSessionManager->GetTransportManager())); mCASESession.MessageDispatch().SetPeerAddress(mDeviceAddress); diff --git a/src/controller/CHIPDevice.h b/src/controller/CHIPDevice.h index 41b54d8d5f1868..aaf64117bcee46 100644 --- a/src/controller/CHIPDevice.h +++ b/src/controller/CHIPDevice.h @@ -258,13 +258,13 @@ class DLL_EXPORT Device : public Messaging::ExchangeDelegate, public SessionEsta * @param[in] payloadHeader Reference to payload header in the message * @param[in] msgBuf The message buffer */ - void OnMessageReceived(Messaging::ExchangeContext * exchange, const PacketHeader & header, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle exchange, const PacketHeader & header, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf) override; /** * @brief ExchangeDelegate implementation of OnResponseTimeout. */ - void OnResponseTimeout(Messaging::ExchangeContext * exchange) override; + void OnResponseTimeout(Messaging::ExchangeHandle exchange) override; /** * @brief diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index 07a84217c3504b..79d1dfbf6e6ef4 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -525,7 +525,7 @@ CHIP_ERROR DeviceController::ServiceEventSignal() return CHIP_NO_ERROR; } -void DeviceController::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, +void DeviceController::OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf) { uint16_t index; @@ -549,9 +549,9 @@ void DeviceController::OnMessageReceived(Messaging::ExchangeContext * ec, const } } -void DeviceController::OnResponseTimeout(Messaging::ExchangeContext * ec) +void DeviceController::OnResponseTimeout(Messaging::ExchangeHandle ec) { - ChipLogProgress(Controller, "Time out! failed to receive response from Exchange: %p", ec); + ChipLogProgress(Controller, "Time out! failed to receive response from Exchange: %d", ec->GetExchangeId()); } void DeviceController::OnNewConnection(SecureSessionHandle session, Messaging::ExchangeManager * mgr) @@ -829,7 +829,7 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam Device * device = nullptr; Transport::PeerAddress peerAddress = Transport::PeerAddress::UDP(Inet::IPAddress::Any); - Messaging::ExchangeContext * exchangeCtxt = nullptr; + Messaging::ExchangeHandle exchangeCtxt; Transport::AdminPairingInfo * admin = mAdmins.FindAdminWithId(mAdminId); @@ -897,7 +897,7 @@ CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParam } #endif exchangeCtxt = mExchangeMgr->NewContext(SecureSessionHandle(), &mPairingSession); - VerifyOrExit(exchangeCtxt != nullptr, err = CHIP_ERROR_INTERNAL); + VerifyOrExit(exchangeCtxt.HasValue(), err = CHIP_ERROR_INTERNAL); err = mPairingSession.Pair(params.GetPeerAddress(), params.GetSetupPINCode(), mNextKeyId++, exchangeCtxt, this); // Immediately persist the updted mNextKeyID value diff --git a/src/controller/CHIPDeviceController.h b/src/controller/CHIPDeviceController.h index 55dc13371fcd10..7617a2fd5577b6 100644 --- a/src/controller/CHIPDeviceController.h +++ b/src/controller/CHIPDeviceController.h @@ -290,9 +290,9 @@ class DLL_EXPORT DeviceController : public Messaging::ExchangeDelegate, private: //////////// ExchangeDelegate Implementation /////////////// - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf) override; - void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + void OnResponseTimeout(Messaging::ExchangeHandle ec) override; //////////// ExchangeMgrDelegate Implementation /////////////// void OnNewConnection(SecureSessionHandle session, Messaging::ExchangeManager * mgr) override; diff --git a/src/messaging/BUILD.gn b/src/messaging/BUILD.gn index 76966ef817eb4b..e12ec2a7e6a99b 100644 --- a/src/messaging/BUILD.gn +++ b/src/messaging/BUILD.gn @@ -26,6 +26,8 @@ static_library("messaging") { "ExchangeContext.cpp", "ExchangeContext.h", "ExchangeDelegate.h", + "ExchangeHandle.cpp", + "ExchangeHandle.h", "ExchangeMessageDispatch.cpp", "ExchangeMessageDispatch.h", "ExchangeMgr.cpp", diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp index 6dd1bf49db73b6..a517bcb87d8d3a 100644 --- a/src/messaging/ExchangeContext.cpp +++ b/src/messaging/ExchangeContext.cpp @@ -49,7 +49,7 @@ using namespace chip::System; namespace chip { namespace Messaging { -static void DefaultOnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, Protocols::Id protocolId, +static void DefaultOnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, Protocols::Id protocolId, uint8_t msgType, PacketBufferHandle && payload) { ChipLogError(ExchangeManager, "Dropping unexpected message %08" PRIX32 ":%d %04" PRIX16 " MsgId:%08" PRIX32, @@ -87,10 +87,7 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp // Don't let method get called on a freed object. VerifyOrDie(mExchangeMgr != nullptr && GetReferenceCount() > 0); - // we hold the exchange context here in case the entity that - // originally generated it tries to close it as a result of - // an error arising below. at the end, we have to close it. - Retain(); + ExchangeHandle ec(this); bool reliableTransmissionRequested = true; @@ -121,7 +118,7 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp } } - err = mDispatch->SendMessage(mSecureSession, mExchangeId, IsInitiator(), GetReliableMessageContext(), + err = mDispatch->SendMessage(mSecureSession, mExchangeId, IsInitiator(), ExchangeHandle(this), reliableTransmissionRequested, protocolId, msgType, std::move(msgBuf)); exit: @@ -131,13 +128,6 @@ CHIP_ERROR ExchangeContext::SendMessage(Protocols::Id protocolId, uint8_t msgTyp SetResponseExpected(false); } - // Release the reference to the exchange context acquired above. Under normal circumstances - // this will merely decrement the reference count, without actually freeing the exchange context. - // However if one of the function calls in this method resulted in a callback to the protocol, - // the protocol may have released its reference, resulting in the exchange context actually - // being freed here. - Release(); - return err; } @@ -160,7 +150,7 @@ void ExchangeContext::DoClose(bool clearRetransTable) // needs to clear the CRMP retransmission table immediately. if (clearRetransTable) { - mExchangeMgr->GetReliableMessageMgr()->ClearRetransTable(static_cast(this)); + mExchangeMgr->GetReliableMessageMgr()->ClearRetransTable(ExchangeHandle(this)); } // Cancel the response timer. @@ -183,7 +173,6 @@ void ExchangeContext::Close() #endif DoClose(false); - Release(); } /** * Abort the Exchange context immediately and release all @@ -200,7 +189,6 @@ void ExchangeContext::Abort() #endif DoClose(true); - Release(); } void ExchangeContextDeletor::Release(ExchangeContext * ec) @@ -326,10 +314,9 @@ void ExchangeContext::CancelResponseTimer() void ExchangeContext::HandleResponseTimeout(System::Layer * aSystemLayer, void * aAppState, System::Error aError) { - ExchangeContext * ec = reinterpret_cast(aAppState); + if (aAppState == nullptr) return; - if (ec == nullptr) - return; + ExchangeHandle ec(reinterpret_cast(aAppState)); // NOTE: we don't set mResponseExpected to false here because the response could still arrive. If the user // wants to never receive the response, they must close the exchange context. @@ -348,10 +335,10 @@ CHIP_ERROR ExchangeContext::HandleMessage(const PacketHeader & packetHeader, con // guard against Close() calls(decrementing the reference // count) by the protocol before the CHIP Exchange // layer has completed its work on the ExchangeContext. - Retain(); + ExchangeHandle ec(this); CHIP_ERROR err = - mDispatch->OnMessageReceived(payloadHeader, packetHeader.GetMessageId(), peerAddress, GetReliableMessageContext()); + mDispatch->OnMessageReceived(payloadHeader, packetHeader.GetMessageId(), peerAddress, ExchangeHandle(this)); SuccessOrExit(err); // The SecureChannel::StandaloneAck message type is only used for CRMP; do not pass such messages to the application layer. @@ -369,20 +356,15 @@ CHIP_ERROR ExchangeContext::HandleMessage(const PacketHeader & packetHeader, con if (mDelegate != nullptr) { - mDelegate->OnMessageReceived(this, packetHeader, payloadHeader, std::move(msgBuf)); + mDelegate->OnMessageReceived(ExchangeHandle(this), packetHeader, payloadHeader, std::move(msgBuf)); } else { - DefaultOnMessageReceived(this, packetHeader, payloadHeader.GetProtocolID(), payloadHeader.GetMessageType(), + DefaultOnMessageReceived(ExchangeHandle(this), packetHeader, payloadHeader.GetProtocolID(), payloadHeader.GetMessageType(), std::move(msgBuf)); } exit: - // Release the reference to the ExchangeContext that was held at the beginning of this function. - // This call should also do the needful of closing the ExchangeContext if the protocol has - // already made a prior call to Close(). - Release(); - return err; } diff --git a/src/messaging/ExchangeContext.h b/src/messaging/ExchangeContext.h index c2aa6ea944b62a..79769ac76446d7 100644 --- a/src/messaging/ExchangeContext.h +++ b/src/messaging/ExchangeContext.h @@ -54,7 +54,7 @@ class ExchangeContextDeletor * It defines methods for encoding and communicating CHIP messages within an ExchangeContext * over various transport mechanisms, for example, TCP, UDP, or CHIP Reliable Messaging. */ -class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public ReferenceCounted +class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public ReferenceCounted { friend class ExchangeManager; friend class ExchangeContextDeletor; @@ -133,7 +133,7 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen ExchangeManager * GetExchangeMgr() const { return mExchangeMgr; } - ReliableMessageContext * GetReliableMessageContext() { return static_cast(this); }; + ReliableMessageContext & GetReliableMessageContext() { return *static_cast(this); }; ExchangeMessageDispatch * GetMessageDispatch() { return mDispatch; } @@ -155,11 +155,6 @@ class DLL_EXPORT ExchangeContext : public ReliableMessageContext, public Referen uint16_t GetExchangeId() const { return mExchangeId; } - /* - * In order to use reference counting (see refCount below) we use a hold/free paradigm where users of the exchange - * can hold onto it while it's out of their direct control to make sure it isn't closed before everyone's ready. - * A customized version of reference counting is used since there are some extra stuff to do within Release. - */ void Close(); void Abort(); diff --git a/src/messaging/ExchangeDelegate.h b/src/messaging/ExchangeDelegate.h index 7656e83c460de1..a10b930cf12d9d 100644 --- a/src/messaging/ExchangeDelegate.h +++ b/src/messaging/ExchangeDelegate.h @@ -24,6 +24,7 @@ #pragma once #include +#include #include #include #include @@ -56,7 +57,7 @@ class DLL_EXPORT ExchangeDelegate * @param[in] payloadHeader A reference to the PayloadHeader object. * @param[in] payload A handle to the PacketBuffer object holding the message payload. */ - virtual void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + virtual void OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) = 0; /** @@ -66,7 +67,7 @@ class DLL_EXPORT ExchangeDelegate * * @param[in] ec A pointer to the ExchangeContext object. */ - virtual void OnResponseTimeout(ExchangeContext * ec) = 0; + virtual void OnResponseTimeout(ExchangeHandle ec) = 0; /** * @brief diff --git a/src/messaging/ExchangeHandle.cpp b/src/messaging/ExchangeHandle.cpp new file mode 100644 index 00000000000000..c8334206108aff --- /dev/null +++ b/src/messaging/ExchangeHandle.cpp @@ -0,0 +1,58 @@ +/* + * + * Copyright (c) 2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include + +namespace chip { +namespace Messaging { + +ExchangeHandle::ExchangeHandle(ExchangeContext * context) : mContext(context) +{ + if (mContext != nullptr) mContext->Retain(); +} + +ExchangeHandle::ExchangeHandle(const ExchangeHandle & that) : mContext(that.mContext) +{ + if (mContext != nullptr) mContext->Retain(); +} + +ExchangeHandle::~ExchangeHandle() +{ + if (mContext != nullptr) mContext->Release(); +} + +ExchangeHandle & ExchangeHandle::operator=(const ExchangeHandle & that) +{ + if (this == &that) return *this; + mContext = that.mContext; + if (mContext != nullptr) mContext->Retain(); + return *this; +} + +void ExchangeHandle::Release() +{ + if (mContext != nullptr) + { + mContext->Release(); + mContext = nullptr; + } +} + +} // namespace Messaging +} // namespace chip diff --git a/src/messaging/ExchangeHandle.h b/src/messaging/ExchangeHandle.h new file mode 100644 index 00000000000000..768f42f416d1a3 --- /dev/null +++ b/src/messaging/ExchangeHandle.h @@ -0,0 +1,62 @@ +/* + * + * Copyright (c) 2020-2021 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +namespace chip { + +namespace Messaging { + +class ExchangeContext; + +/** + * ExchangeHandle acts like a unique_ptr to an ExchangeContext, to help managing the life cycle of the ExchangeContext + */ +class ExchangeHandle +{ +public: + explicit ExchangeHandle() : mContext(nullptr) {} + ~ExchangeHandle(); + + ExchangeHandle(const ExchangeHandle & that); + ExchangeHandle(ExchangeHandle && that) : mContext(that.mContext) { that.mContext = nullptr; } + + ExchangeHandle & operator=(const ExchangeHandle & that); + ExchangeHandle & operator=(ExchangeHandle && that) + { + if (this == &that) return *this; + mContext = that.mContext; + that.mContext = nullptr; + return *this; + } + + bool HasValue() const { return mContext != nullptr; } + void Release(); + + ExchangeContext * operator->() const { return mContext; } + bool operator==(const ExchangeHandle & that) const { return mContext == that.mContext; } + bool operator!=(const ExchangeHandle & that) const { return !(*this == that); } +private: + friend class ExchangeContext; + friend class ExchangeManager; + explicit ExchangeHandle(ExchangeContext * context); + ExchangeContext * mContext; +}; + +} // namespace Messaging +} // namespace chip + diff --git a/src/messaging/ExchangeMessageDispatch.cpp b/src/messaging/ExchangeMessageDispatch.cpp index c998ef6968864e..7d2c95f1b430d9 100644 --- a/src/messaging/ExchangeMessageDispatch.cpp +++ b/src/messaging/ExchangeMessageDispatch.cpp @@ -40,32 +40,33 @@ namespace chip { namespace Messaging { CHIP_ERROR ExchangeMessageDispatch::SendMessage(SecureSessionHandle session, uint16_t exchangeId, bool isInitiator, - ReliableMessageContext * reliableMessageContext, bool isReliableTransmission, + ExchangeHandle exchangeContext, bool isReliableTransmission, Protocols::Id protocol, uint8_t type, System::PacketBufferHandle && message) { + ReliableMessageContext & reliableMessageContext = exchangeContext->GetReliableMessageContext(); ReturnErrorCodeIf(!MessagePermitted(protocol.GetProtocolId(), type), CHIP_ERROR_INVALID_ARGUMENT); PayloadHeader payloadHeader; payloadHeader.SetExchangeID(exchangeId).SetMessageType(protocol, type).SetInitiator(isInitiator); // If there is a pending acknowledgment piggyback it on this message. - if (reliableMessageContext->HasPeerRequestedAck()) + if (reliableMessageContext.HasPeerRequestedAck()) { - payloadHeader.SetAckId(reliableMessageContext->GetPendingPeerAckId()); + payloadHeader.SetAckId(reliableMessageContext.GetPendingPeerAckId()); // Set AckPending flag to false since current outgoing message is going to serve as the ack on this exchange. - reliableMessageContext->SetAckPending(false); + reliableMessageContext.SetAckPending(false); #if !defined(NDEBUG) if (!payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::StandaloneAck)) { ChipLogDetail(ExchangeManager, "Piggybacking Ack for MsgId:%08" PRIX32 " with msg", - reliableMessageContext->GetPendingPeerAckId()); + reliableMessageContext.GetPendingPeerAckId()); } #endif } - if (IsReliableTransmissionAllowed() && reliableMessageContext->AutoRequestAck() && mReliableMessageMgr != nullptr && + if (IsReliableTransmissionAllowed() && reliableMessageContext.AutoRequestAck() && mReliableMessageMgr != nullptr && isReliableTransmission) { payloadHeader.SetNeedsAck(true); @@ -73,7 +74,7 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(SecureSessionHandle session, uin ReliableMessageMgr::RetransTableEntry * entry = nullptr; // Add to Table for subsequent sending - ReturnErrorOnFailure(mReliableMessageMgr->AddToRetransTable(reliableMessageContext, &entry)); + ReturnErrorOnFailure(mReliableMessageMgr->AddToRetransTable(exchangeContext, &entry)); CHIP_ERROR err = SendMessageImpl(session, payloadHeader, std::move(message), &entry->retainedBuf); if (err != CHIP_NO_ERROR) @@ -85,6 +86,7 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(SecureSessionHandle session, uin } else { + VerifyOrDie(!entry->retainedBuf.IsNull()); mReliableMessageMgr->StartRetransmision(entry); } } @@ -100,16 +102,17 @@ CHIP_ERROR ExchangeMessageDispatch::SendMessage(SecureSessionHandle session, uin CHIP_ERROR ExchangeMessageDispatch::OnMessageReceived(const PayloadHeader & payloadHeader, uint32_t messageId, const Transport::PeerAddress & peerAddress, - ReliableMessageContext * reliableMessageContext) + ExchangeHandle exchangeContext) { ReturnErrorCodeIf(!MessagePermitted(payloadHeader.GetProtocolID().GetProtocolId(), payloadHeader.GetMessageType()), CHIP_ERROR_INVALID_ARGUMENT); + ReliableMessageContext & reliableMessageContext = exchangeContext->GetReliableMessageContext(); if (IsReliableTransmissionAllowed()) { if (payloadHeader.IsAckMsg() && payloadHeader.GetAckId().HasValue()) { - ReturnErrorOnFailure(reliableMessageContext->HandleRcvdAck(payloadHeader.GetAckId().Value())); + ReturnErrorOnFailure(reliableMessageContext.HandleRcvdAck(exchangeContext, payloadHeader.GetAckId().Value())); } if (payloadHeader.NeedsAck()) @@ -121,9 +124,9 @@ CHIP_ERROR ExchangeMessageDispatch::OnMessageReceived(const PayloadHeader & payl msgFlags.Set(MessageFlagValues::kPeerRequestedAck); // Also set the flag in the exchange context indicating an ack requested; - reliableMessageContext->SetPeerRequestedAck(true); + reliableMessageContext.SetPeerRequestedAck(true); - ReturnErrorOnFailure(reliableMessageContext->HandleNeedsAck(messageId, msgFlags)); + ReturnErrorOnFailure(reliableMessageContext.HandleNeedsAck(exchangeContext, messageId, msgFlags)); } } diff --git a/src/messaging/ExchangeMessageDispatch.h b/src/messaging/ExchangeMessageDispatch.h index 57179710d84017..731d3e24ab11b8 100644 --- a/src/messaging/ExchangeMessageDispatch.h +++ b/src/messaging/ExchangeMessageDispatch.h @@ -25,6 +25,7 @@ #include #include +#include namespace chip { namespace Messaging { @@ -45,7 +46,7 @@ class ExchangeMessageDispatch : public ReferenceCounted } CHIP_ERROR SendMessage(SecureSessionHandle session, uint16_t exchangeId, bool isInitiator, - ReliableMessageContext * reliableMessageContext, bool isReliableTransmission, Protocols::Id protocol, + ExchangeHandle exchangeContext, bool isReliableTransmission, Protocols::Id protocol, uint8_t type, System::PacketBufferHandle && message); virtual CHIP_ERROR ResendMessage(SecureSessionHandle session, EncryptedPacketBufferHandle && message, @@ -56,7 +57,7 @@ class ExchangeMessageDispatch : public ReferenceCounted virtual CHIP_ERROR OnMessageReceived(const PayloadHeader & payloadHeader, uint32_t messageId, const Transport::PeerAddress & peerAddress, - ReliableMessageContext * reliableMessageContext); + ExchangeHandle exchangeContext); protected: virtual bool MessagePermitted(uint16_t protocol, uint8_t type) = 0; diff --git a/src/messaging/ExchangeMgr.cpp b/src/messaging/ExchangeMgr.cpp index 1215751347af48..441df5591f3278 100644 --- a/src/messaging/ExchangeMgr.cpp +++ b/src/messaging/ExchangeMgr.cpp @@ -114,9 +114,9 @@ CHIP_ERROR ExchangeManager::Shutdown() return CHIP_NO_ERROR; } -ExchangeContext * ExchangeManager::NewContext(SecureSessionHandle session, ExchangeDelegate * delegate) +ExchangeHandle ExchangeManager::NewContext(SecureSessionHandle session, ExchangeDelegate * delegate) { - return mContextPool.CreateObject(this, mNextExchangeId++, session, true, delegate); + return ExchangeHandle(mContextPool.CreateObject(this, mNextExchangeId++, session, true, delegate)); } CHIP_ERROR ExchangeManager::RegisterUnsolicitedMessageHandlerForProtocol(Protocols::Id protocolId, ExchangeDelegate * delegate) @@ -264,21 +264,14 @@ void ExchangeManager::OnMessageReceived(const PacketHeader & packetHeader, const // If we found a handler or we need to create a new exchange context (EC). if (matchingUMH != nullptr || sendAckAndCloseExchange) { - ExchangeContext * ec = nullptr; - - if (sendAckAndCloseExchange) - { // If rcvd msg is from initiator then this exchange is created as not Initiator. // If rcvd msg is not from initiator then this exchange is created as Initiator. // TODO: Figure out which channel to use for the received message - ec = mContextPool.CreateObject(this, payloadHeader.GetExchangeID(), session, !payloadHeader.IsInitiator(), nullptr); - } - else - { - ec = mContextPool.CreateObject(this, payloadHeader.GetExchangeID(), session, false, matchingUMH->Delegate); - } + ExchangeHandle ec(mContextPool.CreateObject(this, payloadHeader.GetExchangeID(), session, + sendAckAndCloseExchange && !payloadHeader.IsInitiator(), + sendAckAndCloseExchange ? nullptr : matchingUMH->Delegate)); - VerifyOrExit(ec != nullptr, err = CHIP_ERROR_NO_MEMORY); + VerifyOrExit(ec.HasValue(), err = CHIP_ERROR_NO_MEMORY); ChipLogDetail(ExchangeManager, "ec id: %d, Delegate: 0x%x", ec->GetExchangeId(), ec->GetDelegate()); diff --git a/src/messaging/ExchangeMgr.h b/src/messaging/ExchangeMgr.h index 25ede5e4901836..b71b516d10b722 100644 --- a/src/messaging/ExchangeMgr.h +++ b/src/messaging/ExchangeMgr.h @@ -99,7 +99,7 @@ class DLL_EXPORT ExchangeManager : public SecureSessionMgrDelegate, public Trans * @return A pointer to the created ExchangeContext object On success. Otherwise NULL if no object * can be allocated or is available. */ - ExchangeContext * NewContext(SecureSessionHandle session, ExchangeDelegate * delegate); + ExchangeHandle NewContext(SecureSessionHandle session, ExchangeDelegate * delegate); void ReleaseContext(ExchangeContext * ec) { mContextPool.ReleaseObject(ec); } diff --git a/src/messaging/ReliableMessageContext.cpp b/src/messaging/ReliableMessageContext.cpp index 91220a995bbd81..2078a0f030fb99 100644 --- a/src/messaging/ReliableMessageContext.cpp +++ b/src/messaging/ReliableMessageContext.cpp @@ -42,15 +42,6 @@ ReliableMessageContext::ReliableMessageContext() : mConfig(gDefaultReliableMessageProtocolConfig), mNextAckTimeTick(0), mPendingPeerAckId(0) {} -void ReliableMessageContext::RetainContext() -{ - GetExchangeContext()->Retain(); -} - -void ReliableMessageContext::ReleaseContext() -{ - GetExchangeContext()->Release(); -} bool ReliableMessageContext::AutoRequestAck() const { @@ -165,12 +156,12 @@ uint64_t ReliableMessageContext::GetActiveRetransmitTimeoutTick() * @retval #CHIP_NO_ERROR if the context was removed. * */ -CHIP_ERROR ReliableMessageContext::HandleRcvdAck(uint32_t AckMsgId) +CHIP_ERROR ReliableMessageContext::HandleRcvdAck(ExchangeHandle exchangeContext, uint32_t AckMsgId) { CHIP_ERROR err = CHIP_NO_ERROR; // Msg is an Ack; Check Retrans Table and remove message context - if (!GetReliableMessageMgr()->CheckAndRemRetransTable(this, AckMsgId)) + if (!GetReliableMessageMgr()->CheckAndRemRetransTable(exchangeContext, AckMsgId)) { #if !defined(NDEBUG) ChipLogError(ExchangeManager, "CHIP MsgId:%08" PRIX32 " not in RetransTable", AckMsgId); @@ -188,7 +179,7 @@ CHIP_ERROR ReliableMessageContext::HandleRcvdAck(uint32_t AckMsgId) return err; } -CHIP_ERROR ReliableMessageContext::HandleNeedsAck(uint32_t MessageId, BitFlags MsgFlags) +CHIP_ERROR ReliableMessageContext::HandleNeedsAck(ExchangeHandle exchangeContext, uint32_t MessageId, BitFlags MsgFlags) { CHIP_ERROR err = CHIP_NO_ERROR; diff --git a/src/messaging/ReliableMessageContext.h b/src/messaging/ReliableMessageContext.h index f8fe36d454e8df..4cb3196d0715d8 100644 --- a/src/messaging/ReliableMessageContext.h +++ b/src/messaging/ReliableMessageContext.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -224,10 +225,8 @@ class ReliableMessageContext BitFlags mFlags; // Internal state flags private: - void RetainContext(); - void ReleaseContext(); - CHIP_ERROR HandleRcvdAck(uint32_t AckMsgId); - CHIP_ERROR HandleNeedsAck(uint32_t MessageId, BitFlags Flags); + CHIP_ERROR HandleRcvdAck(ExchangeHandle exchangeContext, uint32_t AckMsgId); + CHIP_ERROR HandleNeedsAck(ExchangeHandle exchangeContext, uint32_t MessageId, BitFlags Flags); ExchangeContext * GetExchangeContext(); ReliableMessageMgr * GetReliableMessageMgr(); diff --git a/src/messaging/ReliableMessageMgr.cpp b/src/messaging/ReliableMessageMgr.cpp index 97457252632ef1..e3824d3943a6b7 100644 --- a/src/messaging/ReliableMessageMgr.cpp +++ b/src/messaging/ReliableMessageMgr.cpp @@ -37,7 +37,7 @@ namespace chip { namespace Messaging { -ReliableMessageMgr::RetransTableEntry::RetransTableEntry() : rc(nullptr), nextRetransTimeTick(0), sendCount(0) {} +ReliableMessageMgr::RetransTableEntry::RetransTableEntry() : nextRetransTimeTick(0), sendCount(0) {} ReliableMessageMgr::ReliableMessageMgr(BitMapObjectPool & contextPool) : mContextPool(contextPool), mSystemLayer(nullptr), mSessionMgr(nullptr), mCurrentTimerExpiry(0), @@ -86,9 +86,9 @@ void ReliableMessageMgr::TicklessDebugDumpRetransTable(const char * log) for (RetransTableEntry & entry : mRetransTable) { - if (entry.rc) + if (entry.ec.HasValue()) { - ChipLogDetail(ExchangeManager, "EC:%04" PRIX16 " MsgId:%08" PRIX32 " NextRetransTimeCtr:%04" PRIX16, entry.rc, + ChipLogDetail(ExchangeManager, "EC:%04" PRIX16 " MsgId:%08" PRIX32 " NextRetransTimeCtr:%04" PRIX16, entry.ec->GetExchangeId(), entry.msgId, entry.nextRetransTimeTick); } } @@ -106,17 +106,17 @@ void ReliableMessageMgr::ExecuteActions() ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExecuteActions"); #endif - ExecuteForAllContext([](ReliableMessageContext * rc) { - if (rc->IsAckPending()) + ExecuteForAllContext([](ReliableMessageContext & rc) { + if (rc.IsAckPending()) { - if (0 == rc->mNextAckTimeTick) + if (0 == rc.mNextAckTimeTick) { #if defined(RMP_TICKLESS_DEBUG) ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExecuteActions sending ACK"); #endif // Send the Ack in a SecureChannel::StandaloneAck message - rc->SendStandaloneAckMessage(); - rc->SetAckPending(false); + rc.SendStandaloneAckMessage(); + rc.SetAckPending(false); } } }); @@ -127,12 +127,12 @@ void ReliableMessageMgr::ExecuteActions() // has expired for (RetransTableEntry & entry : mRetransTable) { - ReliableMessageContext * rc = entry.rc; - CHIP_ERROR err = CHIP_NO_ERROR; - - if (!rc || entry.nextRetransTimeTick != 0) + if (!entry.ec.HasValue() || entry.nextRetransTimeTick != 0) continue; + CHIP_ERROR err = CHIP_NO_ERROR; + ReliableMessageContext & rc = entry.ec->GetReliableMessageContext(); + uint8_t sendCount = entry.sendCount; uint32_t msgId = entry.retainedBuf.GetMsgId(); @@ -154,7 +154,7 @@ void ReliableMessageMgr::ExecuteActions() if (err == CHIP_NO_ERROR) { // If the retransmission was successful, update the passive timer - entry.nextRetransTimeTick = static_cast(rc->GetActiveRetransmitTimeoutTick()); + entry.nextRetransTimeTick = static_cast(rc.GetActiveRetransmitTimeoutTick()); #if !defined(NDEBUG) ChipLogDetail(ExchangeManager, "Retransmit MsgId:%08" PRIX32 " Send Cnt %d", msgId, entry.sendCount); #endif @@ -191,21 +191,20 @@ void ReliableMessageMgr::ExpireTicks() deltaTicks); #endif - ExecuteForAllContext([deltaTicks](ReliableMessageContext * rc) { - if (rc->IsAckPending()) + ExecuteForAllContext([deltaTicks](ReliableMessageContext & rc) { + if (rc.IsAckPending()) { // Decrement counter of Ack timestamp by the elapsed timer ticks - TickProceed(rc->mNextAckTimeTick, deltaTicks); + TickProceed(rc.mNextAckTimeTick, deltaTicks); #if defined(RMP_TICKLESS_DEBUG) - ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExpireTicks set mNextAckTimeTick to %u", rc->mNextAckTimeTick); + ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExpireTicks set mNextAckTimeTick to %u", rc.mNextAckTimeTick); #endif } }); for (RetransTableEntry & entry : mRetransTable) { - ReliableMessageContext * rc = entry.rc; - if (rc) + if (entry.ec.HasValue()) { // Decrement Retransmit timeout by elapsed timeticks TickProceed(entry.nextRetransTimeTick, deltaTicks); @@ -213,7 +212,7 @@ void ReliableMessageMgr::ExpireTicks() ChipLogDetail(ExchangeManager, "ReliableMessageMgr::ExpireTicks set nextRetransTimeTick to %u", entry.nextRetransTimeTick); #endif - } // rc entry is allocated + } } // Re-Adjust the base time stamp to the most recent tick boundary @@ -244,30 +243,29 @@ void ReliableMessageMgr::Timeout(System::Layer * aSystemLayer, void * aAppState, manager->StartTimer(); } -CHIP_ERROR ReliableMessageMgr::AddToRetransTable(ReliableMessageContext * rc, RetransTableEntry ** rEntry) +CHIP_ERROR ReliableMessageMgr::AddToRetransTable(ExchangeHandle exchangeContext, RetransTableEntry ** rEntry) { bool added = false; CHIP_ERROR err = CHIP_NO_ERROR; - VerifyOrDie(rc != nullptr && !rc->IsOccupied()); + VerifyOrDie(!exchangeContext->GetReliableMessageContext().IsOccupied()); for (RetransTableEntry & entry : mRetransTable) { // Check the exchContext pointer for finding an empty slot in Table - if (!entry.rc) + if (!entry.ec.HasValue()) { // Expire any virtual ticks that have expired so all wakeup sources reflect the current time ExpireTicks(); - entry.rc = rc; + entry.ec = exchangeContext; entry.sendCount = 0; entry.retainedBuf = EncryptedPacketBufferHandle(); *rEntry = &entry; // Increment the reference count - rc->RetainContext(); - rc->SetOccupied(true); + exchangeContext->GetReliableMessageContext().SetOccupied(true); added = true; break; @@ -285,20 +283,20 @@ CHIP_ERROR ReliableMessageMgr::AddToRetransTable(ReliableMessageContext * rc, Re void ReliableMessageMgr::StartRetransmision(RetransTableEntry * entry) { - VerifyOrDie(entry != nullptr && entry->rc != nullptr); + VerifyOrDie(entry != nullptr && entry->ec.HasValue()); - entry->nextRetransTimeTick = static_cast(entry->rc->GetInitialRetransmitTimeoutTick() + + entry->nextRetransTimeTick = static_cast(entry->ec->GetReliableMessageContext().GetInitialRetransmitTimeoutTick() + GetTickCounterFromTimeDelta(System::Timer::GetCurrentEpoch())); // Check if the timer needs to be started and start it. StartTimer(); } -void ReliableMessageMgr::PauseRetransmision(ReliableMessageContext * rc, uint32_t PauseTimeMillis) +void ReliableMessageMgr::PauseRetransmision(ExchangeHandle exchangeContext, uint32_t PauseTimeMillis) { for (RetransTableEntry & entry : mRetransTable) { - if (entry.rc == rc) + if (entry.ec == exchangeContext) { entry.nextRetransTimeTick = static_cast(entry.nextRetransTimeTick + (PauseTimeMillis >> mTimerIntervalShift)); break; @@ -306,11 +304,11 @@ void ReliableMessageMgr::PauseRetransmision(ReliableMessageContext * rc, uint32_ } } -void ReliableMessageMgr::ResumeRetransmision(ReliableMessageContext * rc) +void ReliableMessageMgr::ResumeRetransmision(ExchangeHandle exchangeContext) { for (RetransTableEntry & entry : mRetransTable) { - if (entry.rc == rc) + if (entry.ec == exchangeContext) { entry.nextRetransTimeTick = 0; break; @@ -318,11 +316,11 @@ void ReliableMessageMgr::ResumeRetransmision(ReliableMessageContext * rc) } } -bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t ackMsgId) +bool ReliableMessageMgr::CheckAndRemRetransTable(ExchangeHandle exchangeContext, uint32_t ackMsgId) { for (RetransTableEntry & entry : mRetransTable) { - if ((entry.rc == rc) && entry.retainedBuf.GetMsgId() == ackMsgId) + if ((entry.ec == exchangeContext) && entry.retainedBuf.GetMsgId() == ackMsgId) { // Clear the entry from the retransmision table. ClearRetransTable(entry); @@ -340,22 +338,21 @@ bool ReliableMessageMgr::CheckAndRemRetransTable(ReliableMessageContext * rc, ui CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry) { CHIP_ERROR err = CHIP_NO_ERROR; - ReliableMessageContext * rc = entry->rc; + ExchangeHandle exchangeContext = entry->ec; uint32_t msgId = 0; // Not actually used unless we reach the // line that initializes it properly. - VerifyOrReturnError(rc != nullptr, err); + VerifyOrDie(exchangeContext.HasValue()); // Now that we know this is a valid entry, grab the message id from the // retained buffer. We need to do that now, because we're about to hand it // over to someone else, and on failure it will no longer be available. msgId = entry->retainedBuf.GetMsgId(); - const ExchangeMessageDispatch * dispatcher = rc->GetExchangeContext()->GetMessageDispatch(); + const ExchangeMessageDispatch * dispatcher = exchangeContext->GetMessageDispatch(); VerifyOrExit(dispatcher != nullptr, err = CHIP_ERROR_INCORRECT_STATE); - err = - dispatcher->ResendMessage(rc->GetExchangeContext()->GetSecureSession(), std::move(entry->retainedBuf), &entry->retainedBuf); + err = dispatcher->ResendMessage(exchangeContext->GetSecureSession(), std::move(entry->retainedBuf), &entry->retainedBuf); SuccessOrExit(err); // Update the counters @@ -373,11 +370,11 @@ CHIP_ERROR ReliableMessageMgr::SendFromRetransTable(RetransTableEntry * entry) return err; } -void ReliableMessageMgr::ClearRetransTable(ReliableMessageContext * rc) +void ReliableMessageMgr::ClearRetransTable(ExchangeHandle exchangeContext) { for (RetransTableEntry & entry : mRetransTable) { - if (entry.rc == rc) + if (entry.ec == exchangeContext) { // Clear the retransmit table entry. ClearRetransTable(entry); @@ -387,16 +384,16 @@ void ReliableMessageMgr::ClearRetransTable(ReliableMessageContext * rc) void ReliableMessageMgr::ClearRetransTable(RetransTableEntry & rEntry) { - if (rEntry.rc) + if (rEntry.ec.HasValue()) { - VerifyOrDie(rEntry.rc->IsOccupied() == true); + ReliableMessageContext & rc = rEntry.ec->GetReliableMessageContext(); + VerifyOrDie(rc.IsOccupied() == true); // Expire any virtual ticks that have expired so all wakeup sources reflect the current time ExpireTicks(); - rEntry.rc->ReleaseContext(); - rEntry.rc->SetOccupied(false); - rEntry.rc = nullptr; + rc.SetOccupied(false); + rEntry.ec.Release(); // Clear all other fields rEntry = RetransTableEntry(); @@ -407,18 +404,6 @@ void ReliableMessageMgr::ClearRetransTable(RetransTableEntry & rEntry) } } -void ReliableMessageMgr::FailRetransTableEntries(ReliableMessageContext * rc, CHIP_ERROR err) -{ - for (RetransTableEntry & entry : mRetransTable) - { - if (entry.rc == rc) - { - // Remove the entry from the retransmission table. - ClearRetransTable(entry); - } - } -} - void ReliableMessageMgr::StartTimer() { CHIP_ERROR res = CHIP_NO_ERROR; @@ -427,10 +412,10 @@ void ReliableMessageMgr::StartTimer() // When do we need to next wake up to send an ACK? - ExecuteForAllContext([&nextWakeTimeTick, &foundWake](ReliableMessageContext * rc) { - if (rc->IsAckPending() && rc->mNextAckTimeTick < nextWakeTimeTick) + ExecuteForAllContext([&nextWakeTimeTick, &foundWake](ReliableMessageContext & rc) { + if (rc.IsAckPending() && rc.mNextAckTimeTick < nextWakeTimeTick) { - nextWakeTimeTick = rc->mNextAckTimeTick; + nextWakeTimeTick = rc.mNextAckTimeTick; foundWake = true; #if defined(RMP_TICKLESS_DEBUG) ChipLogDetail(ExchangeManager, "ReliableMessageMgr::StartTimer next ACK time %u", nextWakeTimeTick); @@ -440,8 +425,7 @@ void ReliableMessageMgr::StartTimer() for (RetransTableEntry & entry : mRetransTable) { - ReliableMessageContext * rc = entry.rc; - if (rc) + if (entry.ec.HasValue()) { // When do we need to next wake up for ReliableMessageProtocol retransmit? if (entry.nextRetransTimeTick < nextWakeTimeTick) @@ -510,8 +494,7 @@ int ReliableMessageMgr::TestGetCountRetransTable() for (RetransTableEntry & entry : mRetransTable) { - ReliableMessageContext * rc = entry.rc; - if (rc) + if (entry.ec.HasValue()) count++; } diff --git a/src/messaging/ReliableMessageMgr.h b/src/messaging/ReliableMessageMgr.h index 29dde3dd2a1076..5508ee91d25c6c 100644 --- a/src/messaging/ReliableMessageMgr.h +++ b/src/messaging/ReliableMessageMgr.h @@ -60,7 +60,7 @@ class ReliableMessageMgr { RetransTableEntry(); - ReliableMessageContext * rc; /**< The context for the stored CHIP message. */ + ExchangeHandle ec; /**< The context for the stored CHIP message. */ EncryptedPacketBufferHandle retainedBuf; /**< The packet buffer holding the CHIP message. */ uint16_t nextRetransTimeTick; /**< A counter representing the next retransmission time for the message. */ uint8_t sendCount; /**< A counter representing the number of times the message has been sent. */ @@ -115,7 +115,7 @@ class ReliableMessageMgr * @retval #CHIP_ERROR_RETRANS_TABLE_FULL If there is no empty slot left in the table for addition. * @retval #CHIP_NO_ERROR On success. */ - CHIP_ERROR AddToRetransTable(ReliableMessageContext * rc, RetransTableEntry ** rEntry); + CHIP_ERROR AddToRetransTable(ExchangeHandle exchangeContext, RetransTableEntry ** rEntry); /** * Start retranmisttion of cached encryped packet for current entry. @@ -135,7 +135,7 @@ class ReliableMessageMgr * * @retval #CHIP_NO_ERROR On success. */ - void PauseRetransmision(ReliableMessageContext * rc, uint32_t PauseTimeMillis); + void PauseRetransmision(ExchangeHandle exchangeContext, uint32_t PauseTimeMillis); /** * Re-start retranmisttion of cached encryped packets for the given ReliableMessageContext. @@ -144,7 +144,7 @@ class ReliableMessageMgr * * @retval #CHIP_NO_ERROR On success. */ - void ResumeRetransmision(ReliableMessageContext * rc); + void ResumeRetransmision(ExchangeHandle exchangeContext); /** * Iterate through active exchange contexts and retrans table entries. Clear the entry matching @@ -156,7 +156,7 @@ class ReliableMessageMgr * * @retval #CHIP_NO_ERROR On success. */ - bool CheckAndRemRetransTable(ReliableMessageContext * rc, uint32_t msgId); + bool CheckAndRemRetransTable(ExchangeHandle exchangeContext, uint32_t msgId); /** * Send the specified entry from the retransmission table. @@ -173,7 +173,7 @@ class ReliableMessageMgr * @param[in] rc A pointer to the ExchangeContext object. * */ - void ClearRetransTable(ReliableMessageContext * rc); + void ClearRetransTable(ExchangeHandle exchangeContext); /** * Clear an entry in the retransmission table. @@ -183,16 +183,6 @@ class ReliableMessageMgr */ void ClearRetransTable(RetransTableEntry & rEntry); - /** - * Fail entries matching a specified ExchangeContext. - * - * @param[in] rc A pointer to the ExchangeContext object. - * - * @param[in] err The error for failing table entries. - * - */ - void FailRetransTableEntries(ReliableMessageContext * rc, CHIP_ERROR err); - /** * Iterate through active exchange contexts and retrans table entries. * Determine how many ReliableMessageProtocol ticks we need to sleep before we diff --git a/src/messaging/tests/MessagingContext.cpp b/src/messaging/tests/MessagingContext.cpp index fdd44dfdf0225c..1377fa8fb0beb1 100644 --- a/src/messaging/tests/MessagingContext.cpp +++ b/src/messaging/tests/MessagingContext.cpp @@ -67,13 +67,13 @@ SecureSessionHandle MessagingContext::GetSessionPeerToLocal() return { GetSourceNodeId(), GetLocalKeyId(), GetAdminId() }; } -Messaging::ExchangeContext * MessagingContext::NewExchangeToPeer(Messaging::ExchangeDelegate * delegate) +Messaging::ExchangeHandle MessagingContext::NewExchangeToPeer(Messaging::ExchangeDelegate * delegate) { // TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602 return mExchangeManager.NewContext(GetSessionLocalToPeer(), delegate); } -Messaging::ExchangeContext * MessagingContext::NewExchangeToLocal(Messaging::ExchangeDelegate * delegate) +Messaging::ExchangeHandle MessagingContext::NewExchangeToLocal(Messaging::ExchangeDelegate * delegate) { // TODO: temprary create a SecureSessionHandle from node id, will be fix in PR 3602 return mExchangeManager.NewContext(GetSessionPeerToLocal(), delegate); diff --git a/src/messaging/tests/MessagingContext.h b/src/messaging/tests/MessagingContext.h index 79a5286ae17f1c..8a735a71da6b43 100644 --- a/src/messaging/tests/MessagingContext.h +++ b/src/messaging/tests/MessagingContext.h @@ -79,8 +79,8 @@ class MessagingContext : public IOContext SecureSessionHandle GetSessionLocalToPeer(); SecureSessionHandle GetSessionPeerToLocal(); - Messaging::ExchangeContext * NewExchangeToPeer(Messaging::ExchangeDelegate * delegate); - Messaging::ExchangeContext * NewExchangeToLocal(Messaging::ExchangeDelegate * delegate); + Messaging::ExchangeHandle NewExchangeToPeer(Messaging::ExchangeDelegate * delegate); + Messaging::ExchangeHandle NewExchangeToLocal(Messaging::ExchangeDelegate * delegate); Credentials::OperationalCredentialSet & GetOperationalCredentialSet() { return mOperationalCredentialSet; } diff --git a/src/messaging/tests/TestExchangeMgr.cpp b/src/messaging/tests/TestExchangeMgr.cpp index 7c491c524f3f11..d35006a144cd43 100644 --- a/src/messaging/tests/TestExchangeMgr.cpp +++ b/src/messaging/tests/TestExchangeMgr.cpp @@ -77,14 +77,14 @@ TransportMgr gTransportMgr; class MockAppDelegate : public ExchangeDelegate { public: - void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && buffer) override { IsOnMessageReceivedCalled = true; ec->Close(); } - void OnResponseTimeout(ExchangeContext * ec) override {} + void OnResponseTimeout(ExchangeHandle ec) override {} bool IsOnMessageReceivedCalled = false; }; @@ -94,8 +94,8 @@ void CheckNewContextTest(nlTestSuite * inSuite, void * inContext) TestContext & ctx = *reinterpret_cast(inContext); MockAppDelegate mockAppDelegate; - ExchangeContext * ec1 = ctx.NewExchangeToLocal(&mockAppDelegate); - NL_TEST_ASSERT(inSuite, ec1 != nullptr); + ExchangeHandle ec1 = ctx.NewExchangeToLocal(&mockAppDelegate); + NL_TEST_ASSERT(inSuite, ec1.HasValue()); NL_TEST_ASSERT(inSuite, ec1->IsInitiator() == true); NL_TEST_ASSERT(inSuite, ec1->GetExchangeId() != 0); auto sessionPeerToLocal = ctx.GetSecureSessionManager().GetPeerConnectionState(ec1->GetSecureSession()); @@ -103,8 +103,8 @@ void CheckNewContextTest(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, sessionPeerToLocal->GetPeerKeyID() == ctx.GetLocalKeyId()); NL_TEST_ASSERT(inSuite, ec1->GetDelegate() == &mockAppDelegate); - ExchangeContext * ec2 = ctx.NewExchangeToPeer(&mockAppDelegate); - NL_TEST_ASSERT(inSuite, ec2 != nullptr); + ExchangeHandle ec2 = ctx.NewExchangeToPeer(&mockAppDelegate); + NL_TEST_ASSERT(inSuite, ec2.HasValue()); NL_TEST_ASSERT(inSuite, ec2->GetExchangeId() > ec1->GetExchangeId()); auto sessionLocalToPeer = ctx.GetSecureSessionManager().GetPeerConnectionState(ec2->GetSecureSession()); NL_TEST_ASSERT(inSuite, sessionLocalToPeer->GetPeerNodeId() == ctx.GetDestinationNodeId()); @@ -148,7 +148,7 @@ void CheckExchangeMessages(nlTestSuite * inSuite, void * inContext) // create solicited exchange MockAppDelegate mockSolicitedAppDelegate; - ExchangeContext * ec1 = ctx.NewExchangeToPeer(&mockSolicitedAppDelegate); + ExchangeHandle ec1 = ctx.NewExchangeToPeer(&mockSolicitedAppDelegate); // create unsolicited exchange MockAppDelegate mockUnsolicitedAppDelegate; diff --git a/src/messaging/tests/TestReliableMessageProtocol.cpp b/src/messaging/tests/TestReliableMessageProtocol.cpp index 818335e3c46fa3..42d7f660fd28d4 100644 --- a/src/messaging/tests/TestReliableMessageProtocol.cpp +++ b/src/messaging/tests/TestReliableMessageProtocol.cpp @@ -91,13 +91,13 @@ OutgoingTransport gLoopback; class MockAppDelegate : public ExchangeDelegate { public: - void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && buffer) override { IsOnMessageReceivedCalled = true; } - void OnResponseTimeout(ExchangeContext * ec) override {} + void OnResponseTimeout(ExchangeHandle ec) override {} bool IsOnMessageReceivedCalled = false; }; @@ -119,17 +119,15 @@ void CheckAddClearRetrans(nlTestSuite * inSuite, void * inContext) TestContext & ctx = *reinterpret_cast(inContext); MockAppDelegate mockAppDelegate; - ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockAppDelegate); - NL_TEST_ASSERT(inSuite, exchange != nullptr); + ExchangeHandle exchange = ctx.NewExchangeToPeer(&mockAppDelegate); + NL_TEST_ASSERT(inSuite, exchange.HasValue()); ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr(); - ReliableMessageContext * rc = exchange->GetReliableMessageContext(); NL_TEST_ASSERT(inSuite, rm != nullptr); - NL_TEST_ASSERT(inSuite, rc != nullptr); ReliableMessageMgr::RetransTableEntry * entry; - rm->AddToRetransTable(rc, &entry); + rm->AddToRetransTable(exchange, &entry); NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 1); rm->ClearRetransTable(*entry); NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); @@ -144,7 +142,7 @@ void CheckFailRetrans(nlTestSuite * inSuite, void * inContext) ctx.GetInetLayer().SystemLayer()->Init(nullptr); MockAppDelegate mockAppDelegate; - ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockAppDelegate); + ExchangeHandle exchange = ctx.NewExchangeToPeer(&mockAppDelegate); NL_TEST_ASSERT(inSuite, exchange != nullptr); ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr(); @@ -174,15 +172,14 @@ void CheckResendApplicationMessage(nlTestSuite * inSuite, void * inContext) MockAppDelegate mockSender; // TODO: temporarily create a SecureSessionHandle from node id, will be fix in PR 3602 - ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockSender); - NL_TEST_ASSERT(inSuite, exchange != nullptr); + ExchangeHandle exchange = ctx.NewExchangeToPeer(&mockSender); + NL_TEST_ASSERT(inSuite, exchange.HasValue()); ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr(); - ReliableMessageContext * rc = exchange->GetReliableMessageContext(); + ReliableMessageContext & rc = exchange->GetReliableMessageContext(); NL_TEST_ASSERT(inSuite, rm != nullptr); - NL_TEST_ASSERT(inSuite, rc != nullptr); - rc->SetConfig({ + rc.SetConfig({ 1, // CHIP_CONFIG_RMP_DEFAULT_INITIAL_RETRY_INTERVAL 1, // CHIP_CONFIG_RMP_DEFAULT_ACTIVE_RETRY_INTERVAL }); @@ -222,8 +219,7 @@ void CheckResendApplicationMessage(nlTestSuite * inSuite, void * inContext) NL_TEST_ASSERT(inSuite, gLoopback.mDroppedMessageCount == 2); NL_TEST_ASSERT(inSuite, rm->TestGetCountRetransTable() == 0); - rm->ClearRetransTable(rc); - exchange->Close(); + rm->ClearRetransTable(exchange); } void CheckCloseExchangeAndResendApplicationMessage(nlTestSuite * inSuite, void * inContext) @@ -298,15 +294,14 @@ void CheckSendStandaloneAckMessage(nlTestSuite * inSuite, void * inContext) ctx.GetInetLayer().SystemLayer()->Init(nullptr); MockAppDelegate mockAppDelegate; - ExchangeContext * exchange = ctx.NewExchangeToPeer(&mockAppDelegate); - NL_TEST_ASSERT(inSuite, exchange != nullptr); + ExchangeHandle exchange = ctx.NewExchangeToPeer(&mockAppDelegate); + NL_TEST_ASSERT(inSuite, exchange.HasValue()); ReliableMessageMgr * rm = ctx.GetExchangeManager().GetReliableMessageMgr(); - ReliableMessageContext * rc = exchange->GetReliableMessageContext(); + ReliableMessageContext & rc = exchange->GetReliableMessageContext(); NL_TEST_ASSERT(inSuite, rm != nullptr); - NL_TEST_ASSERT(inSuite, rc != nullptr); - NL_TEST_ASSERT(inSuite, rc->SendStandaloneAckMessage() == CHIP_NO_ERROR); + NL_TEST_ASSERT(inSuite, rc.SendStandaloneAckMessage() == CHIP_NO_ERROR); exchange->Close(); } diff --git a/src/messaging/tests/echo/echo_requester.cpp b/src/messaging/tests/echo/echo_requester.cpp index 2e100386b97d3b..e98691e9774232 100644 --- a/src/messaging/tests/echo/echo_requester.cpp +++ b/src/messaging/tests/echo/echo_requester.cpp @@ -154,7 +154,7 @@ CHIP_ERROR EstablishSecureSession() return err; } -void HandleEchoResponseReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload) +void HandleEchoResponseReceived(chip::Messaging::ExchangeHandle ec, chip::System::PacketBufferHandle && payload) { uint32_t respTime = chip::System::Timer::GetCurrentEpoch(); uint32_t transitTime = respTime - gLastEchoTime; diff --git a/src/messaging/tests/echo/echo_responder.cpp b/src/messaging/tests/echo/echo_responder.cpp index 32dff139291017..795b410b3101f0 100644 --- a/src/messaging/tests/echo/echo_responder.cpp +++ b/src/messaging/tests/echo/echo_responder.cpp @@ -49,7 +49,7 @@ chip::SecureSessionMgr gSessionManager; chip::SecurePairingUsingTestSecret gTestPairing; // Callback handler when a CHIP EchoRequest is received. -void HandleEchoRequestReceived(chip::Messaging::ExchangeContext * ec, chip::System::PacketBufferHandle && payload) +void HandleEchoRequestReceived(chip::Messaging::ExchangeHandle ec, chip::System::PacketBufferHandle && payload) { printf("Echo Request, len=%u ... sending response.\n", payload->DataLength()); } diff --git a/src/protocols/echo/Echo.h b/src/protocols/echo/Echo.h index 9a095aaf374325..558c2353ab39ab 100644 --- a/src/protocols/echo/Echo.h +++ b/src/protocols/echo/Echo.h @@ -47,7 +47,7 @@ enum class MsgType : uint8_t EchoResponse = 0x02 }; -using EchoFunct = void (*)(Messaging::ExchangeContext * ec, System::PacketBufferHandle && payload); +using EchoFunct = void (*)(Messaging::ExchangeHandle ec, System::PacketBufferHandle && payload); class DLL_EXPORT EchoClient : public Messaging::ExchangeDelegate { @@ -99,13 +99,13 @@ class DLL_EXPORT EchoClient : public Messaging::ExchangeDelegate private: Messaging::ExchangeManager * mExchangeMgr = nullptr; - Messaging::ExchangeContext * mExchangeCtx = nullptr; + Messaging::ExchangeHandle mExchangeCtx; EchoFunct OnEchoResponseReceived = nullptr; SecureSessionHandle mSecureSession; - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; - void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + void OnResponseTimeout(Messaging::ExchangeHandle ec) override; }; class DLL_EXPORT EchoServer : public Messaging::ExchangeDelegate @@ -145,9 +145,9 @@ class DLL_EXPORT EchoServer : public Messaging::ExchangeDelegate Messaging::ExchangeManager * mExchangeMgr = nullptr; EchoFunct OnEchoRequestReceived = nullptr; - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; - void OnResponseTimeout(Messaging::ExchangeContext * ec) override {} + void OnResponseTimeout(Messaging::ExchangeHandle ec) override {} }; } // namespace Echo diff --git a/src/protocols/echo/EchoClient.cpp b/src/protocols/echo/EchoClient.cpp index aae35841ee9431..8bf8a14cee0bbb 100644 --- a/src/protocols/echo/EchoClient.cpp +++ b/src/protocols/echo/EchoClient.cpp @@ -38,17 +38,16 @@ CHIP_ERROR EchoClient::Init(Messaging::ExchangeManager * exchangeMgr, SecureSess mExchangeMgr = exchangeMgr; mSecureSession = session; OnEchoResponseReceived = nullptr; - mExchangeCtx = nullptr; return CHIP_NO_ERROR; } void EchoClient::Shutdown() { - if (mExchangeCtx != nullptr) + if (mExchangeCtx.HasValue()) { mExchangeCtx->Abort(); - mExchangeCtx = nullptr; + mExchangeCtx.Release(); } OnEchoResponseReceived = nullptr; @@ -61,15 +60,15 @@ CHIP_ERROR EchoClient::SendEchoRequest(System::PacketBufferHandle && payload, co // Discard any existing exchange context. Effectively we can only have one Echo exchange with // a single node at any one time. - if (mExchangeCtx != nullptr) + if (mExchangeCtx.HasValue()) { mExchangeCtx->Abort(); - mExchangeCtx = nullptr; + mExchangeCtx.Release(); } // Create a new exchange context. mExchangeCtx = mExchangeMgr->NewContext(mSecureSession, this); - if (mExchangeCtx == nullptr) + if (!mExchangeCtx.HasValue()) { return CHIP_ERROR_NO_MEMORY; } @@ -80,13 +79,13 @@ CHIP_ERROR EchoClient::SendEchoRequest(System::PacketBufferHandle && payload, co if (err != CHIP_NO_ERROR) { mExchangeCtx->Abort(); - mExchangeCtx = nullptr; + mExchangeCtx.Release(); } return err; } -void EchoClient::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, +void EchoClient::OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { // Assert that the exchange context matches the client's current context. @@ -99,8 +98,8 @@ void EchoClient::OnMessageReceived(Messaging::ExchangeContext * ec, const Packet // If not, close the exchange and free the payload. if (!payloadHeader.HasMessageType(MsgType::EchoResponse)) { - ec->Close(); - mExchangeCtx = nullptr; + ec.Release(); + mExchangeCtx.Release(); return; } @@ -109,7 +108,7 @@ void EchoClient::OnMessageReceived(Messaging::ExchangeContext * ec, const Packet // because we no longer care whether the echo request message has been // acknowledged at the transport layer. mExchangeCtx->Abort(); - mExchangeCtx = nullptr; + mExchangeCtx.Release(); // Call the registered OnEchoResponseReceived handler, if any. if (OnEchoResponseReceived != nullptr) @@ -118,9 +117,9 @@ void EchoClient::OnMessageReceived(Messaging::ExchangeContext * ec, const Packet } } -void EchoClient::OnResponseTimeout(Messaging::ExchangeContext * ec) +void EchoClient::OnResponseTimeout(Messaging::ExchangeHandle ec) { - ChipLogProgress(Echo, "Time out! failed to receive echo response from Exchange: %p", ec); + ChipLogProgress(Echo, "Time out! failed to receive echo response from Exchange: %d", ec->GetExchangeId()); } } // namespace Echo diff --git a/src/protocols/echo/EchoServer.cpp b/src/protocols/echo/EchoServer.cpp index 5f3a6ec525dc9d..33887611818ac3 100644 --- a/src/protocols/echo/EchoServer.cpp +++ b/src/protocols/echo/EchoServer.cpp @@ -53,7 +53,7 @@ void EchoServer::Shutdown() } } -void EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, +void EchoServer::OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { System::PacketBufferHandle response; @@ -84,7 +84,7 @@ void EchoServer::OnMessageReceived(Messaging::ExchangeContext * ec, const Packet ec->SendMessage(MsgType::EchoResponse, std::move(response)); // Discard the exchange context. - ec->Close(); + ec.Release(); } } // namespace Echo diff --git a/src/protocols/secure_channel/CASEServer.cpp b/src/protocols/secure_channel/CASEServer.cpp index 25adca9cbf1794..9156e114390cb7 100644 --- a/src/protocols/secure_channel/CASEServer.cpp +++ b/src/protocols/secure_channel/CASEServer.cpp @@ -49,9 +49,9 @@ CHIP_ERROR CASEServer::ListenForSessionEstablishment(Messaging::ExchangeManager return CHIP_NO_ERROR; } -CHIP_ERROR CASEServer::InitCASEHandshake(Messaging::ExchangeContext * ec) +CHIP_ERROR CASEServer::InitCASEHandshake(Messaging::ExchangeHandle ec) { - ReturnErrorCodeIf(ec == nullptr, CHIP_ERROR_INVALID_ARGUMENT); + ReturnErrorCodeIf(!ec.HasValue(), CHIP_ERROR_INVALID_ARGUMENT); // TODO - Use PK of the root CA for the initiator to figure out the admin. mAdminId = ec->GetSecureSession().GetAdminId(); @@ -76,10 +76,10 @@ CHIP_ERROR CASEServer::InitCASEHandshake(Messaging::ExchangeContext * ec) return CHIP_NO_ERROR; } -void CASEServer::OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, +void CASEServer::OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) { - ChipLogProgress(Inet, "CASE Server received SigmaR1 message. Starting handshake. EC %p", ec); + ChipLogProgress(Inet, "CASE Server received SigmaR1 message. Starting handshake. EC %d", ec->GetExchangeId()); ReturnOnFailure(InitCASEHandshake(ec)); mPairingSession.OnMessageReceived(ec, packetHeader, payloadHeader, std::move(payload)); diff --git a/src/protocols/secure_channel/CASEServer.h b/src/protocols/secure_channel/CASEServer.h index 5a976b79558525..d782e096b52618 100644 --- a/src/protocols/secure_channel/CASEServer.h +++ b/src/protocols/secure_channel/CASEServer.h @@ -45,9 +45,9 @@ class CASEServer : public SessionEstablishmentDelegate, public Messaging::Exchan void OnSessionEstablished() override; //// ExchangeDelegate Implementation //// - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; - void OnResponseTimeout(Messaging::ExchangeContext * ec) override {} + void OnResponseTimeout(Messaging::ExchangeHandle ec) override {} Messaging::ExchangeMessageDispatch * GetMessageDispatch(Messaging::ReliableMessageMgr * reliableMessageManager, SecureSessionMgr * sessionMgr) override { @@ -68,7 +68,7 @@ class CASEServer : public SessionEstablishmentDelegate, public Messaging::Exchan Credentials::OperationalCredentialSet mCredentials; Credentials::CertificateKeyId mRootKeyId; - CHIP_ERROR InitCASEHandshake(Messaging::ExchangeContext * ec); + CHIP_ERROR InitCASEHandshake(Messaging::ExchangeHandle ec); void Cleanup(); }; diff --git a/src/protocols/secure_channel/CASESession.cpp b/src/protocols/secure_channel/CASESession.cpp index fa0ff9b0333364..54f90e2b5814c2 100644 --- a/src/protocols/secure_channel/CASESession.cpp +++ b/src/protocols/secure_channel/CASESession.cpp @@ -103,10 +103,10 @@ void CASESession::Clear() mTrustedRootId.mId = nullptr; } - if (mExchangeCtxt != nullptr) + if (mExchangeCtxt.HasValue()) { mExchangeCtxt->Close(); - mExchangeCtxt = nullptr; + mExchangeCtxt.Release(); } } @@ -227,11 +227,11 @@ CASESession::ListenForSessionEstablishment(OperationalCredentialSet * operationa CHIP_ERROR CASESession::EstablishSession(const Transport::PeerAddress peerAddress, OperationalCredentialSet * operationalCredentialSet, NodeId peerNodeId, uint16_t myKeyId, - ExchangeContext * exchangeCtxt, SessionEstablishmentDelegate * delegate) + ExchangeHandle exchangeCtxt, SessionEstablishmentDelegate * delegate) { CHIP_ERROR err = CHIP_NO_ERROR; - ReturnErrorCodeIf(exchangeCtxt == nullptr, CHIP_ERROR_INVALID_ARGUMENT); + ReturnErrorCodeIf(!exchangeCtxt.HasValue(), CHIP_ERROR_INVALID_ARGUMENT); err = Init(operationalCredentialSet, myKeyId, delegate); @@ -255,9 +255,9 @@ CHIP_ERROR CASESession::EstablishSession(const Transport::PeerAddress peerAddres return err; } -void CASESession::OnResponseTimeout(ExchangeContext * ec) +void CASESession::OnResponseTimeout(ExchangeHandle ec) { - VerifyOrReturn(ec != nullptr, ChipLogError(Inet, "CASESession::OnResponseTimeout was called by null exchange")); + VerifyOrReturn(ec.HasValue(), ChipLogError(Inet, "CASESession::OnResponseTimeout was called by null exchange")); VerifyOrReturn(mExchangeCtxt == ec, ChipLogError(Inet, "CASESession::OnResponseTimeout exchange doesn't match")); ChipLogError(Inet, "CASESession timed out while waiting for a response from the peer. Expected message type was %d", mNextExpectedMsg); @@ -1109,15 +1109,15 @@ void CASESession::HandleErrorMsg(const System::PacketBufferHandle & msg) Clear(); } -CHIP_ERROR CASESession::ValidateReceivedMessage(ExchangeContext * ec, const PacketHeader & packetHeader, +CHIP_ERROR CASESession::ValidateReceivedMessage(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle & msg) { - VerifyOrReturnError(ec != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(ec.HasValue(), CHIP_ERROR_INVALID_ARGUMENT); // mExchangeCtxt can be nullptr if this is the first message (CASE_SigmaR1) received by CASESession // via UnsolicitedMessageHandler. The exchange context is allocated by exchange manager and provided // to the handler (CASESession object). - if (mExchangeCtxt != nullptr) + if (mExchangeCtxt.HasValue()) { if (mExchangeCtxt != ec) { @@ -1153,7 +1153,7 @@ CHIP_ERROR CASESession::ValidateReceivedMessage(ExchangeContext * ec, const Pack return CHIP_NO_ERROR; } -void CASESession::OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, +void CASESession::OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msg) { CHIP_ERROR err = ValidateReceivedMessage(ec, packetHeader, payloadHeader, msg); diff --git a/src/protocols/secure_channel/CASESession.h b/src/protocols/secure_channel/CASESession.h index 47afdddb0e49dd..f623b9ca90ef56 100644 --- a/src/protocols/secure_channel/CASESession.h +++ b/src/protocols/secure_channel/CASESession.h @@ -111,7 +111,7 @@ class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public Pairin * @return CHIP_ERROR The result of initialization */ CHIP_ERROR EstablishSession(const Transport::PeerAddress peerAddress, OperationalCredentialSet * operationalCredentialSet, - NodeId peerNodeId, uint16_t myKeyId, Messaging::ExchangeContext * exchangeCtxt, + NodeId peerNodeId, uint16_t myKeyId, Messaging::ExchangeHandle exchangeCtxt, SessionEstablishmentDelegate * delegate); /** @@ -179,9 +179,9 @@ class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public Pairin SessionEstablishmentExchangeDispatch & MessageDispatch() { return mMessageDispatch; } //// ExchangeDelegate Implementation //// - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; - void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + void OnResponseTimeout(Messaging::ExchangeHandle ec) override; Messaging::ExchangeMessageDispatch * GetMessageDispatch(Messaging::ReliableMessageMgr * rmMgr, SecureSessionMgr * sessionMgr) override { @@ -231,7 +231,7 @@ class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public Pairin void Clear(); - CHIP_ERROR ValidateReceivedMessage(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, + CHIP_ERROR ValidateReceivedMessage(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle & msg); SessionEstablishmentDelegate * mDelegate = nullptr; @@ -250,7 +250,7 @@ class DLL_EXPORT CASESession : public Messaging::ExchangeDelegate, public Pairin uint8_t mIPK[kIPKSize]; uint8_t mRemoteIPK[kIPKSize]; - Messaging::ExchangeContext * mExchangeCtxt = nullptr; + Messaging::ExchangeHandle mExchangeCtxt; SessionEstablishmentExchangeDispatch mMessageDispatch; struct SigmaErrorMsg diff --git a/src/protocols/secure_channel/MessageCounterManager.cpp b/src/protocols/secure_channel/MessageCounterManager.cpp index eff8b77b770095..281726b7ad7ab7 100644 --- a/src/protocols/secure_channel/MessageCounterManager.cpp +++ b/src/protocols/secure_channel/MessageCounterManager.cpp @@ -85,7 +85,7 @@ CHIP_ERROR MessageCounterManager::QueueReceivedMessageAndStartSync(const PacketH return CHIP_NO_ERROR; } -void MessageCounterManager::OnMessageReceived(Messaging::ExchangeContext * exchangeContext, const PacketHeader & packetHeader, +void MessageCounterManager::OnMessageReceived(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf) { if (payloadHeader.HasMessageType(Protocols::SecureChannel::MsgType::MsgCounterSyncReq)) @@ -98,7 +98,7 @@ void MessageCounterManager::OnMessageReceived(Messaging::ExchangeContext * excha } } -void MessageCounterManager::OnResponseTimeout(Messaging::ExchangeContext * exchangeContext) +void MessageCounterManager::OnResponseTimeout(Messaging::ExchangeHandle exchangeContext) { Transport::PeerConnectionState * state = mExchangeMgr->GetSessionMgr()->GetPeerConnectionState(exchangeContext->GetSecureSession()); @@ -178,12 +178,12 @@ CHIP_ERROR MessageCounterManager::SendMsgCounterSyncReq(SecureSessionHandle sess { CHIP_ERROR err = CHIP_NO_ERROR; - Messaging::ExchangeContext * exchangeContext = nullptr; + Messaging::ExchangeHandle exchangeContext; System::PacketBufferHandle msgBuf; Messaging::SendFlags sendFlags; exchangeContext = mExchangeMgr->NewContext(session, this); - VerifyOrExit(exchangeContext != nullptr, err = CHIP_ERROR_NO_MEMORY); + VerifyOrExit(exchangeContext.HasValue(), err = CHIP_ERROR_NO_MEMORY); msgBuf = MessagePacketBuffer::New(kChallengeSize); VerifyOrExit(!msgBuf.IsNull(), err = CHIP_ERROR_NO_MEMORY); @@ -215,7 +215,7 @@ CHIP_ERROR MessageCounterManager::SendMsgCounterSyncReq(SecureSessionHandle sess return err; } -CHIP_ERROR MessageCounterManager::SendMsgCounterSyncResp(Messaging::ExchangeContext * exchangeContext, +CHIP_ERROR MessageCounterManager::SendMsgCounterSyncResp(Messaging::ExchangeHandle exchangeContext, FixedByteSpan challenge) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -253,7 +253,7 @@ CHIP_ERROR MessageCounterManager::SendMsgCounterSyncResp(Messaging::ExchangeCont return err; } -void MessageCounterManager::HandleMsgCounterSyncReq(Messaging::ExchangeContext * exchangeContext, const PacketHeader & packetHeader, +void MessageCounterManager::HandleMsgCounterSyncReq(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, System::PacketBufferHandle && msgBuf) { CHIP_ERROR err = CHIP_NO_ERROR; @@ -280,7 +280,7 @@ void MessageCounterManager::HandleMsgCounterSyncReq(Messaging::ExchangeContext * return; } -void MessageCounterManager::HandleMsgCounterSyncResp(Messaging::ExchangeContext * exchangeContext, +void MessageCounterManager::HandleMsgCounterSyncResp(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, System::PacketBufferHandle && msgBuf) { CHIP_ERROR err = CHIP_NO_ERROR; diff --git a/src/protocols/secure_channel/MessageCounterManager.h b/src/protocols/secure_channel/MessageCounterManager.h index 42672564983a83..6c88375215c698 100644 --- a/src/protocols/secure_channel/MessageCounterManager.h +++ b/src/protocols/secure_channel/MessageCounterManager.h @@ -101,18 +101,18 @@ class MessageCounterManager : public Messaging::ExchangeDelegate, public Transpo void ProcessPendingMessages(NodeId peerNodeId); - CHIP_ERROR SendMsgCounterSyncResp(Messaging::ExchangeContext * exchangeContext, FixedByteSpan challenge); + CHIP_ERROR SendMsgCounterSyncResp(Messaging::ExchangeHandle exchangeContext, FixedByteSpan challenge); - void HandleMsgCounterSyncReq(Messaging::ExchangeContext * exchangeContext, const PacketHeader & packetHeader, + void HandleMsgCounterSyncReq(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, System::PacketBufferHandle && msgBuf); - void HandleMsgCounterSyncResp(Messaging::ExchangeContext * exchangeContext, const PacketHeader & packetHeader, + void HandleMsgCounterSyncResp(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, System::PacketBufferHandle && msgBuf); - void OnMessageReceived(Messaging::ExchangeContext * exchangeContext, const PacketHeader & packetHeader, + void OnMessageReceived(Messaging::ExchangeHandle exchangeContext, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; - void OnResponseTimeout(Messaging::ExchangeContext * exchangeContext) override; + void OnResponseTimeout(Messaging::ExchangeHandle exchangeContext) override; }; } // namespace secure_channel diff --git a/src/protocols/secure_channel/PASESession.cpp b/src/protocols/secure_channel/PASESession.cpp index 433e027bdeb14a..d45ec13913e281 100644 --- a/src/protocols/secure_channel/PASESession.cpp +++ b/src/protocols/secure_channel/PASESession.cpp @@ -104,10 +104,10 @@ void PASESession::Clear() void PASESession::CloseExchange() { - if (mExchangeCtxt != nullptr) + if (mExchangeCtxt.HasValue()) { mExchangeCtxt->Close(); - mExchangeCtxt = nullptr; + mExchangeCtxt.Release(); } } @@ -301,9 +301,9 @@ CHIP_ERROR PASESession::WaitForPairing(const PASEVerifier & verifier, uint16_t m } CHIP_ERROR PASESession::Pair(const Transport::PeerAddress peerAddress, uint32_t peerSetUpPINCode, uint16_t myKeyId, - Messaging::ExchangeContext * exchangeCtxt, SessionEstablishmentDelegate * delegate) + Messaging::ExchangeHandle exchangeCtxt, SessionEstablishmentDelegate * delegate) { - ReturnErrorCodeIf(exchangeCtxt == nullptr, CHIP_ERROR_INVALID_ARGUMENT); + ReturnErrorCodeIf(!exchangeCtxt.HasValue(), CHIP_ERROR_INVALID_ARGUMENT); CHIP_ERROR err = Init(myKeyId, peerSetUpPINCode, delegate); SuccessOrExit(err); @@ -323,10 +323,10 @@ CHIP_ERROR PASESession::Pair(const Transport::PeerAddress peerAddress, uint32_t return err; } -void PASESession::OnResponseTimeout(ExchangeContext * ec) +void PASESession::OnResponseTimeout(ExchangeHandle ec) { - VerifyOrReturn(ec != nullptr, ChipLogError(SecureChannel, "PASESession::OnResponseTimeout was called by null exchange")); - VerifyOrReturn(mExchangeCtxt == nullptr || mExchangeCtxt == ec, + VerifyOrReturn(ec.HasValue(), ChipLogError(SecureChannel, "PASESession::OnResponseTimeout was called by null exchange")); + VerifyOrReturn(!mExchangeCtxt.HasValue() || mExchangeCtxt == ec, ChipLogError(SecureChannel, "PASESession::OnResponseTimeout exchange doesn't match")); ChipLogError(SecureChannel, "PASESession timed out while waiting for a response from the peer. Expected message type was %d", mNextExpectedMsg); @@ -751,15 +751,15 @@ CHIP_ERROR PASESession::HandleErrorMsg(const System::PacketBufferHandle & msg) return err; } -CHIP_ERROR PASESession::ValidateReceivedMessage(ExchangeContext * exchange, const PacketHeader & packetHeader, +CHIP_ERROR PASESession::ValidateReceivedMessage(ExchangeHandle exchange, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msg) { - VerifyOrReturnError(exchange != nullptr, CHIP_ERROR_INVALID_ARGUMENT); + VerifyOrReturnError(exchange.HasValue(), CHIP_ERROR_INVALID_ARGUMENT); // mExchangeCtxt can be nullptr if this is the first message (PBKDFParamRequest) received by PASESession // via UnsolicitedMessageHandler. The exchange context is allocated by exchange manager and provided // to the handler (PASESession object). - if (mExchangeCtxt != nullptr) + if (mExchangeCtxt.HasValue()) { if (mExchangeCtxt != exchange) { @@ -782,7 +782,7 @@ CHIP_ERROR PASESession::ValidateReceivedMessage(ExchangeContext * exchange, cons return CHIP_NO_ERROR; } -void PASESession::OnMessageReceived(ExchangeContext * exchange, const PacketHeader & packetHeader, +void PASESession::OnMessageReceived(ExchangeHandle exchange, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msg) { CHIP_ERROR err = ValidateReceivedMessage(exchange, packetHeader, payloadHeader, std::move(msg)); diff --git a/src/protocols/secure_channel/PASESession.h b/src/protocols/secure_channel/PASESession.h index c9781040e52ac5..3330c4ded6de16 100644 --- a/src/protocols/secure_channel/PASESession.h +++ b/src/protocols/secure_channel/PASESession.h @@ -123,7 +123,7 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin * @return CHIP_ERROR The result of initialization */ CHIP_ERROR Pair(const Transport::PeerAddress peerAddress, uint32_t peerSetUpPINCode, uint16_t myKeyId, - Messaging::ExchangeContext * exchangeCtxt, SessionEstablishmentDelegate * delegate); + Messaging::ExchangeHandle exchangeCtxt, SessionEstablishmentDelegate * delegate); /** * @brief @@ -216,7 +216,7 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin * @param[in] payloadHeader A reference to the PayloadHeader object. * @param[in] payload A handle to the PacketBuffer object holding the message payload. */ - void OnMessageReceived(Messaging::ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(Messaging::ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && payload) override; /** @@ -226,7 +226,7 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin * * @param[in] ec A pointer to the ExchangeContext object. */ - void OnResponseTimeout(Messaging::ExchangeContext * ec) override; + void OnResponseTimeout(Messaging::ExchangeHandle ec) override; Messaging::ExchangeMessageDispatch * GetMessageDispatch(Messaging::ReliableMessageMgr * rmMgr, SecureSessionMgr * sessionMgr) override @@ -291,7 +291,7 @@ class DLL_EXPORT PASESession : public Messaging::ExchangeDelegate, public Pairin uint16_t mSaltLength = 0; uint8_t * mSalt = nullptr; - Messaging::ExchangeContext * mExchangeCtxt = nullptr; + Messaging::ExchangeHandle mExchangeCtxt; SessionEstablishmentExchangeDispatch mMessageDispatch; diff --git a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp index e0b38323325c2b..f88bf272453d75 100644 --- a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp +++ b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.cpp @@ -47,10 +47,10 @@ CHIP_ERROR SessionEstablishmentExchangeDispatch::SendMessageImpl(SecureSessionHa CHIP_ERROR SessionEstablishmentExchangeDispatch::OnMessageReceived(const PayloadHeader & payloadHeader, uint32_t messageId, const Transport::PeerAddress & peerAddress, - ReliableMessageContext * reliableMessageContext) + ExchangeHandle exchangeContext) { mPeerAddress = peerAddress; - return ExchangeMessageDispatch::OnMessageReceived(payloadHeader, messageId, peerAddress, reliableMessageContext); + return ExchangeMessageDispatch::OnMessageReceived(payloadHeader, messageId, peerAddress, exchangeContext); } bool SessionEstablishmentExchangeDispatch::MessagePermitted(uint16_t protocol, uint8_t type) diff --git a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h index b222d7b318ab7b..4ed96cb67b547a 100644 --- a/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h +++ b/src/protocols/secure_channel/SessionEstablishmentExchangeDispatch.h @@ -45,7 +45,7 @@ class SessionEstablishmentExchangeDispatch : public Messaging::ExchangeMessageDi CHIP_ERROR OnMessageReceived(const PayloadHeader & payloadHeader, uint32_t messageId, const Transport::PeerAddress & peerAddress, - Messaging::ReliableMessageContext * reliableMessageContext) override; + Messaging::ExchangeHandle exchangeContext) override; const Transport::PeerAddress & GetPeerAddress() const { return mPeerAddress; } diff --git a/src/protocols/secure_channel/tests/TestCASESession.cpp b/src/protocols/secure_channel/tests/TestCASESession.cpp index 57d5b6f41a4b48..a7a51c40d8e2b1 100644 --- a/src/protocols/secure_channel/tests/TestCASESession.cpp +++ b/src/protocols/secure_channel/tests/TestCASESession.cpp @@ -121,10 +121,10 @@ void CASE_SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) CASESession pairing; NL_TEST_ASSERT(inSuite, pairing.MessageDispatch().Init(&gTransportMgr) == CHIP_NO_ERROR); - ExchangeContext * context = ctx.NewExchangeToLocal(&pairing); + ExchangeHandle context = ctx.NewExchangeToLocal(&pairing); NL_TEST_ASSERT(inSuite, - pairing.EstablishSession(Transport::PeerAddress(Transport::Type::kBle), &commissionerDevOpCred, 2, 0, nullptr, + pairing.EstablishSession(Transport::PeerAddress(Transport::Type::kBle), &commissionerDevOpCred, 2, 0, ExchangeHandle(), nullptr) != CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, pairing.EstablishSession(Transport::PeerAddress(Transport::Type::kBle), &commissionerDevOpCred, 2, 0, context, @@ -139,7 +139,7 @@ void CASE_SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) gLoopback.mSentMessageCount = 0; gLoopback.mMessageSendError = CHIP_ERROR_BAD_REQUEST; - ExchangeContext * context1 = ctx.NewExchangeToLocal(&pairing1); + ExchangeHandle context1 = ctx.NewExchangeToLocal(&pairing1); NL_TEST_ASSERT(inSuite, pairing1.EstablishSession(Transport::PeerAddress(Transport::Type::kBle), &commissionerDevOpCred, 2, 0, context1, @@ -166,7 +166,7 @@ void CASE_SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inConte ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType( Protocols::SecureChannel::MsgType::CASE_SigmaR1, &pairingAccessory) == CHIP_NO_ERROR); - ExchangeContext * contextCommissioner = ctx.NewExchangeToLocal(&pairingCommissioner); + ExchangeHandle contextCommissioner = ctx.NewExchangeToLocal(&pairingCommissioner); NL_TEST_ASSERT(inSuite, pairingAccessory.ListenForSessionEstablishment(&accessoryDevOpCred, 0, &delegateAccessory) == CHIP_NO_ERROR); diff --git a/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp b/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp index 3010fd3a8237a1..aa16aa8567d2cd 100644 --- a/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp +++ b/src/protocols/secure_channel/tests/TestMessageCounterManager.cpp @@ -73,14 +73,14 @@ const char PAYLOAD[] = "Hello!"; class MockAppDelegate : public ExchangeDelegate { public: - void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && msgBuf) override { ++ReceiveHandlerCallCount; ec->Close(); } - void OnResponseTimeout(ExchangeContext * ec) override {} + void OnResponseTimeout(ExchangeHandle ec) override {} int ReceiveHandlerCallCount = 0; }; @@ -123,8 +123,8 @@ void CheckReceiveMessage(nlTestSuite * inSuite, void * inContext) System::PacketBufferHandle msgBuf = MessagePacketBuffer::NewWithData(PAYLOAD, payload_len); NL_TEST_ASSERT(inSuite, !msgBuf.IsNull()); - Messaging::ExchangeContext * ec = ctx.NewExchangeToPeer(nullptr); - NL_TEST_ASSERT(inSuite, ec != nullptr); + Messaging::ExchangeHandle ec = ctx.NewExchangeToPeer(nullptr); + NL_TEST_ASSERT(inSuite, ec.HasValue()); err = ec->SendMessage(chip::Protocols::Echo::MsgType::EchoRequest, std::move(msgBuf), Messaging::SendFlags{ Messaging::SendMessageFlags::kNoAutoRequestAck }); diff --git a/src/protocols/secure_channel/tests/TestPASESession.cpp b/src/protocols/secure_channel/tests/TestPASESession.cpp index e58b57ad24a958..41a13ecaa30221 100644 --- a/src/protocols/secure_channel/tests/TestPASESession.cpp +++ b/src/protocols/secure_channel/tests/TestPASESession.cpp @@ -76,13 +76,13 @@ class TestSecurePairingDelegate : public SessionEstablishmentDelegate class MockAppDelegate : public ExchangeDelegate { public: - void OnMessageReceived(ExchangeContext * ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, + void OnMessageReceived(ExchangeHandle ec, const PacketHeader & packetHeader, const PayloadHeader & payloadHeader, System::PacketBufferHandle && buffer) override { ec->Close(); } - void OnResponseTimeout(ExchangeContext * ec) override {} + void OnResponseTimeout(ExchangeHandle ec) override {} }; void SecurePairingWaitTest(nlTestSuite * inSuite, void * inContext) @@ -107,10 +107,10 @@ void SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) PASESession pairing; NL_TEST_ASSERT(inSuite, pairing.MessageDispatch().Init(&gTransportMgr) == CHIP_NO_ERROR); - ExchangeContext * context = ctx.NewExchangeToLocal(&pairing); + ExchangeHandle context = ctx.NewExchangeToLocal(&pairing); NL_TEST_ASSERT(inSuite, - pairing.Pair(Transport::PeerAddress(Transport::Type::kBle), 1234, 0, nullptr, nullptr) != CHIP_NO_ERROR); + pairing.Pair(Transport::PeerAddress(Transport::Type::kBle), 1234, 0, ExchangeHandle(), nullptr) != CHIP_NO_ERROR); NL_TEST_ASSERT(inSuite, pairing.Pair(Transport::PeerAddress(Transport::Type::kBle), 1234, 0, context, &delegate) == CHIP_NO_ERROR); @@ -121,7 +121,7 @@ void SecurePairingStartTest(nlTestSuite * inSuite, void * inContext) PASESession pairing1; NL_TEST_ASSERT(inSuite, pairing1.MessageDispatch().Init(&gTransportMgr) == CHIP_NO_ERROR); - ExchangeContext * context1 = ctx.NewExchangeToLocal(&pairing1); + ExchangeHandle context1 = ctx.NewExchangeToLocal(&pairing1); NL_TEST_ASSERT(inSuite, pairing1.Pair(Transport::PeerAddress(Transport::Type::kBle), 1234, 0, context1, &delegate) == CHIP_ERROR_BAD_REQUEST); @@ -145,7 +145,7 @@ void SecurePairingHandshakeTestCommon(nlTestSuite * inSuite, void * inContext, P ctx.GetExchangeManager().RegisterUnsolicitedMessageHandlerForType( Protocols::SecureChannel::MsgType::PBKDFParamRequest, &pairingAccessory) == CHIP_NO_ERROR); - ExchangeContext * contextCommissioner = ctx.NewExchangeToLocal(&pairingCommissioner); + ExchangeHandle contextCommissioner = ctx.NewExchangeToLocal(&pairingCommissioner); NL_TEST_ASSERT(inSuite, pairingAccessory.WaitForPairing(1234, 500, (const uint8_t *) "saltSALT", 8, 0, &delegateAccessory) == diff --git a/src/transport/SecureSessionMgr.h b/src/transport/SecureSessionMgr.h index a716a408c6936a..413b2ce8abd8c3 100644 --- a/src/transport/SecureSessionMgr.h +++ b/src/transport/SecureSessionMgr.h @@ -63,6 +63,8 @@ class EncryptedPacketBufferHandle final : private System::PacketBufferHandle uint32_t GetMsgId() const; + bool IsNull() { return System::PacketBufferHandle::IsNull(); } + /** * Creates a copy of the data in this packet. *