Skip to content

Commit 1066803

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Use the right types for enums in Accessors method signatures. (#13111)
We're using the underlying integer types right now. Use the right enum types. The change to use a "using" declaration for the NumericAttributeTraits is just to make the code more readable when the template arg is long. There are no other changes there.
1 parent d924e39 commit 1066803

File tree

8 files changed

+11454
-9168
lines changed

8 files changed

+11454
-9168
lines changed

examples/all-clusters-app/esp32/main/main.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -241,9 +241,10 @@ class EditAttributeListModel : public ListScreen::Model
241241
ESP_LOGI(TAG, "name and cluster: '%s' (%s)", name.c_str(), cluster.c_str());
242242
if (name == "State" && cluster == "Lock")
243243
{
244+
using namespace chip::app::Clusters;
244245
// update the doorlock attribute here
245-
uint8_t attributeValue = value == "Closed" ? EMBER_ZCL_DOOR_LOCK_STATE_LOCKED : EMBER_ZCL_DOOR_LOCK_STATE_UNLOCKED;
246-
chip::app::Clusters::DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, attributeValue);
246+
auto attributeValue = value == "Closed" ? DoorLock::DlLockState::kLocked : DoorLock::DlLockState::kUnlocked;
247+
DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, attributeValue);
247248
}
248249
}
249250
}
@@ -472,7 +473,8 @@ void SetupPretendDevices()
472473
AddCluster("Lock");
473474
AddAttribute("State", "Open");
474475
// write the door lock state
475-
chip::app::Clusters::DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, EMBER_ZCL_DOOR_LOCK_STATE_UNLOCKED);
476+
chip::app::Clusters::DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT,
477+
chip::app::Clusters::DoorLock::DlLockState::kUnlocked);
476478
AddDevice("Garage 1");
477479
AddEndpoint("Door 1");
478480
AddCluster("Door");

src/app/clusters/door-lock-server/door-lock-server.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ bool DoorLockServer::SetLockState(chip::EndpointId endpointId, DlLockState newLo
7070

7171
emberAfDoorLockClusterPrintln("Setting Lock State to '%hhu'", lockState);
7272

73-
bool status = Attributes::LockState::Set(endpointId, lockState);
73+
bool status = (Attributes::LockState::Set(endpointId, newLockState) == EMBER_ZCL_STATUS_SUCCESS);
7474
if (!status)
7575
{
7676
ChipLogError(Zcl, "Unable to set the Lock State to %hhu: internal error", lockState);
@@ -79,13 +79,13 @@ bool DoorLockServer::SetLockState(chip::EndpointId endpointId, DlLockState newLo
7979
return status;
8080
}
8181

82-
bool DoorLockServer::SetActuatorState(chip::EndpointId endpointId, bool newActuatorState)
82+
bool DoorLockServer::SetActuatorEnabled(chip::EndpointId endpointId, bool newActuatorState)
8383
{
8484
auto actuatorState = static_cast<uint8_t>(newActuatorState);
8585

8686
emberAfDoorLockClusterPrintln("Setting Actuator State to '%hhu'", actuatorState);
8787

88-
bool status = Attributes::LockState::Set(endpointId, actuatorState);
88+
bool status = (Attributes::ActuatorEnabled::Set(endpointId, newActuatorState) == EMBER_ZCL_STATUS_SUCCESS);
8989
if (!status)
9090
{
9191
ChipLogError(Zcl, "Unable to set the Actuator State to %hhu: internal error", actuatorState);
@@ -94,12 +94,12 @@ bool DoorLockServer::SetActuatorState(chip::EndpointId endpointId, bool newActua
9494
return false;
9595
}
9696

97-
bool DoorLockServer::SetDoorState(chip::EndpointId endpointId, DlLockState newDoorState)
97+
bool DoorLockServer::SetDoorState(chip::EndpointId endpointId, DlDoorState newDoorState)
9898
{
9999
auto doorState = static_cast<uint8_t>(newDoorState);
100100

101101
emberAfDoorLockClusterPrintln("Setting Door State to '%hhu'", doorState);
102-
bool status = Attributes::DoorState::Set(endpointId, doorState);
102+
bool status = (Attributes::DoorState::Set(endpointId, newDoorState) == EMBER_ZCL_STATUS_SUCCESS);
103103

104104
if (!status)
105105
{

src/app/clusters/door-lock-server/door-lock-server.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class DoorLockServer
3939
void InitServer(chip::EndpointId endpointId);
4040

4141
bool SetLockState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState newLockState);
42-
bool SetActuatorState(chip::EndpointId endpointId, bool newActuatorState);
43-
bool SetDoorState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState newDoorState);
42+
bool SetActuatorEnabled(chip::EndpointId endpointId, bool newActuatorState);
43+
bool SetDoorState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlDoorState newDoorState);
4444

4545
bool SetLanguage(chip::EndpointId endpointId, const char * newLanguage);
4646
bool SetAutoRelockTime(chip::EndpointId, uint32_t newAutoRelockTimeSec);

src/app/zap-templates/common/attributes/Accessors.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
const zapPath = '../../../../../third_party/zap/repo/dist/src-electron/';
1919
const ListHelper = require('../../common/ListHelper.js');
2020
const StringHelper = require('../../common/StringHelper.js');
21+
const appHelper = require('../../templates/app/helper.js');
2122
const cHelper = require(zapPath + 'generator/helper-c.js')
2223
const zclHelper = require(zapPath + 'generator/helper-zcl.js')
2324
const templateUtil = require(zapPath + 'generator/template-util.js')
@@ -59,8 +60,8 @@ async function accessorGetterType(attr)
5960
type = "chip::MutableByteSpan";
6061
} else {
6162
mayNeedPointer = true;
62-
const options = { 'hash' : {} };
63-
type = await zclHelper.asUnderlyingZclType.call(this, attr.type, options);
63+
const options = { 'hash' : { forceNotNullable : true, forceNotOptional : true, ns : this.parent.name } };
64+
type = await appHelper.zapTypeToEncodableClusterObjectType.call(this, attr.type, options);
6465
}
6566

6667
if (attr.isNullable) {
@@ -84,7 +85,9 @@ async function accessorTraitType(type)
8485
return `OddSizedInteger<${size}, ${signed}>`;
8586
}
8687
}
87-
return zclHelper.asUnderlyingZclType.call(this, type, { 'hash' : {} });
88+
89+
const options = { 'hash' : { forceNotNullable : true, forceNotOptional : true, ns : this.parent.name } };
90+
return appHelper.zapTypeToEncodableClusterObjectType.call(this, type, options);
8891
}
8992

