Skip to content

Commit

Permalink
Example code
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 13, 2023
1 parent a544fdc commit 3ec4b42
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 9 deletions.
28 changes: 27 additions & 1 deletion examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,33 @@ void DiscoveryDelegateImpl::HandleOnUpdated(matter::casting::memory::Strong<matt

void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer)
{
ChipLogProgress(AppServer, "ConnectionHandler called with %" CHIP_ERROR_FORMAT, err.Format());
if (err == CHIP_NO_ERROR)
{
ChipLogProgress(AppServer, "ConnectionHandler: Successfully connected to CastingPlayer(ID: %s)", castingPlayer->GetId());
std::vector<matter::casting::memory::Strong<matter::casting::core::Endpoint>> 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<matter::casting::core::Endpoint> & 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,61 @@ namespace matter {
namespace casting {
namespace clusters {

CHIP_ERROR ContentLauncherCluster::LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request)
std::future<int> ContentLauncherCluster::LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request)
{

std::promise<int> * promise = new std::promise<int>();
memory::Strong<core::Endpoint> 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<LaunchURLContext *>(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<LaunchURLContext *>(context);
ChipLogProgress(AppServer, "ContentLauncherCluster::LaunchURL success");
_interactionContext->promise->set_value(42);
},
[](void * context, CHIP_ERROR error) {
LaunchURLContext * _interactionContext = static_cast<LaunchURLContext *>(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<LaunchURLContext *>(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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@

#include "lib/support/logging/CHIPLogging.h"

#include <future>
#include <iostream>

namespace matter {
namespace casting {
namespace clusters {
Expand All @@ -31,6 +34,8 @@ struct LaunchURLContext
{
memory::Weak<core::Endpoint> endpoint;
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request;
// chip::app::Clusters::ContentLauncher::Commands::LauncherResponse::DecodableType responseType
std::promise<int> * promise;
};

class ContentLauncherCluster : public core::BaseCluster
Expand All @@ -40,7 +45,7 @@ class ContentLauncherCluster : public core::BaseCluster
public:
ContentLauncherCluster(memory::Weak<core::Endpoint> endpoint) : core::BaseCluster(endpoint) {}

CHIP_ERROR LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request);
std::future<int> LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request);
};

}; // namespace clusters
Expand Down

0 comments on commit 3ec4b42

Please sign in to comment.