Increase line & function coverage
[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
897         g_free(cb_data);
898 }
899
900 static gboolean on_service_get_device_info(ZigbeeService *service_object,
901         GDBusMethodInvocation *invocation,
902         gpointer user_data)
903 {
904         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
905         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
906
907         gboolean ret;
908
909         /* Allocate response callback data */
910         resp_cb_data =
911                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
912                         invocation, NULL, 0);
913         if (NULL == resp_cb_data) {
914                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
915
916                 /* Send failure response */
917                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
918
919                 return TRUE;
920         }
921
922         /* Dispatch request */
923         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
924                 ZBLIB_DRIVER_TYPE_SERVICE,
925                 ZBLIB_SERVICE_OPS_GET_DEVICE_INFO,
926                 NULL, 0,
927                 on_service_get_device_info_resp, resp_cb_data);
928         if (FALSE == ret) {
929                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
930
931                 /* Free response callback data */
932                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
933
934                 /* Send failure response */
935                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
936
937                 return TRUE;
938         }
939
940         return TRUE;
941 }
942
943 static void on_service_get_endpoint_list_resp(ZigBeeServiceInterface *service_interface,
944         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
945 {
946         ZigbeeServiceInterfaceRespCbData_t *cb_data =
947                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
948
949         ZigbeeService *service_object;
950         GDBusMethodInvocation *invocation;
951         ZigbeeServiceGetEndpointListResp_t *payload =
952                 (ZigbeeServiceGetEndpointListResp_t*)resp_data;
953         GVariant *v_endpoints = NULL;
954
955         NOT_USED(service_interface);
956         NOT_USED(request_id);
957
958         if (NULL == resp_data || 0 == resp_data_len) {
959                 Z_LOGE("resp_data is null");
960                 g_free(cb_data);
961                 return;
962         }
963
964         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
965         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
966
967         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
968         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
969
970         v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
971                 payload->endpoints, payload->endpoint_count, TRUE, NULL, NULL);
972
973         zigbee_service_complete_get_endpoint_list(service_object, invocation,
974                 payload->result, v_endpoints);
975
976         if (v_endpoints)
977                 g_variant_unref(v_endpoints);
978
979         g_free(cb_data);
980 }
981
982 static gboolean on_service_get_endpoint_list(ZigbeeService *service_object,
983         GDBusMethodInvocation *invocation,
984         GVariant *eui64,
985         gpointer user_data)
986 {
987         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
988         ZigbeeServiceGetEndpointList_t req;
989         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
990
991         GVariantIter *iter = NULL;
992         guint i = 0;
993
994         gboolean ret;
995
996         memset(&req, 0x0, sizeof(ZigbeeServiceGetEndpointList_t));
997
998         /* Update request structure */
999         g_variant_get(eui64, "a(y)", &iter);
1000         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
1001                 i++;
1002                 if (i >= ZIGBEE_EUI64_SIZE)
1003                         break;
1004         }
1005
1006         /* Allocate response callback data */
1007         resp_cb_data =
1008                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1009                         invocation, NULL, 0);
1010         if (NULL == resp_cb_data) {
1011                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1012
1013                 /* Send failure response */
1014                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1015
1016                 return TRUE;
1017         }
1018
1019         /* Dispatch request */
1020         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1021                 ZBLIB_DRIVER_TYPE_SERVICE,
1022                 ZBLIB_SERVICE_OPS_GET_ENDPOINT_LIST,
1023                 &req, sizeof(req),
1024                 on_service_get_endpoint_list_resp, resp_cb_data);
1025         if (FALSE == ret) {
1026                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1027
1028                 /* Free response callback data */
1029                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1030
1031                 /* Send failure response */
1032                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1033
1034                 return TRUE;
1035         }
1036
1037         return TRUE;
1038 }
1039
1040 static void on_service_get_cluster_list_resp(ZigBeeServiceInterface *service_interface,
1041         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1042 {
1043         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1044                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1045
1046         ZigbeeService *service_object;
1047         GDBusMethodInvocation *invocation;
1048         ZigbeeServiceGetClusterListResp_t *payload =
1049                 (ZigbeeServiceGetClusterListResp_t*)resp_data;
1050         GVariant *v_in_clusters = NULL;
1051         GVariant *v_out_clusters = NULL;
1052
1053         NOT_USED(service_interface);
1054         NOT_USED(request_id);
1055
1056         if (NULL == resp_data || 0 == resp_data_len) {
1057                 Z_LOGE("resp_data is null");
1058                 g_free(cb_data);
1059                 return;
1060         }
1061
1062         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1063         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1064
1065         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1066         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1067
1068         v_in_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1069                 payload->in_clusters, (payload->in_cluster_count * sizeof(unsigned short)),
1070                 TRUE, NULL, NULL);
1071         v_out_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1072                 payload->out_clusters, (payload->out_cluster_count * sizeof(unsigned short)),
1073                 TRUE, NULL, NULL);
1074
1075         zigbee_service_complete_get_cluster_list(service_object, invocation,
1076                 payload->result, v_in_clusters, v_out_clusters);
1077
1078         if (v_in_clusters)
1079                 g_variant_unref(v_in_clusters);
1080         if (v_out_clusters)
1081                 g_variant_unref(v_out_clusters);
1082
1083         g_free(cb_data);
1084 }
1085
1086 static gboolean on_service_get_cluster_list(ZigbeeService *service_object,
1087         GDBusMethodInvocation *invocation,
1088         GVariant *eui64,
1089         gchar endpoint,
1090         gpointer user_data)
1091 {
1092         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1093         ZigbeeServiceGetClusterList_t req;
1094         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1095
1096         GVariantIter *iter = NULL;
1097         guint i = 0;
1098
1099         gboolean ret;
1100
1101         memset(&req, 0x0, sizeof(ZigbeeServiceGetClusterList_t));
1102
1103         /* Update request structure */
1104         g_variant_get(eui64, "a(y)", &iter);
1105         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
1106                 i++;
1107                 if (i >= ZIGBEE_EUI64_SIZE)
1108                         break;
1109         }
1110         req.endpoint = endpoint;
1111
1112         /* Allocate response callback data */
1113         resp_cb_data =
1114                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1115                         invocation, NULL, 0);
1116         if (NULL == resp_cb_data) {
1117                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1118
1119                 /* Send failure response */
1120                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1121
1122                 return TRUE;
1123         }
1124
1125         /* Dispatch request */
1126         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1127                 ZBLIB_DRIVER_TYPE_SERVICE,
1128                 ZBLIB_SERVICE_OPS_GET_CLUSTER_LIST,
1129                 &req, sizeof(req),
1130                 on_service_get_cluster_list_resp, resp_cb_data);
1131         if (FALSE == ret) {
1132                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1133
1134                 /* Free response callback data */
1135                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1136
1137                 /* Send failure response */
1138                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1139
1140                 return TRUE;
1141         }
1142
1143         return TRUE;
1144 }
1145
1146 static void on_service_get_node_type_resp(ZigBeeServiceInterface *service_interface,
1147         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1148 {
1149         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1150                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1151
1152         ZigbeeService *service_object;
1153         GDBusMethodInvocation *invocation;
1154         ZigbeeServiceServiceNodeTypeResp_t *payload =
1155                 (ZigbeeServiceServiceNodeTypeResp_t *)resp_data;
1156
1157         NOT_USED(service_interface);
1158         NOT_USED(request_id);
1159
1160         if (NULL == resp_data || 0 == resp_data_len) {
1161                 Z_LOGE("resp_data is null");
1162                 g_free(cb_data);
1163                 return;
1164         }
1165
1166         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1167         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1168
1169         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1170         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1171
1172         zigbee_service_complete_get_node_type(service_object, invocation,
1173                 payload->result, payload->node_type);
1174
1175         g_free(cb_data);
1176 }
1177
1178 static gboolean on_service_get_node_type(ZigbeeService *service_object,
1179         GDBusMethodInvocation *invocation,
1180         GVariant *eui64,
1181         gpointer user_data)
1182 {
1183         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1184         ZigbeeServiceGetNodeType_t req;
1185         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1186
1187         GVariantIter *iter = NULL;
1188         guint i = 0;
1189
1190         gboolean ret;
1191
1192         memset(&req, 0x0, sizeof(ZigbeeServiceGetNodeType_t));
1193
1194         /* Update request structure */
1195         g_variant_get(eui64, "a(y)", &iter);
1196         while (g_variant_iter_loop(iter, "(y)", req.eui64[i])) {
1197                 i++;
1198                 if (i >= ZIGBEE_EUI64_SIZE)
1199                         break;
1200         }
1201
1202         /* Allocate response callback data */
1203         resp_cb_data =
1204                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1205                         invocation, NULL, 0);
1206         if (NULL == resp_cb_data) {
1207                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1208
1209                 /* Send failure response */
1210                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1211
1212                 return TRUE;
1213         }
1214
1215         /* Dispatch request */
1216         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1217                 ZBLIB_DRIVER_TYPE_SERVICE,
1218                 ZBLIB_SERVICE_OPS_GET_NODE_TYPE,
1219                 &req, sizeof(req),
1220                 on_service_get_node_type_resp, resp_cb_data);
1221         if (FALSE == ret) {
1222                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1223
1224                 /* Free response callback data */
1225                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1226
1227                 /* Send failure response */
1228                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1229
1230                 return TRUE;
1231         }
1232
1233         return TRUE;
1234 }
1235
1236 static gboolean on_service_check_privilege(ZigbeeService *service_object,
1237         GDBusMethodInvocation *invocation,
1238         gpointer user_data)
1239 {
1240         NOT_USED(user_data);
1241
1242         zigbee_service_complete_check_privilege(service_object, invocation);
1243
1244         return TRUE;
1245 }
1246
1247 void zigbee_service_dbus_interface_service_notification(ZigBeeServiceInterface *service_interface,
1248         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
1249 {
1250         ZigbeeService *service_object;
1251
1252         zblib_check_null_ret("service_interface", service_interface);
1253
1254         if (NULL == noti_data || 0 == noti_data_len) {
1255                 Z_LOGE("noti_data is NULL");
1256                 return;
1257         }
1258
1259         service_object = _service_interface_ref_zigbee_service(service_interface);
1260         zblib_check_null_ret("service_object", service_object);
1261
1262         NOT_USED(noti_cb_data);
1263
1264         switch (noti_id) {
1265         case ZBLIB_SERVICE_NOTI_FORM_NETWORK_DONE: {
1266                 ZigbeeServiceServiceFormNetworkDone_t *panid_t =
1267                         (ZigbeeServiceServiceFormNetworkDone_t*)noti_data;
1268
1269                 Z_LOGD("form_network_done : [0x%X]", panid_t->pan_id);
1270
1271                 zigbee_service_emit_form_network_done(service_object, panid_t->pan_id);
1272         }
1273         break;
1274         case ZBLIB_SERVICE_NOTI_CHILD_JOINED: {
1275                 ZigbeeServiceServiceChildJoined_t *child_t =
1276                         (ZigbeeServiceServiceChildJoined_t*)noti_data;
1277
1278                 GVariant *v_eui64 = NULL;
1279                 GVariant *v_endpoints = NULL;
1280
1281                 v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1282                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1283                 v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1284                         child_t->endpoints, child_t->endpoint_count, TRUE, NULL, NULL);
1285                 if (NULL == v_eui64 || NULL == v_endpoints) {
1286                         Z_LOGE("Failed to create variant!");
1287                 } else {
1288                         zigbee_service_emit_child_joined(service_object,
1289                                 v_eui64, child_t->endpoint_count, v_endpoints, child_t->node_id);
1290                 }
1291                 if (v_eui64)
1292                         g_variant_unref(v_eui64);
1293                 if (v_endpoints)
1294                         g_variant_unref(v_endpoints);
1295         }
1296         break;
1297         case ZBLIB_SERVICE_NOTI_CHILD_REJOINED: {
1298                 ZigbeeServiceServiceChildRejoined_t *child_t =
1299                         (ZigbeeServiceServiceChildRejoined_t*)noti_data;
1300
1301                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1302                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1303                 if (NULL == v_eui64)
1304                         Z_LOGE("Failed to create variant!");
1305                 else
1306                         zigbee_service_emit_child_rejoined(service_object, v_eui64);
1307                 if (v_eui64)
1308                         g_variant_unref(v_eui64);
1309         }
1310         break;
1311         case ZBLIB_SERVICE_NOTI_CHILD_LEFT: {
1312                 ZigbeeServiceServiceChildLeft_t *child_left_t =
1313                         (ZigbeeServiceServiceChildLeft_t*)noti_data;
1314
1315                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1316                         child_left_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1317                 if (NULL == v_eui64)
1318                         Z_LOGE("Failed to create variant!");
1319                 else {
1320                         zigbee_service_emit_child_left(service_object,
1321                                 v_eui64, child_left_t->status);
1322                         g_variant_unref(v_eui64);
1323                 }
1324         }
1325         break;
1326         case ZBLIB_SERVICE_NOTI_LEAVE_NETWORK_DONE: {
1327                 ZigbeeServiceServiceLeaveNetworkDone_t leave_net_t;
1328                 memcpy(&leave_net_t, noti_data, noti_data_len);
1329                 zigbee_service_emit_leave_network_done(service_object, leave_net_t.pan_id);
1330         }
1331         break;
1332         default:
1333                 Z_LOGE("Unexpected notification [%x]", noti_id);
1334         break;
1335         }
1336
1337         /* ZigbeeService should be dereferenced */
1338         g_object_unref(service_object);
1339 }
1340 /* LCOV_EXCL_STOP */
1341
1342 gboolean zigbee_service_dbus_interface_service_init(ZigBeeServiceInterface *service_interface,
1343         ZigbeeObjectSkeleton *zigbee_object)
1344 {
1345         ZigbeeService *service_object;
1346
1347         if (NULL == service_interface) {
1348                 /* LCOV_EXCL_START */
1349                 Z_LOGE("service_interface is NULL");
1350                 return FALSE;
1351                 /* LCOV_EXCL_STOP */
1352         }
1353
1354         service_object = zigbee_service_skeleton_new();
1355         zigbee_object_skeleton_set_service(zigbee_object, service_object);
1356         g_object_unref(service_object);
1357
1358         Z_LOGI("service_object: [%p]", service_object);
1359
1360         /*
1361          * Register signal handlers for 'service' interface
1362          */
1363         g_signal_connect(service_object,
1364                 "handle-zb-hw-reset",
1365                 G_CALLBACK(on_service_zb_hw_reset), service_interface);
1366
1367         g_signal_connect(service_object,
1368                 "handle-form-network",
1369                 G_CALLBACK(on_service_form_network), service_interface);
1370
1371         g_signal_connect(service_object,
1372                 "handle-coex-start",
1373                 G_CALLBACK(on_service_coex_start), service_interface);
1374
1375         g_signal_connect(service_object,
1376                 "handle-coex-stop",
1377                 G_CALLBACK(on_service_coex_stop), service_interface);
1378
1379         g_signal_connect(service_object,
1380                 "handle-leave-network",
1381                 G_CALLBACK(on_service_leave_network), service_interface);
1382
1383         g_signal_connect(service_object,
1384                 "handle-get-network-info",
1385                 G_CALLBACK(on_service_get_network_info), service_interface);
1386
1387         g_signal_connect(service_object,
1388                 "handle-permit-join",
1389                 G_CALLBACK(on_service_permit_join), service_interface);
1390
1391         g_signal_connect(service_object,
1392                 "handle-leave-request",
1393                 G_CALLBACK(on_service_leave_request), service_interface);
1394
1395         g_signal_connect(service_object,
1396                 "handle-get-device-list",
1397                 G_CALLBACK(on_service_get_device_list), service_interface);
1398
1399         g_signal_connect(service_object,
1400                 "handle-get-mac",
1401                 G_CALLBACK(on_service_get_mac), service_interface);
1402
1403         g_signal_connect(service_object,
1404                 "handle-get-device-info",
1405                 G_CALLBACK(on_service_get_device_info), service_interface);
1406
1407         g_signal_connect(service_object,
1408                 "handle-get-endpoint-list",
1409                 G_CALLBACK(on_service_get_endpoint_list), service_interface);
1410
1411         g_signal_connect(service_object,
1412                 "handle-get-cluster-list",
1413                 G_CALLBACK(on_service_get_cluster_list), service_interface);
1414
1415         g_signal_connect(service_object,
1416                 "handle-get-node-type",
1417                 G_CALLBACK(on_service_get_node_type), service_interface);
1418
1419         g_signal_connect(service_object,
1420                 "handle-check-privilege",
1421                 G_CALLBACK(on_service_check_privilege), service_interface);
1422
1423         return TRUE;
1424 }