9093
async function typeAsDelimitedMacro(type)

src/app/zap-templates/templates/app/attributes/Accessors-src.zapt

+18-15
Original file line numberDiff line numberDiff line change
@@ -58,30 +58,31 @@ EmberAfStatus Get(chip::EndpointId endpoint, {{accessorGetterType this}} value)
5858
{{>value}}.reduce_size(length);
5959
return status;
6060
{{else}}
61-
NumericAttributeTraits<{{accessorTraitType type}}>::StorageType temp;
62-
uint8_t * readable = NumericAttributeTraits<{{accessorTraitType type}}>::ToAttributeStoreRepresentation(temp);
61+
using Traits = NumericAttributeTraits<{{accessorTraitType type}}>;
62+
Traits::StorageType temp;
63+
uint8_t * readable = Traits::ToAttributeStoreRepresentation(temp);
6364
EmberAfStatus status = emberAfReadServerAttribute(endpoint, {{>clusterId}}, Id, readable, sizeof(temp));
6465
VerifyOrReturnError(EMBER_ZCL_STATUS_SUCCESS == status, status);
6566
{{#if isNullable}}
66-
if (NumericAttributeTraits<{{accessorTraitType type}}>::IsNullValue(temp))
67+
if (Traits::IsNullValue(temp))
6768
{
6869
value.SetNull();
6970
}
7071
else
7172
{
72-
value.SetNonNull() = NumericAttributeTraits<{{accessorTraitType type}}>::StorageToWorking(temp);
73+
value.SetNonNull() = Traits::StorageToWorking(temp);
7374
}
7475
{{else}}
75-
if (!NumericAttributeTraits<{{accessorTraitType type}}>::CanRepresentValue(/* isNullable = */ {{isNullable}}, temp))
76+
if (!Traits::CanRepresentValue(/* isNullable = */ {{isNullable}}, temp))
7677
{
7778
return EMBER_ZCL_STATUS_CONSTRAINT_ERROR;
7879
}
79-
*value = NumericAttributeTraits<{{accessorTraitType type}}>::StorageToWorking(temp);
80+
*value = Traits::StorageToWorking(temp);
8081
{{/if}}
8182
return status;
8283
{{/if}}
8384
}
84-
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value)
85+
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name forceNotNullable=true forceNotOptional=true}} value)
8586
{
8687
{{~#if (isString type)}}
8788
{{~#*inline "lengthType"}}uint{{#if (isShortString type)}}8{{else}}16{{/if}}_t{{/inline}}
@@ -93,13 +94,14 @@ EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value)
9394
memcpy(&zclString[{{>sizingBytes}}], value.data(), value.size());
9495
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, zclString, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
9596
{{else}}
96-
if (!NumericAttributeTraits<{{accessorTraitType type}}>::CanRepresentValue(/* isNullable = */ {{isNullable}}, value))
97+
using Traits = NumericAttributeTraits<{{accessorTraitType type}}>;
98+
if (!Traits::CanRepresentValue(/* isNullable = */ {{isNullable}}, value))
9799
{
98100
return EMBER_ZCL_STATUS_CONSTRAINT_ERROR;
99101
}
100-
NumericAttributeTraits<{{accessorTraitType type}}>::StorageType storageValue;
101-
NumericAttributeTraits<{{accessorTraitType type}}>::WorkingToStorage(value, storageValue);
102-
uint8_t * writable = NumericAttributeTraits<{{accessorTraitType type}}>::ToAttributeStoreRepresentation(storageValue);
102+
Traits::StorageType storageValue;
103+
Traits::WorkingToStorage(value, storageValue);
104+
uint8_t * writable = Traits::ToAttributeStoreRepresentation(storageValue);
103105
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, writable, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
104106
{{/if}}
105107
}
@@ -111,14 +113,15 @@ EmberAfStatus SetNull(chip::EndpointId endpoint)
111113
uint8_t zclString[{{>sizingBytes}}] = { {{#if (isShortString type)}}0xFF{{else}}0xFF, 0xFF{{/if}} };
112114
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, zclString, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
113115
{{else}}
114-
NumericAttributeTraits<{{accessorTraitType type}}>::StorageType value;
115-
NumericAttributeTraits<{{accessorTraitType type}}>::SetNull(value);
116-
uint8_t * writable = NumericAttributeTraits<{{accessorTraitType type}}>::ToAttributeStoreRepresentation(value);
116+
using Traits = NumericAttributeTraits<{{accessorTraitType type}}>;
117+
Traits::StorageType value;
118+
Traits::SetNull(value);
119+
uint8_t * writable = Traits::ToAttributeStoreRepresentation(value);
117120
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, writable, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
118121
{{/if}}
119122
}
120123

121-
EmberAfStatus Set(chip::EndpointId endpoint, const DataModel::Nullable<{{asUnderlyingZclType type}}> & value)
124+
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name isArgument=true forceNotOptional=true}} value)
122125
{
123126
if (value.IsNull()) {
124127
return SetNull(endpoint);

src/app/zap-templates/templates/app/attributes/Accessors.zapt

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <app/data-model/Nullable.h>
1111
#include <app/util/af-types.h>
12+
#include <app-common/zap-generated/cluster-objects.h>
1213
#include <lib/support/Span.h>
1314

1415
namespace chip {
@@ -26,10 +27,10 @@ namespace Attributes {
2627
{{else if (canHaveSimpleAccessors this)}}
2728
namespace {{asUpperCamelCase label}} {
2829
EmberAfStatus Get(chip::EndpointId endpoint, {{accessorGetterType this}} value); // {{type}} {{isArray}}
29-
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value);
30+
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name forceNotNullable=true forceNotOptional=true}} value);
3031
{{#if isNullable}}
3132
EmberAfStatus SetNull(chip::EndpointId endpoint);
32-
EmberAfStatus Set(chip::EndpointId endpoint, const DataModel::Nullable<{{asUnderlyingZclType type}}> & value);
33+
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name isArgument=true forceNotOptional=true}} value);
3334
{{/if}}
3435
} // namespace {{asUpperCamelCase label}}
3536

0 commit comments

Comments
 (0)