From 2652466f912a47670c2635d44325b16d5e03ac09 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Mon, 18 Sep 2023 15:32:29 -0400 Subject: [PATCH] Ensure we always have our endianness defined. (#29294) * Ensure we always have our endianness defined. And easily configurable in the build. Fixes https://github.com/project-chip/connectedhomeip/issues/7281 * Address review comment. --- .../all-clusters-app/app-templates/endpoint_config.h | 6 +++--- .../outputs/lighting-app/app-templates/endpoint_config.h | 6 +++--- src/app/util/attribute-storage-null-handling.h | 9 +++++---- src/app/util/attribute-storage.cpp | 3 ++- src/app/util/attribute-table.cpp | 5 +++-- src/app/util/util.cpp | 5 +++-- src/app/zap-templates/templates/app/endpoint_config.zapt | 6 +++--- src/lib/core/BUILD.gn | 1 + src/lib/core/core.gni | 3 +++ 9 files changed, 26 insertions(+), 18 deletions(-) diff --git a/scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/endpoint_config.h b/scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/endpoint_config.h index 88401280135ed7..039211fb318fc0 100644 --- a/scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/endpoint_config.h +++ b/scripts/tools/zap/tests/outputs/all-clusters-app/app-templates/endpoint_config.h @@ -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 \ { \ \ @@ -145,7 +145,7 @@ 2, 'B', '3', \ } -#else // !BIGENDIAN_CPU +#else // !CHIP_CONFIG_BIG_ENDIAN_TARGET #define GENERATED_DEFAULTS \ { \ \ @@ -264,7 +264,7 @@ 2, 'B', '3', \ } -#endif // BIGENDIAN_CPU +#endif // CHIP_CONFIG_BIG_ENDIAN_TARGET #define GENERATED_DEFAULTS_COUNT (32) diff --git a/scripts/tools/zap/tests/outputs/lighting-app/app-templates/endpoint_config.h b/scripts/tools/zap/tests/outputs/lighting-app/app-templates/endpoint_config.h index b9f2e8d69093bf..de91e763395913 100644 --- a/scripts/tools/zap/tests/outputs/lighting-app/app-templates/endpoint_config.h +++ b/scripts/tools/zap/tests/outputs/lighting-app/app-templates/endpoint_config.h @@ -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 \ { \ \ @@ -41,7 +41,7 @@ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \ } -#else // !BIGENDIAN_CPU +#else // !CHIP_CONFIG_BIG_ENDIAN_TARGET #define GENERATED_DEFAULTS \ { \ \ @@ -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) diff --git a/src/app/util/attribute-storage-null-handling.h b/src/app/util/attribute-storage-null-handling.h index 207cea1c0bdfdb..8db72a2321bf7b 100644 --- a/src/app/util/attribute-storage-null-handling.h +++ b/src/app/util/attribute-storage-null-handling.h @@ -17,6 +17,7 @@ #pragma once +#include #include #include #include @@ -30,13 +31,13 @@ namespace app { template struct NumericAttributeTraits { diff --git a/src/app/util/attribute-storage.cpp b/src/app/util/attribute-storage.cpp index 0662ddcaa008b2..58730f19cb05d5 100644 --- a/src/app/util/attribute-storage.cpp +++ b/src/app/util/attribute-storage.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -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 diff --git a/src/app/util/attribute-table.cpp b/src/app/util/attribute-table.cpp index 89806abba798a3..a97dbbc6985645 100644 --- a/src/app/util/attribute-table.cpp +++ b/src/app/util/attribute-table.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -208,13 +209,13 @@ EmberAfStatus emAfWriteAttribute(EndpointId endpoint, ClusterId cluster, Attribu minBytes = reinterpret_cast(&(minv.defaultValue)); maxBytes = reinterpret_cast(&(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 { diff --git a/src/app/util/util.cpp b/src/app/util/util.cpp index dd2085a2a827cb..619ecdcd923dcc 100644 --- a/src/app/util/util.cpp +++ b/src/app/util/util.cpp @@ -23,6 +23,7 @@ #include #include #include +#include // TODO: figure out a clear path for compile-time codegen #include @@ -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]; @@ -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 diff --git a/src/app/zap-templates/templates/app/endpoint_config.zapt b/src/app/zap-templates/templates/app/endpoint_config.zapt index 4722f4c456d237..f67c779089a1f2 100644 --- a/src/app/zap-templates/templates/app/endpoint_config.zapt +++ b/src/app/zap-templates/templates/app/endpoint_config.zapt @@ -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}}) diff --git a/src/lib/core/BUILD.gn b/src/lib/core/BUILD.gn index cbefb82b3058ce..1f7f2769f3c5cd 100644 --- a/src/lib/core/BUILD.gn +++ b/src/lib/core/BUILD.gn @@ -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}", ] } diff --git a/src/lib/core/core.gni b/src/lib/core/core.gni index 5d4a46f41dd708..839f9b6507b734 100644 --- a/src/lib/core/core.gni +++ b/src/lib/core/core.gni @@ -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 == "") {