Skip to content

Commit

Permalink
Implementation of UniqueID attribute in Bridge app (project-chip#35303)
Browse files Browse the repository at this point in the history
* Implementation of UniqueID attribute in Bridge app

* Restyled by clang-format

* Addree review comment

---------

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
yufengwangca and restyled-commits authored Aug 30, 2024
1 parent 125b8e7 commit 0e3434a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 3 deletions.
26 changes: 26 additions & 0 deletions examples/bridge-app/linux/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,19 @@

#include "Device.h"

#include <crypto/RandUtils.h>
#include <cstdio>
#include <platform/CHIPDeviceLayer.h>

#include <string>

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

Device::Device(const char * szDeviceName, std::string szLocation)
{
chip::Platform::CopyString(mName, szDeviceName);
chip::Platform::CopyString(mUniqueId, "");
mLocation = szLocation;
mReachable = false;
mEndpointId = 0;
Expand Down Expand Up @@ -74,6 +77,12 @@ void Device::SetName(const char * szName)
}
}

void Device::SetUniqueId(const char * szDeviceUniqueId)
{
chip::Platform::CopyString(mUniqueId, szDeviceUniqueId);
ChipLogProgress(DeviceLayer, "Device[%s]: New UniqueId=\"%s\"", mName, mUniqueId);
}

void Device::SetLocation(std::string szLocation)
{
bool changed = (mLocation.compare(szLocation) != 0);
Expand All @@ -88,6 +97,23 @@ void Device::SetLocation(std::string szLocation)
}
}

void Device::GenerateUniqueId()
{
// Ensure the buffer is zeroed out
memset(mUniqueId, 0, kDeviceUniqueIdSize + 1);

static const char kRandCharChoices[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";

// Prefix the generated value with "GEN-"
memcpy(mUniqueId, "GEN-", 4);
for (unsigned idx = 4; idx < kDeviceUniqueIdSize; idx++)
{
mUniqueId[idx] = kRandCharChoices[Crypto::GetRandU8() % (sizeof(kRandCharChoices) - 1)];
}

mUniqueId[kDeviceUniqueIdSize] = '\0'; // Ensure null-termination
}

DeviceOnOff::DeviceOnOff(const char * szDeviceName, std::string szLocation) : Device(szDeviceName, szLocation)
{
mOn = false;
Expand Down
11 changes: 8 additions & 3 deletions examples/bridge-app/linux/include/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@
class Device
{
public:
static const int kDeviceNameSize = 32;
static const int kDeviceNameSize = 32;
static const int kDeviceUniqueIdSize = 32;

enum Changed_t
{
Expand All @@ -46,12 +47,15 @@ class Device
bool IsReachable();
void SetReachable(bool aReachable);
void SetName(const char * szDeviceName);
void SetUniqueId(const char * szDeviceUniqueId);
void SetLocation(std::string szLocation);
void GenerateUniqueId();
inline void SetEndpointId(chip::EndpointId id) { mEndpointId = id; };
inline chip::EndpointId GetEndpointId() { return mEndpointId; };
inline void SetParentEndpointId(chip::EndpointId id) { mParentEndpointId = id; };
inline chip::EndpointId GetParentEndpointId() { return mParentEndpointId; };
inline char * GetName() { return mName; };
inline char * GetUniqueId() { return mUniqueId; };
inline std::string GetLocation() { return mLocation; };
inline std::string GetZone() { return mZone; };
inline void SetZone(std::string zone) { mZone = zone; };
Expand All @@ -60,8 +64,9 @@ class Device
virtual void HandleDeviceChange(Device * device, Device::Changed_t changeMask) = 0;

protected:
bool mReachable;
char mName[kDeviceNameSize];
bool mReachable = false;
char mName[kDeviceNameSize + 1] = { 0 };
char mUniqueId[kDeviceUniqueIdSize + 1] = { 0 };
std::string mLocation;
chip::EndpointId mEndpointId;
chip::EndpointId mParentEndpointId;
Expand Down
13 changes: 13 additions & 0 deletions examples/bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ using namespace chip::app::Clusters;
namespace {

const int kNodeLabelSize = 32;
const int kUniqueIdSize = 32;
// Current ZCL implementation of Struct uses a max-size array of 254 bytes
const int kDescriptorAttributeArraySize = 254;

Expand Down Expand Up @@ -126,6 +127,7 @@ DECLARE_DYNAMIC_ATTRIBUTE(Descriptor::Attributes::DeviceTypeList::Id, ARRAY, kDe
DECLARE_DYNAMIC_ATTRIBUTE_LIST_BEGIN(bridgedDeviceBasicAttrs)
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::NodeLabel::Id, CHAR_STRING, kNodeLabelSize, 0), /* NodeLabel */
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::Reachable::Id, BOOLEAN, 1, 0), /* Reachable */
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::UniqueID::Id, CHAR_STRING, kUniqueIdSize, 0),
DECLARE_DYNAMIC_ATTRIBUTE(BridgedDeviceBasicInformation::Attributes::FeatureMap::Id, BITMAP32, 4, 0), /* feature map */
DECLARE_DYNAMIC_ATTRIBUTE_LIST_END();

Expand Down Expand Up @@ -277,6 +279,12 @@ int AddDeviceEndpoint(Device * dev, EmberAfEndpointType * ep, const Span<const E
{
ChipLogProgress(DeviceLayer, "Added device %s to dynamic endpoint %d (index=%d)", dev->GetName(),
gCurrentEndpointId, index);

if (dev->GetUniqueId()[0] == '\0')
{
dev->GenerateUniqueId();
}

return index;
}
if (err != CHIP_ERROR_ENDPOINT_EXISTS)
Expand Down Expand Up @@ -457,6 +465,11 @@ Protocols::InteractionModel::Status HandleReadBridgedDeviceBasicAttribute(Device
MutableByteSpan zclNameSpan(buffer, maxReadLength);
MakeZclCharString(zclNameSpan, dev->GetName());
}
else if ((attributeId == UniqueID::Id) && (maxReadLength == 32))
{
MutableByteSpan zclUniqueIdSpan(buffer, maxReadLength);
MakeZclCharString(zclUniqueIdSpan, dev->GetUniqueId());
}
else if ((attributeId == ClusterRevision::Id) && (maxReadLength == 2))
{
uint16_t rev = ZCL_BRIDGED_DEVICE_BASIC_INFORMATION_CLUSTER_REVISION;
Expand Down

0 comments on commit 0e3434a

Please sign in to comment.