From 6d31e1e0e8ebfe9be7cc1f0b203bf994e3c2890b Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Wed, 13 Apr 2022 23:19:11 -0400 Subject: [PATCH] Pass the country code via commissioning parameters. (#17284) We shouldn't assume the configuration manager exists on the commissioner, or stores anything useful, so switch to passing in the country code via commissioning parameters instead of reading it from the configuration manager. --- src/controller/AutoCommissioner.cpp | 15 +++++++++++++ src/controller/AutoCommissioner.h | 1 + src/controller/CHIPDeviceController.cpp | 30 +++++++++---------------- src/controller/CommissioningDelegate.h | 13 +++++++++++ 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/controller/AutoCommissioner.cpp b/src/controller/AutoCommissioner.cpp index 5af57e2a027a64..ee494f1b1f927d 100644 --- a/src/controller/AutoCommissioner.cpp +++ b/src/controller/AutoCommissioner.cpp @@ -71,6 +71,21 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam mParams.SetWiFiCredentials( WiFiCredentials(ByteSpan(mSsid, creds.ssid.size()), ByteSpan(mCredentials, creds.credentials.size()))); } + + if (params.GetCountryCode().HasValue()) + { + auto & code = params.GetCountryCode().Value(); + MutableCharSpan copiedCode(mCountryCode); + if (CopyCharSpanToMutableCharSpan(code, copiedCode) == CHIP_NO_ERROR) + { + mParams.SetCountryCode(copiedCode); + } + else + { + ChipLogError(Controller, "Country code is too large: %u", static_cast(code.size())); + } + } + // If the AttestationNonce is passed in, using that else using a random one.. if (params.GetAttestationNonce().HasValue()) { diff --git a/src/controller/AutoCommissioner.h b/src/controller/AutoCommissioner.h index 2ec23ab30f0bdb..feba60bc649ebd 100644 --- a/src/controller/AutoCommissioner.h +++ b/src/controller/AutoCommissioner.h @@ -66,6 +66,7 @@ class AutoCommissioner : public CommissioningDelegate uint8_t mSsid[CommissioningParameters::kMaxSsidLen]; uint8_t mCredentials[CommissioningParameters::kMaxCredentialsLen]; uint8_t mThreadOperationalDataset[CommissioningParameters::kMaxThreadDatasetLen]; + char mCountryCode[CommissioningParameters::kMaxCountryCodeLen]; bool mNeedsNetworkSetup = false; ReadCommissioningInfo mDeviceCommissioningInfo; diff --git a/src/controller/CHIPDeviceController.cpp b/src/controller/CHIPDeviceController.cpp index f3e9920fc72719..09a712c98e4078 100644 --- a/src/controller/CHIPDeviceController.cpp +++ b/src/controller/CHIPDeviceController.cpp @@ -40,11 +40,6 @@ #include #include -#if CONFIG_DEVICE_LAYER -#include -#include -#endif - #include #include @@ -1895,23 +1890,18 @@ void DeviceCommissioner::PerformCommissioningStep(DeviceProxy * proxy, Commissio ChipLogProgress(Controller, "Device does not support configurable regulatory location"); regulatoryConfig = capability; } - static constexpr size_t kMaxCountryCodeSize = 3; - char countryCodeStr[kMaxCountryCodeSize] = "XX"; - size_t actualCountryCodeSize = 2; - -#if CONFIG_DEVICE_LAYER - CHIP_ERROR status = - DeviceLayer::ConfigurationMgr().GetCountryCode(countryCodeStr, kMaxCountryCodeSize, actualCountryCodeSize); -#else - CHIP_ERROR status = CHIP_ERROR_NOT_IMPLEMENTED; -#endif - if (status != CHIP_NO_ERROR) + + CharSpan countryCode; + const auto & providedCountryCode = params.GetCountryCode(); + if (providedCountryCode.HasValue()) + { + countryCode = providedCountryCode.Value(); + } + else { - actualCountryCodeSize = 2; - memset(countryCodeStr, 'X', actualCountryCodeSize); - ChipLogError(Controller, "Unable to find country code, defaulting to %s", countryCodeStr); + // Default to "XX", for lack of anything better. + countryCode = CharSpan::fromCharString("XX"); } - chip::CharSpan countryCode(countryCodeStr, actualCountryCodeSize); GeneralCommissioning::Commands::SetRegulatoryConfig::Type request; request.newRegulatoryConfig = regulatoryConfig; diff --git a/src/controller/CommissioningDelegate.h b/src/controller/CommissioningDelegate.h index e15daaf28e5322..f176576c065dce 100644 --- a/src/controller/CommissioningDelegate.h +++ b/src/controller/CommissioningDelegate.h @@ -83,6 +83,7 @@ class CommissioningParameters static constexpr size_t kMaxThreadDatasetLen = 254; static constexpr size_t kMaxSsidLen = 32; static constexpr size_t kMaxCredentialsLen = 64; + static constexpr size_t kMaxCountryCodeLen = 2; // Value to use when setting the commissioning failsafe timer on the node being commissioned. // If the failsafe timer value is passed in as part of the commissioning parameters, that value will be used. If not supplied, @@ -102,6 +103,9 @@ class CommissioningParameters return mDeviceRegulatoryLocation; } + // The country code to be used for the node, if set. + Optional GetCountryCode() const { return mCountryCode; } + // Nonce sent to the node to use during the CSR request. // When using the AutoCommissioner, this value will be ignored in favour of the value supplied by the // OperationalCredentialsDelegate ObtainCsrNonce function. If the credential delegate is not supplied, the value supplied here @@ -222,6 +226,14 @@ class CommissioningParameters return *this; } + // The lifetime of the buffer countryCode is pointing to should exceed the + // lifetime of CommissioningParameters object. + CommissioningParameters & SetCountryCode(CharSpan countryCode) + { + mCountryCode.SetValue(countryCode); + return *this; + } + // The lifetime of the buffer csrNonce is pointing to, should exceed the lifetime of CommissioningParameters object. CommissioningParameters & SetCSRNonce(ByteSpan csrNonce) { @@ -339,6 +351,7 @@ class CommissioningParameters Optional mCSRNonce; Optional mAttestationNonce; Optional mWiFiCreds; + Optional mCountryCode; Optional mThreadOperationalDataset; Optional mNOCChainGenerationParameters; Optional mRootCert;