Skip to content

Commit

Permalink
Pass the country code via commissioning parameters. (#17284)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Oct 11, 2023
1 parent 69f7622 commit 1031022
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
15 changes: 15 additions & 0 deletions src/controller/AutoCommissioner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned>(code.size()));
}
}

// If the AttestationNonce is passed in, using that else using a random one..
if (params.GetAttestationNonce().HasValue())
{
Expand Down
1 change: 1 addition & 0 deletions src/controller/AutoCommissioner.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 10 additions & 20 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@
#include <app-common/zap-generated/ids/Clusters.h>
#include <controller-clusters/zap-generated/CHIPClusters.h>

#if CONFIG_DEVICE_LAYER
#include <platform/CHIPDeviceLayer.h>
#include <platform/ConfigurationManager.h>
#endif

#include <app/server/Dnssd.h>

#include <app/InteractionModelEngine.h>
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions src/controller/CommissioningDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -102,6 +103,9 @@ class CommissioningParameters
return mDeviceRegulatoryLocation;
}

// The country code to be used for the node, if set.
Optional<CharSpan> 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
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -339,6 +351,7 @@ class CommissioningParameters
Optional<ByteSpan> mCSRNonce;
Optional<ByteSpan> mAttestationNonce;
Optional<WiFiCredentials> mWiFiCreds;
Optional<CharSpan> mCountryCode;
Optional<ByteSpan> mThreadOperationalDataset;
Optional<NOCChainGenerationParameters> mNOCChainGenerationParameters;
Optional<ByteSpan> mRootCert;
Expand Down

0 comments on commit 1031022

Please sign in to comment.