diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt index 0651c6103631c8..a482a6170f7766 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt @@ -15,6 +15,7 @@ import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import chip.devicecontroller.ChipDeviceController import chip.devicecontroller.ChipIdLookup +import chip.devicecontroller.InvokeCallback import chip.devicecontroller.ReportCallback import chip.devicecontroller.ResubscriptionAttemptCallback import chip.devicecontroller.SubscriptionEstablishedCallback @@ -23,8 +24,10 @@ import chip.devicecontroller.model.AttributeWriteRequest import chip.devicecontroller.model.ChipAttributePath import chip.devicecontroller.model.ChipEventPath import chip.devicecontroller.model.ChipPathId +import chip.devicecontroller.model.InvokeElement import chip.devicecontroller.model.NodeState import chip.tlv.AnonymousTag +import chip.tlv.ContextSpecificTag import chip.tlv.TlvWriter import com.google.chip.chiptool.ChipClient import com.google.chip.chiptool.R @@ -86,6 +89,18 @@ class WildcardFragment : Fragment() { } } + private val invokeCallback = object : InvokeCallback { + override fun onError(e: java.lang.Exception?) { + Log.e(TAG, "Report error", e) + } + + override fun onResponse(invokeElement: InvokeElement?, successCode: Long) { + val text = "Invoke Response : $invokeElement, $successCode" + requireActivity().runOnUiThread { binding.outputTv.text = text } + } + + } + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -98,6 +113,7 @@ class WildcardFragment : Fragment() { binding.writeBtn.setOnClickListener { scope.launch { showWriteDialog() } } binding.subscribeEventBtn.setOnClickListener { scope.launch { showSubscribeDialog(EVENT) } } binding.readEventBtn.setOnClickListener { scope.launch { showReadDialog(EVENT) } } + binding.invokeBtn.setOnClickListener { scope.launch { showInvokeDialog() } } addressUpdateFragment = childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment @@ -234,6 +250,37 @@ class WildcardFragment : Fragment() { imTimeoutMs) } + private suspend fun invoke(invokeField: String, timedRequestTimeoutMs: Int, imTimeoutMs: Int) { + val endpointId = getChipPathIdForText(binding.endpointIdEd.text.toString()) + val clusterId = getChipPathIdForText(binding.clusterIdEd.text.toString()) + val commandId = getChipPathIdForText(binding.commandIdEd.text.toString()) + + val tlvWriter = TlvWriter() + val fields = if (invokeField.isEmpty()) { listOf() } else { invokeField.split(",") } + var count = 0 + tlvWriter.startStructure(AnonymousTag) + for (field in fields) { + try { + val type = field.split(":")[0] + val value = field.split(":")[1] + + Log.d(TAG, "value : $type - $value") + TLV_MAP[type]?.generate(tlvWriter, value.trim(), ContextSpecificTag(count++)) + } catch (ex: Exception) { + Log.e(TAG, "Invalid value", ex) + return + } + } + tlvWriter.endStructure() + val invokeElement = InvokeElement.newInstance(endpointId, clusterId, commandId, tlvWriter.getEncoded(), null) + deviceController.invoke(invokeCallback, + ChipClient.getConnectedDevicePointer(requireContext(), + addressUpdateFragment.deviceId), + invokeElement, + timedRequestTimeoutMs, + imTimeoutMs) + } + private fun showReadDialog(type: Int) { val dialogView = requireActivity().layoutInflater.inflate(R.layout.read_dialog, null) val dialog = AlertDialog.Builder(requireContext()).apply { @@ -316,12 +363,34 @@ class WildcardFragment : Fragment() { dialog.show() } + private fun showInvokeDialog() { + binding.outputTv.text = "" + val dialogView = requireActivity().layoutInflater.inflate(R.layout.invoke_dialog, null) + val invokeValueEd = dialogView.findViewById(R.id.invokeValueEd) + val dialog = AlertDialog.Builder(requireContext()).apply { + setView(dialogView) + }.create() + + dialogView.findViewById