Skip to content

Commit fdb31db

Browse files
[Code Driven Cluster] microwave oven control cluster part 1 (project-chip#71444)
* Decouple microwave oven control cluster part 1 * Restyled by clang-format * Restyled by gn * Update chip_main.cmake --------- Co-authored-by: Restyled.io <commits@restyled.io>
1 parent 967feab commit fdb31db

8 files changed

Lines changed: 269 additions & 212 deletions

File tree

examples/all-clusters-app/ameba/chip_main.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ endif (matter_enable_ota_requestor)
153153
list(
154154
APPEND ${list_chip_main_sources}
155155

156-
${chip_dir}/src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp
156+
${chip_dir}/src/app/clusters/microwave-oven-control-server/MicrowaveOvenControlCluster.cpp
157157

158158
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp
159159
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/air-quality-instance.cpp

examples/all-clusters-app/realtek/common/chip_main.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ endif (matter_enable_ota_requestor)
152152
list(
153153
APPEND ${list_chip_main_sources}
154154

155-
${chip_dir}/src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp
155+
${chip_dir}/src/app/clusters/microwave-oven-control-server/MicrowaveOvenControlCluster.cpp
156156

157157
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/bridged-actions-stub.cpp
158158
${chip_dir}/examples/all-clusters-app/all-clusters-common/src/air-quality-instance.cpp
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
/*
2+
*
3+
* Copyright (c) 2023-2026 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <app-common/zap-generated/cluster-objects.h>
22+
#include <app/AttributeAccessInterface.h>
23+
#include <app/CommandHandlerInterface.h>
24+
#include <app/clusters/microwave-oven-control-server/Delegate.h>
25+
#include <app/clusters/mode-base-server/mode-base-server.h>
26+
#include <app/clusters/operational-state-server/operational-state-server.h>
27+
28+
namespace chip {
29+
namespace app {
30+
namespace Clusters {
31+
namespace MicrowaveOvenControl {
32+
33+
constexpr uint32_t kDefaultCookTimeSec = 30u;
34+
constexpr uint32_t kMinCookTimeSec = 1u;
35+
constexpr uint8_t kDefaultMinPowerNum = 10u;
36+
constexpr uint8_t kDefaultMaxPowerNum = 100u;
37+
constexpr uint8_t kDefaultPowerStepNum = 10u;
38+
39+
class Delegate;
40+
41+
class Instance : public CommandHandlerInterface, public AttributeAccessInterface
42+
{
43+
public:
44+
/**
45+
* @brief Creates a Microwave Oven Control cluster instance. The Init() function needs to be called for this instance
46+
* to be registered and called by the interaction model at the appropriate times.
47+
* @param aDelegate A pointer to the delegate to be used by this server.
48+
* Note: the caller must ensure that the delegate lives throughout the instance's lifetime.
49+
* @param aEndpointId The endpoint on which this cluster exists. This must match the zap configuration.
50+
* @param aClusterId The ID of the Microwave Oven Control cluster to be instantiated.
51+
* @param aFeature The bitmask value that identifies which features are supported by this instance.
52+
* @param aOpStateInstance The reference of Operational State Instance.
53+
* @param aMicrowaveOvenModeInstance The reference of Microwave Oven Mode Instance.
54+
* Note: a MicrowaveOvenControl instance must relies on an Operational State instance and a Microwave Oven Mode instance.
55+
* Caller must ensure those 2 instances are live and initialized before initializing MicorwaveOvenControl instance.
56+
*/
57+
Instance(Delegate * aDelegate, EndpointId aEndpointId, ClusterId aClusterId, BitMask<MicrowaveOvenControl::Feature> aFeature,
58+
Clusters::OperationalState::Instance & aOpStateInstance, Clusters::ModeBase::Instance & aMicrowaveOvenModeInstance);
59+
60+
~Instance() override;
61+
62+
/**
63+
* @brief Initialise the Microwave Oven Control server instance.
64+
* This function must be called after defining an Instance class object.
65+
* @return Returns an error if the given endpoint and cluster ID have not been enabled in zap or if the
66+
* CommandHandler or AttributeHandler registration fails, else returns CHIP_NO_ERROR.
67+
* This method also checks if the feature setting is valid, if invalid it will returns CHIP_ERROR_INVALID_ARGUMENT.
68+
*/
69+
CHIP_ERROR Init();
70+
71+
bool HasFeature(MicrowaveOvenControl::Feature feature) const;
72+
73+
uint8_t GetCountOfSupportedWattLevels() const;
74+
75+
uint32_t GetCookTimeSec() const;
76+
77+
void SetCookTimeSec(uint32_t cookTimeSec);
78+
79+
private:
80+
Delegate * mDelegate;
81+
EndpointId mEndpointId;
82+
ClusterId mClusterId;
83+
BitMask<MicrowaveOvenControl::Feature> mFeature;
84+
Clusters::OperationalState::Instance & mOpStateInstance;
85+
Clusters::ModeBase::Instance & mMicrowaveOvenModeInstance;
86+
87+
uint32_t mCookTimeSec = kDefaultCookTimeSec;
88+
uint8_t mSupportedWattLevels = 0;
89+
90+
/**
91+
* IM-level implementation of read
92+
* @return appropriately mapped CHIP_ERROR if applicable
93+
*/
94+
CHIP_ERROR Read(const ConcreteReadAttributePath & aPath, AttributeValueEncoder & aEncoder) override;
95+
96+
/**
97+
* @brief Inherited from CommandHandlerInterface
98+
*/
99+
void InvokeCommand(HandlerContext & ctx) override;
100+
101+
/**
102+
* @brief Handle Command: SetCookingParameters.
103+
* @param ctx Returns the Interaction Model status code which was user determined in the business logic.
104+
* If the input value is invalid, returns the Interaction Model status code of INVALID_COMMAND.
105+
* If the operational state is not in 'Stopped', returns the Interaction Model status code of INVALID_IN_STATE.
106+
*/
107+
void HandleSetCookingParameters(HandlerContext & ctx, const Commands::SetCookingParameters::DecodableType & req);
108+
109+
/**
110+
* @brief Handle Command: AddMoreTime.
111+
* @param ctx Returns the Interaction Model status code which was user determined in the business logic.
112+
* If the cook time value is out of range, returns the Interaction Model status code of CONSTRAINT_ERROR.
113+
* If the operational state is in 'Error', returns the Interaction Model status code of INVALID_IN_STATE.
114+
*/
115+
void HandleAddMoreTime(HandlerContext & ctx, const Commands::AddMoreTime::DecodableType & req);
116+
};
117+
118+
/**
119+
* @brief Check if the given cook time is in range.
120+
*/
121+
bool IsCookTimeSecondsInRange(uint32_t cookTimeSec, uint32_t maxCookTimeSec);
122+
123+
/**
124+
* @brief Check if the given cooking power is in range.
125+
*/
126+
bool IsPowerSettingNumberInRange(uint8_t powerSettingNum, uint8_t minCookPowerNum, uint8_t maxCookPowerNum);
127+
128+
} // namespace MicrowaveOvenControl
129+
} // namespace Clusters
130+
} // namespace app
131+
} // namespace chip
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
*
3+
* Copyright (c) 2023-2026 Project CHIP Authors
4+
* All rights reserved.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
#pragma once
20+
21+
#include <app-common/zap-generated/cluster-objects.h>
22+
#include <protocols/interaction_model/StatusCode.h>
23+
24+
namespace chip {
25+
namespace app {
26+
namespace Clusters {
27+
namespace MicrowaveOvenControl {
28+
29+
class Instance;
30+
31+
/** @brief
32+
* Defines methods for implementing application-specific logic for the MicrowaveOvenControl Cluster.
33+
*/
34+
class Delegate
35+
{
36+
public:
37+
Delegate() = default;
38+
39+
virtual ~Delegate() = default;
40+
41+
/**
42+
* @brief Handle Command Callback in application: SetCookingParameters.
43+
* @return Returns the Interaction Model status code which was user determined in the business logic.
44+
* @param cookMode: the input cook mode value. Callee needs to define the cooking mode value in the Microwave Oven Mode
45+
* instance.
46+
* @param cookTimeSec: the input cook time value.
47+
* @param startAfterSetting: if true, the cooking operation will start after handling the command.
48+
* @param powerSettingNum: the input power setting value.
49+
* @param wattSettingIndex: the input watts setting index.
50+
* Note: powerSettingNum and wattSettingIndex must be mutually exclusive.
51+
* If using power as number, wattSettingIndex will be set to NullOptional.
52+
* If using power in watts, powerSettingNum will be set to NullOptional.
53+
*/
54+
virtual Protocols::InteractionModel::Status HandleSetCookingParametersCallback(uint8_t cookMode, uint32_t cookTimeSec,
55+
bool startAfterSetting,
56+
Optional<uint8_t> powerSettingNum,
57+
Optional<uint8_t> wattSettingIndex) = 0;
58+
59+
/**
60+
* @brief Handle Command Callback in application: AddMoreTime.
61+
* @return Returns the Interaction Model status code which was user determined in the business logic.
62+
* @param finalCookTimeSec: the cook time value after adding input time.
63+
*/
64+
virtual Protocols::InteractionModel::Status HandleModifyCookTimeSecondsCallback(uint32_t finalCookTimeSec) = 0;
65+
66+
/**
67+
* Get the watt setting from the supported watts list.
68+
* @param index The index of the watt setting to be returned. It is assumed that watt setting are indexable from 0 and with no
69+
* gaps.
70+
* @param wattSetting A reference to receive the watt setting on success.
71+
* @return Returns a CHIP_NO_ERROR if there was no error and the label was returned successfully,
72+
* CHIP_ERROR_NOT_FOUND if the index in beyond the list of available labels.
73+
*/
74+
virtual CHIP_ERROR GetWattSettingByIndex(uint8_t index, uint16_t & wattSetting) = 0;
75+
76+
virtual uint32_t GetMaxCookTimeSec() const = 0;
77+
78+
virtual uint8_t GetPowerSettingNum() const = 0;
79+
80+
virtual uint8_t GetMinPowerNum() const = 0;
81+
82+
virtual uint8_t GetMaxPowerNum() const = 0;
83+
84+
virtual uint8_t GetPowerStepNum() const = 0;
85+
86+
virtual uint8_t GetCurrentWattIndex() const = 0;
87+
88+
virtual uint16_t GetWattRating() const = 0;
89+
90+
/**
91+
* This method is used by the SDK to set the instance pointer. This is done during the instantiation of a Instance object.
92+
* @param aInstance A pointer to the Instance object related to this delegate object.
93+
* @note This method is for internal SDK use and should only be called by the `Instance` constructor and destructor.
94+
*/
95+
void SetInstance(Instance * aInstance)
96+
{
97+
VerifyOrDie(mInstance == nullptr || aInstance == nullptr || mInstance == aInstance);
98+
mInstance = aInstance;
99+
}
100+
101+
private:
102+
Instance * mInstance = nullptr;
103+
104+
protected:
105+
/**
106+
* @brief Provides access to the const Instance pointer.
107+
* This method is placed in the protected section because it must be called by classes derived from the delegate.
108+
*
109+
* @return A const pointer to the Instance object associated with this delegate.
110+
*/
111+
const Instance * GetInstance() const { return mInstance; }
112+
113+
/**
114+
* @brief Provides access to the Instance pointer.
115+
* This method is placed in the protected section because it must be called by classes derived from the delegate.
116+
*
117+
* @return A pointer to the Instance object associated with this delegate.
118+
*/
119+
Instance * GetInstance() { return mInstance; }
120+
};
121+
122+
} // namespace MicrowaveOvenControl
123+
} // namespace Clusters
124+
} // namespace app
125+
} // namespace chip

