Skip to content

Commit 688cda3

Browse files
committed
Split out io storage and type conversion as shared functions for ember-compatibility
1 parent c9663d6 commit 688cda3

7 files changed

+215
-224
lines changed

src/app/chip_data_model.cmake

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ function(chip_configure_data_model APP_TARGET)
145145
${CHIP_APP_BASE_DIR}/util/DataModelHandler.cpp
146146
${CHIP_APP_BASE_DIR}/util/ember-compatibility-functions.cpp
147147
${CHIP_APP_BASE_DIR}/util/ember-global-attribute-access-interface.cpp
148+
${CHIP_APP_BASE_DIR}/util/ember-io-storage.cpp
148149
${CHIP_APP_BASE_DIR}/util/generic-callback-stubs.cpp
149150
${CHIP_APP_BASE_DIR}/util/privilege-storage.cpp
150151
${CHIP_APP_BASE_DIR}/util/util.cpp

src/app/chip_data_model.gni

+1
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ template("chip_data_model") {
210210
"${_app_root}/util/attribute-table.cpp",
211211
"${_app_root}/util/ember-compatibility-functions.cpp",
212212
"${_app_root}/util/ember-global-attribute-access-interface.cpp",
213+
"${_app_root}/util/ember-io-storage.cpp",
213214
"${_app_root}/util/util.cpp",
214215
]
215216
}

src/app/codegen-interaction-model/Model_Read.cpp

