Skip to content

Commit

Permalink
Use the right types for enums in Accessors method signatures. (#13111)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bzbarsky-apple authored Dec 18, 2021
1 parent 4867993 commit c46fc9b
Show file tree
Hide file tree
Showing 8 changed files with 11,454 additions and 9,168 deletions.
8 changes: 5 additions & 3 deletions examples/all-clusters-app/esp32/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,9 +241,10 @@ class EditAttributeListModel : public ListScreen::Model
ESP_LOGI(TAG, "name and cluster: '%s' (%s)", name.c_str(), cluster.c_str());
if (name == "State" && cluster == "Lock")
{
using namespace chip::app::Clusters;
// update the doorlock attribute here
uint8_t attributeValue = value == "Closed" ? EMBER_ZCL_DOOR_LOCK_STATE_LOCKED : EMBER_ZCL_DOOR_LOCK_STATE_UNLOCKED;
chip::app::Clusters::DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, attributeValue);
auto attributeValue = value == "Closed" ? DoorLock::DlLockState::kLocked : DoorLock::DlLockState::kUnlocked;
DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, attributeValue);
}
}
}
Expand Down Expand Up @@ -472,7 +473,8 @@ void SetupPretendDevices()
AddCluster("Lock");
AddAttribute("State", "Open");
// write the door lock state
chip::app::Clusters::DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT, EMBER_ZCL_DOOR_LOCK_STATE_UNLOCKED);
chip::app::Clusters::DoorLock::Attributes::LockState::Set(DOOR_LOCK_SERVER_ENDPOINT,
chip::app::Clusters::DoorLock::DlLockState::kUnlocked);
AddDevice("Garage 1");
AddEndpoint("Door 1");
AddCluster("Door");
Expand Down
10 changes: 5 additions & 5 deletions src/app/clusters/door-lock-server/door-lock-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ bool DoorLockServer::SetLockState(chip::EndpointId endpointId, DlLockState newLo

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

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

bool DoorLockServer::SetActuatorState(chip::EndpointId endpointId, bool newActuatorState)
bool DoorLockServer::SetActuatorEnabled(chip::EndpointId endpointId, bool newActuatorState)
{
auto actuatorState = static_cast<uint8_t>(newActuatorState);

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

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

bool DoorLockServer::SetDoorState(chip::EndpointId endpointId, DlLockState newDoorState)
bool DoorLockServer::SetDoorState(chip::EndpointId endpointId, DlDoorState newDoorState)
{
auto doorState = static_cast<uint8_t>(newDoorState);

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

if (!status)
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/clusters/door-lock-server/door-lock-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class DoorLockServer
void InitServer(chip::EndpointId endpointId);

bool SetLockState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState newLockState);
bool SetActuatorState(chip::EndpointId endpointId, bool newActuatorState);
bool SetDoorState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlLockState newDoorState);
bool SetActuatorEnabled(chip::EndpointId endpointId, bool newActuatorState);
bool SetDoorState(chip::EndpointId endpointId, chip::app::Clusters::DoorLock::DlDoorState newDoorState);

bool SetLanguage(chip::EndpointId endpointId, const char * newLanguage);
bool SetAutoRelockTime(chip::EndpointId, uint32_t newAutoRelockTimeSec);
Expand Down
9 changes: 6 additions & 3 deletions src/app/zap-templates/common/attributes/Accessors.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
const zapPath = '../../../../../third_party/zap/repo/dist/src-electron/';
const ListHelper = require('../../common/ListHelper.js');
const StringHelper = require('../../common/StringHelper.js');
const appHelper = require('../../templates/app/helper.js');
const cHelper = require(zapPath + 'generator/helper-c.js')
const zclHelper = require(zapPath + 'generator/helper-zcl.js')
const templateUtil = require(zapPath + 'generator/template-util.js')
Expand Down Expand Up @@ -59,8 +60,8 @@ async function accessorGetterType(attr)
type = "chip::MutableByteSpan";
} else {
mayNeedPointer = true;
const options = { 'hash' : {} };
type = await zclHelper.asUnderlyingZclType.call(this, attr.type, options);
const options = { 'hash' : { forceNotNullable : true, forceNotOptional : true, ns : this.parent.name } };
type = await appHelper.zapTypeToEncodableClusterObjectType.call(this, attr.type, options);
}

if (attr.isNullable) {
Expand All @@ -84,7 +85,9 @@ async function accessorTraitType(type)
return `OddSizedInteger<${size}, ${signed}>`;
}
}
return zclHelper.asUnderlyingZclType.call(this, type, { 'hash' : {} });

const options = { 'hash' : { forceNotNullable : true, forceNotOptional : true, ns : this.parent.name } };
return appHelper.zapTypeToEncodableClusterObjectType.call(this, type, options);
}

