Skip to content

Commit

Permalink
Demo functions
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 21, 2023
1 parent 3186f88 commit faaa13f
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 77 deletions.
167 changes: 90 additions & 77 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,98 @@ 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 InvokeContentLauncherLaunchURL(matter::casting::memory::Strong<matter::casting::core::Endpoint> endpoint)
{
ChipLogProgress(AppServer, "OnLaunchURLSuccess with response: %.*s", static_cast<int>(response.data.Value().size()),
response.data.Value().data());
}
matter::casting::memory::Strong<matter::casting::clusters::content_launcher::ContentLauncherCluster> contentLauncherCluster =
endpoint->GetCluster<matter::casting::clusters::content_launcher::ContentLauncherCluster>();
VerifyOrReturn(contentLauncherCluster != nullptr);

void OnLaunchURLFailure(void * context, CHIP_ERROR error)
{
ChipLogError(AppServer, "OnLaunchURLFailure with err %" CHIP_ERROR_FORMAT, error.Format());
matter::casting::core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> * launchURLCommand =
static_cast<matter::casting::core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> *>(
contentLauncherCluster->GetCommand(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Id));
VerifyOrReturn(launchURLCommand != nullptr, ChipLogError(AppServer, "LaunchURL command not found on ContentLauncherCluster"));

chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request;
request.contentURL = chip::CharSpan::fromCharString(kContentURL);
request.displayString = chip::Optional<chip::CharSpan>(chip::CharSpan::fromCharString(kContentDisplayStr));
request.brandingInformation =
chip::MakeOptional(chip::app::Clusters::ContentLauncher::Structs::BrandingInformationStruct::Type());

launchURLCommand->Invoke(
request, nullptr,
[](void * context, const chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType & response) {
ChipLogProgress(AppServer, "LaunchURL Success with response.data: %.*s", static_cast<int>(response.data.Value().size()),
response.data.Value().data());
},
[](void * context, CHIP_ERROR error) {
ChipLogError(AppServer, "LaunchURL Failure with err %" CHIP_ERROR_FORMAT, error.Format());
},
chip::MakeOptional(kTimedInvokeCommandTimeoutMs));
}

void OnCurrentStateReadSuccess(
void * context, chip::Optional<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo::DecodableArgType> before,
chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo::DecodableArgType after)
void ReadApplicationBasicVendorID(matter::casting::memory::Strong<matter::casting::core::Endpoint> endpoint)
{
ChipLogProgress(AppServer, "OnCurrentStateReadSuccess");
/* ChipLogProgress(AppServer, "OnCurrentStateReadSuccess with response: %.*s",
static_cast<int>(response.data.Value().size()), response.data.Value().data());*/
matter::casting::memory::Strong<matter::casting::clusters::application_basic::ApplicationBasicCluster> applicationBasicCluster =
endpoint->GetCluster<matter::casting::clusters::application_basic::ApplicationBasicCluster>();
VerifyOrReturn(applicationBasicCluster != nullptr);

matter::casting::core::Attribute<chip::app::Clusters::ApplicationBasic::Attributes::VendorID::TypeInfo> * vendorIDAttribute =
static_cast<matter::casting::core::Attribute<chip::app::Clusters::ApplicationBasic::Attributes::VendorID::TypeInfo> *>(
applicationBasicCluster->GetAttribute(chip::app::Clusters::ApplicationBasic::Attributes::VendorID::Id));
VerifyOrReturn(vendorIDAttribute != nullptr,
ChipLogError(AppServer, "VendorID attribute not found on ApplicationBasicCluster"));

vendorIDAttribute->Read(
nullptr,
[](void * context,
chip::Optional<chip::app::Clusters::ApplicationBasic::Attributes::VendorID::TypeInfo::DecodableArgType> before,
chip::app::Clusters::ApplicationBasic::Attributes::VendorID::TypeInfo::DecodableArgType after) {
if (before.HasValue())
{
ChipLogProgress(AppServer, "Read VendorID value: %d [Before reading value: %d]", after, before.Value());
}
else
{
ChipLogProgress(AppServer, "Read VendorID value: %d", after);
}
},
[](void * context, CHIP_ERROR error) {
ChipLogError(AppServer, "VendorID Read failure with err %" CHIP_ERROR_FORMAT, error.Format());
});
}

