Skip to content

Commit 0e3434a

Browse files
Implementation of UniqueID attribute in Bridge app (project-chip#35303)
* Implementation of UniqueID attribute in Bridge app * Restyled by clang-format * Addree review comment --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 125b8e7 commit 0e3434a

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

examples/bridge-app/linux/Device.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919

2020
#include "Device.h"
2121

22+
#include <crypto/RandUtils.h>
2223
#include <cstdio>
2324
#include <platform/CHIPDeviceLayer.h>
2425

2526
#include <string>
2627

28+
using namespace chip;
2729
using namespace chip::app::Clusters::Actions;
2830

2931
Device::Device(const char * szDeviceName, std::string szLocation)
3032
{
3133
chip::Platform::CopyString(mName, szDeviceName);
34+
chip::Platform::CopyString(mUniqueId, "");
3235
mLocation = szLocation;
3336
mReachable = false;
3437
mEndpointId = 0;
@@ -74,6 +77,12 @@ void Device::SetName(const char * szName)
7477
}
7578
}
7679

80+
void Device::SetUniqueId(const char * szDeviceUniqueId)
81+
{
82+
chip::Platform::CopyString(mUniqueId, szDeviceUniqueId);
83+
ChipLogProgress(DeviceLayer, "Device[%s]: New UniqueId=\"%s\"", mName, mUniqueId);
84+
}
85+
7786
void Device::SetLocation(std::string szLocation)
7887
{
7988
bool changed = (mLocation.compare(szLocation) != 0);
@@ -88,6 +97,23 @@ void Device::SetLocation(std::string szLocation)
8897
}
8998
}
9099

100+
void Device::GenerateUniqueId()
101+
{
102+
// Ensure the buffer is zeroed out
103+
memset(mUniqueId, 0, kDeviceUniqueIdSize + 1);
104+
105+
static const char kRandCharChoices[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
106+
107+
// Prefix the generated value with "GEN-"
108+
memcpy(mUniqueId, "GEN-", 4);
109+
for (unsigned idx = 4; idx < kDeviceUniqueIdSize; idx++)
110+
{
111+
mUniqueId[idx] = kRandCharChoices[Crypto::GetRandU8() % (sizeof(kRandCharChoices) - 1)];
112+
}
113+
114+
mUniqueId[kDeviceUniqueIdSize] = '\0'; // Ensure null-termination
115+
}
116+
91117
DeviceOnOff::DeviceOnOff(const char * szDeviceName, std::string szLocation) : Device(szDeviceName, szLocation)
92118
{
93119
mOn = false;

examples/bridge-app/linux/include/Device.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
class Device
3131
{
3232
public:
33-
static const int kDeviceNameSize = 32;
33+
static const int kDeviceNameSize = 32;
34+
static const int kDeviceUniqueIdSize = 32;
3435

3536
enum Changed_t
3637
{
@@ -46,12 +47,15 @@ class Device
4647
bool IsReachable();
4748
void SetReachable(bool aReachable);
4849
void SetName(const char * szDeviceName);
50+
void SetUniqueId(const char * szDeviceUniqueId);
4951
void SetLocation(std::string szLocation);
52+
void GenerateUniqueId();
5053
inline void SetEndpointId(chip::EndpointId id) { mEndpointId = id; };
5154
inline chip::EndpointId GetEndpointId() { return mEndpointId; };
5255
inline void SetParentEndpointId(chip::EndpointId id) { mParentEndpointId = id; };
5356
inline chip::EndpointId GetParentEndpointId() { return mParentEndpointId; };
5457
inline char * GetName() { return mName; };
58+
inline char * GetUniqueId() { return mUniqueId; };
5559
inline std::string GetLocation() { return mLocation; };
5660
inline std::string GetZone() { return mZone; };
5761
inline void SetZone(std::string zone) { mZone = zone; };
@@ -60,8 +64,9 @@ class Device
6064
virtual void HandleDeviceChange(Device * device, Device::Changed_t changeMask) = 0;
6165

6266
protected:
63-
bool mReachable;
64-
char mName[kDeviceNameSize];
67+
bool mReachable = false;
68+
char mName[kDeviceNameSize + 1] = { 0 };
69+
char mUniqueId[kDeviceUniqueIdSize + 1] = { 0 };
6570
std::string mLocation;
6671
chip::EndpointId mEndpointId;
6772
chip::EndpointId mParentEndpointId;

examples/bridge-app/linux/main.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ using namespace chip::app::Clusters;
6363
namespace {
6464

6565
const int kNodeLabelSize = 32;
66+
const int kUniqueIdSize = 32;
6667
// Current ZCL implementation of Struct uses a max-size array of 254 bytes
6768
const int kDescriptorAttributeArraySize = 254;
6869

@@ -126,6 +127,7 @@ DECLARE_DYNAMIC_ATTRIBUTE(Descriptor::Attributes::DeviceTypeList::Id, ARRAY, kDe
126127
DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(bridgedDeviceBasicAttrs)
127128
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::NodeLabel::Id, CHAR_STRING, kNodeLabelSize, 0), /* NodeLabel */
128129
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::Reachable::Id, BOOLEAN, 1, 0), /* Reachable */
130+
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::UniqueID::Id, CHAR_STRING, kUniqueIdSize, 0),
129131
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::FeatureMap::Id, BITMAP32, 4, 0), /* feature map */
130132
DECLARE_DYNAMIC_ATTRIBUTE_LIST_END();
131133

@@ -277,6 +279,12 @@ int AddDeviceEndpoint(Device * dev, EmberAfEndpointType * ep, const Span<const E
277279
{
278280
ChipLogProgress(DeviceLayer, "Added device %s to dynamic endpoint %d (index=%d)", dev->GetName(),
279281
gCurrentEndpointId, index);
282+
283+
if (dev->GetUniqueId()[0] == '\0')
284+
{
285+
dev->GenerateUniqueId();
286+
}
287+
280288
return index;
281289
}
282290
if (err != CHIP_ERROR_ENDPOINT_EXISTS)
@@ -457,6 +465,11 @@ Protocols::InteractionModel::Status HandleReadBridgedDeviceBasicAttribute(Device
457465
MutableByteSpan zclNameSpan(buffer, maxReadLength);
458466
MakeZclCharString(zclNameSpan, dev->GetName());
459467
}
468+
else if ((attributeId == UniqueID::Id) && (maxReadLength == 32))
469+
{
470+
MutableByteSpan zclUniqueIdSpan(buffer, maxReadLength);
471+
MakeZclCharString(zclUniqueIdSpan, dev->GetUniqueId());
472+
}
460473
else if ((attributeId == ClusterRevision::Id) && (maxReadLength == 2))
461474
{
462475
uint16_t rev = ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_REVISION;

0 commit comments

Comments
 (0)