From a8242ffb65cd1545094be574ed76d980ed9158ac Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Tue, 21 Mar 2023 06:10:19 +0900 Subject: [PATCH] [Android] Support random pincode (#25511) * Support random pincode in Android * Restyle * Add @nullable * Restyle --- .../clusterclient/MultiAdminClientFragment.kt | 7 +++++- .../java/CHIPDeviceController-JNI.cpp | 25 +++++++++++++++---- .../ChipDeviceController.java | 12 ++++++--- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt index 6f0a7a72b26b0f..6543f5f796393a 100644 --- a/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt +++ b/examples/android/CHIPTool/app/src/main/java/com/google/chip/chiptool/clusterclient/MultiAdminClientFragment.kt @@ -107,9 +107,14 @@ class MultiAdminClientFragment : Fragment() { val testIteration = 1000 val devicePointer = ChipClient.getConnectedDevicePointer(requireContext(), addressUpdateFragment.deviceId) + + var setupPinCode: Long? = null + if (!binding.setupPinCodeEd.text.toString().isEmpty()) { + setupPinCode = binding.setupPinCodeEd.text.toString().toULong().toLong() + } deviceController.openPairingWindowWithPINCallback( devicePointer, testDuration, testIteration.toLong(), - binding.discriminatorEd.text.toString().toInt(), binding.setupPinCodeEd.text.toString().toULong().toLong(), + binding.discriminatorEd.text.toString().toInt(), setupPinCode, object:OpenCommissioningCallback { override fun onError(status: Int, deviceId: Long) { showMessage("OpenCommissioning Fail! \nDevice ID : $deviceId\nErrorCode : $status") diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 710da43628435e..db5e47eb46e788 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -1166,7 +1166,8 @@ JNI_METHOD(jboolean, openPairingWindow)(JNIEnv * env, jobject self, jlong handle } JNI_METHOD(jboolean, openPairingWindowWithPIN) -(JNIEnv * env, jobject self, jlong handle, jlong devicePtr, jint duration, jlong iteration, jint discriminator, jlong setupPinCode) +(JNIEnv * env, jobject self, jlong handle, jlong devicePtr, jint duration, jlong iteration, jint discriminator, + jobject setupPinCode) { VerifyOrReturnValue(chip::CanCastTo(iteration), false); @@ -1182,10 +1183,17 @@ JNI_METHOD(jboolean, openPairingWindowWithPIN) AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); + Optional pinCode = Optional(); + if (setupPinCode != nullptr) + { + jlong jsetupPinCode = chip::JniReferences::GetInstance().LongToPrimitive(setupPinCode); + pinCode = MakeOptional(static_cast(jsetupPinCode)); + } + chip::SetupPayload setupPayload; err = AutoCommissioningWindowOpener::OpenCommissioningWindow( wrapper->Controller(), chipDevice->GetDeviceId(), System::Clock::Seconds16(duration), static_cast(iteration), - discriminator, MakeOptional(static_cast(setupPinCode)), NullOptional, setupPayload); + discriminator, pinCode, NullOptional, setupPayload); if (err != CHIP_NO_ERROR) { @@ -1224,8 +1232,8 @@ JNI_METHOD(jboolean, openPairingWindowCallback) } JNI_METHOD(jboolean, openPairingWindowWithPINCallback) -(JNIEnv * env, jobject self, jlong handle, jlong devicePtr, jint duration, jlong iteration, jint discriminator, jlong setupPinCode, - jobject jcallback) +(JNIEnv * env, jobject self, jlong handle, jlong devicePtr, jint duration, jlong iteration, jint discriminator, + jobject setupPinCode, jobject jcallback) { VerifyOrReturnValue(chip::CanCastTo(iteration), false); @@ -1241,10 +1249,17 @@ JNI_METHOD(jboolean, openPairingWindowWithPINCallback) AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); + Optional pinCode = Optional(); + if (setupPinCode != nullptr) + { + jlong jsetupPinCode = chip::JniReferences::GetInstance().LongToPrimitive(setupPinCode); + pinCode = MakeOptional(static_cast(jsetupPinCode)); + } + chip::SetupPayload setupPayload; err = AndroidCommissioningWindowOpener::OpenCommissioningWindow( wrapper->Controller(), chipDevice->GetDeviceId(), System::Clock::Seconds16(duration), static_cast(iteration), - discriminator, MakeOptional(static_cast(setupPinCode)), NullOptional, jcallback, setupPayload); + discriminator, pinCode, NullOptional, jcallback, setupPayload); if (err != CHIP_NO_ERROR) { diff --git a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java index f57d736a7bd4a2..c22753ccbaa463 100644 --- a/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java +++ b/src/controller/java/src/chip/devicecontroller/ChipDeviceController.java @@ -467,7 +467,11 @@ public boolean openPairingWindow(long devicePtr, int duration) { } public boolean openPairingWindowWithPIN( - long devicePtr, int duration, long iteration, int discriminator, long setupPinCode) { + long devicePtr, + int duration, + long iteration, + int discriminator, + @Nullable Long setupPinCode) { return openPairingWindowWithPIN( deviceControllerPtr, devicePtr, duration, iteration, discriminator, setupPinCode); } @@ -482,7 +486,7 @@ public boolean openPairingWindowWithPINCallback( int duration, long iteration, int discriminator, - long setupPinCode, + @Nullable Long setupPinCode, OpenCommissioningCallback callback) { return openPairingWindowWithPINCallback( deviceControllerPtr, devicePtr, duration, iteration, discriminator, setupPinCode, callback); @@ -935,7 +939,7 @@ private native boolean openPairingWindowWithPIN( int duration, long iteration, int discriminator, - long setupPinCode); + @Nullable Long setupPinCode); private native boolean openPairingWindowCallback( long deviceControllerPtr, long devicePtr, int duration, OpenCommissioningCallback callback); @@ -946,7 +950,7 @@ private native boolean openPairingWindowWithPINCallback( int duration, long iteration, int discriminator, - long setupPinCode, + @Nullable Long setupPinCode, OpenCommissioningCallback callback); private native byte[] getAttestationChallenge(long deviceControllerPtr, long devicePtr);