Skip to content

Commit

Permalink
[nrf noup] [android] Fix crash on DoorLock.lockDoor command
Browse files Browse the repository at this point in the history
Cluster Interaction Tool screen would crash when trying
to send a command that takes an optional argument. The
reason was that optional arguments were incorrectly
converted in the application and the underlying JNI layer.

Signed-off-by: Damian Krolik <[email protected]>
  • Loading branch information
Damian-Nordic committed Jun 6, 2022
1 parent 29e7dbd commit ab8581d
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 504 deletions.
20 changes: 11 additions & 9 deletions scripts/idl/generators/java/ChipClustersCpp.jinja
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
{%- macro encode_optional(target, source, depth, encodable) -%}
if ({{source}} != nullptr) {
jobject optionalValue_{{depth}};
{
jobject optionalValue_{{depth}} = nullptr;
chip::JniReferences::GetInstance().GetOptionalValue({{source}}, optionalValue_{{depth}});
auto & definedValue_{{depth}} = {{target}}.Emplace();
{{ encode_value(
"definedValue_{}".format(depth),
"optionalValue_{}".format(depth),
depth+1,
encodable.without_optional()
)}}
if (optionalValue_{{depth}}) {
auto & definedValue_{{depth}} = {{target}}.Emplace();
{{ encode_value(
"definedValue_{}".format(depth),
"optionalValue_{}".format(depth),
depth+1,
encodable.without_optional()
)}}
}
}
{%- endmacro %}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import java.util.*
import kotlin.collections.HashMap

/**
* ClusterDetailFragment allows user to pick cluster, command, specify parameters and see
Expand Down Expand Up @@ -135,15 +137,15 @@ class ClusterDetailFragment : Fragment() {
)
parameterList.forEach {
val parameterName = it.clusterParameterNameTv.text.toString()
val castType =
selectedInteractionInfo.commandParameters[parameterName]!!.type
val data = castStringToType(it.clusterParameterData.text.toString(), castType)!!
val parameterType = selectedInteractionInfo.commandParameters[parameterName]!!.type
val parameterUnderlyingType = selectedInteractionInfo.commandParameters[parameterName]!!.underlyingType
val data = castStringToType(it.clusterParameterData.text.toString(), parameterType, parameterUnderlyingType)!!
commandArguments[it.clusterParameterNameTv.text.toString()] = data
clusterInteractionHistoryList[0].parameterList.add(
HistoryParameterInfo(
parameterName,
data.toString(),
castType
parameterUnderlyingType
)
)
}
Expand Down Expand Up @@ -185,12 +187,13 @@ class ClusterDetailFragment : Fragment() {
}
}

private fun castStringToType(data: String, type: Class<*>): Any? {
private fun castStringToType(data: String, type: Class<*>, underlyingType: Class<*>): Any? {
return when (type) {
Int::class.java -> data.toInt()
Boolean::class.java -> data.toBoolean()
ByteArray::class.java -> data.encodeToByteArray()
Long::class.java -> data.toLong()
Optional::class.java -> if (data.isEmpty()) Optional.empty() else Optional.of(castStringToType(data, underlyingType, underlyingType)!!)
else -> data
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@
public class CommandParameterInfo {
public CommandParameterInfo() {}

public CommandParameterInfo(String name, Class<?> type) {
/*
* If 'type' is a complex type, such as ArrayList or Optional, 'underlyingType'
* stores information about the wrapped or element type.
*/
public CommandParameterInfo(String name, Class<?> type, Class<?> underlyingType) {
this.name = name;
this.type = type;
this.underlyingType = underlyingType;
}

public String name;
public Class<?> type;
public Class<?> underlyingType;
}
2 changes: 1 addition & 1 deletion src/controller/java/templates/ClusterInfo-java.zapt
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public class ClusterInfoMapping {
{{#chip_cluster_command_arguments}}
{{#if_is_struct type}}
{{else}}
CommandParameterInfo {{asLowerCamelCase ../../name}}{{asLowerCamelCase ../name}}{{asLowerCamelCase label}}CommandParameterInfo = new CommandParameterInfo("{{asLowerCamelCase label}}", {{asJavaType type null parent.parent.name removeGenericType=true}}.class);
CommandParameterInfo {{asLowerCamelCase ../../name}}{{asLowerCamelCase ../name}}{{asLowerCamelCase label}}CommandParameterInfo = new CommandParameterInfo("{{asLowerCamelCase label}}", {{asJavaType type null parent.parent.name removeGenericType=true}}.class, {{asJavaType type null parent.parent.name underlyingType=true}}.class);
{{asLowerCamelCase ../../name}}{{asLowerCamelCase ../name}}CommandParams.put("{{asLowerCamelCase label}}",{{asLowerCamelCase ../../name}}{{asLowerCamelCase ../name}}{{asLowerCamelCase label}}CommandParameterInfo);
{{#not_last}} {{/not_last}}
{{/if_is_struct}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class ClusterWriteMapping {
{{#if isWritableAttribute}}
{{#unless isArray}}
Map<String, CommandParameterInfo> write{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}CommandParams = new LinkedHashMap<String, CommandParameterInfo>();
CommandParameterInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParameterInfo = new CommandParameterInfo("value", {{asJavaType type null parent.parent.name removeGenericType=true}}.class);
CommandParameterInfo {{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParameterInfo = new CommandParameterInfo("value", {{asJavaType type null parent.parent.name removeGenericType=true}}.class, {{asJavaType type null parent.parent.name underlyingType=true}}.class);
write{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}CommandParams.put("value",{{asLowerCamelCase ../name}}{{asLowerCamelCase name}}CommandParameterInfo);
InteractionInfo write{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeInteractionInfo = new InteractionInfo(
(cluster, callback, commandArguments) -> {
Expand Down
30 changes: 16 additions & 14 deletions src/controller/java/templates/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,24 +217,26 @@ async function asJavaType(type, zclType, cluster, options)
classType += asJavaBoxedType(type, zclType);
}

if (!options.hash.forceNotList && (this.isArray || this.entryType)) {
if (!options.hash.removeGenericType) {
classType = 'ArrayList<' + classType + '>';
} else {
classType = 'ArrayList';
if (!options.hash.underlyingType) {
if (!options.hash.forceNotList && (this.isArray || this.entryType)) {
if (!options.hash.removeGenericType) {
classType = 'ArrayList<' + classType + '>';
} else {
classType = 'ArrayList';
}
}
}

if (this.isOptional) {
if (!options.hash.removeGenericType) {
classType = 'Optional<' + classType + '>';
} else {
classType = 'Optional';
if (this.isOptional) {
if (!options.hash.removeGenericType) {
classType = 'Optional<' + classType + '>';
} else {
classType = 'Optional';
}
}
}

if (this.isNullable && options.hash.includeAnnotations) {
classType = '@Nullable ' + classType;
if (this.isNullable && options.hash.includeAnnotations) {
classType = '@Nullable ' + classType;
}
}

return classType;
Expand Down
Loading

0 comments on commit ab8581d

Please sign in to comment.