ffdabcc821a080c835b1783ff4dc165869b143d9
[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         guchar value = 0;
97
98         gboolean ret;
99
100         memset(&req, 0x0, sizeof(ZigbeeZdoBindBindReq_t));
101
102         /* Update request structure */
103         req.node_id = node_id;
104         g_variant_get(src_addr, "a(y)", &iter);
105         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
106                 req.src_addr[i] = value;
107                 i++;
108         }
109         req.src_ep = src_ep;
110         req.cluster_id = cluster_id;
111
112         i = 0;
113         g_variant_get(dst_addr, "a(y)", &iter);
114         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
115                 req.dst_addr[i] = value;
116                 i++;
117         }
118         req.bind_type = bind_type;
119         req.group_addr = group_addr;
120         req.dst_ep = dst_ep;
121
122         /* Allocate response callback data */
123         resp_cb_data =
124                 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
125                         invocation, NULL, 0);
126         if (NULL == resp_cb_data) {
127                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
128
129                 /* Send failure response */
130                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
131
132                 return TRUE;
133         }
134
135         /* Dispatch request */
136         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
137                 ZBLIB_DRIVER_TYPE_ZDO_BIND,
138                 ZBLIB_ZDO_BIND_OPS_BIND_REQ,
139                 &req, sizeof(req),
140                 on_zdo_bind_bind_req_resp, resp_cb_data);
141         if (FALSE == ret) {
142                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
143
144                 /* Free response callback data */
145                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
146
147                 /* Send failure response */
148                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
149
150                 return TRUE;
151         }
152
153         return TRUE;
154 }
155
156 static void on_zdo_bind_unbind_req_resp(ZigBeeServiceInterface *service_interface,
157         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
158 {
159         ZigbeeServiceInterfaceRespCbData_t *cb_data =
160                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
161
162         ZigbeeZdo_bind *bind_object = NULL;
163         GDBusMethodInvocation *invocation = NULL;
164
165         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t *)resp_data;
166
167         NOT_USED(service_interface);
168         NOT_USED(request_id);
169
170         if (NULL == resp_data || 0 == resp_data_len) {
171                 Z_LOGE("resp_data=%p or resp_data_len=%d is null", resp_data, resp_data_len);
172                 g_free(cb_data);
173                 return;
174         }
175
176         bind_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
177         zblib_check_null_free_and_ret("bind_object", bind_object, cb_data);
178
179         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
180         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
181
182         zigbee_zdo_bind_complete_unbind_req(bind_object, invocation, payload->result);
183
184         g_free(cb_data);
185 }
186
187 static gboolean on_zdo_bind_unbind_req(ZigbeeZdo_bind *zdo_bind_object,
188         GDBusMethodInvocation *invocation,
189         gshort node_id,
190         GVariant *src_addr,
191         gchar src_ep,
192         gshort cluster_id,
193         GVariant *dst_addr,
194         gchar bind_type,
195         gshort group_addr,
196         gchar dst_ep,
197         gpointer user_data)
198 {
199         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
200         ZigbeeZdoBindUnbindReq_t req;
201         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
202
203         GVariantIter *iter = NULL;
204         gint i = 0;
205         guchar value = 0;
206
207         gboolean ret;
208
209         memset(&req, 0x0, sizeof(ZigbeeZdoBindUnbindReq_t));
210
211         /* Update request structure */
212         req.node_id = node_id;
213         g_variant_get(src_addr, "a(y)", &iter);
214         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
215                 req.src_addr[i] = value;
216                 i++;
217         }
218         req.src_ep = src_ep;
219         req.cluster_id = cluster_id;
220
221         i = 0;
222         g_variant_get(dst_addr, "a(y)", &iter);
223         while (i < ZIGBEE_EUI64_SIZE && g_variant_iter_loop(iter, "(y)", &value)) {
224                 req.dst_addr[i] = value;
225                 i++;
226         }
227         req.bind_type = bind_type;
228         req.group_addr = group_addr;
229         req.dst_ep = dst_ep;
230
231         /* Allocate response callback data */
232         resp_cb_data =
233                 zigbee_service_dbus_interface_create_resp_cb_data(zdo_bind_object,
234                         invocation, NULL, 0);
235         if (NULL == resp_cb_data) {
236                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
237
238                 /* Send failure response */
239                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
240
241                 return TRUE;
242         }
243
244         /* Dispatch request */
245         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
246                 ZBLIB_DRIVER_TYPE_ZDO_BIND,
247                 ZBLIB_ZDO_BIND_OPS_UNBIND_REQ,
248                 &req, sizeof(req),
249                 on_zdo_bind_unbind_req_resp, resp_cb_data);
250         if (FALSE == ret) {
251                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
252
253                 /* Free response callback data */
254                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
255
256                 /* Send failure response */
257                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
258
259                 return TRUE;
260         }
261
262         return TRUE;
263 }
264
265 void zigbee_service_dbus_interface_zdo_bind_notification(ZigBeeServiceInterface *service_interface,
266         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
267 {
268         ZigbeeZdo_bind *bind_object;
269
270         zblib_check_null_ret("service_interface", service_interface);
271
272         if (NULL == noti_data || 0 == noti_data_len) {
273                 Z_LOGE("noti_data=%p or noti_data_len=%d is null", noti_data, noti_data_len);
274                 return;
275         }
276
277         bind_object = _service_interface_ref_zigbee_zdo_bind(service_interface);
278         zblib_check_null_ret("bind_object", bind_object);
279
280         NOT_USED(noti_cb_data);
281
282         switch(noti_id) {
283         case ZBLIB_ZDO_BIND_NOTI_BIND_REQ: {
284                 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
285
286                 Z_LOGD("'bind_rsp' status : [0x%X]", rsp->status);
287
288                 zigbee_zdo_bind_emit_bind_rsp(bind_object, rsp->status);
289         }
290         break;
291         case ZBLIB_ZDO_BIND_NOTI_UNBIND_REQ: {
292                 ZigbeeZdoBindUnbindNoti_t *rsp = (ZigbeeZdoBindUnbindNoti_t*)noti_data;
293
294                 Z_LOGD("'unbind_rsp' status : [0x%X]", rsp->status);
295
296                 zigbee_zdo_bind_emit_unbind_rsp(bind_object, rsp->status);
297         }
298         break;
299         default:
300                 Z_LOGE("Unexpected notification [%x]", noti_id);
301         break;
302         }
303
304         /* ZigbeeZdo_bind should be dereferenced */
305         g_object_unref(bind_object);
306 }
307
308 gboolean zigbee_service_dbus_interface_zdo_bind_init(ZigBeeServiceInterface *service_interface,
309         ZigbeeObjectSkeleton *zigbee_object)
310 {
311         ZigbeeZdo_bind *zdo_bind_object;
312
313         if (NULL == service_interface) {
314                 Z_LOGE("service_interface is NULL");
315                 return FALSE;
316         }
317
318         zdo_bind_object = zigbee_zdo_bind_skeleton_new();
319         zigbee_object_skeleton_set_zdo_bind(zigbee_object, zdo_bind_object);
320         g_object_unref(zdo_bind_object);
321
322         Z_LOGI("zdo_bind_object: [%p]", zdo_bind_object);
323
324         /*
325          * Register signal handlers for 'zdo_bind' interface
326          */
327         g_signal_connect(zdo_bind_object,
328                 "handle-bind-req",
329                 G_CALLBACK(on_zdo_bind_bind_req), service_interface);
330
331         g_signal_connect(zdo_bind_object,
332                 "handle-unbind-req",
333                 G_CALLBACK(on_zdo_bind_unbind_req), service_interface);
334
335         return TRUE;
336 }