From c2eac3db56997ba96e476fd6284fc91570dd96f4 Mon Sep 17 00:00:00 2001 From: Sharad Binjola Date: Wed, 20 Dec 2023 14:13:07 -0800 Subject: [PATCH] Fixed bugs with read attribute --- .../linux/simple-app-helper.cpp | 32 +++++++++++++------ .../clusters/MediaPlaybackCluster.h | 15 ++++++--- .../tv-casting-common/core/Cluster.h | 8 +++-- .../tv-casting-common/core/Endpoint.h | 3 +- 4 files changed, 41 insertions(+), 17 deletions(-) diff --git a/examples/tv-casting-app/linux/simple-app-helper.cpp b/examples/tv-casting-app/linux/simple-app-helper.cpp index 35e20b09ca6294..20b9809e65a6ae 100644 --- a/examples/tv-casting-app/linux/simple-app-helper.cpp +++ b/examples/tv-casting-app/linux/simple-app-helper.cpp @@ -106,24 +106,38 @@ void ConnectionHandler(CHIP_ERROR err, matter::casting::core::CastingPlayer * ca if (it != endpoints.end()) { unsigned index = (unsigned int) std::distance(endpoints.begin(), it); - matter::casting::memory::Strong cluster = - endpoints[index]->GetCluster(); - if (cluster != nullptr) + + // invoke a command + matter::casting::memory::Strong + contentLauncherCluster = + endpoints[index]->GetCluster(); + if (contentLauncherCluster != nullptr) { - // invoke a command chip::app::Clusters::ContentLauncher::Commands::LaunchURL::Type request; request.contentURL = chip::CharSpan::fromCharString("https://www.test.com/videoid"); request.displayString = chip::Optional(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(5 * 1000))); + contentLauncherCluster->LaunchURL(request, nullptr, OnLaunchURLSuccess, OnLaunchURLFailure, + chip::MakeOptional(static_cast(5 * 1000))); + } - // read an attribute + // read an attribute + matter::casting::memory::Strong mediaPlaybackCluster = + endpoints[index]->GetCluster(); + if (mediaPlaybackCluster != nullptr) + { matter::casting::clusters::media_playback::CurrentState * currentStateAttribute = static_cast( - cluster->GetAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id)); - currentStateAttribute->Read(nullptr, OnCurrentStateReadSuccess, OnCurrentStateReadFailure); + mediaPlaybackCluster->GetAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id)); + if (currentStateAttribute != nullptr) + { + currentStateAttribute->Read(nullptr, OnCurrentStateReadSuccess, OnCurrentStateReadFailure); + } + else + { + ChipLogProgress(AppServer, "currentStateAttribute was nullpr"); + } } } else diff --git a/examples/tv-casting-app/tv-casting-common/clusters/MediaPlaybackCluster.h b/examples/tv-casting-app/tv-casting-common/clusters/MediaPlaybackCluster.h index 721309970e2000..3de3e02cda43d3 100644 --- a/examples/tv-casting-app/tv-casting-common/clusters/MediaPlaybackCluster.h +++ b/examples/tv-casting-app/tv-casting-common/clusters/MediaPlaybackCluster.h @@ -41,13 +41,20 @@ class CurrentState : public core::Attribute +class MediaPlaybackCluster : public core::BaseCluster, public std::enable_shared_from_this { public: - MediaPlaybackCluster(memory::Weak endpoint) : core::BaseCluster(endpoint) + MediaPlaybackCluster(memory::Weak endpoint) : core::BaseCluster(endpoint) {} + + void RegisterAttributes() { - memory::Weak cluster = shared_from_this(); - RegisterAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id, new CurrentState(cluster)); + ChipLogProgress(AppServer, "MediaPlaybackCluster before cluster = shared_from_this()"); + std::shared_ptr cluster = shared_from_this(); + ChipLogProgress(AppServer, "MediaPlaybackCluster after cluster = shared_from_this()"); + CurrentState * currentState = new CurrentState(cluster); + ChipLogProgress(AppServer, "MediaPlaybackCluster currentState == nullptr: %d", currentState == nullptr); + RegisterAttribute(chip::app::Clusters::MediaPlayback::Attributes::CurrentState::Id, currentState); + ChipLogProgress(AppServer, "MediaPlaybackCluster After RegisterAttribute"); } // TODO: add commands diff --git a/examples/tv-casting-app/tv-casting-common/core/Cluster.h b/examples/tv-casting-app/tv-casting-common/core/Cluster.h index 993596f2570973..ce1a2fe3b7c4ff 100644 --- a/examples/tv-casting-app/tv-casting-common/core/Cluster.h +++ b/examples/tv-casting-app/tv-casting-common/core/Cluster.h @@ -48,13 +48,15 @@ class BaseCluster BaseCluster(BaseCluster & other) = delete; void operator=(const BaseCluster &) = delete; - void RegisterAttribute(const chip::AttributeId attributeId, void * attribute) { mAttributes[attributeId] = attribute; } + memory::Weak GetEndpoint() const { return mEndpoint.lock(); } - void * GetAttribute(const chip::AttributeId attributeId) { return mAttributes[attributeId]; } + virtual void RegisterAttributes() {} - memory::Weak GetEndpoint() const { return mEndpoint.lock(); } + void * GetAttribute(const chip::AttributeId attributeId) { return mAttributes[attributeId]; } protected: + void RegisterAttribute(const chip::AttributeId attributeId, void * attribute) { mAttributes[attributeId] = attribute; } + memory::Weak mEndpoint; private: diff --git a/examples/tv-casting-app/tv-casting-common/core/Endpoint.h b/examples/tv-casting-app/tv-casting-common/core/Endpoint.h index 9009db959a4d9c..179c8a403147c7 100644 --- a/examples/tv-casting-app/tv-casting-common/core/Endpoint.h +++ b/examples/tv-casting-app/tv-casting-common/core/Endpoint.h @@ -120,7 +120,8 @@ class Endpoint : public std::enable_shared_from_this void RegisterCluster(const chip::ClusterId clusterId) { static_assert(std::is_base_of::value, "T must be derived from BaseCluster"); - auto cluster = std::make_shared(shared_from_this()); + auto cluster = std::make_shared(shared_from_this()); + cluster->RegisterAttributes(); mClusters[clusterId] = std::static_pointer_cast(cluster); }