Skip to content

Commit 925ca4e

Browse files
[Android]Pass write response status from jni to application
1 parent acedec1 commit 925ca4e

File tree

21 files changed

+286
-50
lines changed

21 files changed

+286
-50
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import chip.devicecontroller.model.AttributeWriteRequest
1717
import chip.devicecontroller.model.ChipAttributePath
1818
import chip.devicecontroller.model.ChipEventPath
1919
import chip.devicecontroller.model.NodeState
20+
import chip.devicecontroller.model.Status
2021
import com.google.chip.chiptool.ChipClient
2122
import com.google.chip.chiptool.GenericChipDeviceListener
2223
import com.google.chip.chiptool.R
@@ -191,8 +192,8 @@ class BasicClientFragment : Fragment() {
191192
Log.e(TAG, "Write ${attribute.name} failure", ex)
192193
}
193194

194-
override fun onResponse(attributePath: ChipAttributePath?) {
195-
showMessage("Write ${attribute.name} success")
195+
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
196+
showMessage("Write ${attribute.name} response: $status")
196197
}
197198
},
198199
devicePtr,

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import chip.devicecontroller.model.AttributeWriteRequest
3232
import chip.devicecontroller.model.ChipAttributePath
3333
import chip.devicecontroller.model.ChipEventPath
3434
import chip.devicecontroller.model.NodeState
35+
import chip.devicecontroller.model.Status
3536
import com.google.chip.chiptool.ChipClient
3637
import com.google.chip.chiptool.GenericChipDeviceListener
3738
import com.google.chip.chiptool.R
@@ -223,9 +224,9 @@ class OtaProviderClientFragment : Fragment() {
223224
showMessage("Error : ${e.toString()}")
224225
}
225226

226-
override fun onResponse(attributePath: ChipAttributePath?) {
227+
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
227228
Log.d(TAG, "onResponse")
228-
showMessage("write Success")
229+
showMessage("$attributePath : Write response: $status")
229230
}
230231
},
231232
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId),
@@ -350,9 +351,9 @@ class OtaProviderClientFragment : Fragment() {
350351
showMessage("error : ${e.toString()}")
351352
}
352353

353-
override fun onResponse(attributePath: ChipAttributePath?) {
354+
override fun onResponse(attributePath: ChipAttributePath, status: Status) {
354355
Log.d(TAG, "onResponse")
355-
showMessage("write success")
356+
showMessage("$attributePath : Write response: $status")
356357
}
357358
},
358359
ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId),

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/PairOnNetworkLongImReadCommand.kt

+3-6
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,12 @@ class PairOnNetworkLongImReadCommand(
6767
}
6868

6969
fun checkUnitTestClusterGeneralStatus(status: Status): Boolean =
70-
(status.getStatus() == CLUSTER_ID_TEST_GENERAL_ERROR_STATUS) &&
71-
!status.getClusterStatus().isPresent()
70+
(status.getStatus() == Status.Code.InvalidDataType) && !status.getClusterStatus().isPresent()
7271

7372
fun checkUnitTestClusterClusterStatus(status: Status): Boolean =
74-
(status.getStatus() == CLUSTER_ID_TEST_CLUSTER_ERROR_STATUS) &&
73+
(status.getStatus() == Status.Code.Failure) &&
7574
status.getClusterStatus().isPresent() &&
76-
status.getClusterStatus().get() == CLUSTER_ID_TEST_CLUSTER_ERROR_CLUSTER_STATUS
75+
(status.getClusterStatus().get() == CLUSTER_ID_TEST_CLUSTER_ERROR_CLUSTER_STATUS)
7776

7877
private fun validateResponse(nodeState: NodeState) {
7978
val endpointZero =
@@ -243,8 +242,6 @@ class PairOnNetworkLongImReadCommand(
243242
private const val CLUSTER_ID_BASIC_VERSION = 0L
244243
private const val CLUSTER_ID_TEST_GENERAL_ERROR_BOOLEAN = 0x0031L
245244
private const val CLUSTER_ID_TEST_CLUSTER_ERROR_BOOLEAN = 0x0032L
246-
private const val CLUSTER_ID_TEST_GENERAL_ERROR_STATUS = 0x8d
247-
private const val CLUSTER_ID_TEST_CLUSTER_ERROR_STATUS = 1
248245
private const val CLUSTER_ID_TEST_CLUSTER_ERROR_CLUSTER_STATUS = 17
249246
}
250247
}

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
}

