Skip to content

Commit

Permalink
ReadAll Endpoint info
Browse files Browse the repository at this point in the history
  • Loading branch information
sharadb-amazon committed Dec 11, 2023
1 parent 6cca0f8 commit abbebec
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 16 deletions.
32 changes: 27 additions & 5 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
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());

// this async call will Load all the endpoints with their respective attributes into the TargetCastingPlayer
// persist the TargetCastingPlayer information into the CastingStore and call mOnCompleted()
support::EndpointListLoader::GetInstance()->Initialize(&exchangeMgr, &sessionHandle);
support::EndpointListLoader::GetInstance()->Load();
},
[](void * context, const chip::ScopedNodeId & peerId, CHIP_ERROR error) {
ChipLogError(AppServer, "CastingPlayer::VerifyOrEstablishConnection Connection to CastingPlayer failed");
Expand Down Expand Up @@ -122,6 +123,27 @@ void CastingPlayer::VerifyOrEstablishConnection(ConnectCallback onCompleted, uns
}
}

void CastingPlayer::RegisterEndpoint(const memory::Strong<Endpoint> endpoint)
{
if (mEndpoints.size() != 0)
{
auto it = std::find_if(mEndpoints.begin(), mEndpoints.end(), [endpoint](const memory::Strong<Endpoint> & _endpoint) {
return _endpoint->GetId() == endpoint->GetId();
});

// If existing endpoint, update mEndpoints. If new endpoint, add it to the vector mEndpoints
if (it != mEndpoints.end())
{
unsigned index = (unsigned int) std::distance(mEndpoints.begin(), it);
mEndpoints[index] = endpoint;
}
else
{
mEndpoints.push_back(endpoint);
}
}
}

