6dac47e0dc89ef9b8a2312e2cb5bcdaa889acb3e
[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_player_settings_t properties;
460
461                 memset(&data, 0x00, sizeof(media_metadata_t));
462                 memset(&properties, 0x00, sizeof(media_player_settings_t));
463
464                 data = g_array_index(in_param1,
465                                         media_metadata_t, 0);
466
467                 properties.metadata.title = g_strdup(data.title);
468                 properties.metadata.artists = g_malloc0(sizeof(char *));
469                 properties.metadata.artists[0] = g_strdup(data.artist);
470                 properties.metadata.album = g_strdup(data.album);
471                 properties.metadata.genres = g_malloc0(sizeof(char *));
472                 properties.metadata.genres[0] = g_strdup(data.genre);
473                 properties.metadata.tracknumber = data.number;
474                 properties.metadata.duration = data.duration;
475
476                 result = _bt_avrcp_set_properties(&properties);
477
478                 g_free((gpointer)properties.metadata.title);
479                 g_free((gpointer)properties.metadata.artists);
480                 g_free((gpointer)properties.metadata.album);
481                 g_free((gpointer)properties.metadata.genres);
482
483                 break;
484         }
485         case BT_AVRCP_SET_PROPERTY: {
486                 int type;
487                 unsigned int value;
488
489                 type = g_array_index(in_param1, int, 0);
490                 value = g_array_index(in_param2, unsigned int, 0);
491
492                 result = _bt_avrcp_set_property(type, value);
493
494                 break;
495         }
496         case BT_AVRCP_SET_PROPERTIES: {
497                 media_player_settings_t properties;
498
499                 memset(&properties, 0x00, sizeof(media_player_settings_t));
500
501                 properties = g_array_index(in_param1,
502                                 media_player_settings_t, 0);
503
504                 result = _bt_avrcp_set_properties(&properties);
505
506                 break;
507         }
508         case BT_RFCOMM_CLIENT_CONNECT: {
509                 bluetooth_device_address_t address = { {0} };
510                 char *input_string;
511                 int connect_type;
512
513                 address = g_array_index(in_param1,
514                                 bluetooth_device_address_t, 0);
515
516                 input_string = &g_array_index(in_param2, char, 0);
517
518                 connect_type = g_array_index(in_param3, int, 0);
519
520                 if (connect_type == BT_RFCOMM_UUID) {
521                         result = _bt_rfcomm_connect_using_uuid(request_id,
522                                                         &address, input_string);
523                 } else {
524                         result = _bt_rfcomm_connect_using_channel(request_id,
525                                                         &address, input_string);
526                 }
527
528                 if (result != BLUETOOTH_ERROR_NONE) {
529                         g_array_append_vals(*out_param1, &address,
530                                         sizeof(bluetooth_device_address_t));
531                 }
532                 break;
533         }
534         case BT_RFCOMM_CLIENT_CANCEL_CONNECT:
535                 result = _bt_rfcomm_cancel_connect();
536                 break;
537         case BT_RFCOMM_CLIENT_IS_CONNECTED: {
538                 gboolean connected = FALSE;
539                 result = _bt_rfcomm_is_connected(&connected);
540
541                 g_array_append_vals(*out_param1, &connected, sizeof(gboolean));
542                 break;
543         }
544         case BT_RFCOMM_SOCKET_DISCONNECT: {
545                 int socket_fd;
546
547                 socket_fd = g_array_index(in_param1, int, 0);
548
549                 result = _bt_rfcomm_disconnect(socket_fd);
550                 break;
551         }
552         case BT_RFCOMM_SOCKET_WRITE: {
553                 int socket_fd;
554                 int length;
555                 char *buffer;
556
557                 socket_fd = g_array_index(in_param1, int, 0);
558                 length = g_array_index(in_param2, int, 0);
559                 buffer = &g_array_index(in_param3, char, 0);
560
561                 result = _bt_rfcomm_write(socket_fd, buffer, length);
562                 break;
563         }
564         case BT_RFCOMM_CREATE_SOCKET: {
565                 char *sender;
566                 char *uuid;
567                 int socket_fd = -1;
568                 int result;
569
570                 sender = dbus_g_method_get_sender(context);
571                 uuid = &g_array_index(in_param1, char, 0);
572
573                 result = _bt_rfcomm_create_socket(sender, uuid);
574
575                 if (result > 0) {
576                         socket_fd = result;
577                         result = BLUETOOTH_ERROR_NONE;
578                 }
579
580                 g_array_append_vals(*out_param1, &socket_fd, sizeof(int));
581
582                 g_free(sender);
583                 break;
584         }
585         case BT_RFCOMM_REMOVE_SOCKET: {
586                 int socket_fd;
587
588                 socket_fd = g_array_index(in_param1, int, 0);
589
590                 result = _bt_rfcomm_remove_socket(socket_fd);
591                 break;
592         }
593         case BT_RFCOMM_LISTEN: {
594                 int socket_fd;
595                 int pending;
596                 gboolean is_native;
597
598                 socket_fd = g_array_index(in_param1, int, 0);
599                 pending = g_array_index(in_param2, int, 0);
600                 is_native = g_array_index(in_param3, gboolean, 0);
601
602                 result = _bt_rfcomm_listen(socket_fd, pending, is_native);
603                 break;
604         }
605         case BT_RFCOMM_IS_UUID_AVAILABLE: {
606                 gboolean available = TRUE;
607                 char *uuid;
608
609                 uuid = &g_array_index(in_param1, char, 0);
610
611                 result = _bt_rfcomm_is_uuid_available(uuid, &available);
612
613                 g_array_append_vals(*out_param1, &available, sizeof(gboolean));
614                 break;
615         }
616         case BT_RFCOMM_ACCEPT_CONNECTION: {
617                 int socket_fd;
618
619                 socket_fd = g_array_index(in_param1, int, 0);
620
621                 result = _bt_rfcomm_accept_connection(socket_fd, request_id);
622                 break;
623         }
624         case BT_RFCOMM_REJECT_CONNECTION: {
625                 int socket_fd;
626
627                 socket_fd = g_array_index(in_param1, int, 0);
628
629                 result = _bt_rfcomm_reject_connection(socket_fd);
630                 break;
631         }
632         default:
633                 result = BLUETOOTH_ERROR_INTERNAL;
634                 break;
635         }
636
637         return result;
638 }
639
640 static int __bt_obexd_request(int function_name,
641                 int request_type,
642                 int request_id,
643                 DBusGMethodInvocation *context,
644                 GArray *in_param1,
645                 GArray *in_param2,
646                 GArray *in_param3,
647                 GArray *in_param4,
648                 GArray **out_param1)
649 {
650         int result;
651
652         switch (function_name) {
653         case BT_OPP_PUSH_FILES: {
654                 int i;
655                 bluetooth_device_address_t address = { {0} };
656                 bt_file_path_t path;
657                 char **file_path;
658                 int file_count;
659
660                 address = g_array_index(in_param1,
661                                 bluetooth_device_address_t, 0);
662
663                 file_count = g_array_index(in_param3, int, 0);
664
665                 file_path = g_new0(char *, file_count + 1);
666
667                 for (i = 0; i < file_count; i++) {
668                         path = g_array_index(in_param2,
669                                         bt_file_path_t, i);
670
671                         file_path[i] = g_strdup(path.path);
672                 }
673
674                 result = _bt_opp_client_push_files(request_id, context,
675                                                 &address, file_path,
676                                                 file_count);
677
678                 for (i = 0; i < file_count; i++) {
679                         g_free(file_path[i]);
680                 }
681
682                 g_free(file_path);
683
684                 break;
685         }
686         case BT_OPP_CANCEL_PUSH: {
687                 result = _bt_opp_client_cancel_push();
688
689                 break;
690         }
691         case BT_OPP_IS_PUSHING_FILES: {
692                 gboolean is_sending = FALSE;
693
694                 result = _bt_opp_client_is_sending(&is_sending);
695
696                 g_array_append_vals(*out_param1, &is_sending,
697                                 sizeof(gboolean));
698                 break;
699         }
700         case BT_OBEX_SERVER_ALLOCATE: {
701                 int app_pid;
702                 gboolean is_native;
703                 char *path;
704                 char *sender;
705
706                 sender = dbus_g_method_get_sender(context);
707                 path = &g_array_index(in_param1, char, 0);
708                 is_native = g_array_index(in_param2, gboolean, 0);
709                 app_pid = g_array_index(in_param3, int, 0);
710
711                 result = _bt_obex_server_allocate(sender, path, app_pid, is_native);
712
713                 g_free(sender);
714                 break;
715         }
716         case BT_OBEX_SERVER_DEALLOCATE: {
717                 int app_pid;
718                 gboolean is_native;
719
720                 is_native = g_array_index(in_param1, gboolean, 0);
721                 app_pid = g_array_index(in_param2, int, 0);
722
723                 result = _bt_obex_server_deallocate(app_pid, is_native);
724                 break;
725         }
726         case BT_OBEX_SERVER_IS_ACTIVATED: {
727                 gboolean is_activated = FALSE;
728
729                 result = _bt_obex_server_is_activated(&is_activated);
730
731                 g_array_append_vals(*out_param1, &is_activated,
732                                 sizeof(gboolean));
733
734                 break;
735         }
736         case BT_OBEX_SERVER_ACCEPT_CONNECTION: {
737                 result = _bt_obex_server_accept_connection(request_id);
738
739                 break;
740         }
741         case BT_OBEX_SERVER_REJECT_CONNECTION: {
742                 result = _bt_obex_server_reject_connection();
743
744                 break;
745         }
746         case BT_OBEX_SERVER_ACCEPT_FILE: {
747                 char *file_name;
748
749                 file_name = &g_array_index(in_param1, char, 0);
750
751                 result = _bt_obex_server_accept_authorize(file_name, TRUE);
752
753                 break;
754         }
755         case BT_OBEX_SERVER_REJECT_FILE: {
756                 result = _bt_obex_server_reject_authorize();
757
758                 break;
759         }
760         case BT_OBEX_SERVER_SET_PATH: {
761                 gboolean is_native;
762                 char *destination_path;
763
764                 destination_path = &g_array_index(in_param1, char, 0);
765                 is_native = g_array_index(in_param2, gboolean, 0);
766
767                 result = _bt_obex_server_set_destination_path(destination_path,
768                                                         is_native);
769
770                 break;
771         }
772         case BT_OBEX_SERVER_SET_ROOT: {
773                 char *root;
774
775                 root = &g_array_index(in_param1, char, 0);
776
777                 result = _bt_obex_server_set_root(root);
778
779                 break;
780         }
781         case BT_OBEX_SERVER_CANCEL_TRANSFER: {
782                 int transfer_id;
783
784                 transfer_id = g_array_index(in_param1, int, 0);
785
786                 result = _bt_obex_server_cancel_transfer(transfer_id);
787
788                 break;
789         }
790         case BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS: {
791                 result = _bt_obex_server_cancel_all_transfers();
792
793                 break;
794         }
795         case BT_OBEX_SERVER_IS_RECEIVING: {
796                 gboolean is_receiving = FALSE;
797
798                 result = _bt_obex_server_is_receiving(&is_receiving);
799
800                 g_array_append_vals(*out_param1, &is_receiving,
801                                 sizeof(gboolean));
802                 break;
803         }
804         default:
805                 BT_ERR("Unknown function!");
806                 result = BLUETOOTH_ERROR_INTERNAL;
807                 break;
808         }
809
810         return result;
811 }
812
813 gboolean bt_service_request(
814                 BtService *service,
815                 int service_type,
816                 int service_function,
817                 int request_type,
818                 GArray *in_param1,
819                 GArray *in_param2,
820                 GArray *in_param3,
821                 GArray *in_param4,
822                 GArray *in_param5,
823                 DBusGMethodInvocation *context)
824 {
825         int result;
826         int request_id = -1;
827         GArray *out_param1 = NULL;
828         GArray *out_param2 = NULL;
829
830         out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
831         out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
832
833         if (request_type == BT_ASYNC_REQ
834              || service_function == BT_OBEX_SERVER_ACCEPT_CONNECTION
835               || service_function == BT_RFCOMM_ACCEPT_CONNECTION) {
836                 /* Set the timer */
837                 request_id = _bt_assign_request_id();
838                 if (request_id < 0) {
839                         BT_ERR("Fail to assign the request id");
840                         result = BLUETOOTH_ERROR_INTERNAL;
841                         goto fail;
842                 }
843         }
844
845         switch (service_type) {
846         case BT_BLUEZ_SERVICE:
847                 result = __bt_bluez_request(service_function, request_type,
848                                         request_id, context, in_param1, in_param2,
849                                         in_param3, in_param4, &out_param1);
850                 break;
851         case BT_OBEX_SERVICE:
852                 result = __bt_obexd_request(service_function, request_type,
853                                         request_id, context, in_param1,
854                                         in_param2, in_param3,
855                                         in_param4, &out_param1);
856                 break;
857         default:
858                 BT_ERR("Unknown service type");
859                 result = BLUETOOTH_ERROR_INTERNAL;
860                 goto fail;
861         }
862
863         if (result != BLUETOOTH_ERROR_NONE) {
864                 BT_ERR("result is not error none: %x", result);
865                 goto fail;
866         }
867
868         g_array_append_vals(out_param2, &result, sizeof(int));
869
870         if (request_type == BT_ASYNC_REQ
871              || service_function == BT_OBEX_SERVER_ACCEPT_CONNECTION
872               || service_function == BT_RFCOMM_ACCEPT_CONNECTION) {
873                 _bt_insert_request_list(request_id, service_function,
874                                         NULL, context);
875         } else {
876                 /* Return result */
877                 dbus_g_method_return(context, out_param1, out_param2);
878         }
879
880         g_array_free(out_param1, TRUE);
881         g_array_free(out_param2, TRUE);
882
883         return TRUE;
884 fail:
885         g_array_append_vals(out_param2, &result, sizeof(int));
886         dbus_g_method_return(context, out_param1, out_param2);
887
888         g_array_free(out_param1, TRUE);
889         g_array_free(out_param2, TRUE);
890
891         if (request_type == BT_ASYNC_REQ)
892                 _bt_delete_request_id(request_id);
893
894         return FALSE;
895 }
896
897 int _bt_service_register(void)
898 {
899         BtService *bt_service;
900         DBusGConnection *conn;
901         DBusGProxy *proxy;
902         GError* err = NULL;
903         guint result = 0;
904
905         conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
906         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
907
908         proxy = dbus_g_proxy_new_for_name(conn, DBUS_SERVICE_DBUS,
909                                 DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS);
910
911         if (proxy == NULL) {
912                 BT_ERR("proxy is NULL");
913                 goto fail;
914         }
915
916         if (!dbus_g_proxy_call(proxy, "RequestName", &err, G_TYPE_STRING,
917                         BT_SERVICE_NAME, G_TYPE_UINT, 0, G_TYPE_INVALID,
918                         G_TYPE_UINT, &result, G_TYPE_INVALID)) {
919                 if (err != NULL) {
920                         BT_ERR("RequestName RPC failed[%s]\n", err->message);
921                         g_error_free(err);
922                 }
923                 g_object_unref(proxy);
924
925                 goto fail;
926         }
927
928         g_object_unref(proxy);
929
930         if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
931                 BT_ERR("Failed to get the primary well-known name.\n");
932                 goto fail;
933         }
934
935         bt_service = g_object_new(BT_SERVICE_TYPE, NULL);
936
937         dbus_g_connection_register_g_object(conn, BT_SERVICE_PATH,
938                                         G_OBJECT(bt_service));
939
940         service_object = bt_service;
941         bt_service_conn = conn;
942
943         return BLUETOOTH_ERROR_NONE;
944
945 fail:
946         if (bt_service_conn) {
947                 dbus_g_connection_unref(bt_service_conn);
948                 bt_service_conn = NULL;
949         }
950
951         return BLUETOOTH_ERROR_INTERNAL;
952 }
953
954 void _bt_service_unregister(void)
955 {
956         if (bt_service_conn) {
957                 if (service_object) {
958                         dbus_g_connection_unregister_g_object(bt_service_conn,
959                                                 G_OBJECT(service_object));
960                         g_object_unref(service_object);
961                         service_object = NULL;
962                 }
963
964                 dbus_g_connection_unref(bt_service_conn);
965                 bt_service_conn = NULL;
966         }
967 }
968