Skip to content

Commit

Permalink
Allow piecemeal migration from EmberAf non-enum-class enums to cluste…
Browse files Browse the repository at this point in the history
…r-objects enum classes (project-chip#11900)

* Enable us to migrate enums to enum class one by one.

Introduces an explicit list of spec enums that are generated as not
enum class.  We can then drive this list down to 0.

* Only generate untyped enums for enums we are already using.

* Fix darwin tests to work with the new enum classes.

* Fix Android codegen to work with the new enum classes.
  • Loading branch information
bzbarsky-apple authored Nov 29, 2021
1 parent 3db5731 commit 489de97
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 1,301 deletions.
4 changes: 4 additions & 0 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,22 @@ namespace Clusters {
{{#zcl_clusters}}
namespace {{asUpperCamelCase name}} {
{{#zcl_enums}}
{{#if (isWeaklyTypedEnum label)}}
// Need to convert consumers to using the new enum classes, so we
// don't just have casts all over.
#ifdef CHIP_USE_ENUM_CLASS_FOR_IM_ENUM
{{/if}}
// Enum for {{label}}
enum class {{asType label}} : {{asUnderlyingZclType type}} {
{{#zcl_enum_items}}
k{{asUpperCamelCase label}} = {{asHex value 2}},
{{/zcl_enum_items}}
};
{{#if (isWeaklyTypedEnum label)}}
#else // CHIP_USE_ENUM_CLASS_FOR_IM_ENUM
using {{asType label}} = EmberAf{{asType label}};
#endif
{{/if}}
{{/zcl_enums}}

{{#zcl_bitmaps}}
Expand Down
2 changes: 2 additions & 0 deletions src/app/zap-templates/templates/app/enums.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
{{#zcl_enums}}
{{#unless (isStrEqual label "Status")}}
{{#unless (isStrEqual label "ReportingDirection")}}
{{#if (isWeaklyTypedEnum label)}}

// Enum for {{label}}
enum EmberAf{{asType label}} : {{asUnderlyingZclType type}} {
{{#zcl_enum_items}}
{{ident}}EMBER_ZCL_{{asDelimitedMacro parent.label}}_{{asDelimitedMacro label}} = {{value}},
{{/zcl_enum_items}}
};
{{/if}}
{{/unless}}
{{/unless}}
{{/zcl_enums}}
Expand Down
82 changes: 82 additions & 0 deletions src/app/zap-templates/templates/app/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,87 @@ async function getResponseCommandName(responseRef, options)
return queryCommand.selectCommandById(db, responseRef, pkgId).then(response => asUpperCamelCase(response.name));
}

// Allow-list of enums that we generate as enums, not enum classes. The goal is
// to drive this down to 0.
function isWeaklyTypedEnum(label)
{
return [
"ApplicationBasicStatus",
"ApplicationLauncherStatus",
"AttributeWritePermission",
"AudioOutputType",
"BarrierControlBarrierPosition",
"BarrierControlMovingState",
"BootReasonType",
"ChangeReasonEnum",
"ColorControlOptions",
"ColorLoopAction",
"ColorLoopDirection",
"ColorMode",
"ContentLaunchStatus",
"ContentLaunchStreamingType",
"DoorLockEventSource",
"DoorLockEventType",
"DoorLockOperatingMode",
"DoorLockOperationEventCode",
"DoorLockProgrammingEventCode",
"DoorLockState",
"DoorLockUserStatus",
"DoorLockUserType",
"DoorState",
"EnhancedColorMode",
"HardwareFaultType",
"HueDirection",
"HueMoveMode",
"HueStepMode",
"IasEnrollResponseCode",
"IasZoneState",
"IasZoneType",
"IdentifyEffectIdentifier",
"IdentifyEffectVariant",
"IdentifyIdentifyType",
"InterfaceType",
"KeypadInputCecKeyCode",
"KeypadInputStatus",
"KeypadLockout",
"LevelControlOptions",
"MediaInputType",
"MediaPlaybackState",
"MediaPlaybackStatus",
"MoveMode",
"NetworkCommissioningError",
"NetworkFaultType",
"NodeOperationalCertStatus",
"OTAAnnouncementReason",
"OTAApplyUpdateAction",
"OTADownloadProtocol",
"OTAQueryStatus",
"OnOffDelayedAllOffEffectVariant",
"OnOffDyingLightEffectVariant",
"OnOffEffectIdentifier",
"PHYRateType",
"RadioFaultType",
"RoutingRole",
"RegulatoryLocationType",
"SaturationMoveMode",
"SaturationStepMode",
"SecurityType",
"SetpointAdjustMode",
"SimpleEnum",
"StartUpOnOffValue",
"StatusCode",
"StepMode",
"TemperatureDisplayMode",
"ThermostatControlSequence",
"ThermostatRunningMode",
"ThermostatSystemMode",
"UpdateStateEnum",
"WcEndProductType",
"WcType",
"WiFiVersionType",
].includes(label);
}

//
// Module exports
//
Expand All @@ -527,3 +608,4 @@ exports.zapTypeToEncodableClusterObjectType = zapTypeToEncodableClusterObjectTyp
exports.zapTypeToDecodableClusterObjectType = zapTypeToDecodableClusterObjectType;
exports.zapTypeToPythonClusterObjectType = zapTypeToPythonClusterObjectType;
exports.getResponseCommandName = getResponseCommandName;
exports.isWeaklyTypedEnum = isWeaklyTypedEnum;
8 changes: 4 additions & 4 deletions src/controller/java/templates/CHIPReadCallbacks-src.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb
{{/if}}

{{#if isOptional}}
{{chipType}} {{asLowerCamelCase name}}Value;
{{zapTypeToDecodableClusterObjectType type forceNotOptional=true forceNotNullable=true ns=parent.parent.name}} {{asLowerCamelCase name}}Value;
{{#if isNullable}}
{{asLowerCamelCase name}}HasValue = entry.{{asLowerCamelCase name}}.HasValue();
if ({{asLowerCamelCase name}}HasValue) {
Expand All @@ -201,7 +201,7 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb

{{#unless isOptional}}
{{#unless isNullable}}
{{chipType}} {{asLowerCamelCase name}}Value = entry.{{asLowerCamelCase name}};
{{zapTypeToDecodableClusterObjectType type ns=parent.parent.name}} {{asLowerCamelCase name}}Value = entry.{{asLowerCamelCase name}};
{{/unless}}
{{/unless}}

Expand Down Expand Up @@ -265,13 +265,13 @@ void CHIP{{asUpperCamelCase parent.name}}{{asUpperCamelCase name}}AttributeCallb
bool entryNull = false;
{{#unless isStruct}}
{{#if isNullable}}
{{chipType}} entryValue;
{{zapTypeToDecodableClusterObjectType type ns=parent.name forceNotList=true}} entryValue;
entryNull = entry.IsNull();
if (!entryNull) {
entryValue = entry.Value();
}
{{else}}
{{chipType}} entryValue = entry;
{{zapTypeToDecodableClusterObjectType type ns=parent.name forceNotList=true}} entryValue = entry;
{{/if}}
{{/unless}}

Expand Down
2 changes: 1 addition & 1 deletion src/controller/java/templates/partials/decode_value.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ if ({{source}}.Value().IsNull()) {
{{else}}
std::string {{target}}ClassName = "java/lang/{{asJavaBasicTypeForZclType type true}}";
std::string {{target}}CtorSignature = "({{asJniSignature type false}})V";
chip::JniReferences::GetInstance().CreateBoxedObject<{{chipType}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), {{>item}}, {{target}});
chip::JniReferences::GetInstance().CreateBoxedObject<{{zapTypeToDecodableClusterObjectType type ns=parent.parent.name forceNotNullable=true forceNotOptional=true}}>({{target}}ClassName.c_str(), {{target}}CtorSignature.c_str(), {{>item}}, {{target}});
{{/if}}
{{#if isOptional}}
chip::JniReferences::GetInstance().CreateOptional({{target}}, {{target}});
Expand Down
Loading

0 comments on commit 489de97

Please sign in to comment.