Skip to content

Commit

Permalink
Allowing for default values to be 4 bytes instead of 2
Browse files Browse the repository at this point in the history
  • Loading branch information
tehampson committed May 11, 2022
1 parent 837b939 commit 543b1e2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
12 changes: 7 additions & 5 deletions src/app/util/attribute-metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ union EmberAfDefaultAttributeValue
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,
* Points to data if size is more than 4 bytes.
* If size is more than 4 bytes, and this value is NULL,
* then the default value is all zeroes.
*/
const uint8_t * ptrToDefaultValue;

/**
* Actual default value if the attribute size is 2 bytes or less.
* Actual default value if the attribute size is 4 bytes or less.
*/
uint16_t defaultValue;
};
Expand Down Expand Up @@ -100,7 +100,9 @@ typedef struct
union EmberAfDefaultOrMinMaxAttributeValue
{
constexpr EmberAfDefaultOrMinMaxAttributeValue(const uint8_t * ptr) : ptrToDefaultValue(ptr) {}
constexpr EmberAfDefaultOrMinMaxAttributeValue(uint16_t val) : defaultValue(val) {}
// Previouly defaultValue size was uint16_t. This is here for that legacy code
constexpr EmberAfDefaultOrMinMaxAttributeValue(uint16_t val) : defaultValue(static_cast<uint32_t>(val)) {}
constexpr EmberAfDefaultOrMinMaxAttributeValue(uint32_t val) : defaultValue(val) {}
constexpr EmberAfDefaultOrMinMaxAttributeValue(const EmberAfAttributeMinMaxValue * ptr) : ptrToMinMaxValue(ptr) {}

/**
Expand All @@ -112,7 +114,7 @@ union EmberAfDefaultOrMinMaxAttributeValue
/**
* Actual default value if the attribute size is 2 bytes or less.
*/
uint16_t defaultValue;
uint32_t defaultValue;
/**
* Points to the min max attribute value structure, if min/max is
* supported for this attribute.
Expand Down
18 changes: 12 additions & 6 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ uint8_t attributeData[ACTUAL_ATTRIBUTE_SIZE];

namespace {

#if (!defined(EMBER_AF_DEFAULT_VALUE_SIZE)) || (EMBER_AF_DEFAULT_VALUE_SIZE == 0)
#define EMBER_AF_DEFAULT_VALUE_SIZE 2
#endif

#if (!defined(ATTRIBUTE_SINGLETONS_SIZE)) || (ATTRIBUTE_SINGLETONS_SIZE == 0)
#define ACTUAL_SINGLETONS_SIZE 1
#else
Expand All @@ -81,8 +85,8 @@ uint8_t singletonAttributeData[ACTUAL_SINGLETONS_SIZE];

uint16_t emberEndpointCount = 0;

// If we have attributes that are more than 2 bytes, then
// we need this data block for the defaults
// If we have attributes that are more than bytes EMBER_AF_DEFAULT_VALUE_SIZE,
// then we need this data block for the defaults
#if (defined(GENERATED_DEFAULTS) && GENERATED_DEFAULTS_COUNT)
constexpr const uint8_t generatedDefaults[] = GENERATED_DEFAULTS;
#endif // GENERATED_DEFAULTS
Expand Down Expand Up @@ -468,21 +472,21 @@ static EmberAfStatus typeSensitiveMemCopy(ClusterId clusterId, uint8_t * dest, u
}
else if (emberAfIsLongStringAttributeType(attributeType))
{
if (bufferSize < 2)
if (bufferSize < 2) // TODO confirm is this should still be 2
{
return EMBER_ZCL_STATUS_INSUFFICIENT_SPACE;
}
emberAfCopyLongString(dest, src, bufferSize - 2);
}
else if (emberAfIsThisDataTypeAListType(attributeType))
{
if (bufferSize < 2)
if (bufferSize < 2) // TODO confirm is this should still be 2
{
return EMBER_ZCL_STATUS_INSUFFICIENT_SPACE;
}

// Just copy the length.
memmove(dest, src, 2);
memmove(dest, src, 2); // TODO confirm is this should still be 2
}
else
{
Expand Down Expand Up @@ -1258,7 +1262,7 @@ void emAfLoadAttributeDefaults(EndpointId endpoint, bool ignoreStorage, Optional
}
else
{
if (emberAfAttributeSize(am) <= 2)
if (emberAfAttributeSize(am) <= EMBER_AF_DEFAULT_VALUE_SIZE)
{
ptr = (uint8_t *) &(am->defaultValue.defaultValue);
}
Expand All @@ -1277,6 +1281,8 @@ void emAfLoadAttributeDefaults(EndpointId endpoint, bool ignoreStorage, Optional
// cases, nudge the pointer forward so it points to the correct byte.
if (emberAfAttributeSize(am) == 1 && ptr != NULL)
{
// TODO I need to think about this a little more to make sure I am doing the right thing for
// BIGENDIAN CPUs, now that we are changing to uint32_t. I also will need to update the comment
*ptr++;
}
#endif // BIGENDIAN
Expand Down
6 changes: 4 additions & 2 deletions src/app/zap-templates/templates/app/endpoint_config.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

#include <lib/core/CHIPConfig.h>

#define EMBER_AF_DEFAULT_VALUE_SIZE 4

{{#endpoint_config allowUnknownStorageOption="false"}}

// Default values for the attributes longer than a pointer,
Expand All @@ -22,8 +24,8 @@
#define ZAP_TYPE(type) ZCL_ ## type ## _ATTRIBUTE_TYPE
#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}
#define ZAP_EMPTY_DEFAULT() {(uint32_t) 0}
#define ZAP_SIMPLE_DEFAULT(x) {(uint32_t) x}

// This is an array of EmberAfAttributeMinMaxValue structures.
#define GENERATED_MIN_MAX_DEFAULT_COUNT {{endpoint_attribute_min_max_count}}
Expand Down

0 comments on commit 543b1e2

Please sign in to comment.