Skip to content

Commit

Permalink
[Android] Decouple generated API - 2 (#28010)
Browse files Browse the repository at this point in the history
* decouple ChipStructs, ChipIDMapping

* modify build.gn

* restore kotlin file

* Restyled by autopep8

* add matter idl file

* Seperate each class

* Restyled by autopep8

* add exclude kotlin check (generated file)

* remove previous generated file

* add kotlin check config

* relocation

* remodified

* Remove MagicNumber check

* remove kotlin check exception

* Update generated kt file

* update TlvReaderExt

* modify unused import

* Update generated file

* Update unit test

* fix kotlin static check error

* Restyled by whitespace

* update build.gn

* Kotlin format

* Run kotlin format during zap regen

* Restyled by autopep8

* Fix traceback print

* Install a newer openjdk for zap generation

* Try to make linter happy

* Try to make linter happy

* Fix unit tests

---------

Co-authored-by: Restyled.io <[email protected]>
Co-authored-by: Andrei Litvin <[email protected]>
Co-authored-by: Andrei Litvin <[email protected]>
  • Loading branch information
4 people authored and pull[bot] committed Feb 16, 2024
1 parent 00956be commit b22d59d
Show file tree
Hide file tree
Showing 160 changed files with 12,460 additions and 3 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/zap_templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ jobs:
uses: ./.github/actions/checkout-submodules-and-bootstrap
with:
platform: linux

# Java formatting requires a newer java to run ktfmt
# TODO: this should be eventually inside Docker files
- name: Update java
run: |
apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -fy --fix-missing openjdk-17-jre
- name: Generate all
run: ./scripts/run_in_build_env.sh scripts/tools/zap_regen_all.py
- name: Ensure git works in current working directory
Expand Down
29 changes: 29 additions & 0 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ style:
- "**/src/controller/java/src/chip/tlv/values.kt"
- "**/src/controller/java/src/chip/WildcardImport
examples/android/CHIPTest/app/src/androidTest/java/com/tcl/chip/chiptest/ExampleInstrumentedTest.kt"
- "**/src/controller/java/tests/chip/devicecontroller/cluster/ChipClusterEventStructTest.kt"
- "**/src/controller/java/tests/chip/devicecontroller/cluster/ChipClusterStructTest.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
Expand All @@ -58,6 +60,7 @@ style:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/Main.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/jsontlv/JsonToTlv.kt"
- "**/src/controller/java/src/chip/jsontlv/TlvToJson.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
Expand Down Expand Up @@ -123,6 +126,7 @@ style:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/jsontlv/TlvToJson.kt"
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadGenerator.kt"
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadParser.kt"
Expand All @@ -133,12 +137,14 @@ style:
UnusedParameter:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/bluetooth/BluetoothManager.kt"
- "**/src/controller/java/generated/java/**/*"
ReturnCount:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/SensorClientFragment.kt"
- "**/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/CommandManager.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/Verhoeff.kt"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
UnusedPrivateProperty:
Expand All @@ -162,6 +168,7 @@ style:
ThrowsCount:
excludes:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/tlv/TlvReader.kt"
- "**/src/controller/java/src/chip/jsontlv/JsonToTlv.kt"
- "**/src/controller/java/src/chip/jsontlv/TlvToJson.kt"
Expand All @@ -174,23 +181,28 @@ style:
UnusedPrivateMember:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
UseCheckOrError:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/tlv/tags.kt"
- "**/src/controller/java/src/chip/tlv/types.kt"
UseRequire:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/jsontlv/JsonToTlv.kt"
- "**/src/controller/java/src/chip/onboardingpayload/Verhoeff10.kt"
UtilityClassWithPublicConstructor:
excludes:
- "**/src/controller/java/src/chip/onboardingpayload/Verhoeff.kt"
MayBeConst:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/Base38.kt"
FunctionOnlyReturningConstant:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/QRCodeOnboardingPayloadGenerator.kt"

exceptions:
Expand All @@ -202,6 +214,7 @@ exceptions:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/setuppayloadscanner/BarcodeFragment.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/Main.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/tests/chip/onboardingpayload/ManualCodeTest.kt"
- "**/src/controller/java/tests/chip/onboardingpayload/QRCodeTest.kt"
SwallowedException:
Expand All @@ -211,14 +224,17 @@ exceptions:
TooGenericExceptionThrown:
excludes:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt"
- "**/src/controller/java/generated/java/**/*"
ThrowingExceptionsWithoutMessageOrCause:
excludes:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt"
- "**/src/controller/java/generated/java/**/*"

naming:
FunctionNaming:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/SelectActionFragment.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/tests/chip/tlv/TlvReadWriteTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvWriterTest.kt"
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
Expand All @@ -229,21 +245,26 @@ naming:
ConstructorParameterNaming:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/HistoryCommandAdapter.kt"
- "**/src/controller/java/generated/java/**/*"
TopLevelPropertyNaming:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"

performance:
SpreadOperator:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"

potential-bugs:
EqualsWithHashCodeExist:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
ImplicitDefaultLocale:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadGenerator.kt"

Expand All @@ -262,6 +283,7 @@ complexity:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/jsontlv/JsonToTlv.kt"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
- "**/src/controller/java/src/chip/tlv/TlvReader.kt"
Expand All @@ -273,15 +295,18 @@ complexity:
NestedBlockDepth:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt"
- "**/src/controller/java/generated/java/**/*"
LongParameterList:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/ClusterDetailFragment.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/OnboardingPayload.kt"
LongMethod:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/provisioning/AddressCommissioningFragment.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt"
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/Base38.kt"
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadGenerator.kt"
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadParser.kt"
Expand All @@ -291,15 +316,18 @@ complexity:
- "**/src/controller/java/tests/chip/tlv/TlvReaderTest.kt"
LargeClass:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/tests/chip/tlv/TlvReadWriteTest.kt"
- "**/src/controller/java/tests/chip/jsontlv/JsonToTlvToJsonTest.kt"
CyclomaticComplexMethod:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/tlv/TlvReader.kt"
- "**/src/controller/java/src/chip/tlv/types.kt"
- "**/src/controller/java/src/chip/jsontlv/TlvToJson.kt"
ComplexCondition:
excludes:
- "**/src/controller/java/generated/java/**/*"
- "**/src/controller/java/src/chip/onboardingpayload/ManualOnboardingPayloadGenerator.kt"

empty-blocks:
Expand All @@ -324,3 +352,4 @@ empty-blocks:
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.kt"
- "**/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.kt"
- "**/src/controller/java/generated/java/**/*"
12 changes: 12 additions & 0 deletions scripts/build/builders/android.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ def copyToSrcAndroid(self):
"OnboardingPayload.jar": "src/controller/java/OnboardingPayload.jar",
"AndroidPlatform.jar": "src/platform/android/AndroidPlatform.jar",
"libCHIPTlv.jar": "src/controller/java/libCHIPTlv.jar",
"CHIPClusters.jar": "src/controller/java/CHIPClusters.jar",
"CHIPClusterID.jar": "src/controller/java/CHIPClusterID.jar",
}

