Skip to content

Commit

Permalink
MigrateClusterReadMapping.java to jinja codegen (#25786)
Browse files Browse the repository at this point in the history
* Fix comment

* Some initial move (no codege) so that I can see codegen deltas

* Start some codegen (but obviously incomplete

* Codegen actually works and is fully backwards compatible

* Remove old zap file

* Fix unit tests

* Updated doccomments ... unfortunately we have a few very similar names in our codegen

* Remove trailing whitespace
  • Loading branch information
andy31415 authored Mar 23, 2023
1 parent 51e7de2 commit a4a407b
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 67 deletions.
3 changes: 1 addition & 2 deletions scripts/pregenerate/using_codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, InteractionInfo>> getReadAttributeMap() {
Map<String, Map<String, InteractionInfo>> readAttributeMap = new HashMap<>();

{%- for cluster in clientClusters | sort(attribute='code') %}
{%- set typeLookup = idl | createLookupContext(cluster) %}
Map<String, InteractionInfo> read{{cluster.name}}InteractionInfo = new LinkedHashMap<>();
{%- for attribute in cluster.attributes | sort(attribute='name') | attributesWithCallback(typeLookup) %}
{#- TODO: add support for struct-typed attributes -#}
Map<String, CommandParameterInfo> read{{cluster.name}}{{attribute.definition.name | upfirst}}CommandParams = new LinkedHashMap<String, CommandParameterInfo>();
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;
}
}

55 changes: 52 additions & 3 deletions scripts/py_matter_idl/matter_idl/generators/java/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
}
)
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Map<String, InteractionInfo>> getReadAttributeMap() {
Map<String, Map<String, InteractionInfo>> readAttributeMap = new HashMap<>();
Map<String, InteractionInfo> readFirstInteractionInfo = new LinkedHashMap<>();Map<String, CommandParameterInfo> readFirstSomeIntegerCommandParams = new LinkedHashMap<String, CommandParameterInfo>();
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<String, InteractionInfo> readSecondInteractionInfo = new LinkedHashMap<>();Map<String, CommandParameterInfo> readSecondSomeBytesCommandParams = new LinkedHashMap<String, CommandParameterInfo>();
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<String, InteractionInfo> readThirdInteractionInfo = new LinkedHashMap<>();Map<String, CommandParameterInfo> readThirdSomeEnumCommandParams = new LinkedHashMap<String, CommandParameterInfo>();
InteractionInfo readThirdSomeEnumAttributeInteractionInfo = new InteractionInfo(
(cluster, callback, commandArguments) -> {
((ChipClusters.ThirdCluster) cluster).readSomeEnumAttribute(
(ChipClusters.IntegerAttributeCallback) callback
);
},
() -> new ClusterInfoMapping.DelegatedIntegerAttributeCallback(),
readThirdSomeEnumCommandParams
);
readThirdInteractionInfo.put("readSomeEnumAttribute", readThirdSomeEnumAttributeInteractionInfo);Map<String, CommandParameterInfo> readThirdOptionsCommandParams = new LinkedHashMap<String, CommandParameterInfo>();
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;
}
}

2 changes: 1 addition & 1 deletion src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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;
Expand All @@ -26,7 +23,6 @@
import java.util.Map;

public class ClusterReadMapping {

public Map<String, Map<String, InteractionInfo>> getReadAttributeMap() {
Map<String, Map<String, InteractionInfo>> readAttributeMap = new HashMap<>();
Map<String, InteractionInfo> readIdentifyInteractionInfo = new LinkedHashMap<>();
Expand Down
49 changes: 0 additions & 49 deletions src/controller/java/templates/ClusterInfo-read-interaction.zapt

This file was deleted.

5 changes: 0 additions & 5 deletions src/controller/java/templates/templates.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
}

0 comments on commit a4a407b

Please sign in to comment.