src/app/clusters/microwave-oven-control-server/microwave-oven-control-server.cpp renamed to src/app/clusters/microwave-oven-control-server/MicrowaveOvenControlCluster.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
*
3-
* Copyright (c) 2023 Project CHIP Authors
3+
* Copyright (c) 2023-2026 Project CHIP Authors
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.

src/app/clusters/microwave-oven-control-server/app_config_dependent_sources.cmake

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2025 Project CHIP Authors
1+
# Copyright (c) 2025-2026 Project CHIP Authors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
1616
TARGET_SOURCES(
1717
${APP_TARGET}
1818
PRIVATE
19-
"${CLUSTER_DIR}/microwave-oven-control-server.cpp"
19+
"${CLUSTER_DIR}/MicrowaveOvenControlCluster.cpp"
20+
"${CLUSTER_DIR}/CodegenIntegration.h"
2021
"${CLUSTER_DIR}/microwave-oven-control-server.h"
22+
"${CLUSTER_DIR}/Delegate.h"
2123
)

src/app/clusters/microwave-oven-control-server/app_config_dependent_sources.gni

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2025 Project CHIP Authors
1+
# Copyright (c) 2025-2026 Project CHIP Authors
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,6 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
app_config_dependent_sources = [
15-
"microwave-oven-control-server.cpp",
15+
"CodegenIntegration.h",
16+
"Delegate.h",
17+
"MicrowaveOvenControlCluster.cpp",
1618
"microwave-oven-control-server.h",
1719
]

0 commit comments

Comments
 (0)