Fix the coverity issues
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_zdo_bind.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 "zigbee_service_interface_common.h"
20
21 #include <zblib_driver_zdo_bind.h>
22
23 /* LCOV_EXCL_START */
24 static void *_service_interface_ref_zigbee_zdo_bind(
25         ZigBeeServiceInterface *service_interface)
26 {
27         ZigbeeObjectSkeleton *zigbee_object = NULL;
28         ZigbeeCustomData_t *custom_data = NULL;
29         ZigbeeZdo_bind *bind_object = NULL;
30
31         custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
32         if (NULL == custom_data) {
33                 Z_LOGE("D-BUS service interface custom_data is NULL!");
34                 return NULL;
35         }
36
37         /* Get zigbee object */
38         zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
39         if (NULL == zigbee_object) {
40                 Z_LOGW("Cannot find ZigBee D-BUS interface object!");
41                 return NULL;
42         }
43
44         bind_object = zigbee_object_get_zdo_bind(ZIGBEE_OBJECT(zigbee_object));
45         return bind_object;
46 }
47
48 static void on_zdo_bind_bind_req_resp(ZigBeeServiceInterface *service_interface,
49         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
50 {
51         ZigbeeServiceInterfaceRespCbData_t *cb_data =
52                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
53
54         ZigbeeZdo_bind *bind_object = NULL;
55         GDBusMethodInvocation *invocation = NULL;
56
57         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
58
59         NOT_USED(service_interface);
60         NOT_USED(request_id);
61
62         if (NULL == resp_data || 0 == resp_data_len) {
63                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
64                 g_free(cb_data);
65                 return;
66         }
67
68         bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
69         zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
70
71         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
72         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
73
74         zigbee_zdo_bind_complete_bind_req(bind_object, invocation, payload->result);
75
76         g_free(cb_data);
77 }
78
79 static gboolean on_zdo_bind_bind_req(ZigbeeZdo_bind *zdo_bind_object,
80         GDBusMethodInvocation *invocation,
81         gshort node_id,
82         GVariant *src_addr,
83         gchar src_ep,
84         gshort cluster_id,
85         GVariant *dst_addr,
86         gchar bind_type,
87         gshort group_addr,
88         gchar dst_ep,
89         gpointer user_data)
90 {
91         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
92         ZigbeeZdoBindBindReq_t req;
93         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
94
95         GVariantIter *iter = NULL;
96         gint i = 0;
97         guchar value = 0;
98
99         gboolean ret;
100
101         memset(&req, 0x0, sizeof(ZigbeeZdoBindBindReq_t));
102
103         /* Update request structure */
104         req.node_id = node_id;
105         g_variant_get(src_addr, "a(y)", &iter);
106         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
107                 req.src_addr[i] = value;
108                 i++;
109         }
110         req.src_ep = src_ep;
111         req.cluster_id = cluster_id;
112
113         i = 0;
114         g_variant_get(dst_addr, "a(y)", &iter);
115         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
116                 req.dst_addr[i] = value;
117                 i++;
118         }
119         req.bind_type = bind_type;
120         req.group_addr = group_addr;
121         req.dst_ep = dst_ep;
122
123         /* Allocate response callback data */
124         resp_cb_data =
125                 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
126                         invocation, NULL, 0);
127         if (NULL == resp_cb_data) {
128                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
129
130                 /* Send failure response */
131                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
132
133                 return TRUE;
134         }
135
136         /* Dispatch request */
137         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
138                 ZBLIB_DRIVER_TYPE_ZDO_BIND,
139                 ZBLIB_ZDO_BIND_OPS_BIND_REQ,
140                 &req, sizeof(req),
141                 on_zdo_bind_bind_req_resp, resp_cb_data);
142         if (FALSE == ret) {
143                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
144
145                 /* Free response callback data */
146                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
147
148                 /* Send failure response */
149                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
150
151                 return TRUE;
152         }
153
154         return TRUE;
155 }
156
157 static void on_zdo_bind_unbind_req_resp(ZigBeeServiceInterface *service_interface,
158         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
159 {
160         ZigbeeServiceInterfaceRespCbData_t *cb_data =
161                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
162
163         ZigbeeZdo_bind *bind_object = NULL;
164         GDBusMethodInvocation *invocation = NULL;
165
166         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
167
168         NOT_USED(service_interface);
169         NOT_USED(request_id);
170
171         if (NULL == resp_data || 0 == resp_data_len) {
172                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
173                 g_free(cb_data);
174                 return;
175         }
176
177         bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
178         zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
179
180         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
181         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
182
183         zigbee_zdo_bind_complete_unbind_req(bind_object, invocation, payload->result);
184
185         g_free(cb_data);
186 }
187
188 static gboolean on_zdo_bind_unbind_req(ZigbeeZdo_bind *zdo_bind_object,
189         GDBusMethodInvocation *invocation,
190         gshort node_id,
191         GVariant *src_addr,
192         gchar src_ep,
193         gshort cluster_id,
194         GVariant *dst_addr,
195         gchar bind_type,
196         gshort group_addr,
197         gchar dst_ep,
198         gpointer user_data)
199 {
200         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
201         ZigbeeZdoBindUnbindReq_t req;
202         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
203
204         GVariantIter *iter = NULL;
205         gint i = 0;
206         guchar value = 0;
207
208         gboolean ret;
209
210         memset(&req, 0x0, sizeof(ZigbeeZdoBindUnbindReq_t));
211
212         /* Update request structure */
213         req.node_id = node_id;
214         g_variant_get(src_addr, "a(y)", &iter);
215         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
216                 req.src_addr[i] = value;
217                 i++;
218         }
219         req.src_ep = src_ep;
220         req.cluster_id = cluster_id;
221
222         i = 0;
223         g_variant_get(dst_addr, "a(y)", &iter);
224         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
225                 req.dst_addr[i] = value;
226                 i++;
227         }
228         req.bind_type = bind_type;
229         req.group_addr = group_addr;
230         req.dst_ep = dst_ep;
231
232         /* Allocate response callback data */
233         resp_cb_data =
234                 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
235                         invocation, NULL, 0);
236         if (NULL == resp_cb_data) {
237                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
238
239                 /* Send failure response */
240                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
241
242                 return TRUE;
243         }
244
245         /* Dispatch request */
246         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
247                 ZBLIB_DRIVER_TYPE_ZDO_BIND,
248                 ZBLIB_ZDO_BIND_OPS_UNBIND_REQ,
249                 &req, sizeof(req),
250                 on_zdo_bind_unbind_req_resp, resp_cb_data);
251         if (FALSE == ret) {
252                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
253
254                 /* Free response callback data */
255                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
256
257                 /* Send failure response */
258                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
259
260                 return TRUE;
261         }
262
263         return TRUE;
264 }
265
266 void zigbee_service_dbus_interface_zdo_bind_notification(ZigBeeServiceInterface *service_interface,
267         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
268 {
269         ZigbeeZdo_bind *bind_object;
270
271         zblib_check_null_ret("service_interface", service_interface);
272
273         if (NULL == noti_data || 0 == noti_data_len) {
274                 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
275                 return;
276         }
277
278         bind_object = _service_interface_ref_zigbee_zdo_bind(service_interface);
279         zblib_check_null_ret("bind_object", bind_object);
280
281         NOT_USED(noti_cb_data);
282
283         switch (noti_id) {
284         case ZBLIB_ZDO_BIND_NOTI_BIND_REQ: {
285                 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
286
287                 Z_LOGD("'bind_rsp' status : [0x%X]", rsp->status);
288
289                 zigbee_zdo_bind_emit_bind_rsp(bind_object, rsp->status);
290         }
291         break;
292         case ZBLIB_ZDO_BIND_NOTI_UNBIND_REQ: {
293                 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
294
295                 Z_LOGD("'unbind_rsp' status : [0x%X]", rsp->status);
296
297                 zigbee_zdo_bind_emit_unbind_rsp(bind_object, rsp->status);
298         }
299         break;
300         default:
301                 Z_LOGE("Unexpected notification [%x]", noti_id);
302         break;
303         }
304
305         /* ZigbeeZdo_bind should be dereferenced */
306         g_object_unref(bind_object);
307 }
308 /* LCOV_EXCL_STOP */
309
310 gboolean zigbee_service_dbus_interface_zdo_bind_init(ZigBeeServiceInterface *service_interface,
311         ZigbeeObjectSkeleton *zigbee_object)
312 {
313         ZigbeeZdo_bind *zdo_bind_object;
314
315         if (NULL == service_interface) {
316                 /* LCOV_EXCL_START */
317                 Z_LOGE("service_interface is NULL");
318                 return FALSE;
319                 /* LCOV_EXCL_STOP */
320         }
321
322         zdo_bind_object = zigbee_zdo_bind_skeleton_new();
323         zigbee_object_skeleton_set_zdo_bind(zigbee_object, zdo_bind_object);
324         g_object_unref(zdo_bind_object);
325
326         Z_LOGI("zdo_bind_object: [%p]", zdo_bind_object);
327
328         /*
329          * Register signal handlers for 'zdo_bind' interface
330          */
331         g_signal_connect(zdo_bind_object,
332                 "handle-bind-req",
333                 G_CALLBACK(on_zdo_bind_bind_req), service_interface);
334
335         g_signal_connect(zdo_bind_object,
336                 "handle-unbind-req",
337                 G_CALLBACK(on_zdo_bind_unbind_req), service_interface);
338
339         return TRUE;
340 }