Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / app / clusters / zll-identify-server / zll-identify-server.c
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 /**
36  * @file
37  * @brief Routines for the ZLL Identify Server plugin,
38  * which contains additions to the Identify server
39  * cluster.
40  *******************************************************************************
41  * # License
42  * <b>Copyright 2018 Silicon Laboratories Inc. www.silabs.com</b>
43  *******************************************************************************
44  *
45  * The licensor of this software is Silicon
46  * Laboratories Inc. Your use of this software is
47  * governed by the terms of Silicon Labs Master
48  * Software License Agreement (MSLA) available at
49  * www.silabs.com/about-us/legal/master-software-license-agreement.
50  * This software is distributed to you in Source Code
51  * format and is governed by the sections of the MSLA
52  * applicable to Source Code.
53  *
54  ******************************************************************************/
55
56 // this file contains all the common includes for clusters in the util
57 #include "app/framework/include/af.h"
58 #include "app/framework/util/common.h"
59
60 #ifndef EZSP_HOST
61 #include "hal/hal.h"
62 #endif
63
64 typedef struct
65 {
66     bool active;
67     bool cancel;
68     EmberAfIdentifyEffectIdentifier effectId;
69     EmberAfIdentifyEffectVariant commandVariant;
70     uint8_t eventsRemaining;
71     uint16_t eventDelay;
72 } EmAfZllIdentifyState;
73
74 void emAfPluginZllIdentifyServerBlinkEffect(uint8_t endpoint);
75
76 void emAfPluginZllIdentifyServerBreatheEffect(uint8_t endpoint);
77
78 void emAfPluginZllIdentifyServerOkayEffect(uint8_t endpoint);
79
80 void emAfPluginZllIdentifyServerChannelChangeEffect(uint8_t endpoint);
81
82 extern EmberEventControl emberAfPluginZllIdentifyServerTriggerEffectEndpointEventControls[];
83
84 static EmAfZllIdentifyState stateTable[EMBER_AF_IDENTIFY_CLUSTER_SERVER_ENDPOINT_COUNT];
85
86 static EmAfZllIdentifyState * getZllIdentifyState(uint8_t endpoint);
87
88 static void deactivateZllIdentify(EmAfZllIdentifyState * state, uint8_t endpoint);
89
90 static EmAfZllIdentifyState * getZllIdentifyState(uint8_t endpoint)
91 {
92     uint8_t index = emberAfFindClusterServerEndpointIndex(endpoint, ZCL_IDENTIFY_CLUSTER_ID);
93     return (index == 0xFF ? NULL : &stateTable[index]);
94 }
95
96 static void deactivateZllIdentify(EmAfZllIdentifyState * state, uint8_t endpoint)
97 {
98     if (state == NULL)
99     {
100         return;
101     }
102
103     state->active = false;
104     state->cancel = false;
105
106     emberAfEndpointEventControlSetInactive(emberAfPluginZllIdentifyServerTriggerEffectEndpointEventControls, endpoint);
107 }
108
109 void emberAfPluginZllIdentifyServerTriggerEffectEndpointEventHandler(uint8_t endpoint)
110 {
111     EmAfZllIdentifyState * state = getZllIdentifyState(endpoint);
112
113     if (state == NULL)
114     {
115         return;
116     }
117
118     switch (state->effectId)
119     {
120     case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK:
121         emAfPluginZllIdentifyServerBlinkEffect(endpoint);
122         break;
123     case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE:
124         emAfPluginZllIdentifyServerBreatheEffect(endpoint);
125         break;
126     case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY:
127         emAfPluginZllIdentifyServerOkayEffect(endpoint);
128         break;
129     case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE:
130         emAfPluginZllIdentifyServerChannelChangeEffect(endpoint);
131         break;
132     case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_FINISH_EFFECT: // At this point, these are functionally equivalent
133     case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_STOP_EFFECT:
134     default:
135         deactivateZllIdentify(state, endpoint);
136         return;
137     }
138     if (state->cancel)
139     {
140         deactivateZllIdentify(state, endpoint);
141         return;
142     }
143
144     if (state->active)
145     {
146         emberAfEndpointEventControlSetDelayMS(emberAfPluginZllIdentifyServerTriggerEffectEndpointEventControls, endpoint,
147                                               state->eventDelay);
148     }
149 }
150
151 bool emberAfIdentifyClusterTriggerEffectCallback(uint8_t effectId, uint8_t effectVariant)
152 {
153     uint8_t endpoint             = emberAfCurrentEndpoint();
154     EmAfZllIdentifyState * state = getZllIdentifyState(endpoint);
155     EmberAfStatus status;
156
157     if (state == NULL)
158     {
159         status = EMBER_ZCL_STATUS_FAILURE;
160         goto default_response;
161     }
162
163     emberAfIdentifyClusterPrintln("RX identify:trigger effect 0x%x variant 0x%x", effectId, effectVariant);
164
165     if (state->active)
166     {
167         switch (state->effectId)
168         {
169         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_FINISH_EFFECT:
170             state->cancel = true;
171             status        = EMBER_ZCL_STATUS_SUCCESS;
172             break;
173         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_STOP_EFFECT:
174             deactivateZllIdentify(state, endpoint);
175             status = EMBER_ZCL_STATUS_SUCCESS;
176             goto default_response;
177         default:
178             status = EMBER_ZCL_STATUS_FAILURE;
179             break;
180         }
181     }
182     else
183     {
184         switch (effectId)
185         {
186         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BLINK:
187             state->eventsRemaining = EMBER_AF_PLUGIN_ZLL_IDENTIFY_SERVER_BLINK_EVENTS;
188             break;
189         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_BREATHE:
190             state->eventsRemaining = EMBER_AF_PLUGIN_ZLL_IDENTIFY_SERVER_BREATHE_EVENTS;
191             break;
192         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_OKAY:
193             state->eventsRemaining = EMBER_AF_PLUGIN_ZLL_IDENTIFY_SERVER_OKAY_EVENTS;
194             break;
195         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_CHANNEL_CHANGE:
196             state->eventsRemaining = EMBER_AF_PLUGIN_ZLL_IDENTIFY_SERVER_CHANNEL_CHANGE_EVENTS;
197             break;
198         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_FINISH_EFFECT: // At this point, these are functionally equivalent
199         case EMBER_ZCL_IDENTIFY_EFFECT_IDENTIFIER_STOP_EFFECT:
200             status = EMBER_ZCL_STATUS_SUCCESS;
201             goto default_response;
202         default:
203             status = EMBER_ZCL_STATUS_FAILURE;
204             goto default_response;
205         }
206         state->active         = true;
207         state->cancel         = false;
208         state->effectId       = (EmberAfIdentifyEffectIdentifier) effectId;
209         state->commandVariant = (EmberAfIdentifyEffectVariant) effectVariant;
210         state->eventDelay     = EMBER_AF_PLUGIN_ZLL_IDENTIFY_SERVER_EVENT_DELAY;
211         emberAfEndpointEventControlSetDelayMS(emberAfPluginZllIdentifyServerTriggerEffectEndpointEventControls, endpoint,
212                                               state->eventDelay);
213         status = EMBER_ZCL_STATUS_SUCCESS;
214     }
215
216 default_response:
217     emberAfSendImmediateDefaultResponse(status);
218     return true;
219 }
220
221 void emAfPluginZllIdentifyServerBlinkEffect(uint8_t endpoint)
222 {
223     EmAfZllIdentifyState * state = getZllIdentifyState(endpoint);
224
225     if (state == NULL || state->eventsRemaining == 0)
226     {
227         deactivateZllIdentify(state, endpoint);
228         return;
229     }
230
231 #ifndef EZSP_HOST
232     halToggleLed(BOARDLED0);
233     halToggleLed(BOARDLED1);
234     halToggleLed(BOARDLED2);
235     halToggleLed(BOARDLED3);
236 #endif
237
238     state->eventsRemaining = state->eventsRemaining - 1;
239 }
240
241 void emAfPluginZllIdentifyServerBreatheEffect(uint8_t endpoint)
242 {
243     emAfPluginZllIdentifyServerBlinkEffect(endpoint);
244 }
245
246 void emAfPluginZllIdentifyServerOkayEffect(uint8_t endpoint)
247 {
248     emAfPluginZllIdentifyServerBlinkEffect(endpoint);
249 }
250
251 void emAfPluginZllIdentifyServerChannelChangeEffect(uint8_t endpoint)
252 {
253     emAfPluginZllIdentifyServerBlinkEffect(endpoint);
254 }