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