diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 9592ab2805a61a..bd61c7132bccb5 100644 --- a/.github/workflows/tests.yaml +++ b/.github/workflows/tests.yaml @@ -531,7 +531,7 @@ jobs: runs-on: ubuntu-latest container: - image: connectedhomeip/chip-build:0.6.40 + image: connectedhomeip/chip-build-java:0.6.42 options: --privileged --sysctl "net.ipv6.conf.all.disable_ipv6=0 net.ipv4.conf.all.forwarding=0 net.ipv6.conf.all.forwarding=0" diff --git a/examples/java-matter-controller/BUILD.gn b/examples/java-matter-controller/BUILD.gn index f27a63dd19d8f2..9aa22bdd18ddb3 100644 --- a/examples/java-matter-controller/BUILD.gn +++ b/examples/java-matter-controller/BUILD.gn @@ -18,8 +18,8 @@ import("//build_overrides/chip.gni") import("${chip_root}/build/chip/java/rules.gni") import("${chip_root}/build/chip/tools.gni") -java_binary("java-matter-controller") { - output_name = "java-matter-controller" +java_library("java") { + output_name = "JavaMatterController.jar" deps = [ "${chip_root}/src/controller/java", "${chip_root}/src/setup_payload/java", @@ -27,45 +27,58 @@ java_binary("java-matter-controller") { ] sources = [ - "java/src/com/matter/controller/Main.java", - "java/src/com/matter/controller/commands/common/Argument.java", - "java/src/com/matter/controller/commands/common/ArgumentType.java", - "java/src/com/matter/controller/commands/common/Command.java", - "java/src/com/matter/controller/commands/common/CommandManager.java", - "java/src/com/matter/controller/commands/common/CredentialsIssuer.java", "java/src/com/matter/controller/commands/common/FutureResult.java", - "java/src/com/matter/controller/commands/common/IPAddress.java", - "java/src/com/matter/controller/commands/common/MatterCommand.java", "java/src/com/matter/controller/commands/common/RealResult.java", - "java/src/com/matter/controller/commands/discover/DiscoverCommand.java", - "java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java", - "java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java", - "java/src/com/matter/controller/commands/pairing/CloseSessionCommand.java", - "java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.java", - "java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.java", - "java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.java", - "java/src/com/matter/controller/commands/pairing/PairCodeCommand.java", - "java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.java", - "java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.java", - "java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.java", - "java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.java", - "java/src/com/matter/controller/commands/pairing/PairingCommand.java", - "java/src/com/matter/controller/commands/pairing/PairingModeType.java", - "java/src/com/matter/controller/commands/pairing/PairingNetworkType.java", - "java/src/com/matter/controller/commands/pairing/UnpairCommand.java", ] javac_flags = [ "-Xlint:deprecation" ] } +kotlin_binary("java-matter-controller") { + output_name = "java-matter-controller" + deps = [ + ":java", + "${chip_root}/third_party/java_deps:kotlin-stdlib", + ] + + sources = [ + "java/src/com/matter/controller/Main.kt", + "java/src/com/matter/controller/commands/common/Argument.kt", + "java/src/com/matter/controller/commands/common/ArgumentType.kt", + "java/src/com/matter/controller/commands/common/Command.kt", + "java/src/com/matter/controller/commands/common/CommandManager.kt", + "java/src/com/matter/controller/commands/common/CredentialsIssuer.kt", + "java/src/com/matter/controller/commands/common/IPAddress.kt", + "java/src/com/matter/controller/commands/common/MatterCommand.kt", + "java/src/com/matter/controller/commands/discover/DiscoverCommand.kt", + "java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt", + "java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt", + "java/src/com/matter/controller/commands/pairing/CloseSessionCommand.kt", + "java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.kt", + "java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairCodeCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairingCommand.kt", + "java/src/com/matter/controller/commands/pairing/PairingModeType.kt", + "java/src/com/matter/controller/commands/pairing/PairingNetworkType.kt", + "java/src/com/matter/controller/commands/pairing/UnpairCommand.kt", + ] + + kotlinc_flags = [ "-Xlint:deprecation" ] +} + group("default") { deps = [ ":java-matter-controller" ] } diff --git a/examples/java-matter-controller/Manifest.txt b/examples/java-matter-controller/Manifest.txt index ebdd1a402677f7..f5ca3d084d7283 100644 --- a/examples/java-matter-controller/Manifest.txt +++ b/examples/java-matter-controller/Manifest.txt @@ -1,3 +1,3 @@ -Main-Class: com.matter.controller.Main -Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/CHIPController.jar ../lib/third_party/connectedhomeip/src/setup_payload/java/SetupPayloadParser.jar ../lib/third_party/connectedhomeip/third_party/java_deps/stub_src/Android.jar ../lib/third_party/connectedhomeip/third_party/java_deps/json-20220924.jar ../lib/third_party/connectedhomeip/third_party/java_deps/jsr305-3.0.2.jar +Main-Class: com.matter.controller.MainKt +Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/CHIPController.jar ../lib/third_party/connectedhomeip/src/setup_payload/java/SetupPayloadParser.jar ../lib/third_party/connectedhomeip/third_party/java_deps/stub_src/Android.jar ../lib/third_party/connectedhomeip/third_party/java_deps/json-20220924.jar ../lib/third_party/connectedhomeip/third_party/java_deps/jsr305-3.0.2.jar ../lib/third_party/connectedhomeip/third_party/java_deps/kotlin-stdlib-1.3.31.jar diff --git a/examples/java-matter-controller/java/src/com/matter/controller/Main.kt b/examples/java-matter-controller/java/src/com/matter/controller/Main.kt new file mode 100644 index 00000000000000..40828d27bab697 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/Main.kt @@ -0,0 +1,100 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller + +import chip.devicecontroller.ChipDeviceController +import chip.devicecontroller.ControllerParams +import com.matter.controller.commands.common.* +import com.matter.controller.commands.discover.* +import com.matter.controller.commands.pairing.* +import java.util.ArrayList + +private fun registerCommandsDiscover( + controller: ChipDeviceController, + commandManager: CommandManager, + credentialsIssuer: CredentialsIssuer +) { + val clusterCommands: ArrayList = ArrayList() + val discoverCommand = DiscoverCommand(controller, credentialsIssuer) + val discoverCommissionablesCommand = DiscoverCommissionablesCommand(controller, credentialsIssuer) + val discoverCommissionersCommand = DiscoverCommissionersCommand(controller, credentialsIssuer) + clusterCommands.add(discoverCommand) + clusterCommands.add(discoverCommissionablesCommand) + clusterCommands.add(discoverCommissionersCommand) + commandManager.register("discover", clusterCommands) +} + +private fun registerCommandsPairing( + controller: ChipDeviceController, + commandManager: CommandManager, + credentialsIssuer: CredentialsIssuer +) { + val clusterCommands: ArrayList = ArrayList() + val unpairCommand = UnpairCommand(controller, credentialsIssuer) + val pairCodeCommand = PairCodeCommand(controller, credentialsIssuer) + val pairCodePaseCommand = PairCodePaseCommand(controller, credentialsIssuer) + val pairCodeWifiCommand = PairCodeWifiCommand(controller, credentialsIssuer) + val pairCodeThreadCommand = PairCodeThreadCommand(controller, credentialsIssuer) + val pairAddressPaseCommand = PairAddressPaseCommand(controller, credentialsIssuer) + val pairAlreadyDiscoveredCommand = PairAlreadyDiscoveredCommand(controller, credentialsIssuer) + val pairOnNetworkCommand = PairOnNetworkCommand(controller, credentialsIssuer) + val pairOnNetworkShortCommand = PairOnNetworkShortCommand(controller, credentialsIssuer) + val pairOnNetworkLongCommand = PairOnNetworkLongCommand(controller, credentialsIssuer) + val pairOnNetworkVendorCommand = PairOnNetworkVendorCommand(controller, credentialsIssuer) + val pairOnNetworkCommissioningModeCommand = PairOnNetworkCommissioningModeCommand(controller, credentialsIssuer) + val pairOnNetworkCommissionerCommand = PairOnNetworkCommissionerCommand(controller, credentialsIssuer) + val pairOnNetworkDeviceTypeCommand = PairOnNetworkDeviceTypeCommand(controller, credentialsIssuer) + val pairOnNetworkInstanceNameCommand = PairOnNetworkInstanceNameCommand(controller, credentialsIssuer) + clusterCommands.add(unpairCommand) + clusterCommands.add(pairCodeCommand) + clusterCommands.add(pairCodePaseCommand) + clusterCommands.add(pairCodeWifiCommand) + clusterCommands.add(pairCodeThreadCommand) + clusterCommands.add(pairAddressPaseCommand) + clusterCommands.add(pairAlreadyDiscoveredCommand) + clusterCommands.add(pairOnNetworkCommand) + clusterCommands.add(pairOnNetworkShortCommand) + clusterCommands.add(pairOnNetworkLongCommand) + clusterCommands.add(pairOnNetworkVendorCommand) + clusterCommands.add(pairOnNetworkCommissioningModeCommand) + clusterCommands.add(pairOnNetworkCommissionerCommand) + clusterCommands.add(pairOnNetworkDeviceTypeCommand) + clusterCommands.add(pairOnNetworkInstanceNameCommand) + commandManager.register("pairing", clusterCommands) +} + +fun main(args: Array) { + val controller = ChipDeviceController( + ControllerParams.newBuilder() + .setUdpListenPort(0) + .setControllerVendorId(0xFFF1) + .setCountryCode("US") + .build() + ) + val credentialsIssuer = CredentialsIssuer() + val commandManager = CommandManager() + registerCommandsDiscover(controller, commandManager, credentialsIssuer) + registerCommandsPairing(controller, commandManager, credentialsIssuer) + try { + commandManager.run(args) + } catch (e: Exception) { + println("Run command failed with exception: " + e.message) + System.exit(1) + } + controller.shutdownCommissioning() +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.java deleted file mode 100644 index 746ae78f4d3dfe..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.java +++ /dev/null @@ -1,164 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.common; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import javax.annotation.Nullable; - -public final class Argument { - private final String mName; - private final ArgumentType mType; - private final long mMin; - private final long mMax; - private final Object mValue; - private final Optional mDesc; - private final boolean mOptional; - - boolean isOptional() { - return mOptional; - } - - public Argument(String name, IPAddress value, boolean optional) { - this.mName = name; - this.mType = ArgumentType.ADDRESS; - this.mMin = 0; - this.mMax = 0; - this.mValue = value; - this.mDesc = Optional.empty(); - this.mOptional = optional; - } - - public Argument(String name, StringBuffer value, @Nullable String desc, boolean optional) { - this.mName = name; - this.mType = ArgumentType.STRING; - this.mMin = 0; - this.mMax = 0; - this.mValue = value; - this.mDesc = Optional.ofNullable(desc); - this.mOptional = optional; - } - - public Argument(String name, AtomicBoolean value, @Nullable String desc, boolean optional) { - this.mName = name; - this.mType = ArgumentType.BOOL; - this.mMin = 0; - this.mMax = 0; - this.mValue = value; - this.mDesc = Optional.ofNullable(desc); - this.mOptional = optional; - } - - public Argument( - String name, - short min, - short max, - AtomicInteger value, - @Nullable String desc, - boolean optional) { - this.mName = name; - this.mType = ArgumentType.NUMBER_INT16; - this.mMin = min; - this.mMax = max; - this.mValue = value; - this.mDesc = Optional.ofNullable(desc); - this.mOptional = optional; - } - - public Argument( - String name, int min, int max, AtomicInteger value, @Nullable String desc, boolean optional) { - this.mName = name; - this.mType = ArgumentType.NUMBER_INT32; - this.mMin = min; - this.mMax = max; - this.mValue = value; - this.mDesc = Optional.ofNullable(desc); - this.mOptional = optional; - } - - public Argument( - String name, long min, long max, AtomicLong value, @Nullable String desc, boolean optional) { - this.mName = name; - this.mType = ArgumentType.NUMBER_INT64; - this.mMin = min; - this.mMax = max; - this.mValue = value; - this.mDesc = Optional.ofNullable(desc); - this.mOptional = optional; - } - - public String getName() { - return mName; - } - - public ArgumentType getType() { - return mType; - } - - public Object getValue() { - return mValue; - } - - public Optional getDesc() { - return mDesc; - } - - public void setValue(String value) { - boolean isValidArgument = false; - - switch (mType) { - case ATTRIBUTE: - String str = (String) mValue; - isValidArgument = value.equals(str); - break; - case NUMBER_INT16: - AtomicInteger numShort = (AtomicInteger) mValue; - numShort.set(Integer.parseInt(value)); - isValidArgument = (numShort.intValue() >= mMin && numShort.intValue() <= mMax); - break; - case NUMBER_INT32: - AtomicInteger num = (AtomicInteger) mValue; - num.set(Integer.parseInt(value)); - isValidArgument = (num.intValue() >= mMin && num.intValue() <= mMax); - break; - case NUMBER_INT64: - AtomicLong numLong = (AtomicLong) mValue; - numLong.set(Long.parseLong(value)); - isValidArgument = (numLong.intValue() >= mMin && numLong.intValue() <= mMax); - break; - case ADDRESS: - try { - IPAddress ipAddress = (IPAddress) mValue; - ipAddress.setAddress(InetAddress.getByName(value)); - isValidArgument = true; - } catch (UnknownHostException e) { - isValidArgument = false; - } - break; - } - - if (!isValidArgument) { - throw new IllegalArgumentException("Invalid argument " + mName + ": " + value); - } - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt new file mode 100644 index 00000000000000..a571ef0869db3b --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Argument.kt @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.common + +import java.net.InetAddress +import java.net.UnknownHostException +import java.util.Optional +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong +import javax.annotation.Nullable + +class Argument { + val name: String + val type: ArgumentType + private val mMin: Long + private val mMax: Long + val value: Any + val desc: Optional + val isOptional: Boolean + + constructor(name: String, value: IPAddress, optional: Boolean) { + this.name = name + type = ArgumentType.ADDRESS + mMin = 0 + mMax = 0 + this.value = value + desc = Optional.empty() + isOptional = optional + } + + constructor(name: String, value: StringBuffer, @Nullable desc: String?, optional: Boolean) { + this.name = name + type = ArgumentType.STRING + mMin = 0 + mMax = 0 + this.value = value + this.desc = Optional.ofNullable(desc) + isOptional = optional + } + + constructor(name: String, value: AtomicBoolean, @Nullable desc: String?, optional: Boolean) { + this.name = name + type = ArgumentType.BOOL + mMin = 0 + mMax = 0 + this.value = value + this.desc = Optional.ofNullable(desc) + isOptional = optional + } + + constructor( + name: String, + min: Short, + max: Short, + value: AtomicInteger, + @Nullable desc: String?, + optional: Boolean + ) { + this.name = name + type = ArgumentType.NUMBER_INT16 + mMin = min.toLong() + mMax = max.toLong() + this.value = value + this.desc = Optional.ofNullable(desc) + isOptional = optional + } + + constructor( + name: String, min: Int, max: Int, value: AtomicInteger, @Nullable desc: String?, optional: Boolean + ) { + this.name = name + type = ArgumentType.NUMBER_INT32 + mMin = min.toLong() + mMax = max.toLong() + this.value = value + this.desc = Optional.ofNullable(desc) + isOptional = optional + } + + constructor( + name: String, min: Long, max: Long, value: AtomicLong, @Nullable desc: String?, optional: Boolean + ) { + this.name = name + type = ArgumentType.NUMBER_INT64 + mMin = min + mMax = max + this.value = value + this.desc = Optional.ofNullable(desc) + isOptional = optional + } + + fun setValue(value: String) { + var isValidArgument = false + when (type) { + ArgumentType.ATTRIBUTE -> { + val str = this.value as String + isValidArgument = value == str + } + + ArgumentType.NUMBER_INT16 -> { + val numShort = this.value as AtomicInteger + numShort.set(value.toInt()) + isValidArgument = numShort.toInt() >= mMin && numShort.toInt() <= mMax + } + + ArgumentType.NUMBER_INT32 -> { + val num = this.value as AtomicInteger + num.set(value.toInt()) + isValidArgument = num.toInt() >= mMin && num.toInt() <= mMax + } + + ArgumentType.NUMBER_INT64 -> { + val numLong = this.value as AtomicLong + numLong.set(value.toLong()) + isValidArgument = numLong.toInt() >= mMin && numLong.toInt() <= mMax + } + + ArgumentType.ADDRESS -> isValidArgument = try { + val ipAddress = this.value as IPAddress + ipAddress.setAddress(InetAddress.getByName(value)) + true + } catch (e: UnknownHostException) { + false + } + + else -> { + } + } + require(isValidArgument) { "Invalid argument " + name + ": " + value } + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.kt similarity index 70% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.kt index 1d614eb5b462d2..91d5d6922c9a3e 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.kt @@ -1,31 +1,22 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.pairing; - -public enum DiscoveryFilterType { - NONE, - SHORT_DISCRIMINATOR, - LONG_DISCRIMINATOR, - VENDOR_ID, - DEVICE_TYPE, - COMMISSIONING_MODE, - INSTANCE_NAME, - COMMISSIONER, - COMPRESSED_FABRIC_ID, -} +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.common + +enum class ArgumentType { + NUMBER_INT8, NUMBER_INT16, NUMBER_INT32, NUMBER_INT64, FLOAT, DOUBLE, BOOL, STRING, CHARSTRING, OCTETSTRING, ATTRIBUTE, ADDRESS, COMPLEX, CUSTOM, VECTOR_BOOL, VECTOR16, VECTOR32, VECTOR_CUSTOM +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.java deleted file mode 100644 index fe33e2459c2c31..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.common; - -import java.util.ArrayList; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import javax.annotation.Nullable; - -/** - * @brief Matter Controller command - * @details The base class of all the commands the Matter Controller supports, which are actions - * that may be performed. Commands are verb-like, such as pair a Matter device or discover - * Matter devices from the environment. - */ -public abstract class Command { - private static final String OPTIONAL_ARGUMENT_PREFIX = "--"; - private static final int OPTIONAL_ARGUMENT_PREFIX_LENGTH = 2; - private final String mName; - private final ArrayList mArgs = new ArrayList(); - private final Optional mHelpText; - - public Command(String commandName) { - this.mName = commandName; - this.mHelpText = Optional.empty(); - } - - public Command(String commandName, @Nullable String helpText) { - this.mName = commandName; - this.mHelpText = Optional.ofNullable(helpText); - } - - public final String getName() { - return mName; - } - - public final Optional getHelpText() { - return mHelpText; - } - - /** - * @brief Get attribute argument if it exists, there is at most one Attribute argument per command - * @return A pointer to an Optional where the Attribute argument will be stored - */ - public final Optional getAttribute() { - return mArgs - .stream() - .filter(arg -> arg.getType() == ArgumentType.ATTRIBUTE) - .findFirst() - .map(arg -> (String) arg.getValue()); - } - - public final String getArgumentName(int index) { - return mArgs.get(index).getName(); - } - - public final int getArgumentsCount() { - return mArgs.size(); - } - - public final boolean getArgumentIsOptional(int index) { - return mArgs.get(index).isOptional(); - } - - /** - * @brief Get argument description if it exists - * @return A pointer to an Optional where the argument description will be stored - */ - public final Optional getArgumentDescription(int index) { - return mArgs.get(index).getDesc(); - } - - public final void addArgumentToList(Argument arg, boolean optional) { - if (arg.isOptional() || mArgs.isEmpty()) { - // Safe to just append to the end of a list. - mArgs.add(arg); - return; - } - - // mandatory arg needs to be inserted before the optional arguments. - int index = 0; - while (index < mArgs.size() && !mArgs.get(index).isOptional()) { - index++; - } - - // Insert before the first optional arg. - mArgs.add(index, arg); - } - - /** - * @brief Add a bool command argument - * @param name The name that will be displayed in the command help - * @param out A pointer to a MutableInteger where the argv value will be stored - * @param desc The description of the argument that will be displayed in the command help - * @param optional Indicate if an optional argument - * @return The number of arguments currently added to the command - */ - public final void addArgument( - String name, AtomicBoolean out, @Nullable String desc, boolean optional) { - Argument arg = new Argument(name, out, desc, optional); - addArgumentToList(arg, optional); - } - - /** - * @brief Add a short command argument - * @param name The name that will be displayed in the command help - * @param min The minimum value of the argv value - * @param max The minimum value of the argv value - * @param out A pointer to a MutableInteger where the argv value will be stored - * @param desc The description of the argument that will be displayed in the command help - * @param optional Indicate if an optional argument - * @return The number of arguments currently added to the command - */ - public final void addArgument( - String name, - short min, - short max, - AtomicInteger out, - @Nullable String desc, - boolean optional) { - Argument arg = new Argument(name, min, max, out, desc, optional); - addArgumentToList(arg, optional); - } - - /** - * @brief Add an int command argument - * @param name The name that will be displayed in the command help - * @param min The minimum value of the argv value - * @param max The minimum value of the argv value - * @param out A pointer to a MutableInteger where the argv value will be stored - * @param desc The description of the argument that will be displayed in the command help - * @param optional Indicate if an optional argument - * @return The number of arguments currently added to the command - */ - public final void addArgument( - String name, int min, int max, AtomicInteger out, @Nullable String desc, boolean optional) { - Argument arg = new Argument(name, min, max, out, desc, optional); - addArgumentToList(arg, optional); - } - - /** - * @brief Add a long Integer command argument - * @param name The name that will be displayed in the command help - * @param min The minimum value of the argv value - * @param max The minimum value of the argv value - * @param out A pointer to a MutableInteger where the argv value will be stored - * @param desc The description of the argument that will be displayed in the command help - * @param optional Indicate if an optional argument - * @return The number of arguments currently added to the command - */ - public final void addArgument( - String name, long min, long max, AtomicLong out, @Nullable String desc, boolean optional) { - Argument arg = new Argument(name, min, max, out, desc, optional); - addArgumentToList(arg, optional); - } - - /** - * @brief Add an IP address command argument - * @param name The name that will be displayed in the command help - * @param out A pointer to a IPAddress where the argv value will be stored - * @param optional Indicate if an optional argument - * @return The number of arguments currently added to the command - */ - public final void addArgument(String name, IPAddress out, boolean optional) { - Argument arg = new Argument(name, out, optional); - addArgumentToList(arg, optional); - } - - /** - * @brief Add a String command argument - * @param name The name that will be displayed in the command help - * @param out A pointer to a StringBuffer where the argv value will be stored - * @param desc The description of the argument that will be displayed in the command help - * @param optional Indicate if an optional argument - * @return The number of arguments currently added to the command - */ - public final void addArgument( - String name, StringBuffer out, @Nullable String desc, boolean optional) { - Argument arg = new Argument(name, out, desc, optional); - addArgumentToList(arg, optional); - } - - /** - * @brief Initialize command arguments - * @param argc The number of arguments to a command (Does not include command itself) - * @param args Supplied command-line arguments as an array of String objects. - */ - public final void initArguments(int argc, String[] args) { - int mandatoryArgsCount = 0; - int currentIndex = 0; - - for (Argument arg : mArgs) { - if (!arg.isOptional()) { - mandatoryArgsCount++; - } - } - - if (argc < mandatoryArgsCount) { - throw new IllegalArgumentException( - "initArguments: Wrong arguments number: " + argc + " instead of " + mandatoryArgsCount); - } - - // Initialize mandatory arguments - for (int i = 0; i < mandatoryArgsCount; i++) { - initArgument(currentIndex++, args[i]); - } - - // Initialize optional arguments - // Optional arguments expect a name and a value, so i is increased by 2 on every step. - for (int i = mandatoryArgsCount; i < argc; i += 2) { - // optional arguments starts with OPTIONAL_ARGUMENT_PREFIX - if (args[i].length() <= OPTIONAL_ARGUMENT_PREFIX_LENGTH - && !args[i].startsWith(OPTIONAL_ARGUMENT_PREFIX)) { - throw new IllegalArgumentException("initArguments: Invalid optional argument: " + args[i]); - } - - if (args[i] - .substring(OPTIONAL_ARGUMENT_PREFIX_LENGTH) - .equals(mArgs.get(currentIndex).getName())) { - if (i + 1 >= argc) { - throw new IllegalArgumentException( - "initArguments: Optional argument " + args[i] + " missing value"); - } - - initArgument(currentIndex++, args[i + 1]); - } - } - } - - public void initArgument(int argIndex, String argValue) { - mArgs.get(argIndex).setValue(argValue); - } - - public abstract void run() throws Exception; -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.kt new file mode 100644 index 00000000000000..0d273acc4477bd --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/Command.kt @@ -0,0 +1,254 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.common + +import java.util.ArrayList +import java.util.Optional +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong +import javax.annotation.Nullable + +/** + * @brief Matter Controller command + * @details The base class of all the commands the Matter Controller supports, which are actions + * that may be performed. Commands are verb-like, such as pair a Matter device or discover + * Matter devices from the environment. + */ +abstract class Command { + val mName: String + private val mArgs = ArrayList() + val mHelpText: Optional + + constructor(commandName: String) { + mName = commandName + mHelpText = Optional.empty() + } + + constructor(commandName: String, @Nullable helpText: String?) { + mName = commandName + mHelpText = Optional.ofNullable(helpText) + } + + fun getName(): String { + return mName + } + + fun getHelpText(): Optional { + return mHelpText + } + + fun getArgumentName(index: Int): String { + return mArgs[index].name + } + + fun getArgumentsCount(): Int { + return mArgs.size + } + + fun getArgumentIsOptional(index: Int): Boolean { + return mArgs[index].isOptional + } + + /** + * @brief Get attribute argument if it exists, there is at most one Attribute argument per command + * @return A pointer to an Optional where the Attribute argument will be stored + */ + fun getAttribute(): Optional { + return mArgs.stream() + .filter { arg: Argument -> arg.type === ArgumentType.ATTRIBUTE } + .findFirst() + .map { arg: Argument -> arg.value as String } + } + + /** + * @brief Get argument description if it exists + * @return A pointer to an Optional where the argument description will be stored + */ + fun getArgumentDescription(index: Int): Optional { + return mArgs[index].desc + } + + fun addArgumentToList(arg: Argument) { + if (arg.isOptional || mArgs.isEmpty()) { + // Safe to just append to the end of a list. + mArgs.add(arg) + return + } + + // mandatory arg needs to be inserted before the optional arguments. + var index = 0 + while (index < mArgs.size && !mArgs[index].isOptional) { + index++ + } + + // Insert before the first optional arg. + mArgs.add(index, arg) + } + + /** + * @brief Add a bool command argument + * @param name The name that will be displayed in the command help + * @param out A pointer to a AtomicBoolean where the argv value will be stored + * @param desc The description of the argument that will be displayed in the command help + * @param optional Indicate if an optional argument + * @return The number of arguments currently added to the command + */ + fun addArgument( + name: String?, out: AtomicBoolean?, @Nullable desc: String?, optional: Boolean + ) { + val arg = Argument(name!!, out!!, desc, optional) + addArgumentToList(arg) + } + + /** + * @brief Add a short command argument + * @param name The name that will be displayed in the command help + * @param min The minimum value of the argv value + * @param max The minimum value of the argv value + * @param out A pointer to a AtomicInteger where the argv value will be stored + * @param desc The description of the argument that will be displayed in the command help + * @param optional Indicate if an optional argument + * @return The number of arguments currently added to the command + */ + fun addArgument( + name: String?, + min: Short, + max: Short, + out: AtomicInteger?, + @Nullable desc: String?, + optional: Boolean + ) { + val arg = Argument(name!!, min, max, out!!, desc, optional) + addArgumentToList(arg) + } + + /** + * @brief Add an int command argument + * @param name The name that will be displayed in the command help + * @param min The minimum value of the argv value + * @param max The minimum value of the argv value + * @param out A pointer to a AtomicInteger where the argv value will be stored + * @param desc The description of the argument that will be displayed in the command help + * @param optional Indicate if an optional argument + * @return The number of arguments currently added to the command + */ + fun addArgument( + name: String?, min: Int, max: Int, out: AtomicInteger?, @Nullable desc: String?, optional: Boolean + ) { + val arg = Argument(name!!, min, max, out!!, desc, optional) + addArgumentToList(arg) + } + + /** + * @brief Add a long Integer command argument + * @param name The name that will be displayed in the command help + * @param min The minimum value of the argv value + * @param max The minimum value of the argv value + * @param out A pointer to a AtomicLong where the argv value will be stored + * @param desc The description of the argument that will be displayed in the command help + * @param optional Indicate if an optional argument + * @return The number of arguments currently added to the command + */ + fun addArgument( + name: String?, min: Long, max: Long, out: AtomicLong?, @Nullable desc: String?, optional: Boolean + ) { + val arg = Argument(name!!, min, max, out!!, desc, optional) + addArgumentToList(arg) + } + + /** + * @brief Add an IP address command argument + * @param name The name that will be displayed in the command help + * @param out A pointer to a IPAddress where the argv value will be stored + * @param optional Indicate if an optional argument + * @return The number of arguments currently added to the command + */ + fun addArgument(name: String?, out: IPAddress?, optional: Boolean) { + val arg = Argument(name!!, out!!, optional) + addArgumentToList(arg) + } + + /** + * @brief Add a String command argument + * @param name The name that will be displayed in the command help + * @param out A pointer to a StringBuffer where the argv value will be stored + * @param desc The description of the argument that will be displayed in the command help + * @param optional Indicate if an optional argument + * @return The number of arguments currently added to the command + */ + fun addArgument( + name: String?, out: StringBuffer?, @Nullable desc: String?, optional: Boolean + ) { + val arg = Argument(name!!, out!!, desc, optional) + addArgumentToList(arg) + } + + /** + * @brief Initialize command arguments + * @param argc The number of arguments to a command (Does not include command itself) + * @param args Supplied command-line arguments as an array of String objects. + */ + fun initArguments(argc: Int, args: Array) { + var mandatoryArgsCount = 0 + var currentIndex = 0 + for (arg in mArgs) { + if (!arg.isOptional) { + mandatoryArgsCount++ + } + } + require(argc >= mandatoryArgsCount) { "initArguments: Wrong arguments number: $argc instead of $mandatoryArgsCount" } + + // Initialize mandatory arguments + for (i in 0 until mandatoryArgsCount) { + initArgument(currentIndex++, args[i]) + } + + // Initialize optional arguments + // Optional arguments expect a name and a value, so i is increased by 2 on every step. + var i = mandatoryArgsCount + while (i < argc) { + + // optional arguments starts with OPTIONAL_ARGUMENT_PREFIX + require( + !(args[i].length <= OPTIONAL_ARGUMENT_PREFIX_LENGTH + && !args[i].startsWith(OPTIONAL_ARGUMENT_PREFIX)) + ) { "initArguments: Invalid optional argument: " + args[i] } + if (args[i] + .substring(OPTIONAL_ARGUMENT_PREFIX_LENGTH) + == mArgs[currentIndex].name + ) { + require(i + 1 < argc) { "initArguments: Optional argument " + args[i] + " missing value" } + initArgument(currentIndex++, args[i + 1]) + } + i += 2 + } + } + + fun initArgument(argIndex: Int, argValue: String?) { + mArgs[argIndex].setValue(argValue!!) + } + + @Throws(Exception::class) + abstract fun run() + + companion object { + private const val OPTIONAL_ARGUMENT_PREFIX = "--" + private const val OPTIONAL_ARGUMENT_PREFIX_LENGTH = 2 + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java deleted file mode 100644 index 47e184ec3ae4d7..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.java +++ /dev/null @@ -1,311 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.common; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; -import java.util.logging.Level; -import java.util.logging.Logger; - -public final class CommandManager { - private final ArrayList mCommandMgr = new ArrayList(); - private final Map> mClusters = - new HashMap>(); - private static Logger logger = Logger.getLogger(CommandManager.class.getName()); - - public final void register(String clusterName, ArrayList commandsList) { - mClusters.put(clusterName, commandsList); - } - - public final void run(String[] args) throws Exception { - Command command; - - if (args.length < 1) { - logger.log(Level.INFO, "Missing cluster name"); - showClusters(); - return; - } - - ArrayList commands = mClusters.get(args[0]); - if (commands == null) { - logger.log(Level.INFO, "Unknown cluster: " + args[0]); - showClusters(); - return; - } - - if (args.length < 2) { - logger.log(Level.INFO, "Missing command name"); - showCluster(args[0], commands); - return; - } - - if (!isGlobalCommand(args[0])) { - command = getCommand(commands, args[1]); - if (command == null) { - System.out.printf("Unknown command: %s", args[1]); - showCluster(args[0], commands); - throw new IllegalArgumentException(); - } - } else if (isEventCommand(args[1])) { - if (args.length < 3) { - logger.log(Level.INFO, "Missing event name"); - showClusterEvents(args[0], args[1], commands); - throw new IllegalArgumentException(); - } - - command = getGlobalCommand(commands, args[1], args[2]); - if (command == null) { - logger.log(Level.INFO, "Unknown event: " + args[2]); - showClusterEvents(args[0], args[1], commands); - throw new IllegalArgumentException(); - } - } else { - if (args.length < 3) { - logger.log(Level.INFO, "Missing attribute name"); - showClusterAttributes(args[0], args[1], commands); - throw new IllegalArgumentException(); - } - - command = getGlobalCommand(commands, args[1], args[2]); - if (command == null) { - logger.log(Level.INFO, "Unknown attribute: " + args[2]); - showClusterAttributes(args[0], args[1], commands); - throw new IllegalArgumentException(); - } - } - - // need skip over binary and command name and only get arguments - String[] temp = Arrays.copyOfRange(args, 2, args.length); - - try { - command.initArguments(temp.length, temp); - } catch (IllegalArgumentException e) { - logger.log(Level.INFO, "Arguments init failed with exception: " + e.getMessage()); - showCommand(args[0], command); - System.exit(1); - } - - command.run(); - } - - private boolean isAttributeCommand(String commandName) { - return commandName.equals("read") - || commandName.equals("write") - || commandName.equals("subscribe"); - } - - private boolean isEventCommand(String commandName) { - return commandName.equals("read-event") || commandName.equals("subscribe-event"); - } - - private boolean isGlobalCommand(String commandName) { - return isAttributeCommand(commandName) || isEventCommand(commandName); - } - - private Command getCommand(ArrayList commands, String commandName) { - for (Command command : commands) { - if (commandName.equals(command.getName())) { - return command; - } - } - - return null; - } - - private Command getGlobalCommand( - ArrayList commands, String commandName, String attributeName) { - for (Command command : commands) { - if (commandName.equals(command.getName()) && attributeName.equals(command.getAttribute())) { - return command; - } - } - - return null; - } - - private void showClusters() { - logger.log(Level.INFO, "Usage:"); - logger.log( - Level.INFO, " java-matter-controller cluster_name command_name [param1 param2 ...]"); - logger.log(Level.INFO, "\n"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - logger.log( - Level.INFO, - " | Clusters: |"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - - for (String key : mClusters.keySet()) { - System.out.printf(" | * %-82s|\n", key.toLowerCase()); - } - - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - } - - private void showCluster(String clusterName, ArrayList commands) { - logger.log(Level.INFO, "Usage:"); - logger.log( - Level.INFO, - " java-matter-controller " + clusterName + " command_name [param1 param2 ...]"); - logger.log(Level.INFO, "\n"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - logger.log( - Level.INFO, - " | Commands: |"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - boolean readCommand = false; - boolean writeCommand = false; - boolean subscribeCommand = false; - boolean readEventCommand = false; - boolean subscribeEventCommand = false; - - for (Command command : commands) { - boolean shouldPrint = true; - String cmdName = command.getName(); - if (isGlobalCommand(cmdName)) { - if (cmdName.equals("read") && !readCommand) { - readCommand = true; - } else if (cmdName.equals("write") && !writeCommand) { - writeCommand = true; - } else if (cmdName.equals("subscribe") && !subscribeCommand) { - subscribeCommand = true; - } else if (cmdName.equals("read-event") && !readEventCommand) { - readEventCommand = true; - } else if (cmdName.equals("subscribe-event") && !subscribeEventCommand) { - subscribeEventCommand = true; - } else { - shouldPrint = false; - } - } - - if (shouldPrint) { - System.out.printf(" | * %-82s|\n", cmdName); - } - } - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+\n"); - } - - private void showClusterAttributes( - String clusterName, String commandName, ArrayList commands) { - logger.log(Level.INFO, "Usage:"); - System.out.printf( - " java-matter-controller %s %s attribute-name [param1 param2 ...]\n", - clusterName, commandName); - logger.log(Level.INFO, "\n"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - logger.log( - Level.INFO, - " | Attributes: |"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - for (Command command : commands) { - if (commandName.equals(command.getName())) { - System.out.printf(" | * %-82s|\n", command.getAttribute().get()); - } - } - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - } - - private void showClusterEvents( - String clusterName, String commandName, ArrayList commands) { - logger.log(Level.INFO, "Usage:"); - System.out.printf( - " java-matter-controller %s %s event-name [param1 param2 ...]\n", - clusterName, commandName); - logger.log(Level.INFO, "\n"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - logger.log( - Level.INFO, - " | Events: |"); - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - - for (Command command : commands) { - if (commandName.equals(command.getName())) { - System.out.printf(" | * %-82s|\n", command.getAttribute().get()); - } - } - logger.log( - Level.INFO, - " +-------------------------------------------------------------------------------------+"); - } - - private void showCommand(String clusterName, Command command) { - logger.log(Level.INFO, "Usage:"); - - String arguments = command.getName(); - String description = ""; - - int argumentsCount = command.getArgumentsCount(); - for (int i = 0; i < argumentsCount; i++) { - String arg = ""; - boolean isOptional = command.getArgumentIsOptional(i); - if (isOptional) { - arg += "[--"; - } - arg += command.getArgumentName(i); - if (isOptional) { - arg += "]"; - } - arguments += " "; - arguments += arg; - - Optional argDescription = command.getArgumentDescription(i); - if (argDescription.isPresent()) { - description += "\n"; - description += arg; - description += ":\n "; - description += argDescription.get(); - description += "\n"; - } - } - System.out.format(" java-matter-controller %s %s\n", clusterName, arguments); - - Optional helpText = command.getHelpText(); - if (helpText.isPresent()) { - System.out.format("\n%s\n", helpText.get()); - } - - if (!description.isEmpty()) { - logger.log(Level.INFO, description); - } - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt new file mode 100644 index 00000000000000..16269383728e17 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CommandManager.kt @@ -0,0 +1,313 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.common + +import java.util.ArrayList +import java.util.Arrays +import java.util.HashMap +import java.util.Optional +import java.util.logging.Level +import java.util.logging.Logger +import kotlin.collections.MutableMap + +class CommandManager { + private val mCommandMgr = ArrayList() + private val mClusters: MutableMap> = HashMap() + fun register(clusterName: String, commandsList: ArrayList) { + mClusters[clusterName] = commandsList + } + + @Throws(Exception::class) + fun run(args: Array) { + val command: Command? + if (args.size < 1) { + logger.log(Level.INFO, "Missing cluster name") + showClusters() + return + } + val commands = mClusters[args[0]] + if (commands == null) { + logger.log(Level.INFO, "Unknown cluster: " + args[0]) + showClusters() + return + } + if (args.size < 2) { + logger.log(Level.INFO, "Missing command name") + showCluster(args[0], commands) + return + } + if (!isGlobalCommand(args[0])) { + command = getCommand(commands, args[1]) + if (command == null) { + System.out.printf("Unknown command: %s", args[1]) + showCluster(args[0], commands) + throw IllegalArgumentException() + } + } else if (isEventCommand(args[1])) { + if (args.size < 3) { + logger.log(Level.INFO, "Missing event name") + showClusterEvents(args[0], args[1], commands) + throw IllegalArgumentException() + } + command = getGlobalCommand(commands, args[1], args[2]) + if (command == null) { + logger.log(Level.INFO, "Unknown event: " + args[2]) + showClusterEvents(args[0], args[1], commands) + throw IllegalArgumentException() + } + } else { + if (args.size < 3) { + logger.log(Level.INFO, "Missing attribute name") + showClusterAttributes(args[0], args[1], commands) + throw IllegalArgumentException() + } + command = getGlobalCommand(commands, args[1], args[2]) + if (command == null) { + logger.log(Level.INFO, "Unknown attribute: " + args[2]) + showClusterAttributes(args[0], args[1], commands) + throw IllegalArgumentException() + } + } + + // need skip over binary and command name and only get arguments + val temp = Arrays.copyOfRange(args, 2, args.size) + try { + command.initArguments(temp.size, temp) + } catch (e: IllegalArgumentException) { + logger.log(Level.INFO, "Arguments init failed with exception: " + e.message) + showCommand(args[0], command) + System.exit(1) + } + command.run() + } + + private fun isAttributeCommand(commandName: String): Boolean { + return commandName == "read" || commandName == "write" || commandName == "subscribe" + } + + private fun isEventCommand(commandName: String): Boolean { + return commandName == "read-event" || commandName == "subscribe-event" + } + + private fun isGlobalCommand(commandName: String): Boolean { + return isAttributeCommand(commandName) || isEventCommand(commandName) + } + + private fun getCommand(commands: ArrayList, commandName: String): Command? { + for (command in commands) { + if (commandName == command.getName()) { + return command + } + } + return null + } + + private fun getGlobalCommand( + commands: ArrayList, commandName: String, attributeName: String + ): Command? { + for (command in commands) { + if (commandName == command.getName() && attributeName == command.getAttribute().get()) { + return command + } + } + return null + } + + private fun showClusters() { + logger.log(Level.INFO, "Usage:") + logger.log( + Level.INFO, " java-matter-controller cluster_name command_name [param1 param2 ...]" + ) + logger.log(Level.INFO, "\n") + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + logger.log( + Level.INFO, + " | Clusters: |" + ) + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + for (key in mClusters.keys) { + System.out.printf(" | * %-82s|\n", key) + } + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + } + + private fun showCluster(clusterName: String, commands: ArrayList) { + logger.log(Level.INFO, "Usage:") + logger.log( + Level.INFO, + " java-matter-controller $clusterName command_name [param1 param2 ...]" + ) + logger.log(Level.INFO, "\n") + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + logger.log( + Level.INFO, + " | Commands: |" + ) + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + var readCommand = false + var writeCommand = false + var subscribeCommand = false + var readEventCommand = false + var subscribeEventCommand = false + for (command in commands) { + var shouldPrint = true + val cmdName = command.getName() + if (isGlobalCommand(cmdName)) { + if (cmdName == "read" && !readCommand) { + readCommand = true + } else if (cmdName == "write" && !writeCommand) { + writeCommand = true + } else if (cmdName == "subscribe" && !subscribeCommand) { + subscribeCommand = true + } else if (cmdName == "read-event" && !readEventCommand) { + readEventCommand = true + } else if (cmdName == "subscribe-event" && !subscribeEventCommand) { + subscribeEventCommand = true + } else { + shouldPrint = false + } + } + if (shouldPrint) { + System.out.printf(" | * %-82s|\n", cmdName) + } + } + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+\n" + ) + } + + private fun showClusterAttributes( + clusterName: String, commandName: String, commands: ArrayList + ) { + logger.log(Level.INFO, "Usage:") + System.out.printf( + " java-matter-controller %s %s attribute-name [param1 param2 ...]\n", + clusterName, commandName + ) + logger.log(Level.INFO, "\n") + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + logger.log( + Level.INFO, + " | Attributes: |" + ) + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + for (command in commands) { + if (commandName == command.getName()) { + System.out.printf(" | * %-82s|\n", command.getAttribute().get()) + } + } + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + } + + private fun showClusterEvents( + clusterName: String, commandName: String, commands: ArrayList + ) { + logger.log(Level.INFO, "Usage:") + System.out.printf( + " java-matter-controller %s %s event-name [param1 param2 ...]\n", + clusterName, commandName + ) + logger.log(Level.INFO, "\n") + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + logger.log( + Level.INFO, + " | Events: |" + ) + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + for (command in commands) { + if (commandName == command.getName()) { + System.out.printf(" | * %-82s|\n", command.getAttribute().get()) + } + } + logger.log( + Level.INFO, + " +-------------------------------------------------------------------------------------+" + ) + } + + private fun showCommand(clusterName: String, command: Command) { + logger.log(Level.INFO, "Usage:") + var arguments: String? = command.getName() + var description = "" + val argumentsCount = command.getArgumentsCount() + for (i in 0 until argumentsCount) { + var arg = "" + val isOptional = command.getArgumentIsOptional(i) + if (isOptional) { + arg += "[--" + } + arg += command.getArgumentName(i) + if (isOptional) { + arg += "]" + } + arguments += " " + arguments += arg + val argDescription = command.getArgumentDescription(i) + if (argDescription.isPresent) { + description += "\n" + description += arg + description += ":\n " + description += argDescription.get() + description += "\n" + } + } + System.out.format(" java-matter-controller %s %s\n", clusterName, arguments) + val helpText = command.getHelpText() + if (helpText.isPresent) { + System.out.format("\n%s\n", helpText.get()) + } + if (!description.isEmpty()) { + logger.log(Level.INFO, description) + } + } + + companion object { + private val logger = Logger.getLogger(CommandManager::class.java.name) + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CredentialsIssuer.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CredentialsIssuer.kt similarity index 83% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/common/CredentialsIssuer.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/common/CredentialsIssuer.kt index a77508ea35a1bf..c97d1d5e7a1dd7 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CredentialsIssuer.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/CredentialsIssuer.kt @@ -15,12 +15,11 @@ * limitations under the License. * */ - -package com.matter.controller.commands.common; +package com.matter.controller.commands.common /** * @brief Credentials Issuer for the Command * @details Contains all credential information of the issuer of the command, such as operational - * credentials for a given fabric, the DAC verifier of the commisioner, etc .. + * credentials for a given fabric, the DAC verifier of the commisioner, etc .. */ -public class CredentialsIssuer {} +class CredentialsIssuer \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/IPAddress.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/IPAddress.java deleted file mode 100644 index d419db43f25686..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/IPAddress.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.matter.controller.commands.common; - -import java.net.InetAddress; - -public final class IPAddress { - private InetAddress address; - - public IPAddress(InetAddress address) { - this.address = address; - } - - public void setAddress(InetAddress address) { - this.address = address; - } - - @Override - public String toString() { - return address.toString(); - } - - public String getHostAddress() { - return address.getHostAddress(); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/IPAddress.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/IPAddress.kt new file mode 100644 index 00000000000000..ab0cb448ae3e2a --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/IPAddress.kt @@ -0,0 +1,17 @@ +package com.matter.controller.commands.common + +import java.net.InetAddress + +class IPAddress(private var address: InetAddress) { + fun setAddress(address: InetAddress) { + this.address = address + } + + fun getHostAddress(): String { + return address.hostAddress + } + + override fun toString(): String { + return address.toString() + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java deleted file mode 100644 index 4d958c47e2f316..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java +++ /dev/null @@ -1,117 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.common; - -import chip.devicecontroller.ChipDeviceController; -import java.util.Optional; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicLong; -import java.util.logging.Logger; - -public abstract class MatterCommand extends Command { - private final ChipDeviceController mChipDeviceController; - private final Optional mCredIssuerCmds; - private final StringBuffer mCommissionerName = new StringBuffer(); - private final StringBuffer mPaaTrustStorePath = new StringBuffer(); - private final StringBuffer mCDTrustStorePath = new StringBuffer(); - private final AtomicLong mCommissionerNodeId = new AtomicLong(); - private final AtomicBoolean mUseMaxSizedCerts = new AtomicBoolean(); - private final AtomicBoolean mOnlyAllowTrustedCdKeys = new AtomicBoolean(); - private FutureResult mFutureResult = new FutureResult(); - private static Logger logger = Logger.getLogger(MatterCommand.class.getName()); - - public MatterCommand( - ChipDeviceController controller, String commandName, CredentialsIssuer credIssuerCmds) { - this(controller, commandName, credIssuerCmds, null); - } - - public MatterCommand( - ChipDeviceController controller, - String commandName, - CredentialsIssuer credIssuerCmds, - String helpText) { - super(commandName, helpText); - this.mCredIssuerCmds = Optional.ofNullable(credIssuerCmds); - this.mChipDeviceController = controller; - - addArgument( - "paa-trust-store-path", - mPaaTrustStorePath, - "Path to directory holding PAA certificate information. Can be absolute or relative to the current working " - + "directory.", - true); - addArgument( - "cd-trust-store-path", - mCDTrustStorePath, - "Path to directory holding CD certificate information. Can be absolute or relative to the current working " - + "directory.", - true); - addArgument( - "commissioner-name", - mCommissionerName, - "Name of fabric to use. Valid values are \"alpha\", \"beta\", \"gamma\", and integers greater than or equal to " - + "4. The default if not specified is \"alpha\".", - true); - addArgument( - "commissioner-nodeid", - 0, - Long.MAX_VALUE, - mCommissionerNodeId, - "The node id to use for java-matter-controller. If not provided, kTestControllerNodeId (112233, 0x1B669) will be used.", - true); - addArgument( - "use-max-sized-certs", - mUseMaxSizedCerts, - "Maximize the size of operational certificates. If not provided or 0 (\"false\"), normally sized operational " - + "certificates are generated.", - true); - addArgument( - "only-allow-trusted-cd-keys", - mOnlyAllowTrustedCdKeys, - "Only allow trusted CD verifying keys (disallow test keys). If not provided or 0 (\"false\"), untrusted CD " - + "verifying keys are allowed. If 1 (\"true\"), test keys are disallowed.", - true); - } - - // This method returns the commissioner instance to be used for running the command. - public ChipDeviceController currentCommissioner() { - return mChipDeviceController; - } - - /////////// Command Interface ///////// - @Override - public void run() throws Exception { - runCommand(); - } - - protected abstract void runCommand(); - - public void setSuccess() { - mFutureResult.setRealResult(RealResult.Success()); - } - - public void setFailure(String error) { - mFutureResult.setRealResult(RealResult.Error(error)); - } - - public void waitCompleteMs(long timeoutMs) { - mFutureResult.setTimeoutMs(timeoutMs); - mFutureResult.waitResult(); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt new file mode 100644 index 00000000000000..a325da82317d0e --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.common + +import chip.devicecontroller.ChipDeviceController +import java.util.Optional +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicLong +import java.util.logging.Logger + +abstract class MatterCommand( + controller: ChipDeviceController, + commandName: String?, + credIssuerCmds: CredentialsIssuer?, + helpText: String? +) : Command(commandName!!, helpText) { + private val logger = Logger.getLogger(MatterCommand::class.java.name) + private val mChipDeviceController: ChipDeviceController + private val mCredIssuerCmds: Optional + private val mCommissionerName = StringBuffer() + private val mPaaTrustStorePath = StringBuffer() + private val mCDTrustStorePath = StringBuffer() + private val mCommissionerNodeId: AtomicLong = AtomicLong() + private val mUseMaxSizedCerts: AtomicBoolean = AtomicBoolean() + private val mOnlyAllowTrustedCdKeys: AtomicBoolean = AtomicBoolean() + private val mFutureResult = FutureResult() + + constructor( + controller: ChipDeviceController, commandName: String?, credIssuerCmds: CredentialsIssuer? + ) : this(controller, commandName, credIssuerCmds, null) + + init { + mCredIssuerCmds = Optional.ofNullable(credIssuerCmds) + mChipDeviceController = controller + addArgument( + "paa-trust-store-path", + mPaaTrustStorePath, + "Path to directory holding PAA certificate information. Can be absolute or relative to the current working " + + "directory.", + true + ) + addArgument( + "cd-trust-store-path", + mCDTrustStorePath, + "Path to directory holding CD certificate information. Can be absolute or relative to the current working " + + "directory.", + true + ) + addArgument( + "commissioner-name", + mCommissionerName, + "Name of fabric to use. Valid values are \"alpha\", \"beta\", \"gamma\", and integers greater than or equal to " + + "4. The default if not specified is \"alpha\".", + true + ) + addArgument( + "commissioner-nodeid", + 0, Long.MAX_VALUE, + mCommissionerNodeId, + "The node id to use for java-matter-controller. If not provided, kTestControllerNodeId (112233, 0x1B669) will be used.", + true + ) + addArgument( + "use-max-sized-certs", + mUseMaxSizedCerts, + "Maximize the size of operational certificates. If not provided or 0 (\"false\"), normally sized operational " + + "certificates are generated.", + true + ) + addArgument( + "only-allow-trusted-cd-keys", + mOnlyAllowTrustedCdKeys, + "Only allow trusted CD verifying keys (disallow test keys). If not provided or 0 (\"false\"), untrusted CD " + + "verifying keys are allowed. If 1 (\"true\"), test keys are disallowed.", + true + ) + } + + // This method returns the commissioner instance to be used for running the command. + fun currentCommissioner(): ChipDeviceController { + return mChipDeviceController + } + + /////////// Command Interface ///////// + @Throws(Exception::class) + override fun run() { + runCommand() + } + + protected abstract fun runCommand() + fun setSuccess() { + mFutureResult.setRealResult(RealResult.Success()) + } + + fun setFailure(error: String?) { + mFutureResult.setRealResult(RealResult.Error(error)) + } + + fun waitCompleteMs(timeoutMs: Long) { + mFutureResult.setTimeoutMs(timeoutMs) + mFutureResult.waitResult() + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.java deleted file mode 100644 index 988e97bb23c3b8..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.discover; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; -import com.matter.controller.commands.common.MatterCommand; -import java.util.concurrent.atomic.AtomicLong; - -public final class DiscoverCommand extends MatterCommand { - private final AtomicLong mNodeId = new AtomicLong(); - private final AtomicLong mFabricId = new AtomicLong(); - - public DiscoverCommand(ChipDeviceController controller, CredentialsIssuer credsIssuer) { - super(controller, "resolve", credsIssuer); - addArgument("nodeid", 0, Long.MAX_VALUE, mNodeId, null, false); - addArgument("fabricid", 0, Long.MAX_VALUE, mFabricId, null, false); - } - - @Override - protected final void runCommand() { - runCommand(mNodeId.get(), mFabricId.get()); - } - - private final void runCommand(long remoteId, long fabricId) {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.kt new file mode 100644 index 00000000000000..d46965ddc9ef9d --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.kt @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.discover + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer +import com.matter.controller.commands.common.MatterCommand +import java.util.concurrent.atomic.AtomicLong + +class DiscoverCommand(controller: ChipDeviceController, credsIssuer: CredentialsIssuer?) : + MatterCommand(controller, "resolve", credsIssuer) { + private val mNodeId: AtomicLong = AtomicLong() + private val mFabricId: AtomicLong = AtomicLong() + + init { + addArgument("nodeid", 0, Long.MAX_VALUE, mNodeId, null, false) + addArgument("fabricid", 0, Long.MAX_VALUE, mFabricId, null, false) + } + + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java deleted file mode 100644 index 2ba38307554fbd..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.discover; - -import chip.devicecontroller.ChipDeviceController; -import chip.devicecontroller.DiscoveredDevice; -import com.matter.controller.commands.common.CredentialsIssuer; -import com.matter.controller.commands.common.MatterCommand; - -public final class DiscoverCommissionablesCommand extends MatterCommand { - private static final int MAX_DISCOVERED_DEVICES = 10; - - public DiscoverCommissionablesCommand( - ChipDeviceController controller, CredentialsIssuer credsIssuer) { - super(controller, "commissionables", credsIssuer); - } - - @Override - protected final void runCommand() { - currentCommissioner().discoverCommissionableNodes(); - - // Pause for 7 seconds - try { - Thread.sleep(7000); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - - getDiscoveredDevice(); - } - - private final void getDiscoveredDevice() { - // Log at most MAX_DISCOVERED_DEVICES discovered devices - for (int i = 0; i < MAX_DISCOVERED_DEVICES; i++) { - DiscoveredDevice device = currentCommissioner().getDiscoveredDevice(i); - if (device == null) { - break; - } - - logDevice(device); - } - } - - private final void logDevice(DiscoveredDevice device) { - System.out.println("Discovered node:"); - System.out.format("\tDiscriminator: %d", device.discriminator); - System.out.format("\tIP Address : %s%n", device.ipAddress); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt new file mode 100644 index 00000000000000..3797ed0f44b6a5 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.discover + +import chip.devicecontroller.ChipDeviceController +import chip.devicecontroller.DiscoveredDevice +import com.matter.controller.commands.common.CredentialsIssuer +import com.matter.controller.commands.common.MatterCommand + +class DiscoverCommissionablesCommand( + controller: ChipDeviceController, credsIssuer: CredentialsIssuer? +) : MatterCommand(controller, "commissionables", credsIssuer) { + private val MAX_DISCOVERED_DEVICES = 10 + + override fun runCommand() { + currentCommissioner().discoverCommissionableNodes() + + // Pause for 7 seconds + try { + Thread.sleep(7000) + } catch (e: InterruptedException) { + throw RuntimeException(e) + } + + getDiscoveredDevice() + } + + private fun getDiscoveredDevice() { + // Log at most MAX_DISCOVERED_DEVICES discovered devices + for (i in 0 until MAX_DISCOVERED_DEVICES) { + val device: DiscoveredDevice = currentCommissioner().getDiscoveredDevice(i) ?: break + logDevice(device) + } + } + + private fun logDevice(device: DiscoveredDevice) { + println("Discovered node:") + System.out.format("\tDiscriminator: %d", device.discriminator) + System.out.format("\tIP Address : %s%n", device.ipAddress) + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java deleted file mode 100644 index ed5eca264a6106..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.discover; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; -import com.matter.controller.commands.common.MatterCommand; - -public final class DiscoverCommissionersCommand extends MatterCommand { - public DiscoverCommissionersCommand( - ChipDeviceController controller, CredentialsIssuer credsIssuer) { - super(controller, "commissioners", credsIssuer); - } - - @Override - protected final void runCommand() { - // mCommissionableNodeController.DiscoverCommissioners(); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt new file mode 100644 index 00000000000000..9efefcfb77c73c --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.discover + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer +import com.matter.controller.commands.common.MatterCommand + +class DiscoverCommissionersCommand( + controller: ChipDeviceController, credsIssuer: CredentialsIssuer? +) : MatterCommand(controller, "commissioners", credsIssuer) { + override fun runCommand() { + // mCommissionableNodeController.DiscoverCommissioners(); + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/CloseSessionCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/CloseSessionCommand.java deleted file mode 100644 index f8b911217594ec..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/CloseSessionCommand.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; -import com.matter.controller.commands.common.MatterCommand; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; - -public final class CloseSessionCommand extends MatterCommand { - private final AtomicLong mDestinationId = new AtomicLong(); - private final AtomicInteger mTimeoutSecs = new AtomicInteger(); - - public CloseSessionCommand(ChipDeviceController controller, CredentialsIssuer credsIssuer) { - super(controller, "close-session", credsIssuer); - addArgument("destination-id", 0, Long.MAX_VALUE, mDestinationId, null, false); - addArgument( - "timeout", - (short) 0, - Short.MAX_VALUE, - mTimeoutSecs, - "Time, in seconds, before this command is considered to have timed out.", - false); - } - - @Override - protected final void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/CloseSessionCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/CloseSessionCommand.kt new file mode 100644 index 00000000000000..8cb1567f041d80 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/CloseSessionCommand.kt @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer +import com.matter.controller.commands.common.MatterCommand +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong + +class CloseSessionCommand(controller: ChipDeviceController, credsIssuer: CredentialsIssuer?) : + MatterCommand(controller, "close-session", credsIssuer) { + private val mDestinationId: AtomicLong = AtomicLong() + private val mTimeoutSecs: AtomicInteger = AtomicInteger() + + init { + addArgument("destination-id", 0, Long.MAX_VALUE, mDestinationId, null, false) + addArgument( + "timeout", 0.toShort(), Short.MAX_VALUE, + mTimeoutSecs, + "Time, in seconds, before this command is considered to have timed out.", + false + ) + } + + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.kt similarity index 68% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.kt index e3ecccc923d1dc..8e74dac15bb5b4 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/ArgumentType.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.kt @@ -15,26 +15,8 @@ * limitations under the License. * */ +package com.matter.controller.commands.pairing -package com.matter.controller.commands.common; - -public enum ArgumentType { - NUMBER_INT8, - NUMBER_INT16, - NUMBER_INT32, - NUMBER_INT64, - FLOAT, - DOUBLE, - BOOL, - STRING, - CHARSTRING, - OCTETSTRING, - ATTRIBUTE, - ADDRESS, - COMPLEX, - CUSTOM, - VECTOR_BOOL, - VECTOR16, - VECTOR32, - VECTOR_CUSTOM, -} +enum class DiscoveryFilterType { + NONE, SHORT_DISCRIMINATOR, LONG_DISCRIMINATOR, VENDOR_ID, DEVICE_TYPE, COMMISSIONING_MODE, INSTANCE_NAME, COMMISSIONER, COMPRESSED_FABRIC_ID +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.java deleted file mode 100644 index 29d4e2c58622a7..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; -import java.util.logging.Level; -import java.util.logging.Logger; - -public final class PairAddressPaseCommand extends PairingCommand { - private static Logger logger = Logger.getLogger(PairAddressPaseCommand.class.getName()); - - public PairAddressPaseCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "address-paseonly", - PairingModeType.ADDRESS_PASE_ONLY, - PairingNetworkType.NONE, - credsIssue); - } - - @Override - public void onPairingComplete(int errorCode) { - logger.log(Level.INFO, "onPairingComplete with error code: " + errorCode); - if (errorCode == 0) { - setSuccess(); - } else { - setFailure("onPairingComplete failure"); - } - } - - @Override - protected void runCommand() { - currentCommissioner() - .establishPaseConnection( - getNodeId(), getRemoteAddr().getHostAddress(), getRemotePort(), getSetupPINCode()); - - currentCommissioner().setCompletionListener(this); - waitCompleteMs(getTimeoutMillis()); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.kt new file mode 100644 index 00000000000000..6346aedb68dec0 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAddressPaseCommand.kt @@ -0,0 +1,37 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer +import java.util.logging.Level +import java.util.logging.Logger + +class PairAddressPaseCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : PairingCommand( + controller, + "address-paseonly", + PairingModeType.ADDRESS_PASE_ONLY, + PairingNetworkType.NONE, + credsIssue +) { + private val logger = Logger.getLogger(PairAddressPaseCommand::class.java.name) + + override fun onPairingComplete(errorCode: Int) { + logger.log(Level.INFO, "onPairingComplete with error code: $errorCode") + if (errorCode == 0) { + setSuccess() + } else { + setFailure("onPairingComplete failure") + } + } + + override fun runCommand() { + currentCommissioner() + .establishPaseConnection( + getNodeId(), + getRemoteAddr().getHostAddress(), + getRemotePort(), + getSetupPINCode() + ) + currentCommissioner().setCompletionListener(this) + waitCompleteMs(getTimeoutMillis()) + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.java deleted file mode 100644 index 259bc61629a630..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairAlreadyDiscoveredCommand extends PairingCommand { - public PairAlreadyDiscoveredCommand( - ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "already-discovered", - PairingModeType.ALREADY_DISCOVERED, - PairingNetworkType.NONE, - credsIssue); - } - - @Override - protected void runCommand() { - currentCommissioner() - .pairDeviceWithAddress( - getNodeId(), - getRemoteAddr().getHostAddress(), - getRemotePort(), - getDiscriminator(), - getSetupPINCode(), - null); - currentCommissioner().setCompletionListener(this); - waitCompleteMs(getTimeoutMillis()); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.kt new file mode 100644 index 00000000000000..ef2e5abce28d99 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairAlreadyDiscoveredCommand.kt @@ -0,0 +1,28 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairAlreadyDiscoveredCommand( + controller: ChipDeviceController, credsIssue: CredentialsIssuer? +) : PairingCommand( + controller, + "already-discovered", + PairingModeType.ALREADY_DISCOVERED, + PairingNetworkType.NONE, + credsIssue +) { + override fun runCommand() { + currentCommissioner() + .pairDeviceWithAddress( + getNodeId(), + getRemoteAddr().getHostAddress(), + getRemotePort(), + getDiscriminator(), + getSetupPINCode(), + null + ) + currentCommissioner().setCompletionListener(this) + waitCompleteMs(getTimeoutMillis()) + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeCommand.java deleted file mode 100644 index a95237b1c89a1f..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeCommand.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairCodeCommand extends PairingCommand { - public PairCodeCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super(controller, "code", PairingModeType.CODE, PairingNetworkType.NONE, credsIssue); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeCommand.kt new file mode 100644 index 00000000000000..65a3a1379af8ba --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeCommand.kt @@ -0,0 +1,9 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairCodeCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : + PairingCommand(controller, "code", PairingModeType.CODE, PairingNetworkType.NONE, credsIssue) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.java deleted file mode 100644 index b56ce3a96b4b5a..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairCodePaseCommand extends PairingCommand { - public PairCodePaseCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "code-paseonly", - PairingModeType.CODE_PASE_ONLY, - PairingNetworkType.NONE, - credsIssue); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.kt new file mode 100644 index 00000000000000..98f07984021b90 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodePaseCommand.kt @@ -0,0 +1,14 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairCodePaseCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : PairingCommand( + controller, + "code-paseonly", + PairingModeType.CODE_PASE_ONLY, + PairingNetworkType.NONE, + credsIssue +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.java deleted file mode 100644 index 150ca17e8a42a9..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairCodeThreadCommand extends PairingCommand { - public PairCodeThreadCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super(controller, "code-thread", PairingModeType.CODE, PairingNetworkType.THREAD, credsIssue); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.kt new file mode 100644 index 00000000000000..1db7ea5f975ca0 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeThreadCommand.kt @@ -0,0 +1,9 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairCodeThreadCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : + PairingCommand(controller, "code-thread", PairingModeType.CODE, PairingNetworkType.THREAD, credsIssue) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.java deleted file mode 100644 index 2aa764026a3303..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairCodeWifiCommand extends PairingCommand { - public PairCodeWifiCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super(controller, "code-wifi", PairingModeType.CODE, PairingNetworkType.WIFI, credsIssue); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.kt new file mode 100644 index 00000000000000..55f924c23e69fe --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairCodeWifiCommand.kt @@ -0,0 +1,9 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairCodeWifiCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : + PairingCommand(controller, "code-wifi", PairingModeType.CODE, PairingNetworkType.WIFI, credsIssue) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.java deleted file mode 100644 index de03adcd7e6850..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkCommand extends PairingCommand { - public PairOnNetworkCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super(controller, "onnetwork", PairingModeType.ON_NETWORK, PairingNetworkType.NONE, credsIssue); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.kt new file mode 100644 index 00000000000000..3d433e297a42cb --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommand.kt @@ -0,0 +1,9 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : + PairingCommand(controller, "onnetwork", PairingModeType.ON_NETWORK, PairingNetworkType.NONE, credsIssue) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.java deleted file mode 100644 index 2a737503d8e345..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkCommissionerCommand extends PairingCommand { - public PairOnNetworkCommissionerCommand( - ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-commissioner", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.COMMISSIONER); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.kt new file mode 100644 index 00000000000000..a12bf67e7d2931 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissionerCommand.kt @@ -0,0 +1,17 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkCommissionerCommand( + controller: ChipDeviceController, credsIssue: CredentialsIssuer? +) : PairingCommand( + controller, + "onnetwork-commissioner", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.COMMISSIONER +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.java deleted file mode 100644 index 62a3c14213bc86..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkCommissioningModeCommand extends PairingCommand { - public PairOnNetworkCommissioningModeCommand( - ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-commissioning-mode", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.COMMISSIONING_MODE); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.kt new file mode 100644 index 00000000000000..23459b7d280d3a --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkCommissioningModeCommand.kt @@ -0,0 +1,17 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkCommissioningModeCommand( + controller: ChipDeviceController, credsIssue: CredentialsIssuer? +) : PairingCommand( + controller, + "onnetwork-commissioning-mode", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.COMMISSIONING_MODE +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.java deleted file mode 100644 index 5a16ae3daf908e..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkDeviceTypeCommand extends PairingCommand { - public PairOnNetworkDeviceTypeCommand( - ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-device-type", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.DEVICE_TYPE); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.kt new file mode 100644 index 00000000000000..bdbc1fde8ad32d --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkDeviceTypeCommand.kt @@ -0,0 +1,17 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkDeviceTypeCommand( + controller: ChipDeviceController, credsIssue: CredentialsIssuer? +) : PairingCommand( + controller, + "onnetwork-device-type", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.DEVICE_TYPE +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.java deleted file mode 100644 index c0db4cdc008f77..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; -import java.net.UnknownHostException; - -public final class PairOnNetworkFabricCommand extends PairingCommand { - public PairOnNetworkFabricCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) - throws UnknownHostException { - super( - controller, - "onnetwork-fabric", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.COMPRESSED_FABRIC_ID); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.kt new file mode 100644 index 00000000000000..5e0ef1a4cd447f --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkFabricCommand.kt @@ -0,0 +1,15 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkFabricCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : PairingCommand( + controller, + "onnetwork-fabric", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.COMPRESSED_FABRIC_ID +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.java deleted file mode 100644 index 45d91665dd3671..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkInstanceNameCommand extends PairingCommand { - public PairOnNetworkInstanceNameCommand( - ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-instance-name", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.INSTANCE_NAME); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.kt new file mode 100644 index 00000000000000..6f14a7209323db --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkInstanceNameCommand.kt @@ -0,0 +1,17 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkInstanceNameCommand( + controller: ChipDeviceController, credsIssue: CredentialsIssuer? +) : PairingCommand( + controller, + "onnetwork-instance-name", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.INSTANCE_NAME +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java deleted file mode 100644 index a75b36a991bd0a..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkLongCommand extends PairingCommand { - private static final int MATTER_PORT = 5540; - - public PairOnNetworkLongCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-long", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.LONG_DISCRIMINATOR); - } - - @Override - protected void runCommand() { - currentCommissioner() - .pairDeviceWithAddress( - getNodeId(), - getRemoteAddr().getHostAddress(), - MATTER_PORT, - getDiscriminator(), - getSetupPINCode(), - null); - currentCommissioner().setCompletionListener(this); - waitCompleteMs(getTimeoutMillis()); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.kt new file mode 100644 index 00000000000000..868240d72a6f7f --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.kt @@ -0,0 +1,29 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkLongCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : PairingCommand( + controller, + "onnetwork-long", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.LONG_DISCRIMINATOR +) { + private val MATTER_PORT = 5540 + + override fun runCommand() { + currentCommissioner() + .pairDeviceWithAddress( + getNodeId(), + getRemoteAddr().getHostAddress(), + MATTER_PORT, + getDiscriminator(), + getSetupPINCode(), + null + ) + currentCommissioner().setCompletionListener(this) + waitCompleteMs(getTimeoutMillis()) + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.java deleted file mode 100644 index 135694632863d1..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkShortCommand extends PairingCommand { - public PairOnNetworkShortCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-short", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.SHORT_DISCRIMINATOR); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.kt new file mode 100644 index 00000000000000..61a999528bad6b --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkShortCommand.kt @@ -0,0 +1,15 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkShortCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : PairingCommand( + controller, + "onnetwork-short", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.SHORT_DISCRIMINATOR +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.java deleted file mode 100644 index 65bdfc57c3cd94..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class PairOnNetworkVendorCommand extends PairingCommand { - public PairOnNetworkVendorCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super( - controller, - "onnetwork-vendor", - PairingModeType.ON_NETWORK, - PairingNetworkType.NONE, - credsIssue, - DiscoveryFilterType.VENDOR_ID); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.kt new file mode 100644 index 00000000000000..05a80aeaed12fb --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkVendorCommand.kt @@ -0,0 +1,15 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class PairOnNetworkVendorCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : PairingCommand( + controller, + "onnetwork-vendor", + PairingModeType.ON_NETWORK, + PairingNetworkType.NONE, + credsIssue, + DiscoveryFilterType.VENDOR_ID +) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java deleted file mode 100644 index 4aa95356a05f54..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java +++ /dev/null @@ -1,249 +0,0 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; -import com.matter.controller.commands.common.IPAddress; -import com.matter.controller.commands.common.MatterCommand; -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; -import java.util.logging.Level; -import java.util.logging.Logger; - -public abstract class PairingCommand extends MatterCommand - implements ChipDeviceController.CompletionListener { - private PairingModeType mPairingMode = PairingModeType.NONE; - private PairingNetworkType mNetworkType = PairingNetworkType.NONE; - private DiscoveryFilterType mFilterType = DiscoveryFilterType.NONE; - private final IPAddress mRemoteAddr; - private final AtomicLong mNodeId = new AtomicLong(); - private final AtomicLong mDiscoveryFilterCode = new AtomicLong(); - private final AtomicLong mTimeoutMillis = new AtomicLong(); - private final AtomicBoolean mDiscoverOnce = new AtomicBoolean(); - private final AtomicBoolean mUseOnlyOnNetworkDiscovery = new AtomicBoolean(); - private final AtomicInteger mRemotePort = new AtomicInteger(); - private final AtomicInteger mDiscriminator = new AtomicInteger(); - private final AtomicInteger mSetupPINCode = new AtomicInteger(); - private final StringBuffer mOperationalDataset = new StringBuffer(); - private final StringBuffer mSSID = new StringBuffer(); - private final StringBuffer mPassword = new StringBuffer(); - private final StringBuffer mOnboardingPayload = new StringBuffer(); - private final StringBuffer mDiscoveryFilterInstanceName = new StringBuffer(); - private static Logger logger = Logger.getLogger(PairingCommand.class.getName()); - - @Override - public void onConnectDeviceComplete() { - logger.log(Level.INFO, "onConnectDeviceComplete"); - } - - @Override - public void onStatusUpdate(int status) { - logger.log(Level.INFO, "onStatusUpdate with status: " + status); - } - - @Override - public void onPairingComplete(int errorCode) { - logger.log(Level.INFO, "onPairingComplete with error code: " + errorCode); - if (errorCode != 0) { - setFailure("onPairingComplete failure"); - } - } - - @Override - public void onPairingDeleted(int errorCode) { - logger.log(Level.INFO, "onPairingDeleted with error code: " + errorCode); - } - - @Override - public void onCommissioningComplete(long nodeId, int errorCode) { - logger.log(Level.INFO, "onCommissioningComplete with error code: " + errorCode); - if (errorCode == 0) { - setSuccess(); - } else { - setFailure("onCommissioningComplete failure"); - } - } - - @Override - public void onReadCommissioningInfo( - int vendorId, int productId, int wifiEndpointId, int threadEndpointId) { - logger.log(Level.INFO, "onReadCommissioningInfo"); - } - - @Override - public void onCommissioningStatusUpdate(long nodeId, String stage, int errorCode) { - logger.log(Level.INFO, "onCommissioningStatusUpdate"); - } - - @Override - public void onNotifyChipConnectionClosed() { - logger.log(Level.INFO, "onNotifyChipConnectionClosed"); - } - - @Override - public void onCloseBleComplete() { - logger.log(Level.INFO, "onCloseBleComplete"); - } - - @Override - public void onError(Throwable error) { - setFailure(error.toString()); - logger.log(Level.INFO, "onError with error: " + error.toString()); - } - - @Override - public void onOpCSRGenerationComplete(byte[] csr) { - logger.log(Level.INFO, "onOpCSRGenerationComplete"); - for (int i = 0; i < csr.length; i++) { - System.out.print(csr[i] + " "); - } - } - - public long getNodeId() { - return mNodeId.get(); - } - - public IPAddress getRemoteAddr() { - return mRemoteAddr; - } - - public int getRemotePort() { - return mRemotePort.get(); - } - - public int getSetupPINCode() { - return mSetupPINCode.get(); - } - - public int getDiscriminator() { - return mDiscriminator.get(); - } - - public long getTimeoutMillis() { - return mTimeoutMillis.get(); - } - - public PairingCommand( - ChipDeviceController controller, - String commandName, - PairingModeType mode, - PairingNetworkType networkType, - CredentialsIssuer credsIssuer) { - this(controller, commandName, mode, networkType, credsIssuer, DiscoveryFilterType.NONE); - } - - public PairingCommand( - ChipDeviceController controller, - String commandName, - PairingModeType mode, - PairingNetworkType networkType, - CredentialsIssuer credsIssuer, - DiscoveryFilterType filterType) { - super(controller, commandName, credsIssuer); - this.mPairingMode = mode; - this.mNetworkType = networkType; - this.mFilterType = filterType; - - try { - this.mRemoteAddr = new IPAddress(InetAddress.getByName("::1")); - } catch (UnknownHostException e) { - throw new RuntimeException(e); - } - - addArgument("node-id", 0, Long.MAX_VALUE, mNodeId, null, false); - - switch (networkType) { - case NONE: - break; - case WIFI: - addArgument("ssid", mSSID, null, false); - addArgument("password", mPassword, null, false); - break; - case THREAD: - addArgument("operationalDataset", mOperationalDataset, null, false); - break; - } - - switch (mode) { - case NONE: - break; - case CODE: - case CODE_PASE_ONLY: - addArgument("payload", mOnboardingPayload, null, false); - addArgument("discover-once", mDiscoverOnce, null, false); - addArgument("use-only-onnetwork-discovery", mUseOnlyOnNetworkDiscovery, null, false); - break; - case ADDRESS_PASE_ONLY: - addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false); - addArgument("device-remote-ip", mRemoteAddr, false); - addArgument("device-remote-port", (short) 0, Short.MAX_VALUE, mRemotePort, null, false); - break; - case BLE: - addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false); - addArgument("discriminator", (short) 0, (short) 4096, mDiscriminator, null, false); - break; - case ON_NETWORK: - addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false); - break; - case SOFT_AP: - addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false); - addArgument("discriminator", (short) 0, (short) 4096, mDiscriminator, null, false); - addArgument("device-remote-ip", mRemoteAddr, false); - addArgument("device-remote-port", (short) 0, Short.MAX_VALUE, mRemotePort, null, false); - break; - case ALREADY_DISCOVERED: - addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false); - addArgument("device-remote-ip", mRemoteAddr, false); - addArgument("device-remote-port", (short) 0, Short.MAX_VALUE, mRemotePort, null, false); - break; - } - - switch (filterType) { - case NONE: - break; - case SHORT_DISCRIMINATOR: - addArgument("discriminator", (short) 0, (short) 4096, mDiscriminator, null, false); - break; - case LONG_DISCRIMINATOR: - addArgument("discriminator", (short) 0, (short) 4096, mDiscriminator, null, false); - break; - case VENDOR_ID: - addArgument("vendor-id", (short) 0, Short.MAX_VALUE, mDiscoveryFilterCode, null, false); - break; - case COMPRESSED_FABRIC_ID: - addArgument("fabric-id", 0, Long.MAX_VALUE, mDiscoveryFilterCode, null, false); - break; - case COMMISSIONING_MODE: - case COMMISSIONER: - break; - case DEVICE_TYPE: - addArgument("device-type", (short) 0, Short.MAX_VALUE, mDiscoveryFilterCode, null, false); - break; - case INSTANCE_NAME: - addArgument("name", mDiscoveryFilterInstanceName, null, false); - break; - } - - addArgument("timeout", (long) 0, Long.MAX_VALUE, mTimeoutMillis, null, false); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.kt new file mode 100644 index 00000000000000..32ddff8e37edcf --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.kt @@ -0,0 +1,253 @@ +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer +import com.matter.controller.commands.common.IPAddress +import com.matter.controller.commands.common.MatterCommand +import java.net.InetAddress +import java.net.UnknownHostException +import java.util.concurrent.atomic.AtomicBoolean +import java.util.concurrent.atomic.AtomicInteger +import java.util.concurrent.atomic.AtomicLong +import java.util.logging.Level +import java.util.logging.Logger + +abstract class PairingCommand( + controller: ChipDeviceController, + commandName: String?, + mode: PairingModeType, + networkType: PairingNetworkType, + credsIssuer: CredentialsIssuer?, + filterType: DiscoveryFilterType +) : MatterCommand(controller, commandName, credsIssuer), ChipDeviceController.CompletionListener { + private val logger = Logger.getLogger(PairingCommand::class.java.name) + private var mPairingMode: PairingModeType = PairingModeType.NONE + private var mNetworkType: PairingNetworkType = PairingNetworkType.NONE + private var mFilterType = DiscoveryFilterType.NONE + private var mRemoteAddr: IPAddress? = null + private val mNodeId: AtomicLong = AtomicLong() + private val mDiscoveryFilterCode: AtomicLong = AtomicLong() + private val mTimeoutMillis: AtomicLong = AtomicLong() + private val mDiscoverOnce: AtomicBoolean = AtomicBoolean() + private val mUseOnlyOnNetworkDiscovery: AtomicBoolean = AtomicBoolean() + private val mRemotePort: AtomicInteger = AtomicInteger() + private val mDiscriminator: AtomicInteger = AtomicInteger() + private val mSetupPINCode: AtomicLong = AtomicLong() + private val mOperationalDataset = StringBuffer() + private val mSSID = StringBuffer() + private val mPassword = StringBuffer() + private val mOnboardingPayload = StringBuffer() + private val mDiscoveryFilterInstanceName = StringBuffer() + + constructor( + controller: ChipDeviceController, + commandName: String?, + mode: PairingModeType, + networkType: PairingNetworkType, + credsIssuer: CredentialsIssuer? + ) : this(controller, commandName, mode, networkType, credsIssuer, DiscoveryFilterType.NONE) + + init { + mPairingMode = mode + mNetworkType = networkType + mFilterType = filterType + try { + mRemoteAddr = IPAddress(InetAddress.getByName("::1")) + } catch (e: UnknownHostException) { + throw RuntimeException(e) + } + addArgument("node-id", 0, Long.MAX_VALUE, mNodeId, null, false) + when (networkType) { + PairingNetworkType.NONE -> {} + PairingNetworkType.WIFI -> { + addArgument("ssid", mSSID, null, false) + addArgument("password", mPassword, null, false) + } + + PairingNetworkType.THREAD -> addArgument("operationalDataset", mOperationalDataset, null, false) + } + when (mode) { + PairingModeType.NONE -> {} + PairingModeType.CODE, PairingModeType.CODE_PASE_ONLY -> { + addArgument("payload", mOnboardingPayload, null, false) + addArgument("discover-once", mDiscoverOnce, null, false) + addArgument("use-only-onnetwork-discovery", mUseOnlyOnNetworkDiscovery, null, false) + } + + PairingModeType.ADDRESS_PASE_ONLY -> { + addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false) + addArgument("device-remote-ip", mRemoteAddr, false) + addArgument("device-remote-port", 0.toShort(), Short.MAX_VALUE, mRemotePort, null, false) + } + + PairingModeType.BLE -> { + addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false) + addArgument("discriminator", 0.toShort(), 4096.toShort(), mDiscriminator, null, false) + } + + PairingModeType.ON_NETWORK -> addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false) + PairingModeType.SOFT_AP -> { + addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false) + addArgument("discriminator", 0.toShort(), 4096.toShort(), mDiscriminator, null, false) + addArgument("device-remote-ip", mRemoteAddr, false) + addArgument("device-remote-port", 0.toShort(), Short.MAX_VALUE, mRemotePort, null, false) + } + + PairingModeType.ALREADY_DISCOVERED -> { + addArgument("setup-pin-code", 0, 134217727, mSetupPINCode, null, false) + addArgument("device-remote-ip", mRemoteAddr, false) + addArgument("device-remote-port", 0.toShort(), Short.MAX_VALUE, mRemotePort, null, false) + } + } + when (filterType) { + DiscoveryFilterType.NONE -> {} + DiscoveryFilterType.SHORT_DISCRIMINATOR -> addArgument( + "discriminator", + 0.toShort(), + 4096.toShort(), + mDiscriminator, + null, + false + ) + + DiscoveryFilterType.LONG_DISCRIMINATOR -> addArgument( + "discriminator", + 0.toShort(), + 4096.toShort(), + mDiscriminator, + null, + false + ) + + DiscoveryFilterType.VENDOR_ID -> addArgument( + "vendor-id", + 0, + Long.MAX_VALUE, + mDiscoveryFilterCode, + null, + false + ) + + DiscoveryFilterType.COMPRESSED_FABRIC_ID -> addArgument( + "fabric-id", + 0, + Long.MAX_VALUE, + mDiscoveryFilterCode, + null, + false + ) + + DiscoveryFilterType.COMMISSIONING_MODE, DiscoveryFilterType.COMMISSIONER -> {} + DiscoveryFilterType.DEVICE_TYPE -> addArgument( + "device-type", + 0, + Long.MAX_VALUE, + mDiscoveryFilterCode, + null, + false + ) + + DiscoveryFilterType.INSTANCE_NAME -> addArgument("name", mDiscoveryFilterInstanceName, null, false) + } + addArgument("timeout", 0L, Long.MAX_VALUE, mTimeoutMillis, null, false) + } + + override fun onConnectDeviceComplete() { + logger.log(Level.INFO, "onConnectDeviceComplete") + } + + override fun onStatusUpdate(status: Int) { + logger.log(Level.INFO, "onStatusUpdate with status: $status") + } + + override fun onPairingComplete(errorCode: Int) { + logger.log(Level.INFO, "onPairingComplete with error code: $errorCode") + if (errorCode != 0) { + setFailure("onPairingComplete failure") + } + } + + override fun onPairingDeleted(errorCode: Int) { + logger.log(Level.INFO, "onPairingDeleted with error code: $errorCode") + } + + override fun onCommissioningComplete(nodeId: Long, errorCode: Int) { + logger.log(Level.INFO, "onCommissioningComplete with error code: $errorCode") + if (errorCode == 0) { + setSuccess() + } else { + setFailure("onCommissioningComplete failure") + } + } + + override fun onReadCommissioningInfo( + vendorId: Int, productId: Int, wifiEndpointId: Int, threadEndpointId: Int + ) { + logger.log(Level.INFO, "onReadCommissioningInfo") + } + + override fun onCommissioningStatusUpdate(nodeId: Long, stage: String?, errorCode: Int) { + logger.log(Level.INFO, "onCommissioningStatusUpdate") + } + + override fun onNotifyChipConnectionClosed() { + logger.log(Level.INFO, "onNotifyChipConnectionClosed") + } + + override fun onCloseBleComplete() { + logger.log(Level.INFO, "onCloseBleComplete") + } + + override fun onError(error: Throwable) { + setFailure(error.toString()) + logger.log(Level.INFO, "onError with error: $error") + } + + override fun onOpCSRGenerationComplete(csr: ByteArray) { + logger.log(Level.INFO, "onOpCSRGenerationComplete") + for (i in csr.indices) { + print(csr[i].toString() + " ") + } + } + + fun getNodeId(): Long { + return mNodeId.get() + } + + fun getRemoteAddr(): IPAddress { + return mRemoteAddr!! + } + + fun getRemotePort(): Int { + return mRemotePort.get() + } + + fun getSetupPINCode(): Long { + return mSetupPINCode.get() + } + + fun getDiscriminator(): Int { + return mDiscriminator.get() + } + + fun getTimeoutMillis(): Long { + return mTimeoutMillis.get() + } +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingModeType.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingModeType.kt similarity index 74% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingModeType.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingModeType.kt index 63556f7b3bfe82..55c8214b6aa7f1 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingModeType.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingModeType.kt @@ -1,30 +1,22 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.pairing; - -public enum PairingModeType { - NONE, - CODE, - CODE_PASE_ONLY, - ADDRESS_PASE_ONLY, - BLE, - SOFT_AP, - ALREADY_DISCOVERED, - ON_NETWORK; -} +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.pairing + +enum class PairingModeType { + NONE, CODE, CODE_PASE_ONLY, ADDRESS_PASE_ONLY, BLE, SOFT_AP, ALREADY_DISCOVERED, ON_NETWORK +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingNetworkType.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingNetworkType.kt similarity index 82% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingNetworkType.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingNetworkType.kt index 650fad5f887552..8a179c87dfe567 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingNetworkType.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingNetworkType.kt @@ -1,25 +1,22 @@ -/* - * Copyright (c) 2022 Project CHIP Authors - * All rights reserved. - * - * 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 com.matter.controller.commands.pairing; - -public enum PairingNetworkType { - NONE, - WIFI, - THREAD, -} +/* + * Copyright (c) 2022 Project CHIP Authors + * All rights reserved. + * + * 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 com.matter.controller.commands.pairing + +enum class PairingNetworkType { + NONE, WIFI, THREAD +} \ No newline at end of file diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.java deleted file mode 100644 index 2c33c8dc551a1b..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.matter.controller.commands.pairing; - -import chip.devicecontroller.ChipDeviceController; -import com.matter.controller.commands.common.CredentialsIssuer; - -public final class UnpairCommand extends PairingCommand { - public UnpairCommand(ChipDeviceController controller, CredentialsIssuer credsIssue) { - super(controller, "unpair", PairingModeType.NONE, PairingNetworkType.NONE, credsIssue); - } - - @Override - protected void runCommand() {} -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.kt new file mode 100644 index 00000000000000..2b1c0d7332cbb0 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/UnpairCommand.kt @@ -0,0 +1,9 @@ +package com.matter.controller.commands.pairing + +import chip.devicecontroller.ChipDeviceController +import com.matter.controller.commands.common.CredentialsIssuer + +class UnpairCommand(controller: ChipDeviceController, credsIssue: CredentialsIssuer?) : + PairingCommand(controller, "unpair", PairingModeType.NONE, PairingNetworkType.NONE, credsIssue) { + override fun runCommand() {} +} \ No newline at end of file diff --git a/third_party/java_deps/BUILD.gn b/third_party/java_deps/BUILD.gn index ffcd48dfe5fe3c..434d205c6bbb6f 100644 --- a/third_party/java_deps/BUILD.gn +++ b/third_party/java_deps/BUILD.gn @@ -24,3 +24,7 @@ java_prebuilt("annotation") { java_prebuilt("json") { jar_path = "artifacts/json-20220924.jar" } + +java_prebuilt("kotlin-stdlib") { + jar_path = "artifacts/kotlin-stdlib-1.3.31.jar" +} diff --git a/third_party/java_deps/set_up_java_deps.sh b/third_party/java_deps/set_up_java_deps.sh index e0210b3bfacdff..8f9699d941c7c8 100755 --- a/third_party/java_deps/set_up_java_deps.sh +++ b/third_party/java_deps/set_up_java_deps.sh @@ -19,3 +19,4 @@ mkdir -p third_party/java_deps/artifacts curl --fail --location --silent --show-error https://repo1.maven.org/maven2/com/google/code/findbugs/jsr305/3.0.2/jsr305-3.0.2.jar -o third_party/java_deps/artifacts/jsr305-3.0.2.jar curl --fail --location --silent --show-error https://repo1.maven.org/maven2/org/json/json/20220924/json-20220924.jar -o third_party/java_deps/artifacts/json-20220924.jar +curl --fail --location --silent --show-error https://repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/1.3.31/kotlin-stdlib-1.3.31.jar -o third_party/java_deps/artifacts/kotlin-stdlib-1.3.31.jar