From 2a75a2eb98db5ffddad9e6241734860d24c585c4 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:11:42 -0400 Subject: [PATCH 01/16] Use chip::app::IsSignedAttributeType --- .../EmberAttributeDataBuffer.cpp | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index 2c764241ebf3d8..6619b963aa437e 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -384,14 +384,7 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer uint8_t raw_bytes[8]; - bool isSigned = (mAttributeType == ZCL_INT8S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT16S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT24S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT32S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT40S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT48S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT56S_ATTRIBUTE_TYPE) // - || (mAttributeType == ZCL_INT64S_ATTRIBUTE_TYPE); + const bool isSigned = chip::app::IsSignedAttributeType(mAttributeType); unsigned byteCount; uint64_t nullValue; From 1ac822c391f4cd56fe0d03dce08af181d6aa1419 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:15:30 -0400 Subject: [PATCH 02/16] Fix up put as well as naming for null value and comment --- .../EmberAttributeDataBuffer.cpp | 24 +++++++++---------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index 6619b963aa437e..4f5e0f778bb9cd 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -387,19 +387,19 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer const bool isSigned = chip::app::IsSignedAttributeType(mAttributeType); unsigned byteCount; - uint64_t nullValue; + uint64_t nullValueAsU64; if (isSigned) { const SignedDecodeInfo info = GetSignedDecodeInfo(mAttributeType); byteCount = info.byteCount; - nullValue = static_cast(info.minValue); // just a bit cast for easy compare + nullValueAsU64 = static_cast(info.minValue); } else { const UnsignedDecodeInfo info = GetUnsignedDecodeInfo(mAttributeType); byteCount = info.byteCount; - nullValue = info.maxValue; + nullValueAsU64 = info.maxValue; } VerifyOrDie(sizeof(raw_bytes) >= byteCount); @@ -438,6 +438,10 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer value.uint_value = (value.uint_value & ~0xFFULL) | raw_bytes[i]; } + // We place the null value as either int_value or uint_value into a union that is + // bit-formatted as both int64 and uint64. When we define the nullValue, + // it is bitcast into u64 hence this comparison. This is ugly, however this + // code prioritizes code size over readability here. if (mIsNullable && (value.uint_value == nullValue)) { // MaxValue is used for NULL setting @@ -446,27 +450,21 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer switch (mAttributeType) { - case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer - return writer.Put(tag, static_cast(value.uint_value)); + case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer case ZCL_INT16U_ATTRIBUTE_TYPE: // Unsigned 16-bit integer - return writer.Put(tag, static_cast(value.uint_value)); case ZCL_INT24U_ATTRIBUTE_TYPE: // Unsigned 24-bit integer case ZCL_INT32U_ATTRIBUTE_TYPE: // Unsigned 32-bit integer - return writer.Put(tag, static_cast(value.uint_value)); case ZCL_INT40U_ATTRIBUTE_TYPE: // Unsigned 40-bit integer case ZCL_INT48U_ATTRIBUTE_TYPE: // Unsigned 48-bit integer case ZCL_INT56U_ATTRIBUTE_TYPE: // Signed 56-bit integer case ZCL_INT64U_ATTRIBUTE_TYPE: // Signed 64-bit integer - return writer.Put(tag, static_cast(value.uint_value)); - case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer - return writer.Put(tag, static_cast(value.int_value)); + return writer.Put(tag, value.uint_value); + case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer case ZCL_INT16S_ATTRIBUTE_TYPE: // Signed 16-bit integer - return writer.Put(tag, static_cast(value.int_value)); case ZCL_INT24S_ATTRIBUTE_TYPE: // Signed 24-bit integer case ZCL_INT32S_ATTRIBUTE_TYPE: // Signed 32-bit integer - return writer.Put(tag, static_cast(value.int_value)); default: - return writer.Put(tag, static_cast(value.int_value)); + return writer.Put(tag, value.int_value); } } From 469c65a1e04a45c76e88233c6c6245db66b93286 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:19:01 -0400 Subject: [PATCH 03/16] Fix up nullable tests --- .../EmberAttributeDataBuffer.cpp | 7 +++++-- .../tests/TestEmberAttributeDataBuffer.cpp | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index 4f5e0f778bb9cd..7dbf39d3c43584 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -442,7 +442,7 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer // bit-formatted as both int64 and uint64. When we define the nullValue, // it is bitcast into u64 hence this comparison. This is ugly, however this // code prioritizes code size over readability here. - if (mIsNullable && (value.uint_value == nullValue)) + if (mIsNullable && (value.uint_value == nullValueAsU64)) { // MaxValue is used for NULL setting return writer.PutNull(tag); @@ -488,10 +488,11 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV:: case 1: return writer.PutBoolean(tag, value != 0); case 0xFF: + VerifyOrReturnError(mIsNullable, CHIP_ERROR_INVALID_ARGUMENT); return writer.PutNull(tag); default: // Unknown types - return CHIP_ERROR_INCORRECT_STATE; + return CHIP_ERROR_INVALID_ARGUMENT; } } case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer @@ -524,6 +525,7 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV:: } if (NumericAttributeTraits::IsNullValue(value.value)) { + VerifyOrReturnError(mIsNullable, CHIP_ERROR_INVALID_ARGUMENT); return writer.PutNull(tag); } return writer.Put(tag, value.value); @@ -541,6 +543,7 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV:: } if (NumericAttributeTraits::IsNullValue(value.value)) { + VerifyOrReturnError(mIsNullable, CHIP_ERROR_INVALID_ARGUMENT); return writer.PutNull(tag); } return writer.Put(tag, value.value); diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index dc83773454d1ff..70380f36337584 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -756,7 +756,7 @@ TEST(TestEmberAttributeBuffer, TestDecodeFailures) { // Bad boolean data EncodeTester tester(CreateFakeMeta(ZCL_BOOLEAN_ATTRIBUTE_TYPE, false /* nullable */)); - EXPECT_EQ(tester.TryDecode(true, { 123 }), CHIP_ERROR_INCORRECT_STATE); + EXPECT_EQ(tester.TryDecode(true, { 123 }), CHIP_ERROR_INVALID_ARGUMENT); } } From f88b744938da7982f427ff957782d0e5d293c5df Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:40:07 -0400 Subject: [PATCH 04/16] Test that you cannot decode a null value for non-nullable double and single --- .../tests/TestEmberAttributeDataBuffer.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 70380f36337584..9e2c31a777be89 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -1120,6 +1120,12 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) EXPECT_TRUE(tester.TryDecode>(DataModel::NullNullable, { 0, 0, 0xC0, 0x7F }).IsSuccess()); } + { + EncodeTester tester(CreateFakeMeta(ZCL_SINGLE_ATTRIBUTE_TYPE, false /* nullable */)); + EXPECT_EQ(tester.TryDecode>(DataModel::NullNullable, { 0, 0, 0xC0, 0x7F }), + CHIP_ERROR_INVALID_ARGUMENT); + } + { EncodeTester tester(CreateFakeMeta(ZCL_DOUBLE_ATTRIBUTE_TYPE, false /* nullable */)); EXPECT_TRUE(tester.TryDecode(123.55, { 0x33, 0x33, 0x33, 0x33, 0x33, 0xE3, 0x5E, 0x40 }).IsSuccess()); @@ -1133,4 +1139,11 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) EXPECT_TRUE( tester.TryDecode>(DataModel::NullNullable, { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }).IsSuccess()); } + + { + EncodeTester tester(CreateFakeMeta(ZCL_DOUBLE_ATTRIBUTE_TYPE, false /* nullable */)); + // non-nullable double + EXPECT_EQ(tester.TryDecode>(DataModel::NullNullable, { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }), + CHIP_ERROR_INVALID_ARGUMENT); + } } From e063fb24887588ddbd3fddd30c97ca3d5f487f11 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:49:13 -0400 Subject: [PATCH 05/16] Allow NAN for non-nullable floating points --- .../EmberAttributeDataBuffer.cpp | 6 ++-- .../tests/TestEmberAttributeDataBuffer.cpp | 30 ++++++++++++++++--- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index 7dbf39d3c43584..d8b8a3fa207354 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -523,9 +523,8 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV:: { return endianReader.StatusCode(); } - if (NumericAttributeTraits::IsNullValue(value.value)) + if (mIsNullable && NumericAttributeTraits::IsNullValue(value.value)) { - VerifyOrReturnError(mIsNullable, CHIP_ERROR_INVALID_ARGUMENT); return writer.PutNull(tag); } return writer.Put(tag, value.value); @@ -541,9 +540,8 @@ CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV:: { return endianReader.StatusCode(); } - if (NumericAttributeTraits::IsNullValue(value.value)) + if (mIsNullable && NumericAttributeTraits::IsNullValue(value.value)) { - VerifyOrReturnError(mIsNullable, CHIP_ERROR_INVALID_ARGUMENT); return writer.PutNull(tag); } return writer.Put(tag, value.value); diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 9e2c31a777be89..6667c04e571c09 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -35,6 +35,7 @@ #include #include +#include #include #include @@ -107,6 +108,28 @@ bool IsEqual(const T & a, const T & b) return a == b; } +template <> +bool IsEqual(const float & a, const float & b) +{ + + if (std::isnan(a) && std::isnan(b)) { + return true; + } + + return a == b; +} + +template <> +bool IsEqual(const double & a, const double & b) +{ + + if (std::isnan(a) && std::isnan(b)) { + return true; + } + + return a == b; +} + template <> bool IsEqual(const ByteSpan & a, const ByteSpan & b) { @@ -1122,8 +1145,8 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) { EncodeTester tester(CreateFakeMeta(ZCL_SINGLE_ATTRIBUTE_TYPE, false /* nullable */)); - EXPECT_EQ(tester.TryDecode>(DataModel::NullNullable, { 0, 0, 0xC0, 0x7F }), - CHIP_ERROR_INVALID_ARGUMENT); + // non-nullable float + EXPECT_TRUE(tester.TryDecode(std::nan("0"), { 0, 0, 0xC0, 0x7F }).IsSuccess()); } { @@ -1143,7 +1166,6 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) { EncodeTester tester(CreateFakeMeta(ZCL_DOUBLE_ATTRIBUTE_TYPE, false /* nullable */)); // non-nullable double - EXPECT_EQ(tester.TryDecode>(DataModel::NullNullable, { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }), - CHIP_ERROR_INVALID_ARGUMENT); + EXPECT_TRUE(tester.TryDecode(std::nan("0"), { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }).IsSuccess()); } } From f5cf0595e65172fdfa521f09ab3cf2d49e628f3d Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:51:18 -0400 Subject: [PATCH 06/16] Add test case for non nullable bool --- .../tests/TestEmberAttributeDataBuffer.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 6667c04e571c09..a0ddf662dcffc6 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -1120,6 +1120,13 @@ TEST(TestEmberAttributeBuffer, TestDecodeBool) EXPECT_TRUE(tester.TryDecode>(false, { 0 }).IsSuccess()); EXPECT_TRUE(tester.TryDecode>(DataModel::NullNullable, { 0xFF }).IsSuccess()); } + + { + // Boolean that is NOT nullable + EncodeTester tester(CreateFakeMeta(ZCL_BOOLEAN_ATTRIBUTE_TYPE, false /* nullable */)); + EXPECT_EQ(tester.TryDecode>(DataModel::NullNullable, { 0xFF }), CHIP_ERROR_INVALID_ARGUMENT); + EXPECT_EQ(tester.TryDecode(true, { 0xFF }), CHIP_ERROR_INVALID_ARGUMENT); + } } TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) From ed35ddd18d529feef50d880f2b7cffe2b7f1bbdb Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 09:51:37 -0400 Subject: [PATCH 07/16] Restyle --- .../tests/TestEmberAttributeDataBuffer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index a0ddf662dcffc6..844cc1166810e6 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -112,7 +112,8 @@ template <> bool IsEqual(const float & a, const float & b) { - if (std::isnan(a) && std::isnan(b)) { + if (std::isnan(a) && std::isnan(b)) + { return true; } @@ -123,7 +124,8 @@ template <> bool IsEqual(const double & a, const double & b) { - if (std::isnan(a) && std::isnan(b)) { + if (std::isnan(a) && std::isnan(b)) + { return true; } From 7ac4e4f7756856df17bc1b830cd71ea36508c172 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 10:17:05 -0400 Subject: [PATCH 08/16] Add a header for efr32 --- .../tests/TestEmberAttributeDataBuffer.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 844cc1166810e6..438e35b427515c 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -35,6 +35,8 @@ #include #include +#include + #include #include #include From 919a42e366ca007645919849edd299836e26c3db Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 10:44:35 -0400 Subject: [PATCH 09/16] Update src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp Co-authored-by: Boris Zbarsky --- .../codegen-data-model-provider/EmberAttributeDataBuffer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index d8b8a3fa207354..83dc11c46aa0d0 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -384,7 +384,7 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer uint8_t raw_bytes[8]; - const bool isSigned = chip::app::IsSignedAttributeType(mAttributeType); + const bool isSigned = IsSignedAttributeType(mAttributeType); unsigned byteCount; uint64_t nullValueAsU64; From b55d7ff1aa8dbe5e97da37473c8204ad9d5ab1b4 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 10:44:43 -0400 Subject: [PATCH 10/16] Update src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp Co-authored-by: Boris Zbarsky --- .../codegen-data-model-provider/EmberAttributeDataBuffer.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index 83dc11c46aa0d0..a9cf4d9b15c316 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -456,8 +456,8 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer case ZCL_INT32U_ATTRIBUTE_TYPE: // Unsigned 32-bit integer case ZCL_INT40U_ATTRIBUTE_TYPE: // Unsigned 40-bit integer case ZCL_INT48U_ATTRIBUTE_TYPE: // Unsigned 48-bit integer - case ZCL_INT56U_ATTRIBUTE_TYPE: // Signed 56-bit integer - case ZCL_INT64U_ATTRIBUTE_TYPE: // Signed 64-bit integer + case ZCL_INT56U_ATTRIBUTE_TYPE: // Unsigned 56-bit integer + case ZCL_INT64U_ATTRIBUTE_TYPE: // Unsigned 64-bit integer return writer.Put(tag, value.uint_value); case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer case ZCL_INT16S_ATTRIBUTE_TYPE: // Signed 16-bit integer From c1c607c7839888da77d77ef9f52a91819dd743ea Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 10:46:15 -0400 Subject: [PATCH 11/16] Remove extra comment --- src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index a9cf4d9b15c316..b80398b2deab93 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -444,7 +444,6 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer // code prioritizes code size over readability here. if (mIsNullable && (value.uint_value == nullValueAsU64)) { - // MaxValue is used for NULL setting return writer.PutNull(tag); } From cb63350634604366ecf2e2acedcd1a5adda0e5e0 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 10:47:02 -0400 Subject: [PATCH 12/16] Replace switch with if --- .../EmberAttributeDataBuffer.cpp | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp index b80398b2deab93..7eb2e9d2b1d2ea 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.cpp @@ -447,24 +447,12 @@ CHIP_ERROR EmberAttributeDataBuffer::EncodeInteger(chip::TLV::TLVWriter & writer return writer.PutNull(tag); } - switch (mAttributeType) + if (isSigned) { - case ZCL_INT8U_ATTRIBUTE_TYPE: // Unsigned 8-bit integer - case ZCL_INT16U_ATTRIBUTE_TYPE: // Unsigned 16-bit integer - case ZCL_INT24U_ATTRIBUTE_TYPE: // Unsigned 24-bit integer - case ZCL_INT32U_ATTRIBUTE_TYPE: // Unsigned 32-bit integer - case ZCL_INT40U_ATTRIBUTE_TYPE: // Unsigned 40-bit integer - case ZCL_INT48U_ATTRIBUTE_TYPE: // Unsigned 48-bit integer - case ZCL_INT56U_ATTRIBUTE_TYPE: // Unsigned 56-bit integer - case ZCL_INT64U_ATTRIBUTE_TYPE: // Unsigned 64-bit integer - return writer.Put(tag, value.uint_value); - case ZCL_INT8S_ATTRIBUTE_TYPE: // Signed 8-bit integer - case ZCL_INT16S_ATTRIBUTE_TYPE: // Signed 16-bit integer - case ZCL_INT24S_ATTRIBUTE_TYPE: // Signed 24-bit integer - case ZCL_INT32S_ATTRIBUTE_TYPE: // Signed 32-bit integer - default: return writer.Put(tag, value.int_value); } + + return writer.Put(tag, value.uint_value); } CHIP_ERROR EmberAttributeDataBuffer::Encode(chip::TLV::TLVWriter & writer, TLV::Tag tag) const From d0e58efb7afda4f6e4c1c4bf52738d0e969699c2 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 10:47:40 -0400 Subject: [PATCH 13/16] Comment fix --- src/app/codegen-data-model-provider/EmberAttributeDataBuffer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.h b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.h index c3d7acfcafb72b..f4a2de268591d4 100644 --- a/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.h +++ b/src/app/codegen-data-model-provider/EmberAttributeDataBuffer.h @@ -85,7 +85,7 @@ class EmberAttributeDataBuffer /// Takes into account internal mIsNullable. CHIP_ERROR DecodeSignedInteger(chip::TLV::TLVReader & reader, EndianWriter & writer); - /// Encodes the UNSIGNED integer into `writer`. + /// Encodes the given integer into `writer`. /// Takes into account internal mIsNullable. CHIP_ERROR EncodeInteger(chip::TLV::TLVWriter & writer, TLV::Tag tag, EndianReader & reader) const; From 5a2681f337fe3607b7ab64fa00a4298f359941b3 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 11:30:52 -0400 Subject: [PATCH 14/16] Another try to make efr32 build of tests happy --- .../tests/TestEmberAttributeDataBuffer.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 438e35b427515c..4dfa3d97740340 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -37,7 +37,6 @@ #include -#include #include #include @@ -114,7 +113,7 @@ template <> bool IsEqual(const float & a, const float & b) { - if (std::isnan(a) && std::isnan(b)) + if (isnan(a) && isnan(b)) { return true; } @@ -126,7 +125,7 @@ template <> bool IsEqual(const double & a, const double & b) { - if (std::isnan(a) && std::isnan(b)) + if (isnan(a) && isnan(b)) { return true; } @@ -1157,7 +1156,7 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) { EncodeTester tester(CreateFakeMeta(ZCL_SINGLE_ATTRIBUTE_TYPE, false /* nullable */)); // non-nullable float - EXPECT_TRUE(tester.TryDecode(std::nan("0"), { 0, 0, 0xC0, 0x7F }).IsSuccess()); + EXPECT_TRUE(tester.TryDecode(NAN, { 0, 0, 0xC0, 0x7F }).IsSuccess()); } { @@ -1177,6 +1176,6 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) { EncodeTester tester(CreateFakeMeta(ZCL_DOUBLE_ATTRIBUTE_TYPE, false /* nullable */)); // non-nullable double - EXPECT_TRUE(tester.TryDecode(std::nan("0"), { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }).IsSuccess()); + EXPECT_TRUE(tester.TryDecode(NAN, { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }).IsSuccess()); } } From a0ec78b9c118ca4c4045256c2c9bbaf6723dd562 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 11:51:44 -0400 Subject: [PATCH 15/16] Move includes around, to try to work around issues within efr32 compiles... --- .../tests/TestEmberAttributeDataBuffer.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 4dfa3d97740340..3feb8f80f21c53 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -16,6 +16,8 @@ */ #include +#include + #include #include @@ -35,7 +37,6 @@ #include #include -#include #include #include From 8276b4d331ed7d45332f7761c1215b4a889d9300 Mon Sep 17 00:00:00 2001 From: Andrei Litvin Date: Mon, 28 Oct 2024 11:59:41 -0400 Subject: [PATCH 16/16] more updates, this time local efr32 compiles --- .../tests/TestEmberAttributeDataBuffer.cpp | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp index 3feb8f80f21c53..04e8b94dcce809 100644 --- a/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp +++ b/src/app/codegen-data-model-provider/tests/TestEmberAttributeDataBuffer.cpp @@ -37,7 +37,6 @@ #include #include - #include #include @@ -113,8 +112,7 @@ bool IsEqual(const T & a, const T & b) template <> bool IsEqual(const float & a, const float & b) { - - if (isnan(a) && isnan(b)) + if (std::isnan(a) && std::isnan(b)) { return true; } @@ -125,8 +123,7 @@ bool IsEqual(const float & a, const float & b) template <> bool IsEqual(const double & a, const double & b) { - - if (isnan(a) && isnan(b)) + if (std::isnan(a) && std::isnan(b)) { return true; } @@ -1157,7 +1154,7 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) { EncodeTester tester(CreateFakeMeta(ZCL_SINGLE_ATTRIBUTE_TYPE, false /* nullable */)); // non-nullable float - EXPECT_TRUE(tester.TryDecode(NAN, { 0, 0, 0xC0, 0x7F }).IsSuccess()); + EXPECT_TRUE(tester.TryDecode(std::nanf("0"), { 0, 0, 0xC0, 0x7F }).IsSuccess()); } { @@ -1177,6 +1174,6 @@ TEST(TestEmberAttributeBuffer, TestDecodeFloatingPoint) { EncodeTester tester(CreateFakeMeta(ZCL_DOUBLE_ATTRIBUTE_TYPE, false /* nullable */)); // non-nullable double - EXPECT_TRUE(tester.TryDecode(NAN, { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }).IsSuccess()); + EXPECT_TRUE(tester.TryDecode(std::nan("0"), { 0, 0, 0, 0, 0, 0, 0xF8, 0x7F }).IsSuccess()); } }