Skip to content

Commit

Permalink
Use controller exception in Java controller (#26708)
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google authored and pull[bot] committed Aug 10, 2023
1 parent 0f72ac9 commit 854375c
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 40 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: Int, nextResubscribeIntervalMsec: Int) {
override fun onResubscriptionAttempt(terminationCause: Long, nextResubscribeIntervalMsec: Long) {
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 int errorCode;
private long errorCode;
private String errorMessage;

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

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

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

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

Expand Down
56 changes: 32 additions & 24 deletions src/controller/java/AndroidCallbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
#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 @@ -105,16 +106,14 @@ 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"));

// 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)));
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)));

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

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

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

jthrowable exception;
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
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)));

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

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

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

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

jthrowable exception;
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
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)));

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

ChipLogError(Controller, "WriteAttributesCallback ReportError is called");
jthrowable exception;
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
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)));

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

ChipLogError(Controller, "InvokeCallback ReportError is called");
jthrowable exception;
err = AndroidClusterExceptions::GetInstance().CreateIllegalStateException(env, message, errorCode, exception);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Unable to create IllegalStateException: %s", ErrorStr(err)));
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)));

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 Project CHIP Authors
* Copyright (c) 2021-2023 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, jint errorCode, jthrowable & outEx)
CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx)
{
CHIP_ERROR err = CHIP_NO_ERROR;
jmethodID exceptionConstructor;
Expand All @@ -34,10 +34,10 @@ CHIP_ERROR AndroidClusterExceptions::CreateChipClusterException(JNIEnv * env, ji
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
chip::JniClass clusterExceptionJniCls(clusterExceptionCls);

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

outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, errorCode);
outEx = (jthrowable) env->NewObject(clusterExceptionCls, exceptionConstructor, static_cast<jlong>(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 Project CHIP Authors
* Copyright (c) 2021-2023 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, jint errorCode, jthrowable & outEx);
CHIP_ERROR CreateChipClusterException(JNIEnv * env, uint32_t errorCode, jthrowable & outEx);

/**
* Creates a Java IllegalStateException in outEx.
Expand Down
43 changes: 43 additions & 0 deletions src/controller/java/AndroidControllerExceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
*
* Copyright (c) 2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "AndroidControllerExceptions.h"

#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>

namespace chip {

CHIP_ERROR AndroidControllerExceptions::CreateAndroidControllerException(JNIEnv * env, const char * message, uint32_t errorCode,
jthrowable & outEx)
{
jclass controllerExceptionCls;
CHIP_ERROR err = JniReferences::GetInstance().GetClassRef(env, "chip/devicecontroller/ChipDeviceControllerException",
controllerExceptionCls);
VerifyOrReturnError(err == CHIP_NO_ERROR, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
JniClass controllerExceptionJniCls(controllerExceptionCls);

jmethodID exceptionConstructor = env->GetMethodID(controllerExceptionCls, "<init>", "(Jjava/lang/String;)V");
outEx = (jthrowable) env->NewObject(controllerExceptionCls, exceptionConstructor, static_cast<jlong>(errorCode),
env->NewStringUTF(message));
VerifyOrReturnError(outEx != nullptr, CHIP_JNI_ERROR_TYPE_NOT_FOUND);
return CHIP_NO_ERROR;
}

} // namespace chip
45 changes: 45 additions & 0 deletions src/controller/java/AndroidControllerExceptions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
*
* Copyright (c) 2023 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once

#include <jni.h>
#include <lib/core/CHIPError.h>

namespace chip {
class AndroidControllerExceptions
{
public:
AndroidControllerExceptions(const AndroidControllerExceptions &) = delete;
AndroidControllerExceptions(const AndroidControllerExceptions &&) = delete;
AndroidControllerExceptions & operator=(const AndroidControllerExceptions &) = delete;

static AndroidControllerExceptions & GetInstance()
{
static AndroidControllerExceptions androidControllerExceptions;
return androidControllerExceptions;
}

/**
* Creates a Java AndroidControllerException object in outEx.
*/
CHIP_ERROR CreateAndroidControllerException(JNIEnv * env, const char * message, uint32_t errorCode, jthrowable & outEx);

private:
AndroidControllerExceptions() {}
};
} // namespace chip
2 changes: 2 additions & 0 deletions src/controller/java/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ 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 int errorCode;
public long errorCode;

public ChipDeviceControllerException() {}

public ChipDeviceControllerException(int errorCode, String message) {
public ChipDeviceControllerException(long 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(int terminationCause, int nextResubscribeIntervalMsec);
void onResubscriptionAttempt(long terminationCause, long 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, 100);
callbackTestUtil.onDeviceConnectionFailure(jniCallback, 100L);

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

class FakeGetConnectedDeviceCallback implements GetConnectedDeviceCallback {
Expand Down

0 comments on commit 854375c

Please sign in to comment.