Skip to content

Commit

Permalink
Use attributes accessors into src/app/clusters/basic/basic.cpp (#10843)
Browse files Browse the repository at this point in the history
* Use attributes accessors into src/app/clusters/basic/basic.cpp

* Update generated accessors
  • Loading branch information
vivien-apple authored and pull[bot] committed Nov 15, 2021
1 parent c6d94cd commit fc4f36c
Show file tree
Hide file tree
Showing 9 changed files with 7,061 additions and 5,907 deletions.
75 changes: 35 additions & 40 deletions src/app/clusters/basic/basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,81 +18,76 @@

#include "basic.h"

#include <app-common/zap-generated/attributes/Accessors.h>
#include <platform/CHIPDeviceLayer.h>

#include <app-common/zap-generated/att-storage.h>
#include <app-common/zap-generated/attribute-id.h>
#include <app-common/zap-generated/attribute-type.h>
#include <app-common/zap-generated/cluster-id.h>
#include <app/util/af.h>
#include <app/util/attribute-storage.h>
#include <lib/support/ZclString.h>
#include <protocols/interaction_model/Constants.h>

#include <cstring>

using namespace chip;
using namespace chip::app::Clusters::Basic;
using namespace chip::DeviceLayer;

void emberAfBasicClusterServerInitCallback(chip::EndpointId endpoint)
{
uint16_t vendorId;
uint16_t productId;
uint16_t productRevision;
uint32_t firmwareRevision;
char cString[65];
uint8_t bufferMemory[65];
MutableByteSpan zclString(bufferMemory);
EmberAfStatus status;

if (ConfigurationMgr().GetVendorName(cString, sizeof(cString)) == CHIP_NO_ERROR)
char vendorName[33];
if (ConfigurationMgr().GetVendorName(vendorName, sizeof(vendorName)) == CHIP_NO_ERROR)
{
MakeZclCharString(zclString, cString);
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_VENDOR_NAME_ATTRIBUTE_ID, CLUSTER_MASK_SERVER, zclString.data(),
ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
status = Attributes::VendorName::Set(endpoint, chip::CharSpan(vendorName, strlen(vendorName)));
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(Zcl, "Error setting Vendor Name: 0x%02x", status));
}

uint16_t vendorId;
if (ConfigurationMgr().GetVendorId(vendorId) == CHIP_NO_ERROR)
{
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_VENDOR_ID_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
reinterpret_cast<uint8_t *>(&vendorId), ZCL_INT16U_ATTRIBUTE_TYPE);
status = Attributes::VendorID::Set(endpoint, vendorId);
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(Zcl, "Error setting Vendor Id: 0x%02x", status));
}

if (ConfigurationMgr().GetProductName(cString, sizeof(cString)) == CHIP_NO_ERROR)
char productName[33];
if (ConfigurationMgr().GetProductName(productName, sizeof(productName)) == CHIP_NO_ERROR)
{
MakeZclCharString(zclString, cString);
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_PRODUCT_NAME_ATTRIBUTE_ID, CLUSTER_MASK_SERVER, zclString.data(),
ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
status = Attributes::ProductName::Set(endpoint, chip::CharSpan(productName, strlen(productName)));
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(Zcl, "Error setting Product Name: 0x%02x", status));
}

uint16_t productId;
if (ConfigurationMgr().GetProductId(productId) == CHIP_NO_ERROR)
{
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_PRODUCT_ID_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
reinterpret_cast<uint8_t *>(&productId), ZCL_INT16U_ATTRIBUTE_TYPE);
status = Attributes::ProductID::Set(endpoint, productId);
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(Zcl, "Error setting Product Id: 0x%02x", status));
}

if (ConfigurationMgr().GetProductRevisionString(cString, sizeof(cString)) == CHIP_NO_ERROR)
char productRevisionString[65];
if (ConfigurationMgr().GetProductRevisionString(productRevisionString, sizeof(productRevisionString)) == CHIP_NO_ERROR)
{
MakeZclCharString(zclString, cString);
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_HARDWARE_VERSION_STRING_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
zclString.data(), ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
status =
Attributes::HardwareVersionString::Set(endpoint, chip::CharSpan(productRevisionString, strlen(productRevisionString)));
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status,
ChipLogError(Zcl, "Error setting Hardware Version String: 0x%02x", status));
}

