Skip to content

Commit

Permalink
Revert "Use controller exception in Java controller (#26708)" (#26799)
Browse files Browse the repository at this point in the history
This reverts commit e022057.
  • Loading branch information
yunhanw-google authored May 24, 2023
1 parent 86267e6 commit bf95967
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PairOnNetworkLongImSubscribeCommand(
}

private inner class InternalResubscriptionAttemptCallback : ResubscriptionAttemptCallback {
override fun onResubscriptionAttempt(terminationCause: Long, nextResubscribeIntervalMsec: Long) {
override fun onResubscriptionAttempt(terminationCause: Int, nextResubscribeIntervalMsec: Int) {
logger.log(Level.INFO, "ResubscriptionAttemptCallback");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@
import java.util.Objects;

public class MatterError {
private long errorCode;
private int errorCode;
private String errorMessage;

public static final MatterError DISCOVERY_SERVICE_LOST =
new MatterError(4L, "Discovery service was lost.");
new MatterError(4, "Discovery service was lost.");

public static final MatterError NO_ERROR = new MatterError(0, null);

public MatterError(long errorCode, String errorMessage) {
public MatterError(int errorCode, String errorMessage) {
this.errorCode = errorCode;
this.errorMessage = errorMessage;
}
Expand All @@ -37,7 +37,7 @@ public boolean isNoError() {
return this.equals(NO_ERROR);
}

public long getErrorCode() {
public int getErrorCode() {
return errorCode;
}

Expand Down
56 changes: 24 additions & 32 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
*/
#include "AndroidCallbacks.h"
#include <controller/java/AndroidClusterExceptions.h>
#include <controller/java/AndroidControllerExceptions.h>
#include <controller/java/CHIPAttributeTLVValueDecoder.h>
#include <controller/java/CHIPEventTLVValueDecoder.h>
#include <jni.h>
Expand Down Expand Up @@ -106,14 +105,16 @@ void GetConnectedDeviceCallback::OnDeviceConnectionFailureFn(void * context, con
JniReferences::GetInstance().FindMethod(env, javaCallback, "onConnectionFailure", "(JLjava/lang/Exception;)V", &failureMethod);
VerifyOrReturn(failureMethod != nullptr, ChipLogError(Controller, "Could not find onConnectionFailure method"));

jthrowable exception;
CHIP_ERROR err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, ErrorStr(error),
error.AsInteger(), exception);
VerifyOrReturn(
err == CHIP_NO_ERROR,
ChipLogError(Controller,
"Unable to create AndroidControllerException on GetConnectedDeviceCallback::OnDeviceConnectionFailureFn: %s",
ErrorStr(err)));
// Create the exception to return.
jclass controllerExceptionCls;
CHIP_ERROR err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ChipDeviceControllerException",
controllerExceptionCls);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Could not find exception type for onConnectionFailure"));
JniClass controllerExceptionJniCls(controllerExceptionCls);

jmethodID exceptionConstructor = env->GetMethodID(controllerExceptionCls, "<init>", "(ILjava/lang/String;)V");
jobject exception =
env->NewObject(controllerExceptionCls, exceptionConstructor, error.AsInteger(), env->NewStringUTF(ErrorStr(error)));

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(javaCallback, failureMethod, peerId.GetNodeId(), exception);
Expand Down Expand Up @@ -557,12 +558,12 @@ CHIP_ERROR ReportCallback::OnResubscriptionNeeded(app::ReadClient * apReadClient

jmethodID onResubscriptionAttemptMethod;
ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod(
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod));
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod));

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
static_cast<jlong>(aTerminationCause.AsInteger()),
static_cast<jlong>(apReadClient->ComputeTimeTillNextSubscription()));
static_cast<jint>(aTerminationCause.AsInteger()),
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));
VerifyOrReturnError(!env->ExceptionCheck(), CHIP_JNI_ERROR_EXCEPTION_THROWN);
return CHIP_NO_ERROR;
}
Expand All @@ -584,10 +585,8 @@ void ReportCallback::ReportError(jobject attributePath, jobject eventPath, const
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();

jthrowable exception;
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
VerifyOrReturn(
err == CHIP_NO_ERROR,
ChipLogError(Controller, "Unable to create AndroidControllerException on ReportCallback::ReportError: %s", ErrorStr(err)));
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));

jmethodID onErrorMethod;
err = JniReferences::GetInstance().FindMethod(
Expand Down Expand Up @@ -814,12 +813,12 @@ CHIP_ERROR ReportEventCallback::OnResubscriptionNeeded(app::ReadClient * apReadC

jmethodID onResubscriptionAttemptMethod;
ReturnLogErrorOnFailure(JniReferences::GetInstance().FindMethod(
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(JJ)V", &onResubscriptionAttemptMethod));
env, mResubscriptionAttemptCallbackRef, "onResubscriptionAttempt", "(II)V", &onResubscriptionAttemptMethod));

DeviceLayer::StackUnlock unlock;
env->CallVoidMethod(mResubscriptionAttemptCallbackRef, onResubscriptionAttemptMethod,
static_cast<jlong>(aTerminationCause.AsInteger()),
static_cast<jlong>(apReadClient->ComputeTimeTillNextSubscription()));
static_cast<jint>(aTerminationCause.AsInteger()),
static_cast<jint>(apReadClient->ComputeTimeTillNextSubscription()));

return CHIP_NO_ERROR;
}
Expand All @@ -840,10 +839,8 @@ void ReportEventCallback::ReportError(jobject eventPath, const char * message, C
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();

jthrowable exception;
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(Controller, "Unable to create AndroidControllerException: %s on eportEventCallback::ReportError",
ErrorStr(err)));
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));

