Increase line & function coverage
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_zcl_level_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_level_control.h"
25
26 /**< Private data */
27 typedef struct {
28         ZblibDriverZclLevelControlOps_t *ops; /**< Operations */
29 } ZblibDriverZclLevelControlPrivData_t;
30
31 /* LCOV_EXCL_START */
32 static gboolean __zblib_driver_zcl_level_control_dispatcher(ZigBeeDriver *driver, guint request_id)
33 {
34         ZigBeeService *service = NULL;
35         ZblibDriverZclLevelControlPrivData_t *priv_data = NULL;
36         ZblibDriverZclLevelControlOps_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_LEVEL_CONTROL_OPS_MOVE_TO_LEVEL: {
60                 zblib_check_null_ret_error("ops->move_to_level",
61                         ops->move_to_level, FALSE);
62
63                 ret = ops->move_to_level(driver, request_id);
64         }
65         break;
66
67         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_MOVE: {
68                 zblib_check_null_ret_error("ops->move",
69                         ops->move, FALSE);
70
71                 ret = ops->move(driver, request_id);
72         }
73         break;
74
75         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_STEP: {
76                 zblib_check_null_ret_error("ops->step",
77                         ops->step, FALSE);
78
79                 ret = ops->step(driver, request_id);
80         }
81         break;
82
83         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_MOVE_TO_LEVEL_WITH_ON_OFF: {
84                 zblib_check_null_ret_error("ops->move_to_level_with_on_off",
85                         ops->move_to_level_with_on_off, FALSE);
86
87                 ret = ops->move_to_level_with_on_off(driver, request_id);
88         }
89         break;
90
91         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_MOVE_WITH_ON_OFF: {
92                 zblib_check_null_ret_error("ops->move_with_on_off",
93                         ops->move_with_on_off, FALSE);
94
95                 ret = ops->move_with_on_off(driver, request_id);
96         }
97         break;
98
99         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_STEP_WITH_ON_OFF: {
100                 zblib_check_null_ret_error("ops->step_with_on_off",
101                         ops->step_with_on_off, FALSE);
102
103                 ret = ops->step_with_on_off(driver, request_id);
104         }
105         break;
106
107         case ZBLIB_ZCL_LEVEL_CONTROL_OPS_STOP: {
108                 zblib_check_null_ret_error("ops->stop",
109                         ops->stop, FALSE);
110
111                 ret = ops->stop(driver, request_id);
112         }
113         break;
114
115         default:
116         break;
117         }
118
119         Z_LOGD("ret: [%d]", ret);
120
121         return ret;
122 }
123 /* LCOV_EXCL_STOP */
124
125 static void __zblib_driver_zcl_level_control_free_hook(ZigBeeDriver *driver)
126 {
127         ZblibDriverZclLevelControlPrivData_t *priv_data = NULL;
128
129         zblib_check_null_ret("driver", driver);
130
131         /* Fetch private data */
132         priv_data = zblib_driver_ref_object(driver);
133         zblib_check_null_ret("priv_data", priv_data);
134
135         /* Free resources */
136         g_free(priv_data);
137 }
138
139 ZigBeeDriver *zblib_driver_zcl_level_control_new(ZigBeePlugin *plugin,
140         const gchar *driver_name,
141         ZblibDriverZclLevelControlOps_t *ops)
142 {
143         ZigBeeDriver *driver = NULL;
144         ZigBeeService *service = NULL;
145         ZblibDriverZclLevelControlPrivData_t *priv_data = NULL;
146         gboolean ret;
147
148         zblib_check_null_ret_error("plugin", plugin, NULL);
149
150         /* Create new driver */
151         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_ZCL_LEVEL_CONTROL);
152         zblib_check_null_ret_error("driver", driver, NULL);
153
154         /* Allocate memory for private data */
155         priv_data = g_malloc0(sizeof(ZblibDriverZclLevelControlPrivData_t));
156
157         /* Update private data */
158         priv_data->ops = ops;
159
160         /* Link service to driver */
161         service = zblib_plugin_ref_service(plugin);
162         if (NULL == service) {
163                 /* LCOV_EXCL_START */
164                 Z_LOGE("zblib_plugin_ref_service failed!");
165
166                 /* Free allocated resources */
167                 g_free(priv_data);
168                 g_free(driver);
169
170                 return NULL;
171                 /* LCOV_EXCL_STOP */
172         }
173         zblib_driver_link_service(driver, service);
174
175         /* Link private data to driver */
176         ret = zblib_driver_link_object(driver, priv_data);
177         if (FALSE == ret) {
178                 /* LCOV_EXCL_START */
179                 Z_LOGE("zblib_driver_link_object failed!");
180
181                 /* Free allocated resources */
182                 g_free(priv_data);
183                 g_free(driver);
184
185                 return NULL;
186                 /* LCOV_EXCL_STOP */
187         }
188
189         /* Set operations dispatcher function */
190         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_zcl_level_control_dispatcher);
191         if (FALSE == ret) {
192                 /* LCOV_EXCL_START */
193                 Z_LOGE("zblib_driver_set_dispatcher failed!");
194
195                 /* Free allocated resources */
196                 g_free(priv_data);
197                 g_free(driver);
198
199                 return NULL;
200                 /* LCOV_EXCL_STOP */
201         }
202
203         /* Set free hook function */
204         ret = zblib_driver_set_free_hook(driver, __zblib_driver_zcl_level_control_free_hook);
205         if (FALSE == ret) {
206                 /* LCOV_EXCL_START */
207                 Z_LOGE("zblib_driver_set_free_hook failed!");
208
209                 /* Free allocated resources */
210                 g_free(priv_data);
211                 g_free(driver);
212
213                 return NULL;
214                 /* LCOV_EXCL_STOP */
215         }
216
217         return driver;
218 }
219
220 /* LCOV_EXCL_START */
221 void zblib_driver_zcl_level_control_free(ZigBeeDriver *driver)
222 {
223         zblib_check_null_ret("driver", driver);
224
225         /* Free driver */
226         zblib_driver_free(driver);
227 }
228 /* LCOV_EXCL_STOP */