988e961ad009570c1e38de29a7c87b9cebb8cb1c
[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         ZigbeeServiceServiceState_t *payload =
55                 (ZigbeeServiceServiceState_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_ret("service_object", service_object);
67
68         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
69         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
142
143         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
144         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
214
215         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
216         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
286
287         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
288         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
358
359         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
360         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
437
438         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
439         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
509
510         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
511         zblib_check_null_ret("invocation", invocation);
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         ZigbeeServiceGetNetworkInfo_t *payload =
571                 (ZigbeeServiceGetNetworkInfo_t*)resp_data;
572
573         GVariant *v_eui64;
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_ret("service_object", service_object);
585
586         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
587         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
662
663         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
664         zblib_check_null_ret("invocation", invocation);
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_ret("service_object", service_object);
743
744         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
745         zblib_check_null_ret("invocation", invocation);
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, "ay", &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         /* Allocate response callback data */
781         resp_cb_data =
782                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
783                         invocation, NULL, 0);
784         if (NULL == resp_cb_data) {
785                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
786
787                 /* Send failure response */
788                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
789
790                 return TRUE;
791         }
792
793         /* Dispatch request */
794         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
795                 ZBLIB_DRIVER_TYPE_SERVICE,
796                 ZBLIB_SERVICE_OPS_LEAVE_REQUEST,
797                 &req, sizeof(req),
798                 on_service_leave_request_resp, resp_cb_data);
799         if (FALSE == ret) {
800                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
801
802                 /* Free response callback data */
803                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
804
805                 /* Send failure response */
806                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
807
808                 return TRUE;
809         }
810
811         return TRUE;
812 }
813
814 static void on_service_get_device_list_resp(ZigBeeServiceInterface *service_interface,
815         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
816 {
817         ZigbeeServiceInterfaceRespCbData_t *cb_data =
818                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
819
820         ZigbeeService *service_object;
821         GDBusMethodInvocation *invocation;
822
823         NOT_USED(service_interface);
824         NOT_USED(request_id);
825
826         if (NULL == resp_data || 0 == resp_data_len) {
827                 Z_LOGE("resp_data is null");
828                 g_free(cb_data);
829                 return;
830         }
831
832         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
833         zblib_check_null_ret("service_object", service_object);
834
835         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
836         zblib_check_null_ret("invocation", invocation);
837
838         /* To-To : zigbee_service_complete_get_device_list */
839         //zigbee_service_complete_get_device_list(service_object, invocation,   resp_data);
840
841         g_free(cb_data);
842 }
843
844 static gboolean on_service_get_device_list(ZigbeeService *service_object,
845         GDBusMethodInvocation *invocation,
846         gpointer user_data)
847 {
848         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
849         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
850
851         gboolean ret;
852
853         /* Allocate response callback data */
854         resp_cb_data =
855                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
856                         invocation, NULL, 0);
857         if (NULL == resp_cb_data) {
858                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
859
860                 /* Send failure response */
861                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
862
863                 return TRUE;
864         }
865
866         /* Dispatch request */
867         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
868                 ZBLIB_DRIVER_TYPE_SERVICE,
869                 ZBLIB_SERVICE_OPS_GET_DEVICE_LIST,
870                 NULL, 0,
871                 on_service_get_device_list_resp, resp_cb_data);
872         if (FALSE == ret) {
873                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
874
875                 /* Free response callback data */
876                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
877
878                 /* Send failure response */
879                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
880
881                 return TRUE;
882         }
883
884         return TRUE;
885 }
886
887 static void on_service_get_mac_resp(ZigBeeServiceInterface *service_interface,
888         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
889 {
890         ZigbeeServiceInterfaceRespCbData_t *cb_data =
891                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
892
893         ZigbeeService *service_object;
894         GDBusMethodInvocation *invocation;
895
896         NOT_USED(cb_data);
897         NOT_USED(service_interface);
898         NOT_USED(request_id);
899
900         if (NULL == resp_data || 0 == resp_data_len) {
901                 Z_LOGE("resp_data is null");
902                 g_free(cb_data);
903                 return;
904         }
905
906         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
907         zblib_check_null_ret("service_object", service_object);
908
909         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
910         zblib_check_null_ret("invocation", invocation);
911
912         /* Todo : plugin must load resp_data as char pointer */
913         //zigbee_service_complete_get_mac(service_object, invocation, resp_data);
914
915         g_free(cb_data);
916
917 }
918
919 static gboolean on_service_get_mac(ZigbeeService *service_object,
920         GDBusMethodInvocation *invocation,
921         gpointer user_data)
922 {
923         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
924         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
925
926         gboolean ret;
927
928         /* Allocate response callback data */
929         resp_cb_data =
930                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
931                         invocation, NULL, 0);
932         if (NULL == resp_cb_data) {
933                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
934
935                 /* Send failure response */
936                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
937
938                 return TRUE;
939         }
940
941         /* Dispatch request */
942         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
943                 ZBLIB_DRIVER_TYPE_SERVICE,
944                 ZBLIB_SERVICE_OPS_GET_MAC,
945                 NULL, 0,
946                 on_service_get_mac_resp, resp_cb_data);
947         if (FALSE == ret) {
948                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
949
950                 /* Free response callback data */
951                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
952
953                 /* Send failure response */
954                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
955
956                 return TRUE;
957         }
958
959         return TRUE;
960 }
961
962 static void on_service_get_device_info_resp(ZigBeeServiceInterface *service_interface,
963         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
964 {
965         ZigbeeServiceInterfaceRespCbData_t *cb_data =
966                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
967
968         ZigbeeService *service_object;
969         GDBusMethodInvocation *invocation;
970
971         NOT_USED(cb_data);
972         NOT_USED(service_interface);
973         NOT_USED(request_id);
974
975         if (NULL == resp_data || 0 == resp_data_len) {
976                 Z_LOGE("resp_data is null");
977                 g_free(cb_data);
978                 return;
979         }
980
981         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
982         zblib_check_null_ret("service_object", service_object);
983
984         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
985         zblib_check_null_ret("invocation", invocation);
986
987         /* To-Do : zigbee_service_complete_get_device_list */
988         //zigbee_service_complete_get_device_info(service_object, invocation, resp_data);
989
990         g_free(cb_data);
991 }
992
993 static gboolean on_service_get_device_info(ZigbeeService *service_object,
994         GDBusMethodInvocation *invocation,
995         gpointer user_data)
996 {
997         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
998         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
999
1000         gboolean ret;
1001
1002         /* Allocate response callback data */
1003         resp_cb_data =
1004                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1005                         invocation, NULL, 0);
1006         if (NULL == resp_cb_data) {
1007                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1008
1009                 /* Send failure response */
1010                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1011
1012                 return TRUE;
1013         }
1014
1015         /* Dispatch request */
1016         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1017                 ZBLIB_DRIVER_TYPE_SERVICE,
1018                 ZBLIB_SERVICE_OPS_GET_DEVICE_INFO,
1019                 NULL, 0,
1020                 on_service_get_device_info_resp, resp_cb_data);
1021         if (FALSE == ret) {
1022                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1023
1024                 /* Free response callback data */
1025                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1026
1027                 /* Send failure response */
1028                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1029
1030                 return TRUE;
1031         }
1032
1033         return TRUE;
1034 }
1035
1036 static void on_service_get_endpoint_list_resp(ZigBeeServiceInterface *service_interface,
1037         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1038 {
1039         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1040                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1041
1042         ZigbeeService *service_object;
1043         GDBusMethodInvocation *invocation;
1044
1045         NOT_USED(cb_data);
1046         NOT_USED(service_interface);
1047         NOT_USED(request_id);
1048
1049         if (NULL == resp_data || 0 == resp_data_len) {
1050                 Z_LOGE("resp_data is null");
1051                 g_free(cb_data);
1052                 return;
1053         }
1054
1055         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1056         zblib_check_null_ret("service_object", service_object);
1057
1058         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1059         zblib_check_null_ret("invocation", invocation);
1060
1061         /* To fix : end-points must be changed gchar* -> GVariant ay */
1062         //zigbee_service_complete_get_endpoint_list(service_object, invocation, resp_data);
1063
1064         g_free(cb_data);
1065 }
1066
1067 static gboolean on_service_get_endpoint_list(ZigbeeService *service_object,
1068         GDBusMethodInvocation *invocation,
1069         GVariant *eui64,
1070         gpointer user_data)
1071 {
1072         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1073         ZigbeeServiceGetEndpointList_t req;
1074         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1075
1076         GVariantIter *iter = NULL;
1077         guint i = 0;
1078
1079         gboolean ret;
1080
1081         memset(&req, 0x0, sizeof(ZigbeeServiceGetEndpointList_t));
1082
1083         /* Update request structure */
1084         g_variant_get(eui64, "ay", &iter);
1085         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
1086                 i++;
1087                 if (i >= ZIGBEE_EUI64_SIZE)
1088                         break;
1089         }
1090
1091         /* Allocate response callback data */
1092         resp_cb_data =
1093                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1094                         invocation, NULL, 0);
1095         if (NULL == resp_cb_data) {
1096                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1097
1098                 /* Send failure response */
1099                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1100
1101                 return TRUE;
1102         }
1103
1104         /* Dispatch request */
1105         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1106                 ZBLIB_DRIVER_TYPE_SERVICE,
1107                 ZBLIB_SERVICE_OPS_GET_ENDPOINT_LIST,
1108                 &req, sizeof(req),
1109                 on_service_get_endpoint_list_resp, resp_cb_data);
1110         if (FALSE == ret) {
1111                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1112
1113                 /* Free response callback data */
1114                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1115
1116                 /* Send failure response */
1117                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1118
1119                 return TRUE;
1120         }
1121
1122         return TRUE;
1123 }
1124
1125 static void on_service_get_cluster_list_resp(ZigBeeServiceInterface *service_interface,
1126         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1127 {
1128         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1129                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1130
1131         ZigbeeService *service_object;
1132         GDBusMethodInvocation *invocation;
1133
1134         NOT_USED(cb_data);
1135         NOT_USED(service_interface);
1136         NOT_USED(request_id);
1137
1138         if (NULL == resp_data || 0 == resp_data_len) {
1139                 Z_LOGE("resp_data is null");
1140                 g_free(cb_data);
1141                 return;
1142         }
1143
1144         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1145         zblib_check_null_ret("service_object", service_object);
1146
1147         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1148         zblib_check_null_ret("invocation", invocation);
1149
1150         /* To-Do : zigbee_service_complete_get_cluster_list */
1151         //zigbee_service_complete_get_cluster_list(service_object, invocation, resp_data);
1152
1153         g_free(cb_data);
1154 }
1155
1156 static gboolean on_service_get_cluster_list(ZigbeeService *service_object,
1157         GDBusMethodInvocation *invocation,
1158         GVariant *eui64,
1159         gchar endpoint,
1160         gpointer user_data)
1161 {
1162         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1163         ZigbeeServiceGetClusterList_t req;
1164         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1165
1166         GVariantIter *iter = NULL;
1167         guint i = 0;
1168
1169         gboolean ret;
1170
1171         memset(&req, 0x0, sizeof(ZigbeeServiceGetClusterList_t));
1172
1173         /* Update request structure */
1174         g_variant_get(eui64, "ay", &iter);
1175         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
1176                 i++;
1177                 if (i >= ZIGBEE_EUI64_SIZE)
1178                         break;
1179         }
1180         req.endpoint = endpoint;
1181
1182         /* Allocate response callback data */
1183         resp_cb_data =
1184                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1185                         invocation, NULL, 0);
1186         if (NULL == resp_cb_data) {
1187                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1188
1189                 /* Send failure response */
1190                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1191
1192                 return TRUE;
1193         }
1194
1195         /* Dispatch request */
1196         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1197                 ZBLIB_DRIVER_TYPE_SERVICE,
1198                 ZBLIB_SERVICE_OPS_GET_CLUSTER_LIST,
1199                 &req, sizeof(req),
1200                 on_service_get_cluster_list_resp, resp_cb_data);
1201         if (FALSE == ret) {
1202                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1203
1204                 /* Free response callback data */
1205                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1206
1207                 /* Send failure response */
1208                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1209
1210                 return TRUE;
1211         }
1212
1213         return TRUE;
1214 }
1215
1216 static void on_service_get_node_type_resp(ZigBeeServiceInterface *service_interface,
1217         guint request_id, gpointer resp_data, guint resp_data_len, gpointer resp_cb_data)
1218 {
1219         ZigbeeServiceInterfaceRespCbData_t *cb_data =
1220                 (ZigbeeServiceInterfaceRespCbData_t *)resp_cb_data;
1221
1222         ZigbeeService *service_object;
1223         GDBusMethodInvocation *invocation;
1224         ZigbeeServiceServiceNodeType_t *payload = 
1225                 (ZigbeeServiceServiceNodeType_t *)resp_data;
1226
1227         NOT_USED(cb_data);
1228         NOT_USED(service_interface);
1229         NOT_USED(request_id);
1230
1231         if (NULL == resp_data || 0 == resp_data_len) {
1232                 Z_LOGE("resp_data is null");
1233                 g_free(cb_data);
1234                 return;
1235         }
1236
1237         service_object = zigbee_service_dbus_interface_ref_interface_object(cb_data);
1238         zblib_check_null_ret("service_object", service_object);
1239
1240         invocation = zigbee_service_dbus_interface_ref_invocation(cb_data);
1241         zblib_check_null_ret("invocation", invocation);
1242
1243         zigbee_service_complete_get_node_type(service_object, invocation,
1244                 payload->result, payload->node_type);
1245
1246         g_free(cb_data);
1247 }
1248
1249 static gboolean on_service_get_node_type(ZigbeeService *service_object,
1250         GDBusMethodInvocation *invocation,
1251         GVariant *eui64,
1252         gpointer user_data)
1253 {
1254         ZigBeeServiceInterface *service_interface = (ZigBeeServiceInterface *)user_data;
1255         ZigbeeServiceGetNodeType_t req;
1256         ZigbeeServiceInterfaceRespCbData_t *resp_cb_data = NULL;
1257
1258         GVariantIter *iter = NULL;
1259         guint i = 0;
1260
1261         gboolean ret;
1262
1263         memset(&req, 0x0, sizeof(ZigbeeServiceGetNodeType_t));
1264
1265         /* Update request structure */
1266         g_variant_get(eui64, "ay", &iter);
1267         while (g_variant_iter_loop(iter, "y", req.eui64[i])) {
1268                 i++;
1269                 if (i >= ZIGBEE_EUI64_SIZE)
1270                         break;
1271         }
1272
1273         /* Allocate response callback data */
1274         resp_cb_data =
1275                 zigbee_service_dbus_interface_create_resp_cb_data(service_object,
1276                         invocation, NULL, 0);
1277         if (NULL == resp_cb_data) {
1278                 Z_LOGE("zigbee_service_dbus_interface_create_resp_cb_data failed!");
1279
1280                 /* Send failure response */
1281                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1282
1283                 return TRUE;
1284         }
1285
1286         /* Dispatch request */
1287         ret = zigbee_service_dbus_interface_dispatch_request(service_interface,
1288                 ZBLIB_DRIVER_TYPE_SERVICE,
1289                 ZBLIB_SERVICE_OPS_GET_NODE_TYPE,
1290                 &req, sizeof(req),
1291                 on_service_get_node_type_resp, resp_cb_data);
1292         if (FALSE == ret) {
1293                 Z_LOGE("zigbee_service_dbus_interface_dispatch_request failed!");
1294
1295                 /* Free response callback data */
1296                 zigbee_service_dbus_interface_destroy_resp_cb_data(resp_cb_data);
1297
1298                 /* Send failure response */
1299                 ZIGBEE_DBUS_FAIL_RESPONSE(invocation, ZIGBEE_DBUS_DEFAULT_REQ_FAILED_MSG);
1300
1301                 return TRUE;
1302         }
1303
1304         return TRUE;
1305 }
1306
1307 void zigbee_service_dbus_interface_service_notification(ZigBeeServiceInterface *service_interface,
1308         guint noti_id, gpointer noti_data, guint noti_data_len, gpointer noti_cb_data)
1309 {
1310         ZigbeeService *service_object;
1311
1312         zblib_check_null_ret("service_interface", service_interface);
1313
1314         if (NULL == noti_data || 0 == noti_data_len) {
1315                 Z_LOGE("noti_data is NULL");
1316                 return;
1317         }
1318
1319         service_object = _service_interface_ref_zigbee_service(service_interface);
1320         zblib_check_null_ret("service_object", service_object);
1321
1322         NOT_USED(noti_cb_data);
1323
1324         switch(noti_id) {
1325         case ZBLIB_SERVICE_NOTI_FORM_NETWORK_DONE: {
1326                 ZigbeeServiceServiceFormNetworkDone_t *panid_t = 
1327                         (ZigbeeServiceServiceFormNetworkDone_t*)noti_data;
1328
1329                 Z_LOGD("form_network_done : [0x%X]", panid_t->pan_id);
1330                 
1331                 zigbee_service_emit_form_network_done(service_object, panid_t->pan_id);
1332         }
1333         break;
1334         case ZBLIB_SERVICE_NOTI_CHILD_JOINED: {
1335                 zigbee_service_emit_child_joined(service_object, (GVariant *)noti_data);
1336         }
1337         break;
1338         case ZBLIB_SERVICE_NOTI_CHILD_REJOINED: {
1339                 ZigbeeServiceServiceChildRejoined_t ieee_addr_t;
1340                 memcpy(&ieee_addr_t, noti_data, noti_data_len);
1341                 zigbee_service_emit_child_rejoined(service_object, ieee_addr_t.eui64);
1342         }
1343         break;
1344         case ZBLIB_SERVICE_NOTI_CHILD_LEFT: {
1345                 ZigbeeServiceServiceChildLeft_t child_left_t;
1346                 memcpy(&child_left_t, noti_data, noti_data_len);
1347                 zigbee_service_emit_child_left(service_object, child_left_t.eui64, child_left_t.status);
1348         }
1349         break;
1350         case ZBLIB_SERVICE_NOTI_LEAVE_NETWORK_DONE: {
1351                 ZigbeeServiceServiceLeaveNetworkDone_t leave_net_t;
1352                 memcpy(&leave_net_t, noti_data, noti_data_len);
1353                 zigbee_service_emit_leave_network_done(service_object, leave_net_t.pan_id);
1354         }
1355         break;
1356         default:
1357                 Z_LOGE("Unexpected notification [%x]", noti_id);
1358         break;
1359         }
1360
1361         /* ZigbeeService should be dereferenced */
1362         g_object_unref(service_object);
1363 }
1364
1365 gboolean zigbee_service_dbus_interface_service_init(ZigBeeServiceInterface *service_interface,
1366         ZigbeeObjectSkeleton *zigbee_object)
1367 {
1368         ZigbeeService *service_object;
1369
1370         if (NULL == service_interface) {
1371                 Z_LOGE("service_interface is NULL");
1372                 return FALSE;
1373         }
1374
1375         service_object = zigbee_service_skeleton_new();
1376         zigbee_object_skeleton_set_service(zigbee_object, service_object);
1377         g_object_unref(service_object);
1378
1379         Z_LOGI("service_object: [%p]", service_object);
1380
1381         /*
1382          * Register signal handlers for 'service' interface
1383          */
1384         g_signal_connect(service_object,
1385                 "handle-enable",
1386                 G_CALLBACK(on_service_enable), service_interface);
1387
1388         g_signal_connect(service_object,
1389                 "handle-disable",
1390                 G_CALLBACK(on_service_disable), service_interface);
1391
1392         g_signal_connect(service_object,
1393                 "handle-zb-hw-reset",
1394                 G_CALLBACK(on_service_zb_hw_reset), service_interface);
1395
1396         g_signal_connect(service_object,
1397                 "handle-form-network",
1398                 G_CALLBACK(on_service_form_network), service_interface);
1399
1400         g_signal_connect(service_object,
1401                 "handle-coex-start",
1402                 G_CALLBACK(on_service_coex_start), service_interface);
1403
1404         g_signal_connect(service_object,
1405                 "handle-coex-stop",
1406                 G_CALLBACK(on_service_coex_stop), service_interface);
1407
1408         g_signal_connect(service_object,
1409                 "handle-leave-network",
1410                 G_CALLBACK(on_service_leave_network), service_interface);
1411
1412         g_signal_connect(service_object,
1413                 "handle-get-network-info",
1414                 G_CALLBACK(on_service_get_network_info), service_interface);
1415
1416         g_signal_connect(service_object,
1417                 "handle-permit-join",
1418                 G_CALLBACK(on_service_permit_join), service_interface);
1419
1420         g_signal_connect(service_object,
1421                 "handle-leave-request",
1422                 G_CALLBACK(on_service_leave_request), service_interface);
1423
1424         g_signal_connect(service_object,
1425                 "handle-get-device-list",
1426                 G_CALLBACK(on_service_get_device_list), service_interface);
1427
1428         g_signal_connect(service_object,
1429                 "handle-get-mac",
1430                 G_CALLBACK(on_service_get_mac), service_interface);
1431
1432         g_signal_connect(service_object,
1433                 "handle-get-device-info",
1434                 G_CALLBACK(on_service_get_device_info), service_interface);
1435
1436         g_signal_connect(service_object,
1437                 "handle-get-endpoint-list",
1438                 G_CALLBACK(on_service_get_endpoint_list), service_interface);
1439
1440         g_signal_connect(service_object,
1441                 "handle-get-cluster-list",
1442                 G_CALLBACK(on_service_get_cluster_list), service_interface);
1443
1444         g_signal_connect(service_object,
1445                 "handle-get-node-type",
1446                 G_CALLBACK(on_service_get_node_type), service_interface);
1447
1448         return TRUE;
1449 }