From d1ca1cccbce6a88d93818586861a478ad047d767 Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 28 Feb 2023 16:04:32 -0800 Subject: [PATCH] [java-matter-controller] Convert from java to kotlin phase I --- .github/workflows/tests.yaml | 2 +- examples/java-matter-controller/BUILD.gn | 25 +++- examples/java-matter-controller/Manifest.txt | 4 +- examples/java-matter-controller/README.md | 42 ++++++ .../java/src/com/matter/controller/Main.java | 125 ------------------ .../java/src/com/matter/controller/Main.kt | 100 ++++++++++++++ .../commands/discover/DiscoverCommand.java | 42 ------ .../commands/discover/DiscoverCommand.kt | 36 +++++ .../DiscoverCommissionablesCommand.java | 65 --------- .../DiscoverCommissionablesCommand.kt | 56 ++++++++ ...d.java => DiscoverCommissionersCommand.kt} | 65 +++++---- 11 files changed, 286 insertions(+), 276 deletions(-) delete mode 100644 examples/java-matter-controller/java/src/com/matter/controller/Main.java create mode 100644 examples/java-matter-controller/java/src/com/matter/controller/Main.kt delete mode 100644 examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.java create mode 100644 examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.kt delete mode 100644 examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java create mode 100644 examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt rename examples/java-matter-controller/java/src/com/matter/controller/commands/discover/{DiscoverCommissionersCommand.java => DiscoverCommissionersCommand.kt} (53%) diff --git a/.github/workflows/tests.yaml b/.github/workflows/tests.yaml index 8fb92717e6c65b..d8f325679f3723 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.44 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..a8d426cc48f92d 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,7 +27,6 @@ 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", @@ -37,9 +36,6 @@ java_binary("java-matter-controller") { "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", @@ -66,6 +62,23 @@ java_binary("java-matter-controller") { 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/discover/DiscoverCommand.kt", + "java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt", + "java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.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..d221d20b748873 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.8.10.jar diff --git a/examples/java-matter-controller/README.md b/examples/java-matter-controller/README.md index fae126f8b7af10..70e3876943bdd7 100644 --- a/examples/java-matter-controller/README.md +++ b/examples/java-matter-controller/README.md @@ -47,6 +47,48 @@ system. You can install it through the following command as root: sudo apt install default-jdk ``` +You also need to install kotlin compiler on your Ubuntu system: + +kotlin compiler version 1.8.10 or above is needed to compile +java-matter-controller, if you already have lowerr version kotlin compiler +installed on your Ubuntu from apt, +you need to remove the Kotlin compiler package, run the following command: + +``` +sudo apt-get remove kotlin +``` + +Wait for the removal process to complete. Once it's done, the Kotlin compiler +will be removed from your system. + +(Optional) If you want to remove any configuration files associated with Kotlin, +run the following command: + +``` +sudo apt-get purge kotlin +``` + +Install kotlin compiler 1.8.10 + +``` +cd /usr/lib \ +&& sudo wget -q https://github.com/JetBrains/kotlin/releases/download/v1.8.10/kotlin-compiler-1.8.10.zip \ +&& sudo unzip kotlin-compiler-*.zip \ +&& sudo rm kotlin-compiler-*.zip \ +&& sudo rm -f kotlinc/bin/*.bat +``` + +Add a directory to PATH permanently by editing the `.bashrc` file located in the +Home directory. Follow these steps: + +1. Open the .bashrc file using a text editor. +2. Go to the end of the file. +3. Paste the export syntax at the end of the file. + +``` +export PATH="/usr/lib/kotlinc/bin:$PATH" +``` + ### Linux ``` diff --git a/examples/java-matter-controller/java/src/com/matter/controller/Main.java b/examples/java-matter-controller/java/src/com/matter/controller/Main.java deleted file mode 100644 index 59c2e666589365..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/Main.java +++ /dev/null @@ -1,125 +0,0 @@ -/* - * Copyright (c) 2022-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; -import java.util.logging.Level; -import java.util.logging.Logger; - -public class Main { - private static Logger logger = Logger.getLogger(Main.class.getName()); - - private static void registerCommandsDiscover( - ChipDeviceController controller, - CommandManager commandManager, - CredentialsIssuer credentialsIssuer) { - ArrayList clusterCommands = new ArrayList(); - DiscoverCommand discoverCommand = new DiscoverCommand(controller, credentialsIssuer); - DiscoverCommissionablesCommand discoverCommissionablesCommand = - new DiscoverCommissionablesCommand(controller, credentialsIssuer); - DiscoverCommissionersCommand discoverCommissionersCommand = - new DiscoverCommissionersCommand(controller, credentialsIssuer); - clusterCommands.add(discoverCommand); - clusterCommands.add(discoverCommissionablesCommand); - clusterCommands.add(discoverCommissionersCommand); - - commandManager.register("discover", clusterCommands); - } - - private static void registerCommandsPairing( - ChipDeviceController controller, - CommandManager commandManager, - CredentialsIssuer credentialsIssuer) { - ArrayList clusterCommands = new ArrayList(); - UnpairCommand unpairCommand = new UnpairCommand(controller, credentialsIssuer); - PairCodeCommand pairCodeCommand = new PairCodeCommand(controller, credentialsIssuer); - PairCodePaseCommand pairCodePaseCommand = - new PairCodePaseCommand(controller, credentialsIssuer); - PairCodeWifiCommand pairCodeWifiCommand = - new PairCodeWifiCommand(controller, credentialsIssuer); - PairCodeThreadCommand pairCodeThreadCommand = - new PairCodeThreadCommand(controller, credentialsIssuer); - PairAddressPaseCommand pairAddressPaseCommand = - new PairAddressPaseCommand(controller, credentialsIssuer); - PairAlreadyDiscoveredCommand pairAlreadyDiscoveredCommand = - new PairAlreadyDiscoveredCommand(controller, credentialsIssuer); - PairOnNetworkCommand pairOnNetworkCommand = - new PairOnNetworkCommand(controller, credentialsIssuer); - PairOnNetworkShortCommand pairOnNetworkShortCommand = - new PairOnNetworkShortCommand(controller, credentialsIssuer); - PairOnNetworkLongCommand pairOnNetworkLongCommand = - new PairOnNetworkLongCommand(controller, credentialsIssuer); - PairOnNetworkVendorCommand pairOnNetworkVendorCommand = - new PairOnNetworkVendorCommand(controller, credentialsIssuer); - PairOnNetworkCommissioningModeCommand pairOnNetworkCommissioningModeCommand = - new PairOnNetworkCommissioningModeCommand(controller, credentialsIssuer); - PairOnNetworkCommissionerCommand pairOnNetworkCommissionerCommand = - new PairOnNetworkCommissionerCommand(controller, credentialsIssuer); - PairOnNetworkDeviceTypeCommand pairOnNetworkDeviceTypeCommand = - new PairOnNetworkDeviceTypeCommand(controller, credentialsIssuer); - PairOnNetworkInstanceNameCommand pairOnNetworkInstanceNameCommand = - new 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); - } - - public static void main(String[] args) { - ChipDeviceController controller = - new ChipDeviceController( - ControllerParams.newBuilder() - .setUdpListenPort(0) - .setControllerVendorId(0xFFF1) - .setCountryCode("US") - .build()); - - CredentialsIssuer credentialsIssuer = new CredentialsIssuer(); - CommandManager commandManager = new CommandManager(); - - registerCommandsDiscover(controller, commandManager, credentialsIssuer); - registerCommandsPairing(controller, commandManager, credentialsIssuer); - - try { - commandManager.run(args); - } catch (Exception e) { - logger.log(Level.INFO, "Run command failed with exception: " + e.getMessage()); - System.exit(1); - } - controller.shutdownCommissioning(); - } -} 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..49509de3491ae2 --- /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() +} 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..941d527191d6cc --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommand.kt @@ -0,0 +1,36 @@ +/* + * 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.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() {} +} 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..d5f7b156eccebb --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt @@ -0,0 +1,56 @@ +/* + * 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.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) + } +} 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.kt similarity index 53% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt index ed5eca264a6106..efdd7573c86609 100644 --- 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.kt @@ -1,35 +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; - -public final class DiscoverCommissionersCommand extends MatterCommand { - public DiscoverCommissionersCommand( - ChipDeviceController controller, CredentialsIssuer credsIssuer) { - super(controller, "commissioners", credsIssuer); - } - - @Override - protected final void runCommand() { - // mCommissionableNodeController.DiscoverCommissioners(); - } -} +/* + * 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.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(); + } +}