Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 7, 2023
1 parent c0786e0 commit ca29f98
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 82 deletions.
75 changes: 51 additions & 24 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ namespace core {

CastingPlayer * CastingPlayer::mTargetCastingPlayer = nullptr;

void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, unsigned long long int commissioningWindowTimeoutSec)
void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, unsigned long long int commissioningWindowTimeoutSec,
EndpointFilter * desiredEndpointFilter)
{
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection called");

Expand All @@ -48,40 +49,48 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
mCommissioningWindowTimeoutSec = commissioningWindowTimeoutSec;
mTargetCastingPlayer = this;

// If this CastingPlayer is the cache of CastingPlayers the app previously connected to (and has nodeId and fabricIndex of),
// simply Find or Re-establish the CASE session and return early
// If *this* CastingPlayer was previously connected to, its nodeId, fabricIndex and other attributes should be present
// in the CastingStore cache. If that is the case, AND, the cached data contains the endpoint desired by the client, if any,
// as per desiredEndpointFilter, simply Find or Re-establish the CASE session and return early
if (cachedCastingPlayers.size() != 0)
{
it = std::find_if(cachedCastingPlayers.begin(), cachedCastingPlayers.end(),
[this](const core::CastingPlayer & castingPlayerParam) { return castingPlayerParam == *this; });

// found the CastingPlayer in cache
if (it != cachedCastingPlayers.end())
{
unsigned index = (unsigned int) std::distance(cachedCastingPlayers.begin(), it);
*this = cachedCastingPlayers[index];

FindOrEstablishSession(
nullptr,
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer, "CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer successful");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_CONNECTED;
support::CastingStore::GetInstance()->AddOrUpdate(*CastingPlayer::GetTargetCastingPlayer());
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_NO_ERROR, CastingPlayer::GetTargetCastingPlayer());
},
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer failed");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(error, nullptr);
mTargetCastingPlayer = nullptr;
});
return; // FindOrEstablishSession called. Return early.
if (desiredEndpointFilter == nullptr || ContainsDesiredEndpoint(&cachedCastingPlayers[index], desiredEndpointFilter))
{
*this = cachedCastingPlayers[index];

FindOrEstablishSession(
nullptr,
[](void * context, chip::Messaging::ExchangeManager & exchangeMgr, const chip::SessionHandle & sessionHandle) {
ChipLogProgress(AppServer,
"CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer successful");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_CONNECTED;
support::CastingStore::GetInstance()->AddOrUpdate(*CastingPlayer::GetTargetCastingPlayer());
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_NO_ERROR,
CastingPlayer::GetTargetCastingPlayer());
},
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer failed");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
support::CastingStore::GetInstance()->Delete(*CastingPlayer::GetTargetCastingPlayer());
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(error, nullptr);
mTargetCastingPlayer = nullptr;
});
return; // FindOrEstablishSession called. Return early.
}
}
}

// this CastingPlayer is not in the list of cached CastingPlayers previously connected to. This VerifyOrEstablishConnection call
// this CastingPlayer is not in the list of cached CastingPlayers previously connected to or the cached data
// does not contain the endpoint the client desires to interact with. So, this VerifyOrEstablishConnection call
// will require User Directed Commissioning.
if (chip::Server::GetInstance().GetFailSafeContext().IsFailSafeArmed())
{
Expand Down Expand Up @@ -167,6 +176,24 @@ void CastingPlayer::FindOrEstablishSession(void * clientContext, chip::OnDeviceC
connectionContext->mOnConnectionFailureCallback);
}

bool CastingPlayer::ContainsDesiredEndpoint(core::CastingPlayer * cachedCastingPlayer, EndpointFilter * desiredEndpointFilter)
{
std::vector<memory::Strong<Endpoint>> cachedEndpoints = cachedCastingPlayer->GetEndpoints();
for (const auto & cachedEndpoint : cachedEndpoints)
{
bool match = true;
match = match && (desiredEndpointFilter->vendorId == 0 || cachedEndpoint->GetVendorId() == desiredEndpointFilter->vendorId);
match =
match && (desiredEndpointFilter->productId == 0 || cachedEndpoint->GetProductId() == desiredEndpointFilter->productId);
// TODO: check deviceTypeList and clusterList as well
if (match)
{
return true;
}
}
return false;
}

