Skip to content

Commit

Permalink
Make generatedAttributes (and generatedDefaults, minMaxDefaults) cons…
Browse files Browse the repository at this point in the history
…texpr.

This should ensure that it does not accidentally end up in .data
instead of .rodata.

The main fix here is adding the constexpr constructor for
EmberAfDefaultOrMinMaxAttributeValue that takes a
EmberAfAttributeMinMaxValue*, which lets us remove the non-constexpr
reinterpret_cast (coming from the C-style cast) to uint8_t* in
ZAP_MIN_MAX_DEFAULTS_INDEX.

The other fixes are just fixing long-standing const-correctness
issues; this allows us to remove the implicit const_cast from
ZAP_LONG_DEFAULTS_INDEX.
  • Loading branch information
bzbarsky-apple committed Nov 29, 2021
1 parent 16db7cd commit c3ab4d9
Show file tree
Hide file tree
Showing 22 changed files with 54 additions and 52 deletions.
2 changes: 1 addition & 1 deletion examples/bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void HandleDeviceStatusChanged(Device * dev, Device::Changed_t itemChangedMask)
uint8_t buffer[kFixedLabelAttributeArraySize];
EmberAfAttributeMetadata am = { .attributeId = ZCL_LABEL_LIST_ATTRIBUTE_ID,
.size = kFixedLabelAttributeArraySize,
.defaultValue = nullptr };
.defaultValue = static_cast<uint16_t>(0) };

EncodeFixedLabel("room", dev->GetLocation(), buffer, sizeof(buffer), &am);

