Fix resource leak problem : 32057, 28497, 41860, 22384, 26742
[platform/core/connectivity/zigbee-manager.git] / zigbee-daemon / zigbee-interface / src / zigbee_service_dbus_interface_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 "zigbee_service_interface_common.h"
20
21 #include <zblib_driver_service.h>
22
23 static void *_service_interface_ref_zigbee_service(ZigBeeServiceInterface *service_interface)
24 {
25         ZigbeeObjectSkeleton *zigbee_object = NULL;
26         ZigbeeCustomData_t *custom_data = NULL;
27         ZigbeeService *service = NULL;
28
29         custom_data = (ZigbeeCustomData_t *)zblib_service_interface_ref_user_data(service_interface);
30         if (NULL == custom_data) {
31                 Z_LOGE("D-BUS service interface custom_data is NULL!");
32                 return NULL;
33         }
34
35         /* Get zigbee object */
36         zigbee_object = g_hash_table_lookup(custom_data->objects, ZIGBEE_SERVICE_PATH);
37         if (NULL == zigbee_object) {
38                 Z_LOGW("Cannot find ZigBee D-BUS interface object!", zigbee_object);
39                 return NULL;
40         }
41
42         service = zigbee_object_get_service(ZIGBEE_OBJECT(zigbee_object));
43         return service;
44 }
45
46 static void on_service_zb_hw_reset_resp(ZigBeeServiceInterface *service_interface,
47         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
48 {
49         ZigbeeServiceInterfaceRespCbData_t *cb_data =
50                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
51
52         ZigbeeService *service_object;
53         GDBusMethodInvocation *invocation;
54         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
55
56         NOT_USED(service_interface);
57         NOT_USED(request_id);
58
59         if (NULL == resp_data || 0 == resp_data_len) {
60                 Z_LOGE("resp_data is null");
61                 g_free(cb_data);
62                 return;
63         }
64
65         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
66         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
67
68         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
69         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
70
71         zigbee_service_complete_zb_hw_reset(service_object, invocation, payload->result);
72
73         g_free(cb_data);
74 }
75
76 static gboolean on_service_zb_hw_reset(ZigbeeService *service_object,
77         GDBusMethodInvocation *invocation,
78         gpointer user_data)
79 {
80         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
81         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
82
83         gboolean ret;
84
85         /* Allocate response callback data */
86         resp_cb_data =
87                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
88                         invocation, NULL, 0);
89         if (NULL == resp_cb_data) {
90                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
91
92                 /* Send failure response */
93                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
94
95                 return TRUE;
96         }
97
98         /* Dispatch request */
99         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
100                 ZBLIB_DRIVER_TYPE_SERVICE,
101                 ZBLIB_SERVICE_OPS_ZB_HW_RESET,
102                 NULL, 0,
103                 on_service_zb_hw_reset_resp, resp_cb_data);
104         if (FALSE == ret) {
105                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
106
107                 /* Free response callback data */
108                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
109
110                 /* Send failure response */
111                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
112
113                 return TRUE;
114         }
115
116         return TRUE;
117 }
118
119 static void on_service_form_network_resp(ZigBeeServiceInterface *service_interface,
120         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
121 {
122         ZigbeeServiceInterfaceRespCbData_t *cb_data =
123                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
124
125         ZigbeeService *service_object;
126         GDBusMethodInvocation *invocation;
127         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
128
129         NOT_USED(service_interface);
130         NOT_USED(request_id);
131
132         if (NULL == resp_data || 0 == resp_data_len) {
133                 Z_LOGE("resp_data is null");
134                 g_free(cb_data);
135                 return;
136         }
137
138         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
139         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
140
141         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
142         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
143
144         zigbee_service_complete_form_network(service_object, invocation, payload->result);
145
146         g_free(cb_data);
147 }
148
149 static gboolean on_service_form_network(ZigbeeService *service_object,
150         GDBusMethodInvocation *invocation,
151         gpointer user_data)
152 {
153         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
154         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
155
156         gboolean ret;
157
158         /* Allocate response callback data */
159         resp_cb_data =
160                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
161                         invocation, NULL, 0);
162         if (NULL == resp_cb_data) {
163                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
164
165                 /* Send failure response */
166                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
167
168                 return TRUE;
169         }
170
171         /* Dispatch request */
172         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
173                 ZBLIB_DRIVER_TYPE_SERVICE,
174                 ZBLIB_SERVICE_OPS_FORM_NETWORK,
175                 NULL, 0,
176                 on_service_form_network_resp, resp_cb_data);
177         if (FALSE == ret) {
178                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
179
180                 /* Free response callback data */
181                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
182
183                 /* Send failure response */
184                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
185
186                 return TRUE;
187         }
188
189         return TRUE;
190 }
191
192 static void on_service_coex_start_resp(ZigBeeServiceInterface *service_interface,
193         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
194 {
195         ZigbeeServiceInterfaceRespCbData_t *cb_data =
196                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
197
198         ZigbeeService *service_object;
199         GDBusMethodInvocation *invocation;
200         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
201
202         NOT_USED(service_interface);
203         NOT_USED(request_id);
204
205         if (NULL == resp_data || 0 == resp_data_len) {
206                 Z_LOGE("resp_data is null");
207                 g_free(cb_data);
208                 return;
209         }
210
211         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
212         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
213
214         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
215         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
216
217         zigbee_service_complete_coex_start(service_object, invocation, payload->result);
218
219         g_free(cb_data);
220 }
221
222 static gboolean on_service_coex_start(ZigbeeService *service_object,
223         GDBusMethodInvocation *invocation,
224         gchar channel,
225         gpointer user_data)
226 {
227         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
228         ZigbeeServiceCoexStart_t req;
229         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
230
231         gboolean ret;
232
233         memset(&req, 0x0, sizeof(ZigbeeServiceCoexStart_t));
234
235         /* Update request structure */
236         req.channel = channel;
237
238         /* Allocate response callback data */
239         resp_cb_data =
240                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
241                         invocation, NULL, 0);
242         if (NULL == resp_cb_data) {
243                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
244
245                 /* Send failure response */
246                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
247
248                 return TRUE;
249         }
250
251         /* Dispatch request */
252         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
253                 ZBLIB_DRIVER_TYPE_SERVICE,
254                 ZBLIB_SERVICE_OPS_COEX_START,
255                 &req, sizeof(req),
256                 on_service_coex_start_resp, resp_cb_data);
257         if (FALSE == ret) {
258                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
259
260                 /* Free response callback data */
261                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
262
263                 /* Send failure response */
264                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
265
266                 return TRUE;
267         }
268
269         return TRUE;
270 }
271
272 static void on_service_coex_stop_resp(ZigBeeServiceInterface *service_interface,
273         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
274 {
275         ZigbeeServiceInterfaceRespCbData_t *cb_data =
276                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
277
278         ZigbeeService *service_object;
279         GDBusMethodInvocation *invocation;
280         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
281
282         NOT_USED(service_interface);
283         NOT_USED(request_id);
284
285         if (NULL == resp_data || 0 == resp_data_len) {
286                 Z_LOGE("resp_data is null");
287                 g_free(cb_data);
288                 return;
289         }
290
291         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
292         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
293
294         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
295         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
296
297         zigbee_service_complete_coex_stop(service_object, invocation, payload->result);
298
299         g_free(cb_data);
300 }
301
302 static gboolean on_service_coex_stop(ZigbeeService *service_object,
303         GDBusMethodInvocation *invocation,
304         gpointer user_data)
305 {
306         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
307         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
308
309         gboolean ret;
310
311         /* Allocate response callback data */
312         resp_cb_data =
313                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
314                         invocation, NULL, 0);
315         if (NULL == resp_cb_data) {
316                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
317
318                 /* Send failure response */
319                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
320
321                 return TRUE;
322         }
323
324         /* Dispatch request */
325         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
326                 ZBLIB_DRIVER_TYPE_SERVICE,
327                 ZBLIB_SERVICE_OPS_COEX_STOP,
328                 NULL, 0,
329                 on_service_coex_stop_resp, resp_cb_data);
330         if (FALSE == ret) {
331                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
332
333                 /* Free response callback data */
334                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
335
336                 /* Send failure response */
337                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
338
339                 return TRUE;
340         }
341
342         return TRUE;
343 }
344
345 static void on_service_leave_network_resp(ZigBeeServiceInterface *service_interface,
346         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
347 {
348         ZigbeeServiceInterfaceRespCbData_t *cb_data =
349                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
350
351         ZigbeeService *service_object;
352         GDBusMethodInvocation *invocation;
353         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
354
355         NOT_USED(service_interface);
356         NOT_USED(request_id);
357
358         if (NULL == resp_data || 0 == resp_data_len) {
359                 Z_LOGE("resp_data is null");
360                 g_free(cb_data);
361                 return;
362         }
363
364         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
365         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
366
367         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
368         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
369
370         zigbee_service_complete_leave_network(service_object, invocation, payload->result);
371
372         g_free(cb_data);
373 }
374
375 static gboolean on_service_leave_network(ZigbeeService *service_object,
376         GDBusMethodInvocation *invocation,
377         gpointer user_data)
378 {
379         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
380         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
381
382         gboolean ret;
383
384         /* Allocate response callback data */
385         resp_cb_data =
386                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
387                         invocation, NULL, 0);
388         if (NULL == resp_cb_data) {
389                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
390
391                 /* Send failure response */
392                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
393
394                 return TRUE;
395         }
396
397         /* Dispatch request */
398         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
399                 ZBLIB_DRIVER_TYPE_SERVICE,
400                 ZBLIB_SERVICE_OPS_LEAVE_NETWORK,
401                 NULL, 0,
402                 on_service_leave_network_resp, resp_cb_data);
403         if (FALSE == ret) {
404                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
405
406                 /* Free response callback data */
407                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
408
409                 /* Send failure response */
410                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
411
412                 return TRUE;
413         }
414
415         return TRUE;
416 }
417
418 static void on_service_get_network_info_resp(ZigBeeServiceInterface *service_interface,
419         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
420 {
421         ZigbeeServiceInterfaceRespCbData_t *cb_data =
422                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
423
424         ZigbeeService *service_object;
425         GDBusMethodInvocation *invocation;
426
427         ZigbeeServiceGetNetworkInfoResp_t *payload =
428                 (ZigbeeServiceGetNetworkInfoResp_t*)resp_data;
429
430         GVariant *v_eui64 = NULL;
431
432         NOT_USED(service_interface);
433         NOT_USED(request_id);
434
435         if (NULL == resp_data || 0 == resp_data_len) {
436                 Z_LOGE("resp_data is null");
437                 g_free(cb_data);
438                 return;
439         }
440
441         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
442         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
443
444         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
445         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
446
447         v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
448                 payload->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
449
450         zigbee_service_complete_get_network_info(service_object, invocation,
451                 payload->result, v_eui64, payload->node_id,
452                 payload->pan_id, payload->channel, payload->radio_tx_power);
453
454         if (v_eui64)
455                 g_variant_unref(v_eui64);
456
457         g_free(cb_data);
458 }
459
460 static gboolean on_service_get_network_info(ZigbeeService *service_object,
461         GDBusMethodInvocation *invocation,
462         gpointer user_data)
463 {
464         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
465         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
466
467         gboolean ret;
468
469         /* Allocate response callback data */
470         resp_cb_data =
471                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
472                         invocation, NULL, 0);
473         if (NULL == resp_cb_data) {
474                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
475
476                 /* Send failure response */
477                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
478
479                 return TRUE;
480         }
481
482         /* Dispatch request */
483         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
484                 ZBLIB_DRIVER_TYPE_SERVICE,
485                 ZBLIB_SERVICE_OPS_GET_NETWORK_INFO,
486                 NULL, 0,
487                 on_service_get_network_info_resp, resp_cb_data);
488         if (FALSE == ret) {
489                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
490
491                 /* Free response callback data */
492                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
493
494                 /* Send failure response */
495                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
496
497                 return TRUE;
498         }
499
500         return TRUE;
501 }
502
503 static void on_service_permit_join_resp(ZigBeeServiceInterface *service_interface,
504         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
505 {
506         ZigbeeServiceInterfaceRespCbData_t *cb_data =
507                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
508
509         ZigbeeService *service_object;
510         GDBusMethodInvocation *invocation;
511         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
512
513         NOT_USED(service_interface);
514         NOT_USED(request_id);
515
516         if (NULL == resp_data || 0 == resp_data_len) {
517                 Z_LOGE("resp_data is null");
518                 g_free(cb_data);
519                 return;
520         }
521
522         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
523         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
524
525         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
526         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
527
528         zigbee_service_complete_permit_join(service_object, invocation, payload->result);
529
530         g_free(cb_data);
531 }
532
533 static gboolean on_service_permit_join(ZigbeeService *service_object,
534         GDBusMethodInvocation *invocation,
535         gint duration,
536         gboolean permit_join,
537         gpointer user_data)
538 {
539         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
540         ZigbeeServicePermitJoin_t req;
541         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
542
543         gboolean ret;
544
545         memset(&req, 0x0, sizeof(ZigbeeServicePermitJoin_t));
546
547         /* Update request structure */
548         req.duration = duration;
549         req.permit_join = permit_join;
550
551         /* Allocate response callback data */
552         resp_cb_data =
553                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
554                         invocation, NULL, 0);
555         if (NULL == resp_cb_data) {
556                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
557
558                 /* Send failure response */
559                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
560
561                 return TRUE;
562         }
563
564         /* Dispatch request */
565         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
566                 ZBLIB_DRIVER_TYPE_SERVICE,
567                 ZBLIB_SERVICE_OPS_PERMIT_JOIN,
568                 &req, sizeof(req),
569                 on_service_permit_join_resp, resp_cb_data);
570         if (FALSE == ret) {
571                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
572
573                 /* Free response callback data */
574                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
575
576                 /* Send failure response */
577                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
578
579                 return TRUE;
580         }
581
582         return TRUE;
583 }
584
585 static void on_service_leave_request_resp(ZigBeeServiceInterface *service_interface,
586         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
587 {
588         ZigbeeServiceInterfaceRespCbData_t *cb_data =
589                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
590
591         ZigbeeService *service_object;
592         GDBusMethodInvocation *invocation;
593         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
594
595         NOT_USED(service_interface);
596         NOT_USED(request_id);
597
598         if (NULL == resp_data || 0 == resp_data_len) {
599                 Z_LOGE("resp_data is null");
600                 g_free(cb_data);
601                 return;
602         }
603
604         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
605         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
606
607         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
608         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
609
610         zigbee_service_complete_leave_request(service_object, invocation, payload->result);
611
612         g_free(cb_data);
613 }
614
615 static gboolean on_service_leave_request(ZigbeeService *service_object,
616         GDBusMethodInvocation *invocation,
617         GVariant *eui64,
618         gchar remove_child,
619         gchar rejoin,
620         gpointer user_data)
621 {
622         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
623         ZigbeeServiceLeaveRequest_t req;
624         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
625
626         GVariantIter *iter = NULL;
627         guint i = 0;
628
629         gboolean ret;
630
631         memset(&req, 0x0, sizeof(ZigbeeServiceLeaveRequest_t));
632
633         /* Update request structure */
634         g_variant_get(eui64, "a(y)", &iter);
635         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
636                 i++;
637                 if (i >= ZIGBEE_EUI64_SIZE)
638                         break;
639         }
640         req.remove_child = remove_child;
641         req.rejoin = rejoin;
642
643         Z_LOGD("IEEE Address = %X:%X:%X:%X:%X:%X:%X:%X",
644                 req.eui64[0], req.eui64[1], req.eui64[2], req.eui64[3],
645                 req.eui64[4], req.eui64[5], req.eui64[6], req.eui64[7]);
646         Z_LOGD("remove_child [%d]", remove_child);
647         Z_LOGD("rejoin [%d]", rejoin);
648
649         /* Allocate response callback data */
650         resp_cb_data =
651                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
652                         invocation, NULL, 0);
653         if (NULL == resp_cb_data) {
654                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
655
656                 /* Send failure response */
657                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
658
659                 return TRUE;
660         }
661
662         /* Dispatch request */
663         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
664                 ZBLIB_DRIVER_TYPE_SERVICE,
665                 ZBLIB_SERVICE_OPS_LEAVE_REQUEST,
666                 &req, sizeof(req),
667                 on_service_leave_request_resp, resp_cb_data);
668         if (FALSE == ret) {
669                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
670
671                 /* Free response callback data */
672                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
673
674                 /* Send failure response */
675                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
676
677                 return TRUE;
678         }
679
680         return TRUE;
681 }
682
683 static void on_service_get_device_list_resp(ZigBeeServiceInterface *service_interface,
684         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
685 {
686         ZigbeeServiceInterfaceRespCbData_t *cb_data =
687                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
688
689         ZigbeeService *service_object;
690         GDBusMethodInvocation *invocation;
691
692         NOT_USED(service_interface);
693         NOT_USED(request_id);
694
695         if (NULL == resp_data || 0 == resp_data_len) {
696                 Z_LOGE("resp_data is null");
697                 g_free(cb_data);
698                 return;
699         }
700
701         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
702         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
703
704         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
705         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
706
707         /* TODO : zigbee_service_complete_get_device_list */
708         /* zigbee_service_complete_get_device_list(service_object, invocation, resp_data); */
709
710         g_free(cb_data);
711 }
712
713 static gboolean on_service_get_device_list(ZigbeeService *service_object,
714         GDBusMethodInvocation *invocation,
715         gpointer user_data)
716 {
717         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
718         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
719
720         gboolean ret;
721
722         /* Allocate response callback data */
723         resp_cb_data =
724                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
725                         invocation, NULL, 0);
726         if (NULL == resp_cb_data) {
727                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
728
729                 /* Send failure response */
730                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
731
732                 return TRUE;
733         }
734
735         /* Dispatch request */
736         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
737                 ZBLIB_DRIVER_TYPE_SERVICE,
738                 ZBLIB_SERVICE_OPS_GET_DEVICE_LIST,
739                 NULL, 0,
740                 on_service_get_device_list_resp, resp_cb_data);
741         if (FALSE == ret) {
742                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
743
744                 /* Free response callback data */
745                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
746
747                 /* Send failure response */
748                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
749
750                 return TRUE;
751         }
752
753         return TRUE;
754 }
755
756 static void on_service_get_mac_resp(ZigBeeServiceInterface *service_interface,
757         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
758 {
759         ZigbeeServiceInterfaceRespCbData_t *cb_data =
760                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
761
762         ZigbeeService *service_object;
763         GDBusMethodInvocation *invocation;
764         ZigbeeServiceGetMacResp_t *payload =
765                 (ZigbeeServiceGetMacResp_t*)resp_data;
766
767         GVariant *v_eui64 = NULL;
768
769         NOT_USED(service_interface);
770         NOT_USED(request_id);
771
772         if (NULL == resp_data || 0 == resp_data_len) {
773                 Z_LOGE("resp_data is null");
774                 g_free(cb_data);
775                 return;
776         }
777
778         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
779         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
780
781         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
782         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
783
784         v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
785                 payload->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
786
787         zigbee_service_complete_get_mac(service_object, invocation, payload->result,
788                 v_eui64);
789
790         if (v_eui64)
791                 g_variant_unref(v_eui64);
792
793         g_free(cb_data);
794 }
795
796 static gboolean on_service_get_mac(ZigbeeService *service_object,
797         GDBusMethodInvocation *invocation,
798         gpointer user_data)
799 {
800         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
801         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
802
803         gboolean ret;
804
805         /* Allocate response callback data */
806         resp_cb_data =
807                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
808                         invocation, NULL, 0);
809         if (NULL == resp_cb_data) {
810                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
811
812                 /* Send failure response */
813                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
814
815                 return TRUE;
816         }
817
818         /* Dispatch request */
819         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
820                 ZBLIB_DRIVER_TYPE_SERVICE,
821                 ZBLIB_SERVICE_OPS_GET_MAC,
822                 NULL, 0,
823                 on_service_get_mac_resp, resp_cb_data);
824         if (FALSE == ret) {
825                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
826
827                 /* Free response callback data */
828                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
829
830                 /* Send failure response */
831                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
832
833                 return TRUE;
834         }
835
836         return TRUE;
837 }
838
839 static void on_service_get_device_info_resp(ZigBeeServiceInterface *service_interface,
840         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
841 {
842         ZigbeeServiceInterfaceRespCbData_t *cb_data =
843                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
844
845         ZigbeeService *service_object;
846         GDBusMethodInvocation *invocation;
847         ZigbeeServiceGetDeviceInfoResp_t *payload =
848                 (ZigbeeServiceGetDeviceInfoResp_t*)resp_data;
849
850         GVariant *variant = NULL;
851         GVariantBuilder* builder = NULL;
852         int i = 0;
853         int index = 0;
854
855         NOT_USED(service_interface);
856         NOT_USED(request_id);
857
858         if (NULL == resp_data || 0 == resp_data_len) {
859                 Z_LOGE("resp_data is null");
860                 g_free(cb_data);
861                 return;
862         }
863
864         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
865         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
866
867         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
868         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
869
870         builder = g_variant_builder_new(G_VARIANT_TYPE("a(qyayyay)"));
871         for (index = 0; index < payload->count; index++) {
872                 ZigbeeServiceGetDeviceInfoDetailResp_t *device = &(payload->list[index]);
873                 GVariantBuilder* mac_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
874                 GVariantBuilder* endpoint_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
875
876                 for (i = 0; i < ZIGBEE_EUI64_SIZE; i++)
877                         g_variant_builder_add(mac_builder, "y", device->eui64[i]);
878                 for (i = 0; i < device->endpoint_count; i++)
879                         g_variant_builder_add(endpoint_builder, "y", device->endpoints[i]);
880
881                 g_variant_builder_add(builder, "(qyayyay)",
882                         device->node_id, device->node_type,
883                         mac_builder, device->endpoint_count, endpoint_builder);
884
885                 g_variant_builder_unref(mac_builder);
886                 g_variant_builder_unref(endpoint_builder);
887         }
888
889         variant = g_variant_builder_end(builder);
890         zigbee_service_complete_get_device_info(service_object, invocation,
891                 payload->result, variant);
892
893         if (variant)
894                 g_variant_unref(variant);
895
896         g_free(cb_data);
897 }
898
899 static gboolean on_service_get_device_info(ZigbeeService *service_object,
900         GDBusMethodInvocation *invocation,
901         gpointer user_data)
902 {
903         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
904         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
905
906         gboolean ret;
907
908         /* Allocate response callback data */
909         resp_cb_data =
910                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
911                         invocation, NULL, 0);
912         if (NULL == resp_cb_data) {
913                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
914
915                 /* Send failure response */
916                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
917
918                 return TRUE;
919         }
920
921         /* Dispatch request */
922         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
923                 ZBLIB_DRIVER_TYPE_SERVICE,
924                 ZBLIB_SERVICE_OPS_GET_DEVICE_INFO,
925                 NULL, 0,
926                 on_service_get_device_info_resp, resp_cb_data);
927         if (FALSE == ret) {
928                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
929
930                 /* Free response callback data */
931                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
932
933                 /* Send failure response */
934                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
935
936                 return TRUE;
937         }
938
939         return TRUE;
940 }
941
942 static void on_service_get_endpoint_list_resp(ZigBeeServiceInterface *service_interface,
943         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
944 {
945         ZigbeeServiceInterfaceRespCbData_t *cb_data =
946                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
947
948         ZigbeeService *service_object;
949         GDBusMethodInvocation *invocation;
950         ZigbeeServiceGetEndpointListResp_t *payload =
951                 (ZigbeeServiceGetEndpointListResp_t*)resp_data;
952         GVariant *v_endpoints = NULL;
953
954         NOT_USED(service_interface);
955         NOT_USED(request_id);
956
957         if (NULL == resp_data || 0 == resp_data_len) {
958                 Z_LOGE("resp_data is null");
959                 g_free(cb_data);
960                 return;
961         }
962
963         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
964         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
965
966         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
967         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
968
969         v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
970                 payload->endpoints, payload->endpoint_count, TRUE, NULL, NULL);
971
972         zigbee_service_complete_get_endpoint_list(service_object, invocation,
973                 payload->result, v_endpoints);
974
975         if (v_endpoints)
976                 g_variant_unref(v_endpoints);
977
978         g_free(cb_data);
979 }
980
981 static gboolean on_service_get_endpoint_list(ZigbeeService *service_object,
982         GDBusMethodInvocation *invocation,
983         GVariant *eui64,
984         gpointer user_data)
985 {
986         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
987         ZigbeeServiceGetEndpointList_t req;
988         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
989
990         GVariantIter *iter = NULL;
991         guint i = 0;
992
993         gboolean ret;
994
995         memset(&req, 0x0, sizeof(ZigbeeServiceGetEndpointList_t));
996
997         /* Update request structure */
998         g_variant_get(eui64, "a(y)", &iter);
999         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
1000                 i++;
1001                 if (i >= ZIGBEE_EUI64_SIZE)
1002                         break;
1003         }
1004
1005         /* Allocate response callback data */
1006         resp_cb_data =
1007                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1008                         invocation, NULL, 0);
1009         if (NULL == resp_cb_data) {
1010                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1011
1012                 /* Send failure response */
1013                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1014
1015                 return TRUE;
1016         }
1017
1018         /* Dispatch request */
1019         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1020                 ZBLIB_DRIVER_TYPE_SERVICE,
1021                 ZBLIB_SERVICE_OPS_GET_ENDPOINT_LIST,
1022                 &req, sizeof(req),
1023                 on_service_get_endpoint_list_resp, resp_cb_data);
1024         if (FALSE == ret) {
1025                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1026
1027                 /* Free response callback data */
1028                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1029
1030                 /* Send failure response */
1031                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1032
1033                 return TRUE;
1034         }
1035
1036         return TRUE;
1037 }
1038
1039 static void on_service_get_cluster_list_resp(ZigBeeServiceInterface *service_interface,
1040         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1041 {
1042         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1043                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1044
1045         ZigbeeService *service_object;
1046         GDBusMethodInvocation *invocation;
1047         ZigbeeServiceGetClusterListResp_t *payload =
1048                 (ZigbeeServiceGetClusterListResp_t*)resp_data;
1049         GVariant *v_in_clusters = NULL;
1050         GVariant *v_out_clusters = NULL;
1051
1052         NOT_USED(service_interface);
1053         NOT_USED(request_id);
1054
1055         if (NULL == resp_data || 0 == resp_data_len) {
1056                 Z_LOGE("resp_data is null");
1057                 g_free(cb_data);
1058                 return;
1059         }
1060
1061         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1062         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1063
1064         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1065         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1066
1067         v_in_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1068                 payload->in_clusters, (payload->in_cluster_count * sizeof(unsigned short)),
1069                 TRUE, NULL, NULL);
1070         v_out_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1071                 payload->out_clusters, (payload->out_cluster_count * sizeof(unsigned short)),
1072                 TRUE, NULL, NULL);
1073
1074         zigbee_service_complete_get_cluster_list(service_object, invocation,
1075                 payload->result, v_in_clusters, v_out_clusters);
1076
1077         if (v_in_clusters)
1078                 g_variant_unref(v_in_clusters);
1079         if (v_out_clusters)
1080                 g_variant_unref(v_out_clusters);
1081
1082         g_free(cb_data);
1083 }
1084
1085 static gboolean on_service_get_cluster_list(ZigbeeService *service_object,
1086         GDBusMethodInvocation *invocation,
1087         GVariant *eui64,
1088         gchar endpoint,
1089         gpointer user_data)
1090 {
1091         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1092         ZigbeeServiceGetClusterList_t req;
1093         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1094
1095         GVariantIter *iter = NULL;
1096         guint i = 0;
1097
1098         gboolean ret;
1099
1100         memset(&req, 0x0, sizeof(ZigbeeServiceGetClusterList_t));
1101
1102         /* Update request structure */
1103         g_variant_get(eui64, "a(y)", &iter);
1104         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
1105                 i++;
1106                 if (i >= ZIGBEE_EUI64_SIZE)
1107                         break;
1108         }
1109         req.endpoint = endpoint;
1110
1111         /* Allocate response callback data */
1112         resp_cb_data =
1113                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1114                         invocation, NULL, 0);
1115         if (NULL == resp_cb_data) {
1116                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1117
1118                 /* Send failure response */
1119                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1120
1121                 return TRUE;
1122         }
1123
1124         /* Dispatch request */
1125         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1126                 ZBLIB_DRIVER_TYPE_SERVICE,
1127                 ZBLIB_SERVICE_OPS_GET_CLUSTER_LIST,
1128                 &req, sizeof(req),
1129                 on_service_get_cluster_list_resp, resp_cb_data);
1130         if (FALSE == ret) {
1131                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1132
1133                 /* Free response callback data */
1134                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1135
1136                 /* Send failure response */
1137                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1138
1139                 return TRUE;
1140         }
1141
1142         return TRUE;
1143 }
1144
1145 static void on_service_get_node_type_resp(ZigBeeServiceInterface *service_interface,
1146         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1147 {
1148         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1149                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1150
1151         ZigbeeService *service_object;
1152         GDBusMethodInvocation *invocation;
1153         ZigbeeServiceServiceNodeTypeResp_t *payload =
1154                 (ZigbeeServiceServiceNodeTypeResp_t *)resp_data;
1155
1156         NOT_USED(service_interface);
1157         NOT_USED(request_id);
1158
1159         if (NULL == resp_data || 0 == resp_data_len) {
1160                 Z_LOGE("resp_data is null");
1161                 g_free(cb_data);
1162                 return;
1163         }
1164
1165         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1166         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1167
1168         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1169         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1170
1171         zigbee_service_complete_get_node_type(service_object, invocation,
1172                 payload->result, payload->node_type);
1173
1174         g_free(cb_data);
1175 }
1176
1177 static gboolean on_service_get_node_type(ZigbeeService *service_object,
1178         GDBusMethodInvocation *invocation,
1179         GVariant *eui64,
1180         gpointer user_data)
1181 {
1182         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1183         ZigbeeServiceGetNodeType_t req;
1184         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1185
1186         GVariantIter *iter = NULL;
1187         guint i = 0;
1188
1189         gboolean ret;
1190
1191         memset(&req, 0x0, sizeof(ZigbeeServiceGetNodeType_t));
1192
1193         /* Update request structure */
1194         g_variant_get(eui64, "a(y)", &iter);
1195         while (g_variant_iter_loop(iter, "(y)", req.eui64[i])) {
1196                 i++;
1197                 if (i >= ZIGBEE_EUI64_SIZE)
1198                         break;
1199         }
1200
1201         /* Allocate response callback data */
1202         resp_cb_data =
1203                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1204                         invocation, NULL, 0);
1205         if (NULL == resp_cb_data) {
1206                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1207
1208                 /* Send failure response */
1209                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1210
1211                 return TRUE;
1212         }
1213
1214         /* Dispatch request */
1215         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1216                 ZBLIB_DRIVER_TYPE_SERVICE,
1217                 ZBLIB_SERVICE_OPS_GET_NODE_TYPE,
1218                 &req, sizeof(req),
1219                 on_service_get_node_type_resp, resp_cb_data);
1220         if (FALSE == ret) {
1221                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1222
1223                 /* Free response callback data */
1224                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1225
1226                 /* Send failure response */
1227                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1228
1229                 return TRUE;
1230         }
1231
1232         return TRUE;
1233 }
1234
1235 static gboolean on_service_check_privilege(ZigbeeService *service_object,
1236         GDBusMethodInvocation *invocation,
1237         gpointer user_data)
1238 {
1239         NOT_USED(user_data);
1240
1241         zigbee_service_complete_check_privilege(service_object, invocation);
1242
1243         return TRUE;
1244 }
1245
1246 void zigbee_service_dbus_interface_service_notification(ZigBeeServiceInterface *service_interface,
1247         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
1248 {
1249         ZigbeeService *service_object;
1250
1251         zblib_check_null_ret("service_interface", service_interface);
1252
1253         if (NULL == noti_data || 0 == noti_data_len) {
1254                 Z_LOGE("noti_data is NULL");
1255                 return;
1256         }
1257
1258         service_object = _service_interface_ref_zigbee_service(service_interface);
1259         zblib_check_null_ret("service_object", service_object);
1260
1261         NOT_USED(noti_cb_data);
1262
1263         switch (noti_id) {
1264         case ZBLIB_SERVICE_NOTI_FORM_NETWORK_DONE: {
1265                 ZigbeeServiceServiceFormNetworkDone_t *panid_t =
1266                         (ZigbeeServiceServiceFormNetworkDone_t*)noti_data;
1267
1268                 Z_LOGD("form_network_done : [0x%X]", panid_t->pan_id);
1269
1270                 zigbee_service_emit_form_network_done(service_object, panid_t->pan_id);
1271         }
1272         break;
1273         case ZBLIB_SERVICE_NOTI_CHILD_JOINED: {
1274                 ZigbeeServiceServiceChildJoined_t *child_t =
1275                         (ZigbeeServiceServiceChildJoined_t*)noti_data;
1276
1277                 GVariant *v_eui64 = NULL;
1278                 GVariant *v_endpoints = NULL;
1279
1280                 v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1281                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1282                 v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1283                         child_t->endpoints, child_t->endpoint_count, TRUE, NULL, NULL);
1284                 if (NULL == v_eui64 || NULL == v_endpoints) {
1285                         Z_LOGE("Failed to create variant!");
1286                 } else {
1287                         zigbee_service_emit_child_joined(service_object,
1288                                 v_eui64, child_t->endpoint_count, v_endpoints, child_t->node_id);
1289                 }
1290                 if (v_eui64)
1291                         g_variant_unref(v_eui64);
1292                 if (v_endpoints)
1293                         g_variant_unref(v_endpoints);
1294         }
1295         break;
1296         case ZBLIB_SERVICE_NOTI_CHILD_REJOINED: {
1297                 ZigbeeServiceServiceChildRejoined_t *child_t =
1298                         (ZigbeeServiceServiceChildRejoined_t*)noti_data;
1299
1300                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1301                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1302                 if (NULL == v_eui64)
1303                         Z_LOGE("Failed to create variant!");
1304                 else
1305                         zigbee_service_emit_child_rejoined(service_object, v_eui64);
1306                 if (v_eui64)
1307                         g_variant_unref(v_eui64);
1308         }
1309         break;
1310         case ZBLIB_SERVICE_NOTI_CHILD_LEFT: {
1311                 ZigbeeServiceServiceChildLeft_t *child_left_t =
1312                         (ZigbeeServiceServiceChildLeft_t*)noti_data;
1313
1314                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1315                         child_left_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1316                 if (NULL == v_eui64)
1317                         Z_LOGE("Failed to create variant!");
1318                 else {
1319                         zigbee_service_emit_child_left(service_object,
1320                                 v_eui64, child_left_t->status);
1321                         g_variant_unref(v_eui64);
1322                 }
1323         }
1324         break;
1325         case ZBLIB_SERVICE_NOTI_LEAVE_NETWORK_DONE: {
1326                 ZigbeeServiceServiceLeaveNetworkDone_t leave_net_t;
1327                 memcpy(&leave_net_t, noti_data, noti_data_len);
1328                 zigbee_service_emit_leave_network_done(service_object, leave_net_t.pan_id);
1329         }
1330         break;
1331         default:
1332                 Z_LOGE("Unexpected notification [%x]", noti_id);
1333         break;
1334         }
1335
1336         /* ZigbeeService should be dereferenced */
1337         g_object_unref(service_object);
1338 }
1339
1340 gboolean zigbee_service_dbus_interface_service_init(ZigBeeServiceInterface *service_interface,
1341         ZigbeeObjectSkeleton *zigbee_object)
1342 {
1343         ZigbeeService *service_object;
1344
1345         if (NULL == service_interface) {
1346                 Z_LOGE("service_interface is NULL");
1347                 return FALSE;
1348         }
1349
1350         service_object = zigbee_service_skeleton_new();
1351         zigbee_object_skeleton_set_service(zigbee_object, service_object);
1352         g_object_unref(service_object);
1353
1354         Z_LOGI("service_object: [%p]", service_object);
1355
1356         /*
1357          * Register signal handlers for 'service' interface
1358          */
1359         g_signal_connect(service_object,
1360                 "handle-zb-hw-reset",
1361                 G_CALLBACK(on_service_zb_hw_reset), service_interface);
1362
1363         g_signal_connect(service_object,
1364                 "handle-form-network",
1365                 G_CALLBACK(on_service_form_network), service_interface);
1366
1367         g_signal_connect(service_object,
1368                 "handle-coex-start",
1369                 G_CALLBACK(on_service_coex_start), service_interface);
1370
1371         g_signal_connect(service_object,
1372                 "handle-coex-stop",
1373                 G_CALLBACK(on_service_coex_stop), service_interface);
1374
1375         g_signal_connect(service_object,
1376                 "handle-leave-network",
1377                 G_CALLBACK(on_service_leave_network), service_interface);
1378
1379         g_signal_connect(service_object,
1380                 "handle-get-network-info",
1381                 G_CALLBACK(on_service_get_network_info), service_interface);
1382
1383         g_signal_connect(service_object,
1384                 "handle-permit-join",
1385                 G_CALLBACK(on_service_permit_join), service_interface);
1386
1387         g_signal_connect(service_object,
1388                 "handle-leave-request",
1389                 G_CALLBACK(on_service_leave_request), service_interface);
1390
1391         g_signal_connect(service_object,
1392                 "handle-get-device-list",
1393                 G_CALLBACK(on_service_get_device_list), service_interface);
1394
1395         g_signal_connect(service_object,
1396                 "handle-get-mac",
1397                 G_CALLBACK(on_service_get_mac), service_interface);
1398
1399         g_signal_connect(service_object,
1400                 "handle-get-device-info",
1401                 G_CALLBACK(on_service_get_device_info), service_interface);
1402
1403         g_signal_connect(service_object,
1404                 "handle-get-endpoint-list",
1405                 G_CALLBACK(on_service_get_endpoint_list), service_interface);
1406
1407         g_signal_connect(service_object,
1408                 "handle-get-cluster-list",
1409                 G_CALLBACK(on_service_get_cluster_list), service_interface);
1410
1411         g_signal_connect(service_object,
1412                 "handle-get-node-type",
1413                 G_CALLBACK(on_service_get_node_type), service_interface);
1414
1415         g_signal_connect(service_object,
1416                 "handle-check-privilege",
1417                 G_CALLBACK(on_service_check_privilege), service_interface);
1418
1419         return TRUE;
1420 }