Code Sync [Tizen3.0]: Merged the tizen_2.4 Spin code to tizen.org
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-request-handler.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <string.h>
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus-glib-bindings.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28 #include <glib.h>
29 #include <dlog.h>
30 #include <security-server.h>
31
32 #include "bluetooth-api.h"
33 #include "bt-service-common.h"
34 #include "bt-service-util.h"
35 #include "bt-service-event.h"
36 #include "bt-service-adapter.h"
37 #include "bt-service-adapter-le.h"
38 #include "bt-service-device.h"
39 #include "bt-service-hid.h"
40 #include "bt-service-network.h"
41 #include "bt-service-audio.h"
42 #include "bt-service-oob.h"
43 #include "bt-service-avrcp.h"
44 #include "bt-service-opp-client.h"
45 #include "bt-service-obex-server.h"
46 #include "bt-service-rfcomm-client.h"
47 #include "bt-service-rfcomm-server.h"
48 #include "bt-request-handler.h"
49 #include "bt-service-pbap.h"
50
51 /* auto generated header by bt-request-service.xml*/
52 #include "bt-service-method.h"
53
54 DBusGConnection *bt_service_conn;
55 BtService *service_object;
56
57 GType bt_service_get_type(void);
58
59 G_DEFINE_TYPE(BtService, bt_service, G_TYPE_OBJECT);
60
61 /*This is part of platform provided code skeleton for client server model*/
62 static void bt_service_class_init(BtServiceClass *service_class)
63 {
64         dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(service_class),
65                                         &dbus_glib_bt_object_info);
66 }
67
68 /*This is part of platform provided code skeleton for client server model*/
69 static void bt_service_init(BtService *service)
70 {
71 }
72
73 static int __bt_bluez_request(int function_name,
74                 int request_type,
75                 int request_id,
76                 DBusGMethodInvocation *context,
77                 GArray *in_param1,
78                 GArray *in_param2,
79                 GArray *in_param3,
80                 GArray *in_param4,
81                 GArray **out_param1)
82 {
83         int result = BLUETOOTH_ERROR_NONE;
84
85         switch (function_name) {
86         case BT_ENABLE_ADAPTER:
87                 result = _bt_enable_adapter();
88                 break;
89         case BT_DISABLE_ADAPTER:
90                 result = _bt_disable_adapter();
91                 break;
92         case BT_RECOVER_ADAPTER:
93                 result = _bt_recover_adapter();
94                 break;
95         case BT_ENABLE_ADAPTER_LE:
96                 result = _bt_enable_adapter_le();
97                 break;
98         case BT_DISABLE_ADAPTER_LE:
99                 result = _bt_disable_adapter_le();
100                 break;
101         case BT_RESET_ADAPTER:
102                 result = _bt_reset_adapter();
103                 break;
104         case BT_CHECK_ADAPTER: {
105                 int enabled = BT_ADAPTER_DISABLED;
106
107                 result = _bt_check_adapter(&enabled);
108
109                 g_array_append_vals(*out_param1, &enabled,
110                                 sizeof(int));
111                 break;
112         }
113         case BT_GET_LOCAL_ADDRESS: {
114                 bluetooth_device_address_t local_address = { {0} };
115                 result = _bt_get_local_address(&local_address);
116
117                 g_array_append_vals(*out_param1, &local_address,
118                                 sizeof(bluetooth_device_address_t));
119                 break;
120         }
121         case BT_GET_LOCAL_VERSION: {
122                 bluetooth_version_t ver = { {0} };
123                 result = _bt_get_local_version(&ver);
124
125                 g_array_append_vals(*out_param1, &ver,
126                                 sizeof(bluetooth_version_t));
127                 break;
128         }
129         case BT_GET_LOCAL_NAME: {
130                 bluetooth_device_name_t local_name = { {0} };
131                 result = _bt_get_local_name(&local_name);
132
133                 g_array_append_vals(*out_param1, &local_name,
134                                 sizeof(bluetooth_device_name_t));
135
136                 break;
137         }
138         case BT_SET_LOCAL_NAME: {
139                 bluetooth_device_name_t local_name = { {0} };
140
141                 local_name = g_array_index(in_param1,
142                                 bluetooth_device_name_t, 0);
143
144                 result = _bt_set_local_name(local_name.name);
145
146                 break;
147         }
148         case BT_IS_SERVICE_USED: {
149                 char *uuid;
150                 gboolean used = FALSE;
151
152                 uuid = &g_array_index(in_param1, char, 0);
153
154                 result = _bt_is_service_used(uuid, &used);
155
156                 if (result == BLUETOOTH_ERROR_NONE) {
157                         g_array_append_vals(*out_param1, &used,
158                                                 sizeof(gboolean));
159                 }
160                 break;
161         }
162         case BT_GET_DISCOVERABLE_MODE: {
163                 int mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
164
165                 result = _bt_get_discoverable_mode(&mode);
166
167                 g_array_append_vals(*out_param1, &mode, sizeof(int));
168                 break;
169         }
170         case BT_SET_DISCOVERABLE_MODE: {
171                 int mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
172                 int time = 0;
173
174                 mode = g_array_index(in_param1, int, 0);
175                 time = g_array_index(in_param2, int, 0);
176
177                 result = _bt_set_discoverable_mode(mode, time);
178                 break;
179         }
180         case BT_GET_DISCOVERABLE_TIME: {
181                 int timeout = 0;
182
183                 result = _bt_get_timeout_value(&timeout);
184
185                 g_array_append_vals(*out_param1, &timeout, sizeof(int));
186                 break;
187         }
188         case BT_START_DISCOVERY:
189                 result = _bt_start_discovery();
190
191                 break;
192         case BT_START_CUSTOM_DISCOVERY: {
193                 bt_discovery_role_type_t role;
194                 role = g_array_index(in_param1, bt_discovery_role_type_t, 0);
195
196                 result = _bt_start_custom_discovery(role);
197                 break;
198         }
199         case BT_CANCEL_DISCOVERY:
200                 result = _bt_cancel_discovery();
201                 break;
202         case BT_START_LE_DISCOVERY:
203                 result = _bt_start_le_discovery();
204                 break;
205         case BT_STOP_LE_DISCOVERY:
206                 result = _bt_stop_le_discovery();
207                 break;
208         case BT_IS_DISCOVERYING: {
209                 gboolean discovering = FALSE;
210                 discovering = _bt_is_discovering();
211                 g_array_append_vals(*out_param1, &discovering, sizeof(gboolean));
212                 break;
213         }
214         case BT_IS_LE_DISCOVERYING: {
215                 gboolean le_discovering = FALSE;
216                 le_discovering = _bt_is_le_discovering();
217                 g_array_append_vals(*out_param1, &le_discovering, sizeof(gboolean));
218                 break;
219         }
220         case BT_ENABLE_RSSI: {
221                 bluetooth_device_address_t bd_addr;
222                 int link_type;
223                 bt_rssi_threshold_t rssi_threshold;
224                 int low_threshold;
225                 int in_range_threshold;
226                 int high_threshold;
227                 BT_DBG("Enable RSSI");
228
229                 bd_addr = g_array_index(in_param1, bluetooth_device_address_t, 0);
230                 link_type = g_array_index(in_param2, int, 0);
231                 rssi_threshold = g_array_index(in_param3, bt_rssi_threshold_t, 0);
232
233                 low_threshold = rssi_threshold.low_threshold;
234                 in_range_threshold = rssi_threshold.in_range_threshold;
235                 high_threshold = rssi_threshold.high_threshold;
236
237                 result = _bt_enable_rssi(&bd_addr, link_type ,low_threshold,
238                                 in_range_threshold, high_threshold);
239                 break;
240         }
241         case BT_GET_RSSI: {
242                 int link_type;
243                 bluetooth_device_address_t bd_addr;
244
245                 BT_DBG("Get RSSI Strength");
246
247                 bd_addr = g_array_index(in_param1, bluetooth_device_address_t, 0);
248                 link_type = g_array_index(in_param2, int, 0);
249                 result = _bt_get_rssi_strength(&bd_addr, link_type);
250                 break;
251         }
252         case BT_IS_CONNECTABLE: {
253                 gboolean is_connectable;
254
255                 is_connectable = _bt_is_connectable();
256                 g_array_append_vals(*out_param1, &is_connectable, sizeof(gboolean));
257                 break;
258         }
259         case BT_SET_CONNECTABLE: {
260                 gboolean is_connectable;
261
262                 is_connectable = g_array_index(in_param1, gboolean, 0);
263
264                 result = _bt_set_connectable(is_connectable);
265                 break;
266         }
267         case BT_SET_ADVERTISING: {
268                 gboolean enable = FALSE;
269                 char *sender = NULL;
270                 gboolean use_reserved_slot = FALSE;
271
272                 enable = g_array_index(in_param1, gboolean, 0);
273                 use_reserved_slot = g_array_index(in_param2, gboolean, 0);
274                 sender = dbus_g_method_get_sender(context);
275                 result = _bt_set_advertising(enable, sender, use_reserved_slot);
276                 g_free(sender);
277                 break;
278         }
279         case BT_SET_CUSTOM_ADVERTISING: {
280                 gboolean enable = FALSE;
281                 bluetooth_advertising_params_t adv_params;
282                 char *sender = NULL;
283                 gboolean use_reserved_slot = FALSE;
284
285                 enable = g_array_index(in_param1, gboolean, 0);
286                 adv_params = g_array_index(in_param2,
287                                         bluetooth_advertising_params_t, 0);
288                 use_reserved_slot = g_array_index(in_param3, gboolean, 0);
289                 sender = dbus_g_method_get_sender(context);
290
291                 BT_DBG("bluetooth_advertising_params_t [%f %f %d %d]",
292                                 adv_params.interval_min, adv_params.interval_max,
293                                 adv_params.filter_policy, adv_params.type);
294                 result = _bt_set_custom_advertising(enable, &adv_params, sender, use_reserved_slot);
295                 g_free(sender);
296                 break;
297         }
298         case BT_GET_ADVERTISING_DATA: {
299                 bluetooth_advertising_data_t adv = { {0} };
300                 int length = 0;
301
302                 result = _bt_get_advertising_data(&adv, &length);
303                 if (result == BLUETOOTH_ERROR_NONE) {
304                         g_array_append_vals(*out_param1, adv.data, length);
305                 }
306
307                 break;
308         }
309         case BT_SET_ADVERTISING_DATA: {
310                 bluetooth_advertising_data_t adv = { {0} };
311                 int length;
312                 char *sender = NULL;
313                 gboolean use_reserved_slot = FALSE;
314
315                 sender = dbus_g_method_get_sender(context);
316
317                 adv = g_array_index(in_param1,
318                                 bluetooth_advertising_data_t, 0);
319                 length = g_array_index(in_param2, int, 0);
320                 use_reserved_slot = g_array_index(in_param3, gboolean, 0);
321
322                 result = _bt_set_advertising_data(&adv, length, sender, use_reserved_slot);
323
324                 g_free(sender);
325                 break;
326         }
327         case BT_GET_SCAN_RESPONSE_DATA: {
328                 bluetooth_scan_resp_data_t rsp = { {0} };
329                 int length = 0;
330
331                 result = _bt_get_scan_response_data(&rsp, &length);
332                 if (result == BLUETOOTH_ERROR_NONE) {
333                         g_array_append_vals(*out_param1, rsp.data, length);
334                 }
335
336                 break;
337         }
338         case BT_SET_SCAN_RESPONSE_DATA: {
339                 bluetooth_scan_resp_data_t rsp = { {0} };
340                 int length;
341                 char *sender = NULL;
342                 gboolean use_reserved_slot = FALSE;
343
344                 sender = dbus_g_method_get_sender(context);
345
346                 rsp = g_array_index(in_param1,
347                                 bluetooth_scan_resp_data_t, 0);
348                 length = g_array_index(in_param2, int, 0);
349                 use_reserved_slot = g_array_index(in_param3, gboolean, 0);
350
351                 result = _bt_set_scan_response_data(&rsp, length, sender, use_reserved_slot);
352
353                 g_free(sender);
354                 break;
355         }
356         case BT_SET_MANUFACTURER_DATA: {
357                 bluetooth_manufacturer_data_t m_data = { 0 };
358
359                 m_data = g_array_index(in_param1,
360                                 bluetooth_manufacturer_data_t, 0);
361
362                 result = _bt_set_manufacturer_data(&m_data);
363                 break;
364         }
365         case BT_SET_SCAN_PARAMETERS: {
366                 bluetooth_le_scan_params_t scan_params;
367
368                 scan_params = g_array_index(in_param1,
369                                         bluetooth_le_scan_params_t, 0);
370
371                 BT_DBG("bluetooth_le_scan_params_t [%f %f %d]",
372                                 scan_params.interval, scan_params.window,
373                                 scan_params.type);
374
375                 result = _bt_set_scan_parameters(&scan_params);
376                 break;
377         }
378         case BT_LE_CONN_UPDATE: {
379                 bluetooth_device_address_t local_address = { {0} };
380                 bluetooth_le_conn_update_t parameters = {0};
381
382                 local_address = g_array_index(in_param1,
383                                         bluetooth_device_address_t, 0);
384                 parameters = g_array_index(in_param2,
385                                         bluetooth_le_conn_update_t, 0);
386
387                 result =  _bt_le_conn_update(local_address.addr,
388                                         parameters.interval_min,
389                                         parameters.interval_max,
390                                         parameters.latency,
391                                         parameters.time_out);
392                 break;
393         }
394         case BT_IS_ADVERTISING: {
395                 gboolean advertising = FALSE;
396                 advertising = _bt_is_advertising();
397
398                 g_array_append_vals(*out_param1, &advertising, sizeof(gboolean));
399                 break;
400         }
401         case BT_ADD_WHITE_LIST: {
402                 bluetooth_device_address_t address = { {0} };
403                 int addr_type = 0;
404
405                 address = g_array_index(in_param1,
406                                 bluetooth_device_address_t, 0);
407                 addr_type = g_array_index(in_param2, int, 0);
408
409                 result = _bt_add_white_list(&address, addr_type);
410                 break;
411         }
412         case BT_REMOVE_WHITE_LIST: {
413                 bluetooth_device_address_t address = { {0} };
414                 int addr_type = 0;
415
416                 address = g_array_index(in_param1,
417                                 bluetooth_device_address_t, 0);
418                 addr_type = g_array_index(in_param2, int, 0);
419
420                 result = _bt_remove_white_list(&address, addr_type);
421                 break;
422         }
423         case BT_CLEAR_WHITE_LIST: {
424                 result = _bt_clear_white_list();
425                 break;
426         }
427         case BT_GET_BONDED_DEVICES:
428                 result = _bt_get_bonded_devices(out_param1);
429                 break;
430
431         case BT_GET_BONDED_DEVICE: {
432                 bluetooth_device_address_t address = { {0} };
433                 bluetooth_device_info_t dev_info;
434
435                 address = g_array_index(in_param1,
436                                 bluetooth_device_address_t, 0);
437
438                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
439                 result = _bt_get_bonded_device_info(&address, &dev_info);
440
441                 if (result == BLUETOOTH_ERROR_NONE) {
442                         g_array_append_vals(*out_param1, &dev_info,
443                                         sizeof(bluetooth_device_info_t));
444                 }
445                 break;
446         }
447         case BT_BOND_DEVICE: {
448                 bluetooth_device_address_t address = { {0} };
449
450                 address = g_array_index(in_param1,
451                                 bluetooth_device_address_t, 0);
452
453                 result = _bt_bond_device(request_id, &address,
454                                 BLUETOOTH_DEV_CONN_DEFAULT, out_param1);
455                 break;
456         }
457         case BT_BOND_DEVICE_BY_TYPE: {
458                 bluetooth_device_address_t address = { {0} };
459                 unsigned short conn_type = 0;
460
461                 address = g_array_index(in_param1,
462                                 bluetooth_device_address_t, 0);
463
464                 conn_type = g_array_index(in_param2,
465                                 unsigned short, 0);
466
467                 result = _bt_bond_device(request_id, &address,
468                                 conn_type, out_param1);
469                 break;
470         }
471         case BT_CANCEL_BONDING: {
472                 result = _bt_cancel_bonding();
473                 break;
474         }
475         case BT_UNBOND_DEVICE: {
476                 bluetooth_device_address_t address = { {0} };
477
478                 address = g_array_index(in_param1,
479                                 bluetooth_device_address_t, 0);
480
481                 result = _bt_unbond_device(request_id, &address, out_param1);
482
483                 break;
484         }
485         case BT_SET_ALIAS: {
486                 bluetooth_device_address_t address = { {0} };
487                 const char *local_name;
488
489                 address = g_array_index(in_param1,
490                                 bluetooth_device_address_t, 0);
491
492                 local_name = &g_array_index(in_param2, char, 0);
493
494                 result = _bt_set_alias(&address, local_name);
495                 break;
496         }
497         case BT_SEARCH_SERVICE: {
498                 bluetooth_device_address_t address = { {0} };
499
500                 address = g_array_index(in_param1,
501                                 bluetooth_device_address_t, 0);
502
503                 result = _bt_search_device(request_id, &address);
504                 if (result != BLUETOOTH_ERROR_NONE) {
505                         g_array_append_vals(*out_param1, &address,
506                                         sizeof(bluetooth_device_address_t));
507                 }
508                 break;
509         }
510         case BT_CANCEL_SEARCH_SERVICE: {
511                 result = _bt_cancel_search_device();
512                 break;
513         }
514         case BT_SET_AUTHORIZATION: {
515                 bluetooth_device_address_t address = { {0} };
516                 gboolean authorize;
517
518                 address = g_array_index(in_param1,
519                                 bluetooth_device_address_t, 0);
520
521                 authorize = g_array_index(in_param2, gboolean, 0);
522
523                 result = _bt_set_authorization(&address, authorize);
524                 break;
525         }
526         case BT_IS_DEVICE_CONNECTED: {
527                 bluetooth_device_address_t address = { {0} };
528                 int type;
529                 gboolean connected = FALSE;
530
531                 address = g_array_index(in_param1,
532                                 bluetooth_device_address_t, 0);
533
534                 type = g_array_index(in_param2, int, 0);
535
536                 result = _bt_is_device_connected(&address, type, &connected);
537
538                 if (result == BLUETOOTH_ERROR_NONE) {
539                         g_array_append_vals(*out_param1, &connected,
540                                                 sizeof(gboolean));
541                 }
542
543                 break;
544         }
545         case BT_HID_CONNECT: {
546                 bluetooth_device_address_t address = { {0} };
547
548                 address = g_array_index(in_param1,
549                                 bluetooth_device_address_t, 0);
550
551                 result = _bt_hid_connect(request_id, &address);
552                 if (result != BLUETOOTH_ERROR_NONE) {
553                         g_array_append_vals(*out_param1, &address,
554                                         sizeof(bluetooth_device_address_t));
555                 }
556                 break;
557         }
558         case BT_HID_DISCONNECT: {
559                 bluetooth_device_address_t address = { {0} };
560
561                 address = g_array_index(in_param1,
562                                 bluetooth_device_address_t, 0);
563
564                 result = _bt_hid_disconnect(request_id, &address);
565                 if (result != BLUETOOTH_ERROR_NONE) {
566                         g_array_append_vals(*out_param1, &address,
567                                         sizeof(bluetooth_device_address_t));
568                 }
569                 break;
570         }
571         case BT_NETWORK_ACTIVATE:
572                 result = _bt_network_activate();
573                 break;
574         case BT_NETWORK_DEACTIVATE:
575                 result = _bt_network_deactivate();
576                 break;
577         case BT_NETWORK_CONNECT: {
578                 bluetooth_device_address_t address = { {0} };
579                 int role;
580
581                 address = g_array_index(in_param1,
582                                 bluetooth_device_address_t, 0);
583
584                 role = g_array_index(in_param2, int, 0);
585
586                 result = _bt_network_connect(request_id, role, &address);
587                 if (result != BLUETOOTH_ERROR_NONE) {
588                         g_array_append_vals(*out_param1, &address,
589                                         sizeof(bluetooth_device_address_t));
590                 }
591                 break;
592         }
593         case BT_NETWORK_DISCONNECT: {
594                 bluetooth_device_address_t address = { {0} };
595
596                 address = g_array_index(in_param1,
597                                 bluetooth_device_address_t, 0);
598
599                 result = _bt_network_disconnect(request_id, &address);
600                 if (result != BLUETOOTH_ERROR_NONE) {
601                         g_array_append_vals(*out_param1, &address,
602                                         sizeof(bluetooth_device_address_t));
603                 }
604                 break;
605         }
606         case BT_NETWORK_SERVER_DISCONNECT: {
607                 bluetooth_device_address_t address = { {0} };
608
609                 address = g_array_index(in_param1,
610                                 bluetooth_device_address_t, 0);
611
612                 result = _bt_network_server_disconnect(request_id, &address);
613                 if (result != BLUETOOTH_ERROR_NONE) {
614                         g_array_append_vals(*out_param1, &address,
615                                         sizeof(bluetooth_device_address_t));
616                 }
617                 break;
618         }
619
620         case BT_AUDIO_CONNECT: {
621                 bluetooth_device_address_t address = { {0} };
622
623                 address = g_array_index(in_param1,
624                                 bluetooth_device_address_t, 0);
625
626                 result = _bt_audio_connect(request_id, BT_AUDIO_ALL,
627                                         &address, out_param1);
628                 break;
629         }
630         case BT_AUDIO_DISCONNECT: {
631                 bluetooth_device_address_t address = { {0} };
632
633                 address = g_array_index(in_param1,
634                                 bluetooth_device_address_t, 0);
635
636                 result = _bt_audio_disconnect(request_id, BT_AUDIO_ALL,
637                                         &address, out_param1);
638                 break;
639         }
640         case BT_AG_CONNECT: {
641                 bluetooth_device_address_t address = { {0} };
642
643                 address = g_array_index(in_param1,
644                                 bluetooth_device_address_t, 0);
645
646                 result = _bt_audio_connect(request_id, BT_AUDIO_HSP,
647                                         &address, out_param1);
648                 break;
649         }
650         case BT_AG_DISCONNECT: {
651                 bluetooth_device_address_t address = { {0} };
652
653                 address = g_array_index(in_param1,
654                                 bluetooth_device_address_t, 0);
655
656                 result = _bt_audio_disconnect(request_id, BT_AUDIO_HSP,
657                                         &address, out_param1);
658                 break;
659         }
660         case BT_AV_CONNECT: {
661                 bluetooth_device_address_t address = { {0} };
662
663                 address = g_array_index(in_param1,
664                                 bluetooth_device_address_t, 0);
665
666                 result = _bt_audio_connect(request_id, BT_AUDIO_A2DP,
667                                         &address, out_param1);
668                 break;
669         }
670         case BT_AV_DISCONNECT: {
671                 bluetooth_device_address_t address = { {0} };
672
673                 address = g_array_index(in_param1,
674                                 bluetooth_device_address_t, 0);
675
676                 result = _bt_audio_disconnect(request_id, BT_AUDIO_A2DP,
677                                         &address, out_param1);
678                 break;
679         }
680         case BT_AVRCP_CONTROL_CONNECT: {
681                 bluetooth_device_address_t address = { {0} };
682
683                 address = g_array_index(in_param1,
684                                 bluetooth_device_address_t, 0);
685
686                 result = _bt_audio_connect(request_id, BT_AVRCP,
687                                         &address, out_param1);
688                 break;
689         }
690         case BT_AVRCP_CONTROL_DISCONNECT: {
691                 bluetooth_device_address_t address = { {0} };
692
693                 address = g_array_index(in_param1,
694                                 bluetooth_device_address_t, 0);
695
696                 result = _bt_audio_disconnect(request_id, BT_AVRCP,
697                                         &address, out_param1);
698                 break;
699         }
700         case BT_HF_CONNECT: {
701                 bluetooth_device_address_t address = { {0} };
702
703                 address = g_array_index(in_param1,
704                                 bluetooth_device_address_t, 0);
705
706                 result = _bt_hf_connect(request_id, &address, out_param1);
707                 break;
708         }
709         case BT_HF_DISCONNECT: {
710                 bluetooth_device_address_t address = { {0} };
711
712                 address = g_array_index(in_param1,
713                                 bluetooth_device_address_t, 0);
714
715                 result = _bt_hf_disconnect(request_id, &address, out_param1);
716                 break;
717         }
718         case BT_GET_SPEAKER_GAIN: {
719                 unsigned int volume;
720
721                 result = _bt_audio_get_speaker_gain(&volume);
722
723                 g_array_append_vals(*out_param1, &volume,
724                                 sizeof(unsigned int));
725                 break;
726         }
727         case BT_SET_SPEAKER_GAIN: {
728                 unsigned int volume;
729
730                 volume = g_array_index(in_param1,
731                                 unsigned int, 0);
732
733                 result = _bt_audio_set_speaker_gain(volume);
734
735                 break;
736         }
737         case BT_SET_CONTENT_PROTECT: {
738                 gboolean status;
739
740                 status = g_array_index(in_param1, gboolean, 0);
741
742                 result = _bt_audio_set_content_protect(status);
743
744                 break;
745         }
746         case BT_OOB_READ_LOCAL_DATA: {
747                 bt_oob_data_t local_oob_data;
748
749                 memset(&local_oob_data, 0x00, sizeof(bt_oob_data_t));
750                 result = _bt_oob_read_local_data(&local_oob_data);
751
752                 g_array_append_vals(*out_param1, &local_oob_data,
753                                 sizeof(bt_oob_data_t));
754
755                 break;
756         }
757         case BT_OOB_ADD_REMOTE_DATA: {
758                 bluetooth_device_address_t address = { {0} };
759                 bt_oob_data_t local_oob_data;
760
761                 address = g_array_index(in_param1,
762                                 bluetooth_device_address_t, 0);
763
764                 local_oob_data = g_array_index(in_param2,
765                                 bt_oob_data_t, 0);
766
767                 result = _bt_oob_add_remote_data(&address, &local_oob_data);
768
769                 break;
770         }
771         case BT_OOB_REMOVE_REMOTE_DATA: {
772                 bluetooth_device_address_t address = { {0} };
773
774                 address = g_array_index(in_param1,
775                                 bluetooth_device_address_t, 0);
776
777                 result = _bt_oob_remove_remote_data(&address);
778
779                 break;
780         }
781         case BT_AVRCP_SET_TRACK_INFO: {
782                 media_metadata_t data;
783                 media_metadata_attributes_t meta_data;
784
785                 memset(&data, 0x00, sizeof(media_metadata_t));
786                 memset(&meta_data, 0x00, sizeof(media_metadata_attributes_t));
787
788                 data = g_array_index(in_param1,
789                                 media_metadata_t, 0);
790
791                 meta_data.title = g_strdup(data.title);
792                 meta_data.artist = g_strdup(data.artist);
793                 meta_data.album = g_strdup(data.album);
794                 meta_data.genre = g_strdup(data.genre);
795                 meta_data.total_tracks = data.total_tracks;
796                 meta_data.number = data.number;
797                 meta_data.duration = (int64_t) data.duration;
798
799                 result = _bt_avrcp_set_track_info(&meta_data);
800
801                 g_free((gpointer)meta_data.title);
802                 g_free((gpointer)meta_data.artist);
803                 g_free((gpointer)meta_data.album);
804                 g_free((gpointer)meta_data.genre);
805
806                 break;
807         }
808         case BT_AVRCP_SET_PROPERTY: {
809                 int type;
810                 unsigned int value;
811
812                 type = g_array_index(in_param1, int, 0);
813                 value = g_array_index(in_param2, unsigned int, 0);
814
815                 result = _bt_avrcp_set_property(type, value);
816
817                 break;
818         }
819         case BT_AVRCP_SET_PROPERTIES: {
820                 media_player_settings_t properties;
821
822                 memset(&properties, 0x00, sizeof(media_player_settings_t));
823
824                 properties = g_array_index(in_param1,
825                                 media_player_settings_t, 0);
826
827                 result = _bt_avrcp_set_properties(&properties);
828
829                 break;
830         }
831         case BT_AVRCP_HANDLE_CONTROL: {
832                 int type;
833
834                 type = g_array_index(in_param1,
835                                 int, 0);
836
837                 result = _bt_avrcp_control_cmd(type);
838
839                 break;
840         }
841         case BT_AVRCP_CONTROL_SET_PROPERTY: {
842                 int type;
843                 unsigned int value;
844
845                 type = g_array_index(in_param1, int, 0);
846                 value = g_array_index(in_param2, unsigned int, 0);
847
848                 result = _bt_avrcp_control_set_property(type, value);
849
850                 break;
851         }
852         case BT_AVRCP_CONTROL_GET_PROPERTY: {
853                 int type;
854                 unsigned int value;
855
856                 type = g_array_index(in_param1,
857                                 int, 0);
858
859                 result = _bt_avrcp_control_get_property(type, &value);
860                 g_array_append_vals(*out_param1, &value, sizeof(int));
861
862                 break;
863         }
864         case BT_AVRCP_GET_TRACK_INFO: {
865                 media_metadata_t meta_data;
866                 media_metadata_attributes_t metadata;
867
868                 memset(&meta_data, 0x00, sizeof(media_metadata_t));
869                 memset(&metadata, 0x00, sizeof(media_metadata_attributes_t));
870
871                 result = _bt_avrcp_control_get_track_info(&metadata);
872
873                 if (BLUETOOTH_ERROR_NONE != result)
874                         break;
875
876                 if (_bt_copy_utf8_string(meta_data.title, metadata.title,
877                                                         BT_META_DATA_MAX_LEN))
878                         BT_ERR("Error in copying Title\n");
879                 if (_bt_copy_utf8_string(meta_data.artist, metadata.artist,
880                                                         BT_META_DATA_MAX_LEN))
881                         BT_ERR("Error in copying Artist\n");
882                 if (_bt_copy_utf8_string(meta_data.album, metadata.album,
883                                                         BT_META_DATA_MAX_LEN))
884                         BT_ERR("Error in copying Album\n");
885                 if (_bt_copy_utf8_string(meta_data.genre, metadata.genre,
886                                                         BT_META_DATA_MAX_LEN))
887                         BT_ERR("Error in copying Genre\n");
888
889                 if (_bt_utf8_validate(meta_data.title) == FALSE)
890                         meta_data.title[0] = '\0';
891
892                 if (_bt_utf8_validate(meta_data.artist) == FALSE)
893                         meta_data.artist[0] = '\0';
894
895                 if (_bt_utf8_validate(meta_data.album) == FALSE)
896                         meta_data.album[0] = '\0';
897
898                 if (_bt_utf8_validate(meta_data.genre) == FALSE)
899                         meta_data.genre[0] = '\0';
900
901                 meta_data.total_tracks = metadata.total_tracks;
902                 meta_data.number = metadata.number;
903                 meta_data.duration = metadata.duration;
904
905                 g_free((gpointer)metadata.title);
906                 g_free((gpointer)metadata.artist);
907                 g_free((gpointer)metadata.album);
908                 g_free((gpointer)metadata.genre);
909
910                 g_array_append_vals(*out_param1, &meta_data,
911                                         sizeof(media_metadata_t));
912                 break;
913         }
914         case BT_RFCOMM_CLIENT_CONNECT: {
915 #ifdef RFCOMM_DIRECT
916                 result = BLUETOOTH_ERROR_NONE;
917 #else
918                 bluetooth_device_address_t address = { {0} };
919                 char *input_string;
920                 int connect_type;
921
922                 address = g_array_index(in_param1,
923                                 bluetooth_device_address_t, 0);
924
925                 input_string = &g_array_index(in_param2, char, 0);
926
927                 connect_type = g_array_index(in_param3, int, 0);
928
929                 if (connect_type == BT_RFCOMM_UUID) {
930                         result = _bt_rfcomm_connect_using_uuid(request_id,
931                                                         &address, input_string);
932                 } else {
933                         result = _bt_rfcomm_connect_using_channel(request_id,
934                                                         &address, input_string);
935                 }
936
937                 if (result != BLUETOOTH_ERROR_NONE) {
938                         g_array_append_vals(*out_param1, &address,
939                                         sizeof(bluetooth_device_address_t));
940                 }
941 #endif
942                 break;
943         }
944         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
945                 result = _bt_rfcomm_cancel_connect();
946                 break;
947         case BT_RFCOMM_CLIENT_IS_CONNECTED: {
948                 gboolean connected = FALSE;
949                 result = _bt_rfcomm_is_connected(&connected);
950
951                 g_array_append_vals(*out_param1, &connected, sizeof(gboolean));
952                 break;
953         }
954         case BT_RFCOMM_SOCKET_DISCONNECT: {
955 #ifdef RFCOMM_DIRECT
956                 result = BLUETOOTH_ERROR_NONE;
957 #else
958                 int socket_fd;
959
960                 socket_fd = g_array_index(in_param1, int, 0);
961
962                 result = _bt_rfcomm_disconnect(socket_fd);
963 #endif
964                 break;
965         }
966         case BT_RFCOMM_SOCKET_WRITE: {
967 #ifdef RFCOMM_DIRECT
968                 result = BLUETOOTH_ERROR_NONE;
969 #else
970                 int socket_fd;
971                 int length;
972                 char *buffer;
973
974                 socket_fd = g_array_index(in_param1, int, 0);
975                 length = g_array_index(in_param2, int, 0);
976                 buffer = &g_array_index(in_param3, char, 0);
977
978                 result = _bt_rfcomm_write(socket_fd, buffer, length);
979 #endif
980                 break;
981         }
982         case BT_RFCOMM_CREATE_SOCKET: {
983 #ifdef RFCOMM_DIRECT
984                 result = BLUETOOTH_ERROR_NONE;
985 #else
986                 char *sender;
987                 char *uuid;
988                 int socket_fd = -1;
989
990                 sender = dbus_g_method_get_sender(context);
991                 uuid = &g_array_index(in_param1, char, 0);
992
993                 result = _bt_rfcomm_create_socket(sender, uuid);
994
995                 if (result > 0) {
996                         socket_fd = result;
997                         result = BLUETOOTH_ERROR_NONE;
998                 }
999
1000                 g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
1001
1002                 g_free(sender);
1003 #endif
1004                 break;
1005         }
1006         case BT_RFCOMM_REMOVE_SOCKET: {
1007 #ifdef RFCOMM_DIRECT
1008                 result = BLUETOOTH_ERROR_NONE;
1009 #else
1010                 int socket_fd;
1011
1012                 socket_fd = g_array_index(in_param1, int, 0);
1013
1014                 result = _bt_rfcomm_remove_socket(socket_fd);
1015 #endif
1016                 break;
1017         }
1018         case BT_RFCOMM_LISTEN: {
1019                 int socket_fd;
1020                 int pending;
1021                 gboolean is_native;
1022
1023                 socket_fd = g_array_index(in_param1, int, 0);
1024                 pending = g_array_index(in_param2, int, 0);
1025                 is_native = g_array_index(in_param3, gboolean, 0);
1026
1027                 result = _bt_rfcomm_listen(socket_fd, pending, is_native);
1028                 break;
1029         }
1030         case BT_RFCOMM_IS_UUID_AVAILABLE: {
1031                 gboolean available = TRUE;
1032                 char *uuid;
1033
1034                 uuid = &g_array_index(in_param1, char, 0);
1035
1036                 result = _bt_rfcomm_is_uuid_available(uuid, &available);
1037
1038                 g_array_append_vals(*out_param1, &available, sizeof(gboolean));
1039                 break;
1040         }
1041         case BT_RFCOMM_ACCEPT_CONNECTION: {
1042                 int socket_fd;
1043
1044                 socket_fd = g_array_index(in_param1, int, 0);
1045                 BT_DBG(" socket fd %d", socket_fd);
1046                 result = _bt_rfcomm_accept_connection();
1047                 break;
1048         }
1049         case BT_RFCOMM_REJECT_CONNECTION: {
1050                 int socket_fd;
1051
1052                 socket_fd = g_array_index(in_param1, int, 0);
1053                 BT_DBG(" socket fd %d", socket_fd);
1054                 result = _bt_rfcomm_reject_connection();
1055                 break;
1056         }
1057         case BT_CONNECT_LE: {
1058                 bluetooth_device_address_t address = { {0} };
1059                 gboolean auto_connect;
1060
1061                 address = g_array_index(in_param1,
1062                                 bluetooth_device_address_t, 0);
1063
1064                 auto_connect = g_array_index(in_param2,
1065                                 gboolean, 0);
1066
1067                 result = _bt_connect_le_device(&address, auto_connect);
1068
1069                 break;
1070         }
1071         case BT_DISCONNECT_LE: {
1072                 bluetooth_device_address_t address = { {0} };
1073
1074                 address = g_array_index(in_param1,
1075                                 bluetooth_device_address_t, 0);
1076
1077                 result = _bt_disconnect_le_device(&address);
1078
1079                 break;
1080         }
1081         case BT_SET_LE_PRIVACY: {
1082                 gboolean set_privacy;
1083
1084                 set_privacy = g_array_index(in_param1, gboolean, 0);
1085
1086                 result = _bt_set_le_privacy(set_privacy);
1087
1088                 break;
1089         }
1090         case BT_HDP_CONNECT:
1091         case BT_HDP_DISCONNECT:
1092         case BT_HDP_SEND_DATA:
1093
1094         case BT_GATT_GET_PRIMARY_SERVICES:
1095         case BT_GATT_DISCOVER_CHARACTERISTICS:
1096         case BT_GATT_SET_PROPERTY_REQUEST:
1097         case BT_GATT_READ_CHARACTERISTIC:
1098         case BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR:
1099                 /* Just call to check the privilege */
1100                 break;
1101         default:
1102                 result = BLUETOOTH_ERROR_INTERNAL;
1103                 break;
1104         }
1105
1106         return result;
1107 }
1108
1109 static int __bt_obexd_request(int function_name,
1110                 int request_type,
1111                 int request_id,
1112                 DBusGMethodInvocation *context,
1113                 GArray *in_param1,
1114                 GArray *in_param2,
1115                 GArray *in_param3,
1116                 GArray *in_param4,
1117                 GArray **out_param1)
1118 {
1119         BT_DBG("+");
1120
1121         int result;
1122
1123         BT_DBG("function_name : %x", function_name);
1124
1125         switch (function_name) {
1126         case BT_OPP_PUSH_FILES: {
1127                 BT_DBG("BT_OPP_PUSH_FILES");
1128                 int i;
1129                 bluetooth_device_address_t address = { {0} };
1130                 bt_file_path_t path;
1131                 char **file_path;
1132                 int file_count;
1133
1134                 address = g_array_index(in_param1,
1135                                 bluetooth_device_address_t, 0);
1136
1137                 file_count = g_array_index(in_param3, int, 0);
1138
1139                 file_path = g_new0(char *, file_count + 1);
1140
1141                 for (i = 0; i < file_count; i++) {
1142                         path = g_array_index(in_param2,
1143                                         bt_file_path_t, i);
1144
1145                         file_path[i] = g_strdup(path.path);
1146                 }
1147                 BT_DBG("_bt_opp_client_push_files");
1148                 result = _bt_opp_client_push_files(request_id, context,
1149                                                 &address, file_path,
1150                                                 file_count);
1151
1152                 for (i = 0; i < file_count; i++) {
1153                         g_free(file_path[i]);
1154                 }
1155
1156                 g_free(file_path);
1157
1158                 break;
1159         }
1160         case BT_OPP_CANCEL_PUSH: {
1161                 result = _bt_opp_client_cancel_push();
1162
1163                 break;
1164         }
1165         case BT_OPP_IS_PUSHING_FILES: {
1166                 gboolean is_sending = FALSE;
1167
1168                 result = _bt_opp_client_is_sending(&is_sending);
1169
1170                 g_array_append_vals(*out_param1, &is_sending,
1171                                 sizeof(gboolean));
1172                 break;
1173         }
1174         case BT_OBEX_SERVER_ALLOCATE: {
1175                 int app_pid;
1176                 gboolean is_native;
1177                 char *path;
1178                 char *sender;
1179
1180                 sender = dbus_g_method_get_sender(context);
1181                 path = &g_array_index(in_param1, char, 0);
1182                 is_native = g_array_index(in_param2, gboolean, 0);
1183                 app_pid = g_array_index(in_param3, int, 0);
1184
1185                 result = _bt_obex_server_allocate(sender, path, app_pid, is_native);
1186
1187                 g_free(sender);
1188                 break;
1189         }
1190         case BT_OBEX_SERVER_DEALLOCATE: {
1191                 int app_pid;
1192                 gboolean is_native;
1193
1194                 is_native = g_array_index(in_param1, gboolean, 0);
1195                 app_pid = g_array_index(in_param2, int, 0);
1196
1197                 result = _bt_obex_server_deallocate(app_pid, is_native);
1198                 break;
1199         }
1200         case BT_OBEX_SERVER_IS_ACTIVATED: {
1201                 gboolean is_activated = FALSE;
1202
1203                 result = _bt_obex_server_is_activated(&is_activated);
1204
1205                 g_array_append_vals(*out_param1, &is_activated,
1206                                 sizeof(gboolean));
1207
1208                 break;
1209         }
1210         case BT_OBEX_SERVER_ACCEPT_CONNECTION: {
1211                 result = _bt_obex_server_accept_connection(request_id);
1212
1213                 break;
1214         }
1215         case BT_OBEX_SERVER_REJECT_CONNECTION: {
1216                 result = _bt_obex_server_reject_connection();
1217
1218                 break;
1219         }
1220         case BT_OBEX_SERVER_ACCEPT_FILE: {
1221                 char *file_name;
1222
1223                 file_name = &g_array_index(in_param1, char, 0);
1224
1225                 result = _bt_obex_server_accept_authorize(file_name, TRUE);
1226
1227                 break;
1228         }
1229         case BT_OBEX_SERVER_REJECT_FILE: {
1230                 result = _bt_obex_server_reject_authorize();
1231
1232                 break;
1233         }
1234         case BT_OBEX_SERVER_SET_PATH: {
1235                 gboolean is_native;
1236                 char *destination_path;
1237
1238                 destination_path = &g_array_index(in_param1, char, 0);
1239                 is_native = g_array_index(in_param2, gboolean, 0);
1240
1241                 result = _bt_obex_server_set_destination_path(destination_path,
1242                                                         is_native);
1243
1244                 break;
1245         }
1246         case BT_OBEX_SERVER_SET_ROOT: {
1247                 char *root;
1248
1249                 root = &g_array_index(in_param1, char, 0);
1250
1251                 result = _bt_obex_server_set_root(root);
1252
1253                 break;
1254         }
1255         case BT_OBEX_SERVER_CANCEL_TRANSFER: {
1256                 int transfer_id;
1257
1258                 transfer_id = g_array_index(in_param1, int, 0);
1259
1260                 result = _bt_obex_server_cancel_transfer(transfer_id);
1261
1262                 break;
1263         }
1264         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS: {
1265                 result = _bt_obex_server_cancel_all_transfers();
1266
1267                 break;
1268         }
1269         case BT_OBEX_SERVER_IS_RECEIVING: {
1270                 gboolean is_receiving = FALSE;
1271
1272                 result = _bt_obex_server_is_receiving(&is_receiving);
1273
1274                 g_array_append_vals(*out_param1, &is_receiving,
1275                                 sizeof(gboolean));
1276                 break;
1277         }
1278         case BT_PBAP_CONNECT: {
1279                 bluetooth_device_address_t address = { {0} };
1280
1281                 address = g_array_index(in_param1,
1282                                 bluetooth_device_address_t, 0);
1283
1284                 result = _bt_pbap_connect(&address);
1285                 break;
1286         }
1287         case BT_PBAP_DISCONNECT: {
1288                 bluetooth_device_address_t address = { {0} };
1289
1290                 address = g_array_index(in_param1,
1291                                 bluetooth_device_address_t, 0);
1292
1293                 result = _bt_pbap_disconnect(&address);
1294                 break;
1295         }
1296         case BT_PBAP_GET_PHONEBOOK_SIZE: {
1297                 bluetooth_device_address_t address = { {0} };
1298                 bt_pbap_folder_t folder = { 0, };
1299
1300                 address = g_array_index(in_param1, bluetooth_device_address_t, 0);
1301                 folder = g_array_index(in_param2, bt_pbap_folder_t, 0);
1302
1303                 result = _bt_pbap_get_phonebook_size(&address, folder.addressbook,
1304                                         folder.folder_type);
1305                 break;
1306         }
1307         case BT_PBAP_GET_PHONEBOOK: {
1308                 bluetooth_device_address_t address = { {0} };
1309                 bt_pbap_folder_t folder = { 0, };
1310                 bt_pbap_pull_parameters_t app_param = { 0, };
1311
1312                 address = g_array_index(in_param1, bluetooth_device_address_t, 0);
1313                 folder = g_array_index(in_param2, bt_pbap_folder_t, 0);
1314                 app_param = g_array_index(in_param3, bt_pbap_pull_parameters_t, 0);
1315
1316                 result = _bt_pbap_get_phonebook(&address, folder.addressbook,
1317                                 folder.folder_type, &app_param);
1318                 break;
1319         }
1320         case BT_PBAP_GET_LIST: {
1321                 bluetooth_device_address_t address = { {0} };
1322                 bt_pbap_folder_t folder = { 0, };
1323                 bt_pbap_list_parameters_t app_param = { 0, };
1324
1325                 address = g_array_index(in_param1, bluetooth_device_address_t, 0);
1326                 folder = g_array_index(in_param2, bt_pbap_folder_t, 0);
1327                 app_param = g_array_index(in_param3, bt_pbap_list_parameters_t, 0);
1328
1329                 result = _bt_pbap_get_list(&address, folder.addressbook,
1330                                 folder.folder_type, &app_param);
1331                 break;
1332         }
1333         case BT_PBAP_PULL_VCARD: {
1334                 bluetooth_device_address_t address = { {0} };
1335                 bt_pbap_folder_t folder = { 0, };
1336                 bt_pbap_pull_vcard_parameters_t app_param = { 0, };
1337
1338                 address = g_array_index(in_param1, bluetooth_device_address_t, 0);
1339                 folder = g_array_index(in_param2, bt_pbap_folder_t, 0);
1340                 app_param = g_array_index(in_param3, bt_pbap_pull_vcard_parameters_t, 0);
1341
1342                 result = _bt_pbap_pull_vcard(&address, folder.addressbook,
1343                                 folder.folder_type, &app_param);
1344                 break;
1345         }
1346         case BT_PBAP_PHONEBOOK_SEARCH: {
1347                 bluetooth_device_address_t address = { {0} };
1348                 bt_pbap_folder_t folder = { 0, };
1349                 bt_pbap_search_parameters_t app_param = { 0, };
1350
1351                 address = g_array_index(in_param1, bluetooth_device_address_t, 0);
1352                 folder = g_array_index(in_param2, bt_pbap_folder_t, 0);
1353                 app_param = g_array_index(in_param3, bt_pbap_search_parameters_t, 0);
1354
1355                 result = _bt_pbap_phonebook_search(&address, folder.addressbook,
1356                                 folder.folder_type, &app_param);
1357                 break;
1358         }
1359
1360         default:
1361                 BT_ERR("Unknown function!");
1362                 result = BLUETOOTH_ERROR_INTERNAL;
1363                 break;
1364         }
1365
1366         BT_DBG("-");
1367
1368         return result;
1369 }
1370
1371 static int __bt_agent_request(int function_name,
1372                 int request_type,
1373                 int request_id,
1374                 DBusGMethodInvocation *context,
1375                 GArray *in_param1,
1376                 GArray *in_param2,
1377                 GArray *in_param3,
1378                 GArray *in_param4,
1379                 GArray **out_param1)
1380 {
1381         int result;
1382         switch(function_name) {
1383         case BT_SET_AUTHORIZATION: {
1384                 int type;
1385                 char *uuid;
1386                 char *path;
1387                 int fd;
1388
1389                 type = g_array_index(in_param1, int, 0);
1390                 uuid = &g_array_index(in_param2, char, 0);
1391                 path = &g_array_index(in_param3, char, 0);
1392                 fd = g_array_index(in_param4, int, 0);
1393                 result = _bt_register_osp_server_in_agent(type, uuid, path, fd);
1394                 break;
1395         }
1396         case BT_UNSET_AUTHORIZATION: {
1397                 int type;
1398                 char *uuid;
1399                 type = g_array_index(in_param1, int, 0);
1400                 uuid = &g_array_index(in_param2, char, 0);
1401                 result = _bt_unregister_osp_server_in_agent(type, uuid);
1402                 break;
1403         }
1404         default:
1405                 BT_ERR("Unknown function!");
1406                 result = BLUETOOTH_ERROR_INTERNAL;
1407                 break;
1408         }
1409
1410         return result;
1411 }
1412
1413 static int __bt_core_request(int function_name,
1414                 int request_type,
1415                 int request_id,
1416                 DBusGMethodInvocation *context,
1417                 GArray *in_param1)
1418 {
1419         int result;
1420
1421         switch (function_name) {
1422         case BT_ENABLE_ADAPTER:
1423         {
1424                 bt_status_t status;
1425
1426                 status = _bt_adapter_get_status();
1427
1428                 if (status == BT_ACTIVATING) {
1429                         BT_DBG("Enabling in progress");
1430                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1431                 } else if (status == BT_ACTIVATED) {
1432                         BT_DBG("Already enabled");
1433                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1434                 } else {
1435                         _bt_adapter_set_status(BT_ACTIVATING);
1436                         _bt_adapter_start_enable_timer();
1437                         result = BLUETOOTH_ERROR_NONE;
1438                 }
1439
1440                 break;
1441         }
1442         case BT_DISABLE_ADAPTER:
1443         {
1444                 bt_status_t status;
1445
1446                 status = _bt_adapter_get_status();
1447                 if (status == BT_DEACTIVATING) {
1448                                 BT_DBG("Disabling in progress");
1449                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
1450                 }
1451                 else if (status == BT_DEACTIVATED) {
1452                                 BT_DBG("Already disabled");
1453                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1454                 }
1455                 else
1456                 {
1457                         _bt_adapter_set_status(BT_DEACTIVATING);
1458                         result = BLUETOOTH_ERROR_NONE;
1459                 }
1460
1461                 break;
1462         }
1463         case BT_ENABLE_ADAPTER_LE:
1464         {
1465                 bt_le_status_t le_status;
1466
1467                 le_status = _bt_adapter_get_le_status();
1468                 if (le_status == BT_LE_ACTIVATING) {
1469                         BT_DBG("Enabling in progress");
1470                         result = BLUETOOTH_ERROR_IN_PROGRESS;
1471                 }
1472                 else if (le_status == BT_LE_ACTIVATED) {
1473                         BT_DBG("Already enabled");
1474                         result = BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1475                 } else {
1476                         _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1477                         _bt_adapter_start_le_enable_timer();
1478                         result = BLUETOOTH_ERROR_NONE;
1479                 }
1480
1481                 break;
1482         }
1483         case BT_DISABLE_ADAPTER_LE:
1484         {
1485                 bt_le_status_t le_status;
1486
1487                 le_status = _bt_adapter_get_le_status();
1488                 if (le_status == BT_LE_DEACTIVATING) {
1489                                 BT_DBG("Disabling in progress");
1490                                 result = BLUETOOTH_ERROR_IN_PROGRESS;
1491                 }
1492                 else if (le_status == BT_LE_DEACTIVATED) {
1493                                 BT_DBG("Already disabled");
1494                                 result = BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1495                 }
1496                 else
1497                 {
1498                         _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1499                         result = BLUETOOTH_ERROR_NONE;
1500                 }
1501
1502                 break;
1503         }
1504         default:
1505                 BT_ERR("Unknown function!");
1506                 result = BLUETOOTH_ERROR_INTERNAL;
1507                 break;
1508         }
1509
1510         return result;
1511 }
1512
1513 gboolean __bt_service_check_privilege(int function_name,
1514                                         int service_type,
1515                                         GArray *in_param5)
1516 {
1517         const char *cookie;
1518         int ret_val;
1519         gboolean result = TRUE;
1520
1521         cookie = (const char *)&g_array_index(in_param5, char, 0);
1522
1523         retv_if(cookie == NULL, FALSE);
1524
1525         switch (function_name) {
1526         case BT_SET_LOCAL_NAME:
1527         case BT_START_DISCOVERY:
1528         case BT_START_CUSTOM_DISCOVERY:
1529         case BT_CANCEL_DISCOVERY:
1530         case BT_OOB_ADD_REMOTE_DATA:
1531         case BT_OOB_REMOVE_REMOTE_DATA:
1532         case BT_SET_ADVERTISING:
1533         case BT_SET_CUSTOM_ADVERTISING:
1534         case BT_START_LE_DISCOVERY:
1535         case BT_STOP_LE_DISCOVERY:
1536
1537         case BT_BOND_DEVICE:
1538         case BT_CANCEL_BONDING:
1539         case BT_UNBOND_DEVICE:
1540         case BT_SET_ALIAS:
1541         case BT_SET_AUTHORIZATION:
1542         case BT_UNSET_AUTHORIZATION:
1543         case BT_SEARCH_SERVICE:
1544
1545         case BT_RFCOMM_CLIENT_CONNECT:
1546         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
1547         case BT_RFCOMM_SOCKET_DISCONNECT:
1548         case BT_RFCOMM_SOCKET_WRITE:
1549         case BT_RFCOMM_CREATE_SOCKET:
1550         case BT_RFCOMM_REMOVE_SOCKET:
1551
1552         case BT_OPP_PUSH_FILES:
1553         case BT_OPP_CANCEL_PUSH:
1554
1555         case BT_OBEX_SERVER_ACCEPT_CONNECTION:
1556         case BT_OBEX_SERVER_REJECT_CONNECTION:
1557         case BT_OBEX_SERVER_ACCEPT_FILE:
1558         case BT_OBEX_SERVER_REJECT_FILE:
1559         case BT_OBEX_SERVER_SET_PATH:
1560         case BT_OBEX_SERVER_SET_ROOT:
1561         case BT_OBEX_SERVER_CANCEL_TRANSFER:
1562         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS:
1563
1564         case BT_AUDIO_CONNECT:
1565         case BT_AUDIO_DISCONNECT:
1566         case BT_AG_CONNECT:
1567         case BT_AG_DISCONNECT:
1568         case BT_AV_CONNECT:
1569         case BT_AV_DISCONNECT:
1570         case BT_AVRCP_CONTROL_CONNECT:
1571         case BT_AVRCP_CONTROL_DISCONNECT:
1572         case BT_HF_CONNECT:
1573         case BT_HF_DISCONNECT:
1574
1575         case BT_HID_CONNECT:
1576         case BT_HID_DISCONNECT:
1577
1578         case BT_CONNECT_LE:
1579         case BT_DISCONNECT_LE:
1580
1581         case BT_SET_ADVERTISING_DATA:
1582         case BT_SET_SCAN_RESPONSE_DATA:
1583
1584         case BT_HDP_CONNECT:
1585         case BT_HDP_DISCONNECT:
1586         case BT_HDP_SEND_DATA:
1587
1588         case BT_NETWORK_ACTIVATE:
1589         case BT_NETWORK_DEACTIVATE:
1590         case BT_NETWORK_CONNECT:
1591         case BT_NETWORK_DISCONNECT:
1592         case BT_NETWORK_SERVER_DISCONNECT:
1593
1594         case BT_GATT_GET_PRIMARY_SERVICES:
1595         case BT_GATT_DISCOVER_CHARACTERISTICS:
1596         case BT_GATT_SET_PROPERTY_REQUEST:
1597         case BT_GATT_READ_CHARACTERISTIC:
1598         case BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR:
1599
1600                 ret_val = security_server_check_privilege_by_cookie(cookie,
1601                                                 BT_PRIVILEGE_PUBLIC, "w");
1602                 if (ret_val == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1603                         BT_ERR("[SMACK] Fail to access: %s", BT_PRIVILEGE_PUBLIC);
1604                         result = FALSE;
1605                 }
1606         break;
1607
1608         case BT_ENABLE_ADAPTER:
1609         case BT_DISABLE_ADAPTER:
1610         case BT_RESET_ADAPTER:
1611         case BT_RECOVER_ADAPTER:
1612         case BT_ENABLE_ADAPTER_LE:
1613         case BT_DISABLE_ADAPTER_LE:
1614         case BT_SET_CONNECTABLE:
1615         case BT_SET_DISCOVERABLE_MODE:
1616         case BT_ADD_WHITE_LIST:
1617         case BT_REMOVE_WHITE_LIST:
1618         case BT_CLEAR_WHITE_LIST:
1619         case BT_SET_MANUFACTURER_DATA:
1620         case BT_SET_SCAN_PARAMETERS:
1621
1622         case BT_CANCEL_SEARCH_SERVICE:
1623         case BT_ENABLE_RSSI:
1624
1625         case BT_RFCOMM_ACCEPT_CONNECTION:
1626         case BT_RFCOMM_REJECT_CONNECTION:
1627         case BT_RFCOMM_LISTEN:
1628
1629         case BT_AVRCP_SET_TRACK_INFO:
1630         case BT_AVRCP_SET_PROPERTY:
1631         case BT_AVRCP_SET_PROPERTIES:
1632         case BT_AVRCP_HANDLE_CONTROL:
1633         case BT_AVRCP_CONTROL_SET_PROPERTY:
1634         case BT_AVRCP_CONTROL_GET_PROPERTY:
1635         case BT_AVRCP_GET_TRACK_INFO:
1636
1637         case BT_SET_CONTENT_PROTECT:
1638         case BT_BOND_DEVICE_BY_TYPE:
1639         case BT_SET_LE_PRIVACY:
1640         case BT_LE_CONN_UPDATE:
1641
1642                 ret_val = security_server_check_privilege_by_cookie(cookie,
1643                                                 BT_PRIVILEGE_PLATFORM, "w");
1644
1645                 if (ret_val == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1646                         BT_ERR("[SMACK] Fail to access: %s", BT_PRIVILEGE_PLATFORM);
1647                         result = FALSE;
1648                 }
1649         break;
1650
1651         case BT_CHECK_ADAPTER:
1652         case BT_GET_RSSI:
1653
1654         case BT_GET_LOCAL_NAME:
1655         case BT_GET_LOCAL_ADDRESS:
1656         case BT_GET_LOCAL_VERSION:
1657         case BT_IS_SERVICE_USED:
1658         case BT_GET_DISCOVERABLE_MODE:
1659         case BT_GET_DISCOVERABLE_TIME:
1660         case BT_IS_DISCOVERYING:
1661         case BT_IS_LE_DISCOVERYING:
1662         case BT_IS_CONNECTABLE:
1663         case BT_GET_BONDED_DEVICES:
1664         case BT_GET_BONDED_DEVICE:
1665         case BT_IS_DEVICE_CONNECTED:
1666         case BT_GET_SPEAKER_GAIN:
1667         case BT_SET_SPEAKER_GAIN:
1668         case BT_OOB_READ_LOCAL_DATA:
1669         case BT_RFCOMM_CLIENT_IS_CONNECTED:
1670         case BT_RFCOMM_IS_UUID_AVAILABLE:
1671         case BT_GET_ADVERTISING_DATA:
1672         case BT_GET_SCAN_RESPONSE_DATA:
1673         case BT_IS_ADVERTISING:
1674
1675         case BT_OBEX_SERVER_ALLOCATE:
1676         case BT_OBEX_SERVER_DEALLOCATE:
1677
1678                 /* Non-privilege control */
1679                 break;
1680         default:
1681                 BT_ERR("Unknown function!");
1682                 result = FALSE;
1683                 break;
1684         }
1685
1686         return result;
1687 }
1688
1689 gboolean bt_service_request(
1690                 BtService *service,
1691                 int service_type,
1692                 int service_function,
1693                 int request_type,
1694                 GArray *in_param1,
1695                 GArray *in_param2,
1696                 GArray *in_param3,
1697                 GArray *in_param4,
1698                 GArray *in_param5,
1699                 DBusGMethodInvocation *context)
1700 {
1701         BT_DBG("+");
1702
1703         int result;
1704         int request_id = -1;
1705         GArray *out_param1 = NULL;
1706         GArray *out_param2 = NULL;
1707
1708         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
1709         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
1710
1711         if (service_type == BT_CORE_SERVICE) {
1712                 BT_DBG("No need to check privilege from bt-core");
1713         }
1714         else if (__bt_service_check_privilege(service_function,
1715                                 service_type, in_param5) == FALSE) {
1716                 result = BLUETOOTH_ERROR_PERMISSION_DEINED;
1717                 goto fail;
1718         }
1719
1720         if (request_type == BT_ASYNC_REQ
1721              || service_function == BT_OBEX_SERVER_ACCEPT_CONNECTION) {
1722                 /* Set the timer */
1723                 request_id = _bt_assign_request_id();
1724                 BT_DBG("Request ID: %d", request_id);
1725
1726                 if (request_id < 0) {
1727                         BT_ERR("Fail to assign the request id");
1728                         result = BLUETOOTH_ERROR_INTERNAL;
1729                         goto fail;
1730                 }
1731         }
1732
1733         BT_DBG("SERVICE TYPE %d", service_type);
1734
1735         switch (service_type) {
1736         case BT_BLUEZ_SERVICE:
1737                 result = __bt_bluez_request(service_function, request_type,
1738                                         request_id, context, in_param1, in_param2,
1739                                         in_param3, in_param4, &out_param1);
1740                 break;
1741         case BT_OBEX_SERVICE:
1742                 result = __bt_obexd_request(service_function, request_type,
1743                                         request_id, context, in_param1,
1744                                         in_param2, in_param3,
1745                                         in_param4, &out_param1);
1746                 break;
1747         case BT_AGENT_SERVICE:
1748                 result = __bt_agent_request(service_function, request_type,
1749                                         request_id, context, in_param1,
1750                                         in_param2, in_param3,
1751                                         in_param4, &out_param1);
1752                 break;
1753         case BT_CORE_SERVICE:
1754                 result = __bt_core_request(service_function, request_type,
1755                                         request_id, context, in_param1);
1756                 break;
1757         case BT_CHECK_PRIVILEGE:
1758                 result = BLUETOOTH_ERROR_NONE;
1759                 break;
1760         default:
1761                 BT_ERR("Unknown service type");
1762                 result = BLUETOOTH_ERROR_INTERNAL;
1763                 goto fail;
1764         }
1765
1766         if (result != BLUETOOTH_ERROR_NONE) {
1767                 goto fail;
1768         }
1769
1770         g_array_append_vals(out_param2, &result, sizeof(int));
1771
1772         if ((request_type == BT_ASYNC_REQ ||
1773                 service_function == BT_OBEX_SERVER_ACCEPT_CONNECTION) &&
1774                 service_function != BT_OPP_PUSH_FILES) {
1775                 _bt_insert_request_list(request_id, service_function,
1776                                         NULL, context);
1777         } else {
1778                 /* Return result */
1779                 if (service_type == BT_CHECK_PRIVILEGE ||
1780                         service_function != BT_OPP_PUSH_FILES)
1781                         dbus_g_method_return(context, out_param1, out_param2);
1782         }
1783
1784         g_array_free(out_param1, TRUE);
1785         g_array_free(out_param2, TRUE);
1786
1787         return TRUE;
1788 fail:
1789         BT_ERR_C("Request is failed [%s] [%x]", _bt_convert_error_to_string(result), result);
1790         g_array_append_vals(out_param2, &result, sizeof(int));
1791         dbus_g_method_return(context, out_param1, out_param2);
1792
1793         g_array_free(out_param1, TRUE);
1794         g_array_free(out_param2, TRUE);
1795
1796         if (request_type == BT_ASYNC_REQ)
1797                 _bt_delete_request_id(request_id);
1798
1799         BT_DBG("-");
1800
1801         return FALSE;
1802 }
1803
1804 int _bt_service_register(void)
1805 {
1806         BtService *bt_service;
1807         DBusGConnection *conn;
1808         DBusGProxy *proxy;
1809         GError *err = NULL;
1810         guint result = 0;
1811
1812         conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
1813         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1814
1815         proxy = dbus_g_proxy_new_for_name(conn, DBUS_SERVICE_DBUS,
1816                                 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
1817
1818         if (proxy == NULL) {
1819                 BT_ERR("proxy is NULL");
1820                 goto fail;
1821         }
1822
1823         if (!dbus_g_proxy_call(proxy, "RequestName", &err, G_TYPE_STRING,
1824                         BT_SERVICE_NAME, G_TYPE_UINT, 0, G_TYPE_INVALID,
1825                         G_TYPE_UINT, &result, G_TYPE_INVALID)) {
1826                 if (err != NULL) {
1827                         BT_ERR("RequestName RPC failed[%s]\n", err->message);
1828                         g_error_free(err);
1829                 }
1830                 g_object_unref(proxy);
1831
1832                 goto fail;
1833         }
1834
1835         g_object_unref(proxy);
1836
1837         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1838                 BT_ERR("Failed to get the primary well-known name.\n");
1839                 goto fail;
1840         }
1841
1842         bt_service = g_object_new(BT_SERVICE_TYPE, NULL);
1843
1844         dbus_g_connection_register_g_object(conn, BT_SERVICE_PATH,
1845                                         G_OBJECT(bt_service));
1846
1847         service_object = bt_service;
1848         bt_service_conn = conn;
1849
1850         return BLUETOOTH_ERROR_NONE;
1851
1852 fail:
1853         if (bt_service_conn) {
1854                 dbus_g_connection_unref(bt_service_conn);
1855                 bt_service_conn = NULL;
1856         }
1857
1858         return BLUETOOTH_ERROR_INTERNAL;
1859 }
1860
1861 void _bt_service_unregister(void)
1862 {
1863         if (bt_service_conn) {
1864                 if (service_object) {
1865                         dbus_g_connection_unregister_g_object(bt_service_conn,
1866                                                 G_OBJECT(service_object));
1867                         g_object_unref(service_object);
1868                         service_object = NULL;
1869                 }
1870
1871                 dbus_g_connection_unref(bt_service_conn);
1872                 bt_service_conn = NULL;
1873         }
1874 }
1875