Skip to content

Commit

Permalink
tv-casting-app: On kBindingsChangedViaCluster, connect to VideoPlayer…
Browse files Browse the repository at this point in the history
… if already commissioned with it (#24268)

* tv-casting-app: On kBindingsChangedViaCluster, connect to VideoPlayer if already commissioned with it

* Addressing chrisdecenzo@'s feedback

* Addressing bzbarsky-apple@'s feedback
  • Loading branch information
sharadb-amazon authored and pull[bot] committed Dec 4, 2023
1 parent bdea0b6 commit 993c54e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,8 @@ class CastingServer
void ReadServerClusters(chip::EndpointId endpointId);

PersistenceManager mPersistenceManager;
bool mInited = false;
bool mInited = false;
bool mUdcInProgress = false;
TargetVideoPlayerInfo mActiveTargetVideoPlayerInfo;
TargetVideoPlayerInfo mCachedTargetVideoPlayerInfo[kMaxCachedVideoPlayers];
uint16_t mTargetVideoPlayerVendorId = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class TargetVideoPlayerInfo
size_t GetNumIPs() const { return mNumIPs; }
const chip::Inet::IPAddress * GetIpAddresses() const { return mIpAddress; }
bool IsSameAs(const chip::Dnssd::DiscoveredNodeData * discoveredNodeData);
bool IsSameAs(const char * deviceName, size_t numIPs, const chip::Inet::IPAddress * ipAddresses);

chip::OperationalDeviceProxy * GetOperationalDeviceProxy()
{
Expand Down
73 changes: 71 additions & 2 deletions examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(chip::Transport::

CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(Dnssd::DiscoveredNodeData * selectedCommissioner)
{
mUdcInProgress = true;
// Send User Directed commissioning request
ReturnErrorOnFailure(SendUserDirectedCommissioningRequest(chip::Transport::PeerAddress::UDP(
selectedCommissioner->resolutionData.ipAddress[0], selectedCommissioner->resolutionData.port,
Expand Down Expand Up @@ -320,24 +321,92 @@ CastingServer::ContentLauncherLaunchURL(TargetEndpointInfo * endpoint, const cha

void CastingServer::DeviceEventCallback(const DeviceLayer::ChipDeviceEvent * event, intptr_t arg)
{
bool runPostCommissioning = false;
chip::NodeId targetPeerNodeId = 0;
chip::FabricIndex targetFabricIndex = 0;
if (event->Type == DeviceLayer::DeviceEventType::kBindingsChangedViaCluster)
{
ChipLogProgress(AppServer, "CastingServer::DeviceEventCallback kBindingsChangedViaCluster received");
if (CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->IsInitialized())
{
ChipLogProgress(AppServer,
"CastingServer::DeviceEventCallback already connected to video player, reading server clusters");
CastingServer::GetInstance()->ReadServerClustersForNode(
CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->GetNodeId());
}
else if (CastingServer::GetInstance()->mUdcInProgress)
{
ChipLogProgress(AppServer,
"CastingServer::DeviceEventCallback UDC is in progress while handling kBindingsChangedViaCluster");
CastingServer::GetInstance()->mUdcInProgress = false;
if (CastingServer::GetInstance()->mTargetVideoPlayerNumIPs > 0)
{
TargetVideoPlayerInfo * connectableVideoPlayerList =
CastingServer::GetInstance()->ReadCachedTargetVideoPlayerInfos();
if (connectableVideoPlayerList == nullptr || !connectableVideoPlayerList[0].IsInitialized())
{
ChipLogError(AppServer, "CastingServer::DeviceEventCallback No cached video players found");
CastingServer::GetInstance()->mCommissioningCompleteCallback(CHIP_ERROR_INCORRECT_STATE);
return;
}

for (size_t i = 0; i < kMaxCachedVideoPlayers && connectableVideoPlayerList[i].IsInitialized(); i++)
{
if (connectableVideoPlayerList[i].IsSameAs(CastingServer::GetInstance()->mTargetVideoPlayerDeviceName,
CastingServer::GetInstance()->mTargetVideoPlayerNumIPs,
CastingServer::GetInstance()->mTargetVideoPlayerIpAddress))
{
ChipLogProgress(AppServer,
"CastingServer::DeviceEventCallback found the video player to initialize/connect to");
targetPeerNodeId = connectableVideoPlayerList[i].GetNodeId();
targetFabricIndex = connectableVideoPlayerList[i].GetFabricIndex();
runPostCommissioning = true;
}
}

if (targetPeerNodeId == 0 && runPostCommissioning == false)
{
ChipLogError(AppServer,
"CastingServer::DeviceEventCallback did NOT find the video player to initialize/connect to");
CastingServer::GetInstance()->mCommissioningCompleteCallback(CHIP_ERROR_INCORRECT_STATE);
return;
}
}
}
}
else if (event->Type == DeviceLayer::DeviceEventType::kCommissioningComplete)
{
ChipLogProgress(AppServer, "CastingServer::DeviceEventCallback kCommissioningComplete received");
CastingServer::GetInstance()->mUdcInProgress = false;
targetPeerNodeId = event->CommissioningComplete.nodeId;
targetFabricIndex = event->CommissioningComplete.fabricIndex;
runPostCommissioning = true;
}

if (runPostCommissioning)
{
ChipLogProgress(AppServer,
"CastingServer::DeviceEventCallback will connect with nodeId=0x" ChipLogFormatX64 " fabricIndex=%d",
ChipLogValueX64(targetPeerNodeId), targetFabricIndex);
CHIP_ERROR err = CastingServer::GetInstance()->GetActiveTargetVideoPlayer()->Initialize(
event->CommissioningComplete.nodeId, event->CommissioningComplete.fabricIndex,
CastingServer::GetInstance()->mOnConnectionSuccessClientCallback,
targetPeerNodeId, targetFabricIndex, CastingServer::GetInstance()->mOnConnectionSuccessClientCallback,
CastingServer::GetInstance()->mOnConnectionFailureClientCallback,
CastingServer::GetInstance()->mTargetVideoPlayerVendorId, CastingServer::GetInstance()->mTargetVideoPlayerProductId,
CastingServer::GetInstance()->mTargetVideoPlayerDeviceType, CastingServer::GetInstance()->mTargetVideoPlayerDeviceName,
CastingServer::GetInstance()->mTargetVideoPlayerNumIPs, CastingServer::GetInstance()->mTargetVideoPlayerIpAddress);

if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "Failed to initialize target video player");
}

err = CastingServer::GetInstance()->mPersistenceManager.AddVideoPlayer(
&CastingServer::GetInstance()->mActiveTargetVideoPlayerInfo);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "AddVideoPlayer(ToCache) error: %" CHIP_ERROR_FORMAT, err.Format());
}

CastingServer::GetInstance()->mCommissioningCompleteCallback(err);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,10 @@ void TargetVideoPlayerInfo::PrintInfo()
}
}

bool TargetVideoPlayerInfo::IsSameAs(const chip::Dnssd::DiscoveredNodeData * discoveredNodeData)
bool TargetVideoPlayerInfo::IsSameAs(const char * deviceName, size_t numIPs, const chip::Inet::IPAddress * ipAddresses)
{
// return false because 'this' VideoPlayer is not null
if (discoveredNodeData == nullptr)
{
return false;
}

// return false because deviceNames are different
if (strcmp(mDeviceName, discoveredNodeData->commissionData.deviceName) != 0)
if (strcmp(mDeviceName, deviceName) != 0)
{
return false;
}
Expand All @@ -146,10 +140,9 @@ bool TargetVideoPlayerInfo::IsSameAs(const chip::Dnssd::DiscoveredNodeData * dis
bool matchFound = false;
for (size_t i = 0; i < mNumIPs && i < chip::Dnssd::CommonResolutionData::kMaxIPAddresses; i++)
{
for (size_t j = 0;
j < discoveredNodeData->resolutionData.numIPs && j < chip::Dnssd::CommonResolutionData::kMaxIPAddresses; j++)
for (size_t j = 0; j < numIPs && j < chip::Dnssd::CommonResolutionData::kMaxIPAddresses; j++)
{
if (mIpAddress[i] == discoveredNodeData->resolutionData.ipAddress[j])
if (mIpAddress[i] == ipAddresses[j])
{
matchFound = true;
break;
Expand All @@ -169,3 +162,15 @@ bool TargetVideoPlayerInfo::IsSameAs(const chip::Dnssd::DiscoveredNodeData * dis

return true;
}

bool TargetVideoPlayerInfo::IsSameAs(const chip::Dnssd::DiscoveredNodeData * discoveredNodeData)
{
// return false because 'this' VideoPlayer is not null
if (discoveredNodeData == nullptr)
{
return false;
}

return IsSameAs(discoveredNodeData->commissionData.deviceName, discoveredNodeData->resolutionData.numIPs,
discoveredNodeData->resolutionData.ipAddress);
}

0 comments on commit 993c54e

Please sign in to comment.