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