Skip to content

Commit

Permalink
[Android] Add cluster Btn in chiptool (Opcred, administratorCommissio…
Browse files Browse the repository at this point in the history
…ning) (#26782)

* Add MultiAdmin test application btn

* remove wildcard import
  • Loading branch information
joonhaengHeo authored and pull[bot] committed Aug 7, 2023
1 parent cea5989 commit 3098704
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ClusterIDMapping
import chip.devicecontroller.ClusterIDMapping.AdministratorCommissioning
import chip.devicecontroller.InvokeCallback
import chip.devicecontroller.OpenCommissioningCallback
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.NodeState
import chip.tlv.AnonymousTag
import chip.tlv.TlvWriter
import com.google.chip.chiptool.ChipClient
Expand Down Expand Up @@ -47,6 +51,9 @@ class MultiAdminClientFragment : Fragment() {
binding.basicCommissioningMethodBtn.setOnClickListener { scope.launch { sendBasicCommissioningCommandClick() } }
binding.enhancedCommissioningMethodBtn.setOnClickListener { scope.launch { sendEnhancedCommissioningCommandClick() } }
binding.revokeBtn.setOnClickListener { scope.launch { sendRevokeCommandClick() } }
binding.readWindowStatusBtn.setOnClickListener { scope.launch { readAdministratorCommissioningClusterAttributeClick(AdministratorCommissioning.Attribute.WindowStatus) } }
binding.readAdminFabricIndexBtn.setOnClickListener { scope.launch { readAdministratorCommissioningClusterAttributeClick(AdministratorCommissioning.Attribute.AdminFabricIndex) } }
binding.readAdminVendorIdBtn.setOnClickListener { scope.launch { readAdministratorCommissioningClusterAttributeClick(AdministratorCommissioning.Attribute.AdminVendorId) } }

return binding.root
}
Expand Down Expand Up @@ -137,9 +144,9 @@ class MultiAdminClientFragment : Fragment() {
val tlvWriter = TlvWriter()
tlvWriter.startStructure(AnonymousTag)
tlvWriter.endStructure()
val invokeElement = InvokeElement.newInstance(0
, ClusterIDMapping.AdministratorCommissioning.ID
, ClusterIDMapping.AdministratorCommissioning.Command.RevokeCommissioning.id
val invokeElement = InvokeElement.newInstance(ADMINISTRATOR_COMMISSIONING_CLUSTER_ENDPOINT_ID
, AdministratorCommissioning.ID
, AdministratorCommissioning.Command.RevokeCommissioning.id
, tlvWriter.getEncoded(), null)

deviceController.invoke(object: InvokeCallback {
Expand All @@ -156,6 +163,25 @@ class MultiAdminClientFragment : Fragment() {
}, getConnectedDevicePointer(), invokeElement, timedInvokeTimeout, 0)
}

private suspend fun readAdministratorCommissioningClusterAttributeClick(attribute: AdministratorCommissioning.Attribute) {
val endpointId = ADMINISTRATOR_COMMISSIONING_CLUSTER_ENDPOINT_ID
val clusterId = AdministratorCommissioning.ID
val attributeId = attribute.id
val attributeName = attribute.name
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)
deviceController.readAttributePath(object: ReportCallback {
override fun onReport(nodeState: NodeState?) {
val value = nodeState?.getEndpointState(endpointId)?.getClusterState(clusterId)?.getAttributeState(attributeId)?.value ?: "null"
Log.i(TAG,"read $attributeName: $value")
showMessage("read $attributeName: $value")
}

override fun onError(attributePath: ChipAttributePath?, eventPath: ChipEventPath?, e: Exception) {
showMessage("read $attributeName - error : ${e?.message}")
}
}, getConnectedDevicePointer(), listOf(attributePath), 0)
}

private suspend fun getConnectedDevicePointer(): Long {
return ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId)
}
Expand All @@ -168,6 +194,7 @@ class MultiAdminClientFragment : Fragment() {

companion object {
private const val TAG = "MultiAdminClientFragment"
private const val ADMINISTRATOR_COMMISSIONING_CLUSTER_ENDPOINT_ID = 0
fun newInstance(): MultiAdminClientFragment = MultiAdminClientFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ import com.google.chip.chiptool.databinding.OpCredClientFragmentBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

import chip.devicecontroller.ClusterIDMapping.*
import chip.devicecontroller.ClusterIDMapping.OperationalCredentials
import chip.devicecontroller.InvokeCallback
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.NodeState
import chip.tlv.AnonymousTag
import chip.tlv.ContextSpecificTag
import chip.tlv.TlvWriter

class OpCredClientFragment : Fragment() {
private val deviceController: ChipDeviceController
Expand Down Expand Up @@ -47,6 +52,8 @@ class OpCredClientFragment : Fragment() {

binding.readSupportedFabricBtn.setOnClickListener { scope.launch { readClusterAttribute(OperationalCredentials.Attribute.SupportedFabrics) } }
binding.readCommissionedFabricBtn.setOnClickListener { scope.launch { readClusterAttribute(OperationalCredentials.Attribute.CommissionedFabrics) } }
binding.readFabricsBtn.setOnClickListener { scope.launch { readClusterAttribute(OperationalCredentials.Attribute.Fabrics) } }
binding.removeFabricsBtn.setOnClickListener { scope.launch { sendRemoveFabricsBtnClick(binding.fabricIndexEd.text.toString().toUInt()) } }

return binding.root
}
Expand Down Expand Up @@ -99,6 +106,32 @@ class OpCredClientFragment : Fragment() {
}, devicePtr, listOf(ChipAttributePath.newInstance(endpointId, clusterId, attributeId)), null, false, 0 /* imTimeoutMs */)
}

private suspend fun sendRemoveFabricsBtnClick(fabricIndex: UInt) {
val devicePtr = ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId)
// TODO : Need to be implement poj-to-tlv
val tlvWriter = TlvWriter()
tlvWriter.startStructure(AnonymousTag)
tlvWriter.put(ContextSpecificTag(OperationalCredentials.RemoveFabricCommandField.FabricIndex.id), fabricIndex)
tlvWriter.endStructure()
val invokeElement = InvokeElement.newInstance(addressUpdateFragment.endpointId
, OperationalCredentials.ID
, OperationalCredentials.Command.RemoveFabric.id
, tlvWriter.getEncoded(), null)

deviceController.invoke(object: InvokeCallback {
override fun onError(ex: Exception?) {
showMessage("RemoveFabric failure $ex")
Log.e(TAG, "RemoveFabric failure", ex)
}

override fun onResponse(invokeElement: InvokeElement?, successCode: Long) {
Log.e(TAG, "onResponse : $invokeElement, Code : $successCode")
showMessage("RemoveFabric success")
}

}, devicePtr, invokeElement, 0, 0)
}

private fun showMessage(msg: String) {
requireActivity().runOnUiThread {
binding.opCredClusterCommandStatus.text = msg
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,45 @@
app:layout_constraintEnd_toEndOf="parent"
android:text="@string/revoke_btn_text" />

<Button
android:id="@+id/readWindowStatusBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="10dp"
app:layout_constraintTop_toBottomOf="@id/revokeBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/readAdminFabricIndexBtn"
android:text="@string/read_window_status_btn_text" />

<Button
android:id="@+id/readAdminFabricIndexBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="10dp"
app:layout_constraintTop_toBottomOf="@id/revokeBtn"
app:layout_constraintEnd_toStartOf="@id/readAdminVendorIdBtn"
app:layout_constraintStart_toEndOf="@id/readWindowStatusBtn"
android:text="@string/read_admin_fabric_index_btn_text" />

<Button
android:id="@+id/readAdminVendorIdBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="10dp"
app:layout_constraintTop_toBottomOf="@id/revokeBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/readAdminFabricIndexBtn"
android:text="@string/read_admin_vendor_id_btn_text" />

<TextView
android:id="@+id/multiAdminClusterCommandStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
app:layout_constraintTop_toBottomOf="@id/revokeBtn"
app:layout_constraintTop_toBottomOf="@id/readWindowStatusBtn"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:singleLine="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,50 @@
android:layout_marginBottom="10dp"
android:text="@string/op_cred_client_read_commissioned_fabric_btn_text" />

<Button
android:id="@+id/readFabricsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/readCommissionedFabricBtn"
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_fabrics_btn_text" />

<Button
android:id="@+id/removeFabricsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/readFabricsBtn"
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_remove_fabrics_btn_text" />

<EditText
android:id="@+id/fabricIndexEd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/readFabricsBtn"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:layout_toEndOf="@+id/removeFabricsBtn"
android:text="1"
android:hint="@string/op_cred_client_fabric_index_hint_text"
android:inputType="number"
android:textSize="20sp" />

<TextView
android:id="@+id/opCredClusterCommandStatus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/readCommissionedFabricBtn"
android:layout_below="@id/removeFabricsBtn"
android:minLines="4"
android:padding="16dp"
android:singleLine="false"
Expand Down
6 changes: 6 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 @@ -102,10 +102,16 @@
<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="read_window_status_btn_text">Read Window Status</string>
<string name="read_admin_fabric_index_btn_text">Read Admin FabricIndex</string>
<string name="read_admin_vendor_id_btn_text">Read Admin VID</string>

<string name="op_cred_client_btn_text">Operational Credentials cluster</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>
<string name="op_cred_client_read_fabrics_btn_text">Read Fabrics</string>
<string name="op_cred_client_remove_fabrics_btn_text">Remove Fabrics</string>
<string name="op_cred_client_fabric_index_hint_text">FabricIndex</string>

<string name="basic_cluster_btn_text">Basic cluster</string>
<string name="basic_cluster_write_btn_text">Write</string>
Expand Down

0 comments on commit 3098704

Please sign in to comment.