Skip to content

Commit f308b00

Browse files
andy31415restyled-commits
authored andcommitted
Add support for more attributes in Bridged Device Basic Information cluster in FabricBridge example (project-chip#34851)
* Add extra attributes to the bridged device basic info structures, remove nonsense comments * Make use of AAI for BridgedDeviceBasicInformation cluster * Restyled by gn * Fix sizes for software version * Update the synchronized device proto to have more data in it * Switch to unique ptr in the registry, making sure memory management works (fixed memory leak on remove device) * Use more std::optional * Bump revision to 4 * Forward attributes from the create call into the bridged device * Make attribute mapping actually work * Restyle * Ensure unique IDs are generated * Restyle * Increase size to 33 to allow for a null terminator * make sure that the rpc structures are initialized * Restyle * Add some fake data to test moving the data around * Remove unused members that were likely just copied over * make the attributes optional * Restyled by clang-format * Fix string size for HW and software versions --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent bf7b30c commit f308b00

File tree

10 files changed

+279
-87
lines changed

10 files changed

+279
-87
lines changed

examples/common/pigweed/BUILD.gn

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pw_proto_library("fabric_admin_service") {
9090

9191
pw_proto_library("fabric_bridge_service") {
9292
sources = [ "protos/fabric_bridge_service.proto" ]
93+
inputs = [ "protos/fabric_bridge_service.options" ]
9394
deps = [ "$dir_pw_protobuf:common_protos" ]
9495
strip_prefix = "protos"
9596
prefix = "fabric_bridge_service"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
chip.rpc.SynchronizedDevice.unique_id max_size:33
2+
chip.rpc.SynchronizedDevice.vendor_name max_size:33
3+
chip.rpc.SynchronizedDevice.product_name max_size:33
4+
chip.rpc.SynchronizedDevice.node_label max_size:33
5+
chip.rpc.SynchronizedDevice.hardware_version_string max_size:65
6+
chip.rpc.SynchronizedDevice.software_version_string max_size:65

examples/common/pigweed/protos/fabric_bridge_service.proto

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@ package chip.rpc;
77
// Define the message for a synchronized end device with necessary fields
88
message SynchronizedDevice {
99
uint64 node_id = 1;
10+
11+
optional string unique_id = 2;
12+
optional string vendor_name = 3;
13+
optional uint32 vendor_id = 4;
14+
optional string product_name = 5;
15+
optional uint32 product_id = 6;
16+
optional string node_label = 7;
17+
optional uint32 hardware_version = 8;
18+
optional string hardware_version_string = 9;
19+
optional uint32 software_version = 10;
20+
optional string software_version_string = 11;
1021
}
1122

1223
service FabricBridge {

examples/fabric-admin/rpc/RpcClient.cpp

+32-4
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,36 @@ CHIP_ERROR AddSynchronizedDevice(chip::NodeId nodeId)
117117
{
118118
ChipLogProgress(NotSpecified, "AddSynchronizedDevice");
119119

120-
chip_rpc_SynchronizedDevice device;
121-
device.node_id = nodeId;
120+
chip_rpc_SynchronizedDevice device = chip_rpc_SynchronizedDevice_init_default;
121+
device.node_id = nodeId;
122+
123+
// TODO: fill this with real data. For now we just add things for testing
124+
strcpy(device.vendor_name, "Test Vendor");
125+
device.has_vendor_name = true;
126+
127+
device.vendor_id = 123;
128+
device.has_vendor_id = true;
129+
130+
strcpy(device.product_name, "Test Product");
131+
device.has_product_name = true;
132+
133+
device.product_id = 234;
134+
device.has_product_id = true;
135+
136+
strcpy(device.node_label, "Device Label");
137+
device.has_node_label = true;
138+
139+
device.hardware_version = 11;
140+
device.has_hardware_version = true;
141+
142+
strcpy(device.hardware_version_string, "Hardware");
143+
device.has_hardware_version_string = true;
144+
145+
device.software_version = 22;
146+
device.has_software_version = true;
147+
148+
strcpy(device.software_version_string, "Test 1.4.22");
149+
device.has_software_version_string = true;
122150

123151
// The RPC call is kept alive until it completes. When a response is received, it will be logged by the handler
124152
// function and the call will complete.
@@ -137,8 +165,8 @@ CHIP_ERROR RemoveSynchronizedDevice(chip::NodeId nodeId)
137165
{
138166
ChipLogProgress(NotSpecified, "RemoveSynchronizedDevice");
139167

140-
chip_rpc_SynchronizedDevice device;
141-
device.node_id = nodeId;
168+
chip_rpc_SynchronizedDevice device = chip_rpc_SynchronizedDevice_init_default;
169+
device.node_id = nodeId;
142170

143171
// The RPC call is kept alive until it completes. When a response is received, it will be logged by the handler
144172
// function and the call will complete.

examples/fabric-bridge-app/fabric-bridge-common/include/BridgedDevice.h

+25-16
Original file line numberDiff line numberDiff line change
@@ -20,41 +20,50 @@
2020

2121
#include <app/util/attribute-storage.h>
2222

23-
#include <stdbool.h>
24-
#include <stdint.h>
25-
26-
#include <functional>
2723
#include <string>
28-
#include <vector>
2924

3025
class BridgedDevice
3126
{
3227
public:
33-
static const int kDeviceNameSize = 32;
28+
/// Defines all attributes that we keep track of for a bridged device
29+
struct BridgedAttributes
30+
{
31+
std::string uniqueId;
32+
std::string vendorName;
33+
uint16_t vendorId = 0;
34+
std::string productName;
35+
uint16_t productId = 0;
36+
std::string nodeLabel;
37+
uint16_t hardwareVersion = 0;
38+
std::string hardwareVersionString;
39+
uint32_t softwareVersion = 0;
40+
std::string softwareVersionString;
41+
};
3442

3543
BridgedDevice(chip::NodeId nodeId);
36-
virtual ~BridgedDevice() {}
44+
virtual ~BridgedDevice() = default;
3745

3846
bool IsReachable();
3947
void SetReachable(bool reachable);
40-
void SetName(const char * name);
41-
void SetLocation(std::string location) { mLocation = location; };
48+
4249
inline void SetEndpointId(chip::EndpointId id) { mEndpointId = id; };
4350
inline chip::EndpointId GetEndpointId() { return mEndpointId; };
4451
inline chip::NodeId GetNodeId() { return mNodeId; };
4552
inline void SetParentEndpointId(chip::EndpointId id) { mParentEndpointId = id; };
4653
inline chip::EndpointId GetParentEndpointId() { return mParentEndpointId; };
47-
inline char * GetName() { return mName; };
48-
inline std::string GetLocation() { return mLocation; };
49-
inline std::string GetZone() { return mZone; };
50-
inline void SetZone(std::string zone) { mZone = zone; };
54+
55+
[[nodiscard]] const BridgedAttributes & GetBridgedAttributes() const { return mAttributes; }
56+
void SetBridgedAttributes(const BridgedAttributes & value) { mAttributes = value; }
57+
58+
/// Convenience method to set just the unique id of a bridged device as it
59+
/// is one of the few attributes that is not always bulk-set
60+
void SetUniqueId(const std::string & value) { mAttributes.uniqueId = value; }
5161

5262
protected:
5363
bool mReachable;
54-
char mName[kDeviceNameSize];
55-
std::string mLocation;
5664
chip::NodeId mNodeId;
5765
chip::EndpointId mEndpointId;
5866
chip::EndpointId mParentEndpointId;
59-
std::string mZone;
67+
68+
BridgedAttributes mAttributes;
6069
};

examples/fabric-bridge-app/fabric-bridge-common/include/BridgedDeviceManager.h

+23-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include "BridgedDevice.h"
2424

25+
#include <memory>
26+
2527
class BridgedDeviceManager
2628
{
2729
public:
@@ -41,13 +43,19 @@ class BridgedDeviceManager
4143
* This function attempts to add a device to a dynamic endpoint. It tries to find an available
4244
* endpoint slot and retries the addition process up to a specified maximum number of times if
4345
* the endpoint already exists. If the addition is successful, it returns the index of the
44-
* dynamic endpoint; otherwise, it returns -1.
46+
* dynamic endpoint;
47+
*
48+
* Ensures that the device has a unique id:
49+
* - device MUST set its unique id if any BEFORE calling this method
50+
* - if no unique id (i.e. empty string), a new unique id will be generated
51+
* - Add will fail if the unique id is not unique
4552
*
4653
* @param dev A pointer to the device to be added.
4754
* @param parentEndpointId The parent endpoint ID. Defaults to an invalid endpoint ID.
48-
* @return int The index of the dynamic endpoint if successful, -1 otherwise.
55+
* @return int The index of the dynamic endpoint if successful, nullopt otherwise
4956
*/
50-
int AddDeviceEndpoint(BridgedDevice * dev, chip::EndpointId parentEndpointId = chip::kInvalidEndpointId);
57+
std::optional<unsigned> AddDeviceEndpoint(std::unique_ptr<BridgedDevice> dev,
58+
chip::EndpointId parentEndpointId = chip::kInvalidEndpointId);
5159

5260
/**
5361
* @brief Removes a device from a dynamic endpoint.
@@ -95,16 +103,26 @@ class BridgedDeviceManager
95103
* @param nodeId The NodeId of the device to be removed.
96104
* @return int The index of the removed dynamic endpoint if successful, -1 otherwise.
97105
*/
98-
int RemoveDeviceByNodeId(chip::NodeId nodeId);
106+
std::optional<unsigned> RemoveDeviceByNodeId(chip::NodeId nodeId);
107+
108+
/**
109+
* Finds the device with the given unique id (if any)
110+
*/
111+
BridgedDevice * GetDeviceByUniqueId(const std::string & id);
99112

100113
private:
101114
friend BridgedDeviceManager & BridgeDeviceMgr();
102115

116+
/**
117+
* Creates a new unique ID that is not used by any other mDevice
118+
*/
119+
std::string GenerateUniqueId();
120+
103121
static BridgedDeviceManager sInstance;
104122

105123
chip::EndpointId mCurrentEndpointId;
106124
chip::EndpointId mFirstDynamicEndpointId;
107-
BridgedDevice * mDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];
125+
std::unique_ptr<BridgedDevice> mDevices[CHIP_DEVICE_CONFIG_DYNAMIC_ENDPOINT_COUNT + 1];
108126
};
109127

110128
/**

examples/fabric-bridge-app/fabric-bridge-common/src/BridgedDevice.cpp

+2-9
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,10 @@ void BridgedDevice::SetReachable(bool reachable)
4343

4444
if (reachable)
4545
{
46-
ChipLogProgress(NotSpecified, "BridgedDevice[%s]: ONLINE", mName);
46+
ChipLogProgress(NotSpecified, "BridgedDevice[%s]: ONLINE", mAttributes.uniqueId.c_str());
4747
}
4848
else
4949
{
50-
ChipLogProgress(NotSpecified, "BridgedDevice[%s]: OFFLINE", mName);
50+
ChipLogProgress(NotSpecified, "BridgedDevice[%s]: OFFLINE", mAttributes.uniqueId.c_str());
5151
}
5252
}
53-
54-
void BridgedDevice::SetName(const char * name)
55-
{
56-
ChipLogProgress(NotSpecified, "BridgedDevice[%s]: New Name=\"%s\"", mName, name);
57-
58-
chip::Platform::CopyString(mName, name);
59-
}

examples/fabric-bridge-app/fabric-bridge-common/src/BridgedDeviceBasicInformationImpl.cpp

+28-1
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,41 @@ CHIP_ERROR BridgedDeviceBasicInformationImpl::Read(const ConcreteReadAttributePa
4444
encoder.Encode(dev->IsReachable());
4545
break;
4646
case BasicInformation::Attributes::NodeLabel::Id:
47-
encoder.Encode(CharSpan::fromCharString(dev->GetName()));
47+
encoder.Encode(CharSpan::fromCharString(dev->GetBridgedAttributes().nodeLabel.c_str()));
4848
break;
4949
case BasicInformation::Attributes::ClusterRevision::Id:
5050
encoder.Encode(kBridgedDeviceBasicInformationClusterRevision);
5151
break;
5252
case BasicInformation::Attributes::FeatureMap::Id:
5353
encoder.Encode(kBridgedDeviceBasicInformationFeatureMap);
5454
break;
55+
case BasicInformation::Attributes::UniqueID::Id:
56+
encoder.Encode(CharSpan::fromCharString(dev->GetBridgedAttributes().uniqueId.c_str()));
57+
break;
58+
case BasicInformation::Attributes::VendorName::Id:
59+
encoder.Encode(CharSpan::fromCharString(dev->GetBridgedAttributes().vendorName.c_str()));
60+
break;
61+
case BasicInformation::Attributes::VendorID::Id:
62+
encoder.Encode(dev->GetBridgedAttributes().vendorId);
63+
break;
64+
case BasicInformation::Attributes::ProductName::Id:
65+
encoder.Encode(CharSpan::fromCharString(dev->GetBridgedAttributes().productName.c_str()));
66+
break;
67+
case BasicInformation::Attributes::ProductID::Id:
68+
encoder.Encode(dev->GetBridgedAttributes().productId);
69+
break;
70+
case BasicInformation::Attributes::HardwareVersion::Id:
71+
encoder.Encode(dev->GetBridgedAttributes().hardwareVersion);
72+
break;
73+
case BasicInformation::Attributes::HardwareVersionString::Id:
74+
encoder.Encode(CharSpan::fromCharString(dev->GetBridgedAttributes().hardwareVersionString.c_str()));
75+
break;
76+
case BasicInformation::Attributes::SoftwareVersion::Id:
77+
encoder.Encode(dev->GetBridgedAttributes().softwareVersion);
78+
break;
79+
case BasicInformation::Attributes::SoftwareVersionString::Id:
80+
encoder.Encode(CharSpan::fromCharString(dev->GetBridgedAttributes().softwareVersionString.c_str()));
81+
break;
5582
default:
5683
return CHIP_ERROR_INVALID_ARGUMENT;
5784
}

0 commit comments

Comments
 (0)