Skip to content

Commit

Permalink
[Android]CHIP_ERROR should be handled by Long in Java/Kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhanw-google committed Mar 25, 2024
1 parent 04251f1 commit 48078be
Show file tree
Hide file tree
Showing 13 changed files with 88 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ open class GenericChipDeviceListener : ChipDeviceController.CompletionListener {
// No op
}

override fun onPairingComplete(code: Int) {
override fun onPairingComplete(code: Long) {
// No op
}

override fun onPairingDeleted(code: Int) {
override fun onPairingDeleted(code: Long) {
// No op
}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
// No op
}

Expand All @@ -33,7 +33,7 @@ open class GenericChipDeviceListener : ChipDeviceController.CompletionListener {
// No op
}

override fun onCommissioningStatusUpdate(nodeId: Long, stage: String, errorCode: Int) {
override fun onCommissioningStatusUpdate(nodeId: Long, stage: String, errorCode: Long) {
// No op
}

Expand All @@ -57,7 +57,7 @@ open class GenericChipDeviceListener : ChipDeviceController.CompletionListener {
// No op
}

override fun onICDRegistrationComplete(errorCode: Int, icdDeviceInfo: ICDDeviceInfo) {
override fun onICDRegistrationComplete(errorCode: Long, icdDeviceInfo: ICDDeviceInfo) {
// No op
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ class DeviceProvisioningFragment : Fragment() {
Log.d(TAG, "Pairing status update: $status")
}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
if (errorCode == STATUS_PAIRING_SUCCESS) {
FragmentUtil.getHost(this@DeviceProvisioningFragment, Callback::class.java)
?.onCommissioningComplete(0, nodeId)
Expand All @@ -260,7 +260,7 @@ class DeviceProvisioningFragment : Fragment() {
}
}

override fun onPairingComplete(code: Int) {
override fun onPairingComplete(code: Long) {
Log.d(TAG, "onPairingComplete: $code")

if (code != STATUS_PAIRING_SUCCESS) {
Expand All @@ -274,7 +274,7 @@ class DeviceProvisioningFragment : Fragment() {
Log.d(TAG, String(csr))
}

override fun onPairingDeleted(code: Int) {
override fun onPairingDeleted(code: Long) {
Log.d(TAG, "onPairingDeleted: $code")
}

Expand All @@ -293,7 +293,7 @@ class DeviceProvisioningFragment : Fragment() {
)
}

override fun onICDRegistrationComplete(errorCode: Int, icdDeviceInfo: ICDDeviceInfo) {
override fun onICDRegistrationComplete(errorCode: Long, icdDeviceInfo: ICDDeviceInfo) {
Log.d(
TAG,
"onICDRegistrationComplete - errorCode: $errorCode, symmetricKey : ${icdDeviceInfo.symmetricKey.toHex()}, icdDeviceInfo : $icdDeviceInfo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class PairAddressPaseCommand(controller: ChipDeviceController, credsIssue: Crede
PairingModeType.ADDRESS_PASE_ONLY,
PairingNetworkType.NONE
) {
override fun onPairingComplete(errorCode: Int) {
override fun onPairingComplete(errorCode: Long) {
logger.log(Level.INFO, "onPairingComplete with error code: $errorCode")
if (errorCode == 0) {
setSuccess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,18 +127,18 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onStatusUpdate with status: $status")
}

override fun onPairingComplete(errorCode: Int) {
override fun onPairingComplete(errorCode: Long) {
logger.log(Level.INFO, "onPairingComplete with error code: $errorCode")
if (errorCode != 0) {
setFailure("onPairingComplete failure")
}
}

override fun onPairingDeleted(errorCode: Int) {
override fun onPairingDeleted(errorCode: Long) {
logger.log(Level.INFO, "onPairingDeleted with error code: $errorCode")
}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
logger.log(Level.INFO, "onCommissioningComplete with error code: $errorCode")
if (errorCode == 0) {
setSuccess()
Expand All @@ -156,7 +156,7 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onReadCommissioningInfo")
}

override fun onCommissioningStatusUpdate(nodeId: Long, stage: String?, errorCode: Int) {
override fun onCommissioningStatusUpdate(nodeId: Long, stage: String?, errorCode: Long) {
logger.log(Level.INFO, "onCommissioningStatusUpdate")
}

Expand Down Expand Up @@ -186,7 +186,7 @@ abstract class PairingCommand(
.updateCommissioningICDRegistrationInfo(ICDRegistrationInfo.newBuilder().build())
}

override fun onICDRegistrationComplete(errorCode: Int, icdDeviceInfo: ICDDeviceInfo) {
override fun onICDRegistrationComplete(errorCode: Long, icdDeviceInfo: ICDDeviceInfo) {
logger.log(
Level.INFO,
"onICDRegistrationComplete with errorCode: $errorCode, symmetricKey: ${icdDeviceInfo.symmetricKey.toHex()}, icdDeviceInfo: $icdDeviceInfo"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,18 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onStatusUpdate with status: $status")
}

override fun onPairingComplete(errorCode: Int) {
override fun onPairingComplete(errorCode: Long) {
logger.log(Level.INFO, "onPairingComplete with error code: $errorCode")
if (errorCode != 0) {
setFailure("onPairingComplete failure")
}
}

override fun onPairingDeleted(errorCode: Int) {
override fun onPairingDeleted(errorCode: Long) {
logger.log(Level.INFO, "onPairingDeleted with error code: $errorCode")
}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
logger.log(Level.INFO, "onCommissioningComplete with error code: $errorCode")
if (errorCode == 0) {
setSuccess()
Expand All @@ -154,7 +154,7 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onReadCommissioningInfo")
}

override fun onCommissioningStatusUpdate(nodeId: Long, stage: String?, errorCode: Int) {
override fun onCommissioningStatusUpdate(nodeId: Long, stage: String?, errorCode: Long) {
logger.log(Level.INFO, "onCommissioningStatusUpdate")
}

Expand All @@ -178,7 +178,7 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onICDRegistrationInfoRequired")
}

override fun onICDRegistrationComplete(errorCode: Int, icdDeviceInfo: ICDDeviceInfo) {
override fun onICDRegistrationComplete(errorCode: Long, icdDeviceInfo: ICDDeviceInfo) {
logger.log(
Level.INFO,
"onICDRegistrationComplete with errorCode: $errorCode, symmetricKey: ${icdDeviceInfo.symmetricKey.toHex()}, icdDeviceInfo: $icdDeviceInfo"
Expand Down
36 changes: 21 additions & 15 deletions src/controller/java/AndroidDeviceControllerWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,18 @@ void AndroidDeviceControllerWrapper::SetJavaObjectRef(JavaVM * vm, jobject obj)
}
}

void AndroidDeviceControllerWrapper::CallJavaMethod(const char * methodName, jint argument)
void AndroidDeviceControllerWrapper::CallJavaIntMethod(const char * methodName, jint argument)
{
JniReferences::GetInstance().CallVoidInt(JniReferences::GetInstance().GetEnvForCurrentThread(), mJavaObjectRef.ObjectRef(),
methodName, argument);
}

void AndroidDeviceControllerWrapper::CallJavaLongMethod(const char * methodName, jlong argument)
{
JniReferences::GetInstance().CallVoidLong(JniReferences::GetInstance().GetEnvForCurrentThread(), mJavaObjectRef.ObjectRef(),
methodName, argument);
}

AndroidDeviceControllerWrapper * AndroidDeviceControllerWrapper::AllocateNew(
JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId, chip::FabricId fabricId, const chip::CATValues & cats,
chip::System::Layer * systemLayer, chip::Inet::EndPointManager<Inet::TCPEndPoint> * tcpEndPointManager,
Expand Down Expand Up @@ -697,19 +703,19 @@ CHIP_ERROR AndroidDeviceControllerWrapper::SetICDCheckInDelegate(jobject checkIn
void AndroidDeviceControllerWrapper::OnStatusUpdate(chip::Controller::DevicePairingDelegate::Status status)
{
chip::DeviceLayer::StackUnlock unlock;
CallJavaMethod("onStatusUpdate", static_cast<jint>(status));
CallJavaIntMethod("onStatusUpdate", static_cast<jint>(status));
}

void AndroidDeviceControllerWrapper::OnPairingComplete(CHIP_ERROR error)
{
chip::DeviceLayer::StackUnlock unlock;
CallJavaMethod("onPairingComplete", static_cast<jint>(error.AsInteger()));
CallJavaLongMethod("onPairingComplete", static_cast<jlong>(error.AsInteger()));
}

void AndroidDeviceControllerWrapper::OnPairingDeleted(CHIP_ERROR error)
{
chip::DeviceLayer::StackUnlock unlock;
CallJavaMethod("onPairingDeleted", static_cast<jint>(error.AsInteger()));
CallJavaLongMethod("onPairingDeleted", static_cast<jlong>(error.AsInteger()));
}

void AndroidDeviceControllerWrapper::OnCommissioningComplete(NodeId deviceId, CHIP_ERROR error)
Expand All @@ -727,11 +733,11 @@ void AndroidDeviceControllerWrapper::OnCommissioningComplete(NodeId deviceId, CH

JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
jmethodID onCommissioningCompleteMethod;
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mJavaObjectRef.ObjectRef(), "onCommissioningComplete", "(JI)V",
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mJavaObjectRef.ObjectRef(), "onCommissioningComplete", "(JJ)V",
&onCommissioningCompleteMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Error finding Java method: %" CHIP_ERROR_FORMAT, err.Format()));
env->CallVoidMethod(mJavaObjectRef.ObjectRef(), onCommissioningCompleteMethod, static_cast<jlong>(deviceId),
static_cast<jint>(error.AsInteger()));
static_cast<jlong>(error.AsInteger()));

if (ssidStr != nullptr)
{
Expand Down Expand Up @@ -762,12 +768,12 @@ void AndroidDeviceControllerWrapper::OnCommissioningStatusUpdate(PeerId peerId,
JniLocalReferenceScope scope(env);
jmethodID onCommissioningStatusUpdateMethod;
CHIP_ERROR err = JniReferences::GetInstance().FindMethod(env, mJavaObjectRef.ObjectRef(), "onCommissioningStatusUpdate",
"(JLjava/lang/String;I)V", &onCommissioningStatusUpdateMethod);
"(JLjava/lang/String;J)V", &onCommissioningStatusUpdateMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Error finding Java method: %" CHIP_ERROR_FORMAT, err.Format()));

UtfString jStageCompleted(env, StageToString(stageCompleted));
env->CallVoidMethod(mJavaObjectRef.ObjectRef(), onCommissioningStatusUpdateMethod, static_cast<jlong>(peerId.GetNodeId()),
jStageCompleted.jniValue(), static_cast<jint>(error.AsInteger()));
jStageCompleted.jniValue(), static_cast<jlong>(error.AsInteger()));
}

void AndroidDeviceControllerWrapper::OnReadCommissioningInfo(const chip::Controller::ReadCommissioningInfo & info)
Expand Down Expand Up @@ -800,12 +806,12 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Could not get JNIEnv for current thread"));
JniLocalReferenceScope scope(env);

VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Error invoking Java callback: no JNIEnv"));
VerifyOrReturn(env != nullptr, ChipLogError(Controller, "Error invoking Java callback: no JNIEnv"));

err = JniReferences::GetInstance().FindMethod(
env, mJavaObjectRef.ObjectRef(), "onScanNetworksSuccess",
"(Ljava/lang/Integer;Ljava/util/Optional;Ljava/util/Optional;Ljava/util/Optional;)V", &javaMethod);
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Zcl, "Error invoking Java callback: %s", ErrorStr(err)));
VerifyOrReturn(err == CHIP_NO_ERROR, ChipLogError(Controller, "Error invoking Java callback: %s", ErrorStr(err)));

jobject NetworkingStatus;
std::string NetworkingStatusClassName = "java/lang/Integer";
Expand Down Expand Up @@ -937,7 +943,7 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
threadInterfaceScanResultStructClass);
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "Could not find class ThreadScanResult");
ChipLogError(Controller, "Could not find class ThreadScanResult");
return;
}
jmethodID threadInterfaceScanResultStructCtor =
Expand All @@ -946,7 +952,7 @@ void AndroidDeviceControllerWrapper::OnScanNetworksSuccess(
"Integer;[BLjava/lang/Integer;Ljava/lang/Integer;)V");
if (threadInterfaceScanResultStructCtor == nullptr)
{
ChipLogError(Zcl, "Could not find ThreadScanResult constructor");
ChipLogError(Controller, "Could not find ThreadScanResult constructor");
return;
}

Expand All @@ -966,7 +972,7 @@ void AndroidDeviceControllerWrapper::OnScanNetworksFailure(CHIP_ERROR error)
{
chip::DeviceLayer::StackUnlock unlock;

CallJavaMethod("onScanNetworksFailure", static_cast<jint>(error.AsInteger()));
CallJavaLongMethod("onScanNetworksFailure", static_cast<jlong>(error.AsInteger()));
}

void AndroidDeviceControllerWrapper::OnICDRegistrationInfoRequired()
Expand Down Expand Up @@ -1022,7 +1028,7 @@ void AndroidDeviceControllerWrapper::OnICDRegistrationComplete(chip::NodeId icdN
jbyteArray jSymmetricKey = nullptr;
CHIP_ERROR methodErr =
JniReferences::GetInstance().FindMethod(env, mJavaObjectRef.ObjectRef(), "onICDRegistrationComplete",
"(ILchip/devicecontroller/ICDDeviceInfo;)V", &onICDRegistrationCompleteMethod);
"(JLchip/devicecontroller/ICDDeviceInfo;)V", &onICDRegistrationCompleteMethod);
VerifyOrReturn(methodErr == CHIP_NO_ERROR,
ChipLogError(Controller, "Error finding Java method: %" CHIP_ERROR_FORMAT, methodErr.Format()));

Expand All @@ -1045,7 +1051,7 @@ void AndroidDeviceControllerWrapper::OnICDRegistrationComplete(chip::NodeId icdN
static_cast<jlong>(mAutoCommissioner.GetCommissioningParameters().GetICDMonitoredSubject().Value()),
static_cast<jlong>(Controller()->GetFabricId()), static_cast<jint>(Controller()->GetFabricIndex()));

env->CallVoidMethod(mJavaObjectRef.ObjectRef(), onICDRegistrationCompleteMethod, static_cast<jint>(err.AsInteger()),
env->CallVoidMethod(mJavaObjectRef.ObjectRef(), onICDRegistrationCompleteMethod, static_cast<jlong>(err.AsInteger()),
icdDeviceInfoObj);
}

Expand Down
3 changes: 2 additions & 1 deletion src/controller/java/AndroidDeviceControllerWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel
}
#endif // JAVA_MATTER_CONTROLLER_TEST

void CallJavaMethod(const char * methodName, jint argument);
void CallJavaIntMethod(const char * methodName, jint argument);
void CallJavaLongMethod(const char * methodName, jlong argument);
CHIP_ERROR InitializeOperationalCredentialsIssuer();

/**
Expand Down
6 changes: 3 additions & 3 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ void JNI_OnUnload(JavaVM * jvm, void * reserved)
chip::Platform::MemoryShutdown();
}

JNI_METHOD(jint, onNOCChainGeneration)
JNI_METHOD(jlong, onNOCChainGeneration)
(JNIEnv * env, jobject self, jlong handle, jobject controllerParams)
{
chip::DeviceLayer::StackLock lock;
Expand Down Expand Up @@ -266,15 +266,15 @@ JNI_METHOD(jint, onNOCChainGeneration)
{
ChipLogError(Controller, "Failed to SetNocChain for the device: %" CHIP_ERROR_FORMAT, err.Format());
}
return static_cast<jint>(err.AsInteger());
return static_cast<jlong>(err.AsInteger());
#endif // JAVA_MATTER_CONTROLLER_TEST
}
exit:
#ifndef JAVA_MATTER_CONTROLLER_TEST
err = wrapper->GetAndroidOperationalCredentialsIssuer()->NOCChainGenerated(err, ByteSpan(), ByteSpan(), ByteSpan(), ipkOptional,
adminSubjectOptional);
#endif // JAVA_MATTER_CONTROLLER_TEST
return static_cast<jint>(err.AsInteger());
return static_cast<jlong>(err.AsInteger());
}

JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self, jobject controllerParams)
Expand Down
Loading

0 comments on commit 48078be

Please sign in to comment.