diff --git a/examples/tv-casting-app/linux/simple-app-helper.cpp b/examples/tv-casting-app/linux/simple-app-helper.cpp index fe248c2a66d2a8..b72ed361251e75 100644 --- a/examples/tv-casting-app/linux/simple-app-helper.cpp +++ b/examples/tv-casting-app/linux/simple-app-helper.cpp @@ -63,7 +63,33 @@ void DiscoveryDelegateImpl::HandleOnUpdated(matter::casting::memory::StrongGetId()); + std::vector> endpoints = castingPlayer->GetEndpoints(); + + // Find the desired Endpoint and auto-trigger some Matter Casting demo interactions + auto it = std::find_if(endpoints.begin(), endpoints.end(), + [](const matter::casting::memory::Strong & endpoint) { + ChipLogProgress(AppServer, "Endpoint's VendorID in comparator: %d", endpoint->GetVendorId()); + return endpoint->GetVendorId() == 65521; + }); + if (it != endpoints.end()) + { + unsigned index = (unsigned int) std::distance(endpoints.begin(), it); + // do something with endpoints[index] + (void) index; + } + else + { + ChipLogError(AppServer, "Desired Endpoint not found on the CastingPlayer(ID: %s)", castingPlayer->GetId()); + } + } + else + { + ChipLogProgress(AppServer, "ConnectionHandler: Failed to connect to CastingPlayer(ID: %s) with err %" CHIP_ERROR_FORMAT, + castingPlayer->GetId(), err.Format()); + } } #if defined(ENABLE_CHIP_SHELL) diff --git a/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.cpp b/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.cpp index 8fed1f4a93caaf..44e3fb1ef502bb 100644 --- a/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.cpp +++ b/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.cpp @@ -22,31 +22,61 @@ namespace matter { namespace casting { namespace clusters { -CHIP_ERROR ContentLauncherCluster::LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request) +std::future ContentLauncherCluster::LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request) { - + std::promise * promise = new std::promise(); memory::Strong endpoint = this->GetEndpoint().lock(); if (endpoint) { LaunchURLContext * interactionContext = new LaunchURLContext(); interactionContext->endpoint = this->GetEndpoint(); - interactionContext->request = request; + interactionContext->request = request; + interactionContext->promise = promise; + endpoint->GetCastingPlayer()->FindOrEstablishSession( interactionContext, [](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) { LaunchURLContext * _interactionContext = static_cast(context); - support::MediaClusterBase cluster(exchangeMgr, sessionHandle, _interactionContext->endpoint.lock()->GetId()); + CHIP_ERROR err = cluster.template InvokeCommand( + _interactionContext->request, _interactionContext, + [](void * context, + const chip::app::Clusters::ContentLauncher::Commands::LauncherResponse::DecodableType & response) { + LaunchURLContext * _interactionContext = static_cast(context); + ChipLogProgress(AppServer, "ContentLauncherCluster::LaunchURL success"); + _interactionContext->promise->set_value(42); + }, + [](void * context, CHIP_ERROR error) { + LaunchURLContext * _interactionContext = static_cast(context); + ChipLogError(AppServer, + "ContentLauncherCluster::LaunchURL failure on EndpointId: %d with error: %" CHIP_ERROR_FORMAT, + _interactionContext->endpoint.lock()->GetId(), error.Format()); + _interactionContext->promise->set_value(-1); + }); + if (err != CHIP_NO_ERROR) + { + ChipLogError(AppServer, + "ContentLauncherCluster::LaunchURL failure on EndpointId: %d with error: %" CHIP_ERROR_FORMAT, + _interactionContext->endpoint.lock()->GetId(), err.Format()); + _interactionContext->promise->set_value(-1); + } }, [](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) { - ChipLogError(AppServer, "ContentLauncherCluster::LaunchURL failed"); + LaunchURLContext * _interactionContext = static_cast(context); + ChipLogError( + AppServer, + "ContentLauncherCluster::LaunchURL failure in retrieving session info for peerId.nodeId: 0x" ChipLogFormatX64 + ", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT, + ChipLogValueX64(peerId.GetNodeId()), peerId.GetFabricIndex(), error.Format()); + _interactionContext->promise->set_value(-1); }); } else { + ChipLogError(AppServer, "ContentLauncherCluster::LaunchURL failure in retrieving Endpoint"); + promise->set_value(-1); } - - return CHIP_NO_ERROR; + return promise->get_future(); } }; // namespace clusters diff --git a/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.h b/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.h index e910c6f8d250ae..20e2f704b4223c 100644 --- a/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.h +++ b/examples/tv-casting-app/tv-casting-common/clusters/ContentLauncherCluster.h @@ -23,6 +23,9 @@ #include "lib/support/logging/CHIPLogging.h" +#include +#include + namespace matter { namespace casting { namespace clusters { @@ -31,6 +34,8 @@ struct LaunchURLContext { memory::Weak endpoint; chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request; + // chip::app::Clusters::ContentLauncher::Commands::LauncherResponse::DecodableType responseType + std::promise * promise; }; class ContentLauncherCluster : public core::BaseCluster @@ -40,7 +45,7 @@ class ContentLauncherCluster : public core::BaseCluster public: ContentLauncherCluster(memory::Weak endpoint) : core::BaseCluster(endpoint) {} - CHIP_ERROR LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request); + std::future LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request); }; }; // namespace clusters