void CastingPlayer::LogDetail() const
{
if (strlen(mAttributes.id) != 0)
Expand Down
16 changes: 15 additions & 1 deletion examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "Endpoint.h"
#include "Types.h"
#include "support/ChipDeviceEventHandler.h"
#include "support/EndpointListLoader.h"

#include "lib/support/logging/CHIPLogging.h"
#include <inet/IPAddress.h>
Expand All @@ -37,6 +38,13 @@ const int kPortMaxLength = 5; // port is uint16_t
const int kIdMaxLength = chip::Dnssd::kHostNameMaxLength + kPortMaxLength + 1;
const unsigned long long int kCommissioningWindowTimeoutSec = 3 * 60; // 3 minutes

struct EndpointFilter
{
uint16_t vendorId = 0;
uint16_t productId = 0;
std::vector<chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType> deviceTypeList;
};

class CastingPlayerAttributes
{
public:
Expand All @@ -56,6 +64,8 @@ class CastingPlayerAttributes
chip::FabricIndex fabricIndex = 0;
};

class Endpoint;

/**
* @brief Represents CastingPlayer ConnectionState.
*
Expand Down Expand Up @@ -110,7 +120,8 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
* Defaults to kCommissioningWindowTimeoutSec.
*/
void VerifyOrEstablishConnection(ConnectCallback onCompleted,
unsigned long long int commissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec);
unsigned long long int commissioningWindowTimeoutSec = kCommissioningWindowTimeoutSec,
EndpointFilter * desiredEndpointFilter = nullptr);
void LogDetail() const;

const char * GetId() const { return mAttributes.id; }
Expand Down Expand Up @@ -185,10 +196,13 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
void FindOrEstablishSession(void * clientContext, chip::OnDeviceConnected onDeviceConnected,
chip::OnDeviceConnectionFailure onDeviceConnectionFailure);

bool ContainsDesiredEndpoint(core::CastingPlayer * cachedCastingPlayer, EndpointFilter * desiredEndpointFilter);

// ChipDeviceEventHandler handles chip::DeviceLayer::ChipDeviceEvent events and helps the CastingPlayer class commission with
// and connect to a CastingPlayer
friend class support::ChipDeviceEventHandler;
friend class ConnectionContext;
friend class support::EndpointListLoader;
};

class ConnectionContext
Expand Down
17 changes: 9 additions & 8 deletions examples/tv-casting-app/tv-casting-common/core/Endpoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "Types.h"

#include "lib/support/logging/CHIPLogging.h"
#include <app-common/zap-generated/cluster-objects.h>

#include <iostream>
#include <map>
Expand All @@ -39,8 +40,7 @@ class EndpointAttributes
chip::EndpointId id = 0;
uint16_t vendorId = 0;
uint16_t productId = 0;
uint32_t type = 0;
chip::CharSpan name;
std::vector<chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType> deviceTypeList;
};

class CastingPlayer;
Expand All @@ -49,13 +49,13 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
{

private:
memory::Weak<CastingPlayer> mCastingPlayer;
CastingPlayer * mCastingPlayer;

std::map<chip::ClusterId, memory::Strong<BaseCluster>> mClusters;
EndpointAttributes mAttributes;

public:
Endpoint(memory::Weak<CastingPlayer> castingPlayer, const EndpointAttributes & attributes)
Endpoint(CastingPlayer * castingPlayer, const EndpointAttributes & attributes)
{
this->mCastingPlayer = castingPlayer;
this->mAttributes = attributes;
Expand All @@ -68,7 +68,7 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
void operator=(const Endpoint &) = delete;

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

public:
/**
Expand All @@ -78,13 +78,14 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>

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

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

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

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

uint32_t GetType() const { return mAttributes.type; }
std::vector<chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType> GetDeviceTypeList() const
{
return mAttributes.deviceTypeList;
}

public:
template <typename T>
Expand Down
2 changes: 2 additions & 0 deletions examples/tv-casting-app/tv-casting-common/core/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class AppParameters;
class ByteSpanDataProvider;
class ServerInitParamsProvider;

class EndpointListLoader;

} // namespace support

}; // namespace casting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,11 @@ void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * e
ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle: Connection to CastingPlayer successful");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_CONNECTED;

EndpointListLoader::GetInstance()->Initialize(CastingPlayer::GetTargetCastingPlayer(), &exchangeMgr,
&sessionHandle);
EndpointListLoader::GetInstance()->Load();
EndpointListLoader::GetInstance()->Initialize(&exchangeMgr, &sessionHandle);

support::CastingStore::GetInstance()->AddOrUpdate(*CastingPlayer::GetTargetCastingPlayer());
VerifyOrReturn(CastingPlayer::GetTargetCastingPlayer()->mOnCompleted);
CastingPlayer::GetTargetCastingPlayer()->mOnCompleted(CHIP_NO_ERROR, CastingPlayer::GetTargetCastingPlayer());
// this will load all the endpoints with their respective attributes into the TargetCastingPlayer
// and call mOnCompleted()
EndpointListLoader::GetInstance()->Load();
},
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "ChipDeviceEventHandler::Handle: Connection to CastingPlayer failed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace support {
/**
* @brief Handles chip::DeviceLayer::ChipDeviceEvent events (such as kFailSafeTimerExpired, kBindingsChangedViaCluster,
* kCommissioningComplete) sent by the Matter DeviceLayer.
* ChipDeviceEventHandlerhelps the CastingPlayer class commission with and connect to a CastingPlayer
* ChipDeviceEventHandler helps the CastingPlayer class commission with and connect to a CastingPlayer
*/
class ChipDeviceEventHandler
{
Expand Down
Loading

0 comments on commit ca29f98

Please sign in to comment.