Skip to content

Commit

Permalink
Enable float/double attribute support in clients.
Browse files Browse the repository at this point in the history
  • Loading branch information
bzbarsky-apple committed Dec 1, 2021
1 parent 81b9e20 commit 05ccb30
Show file tree
Hide file tree
Showing 28 changed files with 2,401 additions and 3 deletions.
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
4 changes: 4 additions & 0 deletions src/app/util/im-client-callbacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ typedef void (*Int64uAttributeCallback)(void * context, uint64_t value);
typedef void (*NullableInt64uAttributeCallback)(void * context, const chip::app::DataModel::Nullable<uint64_t> & value);
typedef void (*Int64sAttributeCallback)(void * context, int64_t value);
typedef void (*NullableInt64sAttributeCallback)(void * context, const chip::app::DataModel::Nullable<int64_t> & value);
typedef void (*FloatAttributeCallback)(void * context, float value);
typedef void (*NullableFloatAttributeCallback)(void * context, const chip::app::DataModel::Nullable<float> & value);
typedef void (*DoubleAttributeCallback)(void * context, double value);
typedef void (*NullableDoubleAttributeCallback)(void * context, const chip::app::DataModel::Nullable<double> & value);
typedef void (*OctetStringAttributeCallback)(void * context, const chip::ByteSpan value);
typedef void (*NullableOctetStringAttributeCallback)(void * context, const chip::app::DataModel::Nullable<chip::ByteSpan> & value);
typedef void (*CharStringAttributeCallback)(void * context, const chip::CharSpan value);
Expand Down
5 changes: 4 additions & 1 deletion src/app/zap-templates/common/ClustersHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ function asChipCallback(item)
return { name : 'Int' + basicType.replace(/[^0-9]/g, '') + 'u', type : basicType };
case 'bool':
return { name : 'Boolean', type : 'bool' };
// TODO: Add float and double
case 'float':
return { name : 'Float', type : 'float' };
case 'double':
return { name : 'Double', type : 'double' };
default:
return { name : 'Unsupported', type : null };
}
Expand Down
10 changes: 10 additions & 0 deletions src/controller/java/templates/ChipClusters-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ public class ChipClusters {
void onError(Exception error);
}

public interface FloatAttributeCallback {
void onSuccess(float value);
void onError(Exception error);
}

public interface DoubleAttributeCallback {
void onSuccess(double value);
void onError(Exception error);
}

public static abstract class BaseChipCluster {
protected long chipClusterPtr;

Expand Down
44 changes: 44 additions & 0 deletions src/controller/java/templates/ClusterInfo-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,50 @@ public class ClusterInfoMapping {
}
}

public static class DelegatedFloatAttributeCallback implements ChipClusters.FloatAttributeCallback, DelegatedClusterCallback {
private ClusterCommandCallback callback;

@Override
public void setCallbackDelegate(ClusterCommandCallback callback) {
this.callback = callback;
}

@Override
public void onSuccess(float value) {
Map<CommandResponseInfo, Object> responseValues = new LinkedHashMap<>();
CommandResponseInfo setupPINResponseValue = new CommandResponseInfo("value", "float");
responseValues.put(setupPINResponseValue, value);
callback.onSuccess(responseValues);
}

@Override
public void onError(Exception error) {
callback.onFailure(error);
}
}

public static class DelegatedDoubleAttributeCallback implements ChipClusters.DoubleAttributeCallback, DelegatedClusterCallback {
private ClusterCommandCallback callback;

@Override
public void setCallbackDelegate(ClusterCommandCallback callback) {
this.callback = callback;
}

@Override
public void onSuccess(double value) {
Map<CommandResponseInfo, Object> responseValues = new LinkedHashMap<>();
CommandResponseInfo setupPINResponseValue = new CommandResponseInfo("value", "double");
responseValues.put(setupPINResponseValue, value);
callback.onSuccess(responseValues);
}

@Override
public void onError(Exception error) {
callback.onFailure(error);
}
}

public static class DelegatedDefaultClusterCallback implements DefaultClusterCallback, DelegatedClusterCallback {
private ClusterCommandCallback callback;

Expand Down
Loading

0 comments on commit 05ccb30

Please sign in to comment.