1f116669dffe463802beefb3460143b9457824cc
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-request-handler.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
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
20 #include <string.h>
21 #include <dbus/dbus-glib.h>
22 #include <dbus/dbus-glib-bindings.h>
23 #include <dbus/dbus-glib-lowlevel.h>
24 #include <glib.h>
25 #include <dlog.h>
26
27 #include "bluetooth-api.h"
28 #include "bt-service-common.h"
29 #include "bt-service-util.h"
30 #include "bt-service-event.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-device.h"
33 #include "bt-service-hid.h"
34 #include "bt-service-network.h"
35 #include "bt-service-audio.h"
36 #include "bt-service-oob.h"
37 #include "bt-service-avrcp.h"
38 #include "bt-service-opp-client.h"
39 #include "bt-service-obex-server.h"
40 #include "bt-service-rfcomm-client.h"
41 #include "bt-service-rfcomm-server.h"
42 #include "bt-request-handler.h"
43
44 /* auto generated header by bt-request-service.xml*/
45 #include "bt-service-method.h"
46
47 DBusGConnection *bt_service_conn;
48 BtService *service_object;
49
50 GType bt_service_get_type (void);
51
52 G_DEFINE_TYPE(BtService, bt_service, G_TYPE_OBJECT);
53
54 /*This is part of platform provided code skeleton for client server model*/
55 static void bt_service_class_init (BtServiceClass *service_class)
56 {
57         dbus_g_object_type_install_info(G_TYPE_FROM_CLASS(service_class),
58                                         &dbus_glib_bt_object_info);
59 }
60
61 /*This is part of platform provided code skeleton for client server model*/
62 static void bt_service_init (BtService *service)
63 {
64 }
65
66 static int __bt_bluez_request(int function_name,
67                 int request_type,
68                 int request_id,
69                 DBusGMethodInvocation *context,
70                 GArray *in_param1,
71                 GArray *in_param2,
72                 GArray *in_param3,
73                 GArray *in_param4,
74                 GArray **out_param1)
75 {
76         int result = BLUETOOTH_ERROR_NONE;
77
78         switch (function_name) {
79         case BT_ENABLE_ADAPTER:
80                 result = _bt_enable_adapter();
81                 break;
82         case BT_DISABLE_ADAPTER:
83                 result = _bt_disable_adapter();
84                 break;
85         case BT_RESET_ADAPTER:
86                 result = _bt_reset_adapter();
87                 break;
88         case BT_CHECK_ADAPTER: {
89                 int enabled = 0;
90
91                 result = _bt_check_adapter(&enabled);
92
93                 g_array_append_vals(*out_param1, &enabled,
94                                 sizeof(int));
95                 break;
96         }
97         case BT_GET_LOCAL_ADDRESS: {
98                 bluetooth_device_address_t local_address = { {0} };
99                 result = _bt_get_local_address(&local_address);
100
101                 g_array_append_vals(*out_param1, &local_address,
102                                 sizeof(bluetooth_device_address_t));
103                 break;
104         }
105         case BT_GET_LOCAL_NAME: {
106                 bluetooth_device_name_t local_name = { {0} };
107                 result = _bt_get_local_name(&local_name);
108
109                 g_array_append_vals(*out_param1, &local_name,
110                                 sizeof(bluetooth_device_name_t));
111
112                 break;
113         }
114         case BT_SET_LOCAL_NAME: {
115                 bluetooth_device_name_t local_name = { {0} };
116
117                 local_name = g_array_index(in_param1,
118                                 bluetooth_device_name_t, 0);
119
120                 result = _bt_set_local_name(local_name.name);
121
122                 break;
123         }
124         case BT_IS_SERVICE_USED: {
125                 char *uuid;
126                 gboolean used = FALSE;
127
128                 uuid = &g_array_index(in_param1, char, 0);
129
130                 result = _bt_is_service_used(uuid, &used);
131
132                 if (result == BLUETOOTH_ERROR_NONE) {
133                         g_array_append_vals(*out_param1, &used,
134                                                 sizeof(gboolean));
135                 }
136                 break;
137         }
138         case BT_GET_DISCOVERABLE_MODE: {
139                 int mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
140
141                 result = _bt_get_discoverable_mode(&mode);
142
143                 g_array_append_vals(*out_param1, &mode, sizeof(int));
144                 break;
145         }
146         case BT_SET_DISCOVERABLE_MODE: {
147                 int mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
148                 int time = 0;
149
150                 mode = g_array_index(in_param1, int, 0);
151                 time = g_array_index(in_param2, int, 0);
152
153                 result = _bt_set_discoverable_mode(mode, time);
154                 break;
155         }
156         case BT_GET_DISCOVERABLE_TIME: {
157                 int timeout = 0;
158
159                 result = _bt_get_timeout_value(&timeout);
160
161                 g_array_append_vals(*out_param1, &timeout, sizeof(int));
162                 break;
163         }
164         case BT_START_DISCOVERY:
165                 result = _bt_start_discovery();
166                 break;
167
168         case BT_CANCEL_DISCOVERY:
169                 result = _bt_cancel_discovery();
170                 break;
171
172         case BT_IS_DISCOVERYING: {
173                 gboolean discovering = FALSE;
174                 discovering = _bt_is_discovering();
175
176                 g_array_append_vals(*out_param1, &discovering, sizeof(gboolean));
177                 break;
178         }
179         case BT_GET_BONDED_DEVICES:
180                 result = _bt_get_bonded_devices(out_param1);
181                 break;
182
183         case BT_GET_BONDED_DEVICE: {
184                 bluetooth_device_address_t address = { {0} };
185                 bluetooth_device_info_t dev_info;
186
187                 address = g_array_index(in_param1,
188                                 bluetooth_device_address_t, 0);
189
190                 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
191                 result = _bt_get_bonded_device_info(&address, &dev_info);
192
193                 if (result == BLUETOOTH_ERROR_NONE) {
194                         g_array_append_vals(*out_param1, &dev_info,
195                                         sizeof(bluetooth_device_info_t));
196                 }
197                 break;
198         }
199         case BT_BOND_DEVICE: {
200                 bluetooth_device_address_t address = { {0} };
201
202                 address = g_array_index(in_param1,
203                                 bluetooth_device_address_t, 0);
204
205                 result = _bt_bond_device(request_id, &address, out_param1);
206                 break;
207         }
208         case BT_CANCEL_BONDING: {
209                 result = _bt_cancel_bonding();
210                 break;
211         }
212         case BT_UNBOND_DEVICE: {
213                 bluetooth_device_address_t address = { {0} };
214
215                 address = g_array_index(in_param1,
216                                 bluetooth_device_address_t, 0);
217
218                 result = _bt_unbond_device(request_id, &address, out_param1);
219
220                 break;
221         }
222         case BT_SET_ALIAS: {
223                 bluetooth_device_address_t address = { {0} };
224                 const char *local_name;
225
226                 address = g_array_index(in_param1,
227                                 bluetooth_device_address_t, 0);
228
229                 local_name = &g_array_index(in_param2, char, 0);
230
231                 result = _bt_set_alias(&address, local_name);
232                 break;
233         }
234         case BT_SEARCH_SERVICE: {
235                 bluetooth_device_address_t address = { {0} };
236
237                 address = g_array_index(in_param1,
238                                 bluetooth_device_address_t, 0);
239
240                 result = _bt_search_device(request_id, &address);
241                 if (result != BLUETOOTH_ERROR_NONE) {
242                         g_array_append_vals(*out_param1, &address,
243                                         sizeof(bluetooth_device_address_t));
244                 }
245                 break;
246         }
247         case BT_CANCEL_SEARCH_SERVICE: {
248                 result = _bt_cancel_search_device();
249                 break;
250         }
251         case BT_SET_AUTHORIZATION: {
252                 bluetooth_device_address_t address = { {0} };
253                 gboolean authorize;
254
255                 address = g_array_index(in_param1,
256                                 bluetooth_device_address_t, 0);
257
258                 authorize = g_array_index(in_param2, gboolean, 0);
259
260                 result = _bt_set_authorization(&address, authorize);
261                 break;
262         }
263         case BT_IS_DEVICE_CONNECTED: {
264                 bluetooth_device_address_t address = { {0} };
265                 int type;
266                 gboolean connected = FALSE;
267
268                 address = g_array_index(in_param1,
269                                 bluetooth_device_address_t, 0);
270
271                 type = g_array_index(in_param2, int, 0);
272
273                 result = _bt_is_device_connected(&address, type, &connected);
274
275                 if (result == BLUETOOTH_ERROR_NONE) {
276                         g_array_append_vals(*out_param1, &connected,
277                                                 sizeof(gboolean));
278                 }
279
280                 break;
281         }
282         case BT_HID_CONNECT: {
283                 bluetooth_device_address_t address = { {0} };
284
285                 address = g_array_index(in_param1,
286                                 bluetooth_device_address_t, 0);
287
288                 result = _bt_hid_connect(request_id, &address);
289                 if (result != BLUETOOTH_ERROR_NONE) {
290                         g_array_append_vals(*out_param1, &address,
291                                         sizeof(bluetooth_device_address_t));
292                 }
293                 break;
294         }
295         case BT_HID_DISCONNECT: {
296                 bluetooth_device_address_t address = { {0} };
297
298                 address = g_array_index(in_param1,
299                                 bluetooth_device_address_t, 0);
300
301                 result = _bt_hid_disconnect(request_id, &address);
302                 if (result != BLUETOOTH_ERROR_NONE) {
303                         g_array_append_vals(*out_param1, &address,
304                                         sizeof(bluetooth_device_address_t));
305                 }
306                 break;
307         }
308         case BT_NETWORK_ACTIVATE:
309                 result = _bt_network_activate();
310                 break;
311         case BT_NETWORK_DEACTIVATE:
312                 result = _bt_network_deactivate();
313                 break;
314         case BT_NETWORK_CONNECT: {
315                 bluetooth_device_address_t address = { {0} };
316                 int role;
317
318                 address = g_array_index(in_param1,
319                                 bluetooth_device_address_t, 0);
320
321                 role = g_array_index(in_param2, int, 0);
322
323                 result = _bt_network_connect(request_id, role, &address);
324                 if (result != BLUETOOTH_ERROR_NONE) {
325                         g_array_append_vals(*out_param1, &address,
326                                         sizeof(bluetooth_device_address_t));
327                 }
328                 break;
329         }
330         case BT_NETWORK_DISCONNECT: {
331                 bluetooth_device_address_t address = { {0} };
332
333                 address = g_array_index(in_param1,
334                                 bluetooth_device_address_t, 0);
335
336                 result = _bt_network_disconnect(request_id, &address);
337                 if (result != BLUETOOTH_ERROR_NONE) {
338                         g_array_append_vals(*out_param1, &address,
339                                         sizeof(bluetooth_device_address_t));
340                 }
341                 break;
342         }
343         case BT_AUDIO_CONNECT: {
344                 bluetooth_device_address_t address = { {0} };
345
346                 address = g_array_index(in_param1,
347                                 bluetooth_device_address_t, 0);
348
349                 result = _bt_audio_connect(request_id, BT_AUDIO_ALL,
350                                         &address, out_param1);
351                 break;
352         }
353         case BT_AUDIO_DISCONNECT: {
354                 bluetooth_device_address_t address = { {0} };
355
356                 address = g_array_index(in_param1,
357                                 bluetooth_device_address_t, 0);
358
359                 result = _bt_audio_disconnect(request_id, BT_AUDIO_ALL,
360                                         &address, out_param1);
361                 break;
362         }
363         case BT_AG_CONNECT: {
364                 bluetooth_device_address_t address = { {0} };
365
366                 address = g_array_index(in_param1,
367                                 bluetooth_device_address_t, 0);
368
369                 result = _bt_audio_connect(request_id, BT_AUDIO_HSP,
370                                         &address, out_param1);
371                 break;
372         }
373         case BT_AG_DISCONNECT: {
374                 bluetooth_device_address_t address = { {0} };
375
376                 address = g_array_index(in_param1,
377                                 bluetooth_device_address_t, 0);
378
379                 result = _bt_audio_disconnect(request_id, BT_AUDIO_HSP,
380                                         &address, out_param1);
381                 break;
382         }
383         case BT_AV_CONNECT: {
384                 bluetooth_device_address_t address = { {0} };
385
386                 address = g_array_index(in_param1,
387                                 bluetooth_device_address_t, 0);
388
389                 result = _bt_audio_connect(request_id, BT_AUDIO_A2DP,
390                                         &address, out_param1);
391                 break;
392         }
393         case BT_AV_DISCONNECT: {
394                 bluetooth_device_address_t address = { {0} };
395
396                 address = g_array_index(in_param1,
397                                 bluetooth_device_address_t, 0);
398
399                 result = _bt_audio_disconnect(request_id, BT_AUDIO_A2DP,
400                                         &address, out_param1);
401                 break;
402         }
403         case BT_GET_SPEAKER_GAIN: {
404                 unsigned int volume;
405
406                 result = _bt_audio_get_speaker_gain(&volume);
407
408                 g_array_append_vals(*out_param1, &volume,
409                                 sizeof(unsigned int));
410                 break;
411         }
412         case BT_SET_SPEAKER_GAIN: {
413                 unsigned int volume;
414
415                 volume = g_array_index(in_param1,
416                                 unsigned int, 0);
417
418                 result = _bt_audio_set_speaker_gain(volume);
419
420                 break;
421         }
422         case BT_OOB_READ_LOCAL_DATA: {
423                 bt_oob_data_t local_oob_data;
424
425                 memset(&local_oob_data, 0x00, sizeof(bt_oob_data_t));
426                 result = _bt_oob_read_local_data(&local_oob_data);
427
428                 g_array_append_vals(*out_param1, &local_oob_data,
429                                 sizeof(bt_oob_data_t));
430
431                 break;
432         }
433         case BT_OOB_ADD_REMOTE_DATA: {
434                 bluetooth_device_address_t address = { {0} };
435                 bt_oob_data_t local_oob_data;
436
437                 address = g_array_index(in_param1,
438                                 bluetooth_device_address_t, 0);
439
440                 local_oob_data = g_array_index(in_param2,
441                                 bt_oob_data_t, 0);
442
443                 result = _bt_oob_add_remote_data(&address, &local_oob_data);
444
445                 break;
446         }
447         case BT_OOB_REMOVE_REMOTE_DATA: {
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_oob_remove_remote_data(&address);
454
455                 break;
456         }
457         case BT_AVRCP_SET_TRACK_INFO: {
458                 media_metadata_t data;
459                 media_metadata_attributes_t meta_data;
460
461                 memset(&data, 0x00, sizeof(media_metadata_t));
462                 memset(&meta_data, 0x00, sizeof(media_metadata_attributes_t));
463
464                 data = g_array_index(in_param1,
465                                 media_metadata_t, 0);
466
467                 meta_data.title = g_strdup(data.title);
468                 meta_data.artist = g_strdup(data.artist);
469                 meta_data.album = g_strdup(data.album);
470                 meta_data.genre = g_strdup(data.genre);
471                 meta_data.total_tracks = data.total_tracks;
472                 meta_data.number = data.number;
473                 meta_data.duration = data.duration;
474
475                 result = _bt_avrcp_set_track_info(&meta_data);
476
477                 g_free((gpointer)meta_data.title);
478                 g_free((gpointer)meta_data.artist);
479                 g_free((gpointer)meta_data.album);
480                 g_free((gpointer)meta_data.genre);
481
482                 break;
483         }
484         case BT_AVRCP_SET_PROPERTY: {
485                 int type;
486                 unsigned int value;
487
488                 type = g_array_index(in_param1, int, 0);
489                 value = g_array_index(in_param2, unsigned int, 0);
490
491                 result = _bt_avrcp_set_property(type, value);
492
493                 break;
494         }
495         case BT_AVRCP_SET_PROPERTIES: {
496                 media_player_settings_t properties;
497
498                 memset(&properties, 0x00, sizeof(media_player_settings_t));
499
500                 properties = g_array_index(in_param1,
501                                 media_player_settings_t, 0);
502
503                 result = _bt_avrcp_set_properties(&properties);
504
505                 break;
506         }
507         case BT_RFCOMM_CLIENT_CONNECT: {
508                 bluetooth_device_address_t address = { {0} };
509                 char *input_string;
510                 int connect_type;
511
512                 address = g_array_index(in_param1,
513                                 bluetooth_device_address_t, 0);
514
515                 input_string = &g_array_index(in_param2, char, 0);
516
517                 connect_type = g_array_index(in_param3, int, 0);
518
519                 if (connect_type == BT_RFCOMM_UUID) {
520                         result = _bt_rfcomm_connect_using_uuid(request_id,
521                                                         &address, input_string);
522                 } else {
523                         result = _bt_rfcomm_connect_using_channel(request_id,
524                                                         &address, input_string);
525                 }
526
527                 if (result != BLUETOOTH_ERROR_NONE) {
528                         g_array_append_vals(*out_param1, &address,
529                                         sizeof(bluetooth_device_address_t));
530                 }
531                 break;
532         }
533         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
534                 result = _bt_rfcomm_cancel_connect();
535                 break;
536         case BT_RFCOMM_CLIENT_IS_CONNECTED: {
537                 gboolean connected = FALSE;
538                 result = _bt_rfcomm_is_connected(&connected);
539
540                 g_array_append_vals(*out_param1, &connected, sizeof(gboolean));
541                 break;
542         }
543         case BT_RFCOMM_SOCKET_DISCONNECT: {
544                 int socket_fd;
545
546                 socket_fd = g_array_index(in_param1, int, 0);
547
548                 result = _bt_rfcomm_disconnect(socket_fd);
549                 break;
550         }
551         case BT_RFCOMM_SOCKET_WRITE: {
552                 int socket_fd;
553                 int length;
554                 char *buffer;
555
556                 socket_fd = g_array_index(in_param1, int, 0);
557                 length = g_array_index(in_param2, int, 0);
558                 buffer = &g_array_index(in_param3, char, 0);
559
560                 result = _bt_rfcomm_write(socket_fd, buffer, length);
561                 break;
562         }
563         case BT_RFCOMM_CREATE_SOCKET: {
564                 char *sender;
565                 char *uuid;
566                 int socket_fd = -1;
567                 int result;
568
569                 sender = dbus_g_method_get_sender(context);
570                 uuid = &g_array_index(in_param1, char, 0);
571
572                 result = _bt_rfcomm_create_socket(sender, uuid);
573
574                 if (result > 0) {
575                         socket_fd = result;
576                         result = BLUETOOTH_ERROR_NONE;
577                 }
578
579                 g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
580
581                 g_free(sender);
582                 break;
583         }
584         case BT_RFCOMM_REMOVE_SOCKET: {
585                 int socket_fd;
586
587                 socket_fd = g_array_index(in_param1, int, 0);
588
589                 result = _bt_rfcomm_remove_socket(socket_fd);
590                 break;
591         }
592         case BT_RFCOMM_LISTEN: {
593                 int socket_fd;
594                 int pending;
595                 gboolean is_native;
596
597                 socket_fd = g_array_index(in_param1, int, 0);
598                 pending = g_array_index(in_param2, int, 0);
599                 is_native = g_array_index(in_param3, gboolean, 0);
600
601                 result = _bt_rfcomm_listen(socket_fd, pending, is_native);
602                 break;
603         }
604         case BT_RFCOMM_IS_UUID_AVAILABLE: {
605                 gboolean available = TRUE;
606                 char *uuid;
607
608                 uuid = &g_array_index(in_param1, char, 0);
609
610                 result = _bt_rfcomm_is_uuid_available(uuid, &available);
611
612                 g_array_append_vals(*out_param1, &available, sizeof(gboolean));
613                 break;
614         }
615         case BT_RFCOMM_ACCEPT_CONNECTION: {
616                 int socket_fd;
617
618                 socket_fd = g_array_index(in_param1, int, 0);
619
620                 result = _bt_rfcomm_accept_connection(socket_fd, request_id);
621                 break;
622         }
623         case BT_RFCOMM_REJECT_CONNECTION: {
624                 int socket_fd;
625
626                 socket_fd = g_array_index(in_param1, int, 0);
627
628                 result = _bt_rfcomm_reject_connection(socket_fd);
629                 break;
630         }
631         default:
632                 result = BLUETOOTH_ERROR_INTERNAL;
633                 break;
634         }
635
636         return result;
637 }
638
639 static int __bt_obexd_request(int function_name,
640                 int request_type,
641                 int request_id,
642                 DBusGMethodInvocation *context,
643                 GArray *in_param1,
644                 GArray *in_param2,
645                 GArray *in_param3,
646                 GArray *in_param4,
647                 GArray **out_param1)
648 {
649         int result;
650
651         switch (function_name) {
652         case BT_OPP_PUSH_FILES: {
653                 int i;
654                 bluetooth_device_address_t address = { {0} };
655                 bt_file_path_t path;
656                 char **file_path;
657                 int file_count;
658
659                 address = g_array_index(in_param1,
660                                 bluetooth_device_address_t, 0);
661
662                 file_count = g_array_index(in_param3, int, 0);
663
664                 file_path = g_new0(char *, file_count + 1);
665
666                 for (i = 0; i < file_count; i++) {
667                         path = g_array_index(in_param2,
668                                         bt_file_path_t, i);
669
670                         file_path[i] = g_strdup(path.path);
671                 }
672
673                 result = _bt_opp_client_push_files(request_id, context,
674                                                 &address, file_path,
675                                                 file_count);
676
677                 for (i = 0; i < file_count; i++) {
678                         g_free(file_path[i]);
679                 }
680
681                 g_free(file_path);
682
683                 break;
684         }
685         case BT_OPP_CANCEL_PUSH: {
686                 result = _bt_opp_client_cancel_push();
687
688                 break;
689         }
690         case BT_OPP_IS_PUSHING_FILES: {
691                 gboolean is_sending = FALSE;
692
693                 result = _bt_opp_client_is_sending(&is_sending);
694
695                 g_array_append_vals(*out_param1, &is_sending,
696                                 sizeof(gboolean));
697                 break;
698         }
699         case BT_OBEX_SERVER_ALLOCATE: {
700                 int app_pid;
701                 gboolean is_native;
702                 char *path;
703                 char *sender;
704
705                 sender = dbus_g_method_get_sender(context);
706                 path = &g_array_index(in_param1, char, 0);
707                 is_native = g_array_index(in_param2, gboolean, 0);
708                 app_pid = g_array_index(in_param3, int, 0);
709
710                 result = _bt_obex_server_allocate(sender, path, app_pid, is_native);
711
712                 g_free(sender);
713                 break;
714         }
715         case BT_OBEX_SERVER_DEALLOCATE: {
716                 int app_pid;
717                 gboolean is_native;
718
719                 is_native = g_array_index(in_param1, gboolean, 0);
720                 app_pid = g_array_index(in_param2, int, 0);
721
722                 result = _bt_obex_server_deallocate(app_pid, is_native);
723                 break;
724         }
725         case BT_OBEX_SERVER_IS_ACTIVATED: {
726                 gboolean is_activated = FALSE;
727
728                 result = _bt_obex_server_is_activated(&is_activated);
729
730                 g_array_append_vals(*out_param1, &is_activated,
731                                 sizeof(gboolean));
732
733                 break;
734         }
735         case BT_OBEX_SERVER_ACCEPT_CONNECTION: {
736                 result = _bt_obex_server_accept_connection(request_id);
737
738                 break;
739         }
740         case BT_OBEX_SERVER_REJECT_CONNECTION: {
741                 result = _bt_obex_server_reject_connection();
742
743                 break;
744         }
745         case BT_OBEX_SERVER_ACCEPT_FILE: {
746                 char *file_name;
747
748                 file_name = &g_array_index(in_param1, char, 0);
749
750                 result = _bt_obex_server_accept_authorize(file_name, TRUE);
751
752                 break;
753         }
754         case BT_OBEX_SERVER_REJECT_FILE: {
755                 result = _bt_obex_server_reject_authorize();
756
757                 break;
758         }
759         case BT_OBEX_SERVER_SET_PATH: {
760                 gboolean is_native;
761                 char *destination_path;
762
763                 destination_path = &g_array_index(in_param1, char, 0);
764                 is_native = g_array_index(in_param2, gboolean, 0);
765
766                 result = _bt_obex_server_set_destination_path(destination_path,
767                                                         is_native);
768
769                 break;
770         }
771         case BT_OBEX_SERVER_SET_ROOT: {
772                 char *root;
773
774                 root = &g_array_index(in_param1, char, 0);
775
776                 result = _bt_obex_server_set_root(root);
777
778                 break;
779         }
780         case BT_OBEX_SERVER_CANCEL_TRANSFER: {
781                 int transfer_id;
782
783                 transfer_id = g_array_index(in_param1, int, 0);
784
785                 result = _bt_obex_server_cancel_transfer(transfer_id);
786
787                 break;
788         }
789         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS: {
790                 result = _bt_obex_server_cancel_all_transfers();
791
792                 break;
793         }
794         default:
795                 BT_ERR("Unknown function!");
796                 result = BLUETOOTH_ERROR_INTERNAL;
797                 break;
798         }
799
800         return result;
801 }
802
803 gboolean bt_service_request(
804                 BtService *service,
805                 int service_type,
806                 int service_function,
807                 int request_type,
808                 GArray *in_param1,
809                 GArray *in_param2,
810                 GArray *in_param3,
811                 GArray *in_param4,
812                 GArray *in_param5,
813                 DBusGMethodInvocation *context)
814 {
815         int result;
816         int request_id = -1;
817         GArray *out_param1 = NULL;
818         GArray *out_param2 = NULL;
819
820         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
821         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
822
823         if (request_type == BT_ASYNC_REQ
824              || service_function == BT_OBEX_SERVER_ACCEPT_CONNECTION
825               || service_function == BT_RFCOMM_ACCEPT_CONNECTION) {
826                 /* Set the timer */
827                 request_id = _bt_assign_request_id();
828                 if (request_id < 0) {
829                         BT_ERR("Fail to assign the request id");
830                         result = BLUETOOTH_ERROR_INTERNAL;
831                         goto fail;
832                 }
833         }
834
835         switch (service_type) {
836         case BT_BLUEZ_SERVICE:
837                 result = __bt_bluez_request(service_function, request_type,
838                                         request_id, context, in_param1, in_param2,
839                                         in_param3, in_param4, &out_param1);
840                 break;
841         case BT_OBEX_SERVICE:
842                 result = __bt_obexd_request(service_function, request_type,
843                                         request_id, context, in_param1,
844                                         in_param2, in_param3,
845                                         in_param4, &out_param1);
846                 break;
847         default:
848                 BT_ERR("Unknown service type");
849                 result = BLUETOOTH_ERROR_INTERNAL;
850                 goto fail;
851         }
852
853         if (result != BLUETOOTH_ERROR_NONE) {
854                 BT_ERR("result is not error none: %x", result);
855                 goto fail;
856         }
857
858         g_array_append_vals(out_param2, &result, sizeof(int));
859
860         if (request_type == BT_ASYNC_REQ
861              || service_function == BT_OBEX_SERVER_ACCEPT_CONNECTION
862               || service_function == BT_RFCOMM_ACCEPT_CONNECTION) {
863                 _bt_insert_request_list(request_id, service_function,
864                                         NULL, context);
865         } else {
866                 /* Return result */
867                 dbus_g_method_return(context, out_param1, out_param2);
868         }
869
870         g_array_free(out_param1, TRUE);
871         g_array_free(out_param2, TRUE);
872
873         return TRUE;
874 fail:
875         g_array_append_vals(out_param2, &result, sizeof(int));
876         dbus_g_method_return(context, out_param1, out_param2);
877
878         g_array_free(out_param1, TRUE);
879         g_array_free(out_param2, TRUE);
880
881         if (request_type == BT_ASYNC_REQ)
882                 _bt_delete_request_id(request_id);
883
884         return FALSE;
885 }
886
887 int _bt_service_register(void)
888 {
889         BtService *bt_service;
890         DBusGConnection *conn;
891         DBusGProxy *proxy;
892         GError* err = NULL;
893         guint result = 0;
894
895         conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
896         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
897
898         proxy = dbus_g_proxy_new_for_name(conn, DBUS_SERVICE_DBUS,
899                                 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
900
901         if (proxy == NULL) {
902                 BT_ERR("proxy is NULL");
903                 goto fail;
904         }
905
906         if (!dbus_g_proxy_call(proxy, "RequestName", &err, G_TYPE_STRING,
907                         BT_SERVICE_NAME, G_TYPE_UINT, 0, G_TYPE_INVALID,
908                         G_TYPE_UINT, &result, G_TYPE_INVALID)) {
909                 if (err != NULL) {
910                         BT_ERR("RequestName RPC failed[%s]\n", err->message);
911                         g_error_free(err);
912                 }
913                 g_object_unref(proxy);
914
915                 goto fail;
916         }
917
918         g_object_unref(proxy);
919
920         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
921                 BT_ERR("Failed to get the primary well-known name.\n");
922                 goto fail;
923         }
924
925         bt_service = g_object_new(BT_SERVICE_TYPE, NULL);
926         if (bt_service == NULL) {
927                 BT_ERR("bt_service is NULL");
928                 goto fail;
929         }
930
931         dbus_g_connection_register_g_object(conn, BT_SERVICE_PATH,
932                                         G_OBJECT(bt_service));
933
934         service_object = bt_service;
935         bt_service_conn = conn;
936
937         return BLUETOOTH_ERROR_NONE;
938
939 fail:
940         if (bt_service_conn) {
941                 dbus_g_connection_unref(bt_service_conn);
942                 bt_service_conn = NULL;
943         }
944
945         return BLUETOOTH_ERROR_INTERNAL;
946 }
947
948 void _bt_service_unregister(void)
949 {
950         if (bt_service_conn) {
951                 if (service_object) {
952                         dbus_g_connection_unregister_g_object(bt_service_conn,
953                                                 G_OBJECT(service_object));
954                         g_object_unref(service_object);
955                         service_object = NULL;
956                 }
957
958                 dbus_g_connection_unref(bt_service_conn);
959                 bt_service_conn = NULL;
960         }
961 }
962