From d5e0ea5f0f0f95fae0035c588eab47b144adf03e Mon Sep 17 00:00:00 2001 From: cecille Date: Sat, 21 Oct 2023 20:35:20 -0400 Subject: [PATCH 1/3] Commissioning time sync: copy parameters to autocommissioner --- src/controller/AutoCommissioner.cpp | 28 +++++++++++++++++++++++++- src/controller/AutoCommissioner.h | 11 ++++++++++ src/controller/CommissioningDelegate.h | 2 ++ 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/controller/AutoCommissioner.cpp b/src/controller/AutoCommissioner.cpp index 72470edec40edc..596119623216de 100644 --- a/src/controller/AutoCommissioner.cpp +++ b/src/controller/AutoCommissioner.cpp @@ -85,7 +85,8 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam IsUnsafeSpan(params.GetIcac(), mParams.GetIcac()) || IsUnsafeSpan(params.GetIpk(), mParams.GetIpk()) || IsUnsafeSpan(params.GetAttestationElements(), mParams.GetAttestationElements()) || IsUnsafeSpan(params.GetAttestationSignature(), mParams.GetAttestationSignature()) || - IsUnsafeSpan(params.GetPAI(), mParams.GetPAI()) || IsUnsafeSpan(params.GetDAC(), mParams.GetDAC())); + IsUnsafeSpan(params.GetPAI(), mParams.GetPAI()) || IsUnsafeSpan(params.GetDAC(), mParams.GetDAC()) || + IsUnsafeSpan(params.GetTimeZone(), mParams.GetTimeZone()) || IsUnsafeSpan(params.GetDSTOffsets(), mParams.GetDSTOffsets())); mParams = params; @@ -174,6 +175,31 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam } mParams.SetCSRNonce(ByteSpan(mCSRNonce, sizeof(mCSRNonce))); + if (params.GetDSTOffsets().HasValue()) { + ChipLogProgress(Controller, "Setting DST offsets from parameters"); + size_t size = params.GetDSTOffsets().Value().size(); + size = size > kMaxSupportedDstStructs ? kMaxSupportedDstStructs : size; + for (size_t i = 0; i < size; ++i) { + mDstOffsetsBuf[i] = params.GetDSTOffsets().Value()[i]; + } + auto list = app::DataModel::List(mDstOffsetsBuf, size); + mParams.SetDSTOffsets(list); + } + if (params.GetTimeZone().HasValue()) { + ChipLogProgress(Controller, "Setting Time Zone from parameters"); + size_t size = params.GetTimeZone().Value().size(); + size = size > kMaxSupportedTimeZones ? kMaxSupportedTimeZones : size; + for (size_t i = 0; i < size; ++i) { + mTimeZoneBuf[i] = params.GetTimeZone().Value()[i]; + if (mTimeZoneBuf[i].name.HasValue()) { + auto span = MutableCharSpan(mTimeZoneNames[i], kMaxTimeZoneNameLen); + CopyCharSpanToMutableCharSpan(mTimeZoneBuf[i].name.Value(), span); + mTimeZoneBuf[i].name.SetValue(span); + + } + } + } + return CHIP_NO_ERROR; } diff --git a/src/controller/AutoCommissioner.h b/src/controller/AutoCommissioner.h index 66dafeef6851cb..4bf6217ee4c019 100644 --- a/src/controller/AutoCommissioner.h +++ b/src/controller/AutoCommissioner.h @@ -102,6 +102,17 @@ class AutoCommissioner : public CommissioningDelegate uint8_t mThreadOperationalDataset[CommissioningParameters::kMaxThreadDatasetLen]; char mCountryCode[CommissioningParameters::kMaxCountryCodeLen]; + // Time zone is statically allocated because it is max 2 and not trivially destructible + static constexpr size_t kMaxSupportedTimeZones = 2; + app::Clusters::TimeSynchronization::Structs::TimeZoneStruct::Type mTimeZoneBuf[kMaxSupportedTimeZones]; + static constexpr size_t kMaxTimeZoneNameLen = 64; + char mTimeZoneNames[kMaxTimeZoneNameLen][kMaxSupportedTimeZones]; + + // DSTOffsetStructs are similarly not trivially destructible. They don't have a defined size, but we're + // going to do static allocation of the buffers anyway until we replace chip::Optional with std::optional. + static constexpr size_t kMaxSupportedDstStructs = 10; + app::Clusters::TimeSynchronization::Structs::DSTOffsetStruct::Type mDstOffsetsBuf[kMaxSupportedDstStructs]; + bool mNeedsNetworkSetup = false; ReadCommissioningInfo mDeviceCommissioningInfo; bool mNeedsDST = false; diff --git a/src/controller/CommissioningDelegate.h b/src/controller/CommissioningDelegate.h index 47ca12166ec8bf..3ccfd87084dab2 100644 --- a/src/controller/CommissioningDelegate.h +++ b/src/controller/CommissioningDelegate.h @@ -511,6 +511,8 @@ class CommissioningParameters mAttestationSignature.ClearValue(); mPAI.ClearValue(); mDAC.ClearValue(); + mTimeZone.ClearValue(); + mDSTOffsets.ClearValue(); } private: From 6fc80bc6ffdb9bc1963f533ab07b5d208fd84b24 Mon Sep 17 00:00:00 2001 From: "Restyled.io" Date: Mon, 23 Oct 2023 13:16:03 +0000 Subject: [PATCH 2/3] Restyled by clang-format --- src/controller/AutoCommissioner.cpp | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/src/controller/AutoCommissioner.cpp b/src/controller/AutoCommissioner.cpp index 596119623216de..8bbc7d61da3c8a 100644 --- a/src/controller/AutoCommissioner.cpp +++ b/src/controller/AutoCommissioner.cpp @@ -86,7 +86,8 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam IsUnsafeSpan(params.GetAttestationElements(), mParams.GetAttestationElements()) || IsUnsafeSpan(params.GetAttestationSignature(), mParams.GetAttestationSignature()) || IsUnsafeSpan(params.GetPAI(), mParams.GetPAI()) || IsUnsafeSpan(params.GetDAC(), mParams.GetDAC()) || - IsUnsafeSpan(params.GetTimeZone(), mParams.GetTimeZone()) || IsUnsafeSpan(params.GetDSTOffsets(), mParams.GetDSTOffsets())); + IsUnsafeSpan(params.GetTimeZone(), mParams.GetTimeZone()) || + IsUnsafeSpan(params.GetDSTOffsets(), mParams.GetDSTOffsets())); mParams = params; @@ -175,27 +176,31 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam } mParams.SetCSRNonce(ByteSpan(mCSRNonce, sizeof(mCSRNonce))); - if (params.GetDSTOffsets().HasValue()) { + if (params.GetDSTOffsets().HasValue()) + { ChipLogProgress(Controller, "Setting DST offsets from parameters"); size_t size = params.GetDSTOffsets().Value().size(); - size = size > kMaxSupportedDstStructs ? kMaxSupportedDstStructs : size; - for (size_t i = 0; i < size; ++i) { + size = size > kMaxSupportedDstStructs ? kMaxSupportedDstStructs : size; + for (size_t i = 0; i < size; ++i) + { mDstOffsetsBuf[i] = params.GetDSTOffsets().Value()[i]; } auto list = app::DataModel::List(mDstOffsetsBuf, size); mParams.SetDSTOffsets(list); } - if (params.GetTimeZone().HasValue()) { + if (params.GetTimeZone().HasValue()) + { ChipLogProgress(Controller, "Setting Time Zone from parameters"); size_t size = params.GetTimeZone().Value().size(); - size = size > kMaxSupportedTimeZones ? kMaxSupportedTimeZones : size; - for (size_t i = 0; i < size; ++i) { + size = size > kMaxSupportedTimeZones ? kMaxSupportedTimeZones : size; + for (size_t i = 0; i < size; ++i) + { mTimeZoneBuf[i] = params.GetTimeZone().Value()[i]; - if (mTimeZoneBuf[i].name.HasValue()) { + if (mTimeZoneBuf[i].name.HasValue()) + { auto span = MutableCharSpan(mTimeZoneNames[i], kMaxTimeZoneNameLen); CopyCharSpanToMutableCharSpan(mTimeZoneBuf[i].name.Value(), span); mTimeZoneBuf[i].name.SetValue(span); - } } } From f414b098862f0f77baa8e53cdd51a1cf2dd1703e Mon Sep 17 00:00:00 2001 From: cecille Date: Mon, 23 Oct 2023 12:41:27 -0400 Subject: [PATCH 3/3] use a min function rather than being crazy --- src/controller/AutoCommissioner.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/controller/AutoCommissioner.cpp b/src/controller/AutoCommissioner.cpp index 8bbc7d61da3c8a..85367d4532ccc7 100644 --- a/src/controller/AutoCommissioner.cpp +++ b/src/controller/AutoCommissioner.cpp @@ -179,8 +179,7 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam if (params.GetDSTOffsets().HasValue()) { ChipLogProgress(Controller, "Setting DST offsets from parameters"); - size_t size = params.GetDSTOffsets().Value().size(); - size = size > kMaxSupportedDstStructs ? kMaxSupportedDstStructs : size; + size_t size = std::min(params.GetDSTOffsets().Value().size(), kMaxSupportedDstStructs); for (size_t i = 0; i < size; ++i) { mDstOffsetsBuf[i] = params.GetDSTOffsets().Value()[i]; @@ -191,8 +190,7 @@ CHIP_ERROR AutoCommissioner::SetCommissioningParameters(const CommissioningParam if (params.GetTimeZone().HasValue()) { ChipLogProgress(Controller, "Setting Time Zone from parameters"); - size_t size = params.GetTimeZone().Value().size(); - size = size > kMaxSupportedTimeZones ? kMaxSupportedTimeZones : size; + size_t size = std::min(params.GetTimeZone().Value().size(), kMaxSupportedTimeZones); for (size_t i = 0; i < size; ++i) { mTimeZoneBuf[i] = params.GetTimeZone().Value()[i];