Skip to content

Commit

Permalink
Enable float/double typed attributes. (#12439)
Browse files Browse the repository at this point in the history
* Enable float and double attributes on the server.

* Enable float/double attribute support in clients.

* Add tests for float/double attributes.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Apr 25, 2022
1 parent 8531716 commit c270cce
Show file tree
Hide file tree
Showing 38 changed files with 8,105 additions and 3,317 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15785,7 +15785,7 @@
"code": 23,
"mfgCode": null,
"side": "server",
"included": 0,
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
Expand All @@ -15800,7 +15800,7 @@
"code": 24,
"mfgCode": null,
"side": "server",
"included": 0,
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
Expand Down Expand Up @@ -16445,7 +16445,7 @@
"code": 32791,
"mfgCode": null,
"side": "server",
"included": 0,
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
Expand All @@ -16460,7 +16460,7 @@
"code": 32792,
"mfgCode": null,
"side": "server",
"included": 0,
"included": 1,
"storageOption": "RAM",
"singleton": 0,
"bounded": 0,
Expand Down
46 changes: 46 additions & 0 deletions examples/chip-tool/commands/common/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,28 @@ bool Command::InitArgument(size_t argIndex, char * argValue)
break;
}

case ArgumentType::Float: {
if (arg.optional)
arg.value = &(static_cast<chip::Optional<float> *>(arg.value))->Emplace();
float * value = static_cast<float *>(arg.value);
std::stringstream ss;
ss << argValue;
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof());
break;
}

case ArgumentType::Double: {
if (arg.optional)
arg.value = &(static_cast<chip::Optional<double> *>(arg.value))->Emplace();
double * value = static_cast<double *>(arg.value);
std::stringstream ss;
ss << argValue;
ss >> *value;
isValidArgument = (!ss.fail() && ss.eof());
break;
}

case ArgumentType::Address: {
if (arg.optional)
arg.value = &(reinterpret_cast<chip::Optional<AddressWithInterface> *>(arg.value))->Emplace();
Expand Down Expand Up @@ -429,6 +451,30 @@ size_t Command::AddArgument(const char * name, AddressWithInterface * out, bool
return AddArgumentToList(std::move(arg));
}

size_t Command::AddArgument(const char * name, float min, float max, float * out, bool optional)
{
Argument arg;
arg.type = ArgumentType::Float;
arg.name = name;
arg.value = reinterpret_cast<void *>(out);
arg.optional = optional;
// Ignore min/max for now; they're always +-Infinity anyway.

return AddArgumentToList(std::move(arg));
}

size_t Command::AddArgument(const char * name, double min, double max, double * out, bool optional)
{
Argument arg;
arg.type = ArgumentType::Double;
arg.name = name;
arg.value = reinterpret_cast<void *>(out);
arg.optional = optional;
// Ignore min/max for now; they're always +-Infinity anyway.

return AddArgumentToList(std::move(arg));
}

size_t Command::AddArgument(const char * name, int64_t min, uint64_t max, void * out, ArgumentType type, bool optional)
{
Argument arg;
Expand Down
5 changes: 5 additions & 0 deletions examples/chip-tool/commands/common/Command.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ enum ArgumentType
Number_int16,
Number_int32,
Number_int64,
Float,
Double,
Boolean,
String,
CharString,
Expand Down Expand Up @@ -153,6 +155,9 @@ class Command
return AddArgument(name, min, max, reinterpret_cast<void *>(out), Number_uint64, optional);
}

size_t AddArgument(const char * name, float min, float max, float * out, bool optional = false);
size_t AddArgument(const char * name, double min, double max, double * out, bool optional = false);

template <typename T, typename = std::enable_if_t<std::is_enum<T>::value>>
size_t AddArgument(const char * name, int64_t min, uint64_t max, T * out, bool optional = false)
{
Expand Down
26 changes: 26 additions & 0 deletions examples/chip-tool/templates/commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ static void OnInt64sAttributeResponse(void * context, int64_t value)
command->SetCommandExitStatus(CHIP_NO_ERROR);
}

static void OnFloatAttributeReport(void * context, float value)
{
ChipLogProgress(chipTool, "Float attribute Response: %f", value);
}

static void OnFloatAttributeResponse(void * context, float value)
{
OnFloatAttributeReport(context, value);

ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}

static void OnDoubleAttributeReport(void * context, double value)
{
ChipLogProgress(chipTool, "Double attribute Response: %f", value);
}

static void OnDoubleAttributeResponse(void * context, double value)
{
OnDoubleAttributeReport(context, value);

ModelCommand * command = static_cast<ModelCommand *>(context);
command->SetCommandExitStatus(CHIP_NO_ERROR);
}

static void OnOctetStringAttributeReport(void * context, const chip::ByteSpan value)
{
char buffer[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
Expand Down
4 changes: 2 additions & 2 deletions examples/chip-tool/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function asTypeMinValue(type)
return '0';
case 'float':
case 'double':
return `-std::numeric_limits<${basicType}>::infinity`;
return `-std::numeric_limits<${basicType}>::infinity()`;
default:
error = 'asTypeMinValue: Unhandled underlying type ' + zclType + ' for original type ' + type;
throw error;
Expand Down Expand Up @@ -87,7 +87,7 @@ function asTypeMaxValue(type)
return 'UINT' + parseInt(basicType.slice(4)) + '_MAX';
case 'float':
case 'double':
return `std::numeric_limits<${basicType}>::infinity`;
return `std::numeric_limits<${basicType}>::infinity()`;
default:
return 'err';
error = 'asTypeMaxValue: Unhandled underlying type ' + zclType + ' for original type ' + type;
Expand Down
1 change: 1 addition & 0 deletions examples/chip-tool/templates/tests-commands.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <commands/tests/TestCommand.h>
#include <commands/common/CommandInvoker.h>
#include <math.h> // For INFINITY

class TestList : public Command
{
Expand Down
Loading

0 comments on commit c270cce

Please sign in to comment.