Skip to content

Commit

Permalink
Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 14, 2023
1 parent bd72bba commit 44c8527
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 48 deletions.
52 changes: 32 additions & 20 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,34 +61,46 @@ void DiscoveryDelegateImpl::HandleOnUpdated(matter::casting::memory::Strong<matt
ChipLogProgress(AppServer, "Updated CastingPlayer with ID: %s", player->GetId());
}

void OnLaunchURLSuccess(void * context,
const chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType & response)
{}

void OnLaunchURLFailure(void * context, CHIP_ERROR error) {}

void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer)
{
if (err == CHIP_NO_ERROR)
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogProgress(AppServer,
"ConnectionHandler: Failed to connect to CastingPlayer(ID: %s) with err %" CHIP_ERROR_FORMAT,
castingPlayer->GetId(), err.Format()));

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) {
return endpoint->GetVendorId() == 65521;
});
if (it != endpoints.end())
{
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
unsigned index = (unsigned int) std::distance(endpoints.begin(), it);
matter::casting::memory::Strong<matter::casting::clusters::ContentLauncherCluster> cluster =
endpoints[index]->GetCluster<matter::casting::clusters::ContentLauncherCluster>();
if (cluster != nullptr)
{
ChipLogError(AppServer, "Desired Endpoint not found on the CastingPlayer(ID: %s)", castingPlayer->GetId());
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request;
request.contentURL = chip::CharSpan::fromCharString("https://www.test.com/videoid");
request.displayString = chip::Optional<chip::CharSpan>(chip::CharSpan::fromCharString("Test video"));
request.brandingInformation =
chip::MakeOptional(chip::app::Clusters::ContentLauncher::Structs::BrandingInformationStruct::Type());
cluster->LaunchURL(request, nullptr, OnLaunchURLSuccess, OnLaunchURLFailure,
chip::MakeOptional(static_cast<unsigned short>(5 * 1000)));
}
}
else
{
ChipLogProgress(AppServer, "ConnectionHandler: Failed to connect to CastingPlayer(ID: %s) with err %" CHIP_ERROR_FORMAT,
castingPlayer->GetId(), err.Format());
ChipLogError(AppServer, "Desired Endpoint not found on the CastingPlayer(ID: %s)", castingPlayer->GetId());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,61 +22,67 @@ namespace matter {
namespace casting {
namespace clusters {

std::future<int> ContentLauncherCluster::LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request)
void ContentLauncherCluster::LaunchURL(
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
chip::Controller::CommandResponseSuccessCallback<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType>
successCb,
chip::Controller::CommandResponseFailureCallback failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs)
{
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->promise = promise;
LaunchURLContext * interactionContext =
new LaunchURLContext(endpoint, request, context, successCb, failureCb, timedInvokeTimeoutMs);

endpoint->GetCastingPlayer()->FindOrEstablishSession(
interactionContext,
// FindOrEstablishSession success handler
[](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());
ChipLogProgress(AppServer, "ContentLauncherCluster::LaunchURL found session info");
support::MediaClusterBase cluster(exchangeMgr, sessionHandle, _interactionContext->mEndpoint->GetId());
CHIP_ERROR err = cluster.template InvokeCommand(
_interactionContext->request, _interactionContext,
_interactionContext->mRequest, _interactionContext,
// Interaction success handler
[](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);
_interactionContext->mSuccessCb(_interactionContext->mClientContext, response);
},
// Interaction failure handler
[](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);
_interactionContext->mEndpoint->GetId(), error.Format());
_interactionContext->mFailureCb(_interactionContext->mClientContext, error);
});
// error in starting the interaction
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);
_interactionContext->mEndpoint->GetId(), err.Format());
_interactionContext->mFailureCb(_interactionContext->mClientContext, err);
}
},
// FindOrEstablishSession failure handler
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
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);
_interactionContext->mFailureCb(_interactionContext->mClientContext, error);
});
}
else
{
ChipLogError(AppServer, "ContentLauncherCluster::LaunchURL failure in retrieving Endpoint");
promise->set_value(-1);
failureCb(context, CHIP_ERROR_INCORRECT_STATE);
}
return promise->get_future();
}

}; // namespace clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,50 @@

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

#include <future>
#include <iostream>

namespace matter {
namespace casting {
namespace clusters {

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
{
private:
protected:
public:
ContentLauncherCluster(memory::Weak<core::Endpoint> endpoint) : core::BaseCluster(endpoint) {}

std::future<int> LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request);
void LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
chip::Controller::CommandResponseSuccessCallback<
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType>
successCb,
chip::Controller::CommandResponseFailureCallback failureCb,
const chip::Optional<uint16_t> & timedInvokeTimeoutMs);
};

struct LaunchURLContext
{
LaunchURLContext(memory::Strong<core::Endpoint> endpoint,
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
chip::Controller::CommandResponseSuccessCallback<
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType>
successCb,
chip::Controller::CommandResponseFailureCallback failureCb,
const chip::Optional<uint16_t> & timedInvokeTimeoutMs)
{
mEndpoint = endpoint;
mRequest = request;
mClientContext = context;
mSuccessCb = successCb;
mFailureCb = failureCb;
mTimedInvokeTimeoutMs = timedInvokeTimeoutMs;
}

memory::Strong<core::Endpoint> mEndpoint;
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type mRequest;
void * mClientContext;
chip::Controller::CommandResponseSuccessCallback<
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> * mSuccessCb;
chip::Controller::CommandResponseFailureCallback * mFailureCb;
chip::Optional<uint16_t> mTimedInvokeTimeoutMs;
};

}; // namespace clusters
Expand Down

0 comments on commit 44c8527

Please sign in to comment.