Skip to content

Commit

Permalink
Implement initial Kotlin generator (project-chip#30009)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored Oct 26, 2023
1 parent 6e86760 commit 7f25fb2
Show file tree
Hide file tree
Showing 104 changed files with 34,566 additions and 0 deletions.
4 changes: 4 additions & 0 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ style:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/EnterNetworkFragment.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt"
- "**/src/controller/java/src/matter/onboardingpayload/Base38.kt"
- "**/src/controller/java/generated/java/**/*"
ForbiddenComment:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt"
Expand Down Expand Up @@ -246,6 +247,9 @@ naming:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/HistoryCommandAdapter.kt"
- "**/src/controller/java/generated/java/**/*"
FunctionParameterNaming:
excludes:
- "**/src/controller/java/generated/java/**/*"
TopLevelPropertyNaming:
excludes:
- "**/src/controller/java/generated/java/**/*"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
{%- macro encode_value(source, encodable, depth) -%}
{%- if encodable.is_nullable and encodable.is_optional -%}
{{encode_value(source, encodable.without_nullable().without_optional(), depth + 1)}}?
{%- elif encodable.is_nullable -%}
{{encode_value(source, encodable.without_nullable(), depth + 1)}}?
{%- elif encodable.is_optional -%}
{{encode_value(source, encodable.without_optional(), depth + 1)}}?
{%- elif encodable.is_list -%}
ArrayList<{{encode_value(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
ChipStructs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.boxed_java_type}}
{%- endif -%}
{%- endmacro -%}

{%- macro encode_value_without_optional(source, encodable, depth) -%}
{%- if encodable.is_nullable -%}
{{encode_value_without_optional(source, encodable.without_nullable(), depth + 1)}}?
{%- elif encodable.is_list -%}
List<{{encode_value_without_optional(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
ChipStructs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.boxed_java_type}}
{%- endif -%}
{%- endmacro -%}

{%- macro encode_value_without_optional_nullable(source, encodable, depth) -%}
{%- if encodable.is_list -%}
ArrayList<{{encode_value_without_optional_nullable(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
ChipStructs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.boxed_java_type}}
{%- endif -%}
{%- endmacro -%}

/*
*
* 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 matter.devicecontroller.cluster.clusters

import java.util.ArrayList
{% set typeLookup = idl | createLookupContext(cluster) %}
class {{cluster.name}}Cluster(private val endpointId: UShort) {
companion object {
const val CLUSTER_ID: UInt = {{cluster.code}}u
}
{% for command in cluster.commands | sort(attribute='code') -%}
{%- set callbackName = command | javaCommandCallbackName() -%}
{%- if not command.is_timed_invoke %}
fun {{command.name | lowfirst_except_acronym}}(callback: {{callbackName}}Callback
{%- if command.input_param -%}
{%- for field in (cluster.structs | named(command.input_param)).fields -%}
, {{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}
{%- endfor -%}
{%- endif -%}
) {
// Implementation needs to be added here
}
{%- endif %}

fun {{command.name | lowfirst_except_acronym}}(callback: {{callbackName}}Callback
{%- if command.input_param -%}
{%- for field in (cluster.structs | named(command.input_param)).fields -%}
, {{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}
{%- endfor -%}
{%- endif -%}
, timedInvokeTimeoutMs: Int) {
// Implementation needs to be added here
}
{% endfor %}
{%- set already_handled_command = [] -%}
{%- for command in cluster.commands | sort(attribute='code') -%}
{%- if command | isCommandNotDefaultCallback() -%}
{%- set callbackName = command | javaCommandCallbackName() -%}
{%- if callbackName not in already_handled_command %}
interface {{callbackName}}Callback {
fun onSuccess(
{%- for field in (cluster.structs | named(command.output_param)).fields -%}
{{field.name | lowfirst_except_acronym}}: {{encode_value(cluster, field | asEncodable(typeLookup), 0)}}{% if not loop.last %}, {% endif %}
{%- endfor -%}
)
fun onError(error: Exception)
}
{% if already_handled_command.append(callbackName) -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endfor %}
{%- set already_handled_attribute = [] -%}
{% for attribute in cluster.attributes | rejectattr('definition', 'is_field_global_name', typeLookup) %}
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%}
{%- set interfaceName = attribute | javaAttributeCallbackName(typeLookup) -%}
{%- if interfaceName not in already_handled_attribute %}
interface {{interfaceName}} {
fun onSuccess(value: {{encode_value(cluster, encodable, 0)}})
fun onError(ex: Exception)
fun onSubscriptionEstablished(subscriptionId: Long)
}
{% if already_handled_attribute.append(interfaceName) -%}
{#- This block does nothing, it only exists to append to already_handled_attribute. -#}
{%- endif -%}
{%- endif -%}
{% endfor -%}
{% for attribute in cluster.attributes | sort(attribute='code') %}
fun read{{ attribute.definition.name | upfirst }}Attribute(
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }}
) {
// Implementation needs to be added here
}
{% if attribute | isFabricScopedList(typeLookup) %}
fun read{{ attribute.definition.name | upfirst }}AttributeWithFabricFilter(
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }},
isFabricFiltered: Boolean
) {
// Implementation needs to be added here
}

{% endif -%}
{%- if attribute.is_writable %}
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%}
{%- set encodable2 = attribute.definition | asEncodable(typeLookup) -%}
{%- if not attribute.requires_timed_write %}
fun write{{ attribute.definition.name | upfirst }}Attribute(
callback: DefaultClusterCallback,
value: {{ encode_value_without_optional_nullable(cluster, encodable, 0) }}
) {
// Implementation needs to be added here
}
{% endif %}
fun write{{ attribute.definition.name | upfirst }}Attribute(
callback: DefaultClusterCallback,
value: {{ encode_value_without_optional_nullable(cluster, encodable2, 0) }},
timedWriteTimeoutMs: Int
) {
// Implementation needs to be added here
}
{% endif %}
{%- if attribute.is_subscribable %}
fun subscribe{{ attribute.definition.name | upfirst }}Attribute(
callback: {{ attribute | javaAttributeCallbackName(typeLookup) }},
minInterval: Int,
maxInterval: Int
) {
// Implementation needs to be added here
}
{% endif -%}
{%- endfor -%}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

matter_clusters_sources = [
{%- for cluster in clientClusters | sort(attribute='name') %}
{%- set typeLookup = idl | createLookupContext(cluster) %}
"${chip_root}/src/controller/java/generated/java/matter/devicecontroller/cluster/clusters/{{cluster.name}}Cluster.kt",
{%- endfor %}
]
Loading

0 comments on commit 7f25fb2

Please sign in to comment.