Skip to content

Commit

Permalink
Use default application dispatch if an application hasn't provided one (
Browse files Browse the repository at this point in the history
#6053)

* Use default application dispatch if an application hasn't provided delegate

* close exchange instead of releasing it
  • Loading branch information
pan-apple authored Apr 15, 2021
1 parent 24f26f6 commit f348eb4
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 19 additions & 6 deletions src/messaging/ExchangeContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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())
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/protocols/secure_channel/NetworkProvisioning.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}

Expand Down

0 comments on commit f348eb4

Please sign in to comment.