From 808a8c7f6eca190118290a5df9c7e401ba420430 Mon Sep 17 00:00:00 2001 From: joonhaengHeo <85541460+joonhaengHeo@users.noreply.github.com> Date: Fri, 8 Mar 2024 13:21:39 +0900 Subject: [PATCH] Remove unset threadNetwork Scan (#31704) * divide android commissioning parameter * Fix countrycode copy issue * Restyled by clang-format * Update commissioningParameter --------- Co-authored-by: Restyled.io --- src/controller/CommissioningDelegate.h | 3 ++ .../java/AndroidDeviceControllerWrapper.cpp | 24 ++++++++++- .../java/AndroidDeviceControllerWrapper.h | 32 ++++++++------- .../java/CHIPDeviceController-JNI.cpp | 41 +++++-------------- 4 files changed, 53 insertions(+), 47 deletions(-) diff --git a/src/controller/CommissioningDelegate.h b/src/controller/CommissioningDelegate.h index 175c246e908f9d..2a78663541dd49 100644 --- a/src/controller/CommissioningDelegate.h +++ b/src/controller/CommissioningDelegate.h @@ -370,9 +370,12 @@ class CommissioningParameters mAttestationNonce.SetValue(attestationNonce); return *this; } + + // If a WiFiCredentials is provided, then the WiFiNetworkScan will not be attempted CommissioningParameters & SetWiFiCredentials(WiFiCredentials wifiCreds) { mWiFiCreds.SetValue(wifiCreds); + mAttemptWiFiNetworkScan.SetValue(false); return *this; } diff --git a/src/controller/java/AndroidDeviceControllerWrapper.cpp b/src/controller/java/AndroidDeviceControllerWrapper.cpp index 74e1187ef2a03b..c965bb0f17584b 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.cpp +++ b/src/controller/java/AndroidDeviceControllerWrapper.cpp @@ -104,7 +104,7 @@ AndroidDeviceControllerWrapper * AndroidDeviceControllerWrapper::AllocateNew( jobject keypairDelegate, jbyteArray rootCertificate, jbyteArray intermediateCertificate, jbyteArray nodeOperationalCertificate, jbyteArray ipkEpochKey, uint16_t listenPort, uint16_t controllerVendorId, uint16_t failsafeTimerSeconds, bool attemptNetworkScanWiFi, bool attemptNetworkScanThread, bool skipCommissioningComplete, - bool skipAttestationCertificateValidation, CHIP_ERROR * errInfoOnFailure) + bool skipAttestationCertificateValidation, jstring countryCode, CHIP_ERROR * errInfoOnFailure) { if (errInfoOnFailure == nullptr) { @@ -207,11 +207,30 @@ AndroidDeviceControllerWrapper * AndroidDeviceControllerWrapper::AllocateNew( wrapper->mGroupDataProvider.SetStorageDelegate(wrapperStorage); wrapper->mGroupDataProvider.SetSessionKeystore(initParams.sessionKeystore); - CommissioningParameters params = wrapper->mAutoCommissioner.GetCommissioningParameters(); + CommissioningParameters params = wrapper->GetCommissioningParameters(); params.SetFailsafeTimerSeconds(failsafeTimerSeconds); params.SetAttemptWiFiNetworkScan(attemptNetworkScanWiFi); params.SetAttemptThreadNetworkScan(attemptNetworkScanThread); params.SetSkipCommissioningComplete(skipCommissioningComplete); + + if (countryCode != nullptr) + { + JniUtfString countryCodeJniString(env, countryCode); + if (countryCodeJniString.size() != kCountryCodeBufferLen) + { + *errInfoOnFailure = CHIP_ERROR_INVALID_ARGUMENT; + return nullptr; + } + + MutableCharSpan copiedCode(wrapper->mCountryCode); + if (CopyCharSpanToMutableCharSpan(countryCodeJniString.charSpan(), copiedCode) != CHIP_NO_ERROR) + { + *errInfoOnFailure = CHIP_ERROR_INVALID_ARGUMENT; + return nullptr; + } + params.SetCountryCode(copiedCode); + } + wrapper->UpdateCommissioningParameters(params); CHIP_ERROR err = wrapper->mGroupDataProvider.Init(); @@ -526,6 +545,7 @@ CHIP_ERROR AndroidDeviceControllerWrapper::UpdateCommissioningParameters(const c { // this will wipe out any custom attestationNonce and csrNonce that was being used. // however, Android APIs don't allow these to be set to custom values today. + mCommissioningParameter = params; return mAutoCommissioner.SetCommissioningParameters(params); } diff --git a/src/controller/java/AndroidDeviceControllerWrapper.h b/src/controller/java/AndroidDeviceControllerWrapper.h index ac279370f62c4a..5ccb2edb3b2050 100644 --- a/src/controller/java/AndroidDeviceControllerWrapper.h +++ b/src/controller/java/AndroidDeviceControllerWrapper.h @@ -50,6 +50,8 @@ constexpr uint8_t kUserActiveModeTriggerInstructionBufferLen = 128 + 1; // 128bytes is max UserActiveModeTriggerInstruction size and 1 byte is for escape sequence. + +constexpr uint8_t kCountryCodeBufferLen = 2; /** * This class contains all relevant information for the JNI view of CHIPDeviceController * to handle all controller-related processing. @@ -123,10 +125,7 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel chip::Credentials::PartialDACVerifier * GetPartialDACVerifier() { return &mPartialDACVerifier; } - const chip::Controller::CommissioningParameters & GetCommissioningParameters() const - { - return mAutoCommissioner.GetCommissioningParameters(); - } + const chip::Controller::CommissioningParameters & GetCommissioningParameters() const { return mCommissioningParameter; } static AndroidDeviceControllerWrapper * FromJNIHandle(jlong handle) { @@ -171,20 +170,19 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel * @param[in] skipCommissioningComplete whether to skip the CASE commissioningComplete command during commissioning * @param[out] errInfoOnFailure a pointer to a CHIP_ERROR that will be populated if this method returns nullptr */ - static AndroidDeviceControllerWrapper * - AllocateNew(JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId, chip::FabricId fabricId, - const chip::CATValues & cats, chip::System::Layer * systemLayer, - chip::Inet::EndPointManager * tcpEndPointManager, - chip::Inet::EndPointManager * udpEndPointManager, + static AndroidDeviceControllerWrapper * AllocateNew( + JavaVM * vm, jobject deviceControllerObj, chip::NodeId nodeId, chip::FabricId fabricId, const chip::CATValues & cats, + chip::System::Layer * systemLayer, chip::Inet::EndPointManager * tcpEndPointManager, + chip::Inet::EndPointManager * udpEndPointManager, #ifdef JAVA_MATTER_CONTROLLER_TEST - ExampleOperationalCredentialsIssuerPtr opCredsIssuer, + ExampleOperationalCredentialsIssuerPtr opCredsIssuer, #else - AndroidOperationalCredentialsIssuerPtr opCredsIssuer, + AndroidOperationalCredentialsIssuerPtr opCredsIssuer, #endif - jobject keypairDelegate, jbyteArray rootCertificate, jbyteArray intermediateCertificate, - jbyteArray nodeOperationalCertificate, jbyteArray ipkEpochKey, uint16_t listenPort, uint16_t controllerVendorId, - uint16_t failsafeTimerSeconds, bool attemptNetworkScanWiFi, bool attemptNetworkScanThread, - bool skipCommissioningComplete, bool skipAttestationCertificateValidation, CHIP_ERROR * errInfoOnFailure); + jobject keypairDelegate, jbyteArray rootCertificate, jbyteArray intermediateCertificate, + jbyteArray nodeOperationalCertificate, jbyteArray ipkEpochKey, uint16_t listenPort, uint16_t controllerVendorId, + uint16_t failsafeTimerSeconds, bool attemptNetworkScanWiFi, bool attemptNetworkScanThread, bool skipCommissioningComplete, + bool skipAttestationCertificateValidation, jstring countryCode, CHIP_ERROR * errInfoOnFailure); void Shutdown(); @@ -246,6 +244,8 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel std::vector mIcacCertificate; std::vector mRcacCertificate; + char mCountryCode[kCountryCodeBufferLen]; + chip::Controller::AutoCommissioner mAutoCommissioner; chip::Credentials::PartialDACVerifier mPartialDACVerifier; @@ -262,6 +262,8 @@ class AndroidDeviceControllerWrapper : public chip::Controller::DevicePairingDel chip::MutableCharSpan mUserActiveModeTriggerInstruction = chip::MutableCharSpan(mUserActiveModeTriggerInstructionBuffer); chip::BitMask mUserActiveModeTriggerHint; + chip::Controller::CommissioningParameters mCommissioningParameter; + AndroidDeviceControllerWrapper(ChipDeviceControllerPtr controller, #ifdef JAVA_MATTER_CONTROLLER_TEST ExampleOperationalCredentialsIssuerPtr opCredsIssuer diff --git a/src/controller/java/CHIPDeviceController-JNI.cpp b/src/controller/java/CHIPDeviceController-JNI.cpp index 99520d1a32c9a0..f1c7487e4e3f87 100644 --- a/src/controller/java/CHIPDeviceController-JNI.cpp +++ b/src/controller/java/CHIPDeviceController-JNI.cpp @@ -371,6 +371,10 @@ JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self, jobject contr jobject countryCodeOptional = env->CallObjectMethod(controllerParams, getCountryCode); jobject regulatoryLocationOptional = env->CallObjectMethod(controllerParams, getRegulatoryLocation); + jobject countryCode; + err = chip::JniReferences::GetInstance().GetOptionalValue(countryCodeOptional, countryCode); + SuccessOrExit(err); + #ifdef JAVA_MATTER_CONTROLLER_TEST std::unique_ptr opCredsIssuer( new chip::Controller::ExampleOperationalCredentialsIssuer()); @@ -383,7 +387,7 @@ JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self, jobject contr DeviceLayer::TCPEndPointManager(), DeviceLayer::UDPEndPointManager(), std::move(opCredsIssuer), keypairDelegate, rootCertificate, intermediateCertificate, operationalCertificate, ipk, listenPort, controllerVendorId, failsafeTimerSeconds, attemptNetworkScanWiFi, attemptNetworkScanThread, skipCommissioningComplete, - skipAttestationCertificateValidation, &err); + skipAttestationCertificateValidation, static_cast(countryCode), &err); SuccessOrExit(err); if (caseFailsafeTimerSeconds > 0) @@ -411,29 +415,6 @@ JNI_METHOD(jlong, newDeviceController)(JNIEnv * env, jobject self, jobject contr } } - jobject countryCode; - err = chip::JniReferences::GetInstance().GetOptionalValue(countryCodeOptional, countryCode); - SuccessOrExit(err); - - if (countryCode != nullptr) - { - jstring countryCodeStr = static_cast(countryCode); - JniUtfString countryCodeJniString(env, countryCodeStr); - - VerifyOrExit(countryCodeJniString.size() == 2, err = CHIP_ERROR_INVALID_ARGUMENT); - - chip::Controller::CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters(); - commissioningParams.SetCountryCode(countryCodeJniString.charSpan()); - - // The wrapper internally has reserved storage for the country code and will copy the value. - err = wrapper->UpdateCommissioningParameters(commissioningParams); - if (err != CHIP_NO_ERROR) - { - ChipLogError(Controller, "UpdateCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format()); - SuccessOrExit(err); - } - } - jobject regulatoryLocation; err = chip::JniReferences::GetInstance().GetOptionalValue(regulatoryLocationOptional, regulatoryLocation); SuccessOrExit(err); @@ -876,7 +857,7 @@ JNI_METHOD(void, updateCommissioningNetworkCredentials) chip::DeviceLayer::StackLock lock; AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); - CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters(); + CommissioningParameters commissioningParams = wrapper->GetAutoCommissioner()->GetCommissioningParameters(); CHIP_ERROR err = wrapper->ApplyNetworkCredentials(commissioningParams, networkCredentials); if (err != CHIP_NO_ERROR) { @@ -884,10 +865,10 @@ JNI_METHOD(void, updateCommissioningNetworkCredentials) JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); return; } - err = wrapper->UpdateCommissioningParameters(commissioningParams); + err = wrapper->GetAutoCommissioner()->SetCommissioningParameters(commissioningParams); if (err != CHIP_NO_ERROR) { - ChipLogError(Controller, "UpdateCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format()); + ChipLogError(Controller, "SetCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format()); JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); return; } @@ -911,7 +892,7 @@ JNI_METHOD(void, updateCommissioningICDRegistrationInfo) chip::DeviceLayer::StackLock lock; AndroidDeviceControllerWrapper * wrapper = AndroidDeviceControllerWrapper::FromJNIHandle(handle); - CommissioningParameters commissioningParams = wrapper->GetCommissioningParameters(); + CommissioningParameters commissioningParams = wrapper->GetAutoCommissioner()->GetCommissioningParameters(); CHIP_ERROR err = wrapper->ApplyICDRegistrationInfo(commissioningParams, icdRegistrationInfo); if (err != CHIP_NO_ERROR) { @@ -919,10 +900,10 @@ JNI_METHOD(void, updateCommissioningICDRegistrationInfo) JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); return; } - err = wrapper->UpdateCommissioningParameters(commissioningParams); + err = wrapper->GetAutoCommissioner()->SetCommissioningParameters(commissioningParams); if (err != CHIP_NO_ERROR) { - ChipLogError(Controller, "UpdateCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format()); + ChipLogError(Controller, "SetCommissioningParameters failed. Err = %" CHIP_ERROR_FORMAT, err.Format()); JniReferences::GetInstance().ThrowError(env, sChipDeviceControllerExceptionCls, err); return; }