Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Support Opcreds button to check fabric features #10102

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions src/android/CHIPTool/.idea/runConfigurations.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import chip.setuppayload.SetupPayloadParser
import chip.setuppayload.SetupPayloadParser.UnrecognizedQrCodeException
import com.google.chip.chiptool.attestation.AttestationTestFragment
import com.google.chip.chiptool.clusterclient.MultiAdminClientFragment
import com.google.chip.chiptool.clusterclient.OpCredClientFragment
import com.google.chip.chiptool.clusterclient.OnOffClientFragment
import com.google.chip.chiptool.clusterclient.SensorClientFragment
import com.google.chip.chiptool.provisioning.AddressCommissioningFragment
Expand Down Expand Up @@ -122,6 +123,10 @@ class CHIPToolActivity :
showFragment(MultiAdminClientFragment.newInstance())
}

override fun handleOpCredClicked() {
showFragment(OpCredClientFragment.newInstance())
}

override fun handleAttestationTestClicked() {
showFragment(AttestationTestFragment.newInstance())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class SelectActionFragment : Fragment() {
onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
sensorClustersBtn.setOnClickListener{ getCallback()?.handleSensorClicked() }
multiAdminClusterBtn.setOnClickListener{ getCallback()?.handleMultiAdminClicked() }
opCredClustersBtn.setOnClickListener{ getCallback()?.handleOpCredClicked() }
attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
}
}
Expand Down Expand Up @@ -112,6 +113,8 @@ class SelectActionFragment : Fragment() {
fun handleSensorClicked()
/** Notifies listener of Multi-admin Clusters button click. */
fun handleMultiAdminClicked()
/** Notifies listener of Operational Credentials Clusters button click. */
fun handleOpCredClicked()
/** Notifies listener of attestation command button clicked. */
fun handleAttestationTestClicked()
/** Notifies listener of a click to manually input the CHIP device address.. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.google.chip.chiptool.clusterclient

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import chip.devicecontroller.ChipClusters
import chip.devicecontroller.ChipDeviceController
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
import com.google.chip.chiptool.util.DeviceIdUtil
import kotlinx.android.synthetic.main.op_cred_client_fragment.*
import kotlinx.android.synthetic.main.op_cred_client_fragment.view.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch

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

private val scope = CoroutineScope(Dispatchers.Main + Job())

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return inflater.inflate(R.layout.op_cred_client_fragment, container, false).apply {
deviceController.setCompletionListener(ChipControllerCallback())

opCredClusterUpdateAddressBtn.setOnClickListener { scope.launch { updateAddressClick() } }
readSupportedFabricBtn.setOnClickListener { scope.launch { sendReadOpCredSupportedFabricAttrClick() } }
readCommissionedFabricBtn.setOnClickListener { scope.launch { sendReadOpCredCommissionedFabricAttrClick() } }
}
}

override fun onStart() {
super.onStart()
// TODO: use the fabric ID that was used to commission the device
val testFabricId = "5544332211"
opCredClusterFabricIdEd.setText(testFabricId)
opCredClusterDeviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString())
}

inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
}

override fun onNotifyChipConnectionClosed() {
Log.d(TAG, "onNotifyChipConnectionClosed")
}

override fun onCloseBleComplete() {
Log.d(TAG, "onCloseBleComplete")
}

override fun onError(error: Throwable?) {
Log.d(TAG, "onError: $error")
}
}

override fun onStop() {
super.onStop()
scope.cancel()
}

private fun updateAddressClick() {
try{
chulspro marked this conversation as resolved.
Show resolved Hide resolved
deviceController.updateDevice(
opCredClusterFabricIdEd.text.toString().toULong().toLong(),
opCredClusterDeviceIdEd.text.toString().toULong().toLong()
)
showMessage("Address update started")
} catch (ex: Exception) {
showMessage("Address update failed: $ex")
}
}

private suspend fun sendReadOpCredSupportedFabricAttrClick() {
getOpCredClusterForDevice().readSupportedFabricsAttribute(object : ChipClusters.IntegerAttributeCallback {
override fun onSuccess(value: Int) {
Log.v(TAG, "OpCred supported Fabric attribute value: $value")
showMessage("OpCred supported Fabric attribute value: $value")
}

override fun onError(ex: Exception) {
Log.e(TAG, "Error reading OpCred supported Fabric attribute", ex)
}
})
}

private suspend fun sendReadOpCredCommissionedFabricAttrClick() {
getOpCredClusterForDevice().readCommissionedFabricsAttribute(object : ChipClusters.IntegerAttributeCallback {
override fun onSuccess(value: Int) {
Log.v(TAG, "OpCred Commissioned Fabric attribute value: $value")
showMessage("OpCred Commissioned Fabric attribute value: $value")
}

override fun onError(ex: Exception) {
Log.e(TAG, "Error reading OpCred Commissioned Fabric attribute", ex)
}
})
}

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

private suspend fun getOpCredClusterForDevice(): ChipClusters.OperationalCredentialsCluster {
return ChipClusters.OperationalCredentialsCluster(
ChipClient.getConnectedDevicePointer(requireContext(), opCredClusterDeviceIdEd.text.toString().toLong()), 0
)
}

companion object {
private const val TAG = "OpCredClientFragment"
fun newInstance(): OpCredClientFragment = OpCredClientFragment()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<EditText
android:id="@+id/opCredClusterFabricIdEd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:hint="@string/enter_fabric_id_hint_text"
android:inputType="text"
android:textSize="20sp" />

<EditText
android:id="@+id/opCredClusterDeviceIdEd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_toEndOf="@id/opCredClusterFabricIdEd"
android:hint="@string/enter_device_id_hint_text"
android:inputType="text"
android:textSize="20sp" />

<Button
android:id="@+id/opCredClusterUpdateAddressBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/opCredClusterFabricIdEd"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="@string/update_device_address_btn_text" />

<Button
android:id="@+id/readSupportedFabricBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/opCredClusterUpdateAddressBtn"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="@string/op_cred_client_read_supported_fabric_btn_text" />

<Button
android:id="@+id/readCommissionedFabricBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/readSupportedFabricBtn"
android:layout_alignParentStart="true"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="@string/op_cred_client_read_commissioned_fabric_btn_text" />

<TextView
android:id="@+id/opCredClusterCommandStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/readCommissionedFabricBtn"
android:minLines="4"
android:padding="16dp"
android:singleLine="false"
android:textSize="20sp" />

</RelativeLayout>
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@
android:layout_marginTop="8dp"
android:text="@string/multi_admin_client_btn_text" />

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

<Button
android:id="@+id/attestationTestBtn"
android:layout_height="wrap_content"
Expand Down
4 changes: 4 additions & 0 deletions src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,8 @@
<string name="enter_setup_pin_code_hint_text">Enter Setup PIN Code</string>
<string name="enhanced_commissioning_method_btn_text">Enhanced Commissioning Method</string>
<string name="revoke_btn_text">Revoke</string>

<string name="op_cred_client_btn_text">Operational Credentials clusters</string>
<string name="op_cred_client_read_supported_fabric_btn_text">Read Supported Fabric count</string>
<string name="op_cred_client_read_commissioned_fabric_btn_text">Read Commissioned Fabric count</string>
</resources>