From 5318489b87fa068633fcf437dc8274508b96f654 Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Wed, 8 Nov 2023 10:29:50 +0900 Subject: [PATCH] [Android] Remove JNI code in each cluster. (#30141) * Remove Java ChipClusters readAttribute jni * Implement write, subscribe, invoke * Implement Struct TLV parinsg * Fix write, invoke crash * Remove native method, fix empty command response issue * Remove android controller data model code * Attribute ID change to private static variable. * Restyled by whitespace * Restyled by google-java-format * Restyled by clang-format * Restyled by autopep8 * Use ChipDeviceController API * Replace List.of -> Arrays.asList --------- Co-authored-by: Restyled.io --- .../clusterclient/GroupSettingFragment.kt | 251 +- .../generators/java/ChipClusters_java.jinja | 421 +- .../java/ChipEventStructs_java.jinja | 95 + .../generators/java/ChipStructs_java.jinja | 95 + .../matter_idl/generators/java/__init__.py | 31 + .../several_clusters/java/ChipClusters.java | 403 +- .../java/ChipEventStructs.java | 2 + .../several_clusters/java/ChipStructs.java | 74 + src/controller/java/BUILD.gn | 17 +- src/controller/java/BaseCHIPCluster-JNI.cpp | 394 +- .../java/CHIPDeviceController-JNI.cpp | 8 +- .../chip/devicecontroller/ChipClusters.java | 52442 +++++++++++----- .../devicecontroller/ChipEventStructs.java | 2018 + .../chip/devicecontroller/ChipStructs.java | 4196 ++ .../ChipDeviceController.java | 8 +- .../chip/devicecontroller/ChipTLVType.java | 366 + 16 files changed, 45674 insertions(+), 15147 deletions(-) create mode 100644 src/controller/java/src/chip/devicecontroller/ChipTLVType.java diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/GroupSettingFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/GroupSettingFragment.kt index 9687237a814e0d..8943801c080c80 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/GroupSettingFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/GroupSettingFragment.kt @@ -12,19 +12,12 @@ import android.widget.EditText import android.widget.Spinner import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope +import chip.devicecontroller.ChipClusters import chip.devicecontroller.ChipDeviceController -import chip.devicecontroller.ClusterIDMapping +import chip.devicecontroller.ChipStructs +import chip.devicecontroller.ChipStructs.AccessControlClusterAccessControlEntryStruct +import chip.devicecontroller.ChipStructs.GroupKeyManagementClusterGroupKeySetStruct import chip.devicecontroller.GroupKeySecurityPolicy -import chip.devicecontroller.InvokeCallback -import chip.devicecontroller.ReportCallback -import chip.devicecontroller.WriteAttributesCallback -import chip.devicecontroller.cluster.structs.AccessControlClusterAccessControlEntryStruct -import chip.devicecontroller.cluster.structs.GroupKeyManagementClusterGroupKeySetStruct -import chip.devicecontroller.model.AttributeWriteRequest -import chip.devicecontroller.model.ChipAttributePath -import chip.devicecontroller.model.ChipEventPath -import chip.devicecontroller.model.InvokeElement -import chip.devicecontroller.model.NodeState import com.google.chip.chiptool.ChipClient import com.google.chip.chiptool.GenericChipDeviceListener import com.google.chip.chiptool.R @@ -33,10 +26,6 @@ import com.google.chip.chiptool.util.DeviceIdUtil import java.lang.Exception import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch -import matter.tlv.AnonymousTag -import matter.tlv.ContextSpecificTag -import matter.tlv.TlvReader -import matter.tlv.TlvWriter class GroupSettingFragment : Fragment() { private val deviceController: ChipDeviceController @@ -260,14 +249,17 @@ class GroupSettingFragment : Fragment() { scope.launch { val keySetWritestruct = GroupKeyManagementClusterGroupKeySetStruct( - keySetIdEd.text.toString().toUInt(), - GroupKeySecurityPolicy.valueOf(keySecurityPolicySp.selectedItem.toString()).id.toUInt(), + keySetIdEd.text.toString().toUInt().toInt(), + GroupKeySecurityPolicy.valueOf(keySecurityPolicySp.selectedItem.toString()) + .id + .toUInt() + .toInt(), hexStringToByteArray(epochKey0Ed.text.toString()), - epochStartTime0Ed.text.toString().toULong(), + epochStartTime0Ed.text.toString().toULong().toLong(), hexStringToByteArray(epochKey1Ed.text.toString()), - epochStartTime1Ed.text.toString().toULong(), + epochStartTime1Ed.text.toString().toULong().toLong(), hexStringToByteArray(epochKey2Ed.text.toString()), - epochStartTime2Ed.text.toString().toULong() + epochStartTime2Ed.text.toString().toULong().toLong() ) sendKeySetWrite(keySetWritestruct) requireActivity().runOnUiThread { dialog.dismiss() } @@ -279,41 +271,24 @@ class GroupSettingFragment : Fragment() { private suspend fun sendKeySetWrite( groupKeySetStruct: GroupKeyManagementClusterGroupKeySetStruct ) { - val tlvWriter = - TlvWriter() - .startStructure(AnonymousTag) - .apply { - groupKeySetStruct.toTlv( - ContextSpecificTag( - ClusterIDMapping.GroupKeyManagement.KeySetWriteCommandField.GroupKeySet.id - ), - this - ) - } - .endStructure() - - deviceController.invoke( - object : InvokeCallback { - override fun onError(e: java.lang.Exception?) { + val cluster = + ChipClusters.GroupKeyManagementCluster( + ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), + 0 + ) + cluster.keySetWrite( + object : ChipClusters.DefaultClusterCallback { + override fun onError(e: Exception?) { Log.d(TAG, "onError : ", e) showMessage("onError : ${e.toString()}") } - override fun onResponse(invokeElement: InvokeElement?, successCode: Long) { + override fun onSuccess() { Log.d(TAG, "onResponse") - showMessage("onResponse : $successCode") + showMessage("onResponse") } }, - ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), - InvokeElement.newInstance( - 0, - ClusterIDMapping.GroupKeyManagement.ID, - ClusterIDMapping.GroupKeyManagement.Command.KeySetWrite.id, - tlvWriter.getEncoded(), - null - ), - 0, - 0 + groupKeySetStruct ) } @@ -336,39 +311,30 @@ class GroupSettingFragment : Fragment() { } private suspend fun writeGroupKeyMap(groupId: UInt, groupKeySetId: UInt) { - val tlvWriter = - TlvWriter().apply { - startArray(AnonymousTag) - startStructure(AnonymousTag) - put(ContextSpecificTag(1), groupId) - put(ContextSpecificTag(2), groupKeySetId) - endStructure() - endArray() - } - - deviceController.write( - object : WriteAttributesCallback { - override fun onError(attributePath: ChipAttributePath?, e: Exception?) { + val cluster = + ChipClusters.GroupKeyManagementCluster( + ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), + 0 + ) + cluster.writeGroupKeyMapAttribute( + object : ChipClusters.DefaultClusterCallback { + override fun onError(e: Exception?) { Log.d(TAG, "onError : ", e) showMessage("Error : ${e.toString()}") } - override fun onResponse(attributePath: ChipAttributePath?) { + override fun onSuccess() { Log.d(TAG, "onResponse") showMessage("write Success") } }, - ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), - listOf( - AttributeWriteRequest.newInstance( - 0, - ClusterIDMapping.GroupKeyManagement.ID, - ClusterIDMapping.GroupKeyManagement.Attribute.GroupKeyMap.id, - tlvWriter.getEncoded() + arrayListOf( + ChipStructs.GroupKeyManagementClusterGroupKeyMapStruct( + groupId.toInt(), + groupKeySetId.toInt(), + deviceController.fabricIndex ) - ), - 0, - 0 + ) ) } @@ -390,75 +356,52 @@ class GroupSettingFragment : Fragment() { } private suspend fun sendAddGroup(groupId: UInt, groupName: String) { - val tlvWriter = - TlvWriter().apply { - startStructure(AnonymousTag) - put(ContextSpecificTag(0), groupId) - put(ContextSpecificTag(1), groupName) - endStructure() - } - - deviceController.invoke( - object : InvokeCallback { + val cluster = + ChipClusters.GroupsCluster( + ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), + 0 + ) + cluster.addGroup( + object : ChipClusters.GroupsCluster.AddGroupResponseCallback { override fun onError(e: Exception?) { Log.d(TAG, "onError : ", e) showMessage("Error : ${e.toString()}") } - override fun onResponse(invokeElement: InvokeElement?, successCode: Long) { + override fun onSuccess(status: Int?, groupID: Int?) { Log.d(TAG, "onResponse") - showMessage("onResponse : $successCode") + showMessage("onResponse : $status, $groupID") } }, - ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), - InvokeElement.newInstance( - 0, - ClusterIDMapping.Groups.ID, - ClusterIDMapping.Groups.Command.AddGroup.id, - tlvWriter.getEncoded(), - null - ), - 0, - 0 + groupId.toInt(), + groupName ) } private suspend fun readAccessControl() { - val endpointId = 0 - val clusterId = ClusterIDMapping.AccessControl.ID - val attributeId = ClusterIDMapping.AccessControl.Attribute.Acl.id - - val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId) - deviceController.readAttributePath( - object : ReportCallback { - override fun onError( - attributePath: ChipAttributePath?, - eventPath: ChipEventPath?, - e: Exception - ) { + val cluster = + ChipClusters.AccessControlCluster( + ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), + 0 + ) + cluster.readAclAttribute( + object : ChipClusters.AccessControlCluster.AclAttributeCallback { + override fun onError(e: Exception?) { Log.d(TAG, "onError : ", e) showMessage("Error : $e") } - override fun onReport(nodeState: NodeState?) { - Log.d(TAG, "onResponse") - val tlv = - nodeState - ?.getEndpointState(endpointId) - ?.getClusterState(clusterId) - ?.getAttributeState(attributeId) - ?.tlv - requireActivity().runOnUiThread { showAddAccessControlDialog(tlv) } + override fun onSuccess(value: MutableList?) { + requireActivity().runOnUiThread { showAddAccessControlDialog(value) } } - }, - ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId), - listOf(attributePath), - 0 + } ) } - private fun showAddAccessControlDialog(tlv: ByteArray?) { - if (tlv == null) { + private fun showAddAccessControlDialog( + value: List? + ) { + if (value == null) { Log.d(TAG, "Access Control read fail") showMessage("Access Control read fail") return @@ -479,7 +422,7 @@ class GroupSettingFragment : Fragment() { dialogView.findViewById