Skip to content

Commit

Permalink
Implement wildcard read/subscribe for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
austinh0 committed Feb 1, 2022
1 parent 9f16e9a commit b880b8c
Show file tree
Hide file tree
Showing 30 changed files with 14,757 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1524,4 +1524,3 @@ endpoint 1 {
binding cluster OnOff;
binding cluster Scenes;
}

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.google.chip.chiptool.clusterclient.OpCredClientFragment
import com.google.chip.chiptool.clusterclient.BasicClientFragment
import com.google.chip.chiptool.clusterclient.OnOffClientFragment
import com.google.chip.chiptool.clusterclient.SensorClientFragment
import com.google.chip.chiptool.clusterclient.WildcardFragment
import com.google.chip.chiptool.provisioning.AddressCommissioningFragment
import com.google.chip.chiptool.provisioning.DeviceProvisioningFragment
import com.google.chip.chiptool.provisioning.EnterNetworkFragment
Expand Down Expand Up @@ -140,6 +141,10 @@ class CHIPToolActivity :
showFragment(ClusterInteractionFragment.newInstance())
}

override fun handleWildcardClicked() {
showFragment(WildcardFragment.newInstance())
}

override fun handleOnOffClicked() {
showFragment(OnOffClientFragment.newInstance())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ class SelectActionFragment : Fragment() {
setOnClickListener { getCallback()?.onProvisionThreadCredentialsClicked() }
}
onOffClusterBtn.setOnClickListener { getCallback()?.handleOnOffClicked() }
sensorClustersBtn.setOnClickListener{ getCallback()?.handleSensorClicked() }
multiAdminClusterBtn.setOnClickListener{ getCallback()?.handleMultiAdminClicked() }
opCredClustersBtn.setOnClickListener{ getCallback()?.handleOpCredClicked() }
basicClusterBtn.setOnClickListener{ getCallback()?.handleBasicClicked() }
sensorClustersBtn.setOnClickListener { getCallback()?.handleSensorClicked() }
multiAdminClusterBtn.setOnClickListener { getCallback()?.handleMultiAdminClicked() }
opCredClustersBtn.setOnClickListener { getCallback()?.handleOpCredClicked() }
basicClusterBtn.setOnClickListener { getCallback()?.handleBasicClicked() }
attestationTestBtn.setOnClickListener { getCallback()?.handleAttestationTestClicked() }
clusterInteractionBtn.setOnClickListener { getCallback()?.handleClusterInteractionClicked() }
provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleCustomFlowClicked() }
provisionCustomFlowBtn.setOnClickListener{ getCallback()?.handleCustomFlowClicked() }
wildcardBtn.setOnClickListener { getCallback()?.handleWildcardClicked() }
}
}

