Skip to content

Commit

Permalink
Addressing comments from sharadb-amazon
Browse files Browse the repository at this point in the history
  • Loading branch information
cheunj3 committed Sep 14, 2023
1 parent cde57b6 commit dbcd5a3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 106 deletions.
7 changes: 4 additions & 3 deletions examples/tv-casting-app/linux/simple-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class DiscoveryDelegateImpl : public DiscoveryDelegate
int commissionersCount = 0;

public:
void HandleOnAdded(Strong<CastingPlayer> player) override
void HandleOnAdded(matter::casting::memory::Strong<CastingPlayer> player) override
{
if (commissionersCount == 0)
{
Expand All @@ -96,7 +96,7 @@ class DiscoveryDelegateImpl : public DiscoveryDelegate
ChipLogProgress(AppServer, "Discovered Commissioners #%d", commissionersCount);
player->LogDetail();
}
void HandleOnUpdated(Strong<CastingPlayer> player) override
void HandleOnUpdated(matter::casting::memory::Strong<CastingPlayer> player) override
{
ChipLogProgress(AppServer, "Updated commissioner with ID: %s", player->GetId());
}
Expand Down Expand Up @@ -180,7 +180,8 @@ int main(int argc, char * argv[])
ChipLogError(AppServer, "CastingPlayerDiscovery::SetDelegate failed %" CHIP_ERROR_FORMAT, err.Format()));

// Discover CastingPlayers
err = CastingPlayerDiscovery::GetInstance()->StartDiscovery(35); // 35 represents device type: Matter video players
const uint64_t kTargetPlayerDeviceType = 35; // 35 represents device type: Matter video players
err = CastingPlayerDiscovery::GetInstance()->StartDiscovery(kTargetPlayerDeviceType);
VerifyOrReturnValue(err == CHIP_NO_ERROR, 0,
ChipLogError(AppServer, "CastingPlayerDiscovery::StartDiscovery failed %" CHIP_ERROR_FORMAT, err.Format()));

