-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Change to generate Java files using jinja (#29879)
* Change java generated files using jinja * Restyled by autopep8 * modify test app list --------- Co-authored-by: Restyled.io <[email protected]>
- Loading branch information
Showing
23 changed files
with
59,404 additions
and
62,910 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
308 changes: 308 additions & 0 deletions
308
scripts/py_matter_idl/matter_idl/generators/java/ChipClusters_java.jinja
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,308 @@ | ||
{%- macro encode_value(source, encodable, depth) -%} | ||
{%- if encodable.is_nullable -%} | ||
@Nullable {{encode_value(source, encodable.without_nullable(), depth + 1)}} | ||
{%- elif encodable.is_optional -%} | ||
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 -%} | ||
@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 chip.devicecontroller; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public class ChipClusters { | ||
|
||
public interface DefaultClusterCallback { | ||
void onSuccess(); | ||
void onError(Exception error); | ||
} | ||
|
||
public interface CharStringAttributeCallback { | ||
/** Indicates a successful read for a CHAR_STRING attribute. */ | ||
void onSuccess(String value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public interface OctetStringAttributeCallback { | ||
/** Indicates a successful read for an OCTET_STRING attribute. */ | ||
void onSuccess(byte[] value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public interface IntegerAttributeCallback { | ||
void onSuccess(int value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public interface LongAttributeCallback { | ||
void onSuccess(long value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public interface BooleanAttributeCallback { | ||
void onSuccess(boolean value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public interface FloatAttributeCallback { | ||
void onSuccess(float value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public interface DoubleAttributeCallback { | ||
void onSuccess(double value); | ||
void onError(Exception error); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
|
||
public static abstract class BaseChipCluster { | ||
protected long chipClusterPtr; | ||
|
||
public BaseChipCluster(long devicePtr, int endpointId) { | ||
chipClusterPtr = initWithDevice(devicePtr, endpointId); | ||
} | ||
|
||
/** | ||
* Sets the timeout, in milliseconds, after which commands sent through this cluster will fail | ||
* with a timeout (regardless of whether or not a response has been received). If set to an | ||
* empty optional, the default timeout will be used. | ||
*/ | ||
public void setCommandTimeout(Optional<Long> timeoutMillis) { | ||
setCommandTimeout(chipClusterPtr, timeoutMillis); | ||
} | ||
|
||
private native void setCommandTimeout(long clusterPtr, Optional<Long> timeoutMillis); | ||
|
||
/** Returns the current timeout (in milliseconds) for commands sent through this cluster. */ | ||
public Optional<Long> getCommandTimeout() { | ||
Optional<Long> timeout = getCommandTimeout(chipClusterPtr); | ||
return timeout == null ? Optional.empty() : timeout; | ||
} | ||
|
||
private native Optional<Long> getCommandTimeout(long clusterPtr); | ||
|
||
public abstract long initWithDevice(long devicePtr, int endpointId); | ||
|
||
public native void deleteCluster(long chipClusterPtr); | ||
|
||
@SuppressWarnings("deprecation") | ||
protected void finalize() throws Throwable { | ||
super.finalize(); | ||
|
||
if (chipClusterPtr != 0) { | ||
deleteCluster(chipClusterPtr); | ||
chipClusterPtr = 0; | ||
} | ||
} | ||
} | ||
{% for cluster in clientClusters | sort(attribute='code') %} | ||
{%- set typeLookup = idl | createLookupContext(cluster) %} | ||
public static class {{cluster.name}}Cluster extends BaseChipCluster { | ||
public static final long CLUSTER_ID = {{cluster.code}}L; | ||
|
||
public {{cluster.name}}Cluster(long devicePtr, int endpointId) { | ||
super(devicePtr, endpointId); | ||
} | ||
|
||
@Override | ||
public native long initWithDevice(long devicePtr, int endpointId); | ||
|
||
{% for command in cluster.commands | sort(attribute='code') -%} | ||
{%- set callbackName = command | javaCommandCallbackName() -%} | ||
{%- if not command.is_timed_invoke %} | ||
public void {{command.name | lowfirst_except_acronym}}({{callbackName}}Callback callback | ||
{%- if command.input_param -%} | ||
{%- for field in (cluster.structs | named(command.input_param)).fields -%} | ||
{%- set encodable = field | asEncodable(typeLookup) -%} | ||
, {{encode_value(cluster, encodable, 0)}} {{field.name | lowfirst_except_acronym}} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
) { | ||
{{command.name | lowfirst_except_acronym}}(chipClusterPtr, callback | ||
{%- if command.input_param -%} | ||
{%- for field in (cluster.structs | named(command.input_param)).fields -%} | ||
, {{field.name | lowfirst_except_acronym}} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
, null); | ||
} | ||
{%- endif %} | ||
|
||
public void {{command.name | lowfirst_except_acronym}}({{callbackName}}Callback callback | ||
{%- if command.input_param -%} | ||
{%- for field in (cluster.structs | named(command.input_param)).fields -%} | ||
{%- set encodable = field | asEncodable(typeLookup) -%} | ||
, {{encode_value(cluster, encodable, 0)}} {{field.name | lowfirst_except_acronym}} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
, int timedInvokeTimeoutMs) { | ||
{{command.name | lowfirst_except_acronym}}(chipClusterPtr, callback | ||
{%- if command.input_param -%} | ||
{%- for field in (cluster.structs | named(command.input_param)).fields -%} | ||
, {{field.name | lowfirst_except_acronym}} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
, timedInvokeTimeoutMs); | ||
} | ||
{% endfor %} | ||
{%- for command in cluster.commands | sort(attribute='code') -%} | ||
{%- set callbackName = command | javaCommandCallbackName() %} | ||
private native void {{command.name | lowfirst_except_acronym}}(long chipClusterPtr, {{callbackName}}Callback callback | ||
{%- if command.input_param -%} | ||
{%- for field in (cluster.structs | named(command.input_param)).fields -%} | ||
{%- set encodable = field | asEncodable(typeLookup) -%} | ||
, {{encode_value(cluster, encodable, 0)}} {{field.name | lowfirst_except_acronym}} | ||
{%- endfor -%} | ||
{%- endif -%} | ||
, @Nullable Integer timedInvokeTimeoutMs); | ||
{% 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 %} | ||
public interface {{callbackName}}Callback { | ||
void onSuccess( | ||
{%- for field in (cluster.structs | named(command.output_param)).fields -%} | ||
{%- set encodable = field | asEncodable(typeLookup) -%} | ||
{{encode_value(cluster, encodable, 0)}} {{field.name | lowfirst_except_acronym}} | ||
{%- if loop.index0 < loop.length - 1 -%}{{", "}}{%- endif -%} | ||
{%- endfor -%} | ||
); | ||
|
||
void onError(Exception error); | ||
} | ||
{%- if already_handled_command.append(callbackName) -%} | ||
{#- This block does nothing, it only exists to append to already_handled_command. -#} | ||
{%- 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 %} | ||
public interface {{interfaceName}} { | ||
void onSuccess({{encode_value_without_optional(cluster, encodable, 0)}} value); | ||
void onError(Exception ex); | ||
default void onSubscriptionEstablished(long subscriptionId) {} | ||
} | ||
{%- 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') %} | ||
public void read{{attribute.definition.name | upfirst}}Attribute( | ||
{{attribute | javaAttributeCallbackName(typeLookup) }} callback) { | ||
read{{attribute.definition.name | upfirst}}Attribute(chipClusterPtr, callback | ||
{%- if attribute | isFabricScopedList(typeLookup) -%} | ||
, true | ||
{%- endif -%} | ||
); | ||
} | ||
{%- if attribute | isFabricScopedList(typeLookup) %} | ||
public void read{{attribute.definition.name | upfirst}}AttributeWithFabricFilter( | ||
{{attribute | javaAttributeCallbackName(typeLookup) }} callback, boolean isFabricFiltered) { | ||
read{{attribute.definition.name | upfirst}}Attribute(chipClusterPtr, callback, isFabricFiltered); | ||
} | ||
|
||
{% endif -%} | ||
{%- if attribute.is_writable %} | ||
{%- set encodable = attribute.definition | asEncodable(typeLookup) -%} | ||
{%- set encodable2 = attribute.definition | asEncodable(typeLookup) -%} | ||
{%- if not attribute.requires_timed_write %} | ||
public void write{{attribute.definition.name | upfirst}}Attribute(DefaultClusterCallback callback, {{encode_value_without_optional_nullable(cluster, encodable, 0)}} value) { | ||
write{{attribute.definition.name | upfirst}}Attribute(chipClusterPtr, callback, value, null); | ||
} | ||
|
||
{% endif %} | ||
public void write{{attribute.definition.name | upfirst}}Attribute(DefaultClusterCallback callback, {{encode_value_without_optional_nullable(cluster, encodable2, 0)}} value, int timedWriteTimeoutMs) { | ||
write{{attribute.definition.name | upfirst}}Attribute(chipClusterPtr, callback, value, timedWriteTimeoutMs); | ||
} | ||
|
||
{% endif %} | ||
{% if attribute.is_subscribable %} | ||
public void subscribe{{attribute.definition.name | upfirst}}Attribute( | ||
{{attribute | javaAttributeCallbackName(typeLookup) }} callback, int minInterval, int maxInterval) { | ||
subscribe{{attribute.definition.name | upfirst}}Attribute(chipClusterPtr, callback, minInterval, maxInterval); | ||
} | ||
{% endif -%} | ||
{%- endfor %} | ||
{% for attribute in cluster.attributes %} | ||
private native void read{{attribute.definition.name | upfirst}}Attribute(long chipClusterPtr | ||
, {{attribute | javaAttributeCallbackName(typeLookup) }} callback | ||
{%- if attribute | isFabricScopedList(typeLookup) -%} | ||
, boolean isFabricFiltered | ||
{%- endif -%} | ||
); | ||
{% if attribute.is_writable -%} | ||
{%- set encodable = attribute.definition | asEncodable(typeLookup) %} | ||
private native void write{{attribute.definition.name | upfirst}}Attribute(long chipClusterPtr, DefaultClusterCallback callback, {{encode_value_without_optional_nullable(cluster, encodable, 0)}} value, @Nullable Integer timedWriteTimeoutMs); | ||
{% endif -%} | ||
{% if attribute.is_subscribable %} | ||
private native void subscribe{{attribute.definition.name | upfirst}}Attribute(long chipClusterPtr | ||
, {{attribute | javaAttributeCallbackName(typeLookup) }} callback, int minInterval, int maxInterval); | ||
{%- endif -%} | ||
{% endfor %} | ||
} | ||
{%- endfor %} | ||
} |
Oops, something went wrong.