uint16_t productRevision;
if (ConfigurationMgr().GetProductRevision(productRevision) == CHIP_NO_ERROR)
{
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_HARDWARE_VERSION_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
reinterpret_cast<uint8_t *>(&productRevision), ZCL_INT16U_ATTRIBUTE_TYPE);
status = Attributes::HardwareVersion::Set(endpoint, productRevision);
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(Zcl, "Error setting Hardware Version: 0x%02x", status));
}

if (ConfigurationMgr().GetFirmwareRevisionString(cString, sizeof(cString)) == CHIP_NO_ERROR)
char firmwareRevisionString[65];
if (ConfigurationMgr().GetFirmwareRevisionString(firmwareRevisionString, sizeof(firmwareRevisionString)) == CHIP_NO_ERROR)
{
MakeZclCharString(zclString, cString);
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_SOFTWARE_VERSION_STRING_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
zclString.data(), ZCL_CHAR_STRING_ATTRIBUTE_TYPE);
status = Attributes::SoftwareVersionString::Set(endpoint,
chip::CharSpan(firmwareRevisionString, strlen(firmwareRevisionString)));
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status,
ChipLogError(Zcl, "Error setting Software Version String: 0x%02x", status));
}

uint16_t firmwareRevision;
if (ConfigurationMgr().GetFirmwareRevision(firmwareRevision) == CHIP_NO_ERROR)
{
emberAfWriteAttribute(endpoint, ZCL_BASIC_CLUSTER_ID, ZCL_SOFTWARE_VERSION_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
reinterpret_cast<uint8_t *>(&firmwareRevision), ZCL_INT32U_ATTRIBUTE_TYPE);
status = Attributes::HardwareVersion::Set(endpoint, firmwareRevision);
VerifyOrReturn(EMBER_ZCL_STATUS_SUCCESS == status, ChipLogError(Zcl, "Error setting Software Version: 0x%02x", status));
}
}
4 changes: 0 additions & 4 deletions src/app/zap-templates/common/attributes/Accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,6 @@ function isUnsupportedType(type)

