Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-fix OTA exchange leaks #20632

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/app/clusters/ota-requestor/DefaultOTARequestor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,14 @@ void DefaultOTARequestor::OnDownloadStateChanged(OTADownloader::State state, OTA
{
case OTADownloader::State::kComplete:
mOtaRequestorDriver->UpdateDownloaded();
mBdxMessenger.Reset();
break;
case OTADownloader::State::kIdle:
if (reason != OTAChangeReasonEnum::kSuccess)
{
RecordErrorUpdateState(CHIP_ERROR_CONNECTION_ABORTED, reason);
}
mBdxMessenger.Reset();
break;
default:
break;
Expand Down Expand Up @@ -823,10 +825,10 @@ CHIP_ERROR DefaultOTARequestor::StartDownload(OperationalDeviceProxy & devicePro
Optional<SessionHandle> session = deviceProxy.GetSecureSession();
VerifyOrReturnError(session.HasValue(), CHIP_ERROR_INCORRECT_STATE);

mExchangeCtx = exchangeMgr->NewContext(session.Value(), &mBdxMessenger);
VerifyOrReturnError(mExchangeCtx != nullptr, CHIP_ERROR_NO_MEMORY);
chip::Messaging::ExchangeContext * exchangeCtx = exchangeMgr->NewContext(session.Value(), &mBdxMessenger);
VerifyOrReturnError(exchangeCtx != nullptr, CHIP_ERROR_NO_MEMORY);
bzbarsky-apple marked this conversation as resolved.
Show resolved Hide resolved

mBdxMessenger.Init(mBdxDownloader, mExchangeCtx);
mBdxMessenger.Init(mBdxDownloader, exchangeCtx);
mBdxDownloader->SetMessageDelegate(&mBdxMessenger);
mBdxDownloader->SetStateDelegate(this);

Expand Down
36 changes: 25 additions & 11 deletions src/app/clusters/ota-requestor/DefaultOTARequestor.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,14 @@ class DefaultOTARequestor : public OTARequestorInterface, public BDXDownloader::
{
sendFlags.Set(chip::Messaging::SendMessageFlags::kExpectResponse);
}
ReturnErrorOnFailure(mExchangeCtx->SendMessage(event.msgTypeData.ProtocolId, event.msgTypeData.MessageType,
event.MsgData.Retain(), sendFlags));
return CHIP_NO_ERROR;
CHIP_ERROR err = mExchangeCtx->SendMessage(event.msgTypeData.ProtocolId, event.msgTypeData.MessageType,
event.MsgData.Retain(), sendFlags);
if (err != CHIP_NO_ERROR)
{
Reset();
}

return err;
}

CHIP_ERROR OnMessageReceived(chip::Messaging::ExchangeContext * ec, const chip::PayloadHeader & payloadHeader,
Expand All @@ -150,13 +155,14 @@ class DefaultOTARequestor : public OTARequestorInterface, public BDXDownloader::
return CHIP_NO_ERROR;
}

mDownloader->OnMessageReceived(payloadHeader, payload.Retain());
mDownloader->OnMessageReceived(payloadHeader, std::move(payload));

// For a receiver using BDX Protocol, all received messages will require a response except for a StatusReport
if (!payloadHeader.HasMessageType(chip::Protocols::SecureChannel::MsgType::StatusReport))
{
ec->WillSendMessage();
}

return CHIP_NO_ERROR;
}

Expand All @@ -169,12 +175,21 @@ class DefaultOTARequestor : public OTARequestorInterface, public BDXDownloader::
}
}

void OnExchangeClosing(Messaging::ExchangeContext * ec) override { mExchangeCtx = nullptr; }

void Init(chip::BDXDownloader * downloader, chip::Messaging::ExchangeContext * ec)
{
mExchangeCtx = ec;
mDownloader = downloader;
}

void Reset()
{
VerifyOrReturn(mExchangeCtx != nullptr);
mExchangeCtx->Close();
mExchangeCtx = nullptr;
}

private:
chip::Messaging::ExchangeContext * mExchangeCtx;
chip::BDXDownloader * mDownloader;
Expand Down Expand Up @@ -292,13 +307,12 @@ class DefaultOTARequestor : public OTARequestorInterface, public BDXDownloader::
*/
static void OnCommissioningCompleteRequestor(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg);

OTARequestorStorage * mStorage = nullptr;
OTARequestorDriver * mOtaRequestorDriver = nullptr;
CASESessionManager * mCASESessionManager = nullptr;
OnConnectedAction mOnConnectedAction = kQueryImage;
Messaging::ExchangeContext * mExchangeCtx = nullptr;
BDXDownloader * mBdxDownloader = nullptr; // TODO: this should be OTADownloader
BDXMessenger mBdxMessenger; // TODO: ideally this is held by the application
OTARequestorStorage * mStorage = nullptr;
OTARequestorDriver * mOtaRequestorDriver = nullptr;
CASESessionManager * mCASESessionManager = nullptr;
OnConnectedAction mOnConnectedAction = kQueryImage;
BDXDownloader * mBdxDownloader = nullptr; // TODO: this should be OTADownloader
BDXMessenger mBdxMessenger; // TODO: ideally this is held by the application
uint8_t mUpdateTokenBuffer[kMaxUpdateTokenLen];
ByteSpan mUpdateToken;
uint32_t mCurrentVersion = 0;
Expand Down