Skip to content

Commit

Permalink
Fixed bugs with read attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 20, 2023
1 parent b144bbb commit c2eac3d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 17 deletions.
32 changes: 23 additions & 9 deletions examples/tv-casting-app/linux/simple-app-helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<matter::casting::clusters::content_launcher::ContentLauncherCluster> cluster =
endpoints[index]->GetCluster<matter::casting::clusters::content_launcher::ContentLauncherCluster>();
if (cluster != nullptr)

// 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)
{
// 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>(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)));
contentLauncherCluster->LaunchURL(request, nullptr, OnLaunchURLSuccess, OnLaunchURLFailure,
chip::MakeOptional(static_cast<unsigned short>(5 * 1000)));
}

// read an attribute
// 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::clusters::media_playback::CurrentState * currentStateAttribute =
static_cast<matter::casting::clusters::media_playback::CurrentState *>(
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,20 @@ class CurrentState : public core::Attribute<chip::app::Clusters::MediaPlayback::
{}
};

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

void RegisterAttributes()
{
memory::Weak<MediaPlaybackCluster> 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<MediaPlaybackCluster> 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
Expand Down
8 changes: 5 additions & 3 deletions examples/tv-casting-app/tv-casting-common/core/Cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Endpoint> GetEndpoint() const { return mEndpoint.lock(); }

void * GetAttribute(const chip::AttributeId attributeId) { return mAttributes[attributeId]; }
virtual void RegisterAttributes() {}

memory::Weak<Endpoint> 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<Endpoint> mEndpoint;

private:
Expand Down
3 changes: 2 additions & 1 deletion examples/tv-casting-app/tv-casting-common/core/Endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
void RegisterCluster(const chip::ClusterId clusterId)
{
static_assert(std::is_base_of<BaseCluster, T>::value, "T must be derived from BaseCluster");
auto cluster = std::make_shared<T>(shared_from_this());
auto cluster = std::make_shared<T>(shared_from_this());
cluster->RegisterAttributes();
mClusters[clusterId] = std::static_pointer_cast<BaseCluster>(cluster);
}

Expand Down

0 comments on commit c2eac3d

Please sign in to comment.