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