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