Skip to content

Commit 7f25fb2

Browse files
authored
Implement initial Kotlin generator (#30009)
1 parent 6e86760 commit 7f25fb2

File tree

104 files changed

+34566
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

104 files changed

+34566
-0
lines changed

kotlin-detect-config.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ style:
155155
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/EnterNetworkFragment.kt"
156156
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt"
157157
- "**/src/controller/java/src/matter/onboardingpayload/Base38.kt"
158+
- "**/src/controller/java/generated/java/**/*"
158159
ForbiddenComment:
159160
excludes:
160161
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt"
@@ -246,6 +247,9 @@ naming:
246247
excludes:
247248
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/HistoryCommandAdapter.kt"
248249
- "**/src/controller/java/generated/java/**/*"
250+
FunctionParameterNaming:
251+
excludes:
252+
- "**/src/controller/java/generated/java/**/*"
249253
TopLevelPropertyNaming:
250254
excludes:
251255
- "**/src/controller/java/generated/java/**/*"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
{%- macro encode_value(source, encodable, depth) -%}
2+
{%- if encodable.is_nullable and encodable.is_optional -%}
3+
{{encode_value(source, encodable.without_nullable().without_optional(), depth + 1)}}?
4+
{%- elif encodable.is_nullable -%}
5+
{{encode_value(source, encodable.without_nullable(), depth + 1)}}?
6+
{%- elif encodable.is_optional -%}
7+
{{encode_value(source, encodable.without_optional(), depth + 1)}}?
8+
{%- elif encodable.is_list -%}
9+
ArrayList<{{encode_value(source, encodable.without_list(), depth + 1)}}>
10+
{%- elif encodable.is_struct -%}
11+
{%- set struct = encodable.get_underlying_struct() -%}
12+
ChipStructs.{{source.name}}Cluster{{struct.name}}
13+
{%- else -%}
14+
{{encodable.boxed_java_type}}
15+
{%- endif -%}
16+
{%- endmacro -%}
17+
18+
{%- macro encode_value_without_optional(source, encodable, depth) -%}
19+
{%- if encodable.is_nullable -%}
20+
{{encode_value_without_optional(source, encodable.without_nullable(), depth + 1)}}?
21+
{%- elif encodable.is_list -%}
22+
List<{{encode_value_without_optional(source, encodable.without_list(), depth + 1)}}>
23+
{%- elif encodable.is_struct -%}
24+
{%- set struct = encodable.get_underlying_struct() -%}
25+
ChipStructs.{{source.name}}Cluster{{struct.name}}
26+
{%- else -%}
27+
{{encodable.boxed_java_type}}
28+
{%- endif -%}
29+
{%- endmacro -%}
30+
31+
{%- macro encode_value_without_optional_nullable(source, encodable, depth) -%}
32+
{%- if encodable.is_list -%}
33+
ArrayList<{{encode_value_without_optional_nullable(source, encodable.without_list(), depth + 1)}}>
34+
{%- elif encodable.is_struct -%}
35+
{%- set struct = encodable.get_underlying_struct() -%}
36+
ChipStructs.{{source.name}}Cluster{{struct.name}}
37+
{%- else -%}
38+
{{encodable.boxed_java_type}}
39+
{%- endif -%}
40+
{%- endmacro -%}
41+
42+
/*
43+
*
44+
* Copyright (c) 2023 Project CHIP Authors
45+
*
46+
* Licensed under the Apache License, Version 2.0 (the "License");
47+
* you may not use this file except in compliance with the License.
48+
* You may obtain a copy of the License at
49+
*
50+
* http://www.apache.org/licenses/LICENSE-2.0
51+
*
52+
* Unless required by applicable law or agreed to in writing, software
53+
* distributed under the License is distributed on an "AS IS" BASIS,
54+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
55+
* See the License for the specific language governing permissions and
56+
* limitations under the License.
57+
*/
58+
59+
package matter.devicecontroller.cluster.clusters
60+
61+
import java.util.ArrayList
62+
{% set typeLookup = idl | createLookupContext(cluster) %}
63+
class {{cluster.name}}Cluster(private val endpointId: UShort) {
64+
companion object {
65+
const val CLUSTER_ID: UInt = {{cluster.code}}u
66+
}
67+
{% for command in cluster.commands | sort(attribute='code') -%}
68+
{%- set callbackName = command | javaCommandCallbackName() -%}
69+
{%- if not command.is_timed_invoke %}
70+
fun {{command.name | lowfirst_except_acronym}}(callback: {{callbackName}}Callback
71+
{%- if command.input_param -%}
72+
{%- for field in (cluster.structs | named(command.input_param)).fields -%}
73+
, {{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}
74+
{%- endfor -%}
75+
{%- endif -%}
76+
) {
77+
// Implementation needs to be added here
78+
}
79+
{%- endif %}
80+
81+
fun {{command.name | lowfirst_except_acronym}}(callback: {{callbackName}}Callback
82+
{%- if command.input_param -%}
83+
{%- for field in (cluster.structs | named(command.input_param)).fields -%}
84+
, {{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}
85+
{%- endfor -%}
86+
{%- endif -%}
87+
, timedInvokeTimeoutMs: Int) {
88+
// Implementation needs to be added here
89+
}
90+
{% endfor %}
91+
{%- set already_handled_command = [] -%}
92+
{%- for command in cluster.commands | sort(attribute='code') -%}
93+
{%- if command | isCommandNotDefaultCallback() -%}
94+
{%- set callbackName = command | javaCommandCallbackName() -%}
95+
{%- if callbackName not in already_handled_command %}
96+
interface {{callbackName}}Callback {
97+
fun onSuccess(
98+
{%- for field in (cluster.structs | named(command.output_param)).fields -%}
99+
{{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}{% if not loop.last %}, {% endif %}
100+
{%- endfor -%}
101+
)
102+
fun onError(error: Exception)
103+
}
104+
{% if already_handled_command.append(callbackName) -%}
105+
{%- endif -%}
106+
{%- endif -%}
107+
{%- endif -%}
108+
{%- endfor %}
109+
{%- set already_handled_attribute = [] -%}
110+
{% for attribute in cluster.attributes | rejectattr('definition', 'is_field_global_name', typeLookup) %}
111+
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%}
112+
{%- set interfaceName = attribute | javaAttributeCallbackName(typeLookup) -%}
113+
{%- if interfaceName not in already_handled_attribute %}
114+
interface {{interfaceName}} {
115+
fun onSuccess(value: {{encode_value(cluster, encodable, 0)}})
116+
fun onError(ex: Exception)
117+
fun onSubscriptionEstablished(subscriptionId: Long)
118+
}
119+
{% if already_handled_attribute.append(interfaceName) -%}
120+
{#- This block does nothing, it only exists to append to already_handled_attribute. -#}
121+
{%- endif -%}
122+
{%- endif -%}
123+
{% endfor -%}
124+
{% for attribute in cluster.attributes | sort(attribute='code') %}
125+
fun read{{ attribute.definition.name | upfirst }}Attribute(
126+
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }}
127+
) {
128+
// Implementation needs to be added here
129+
}
130+
{% if attribute | isFabricScopedList(typeLookup) %}
131+
fun read{{ attribute.definition.name | upfirst }}AttributeWithFabricFilter(
132+
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }},
133+
isFabricFiltered: Boolean
134+
) {
135+
// Implementation needs to be added here
136+
}
137+
138+
{% endif -%}
139+
{%- if attribute.is_writable %}
140+
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%}
141+
{%- set encodable2 = attribute.definition | asEncodable(typeLookup) -%}
142+
{%- if not attribute.requires_timed_write %}
143+
fun write{{ attribute.definition.name | upfirst }}Attribute(
144+
callback: DefaultClusterCallback,
145+
value: {{ encode_value_without_optional_nullable(cluster, encodable, 0) }}
146+
) {
147+
// Implementation needs to be added here
148+
}
149+
{% endif %}
150+
fun write{{ attribute.definition.name | upfirst }}Attribute(
151+
callback: DefaultClusterCallback,
152+
value: {{ encode_value_without_optional_nullable(cluster, encodable2, 0) }},
153+
timedWriteTimeoutMs: Int
154+
) {
155+
// Implementation needs to be added here
156+
}
157+
{% endif %}
158+
{%- if attribute.is_subscribable %}
159+
fun subscribe{{ attribute.definition.name | upfirst }}Attribute(
160+
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }},
161+
minInterval: Int,
162+
maxInterval: Int
163+
) {
164+
// Implementation needs to be added here
165+
}
166+
{% endif -%}
167+
{%- endfor -%}
168+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import("//build_overrides/build.gni")
2+
import("//build_overrides/chip.gni")
3+
4+
matter_clusters_sources = [
5+
{%- for cluster in clientClusters | sort(attribute='name') %}
6+
{%- set typeLookup = idl | createLookupContext(cluster) %}
7+
"${chip_root}/src/controller/java/generated/java/matter/devicecontroller/cluster/clusters/{{cluster.name}}Cluster.kt",
8+
{%- endfor %}
9+
]

0 commit comments

Comments
 (0)