From e241e32083a1b21dfdef438f17ffdf834b5d1153 Mon Sep 17 00:00:00 2001 From: mideayanghui <106149377+mideayanghui@users.noreply.github.com> Date: Tue, 9 Jan 2024 10:02:01 +0800 Subject: [PATCH] Fixes: [OPSTATE] specific phase in the list should not null (#31176) * fix #27931 [OPSTATE] specific phase in the list should not null * Restyled by clang-format * remove unuseful namespace * modify the document of api * modify the document for api --------- Co-authored-by: Restyled.io --- .../include/operational-state-delegate-impl.h | 25 ++---- .../src/operational-state-delegate-impl.cpp | 5 +- .../ameba/main/OperationalStateManager.cpp | 5 +- .../main/include/OperationalStateManager.h | 25 ++---- .../chef-rvc-operational-state-delegate.cpp | 5 +- .../chef-rvc-operational-state-delegate.h | 25 ++---- .../include/operational-state-delegate-impl.h | 18 ++--- .../src/operational-state-delegate-impl.cpp | 7 +- .../include/microwave-oven-device.h | 19 ++--- .../src/microwave-oven-device.cpp | 8 +- .../include/rvc-operational-state-delegate.h | 17 ++-- .../src/rvc-operational-state-delegate.cpp | 8 +- .../operational-state-cluster-objects.h | 44 ---------- .../operational-state-server.cpp | 28 ++++--- .../operational-state-server.h | 15 +++- .../TestOperationalStateClusterObjects.cpp | 80 ------------------- 16 files changed, 94 insertions(+), 240 deletions(-) diff --git a/examples/all-clusters-app/all-clusters-common/include/operational-state-delegate-impl.h b/examples/all-clusters-app/all-clusters-common/include/operational-state-delegate-impl.h index fd6df4b90359f1..c8d63c2bb6efc2 100644 --- a/examples/all-clusters-app/all-clusters-common/include/operational-state-delegate-impl.h +++ b/examples/all-clusters-app/all-clusters-common/include/operational-state-delegate-impl.h @@ -50,14 +50,18 @@ class GenericOperationalStateDelegateImpl : public Delegate CHIP_ERROR GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState) override; /** - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) override; + CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; // command callback /** @@ -86,7 +90,7 @@ class GenericOperationalStateDelegateImpl : public Delegate protected: Span mOperationalStateList; - Span mOperationalPhaseList; + Span mOperationalPhaseList; }; // This is an application level delegate to handle operational state commands according to the specific business logic. @@ -100,16 +104,10 @@ class OperationalStateDelegate : public GenericOperationalStateDelegateImpl GenericOperationalState(to_underlying(OperationalStateEnum::kError)), }; - const GenericOperationalPhase opPhaseList[1] = { - // Phase List is null - GenericOperationalPhase(DataModel::Nullable()), - }; - public: OperationalStateDelegate() { GenericOperationalStateDelegateImpl::mOperationalStateList = Span(opStateList); - GenericOperationalStateDelegateImpl::mOperationalPhaseList = Span(opPhaseList); } }; @@ -134,18 +132,11 @@ class RvcOperationalStateDelegate : public OperationalState::GenericOperationalS OperationalState::GenericOperationalState(to_underlying(Clusters::RvcOperationalState::OperationalStateEnum::kDocked)), }; - const OperationalState::GenericOperationalPhase rvcOpPhaseList[1] = { - // Phase List is null - OperationalState::GenericOperationalPhase(DataModel::Nullable()), - }; - public: RvcOperationalStateDelegate() { GenericOperationalStateDelegateImpl::mOperationalStateList = Span(rvcOpStateList); - GenericOperationalStateDelegateImpl::mOperationalPhaseList = - Span(rvcOpPhaseList); } }; diff --git a/examples/all-clusters-app/all-clusters-common/src/operational-state-delegate-impl.cpp b/examples/all-clusters-app/all-clusters-common/src/operational-state-delegate-impl.cpp index ec3e392152384f..02bcd4a775a07e 100644 --- a/examples/all-clusters-app/all-clusters-common/src/operational-state-delegate-impl.cpp +++ b/examples/all-clusters-app/all-clusters-common/src/operational-state-delegate-impl.cpp @@ -33,14 +33,13 @@ CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalStateAtIndex(size_ return CHIP_NO_ERROR; } -CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) +CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) { if (index >= mOperationalPhaseList.size()) { return CHIP_ERROR_NOT_FOUND; } - operationalPhase = mOperationalPhaseList[index]; - return CHIP_NO_ERROR; + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); } void GenericOperationalStateDelegateImpl::HandlePauseStateCallback(GenericOperationalError & err) diff --git a/examples/all-clusters-app/ameba/main/OperationalStateManager.cpp b/examples/all-clusters-app/ameba/main/OperationalStateManager.cpp index 68ee456aee9461..8dbeda60d75526 100644 --- a/examples/all-clusters-app/ameba/main/OperationalStateManager.cpp +++ b/examples/all-clusters-app/ameba/main/OperationalStateManager.cpp @@ -33,14 +33,13 @@ CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalStateAtIndex(size_ return CHIP_NO_ERROR; } -CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) +CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) { if (index >= mOperationalPhaseList.size()) { return CHIP_ERROR_NOT_FOUND; } - operationalPhase = mOperationalPhaseList[index]; - return CHIP_NO_ERROR; + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); } void GenericOperationalStateDelegateImpl::HandlePauseStateCallback(GenericOperationalError & err) diff --git a/examples/all-clusters-app/ameba/main/include/OperationalStateManager.h b/examples/all-clusters-app/ameba/main/include/OperationalStateManager.h index 875534cc93638d..2183e92078e6c7 100644 --- a/examples/all-clusters-app/ameba/main/include/OperationalStateManager.h +++ b/examples/all-clusters-app/ameba/main/include/OperationalStateManager.h @@ -54,14 +54,18 @@ class GenericOperationalStateDelegateImpl : public Delegate CHIP_ERROR GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState) override; /** - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) override; + CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; // command callback /** @@ -90,7 +94,7 @@ class GenericOperationalStateDelegateImpl : public Delegate protected: Span mOperationalStateList; - Span mOperationalPhaseList; + Span mOperationalPhaseList; }; // This is an application level delegate to handle operational state commands according to the specific business logic. @@ -104,16 +108,10 @@ class OperationalStateDelegate : public GenericOperationalStateDelegateImpl GenericOperationalState(to_underlying(OperationalStateEnum::kError)), }; - const GenericOperationalPhase opPhaseList[1] = { - // Phase List is null - GenericOperationalPhase(DataModel::Nullable()), - }; - public: OperationalStateDelegate() { GenericOperationalStateDelegateImpl::mOperationalStateList = Span(opStateList); - GenericOperationalStateDelegateImpl::mOperationalPhaseList = Span(opPhaseList); } }; @@ -140,18 +138,11 @@ class RvcOperationalStateDelegate : public OperationalState::GenericOperationalS OperationalState::GenericOperationalState(to_underlying(Clusters::RvcOperationalState::OperationalStateEnum::kDocked)), }; - const OperationalState::GenericOperationalPhase rvcOpPhaseList[1] = { - // Phase List is null - OperationalState::GenericOperationalPhase(DataModel::Nullable()), - }; - public: RvcOperationalStateDelegate() { GenericOperationalStateDelegateImpl::mOperationalStateList = Span(rvcOpStateList); - GenericOperationalStateDelegateImpl::mOperationalPhaseList = - Span(rvcOpPhaseList); } }; diff --git a/examples/chef/common/chef-rvc-operational-state-delegate.cpp b/examples/chef/common/chef-rvc-operational-state-delegate.cpp index 119c2cfdb019be..9e376567b10f48 100644 --- a/examples/chef/common/chef-rvc-operational-state-delegate.cpp +++ b/examples/chef/common/chef-rvc-operational-state-delegate.cpp @@ -37,14 +37,13 @@ CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalStateAtIndex(size_ return CHIP_NO_ERROR; } -CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) +CHIP_ERROR GenericOperationalStateDelegateImpl::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) { if (index >= mOperationalPhaseList.size()) { return CHIP_ERROR_NOT_FOUND; } - operationalPhase = mOperationalPhaseList[index]; - return CHIP_NO_ERROR; + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); } void GenericOperationalStateDelegateImpl::HandlePauseStateCallback(GenericOperationalError & err) diff --git a/examples/chef/common/chef-rvc-operational-state-delegate.h b/examples/chef/common/chef-rvc-operational-state-delegate.h index fd6df4b90359f1..c8d63c2bb6efc2 100644 --- a/examples/chef/common/chef-rvc-operational-state-delegate.h +++ b/examples/chef/common/chef-rvc-operational-state-delegate.h @@ -50,14 +50,18 @@ class GenericOperationalStateDelegateImpl : public Delegate CHIP_ERROR GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState) override; /** - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) override; + CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; // command callback /** @@ -86,7 +90,7 @@ class GenericOperationalStateDelegateImpl : public Delegate protected: Span mOperationalStateList; - Span mOperationalPhaseList; + Span mOperationalPhaseList; }; // This is an application level delegate to handle operational state commands according to the specific business logic. @@ -100,16 +104,10 @@ class OperationalStateDelegate : public GenericOperationalStateDelegateImpl GenericOperationalState(to_underlying(OperationalStateEnum::kError)), }; - const GenericOperationalPhase opPhaseList[1] = { - // Phase List is null - GenericOperationalPhase(DataModel::Nullable()), - }; - public: OperationalStateDelegate() { GenericOperationalStateDelegateImpl::mOperationalStateList = Span(opStateList); - GenericOperationalStateDelegateImpl::mOperationalPhaseList = Span(opPhaseList); } }; @@ -134,18 +132,11 @@ class RvcOperationalStateDelegate : public OperationalState::GenericOperationalS OperationalState::GenericOperationalState(to_underlying(Clusters::RvcOperationalState::OperationalStateEnum::kDocked)), }; - const OperationalState::GenericOperationalPhase rvcOpPhaseList[1] = { - // Phase List is null - OperationalState::GenericOperationalPhase(DataModel::Nullable()), - }; - public: RvcOperationalStateDelegate() { GenericOperationalStateDelegateImpl::mOperationalStateList = Span(rvcOpStateList); - GenericOperationalStateDelegateImpl::mOperationalPhaseList = - Span(rvcOpPhaseList); } }; diff --git a/examples/dishwasher-app/dishwasher-common/include/operational-state-delegate-impl.h b/examples/dishwasher-app/dishwasher-common/include/operational-state-delegate-impl.h index 5133fb75c93b00..9fd023db8194d9 100644 --- a/examples/dishwasher-app/dishwasher-common/include/operational-state-delegate-impl.h +++ b/examples/dishwasher-app/dishwasher-common/include/operational-state-delegate-impl.h @@ -50,12 +50,16 @@ class OperationalStateDelegate : public Delegate /** * Get the list of supported operational phases. - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) override; + CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; // command callback /** @@ -91,13 +95,7 @@ class OperationalStateDelegate : public Delegate }; app::DataModel::List mOperationalStateList = Span(rvcOpStateList); - - const GenericOperationalPhase opPhaseList[1] = { - // Phase List is null - GenericOperationalPhase(DataModel::Nullable()), - }; - - Span mOperationalPhaseList = Span(opPhaseList); + const Span mOperationalPhaseList; }; void Shutdown(); diff --git a/examples/dishwasher-app/dishwasher-common/src/operational-state-delegate-impl.cpp b/examples/dishwasher-app/dishwasher-common/src/operational-state-delegate-impl.cpp index 815ab69faa30df..a4462217b7904c 100644 --- a/examples/dishwasher-app/dishwasher-common/src/operational-state-delegate-impl.cpp +++ b/examples/dishwasher-app/dishwasher-common/src/operational-state-delegate-impl.cpp @@ -32,14 +32,13 @@ CHIP_ERROR OperationalStateDelegate::GetOperationalStateAtIndex(size_t index, Ge return CHIP_NO_ERROR; } -CHIP_ERROR OperationalStateDelegate::GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) +CHIP_ERROR OperationalStateDelegate::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) { - if (index > mOperationalPhaseList.size() - 1) + if (index >= mOperationalPhaseList.size()) { return CHIP_ERROR_NOT_FOUND; } - operationalPhase = mOperationalPhaseList[index]; - return CHIP_NO_ERROR; + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); } void OperationalStateDelegate::HandlePauseStateCallback(GenericOperationalError & err) diff --git a/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h b/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h index a3f0bd32831f73..c42c36ac5a4077 100644 --- a/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h +++ b/examples/microwave-oven-app/microwave-oven-common/include/microwave-oven-device.h @@ -111,14 +111,18 @@ class ExampleMicrowaveOvenDevice : public MicrowaveOvenControl::Delegate, CHIP_ERROR GetOperationalStateAtIndex(size_t index, OperationalState::GenericOperationalState & operationalState) override; /** - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, OperationalState::GenericOperationalPhase & operationalPhase) override; + CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; /** * Handle Command Callback in application: Pause @@ -220,14 +224,7 @@ class ExampleMicrowaveOvenDevice : public MicrowaveOvenControl::Delegate, const app::DataModel::List mOperationalStateList = Span(mOpStateList); - - const OperationalState::GenericOperationalPhase mOpPhaseList[1] = { - // Phase List is null - OperationalState::GenericOperationalPhase(DataModel::Nullable()), - }; - - Span mOperationalPhaseList = - Span(mOpPhaseList); + const Span mOperationalPhaseList; }; } // namespace Clusters diff --git a/examples/microwave-oven-app/microwave-oven-common/src/microwave-oven-device.cpp b/examples/microwave-oven-app/microwave-oven-common/src/microwave-oven-device.cpp index 59823844749286..2b584701e42192 100644 --- a/examples/microwave-oven-app/microwave-oven-common/src/microwave-oven-device.cpp +++ b/examples/microwave-oven-app/microwave-oven-common/src/microwave-oven-device.cpp @@ -80,15 +80,13 @@ CHIP_ERROR ExampleMicrowaveOvenDevice::GetOperationalStateAtIndex(size_t index, return CHIP_NO_ERROR; } -CHIP_ERROR ExampleMicrowaveOvenDevice::GetOperationalPhaseAtIndex(size_t index, - OperationalState::GenericOperationalPhase & operationalPhase) +CHIP_ERROR ExampleMicrowaveOvenDevice::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) { - if (index > mOperationalPhaseList.size() - 1) + if (index >= mOperationalPhaseList.size()) { return CHIP_ERROR_NOT_FOUND; } - operationalPhase = mOperationalPhaseList[index]; - return CHIP_NO_ERROR; + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); } void ExampleMicrowaveOvenDevice::HandlePauseStateCallback(OperationalState::GenericOperationalError & err) diff --git a/examples/rvc-app/rvc-common/include/rvc-operational-state-delegate.h b/examples/rvc-app/rvc-common/include/rvc-operational-state-delegate.h index b2737a0c34b27f..d69843d6de8fb8 100644 --- a/examples/rvc-app/rvc-common/include/rvc-operational-state-delegate.h +++ b/examples/rvc-app/rvc-common/include/rvc-operational-state-delegate.h @@ -47,11 +47,7 @@ class RvcOperationalStateDelegate : public OperationalState::Delegate OperationalState::GenericOperationalState(to_underlying(Clusters::RvcOperationalState::OperationalStateEnum::kCharging)), OperationalState::GenericOperationalState(to_underlying(Clusters::RvcOperationalState::OperationalStateEnum::kDocked)), }; - - const Clusters::OperationalState::GenericOperationalPhase mOperationalPhaseList[1] = { - // Phase List is null - OperationalState::GenericOperationalPhase(DataModel::Nullable()), - }; + const Span mOperationalPhaseList; RvcDevice * mPauseRvcDeviceInstance; HandleOpStateCommand mPauseCallback; @@ -77,15 +73,18 @@ class RvcOperationalStateDelegate : public OperationalState::Delegate Clusters::OperationalState::GenericOperationalState & operationalState) override; /** - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, - Clusters::OperationalState::GenericOperationalPhase & operationalPhase) override; + CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; // command callback /** diff --git a/examples/rvc-app/rvc-common/src/rvc-operational-state-delegate.cpp b/examples/rvc-app/rvc-common/src/rvc-operational-state-delegate.cpp index c94de8f231bbe4..568fa870a9e860 100644 --- a/examples/rvc-app/rvc-common/src/rvc-operational-state-delegate.cpp +++ b/examples/rvc-app/rvc-common/src/rvc-operational-state-delegate.cpp @@ -33,15 +33,13 @@ CHIP_ERROR RvcOperationalStateDelegate::GetOperationalStateAtIndex(size_t index, return CHIP_NO_ERROR; } -CHIP_ERROR RvcOperationalStateDelegate::GetOperationalPhaseAtIndex(size_t index, - OperationalState::GenericOperationalPhase & operationalPhase) +CHIP_ERROR RvcOperationalStateDelegate::GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) { - if (index >= ArraySize(mOperationalPhaseList)) + if (index >= mOperationalPhaseList.size()) { return CHIP_ERROR_NOT_FOUND; } - operationalPhase = mOperationalPhaseList[index]; - return CHIP_NO_ERROR; + return CopyCharSpanToMutableCharSpan(mOperationalPhaseList[index], operationalPhase); } void RvcOperationalStateDelegate::HandlePauseStateCallback(OperationalState::GenericOperationalError & err) diff --git a/src/app/clusters/operational-state-server/operational-state-cluster-objects.h b/src/app/clusters/operational-state-server/operational-state-cluster-objects.h index 1910e00d20effb..58fd8401e8adcc 100644 --- a/src/app/clusters/operational-state-server/operational-state-cluster-objects.h +++ b/src/app/clusters/operational-state-server/operational-state-cluster-objects.h @@ -172,50 +172,6 @@ struct GenericOperationalError : public app::Clusters::detail::Structs::ErrorSta char mErrorStateDetailsBuffer[kOperationalErrorDetailsMaxSize]; }; -/** - * A class which represents the operational phase of an Operational State cluster derivation instance. - */ -struct GenericOperationalPhase -{ - GenericOperationalPhase(app::DataModel::Nullable name) { Set(name); } - - GenericOperationalPhase(const GenericOperationalPhase & ph) { *this = ph; } - - GenericOperationalPhase & operator=(const GenericOperationalPhase & ph) - { - Set(ph.mPhaseName); - return *this; - } - - bool IsMissing() const { return mPhaseName.IsNull(); } - app::DataModel::Nullable mPhaseName; - -private: - void Set(app::DataModel::Nullable name) - { - if (name.IsNull()) - { - mPhaseName.SetNull(); - } - else - { - memset(mPhaseNameBuffer, 0, sizeof(mPhaseNameBuffer)); - if (name.Value().size() > sizeof(mPhaseNameBuffer)) - { - memcpy(mPhaseNameBuffer, name.Value().data(), sizeof(mPhaseNameBuffer)); - mPhaseName = app::DataModel::Nullable(CharSpan(mPhaseNameBuffer, sizeof(mPhaseNameBuffer))); - } - else - { - memcpy(mPhaseNameBuffer, name.Value().data(), name.Value().size()); - mPhaseName = app::DataModel::Nullable(CharSpan(mPhaseNameBuffer, name.Value().size())); - } - } - } - - char mPhaseNameBuffer[kOperationalPhaseNameMaxSize]; -}; - /** * A class which represents the operational error event of an Operational State cluster derivation instance. */ diff --git a/src/app/clusters/operational-state-server/operational-state-server.cpp b/src/app/clusters/operational-state-server/operational-state-server.cpp index c7b7cd1951df26..a76d14e20d350b 100644 --- a/src/app/clusters/operational-state-server/operational-state-server.cpp +++ b/src/app/clusters/operational-state-server/operational-state-server.cpp @@ -180,7 +180,8 @@ void Instance::ReportPhaseListChange() bool Instance::IsSupportedPhase(uint8_t aPhase) { - GenericOperationalPhase phase = GenericOperationalPhase(DataModel::Nullable()); + char buffer[kMaxPhaseNameLength]; + MutableCharSpan phase(buffer); if (mDelegate->GetOperationalPhaseAtIndex(aPhase, phase) != CHIP_ERROR_NOT_FOUND) { return true; @@ -301,20 +302,29 @@ CHIP_ERROR Instance::Read(const ConcreteReadAttributePath & aPath, AttributeValu break; case OperationalState::Attributes::PhaseList::Id: { - GenericOperationalPhase phase = GenericOperationalPhase(DataModel::Nullable()); - size_t index = 0; - if (mDelegate->GetOperationalPhaseAtIndex(index, phase) == CHIP_ERROR_NOT_FOUND || phase.IsMissing()) + char buffer[kMaxPhaseNameLength]; + MutableCharSpan phase(buffer); + size_t index = 0; + + if (mDelegate->GetOperationalPhaseAtIndex(index, phase) == CHIP_ERROR_NOT_FOUND) { return aEncoder.EncodeNull(); } - return aEncoder.EncodeList([&](const auto & encoder) -> CHIP_ERROR { - while (this->mDelegate->GetOperationalPhaseAtIndex(index, phase) != CHIP_ERROR_NOT_FOUND) + + return aEncoder.EncodeList([delegate = mDelegate](const auto & encoder) -> CHIP_ERROR { + for (uint8_t i = 0; true; i++) { - ReturnErrorOnFailure(encoder.Encode(phase.mPhaseName)); - index++; + char buffer2[kMaxPhaseNameLength]; + MutableCharSpan phase2(buffer2); + auto err = delegate->GetOperationalPhaseAtIndex(i, phase2); + if (err == CHIP_ERROR_NOT_FOUND) + { + return CHIP_NO_ERROR; + } + ReturnErrorOnFailure(err); + ReturnErrorOnFailure(encoder.Encode(phase2)); } - return CHIP_NO_ERROR; }); } break; diff --git a/src/app/clusters/operational-state-server/operational-state-server.h b/src/app/clusters/operational-state-server/operational-state-server.h index 88725a1ff32b65..ed0993ea7aed31 100644 --- a/src/app/clusters/operational-state-server/operational-state-server.h +++ b/src/app/clusters/operational-state-server/operational-state-server.h @@ -65,6 +65,11 @@ class Instance : public CommandHandlerInterface, public AttributeAccessInterface ~Instance() override; + /** + * Phase name's max length + */ + static constexpr uint8_t kMaxPhaseNameLength = 64; + /** * Initialise the operational state server instance. * This function must be called after defining an Instance class object. @@ -262,14 +267,18 @@ class Delegate virtual CHIP_ERROR GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState) = 0; /** - * Fills in the provided GenericOperationalPhase with the phase at index `index` if there is one, + * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. + * + * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null + * (there are no phases defined at all). + * * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. * @param index The index of the phase, with 0 representing the first phase. - * @param operationalPhase The GenericOperationalPhase is filled. + * @param operationalPhase The MutableCharSpan is filled. */ - virtual CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, GenericOperationalPhase & operationalPhase) = 0; + virtual CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) = 0; // command callback /** diff --git a/src/app/tests/TestOperationalStateClusterObjects.cpp b/src/app/tests/TestOperationalStateClusterObjects.cpp index 0684826f2194d1..9f7d1e1efcadac 100644 --- a/src/app/tests/TestOperationalStateClusterObjects.cpp +++ b/src/app/tests/TestOperationalStateClusterObjects.cpp @@ -465,83 +465,6 @@ void TestStructGenericOperationalErrorFuncSet(nlTestSuite * inSuite, void * inCo kOperationalErrorDetailsMaxSize) == 0); } -void TestStructGenericOperationalPhaseConstructor(nlTestSuite * inSuite, void * inContext) -{ - using namespace chip::app; - using namespace chip::app::Clusters::OperationalState; - - GenericOperationalPhase phase = GenericOperationalPhase(DataModel::Nullable()); - NL_TEST_ASSERT(inSuite, phase.IsMissing() == true); - - char phaseBuffer[kOperationalPhaseNameMaxSize] = "start"; - GenericOperationalPhase phase2(DataModel::Nullable(CharSpan::fromCharString(phaseBuffer))); - NL_TEST_ASSERT(inSuite, phase2.IsMissing() == false); - NL_TEST_ASSERT(inSuite, phase2.mPhaseName.Value().size() == strlen(phaseBuffer)); - NL_TEST_ASSERT(inSuite, memcmp(const_cast(phase2.mPhaseName.Value().data()), phaseBuffer, strlen(phaseBuffer)) == 0); -} - -void TestStructGenericOperationalPhaseCopyConstructor(nlTestSuite * inSuite, void * inContext) -{ - using namespace chip::app; - using namespace chip::app::Clusters::OperationalState; - - char phaseBuffer[kOperationalPhaseNameMaxSize] = "start"; - GenericOperationalPhase phase(DataModel::Nullable(CharSpan::fromCharString(phaseBuffer))); - - GenericOperationalPhase phase2(phase); - - NL_TEST_ASSERT(inSuite, phase2.IsMissing() == false); - NL_TEST_ASSERT(inSuite, phase2.mPhaseName.Value().size() == phase.mPhaseName.Value().size()); - NL_TEST_ASSERT(inSuite, - memcmp(const_cast(phase2.mPhaseName.Value().data()), const_cast(phase.mPhaseName.Value().data()), - phase.mPhaseName.Value().size()) == 0); -} - -void TestStructGenericOperationalPhaseCopyAssignment(nlTestSuite * inSuite, void * inContext) -{ - using namespace chip::app; - using namespace chip::app::Clusters::OperationalState; - - // copy assignment with null-name - GenericOperationalPhase phase = GenericOperationalPhase(DataModel::Nullable()); - NL_TEST_ASSERT(inSuite, phase.IsMissing() == true); - - // copy assignment with name - char phaseBuffer[kOperationalPhaseNameMaxSize] = "start"; - GenericOperationalPhase phase2(DataModel::Nullable(CharSpan::fromCharString(phaseBuffer))); - phase = phase2; - - NL_TEST_ASSERT(inSuite, phase.IsMissing() == false); - NL_TEST_ASSERT(inSuite, phase.mPhaseName.Value().size() == phase2.mPhaseName.Value().size()); - NL_TEST_ASSERT(inSuite, - memcmp(const_cast(phase.mPhaseName.Value().data()), const_cast(phase2.mPhaseName.Value().data()), - phase.mPhaseName.Value().size()) == 0); - - // copy assignment with name, name's len = kOperationalPhaseNameMaxSize - for (size_t i = 0; i < sizeof(phaseBuffer); i++) - { - phaseBuffer[i] = 1; - } - phase = GenericOperationalPhase(DataModel::Nullable(CharSpan(phaseBuffer, sizeof(phaseBuffer)))); - - NL_TEST_ASSERT(inSuite, phase.IsMissing() == false); - NL_TEST_ASSERT(inSuite, phase.mPhaseName.Value().size() == sizeof(phaseBuffer)); - NL_TEST_ASSERT(inSuite, memcmp(const_cast(phase.mPhaseName.Value().data()), phaseBuffer, sizeof(phaseBuffer)) == 0); - - // copy assignment with name, name's len = kOperationalPhaseNameMaxSize + 1 - char phaseBuffer2[kOperationalPhaseNameMaxSize + 1]; - for (size_t i = 0; i < sizeof(phaseBuffer2); i++) - { - phaseBuffer2[i] = 2; - } - phase = GenericOperationalPhase(DataModel::Nullable(CharSpan(phaseBuffer2, sizeof(phaseBuffer2)))); - - NL_TEST_ASSERT(inSuite, phase.IsMissing() == false); - NL_TEST_ASSERT(inSuite, phase.mPhaseName.Value().size() == kOperationalPhaseNameMaxSize); - NL_TEST_ASSERT(inSuite, - memcmp(const_cast(phase.mPhaseName.Value().data()), phaseBuffer2, kOperationalPhaseNameMaxSize) == 0); -} - const nlTest sTests[] = { NL_TEST_DEF("Test struct GenericOperationalState: constructor with only StateID", TestStructGenericOperationalStateConstructorWithOnlyStateID), @@ -559,9 +482,6 @@ const nlTest sTests[] = { NL_TEST_DEF("Test struct GenericOperationalError: copy constructor", TestStructGenericOperationalErrorCopyConstructor), NL_TEST_DEF("Test struct GenericOperationalError: copy assignment", TestStructGenericOperationalErrorCopyAssignment), NL_TEST_DEF("Test struct GenericOperationalError: member function 'Set'", TestStructGenericOperationalErrorFuncSet), - NL_TEST_DEF("Test struct GenericOperationalPhase: constructor", TestStructGenericOperationalPhaseConstructor), - NL_TEST_DEF("Test struct GenericOperationalPhase: copy constructor", TestStructGenericOperationalPhaseCopyConstructor), - NL_TEST_DEF("Test struct GenericOperationalPhase: copy assignment", TestStructGenericOperationalPhaseCopyAssignment), NL_TEST_SENTINEL() };