Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MigrateClusterReadMapping.java to jinja codegen #25786

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}

69 changes: 65 additions & 4 deletions scripts/py_matter_idl/matter_idl/generators/java/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ def FieldToGlobalName(field: Field, context: TypeLookupContext) -> Union[str, No
return 'CharString'
elif type(actual) == BasicInteger:
# TODO: unclear why this, but tries to match zap:
if actual.idl_name.lower() in ['vendor_id', 'fabric_idx']:
# once java fully moves to jinja, these can probably be removed
if actual.idl_name.lower() in {'vendor_id', 'fabric_idx', 'group_id'}:
return None

if actual.is_signed:
Expand All @@ -70,7 +71,54 @@ 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:
"""
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
Figure out what callback name to use when a variable requires a read callback.

These are split into native types, like Boolean/Float/Double/CharString, where
one callback type can support anything.

For specific types (e.g. A struct) codegen will generate its own callback name
specific to that type.
"""
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:
"""
andy31415 marked this conversation as resolved.
Show resolved Hide resolved
Figure out what callback name to use when a variable requires a read callback.

These are split into native types, like Boolean/Float/Double/CharString, where
one callback type can support anything.

For specific types (e.g. A struct) codegen will generate its own callback name
specific to that type.
"""
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 @@ -86,7 +134,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 @@ -372,6 +420,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 @@ -428,11 +478,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
48 changes: 0 additions & 48 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"
}
]
}