Skip to content

Commit

Permalink
[Android]Pass write response status from jni to application (project-…
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored Feb 18, 2024
1 parent acedec1 commit dbc6ed3
Show file tree
Hide file tree
Showing 22 changed files with 296 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import chip.devicecontroller.model.AttributeWriteRequest
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.NodeState
import chip.devicecontroller.model.Status
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
Expand Down Expand Up @@ -191,8 +192,8 @@ class BasicClientFragment : Fragment() {
Log.e(TAG, "Write ${attribute.name} failure", ex)
}

override fun onResponse(attributePath: ChipAttributePath?) {
showMessage("Write ${attribute.name} success")
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
showMessage("Write ${attribute.name} response: $status")
}
},
devicePtr,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import chip.devicecontroller.model.AttributeWriteRequest
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.NodeState
import chip.devicecontroller.model.Status
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.GenericChipDeviceListener
import com.google.chip.chiptool.R
Expand Down Expand Up @@ -223,9 +224,9 @@ class OtaProviderClientFragment : Fragment() {
showMessage("Error : ${e.toString()}")
}

override fun onResponse(attributePath: ChipAttributePath?) {
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
Log.d(TAG, "onResponse")
showMessage("write Success")
showMessage("$attributePath : Write response: $status")
}
},
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId),
Expand Down Expand Up @@ -350,9 +351,9 @@ class OtaProviderClientFragment : Fragment() {
showMessage("error : ${e.toString()}")
}

override fun onResponse(attributePath: ChipAttributePath?) {
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
Log.d(TAG, "onResponse")
showMessage("write success")
showMessage("$attributePath : Write response: $status")
}
},
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import chip.devicecontroller.model.ChipEventPath
import chip.devicecontroller.model.ChipPathId
import chip.devicecontroller.model.InvokeElement
import chip.devicecontroller.model.NodeState
import chip.devicecontroller.model.Status
import com.google.chip.chiptool.ChipClient
import com.google.chip.chiptool.R
import com.google.chip.chiptool.databinding.WildcardFragmentBinding
Expand Down Expand Up @@ -92,8 +93,8 @@ class WildcardFragment : Fragment() {
Log.e(TAG, "Report error for $attributePath: $ex")
}

