Skip to content

Commit

Permalink
[ICD][Android] Add ICD storage API, UI (#31193)
Browse files Browse the repository at this point in the history
* Add ICD Client Storage

* Add icd list command in java controller

* Restyled by google-java-format

* Restyled by clang-format

* Modify kotlin codestyle

* Restyled by google-java-format

* Change from comments

* Add symmetric Key parameter

* Kotlin code style

* Restyled by clang-format

* Fix kotlin_controller build error

* Restyle in kotlin

* Update ICDRegistrationComplete callback

* Restyled by google-java-format

* Restyled by clang-format

* Fix Darwin build error

* Restyled by clang-format

* Modify using GetLocalClassRef

* Update error variable

* Restyled by clang-format

---------

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Jan 15, 2024
1 parent 0ce2626 commit 1067064
Show file tree
Hide file tree
Showing 21 changed files with 545 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.google.chip.chiptool

import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ICDDeviceInfo

open class GenericChipDeviceListener : ChipDeviceController.CompletionListener {
override fun onConnectDeviceComplete() {
Expand Down Expand Up @@ -56,7 +57,12 @@ open class GenericChipDeviceListener : ChipDeviceController.CompletionListener {
// No op
}

override fun onICDRegistrationComplete(icdNodeId: Long, icdCounter: Long) {
override fun onICDRegistrationComplete(
errorCode: Int,
icdNodeId: Long,
icdCounter: Long,
icdDeviceInfo: ICDDeviceInfo
) {
// No op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class SelectActionFragment : Fragment() {
binding.unpairDeviceBtn.setOnClickListener { handleUnpairDeviceClicked() }
binding.groupSettingBtn.setOnClickListener { handleGroupSettingClicked() }
binding.otaProviderBtn.setOnClickListener { handleOTAProviderClicked() }
binding.icdBtn.setOnClickListener { handleICDClicked() }

return binding.root
}
Expand Down Expand Up @@ -244,6 +245,10 @@ class SelectActionFragment : Fragment() {
showFragment(GroupSettingFragment.newInstance())
}

private fun handleICDClicked() {
showFragment(ICDFragment.newInstance())
}

companion object {

@JvmStatic fun newInstance() = SelectActionFragment()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.google.chip.chiptool.clusterclient

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.ICDFragmentBinding
import kotlinx.coroutines.CoroutineScope

class ICDFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private lateinit var scope: CoroutineScope

private lateinit var addressUpdateFragment: AddressUpdateFragment

private var _binding: ICDFragmentBinding? = null
private val binding
get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = ICDFragmentBinding.inflate(inflater, container, false)
scope = viewLifecycleOwner.lifecycleScope

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment

val clientInfo = deviceController.icdClientInfo
var msg = ""

for (info in clientInfo) {
msg += "$info\n"
}
showMessage(msg)

return binding.root
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

private fun showMessage(msg: String) {
requireActivity().runOnUiThread { binding.icdTv.text = msg }
}

companion object {
fun newInstance(): ICDFragment = ICDFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.AttestationInfo
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.DeviceAttestationDelegate
import chip.devicecontroller.ICDDeviceInfo
import chip.devicecontroller.ICDRegistrationInfo
import chip.devicecontroller.NetworkCredentials
import com.google.chip.chiptool.ChipClient
Expand Down Expand Up @@ -293,11 +294,22 @@ class DeviceProvisioningFragment : Fragment() {
)
}

override fun onICDRegistrationComplete(icdNodeId: Long, icdCounter: Long) {
Log.d(TAG, "onICDRegistrationComplete - icdNodeId : $icdNodeId, icdCounter : $icdCounter")
override fun onICDRegistrationComplete(
errorCode: Int,
icdNodeId: Long,
icdCounter: Long,
icdDeviceInfo: ICDDeviceInfo
) {
Log.d(
TAG,
"onICDRegistrationComplete - errorCode: $errorCode, icdNodeId : $icdNodeId, icdCounter : $icdCounter, symmetricKey : ${icdDeviceInfo.symmetricKey.toHex()}"
)
}
}

private fun ByteArray.toHex(): String =
joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }

/** Callback from [DeviceProvisioningFragment] notifying any registered listeners. */
interface Callback {
/** Notifies that commissioning has been completed. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/addressUpdateFragment"
android:name="com.google.chip.chiptool.clusterclient.AddressUpdateFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<TextView
android:id="@+id/icdTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@id/addressUpdateFragment"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:singleLine="false"
android:minLines="4"
android:textSize="20sp"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/ota_provider_btn_text" />

<Button
android:id="@+id/icdBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/icd_btn_text" />
</LinearLayout>

</ScrollView>
2 changes: 2 additions & 0 deletions examples/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,6 @@
<string name="ota_provider_endpoint_id_text">Endpoint ID</string>
<string name="ota_provider_write_text">Write</string>
<string name="ota_provider_node_id_text">Node ID</string>

<string name="icd_btn_text">Intermittently Connected Device</string>
</resources>
1 change: 1 addition & 0 deletions examples/java-matter-controller/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ 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/icd/ICDListCommand.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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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.icd.*
import com.matter.controller.commands.pairing.*

private fun getDiscoveryCommands(
Expand Down Expand Up @@ -69,6 +70,15 @@ private fun getImCommands(
)
}

private fun getICDCommands(
controller: ChipDeviceController,
credentialsIssuer: CredentialsIssuer
): List<Command> {
return listOf(
ICDListCommand(controller, credentialsIssuer),
)
}

fun main(args: Array<String>) {
val controller =
ChipDeviceController(
Expand All @@ -84,6 +94,7 @@ fun main(args: Array<String>) {
commandManager.register("discover", getDiscoveryCommands(controller, credentialsIssuer))
commandManager.register("pairing", getPairingCommands(controller, credentialsIssuer))
commandManager.register("im", getImCommands(controller, credentialsIssuer))
commandManager.register("icd", getICDCommands(controller, credentialsIssuer))

try {
commandManager.run(args)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.icd

import chip.devicecontroller.ChipDeviceController
import com.matter.controller.commands.common.CredentialsIssuer
import com.matter.controller.commands.common.MatterCommand

class ICDListCommand(val controller: ChipDeviceController, credsIssuer: CredentialsIssuer?) :
MatterCommand(controller, credsIssuer, "list") {

override fun runCommand() {
val info = controller.icdClientInfo
println(" +-----------------------------------------------------------------------------+")
System.out.format(
" | %-52s %10s : %8d |\n",
"Known ICDs",
"FabricIndex",
controller.fabricIndex
)
println(" +-----------------------------------------------------------------------------+")
System.out.format(
" | %20s | %15s | %15s | %16s |\n",
"Node ID",
"Start Counter",
"Counter Offset",
"MonitoredSubject"
)

for (i in info) {
println(" +-----------------------------------------------------------------------------+")
System.out.format(
" | %20d | %15d | %15d | %16d |\n",
i.peerNodeId,
i.startCounter,
i.offset,
i.monitoredSubject
)
System.out.format(" | %-10s : %62s |\n", "aes key", i.icdAesKey.toHex())
System.out.format(" | %-10s : %62s |\n", "hamc key", i.icdHmacKey.toHex())
}
println(" +-----------------------------------------------------------------------------+")
}

private fun ByteArray.toHex(): String =
joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package com.matter.controller.commands.pairing

import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ICDDeviceInfo
import chip.devicecontroller.ICDRegistrationInfo
import chip.devicecontroller.NetworkCredentials
import com.matter.controller.commands.common.CredentialsIssuer
Expand Down Expand Up @@ -185,10 +186,15 @@ abstract class PairingCommand(
.updateCommissioningICDRegistrationInfo(ICDRegistrationInfo.newBuilder().build())
}

override fun onICDRegistrationComplete(icdNodeId: Long, icdCounter: Long) {
override fun onICDRegistrationComplete(
errorCode: Int,
icdNodeId: Long,
icdCounter: Long,
icdDeviceInfo: ICDDeviceInfo
) {
logger.log(
Level.INFO,
"onICDRegistrationComplete with icdNodeId: $icdNodeId, icdCounter: $icdCounter"
"onICDRegistrationComplete with errorCode: $errorCode, icdNodeId: $icdNodeId, icdCounter: $icdCounter, symmetricKey: ${icdDeviceInfo.symmetricKey.toHex()}"
)
}

Expand Down Expand Up @@ -244,6 +250,9 @@ abstract class PairingCommand(
return useOnlyOnNetworkDiscovery.get()
}

private fun ByteArray.toHex(): String =
joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }

companion object {
private val logger = Logger.getLogger(PairingCommand::class.java.name)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package com.matter.controller.commands.pairing

import chip.devicecontroller.ICDDeviceInfo
import com.matter.controller.commands.common.CredentialsIssuer
import com.matter.controller.commands.common.IPAddress
import com.matter.controller.commands.common.MatterCommand
Expand Down Expand Up @@ -177,10 +178,15 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onICDRegistrationInfoRequired")
}

override fun onICDRegistrationComplete(icdNodeId: Long, icdCounter: Long) {
override fun onICDRegistrationComplete(
errorCode: Int,
icdNodeId: Long,
icdCounter: Long,
icdDeviceInfo: ICDDeviceInfo
) {
logger.log(
Level.INFO,
"onICDRegistrationComplete with icdNodeId: $icdNodeId, icdCounter: $icdCounter"
"onICDRegistrationComplete with errorCode: $errorCode, icdNodeId: $icdNodeId, icdCounter: $icdCounter, symmetricKey: ${icdDeviceInfo.symmetricKey.toHex()}"
)
}

Expand Down Expand Up @@ -212,6 +218,9 @@ abstract class PairingCommand(
return onboardingPayload.toString()
}

private fun ByteArray.toHex(): String =
joinToString(separator = "") { eachByte -> "%02x".format(eachByte) }

private fun String.hexToByteArray(): ByteArray {
return chunked(2).map { byteStr -> byteStr.toUByte(16).toByte() }.toByteArray()
}
Expand Down
Loading

0 comments on commit 1067064

Please sign in to comment.