kotlin-detect-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ style:
3636
- "**/src/controller/java/src/matter/tlv/types.kt"
3737
- "**/src/controller/java/src/matter/tlv/utils.kt"
3838
- "**/src/controller/java/src/matter/tlv/values.kt"
39-
- "**/src/controller/java/src/chip/WildcardImport
40-
examples/android/CHIPTest/app/src/androidTest/java/com/tcl/chip/chiptest/ExampleInstrumentedTest.kt"
39+
- "**/src/controller/java/src/matter/tlv/values.kt"
40+
- "**/src/controller/java/src/matter/controller/model/Status.kt"
4141
- "**/src/controller/java/tests/chip/devicecontroller/cluster/ChipClusterEventStructTest.kt"
4242
- "**/src/controller/java/tests/chip/devicecontroller/cluster/ChipClusterStructTest.kt"
4343
- "**/src/controller/java/tests/matter/jsontlv/JsonToTlvToJsonTest.kt"

scripts/py_matter_idl/matter_idl/generators/java/ChipClusters_java.jinja

+10-2
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ import chip.devicecontroller.model.ClusterState;
102102
import chip.devicecontroller.model.EndpointState;
103103
import chip.devicecontroller.model.InvokeElement;
104104
import chip.devicecontroller.model.NodeState;
105+
import chip.devicecontroller.model.Status;
105106

106107
import javax.annotation.Nullable;
107108
import java.util.ArrayList;
@@ -318,8 +319,15 @@ public class ChipClusters {
318319
}
319320

320321
@Override
321-
public void onResponse(ChipAttributePath attributePath) {
322-
callback.onSuccess();
322+
public void onResponse(ChipAttributePath attributePath, Status status) {
323+
if (status.getStatus() == Status.Code.Success)
324+
{
325+
callback.onSuccess();
326+
}
327+
else
328+
{
329+
callback.onError(new StatusException(status.getStatus()));
330+
}
323331
}
324332

325333
@Override

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/BUILD.gn

+2
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ kotlin_library("kotlin_matter_controller") {
391391
"src/matter/controller/WriteAttributesCallbackJni.kt",
392392
"src/matter/controller/model/Paths.kt",
393393
"src/matter/controller/model/States.kt",
394+
"src/matter/controller/model/Status.kt",
394395
]
395396

396397
sources += matter_structs_sources
@@ -477,6 +478,7 @@ android_library("java") {
477478
"src/chip/devicecontroller/ReportCallback.java",
478479
"src/chip/devicecontroller/ReportCallbackJni.java",
479480
"src/chip/devicecontroller/ResubscriptionAttemptCallback.java",
481+
"src/chip/devicecontroller/StatusException.java",
480482
"src/chip/devicecontroller/SubscriptionEstablishedCallback.java",
481483
"src/chip/devicecontroller/ThreadScanResult.java",
482484
"src/chip/devicecontroller/UnpairDeviceCallback.java",

src/controller/java/generated/java/chip/devicecontroller/ChipClusters.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import chip.devicecontroller.model.EndpointState;
2626
import chip.devicecontroller.model.InvokeElement;
2727
import chip.devicecontroller.model.NodeState;
28+
import chip.devicecontroller.model.Status;
2829

2930
import javax.annotation.Nullable;
3031
import java.util.ArrayList;
@@ -241,8 +242,15 @@ static class WriteAttributesCallbackImpl implements WriteAttributesCallback {
241242
}
242243

243244
@Override
244-
public void onResponse(ChipAttributePath attributePath) {
245-
callback.onSuccess();
245+
public void onResponse(ChipAttributePath attributePath, Status status) {
246+
if (status.getStatus() == Status.Code.Success)
247+
{
248+
callback.onSuccess();
249+
}
250+
else
251+
{
252+
callback.onError(new StatusException(status.getStatus()));
253+
}
246254
}
247255

248256
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2024 Project CHIP Authors
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
package chip.devicecontroller;
19+
20+
import chip.devicecontroller.model.Status;
21+
22+
/** Exception class holding error codes defined by Interaction Model */
23+
public class StatusException extends Exception {
24+
private static final long serialVersionUID = 1L;
25+
26+
public Status.Code code = Status.Code.Success;
27+
28+
public StatusException() {}
29+
30+
public StatusException(Status.Code code) {
31+
super(String.format("CHIP IM status error: %s", code.name()));
32+
this.code = code;
33+
}
34+
}

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() {

0 commit comments

Comments
 (0)