Skip to content

Commit

Permalink
[Fabric-Bridge] Don't track device changes for synced devices (projec…
Browse files Browse the repository at this point in the history
…t-chip#33769)

* Don't track device change status for synced devices

* Address review comments
  • Loading branch information
yufengwangca committed Jun 10, 2024
1 parent 5ee4ef6 commit c158260
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 44 deletions.
31 changes: 8 additions & 23 deletions examples/fabric-bridge-app/linux/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@

using namespace chip::app::Clusters::Actions;

Device::Device(const char * szDeviceName, std::string szLocation)
Device::Device(chip::NodeId nodeId, const char * name)
{
chip::Platform::CopyString(mName, szDeviceName);
mLocation = szLocation;
chip::Platform::CopyString(mName, name);
mReachable = false;
mEndpointId = 0;
}
Expand All @@ -38,37 +37,23 @@ bool Device::IsReachable()
return mReachable;
}

void Device::SetReachable(bool aReachable)
void Device::SetReachable(bool reachable)
{
bool changed = (mReachable != aReachable);
mReachable = reachable;

mReachable = aReachable;

if (aReachable)
if (reachable)
{
ChipLogProgress(NotSpecified, "Device[%s]: ONLINE", mName);
}
else
{
ChipLogProgress(NotSpecified, "Device[%s]: OFFLINE", mName);
}

if (changed)
{
HandleDeviceChange(this, kChanged_Reachable);
}
}

void Device::SetName(const char * szName)
void Device::SetName(const char * name)
{
bool changed = (strncmp(mName, szName, sizeof(mName)) != 0);
ChipLogProgress(NotSpecified, "Device[%s]: New Name=\"%s\"", mName, name);

ChipLogProgress(NotSpecified, "Device[%s]: New Name=\"%s\"", mName, szName);

chip::Platform::CopyString(mName, szName);

if (changed)
{
HandleDeviceChange(this, kChanged_Name);
}
chip::Platform::CopyString(mName, name);
}
7 changes: 6 additions & 1 deletion examples/fabric-bridge-app/linux/DeviceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ using namespace chip::DeviceLayer;
using namespace chip::app::Clusters;

namespace {

constexpr uint8_t kMaxRetries = 10;

} // namespace

DeviceManager::DeviceManager()
// Define the static member
DeviceManager DeviceManager::sInstance;

void DeviceManager::Init()
{
memset(mDevices, 0, sizeof(mDevices));
mFirstDynamicEndpointId = static_cast<chip::EndpointId>(
Expand Down
20 changes: 5 additions & 15 deletions examples/fabric-bridge-app/linux/include/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,13 @@ class Device
public:
static const int kDeviceNameSize = 32;

enum Changed_t
{
kChanged_Reachable = 1u << 0,
kChanged_Location = 1u << 1,
kChanged_Name = 1u << 2,
kChanged_Last = kChanged_Name,
} Changed;

Device(const char * szDeviceName, std::string szLocation);
Device(chip::NodeId nodeId, const char * name);
virtual ~Device() {}

bool IsReachable();
void SetReachable(bool aReachable);
void SetName(const char * szDeviceName);
void SetLocation(std::string szLocation);
void SetReachable(bool reachable);
void SetName(const char * name);
void SetLocation(std::string location) { mLocation = location; };
inline void SetEndpointId(chip::EndpointId id) { mEndpointId = id; };
inline chip::EndpointId GetEndpointId() { return mEndpointId; };
inline void SetParentEndpointId(chip::EndpointId id) { mParentEndpointId = id; };
Expand All @@ -56,13 +48,11 @@ class Device
inline std::string GetZone() { return mZone; };
inline void SetZone(std::string zone) { mZone = zone; };

private:
virtual void HandleDeviceChange(Device * device, Device::Changed_t changeMask) = 0;

protected:
bool mReachable;
char mName[kDeviceNameSize];
std::string mLocation;
chip::NodeId mNodeId;
chip::EndpointId mEndpointId;
chip::EndpointId mParentEndpointId;
std::string mZone;
Expand Down
19 changes: 18 additions & 1 deletion examples/fabric-bridge-app/linux/include/DeviceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
class DeviceManager
{
public:
DeviceManager();
DeviceManager() = default;

void Init();

/**
* @brief Adds a device to a dynamic endpoint.
Expand Down Expand Up @@ -62,7 +64,22 @@ class DeviceManager
Device * GetDevice(uint16_t index) const;

private:
friend DeviceManager & DeviceMgr();

static DeviceManager sInstance;

chip::EndpointId mCurrentEndpointId;
chip::EndpointId mFirstDynamicEndpointId;
Device * mDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];
};

/**
* Returns the public interface of the DeviceManager singleton object.
*
* Applications should use this to access features of the DeviceManager
* object.
*/
inline DeviceManager & DeviceMgr()
{
return DeviceManager::sInstance;
}
8 changes: 4 additions & 4 deletions examples/fabric-bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ void AttemptRpcClientConnect(System::Layer * systemLayer, void * appState)
}
#endif // defined(PW_RPC_FABRIC_BRIDGE_SERVICE) && PW_RPC_FABRIC_BRIDGE_SERVICE

DeviceManager gDeviceManager;

} // namespace

void ApplicationInit()
Expand All @@ -111,6 +109,8 @@ void ApplicationInit()
// Start a thread for bridge polling
std::thread pollingThread(BridgePollingThread);
pollingThread.detach();

DeviceMgr().Init();
}

void ApplicationShutdown() {}
Expand All @@ -135,7 +135,7 @@ Protocols::InteractionModel::Status emberAfExternalAttributeReadCallback(Endpoin
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
AttributeId attributeId = attributeMetadata->attributeId;

Device * dev = gDeviceManager.GetDevice(endpointIndex);
Device * dev = DeviceMgr().GetDevice(endpointIndex);
if (dev != nullptr && clusterId == app::Clusters::BridgedDeviceBasicInformation::Id)
{
using namespace app::Clusters::BridgedDeviceBasicInformation::Attributes;
Expand Down Expand Up @@ -179,7 +179,7 @@ Protocols::InteractionModel::Status emberAfExternalAttributeWriteCallback(Endpoi
uint16_t endpointIndex = emberAfGetDynamicIndexFromEndpoint(endpoint);
Protocols::InteractionModel::Status ret = Protocols::InteractionModel::Status::Failure;

Device * dev = gDeviceManager.GetDevice(endpointIndex);
Device * dev = DeviceMgr().GetDevice(endpointIndex);
if (dev != nullptr && dev->IsReachable())
{
ChipLogProgress(NotSpecified, "emberAfExternalAttributeWriteCallback: ep=%d, clusterId=%d", endpoint, clusterId);
Expand Down

0 comments on commit c158260

Please sign in to comment.