Skip to content

Commit

Permalink
Initial implementation for kotlin-matter-controller (#29636)
Browse files Browse the repository at this point in the history
* Initial implementaton for kotlin-matter-controller

* Address review comments

* Use lock.wait(remainingTime) to allow the thread to wait for the remaining time or until it's notified
  • Loading branch information
yufengwangca authored Oct 11, 2023
1 parent 87c16cf commit fd925e4
Show file tree
Hide file tree
Showing 33 changed files with 2,267 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ darwin-framework-tool/README
java-matter-controller/README
```

## Kotlin matter controller example

```{toctree}
:glob:
:maxdepth: 1
kotlin-matter-controller/README
```

## Virtual Device App example

```{toctree}
Expand Down
20 changes: 20 additions & 0 deletions examples/kotlin-matter-controller/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
*.iml
.gradle
/local.properties
/.idea/caches
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
/.idea/navEditor.xml
/.idea/assetWizardSettings.xml
.DS_Store
/build
/captures
.externalNativeBuild
.cxx
local.properties

# Shared libs & JAR libs (those libs are copied into source tree for easy Android build).
*.so
*.jar
*.map
25 changes: 25 additions & 0 deletions examples/kotlin-matter-controller/.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Copyright (c) 2020-2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")

# The location of the build configuration file.
buildconfig = "${build_root}/config/BUILDCONFIG.gn"

# CHIP uses angle bracket includes.
check_system_includes = true

default_args = {
import("//args.gni")
}
56 changes: 56 additions & 0 deletions examples/kotlin-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright (c) 2022-2023 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/build.gni")
import("//build_overrides/chip.gni")

import("${chip_root}/build/chip/java/rules.gni")
import("${chip_root}/build/chip/tools.gni")

kotlin_binary("kotlin-matter-controller") {
output_name = "kotlin-matter-controller"
deps = [
"${chip_root}/src/controller/java:kotlin_matter_controller",
"${chip_root}/src/controller/java:onboarding_payload",
"${chip_root}/third_party/java_deps:annotation",
]

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/FutureResult.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/common/RealResult.kt",
"java/src/com/matter/controller/commands/pairing/DiscoveryFilterType.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/PairOnNetworkLongImReadCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImSubscribeCommand.kt",
"java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImWriteCommand.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",
]

kotlinc_flags = [ "-Xlint:deprecation" ]
}

