Skip to content

Commit 4e0f4c3

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

File tree

15 files changed

+175
-37
lines changed

15 files changed

+175
-37
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/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
}

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.StatusCode.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

+1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,7 @@ android_library("java") {
477477
"src/chip/devicecontroller/ReportCallback.java",
478478
"src/chip/devicecontroller/ReportCallbackJni.java",
479479
"src/chip/devicecontroller/ResubscriptionAttemptCallback.java",
480+
"src/chip/devicecontroller/StatusException.java",
480481
"src/chip/devicecontroller/SubscriptionEstablishedCallback.java",
481482
"src/chip/devicecontroller/ThreadScanResult.java",
482483
"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.StatusCode.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 clusters. */
23+
public class StatusException extends Exception {
24+
private static final long serialVersionUID = 1L;
25+
26+
public Status.StatusCode code = Status.StatusCode.Success;
27+
28+
public StatusException() {}
29+
30+
public StatusException(Status.StatusCode 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() {

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

+73-5
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,84 @@
2222
import java.util.Optional;
2323

2424
public final class Status {
25-
private Integer status;
25+
public enum StatusCode {
26+
Success(0x0),
27+
Failure(0x01),
28+
InvalidSusbscription(0x7d),
29+
UnsupportedAccess(0x7e),
30+
UnsupportedEndPoint(0x7f),
31+
InvalidAction(0x80),
32+
UnsupportedCommand(0x81),
33+
Deprecated82(0x82),
34+
Deprecated83(0x83),
35+
Deprecated84(0x84),
36+
InvalidCommand(0x85),
37+
UnsupportedAttribute(0x86),
38+
ConstraintError(0x87),
39+
UnsupportedWrite(0x88),
40+
ResourceExhausted(0x89),
41+
Deprecated8a(0x8a),
42+
NotFound(0x8b),
43+
UnreportableAttribute(0x8c),
44+
InvalidDataType(0x8d),
45+
Deprecated8e(0x8e),
46+
UnsupportedRead(0x8f),
47+
Deprecated90(0x90),
48+
Deprecated91(0x91),
49+
DataVersionMismatch(0x92),
50+
Deprecated93(0x93),
51+
Timeout(0x94),
52+
Reserved95(0x95),
53+
Reserved96(0x96),
54+
Reserved97(0x97),
55+
Reserved98(0x98),
56+
Reserved99(0x99),
57+
Reserved9a(0x9a),
58+
Busy(0x9c),
59+
Deprecatedc0(0xc0),
60+
Deprecatedc1(0xc1),
61+
Deprecatedc2(0xc2),
62+
UnsupportedCluster(0xc3),
63+
Deprecatedc4(0xc4),
64+
NoUpstreamSubsricption(0xc5),
65+
NeedTimedInteraction(0xc6),
66+
UnsupportedEvent(0xc7),
67+
PathExhausted(0xc8),
68+
TimedRequestMismatch(0xc9),
69+
FailsafeRequired(0xca),
70+
InvalidInState(0xcb),
71+
NoCommandResponse(0xcc);
72+
73+
private int id = 0;
74+
75+
StatusCode(int id) {
76+
this.id = id;
77+
}
78+
79+
public int getId() {
80+
return id;
81+
}
82+
83+
public static StatusCode fromId(int id) {
84+
for (StatusCode type : values()) {
85+
if (type.getId() == id) {
86+
return type;
87+
}
88+
}
89+
return null;
90+
}
91+
}
92+
93+
private StatusCode status = StatusCode.Success;
2694
private Optional<Integer> clusterStatus;
2795

2896
private Status(int status, Optional<Integer> clusterStatus) {
29-
this.status = status;
97+
this.status = StatusCode.fromId(status);
3098
this.clusterStatus = clusterStatus;
3199
}
32100

33101
// Getters
34-
public Integer getStatus() {
102+
public StatusCode getStatus() {
35103
return status;
36104
}
37105

@@ -43,7 +111,7 @@ public String toString() {
43111
return String.format(
44112
Locale.ENGLISH,
45113
"status %s, clusterStatus %s",
46-
String.valueOf(status),
114+
status.name(),
47115
clusterStatus.isPresent() ? String.valueOf(clusterStatus.get()) : "None");
48116
}
49117

@@ -55,7 +123,7 @@ public static Status newInstance(int status) {
55123
return new Status(status, Optional.empty());
56124
}
57125

58-
static Status newInstance(int status, Integer clusterStatus) {
126+
public static Status newInstance(int status, Integer clusterStatus) {
59127
return new Status(status, Optional.ofNullable(clusterStatus));
60128
}
61129
}

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)