Increase line & function coverage
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-lib / src / drivers / zblib_driver_service.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_service.h>
23 #include <zblib_request.h>
24
25 #include "zblib_driver_service.h"
26
27 /**< Private data */
28 typedef struct {
29         ZblibDriverServiceOps_t *ops; /**< Operations */
30 } ZblibDriverServicePrivData_t;
31
32 /* LCOV_EXCL_START */
33 static gboolean __zblib_driver_service_dispatcher(ZigBeeDriver *driver, guint request_id)
34 {
35         ZigBeeService *service = NULL;
36         ZblibDriverServicePrivData_t *priv_data = NULL;
37         ZblibDriverServiceOps_t *ops = NULL;
38         guint request_type;
39         guint ops_id;
40         gboolean ret = FALSE;
41
42         zblib_check_null_ret_error("driver", driver, FALSE);
43
44         service = zblib_driver_ref_service(driver);
45         zblib_check_null_ret_error("service", service, FALSE);
46
47         /* Fetch private data */
48         priv_data = zblib_driver_ref_object(driver);
49         zblib_check_null_ret_error("priv_data", priv_data, FALSE);
50
51         ops = priv_data->ops;
52         zblib_check_null_ret_error("ops", ops, FALSE);
53
54         /* Fetch request_type using request_id */
55         request_type = zblib_request_ref_request_type_by_request_id(service, request_id);
56
57         /* Fetch ops ID */
58         ops_id = zblib_request_get_ops_id(request_type);
59         switch (ops_id) {
60         case ZBLIB_SERVICE_OPS_ZB_HW_RESET: {
61                 zblib_check_null_ret_error("ops->zb_hw_reset",
62                         ops->zb_hw_reset, FALSE);
63
64                 ret = ops->zb_hw_reset(driver, request_id);
65         }
66         break;
67
68         case ZBLIB_SERVICE_OPS_FORM_NETWORK: {
69                 zblib_check_null_ret_error("ops->form_network",
70                         ops->form_network, FALSE);
71
72                 ret = ops->form_network(driver, request_id);
73         }
74         break;
75
76         case ZBLIB_SERVICE_OPS_COEX_START: {
77                 zblib_check_null_ret_error("ops->coex_start",
78                         ops->coex_start, FALSE);
79
80                 ret = ops->coex_start(driver, request_id);
81         }
82         break;
83
84         case ZBLIB_SERVICE_OPS_COEX_STOP: {
85                 zblib_check_null_ret_error("ops->coex_stop",
86                         ops->coex_stop, FALSE);
87
88                 ret = ops->coex_stop(driver, request_id);
89         }
90         break;
91
92         case ZBLIB_SERVICE_OPS_LEAVE_NETWORK: {
93                 zblib_check_null_ret_error("ops->leave_network",
94                         ops->leave_network, FALSE);
95
96                 ret = ops->leave_network(driver, request_id);
97         }
98         break;
99
100         case ZBLIB_SERVICE_OPS_GET_NETWORK_INFO: {
101                 zblib_check_null_ret_error("ops->get_network_info",
102                         ops->get_network_info, FALSE);
103
104                 ret = ops->get_network_info(driver, request_id);
105         }
106         break;
107
108         case ZBLIB_SERVICE_OPS_PERMIT_JOIN: {
109                 zblib_check_null_ret_error("ops->permit_join",
110                         ops->permit_join, FALSE);
111
112                 ret = ops->permit_join(driver, request_id);
113         }
114         break;
115
116         case ZBLIB_SERVICE_OPS_LEAVE_REQUEST: {
117                 zblib_check_null_ret_error("ops->leave_request",
118                         ops->leave_request, FALSE);
119
120                 ret = ops->leave_request(driver, request_id);
121         }
122         break;
123
124         case ZBLIB_SERVICE_OPS_GET_DEVICE_LIST: {
125                 zblib_check_null_ret_error("ops->get_device_list",
126                         ops->get_device_list, FALSE);
127
128                 ret = ops->get_device_list(driver, request_id);
129         }
130         break;
131
132         case ZBLIB_SERVICE_OPS_GET_MAC: {
133                 zblib_check_null_ret_error("ops->get_mac",
134                         ops->get_mac, FALSE);
135
136                 ret = ops->get_mac(driver, request_id);
137         }
138         break;
139
140         case ZBLIB_SERVICE_OPS_GET_DEVICE_INFO: {
141                 zblib_check_null_ret_error("ops->get_device_info",
142                         ops->get_device_info, FALSE);
143
144                 ret = ops->get_device_info(driver, request_id);
145         }
146         break;
147
148         case ZBLIB_SERVICE_OPS_GET_ENDPOINT_LIST: {
149                 zblib_check_null_ret_error("ops->get_endpoint_list",
150                         ops->get_endpoint_list, FALSE);
151
152                 ret = ops->get_endpoint_list(driver, request_id);
153         }
154         break;
155
156         case ZBLIB_SERVICE_OPS_GET_CLUSTER_LIST: {
157                 zblib_check_null_ret_error("ops->get_cluster_list",
158                         ops->get_cluster_list, FALSE);
159
160                 ret = ops->get_cluster_list(driver, request_id);
161         }
162         break;
163
164         case ZBLIB_SERVICE_OPS_GET_NODE_TYPE: {
165                 zblib_check_null_ret_error("ops->get_node_type",
166                         ops->get_node_type, FALSE);
167
168                 ret = ops->get_node_type(driver, request_id);
169         }
170         break;
171
172         default:
173         break;
174         }
175
176         Z_LOGD("ret: [%d]", ret);
177
178         return ret;
179 }
180 /* LCOV_EXCL_STOP */
181
182 static void __zblib_driver_service_free_hook(ZigBeeDriver *driver)
183 {
184         ZblibDriverServicePrivData_t *priv_data = NULL;
185
186         zblib_check_null_ret("driver", driver);
187
188         /* Fetch private data */
189         priv_data = zblib_driver_ref_object(driver);
190         zblib_check_null_ret("priv_data", priv_data);
191
192         /* Free resources */
193         g_free(priv_data);
194 }
195
196 ZigBeeDriver *zblib_driver_service_new(ZigBeePlugin *plugin,
197         const gchar *driver_name,
198         ZblibDriverServiceOps_t *ops)
199 {
200         ZigBeeDriver *driver = NULL;
201         ZigBeeService *service = NULL;
202         ZblibDriverServicePrivData_t *priv_data = NULL;
203         gboolean ret;
204
205         zblib_check_null_ret_error("plugin", plugin, NULL);
206
207         /* Create new driver */
208         driver = zblib_driver_new(plugin, driver_name, ZBLIB_DRIVER_TYPE_SERVICE);
209         zblib_check_null_ret_error("driver", driver, NULL);
210
211         /* Allocate memory for private data */
212         priv_data = g_malloc0(sizeof(ZblibDriverServicePrivData_t));
213
214         /* Update private data */
215         priv_data->ops = ops;
216
217         /* Link service to driver */
218         service = zblib_plugin_ref_service(plugin);
219         if (NULL == service) {
220                 /* LCOV_EXCL_START */
221                 Z_LOGE("zblib_plugin_ref_service failed!");
222
223                 /* Free allocated resources */
224                 g_free(priv_data);
225                 g_free(driver);
226
227                 return NULL;
228                 /* LCOV_EXCL_STOP */
229         }
230         zblib_driver_link_service(driver, service);
231
232         /* Link private data to driver */
233         ret = zblib_driver_link_object(driver, priv_data);
234         if (FALSE == ret) {
235                 /* LCOV_EXCL_START */
236                 Z_LOGE("zblib_driver_link_object failed!");
237
238                 /* Free allocated resources */
239                 g_free(priv_data);
240                 g_free(driver);
241
242                 return NULL;
243                 /* LCOV_EXCL_STOP */
244         }
245
246         /* Set operations dispatcher function */
247         ret = zblib_driver_set_dispatcher(driver, __zblib_driver_service_dispatcher);
248         if (FALSE == ret) {
249                 /* LCOV_EXCL_START */
250                 Z_LOGE("zblib_driver_set_dispatcher failed!");
251
252                 /* Free allocated resources */
253                 g_free(priv_data);
254                 g_free(driver);
255
256                 return NULL;
257                 /* LCOV_EXCL_STOP */
258         }
259
260         /* Set free hook function */
261         ret = zblib_driver_set_free_hook(driver, __zblib_driver_service_free_hook);
262         if (FALSE == ret) {
263                 /* LCOV_EXCL_START */
264                 Z_LOGE("zblib_driver_set_free_hook failed!");
265
266                 /* Free allocated resources */
267                 g_free(priv_data);
268                 g_free(driver);
269
270                 return NULL;
271                 /* LCOV_EXCL_START */
272         }
273
274         return driver;
275 }
276
277 /* LCOV_EXCL_START */
278 void zblib_driver_service_free(ZigBeeDriver *driver)
279 {
280         zblib_check_null_ret("driver", driver);
281
282         /* Free driver */
283         zblib_driver_free(driver);
284 }
285 /* LCOV_EXCL_STOP */