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