function canHaveSimpleAccessors(type)
{
if (StringHelper.isString(type)) {
return false;
}

if (ListHelper.isList(type)) {
return false;
}
Expand Down
30 changes: 25 additions & 5 deletions src/app/zap-templates/templates/app/attributes/Accessors-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,34 @@ namespace Attributes {
{{#if (canHaveSimpleAccessors type)}}
namespace {{asUpperCamelCase label}} {

EmberAfStatus Get(chip::EndpointId endpoint, {{asUnderlyingZclType type}} * {{asLowerCamelCase label}})
{
return emberAfReadServerAttribute(endpoint, Clusters::{{asUpperCamelCase parent.label}}::Id, Id, (uint8_t *) {{asLowerCamelCase label}}, sizeof(*{{asLowerCamelCase label}}));
{{#*inline "clusterId"}}Clusters::{{asUpperCamelCase parent.label}}::Id{{/inline}}
{{#*inline "sizingBytes"}}{{#if (isShortString type)}}1{{else}}2{{/if}}{{/inline}}

EmberAfStatus Get(chip::EndpointId endpoint, {{#if (isCharString type)}}chip::MutableCharSpan{{else if (isOctetString type)}}chip::MutableByteSpan{{else}}{{asUnderlyingZclType type}} *{{/if}} value)
{
{{#if (isString type)}}
VerifyOrReturnError(value.size() == {{maxLength}}, EMBER_ZCL_STATUS_INVALID_ARGUMENT);
uint8_t zclString[{{maxLength}} + {{>sizingBytes}}];
EmberAfStatus status = emberAfReadServerAttribute(endpoint, {{>clusterId}}, Id, zclString, sizeof(zclString));
VerifyOrReturnError(EMBER_ZCL_STATUS_SUCCESS == status, status);
memcpy(value.data(), &zclString[{{>sizingBytes}}], {{maxLength}});
value.reduce_size(emberAf{{#if (isLongString type)}}Long{{/if}}StringLength(zclString));
return status;
{{else}}
return emberAfReadServerAttribute(endpoint, {{>clusterId}}, Id, (uint8_t *) value, sizeof(*value));
{{/if}}
}
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} {{asLowerCamelCase label}})
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value)
{
return emberAfWriteServerAttribute(endpoint, Clusters::{{asUpperCamelCase parent.label}}::Id, Id, (uint8_t *) &{{asLowerCamelCase label}}, ZCL_{{asDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{#if (isString type)}}
VerifyOrReturnError(value.size() <= {{maxLength}}, EMBER_ZCL_STATUS_INVALID_ARGUMENT);
uint8_t zclString[{{maxLength}} + {{>sizingBytes}}];
emberAfCopyInt{{#if (isShortString type)}}8{{else}}16{{/if}}u(zclString, 0, static_cast<uint{{#if (isShortString type)}}8{{else}}16{{/if}}_t>(value.size()));
memcpy(&zclString[{{>sizingBytes}}], value.data(), value.size());
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, zclString, ZCL_{{asDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{else}}
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, (uint8_t *) &value, ZCL_{{asDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{/if}}
}

} // namespace {{asUpperCamelCase label}}
Expand Down
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/attributes/Accessors.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ namespace Attributes {
{{#if clusterRef}}
{{#if (canHaveSimpleAccessors type)}}
namespace {{asUpperCamelCase label}} {
EmberAfStatus Get(chip::EndpointId endpoint, {{asUnderlyingZclType type}} * {{asLowerCamelCase label}}); // {{type}} {{isArray}}
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} {{asLowerCamelCase label}});
EmberAfStatus Get(chip::EndpointId endpoint, {{#if (isCharString type)}}chip::MutableCharSpan{{else if (isOctetString type)}}chip::MutableByteSpan{{else}}{{asUnderlyingZclType type}} *{{/if}} value); // {{type}} {{isArray}}
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value);
} // namespace {{asUpperCamelCase label}}

{{/if}}
Expand Down
2 changes: 1 addition & 1 deletion src/include/platform/ConfigurationManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ConfigurationManager
virtual CHIP_ERROR GetPrimary802154MACAddress(uint8_t * buf) = 0;
virtual CHIP_ERROR GetManufacturingDate(uint16_t & year, uint8_t & month, uint8_t & dayOfMonth) = 0;
virtual CHIP_ERROR GetFirmwareRevisionString(char * buf, size_t bufSize) = 0;
virtual CHIP_ERROR GetFirmwareRevision(uint32_t & firmwareRev) = 0;
virtual CHIP_ERROR GetFirmwareRevision(uint16_t & firmwareRev) = 0;
virtual CHIP_ERROR GetSetupPinCode(uint32_t & setupPinCode) = 0;
virtual CHIP_ERROR GetSetupDiscriminator(uint16_t & setupDiscriminator) = 0;
#if CHIP_ENABLE_ROTATING_DEVICE_ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class GenericConfigurationManagerImpl : public ConfigurationManager
CHIP_ERROR GetProductRevision(uint16_t & productRev) override;
CHIP_ERROR StoreProductRevision(uint16_t productRev) override;
CHIP_ERROR GetFirmwareRevisionString(char * buf, size_t bufSize) override;
CHIP_ERROR GetFirmwareRevision(uint32_t & firmwareRev) override;
CHIP_ERROR GetFirmwareRevision(uint16_t & firmwareRev) override;
CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize, size_t & serialNumLen) override;
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override;
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override;
Expand Down Expand Up @@ -134,7 +134,7 @@ inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetProductId(uint1
}

template <class ImplClass>
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetFirmwareRevision(uint32_t & firmwareRev)
inline CHIP_ERROR GenericConfigurationManagerImpl<ImplClass>::GetFirmwareRevision(uint16_t & firmwareRev)
{
firmwareRev = static_cast<uint32_t>(CHIP_DEVICE_CONFIG_DEVICE_FIRMWARE_REVISION);
return CHIP_NO_ERROR;
Expand Down
2 changes: 1 addition & 1 deletion src/platform/fake/ConfigurationManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class ConfigurationManagerImpl final : public ConfigurationManager
CHIP_ERROR GetProductRevision(uint16_t & productRev) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR StoreProductRevision(uint16_t productRev) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetFirmwareRevisionString(char * buf, size_t bufSize) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetFirmwareRevision(uint32_t & firmwareRev) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetFirmwareRevision(uint16_t & firmwareRev) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetSerialNumber(char * buf, size_t bufSize, size_t & serialNumLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR StoreSerialNumber(const char * serialNum, size_t serialNumLen) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR GetPrimaryMACAddress(MutableByteSpan buf) override { return CHIP_ERROR_NOT_IMPLEMENTED; }
Expand Down
Loading

0 comments on commit fc4f36c

Please sign in to comment.