Skip to content

Commit

Permalink
Move cluster-defined enums/bitmaps into a separate header. (#15686)
Browse files Browse the repository at this point in the history
We want to put basic cluster-defined constants (enums/bitmaps) into a
header we can use from anywhere (just like data model concepts like
NodeId and whatnot).  But we don't want that for the C++ structs in
cluster-objects, which can have ABI issues.

Move the constants into a separate header, put that header into
src/lib/core in terms of include dependencies.
  • Loading branch information
bzbarsky-apple authored Mar 2, 2022
1 parent ac101cd commit 169b48a
Show file tree
Hide file tree
Showing 7 changed files with 2,572 additions and 2,042 deletions.
5 changes: 5 additions & 0 deletions src/app/common/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@
"path": "../../zap-templates/templates/app/cluster-objects-src.zapt",
"name": "Cluster objects header for Interaction Model",
"output": "cluster-objects.cpp"
},
{
"path": "../../zap-templates/templates/app/cluster-enums.zapt",
"name": "Enum and bitmap definitions for clusters",
"output": "cluster-enums.h"
}
]
}
46 changes: 46 additions & 0 deletions src/app/zap-templates/templates/app/cluster-enums.zapt
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{{> header}}

#include <stdint.h>

#pragma once

namespace chip {
namespace app {
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}}

// Bitmap for {{label}}
enum class {{asType label}} : {{asUnderlyingZclType type}} {
{{#zcl_bitmap_items}}
k{{asUpperCamelCase label}} = {{asHex mask}},
{{/zcl_bitmap_items}}
};
{{/zcl_bitmaps}}
} // namespace {{asUpperCamelCase name}}

{{/zcl_clusters}}
} // namespace Clusters
} // namespace app
} // namespace chip
30 changes: 1 addition & 29 deletions src/app/zap-templates/templates/app/cluster-objects.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <app/ConcreteAttributePath.h>
#include <app/EventLoggingTypes.h>
#include <app/util/basic-types.h>
#include <lib/core/ClusterEnums.h>
#include <lib/support/BitFlags.h>
#include <protocols/interaction_model/Constants.h>
#include <app-common/zap-generated/enums.h>
Expand All @@ -35,35 +36,6 @@ namespace Structs {

{{#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}}

// Bitmap for {{label}}
enum class {{asType label}} : {{asUnderlyingZclType type}} {
{{#zcl_bitmap_items}}
k{{asUpperCamelCase label}} = {{asHex mask}},
{{/zcl_bitmap_items}}
};
{{/zcl_bitmaps}}

{{#zcl_structs}}
{{#first}}
namespace Structs {
Expand Down
5 changes: 5 additions & 0 deletions src/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ static_library("core") {
output_name = "libChipCore"

sources = [
# For now cluster enum/bitmap definitions are in zzz-generated.
# We should consider putting them directly in this directory
# instead.
"${chip_root}/zzz_generated/app-common/app-common/zap-generated/cluster-enums.h",
"CHIPCallback.h",
"CHIPCircularTLVBuffer.cpp",
"CHIPCircularTLVBuffer.h",
Expand All @@ -100,6 +104,7 @@ static_library("core") {
"CHIPTLVUpdater.cpp",
"CHIPTLVUtilities.cpp",
"CHIPTLVWriter.cpp",
"ClusterEnums.h",
"DataModelTypes.h",
"GroupId.h",
"NodeId.h",
Expand Down
28 changes: 28 additions & 0 deletions src/lib/core/ClusterEnums.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

/**
* Enum and bitmap definitions from cluster specifications.
*/

// For now, include the generated enum file, since we are still working on
// converting our enums to enum classes. Once that is complete and things are
// more stable, we can consider other options here.

#include <app-common/zap-generated/cluster-enums.h>
Loading

0 comments on commit 169b48a

Please sign in to comment.