diff --git a/scripts/pregenerate/using_codegen.py b/scripts/pregenerate/using_codegen.py index 1040bb7ac1ae58..5de358a0f483b1 100644 --- a/scripts/pregenerate/using_codegen.py +++ b/scripts/pregenerate/using_codegen.py @@ -80,8 +80,7 @@ def __init__(self, sdk_root): self.sdk_root = sdk_root def Accept(self, idl: InputIdlFile): - # Java is highly specific, a single path is acceptable for dynamic - # bridge codegen + # Java is highly specific, a single path is acceptable for codegen return idl.relative_path == "src/controller/data_model/controller-clusters.matter" def CreateTarget(self, idl: InputIdlFile, runner): diff --git a/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersCpp.jinja b/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersCpp.jinja index d56f2701113734..f37a6b893b7f42 100644 --- a/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersCpp.jinja +++ b/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersCpp.jinja @@ -185,7 +185,7 @@ JNI_METHOD(void, {{cluster.name}}Cluster, subscribe{{attr.definition.name | capi { chip::DeviceLayer::StackLock lock; - {%- set callbackName = attr | callbackName(cluster, typeLookup) -%} + {%- set callbackName = attr | callbackName(typeLookup) -%} std::unique_ptr<{{callbackName}}, void (*)({{callbackName}} *)> onSuccess(Platform::New<{{callbackName}}>(callback, true), chip::Platform::Delete<{{callbackName}}>); VerifyOrReturn(onSuccess.get() != nullptr, chip::AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error creating native success callback", CHIP_ERROR_NO_MEMORY)); diff --git a/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersRead.jinja b/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersRead.jinja index f11b7913c44e0c..03684f27f83206 100644 --- a/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersRead.jinja +++ b/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersRead.jinja @@ -31,7 +31,7 @@ JNI_METHOD(void, {{cluster.name | capitalcase}}Cluster, read{{attr.definition.na { chip::DeviceLayer::StackLock lock; using TypeInfo = chip::app::Clusters::{{cluster.name | capitalcase}}::Attributes::{{attr.definition.name | capitalcase}}::TypeInfo; - {%- set callbackName = attr | callbackName(cluster, typeLookup) %} + {%- set callbackName = attr | callbackName(typeLookup) %} std::unique_ptr<{{callbackName}}, void (*)({{callbackName}} *)> onSuccess(chip::Platform::New<{{callbackName}}>(callback, false), chip::Platform::Delete<{{callbackName}}>); VerifyOrReturn(onSuccess.get() != nullptr, chip::AndroidClusterExceptions::GetInstance().ReturnIllegalStateException(env, callback, "Error creating native success callback", CHIP_ERROR_NO_MEMORY)); diff --git a/scripts/py_matter_idl/matter_idl/generators/java/ClusterReadMapping.jinja b/scripts/py_matter_idl/matter_idl/generators/java/ClusterReadMapping.jinja new file mode 100644 index 00000000000000..4d65cbad1b5413 --- /dev/null +++ b/scripts/py_matter_idl/matter_idl/generators/java/ClusterReadMapping.jinja @@ -0,0 +1,51 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package chip.devicecontroller; + +import chip.clusterinfo.CommandParameterInfo; +import chip.clusterinfo.InteractionInfo; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public class ClusterReadMapping { + public Map> getReadAttributeMap() { + Map> readAttributeMap = new HashMap<>(); + + {%- for cluster in clientClusters | sort(attribute='code') %} + {%- set typeLookup = idl | createLookupContext(cluster) %} + Map read{{cluster.name}}InteractionInfo = new LinkedHashMap<>(); + {%- for attribute in cluster.attributes | sort(attribute='name') | attributesWithCallback(typeLookup) %} + {#- TODO: add support for struct-typed attributes -#} + Map read{{cluster.name}}{{attribute.definition.name | upfirst}}CommandParams = new LinkedHashMap(); + InteractionInfo read{{cluster.name}}{{attribute.definition.name | upfirst}}AttributeInteractionInfo = new InteractionInfo( + (cluster, callback, commandArguments) -> { + ((ChipClusters.{{cluster.name}}Cluster) cluster).read{{attribute.definition.name | upfirst}}Attribute( + ({{ attribute | chipClustersCallbackName(typeLookup) }}) callback + ); + }, + () -> new ClusterInfoMapping.{{ attribute | delegatedCallbackName(typeLookup)}}(), + read{{cluster.name}}{{attribute.definition.name | upfirst}}CommandParams + ); + read{{cluster.name}}InteractionInfo.put("read{{attribute.definition.name | upfirst}}Attribute", read{{cluster.name}}{{attribute.definition.name | upfirst}}AttributeInteractionInfo); + {%- endfor %} + readAttributeMap.put("{{cluster.name | lowfirst_except_acronym}}", read{{cluster.name}}InteractionInfo); + {%- endfor -%} + return readAttributeMap; + } +} + diff --git a/scripts/py_matter_idl/matter_idl/generators/java/__init__.py b/scripts/py_matter_idl/matter_idl/generators/java/__init__.py index 51e2c9e145d7b6..f1bae24f1f856d 100644 --- a/scripts/py_matter_idl/matter_idl/generators/java/__init__.py +++ b/scripts/py_matter_idl/matter_idl/generators/java/__init__.py @@ -66,7 +66,43 @@ def FieldToGlobalName(field: Field, context: TypeLookupContext) -> Union[str, No return None -def CallbackName(attr: Attribute, cluster: Cluster, context: TypeLookupContext) -> str: +def GlobalNameToJavaName(name: str) -> str: + if name in {'Int8u', 'Int8s', 'Int16u', 'Int16s'}: + return 'Integer' + + if name.startswith('Int'): + return 'Long' + + # Double/Float/Booleans/CharString/OctetString + return name + + +def DelegatedCallbackName(attr: Attribute, context: TypeLookupContext) -> str: + """ + Figure out what callback name to use for delegate callback construction. + """ + global_name = FieldToGlobalName(attr.definition, context) + + if global_name: + return 'Delegated{}AttributeCallback'.format(GlobalNameToJavaName(global_name)) + + return 'Delegated{}Cluster{}AttributeCallback'.format(context.cluster.name, capitalcase(attr.definition.name)) + + +def ChipClustersCallbackName(attr: Attribute, context: TypeLookupContext) -> str: + """ + Figure out what callback name to use when building a ChipCluster.*AttributeCallback + in java codegen. + """ + global_name = FieldToGlobalName(attr.definition, context) + + if global_name: + return 'ChipClusters.{}AttributeCallback'.format(GlobalNameToJavaName(global_name)) + + return 'ChipClusters.{}Cluster.{}AttributeCallback'.format(context.cluster.name, capitalcase(attr.definition.name)) + + +def CallbackName(attr: Attribute, context: TypeLookupContext) -> str: """ Figure out what callback name to use when a variable requires a read callback. @@ -82,7 +118,7 @@ def CallbackName(attr: Attribute, cluster: Cluster, context: TypeLookupContext) return 'CHIP{}AttributeCallback'.format(capitalcase(global_name)) return 'CHIP{}{}AttributeCallback'.format( - capitalcase(cluster.name), + capitalcase(context.cluster.name), capitalcase(attr.definition.name) ) @@ -368,6 +404,8 @@ def __init__(self, storage: GeneratorStorage, idl: Idl, **kargs): self.jinja_env.filters['attributesWithCallback'] = attributesWithSupportedCallback self.jinja_env.filters['callbackName'] = CallbackName + self.jinja_env.filters['chipClustersCallbackName'] = ChipClustersCallbackName + self.jinja_env.filters['delegatedCallbackName'] = DelegatedCallbackName self.jinja_env.filters['commandCallbackName'] = CommandCallbackName self.jinja_env.filters['named'] = NamedFilter self.jinja_env.filters['toBoxedJavaType'] = ToBoxedJavaType @@ -424,11 +462,22 @@ def internal_render_all(self): Renders .java files required for java matter support """ + clientClusters = [c for c in self.idl.clusters if c.side == ClusterSide.CLIENT] + + self.internal_render_one_output( + template_path="java/ClusterReadMapping.jinja", + output_file_name="java/chip/devicecontroller/ClusterReadMapping.java", + vars={ + 'idl': self.idl, + 'clientClusters': clientClusters, + } + ) + self.internal_render_one_output( template_path="java/ClusterWriteMapping.jinja", output_file_name="java/chip/devicecontroller/ClusterWriteMapping.java", vars={ 'idl': self.idl, - 'clientClusters': [c for c in self.idl.clusters if c.side == ClusterSide.CLIENT], + 'clientClusters': clientClusters, } ) diff --git a/scripts/py_matter_idl/matter_idl/tests/available_tests.yaml b/scripts/py_matter_idl/matter_idl/tests/available_tests.yaml index c1b253ec23f0f9..eebdf5bba7528d 100644 --- a/scripts/py_matter_idl/matter_idl/tests/available_tests.yaml +++ b/scripts/py_matter_idl/matter_idl/tests/available_tests.yaml @@ -38,6 +38,7 @@ java-jni: java-class: inputs/several_clusters.matter: java/chip/devicecontroller/ClusterWriteMapping.java: outputs/several_clusters/java/ClusterWriteMapping.java + java/chip/devicecontroller/ClusterReadMapping.java: outputs/several_clusters/java/ClusterReadMapping.java bridge: inputs/simple_attribute.matter: diff --git a/scripts/py_matter_idl/matter_idl/tests/outputs/several_clusters/java/ClusterReadMapping.java b/scripts/py_matter_idl/matter_idl/tests/outputs/several_clusters/java/ClusterReadMapping.java new file mode 100644 index 00000000000000..adb4b973b2e2ee --- /dev/null +++ b/scripts/py_matter_idl/matter_idl/tests/outputs/several_clusters/java/ClusterReadMapping.java @@ -0,0 +1,76 @@ +/* + * + * Copyright (c) 2023 Project CHIP Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package chip.devicecontroller; + +import chip.clusterinfo.CommandParameterInfo; +import chip.clusterinfo.InteractionInfo; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +public class ClusterReadMapping { + public Map> getReadAttributeMap() { + Map> readAttributeMap = new HashMap<>(); + Map readFirstInteractionInfo = new LinkedHashMap<>();Map readFirstSomeIntegerCommandParams = new LinkedHashMap(); + InteractionInfo readFirstSomeIntegerAttributeInteractionInfo = new InteractionInfo( + (cluster, callback, commandArguments) -> { + ((ChipClusters.FirstCluster) cluster).readSomeIntegerAttribute( + (ChipClusters.IntegerAttributeCallback) callback + ); + }, + () -> new ClusterInfoMapping.DelegatedIntegerAttributeCallback(), + readFirstSomeIntegerCommandParams + ); + readFirstInteractionInfo.put("readSomeIntegerAttribute", readFirstSomeIntegerAttributeInteractionInfo); + readAttributeMap.put("first", readFirstInteractionInfo); + Map readSecondInteractionInfo = new LinkedHashMap<>();Map readSecondSomeBytesCommandParams = new LinkedHashMap(); + InteractionInfo readSecondSomeBytesAttributeInteractionInfo = new InteractionInfo( + (cluster, callback, commandArguments) -> { + ((ChipClusters.SecondCluster) cluster).readSomeBytesAttribute( + (ChipClusters.OctetStringAttributeCallback) callback + ); + }, + () -> new ClusterInfoMapping.DelegatedOctetStringAttributeCallback(), + readSecondSomeBytesCommandParams + ); + readSecondInteractionInfo.put("readSomeBytesAttribute", readSecondSomeBytesAttributeInteractionInfo); + readAttributeMap.put("second", readSecondInteractionInfo); + Map readThirdInteractionInfo = new LinkedHashMap<>();Map readThirdSomeEnumCommandParams = new LinkedHashMap(); + InteractionInfo readThirdSomeEnumAttributeInteractionInfo = new InteractionInfo( + (cluster, callback, commandArguments) -> { + ((ChipClusters.ThirdCluster) cluster).readSomeEnumAttribute( + (ChipClusters.IntegerAttributeCallback) callback + ); + }, + () -> new ClusterInfoMapping.DelegatedIntegerAttributeCallback(), + readThirdSomeEnumCommandParams + ); + readThirdInteractionInfo.put("readSomeEnumAttribute", readThirdSomeEnumAttributeInteractionInfo);Map readThirdOptionsCommandParams = new LinkedHashMap(); + InteractionInfo readThirdOptionsAttributeInteractionInfo = new InteractionInfo( + (cluster, callback, commandArguments) -> { + ((ChipClusters.ThirdCluster) cluster).readOptionsAttribute( + (ChipClusters.IntegerAttributeCallback) callback + ); + }, + () -> new ClusterInfoMapping.DelegatedIntegerAttributeCallback(), + readThirdOptionsCommandParams + ); + readThirdInteractionInfo.put("readOptionsAttribute", readThirdOptionsAttributeInteractionInfo); + readAttributeMap.put("third", readThirdInteractionInfo);return readAttributeMap; + } +} + diff --git a/src/controller/java/BUILD.gn b/src/controller/java/BUILD.gn index 9dc043fccc91a8..1aeeb8b27b70df 100644 --- a/src/controller/java/BUILD.gn +++ b/src/controller/java/BUILD.gn @@ -212,6 +212,7 @@ android_library("java") { data_deps = [ ":jni" ] sources = [ + "generated/java/chip/devicecontroller/ClusterReadMapping.java", "generated/java/chip/devicecontroller/ClusterWriteMapping.java", "src/chip/clusterinfo/ClusterCommandCallback.java", "src/chip/clusterinfo/ClusterInfo.java", @@ -262,7 +263,6 @@ android_library("java") { "zap-generated/chip/devicecontroller/ChipIdLookup.java", "zap-generated/chip/devicecontroller/ChipStructs.java", "zap-generated/chip/devicecontroller/ClusterInfoMapping.java", - "zap-generated/chip/devicecontroller/ClusterReadMapping.java", ] if (build_java_matter_controller) { diff --git a/src/controller/java/zap-generated/chip/devicecontroller/ClusterReadMapping.java b/src/controller/java/generated/java/chip/devicecontroller/ClusterReadMapping.java similarity index 99% rename from src/controller/java/zap-generated/chip/devicecontroller/ClusterReadMapping.java rename to src/controller/java/generated/java/chip/devicecontroller/ClusterReadMapping.java index 59f9942c5c7c51..509b9168a976d7 100644 --- a/src/controller/java/zap-generated/chip/devicecontroller/ClusterReadMapping.java +++ b/src/controller/java/generated/java/chip/devicecontroller/ClusterReadMapping.java @@ -1,6 +1,6 @@ /* * - * Copyright (c) 2022 Project CHIP Authors + * Copyright (c) 2023 Project CHIP Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,9 +14,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -// THIS FILE IS GENERATED BY ZAP - package chip.devicecontroller; import chip.clusterinfo.CommandParameterInfo; @@ -26,7 +23,6 @@ import java.util.Map; public class ClusterReadMapping { - public Map> getReadAttributeMap() { Map> readAttributeMap = new HashMap<>(); Map readIdentifyInteractionInfo = new LinkedHashMap<>(); diff --git a/src/controller/java/templates/ClusterInfo-read-interaction.zapt b/src/controller/java/templates/ClusterInfo-read-interaction.zapt deleted file mode 100644 index 94710e5ce9d7f3..00000000000000 --- a/src/controller/java/templates/ClusterInfo-read-interaction.zapt +++ /dev/null @@ -1,49 +0,0 @@ -{{> header}} -{{#if (chip_has_client_clusters)}} - -package chip.devicecontroller; - -import chip.clusterinfo.CommandParameterInfo; -import chip.clusterinfo.InteractionInfo; -import java.util.HashMap; -import java.util.LinkedHashMap; -import java.util.Map; - -public class ClusterReadMapping { - - public Map> getReadAttributeMap() { - Map> readAttributeMap = new HashMap<>(); - {{#chip_client_clusters}} - Map read{{asUpperCamelCase name}}InteractionInfo = new LinkedHashMap<>(); - {{#zcl_attributes_server removeKeys='isOptional'}} - {{! TODO: Add support for struct-typed attributes }} - {{#if_unsupported_attribute_callback type isArray ../id}} - {{else}} - Map read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}CommandParams = new LinkedHashMap(); - InteractionInfo read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeInteractionInfo = new InteractionInfo( - (cluster, callback, commandArguments) -> { - ((ChipClusters.{{asUpperCamelCase ../name}}Cluster) cluster).read{{asUpperCamelCase name}}Attribute( - ({{#if_basic_attribute type ../id}} - ChipClusters.{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback - {{else}} - ChipClusters.{{asUpperCamelCase ../name}}Cluster.{{asUpperCamelCase name}}AttributeCallback - {{/if_basic_attribute}}) callback - ); - }, - {{#if_basic_attribute type ../id}} - () -> new ClusterInfoMapping.Delegated{{as_underlying_java_zcl_type type ../id boolean="Boolean"}}AttributeCallback(), - {{else}} - () -> new ClusterInfoMapping.Delegated{{asUpperCamelCase parent.name}}Cluster{{asUpperCamelCase name}}AttributeCallback(), - {{/if_basic_attribute}} - read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}CommandParams - ); - read{{asUpperCamelCase ../name}}InteractionInfo.put("read{{asUpperCamelCase name}}Attribute", read{{asUpperCamelCase ../name}}{{asUpperCamelCase name}}AttributeInteractionInfo); - {{/if_unsupported_attribute_callback}} - {{/zcl_attributes_server}} - readAttributeMap.put("{{asLowerCamelCase name}}", read{{asUpperCamelCase name}}InteractionInfo); - {{/chip_client_clusters}} - return readAttributeMap; - } -} - -{{/if}} \ No newline at end of file diff --git a/src/controller/java/templates/templates.json b/src/controller/java/templates/templates.json index 744573fa9ca6cd..1274530760f33a 100644 --- a/src/controller/java/templates/templates.json +++ b/src/controller/java/templates/templates.json @@ -98,11 +98,6 @@ "path": "ClusterInfo-java.zapt", "name": "Cluster information mapping for Java", "output": "src/controller/java/zap-generated/chip/devicecontroller/ClusterInfoMapping.java" - }, - { - "path": "ClusterInfo-read-interaction.zapt", - "name": "Generate read interaction for cluster information map", - "output": "src/controller/java/zap-generated/chip/devicecontroller/ClusterReadMapping.java" } ] }