Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Nov 30, 2023
1 parent b3ef405 commit 8e4f7cf
Show file tree
Hide file tree
Showing 5 changed files with 277 additions and 2 deletions.
4 changes: 4 additions & 0 deletions examples/tv-casting-app/tv-casting-common/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,16 @@ chip_data_model("tv-casting-common") {

# Add simplified casting API files here
sources += [
#"core/Attribute.h",
"core/CastingApp.cpp",
"core/CastingApp.h",
"core/CastingPlayer.cpp",
"core/CastingPlayer.h",
"core/CastingPlayerDiscovery.cpp",
"core/CastingPlayerDiscovery.h",
"core/Cluster.h",

#"core/Endpoint.h",
"core/Types.h",
"support/AppParameters.h",
"support/CastingStore.cpp",
Expand Down
76 changes: 76 additions & 0 deletions examples/tv-casting-app/tv-casting-common/core/Attribute.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Types.h"
#include "Cluster.h"

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

namespace matter {
namespace casting {
namespace core {

enum ReadAttributeError
{
READ_ATTRIBUTE_NO_ERROR
};

enum WriteAttributeError
{
WRITE_ATTRIBUTE_NO_ERROR
};

template <typename ValueType>
using ReadAttributeCallback = std::function<void(Optional<ValueType> before, ValueType after, ReadAttributeError)>;

using WriteAttributeCallback = std::function<void(WriteAttributeError)>;

class BaseCluster;

template <typename ValueType>
class Attribute
{
private:
memory::Weak<BaseCluster> cluster;
ValueType value;

public:
Attribute(memory::Weak<BaseCluster> cluster) { this->cluster = cluster; }

~Attribute() {}

Attribute() = delete;
Attribute(Attribute & other) = delete;
void operator=(const Attribute &) = delete;

protected:
memory::Strong<BaseCluster> GetCluster() const { return cluster.lock(); }

public:
ValueType GetValue();
void Read(ReadAttributeCallback<ValueType> onRead);
void Write(ValueType value, WriteAttributeCallback onWrite);
bool SubscribeAttribute(AttributeId attributeId, ReadAttributeCallback<ValueType> callback);
bool UnsubscribeAttribute(AttributeId attributeId, ReadAttributeCallback<ValueType> callback);
};

}; // namespace core
}; // namespace casting
}; // namespace matter
37 changes: 35 additions & 2 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@
*/

#include "CastingPlayer.h"
#include "Endpoint.h"

#include "support/CastingStore.h"

#include <app/server/Server.h>

#include <controller/CHIPCluster.h>

#include <app-common/zap-generated/cluster-objects.h>


namespace matter {
namespace casting {
namespace core {
Expand Down Expand Up @@ -101,16 +108,42 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection failed with %" CHIP_ERROR_FORMAT, err.Format());
support::ChipDeviceEventHandler::SetUdcStatus(false);
mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
mCommissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec;
mOnCompleted = nullptr;
mTargetCastingPlayer = nullptr;
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection failed with %" CHIP_ERROR_FORMAT, err.Format());
mOnCompleted(err, nullptr);
mOnCompleted = nullptr;
}
}


class MediaClusterBase : public chip::Controller::ClusterBase
{
public:
MediaClusterBase(chip::Messaging::ExchangeManager & exchangeManager, const chip::SessionHandle & session,
chip::EndpointId endpoint) :
ClusterBase(exchangeManager, session, endpoint)
{}
};

CHIP_ERROR GetPartsList(chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle)
{
MediaClusterBase cluster(exchangeMgr, sessionHandle, 1);

return cluster.template ReadAttribute<chip::app::Clusters::Descriptor::Attributes::PartsList::TypeInfo>(nullptr,
[](void * context, chip::app::Clusters::Descriptor::Attributes::PartsList::TypeInfo::DecodableArgType responseData)
{

},
[](void * context, CHIP_ERROR err)
{

});

}

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CHIP_ERROR CastingPlayer::SendUserDirectedCommissioningRequest()
{
Expand Down
54 changes: 54 additions & 0 deletions examples/tv-casting-app/tv-casting-common/core/Cluster.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Types.h"
#include "Endpoint.h"

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

namespace matter {
namespace casting {
namespace core {

class Endpoint;

// Base cluster class
class BaseCluster
{
private:
protected:
memory::Weak<Endpoint> endpoint;

public:
BaseCluster(memory::Weak<Endpoint> endpoint) { this->endpoint = endpoint; }

virtual ~BaseCluster() {}

BaseCluster() = delete;
BaseCluster(BaseCluster & other) = delete;
void operator=(const BaseCluster &) = delete;

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

}; // namespace core
}; // namespace casting
}; // namespace matter
108 changes: 108 additions & 0 deletions examples/tv-casting-app/tv-casting-common/core/Endpoint.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
*
* Copyright (c) 2023 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include "Cluster.h"
#include "Types.h"

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

#include <iostream>
#include <map>
#include <memory>
#include <type_traits>

namespace matter {
namespace casting {
namespace core {

class EndpointAttributes
{
public:
chip::EndpointId id = 0;
uint16_t vendorId = 0;
uint16_t productId = 0;
uint32_t type = 0;
chip::CharSpan name;
};

class Endpoint : public std::enable_shared_from_this<Endpoint>
{

private:
memory::Weak<CastingPlayer> player;

std::map<chip::ClusterId, memory::Strong<BaseCluster>> clusters;
EndpointAttributes attributes;

public:
Endpoint(memory::Weak<CastingPlayer> player, const EndpointAttributes & attributes)
{
this->player = player;
this->attributes = attributes;
}

~Endpoint() {}

Endpoint() = delete;
Endpoint(Endpoint & other) = delete;
void operator=(const Endpoint &) = delete;

protected:
memory::Strong<CastingPlayer> GetCastingPlayer() const { return player.lock(); }

public:
chip::EndpointId GetId() const { return attributes.id; }

chip::CharSpan GetName() const { return attributes.name; }

uint16_t GetProductId() const { return attributes.productId; }

uint16_t GetVendorId() const { return attributes.vendorId; }

uint32_t GetType() const { return attributes.type; }

public:
template <typename T>
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());
clusters[clusterId] = cluster;
}

template <typename T>
memory::Strong<T> GetCluster()
{
static_assert(std::is_base_of<BaseCluster, T>::value, "T must be derived from BaseCluster");
for (const auto & pair : clusters)
{
auto cluster = std::dynamic_pointer_cast<T>(pair.second);
if (cluster)
{
return cluster;
}
}
return nullptr;
}
};

}; // namespace core
}; // namespace casting
}; // namespace matter

0 comments on commit 8e4f7cf

Please sign in to comment.