/src/connectedhomeip/examples/all-clusters-app/all-clusters-common/include/operational-state-delegate-impl.h
Line | Count | Source |
1 | | /* |
2 | | * |
3 | | * Copyright (c) 2023 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/clusters/operational-state-server/operational-state-server.h> |
23 | | |
24 | | #include <protocols/interaction_model/StatusCode.h> |
25 | | |
26 | | namespace chip { |
27 | | namespace app { |
28 | | namespace Clusters { |
29 | | |
30 | | namespace OperationalState { |
31 | | |
32 | | // This is an application level delegate to handle operational state commands according to the specific business logic. |
33 | | class GenericOperationalStateDelegateImpl : public Delegate |
34 | | { |
35 | | public: |
36 | | uint32_t mRunningTime = 0; |
37 | | uint32_t mPausedTime = 0; |
38 | | app::DataModel::Nullable<uint32_t> mCountDownTime; |
39 | | |
40 | | /** |
41 | | * Get the countdown time. This attribute is not used in this application. |
42 | | * @return The current countdown time. |
43 | | */ |
44 | | app::DataModel::Nullable<uint32_t> GetCountdownTime() override; |
45 | | |
46 | | /** |
47 | | * Fills in the provided GenericOperationalState with the state at index `index` if there is one, |
48 | | * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of states. |
49 | | * Note: This is used by the SDK to populate the operational state list attribute. If the contents of this list changes, |
50 | | * the device SHALL call the Instance's ReportOperationalStateListChange method to report that this attribute has changed. |
51 | | * @param index The index of the state, with 0 representing the first state. |
52 | | * @param operationalState The GenericOperationalState is filled. |
53 | | */ |
54 | | CHIP_ERROR GetOperationalStateAtIndex(size_t index, GenericOperationalState & operationalState) override; |
55 | | |
56 | | /** |
57 | | * Fills in the provided MutableCharSpan with the phase at index `index` if there is one, |
58 | | * or returns CHIP_ERROR_NOT_FOUND if the index is out of range for the list of phases. |
59 | | * |
60 | | * If CHIP_ERROR_NOT_FOUND is returned for index 0, that indicates that the PhaseList attribute is null |
61 | | * (there are no phases defined at all). |
62 | | * |
63 | | * Note: This is used by the SDK to populate the phase list attribute. If the contents of this list changes, the |
64 | | * device SHALL call the Instance's ReportPhaseListChange method to report that this attribute has changed. |
65 | | * @param index The index of the phase, with 0 representing the first phase. |
66 | | * @param operationalPhase The MutableCharSpan is filled. |
67 | | */ |
68 | | CHIP_ERROR GetOperationalPhaseAtIndex(size_t index, MutableCharSpan & operationalPhase) override; |
69 | | |
70 | | // command callback |
71 | | /** |
72 | | * Handle Command Callback in application: Pause |
73 | | * @param[out] get operational error after callback. |
74 | | */ |
75 | | void HandlePauseStateCallback(GenericOperationalError & err) override; |
76 | | |
77 | | /** |
78 | | * Handle Command Callback in application: Resume |
79 | | * @param[out] get operational error after callback. |
80 | | */ |
81 | | void HandleResumeStateCallback(GenericOperationalError & err) override; |
82 | | |
83 | | /** |
84 | | * Handle Command Callback in application: Start |
85 | | * @param[out] get operational error after callback. |
86 | | */ |
87 | | void HandleStartStateCallback(GenericOperationalError & err) override; |
88 | | |
89 | | /** |
90 | | * Handle Command Callback in application: Stop |
91 | | * @param[out] get operational error after callback. |
92 | | */ |
93 | | void HandleStopStateCallback(GenericOperationalError & err) override; |
94 | | |
95 | | protected: |
96 | | /// Sets the operational-state ID list from any contiguous array of single-byte state values |
97 | | /// (e.g. an `OperationalStateEnum[]`), so callers don't have to cast each entry to uint8_t. |
98 | | /// The IDs are stored as bytes; the spec only attaches a label to manufacturer-specific states, |
99 | | /// and that is supplied separately when the state is reported. |
100 | | template <typename T> |
101 | | void SetOperationalStateList(Span<const T> states) |
102 | 0 | { |
103 | 0 | static_assert(sizeof(T) == 1, "Operational state IDs must be a single byte (uint8_t-sized)."); |
104 | 0 | mOperationalStateIds = Span<const uint8_t>(reinterpret_cast<const uint8_t *>(states.data()), states.size()); |
105 | 0 | } |
106 | | |
107 | | Span<const uint8_t> mOperationalStateIds; |
108 | | Span<const CharSpan> mOperationalPhaseList; |
109 | | }; |
110 | | |
111 | | // This is an application level delegate to handle operational state commands according to the specific business logic. |
112 | | class OperationalStateDelegate : public GenericOperationalStateDelegateImpl |
113 | | { |
114 | | private: |
115 | | static constexpr OperationalStateEnum kOpStateIds[] = { |
116 | | OperationalStateEnum::kStopped, |
117 | | OperationalStateEnum::kRunning, |
118 | | OperationalStateEnum::kPaused, |
119 | | OperationalStateEnum::kError, |
120 | | }; |
121 | | |
122 | | const uint32_t kExampleCountDown = 30; |
123 | | |
124 | | public: |
125 | 0 | OperationalStateDelegate() { SetOperationalStateList(Span<const OperationalStateEnum>(kOpStateIds)); } |
126 | | |
127 | | /** |
128 | | * Handle Command Callback in application: Start |
129 | | * @param[out] get operational error after callback. |
130 | | */ |
131 | | void HandleStartStateCallback(GenericOperationalError & err) override |
132 | 0 | { |
133 | 0 | mCountDownTime.SetNonNull(static_cast<uint32_t>(kExampleCountDown)); |
134 | 0 | GenericOperationalStateDelegateImpl::HandleStartStateCallback(err); |
135 | 0 | } |
136 | | |
137 | | /** |
138 | | * Handle Command Callback in application: Stop |
139 | | * @param[out] get operational error after callback. |
140 | | */ |
141 | | void HandleStopStateCallback(GenericOperationalError & err) override |
142 | 0 | { |
143 | 0 | GenericOperationalStateDelegateImpl::HandleStopStateCallback(err); |
144 | 0 | mCountDownTime.SetNull(); |
145 | 0 | } |
146 | | }; |
147 | | |
148 | | Instance * GetOperationalStateInstance(); |
149 | | OperationalStateDelegate * GetOperationalStateDelegate(); |
150 | | |
151 | | void Shutdown(); |
152 | | |
153 | | } // namespace OperationalState |
154 | | } // namespace Clusters |
155 | | } // namespace app |
156 | | } // namespace chip |