Skip to content

Commit

Permalink
Reading 8- or 16-bit attributes from big-endian device returns 0 (#6166)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjzander-signify committed Apr 20, 2021
1 parent f3ec8e6 commit de63682
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 7 deletions.
37 changes: 34 additions & 3 deletions src/app/util/af-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,37 @@ typedef void (*EmberAfGenericClusterFunction)(void);
*/
#define EMBER_AF_NULL_MANUFACTURER_CODE 0x0000

/**
* @brief Type for align a 16 bits value.
*
* This structure has the same size as a pointer and is used to align
* a 16 bit value to the 16 least significant bits of a pointer.
* Note that this structure is aware of the endianess and the pointer size.
*/
typedef struct
{
#if (BIGENDIAN_CPU)
#if (UINTPTR_MAX == UINT32_MAX)
uint32_t align : 16;
#elif (UINTPTR_MAX == UINT64_MAX)
uint64_t align : 48;
#endif
#endif

/**
* Actual value.
*/
uint16_t value;

#if (!BIGENDIAN_CPU)
#if (UINTPTR_MAX == UINT32_MAX)
uint32_t align : 16;
#elif (UINTPTR_MAX == UINT64_MAX)
uint64_t align : 48;
#endif
#endif
} EmberAfAlignValue;

/**
* @brief Type for default values.
*
Expand All @@ -106,7 +137,7 @@ typedef union
/**
* Actual default value if the attribute size is 2 bytes or less.
*/
uint16_t defaultValue;
EmberAfAlignValue defaultValue;
} EmberAfDefaultAttributeValue;

/**
Expand Down Expand Up @@ -143,9 +174,9 @@ typedef union
*/
uint8_t * ptrToDefaultValue;
/**
* Actual default value if the attribute size is 2 bytes or less.
* Default value of the attribute.
*/
uint16_t defaultValue;
EmberAfAlignValue defaultValue;
/**
* Points to the min max attribute value structure, if min/max is
* supported for this attribute.
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,7 @@ void emAfLoadAttributeDefaults(EndpointId endpoint, bool writeTokens)
{
if (emberAfAttributeSize(am) <= 2)
{
ptr = (uint8_t *) &(am->defaultValue.ptrToMinMaxValue->defaultValue.defaultValue);
ptr = (uint8_t *) &(am->defaultValue.ptrToMinMaxValue->defaultValue.defaultValue.value);
}
else
{
Expand All @@ -1099,7 +1099,7 @@ void emAfLoadAttributeDefaults(EndpointId endpoint, bool writeTokens)
{
if (emberAfAttributeSize(am) <= 2)
{
ptr = (uint8_t *) &(am->defaultValue.defaultValue);
ptr = (uint8_t *) &(am->defaultValue.defaultValue.value);
}
else
{
Expand Down
4 changes: 2 additions & 2 deletions src/app/util/attribute-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,8 +536,8 @@ EmberAfStatus emAfWriteAttribute(EndpointId endpoint, ClusterId cluster, Attribu
if (dataLen <= 2)
{
int8_t minR, maxR;
uint8_t * minI = (uint8_t *) &(minv.defaultValue);
uint8_t * maxI = (uint8_t *) &(maxv.defaultValue);
uint8_t * minI = (uint8_t *) &(minv.defaultValue.value);
uint8_t * maxI = (uint8_t *) &(maxv.defaultValue.value);
// On big endian cpu with length 1 only the second byte counts
#if (BIGENDIAN_CPU)
if (dataLen == 1)
Expand Down

0 comments on commit de63682

Please sign in to comment.