#if CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY_CLIENT
CHIP_ERROR CastingPlayer::SendUserDirectedCommissioningRequest()
{
Expand Down Expand Up @@ -185,7 +207,7 @@ bool CastingPlayer::ContainsDesiredEndpoint(core::CastingPlayer * cachedCastingP
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
// TODO: check deviceTypeList and clusters/serverList as well
if (match)
{
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ struct EndpointFilter
uint16_t vendorId = 0;
uint16_t productId = 0;
std::vector<chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType> requiredDeviceTypes;
std::vector<chip::ClusterId> requiredClusterIds;
};

class CastingPlayerAttributes
Expand Down Expand Up @@ -153,12 +152,12 @@ class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>

void SetFabricIndex(chip::FabricIndex fabricIndex) { mAttributes.fabricIndex = fabricIndex; }

void RegisterEndpoint(const memory::Strong<Endpoint> endpoint) { endpoints.push_back(endpoint); }
void RegisterEndpoint(const memory::Strong<Endpoint> endpoint);

const std::vector<memory::Strong<Endpoint>> GetEndpoints() const { return endpoints; }
const std::vector<memory::Strong<Endpoint>> GetEndpoints() const { return mEndpoints; }

private:
std::vector<memory::Strong<Endpoint>> endpoints;
std::vector<memory::Strong<Endpoint>> mEndpoints;
ConnectionState mConnectionState = CASTING_PLAYER_NOT_CONNECTED;
CastingPlayerAttributes mAttributes;
static CastingPlayer * mTargetCastingPlayer;
Expand Down
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 @@ -89,7 +89,7 @@ class Endpoint : public std::enable_shared_from_this<Endpoint>
std::vector<chip::ClusterId> GetServerList()
{
std::vector<chip::ClusterId> serverList;
for(auto const& cluster: mClusters)
for (auto const & cluster : mClusters)
{
serverList.push_back(cluster.first);
}
Expand Down
163 changes: 160 additions & 3 deletions examples/tv-casting-app/tv-casting-common/support/CastingStore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ std::vector<core::CastingPlayer> CastingStore::ReadAll()
ChipLogError(AppServer, "TLVReader.EnterContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

core::CastingPlayerAttributes attributes;
std::vector<core::EndpointAttributes> endpointAttributesList;
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
chip::TLV::Tag castingPlayerContainerTag = reader.GetTag();
Expand Down Expand Up @@ -196,13 +197,170 @@ std::vector<core::CastingPlayer> CastingStore::ReadAll()
continue;
}

if (castingPlayerContainerTagNum == kCastingPlayerEndpointsContainerTag)
{
// Entering Endpoints container
chip::TLV::TLVType endpointsContainerType = chip::TLV::kTLVType_Array;
err = reader.EnterContainer(endpointsContainerType);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.EnterContainer failed %" CHIP_ERROR_FORMAT, err.Format()));
core::EndpointAttributes endpointAttributes;
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
// Entering Endpoint container
chip::TLV::TLVType endpointContainerType = chip::TLV::kTLVType_Structure;
err = reader.EnterContainer(endpointContainerType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.EnterContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

while ((err = reader.Next()) == CHIP_NO_ERROR)
{
chip::TLV::Tag endpointContainerTag = reader.GetTag();
VerifyOrReturnValue(chip::TLV::IsContextTag(endpointContainerTag), std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "Unexpected non-context TLV tag"));

uint8_t endpointContainerTagNum = static_cast<uint8_t>(chip::TLV::TagNumFromTag(endpointContainerTag));
if (endpointContainerTagNum == kCastingPlayerEndpointIdTag)
{
err = reader.Get(endpointAttributes.mId);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
continue;
}

if (endpointContainerTagNum == kCastingPlayerEndpointVendorIdTag)
{
err = reader.Get(endpointAttributes.mVendorId);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
continue;
}

if (endpointContainerTagNum == kCastingPlayerEndpointProductIdTag)
{
err = reader.Get(endpointAttributes.mProductId);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
continue;
}

std::vector<chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType> deviceTypeList;
if (endpointContainerTagNum == kCastingPlayerEndpointDeviceTypeListContainerTag)
{
// Entering DeviceTypeList container
chip::TLV::TLVType deviceTypeListContainerType = chip::TLV::kTLVType_Array;
err = reader.EnterContainer(deviceTypeListContainerType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.EnterContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

while ((err = reader.Next()) == CHIP_NO_ERROR)
{
// Entering DeviceTypeStruct container
chip::TLV::TLVType deviceTypeStructContainerType = chip::TLV::kTLVType_Structure;
err = reader.EnterContainer(deviceTypeStructContainerType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.EnterContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

chip::app::Clusters::Descriptor::Structs::DeviceTypeStruct::DecodableType deviceTypeStruct;
while ((err = reader.Next()) == CHIP_NO_ERROR)
{
chip::TLV::Tag deviceTypeStructContainerTag = reader.GetTag();
VerifyOrReturnValue(chip::TLV::IsContextTag(deviceTypeStructContainerTag),
std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "Unexpected non-context TLV tag"));

uint8_t deviceTypeStructContainerTagNum =
static_cast<uint8_t>(chip::TLV::TagNumFromTag(deviceTypeStructContainerTag));
if (deviceTypeStructContainerTagNum == kCastingPlayerEndpointDeviceTypeTag)
{
err = reader.Get(deviceTypeStruct.deviceType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
continue;
}

if (deviceTypeStructContainerTagNum == kCastingPlayerEndpointDeviceTypeRevisionTag)
{
err = reader.Get(deviceTypeStruct.revision);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.Get failed %" CHIP_ERROR_FORMAT, err.Format()));
continue;
}
}

if (err == CHIP_END_OF_TLV)
{
// Exiting DeviceTypeStruct container
err = reader.ExitContainer(deviceTypeStructContainerType);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer,
"TLVReader.ExitContainer failed %" CHIP_ERROR_FORMAT,
err.Format()));

deviceTypeList.push_back(deviceTypeStruct);
break;
}
}
if (err == CHIP_END_OF_TLV)
{
// Exiting DeviceTypeList container
err = reader.ExitContainer(deviceTypeListContainerType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.ExitContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

endpointAttributes.mDeviceTypeList = deviceTypeList;
break;
}
continue;
}
}

if (err == CHIP_END_OF_TLV)
{
// Exiting Endpoint container
err = reader.ExitContainer(endpointContainerType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.ExitContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

endpointAttributesList.push_back(endpointAttributes);
break;
}
}

if (err == CHIP_END_OF_TLV)
{
// Exiting Endpoints container
err = reader.ExitContainer(endpointsContainerType);
VerifyOrReturnValue(
err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.ExitContainer failed %" CHIP_ERROR_FORMAT, err.Format()));
break;
}

continue;
}

if (err == CHIP_END_OF_TLV)
{
// Exiting CastingPlayer container
err = reader.ExitContainer(castingPlayerContainerType);
VerifyOrReturnValue(err == CHIP_NO_ERROR, std::vector<core::CastingPlayer>(),
ChipLogError(AppServer, "TLVReader.ExitContainer failed %" CHIP_ERROR_FORMAT, err.Format()));

// create a castingPlayer with Endpoints and add it to the castingPlayers to be returned
core::CastingPlayer castingPlayer(attributes);
for (core::EndpointAttributes endpointAttributes : endpointAttributesList)
{
std::shared_ptr<core::Endpoint> endpoint(new core::Endpoint(&castingPlayer, endpointAttributes));
castingPlayer.RegisterEndpoint(endpoint);
}
castingPlayers.push_back(castingPlayer);
break;
}
Expand Down Expand Up @@ -367,10 +525,9 @@ CHIP_ERROR CastingStore::WriteAll(std::vector<core::CastingPlayer> castingPlayer
ReturnErrorOnFailure(tlvWriter.StartContainer(chip::TLV::ContextTag(kCastingPlayerEndpointServerListContainerTag),
chip::TLV::kTLVType_Array, serverListContainerType));
std::vector<chip::ClusterId> serverList = endpoint->GetServerList();
for(chip::ClusterId clusterId : serverList)
for (chip::ClusterId clusterId : serverList)
{
ReturnErrorOnFailure(
tlvWriter.Put(chip::TLV::ContextTag(kCastingPlayerEndpointServerClusterIdTag), clusterId));
ReturnErrorOnFailure(tlvWriter.Put(chip::TLV::ContextTag(kCastingPlayerEndpointServerClusterIdTag), clusterId));
}
// ServerList container ends
ReturnErrorOnFailure(tlvWriter.EndContainer(serverListContainerType));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,9 @@ void ChipDeviceEventHandler::Handle(const chip::DeviceLayer::ChipDeviceEvent * e
ChipLogProgress(AppServer, "ChipDeviceEventHandler::Handle: Connection to CastingPlayer successful");
CastingPlayer::GetTargetCastingPlayer()->mConnectionState = CASTING_PLAYER_CONNECTED;

// this async call will Load all the endpoints with their respective attributes into the TargetCastingPlayer
// persist the TargetCastingPlayer information into the CastingStore and call mOnCompleted()
EndpointListLoader::GetInstance()->Initialize(&exchangeMgr, &sessionHandle);

// 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) {
Expand Down

0 comments on commit abbebec

Please sign in to comment.