Skip to content

Commit

Permalink
Updates to GetDiscoveredCommissioner
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Sep 11, 2023
1 parent d792649 commit 463b2a5
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@

#define CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT 4

// cached players that were seen before this window (in days) will not be surfaced as "discovered"
#define CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_DAYS 60L

// Include the CHIPProjectConfig from config/standalone
// Add this at the end so that we can hit our #defines first
#include <CHIPProjectConfig.h>
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ class CastingServer : public AppDelegate
PersistenceManager mPersistenceManager;
bool mInited = false;
bool mUdcInProgress = false;
chip::Dnssd::DiscoveredNodeData mStrNodeDataList[64];
TargetVideoPlayerInfo mActiveTargetVideoPlayerInfo;
TargetVideoPlayerInfo mCachedTargetVideoPlayerInfo[kMaxCachedVideoPlayers];
uint16_t mTargetVideoPlayerVendorId = 0;
Expand Down
83 changes: 77 additions & 6 deletions examples/tv-casting-app/tv-casting-common/src/CastingServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,30 +240,101 @@ CHIP_ERROR CastingServer::SendUserDirectedCommissioningRequest(Dnssd::Discovered
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT

CHIP_ERROR ConvertToDiscoveredNodeData(TargetVideoPlayerInfo * player, Dnssd::DiscoveredNodeData & outNodeData)
{
if (player == nullptr)
return CHIP_ERROR_INVALID_ARGUMENT;

return CHIP_NO_ERROR;
}

const Dnssd::DiscoveredNodeData *
CastingServer::GetDiscoveredCommissioner(int index, chip::Optional<TargetVideoPlayerInfo *> & outAssociatedConnectableVideoPlayer)
{
const Dnssd::DiscoveredNodeData * discoveredNodeData = mCommissionableNodeController.GetDiscoveredCommissioner(index);
if (discoveredNodeData != nullptr)
// get actively discovered commissioner at 'index', if any
const Dnssd::DiscoveredNodeData * activelyDiscoveredNodeData = mCommissionableNodeController.GetDiscoveredCommissioner(index);

// return actively discovered commissioner, if any (+ associate it to a corresponding cached video player)
if (activelyDiscoveredNodeData != nullptr)
{
for (size_t i = 0; i < kMaxCachedVideoPlayers && mCachedTargetVideoPlayerInfo[i].IsInitialized(); i++)
{
if (mCachedTargetVideoPlayerInfo[i].IsSameAs(discoveredNodeData))
if (mCachedTargetVideoPlayerInfo[i].IsSameAs(activelyDiscoveredNodeData))
{
outAssociatedConnectableVideoPlayer = MakeOptional(&mCachedTargetVideoPlayerInfo[i]);

mCachedTargetVideoPlayerInfo[i].SetLastDiscovered(System::SystemClock().GetMonotonicMilliseconds64());
ChipLogProgress(AppServer, "Updating cache of VideoPlayers with MACAddress: %.*s",
static_cast<int>(response.size()), response.data());
CHIP_ERROR err = CastingServer::GetInstance()->mPersistenceManager.AddVideoPlayer(&mCachedTargetVideoPlayerInfo[i]);
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "AddVideoPlayer(ToCache) error: %" CHIP_ERROR_FORMAT, err.Format());
}
}
}

return activelyDiscoveredNodeData;
}
else // if no actively discovered commissioner at 'index', return a cached video player that supports STR/WoL
{
chip::Dnssd::DiscoveredNodeData * strNodeData = nullptr;
// count total number of actively discoverable commissioners
int discoveredCommissionerCount;
for (discoveredCommissionerCount = 0;
mCommissionableNodeController.GetDiscoveredCommissioner(discoveredCommissionerCount) != nullptr;
discoveredCommissionerCount++)
;

// counter used to determine which video player to return from the list of cached video players
int targetSTRVideoPlayerCounter = index - discoveredCommissionerCount;
if (targetSTRVideoPlayerCounter < 0) // cannot be less than 0
{
ChipLogError(AppServer, "CastingServer::GetDiscoveredCommissioner encountered invalid targetSTRVideoPlayerCounter %d",
targetSTRVideoPlayerCounter);
return nullptr;
}
strNodeData = &mStrNodeDataList[targetSTRVideoPlayerCounter];

// search/loop through all the cached video players
for (size_t i = 0; i < kMaxCachedVideoPlayers && mCachedTargetVideoPlayerInfo[i].IsInitialized(); i++)
{
// search/loop through all actively discovered commissioners so we can skip mCachedTargetVideoPlayerInfo[i] if it is the
// same as an actively discovered commissioner
const Dnssd::DiscoveredNodeData * discoveredNodeData = nullptr;
for (int j = 0; j < CHIP_DEVICE_CONFIG_MAX_DISCOVERED_NODES; j++)
{
discoveredNodeData = mCommissionableNodeController.GetDiscoveredCommissioner(j);
if (discoveredNodeData != nullptr && discoveredNodeData->resolutionData.IsValid())
{
// consider a cached video player only if it was NOT in the list of actively discoverable commissioners
if (!mCachedTargetVideoPlayerInfo[i].IsSameAs(discoveredNodeData))
{
// if this cached video player supports STR/WoL and was discoverable recently, convert it to a
// DiscoveredNodeData object and break out of the loop
if (mCachedTargetVideoPlayerInfo[i].GetMACAddress() != nullptr &&
mCachedTargetVideoPlayerInfo[i].GetMACAddress()->size() > 0
#ifdef CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_DAYS
&& mCachedTargetVideoPlayerInfo[i].GetLastDiscovered().count() >
(System::SystemClock().GetMonotonicMilliseconds64().count() -
CHIP_DEVICE_CONFIG_STR_CACHE_LAST_DISCOVERED_DAYS * 24 * 60 * 60 * 1000)
#endif
)
{
// ensure we are returning the cached video player at the correct index
if (targetSTRVideoPlayerCounter-- == 0)
{
if (ConvertToDiscoveredNodeData(&mCachedTargetVideoPlayerInfo[i], *strNodeData) == CHIP_NO_ERROR)
{
return nullptr;
}
}
break;
}
}
}
}
}
return strNodeData;
}
return discoveredNodeData;
}

void CastingServer::ReadServerClustersForNode(NodeId nodeId)
Expand Down

0 comments on commit 463b2a5

Please sign in to comment.