Skip to content

Commit 541f2a9

Browse files
[Android]Pass write response status from jni to application
1 parent 5589ecb commit 541f2a9

File tree

8 files changed

+39
-23
lines changed

8 files changed

+39
-23
lines changed

examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/WildcardFragment.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import chip.devicecontroller.model.ChipEventPath
2727
import chip.devicecontroller.model.ChipPathId
2828
import chip.devicecontroller.model.InvokeElement
2929
import chip.devicecontroller.model.NodeState
30+
import chip.devicecontroller.model.Status
3031
import com.google.chip.chiptool.ChipClient
3132
import com.google.chip.chiptool.R
3233
import com.google.chip.chiptool.databinding.WildcardFragmentBinding
@@ -92,8 +93,8 @@ class WildcardFragment : Fragment() {
9293
Log.e(TAG, "Report error for $attributePath: $ex")
9394
}
9495

95-
override fun onResponse(attributePath: ChipAttributePath?) {
96-
val text = "$attributePath : Write Success"
96+
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
97+
val text = "$attributePath : Write response: $Status"
9798
requireActivity().runOnUiThread { binding.outputTv.text = text }
9899
}
99100

examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongImWriteCommand.kt

+3-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import chip.devicecontroller.GetConnectedDeviceCallbackJni.GetConnectedDeviceCal
2222
import chip.devicecontroller.WriteAttributesCallback
2323
import chip.devicecontroller.model.AttributeWriteRequest
2424
import chip.devicecontroller.model.ChipAttributePath
25+
import chip.devicecontroller.model.Status
2526
import com.matter.controller.commands.common.CredentialsIssuer
2627
import java.util.logging.Level
2728
import java.util.logging.Logger
@@ -51,11 +52,8 @@ class PairOnNetworkLongImWriteCommand(
5152
setFailure("write failure")
5253
}
5354

54-
override fun onResponse(attributePath: ChipAttributePath?) {
55-
logger.log(Level.INFO, "Write receive OnResponse on ")
56-
if (attributePath != null) {
57-
logger.log(Level.INFO, attributePath.toString())
58-
}
55+
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
56+
logger.log(Level.INFO, "$attributePath : Write response: $Status")
5957
setSuccess()
6058
}
6159
}

src/controller/java/AndroidCallbacks.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -669,23 +669,27 @@ void WriteAttributesCallback::OnResponse(const app::WriteClient * apWriteClient,
669669
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
670670
VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread"));
671671
JniLocalReferenceScope scope(env);
672-
673-
if (aStatus.mStatus != Protocols::InteractionModel::Status::Success)
674-
{
675-
ReportError(&aPath, aStatus.mStatus);
676-
return;
677-
}
678-
679672
jmethodID onResponseMethod;
680673
VerifyOrReturn(mWrapperCallbackRef.HasValidObjectRef(),
681674
ChipLogError(Controller, "mWrapperCallbackRef is not valid in %s", __func__));
682675
jobject wrapperCallback = mWrapperCallbackRef.ObjectRef();
683-
err = JniReferences::GetInstance().FindMethod(env, wrapperCallback, "onResponse", "(IJJ)V", &onResponseMethod);
676+
err = JniReferences::GetInstance().FindMethod(env, wrapperCallback, "onResponse", "(IJJILjava/lang/Integer;)V",
677+
&onResponseMethod);
684678
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to find onError method: %s", ErrorStr(err)));
685679

680+
jobject jClusterState = nullptr;
681+
if (aStatus.mClusterStatus.HasValue())
682+
{
683+
err = JniReferences::GetInstance().CreateBoxedObject<jint>(
684+
"java/lang/Integer", "(I)V", static_cast<jint>(aStatus.mClusterStatus.Value()), jClusterState);
685+
VerifyOrReturn(err == CHIP_NO_ERROR,
686+
ChipLogError(Controller, "Could not CreateBoxedObject with error %" CHIP_ERROR_FORMAT, err.Format()));
687+
}
688+
686689
DeviceLayer::StackUnlock unlock;
687690
env->CallVoidMethod(wrapperCallback, onResponseMethod, static_cast<jint>(aPath.mEndpointId),
688-
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId));
691+
static_cast<jlong>(aPath.mClusterId), static_cast<jlong>(aPath.mAttributeId), aStatus.mStatus,
692+
jClusterState);
689693
VerifyOrReturn(!env->ExceptionCheck(), env->ExceptionDescribe());
690694
}
691695

