Skip to content

Commit

Permalink
Fix kotlin static code analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
joonhaengHeo committed Sep 22, 2023
1 parent ebb365c commit 77ccbc6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.google.chip.chiptool.clusterclient

import android.content.Context
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand Down Expand Up @@ -61,7 +62,9 @@ class AddressUpdateFragment : Fragment() {
binding.deviceIdEd.setText(deviceIdList[position].toULong(16).toString())
}

override fun onNothingSelected(parent: AdapterView<*>?) {}
override fun onNothingSelected(parent: AdapterView<*>?) {
Log.d(TAG, "onNothingSelected")
}
}
}

Expand All @@ -81,6 +84,7 @@ class AddressUpdateFragment : Fragment() {
}

companion object {
private const val TAG = "AddressUpdateFragment"
// Refer from NodeId.h (src/lib/core/NodeId.h)
private const val MIN_GROUP_NODE_ID = 0xFFFF_FFFF_FFFF_0000UL
private const val MASK_GROUP_ID = 0x0000_0000_0000_FFFFUL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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
Expand Down Expand Up @@ -258,55 +259,43 @@ class GroupSettingFragment : Fragment() {
val dialog = AlertDialog.Builder(requireContext()).apply { setView(dialogView) }.create()
dialogView.findViewById<Button>(R.id.addKeySetDialogBtn).setOnClickListener {
scope.launch {
sendKeySetWrite(
keySetIdEd.text.toString().toUInt(),
ChipDeviceController.GroupKeySecurityPolicy.valueOf(
keySecurityPolicySp.selectedItem.toString()
)
.id
.toUInt(),
hexStringToByteArray(epochKey0Ed.text.toString()),
epochStartTime0Ed.text.toString().toUInt(),
hexStringToByteArray(epochKey1Ed.text.toString()),
epochStartTime1Ed.text.toString().toUInt(),
hexStringToByteArray(epochKey2Ed.text.toString()),
epochStartTime2Ed.text.toString().toUInt()
)
val keySetWritestruct =
GroupKeyManagementClusterGroupKeySetStruct(
keySetIdEd.text.toString().toUInt(),
ChipDeviceController.GroupKeySecurityPolicy.valueOf(
keySecurityPolicySp.selectedItem.toString()
)
.id
.toUInt(),
hexStringToByteArray(epochKey0Ed.text.toString()),
epochStartTime0Ed.text.toString().toULong(),
hexStringToByteArray(epochKey1Ed.text.toString()),
epochStartTime1Ed.text.toString().toULong(),
hexStringToByteArray(epochKey2Ed.text.toString()),
epochStartTime2Ed.text.toString().toULong()
)
sendKeySetWrite(keySetWritestruct)
requireActivity().runOnUiThread { dialog.dismiss() }
}
}
dialog.show()
}

private suspend fun sendKeySetWrite(
keySetId: UInt,
keyPolicy: UInt,
epochKey0: ByteArray,
epochStartTime0: UInt,
epochKey1: ByteArray,
epochStartTime1: UInt,
epochKey2: ByteArray,
epochStartTime2: UInt
groupKeySetStruct: GroupKeyManagementClusterGroupKeySetStruct
) {
val tlvWriter =
TlvWriter().apply {
startStructure(AnonymousTag)
startStructure(
ContextSpecificTag(
ClusterIDMapping.GroupKeyManagement.KeySetWriteCommandField.GroupKeySet.id
TlvWriter()
.startStructure(AnonymousTag)
.apply {
groupKeySetStruct.toTlv(
ContextSpecificTag(
ClusterIDMapping.GroupKeyManagement.KeySetWriteCommandField.GroupKeySet.id
),
this
)
)
put(ContextSpecificTag(0), keySetId)
put(ContextSpecificTag(1), keyPolicy)
put(ContextSpecificTag(2), epochKey0)
put(ContextSpecificTag(3), epochStartTime0)
put(ContextSpecificTag(4), epochKey1)
put(ContextSpecificTag(5), epochStartTime1)
put(ContextSpecificTag(6), epochKey2)
put(ContextSpecificTag(7), epochStartTime2)
endStructure()
endStructure()
}
}
.endStructure()

deviceController.invoke(
object : InvokeCallback {
Expand Down Expand Up @@ -604,8 +593,6 @@ class GroupSettingFragment : Fragment() {
}

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

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
}
Expand All @@ -632,13 +619,17 @@ class GroupSettingFragment : Fragment() {

fun newInstance(): GroupSettingFragment = GroupSettingFragment()

private const val HEX_RADIX = 16
private const val HEX_BIT_SHIFT = 4

private fun hexStringToByteArray(hexString: String): ByteArray {
val len = hexString.length
val data = ByteArray(len / 2)
var i = 0
while (i < len) {
data[i / 2] =
((Character.digit(hexString[i], 16) shl 4) + Character.digit(hexString[i + 1], 16))
((Character.digit(hexString[i], HEX_RADIX) shl HEX_BIT_SHIFT) +
Character.digit(hexString[i + 1], HEX_RADIX))
.toByte()
i += 2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ object DeviceIdUtil {
private const val DEVICE_ID_PREFS_KEY = "device_id"
private const val DEVICE_ID_LIST_PREFS_KEY = "device_id_list"
private const val DEFAULT_DEVICE_ID = 1L
private const val HEX_RADIX = 16

fun getNextAvailableId(context: Context): Long {
val prefs = getPrefs(context)
Expand All @@ -26,7 +27,8 @@ object DeviceIdUtil {

fun setCommissionedNodeId(context: Context, nodeId: Long) {
val nodeList = getCommissionedNodeId(context)
nodeList.add(nodeId.toULong().toString(16))

nodeList.add(nodeId.toULong().toString(HEX_RADIX))

getPrefs(context).edit().putStringSet(DEVICE_ID_LIST_PREFS_KEY, nodeList.toSet()).apply()
}
Expand Down
1 change: 1 addition & 0 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ complexity:
excludes:
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/CHIPToolActivity.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/clusterinteraction/ClusterDetailFragment.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/GroupSettingFragment.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/OnOffClientFragment.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/SensorClientFragment.kt"
- "**/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt"
Expand Down

0 comments on commit 77ccbc6

Please sign in to comment.