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