Expand Down
11 changes: 6 additions & 5 deletions src/app/util/af-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ typedef void (*EmberAfGenericClusterFunction)(void);
*/
union EmberAfDefaultAttributeValue
{
constexpr EmberAfDefaultAttributeValue(uint8_t * ptr) : ptrToDefaultValue(ptr) {}
constexpr EmberAfDefaultAttributeValue(const uint8_t * ptr) : ptrToDefaultValue(ptr) {}
constexpr EmberAfDefaultAttributeValue(uint16_t val) : defaultValue(val) {}

/**
* Points to data if size is more than 2 bytes.
* If size is more than 2 bytes, and this value is NULL,
* then the default value is all zeroes.
*/
uint8_t * ptrToDefaultValue;
const uint8_t * ptrToDefaultValue;

/**
* Actual default value if the attribute size is 2 bytes or less.
Expand Down Expand Up @@ -144,15 +144,16 @@ typedef struct
*/
union EmberAfDefaultOrMinMaxAttributeValue
{
constexpr EmberAfDefaultOrMinMaxAttributeValue(uint8_t * ptr) : ptrToDefaultValue(ptr) {}
constexpr EmberAfDefaultOrMinMaxAttributeValue(const uint8_t * ptr) : ptrToDefaultValue(ptr) {}
constexpr EmberAfDefaultOrMinMaxAttributeValue(uint16_t val) : defaultValue(val) {}
constexpr EmberAfDefaultOrMinMaxAttributeValue(const EmberAfAttributeMinMaxValue * ptr) : ptrToMinMaxValue(ptr) {}

/**
* Points to data if size is more than 2 bytes.
* If size is more than 2 bytes, and this value is NULL,
* then the default value is all zeroes.
*/
uint8_t * ptrToDefaultValue;
const uint8_t * ptrToDefaultValue;
/**
* Actual default value if the attribute size is 2 bytes or less.
*/
Expand All @@ -161,7 +162,7 @@ union EmberAfDefaultOrMinMaxAttributeValue
* Points to the min max attribute value structure, if min/max is
* supported for this attribute.
*/
EmberAfAttributeMinMaxValue * ptrToMinMaxValue;
const EmberAfAttributeMinMaxValue * ptrToMinMaxValue;
};

// Attribute masks modify how attributes are used by the framework
Expand Down
2 changes: 1 addition & 1 deletion src/app/util/af.h
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ uint8_t emberAfGetLastSequenceNumber(void);
* greater than 4 is being compared
* 1, if val2 is smaller.
*/
int8_t emberAfCompareValues(uint8_t * val1, uint8_t * val2, uint16_t len, bool signedNumber);
int8_t emberAfCompareValues(const uint8_t * val1, const uint8_t * val2, uint16_t len, bool signedNumber);

/**
* @brief populates the passed EUI64 with the local EUI64 MAC address.
Expand Down
21 changes: 11 additions & 10 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ EmberAfDefinedEndpoint emAfEndpoints[MAX_ENDPOINT_COUNT];

uint8_t attributeData[ACTUAL_ATTRIBUTE_SIZE];

namespace {

#if (!defined(ATTRIBUTE_SINGLETONS_SIZE)) || (ATTRIBUTE_SINGLETONS_SIZE == 0)
#define ACTUAL_SINGLETONS_SIZE 1
#else
Expand All @@ -80,25 +82,25 @@ uint16_t emberEndpointCount = 0;
// If we have attributes that are more than 2 bytes, then
// we need this data block for the defaults
#if (defined(GENERATED_DEFAULTS) && GENERATED_DEFAULTS_COUNT)
const uint8_t generatedDefaults[] = GENERATED_DEFAULTS;
constexpr const uint8_t generatedDefaults[] = GENERATED_DEFAULTS;
#endif // GENERATED_DEFAULTS

#if (defined(GENERATED_MIN_MAX_DEFAULTS) && GENERATED_MIN_MAX_DEFAULT_COUNT)
const EmberAfAttributeMinMaxValue minMaxDefaults[] = GENERATED_MIN_MAX_DEFAULTS;
constexpr const EmberAfAttributeMinMaxValue minMaxDefaults[] = GENERATED_MIN_MAX_DEFAULTS;
#endif // GENERATED_MIN_MAX_DEFAULTS

#ifdef GENERATED_FUNCTION_ARRAYS
GENERATED_FUNCTION_ARRAYS
#endif

const EmberAfAttributeMetadata generatedAttributes[] = GENERATED_ATTRIBUTES;
const EmberAfCluster generatedClusters[] = GENERATED_CLUSTERS;
const EmberAfEndpointType generatedEmberAfEndpointTypes[] = GENERATED_ENDPOINT_TYPES;
constexpr const EmberAfAttributeMetadata generatedAttributes[] = GENERATED_ATTRIBUTES;
constexpr const EmberAfCluster generatedClusters[] = GENERATED_CLUSTERS;
constexpr const EmberAfEndpointType generatedEmberAfEndpointTypes[] = GENERATED_ENDPOINT_TYPES;

const EmberAfManufacturerCodeEntry clusterManufacturerCodes[] = GENERATED_CLUSTER_MANUFACTURER_CODES;
const uint16_t clusterManufacturerCodeCount = GENERATED_CLUSTER_MANUFACTURER_CODE_COUNT;
const EmberAfManufacturerCodeEntry attributeManufacturerCodes[] = GENERATED_ATTRIBUTE_MANUFACTURER_CODES;
const uint16_t attributeManufacturerCodeCount = GENERATED_ATTRIBUTE_MANUFACTURER_CODE_COUNT;
constexpr const EmberAfManufacturerCodeEntry clusterManufacturerCodes[] = GENERATED_CLUSTER_MANUFACTURER_CODES;
constexpr const uint16_t clusterManufacturerCodeCount = GENERATED_CLUSTER_MANUFACTURER_CODE_COUNT;
constexpr const EmberAfManufacturerCodeEntry attributeManufacturerCodes[] = GENERATED_ATTRIBUTE_MANUFACTURER_CODES;
constexpr const uint16_t attributeManufacturerCodeCount = GENERATED_ATTRIBUTE_MANUFACTURER_CODE_COUNT;

#if !defined(EMBER_SCRIPTED_TEST)
#define endpointNumber(x) fixedEndpoints[x]
Expand All @@ -109,7 +111,6 @@ const uint16_t attributeManufacturerCodeCount = GENERATED_ATTR
#define endpointNetworkIndex(x) fixedNetworks[x]
#endif

namespace {
app::AttributeAccessInterface * gAttributeAccessOverrides = nullptr;
} // anonymous namespace

Expand Down
2 changes: 1 addition & 1 deletion src/app/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -899,7 +899,7 @@ void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size)
// You can pass in val1 as NULL, which will assume that it is
// pointing to an array of all zeroes. This is used so that
// default value of NULL is treated as all zeroes.
int8_t emberAfCompareValues(uint8_t * val1, uint8_t * val2, uint16_t len, bool signedNumber)
int8_t emberAfCompareValues(const uint8_t * val1, const uint8_t * val2, uint16_t len, bool signedNumber)
{
uint8_t i, j, k;
if (signedNumber)
Expand Down
4 changes: 2 additions & 2 deletions src/app/zap-templates/templates/app/endpoint_config.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
#define GENERATED_DEFAULTS_COUNT ({{endpoint_attribute_long_defaults_count}})

#define ZAP_TYPE(type) ZCL_ ## type ## _ATTRIBUTE_TYPE
#define ZAP_LONG_DEFAULTS_INDEX(index) {(uint8_t*)(&generatedDefaults[index])}
#define ZAP_MIN_MAX_DEFAULTS_INDEX(index) {(uint8_t*)(&minMaxDefaults[index])}
#define ZAP_LONG_DEFAULTS_INDEX(index) { &generatedDefaults[index] }
#define ZAP_MIN_MAX_DEFAULTS_INDEX(index) { &minMaxDefaults[index] }
#define ZAP_EMPTY_DEFAULT() {(uint16_t) 0}
#define ZAP_SIMPLE_DEFAULT(x) {(uint16_t) x}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions zzz_generated/bridge-app/zap-generated/endpoint_config.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions zzz_generated/lighting-app/zap-generated/endpoint_config.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions zzz_generated/lock-app/zap-generated/endpoint_config.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit c3ab4d9

Please sign in to comment.