group("default") {
deps = [ ":kotlin-matter-controller" ]
}
2 changes: 2 additions & 0 deletions examples/kotlin-matter-controller/Manifest.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Main-Class: com.matter.controller.MainKt
Class-Path: ../lib/third_party/connectedhomeip/src/controller/java/*.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.20.jar ../lib/third_party/connectedhomeip/third_party/java_deps/kotlinx-coroutines-core-jvm-1.7.3.jar
118 changes: 118 additions & 0 deletions examples/kotlin-matter-controller/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Matter Controller Kotlin App Example

This is a Matter Controller Kotlin app that can be used to commission and
control Matter accessory devices.

<hr>

- [Matter Controller Kotlin App Example](#matter-controller-kotlin-app-example)
- [Requirements for building](#requirements-for-building)
- [Preparing for build](#preparing-for-build)
- [Building & Running the app](#building--running-the-app)

<hr>

## Requirements for building

You need to have the following two software installed on your Ubuntu system:

1. Java Runtime Environment (JRE)
2. Java Development Kit (JDK)

openjdk version "11.0.20.1" or above is needed

First, check if we have the Java Runtime Environment and Java Development Kit
installed on our system or not.

```shell
java -version
```

If not, you can install the Java Runtime Environment and Java Development Kit on
your system through the following command as root:

```
sudo apt install default-jre
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
kotlin-matter-controller, if you already have lower version kotlin compiler
installed on your Ubuntu from apt,
you need to remove the Kotlin compiler package, run the following command:

```shell
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:

```shell
sudo apt-get purge kotlin
```

Install kotlin compiler 1.8.10 or above, such as
[kotlin-compiler-1.8.10-url](https://github.com/JetBrains/kotlin/releases/download/v1.8.10/kotlin-compiler-1.8.10.zip)

```shell
cd /usr/lib \
&& sudo wget -q [kotlin-compiler-1.8.10-url] \
&& 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.

```shell
export PATH="/usr/lib/kotlinc/bin:$PATH"
```

### Linux

```shell
export JAVA_PATH=[JDK path]
```

<hr>

## Preparing for build

Complete the following steps to prepare the Matter build:

1. Check out the Matter repository.

2. Run bootstrap (**only required first time**)

```shell
source scripts/bootstrap.sh
```

## Building & Running the app

This is the simplest option. In the command line, run the following command from
the top Matter directory:

```shell
./scripts/build/build_examples.py --target linux-x64-kotlin-matter-controller build
```

The Java executable file `kotlin-matter-controller` will be generated at
`out/android-x86-kotlin-matter-controller/bin/`

Run the kotlin-matter-controller

```shell
java -Djava.library.path=../lib/jni -jar kotlin-matter-controller
```
26 changes: 26 additions & 0 deletions examples/kotlin-matter-controller/args.gni
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright (c) 2020-2022 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import("//build_overrides/chip.gni")

import("${chip_root}/config/standalone/args.gni")

chip_device_project_config_include = "<CHIPProjectAppConfig.h>"
chip_project_config_include = "<CHIPProjectAppConfig.h>"
chip_system_project_config_include = "<SystemProjectConfig.h>"

chip_project_config_include_dirs =
[ "${chip_root}/examples/kotlin-matter-controller/include" ]
chip_project_config_include_dirs += [ "${chip_root}/config/standalone" ]
chip_stack_lock_tracking = "fatal"
1 change: 1 addition & 0 deletions examples/kotlin-matter-controller/build_overrides
56 changes: 56 additions & 0 deletions examples/kotlin-matter-controller/include/CHIPProjectAppConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
*
* Copyright (c) 2020-2022 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CHIPPROJECTCONFIG_H
#define CHIPPROJECTCONFIG_H

#define CHIP_CONFIG_MAX_FABRICS 17

#define CHIP_CONFIG_EVENT_LOGGING_NUM_EXTERNAL_CALLBACKS 2

// Enable support functions for parsing command-line arguments
#define CHIP_CONFIG_ENABLE_ARG_PARSER 1

// Use a default pairing code if one hasn't been provisioned in flash.
#define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_PIN_CODE 20202021
#define CHIP_DEVICE_CONFIG_USE_TEST_SETUP_DISCRIMINATOR 0xF00

// Enable reading DRBG seed data from /dev/(u)random.
// This is needed for test applications and the CHIP device manager to function
// properly when CHIP_CONFIG_RNG_IMPLEMENTATION_CHIPDRBG is enabled.
#define CHIP_CONFIG_DEV_RANDOM_DRBG_SEED 1

// For convenience, Chip Security Test Mode can be enabled and the
// requirement for authentication in various protocols can be disabled.
//
// WARNING: These options make it possible to circumvent basic Chip security functionality,
// including message encryption. Because of this they MUST NEVER BE ENABLED IN PRODUCTION BUILDS.
//
#define CHIP_CONFIG_SECURITY_TEST_MODE 0

#define CHIP_CONFIG_ENABLE_UPDATE 1

#define CHIP_SYSTEM_CONFIG_PACKETBUFFER_POOL_SIZE 0

#define CHIP_CONFIG_DATA_MANAGEMENT_CLIENT_EXPERIMENTAL 1

#define CHIP_DEVICE_CONFIG_ENABLE_COMMISSIONER_DISCOVERY 1

// Enable some test-only interaction model APIs.
#define CONFIG_BUILD_FOR_HOST_UNIT_TEST 1

#endif /* CHIPPROJECTCONFIG_H */
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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 com.matter.controller.commands.common.Command
import com.matter.controller.commands.common.CommandManager
import com.matter.controller.commands.common.CredentialsIssuer
import com.matter.controller.commands.pairing.PairOnNetworkLongCommand
import com.matter.controller.commands.pairing.PairOnNetworkLongImInvokeCommand
import com.matter.controller.commands.pairing.PairOnNetworkLongImReadCommand
import com.matter.controller.commands.pairing.PairOnNetworkLongImSubscribeCommand
import com.matter.controller.commands.pairing.PairOnNetworkLongImWriteCommand
import matter.controller.ControllerParams
import matter.controller.MatterController
import matter.controller.MatterControllerImpl

private fun getPairingCommands(
controller: MatterController,
credentialsIssuer: CredentialsIssuer
): List<Command> {
return listOf(
PairOnNetworkLongCommand(controller, credentialsIssuer),
)
}

private fun getImCommands(
controller: MatterController,
credentialsIssuer: CredentialsIssuer
): List<Command> {
return listOf(
PairOnNetworkLongImReadCommand(controller, credentialsIssuer),
PairOnNetworkLongImSubscribeCommand(controller, credentialsIssuer),
PairOnNetworkLongImWriteCommand(controller, credentialsIssuer),
PairOnNetworkLongImInvokeCommand(controller, credentialsIssuer),
)
}

fun main(args: Array<String>) {
val controller: MatterController = MatterControllerImpl(ControllerParams(countryCode = "US"))
val credentialsIssuer = CredentialsIssuer()
val commandManager = CommandManager()

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)
controller.close()
System.exit(1)
}

controller.close()
}
Loading

0 comments on commit fd925e4

Please sign in to comment.