Skip to content

Commit

Permalink
[Android]CHIP_ERROR should be handled by Long in Java/Kotlin (#32702)
Browse files Browse the repository at this point in the history
* [Android]CHIP_ERROR should be handled by Long in Java/Kotlin

* Update MatterController.kt

* Update CompletionListenerAdapter.kt

* Update PairingCommand.kt
  • Loading branch information
yunhanw-google authored and pull[bot] committed Apr 29, 2024
1 parent b651538 commit 7471137
Show file tree
Hide file tree
Showing 22 changed files with 110 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class CHIPToolActivity :
}
}

override fun onCommissioningComplete(code: Int, nodeId: Long) {
override fun onCommissioningComplete(code: Long, nodeId: Long) {
runOnUiThread {
Toast.makeText(this, getString(R.string.commissioning_completed, code), Toast.LENGTH_SHORT)
.show()
Expand Down
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 @@ -98,7 +98,7 @@ class BasicClientFragment : Fragment() {
inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ class GroupSettingFragment : Fragment() {
}

inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class MultiAdminClientFragment : Fragment() {
inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class OnOffClientFragment : Fragment() {
inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
showMessage("Address update complete for nodeId $nodeId with code $errorCode")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OpCredClientFragment : Fragment() {
inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onConnectDeviceComplete() {}

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ class OtaProviderClientFragment : Fragment() {
}

inner class ChipControllerCallback : GenericChipDeviceListener() {
override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: Long) {
Log.d(TAG, "onCommissioningComplete for nodeId $nodeId: $errorCode")
showMessage("Address update complete for nodeId $nodeId with code $errorCode")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ class DeviceProvisioningFragment : Fragment() {
override fun onDeviceAttestationCompleted(
devicePtr: Long,
attestationInfo: AttestationInfo,
errorCode: Int
errorCode: Long
) {}
}

Expand Down Expand Up @@ -249,18 +249,18 @@ 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)
?.onCommissioningComplete(0L, nodeId)
} else {
showMessage(R.string.rendezvous_over_ble_pairing_failure_text)
FragmentUtil.getHost(this@DeviceProvisioningFragment, Callback::class.java)
?.onCommissioningComplete(errorCode)
}
}

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 All @@ -318,14 +318,14 @@ class DeviceProvisioningFragment : Fragment() {
/** Callback from [DeviceProvisioningFragment] notifying any registered listeners. */
interface Callback {
/** Notifies that commissioning has been completed. */
fun onCommissioningComplete(code: Int, nodeId: Long = 0L)
fun onCommissioningComplete(code: Long, nodeId: Long = 0L)
}

companion object {
private const val TAG = "DeviceProvisioningFragment"
private const val ARG_DEVICE_INFO = "device_info"
private const val ARG_NETWORK_CREDENTIALS = "network_credentials"
private const val STATUS_PAIRING_SUCCESS = 0
private const val STATUS_PAIRING_SUCCESS = 0L

/**
* Set for the fail-safe timer before onDeviceAttestationFailed is invoked.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ 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) {
if (errorCode == 0L) {
setSuccess()
} else {
setFailure("onPairingComplete failure")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,20 +127,20 @@ 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) {
if (errorCode != 0L) {
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) {
if (errorCode == 0L) {
setSuccess()
} else {
setFailure("onCommissioningComplete failure")
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,20 +125,20 @@ abstract class PairingCommand(
logger.log(Level.INFO, "onStatusUpdate with status: $status")
}

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

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

override fun onCommissioningComplete(nodeId: Long, errorCode: Int) {
override fun onCommissioningComplete(nodeId: Long, errorCode: UInt) {
logger.log(Level.INFO, "onCommissioningComplete with error code: $errorCode")
if (errorCode == 0) {
if (errorCode == 0U) {
setSuccess()
} else {
setFailure("onCommissioningComplete failure")
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: UInt) {
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: UInt, 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
Loading

0 comments on commit 7471137

Please sign in to comment.