From 1091208192da74f18a6eeda4a99ebfad72f6d722 Mon Sep 17 00:00:00 2001 From: Carol Yang Date: Wed, 9 Mar 2022 09:24:28 -0800 Subject: [PATCH] [OTA] Tear down CASE session during a timeout error (#15939) --- .../clusters/ota-requestor/OTARequestor.cpp | 36 ++++++++++++++++++- src/app/clusters/ota-requestor/OTARequestor.h | 7 +++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/src/app/clusters/ota-requestor/OTARequestor.cpp b/src/app/clusters/ota-requestor/OTARequestor.cpp index 4aca7370301df9..d70298bdf6abc1 100644 --- a/src/app/clusters/ota-requestor/OTARequestor.cpp +++ b/src/app/clusters/ota-requestor/OTARequestor.cpp @@ -184,7 +184,14 @@ void OTARequestor::OnQueryImageFailure(void * context, CHIP_ERROR error) OTARequestor * requestorCore = static_cast(context); VerifyOrDie(requestorCore != nullptr); - ChipLogDetail(SoftwareUpdate, "QueryImage failure response %" CHIP_ERROR_FORMAT, error.Format()); + ChipLogError(SoftwareUpdate, "Received QueryImage failure response: %" CHIP_ERROR_FORMAT, error.Format()); + + if (error == CHIP_ERROR_TIMEOUT) + { + ChipLogError(SoftwareUpdate, "CASE session may be invalid, tear down session"); + requestorCore->DisconnectFromProvider(); + } + requestorCore->RecordErrorUpdateState(UpdateFailureState::kQuerying, error); } @@ -305,6 +312,33 @@ void OTARequestor::ConnectToProvider(OnConnectedAction onConnectedAction) } } +void OTARequestor::DisconnectFromProvider() +{ + if (mServer == nullptr) + { + ChipLogError(SoftwareUpdate, "Server not set"); + RecordErrorUpdateState(UpdateFailureState::kUnknown, CHIP_ERROR_INCORRECT_STATE); + return; + } + + if (!mProviderLocation.HasValue()) + { + ChipLogError(SoftwareUpdate, "Provider location not set"); + RecordErrorUpdateState(UpdateFailureState::kUnknown, CHIP_ERROR_INCORRECT_STATE); + return; + } + + FabricInfo * fabricInfo = mServer->GetFabricTable().FindFabricWithIndex(mProviderLocation.Value().fabricIndex); + if (fabricInfo == nullptr) + { + ChipLogError(SoftwareUpdate, "Cannot find fabric"); + RecordErrorUpdateState(UpdateFailureState::kUnknown, CHIP_ERROR_INCORRECT_STATE); + return; + } + + mCASESessionManager->ReleaseSession(fabricInfo->GetPeerIdForNode(mProviderLocation.Value().providerNodeID)); +} + // Requestor is directed to cancel image update in progress. All the Requestor state is // cleared, UpdateState is reset to Idle void OTARequestor::CancelImageUpdate() diff --git a/src/app/clusters/ota-requestor/OTARequestor.h b/src/app/clusters/ota-requestor/OTARequestor.h index 374fb6112b9d0d..b5de8b40b0c55b 100644 --- a/src/app/clusters/ota-requestor/OTARequestor.h +++ b/src/app/clusters/ota-requestor/OTARequestor.h @@ -242,12 +242,17 @@ class OTARequestor : public OTARequestorInterface, public BDXDownloader::StateDe }; /** - * Called to establish a session to mProviderLocation. + * Called to establish a session to provider indicated by mProviderLocation * * @param onConnectedAction The action to take once session to provider has been established */ void ConnectToProvider(OnConnectedAction onConnectedAction); + /** + * Called to tear down a session to provider indicated by mProviderLocation + */ + void DisconnectFromProvider(); + /** * Start download of the software image returned in QueryImageResponse */