Skip to content

Commit

Permalink
[java-matter-controller] Convert from java to kotlin phase II (#25491)
Browse files Browse the repository at this point in the history
* [java-matter-controller] Convert from java to kotlin phase II

* Address review comments
  • Loading branch information
yufengwangca authored and pull[bot] committed Sep 14, 2023
1 parent 42a552b commit 9110132
Show file tree
Hide file tree
Showing 47 changed files with 1,158 additions and 920 deletions.
46 changes: 23 additions & 23 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,6 @@ java_library("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/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/PairOnNetworkLongImInvokeCommand.java",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImWriteCommand.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" ]
Expand All @@ -76,6 +53,29 @@ kotlin_binary("java-matter-controller") {
"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/PairOnNetworkLongImInvokeCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImWriteCommand.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" ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,13 @@ fun main(args: Array<String>) {
commandManager.register("discover", getDiscoveryCommands(controller, credentialsIssuer))
commandManager.register("pairing", getPairingCommands(controller, credentialsIssuer))
commandManager.register("im", getImCommands(controller, credentialsIssuer))

try {
commandManager.run(args)
} catch (e: Exception) {
println("Run command failed with exception: " + e.message)
System.exit(1)
}

controller.shutdownCommissioning()
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,22 @@ public final void addArgument(
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, short min, short max, AtomicLong 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,9 @@ import com.matter.controller.commands.common.CredentialsIssuer
import com.matter.controller.commands.common.MatterCommand
import java.util.concurrent.TimeUnit

private const val MAX_DISCOVERED_DEVICES = 10
private const val TIME_TO_WAIT_FOR_RESULTS_SECONDS = 7L

class DiscoverCommissionablesCommand(
controller: ChipDeviceController, credsIssuer: CredentialsIssuer?
) : MatterCommand(controller, "commissionables", credsIssuer) {

override fun runCommand() {
currentCommissioner().discoverCommissionableNodes()

Expand All @@ -55,4 +51,9 @@ class DiscoverCommissionablesCommand(
System.out.format("\tDiscriminator: %d", device.discriminator)
System.out.format("\tIP Address : %s%n", device.ipAddress)
}

companion object {
private const val MAX_DISCOVERED_DEVICES = 10
private const val TIME_TO_WAIT_FOR_RESULTS_SECONDS = 7L
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.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 destinationId: AtomicLong = AtomicLong()
private val timeoutSecs: AtomicInteger = AtomicInteger()

init {
addArgument("destination-id", 0, Long.MAX_VALUE, destinationId, null, false)
addArgument(
"timeout", 0.toShort(), Short.MAX_VALUE,
timeoutSecs,
"Time, in seconds, before this command is considered to have timed out.",
false
)
}

override fun runCommand() {}
}
Original file line number Diff line number Diff line change
@@ -1,31 +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.pairing;

public enum DiscoveryFilterType {
NONE,
SHORT_DISCRIMINATOR,
LONG_DISCRIMINATOR,
VENDOR_ID,
DEVICE_TYPE,
COMMISSIONING_MODE,
INSTANCE_NAME,
COMMISSIONER,
COMPRESSED_FABRIC_ID,
}
/*
* 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.pairing

enum class DiscoveryFilterType {
NONE,
SHORT_DISCRIMINATOR,
LONG_DISCRIMINATOR,
VENDOR_ID,
DEVICE_TYPE,
COMMISSIONING_MODE,
INSTANCE_NAME,
COMMISSIONER,
COMPRESSED_FABRIC_ID
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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.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",
credsIssue,
PairingModeType.ADDRESS_PASE_ONLY,
PairingNetworkType.NONE
) {
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())
}

companion object {
private val logger = Logger.getLogger(PairAddressPaseCommand::class.java.name)
}
}
Loading

0 comments on commit 9110132

Please sign in to comment.