jmethodID onErrorMethod;
err = JniReferences::GetInstance().FindMethod(
Expand Down Expand Up @@ -946,11 +943,8 @@ void WriteAttributesCallback::ReportError(jobject attributePath, const char * me

ChipLogError(Controller, "WriteAttributesCallback ReportError is called");
jthrowable exception;
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR,
ChipLogError(Controller,
"Unable to create AndroidControllerException on WriteAttributesCallback::ReportError: %s",
ErrorStr(err)));
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));

jmethodID onErrorMethod;
err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onError",
Expand Down Expand Up @@ -1049,10 +1043,8 @@ void InvokeCallback::ReportError(const char * message, ChipError::StorageType er

ChipLogError(Controller, "InvokeCallback ReportError is called");
jthrowable exception;
err = AndroidControllerExceptions::GetInstance().CreateAndroidControllerException(env, message, errorCode, exception);
VerifyOrReturn(
err == CHIP_NO_ERROR,
ChipLogError(Controller, "Unable to create AndroidControllerException: %s on InvokeCallback::ReportError", ErrorStr(err)));
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));

jmethodID onErrorMethod;
err = JniReferences::GetInstance().FindMethod(env, mJavaCallbackRef, "onError", "(Ljava/lang/Exception;)V", &onErrorMethod);
Expand Down
8 changes: 4 additions & 4 deletions src/controller/java/AndroidClusterExceptions.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021-2023 Project CHIP Authors
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,7 +24,7 @@

namespace chip {

CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx)
CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx)
{
CHIP_ERROR err = CHIP_NO_ERROR;
jmethodID exceptionConstructor;
Expand All @@ -34,10 +34,10 @@ CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, ui
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
chip::JniClass clusterExceptionJniCls(clusterExceptionCls);

exceptionConstructor = env->GetMethodID(clusterExceptionCls, "<init>", "(J)V");
exceptionConstructor = env->GetMethodID(clusterExceptionCls, "<init>", "(I)V");
VerifyOrReturnError(exceptionConstructor != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);

outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, static_cast<jlong>(errorCode));
outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, errorCode);
VerifyOrReturnError(outEx != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);

return err;
Expand Down
4 changes: 2 additions & 2 deletions src/controller/java/AndroidClusterExceptions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
*
* Copyright (c) 2021-2023 Project CHIP Authors
* Copyright (c) 2021 Project CHIP Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,7 +37,7 @@ class AndroidClusterExceptions
/**
* Creates a Java ChipClusterException object in outEx.
*/
CHIP_ERROR CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx);
CHIP_ERROR CreateChipClusterException(JNIEnv * env, jint errorCode, jthrowable & outEx);

/**
* Creates a Java IllegalStateException in outEx.
Expand Down
43 changes: 0 additions & 43 deletions src/controller/java/AndroidControllerExceptions.cpp

This file was deleted.

45 changes: 0 additions & 45 deletions src/controller/java/AndroidControllerExceptions.h

This file was deleted.

2 changes: 0 additions & 2 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ shared_library("jni") {
"AndroidClusterExceptions.h",
"AndroidCommissioningWindowOpener.cpp",
"AndroidCommissioningWindowOpener.h",
"AndroidControllerExceptions.cpp",
"AndroidControllerExceptions.h",
"AndroidCurrentFabricRemover.cpp",
"AndroidCurrentFabricRemover.h",
"AndroidDeviceControllerWrapper.cpp",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
public class ChipDeviceControllerException extends RuntimeException {
private static final long serialVersionUID = 1L;

public long errorCode;
public int errorCode;

public ChipDeviceControllerException() {}

public ChipDeviceControllerException(long errorCode, String message) {
public ChipDeviceControllerException(int errorCode, String message) {
super(message != null ? message : String.format("Error Code %d", errorCode));
this.errorCode = errorCode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
package chip.devicecontroller;

public interface ResubscriptionAttemptCallback {
void onResubscriptionAttempt(long terminationCause, long nextResubscribeIntervalMsec);
void onResubscriptionAttempt(int terminationCause, int nextResubscribeIntervalMsec);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public void deviceConnected() {
public void connectionFailure() {
var callback = new FakeGetConnectedDeviceCallback();
var jniCallback = new GetConnectedDeviceCallbackJni(callback);
callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100L);
callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100);

assertThat(callback.error).isInstanceOf(ChipDeviceControllerException.class);
assertThat(((ChipDeviceControllerException) callback.error).errorCode).isEqualTo(100L);
assertThat(((ChipDeviceControllerException) callback.error).errorCode).isEqualTo(100);
}

class FakeGetConnectedDeviceCallback implements GetConnectedDeviceCallback {
Expand Down

0 comments on commit bf95967

Please sign in to comment.