+10-101
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <app/util/attribute-storage-null-handling.h>
3535
#include <app/util/attribute-storage.h>
3636
#include <app/util/ember-global-attribute-access-interface.h>
37+
#include <app/util/ember-io-storage.h>
3738
#include <app/util/endpoint-config-api.h>
3839
#include <app/util/odd-sized-integers.h>
3940
#include <lib/core/CHIPError.h>
@@ -45,99 +46,7 @@ namespace chip {
4546
namespace app {
4647
namespace CodegenDataModel {
4748
namespace {
48-
// On some apps, ATTRIBUTE_LARGEST can as small as 3, making compiler unhappy since data[kAttributeReadBufferSize] cannot hold
49-
// uint64_t. Make kAttributeReadBufferSize at least 8 so it can fit all basic types.
50-
constexpr size_t kAttributeReadBufferSize = (ATTRIBUTE_LARGEST >= 8 ? ATTRIBUTE_LARGEST : 8);
51-
uint8_t attributeReadBufferSpace[kAttributeReadBufferSize];
52-
53-
// BasicType maps the type to basic int(8|16|32|64)(s|u) types.
54-
// TODO: this code should be SHARED!
55-
EmberAfAttributeType BaseType(EmberAfAttributeType type)
56-
{
57-
switch (type)
58-
{
59-
case ZCL_ACTION_ID_ATTRIBUTE_TYPE: // Action Id
60-
case ZCL_FABRIC_IDX_ATTRIBUTE_TYPE: // Fabric Index
61-
case ZCL_BITMAP8_ATTRIBUTE_TYPE: // 8-bit bitmap
62-
case ZCL_ENUM8_ATTRIBUTE_TYPE: // 8-bit enumeration
63-
case ZCL_STATUS_ATTRIBUTE_TYPE: // Status Code
64-
case ZCL_PERCENT_ATTRIBUTE_TYPE: // Percentage
65-
static_assert(std::is_same<chip::Percent, uint8_t>::value,
66-
"chip::Percent is expected to be uint8_t, change this when necessary");
67-
return ZCL_INT8U_ATTRIBUTE_TYPE;
68-
69-
case ZCL_ENDPOINT_NO_ATTRIBUTE_TYPE: // Endpoint Number
70-
case ZCL_GROUP_ID_ATTRIBUTE_TYPE: // Group Id
71-
case ZCL_VENDOR_ID_ATTRIBUTE_TYPE: // Vendor Id
72-
case ZCL_ENUM16_ATTRIBUTE_TYPE: // 16-bit enumeration
73-
case ZCL_BITMAP16_ATTRIBUTE_TYPE: // 16-bit bitmap
74-
case ZCL_PERCENT100THS_ATTRIBUTE_TYPE: // 100ths of a percent
75-
static_assert(std::is_same<chip::EndpointId, uint16_t>::value,
76-
"chip::EndpointId is expected to be uint16_t, change this when necessary");
77-
static_assert(std::is_same<chip::GroupId, uint16_t>::value,
78-
"chip::GroupId is expected to be uint16_t, change this when necessary");
79-
static_assert(std::is_same<chip::Percent100ths, uint16_t>::value,
80-
"chip::Percent100ths is expected to be uint16_t, change this when necessary");
81-
return ZCL_INT16U_ATTRIBUTE_TYPE;
82-
83-
case ZCL_CLUSTER_ID_ATTRIBUTE_TYPE: // Cluster Id
84-
case ZCL_ATTRIB_ID_ATTRIBUTE_TYPE: // Attribute Id
85-
case ZCL_FIELD_ID_ATTRIBUTE_TYPE: // Field Id
86-
case ZCL_EVENT_ID_ATTRIBUTE_TYPE: // Event Id
87-
case ZCL_COMMAND_ID_ATTRIBUTE_TYPE: // Command Id
88-
case ZCL_TRANS_ID_ATTRIBUTE_TYPE: // Transaction Id
89-
case ZCL_DEVTYPE_ID_ATTRIBUTE_TYPE: // Device Type Id
90-
case ZCL_DATA_VER_ATTRIBUTE_TYPE: // Data Version
91-
case ZCL_BITMAP32_ATTRIBUTE_TYPE: // 32-bit bitmap
92-
case ZCL_EPOCH_S_ATTRIBUTE_TYPE: // Epoch Seconds
93-
case ZCL_ELAPSED_S_ATTRIBUTE_TYPE: // Elapsed Seconds
94-
static_assert(std::is_same<chip::ClusterId, uint32_t>::value,
95-
"chip::Cluster is expected to be uint32_t, change this when necessary");
96-
static_assert(std::is_same<chip::AttributeId, uint32_t>::value,
97-
"chip::AttributeId is expected to be uint32_t, change this when necessary");
98-
static_assert(std::is_same<chip::AttributeId, uint32_t>::value,
99-
"chip::AttributeId is expected to be uint32_t, change this when necessary");
100-
static_assert(std::is_same<chip::EventId, uint32_t>::value,
101-
"chip::EventId is expected to be uint32_t, change this when necessary");
102-
static_assert(std::is_same<chip::CommandId, uint32_t>::value,
103-
"chip::CommandId is expected to be uint32_t, change this when necessary");
104-
static_assert(std::is_same<chip::TransactionId, uint32_t>::value,
105-
"chip::TransactionId is expected to be uint32_t, change this when necessary");
106-
static_assert(std::is_same<chip::DeviceTypeId, uint32_t>::value,
107-
"chip::DeviceTypeId is expected to be uint32_t, change this when necessary");
108-
static_assert(std::is_same<chip::DataVersion, uint32_t>::value,
109-
"chip::DataVersion is expected to be uint32_t, change this when necessary");
110-
return ZCL_INT32U_ATTRIBUTE_TYPE;
111-
112-
case ZCL_AMPERAGE_MA_ATTRIBUTE_TYPE: // Amperage milliamps
113-
case ZCL_ENERGY_MWH_ATTRIBUTE_TYPE: // Energy milliwatt-hours
114-
case ZCL_POWER_MW_ATTRIBUTE_TYPE: // Power milliwatts
115-
case ZCL_VOLTAGE_MV_ATTRIBUTE_TYPE: // Voltage millivolts
116-
return ZCL_INT64S_ATTRIBUTE_TYPE;
117-
118-
case ZCL_EVENT_NO_ATTRIBUTE_TYPE: // Event Number
119-
case ZCL_FABRIC_ID_ATTRIBUTE_TYPE: // Fabric Id
120-
case ZCL_NODE_ID_ATTRIBUTE_TYPE: // Node Id
121-
case ZCL_BITMAP64_ATTRIBUTE_TYPE: // 64-bit bitmap
122-
case ZCL_EPOCH_US_ATTRIBUTE_TYPE: // Epoch Microseconds
123-
case ZCL_POSIX_MS_ATTRIBUTE_TYPE: // POSIX Milliseconds
124-
case ZCL_SYSTIME_MS_ATTRIBUTE_TYPE: // System time Milliseconds
125-
case ZCL_SYSTIME_US_ATTRIBUTE_TYPE: // System time Microseconds
126-
static_assert(std::is_same<chip::EventNumber, uint64_t>::value,
127-
"chip::EventNumber is expected to be uint64_t, change this when necessary");
128-
static_assert(std::is_same<chip::FabricId, uint64_t>::value,
129-
"chip::FabricId is expected to be uint64_t, change this when necessary");
130-
static_assert(std::is_same<chip::NodeId, uint64_t>::value,
131-
"chip::NodeId is expected to be uint64_t, change this when necessary");
132-
return ZCL_INT64U_ATTRIBUTE_TYPE;
133-
134-
case ZCL_TEMPERATURE_ATTRIBUTE_TYPE: // Temperature
135-
return ZCL_INT16S_ATTRIBUTE_TYPE;
136-
137-
default:
138-
return type;
139-
}
140-
}
49+
using namespace chip::app::Compatibility::Internal;
14150

14251
// Fetch the source for the given attribute path: either a cluster (for global ones) or attribute
14352
// path.
@@ -311,7 +220,7 @@ CHIP_ERROR EncodeEmberValue(ByteSpan data, const EmberAfAttributeMetadata * meta
311220

312221
const bool isNullable = metadata->IsNullable();
313222

314-
switch (BaseType(metadata->attributeType))
223+
switch (AttributeBaseType(metadata->attributeType))
315224
{
316225
case ZCL_NO_DATA_ATTRIBUTE_TYPE: // No data
317226
return encoder.EncodeNull();
@@ -417,19 +326,19 @@ CHIP_ERROR Model::ReadAttribute(const InteractionModel::ReadAttributeRequest & r
417326

418327
// At this point, we have to use ember directly to read the data.
419328
EmberAfAttributeSearchRecord record;
420-
record.endpoint = request.path.mEndpointId;
421-
record.clusterId = request.path.mClusterId;
422-
record.attributeId = request.path.mAttributeId;
423-
Protocols::InteractionModel::Status status =
424-
emAfReadOrWriteAttribute(&record, &attributeMetadata, attributeReadBufferSpace, sizeof(attributeReadBufferSpace),
425-
/* write = */ false);
329+
record.endpoint = request.path.mEndpointId;
330+
record.clusterId = request.path.mClusterId;
331+
record.attributeId = request.path.mAttributeId;
332+
Protocols::InteractionModel::Status status = emAfReadOrWriteAttribute(
333+
&record, &attributeMetadata, gEmberAttributeIOBufferSpan.data(), gEmberAttributeIOBufferSpan.size(),
334+
/* write = */ false);
426335

427336
if (status != Protocols::InteractionModel::Status::Success)
428337
{
429338
return ChipError(ChipError::SdkPart::kIMGlobalStatus, to_underlying(status), __FILE__, __LINE__);
430339
}
431340

432-
return EncodeEmberValue(ByteSpan(attributeReadBufferSpace), attributeMetadata, encoder);
341+
return EncodeEmberValue(gEmberAttributeIOBufferSpan, attributeMetadata, encoder);
433342
}
434343

435344
} // namespace CodegenDataModel

src/app/codegen-interaction-model/tests/BUILD.gn

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import("${chip_root}/src/app/codegen-interaction-model/model.gni")
1818
source_set("ember_extra_files") {
1919
sources = [
2020
# This IS TERRIBLE, however we want to pretent AAI exists for global
21-
# items
21+
# items and we need a shared IO storage to reduce overhead between
22+
# data-model access and ember-compatibility (we share the same buffer)
2223
"${chip_root}/src/app/util/ember-global-attribute-access-interface.cpp",
24+
"${chip_root}/src/app/util/ember-io-storage.cpp",
2325
"InteractionModelTemporaryOverrides.cpp",
2426
]
2527

0 commit comments

Comments
 (0)