diff --git a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java index 31075df71fb432..86c1ba56f1419a 100644 --- a/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java +++ b/examples/tv-casting-app/android/App/app/src/main/java/com/matter/casting/ConnectionExampleFragment.java @@ -306,7 +306,8 @@ public void onClick(DialogInterface dialog, int which) { TAG, "displayPasscodeInputDialog() continueConnecting() failed, calling stopConnecting() due to: " + err); - // Attempt to cancel the connection attempt with the CastingPlayer/Commissioner. + // Since continueConnecting() failed, Attempt to cancel the connection attempt with + // the CastingPlayer/Commissioner. err = targetCastingPlayer.stopConnecting(); if (err.hasError()) { Log.e(TAG, "displayPasscodeInputDialog() stopConnecting() failed due to: " + err); diff --git a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp index 11e8a1450d491d..4a8830c04564f8 100644 --- a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp +++ b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp @@ -220,7 +220,8 @@ CHIP_ERROR CastingPlayer::ContinueConnecting() CHIP_ERROR CastingPlayer::StopConnecting() { - ChipLogProgress(AppServer, "CastingPlayer::StopConnecting()"); + ChipLogProgress(AppServer, "CastingPlayer::StopConnecting() called, while ChipDeviceEventHandler.sUdcInProgress: %s", + support::ChipDeviceEventHandler::isUdcInProgress() ? "true" : "false"); VerifyOrReturnValue(mConnectionState == CASTING_PLAYER_CONNECTING, CHIP_ERROR_INCORRECT_STATE, ChipLogError(AppServer, "CastingPlayer::StopConnecting() called while not in connecting state");); VerifyOrReturnValue( @@ -231,7 +232,19 @@ CHIP_ERROR CastingPlayer::StopConnecting() CHIP_ERROR err = CHIP_NO_ERROR; mIdOptions.resetState(); - mIdOptions.mCancelPasscode = true; + mIdOptions.mCancelPasscode = true; + mConnectionState = CASTING_PLAYER_NOT_CONNECTED; + mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec; + mTargetCastingPlayer = nullptr; + + // If a CastingPlayer::ContinueConnecting() error occurs, StopConnecting() can be called while sUdcInProgress == true. + // sUdcInProgress should be set to false before sending the CancelPasscode IdentificationDeclaration message to the + // CastingPlayer/Commissioner. + if (support::ChipDeviceEventHandler::isUdcInProgress()) + { + support::ChipDeviceEventHandler::SetUdcStatus(false); + } + ChipLogProgress( AppServer, "CastingPlayer::StopConnecting() calling SendUserDirectedCommissioningRequest() to indicate user canceled passcode entry"); @@ -239,11 +252,10 @@ CHIP_ERROR CastingPlayer::StopConnecting() SuccessOrExit(err = SendUserDirectedCommissioningRequest()); #endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT - // SendUserDirectedCommissioningRequest() sets SetUdcStatus(true) when sending a UDC message. + // CastingPlayer::SendUserDirectedCommissioningRequest() calls SetUdcStatus(true) before sending the UDC + // IdentificationDeclaration message. Since StopConnecting() is attempting to cancel the commissioning proces, we need to set + // the UDC status to false after sending the message. support::ChipDeviceEventHandler::SetUdcStatus(false); - mConnectionState = CASTING_PLAYER_NOT_CONNECTED; - mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec; - mTargetCastingPlayer = nullptr; exit: if (err != CHIP_NO_ERROR) diff --git a/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.cpp b/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.cpp index fd6039d5256031..bb226a15298229 100644 --- a/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.cpp +++ b/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.cpp @@ -222,6 +222,11 @@ CHIP_ERROR ChipDeviceEventHandler::SetUdcStatus(bool udcInProgress) return CHIP_NO_ERROR; } +bool ChipDeviceEventHandler::isUdcInProgress() +{ + return sUdcInProgress; +} + }; // namespace support }; // namespace casting }; // namespace matter diff --git a/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.h b/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.h index d8660a007cc585..cdebdf698b927c 100644 --- a/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.h +++ b/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.h @@ -45,6 +45,11 @@ class ChipDeviceEventHandler */ static CHIP_ERROR SetUdcStatus(bool udcInProgress); + /** + * @brief Returns true if User Directed Commissioning (UDC) is in progress, false otherwise. + */ + static bool isUdcInProgress(); + private: /** * @brief if kFailSafeTimerExpired is received and a request to connect to a CastingPlayer is pending, open a basic