Skip to content

Commit

Permalink
[Android] Support random pincode (#25511)
Browse files Browse the repository at this point in the history
* Support random pincode in Android

* Restyle

* Add @nullable

* Restyle
  • Loading branch information
joonhaengHeo authored Mar 20, 2023
1 parent cd3bdaa commit a8242ff
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
25 changes: 20 additions & 5 deletions src/controller/java/CHIPDeviceController-JNI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint32_t>(iteration), false);

Expand All @@ -1182,10 +1183,17 @@ JNI_METHOD(jboolean, openPairingWindowWithPIN)

AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

Optional<uint32_t> pinCode = Optional<uint32_t>();
if (setupPinCode != nullptr)
{
jlong jsetupPinCode = chip::JniReferences::GetInstance().LongToPrimitive(setupPinCode);
pinCode = MakeOptional(static_cast<uint32_t>(jsetupPinCode));
}

chip::SetupPayload setupPayload;
err = AutoCommissioningWindowOpener::OpenCommissioningWindow(
wrapper->Controller(), chipDevice->GetDeviceId(), System::Clock::Seconds16(duration), static_cast<uint32_t>(iteration),
discriminator, MakeOptional(static_cast<uint32_t>(setupPinCode)), NullOptional, setupPayload);
discriminator, pinCode, NullOptional, setupPayload);

if (err != CHIP_NO_ERROR)
{
Expand Down Expand Up @@ -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<uint32_t>(iteration), false);

Expand All @@ -1241,10 +1249,17 @@ JNI_METHOD(jboolean, openPairingWindowWithPINCallback)

AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle);

Optional<uint32_t> pinCode = Optional<uint32_t>();
if (setupPinCode != nullptr)
{
jlong jsetupPinCode = chip::JniReferences::GetInstance().LongToPrimitive(setupPinCode);
pinCode = MakeOptional(static_cast<uint32_t>(jsetupPinCode));
}

chip::SetupPayload setupPayload;
err = AndroidCommissioningWindowOpener::OpenCommissioningWindow(
wrapper->Controller(), chipDevice->GetDeviceId(), System::Clock::Seconds16(duration), static_cast<uint32_t>(iteration),
discriminator, MakeOptional(static_cast<uint32_t>(setupPinCode)), NullOptional, jcallback, setupPayload);
discriminator, pinCode, NullOptional, jcallback, setupPayload);

if (err != CHIP_NO_ERROR)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a8242ff

Please sign in to comment.