src/controller/java/src/chip/devicecontroller/WriteAttributesCallback.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package chip.devicecontroller;
1919

2020
import chip.devicecontroller.model.ChipAttributePath;
21+
import chip.devicecontroller.model.Status;
2122
import javax.annotation.Nullable;
2223

2324
/** An interface for receiving write response. */
@@ -40,8 +41,9 @@ public interface WriteAttributesCallback {
4041
* path.
4142
*
4243
* @param attributePath The attribute path field in write response.
44+
* @param status The status field in write response.
4345
*/
44-
void onResponse(ChipAttributePath attributePath);
46+
void onResponse(ChipAttributePath attributePath, Status status);
4547

4648
default void onDone() {}
4749
}

src/controller/java/src/chip/devicecontroller/WriteAttributesCallbackJni.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
package chip.devicecontroller;
1919

2020
import chip.devicecontroller.model.ChipAttributePath;
21+
import chip.devicecontroller.model.Status;
22+
import javax.annotation.Nullable;
2123

2224
/** JNI wrapper callback class for {@link WriteAttributesCallback}. */
2325
public final class WriteAttributesCallbackJni {
@@ -45,9 +47,15 @@ private void onError(
4547
e);
4648
}
4749

48-
private void onResponse(int endpointId, long clusterId, long attributeId) {
50+
private void onResponse(
51+
int endpointId,
52+
long clusterId,
53+
long attributeId,
54+
int status,
55+
@Nullable Integer clusterStatus) {
4956
wrappedWriteAttributesCallback.onResponse(
50-
ChipAttributePath.newInstance(endpointId, clusterId, attributeId));
57+
ChipAttributePath.newInstance(endpointId, clusterId, attributeId),
58+
Status.newInstance(status, clusterStatus));
5159
}
5260

5361
private void onDone() {

src/controller/java/src/chip/devicecontroller/model/Status.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static Status newInstance(int status) {
5555
return new Status(status, Optional.empty());
5656
}
5757

58-
static Status newInstance(int status, Integer clusterStatus) {
58+
public static Status newInstance(int status, Integer clusterStatus) {
5959
return new Status(status, Optional.ofNullable(clusterStatus));
6060
}
6161
}

src/controller/java/src/matter/controller/MatterControllerImpl.kt

+4-2
Original file line numberDiff line numberDiff line change
@@ -340,8 +340,10 @@ class MatterControllerImpl(params: ControllerParams) : MatterController {
340340
return suspendCancellableCoroutine { continuation ->
341341
val writeCallback =
342342
object : WriteAttributesCallback {
343-
override fun onResponse(attributePath: AttributePath) {
344-
logger.log(Level.INFO, "write success for attributePath:%s", attributePath.toString())
343+
override fun onResponse(attributePath: AttributePath, status: Status) {
344+
logger.log(Level.INFO, "Receive write response for attributePath:%s, status:%s"",
345+
attributePath.toString(), logger.log(Level.INFO, "write success for attributePath:%s",
346+
status.toString()))
345347
}
346348
347349
override fun onError(attributePath: AttributePath?, ex: Exception) {

src/controller/java/src/matter/controller/WriteAttributesCallback.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ interface WriteAttributesCallback {
3838
* path.
3939
*
4040
* @param attributePath The attribute path field in write response.
41+
* @param status The attribute status field in write response.
4142
*/
42-
fun onResponse(attributePath: AttributePath)
43+
fun onResponse(attributePath: AttributePath, status: Status)
4344

4445
fun onDone() {}
4546
}

0 commit comments

Comments
 (0)