async function typeAsDelimitedMacro(type)
Expand Down
33 changes: 18 additions & 15 deletions src/app/zap-templates/templates/app/attributes/Accessors-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -58,30 +58,31 @@ EmberAfStatus Get(chip::EndpointId endpoint, {{accessorGetterType this}} value)
{{>value}}.reduce_size(length);
return status;
{{else}}
NumericAttributeTraits<{{accessorTraitType type}}>::StorageType temp;
uint8_t * readable = NumericAttributeTraits<{{accessorTraitType type}}>::ToAttributeStoreRepresentation(temp);
using Traits = NumericAttributeTraits<{{accessorTraitType type}}>;
Traits::StorageType temp;
uint8_t * readable = Traits::ToAttributeStoreRepresentation(temp);
EmberAfStatus status = emberAfReadServerAttribute(endpoint, {{>clusterId}}, Id, readable, sizeof(temp));
VerifyOrReturnError(EMBER_ZCL_STATUS_SUCCESS == status, status);
{{#if isNullable}}
if (NumericAttributeTraits<{{accessorTraitType type}}>::IsNullValue(temp))
if (Traits::IsNullValue(temp))
{
value.SetNull();
}
else
{
value.SetNonNull() = NumericAttributeTraits<{{accessorTraitType type}}>::StorageToWorking(temp);
value.SetNonNull() = Traits::StorageToWorking(temp);
}
{{else}}
if (!NumericAttributeTraits<{{accessorTraitType type}}>::CanRepresentValue(/* isNullable = */ {{isNullable}}, temp))
if (!Traits::CanRepresentValue(/* isNullable = */ {{isNullable}}, temp))
{
return EMBER_ZCL_STATUS_CONSTRAINT_ERROR;
}
*value = NumericAttributeTraits<{{accessorTraitType type}}>::StorageToWorking(temp);
*value = Traits::StorageToWorking(temp);
{{/if}}
return status;
{{/if}}
}
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value)
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name forceNotNullable=true forceNotOptional=true}} value)
{
{{~#if (isString type)}}
{{~#*inline "lengthType"}}uint{{#if (isShortString type)}}8{{else}}16{{/if}}_t{{/inline}}
Expand All @@ -93,13 +94,14 @@ EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value)
memcpy(&zclString[{{>sizingBytes}}], value.data(), value.size());
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, zclString, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{else}}
if (!NumericAttributeTraits<{{accessorTraitType type}}>::CanRepresentValue(/* isNullable = */ {{isNullable}}, value))
using Traits = NumericAttributeTraits<{{accessorTraitType type}}>;
if (!Traits::CanRepresentValue(/* isNullable = */ {{isNullable}}, value))
{
return EMBER_ZCL_STATUS_CONSTRAINT_ERROR;
}
NumericAttributeTraits<{{accessorTraitType type}}>::StorageType storageValue;
NumericAttributeTraits<{{accessorTraitType type}}>::WorkingToStorage(value, storageValue);
uint8_t * writable = NumericAttributeTraits<{{accessorTraitType type}}>::ToAttributeStoreRepresentation(storageValue);
Traits::StorageType storageValue;
Traits::WorkingToStorage(value, storageValue);
uint8_t * writable = Traits::ToAttributeStoreRepresentation(storageValue);
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, writable, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{/if}}
}
Expand All @@ -111,14 +113,15 @@ EmberAfStatus SetNull(chip::EndpointId endpoint)
uint8_t zclString[{{>sizingBytes}}] = { {{#if (isShortString type)}}0xFF{{else}}0xFF, 0xFF{{/if}} };
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, zclString, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{else}}
NumericAttributeTraits<{{accessorTraitType type}}>::StorageType value;
NumericAttributeTraits<{{accessorTraitType type}}>::SetNull(value);
uint8_t * writable = NumericAttributeTraits<{{accessorTraitType type}}>::ToAttributeStoreRepresentation(value);
using Traits = NumericAttributeTraits<{{accessorTraitType type}}>;
Traits::StorageType value;
Traits::SetNull(value);
uint8_t * writable = Traits::ToAttributeStoreRepresentation(value);
return emberAfWriteServerAttribute(endpoint, {{>clusterId}}, Id, writable, ZCL_{{typeAsDelimitedMacro type}}_ATTRIBUTE_TYPE);
{{/if}}
}

EmberAfStatus Set(chip::EndpointId endpoint, const DataModel::Nullable<{{asUnderlyingZclType type}}> & value)
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name isArgument=true forceNotOptional=true}} value)
{
if (value.IsNull()) {
return SetNull(endpoint);
Expand Down
5 changes: 3 additions & 2 deletions src/app/zap-templates/templates/app/attributes/Accessors.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#include <app/data-model/Nullable.h>
#include <app/util/af-types.h>
#include <app-common/zap-generated/cluster-objects.h>
#include <lib/support/Span.h>

namespace chip {
Expand All @@ -26,10 +27,10 @@ namespace Attributes {
{{else if (canHaveSimpleAccessors this)}}
namespace {{asUpperCamelCase label}} {
EmberAfStatus Get(chip::EndpointId endpoint, {{accessorGetterType this}} value); // {{type}} {{isArray}}
EmberAfStatus Set(chip::EndpointId endpoint, {{asUnderlyingZclType type}} value);
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name forceNotNullable=true forceNotOptional=true}} value);
{{#if isNullable}}
EmberAfStatus SetNull(chip::EndpointId endpoint);
EmberAfStatus Set(chip::EndpointId endpoint, const DataModel::Nullable<{{asUnderlyingZclType type}}> & value);
EmberAfStatus Set(chip::EndpointId endpoint, {{zapTypeToEncodableClusterObjectType type ns=parent.name isArgument=true forceNotOptional=true}} value);
{{/if}}
} // namespace {{asUpperCamelCase label}}

Expand Down
Loading

0 comments on commit c46fc9b

Please sign in to comment.