diff --git a/src/app/data-model/Encode.h b/src/app/data-model/Encode.h index 3e930a1c0ec24a..1cde773928640a 100644 --- a/src/app/data-model/Encode.h +++ b/src/app/data-model/Encode.h @@ -31,6 +31,18 @@ namespace chip { namespace app { namespace DataModel { +namespace detail { +// A way to detect whether an enum has a kUnknownEnumValue value, for use in enable_if. +template +using VoidType = void; + +template +constexpr bool HasUnknownValue = false; + +template +constexpr bool HasUnknownValue> = true; +} // namespace detail + /* * @brief * Set of overloaded encode methods that based on the type of cluster element passed in, @@ -48,9 +60,18 @@ CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag, X x) return writer.Put(tag, x); } -template ::value, int> = 0> +template ::value && !detail::HasUnknownValue, int> = 0> +CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag, X x) +{ + return writer.Put(tag, x); +} + +template ::value && detail::HasUnknownValue, int> = 0> CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag, X x) { + if (x == X::kUnknownEnumValue) { + return CHIP_IM_GLOBAL_STATUS(ConstraintError); + } return writer.Put(tag, x); } diff --git a/src/app/tests/suites/TestCluster.yaml b/src/app/tests/suites/TestCluster.yaml index 5f127d112aeca9..e89a892dd32ee5 100644 --- a/src/app/tests/suites/TestCluster.yaml +++ b/src/app/tests/suites/TestCluster.yaml @@ -1068,11 +1068,8 @@ tests: - name: "arg2" value: 101 response: - values: - - name: "arg1" - value: 20003 - - name: "arg2" - value: 4 + # Attempting to echo back the invalid enum value should fail. + error: FAILURE # Tests for Struct diff --git a/zzz_generated/chip-tool/zap-generated/test/Commands.h b/zzz_generated/chip-tool/zap-generated/test/Commands.h index 1666169997e780..b734e65df2218e 100644 --- a/zzz_generated/chip-tool/zap-generated/test/Commands.h +++ b/zzz_generated/chip-tool/zap-generated/test/Commands.h @@ -58751,13 +58751,7 @@ class TestClusterSuite : public TestCommand } break; case 156: - VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); - { - chip::app::Clusters::UnitTesting::Commands::TestEnumsResponse::DecodableType value; - VerifyOrReturn(CheckDecodeValue(chip::app::DataModel::Decode(*data, value))); - VerifyOrReturn(CheckValue("arg1", value.arg1, 20003U)); - VerifyOrReturn(CheckValue("arg2", value.arg2, 4U)); - } + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), EMBER_ZCL_STATUS_FAILURE)); break; case 157: VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); diff --git a/zzz_generated/darwin-framework-tool/zap-generated/test/Commands.h b/zzz_generated/darwin-framework-tool/zap-generated/test/Commands.h index 391eef964a706e..bfccaf83dbe46f 100644 --- a/zzz_generated/darwin-framework-tool/zap-generated/test/Commands.h +++ b/zzz_generated/darwin-framework-tool/zap-generated/test/Commands.h @@ -82389,7 +82389,7 @@ class TestCluster : public TestCommandBridge { VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); break; case 156: - VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); + VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), EMBER_ZCL_STATUS_FAILURE)); break; case 157: VerifyOrReturn(CheckValue("status", chip::to_underlying(status.mStatus), 0)); @@ -87068,18 +87068,11 @@ class TestCluster : public TestCommandBridge { completion:^(MTRUnitTestingClusterTestEnumsResponseParams * _Nullable values, NSError * _Nullable err) { NSLog(@"Send a command with a vendor_id and invalid enum Error: %@", err); - VerifyOrReturn(CheckValue("status", err ? err.code : 0, 0)); - - { - id actualValue = values.arg1; - VerifyOrReturn(CheckValue("arg1", actualValue, 20003U)); - } - - { - id actualValue = values.arg2; - VerifyOrReturn(CheckValue("arg2", actualValue, 4U)); - } - + VerifyOrReturn(CheckValue("status", + err ? ([err.domain isEqualToString:MTRInteractionErrorDomain] ? err.code + : EMBER_ZCL_STATUS_FAILURE) + : 0, + EMBER_ZCL_STATUS_FAILURE)); NextTest(); }];