override fun onResponse(attributePath: ChipAttributePath?) {
val text = "$attributePath : Write Success"
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
val text = "$attributePath : Write response: $status"
requireActivity().runOnUiThread { binding.outputTv.text = text }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ class PairOnNetworkLongImReadCommand(
}

fun checkUnitTestClusterGeneralStatus(status: Status): Boolean =
(status.getStatus() == CLUSTER_ID_TEST_GENERAL_ERROR_STATUS) &&
!status.getClusterStatus().isPresent()
(status.getStatus() == Status.Code.InvalidDataType) && !status.getClusterStatus().isPresent()

fun checkUnitTestClusterClusterStatus(status: Status): Boolean =
(status.getStatus() == CLUSTER_ID_TEST_CLUSTER_ERROR_STATUS) &&
(status.getStatus() == Status.Code.Failure) &&
status.getClusterStatus().isPresent() &&
status.getClusterStatus().get() == CLUSTER_ID_TEST_CLUSTER_ERROR_CLUSTER_STATUS
(status.getClusterStatus().get() == CLUSTER_ID_TEST_CLUSTER_ERROR_CLUSTER_STATUS)

private fun validateResponse(nodeState: NodeState) {
val endpointZero =
Expand Down Expand Up @@ -243,8 +242,6 @@ class PairOnNetworkLongImReadCommand(
private const val CLUSTER_ID_BASIC_VERSION = 0L
private const val CLUSTER_ID_TEST_GENERAL_ERROR_BOOLEAN = 0x0031L
private const val CLUSTER_ID_TEST_CLUSTER_ERROR_BOOLEAN = 0x0032L
private const val CLUSTER_ID_TEST_GENERAL_ERROR_STATUS = 0x8d
private const val CLUSTER_ID_TEST_CLUSTER_ERROR_STATUS = 1
private const val CLUSTER_ID_TEST_CLUSTER_ERROR_CLUSTER_STATUS = 17
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCal
import chip.devicecontroller.WriteAttributesCallback
import chip.devicecontroller.model.AttributeWriteRequest
import chip.devicecontroller.model.ChipAttributePath
import chip.devicecontroller.model.Status
import com.matter.controller.commands.common.CredentialsIssuer
import java.util.logging.Level
import java.util.logging.Logger
Expand Down Expand Up @@ -51,11 +52,8 @@ class PairOnNetworkLongImWriteCommand(
setFailure("write failure")
}

override fun onResponse(attributePath: ChipAttributePath?) {
logger.log(Level.INFO, "Write receive OnResponse on ")
if (attributePath != null) {
logger.log(Level.INFO, attributePath.toString())
}
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
logger.log(Level.INFO, "$attributePath : Write response: $status")
setSuccess()
}
}
Expand Down
4 changes: 2 additions & 2 deletions kotlin-detect-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ style:
- "**/src/controller/java/src/matter/tlv/types.kt"
- "**/src/controller/java/src/matter/tlv/utils.kt"
- "**/src/controller/java/src/matter/tlv/values.kt"
- "**/src/controller/java/src/chip/WildcardImport
examples/android/CHIPTest/app/src/androidTest/java/com/tcl/chip/chiptest/ExampleInstrumentedTest.kt"
- "**/src/controller/java/src/matter/tlv/values.kt"
- "**/src/controller/java/src/matter/controller/model/Status.kt"
- "**/src/controller/java/tests/chip/devicecontroller/cluster/ChipClusterEventStructTest.kt"
- "**/src/controller/java/tests/chip/devicecontroller/cluster/ChipClusterStructTest.kt"
- "**/src/controller/java/tests/matter/jsontlv/JsonToTlvToJsonTest.kt"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ import chip.devicecontroller.model.ClusterState;
import chip.devicecontroller.model.EndpointState;
import chip.devicecontroller.model.InvokeElement;
import chip.devicecontroller.model.NodeState;
import chip.devicecontroller.model.Status;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -318,8 +319,15 @@ public class ChipClusters {
}

@Override
public void onResponse(ChipAttributePath attributePath) {
callback.onSuccess();
public void onResponse(ChipAttributePath attributePath, Status status) {
if (status.getStatus() == Status.Code.Success)
{
callback.onSuccess();
}
else
{
callback.onError(new StatusException(status.getStatus()));
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import chip.devicecontroller.model.EndpointState;
import chip.devicecontroller.model.InvokeElement;
import chip.devicecontroller.model.NodeState;
import chip.devicecontroller.model.Status;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -241,8 +242,15 @@ static class WriteAttributesCallbackImpl implements WriteAttributesCallback {
}

@Override
public void onResponse(ChipAttributePath attributePath) {
callback.onSuccess();
public void onResponse(ChipAttributePath attributePath, Status status) {
if (status.getStatus() == Status.Code.Success)
{
callback.onSuccess();
}
else
{
callback.onError(new StatusException(status.getStatus()));
}
}

@Override
Expand Down
22 changes: 13 additions & 9 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -669,23 +669,27 @@ void WriteAttributesCallback::OnResponse(const app::WriteClient * apWriteClient,
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread"));
JniLocalReferenceScope scope(env);

if (aStatus.mStatus != Protocols::InteractionModel::Status::Success)
{
ReportError(&aPath, aStatus.mStatus);
return;
}

jmethodID onResponseMethod;
VerifyOrReturn(mWrapperCallbackRef.HasValidObjectRef(),
ChipLogError(Controller, "mWrapperCallbackRef is not valid in %s", __func__));
jobject wrapperCallback = mWrapperCallbackRef.ObjectRef();
err = JniReferences::GetInstance().FindMethod(env, wrapperCallback, "onResponse", "(IJJ)V", &onResponseMethod);
err = JniReferences::GetInstance().FindMethod(env, wrapperCallback, "onResponse", "(IJJILjava/lang/Integer;)V",
&onResponseMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to find onError method: %s", ErrorStr(err)));

jobject jClusterState = nullptr;
if (aStatus.mClusterStatus.HasValue())
{
err = JniReferences::GetInstance().CreateBoxedObject<jint>(
"java/lang/Integer", "(I)V", static_cast<jint>(aStatus.mClusterStatus.Value()), jClusterState);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(Controller, "Could not CreateBoxedObject with error %" CHIP_ERROR_FORMAT, err.Format()));
}

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(wrapperCallback, onResponseMethod, static_cast<jint>(aPath.mEndpointId),
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId));
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId), aStatus.mStatus,
jClusterState);
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
}

Expand Down
2 changes: 2 additions & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ kotlin_library("kotlin_matter_controller") {
"src/matter/controller/WriteAttributesCallbackJni.kt",
"src/matter/controller/model/Paths.kt",
"src/matter/controller/model/States.kt",
"src/matter/controller/model/Status.kt",
]

sources += matter_structs_sources
Expand Down Expand Up @@ -477,6 +478,7 @@ android_library("java") {
"src/chip/devicecontroller/ReportCallback.java",
"src/chip/devicecontroller/ReportCallbackJni.java",
"src/chip/devicecontroller/ResubscriptionAttemptCallback.java",
"src/chip/devicecontroller/StatusException.java",
"src/chip/devicecontroller/SubscriptionEstablishedCallback.java",
"src/chip/devicecontroller/ThreadScanResult.java",
"src/chip/devicecontroller/UnpairDeviceCallback.java",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import chip.devicecontroller.model.EndpointState;
import chip.devicecontroller.model.InvokeElement;
import chip.devicecontroller.model.NodeState;
import chip.devicecontroller.model.Status;

import javax.annotation.Nullable;
import java.util.ArrayList;
Expand Down Expand Up @@ -241,8 +242,15 @@ static class WriteAttributesCallbackImpl implements WriteAttributesCallback {
}

@Override
public void onResponse(ChipAttributePath attributePath) {
callback.onSuccess();
public void onResponse(ChipAttributePath attributePath, Status status) {
if (status.getStatus() == Status.Code.Success)
{
callback.onSuccess();
}
else
{
callback.onError(new StatusException(status.getStatus()));
}
}

@Override
Expand Down
34 changes: 34 additions & 0 deletions src/controller/java/src/chip/devicecontroller/StatusException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright (c) 2024 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package chip.devicecontroller;

import chip.devicecontroller.model.Status;

/** Exception class holding error codes defined by Interaction Model */
public class StatusException extends Exception {
private static final long serialVersionUID = 1L;

public Status.Code code = Status.Code.Success;

public StatusException() {}

public StatusException(Status.Code code) {
super(String.format("CHIP IM status error: %s", code.name()));
this.code = code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package chip.devicecontroller;

import chip.devicecontroller.model.ChipAttributePath;
import chip.devicecontroller.model.Status;
import javax.annotation.Nullable;

/** An interface for receiving write response. */
Expand All @@ -40,8 +41,9 @@ public interface WriteAttributesCallback {
* path.
*
* @param attributePath The attribute path field in write response.
* @param status The status field in write response.
*/
void onResponse(ChipAttributePath attributePath);
void onResponse(ChipAttributePath attributePath, Status status);

default void onDone() {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
package chip.devicecontroller;

import chip.devicecontroller.model.ChipAttributePath;
import chip.devicecontroller.model.Status;
import javax.annotation.Nullable;

/** JNI wrapper callback class for {@link WriteAttributesCallback}. */
public final class WriteAttributesCallbackJni {
Expand Down Expand Up @@ -45,9 +47,15 @@ private void onError(
e);
}

private void onResponse(int endpointId, long clusterId, long attributeId) {
private void onResponse(
int endpointId,
long clusterId,
long attributeId,
int status,
@Nullable Integer clusterStatus) {
wrappedWriteAttributesCallback.onResponse(
ChipAttributePath.newInstance(endpointId, clusterId, attributeId));
ChipAttributePath.newInstance(endpointId, clusterId, attributeId),
Status.newInstance(status, clusterStatus));
}

private void onDone() {
Expand Down
Loading

0 comments on commit dbc6ed3

Please sign in to comment.