Expand Down Expand Up @@ -122,10 +123,12 @@ class SelectActionFragment : Fragment() {
fun handleBasicClicked()
/** Notifies listener of attestation command button clicked. */
fun handleAttestationTestClicked()
/** Notifies listener of a click to manually input the CHIP device address.. */
/** Notifies listener of a click to manually input the CHIP device address. */
fun onShowDeviceAddressInput()
/** Notifies listener of cluster interaction button click.. */
/** Notifies listener of cluster interaction button click. */
fun handleClusterInteractionClicked()
/** Notifies listener of wildcard button click. */
fun handleWildcardClicked()
/** Notifies listener of provision-custom-flow button click. */
fun handleCustomFlowClicked()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.google.chip.chiptool.clusterclient

import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import androidx.appcompat.widget.MenuItemHoverListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import chip.devicecontroller.ChipDeviceController
import chip.devicecontroller.ReportCallback
import chip.devicecontroller.SubscriptionEstablishedCallback
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipPathId
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import kotlinx.android.synthetic.main.wildcard_fragment.attributeIdEd
import kotlinx.android.synthetic.main.wildcard_fragment.clusterIdEd
import kotlinx.android.synthetic.main.wildcard_fragment.endpointIdEd
import kotlinx.android.synthetic.main.wildcard_fragment.outputTv
import kotlinx.android.synthetic.main.wildcard_fragment.view.readBtn
import kotlinx.android.synthetic.main.wildcard_fragment.view.subscribeBtn
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

class WildcardFragment : Fragment() {
private val deviceController: ChipDeviceController
get() = ChipClient.getDeviceController(requireContext())

private lateinit var scope: CoroutineScope

private lateinit var addressUpdateFragment: AddressUpdateFragment

private val reportCallback = object : ReportCallback {
override fun onError(attributePath: ChipAttributePath, ex: Exception) {
Log.i(TAG, "Report error for $attributePath: $ex")
}

override fun onReport(values: Map<ChipAttributePath, Any?>) {
Log.i(TAG, "Received report with ${values.size} values")
val builder = StringBuilder()
values.forEach { builder.append("${it.key}: ${it.value}\n") }
val output = builder.toString()

Log.i(TAG, output)
requireActivity().runOnUiThread { outputTv.text = output }
}
}

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?,
): View {
scope = viewLifecycleOwner.lifecycleScope
return inflater.inflate(R.layout.wildcard_fragment, container, false).apply {
subscribeBtn.setOnClickListener { scope.launch { showSubscribeDialog() } }
readBtn.setOnClickListener { scope.launch { read() } }

addressUpdateFragment =
childFragmentManager.findFragmentById(R.id.addressUpdateFragment) as AddressUpdateFragment
}
}

private suspend fun subscribe(minInterval: Int, maxInterval: Int) {
val subscriptionEstablishedCallback =
SubscriptionEstablishedCallback { Log.i(TAG, "Subscription to device established") }

val endpointId = getChipPathIdForText(endpointIdEd.text.toString())
val clusterId = getChipPathIdForText(clusterIdEd.text.toString())
val attributeId = getChipPathIdForText(attributeIdEd.text.toString())
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)

deviceController.subscribeToPath(subscriptionEstablishedCallback,
reportCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
attributePath,
minInterval,
maxInterval)
}

private suspend fun read() {
val endpointId = getChipPathIdForText(endpointIdEd.text.toString())
val clusterId = getChipPathIdForText(clusterIdEd.text.toString())
val attributeId = getChipPathIdForText(attributeIdEd.text.toString())
val attributePath = ChipAttributePath.newInstance(endpointId, clusterId, attributeId)

deviceController.readPath(reportCallback,
ChipClient.getConnectedDevicePointer(requireContext(),
addressUpdateFragment.deviceId),
attributePath)
}

private fun showSubscribeDialog() {
val dialogView = requireActivity().layoutInflater.inflate(R.layout.subscribe_dialog, null)
val dialog = AlertDialog.Builder(requireContext()).apply {
setView(dialogView)
}.create()

val minIntervalEd = dialogView.findViewById<EditText>(R.id.minIntervalEd)
val maxIntervalEd = dialogView.findViewById<EditText>(R.id.maxIntervalEd)
dialogView.findViewById<Button>(R.id.subscribeBtn).setOnClickListener {
scope.launch {
subscribe(minIntervalEd.text.toString().toInt(), maxIntervalEd.text.toString().toInt())
requireActivity().runOnUiThread { dialog.dismiss() }
}
}
dialog.show()
}

private fun getChipPathIdForText(text: String): ChipPathId {
return if (text.isEmpty()) ChipPathId.forWildcard() else ChipPathId.forId(text.toLong())
}

