Implement ZCL Color Control Cluster
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_color_control.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Contact: Suresh Kumar N (suresh.n@samsung.com)
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 #include <zblib.h>
20 #include <zblib_driver.h>
21 #include <zblib_plugin.h>
22 #include <zblib_request.h>
23
24 #include "zblib_driver_zcl_color_control.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverZclColorControlOps_t *ops; /**< Operations */
29 } ZblibDriverZclColorControlPrivData_t;
30
31 static gboolean __zblib_driver_zcl_color_control_dispatcher(ZigBeeDriver *driver, guint request_id)
32 {
33         ZigBeeService *service = NULL;
34         ZblibDriverZclColorControlPrivData_t *priv_data = NULL;
35         ZblibDriverZclColorControlOps_t *ops = NULL;
36         guint request_type;
37         guint ops_id;
38         gboolean ret = FALSE;
39
40         zblib_check_null_ret_error("driver", driver, FALSE);
41
42         service = zblib_driver_ref_service(driver);
43         zblib_check_null_ret_error("service", service, FALSE);
44
45         /* Fetch private data */
46         priv_data = zblib_driver_ref_object(driver);
47         zblib_check_null_ret_error("priv_data", priv_data, FALSE);
48
49         ops = priv_data->ops;
50         zblib_check_null_ret_error("ops", ops, FALSE);
51
52         /* Fetch request_type using request_id */
53         request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
54
55         /* Fetch ops ID */
56         ops_id = zblib_request_get_ops_id(request_type);
57         switch (ops_id) {
58         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_TO_HUE: {
59                 zblib_check_null_ret_error("ops->move_to_hue",
60                         ops->move_to_hue, FALSE);
61
62                 ret = ops->move_to_hue(driver, request_id);
63         }
64         break;
65
66         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_HUE: {
67                 zblib_check_null_ret_error("ops->move_hue",
68                         ops->move_hue, FALSE);
69
70                 ret = ops->move_hue(driver, request_id);
71         }
72         break;
73
74         case ZBLIB_ZCL_COLOR_CONTROL_OPS_STEP_HUE: {
75                 zblib_check_null_ret_error("ops->step_hue",
76                         ops->step_hue, FALSE);
77
78                 ret = ops->step_hue(driver, request_id);
79         }
80         break;
81
82         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_TO_STAURATION: {
83                 zblib_check_null_ret_error("ops->move_to_saturation",
84                         ops->move_to_saturation, FALSE);
85
86                 ret = ops->move_to_saturation(driver, request_id);
87         }
88         break;
89
90         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_STAURATION: {
91                 zblib_check_null_ret_error("ops->move_saturation",
92                         ops->move_saturation, FALSE);
93
94                 ret = ops->move_saturation(driver, request_id);
95         }
96         break;
97
98         case ZBLIB_ZCL_COLOR_CONTROL_OPS_STEP_STAURATION: {
99                 zblib_check_null_ret_error("ops->step_saturation",
100                         ops->step_saturation, FALSE);
101
102                 ret = ops->step_saturation(driver, request_id);
103         }
104         break;
105
106         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_TO_HUE_AND_SATURATION: {
107                 zblib_check_null_ret_error("ops->move_to_hue_and_saturation",
108                         ops->move_to_hue_and_saturation, FALSE);
109
110                 ret = ops->move_to_hue_and_saturation(driver, request_id);
111         }
112         break;
113
114         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_TO_COLOR: {
115                 zblib_check_null_ret_error("ops->move_to_color",
116                         ops->move_to_color, FALSE);
117
118                 ret = ops->move_to_color(driver, request_id);
119         }
120         break;
121
122         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_COLOR: {
123                 zblib_check_null_ret_error("ops->move_color",
124                         ops->move_color, FALSE);
125
126                 ret = ops->move_color(driver, request_id);
127         }
128         break;
129
130         case ZBLIB_ZCL_COLOR_CONTROL_OPS_STEP_COLOR: {
131                 zblib_check_null_ret_error("ops->step_color",
132                         ops->step_color, FALSE);
133
134                 ret = ops->step_color(driver, request_id);
135         }
136         break;
137
138         case ZBLIB_ZCL_COLOR_CONTROL_OPS_MOVE_COLOR_TEMPERATURE: {
139                 zblib_check_null_ret_error("ops->move_color_temperature",
140                         ops->move_color_temperature, FALSE);
141
142                 ret = ops->move_color_temperature(driver, request_id);
143         }
144         break;
145
146         default:
147         break;
148         }
149
150         Z_LOGD("ret: [%d]", ret);
151
152         return ret;
153 }
154
155 static void __zblib_driver_zcl_color_control_free_hook(ZigBeeDriver *driver)
156 {
157         ZblibDriverZclColorControlPrivData_t *priv_data = NULL;
158
159         zblib_check_null_ret("driver", driver);
160
161         /* Fetch private data */
162         priv_data = zblib_driver_ref_object(driver);
163         zblib_check_null_ret("priv_data", priv_data);
164
165         /* Free resources */
166         g_free(priv_data);
167 }
168
169 ZigBeeDriver *zblib_driver_zcl_color_control_new(ZigBeePlugin *plugin,
170         const gchar *driver_name,
171         ZblibDriverZclColorControlOps_t *ops)
172 {
173         ZigBeeDriver *driver = NULL;
174         ZigBeeService *service = NULL;
175         ZblibDriverZclColorControlPrivData_t *priv_data = NULL;
176         gboolean ret;
177
178         zblib_check_null_ret_error("plugin", plugin, NULL);
179
180         /* Create new driver */
181         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_COLOR_CONTROL);
182         zblib_check_null_ret_error("driver", driver, NULL);
183
184         /* Allocate memory for private data */
185         priv_data = g_malloc0(sizeof(ZblibDriverZclColorControlPrivData_t));
186
187         /* Update private data */
188         priv_data->ops = ops;
189
190         /* Link service to driver */
191         service = zblib_plugin_ref_service(plugin);
192         if (NULL == service) {
193                 Z_LOGE("zblib_plugin_ref_service failed!");
194
195                 /* Free allocated resources */
196                 g_free(priv_data);
197                 g_free(driver);
198
199                 return NULL;
200         }
201         zblib_driver_link_service(driver, service);
202
203         /* Link private data to driver */
204         ret = zblib_driver_link_object(driver, priv_data);
205         if (FALSE == ret) {
206                 Z_LOGE("zblib_driver_link_object failed!");
207
208                 /* Free allocated resources */
209                 g_free(priv_data);
210                 g_free(driver);
211
212                 return NULL;
213         }
214
215         /* Set operations dispatcher function */
216         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_color_control_dispatcher);
217         if (FALSE == ret) {
218                 Z_LOGE("zblib_driver_set_dispatcher failed!");
219
220                 /* Free allocated resources */
221                 g_free(priv_data);
222                 g_free(driver);
223
224                 return NULL;
225         }
226
227         /* Set free hook function */
228         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_color_control_free_hook);
229         if (FALSE == ret) {
230                 Z_LOGE("zblib_driver_set_free_hook failed!");
231
232                 /* Free allocated resources */
233                 g_free(priv_data);
234                 g_free(driver);
235
236                 return NULL;
237         }
238
239         return driver;
240 }
241
242 void zblib_driver_zcl_color_control_free(ZigBeeDriver *driver)
243 {
244         zblib_check_null_ret("driver", driver);
245
246         /* Free driver */
247         zblib_driver_free(driver);
248 }