|
1 | 1 | package com.google.chip.chiptool.clusterclient
|
2 | 2 |
|
| 3 | +import android.content.Context |
3 | 4 | import android.os.Bundle
|
| 5 | +import android.util.Log |
4 | 6 | import android.view.LayoutInflater
|
5 | 7 | import android.view.View
|
6 | 8 | import android.view.ViewGroup
|
| 9 | +import android.widget.AdapterView |
| 10 | +import android.widget.ArrayAdapter |
7 | 11 | import androidx.fragment.app.Fragment
|
8 | 12 | import chip.devicecontroller.ChipDeviceController
|
9 | 13 | import com.google.chip.chiptool.ChipClient
|
@@ -41,13 +45,70 @@ class AddressUpdateFragment : Fragment() {
|
41 | 45 | super.onViewCreated(view, savedInstanceState)
|
42 | 46 |
|
43 | 47 | val compressedFabricId = deviceController.compressedFabricId
|
44 |
| - binding.fabricIdEd.setText(compressedFabricId.toULong().toString(16).padStart(16, '0')) |
45 |
| - binding.deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString()) |
| 48 | + binding.fabricIdEd.setText(compressedFabricId.toULong().toString().padStart(16, '0')) |
| 49 | + binding.deviceIdEd.setText(DeviceIdUtil.getLastDeviceId(requireContext()).toString(16)) |
46 | 50 | binding.epIdEd.setText(endpointId.toString())
|
| 51 | + |
| 52 | + updateDeviceIdSpinner() |
| 53 | + } |
| 54 | + |
| 55 | + fun updateDeviceIdSpinner() { |
| 56 | + val deviceIdList = DeviceIdUtil.getCommissionedNodeId(requireContext()) |
| 57 | + binding.deviceIdSpinner.adapter = |
| 58 | + ArrayAdapter(requireContext(), android.R.layout.simple_spinner_dropdown_item, deviceIdList) |
| 59 | + binding.deviceIdSpinner.onItemSelectedListener = |
| 60 | + object : AdapterView.OnItemSelectedListener { |
| 61 | + override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) { |
| 62 | + binding.deviceIdEd.setText(deviceIdList[position].toULong(16).toString()) |
| 63 | + } |
| 64 | + |
| 65 | + override fun onNothingSelected(parent: AdapterView<*>?) { |
| 66 | + Log.d(TAG, "onNothingSelected") |
| 67 | + } |
| 68 | + } |
47 | 69 | }
|
48 | 70 |
|
49 | 71 | override fun onDestroyView() {
|
50 | 72 | super.onDestroyView()
|
51 | 73 | _binding = null
|
52 | 74 | }
|
| 75 | + |
| 76 | + suspend fun getDevicePointer(context: Context): Long { |
| 77 | + return if (isGroupId()) { |
| 78 | + deviceController.getGroupDevicePointer(getGroupId().toInt()) |
| 79 | + } else { |
| 80 | + ChipClient.getConnectedDevicePointer(context, getNodeId().toLong()) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + fun isGroupId(): Boolean { |
| 85 | + return isGroupNodeId(getNodeId()) |
| 86 | + } |
| 87 | + |
| 88 | + fun getGroupId(): UInt { |
| 89 | + return getGroupIdFromNodeId(getNodeId()) |
| 90 | + } |
| 91 | + |
| 92 | + fun getNodeId(): ULong { |
| 93 | + return binding.deviceIdEd.text.toString().toULong() |
| 94 | + } |
| 95 | + |
| 96 | + companion object { |
| 97 | + private const val TAG = "AddressUpdateFragment" |
| 98 | + // Refer from NodeId.h (src/lib/core/NodeId.h) |
| 99 | + private const val MIN_GROUP_NODE_ID = 0xFFFF_FFFF_FFFF_0000UL |
| 100 | + private const val MASK_GROUP_ID = 0x0000_0000_0000_FFFFUL |
| 101 | + |
| 102 | + fun isGroupNodeId(nodeId: ULong): Boolean { |
| 103 | + return nodeId >= MIN_GROUP_NODE_ID |
| 104 | + } |
| 105 | + |
| 106 | + fun getNodeIdFromGroupId(groupId: UInt): ULong { |
| 107 | + return groupId.toULong() or MIN_GROUP_NODE_ID |
| 108 | + } |
| 109 | + |
| 110 | + fun getGroupIdFromNodeId(nodeId: ULong): UInt { |
| 111 | + return (nodeId and MASK_GROUP_ID).toUInt() |
| 112 | + } |
| 113 | + } |
53 | 114 | }
|
0 commit comments