b08b0bc9b450772979c706ec1abb36e6e33fb543
[platform/upstream/connectedhomeip.git] / examples / bridge-app / bridge-common / gen / IMClusterCommandHandler.cpp
1 /*
2  *
3  *    Copyright (c) 2021 Project CHIP Authors
4  *
5  *    Licensed under the Apache License, Version 2.0 (the "License");
6  *    you may not use this file except in compliance with the License.
7  *    You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *    Unless required by applicable law or agreed to in writing, software
12  *    distributed under the License is distributed on an "AS IS" BASIS,
13  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *    See the License for the specific language governing permissions and
15  *    limitations under the License.
16  */
17
18 // THIS FILE IS GENERATED BY ZAP
19
20 #include <cinttypes>
21 #include <cstdint>
22
23 #include "af-structs.h"
24 #include "call-command-handler.h"
25 #include "callback.h"
26 #include "cluster-id.h"
27 #include "command-id.h"
28 #include "util.h"
29
30 #include <app/InteractionModelEngine.h>
31
32 // Currently we need some work to keep compatible with ember lib.
33 #include <util/ember-compatibility-functions.h>
34
35 namespace chip {
36 namespace app {
37
38 // Cluster specific command parsing
39
40 namespace clusters {
41
42 namespace Basic {
43
44 void DispatchServerCommand(app::Command * command, CommandId commandId, EndpointId endpointId, TLV::TLVReader & dataTlv)
45 {
46     {
47         switch (commandId)
48         {
49         case ZCL_RESET_TO_FACTORY_DEFAULTS_COMMAND_ID: {
50             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
51             emberAfBasicClusterResetToFactoryDefaultsCallback();
52             break;
53         }
54         default: {
55             // Unrecognized command ID, error status will apply.
56             // TODO: Encode response for command not found
57             ChipLogError(Zcl, "Unknown command %" PRIx16 " for cluster %" PRIx16, commandId, ZCL_BASIC_CLUSTER_ID);
58             break;
59         }
60         }
61     }
62 }
63
64 } // namespace Basic
65
66 namespace LevelControl {
67
68 void DispatchServerCommand(app::Command * command, CommandId commandId, EndpointId endpointId, TLV::TLVReader & dataTlv)
69 {
70     {
71         switch (commandId)
72         {
73         case ZCL_MOVE_COMMAND_ID: {
74             CHIP_ERROR TLVError = CHIP_NO_ERROR;
75             uint8_t moveMode;
76             uint8_t rate;
77             uint8_t optionMask;
78             uint8_t optionOverride;
79
80             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
81             {
82                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
83                 {
84                 case 0:
85                     TLVError = dataTlv.Get(moveMode);
86                     break;
87                 case 1:
88                     TLVError = dataTlv.Get(rate);
89                     break;
90                 case 2:
91                     TLVError = dataTlv.Get(optionMask);
92                     break;
93                 case 3:
94                     TLVError = dataTlv.Get(optionOverride);
95                     break;
96                 default:
97                     // Unsupported tag, ignore it.
98                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
99                     break;
100                 }
101                 if (TLVError != CHIP_NO_ERROR)
102                 {
103                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
104                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
105                 }
106             }
107             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
108             emberAfLevelControlClusterMoveCallback(moveMode, rate, optionMask, optionOverride);
109             break;
110         }
111         case ZCL_MOVE_TO_LEVEL_COMMAND_ID: {
112             CHIP_ERROR TLVError = CHIP_NO_ERROR;
113             uint8_t level;
114             uint16_t transitionTime;
115             uint8_t optionMask;
116             uint8_t optionOverride;
117
118             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
119             {
120                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
121                 {
122                 case 0:
123                     TLVError = dataTlv.Get(level);
124                     break;
125                 case 1:
126                     TLVError = dataTlv.Get(transitionTime);
127                     break;
128                 case 2:
129                     TLVError = dataTlv.Get(optionMask);
130                     break;
131                 case 3:
132                     TLVError = dataTlv.Get(optionOverride);
133                     break;
134                 default:
135                     // Unsupported tag, ignore it.
136                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
137                     break;
138                 }
139                 if (TLVError != CHIP_NO_ERROR)
140                 {
141                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
142                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
143                 }
144             }
145             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
146             emberAfLevelControlClusterMoveToLevelCallback(level, transitionTime, optionMask, optionOverride);
147             break;
148         }
149         case ZCL_MOVE_TO_LEVEL_WITH_ON_OFF_COMMAND_ID: {
150             CHIP_ERROR TLVError = CHIP_NO_ERROR;
151             uint8_t level;
152             uint16_t transitionTime;
153
154             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
155             {
156                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
157                 {
158                 case 0:
159                     TLVError = dataTlv.Get(level);
160                     break;
161                 case 1:
162                     TLVError = dataTlv.Get(transitionTime);
163                     break;
164                 default:
165                     // Unsupported tag, ignore it.
166                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
167                     break;
168                 }
169                 if (TLVError != CHIP_NO_ERROR)
170                 {
171                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
172                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
173                 }
174             }
175             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
176             emberAfLevelControlClusterMoveToLevelWithOnOffCallback(level, transitionTime);
177             break;
178         }
179         case ZCL_MOVE_WITH_ON_OFF_COMMAND_ID: {
180             CHIP_ERROR TLVError = CHIP_NO_ERROR;
181             uint8_t moveMode;
182             uint8_t rate;
183
184             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
185             {
186                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
187                 {
188                 case 0:
189                     TLVError = dataTlv.Get(moveMode);
190                     break;
191                 case 1:
192                     TLVError = dataTlv.Get(rate);
193                     break;
194                 default:
195                     // Unsupported tag, ignore it.
196                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
197                     break;
198                 }
199                 if (TLVError != CHIP_NO_ERROR)
200                 {
201                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
202                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
203                 }
204             }
205             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
206             emberAfLevelControlClusterMoveWithOnOffCallback(moveMode, rate);
207             break;
208         }
209         case ZCL_STEP_COMMAND_ID: {
210             CHIP_ERROR TLVError = CHIP_NO_ERROR;
211             uint8_t stepMode;
212             uint8_t stepSize;
213             uint16_t transitionTime;
214             uint8_t optionMask;
215             uint8_t optionOverride;
216
217             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
218             {
219                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
220                 {
221                 case 0:
222                     TLVError = dataTlv.Get(stepMode);
223                     break;
224                 case 1:
225                     TLVError = dataTlv.Get(stepSize);
226                     break;
227                 case 2:
228                     TLVError = dataTlv.Get(transitionTime);
229                     break;
230                 case 3:
231                     TLVError = dataTlv.Get(optionMask);
232                     break;
233                 case 4:
234                     TLVError = dataTlv.Get(optionOverride);
235                     break;
236                 default:
237                     // Unsupported tag, ignore it.
238                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
239                     break;
240                 }
241                 if (TLVError != CHIP_NO_ERROR)
242                 {
243                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
244                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
245                 }
246             }
247             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
248             emberAfLevelControlClusterStepCallback(stepMode, stepSize, transitionTime, optionMask, optionOverride);
249             break;
250         }
251         case ZCL_STEP_WITH_ON_OFF_COMMAND_ID: {
252             CHIP_ERROR TLVError = CHIP_NO_ERROR;
253             uint8_t stepMode;
254             uint8_t stepSize;
255             uint16_t transitionTime;
256
257             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
258             {
259                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
260                 {
261                 case 0:
262                     TLVError = dataTlv.Get(stepMode);
263                     break;
264                 case 1:
265                     TLVError = dataTlv.Get(stepSize);
266                     break;
267                 case 2:
268                     TLVError = dataTlv.Get(transitionTime);
269                     break;
270                 default:
271                     // Unsupported tag, ignore it.
272                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
273                     break;
274                 }
275                 if (TLVError != CHIP_NO_ERROR)
276                 {
277                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
278                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
279                 }
280             }
281             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
282             emberAfLevelControlClusterStepWithOnOffCallback(stepMode, stepSize, transitionTime);
283             break;
284         }
285         case ZCL_STOP_COMMAND_ID: {
286             CHIP_ERROR TLVError = CHIP_NO_ERROR;
287             uint8_t optionMask;
288             uint8_t optionOverride;
289
290             while ((TLVError = dataTlv.Next()) == CHIP_NO_ERROR)
291             {
292                 switch (TLV::TagNumFromTag(dataTlv.GetTag()))
293                 {
294                 case 0:
295                     TLVError = dataTlv.Get(optionMask);
296                     break;
297                 case 1:
298                     TLVError = dataTlv.Get(optionOverride);
299                     break;
300                 default:
301                     // Unsupported tag, ignore it.
302                     ChipLogProgress(Zcl, "Unknown TLV tag during processing.");
303                     break;
304                 }
305                 if (TLVError != CHIP_NO_ERROR)
306                 {
307                     ChipLogProgress(Zcl, "Failed to decode TLV data with tag %" PRIx32 ": %" PRId32,
308                                     TLV::TagNumFromTag(dataTlv.GetTag()), TLVError);
309                 }
310             }
311             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
312             emberAfLevelControlClusterStopCallback(optionMask, optionOverride);
313             break;
314         }
315         case ZCL_STOP_WITH_ON_OFF_COMMAND_ID: {
316             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
317             emberAfLevelControlClusterStopWithOnOffCallback();
318             break;
319         }
320         default: {
321             // Unrecognized command ID, error status will apply.
322             // TODO: Encode response for command not found
323             ChipLogError(Zcl, "Unknown command %" PRIx16 " for cluster %" PRIx16, commandId, ZCL_LEVEL_CONTROL_CLUSTER_ID);
324             break;
325         }
326         }
327     }
328 }
329
330 } // namespace LevelControl
331
332 namespace OnOff {
333
334 void DispatchServerCommand(app::Command * command, CommandId commandId, EndpointId endpointId, TLV::TLVReader & dataTlv)
335 {
336     {
337         switch (commandId)
338         {
339         case ZCL_OFF_COMMAND_ID: {
340             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
341             emberAfOnOffClusterOffCallback();
342             break;
343         }
344         case ZCL_ON_COMMAND_ID: {
345             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
346             emberAfOnOffClusterOnCallback();
347             break;
348         }
349         case ZCL_TOGGLE_COMMAND_ID: {
350             // TODO(#5098) We should pass the Command Object and EndpointId to the cluster callbacks.
351             emberAfOnOffClusterToggleCallback();
352             break;
353         }
354         default: {
355             // Unrecognized command ID, error status will apply.
356             // TODO: Encode response for command not found
357             ChipLogError(Zcl, "Unknown command %" PRIx16 " for cluster %" PRIx16, commandId, ZCL_ON_OFF_CLUSTER_ID);
358             break;
359         }
360         }
361     }
362 }
363
364 } // namespace OnOff
365
366 } // namespace clusters
367
368 void DispatchSingleClusterCommand(chip::ClusterId aClusterId, chip::CommandId aCommandId, chip::EndpointId aEndPointId,
369                                   chip::TLV::TLVReader & aReader, Command * apCommandObj)
370 {
371     ChipLogDetail(Zcl, "Received Cluster Command: Cluster=%" PRIx16 " Command=%" PRIx8 " Endpoint=%" PRIx8, aClusterId, aCommandId,
372                   aEndPointId);
373     Compatibility::SetupEmberAfObjects(apCommandObj, aClusterId, aCommandId, aEndPointId);
374     switch (aClusterId)
375     {
376     case ZCL_BASIC_CLUSTER_ID:
377         clusters::Basic::DispatchServerCommand(apCommandObj, aCommandId, aEndPointId, aReader);
378         break;
379     case ZCL_LEVEL_CONTROL_CLUSTER_ID:
380         clusters::LevelControl::DispatchServerCommand(apCommandObj, aCommandId, aEndPointId, aReader);
381         break;
382     case ZCL_ON_OFF_CLUSTER_ID:
383         clusters::OnOff::DispatchServerCommand(apCommandObj, aCommandId, aEndPointId, aReader);
384         break;
385     default:
386         // Unrecognized cluster ID, error status will apply.
387         // TODO: Encode response for Cluster not found
388         ChipLogError(Zcl, "Unknown cluster %" PRIx16, aClusterId);
389         break;
390     }
391     Compatibility::ResetEmberAfObjects();
392 }
393
394 } // namespace app
395 } // namespace chip