companion object {
private const val TAG = "WildcardFragment"

fun newInstance(): WildcardFragment = WildcardFragment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
android:layout_marginTop="8dp"
android:visibility="gone"
android:text="@string/attestation_test_btn_text" />

<Button
android:id="@+id/wildcardBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="@string/wildcard_btn_text" />

<Button
android:id="@+id/clusterInteractionBtn"
android:layout_width="wrap_content"
Expand Down
125 changes: 125 additions & 0 deletions src/android/CHIPTool/app/src/main/res/layout/wildcard_fragment.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="16dp"
android:paddingEnd="16dp"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/addressUpdateFragment"
android:name="com.google.chip.chiptool.clusterclient.AddressUpdateFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>

<androidx.constraintlayout.helper.widget.Flow
android:id="@+id/flow"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:constraint_referenced_ids="endpointIdLabel,endpointIdEd,clusterIdLabel,clusterIdEd,attributeIdLabel,attributeIdEd"
app:flow_horizontalBias="0.0"
app:flow_horizontalGap="8dp"
app:flow_horizontalStyle="packed"
app:flow_maxElementsWrap="2"
app:flow_wrapMode="aligned"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/addressUpdateFragment" />

<TextView
android:id="@+id/endpointIdLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:text="@string/endpoint_id_label" />

<EditText
android:id="@+id/endpointIdEd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/wildcard_help_label" />

<TextView
android:id="@+id/clusterIdLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:text="@string/cluster_id_label" />

<EditText
android:id="@+id/clusterIdEd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="number"
android:hint="@string/wildcard_help_label"/>

<TextView
android:id="@+id/attributeIdLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:textSize="16sp"
android:text="@string/attribute_id_label" />

<EditText
android:id="@+id/attributeIdEd"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:inputType="number"
android:hint="@string/wildcard_help_label"/>

<Button
android:id="@+id/readBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_gravity="center"
android:text="@string/wildcard_read_btn_text"
android:textSize="16sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="@id/subscribeBtn"
app:layout_constraintTop_toBottomOf="@id/flow"/>

<Button
android:id="@+id/subscribeBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:layout_gravity="center"
android:text="@string/wildcard_subscribe_btn_text"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="@id/readBtn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/flow"/>

<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fadeScrollbars="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/subscribeBtn">
<TextView
android:id="@+id/outputTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:minLines="4"
android:singleLine="false"
android:textSize="20sp" />
</ScrollView>


</androidx.constraintlayout.widget.ConstraintLayout>
11 changes: 10 additions & 1 deletion src/android/CHIPTool/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
<string name="sensor_client_unsubscribe_error_text">Failed to unsubscribe from the sensor: %1$s</string>
<string name="cluster_interaction_tool">Cluster Interaction Tool</string>

<string name="wildcard_btn_text">Wildcard</string>

<string name="multi_admin_client_btn_text">Multi-admin cluster</string>
<string name="basic_commissioning_method_btn_text">Basic Commissioning Method</string>
<string name="enter_discriminator_hint_text">Enter Discriminator</string>
Expand Down Expand Up @@ -128,11 +130,18 @@
<string name="basic_cluster_reachable_text">Reachable</string>
<string name="basic_cluster_cluster_revision_text">ClusterRevision</string>

<string name="subscribe_dialog_title_text">Subscribe on/off</string>
<string name="subscribe_dialog_title_text">Subscribe</string>
<string name="subscribe_dialog_subscribe_btn_text">Subscribe</string>
<string name="subscribe_dialog_min_interval_hint">Minimum interval (seconds)</string>
<string name="subscribe_dialog_max_interval_hint">Maximum interval (seconds)</string>

<string name="endpoint_id_label">Endpoint ID</string>
<string name="cluster_id_label">Cluster ID</string>
<string name="attribute_id_label">Attribute ID</string>
<string name="wildcard_help_label">Leave blank for wildcard</string>
<string name="wildcard_subscribe_btn_text">Subscribe</string>
<string name="wildcard_read_btn_text">Read</string>

<string name="provision_custom_flow_btn_text">Provision CHIP device with Custom Flow</string>
<string name="chip_device_info_commissioning_flow_label">Commissioning Flow:</string>
<string name="chip_device_info_custom_flow_btn_text">Read from Ledger</string>
Expand Down
14 changes: 14 additions & 0 deletions src/controller/java/AndroidCallbacks-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,17 @@ JNI_METHOD(void, GetConnectedDeviceCallbackJni, deleteCallback)(JNIEnv * env, jo
VerifyOrReturn(connectedDeviceCallback != nullptr, ChipLogError(Controller, "GetConnectedDeviceCallback handle is nullptr"));
delete connectedDeviceCallback;
}

JNI_METHOD(jlong, ReportCallbackJni, newCallback)
(JNIEnv * env, jobject self, jobject subscriptionEstablishedCallbackJava, jobject reportCallbackJava)
{
ReportCallback * reportCallback = new ReportCallback(subscriptionEstablishedCallbackJava, reportCallbackJava);
return reinterpret_cast<jlong>(reportCallback);
}

JNI_METHOD(void, ReportCallbackJni, deleteCallback)(JNIEnv * env, jobject self, jlong callbackHandle)
{
ReportCallback * reportCallback = reinterpret_cast<ReportCallback *>(callbackHandle);
VerifyOrReturn(reportCallback != nullptr, ChipLogError(Controller, "ReportCallback handle is nullptr"));
delete reportCallback;
}
Loading

0 comments on commit b880b8c

Please sign in to comment.