Skip to content

Commit

Permalink
Removed redundant command types
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 21, 2023
1 parent 950b8cf commit bd40f38
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 44 deletions.
36 changes: 24 additions & 12 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ void OnLaunchURLFailure(void * context, CHIP_ERROR error)
ChipLogError(AppServer, "OnLaunchURLFailure with err %" CHIP_ERROR_FORMAT, error.Format());
}

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 OnCurrentStateReadSuccess(
void * context, chip::Optional<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo::DecodableArgType> before,
chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo::DecodableArgType after)
{
ChipLogProgress(AppServer, "OnCurrentStateReadSuccess");
/* ChipLogProgress(AppServer, "OnCurrentStateReadSuccess with response: %.*s",
Expand Down Expand Up @@ -113,22 +113,34 @@ void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * ca
endpoints[index]->GetCluster<matter::casting::clusters::content_launcher::ContentLauncherCluster>();
if (contentLauncherCluster != 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());
contentLauncherCluster->LaunchURL(request, nullptr, OnLaunchURLSuccess, OnLaunchURLFailure,
chip::MakeOptional(static_cast<unsigned short>(5 * 1000)));
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));

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");
}
}

// 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> *>(
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)
{
Expand Down
2 changes: 1 addition & 1 deletion examples/tv-casting-app/tv-casting-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ chip_data_model("tv-casting-common") {

# Add simplified casting API files here
sources += [
"clusters/ClusterTemplates.h",
"clusters/ContentLauncherCluster.cpp",
"clusters/ContentLauncherCluster.h",
"clusters/MediaPlaybackCluster.h",
Expand All @@ -106,6 +105,7 @@ chip_data_model("tv-casting-common") {
"core/CastingPlayerDiscovery.cpp",
"core/CastingPlayerDiscovery.h",
"core/Cluster.h",
"core/Command.h",
"core/Endpoint.h",
"core/Types.h",
"support/AppParameters.h",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ namespace matter {
namespace casting {
namespace clusters {
namespace content_launcher {

/*
void ContentLauncherCluster::LaunchURL(
chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> successCb,
FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs)
core::SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> successCb,
core::FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs)
{
Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> command(this->GetEndpoint());
core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> command(this->GetEndpoint());
command.Invoke(request, context, successCb, failureCb, timedInvokeTimeoutMs);
}
}*/

}; // namespace content_launcher
}; // namespace clusters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#pragma once

#include "clusters/ClusterTemplates.h"
#include "core/Command.h"
#include "core/Endpoint.h"
#include "core/Types.h"