Expand Down
100 changes: 58 additions & 42 deletions examples/tv-casting-app/tv-casting-common/core/CastingPlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ namespace matter {
namespace casting {
namespace core {

using namespace memory;
// using namespace endpoint;

enum ConnectionError
Expand All @@ -48,90 +47,107 @@ const int kIdMaxLength = chip::Dnssd::kHostNameMaxLength + kPortMaxLength;
class Attributes
{
public:
char id[kIdMaxLength] = {};
char name[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char host_name[chip::Dnssd::kHostNameMaxLength + 1] = {};
size_t num_IPs; // number of valid IP addresses
chip::Inet::IPAddress ip_address[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];
uint16_t product_id;
uint16_t vendor_id;
uint32_t type;
char id[kIdMaxLength] = {};
char deviceName[chip::Dnssd::kMaxDeviceNameLen + 1] = {};
char hostName[chip::Dnssd::kHostNameMaxLength + 1] = {};
char instanceName[chip::Dnssd::kHostNameMaxLength + 1] = {};
unsigned int numIPs; // number of valid IP addresses
chip::Inet::IPAddress ipAddresses[chip::Dnssd::CommonResolutionData::kMaxIPAddresses];
uint16_t productId;
uint16_t vendorId;
uint32_t deviceType;
};

/**
* @brief CastingPlayer represent a Casting Server which can be connected to once discovered
* through Matter Commissioner discovery over DNS-SD.
*/
class CastingPlayer : public std::enable_shared_from_this<CastingPlayer>
{
private:
// std::vector<Strong<Endpoint>> endpoints;
bool connected = false;
Attributes attributes;
// std::vector<memory::Strong<Endpoint>> endpoints;
bool mConnected = false;
Attributes mAttributes;

public:
CastingPlayer(Attributes playerAttributes) { attributes = playerAttributes; }
const char * GetId() const { return attributes.id; }
CastingPlayer(Attributes playerAttributes) { mAttributes = playerAttributes; }
const char * GetId() const { return mAttributes.id; }

const char * GetName() const { return attributes.name; }
const char * GetDeviceName() const { return mAttributes.deviceName; }

const char * GetHostName() const { return attributes.host_name; }
const char * GetHostName() const { return mAttributes.hostName; }

size_t GetNumIPs() const { return attributes.num_IPs; }
const char * GetInstanceName() const { return mAttributes.instanceName; }

const chip::Inet::IPAddress & GetIPAddress(size_t idx) const { return attributes.ip_address[idx]; }
uint GetNumIPs() const { return mAttributes.numIPs; }

uint16_t GetProductId() const { return attributes.product_id; }
chip::Inet::IPAddress * GetIPAddresses() { return mAttributes.ipAddresses; }

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

uint32_t GetType() const { return attributes.type; }
uint16_t GetVendorId() const { return mAttributes.vendorId; }

uint32_t GetDeviceType() const { return mAttributes.deviceType; }

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

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

// const std::vector<Strong<Endpoint>> GetEndpoints() const { return endpoints; }
// Only compare the ID when checking if the Casting Players are equivalent
bool operator==(const CastingPlayer & other) const
{
int compareResult = strcmp(this->mAttributes.id, other.mAttributes.id);
return (compareResult == 0) ? 1 : 0;
}

public:
bool IsConnected() const { return connected; }
bool IsConnected() const { return mConnected; }

void Connect(const long timeout, ConnectCallback onCompleted);
void Disconnect(const long timeout, DisconnectCallback onCompleted);

void LogDetail() const
{
if (strlen(attributes.id) != 0)
if (strlen(mAttributes.id) != 0)
{
ChipLogDetail(Discovery, "\tID: %s", mAttributes.id);
}
if (strlen(mAttributes.deviceName) != 0)
{
ChipLogDetail(Discovery, "\tID: %s", attributes.id);
ChipLogDetail(Discovery, "\tName: %s", mAttributes.deviceName);
}
if (strlen(attributes.name) != 0)
if (strlen(mAttributes.hostName) != 0)
{
ChipLogDetail(Discovery, "\tName: %s", attributes.name);
ChipLogDetail(Discovery, "\tHost Name: %s", mAttributes.hostName);
}
if (strlen(attributes.host_name) != 0)
if (strlen(mAttributes.instanceName) != 0)
{
ChipLogDetail(Discovery, "\tHost Name: %s", attributes.host_name);
ChipLogDetail(Discovery, "\tInstance Name: %s", mAttributes.instanceName);
}
if (attributes.num_IPs > 0)
if (mAttributes.numIPs > 0)
{
ChipLogDetail(Discovery, "\tNumber of IPs: %zu", attributes.num_IPs);
ChipLogDetail(Discovery, "\tNumber of IPs: %u", mAttributes.numIPs);
}
char buf[chip::Inet::IPAddress::kMaxStringLength];
if (strlen(attributes.ip_address[0].ToString(buf)) != 0)
if (strlen(mAttributes.ipAddresses[0].ToString(buf)) != 0)
{
for (unsigned j = 0; j < attributes.num_IPs; j++)
for (unsigned j = 0; j < mAttributes.numIPs; j++)
{
char * ipAddressOut = attributes.ip_address[j].ToString(buf);
char * ipAddressOut = mAttributes.ipAddresses[j].ToString(buf);
ChipLogDetail(AppServer, "\tIP Address #%d: %s", j + 1, ipAddressOut);
}
}
if (attributes.product_id > 0)
if (mAttributes.productId > 0)
{
ChipLogDetail(Discovery, "\tProduct ID: %u", attributes.product_id);
ChipLogDetail(Discovery, "\tProduct ID: %u", mAttributes.productId);
}
if (attributes.vendor_id > 0)
if (mAttributes.vendorId > 0)
{
ChipLogDetail(Discovery, "\tVendor ID: %u", attributes.vendor_id);
ChipLogDetail(Discovery, "\tVendor ID: %u", mAttributes.vendorId);
}
if (attributes.type > 0)
if (mAttributes.deviceType > 0)
{
ChipLogDetail(Discovery, "\tType: %" PRIu32, attributes.type);
ChipLogDetail(Discovery, "\tDevice Type: %" PRIu32, mAttributes.deviceType);
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,62 +27,6 @@ using namespace chip::Controller;
using namespace chip::Dnssd;
using namespace std;

void DeviceDiscoveryDelegateImpl::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
{

VerifyOrReturn(mClientDelegate != nullptr,
ChipLogError(NotSpecified, "CastingPlayerDeviceDiscoveryDelegate, mClientDelegate is a nullptr"));

// convert nodeData to CastingPlayer
Attributes attributes;
strcpy(attributes.id, nodeData.resolutionData.hostName);

char port[kPortMaxLength] = {};
snprintf(port, sizeof(port), "%u", nodeData.resolutionData.port);
strcat(attributes.id, port);

strcpy(attributes.name, nodeData.commissionData.deviceName);
strcpy(attributes.host_name, nodeData.resolutionData.hostName);
attributes.num_IPs = nodeData.resolutionData.numIPs;
for (unsigned j = 0; j < attributes.num_IPs; j++)
{
attributes.ip_address[j] = nodeData.resolutionData.ipAddress[j];
}
attributes.product_id = nodeData.commissionData.productId;
attributes.vendor_id = nodeData.commissionData.vendorId;
attributes.type = nodeData.commissionData.deviceType;

Strong<CastingPlayer> player = std::make_shared<CastingPlayer>(attributes);

std::vector<Strong<CastingPlayer>> * castingPlayers = CastingPlayerDiscovery::GetInstance()->GetCastingPlayers();

// Add to or update castingPlayers
if (castingPlayers->size() != 0)
{

auto it = std::find_if((*castingPlayers).begin(), (*castingPlayers).end(),
[&player](const Strong<CastingPlayer> & castingPlayer) {
int compareResult = strcmp(castingPlayer->GetId(), player->GetId());
return (compareResult == 0) ? 1 : 0;
});

// ID match found in castingPlayer, perfom update
if (it != (*castingPlayers).end())
{
unsigned index = (unsigned int) std::distance((*castingPlayers).begin(), it);
(*castingPlayers)[index] = *it;
ChipLogProgress(AppServer, "Updated Casting Player");

mClientDelegate->HandleOnUpdated(player);
return;
}
}

castingPlayers->push_back(player);
mClientDelegate->HandleOnAdded(player);
return;
}

CastingPlayerDiscovery * CastingPlayerDiscovery::_castingPlayerDiscovery = nullptr;

CastingPlayerDiscovery::CastingPlayerDiscovery() {}
Expand All @@ -98,6 +42,7 @@ CastingPlayerDiscovery * CastingPlayerDiscovery::GetInstance()

CHIP_ERROR CastingPlayerDiscovery::StartDiscovery(uint64_t deviceTypeFilter)
{
ChipLogProgress(Discovery, "CastingPlayerDiscovery::StartDiscovery called");
VerifyOrReturnError(mState == DISCOVERY_READY, CHIP_ERROR_INCORRECT_STATE);

mCommissionableNodeController.RegisterDeviceDiscoveryDelegate(&mDelegate);
Expand Down Expand Up @@ -126,6 +71,62 @@ CHIP_ERROR CastingPlayerDiscovery::StopDiscovery()
return CHIP_NO_ERROR;
}

void DeviceDiscoveryDelegateImpl::OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData)
{

VerifyOrReturn(mClientDelegate != nullptr,
ChipLogError(NotSpecified, "CastingPlayerDeviceDiscoveryDelegate, mClientDelegate is a nullptr"));

// convert nodeData to CastingPlayer
Attributes attributes;
strcpy(attributes.id, nodeData.resolutionData.hostName);

char port[kPortMaxLength] = {};
snprintf(port, sizeof(port), "%u", nodeData.resolutionData.port);
strcat(attributes.id, port);

strcpy(attributes.deviceName, nodeData.commissionData.deviceName);
strcpy(attributes.hostName, nodeData.resolutionData.hostName);
strcpy(attributes.instanceName, nodeData.commissionData.instanceName);
attributes.numIPs = (unsigned int) nodeData.resolutionData.numIPs;
for (unsigned j = 0; j < attributes.numIPs; j++)
{
attributes.ipAddresses[j] = nodeData.resolutionData.ipAddress[j];
}
attributes.productId = nodeData.commissionData.productId;
attributes.vendorId = nodeData.commissionData.vendorId;
attributes.deviceType = nodeData.commissionData.deviceType;

memory::Strong<CastingPlayer> player = std::make_shared<CastingPlayer>(attributes);

std::vector<memory::Strong<CastingPlayer>> castingPlayers = CastingPlayerDiscovery::GetInstance()->GetCastingPlayers();

// Add to or update castingPlayers
if (castingPlayers.size() != 0)
{
auto it =
std::find_if(castingPlayers.begin(), castingPlayers.end(),
[&player](const memory::Strong<CastingPlayer> & castingPlayer) { return *castingPlayer == *player; });

// ID match found in castingPlayer, perfom update
if (it != castingPlayers.end())
{
unsigned index = (unsigned int) std::distance(castingPlayers.begin(), it);
castingPlayers[index] = player;
CastingPlayerDiscovery::GetInstance()->mCastingPlayers = castingPlayers;
ChipLogProgress(AppServer, "Updated Casting Player");

mClientDelegate->HandleOnUpdated(player);
return;
}
}

castingPlayers.push_back(player);
CastingPlayerDiscovery::GetInstance()->mCastingPlayers = castingPlayers;
mClientDelegate->HandleOnAdded(player);
return;
}

}; // namespace core
}; // namespace casting
}; // namespace matter
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,25 @@ enum CastingPlayerDiscoveryState
DISCOVERY_RUNNING, // After StartDiscovery success
};

/**
* @brief DiscoveryDelegate handles callbacks as CastingPlayers are discovered, updated, or lost
* from the network.
*/
class DLL_EXPORT DiscoveryDelegate
{
public:
virtual ~DiscoveryDelegate() {}
virtual void HandleOnAdded(Strong<CastingPlayer> player) = 0;
virtual void HandleOnUpdated(Strong<CastingPlayer> players) = 0;
// virtual void HandleOnRemoved(std::vector<Strong<CastingPlayer>> players);
virtual void HandleOnAdded(memory::Strong<CastingPlayer> player) = 0;
virtual void HandleOnUpdated(memory::Strong<CastingPlayer> players) = 0;
// virtual void HandleOnRemoved(memory::Strong<CastingPlayer> players) = 0;
};

class CastingPlayerDiscovery;

/**
* @brief DeviceDiscoveryDelegateImpl defines functionality for when callback
* OnDiscoveredDevice is thrown.
*/
class DeviceDiscoveryDelegateImpl : public chip::Controller::DeviceDiscoveryDelegate
{
private:
Expand All @@ -63,11 +71,15 @@ class DeviceDiscoveryDelegateImpl : public chip::Controller::DeviceDiscoveryDele
void OnDiscoveredDevice(const chip::Dnssd::DiscoveredNodeData & nodeData) override;
};

/**
* @brief CastingPlayerDiscovery represents the discovery of Casting Players.
* This class is a singleton.
*/
class CastingPlayerDiscovery
{

private:
std::vector<Strong<CastingPlayer>> mCastingPlayers;
std::vector<memory::Strong<CastingPlayer>> mCastingPlayers;
DeviceDiscoveryDelegateImpl mDelegate;

CastingPlayerDiscovery();
Expand Down Expand Up @@ -109,7 +121,9 @@ class CastingPlayerDiscovery
mDelegate = DeviceDiscoveryDelegateImpl(clientDelegate);
}

std::vector<Strong<CastingPlayer>> * GetCastingPlayers() { return &mCastingPlayers; }
std::vector<memory::Strong<CastingPlayer>> GetCastingPlayers() { return mCastingPlayers; }

friend class DeviceDiscoveryDelegateImpl;
};

}; // namespace core
Expand Down

0 comments on commit dbcd5a3

Please sign in to comment.