Fix to follow coding convention
[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         g_free(cb_data);
455 }
456
457 static gboolean on_service_get_network_info(ZigbeeService *service_object,
458         GDBusMethodInvocation *invocation,
459         gpointer user_data)
460 {
461         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
462         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
463
464         gboolean ret;
465
466         /* Allocate response callback data */
467         resp_cb_data =
468                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
469                         invocation, NULL, 0);
470         if (NULL == resp_cb_data) {
471                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
472
473                 /* Send failure response */
474                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
475
476                 return TRUE;
477         }
478
479         /* Dispatch request */
480         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
481                 ZBLIB_DRIVER_TYPE_SERVICE,
482                 ZBLIB_SERVICE_OPS_GET_NETWORK_INFO,
483                 NULL, 0,
484                 on_service_get_network_info_resp, resp_cb_data);
485         if (FALSE == ret) {
486                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
487
488                 /* Free response callback data */
489                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
490
491                 /* Send failure response */
492                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
493
494                 return TRUE;
495         }
496
497         return TRUE;
498 }
499
500 static void on_service_permit_join_resp(ZigBeeServiceInterface *service_interface,
501         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
502 {
503         ZigbeeServiceInterfaceRespCbData_t *cb_data =
504                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
505
506         ZigbeeService *service_object;
507         GDBusMethodInvocation *invocation;
508         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
509
510         NOT_USED(service_interface);
511         NOT_USED(request_id);
512
513         if (NULL == resp_data || 0 == resp_data_len) {
514                 Z_LOGE("resp_data is null");
515                 g_free(cb_data);
516                 return;
517         }
518
519         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
520         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
521
522         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
523         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
524
525         zigbee_service_complete_permit_join(service_object, invocation, payload->result);
526
527         g_free(cb_data);
528 }
529
530 static gboolean on_service_permit_join(ZigbeeService *service_object,
531         GDBusMethodInvocation *invocation,
532         gint duration,
533         gboolean permit_join,
534         gpointer user_data)
535 {
536         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
537         ZigbeeServicePermitJoin_t req;
538         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
539
540         gboolean ret;
541
542         memset(&req, 0x0, sizeof(ZigbeeServicePermitJoin_t));
543
544         /* Update request structure */
545         req.duration = duration;
546         req.permit_join = permit_join;
547
548         /* Allocate response callback data */
549         resp_cb_data =
550                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
551                         invocation, NULL, 0);
552         if (NULL == resp_cb_data) {
553                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
554
555                 /* Send failure response */
556                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
557
558                 return TRUE;
559         }
560
561         /* Dispatch request */
562         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
563                 ZBLIB_DRIVER_TYPE_SERVICE,
564                 ZBLIB_SERVICE_OPS_PERMIT_JOIN,
565                 &req, sizeof(req),
566                 on_service_permit_join_resp, resp_cb_data);
567         if (FALSE == ret) {
568                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
569
570                 /* Free response callback data */
571                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
572
573                 /* Send failure response */
574                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
575
576                 return TRUE;
577         }
578
579         return TRUE;
580 }
581
582 static void on_service_leave_request_resp(ZigBeeServiceInterface *service_interface,
583         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
584 {
585         ZigbeeServiceInterfaceRespCbData_t *cb_data =
586                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
587
588         ZigbeeService *service_object;
589         GDBusMethodInvocation *invocation;
590         ZigbeeGeneralResp_t *payload = (ZigbeeGeneralResp_t*)resp_data;
591
592         NOT_USED(service_interface);
593         NOT_USED(request_id);
594
595         if (NULL == resp_data || 0 == resp_data_len) {
596                 Z_LOGE("resp_data is null");
597                 g_free(cb_data);
598                 return;
599         }
600
601         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
602         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
603
604         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
605         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
606
607         zigbee_service_complete_leave_request(service_object, invocation, payload->result);
608
609         g_free(cb_data);
610 }
611
612 static gboolean on_service_leave_request(ZigbeeService *service_object,
613         GDBusMethodInvocation *invocation,
614         GVariant *eui64,
615         gchar remove_child,
616         gchar rejoin,
617         gpointer user_data)
618 {
619         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
620         ZigbeeServiceLeaveRequest_t req;
621         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
622
623         GVariantIter *iter = NULL;
624         guint i = 0;
625
626         gboolean ret;
627
628         memset(&req, 0x0, sizeof(ZigbeeServiceLeaveRequest_t));
629
630         /* Update request structure */
631         g_variant_get(eui64, "a(y)", &iter);
632         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
633                 i++;
634                 if (i >= ZIGBEE_EUI64_SIZE)
635                         break;
636         }
637         req.remove_child = remove_child;
638         req.rejoin = rejoin;
639
640         Z_LOGD("IEEE Address = %X:%X:%X:%X:%X:%X:%X:%X",
641                 req.eui64[0], req.eui64[1], req.eui64[2], req.eui64[3],
642                 req.eui64[4], req.eui64[5], req.eui64[6], req.eui64[7]);
643         Z_LOGD("remove_child [%d]", remove_child);
644         Z_LOGD("rejoin [%d]", rejoin);
645
646         /* Allocate response callback data */
647         resp_cb_data =
648                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
649                         invocation, NULL, 0);
650         if (NULL == resp_cb_data) {
651                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
652
653                 /* Send failure response */
654                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
655
656                 return TRUE;
657         }
658
659         /* Dispatch request */
660         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
661                 ZBLIB_DRIVER_TYPE_SERVICE,
662                 ZBLIB_SERVICE_OPS_LEAVE_REQUEST,
663                 &req, sizeof(req),
664                 on_service_leave_request_resp, resp_cb_data);
665         if (FALSE == ret) {
666                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
667
668                 /* Free response callback data */
669                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
670
671                 /* Send failure response */
672                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
673
674                 return TRUE;
675         }
676
677         return TRUE;
678 }
679
680 static void on_service_get_device_list_resp(ZigBeeServiceInterface *service_interface,
681         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
682 {
683         ZigbeeServiceInterfaceRespCbData_t *cb_data =
684                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
685
686         ZigbeeService *service_object;
687         GDBusMethodInvocation *invocation;
688
689         NOT_USED(service_interface);
690         NOT_USED(request_id);
691
692         if (NULL == resp_data || 0 == resp_data_len) {
693                 Z_LOGE("resp_data is null");
694                 g_free(cb_data);
695                 return;
696         }
697
698         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
699         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
700
701         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
702         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
703
704         /* TODO : zigbee_service_complete_get_device_list */
705         /* zigbee_service_complete_get_device_list(service_object, invocation, resp_data); */
706
707         g_free(cb_data);
708 }
709
710 static gboolean on_service_get_device_list(ZigbeeService *service_object,
711         GDBusMethodInvocation *invocation,
712         gpointer user_data)
713 {
714         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
715         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
716
717         gboolean ret;
718
719         /* Allocate response callback data */
720         resp_cb_data =
721                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
722                         invocation, NULL, 0);
723         if (NULL == resp_cb_data) {
724                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
725
726                 /* Send failure response */
727                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
728
729                 return TRUE;
730         }
731
732         /* Dispatch request */
733         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
734                 ZBLIB_DRIVER_TYPE_SERVICE,
735                 ZBLIB_SERVICE_OPS_GET_DEVICE_LIST,
736                 NULL, 0,
737                 on_service_get_device_list_resp, resp_cb_data);
738         if (FALSE == ret) {
739                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
740
741                 /* Free response callback data */
742                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
743
744                 /* Send failure response */
745                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
746
747                 return TRUE;
748         }
749
750         return TRUE;
751 }
752
753 static void on_service_get_mac_resp(ZigBeeServiceInterface *service_interface,
754         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
755 {
756         ZigbeeServiceInterfaceRespCbData_t *cb_data =
757                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
758
759         ZigbeeService *service_object;
760         GDBusMethodInvocation *invocation;
761         ZigbeeServiceGetMacResp_t *payload =
762                 (ZigbeeServiceGetMacResp_t*)resp_data;
763
764         GVariant *v_eui64 = NULL;
765
766         NOT_USED(service_interface);
767         NOT_USED(request_id);
768
769         if (NULL == resp_data || 0 == resp_data_len) {
770                 Z_LOGE("resp_data is null");
771                 g_free(cb_data);
772                 return;
773         }
774
775         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
776         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
777
778         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
779         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
780
781         v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
782                 payload->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
783
784         zigbee_service_complete_get_mac(service_object, invocation, payload->result,
785                 v_eui64);
786
787         g_free(cb_data);
788 }
789
790 static gboolean on_service_get_mac(ZigbeeService *service_object,
791         GDBusMethodInvocation *invocation,
792         gpointer user_data)
793 {
794         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
795         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
796
797         gboolean ret;
798
799         /* Allocate response callback data */
800         resp_cb_data =
801                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
802                         invocation, NULL, 0);
803         if (NULL == resp_cb_data) {
804                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
805
806                 /* Send failure response */
807                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
808
809                 return TRUE;
810         }
811
812         /* Dispatch request */
813         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
814                 ZBLIB_DRIVER_TYPE_SERVICE,
815                 ZBLIB_SERVICE_OPS_GET_MAC,
816                 NULL, 0,
817                 on_service_get_mac_resp, resp_cb_data);
818         if (FALSE == ret) {
819                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
820
821                 /* Free response callback data */
822                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
823
824                 /* Send failure response */
825                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
826
827                 return TRUE;
828         }
829
830         return TRUE;
831 }
832
833 static void on_service_get_device_info_resp(ZigBeeServiceInterface *service_interface,
834         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
835 {
836         ZigbeeServiceInterfaceRespCbData_t *cb_data =
837                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
838
839         ZigbeeService *service_object;
840         GDBusMethodInvocation *invocation;
841         ZigbeeServiceGetDeviceInfoResp_t *payload =
842                 (ZigbeeServiceGetDeviceInfoResp_t*)resp_data;
843
844         GVariant *variant = NULL;
845         GVariantBuilder* builder = NULL;
846         int i = 0;
847         int index = 0;
848
849         NOT_USED(service_interface);
850         NOT_USED(request_id);
851
852         if (NULL == resp_data || 0 == resp_data_len) {
853                 Z_LOGE("resp_data is null");
854                 g_free(cb_data);
855                 return;
856         }
857
858         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
859         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
860
861         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
862         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
863
864         builder = g_variant_builder_new(G_VARIANT_TYPE("a(qyayyay)"));
865         for (index = 0; index < payload->count; index++) {
866                 ZigbeeServiceGetDeviceInfoDetailResp_t *device = &(payload->list[index]);
867                 GVariantBuilder* mac_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
868                 GVariantBuilder* endpoint_builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
869
870                 for (i = 0; i < ZIGBEE_EUI64_SIZE; i++)
871                         g_variant_builder_add(mac_builder, "y", device->eui64[i]);
872                 for (i = 0; i < device->endpoint_count; i++)
873                         g_variant_builder_add(endpoint_builder, "y", device->endpoints[i]);
874
875                 g_variant_builder_add(builder, "(qyayyay)",
876                         device->node_id, device->node_type,
877                         mac_builder, device->endpoint_count, endpoint_builder);
878
879                 g_variant_builder_unref(mac_builder);
880                 g_variant_builder_unref(endpoint_builder);
881         }
882
883         variant = g_variant_builder_end(builder);
884         zigbee_service_complete_get_device_info(service_object, invocation,
885                 payload->result, variant);
886
887         g_free(cb_data);
888 }
889
890 static gboolean on_service_get_device_info(ZigbeeService *service_object,
891         GDBusMethodInvocation *invocation,
892         gpointer user_data)
893 {
894         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
895         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
896
897         gboolean ret;
898
899         /* Allocate response callback data */
900         resp_cb_data =
901                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
902                         invocation, NULL, 0);
903         if (NULL == resp_cb_data) {
904                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
905
906                 /* Send failure response */
907                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
908
909                 return TRUE;
910         }
911
912         /* Dispatch request */
913         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
914                 ZBLIB_DRIVER_TYPE_SERVICE,
915                 ZBLIB_SERVICE_OPS_GET_DEVICE_INFO,
916                 NULL, 0,
917                 on_service_get_device_info_resp, resp_cb_data);
918         if (FALSE == ret) {
919                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
920
921                 /* Free response callback data */
922                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
923
924                 /* Send failure response */
925                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
926
927                 return TRUE;
928         }
929
930         return TRUE;
931 }
932
933 static void on_service_get_endpoint_list_resp(ZigBeeServiceInterface *service_interface,
934         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
935 {
936         ZigbeeServiceInterfaceRespCbData_t *cb_data =
937                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
938
939         ZigbeeService *service_object;
940         GDBusMethodInvocation *invocation;
941         ZigbeeServiceGetEndpointListResp_t *payload =
942                 (ZigbeeServiceGetEndpointListResp_t*)resp_data;
943         GVariant *v_endpoints = NULL;
944
945         NOT_USED(service_interface);
946         NOT_USED(request_id);
947
948         if (NULL == resp_data || 0 == resp_data_len) {
949                 Z_LOGE("resp_data is null");
950                 g_free(cb_data);
951                 return;
952         }
953
954         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
955         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
956
957         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
958         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
959
960         v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
961                 payload->endpoints, payload->endpoint_count, TRUE, NULL, NULL);
962
963         zigbee_service_complete_get_endpoint_list(service_object, invocation,
964                 payload->result, v_endpoints);
965
966         g_free(cb_data);
967 }
968
969 static gboolean on_service_get_endpoint_list(ZigbeeService *service_object,
970         GDBusMethodInvocation *invocation,
971         GVariant *eui64,
972         gpointer user_data)
973 {
974         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
975         ZigbeeServiceGetEndpointList_t req;
976         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
977
978         GVariantIter *iter = NULL;
979         guint i = 0;
980
981         gboolean ret;
982
983         memset(&req, 0x0, sizeof(ZigbeeServiceGetEndpointList_t));
984
985         /* Update request structure */
986         g_variant_get(eui64, "a(y)", &iter);
987         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
988                 i++;
989                 if (i >= ZIGBEE_EUI64_SIZE)
990                         break;
991         }
992
993         /* Allocate response callback data */
994         resp_cb_data =
995                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
996                         invocation, NULL, 0);
997         if (NULL == resp_cb_data) {
998                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
999
1000                 /* Send failure response */
1001                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1002
1003                 return TRUE;
1004         }
1005
1006         /* Dispatch request */
1007         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1008                 ZBLIB_DRIVER_TYPE_SERVICE,
1009                 ZBLIB_SERVICE_OPS_GET_ENDPOINT_LIST,
1010                 &req, sizeof(req),
1011                 on_service_get_endpoint_list_resp, resp_cb_data);
1012         if (FALSE == ret) {
1013                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1014
1015                 /* Free response callback data */
1016                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1017
1018                 /* Send failure response */
1019                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1020
1021                 return TRUE;
1022         }
1023
1024         return TRUE;
1025 }
1026
1027 static void on_service_get_cluster_list_resp(ZigBeeServiceInterface *service_interface,
1028         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1029 {
1030         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1031                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1032
1033         ZigbeeService *service_object;
1034         GDBusMethodInvocation *invocation;
1035         ZigbeeServiceGetClusterListResp_t *payload =
1036                 (ZigbeeServiceGetClusterListResp_t*)resp_data;
1037         GVariant *v_in_clusters = NULL;
1038         GVariant *v_out_clusters = NULL;
1039
1040         NOT_USED(service_interface);
1041         NOT_USED(request_id);
1042
1043         if (NULL == resp_data || 0 == resp_data_len) {
1044                 Z_LOGE("resp_data is null");
1045                 g_free(cb_data);
1046                 return;
1047         }
1048
1049         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1050         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1051
1052         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1053         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1054
1055         v_in_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1056                 payload->in_clusters, (payload->in_cluster_count * sizeof(unsigned short)),
1057                 TRUE, NULL, NULL);
1058         v_out_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1059                 payload->out_clusters, (payload->out_cluster_count * sizeof(unsigned short)),
1060                 TRUE, NULL, NULL);
1061
1062         zigbee_service_complete_get_cluster_list(service_object, invocation,
1063                 payload->result, v_in_clusters, v_out_clusters);
1064
1065         g_free(cb_data);
1066 }
1067
1068 static gboolean on_service_get_cluster_list(ZigbeeService *service_object,
1069         GDBusMethodInvocation *invocation,
1070         GVariant *eui64,
1071         gchar endpoint,
1072         gpointer user_data)
1073 {
1074         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1075         ZigbeeServiceGetClusterList_t req;
1076         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1077
1078         GVariantIter *iter = NULL;
1079         guint i = 0;
1080
1081         gboolean ret;
1082
1083         memset(&req, 0x0, sizeof(ZigbeeServiceGetClusterList_t));
1084
1085         /* Update request structure */
1086         g_variant_get(eui64, "a(y)", &iter);
1087         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
1088                 i++;
1089                 if (i >= ZIGBEE_EUI64_SIZE)
1090                         break;
1091         }
1092         req.endpoint = endpoint;
1093
1094         /* Allocate response callback data */
1095         resp_cb_data =
1096                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1097                         invocation, NULL, 0);
1098         if (NULL == resp_cb_data) {
1099                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1100
1101                 /* Send failure response */
1102                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1103
1104                 return TRUE;
1105         }
1106
1107         /* Dispatch request */
1108         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1109                 ZBLIB_DRIVER_TYPE_SERVICE,
1110                 ZBLIB_SERVICE_OPS_GET_CLUSTER_LIST,
1111                 &req, sizeof(req),
1112                 on_service_get_cluster_list_resp, resp_cb_data);
1113         if (FALSE == ret) {
1114                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1115
1116                 /* Free response callback data */
1117                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1118
1119                 /* Send failure response */
1120                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1121
1122                 return TRUE;
1123         }
1124
1125         return TRUE;
1126 }
1127
1128 static void on_service_get_node_type_resp(ZigBeeServiceInterface *service_interface,
1129         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1130 {
1131         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1132                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1133
1134         ZigbeeService *service_object;
1135         GDBusMethodInvocation *invocation;
1136         ZigbeeServiceServiceNodeTypeResp_t *payload =
1137                 (ZigbeeServiceServiceNodeTypeResp_t *)resp_data;
1138
1139         NOT_USED(service_interface);
1140         NOT_USED(request_id);
1141
1142         if (NULL == resp_data || 0 == resp_data_len) {
1143                 Z_LOGE("resp_data is null");
1144                 g_free(cb_data);
1145                 return;
1146         }
1147
1148         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1149         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1150
1151         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1152         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1153
1154         zigbee_service_complete_get_node_type(service_object, invocation,
1155                 payload->result, payload->node_type);
1156
1157         g_free(cb_data);
1158 }
1159
1160 static gboolean on_service_get_node_type(ZigbeeService *service_object,
1161         GDBusMethodInvocation *invocation,
1162         GVariant *eui64,
1163         gpointer user_data)
1164 {
1165         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1166         ZigbeeServiceGetNodeType_t req;
1167         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1168
1169         GVariantIter *iter = NULL;
1170         guint i = 0;
1171
1172         gboolean ret;
1173
1174         memset(&req, 0x0, sizeof(ZigbeeServiceGetNodeType_t));
1175
1176         /* Update request structure */
1177         g_variant_get(eui64, "a(y)", &iter);
1178         while (g_variant_iter_loop(iter, "(y)", req.eui64[i])) {
1179                 i++;
1180                 if (i >= ZIGBEE_EUI64_SIZE)
1181                         break;
1182         }
1183
1184         /* Allocate response callback data */
1185         resp_cb_data =
1186                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1187                         invocation, NULL, 0);
1188         if (NULL == resp_cb_data) {
1189                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1190
1191                 /* Send failure response */
1192                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1193
1194                 return TRUE;
1195         }
1196
1197         /* Dispatch request */
1198         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1199                 ZBLIB_DRIVER_TYPE_SERVICE,
1200                 ZBLIB_SERVICE_OPS_GET_NODE_TYPE,
1201                 &req, sizeof(req),
1202                 on_service_get_node_type_resp, resp_cb_data);
1203         if (FALSE == ret) {
1204                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1205
1206                 /* Free response callback data */
1207                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1208
1209                 /* Send failure response */
1210                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1211
1212                 return TRUE;
1213         }
1214
1215         return TRUE;
1216 }
1217
1218 static gboolean on_service_check_privilege(ZigbeeService *service_object,
1219         GDBusMethodInvocation *invocation,
1220         gpointer user_data)
1221 {
1222         NOT_USED(user_data);
1223
1224         zigbee_service_complete_check_privilege(service_object, invocation);
1225
1226         return TRUE;
1227 }
1228
1229 void zigbee_service_dbus_interface_service_notification(ZigBeeServiceInterface *service_interface,
1230         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
1231 {
1232         ZigbeeService *service_object;
1233
1234         zblib_check_null_ret("service_interface", service_interface);
1235
1236         if (NULL == noti_data || 0 == noti_data_len) {
1237                 Z_LOGE("noti_data is NULL");
1238                 return;
1239         }
1240
1241         service_object = _service_interface_ref_zigbee_service(service_interface);
1242         zblib_check_null_ret("service_object", service_object);
1243
1244         NOT_USED(noti_cb_data);
1245
1246         switch (noti_id) {
1247         case ZBLIB_SERVICE_NOTI_FORM_NETWORK_DONE: {
1248                 ZigbeeServiceServiceFormNetworkDone_t *panid_t =
1249                         (ZigbeeServiceServiceFormNetworkDone_t*)noti_data;
1250
1251                 Z_LOGD("form_network_done : [0x%X]", panid_t->pan_id);
1252
1253                 zigbee_service_emit_form_network_done(service_object, panid_t->pan_id);
1254         }
1255         break;
1256         case ZBLIB_SERVICE_NOTI_CHILD_JOINED: {
1257                 ZigbeeServiceServiceChildJoined_t *child_t =
1258                         (ZigbeeServiceServiceChildJoined_t*)noti_data;
1259
1260                 GVariant *v_eui64 = NULL;
1261                 GVariant *v_endpoints = NULL;
1262
1263                 v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1264                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1265                 v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1266                         child_t->endpoints, child_t->endpoint_count, TRUE, NULL, NULL);
1267                 if (NULL == v_eui64 || NULL == v_endpoints) {
1268                         Z_LOGE("Failed to create variant!");
1269                         if (v_eui64)
1270                                 g_object_unref(v_eui64);
1271                         if (v_endpoints)
1272                                 g_object_unref(v_endpoints);
1273                 } else {
1274                         zigbee_service_emit_child_joined(service_object,
1275                                 v_eui64, child_t->endpoint_count, v_endpoints, child_t->node_id);
1276                 }
1277         }
1278         break;
1279         case ZBLIB_SERVICE_NOTI_CHILD_REJOINED: {
1280                 ZigbeeServiceServiceChildRejoined_t *child_t =
1281                         (ZigbeeServiceServiceChildRejoined_t*)noti_data;
1282
1283                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1284                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1285                 if (NULL == v_eui64)
1286                         Z_LOGE("Failed to create variant!");
1287                 else
1288                         zigbee_service_emit_child_rejoined(service_object, v_eui64);
1289         }
1290         break;
1291         case ZBLIB_SERVICE_NOTI_CHILD_LEFT: {
1292                 ZigbeeServiceServiceChildLeft_t *child_left_t =
1293                         (ZigbeeServiceServiceChildLeft_t*)noti_data;
1294
1295                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1296                         child_left_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1297                 if (NULL == v_eui64)
1298                         Z_LOGE("Failed to create variant!");
1299                 else {
1300                         zigbee_service_emit_child_left(service_object,
1301                                 v_eui64, child_left_t->status);
1302                 }
1303         }
1304         break;
1305         case ZBLIB_SERVICE_NOTI_LEAVE_NETWORK_DONE: {
1306                 ZigbeeServiceServiceLeaveNetworkDone_t leave_net_t;
1307                 memcpy(&leave_net_t, noti_data, noti_data_len);
1308                 zigbee_service_emit_leave_network_done(service_object, leave_net_t.pan_id);
1309         }
1310         break;
1311         default:
1312                 Z_LOGE("Unexpected notification [%x]", noti_id);
1313         break;
1314         }
1315
1316         /* ZigbeeService should be dereferenced */
1317         g_object_unref(service_object);
1318 }
1319
1320 gboolean zigbee_service_dbus_interface_service_init(ZigBeeServiceInterface *service_interface,
1321         ZigbeeObjectSkeleton *zigbee_object)
1322 {
1323         ZigbeeService *service_object;
1324
1325         if (NULL == service_interface) {
1326                 Z_LOGE("service_interface is NULL");
1327                 return FALSE;
1328         }
1329
1330         service_object = zigbee_service_skeleton_new();
1331         zigbee_object_skeleton_set_service(zigbee_object, service_object);
1332         g_object_unref(service_object);
1333
1334         Z_LOGI("service_object: [%p]", service_object);
1335
1336         /*
1337          * Register signal handlers for 'service' interface
1338          */
1339         g_signal_connect(service_object,
1340                 "handle-zb-hw-reset",
1341                 G_CALLBACK(on_service_zb_hw_reset), service_interface);
1342
1343         g_signal_connect(service_object,
1344                 "handle-form-network",
1345                 G_CALLBACK(on_service_form_network), service_interface);
1346
1347         g_signal_connect(service_object,
1348                 "handle-coex-start",
1349                 G_CALLBACK(on_service_coex_start), service_interface);
1350
1351         g_signal_connect(service_object,
1352                 "handle-coex-stop",
1353                 G_CALLBACK(on_service_coex_stop), service_interface);
1354
1355         g_signal_connect(service_object,
1356                 "handle-leave-network",
1357                 G_CALLBACK(on_service_leave_network), service_interface);
1358
1359         g_signal_connect(service_object,
1360                 "handle-get-network-info",
1361                 G_CALLBACK(on_service_get_network_info), service_interface);
1362
1363         g_signal_connect(service_object,
1364                 "handle-permit-join",
1365                 G_CALLBACK(on_service_permit_join), service_interface);
1366
1367         g_signal_connect(service_object,
1368                 "handle-leave-request",
1369                 G_CALLBACK(on_service_leave_request), service_interface);
1370
1371         g_signal_connect(service_object,
1372                 "handle-get-device-list",
1373                 G_CALLBACK(on_service_get_device_list), service_interface);
1374
1375         g_signal_connect(service_object,
1376                 "handle-get-mac",
1377                 G_CALLBACK(on_service_get_mac), service_interface);
1378
1379         g_signal_connect(service_object,
1380                 "handle-get-device-info",
1381                 G_CALLBACK(on_service_get_device_info), service_interface);
1382
1383         g_signal_connect(service_object,
1384                 "handle-get-endpoint-list",
1385                 G_CALLBACK(on_service_get_endpoint_list), service_interface);
1386
1387         g_signal_connect(service_object,
1388                 "handle-get-cluster-list",
1389                 G_CALLBACK(on_service_get_cluster_list), service_interface);
1390
1391         g_signal_connect(service_object,
1392                 "handle-get-node-type",
1393                 G_CALLBACK(on_service_get_node_type), service_interface);
1394
1395         g_signal_connect(service_object,
1396                 "handle-check-privilege",
1397                 G_CALLBACK(on_service_check_privilege), service_interface);
1398
1399         return TRUE;
1400 }