for jarName in jars.keys():
Expand Down Expand Up @@ -573,6 +575,16 @@ def build_outputs(self):
"lib",
"src/controller/java/OnboardingPayload.jar",
),
"CHIPClusters.jar": os.path.join(
self.output_dir,
"lib",
"src/controller/java/CHIPClusters.jar",
),
"CHIPClusterID.jar": os.path.join(
self.output_dir,
"lib",
"src/controller/java/CHIPClusterID.jar",
),
"jni/%s/libCHIPController.so"
% self.board.AbiName(): os.path.join(
self.output_dir,
Expand Down
4 changes: 4 additions & 0 deletions scripts/build/testdata/dry_run_android-arm64-chip-tool.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,9 @@ cp {out}/android-arm64-chip-tool/lib/src/platform/android/AndroidPlatform.jar {r

cp {out}/android-arm64-chip-tool/lib/src/controller/java/libCHIPTlv.jar {root}/examples/android/CHIPTool/app/libs/libCHIPTlv.jar

cp {out}/android-arm64-chip-tool/lib/src/controller/java/CHIPClusters.jar {root}/examples/android/CHIPTool/app/libs/CHIPClusters.jar

cp {out}/android-arm64-chip-tool/lib/src/controller/java/CHIPClusterID.jar {root}/examples/android/CHIPTool/app/libs/CHIPClusterID.jar

# Building APP android-arm64-chip-tool
{root}/examples/android/CHIPTool/gradlew -p {root}/examples/android/CHIPTool -PmatterBuildSrcDir={out}/android-arm64-chip-tool -PmatterSdkSourceBuild=false -PbuildDir={out}/android-arm64-chip-tool assembleDebug
2 changes: 2 additions & 0 deletions scripts/py_matter_idl/files.gni
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ matter_idl_generator_templates = [
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/CHIPCallbackTypes.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersCpp.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ChipClustersRead.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ChipEventStructs.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/CHIPGlobalCallbacks_cpp.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/CHIPReadCallbacks_h.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ChipStructs.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ClusterIDMapping.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ClusterReadMapping.jinja",
"${chip_root}/scripts/py_matter_idl/matter_idl/generators/java/ClusterWriteMapping.jinja",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
{%- macro encode_value(source, encodable, depth) -%}
{%- if encodable.is_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 -%}
List<{{encode_value(source, encodable.without_list(), depth + 1)}}>
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
chip.devicecontroller.cluster.structs.{{source.name}}Cluster{{struct.name}}
{%- else -%}
{{encodable.kotlin_type}}
{%- endif -%}
{%- endmacro -%}

{%- macro encode_tlv(encodable, tag, name, depth) %}
{%- if encodable.is_nullable -%}
if ({{name}} != null) {
{{encode_tlv(encodable.without_nullable(), tag, name, depth + 1)}}
} else {
putNull({{tag}})
}
{%- elif encodable.is_optional -%}
if ({{name}}.isPresent) {
val opt{{name}} = {{name}}.get()
{{encode_tlv(encodable.without_optional(), tag, "opt" + name, depth + 1)}}
}
{%- elif encodable.is_list -%}
startList({{tag}})
for (item in {{name}}.iterator()) {
{{encode_tlv(encodable.without_list(), "AnonymousTag", "item", depth + 1)}}
}
endList()
{%- elif encodable.is_struct -%}
{{name}}.toTlv({{tag}}, this)
{%- else -%}
put({{tag}}, {{name}})
{%- endif -%}
{%- endmacro -%}

{%- macro decode_tlv(source, encodable, tag, depth) %}
{%- if encodable.is_nullable -%}
if (!tlvReader.isNull()) {
{{decode_tlv(source, encodable.without_nullable(), tag, depth + 1)}}
} else {
tlvReader.getNull({{tag}})
null
}
{%- elif encodable.is_optional -%}
if (tlvReader.isNextTag({{tag}})) {
Optional.of({{decode_tlv(source, encodable.without_optional(), tag, depth + 1)}})
} else {
Optional.empty()
}
{%- elif encodable.is_list -%}
{%- set encodablewithoutlist = encodable.without_list() -%}
buildList <{{encode_value(source, encodablewithoutlist, depth + 1)}}> {
tlvReader.enterList({{tag}})
while(!tlvReader.isEndOfContainer()) {
this.add({{decode_tlv(source, encodablewithoutlist, "AnonymousTag", depth + 1)}})
}
tlvReader.exitContainer()
}
{%- elif encodable.is_struct -%}
{%- set struct = encodable.get_underlying_struct() -%}
chip.devicecontroller.cluster.structs.{{source.name}}Cluster{{struct.name}}.fromTlv({{tag}}, tlvReader)
{%- else -%}
tlvReader.get{{encodable.kotlin_type}}({{tag}})
{%- endif -%}
{%- endmacro -%}

{%- macro contextSpecificTag(field) -%}
ContextSpecificTag(TAG_{{field.name | constcase}})
{%- 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.cluster.eventstructs

import chip.devicecontroller.cluster.*
import chip.tlv.AnonymousTag
import chip.tlv.ContextSpecificTag
import chip.tlv.Tag
import chip.tlv.TlvParsingException
import chip.tlv.TlvReader
import chip.tlv.TlvWriter

import java.util.Optional

class {{cluster.name}}Cluster{{event.name}}Event (
{%- for field in event.fields -%}
{%- set encodable = field | asEncodable(typeLookup) %}
val {{field.name}}: {{encode_value(cluster, encodable, 0)}}
{%- if loop.index0 < loop.length - 1 -%}{{","}}{%- endif -%}
{%- endfor -%}) {
override fun toString(): String = buildString {
append("{{cluster.name}}Cluster{{event.name}}Event {\n")
{%- for field in event.fields %}
append("\t{{field.name}} : ${{field.name}}\n")
{%- endfor %}
append("}\n")
}

fun toTlv(tag: Tag, tlvWriter: TlvWriter) {
tlvWriter.apply {
startStructure(tag)
{% for field in event.fields %}
{%- set encodable = field | asEncodable(typeLookup) %}
{%- set tag = contextSpecificTag(field) -%}
{{encode_tlv(encodable, tag, field.name, 0)}}
{% endfor -%}
endStructure()
}
}

companion object {
{%- for field in event.fields %}
private const val TAG_{{field.name | constcase}} = {{field.code}}
{%- endfor %}

fun fromTlv(tag: Tag, tlvReader: TlvReader) : {{cluster.name}}Cluster{{event.name}}Event {
tlvReader.enterStructure(tag)
{% for field in event.fields %}
{%- set decodable = field | asEncodable(typeLookup) %}
{%- set tag = contextSpecificTag(field) -%}
val {{field.name}} = {{decode_tlv(cluster, decodable, tag, 0)}}
{% endfor %}
tlvReader.exitContainer()

return {{cluster.name}}Cluster{{event.name}}Event(
{%- for field in event.fields -%}
{%- set encodable = field | asEncodable(typeLookup) -%}
{{field.name}}
{%- if loop.index0 < loop.length - 1 -%}{{", "}}{%- endif -%}
{%- endfor -%}
)
}
}
}
Loading

0 comments on commit b22d59d

Please sign in to comment.