diff --git a/examples/tv-casting-app/linux/main.cpp b/examples/tv-casting-app/linux/main.cpp index 681fefffa0de7c..2c5815069fec16 100644 --- a/examples/tv-casting-app/linux/main.cpp +++ b/examples/tv-casting-app/linux/main.cpp @@ -332,6 +332,10 @@ class TargetEndpointInfo class TargetVideoPlayerInfo { public: + TargetVideoPlayerInfo() : + mOnConnectedCallback(HandleDeviceConnected, this), mOnConnectionFailureCallback(HandleDeviceConnectionFailure, this) + {} + bool IsInitialized() { return mInitialized; } CHIP_ERROR Initialize(NodeId nodeId, FabricIndex fabricIndex) @@ -361,7 +365,23 @@ class TargetVideoPlayerInfo PeerId peerID = fabric->GetPeerIdForNode(nodeId); - mOperationalDeviceProxy = server->GetCASESessionManager()->FindExistingSession(peerID); + // + // TODO: The code here is assuming that we can create an OperationalDeviceProxy instance and attach it immediately + // to a CASE session that just got established to us by the tv-app. While this will work most of the time, + // this is a dangerous assumption to make since it is entirely possible for that secure session to have been + // evicted in the time since that session was established to the point here when we desire to interact back + // with that peer. If that is the case, our `OnConnected` callback will not get invoked syncronously and + // mOperationalDeviceProxy will still have a value of null, triggering the check below to fail. + // + mOperationalDeviceProxy = nullptr; + CHIP_ERROR err = + server->GetCASESessionManager()->FindOrEstablishSession(peerID, &mOnConnectedCallback, &mOnConnectionFailureCallback); + if (err != CHIP_NO_ERROR) + { + ChipLogError(AppServer, "Could not establish a session to the peer"); + return err; + } + if (mOperationalDeviceProxy == nullptr) { ChipLogError(AppServer, "Failed to find an existing instance of OperationalDeviceProxy to the peer"); @@ -444,12 +464,27 @@ class TargetVideoPlayerInfo } private: + static void HandleDeviceConnected(void * context, OperationalDeviceProxy * device) + { + TargetVideoPlayerInfo * _this = static_cast(context); + _this->mOperationalDeviceProxy = device; + } + + static void HandleDeviceConnectionFailure(void * context, PeerId peerId, CHIP_ERROR error) + { + TargetVideoPlayerInfo * _this = static_cast(context); + _this->mOperationalDeviceProxy = nullptr; + } + static constexpr size_t kMaxNumberOfEndpoints = 5; TargetEndpointInfo mEndpoints[kMaxNumberOfEndpoints]; NodeId mNodeId; FabricIndex mFabricIndex; OperationalDeviceProxy * mOperationalDeviceProxy; + Callback::Callback mOnConnectedCallback; + Callback::Callback mOnConnectionFailureCallback; + bool mInitialized = false; }; TargetVideoPlayerInfo gTargetVideoPlayerInfo;