Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reading 8- or 16-bit attributes from big-endian device returns 0 (#6166) #6175

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 defined(BIGENDIAN_CPU) && (BIGENDIAN_CPU != 0)
// Big endian
#if (UINTPTR_MAX == UINT32_MAX)
uint32_t align : 16;
uint16_t value;
#elif (UINTPTR_MAX == UINT64_MAX)
uint64_t align : 48;
uint16_t value;
Comment on lines +106 to +107
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be making assumptions about how a bitfield and an integer will pack that might not be valid in practice.... Nothing I'm aware of requires them to pack with no gaps here.

At the very least we should static_assert that sizeof(EmberAfAlignValue16) == sizeof(uint8_t*), right? That way we will catch it if things pack wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be making assumptions about how a bitfield and an integer will pack that might not be valid in practice.... Nothing I'm aware of requires them to pack with no gaps here.
You are right on that one.

#endif
#else
// Little endian
#if (UINTPTR_MAX == UINT32_MAX)
uint16_t value;
uint32_t align : 16;
#elif (UINTPTR_MAX == UINT64_MAX)
uint16_t value;
uint64_t align : 48;
#endif

#endif // defined(BIGENDIAN_CPU) && (BIGENDIAN_CPU != 0)
} EmberAfAlignValue16;

/**
* @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;
EmberAfAlignValue16 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.
Comment on lines -146 to +177
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this change to the comment? The new comment does not seem to be correct if the attribute type is larger than 2-byte; in that case this field is not really the default value in any sense; the ptrToDefaultValue points to where the default value is.

*/
uint16_t defaultValue;
EmberAfAlignValue16 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