Skip to content

Commit

Permalink
Add support for float and double types. (#12252)
Browse files Browse the repository at this point in the history
* Adds Encode and Decode support for float and double.

* Adds support for float/double in various code generation type
  helpers (for chip-tool, java, darwin).

* Adds float and double members to the SimpleStruct test struct.

* Adds a command to send and receive a SimpleStruct, and a yaml test
  that does that.

* Fixes chip-tool to handle BitFlags value checking correctly (since
  SimpleStruct has a BitFlags member).

* Removes some code in ClustersHelper.js that failed for float/double
  types and whose output was completely unused.

* Fixes templates for old-style commands and java that completely blew
  up on structs in a response command field to at least generate code
  successfully, though that code wouldn't work right if it ever ran.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Aug 1, 2023
1 parent 26cf972 commit 1403270
Show file tree
Hide file tree
Showing 58 changed files with 2,916 additions and 1,853 deletions.
20 changes: 18 additions & 2 deletions examples/all-clusters-app/all-clusters-common/all-clusters-app.zap
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"endpointTypes": [
{
"name": "Anonymous Endpoint Type",
"deviceTypeName": "CHIP-All-Clusters-Server",
"deviceTypeName": "MA-all-clusters-app",
"deviceTypeCode": 0,
"deviceTypeProfileId": 259,
"clusters": [
Expand Down Expand Up @@ -6812,7 +6812,7 @@
},
{
"name": "Anonymous Endpoint Type",
"deviceTypeName": "CHIP-All-Clusters-Server",
"deviceTypeName": "MA-all-clusters-app",
"deviceTypeCode": 0,
"deviceTypeProfileId": 259,
"clusters": [
Expand Down Expand Up @@ -15311,6 +15311,14 @@
"source": "client",
"incoming": 1,
"outgoing": 0
},
{
"name": "SimpleStructEchoRequest",
"code": 17,
"mfgCode": null,
"source": "client",
"incoming": 1,
"outgoing": 0
}
],
"attributes": [
Expand Down Expand Up @@ -15378,6 +15386,14 @@
"source": "server",
"incoming": 0,
"outgoing": 1
},
{
"name": "SimpleStructResponse",
"code": 9,
"mfgCode": null,
"source": "server",
"incoming": 0,
"outgoing": 1
}
],
"attributes": [
Expand Down
7 changes: 7 additions & 0 deletions examples/chip-tool/commands/tests/TestCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <app/tests/suites/pics/PICSBooleanExpressionParser.h>
#include <app/tests/suites/pics/PICSBooleanReader.h>
#include <controller/ExampleOperationalCredentialsIssuer.h>
#include <lib/support/BitFlags.h>
#include <lib/support/TypeTraits.h>
#include <lib/support/UnitTestUtils.h>
#include <type_traits>
Expand Down Expand Up @@ -125,6 +126,12 @@ class TestCommand : public CHIPCommand
return true;
}

template <typename T, typename U>
bool CheckValue(const char * itemName, chip::BitFlags<T> current, U expected)
{
return CheckValue(itemName, current.Raw(), expected);
}

template <typename T, typename U, typename std::enable_if_t<std::is_enum<T>::value, int> = 0>
bool CheckValue(const char * itemName, T current, U expected)
{
Expand Down
7 changes: 7 additions & 0 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ CHIP_ERROR LogValue(const char * label, size_t indent, X value)
return CHIP_NO_ERROR;
}

template <typename X, typename std::enable_if_t<std::is_floating_point<X>::value, int> = 0>
CHIP_ERROR LogValue(const char * label, size_t indent, X value)
{
ChipLogProgress(chipTool, "%s%s: %s", IndentStr(indent).c_str(), label, std::to_string(value).c_str());
return CHIP_NO_ERROR;
}

CHIP_ERROR LogValue(const char * label, size_t indent, bool value)
{
ChipLogProgress(chipTool, "%s%s: %s", IndentStr(indent).c_str(), label, value ? "TRUE" : "FALSE");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{{#if_is_bitmap type}}
static_cast<{{zapTypeToEncodableClusterObjectType type ns=ns}}>({{definedValue}});
{{else}}
{{asTypeLiteralSuffix definedValue type}};
{{asTypedLiteral definedValue type}};
{{/if_is_bitmap}}
{{/if_chip_enum}}
{{/if_is_struct}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
VerifyOrReturn(CheckValue
{{~#if (isOctetString type)}}AsString("{{label}}", {{actual}}, chip::ByteSpan(chip::Uint8::from_const_char("{{octetStringEscapedForCLiteral expected}}"), {{expected.length}}))
{{else if (isCharString type)}}AsString("{{label}}", {{actual}}, chip::CharSpan("{{expected}}", {{expected.length}}))
{{else}}<{{chipType}}>("{{label}}", {{actual}}, {{asTypeLiteralSuffix expected type}})
{{else}}("{{label}}", {{actual}}, {{asTypedLiteral expected type}})
{{/if}}
);
{{/if_is_struct}}
Expand Down
21 changes: 21 additions & 0 deletions src/app/clusters/test-cluster-server/test-cluster-server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,27 @@ bool emberAfTestClusterClusterTestNullableOptionalRequestCallback(
return true;
}

bool emberAfTestClusterClusterSimpleStructEchoRequestCallback(CommandHandler * commandObj, const ConcreteCommandPath & commandPath,
const Commands::SimpleStructEchoRequest::DecodableType & commandData)
{
Commands::SimpleStructResponse::Type response;
response.arg1.a = commandData.arg1.a;
response.arg1.b = commandData.arg1.b;
response.arg1.c = commandData.arg1.c;
response.arg1.d = commandData.arg1.d;
response.arg1.e = commandData.arg1.e;
response.arg1.f = commandData.arg1.f;
response.arg1.g = commandData.arg1.g;
response.arg1.h = commandData.arg1.h;

CHIP_ERROR err = commandObj->AddResponseData(commandPath, response);
if (err != CHIP_NO_ERROR)
{
emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_FAILURE);
}
return true;
}

// -----------------------------------------------------------------------------
// Plugin initialization

Expand Down
6 changes: 6 additions & 0 deletions src/app/data-model/Decode.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ CHIP_ERROR Decode(TLV::TLVReader & reader, X & x)
return reader.Get(x);
}

template <typename X, typename std::enable_if_t<std::is_floating_point<X>::value, int> = 0>
CHIP_ERROR Decode(TLV::TLVReader & reader, X & x)
{
return reader.Get(x);
}

template <typename X, typename std::enable_if_t<std::is_enum<X>::value, int> = 0>
CHIP_ERROR Decode(TLV::TLVReader & reader, X & x)
{
Expand Down
6 changes: 6 additions & 0 deletions src/app/data-model/Encode.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag, X x)
return writer.Put(tag, x);
}

template <typename X, typename std::enable_if_t<std::is_floating_point<X>::value, int> = 0>
CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag, X x)
{
return writer.Put(tag, x);
}

template <typename X, typename std::enable_if_t<std::is_enum<X>::value, int> = 0>
CHIP_ERROR Encode(TLV::TLVWriter & writer, TLV::Tag tag, X x)
{
Expand Down
71 changes: 71 additions & 0 deletions src/app/tests/suites/TestCluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
}
response:
values:
Expand All @@ -893,6 +895,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
}
response:
values:
Expand All @@ -918,6 +922,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
},
}
response:
Expand All @@ -943,6 +949,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
},
}
response:
Expand Down Expand Up @@ -970,6 +978,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
},
d:
[
Expand All @@ -980,6 +990,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
{
a: 2,
Expand All @@ -988,6 +1000,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
],
e: [1, 2, 3],
Expand Down Expand Up @@ -1024,6 +1038,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
},
d:
[
Expand All @@ -1034,6 +1050,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
{
a: 2,
Expand All @@ -1042,6 +1060,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
],
e: [1, 2, 3],
Expand All @@ -1058,6 +1078,37 @@ tests:
- name: "value"
value: false

- label: "Send Test Command With Struct Argument and see what we get back"
command: "SimpleStructEchoRequest"
arguments:
values:
- name: "arg1"
value:
{
a: 17,
b: false,
c: 2,
d: "octet_string",
e: "char_string",
f: 1,
g: 0.1,
h: 0.1,
}
response:
values:
- name: "arg1"
value:
{
a: 17,
b: false,
c: 2,
d: "octet_string",
e: "char_string",
f: 1,
g: 0.1,
h: 0.1,
}

# Tests for List

- label: "Send Test Command With List of INT8U and none of them is set to 0"
Expand Down Expand Up @@ -1121,6 +1172,8 @@ tests:
d: "first_octet_string",
e: "first_char_string",
f: 1,
g: 0,
h: 0,
},
{
a: 1,
Expand All @@ -1129,6 +1182,8 @@ tests:
d: "second_octet_string",
e: "second_char_string",
f: 1,
g: 0,
h: 0,
},
]
response:
Expand All @@ -1152,6 +1207,8 @@ tests:
d: "second_octet_string",
e: "second_char_string",
f: 1,
g: 0,
h: 0,
},
{
a: 0,
Expand All @@ -1160,6 +1217,8 @@ tests:
d: "first_octet_string",
e: "first_char_string",
f: 1,
g: 0,
h: 0,
},
]
response:
Expand Down Expand Up @@ -1188,6 +1247,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
},
d:
[
Expand All @@ -1198,6 +1259,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
{
a: 2,
Expand All @@ -1206,6 +1269,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
],
e: [1, 2, 3],
Expand Down Expand Up @@ -1244,6 +1309,8 @@ tests:
d: "octet_string",
e: "char_string",
f: 1,
g: 0,
h: 0,
},
d:
[
Expand All @@ -1254,6 +1321,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
{
a: 2,
Expand All @@ -1262,6 +1331,8 @@ tests:
d: "nested_octet_string",
e: "nested_char_string",
f: 1,
g: 0,
h: 0,
},
],
e: [1, 2, 3],
Expand Down
Loading

0 comments on commit 1403270

Please sign in to comment.