Skip to content

Commit

Permalink
Ensure we always have our endianness defined. (#29294)
Browse files Browse the repository at this point in the history
* Ensure we always have our endianness defined.

And easily configurable in the build.

Fixes #7281

* Address review comment.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Oct 9, 2023
1 parent 089a9b0 commit 2652466
Show file tree
Hide file tree
Showing 9 changed files with 26 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// Default values for the attributes longer than a pointer,
// in a form of a binary blob
// Separate block is generated for big-endian and little-endian cases.
#if BIGENDIAN_CPU
#if CHIP_CONFIG_BIG_ENDIAN_TARGET
#define GENERATED_DEFAULTS \
{ \
\
Expand Down Expand Up @@ -145,7 +145,7 @@
2, 'B', '3', \
}

#else // !BIGENDIAN_CPU
#else // !CHIP_CONFIG_BIG_ENDIAN_TARGET
#define GENERATED_DEFAULTS \
{ \
\
Expand Down Expand Up @@ -264,7 +264,7 @@
2, 'B', '3', \
}

#endif // BIGENDIAN_CPU
#endif // CHIP_CONFIG_BIG_ENDIAN_TARGET

#define GENERATED_DEFAULTS_COUNT (32)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
// Default values for the attributes longer than a pointer,
// in a form of a binary blob
// Separate block is generated for big-endian and little-endian cases.
#if BIGENDIAN_CPU
#if CHIP_CONFIG_BIG_ENDIAN_TARGET
#define GENERATED_DEFAULTS \
{ \
\
Expand All @@ -41,7 +41,7 @@
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
}

#else // !BIGENDIAN_CPU
#else // !CHIP_CONFIG_BIG_ENDIAN_TARGET
#define GENERATED_DEFAULTS \
{ \
\
Expand All @@ -56,7 +56,7 @@
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
}

#endif // BIGENDIAN_CPU
#endif // CHIP_CONFIG_BIG_ENDIAN_TARGET

#define GENERATED_DEFAULTS_COUNT (2)

Expand Down
9 changes: 5 additions & 4 deletions src/app/util/attribute-storage-null-handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#pragma once

#include <lib/core/CHIPConfig.h>
#include <lib/core/TLV.h>
#include <lib/support/BitFlags.h>
#include <lib/support/BitMask.h>
Expand All @@ -30,13 +31,13 @@ namespace app {

template <typename T,
bool IsBigEndian =
// BIGENDIAN_CPU to match how the attribute store works, because that's
// CHIP_CONFIG_BIG_ENDIAN_TARGET to match how the attribute store works, because that's
// what where our data buffer is eventually ending up or coming from.
#if BIGENDIAN_CPU
#if CHIP_CONFIG_BIG_ENDIAN_TARGET
true
#else // BIGENDIAN_CPU
#else // CHIP_CONFIG_BIG_ENDIAN_TARGET
false
#endif // BIGENDIAN_CPU
#endif // CHIP_CONFIG_BIG_ENDIAN_TARGET
>
struct NumericAttributeTraits
{
Expand Down
3 changes: 2 additions & 1 deletion src/app/util/attribute-storage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <app/util/attribute-storage.h>
#include <app/util/config.h>
#include <app/util/generic-callbacks.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/LockTracker.h>
Expand Down Expand Up @@ -1312,7 +1313,7 @@ void emAfLoadAttributeDefaults(EndpointId endpoint, bool ignoreStorage, Optional
// At this point, ptr either points to a default value, or is NULL, in which case
// it should be treated as if it is pointing to an array of all zeroes.

#if (BIGENDIAN_CPU)
#if (CHIP_CONFIG_BIG_ENDIAN_TARGET)
// The default values for attributes that are less than or equal to
// defaultValueSizeForBigEndianNudger in bytes are stored in an
// uint32_t. On big-endian platforms, a pointer to the default value
Expand Down
5 changes: 3 additions & 2 deletions src/app/util/attribute-table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <app/util/error-mapping.h>
#include <app/util/generic-callbacks.h>
#include <app/util/odd-sized-integers.h>
#include <lib/core/CHIPConfig.h>

#include <app/reporting/reporting.h>
#include <protocols/interaction_model/Constants.h>
Expand Down Expand Up @@ -208,13 +209,13 @@ EmberAfStatus emAfWriteAttribute(EndpointId endpoint, ClusterId cluster, Attribu
minBytes = reinterpret_cast<const uint8_t *>(&(minv.defaultValue));
maxBytes = reinterpret_cast<const uint8_t *>(&(maxv.defaultValue));
// On big endian cpu with length 1 only the second byte counts
#if (BIGENDIAN_CPU)
#if (CHIP_CONFIG_BIG_ENDIAN_TARGET)
if (dataLen == 1)
{
minBytes++;
maxBytes++;
}
#endif // BIGENDIAN_CPU
#endif // CHIP_CONFIG_BIG_ENDIAN_TARGET
}
else
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/util/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <app/util/af.h>
#include <app/util/config.h>
#include <app/util/generic-callbacks.h>
#include <lib/core/CHIPConfig.h>

// TODO: figure out a clear path for compile-time codegen
#include <app/PluginApplicationCallbacks.h>
Expand Down Expand Up @@ -79,7 +80,7 @@ EmberAfDifferenceType emberAfGetDifference(uint8_t * pData, EmberAfDifferenceTyp
for (i = 0; i < dataSize; i++)
{
value2 = value2 << 8;
#if (BIGENDIAN_CPU)
#if (CHIP_CONFIG_BIG_ENDIAN_TARGET)
value2 += pData[i];
#else // BIGENDIAN
value2 += pData[dataSize - i - 1];
Expand Down Expand Up @@ -224,7 +225,7 @@ void emberAfCopyLongString(uint8_t * dest, const uint8_t * src, size_t size)
}
}

#if (BIGENDIAN_CPU)
#if (CHIP_CONFIG_BIG_ENDIAN_TARGET)
#define EM_BIG_ENDIAN true
#else
#define EM_BIG_ENDIAN false
Expand Down
6 changes: 3 additions & 3 deletions src/app/zap-templates/templates/app/endpoint_config.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
// Default values for the attributes longer than a pointer,
// in a form of a binary blob
// Separate block is generated for big-endian and little-endian cases.
#if BIGENDIAN_CPU
#if CHIP_CONFIG_BIG_ENDIAN_TARGET
#define GENERATED_DEFAULTS {{endpoint_attribute_long_defaults endian="big"}}

#else // !BIGENDIAN_CPU
#else // !CHIP_CONFIG_BIG_ENDIAN_TARGET
#define GENERATED_DEFAULTS {{endpoint_attribute_long_defaults endian="little"}}
#endif // BIGENDIAN_CPU
#endif // CHIP_CONFIG_BIG_ENDIAN_TARGET

#define GENERATED_DEFAULTS_COUNT ({{endpoint_attribute_long_defaults_count}})

Expand Down
1 change: 1 addition & 0 deletions src/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ buildconfig_header("chip_buildconfig") {
"CHIP_CONFIG_MINMDNS_DYNAMIC_OPERATIONAL_RESPONDER_LIST=${chip_config_minmdns_dynamic_operational_responder_list}",
"CHIP_CONFIG_MINMDNS_MAX_PARALLEL_RESOLVES=${chip_config_minmdns_max_parallel_resolves}",
"CHIP_CONFIG_CANCELABLE_HAS_INFO_STRING_FIELD=${chip_config_cancelable_has_info_string_field}",
"CHIP_CONFIG_BIG_ENDIAN_TARGET=${chip_target_is_big_endian}",
]
}

Expand Down
3 changes: 3 additions & 0 deletions src/lib/core/core.gni
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ declare_args() {
# If set to true, adds a string "info" field to Cancelable.
# Only here for backwards compat. Generally, THIS SHOULD NOT BE SET TO TRUE.
chip_config_cancelable_has_info_string_field = false

# Whether the target architecture is big-endian (true) or little-endian (false).
chip_target_is_big_endian = false
}

if (chip_target_style == "") {
Expand Down

0 comments on commit 2652466

Please sign in to comment.