Skip to content

Commit

Permalink
[Android]Pass write response status from jni to application
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Feb 16, 2024
1 parent 5589ecb commit 321140b
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 22 deletions.
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 @@ -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
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ class MatterControllerImpl(params: ControllerParams) : MatterController {
return suspendCancellableCoroutine { continuation ->
val writeCallback =
object : WriteAttributesCallback {
override fun onResponse(attributePath: AttributePath) {
logger.log(Level.INFO, "write success for attributePath:%s", attributePath.toString())
override fun onResponse(attributePath: AttributePath, status: Status) {
logger.log(Level.INFO, "Receive write response for attributePath:%s, status:%s"", attributePath.toString(), logger.log(Level.INFO, "write success for attributePath:%s", status.toString()))
}
override fun onError(attributePath: AttributePath?, ex: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ interface WriteAttributesCallback {
* path.
*
* @param attributePath The attribute path field in write response.
* @param status The attribute status field in write response.
*/
fun onResponse(attributePath: AttributePath)
fun onResponse(attributePath: AttributePath, status: Status)

fun onDone() {}
}

0 comments on commit 321140b

Please sign in to comment.