Expand All @@ -29,14 +29,26 @@ namespace casting {
namespace clusters {
namespace content_launcher {

class ContentLauncherCluster : public core::BaseCluster
class ContentLauncherCluster : public core::BaseCluster, public std::enable_shared_from_this<ContentLauncherCluster>
{
public:
ContentLauncherCluster(memory::Weak<core::Endpoint> endpoint) : core::BaseCluster(endpoint) {}

void LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType> successCb,
FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs);
void SetUp()
{
ChipLogProgress(AppServer, "ContentLauncherCluster before cluster = shared_from_this()");
std::shared_ptr<ContentLauncherCluster> cluster = shared_from_this();
ChipLogProgress(AppServer, "ContentLauncherCluster after cluster = shared_from_this()");
core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type> * launchURL =
new core::Command<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type>(cluster->GetEndpoint());
ChipLogProgress(AppServer, "ContentLauncherCluster launchURL == nullptr: %d", launchURL == nullptr);
RegisterCommand(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Id, launchURL);
ChipLogProgress(AppServer, "ContentLauncherCluster After RegisterAttribute");
}

/*void LaunchURL(chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request, void * context,
core::SuccessCallbackType<chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type::ResponseType>
successCb, core::FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs);*/
};

}; // namespace content_launcher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ class MediaPlaybackCluster : public core::BaseCluster, public std::enable_shared
public:
MediaPlaybackCluster(memory::Weak<core::Endpoint> endpoint) : core::BaseCluster(endpoint) {}

void RegisterAttributes()
void SetUp()
{
ChipLogProgress(AppServer, "MediaPlaybackCluster before cluster = shared_from_this()");
std::shared_ptr<MediaPlaybackCluster> cluster = shared_from_this();
ChipLogProgress(AppServer, "MediaPlaybackCluster after cluster = shared_from_this()");
core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> * currentState = new core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo>(cluster);
core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo> * currentState =
new core::Attribute<chip::app::Clusters::MediaPlayback::Attributes::CurrentState::TypeInfo>(cluster);
ChipLogProgress(AppServer, "MediaPlaybackCluster currentState == nullptr: %d", currentState == nullptr);
RegisterAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id, currentState);
ChipLogProgress(AppServer, "MediaPlaybackCluster After RegisterAttribute");
Expand Down
34 changes: 22 additions & 12 deletions examples/tv-casting-app/tv-casting-common/core/Attribute.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "Cluster.h"
#include "Types.h"

#include "clusters/ClusterTemplates.h"
#include "core/Command.h"

#include "lib/support/logging/CHIPLogging.h"
namespace matter {
Expand All @@ -31,15 +31,16 @@ namespace core {
using WriteAttributeCallback = std::function<void(void * context, CHIP_ERROR)>;

template <typename TypeInfoDecodableArgType>
using ReadResponseSuccessCallbackFn = std::function<void(void * context, chip::Optional<TypeInfoDecodableArgType> before, TypeInfoDecodableArgType after)>;
using ReadResponseSuccessCallbackFn =
std::function<void(void * context, chip::Optional<TypeInfoDecodableArgType> before, TypeInfoDecodableArgType after)>;
using ReadResponseFailureCallbackFn = std::function<void(void * context, CHIP_ERROR err)>;

template <typename TypeInfoDecodableArgType>
struct ReadAttributeContext
{
ReadAttributeContext(void * attribute, memory::Strong<core::Endpoint> endpoint, void * clientContext,
ReadResponseSuccessCallbackFn<TypeInfoDecodableArgType> successCb,
ReadResponseFailureCallbackFn failureCb, bool aIsFabricFiltered) :
ReadResponseSuccessCallbackFn<TypeInfoDecodableArgType> successCb, ReadResponseFailureCallbackFn failureCb,
bool aIsFabricFiltered) :
mEndpoint(endpoint),
mClientContext(clientContext), mSuccessCb(successCb), mFailureCb(failureCb), mAIsFabricFiltered(aIsFabricFiltered)
{
Expand Down Expand Up @@ -76,7 +77,10 @@ class Attribute
Attribute(Attribute & other) = delete;
void operator=(const Attribute &) = delete;

chip::Optional<typename TypeInfo::DecodableArgType> GetValue() { return hasValue ? chip::MakeOptional(value) : chip::NullOptional; }
chip::Optional<typename TypeInfo::DecodableArgType> GetValue()
{
return hasValue ? chip::MakeOptional(value) : chip::NullOptional;
}

void Read(void * context, ReadResponseSuccessCallbackFn<typename TypeInfo::DecodableArgType> successCb,
ReadResponseFailureCallbackFn failureCb, bool aIsFabricFiltered = true)
Expand All @@ -85,13 +89,15 @@ class Attribute
if (endpoint)
{
ReadAttributeContext<typename TypeInfo::DecodableArgType> * readAttributeContext =
new ReadAttributeContext<typename TypeInfo::DecodableArgType>(this, endpoint, context, successCb, failureCb, aIsFabricFiltered);
new ReadAttributeContext<typename TypeInfo::DecodableArgType>(this, endpoint, context, successCb, failureCb,
aIsFabricFiltered);

endpoint->GetCastingPlayer()->FindOrEstablishSession(
readAttributeContext,
// FindOrEstablishSession success handler
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext = static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext =
static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ChipLogProgress(AppServer, "<Attribute>::Read() Found or established session");

// Read attribute
Expand All @@ -101,10 +107,12 @@ class Attribute
_readAttributeContext,
// Read success handler
[](void * context, typename TypeInfo::DecodableArgType response) {
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext = static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext =
static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ChipLogProgress(AppServer, "<Attribute>::Read() success");
Attribute<TypeInfo> * _attribute = static_cast<Attribute<TypeInfo> *>(_readAttributeContext->mAttribute);
_attribute->value = response;
Attribute<TypeInfo> * _attribute =
static_cast<Attribute<TypeInfo> *>(_readAttributeContext->mAttribute);
_attribute->value = response;
if (_attribute->hasValue)
{
_readAttributeContext->mSuccessCb(_readAttributeContext->mClientContext,
Expand All @@ -120,7 +128,8 @@ class Attribute
},
// Read failure handler
[](void * context, CHIP_ERROR error) {
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext = static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext =
static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ChipLogError(AppServer,
"<Attribute>::Read() failure response on EndpointId: %d with error: "
"%" CHIP_ERROR_FORMAT,
Expand All @@ -143,7 +152,8 @@ class Attribute
},
// FindOrEstablishSession failure handler
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext = static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ReadAttributeContext<typename TypeInfo::DecodableArgType> * _readAttributeContext =
static_cast<ReadAttributeContext<typename TypeInfo::DecodableArgType> *>(context);
ChipLogError(AppServer,
"<Attribute>::Read() failure in retrieving session info for peerId.nodeId: "
"0x" ChipLogFormatX64 ", peer.fabricIndex: %d with error: %" CHIP_ERROR_FORMAT,
Expand Down
11 changes: 7 additions & 4 deletions examples/tv-casting-app/tv-casting-common/core/Cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,23 @@ class BaseCluster

memory::Weak<Endpoint> GetEndpoint() const { return mEndpoint.lock(); }

virtual void RegisterAttributes() {}
virtual void SetUp() {}

void * GetAttribute(const chip::AttributeId attributeId) { return mAttributes[attributeId]; }
void * GetCommand(const chip::CommandId commandId) { return mCommands[commandId]; }

/*template <typename RequestType>
void InvokeCommand(RequestType request, void * context, clusters::SuccessCallbackType<typename RequestType::ResponseType> successCb,
clusters::FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs);*/
/*template <typename RequestType>
void InvokeCommand(RequestType request, void * context, clusters::SuccessCallbackType<typename RequestType::ResponseType>
successCb, clusters::FailureCallbackType failureCb, const chip::Optional<uint16_t> & timedInvokeTimeoutMs);*/

protected:
void RegisterAttribute(const chip::AttributeId attributeId, void * attribute) { mAttributes[attributeId] = attribute; }
void RegisterCommand(const chip::CommandId commandId, void * command) { mCommands[commandId] = command; }

memory::Weak<Endpoint> mEndpoint;

private:
std::map<chip::CommandId, void *> mCommands;
std::map<chip::AttributeId, void *> mAttributes;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

namespace matter {
namespace casting {
namespace clusters {
namespace core {

template <typename ResponseType>
using SuccessCallbackType = std::function<void(void * context, const ResponseType & responseObject)>;
Expand Down Expand Up @@ -134,6 +134,6 @@ class Command
memory::Weak<core::Endpoint> mEndpoint;
};

}; // namespace clusters
}; // namespace core
}; // namespace casting
}; // namespace matter
2 changes: 1 addition & 1 deletion examples/tv-casting-app/tv-casting-common/core/Endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
{
static_assert(std::is_base_of<BaseCluster, T>::value, "T must be derived from BaseCluster");
auto cluster = std::make_shared<T>(shared_from_this());
cluster->RegisterAttributes();
cluster->SetUp();
mClusters[clusterId] = std::static_pointer_cast<BaseCluster>(cluster);
}

Expand Down

0 comments on commit bd40f38

Please sign in to comment.