Skip to content

Commit 1007112

Browse files
bzbarsky-applepull[bot]
authored andcommitted
Enable float/double typed attributes. (#12439)
* Enable float and double attributes on the server. * Enable float/double attribute support in clients. * Add tests for float/double attributes.
1 parent 08836d2 commit 1007112

38 files changed

+8105
-3317
lines changed

examples/all-clusters-app/all-clusters-common/all-clusters-app.zap

+4-4
Original file line numberDiff line numberDiff line change
@@ -15785,7 +15785,7 @@
1578515785
"code": 23,
1578615786
"mfgCode": null,
1578715787
"side": "server",
15788-
"included": 0,
15788+
"included": 1,
1578915789
"storageOption": "RAM",
1579015790
"singleton": 0,
1579115791
"bounded": 0,
@@ -15800,7 +15800,7 @@
1580015800
"code": 24,
1580115801
"mfgCode": null,
1580215802
"side": "server",
15803-
"included": 0,
15803+
"included": 1,
1580415804
"storageOption": "RAM",
1580515805
"singleton": 0,
1580615806
"bounded": 0,
@@ -16445,7 +16445,7 @@
1644516445
"code": 32791,
1644616446
"mfgCode": null,
1644716447
"side": "server",
16448-
"included": 0,
16448+
"included": 1,
1644916449
"storageOption": "RAM",
1645016450
"singleton": 0,
1645116451
"bounded": 0,
@@ -16460,7 +16460,7 @@
1646016460
"code": 32792,
1646116461
"mfgCode": null,
1646216462
"side": "server",
16463-
"included": 0,
16463+
"included": 1,
1646416464
"storageOption": "RAM",
1646516465
"singleton": 0,
1646616466
"bounded": 0,

examples/chip-tool/commands/common/Command.cpp

+46
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,28 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
357357
break;
358358
}
359359

360+
case ArgumentType::Float: {
361+
if (arg.optional)
362+
arg.value = &(static_cast<chip::Optional<float> *>(arg.value))->Emplace();
363+
float * value = static_cast<float *>(arg.value);
364+
std::stringstream ss;
365+
ss << argValue;
366+
ss >> *value;
367+
isValidArgument = (!ss.fail() && ss.eof());
368+
break;
369+
}
370+
371+
case ArgumentType::Double: {
372+
if (arg.optional)
373+
arg.value = &(static_cast<chip::Optional<double> *>(arg.value))->Emplace();
374+
double * value = static_cast<double *>(arg.value);
375+
std::stringstream ss;
376+
ss << argValue;
377+
ss >> *value;
378+
isValidArgument = (!ss.fail() && ss.eof());
379+
break;
380+
}
381+
360382
case ArgumentType::Address: {
361383
if (arg.optional)
362384
arg.value = &(reinterpret_cast<chip::Optional<AddressWithInterface> *>(arg.value))->Emplace();
@@ -429,6 +451,30 @@ size_t Command::AddArgument(const char * name, AddressWithInterface * out, bool
429451
return AddArgumentToList(std::move(arg));
430452
}
431453

454+
size_t Command::AddArgument(const char * name, float min, float max, float * out, bool optional)
455+
{
456+
Argument arg;
457+
arg.type = ArgumentType::Float;
458+
arg.name = name;
459+
arg.value = reinterpret_cast<void *>(out);
460+
arg.optional = optional;
461+
// Ignore min/max for now; they're always +-Infinity anyway.
462+
463+
return AddArgumentToList(std::move(arg));
464+
}
465+
466+
size_t Command::AddArgument(const char * name, double min, double max, double * out, bool optional)
467+
{
468+
Argument arg;
469+
arg.type = ArgumentType::Double;
470+
arg.name = name;
471+
arg.value = reinterpret_cast<void *>(out);
472+
arg.optional = optional;
473+
// Ignore min/max for now; they're always +-Infinity anyway.
474+
475+
return AddArgumentToList(std::move(arg));
476+
}
477+
432478
size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, void * out, ArgumentType type, bool optional)
433479
{
434480
Argument arg;

examples/chip-tool/commands/common/Command.h

+5
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ enum ArgumentType
6363
Number_int16,
6464
Number_int32,
6565
Number_int64,
66+
Float,
67+
Double,
6668
Boolean,
6769
String,
6870
CharString,
@@ -153,6 +155,9 @@ class Command
153155
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_uint64, optional);
154156
}
155157

158+
size_t AddArgument(const char * name, float min, float max, float * out, bool optional = false);
159+
size_t AddArgument(const char * name, double min, double max, double * out, bool optional = false);
160+
156161
template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
157162
size_t AddArgument(const char * name, int64_t min, uint64_t max, T * out, bool optional = false)
158163
{

examples/chip-tool/templates/commands.zapt

+26
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,32 @@ static void OnInt64sAttributeResponse(void * context, int64_t value)
309309
command->SetCommandExitStatus(CHIP_NO_ERROR);
310310
}
311311

312+
static void OnFloatAttributeReport(void * context, float value)
313+
{
314+
ChipLogProgress(chipTool, "Float attribute Response: %f", value);
315+
}
316+
317+
static void OnFloatAttributeResponse(void * context, float value)
318+
{
319+
OnFloatAttributeReport(context, value);
320+
321+
ModelCommand * command = static_cast<ModelCommand *>(context);
322+
command->SetCommandExitStatus(CHIP_NO_ERROR);
323+
}
324+
325+
static void OnDoubleAttributeReport(void * context, double value)
326+
{
327+
ChipLogProgress(chipTool, "Double attribute Response: %f", value);
328+
}
329+
330+
static void OnDoubleAttributeResponse(void * context, double value)
331+
{
332+
OnDoubleAttributeReport(context, value);
333+
334+
ModelCommand * command = static_cast<ModelCommand *>(context);
335+
command->SetCommandExitStatus(CHIP_NO_ERROR);
336+
}
337+
312338
static void OnOctetStringAttributeReport(void * context, const chip::ByteSpan value)
313339
{
314340
char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];

examples/chip-tool/templates/helper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function asTypeMinValue(type)
5050
return '0';
5151
case 'float':
5252
case 'double':
53-
return `-std::numeric_limits<${basicType}>::infinity`;
53+
return `-std::numeric_limits<${basicType}>::infinity()`;
5454
default:
5555
error = 'asTypeMinValue: Unhandled underlying type ' + zclType + ' for original type ' + type;
5656
throw error;
@@ -87,7 +87,7 @@ function asTypeMaxValue(type)
8787
return 'UINT' + parseInt(basicType.slice(4)) + '_MAX';
8888
case 'float':
8989
case 'double':
90-
return `std::numeric_limits<${basicType}>::infinity`;
90+
return `std::numeric_limits<${basicType}>::infinity()`;
9191
default:
9292
return 'err';
9393
error = 'asTypeMaxValue: Unhandled underlying type ' + zclType + ' for original type ' + type;

examples/chip-tool/templates/tests-commands.zapt

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <commands/tests/TestCommand.h>
66
#include <commands/common/CommandInvoker.h>
7+
#include <math.h> // For INFINITY
78

89
class TestList : public Command
910
{

0 commit comments

Comments
 (0)