Add missing memory deallocation
[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_clusters = NULL;
1188
1189         NOT_USED(service_interface);
1190         NOT_USED(request_id);
1191
1192         if (NULL == resp_data || 0 == resp_data_len) {
1193                 Z_LOGE("resp_data is null");
1194                 g_free(cb_data);
1195                 return;
1196         }
1197
1198         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1199         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1200
1201         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1202         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1203
1204         v_clusters = g_variant_new_from_data(G_VARIANT_TYPE("aq"),
1205                 payload->clusters, payload->cluster_count, TRUE, NULL, NULL);
1206
1207         zigbee_service_complete_get_cluster_list(service_object, invocation,
1208                 payload->result, v_clusters);
1209
1210         g_free(cb_data);
1211 }
1212
1213 static gboolean on_service_get_cluster_list(ZigbeeService *service_object,
1214         GDBusMethodInvocation *invocation,
1215         GVariant *eui64,
1216         gchar endpoint,
1217         gpointer user_data)
1218 {
1219         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1220         ZigbeeServiceGetClusterList_t req;
1221         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1222
1223         GVariantIter *iter = NULL;
1224         guint i = 0;
1225
1226         gboolean ret;
1227
1228         memset(&req, 0x0, sizeof(ZigbeeServiceGetClusterList_t));
1229
1230         /* Update request structure */
1231         g_variant_get(eui64, "a(y)", &iter);
1232         while (g_variant_iter_loop(iter, "(y)", &(req.eui64[i]))) {
1233                 i++;
1234                 if (i >= ZIGBEE_EUI64_SIZE)
1235                         break;
1236         }
1237         req.endpoint = endpoint;
1238
1239         /* Allocate response callback data */
1240         resp_cb_data =
1241                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1242                         invocation, NULL, 0);
1243         if (NULL == resp_cb_data) {
1244                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1245
1246                 /* Send failure response */
1247                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1248
1249                 return TRUE;
1250         }
1251
1252         /* Dispatch request */
1253         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1254                 ZBLIB_DRIVER_TYPE_SERVICE,
1255                 ZBLIB_SERVICE_OPS_GET_CLUSTER_LIST,
1256                 &req, sizeof(req),
1257                 on_service_get_cluster_list_resp, resp_cb_data);
1258         if (FALSE == ret) {
1259                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1260
1261                 /* Free response callback data */
1262                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1263
1264                 /* Send failure response */
1265                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1266
1267                 return TRUE;
1268         }
1269
1270         return TRUE;
1271 }
1272
1273 static void on_service_get_node_type_resp(ZigBeeServiceInterface *service_interface,
1274         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1275 {
1276         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1277                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1278
1279         ZigbeeService *service_object;
1280         GDBusMethodInvocation *invocation;
1281         ZigbeeServiceServiceNodeTypeResp_t *payload =
1282                 (ZigbeeServiceServiceNodeTypeResp_t *)resp_data;
1283
1284         NOT_USED(service_interface);
1285         NOT_USED(request_id);
1286
1287         if (NULL == resp_data || 0 == resp_data_len) {
1288                 Z_LOGE("resp_data is null");
1289                 g_free(cb_data);
1290                 return;
1291         }
1292
1293         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1294         zblib_check_null_free_and_ret("service_object", service_object, cb_data);
1295
1296         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1297         zblib_check_null_free_and_ret("invocation", invocation, cb_data);
1298
1299         zigbee_service_complete_get_node_type(service_object, invocation,
1300                 payload->result, payload->node_type);
1301
1302         g_free(cb_data);
1303 }
1304
1305 static gboolean on_service_get_node_type(ZigbeeService *service_object,
1306         GDBusMethodInvocation *invocation,
1307         GVariant *eui64,
1308         gpointer user_data)
1309 {
1310         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1311         ZigbeeServiceGetNodeType_t req;
1312         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1313
1314         GVariantIter *iter = NULL;
1315         guint i = 0;
1316
1317         gboolean ret;
1318
1319         memset(&req, 0x0, sizeof(ZigbeeServiceGetNodeType_t));
1320
1321         /* Update request structure */
1322         g_variant_get(eui64, "a(y)", &iter);
1323         while (g_variant_iter_loop(iter, "(y)", req.eui64[i])) {
1324                 i++;
1325                 if (i >= ZIGBEE_EUI64_SIZE)
1326                         break;
1327         }
1328
1329         /* Allocate response callback data */
1330         resp_cb_data =
1331                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1332                         invocation, NULL, 0);
1333         if (NULL == resp_cb_data) {
1334                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1335
1336                 /* Send failure response */
1337                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1338
1339                 return TRUE;
1340         }
1341
1342         /* Dispatch request */
1343         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1344                 ZBLIB_DRIVER_TYPE_SERVICE,
1345                 ZBLIB_SERVICE_OPS_GET_NODE_TYPE,
1346                 &req, sizeof(req),
1347                 on_service_get_node_type_resp, resp_cb_data);
1348         if (FALSE == ret) {
1349                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1350
1351                 /* Free response callback data */
1352                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1353
1354                 /* Send failure response */
1355                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1356
1357                 return TRUE;
1358         }
1359
1360         return TRUE;
1361 }
1362
1363 void zigbee_service_dbus_interface_service_notification(ZigBeeServiceInterface *service_interface,
1364         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
1365 {
1366         ZigbeeService *service_object;
1367
1368         zblib_check_null_ret("service_interface", service_interface);
1369
1370         if (NULL == noti_data || 0 == noti_data_len) {
1371                 Z_LOGE("noti_data is NULL");
1372                 return;
1373         }
1374
1375         service_object = _service_interface_ref_zigbee_service(service_interface);
1376         zblib_check_null_ret("service_object", service_object);
1377
1378         NOT_USED(noti_cb_data);
1379
1380         switch(noti_id) {
1381         case ZBLIB_SERVICE_NOTI_FORM_NETWORK_DONE: {
1382                 ZigbeeServiceServiceFormNetworkDone_t *panid_t =
1383                         (ZigbeeServiceServiceFormNetworkDone_t*)noti_data;
1384
1385                 Z_LOGD("form_network_done : [0x%X]", panid_t->pan_id);
1386
1387                 zigbee_service_emit_form_network_done(service_object, panid_t->pan_id);
1388         }
1389         break;
1390         case ZBLIB_SERVICE_NOTI_CHILD_JOINED: {
1391                 ZigbeeServiceServiceChildJoined_t *child_t =
1392                         (ZigbeeServiceServiceChildJoined_t*)noti_data;
1393
1394                 GVariant *v_eui64 = NULL;
1395                 GVariant *v_endpoints = NULL;
1396
1397                 v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1398                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1399                 v_endpoints = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1400                         child_t->endpoints, child_t->endpoint_count, TRUE, NULL, NULL);
1401                 if (NULL == v_eui64 || NULL == v_endpoints) {
1402                         Z_LOGE("Failed to create variant!");
1403                         if (v_eui64)
1404                                 g_object_unref(v_eui64);
1405                         if (v_endpoints)
1406                                 g_object_unref(v_endpoints);
1407                 } else {
1408                         zigbee_service_emit_child_joined(service_object,
1409                                 v_eui64, child_t->endpoint_count, v_endpoints, child_t->node_id);
1410                 }
1411         }
1412         break;
1413         case ZBLIB_SERVICE_NOTI_CHILD_REJOINED: {
1414                 ZigbeeServiceServiceChildRejoined_t *child_t =
1415                         (ZigbeeServiceServiceChildRejoined_t*)noti_data;
1416
1417                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1418                         child_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1419                 if (NULL == v_eui64) {
1420                         Z_LOGE("Failed to create variant!");
1421                 } else {
1422                         zigbee_service_emit_child_rejoined(service_object, v_eui64);
1423                 }
1424         }
1425         break;
1426         case ZBLIB_SERVICE_NOTI_CHILD_LEFT: {
1427                 ZigbeeServiceServiceChildLeft_t *child_left_t =
1428                         (ZigbeeServiceServiceChildLeft_t*)noti_data;
1429
1430                 GVariant *v_eui64 = g_variant_new_from_data(G_VARIANT_TYPE("a(y)"),
1431                         child_left_t->eui64, ZIGBEE_EUI64_SIZE, TRUE, NULL, NULL);
1432                 if (NULL == v_eui64) {
1433                         Z_LOGE("Failed to create variant!");
1434                 } else {
1435                         zigbee_service_emit_child_left(service_object,
1436                                 v_eui64, child_left_t->status);
1437                 }
1438         }
1439         break;
1440         case ZBLIB_SERVICE_NOTI_LEAVE_NETWORK_DONE: {
1441                 ZigbeeServiceServiceLeaveNetworkDone_t leave_net_t;
1442                 memcpy(&leave_net_t, noti_data, noti_data_len);
1443                 zigbee_service_emit_leave_network_done(service_object, leave_net_t.pan_id);
1444         }
1445         break;
1446         default:
1447                 Z_LOGE("Unexpected notification [%x]", noti_id);
1448         break;
1449         }
1450
1451         /* ZigbeeService should be dereferenced */
1452         g_object_unref(service_object);
1453 }
1454
1455 gboolean zigbee_service_dbus_interface_service_init(ZigBeeServiceInterface *service_interface,
1456         ZigbeeObjectSkeleton *zigbee_object)
1457 {
1458         ZigbeeService *service_object;
1459
1460         if (NULL == service_interface) {
1461                 Z_LOGE("service_interface is NULL");
1462                 return FALSE;
1463         }
1464
1465         service_object = zigbee_service_skeleton_new();
1466         zigbee_object_skeleton_set_service(zigbee_object, service_object);
1467         g_object_unref(service_object);
1468
1469         Z_LOGI("service_object: [%p]", service_object);
1470
1471         /*
1472          * Register signal handlers for 'service' interface
1473          */
1474         g_signal_connect(service_object,
1475                 "handle-enable",
1476                 G_CALLBACK(on_service_enable), service_interface);
1477
1478         g_signal_connect(service_object,
1479                 "handle-disable",
1480                 G_CALLBACK(on_service_disable), service_interface);
1481
1482         g_signal_connect(service_object,
1483                 "handle-zb-hw-reset",
1484                 G_CALLBACK(on_service_zb_hw_reset), service_interface);
1485
1486         g_signal_connect(service_object,
1487                 "handle-form-network",
1488                 G_CALLBACK(on_service_form_network), service_interface);
1489
1490         g_signal_connect(service_object,
1491                 "handle-coex-start",
1492                 G_CALLBACK(on_service_coex_start), service_interface);
1493
1494         g_signal_connect(service_object,
1495                 "handle-coex-stop",
1496                 G_CALLBACK(on_service_coex_stop), service_interface);
1497
1498         g_signal_connect(service_object,
1499                 "handle-leave-network",
1500                 G_CALLBACK(on_service_leave_network), service_interface);
1501
1502         g_signal_connect(service_object,
1503                 "handle-get-network-info",
1504                 G_CALLBACK(on_service_get_network_info), service_interface);
1505
1506         g_signal_connect(service_object,
1507                 "handle-permit-join",
1508                 G_CALLBACK(on_service_permit_join), service_interface);
1509
1510         g_signal_connect(service_object,
1511                 "handle-leave-request",
1512                 G_CALLBACK(on_service_leave_request), service_interface);
1513
1514         g_signal_connect(service_object,
1515                 "handle-get-device-list",
1516                 G_CALLBACK(on_service_get_device_list), service_interface);
1517
1518         g_signal_connect(service_object,
1519                 "handle-get-mac",
1520                 G_CALLBACK(on_service_get_mac), service_interface);
1521
1522         g_signal_connect(service_object,
1523                 "handle-get-device-info",
1524                 G_CALLBACK(on_service_get_device_info), service_interface);
1525
1526         g_signal_connect(service_object,
1527                 "handle-get-endpoint-list",
1528                 G_CALLBACK(on_service_get_endpoint_list), service_interface);
1529
1530         g_signal_connect(service_object,
1531                 "handle-get-cluster-list",
1532                 G_CALLBACK(on_service_get_cluster_list), service_interface);
1533
1534         g_signal_connect(service_object,
1535                 "handle-get-node-type",
1536                 G_CALLBACK(on_service_get_node_type), service_interface);
1537
1538         return TRUE;
1539 }