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 c02ce5b9aabd0b..515e7a0ac5f780 100644 --- a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp +++ b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp @@ -28,8 +28,18 @@ CastingPlayer * CastingPlayer::mTargetCastingPlayer = nullptr; void CastingPlayer::Connect(ConnectCallback onCompleted, const chip::System::Clock::Seconds16 commissioningWindowTimeout) { - chip::Inet::IPAddress * ipAddressToUse = GetIpAddressForUDCRequest(); - CHIP_ERROR err = ipAddressToUse != nullptr ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE; + ChipLogProgress(AppServer, "CastingPlayer::Connect called"); + + chip::Inet::IPAddress * ipAddressToUse = nullptr; + + CHIP_ERROR err = CHIP_NO_ERROR; + err = (mConnectionState != CASTING_PLAYER_CONNECTING ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE); + VerifyOrExit(mConnectionState != CASTING_PLAYER_CONNECTING, + ChipLogError(AppServer, "CastingPlayer::Connect called while already connecting to this CastingPlayer")); + mConnectionState = CASTING_PLAYER_CONNECTING; + + ipAddressToUse = GetIpAddressForUDCRequest(); + err = ipAddressToUse != nullptr ? CHIP_NO_ERROR : CHIP_ERROR_INCORRECT_STATE; VerifyOrExit(ipAddressToUse != nullptr, ChipLogError(AppServer, "No IP Address found to send UDC request to")); SuccessOrExit(err = support::ChipDeviceEventHandler::SetUdcStatus(true)); @@ -45,6 +55,7 @@ void CastingPlayer::Connect(ConnectCallback onCompleted, const chip::System::Clo if (err != CHIP_NO_ERROR) { support::ChipDeviceEventHandler::SetUdcStatus(false); + mConnectionState = CASTING_PLAYER_NOT_CONNECTED; ChipLogError(AppServer, "CastingPlayer::Connect failed with %" CHIP_ERROR_FORMAT, err.Format()); } } diff --git a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h index 01cb5c2e31db27..4ffc3b1d845ede 100644 --- a/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h +++ b/examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h @@ -55,6 +55,46 @@ class CastingPlayerAttributes uint32_t deviceType; }; +/** + * @brief Represents CastingPlayer ConnectionState. + * + */ +enum ConnectionState +{ + CASTING_PLAYER_NOT_CONNECTED, + CASTING_PLAYER_CONNECTING, + CASTING_PLAYER_CONNECTED, +}; + +class ConnectionContext +{ +public: + ConnectionContext(CastingPlayer * castingPlayer, chip::OnDeviceConnected handleDeviceConnected, + chip::OnDeviceConnectionFailure handleConnectionFailure) + { + mTargetVideoPlayerInfo = targetVideoPlayerInfo; + mOnConnectedCallback = new chip::Callback::Callback(handleDeviceConnected, this); + mOnConnectionFailureCallback = new chip::Callback::Callback(handleConnectionFailure, this); + } + + ~ConnectionContext() + { + if (mOnConnectedCallback != nullptr) + { + delete mOnConnectedCallback; + } + + if (mOnConnectionFailureCallback != nullptr) + { + delete mOnConnectionFailureCallback; + } + } + + CastingPlayer * mCastingPlayer; + chip::Callback::Callback * mOnConnectedCallback = nullptr; + chip::Callback::Callback * mOnConnectionFailureCallback = nullptr; +}; + /** * @brief CastingPlayer represents a Matter commissioner that is able to play media to a physical * output or to a display screen which is part of the device. @@ -63,7 +103,7 @@ class CastingPlayer : public std::enable_shared_from_this { private: // std::vector> endpoints; - bool mConnected = false; + ConnectionState mConnectionState = CASTING_PLAYER_NOT_CONNECTED; CastingPlayerAttributes mAttributes; static CastingPlayer * mTargetCastingPlayer; ConnectCallback mOnCompleted; @@ -113,7 +153,7 @@ class CastingPlayer : public std::enable_shared_from_this /** * @return true if this CastingPlayer is connected to the CastingApp */ - bool IsConnected() const { return mConnected; } + bool IsConnected() const { return mConnectionState == CASTING_PLAYER_CONNECTED; } void Connect(ConnectCallback onCompleted, const chip::System::Clock::Seconds16 commissioningWindowTimeout = kCommissioningWindowTimeout); 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 9a469b9eafb53d..8c932f8bc3f18b 100644 --- a/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.cpp +++ b/examples/tv-casting-app/tv-casting-common/support/ChipDeviceEventHandler.cpp @@ -52,6 +52,12 @@ void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * e { ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle will connect with nodeId=0x" ChipLogFormatX64 " fabricIndex=%d", ChipLogValueX64(targetPeerNodeId), targetFabricIndex); + ConnectionContext * connectionContext = new ConnectionContext(CastingPlayer::GetTargetCastingPlayer(), + CastingPlayer::HandleDeviceConnected, CastingPlayer::HandleDeviceConnectionFailure); + chip::Server::GetInstance().GetCASESessionManager()->FindOrEstablishSession(ScopedNodeId(mNodeId, mFabricIndex), + connectionContext->mOnConnectedCallback, + connectionContext->mOnConnectionFailureCallback); + // TODO /*CHIP_ERROR err = CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->Initialize( targetPeerNodeId, targetFabricIndex, CastingServer::GetInstance()->mOnConnectionSuccessClientCallback,