void OnCurrentStateReadFailure(void * context, CHIP_ERROR error)
void SubscribeToMediaPlaybackCurrentState(matter::casting::memory::Strong<matter::casting::core::Endpoint> endpoint)
{
ChipLogError(AppServer, "OnCurrentStateReadFailure with err %" CHIP_ERROR_FORMAT, error.Format());
matter::casting::memory::Strong<matter::casting::clusters::media_playback::MediaPlaybackCluster> mediaPlaybackCluster =
endpoint->GetCluster<matter::casting::clusters::media_playback::MediaPlaybackCluster>();
VerifyOrReturn(mediaPlaybackCluster != nullptr);

matter::casting::core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> *
currentStateAttribute =
static_cast<matter::casting::core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> *>(
mediaPlaybackCluster->GetAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id));
VerifyOrReturn(currentStateAttribute != nullptr,
ChipLogError(AppServer, "CurrentState attribute not found on MediaPlaybackCluster"));

currentStateAttribute->Subscribe(
nullptr,
[](void * context,
chip::Optional<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo::DecodableArgType> before,
chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo::DecodableArgType after) {
if (before.HasValue())
{
ChipLogProgress(AppServer, "Read CurrentState value: %d [Before reading value: %d]", static_cast<int>(after),
static_cast<int>(before.Value()));
}
else
{
ChipLogProgress(AppServer, "Read CurrentState value: %d", static_cast<int>(after));
}
},
[](void * context, CHIP_ERROR error) {
ChipLogError(AppServer, "CurrentState Read failure with err %" CHIP_ERROR_FORMAT, error.Format());
},
kMinIntervalFloorSeconds, kMaxIntervalCeilingSeconds);
}

void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * castingPlayer)
Expand All @@ -106,69 +174,14 @@ void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * ca
{
unsigned index = (unsigned int) std::distance(endpoints.begin(), it);

// invoke a command
matter::casting::memory::Strong<matter::casting::clusters::content_launcher::ContentLauncherCluster>
contentLauncherCluster =
endpoints[index]->GetCluster<matter::casting::clusters::content_launcher::ContentLauncherCluster>();
if (contentLauncherCluster != nullptr)
{
matter::casting::core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> * launchURL =
static_cast<matter::casting::core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> *>(
contentLauncherCluster->GetCommand(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Id));
// demonstrate invoking a command
InvokeContentLauncherLaunchURL(endpoints[index]);

if (launchURL != nullptr)
{
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());
launchURL->Invoke(request, nullptr, OnLaunchURLSuccess, OnLaunchURLFailure,
chip::MakeOptional(static_cast<unsigned short>(5 * 1000)));
}
else
{
ChipLogProgress(AppServer, "launchURL was nullpr");
}
}
// demonstrate reading an attribute
ReadApplicationBasicVendorID(endpoints[index]);

// read an attribute
/*matter::casting::memory::Strong<matter::casting::clusters::media_playback::MediaPlaybackCluster> mediaPlaybackCluster =
endpoints[index]->GetCluster<matter::casting::clusters::media_playback::MediaPlaybackCluster>();
if (mediaPlaybackCluster != nullptr)
{
matter::casting::core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> *
currentStateAttribute = static_cast<
matter::casting::core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> *>(
mediaPlaybackCluster->GetAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id));
if (currentStateAttribute != nullptr)
{
currentStateAttribute->Read(nullptr, OnCurrentStateReadSuccess, OnCurrentStateReadFailure);
}
else
{
ChipLogProgress(AppServer, "currentStateAttribute was nullpr");
}
}*/

// subscribe to an attribute
matter::casting::memory::Strong<matter::casting::clusters::media_playback::MediaPlaybackCluster> mediaPlaybackCluster =
endpoints[index]->GetCluster<matter::casting::clusters::media_playback::MediaPlaybackCluster>();
if (mediaPlaybackCluster != nullptr)
{
matter::casting::core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> *
currentStateAttribute = static_cast<
matter::casting::core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> *>(
mediaPlaybackCluster->GetAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id));
if (currentStateAttribute != nullptr)
{
currentStateAttribute->Subscribe(nullptr, OnCurrentStateReadSuccess, OnCurrentStateReadFailure, 0, 1);
}
else
{
ChipLogProgress(AppServer, "currentStateAttribute was nullpr");
}
}
// demonstrate subscribing to an attribute
SubscribeToMediaPlaybackCurrentState(endpoints[index]);
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions examples/tv-casting-app/linux/simple-app-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
*/
const uint64_t kTargetPlayerDeviceType = 35;

const char kContentURL[] = "https://www.test.com/videoid";
const char kContentDisplayStr[] = "Test video";
const unsigned short kTimedInvokeCommandTimeoutMs = 5 * 1000;
const uint16_t kMinIntervalFloorSeconds = 0;
const uint16_t kMaxIntervalCeilingSeconds = 1;

/**
* @brief Singleton that reacts to CastingPlayer discovery results
*/
Expand Down

0 comments on commit faaa13f

Please sign in to comment.