Skip to content

Commit

Permalink
Ensure we always have our endianness defined.
Browse files Browse the repository at this point in the history
And easily configurable in the build.

Fixes project-chip#7281
  • Loading branch information
bzbarsky-apple committed Sep 15, 2023
1 parent ef5bda7 commit b5f5f04
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 23 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
8 changes: 3 additions & 5 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,9 @@ if (chip_device_platform != "none" && chip_device_platform != "external") {
defines += [ "CHIP_DEVICE_CONFIG_FIRMWARE_BUILD_TIME=\"${chip_device_config_firmware_build_time}\"" ]
}

if (chip_use_transitional_commissionable_data_provider) {
defines += [ "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=1" ]
} else {
defines += [ "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=0" ]
}
defines += [ "CHIP_USE_TRANSITIONAL_COMMISSIONABLE_DATA_PROVIDER=${chip_use_transitional_commissionable_data_provider}" ]

defines += [ "CHIP_CONFIG_BIG_ENDIAN_TARGET=${chip_target_is_big_endian}" ]

if (chip_device_platform == "cc13x2_26x2") {
device_layer_target_define = "CC13X2_26X2"
Expand Down
3 changes: 3 additions & 0 deletions src/platform/device.gni
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ declare_args() {

# Substitute fake platform when building with chip_device_platform=auto.
chip_fake_platform = false

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

if (chip_device_platform == "auto") {
Expand Down

0 comments on commit b5f5f04

Please sign in to comment.