From f7ab9f4cef0e6cf5d786fc55f3181a22cc6cefbf Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Thu, 15 Apr 2021 07:29:59 -0700 Subject: [PATCH 1/2] Use default application dispatch if an application hasn't provided delegate --- src/messaging/ExchangeContext.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/messaging/ExchangeContext.cpp b/src/messaging/ExchangeContext.cpp index 1ada25cc7d8a36..b5f89468395cb9 100644 --- a/src/messaging/ExchangeContext.cpp +++ b/src/messaging/ExchangeContext.cpp @@ -125,7 +125,14 @@ CHIP_ERROR ExchangeContext::SendMessageImpl(Protocols::Id protocolId, uint8_t ms bool reliableTransmissionRequested = !sendFlags.Has(SendMessageFlags::kNoAutoRequestAck); - VerifyOrExit(GetMessageDispatch() != nullptr, err = CHIP_ERROR_INCORRECT_STATE); + ExchangeMessageDispatch * dispatch = GetMessageDispatch(); + ApplicationExchangeDispatch defaultDispatch; + + if (dispatch == nullptr) + { + defaultDispatch.Init(mExchangeMgr->GetReliableMessageMgr(), mExchangeMgr->GetSessionMgr()); + dispatch = &defaultDispatch; + } // If a response message is expected... if (sendFlags.Has(SendMessageFlags::kExpectResponse)) @@ -143,8 +150,8 @@ CHIP_ERROR ExchangeContext::SendMessageImpl(Protocols::Id protocolId, uint8_t ms } } - err = GetMessageDispatch()->SendMessage(mSecureSession, mExchangeId, IsInitiator(), mReliableMessageContext, - reliableTransmissionRequested, protocolId, msgType, std::move(msgBuf)); + err = dispatch->SendMessage(mSecureSession, mExchangeId, IsInitiator(), mReliableMessageContext, reliableTransmissionRequested, + protocolId, msgType, std::move(msgBuf)); exit: if (err != CHIP_NO_ERROR && IsResponseExpected()) @@ -361,10 +368,16 @@ CHIP_ERROR ExchangeContext::HandleMessage(const PacketHeader & packetHeader, con // layer has completed its work on the ExchangeContext. Retain(); - CHIP_ERROR err = CHIP_NO_ERROR; - VerifyOrExit(GetMessageDispatch() != nullptr, err = CHIP_ERROR_INCORRECT_STATE); + ExchangeMessageDispatch * dispatch = GetMessageDispatch(); + ApplicationExchangeDispatch defaultDispatch; + + if (dispatch == nullptr) + { + defaultDispatch.Init(mExchangeMgr->GetReliableMessageMgr(), mExchangeMgr->GetSessionMgr()); + dispatch = &defaultDispatch; + } - err = GetMessageDispatch()->OnMessageReceived(payloadHeader, packetHeader.GetMessageId(), peerAddress, mReliableMessageContext); + CHIP_ERROR err = dispatch->OnMessageReceived(payloadHeader, packetHeader.GetMessageId(), peerAddress, mReliableMessageContext); SuccessOrExit(err); // The SecureChannel::StandaloneAck message type is only used for CRMP; do not pass such messages to the application layer. From d85558a884b0e35aed32dcdff271043264a80aa7 Mon Sep 17 00:00:00 2001 From: Pankaj Garg Date: Thu, 15 Apr 2021 08:31:17 -0700 Subject: [PATCH 2/2] close exchange instead of releasing it --- src/protocols/secure_channel/NetworkProvisioning.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/protocols/secure_channel/NetworkProvisioning.cpp b/src/protocols/secure_channel/NetworkProvisioning.cpp index 78d7dd15edc481..c05553c50298ff 100644 --- a/src/protocols/secure_channel/NetworkProvisioning.cpp +++ b/src/protocols/secure_channel/NetworkProvisioning.cpp @@ -69,7 +69,7 @@ void NetworkProvisioning::OnMessageReceived(Messaging::ExchangeContext * exchang // Currently, the only mechanism to get this callback is via unsolicited message handler. // That means that we now own the reference to exchange context. Let's free the reference since we no longer // need it. - exchangeContext->Release(); + exchangeContext->Close(); } CHIP_ERROR NetworkProvisioning::HandleNetworkProvisioningMessage(uint8_t msgType, const System::PacketBufferHandle & msgBuf) @@ -196,7 +196,7 @@ CHIP_ERROR NetworkProvisioning::SendMessageUsingExchange(uint8_t msgType, System VerifyOrReturnError(exchangeContext != nullptr, CHIP_ERROR_INTERNAL); CHIP_ERROR err = exchangeContext->SendMessage(Protocols::NetworkProvisioning::Id, msgType, std::move(msgPayload), Messaging::SendMessageFlags::kNoAutoRequestAck); - exchangeContext->Release(); + exchangeContext->Close(); return err; }