Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / clusters / scenes-client / scenes-client.cpp
1 /**
2  *
3  *    Copyright (c) 2020 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 /**
19  *
20  *    Copyright (c) 2020 Silicon Labs
21  *
22  *    Licensed under the Apache License, Version 2.0 (the "License");
23  *    you may not use this file except in compliance with the License.
24  *    You may obtain a copy of the License at
25  *
26  *        http://www.apache.org/licenses/LICENSE-2.0
27  *
28  *    Unless required by applicable law or agreed to in writing, software
29  *    distributed under the License is distributed on an "AS IS" BASIS,
30  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  *    See the License for the specific language governing permissions and
32  *    limitations under the License.
33  */
34 /****************************************************************************
35  * @file
36  * @brief Routines for the Scenes Client plugin, which
37  *implements the client side of the Scenes cluster.
38  *******************************************************************************
39  ******************************************************************************/
40
41 #include "scenes-client.h"
42 #include <app/util/af.h>
43
44 #include "gen/command-id.h"
45
46 using namespace chip;
47
48 bool emberAfScenesClusterAddSceneResponseCallback(uint8_t status, GroupId groupId, uint8_t sceneId)
49 {
50     return emberAfPluginScenesClientParseAddSceneResponse(emberAfCurrentCommand(), status, groupId, sceneId);
51 }
52
53 bool emberAfScenesClusterViewSceneResponseCallback(uint8_t status, GroupId groupId, uint8_t sceneId, uint16_t transitionTime,
54                                                    uint8_t * sceneName, uint8_t * extensionFieldSets)
55 {
56     return emberAfPluginScenesClientParseViewSceneResponse(emberAfCurrentCommand(), status, groupId, sceneId, transitionTime,
57                                                            sceneName, extensionFieldSets);
58 }
59
60 bool emberAfScenesClusterRemoveSceneResponseCallback(uint8_t status, GroupId groupId, uint8_t sceneId)
61 {
62     emberAfScenesClusterPrintln("RX: RemoveSceneResponse 0x%x, 0x%2x, 0x%x", status, groupId, sceneId);
63     emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
64     return true;
65 }
66
67 bool emberAfScenesClusterRemoveAllScenesResponseCallback(uint8_t status, GroupId groupId)
68 {
69     emberAfScenesClusterPrintln("RX: RemoveAllScenesResponse 0x%x, 0x%2x", status, groupId);
70     emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
71     return true;
72 }
73
74 bool emberAfScenesClusterStoreSceneResponseCallback(uint8_t status, GroupId groupId, uint8_t sceneId)
75 {
76     emberAfScenesClusterPrintln("RX: StoreSceneResponse 0x%x, 0x%2x, 0x%x", status, groupId, sceneId);
77     emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
78     return true;
79 }
80
81 bool emberAfScenesClusterGetSceneMembershipResponseCallback(uint8_t status, uint8_t capacity, GroupId groupId, uint8_t sceneCount,
82                                                             uint8_t * sceneList)
83 {
84     emberAfScenesClusterPrint("RX: GetSceneMembershipResponse 0x%x, 0x%x, 0x%2x", status, capacity, groupId);
85
86     // Scene count and the scene list only appear in the payload if the status is
87     // SUCCESS.
88     if (status == EMBER_ZCL_STATUS_SUCCESS)
89     {
90         uint8_t i;
91         emberAfScenesClusterPrint(", 0x%x,", sceneCount);
92         for (i = 0; i < sceneCount; i++)
93         {
94             emberAfScenesClusterPrint(" [0x%x]", sceneList[i]);
95         }
96     }
97
98     emberAfScenesClusterPrintln("");
99     emberAfSendImmediateDefaultResponse(EMBER_ZCL_STATUS_SUCCESS);
100     return true;
101 }
102
103 bool emberAfPluginScenesClientParseAddSceneResponse(const EmberAfClusterCommand * cmd, uint8_t status, GroupId groupId,
104                                                     uint8_t sceneId)
105 {
106     bool enhanced = (cmd->commandId == ZCL_ENHANCED_ADD_SCENE_COMMAND_ID);
107     emberAfScenesClusterPrintln("RX: %pAddSceneResponse 0x%x, 0x%2x, 0x%x", (enhanced ? "Enhanced" : ""), status, groupId, sceneId);
108     emberAfSendDefaultResponse(cmd, EMBER_ZCL_STATUS_SUCCESS);
109     return true;
110 }
111
112 bool emberAfPluginScenesClientParseViewSceneResponse(const EmberAfClusterCommand * cmd, uint8_t status, GroupId groupId,
113                                                      uint8_t sceneId, uint16_t transitionTime, const uint8_t * sceneName,
114                                                      const uint8_t * extensionFieldSets)
115 {
116     bool enhanced = (cmd->commandId == ZCL_ENHANCED_VIEW_SCENE_COMMAND_ID);
117
118     emberAfScenesClusterPrint("RX: %pViewSceneResponse 0x%x, 0x%2x, 0x%x", (enhanced ? "Enhanced" : ""), status, groupId, sceneId);
119
120     // Transition time, scene name, and the extension field sets only appear in
121     // the payload if the status is SUCCESS.
122     if (status == EMBER_ZCL_STATUS_SUCCESS)
123     {
124         uint16_t extensionFieldSetsLen =
125             static_cast<uint16_t>(emberAfCurrentCommand()->bufLen -
126                                   (emberAfCurrentCommand()->payloadStartIndex + sizeof(status) + sizeof(groupId) + sizeof(sceneId) +
127                                    sizeof(transitionTime) + emberAfStringLength(sceneName) + 1));
128         uint16_t extensionFieldSetsIndex = 0;
129
130         emberAfScenesClusterPrint(", 0x%2x, \"", transitionTime);
131         emberAfScenesClusterPrintString(sceneName);
132         emberAfScenesClusterPrint("\",");
133
134         // Each extension field set contains at least a two-byte cluster id and a
135         // one-byte length.
136         while (extensionFieldSetsIndex + 3 <= extensionFieldSetsLen)
137         {
138             ClusterId clusterId;
139             uint8_t length;
140             clusterId               = emberAfGetInt16u(extensionFieldSets, extensionFieldSetsIndex, extensionFieldSetsLen);
141             extensionFieldSetsIndex = static_cast<uint16_t>(extensionFieldSetsIndex + 2);
142             length                  = emberAfGetInt8u(extensionFieldSets, extensionFieldSetsIndex, extensionFieldSetsLen);
143             extensionFieldSetsIndex++;
144             emberAfScenesClusterPrint(" [0x%2x 0x%x ", clusterId, length);
145             if (extensionFieldSetsIndex + length <= extensionFieldSetsLen)
146             {
147                 emberAfScenesClusterPrintBuffer(extensionFieldSets + extensionFieldSetsIndex, length, false);
148             }
149             emberAfScenesClusterPrint("]");
150             emberAfScenesClusterFlush();
151             extensionFieldSetsIndex = static_cast<uint16_t>(extensionFieldSetsIndex + length);
152         }
153     }
154
155     emberAfScenesClusterPrintln("");
156     emberAfSendDefaultResponse(cmd, EMBER_ZCL_STATUS_SUCCESS);
157     return true;
158 }