6b1e0f5f6eafbe95c7fb30f48b94cc33f5f9806f
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.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 <fcntl.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <stdlib.h>
29 #include <sys/socket.h>
30
31
32 #include <gio/gunixfdlist.h>
33
34
35 #include "bluetooth-api.h"
36 #include "bluetooth-audio-api.h"
37 #include "bluetooth-hid-api.h"
38 #include "bluetooth-media-control.h"
39 #include "bt-internal-types.h"
40
41 #include "bt-common.h"
42 #include "bt-request-sender.h"
43 #include "bt-event-handler.h"
44
45 static bt_user_info_t user_info[BT_MAX_USER_INFO];
46 static DBusGConnection *system_conn = NULL;
47 static GDBusConnection *system_gdbus_conn = NULL;
48
49 static guint bus_id;
50
51 static GDBusConnection *system_gconn = NULL;
52
53 GDBusConnection *_bt_gdbus_init_system_gconn(void)
54 {
55         GError *error = NULL;
56
57         if (!g_thread_supported()) {
58                 g_thread_init(NULL);
59         }
60
61         dbus_g_thread_init();
62
63         g_type_init();
64
65         if (system_gconn != NULL)
66                 return system_gconn;
67
68         system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
69
70         if (!system_gconn) {
71                 BT_ERR("Unable to connect to dbus: %s", error->message);
72                 g_clear_error(&error);
73         }
74
75         return system_gconn;
76 }
77
78 GDBusConnection *_bt_gdbus_get_system_gconn(void)
79 {
80         GDBusConnection *local_system_gconn = NULL;
81         GError *error = NULL;
82
83         if (system_gconn == NULL) {
84                 system_gconn = _bt_gdbus_init_system_gconn();
85         } else if (g_dbus_connection_is_closed(system_gconn)){
86
87                 local_system_gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
88
89                 if (!local_system_gconn) {
90                         BT_ERR("Unable to connect to dbus: %s", error->message);
91                         g_clear_error(&error);
92                 }
93
94                 system_gconn = local_system_gconn;
95         }
96
97         return system_gconn;
98 }
99
100 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
101 {
102         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
103                                 addr->addr[3], addr->addr[4], addr->addr[5]);
104 }
105
106 void _bt_set_user_data(int type, void *callback, void *user_data)
107 {
108         user_info[type].cb = callback;
109         user_info[type].user_data = user_data;
110 }
111
112 bt_user_info_t *_bt_get_user_data(int type)
113 {
114         return &user_info[type];
115 }
116
117 void _bt_common_event_cb(int event, int result, void *param,
118                                         void *callback, void *user_data)
119 {
120         bluetooth_event_param_t bt_event = { 0, };
121         bt_event.event = event;
122         bt_event.result = result;
123         bt_event.param_data = param;
124
125         if (callback)
126                 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
127                                         user_data);
128 }
129
130 void _bt_input_event_cb(int event, int result, void *param,
131                                         void *callback, void *user_data)
132 {
133         hid_event_param_t bt_event = { 0, };
134         bt_event.event = event;
135         bt_event.result = result;
136         bt_event.param_data = param;
137
138         if (callback)
139                 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
140                                         user_data);
141 }
142
143 void _bt_headset_event_cb(int event, int result, void *param,
144                                         void *callback, void *user_data)
145 {
146         bt_audio_event_param_t bt_event = { 0, };
147         bt_event.event = event;
148         bt_event.result = result;
149         bt_event.param_data = param;
150
151         if (callback)
152                 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
153                                         user_data);
154 }
155
156 void _bt_hf_event_cb(int event, int result, void *param,
157                                         void *callback, void *user_data)
158 {
159         bt_hf_event_param_t bt_event = { 0, };
160         bt_event.event = event;
161         bt_event.result = result;
162         bt_event.param_data = param;
163
164         if (callback)
165                 ((bt_hf_func_ptr)callback)(bt_event.event, &bt_event,
166                                         user_data);
167 }
168
169
170 void _bt_avrcp_event_cb(int event, int result, void *param,
171                                         void *callback, void *user_data)
172 {
173         media_event_param_t bt_event = { 0, };
174         bt_event.event = event;
175         bt_event.result = result;
176         bt_event.param_data = param;
177
178         if (callback)
179                 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
180                                         user_data);
181 }
182
183 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
184                                 unsigned int cod)
185 {
186         ret_if(device_class == NULL);
187
188         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
189         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
190         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
191
192         if (cod & 0x002000) {
193                 device_class->service_class |=
194                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
195         }
196 }
197
198 void _bt_convert_addr_string_to_type(unsigned char *addr,
199                                         const char *address)
200 {
201         int i;
202         char *ptr = NULL;
203
204         ret_if(address == NULL);
205         ret_if(addr == NULL);
206
207         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
208                 addr[i] = strtol(address, &ptr, 16);
209                 if (ptr[0] != '\0') {
210                         if (ptr[0] != ':')
211                                 return;
212
213                         address = ptr + 1;
214                 }
215         }
216 }
217
218 void _bt_convert_addr_type_to_string(char *address,
219                                 unsigned char *addr)
220 {
221         ret_if(address == NULL);
222         ret_if(addr == NULL);
223
224         g_snprintf(address, BT_ADDRESS_STRING_SIZE,
225                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
226                         addr[0], addr[1], addr[2],
227                         addr[3], addr[4], addr[5]);
228 }
229
230 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
231 {
232         int i;
233         const char *p = src;
234         char *next;
235         int count;
236
237         if (dest == NULL || src == NULL)
238                 return BLUETOOTH_ERROR_INVALID_PARAM;
239
240         i = 0;
241         while (*p != '\0' && i < length) {
242                 next = g_utf8_next_char(p);
243                 count = next - p;
244
245                 while (count > 0 && ((i + count) < length)) {
246                         dest[i++] = *p;
247                         p++;
248                         count --;
249                 }
250                 p = next;
251         }
252         return BLUETOOTH_ERROR_NONE;
253 }
254
255 gboolean _bt_utf8_validate(char *name)
256 {
257         BT_DBG("+");
258         gunichar2 *u16;
259         glong items_written = 0;
260
261         if (FALSE == g_utf8_validate(name, -1, NULL))
262                 return FALSE;
263
264         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
265         if (u16 == NULL)
266                 return FALSE;
267
268         g_free(u16);
269
270         if (items_written != g_utf8_strlen(name, -1))
271                 return FALSE;
272
273         BT_DBG("-");
274         return TRUE;
275 }
276
277
278 static GDBusProxy *profile_gproxy;
279 static GDBusConnection *gconn;
280 static int latest_id = -1;
281 #define BT_RFCOMM_ID_MAX 245
282 static gboolean id_used[BT_RFCOMM_ID_MAX];
283 GDBusNodeInfo *new_conn_node;
284
285 static const gchar rfcomm_agent_xml[] =
286 "<node name='/'>"
287 " <interface name='org.bluez.Profile1'>"
288 "     <method name='NewConnection'>"
289 "          <arg type='o' name='object' direction='in'/>"
290 "          <arg type='h' name='fd' direction='in'/>"
291 "          <arg type='a{sv}' name='properties' direction='in'/>"
292 "     </method>"
293 "  </interface>"
294 "</node>";
295
296 /* Remote socket address */
297 struct sockaddr_remote {
298         sa_family_t     family;
299         bluetooth_device_address_t      remote_bdaddr;
300         unsigned char   channel;
301 };
302
303 static void __get_remote_address(int fd, bluetooth_device_address_t *bdaddr)
304 {
305         struct sockaddr_remote address;
306         socklen_t address_len;
307
308         address_len = sizeof(address);
309         if (getpeername(fd, (struct sockaddr *) &address, &address_len) != 0) {
310                 BT_DBG("getpeername failed");
311                 return;
312         }
313
314         memcpy(bdaddr->addr, address.remote_bdaddr.addr,
315                                         BLUETOOTH_ADDRESS_LENGTH);
316 }
317
318 static void __new_connection_method(GDBusConnection *connection,
319                                             const gchar *sender,
320                                             const gchar *object_path,
321                                             const gchar *interface_name,
322                                             const gchar *method_name,
323                                             GVariant *parameters,
324                                             GDBusMethodInvocation *invocation,
325                                             gpointer user_data)
326 {
327         BT_DBG("method %s", method_name);
328         if (g_strcmp0(method_name, "NewConnection") == 0) {
329                 int index;
330                 GDBusMessage *msg;
331                 GUnixFDList *fd_list;
332                 GVariantBuilder *properties;
333                 char *obj_path;
334                 char addr[20];
335                 bluetooth_device_address_t remote_addr, remote_addr1;
336                 bt_new_connection_cb cb = user_data;
337                 int fd;
338
339                 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
340                                                                 &properties);
341
342                 msg = g_dbus_method_invocation_get_message(invocation);
343                 fd_list = g_dbus_message_get_unix_fd_list(msg);
344                 if (fd_list == NULL) {
345                         GQuark quark = g_quark_from_string("rfcomm-app");
346                         GError *err = g_error_new(quark, 0, "No fd in message");
347                         g_dbus_method_invocation_return_gerror(invocation, err);
348                         g_error_free(err);
349                         return;
350                 }
351
352
353                 fd = g_unix_fd_list_get(fd_list, index, NULL);
354                 if (fd == -1) {
355                         BT_ERR("Invalid fd return");
356                         GQuark quark = g_quark_from_string("rfcomm-app");
357                         GError *err = g_error_new(quark, 0, "Invalid FD return");
358                         g_dbus_method_invocation_return_gerror(invocation, err);
359                         g_error_free(err);
360                         return;
361                 }
362
363                 __get_remote_address(fd, &remote_addr);
364                 _bt_swap_addr(remote_addr1.addr, remote_addr.addr);
365                 _bt_convert_addr_type_to_string(addr, remote_addr1.addr);
366                 BT_INFO("fd: %d, address %s", fd, addr);
367
368                 g_dbus_method_invocation_return_value(invocation, NULL);
369
370                 if (cb)
371                         cb(object_path, fd, &remote_addr1);
372         }
373 }
374
375
376 static const GDBusInterfaceVTable method_table = {
377         __new_connection_method,
378         NULL,
379         NULL,
380 };
381
382 void _bt_swap_addr(unsigned char *dst, const unsigned char *src)
383 {
384         int i;
385
386         for (i = 0; i < 6; i++)
387                 dst[i] = src[5-i];
388 }
389
390 int __rfcomm_assign_id(void)
391 {
392         int index;
393
394         BT_DBG("latest_id: %d", latest_id);
395
396         index = latest_id + 1;
397
398         if (index >= BT_RFCOMM_ID_MAX)
399                 index = 0;
400
401         BT_DBG("index: %d", index);
402
403         while (id_used[index] == TRUE) {
404                 if (index == latest_id) {
405                         /* No available ID */
406                         BT_ERR("All request ID is used");
407                         return -1;
408                 }
409
410                 index++;
411
412                 if (index >= BT_RFCOMM_ID_MAX)
413                         index = 0;
414         }
415
416         latest_id = index;
417         id_used[index] = TRUE;
418
419         BT_DBG("Assigned Id: %d", latest_id);
420
421         return latest_id;
422 }
423
424 void __rfcomm_delete_id(int id)
425 {
426         ret_if(id >= BT_RFCOMM_ID_MAX);
427         ret_if(id < 0);
428
429         id_used[id] = FALSE;
430
431         /* Next server will use this ID */
432         latest_id = id - 1;
433 }
434
435 static GDBusConnection *__get_gdbus_connection()
436 {
437         GError *err = NULL;
438
439         if (gconn == NULL)
440                 gconn = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &err);
441
442         if (!gconn) {
443                 if (err) {
444                         BT_ERR("Unable to connect to dbus: %s", err->message);
445                         g_clear_error(&err);
446                 }
447                 return NULL;
448         }
449
450         return gconn;
451 }
452
453 static GDBusProxy *__bt_gdbus_get_profile_proxy(void)
454 {
455         GDBusConnection *gconn;
456         GError *err = NULL;
457
458         if (profile_gproxy)
459                 return profile_gproxy;
460
461         gconn = __get_gdbus_connection();
462         if (gconn == NULL)
463                 return NULL;
464
465         profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
466                                                 NULL, BT_BLUEZ_NAME,
467                                                 "/org/bluez",
468                                                 "org.bluez.ProfileManager1",
469                                                 NULL, &err);
470         if (err) {
471                 BT_ERR("Unable to create proxy: %s", err->message);
472                 g_clear_error(&err);
473         }
474
475         return profile_gproxy;
476 }
477
478 void _bt_unregister_gdbus(int object_id)
479 {
480         GDBusConnection *gconn;
481
482         gconn = __get_gdbus_connection();
483         if (gconn == NULL)
484                 return;
485
486         g_dbus_connection_unregister_object(gconn, object_id);
487 }
488
489 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
490 {
491         GDBusConnection *gconn;
492         int id;
493         GError *error = NULL;
494
495         gconn = __get_gdbus_connection();
496         if (gconn == NULL)
497                 return -1;
498
499         if (new_conn_node == NULL)
500                 new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
501
502         if (new_conn_node == NULL)
503                 return -1;
504
505         id = g_dbus_connection_register_object(gconn, path,
506                                                 new_conn_node->interfaces[0],
507                                                 &method_table,
508                                                 cb, NULL, &error);
509         if (id == 0) {
510                 BT_ERR("Failed to register: %s", error->message);
511                 g_error_free(error);
512                 return -1;
513         }
514
515         BT_DBG("NEW CONNECTION ID %d", id);
516
517         return id;
518 }
519
520
521 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
522 {
523         GVariantBuilder *option_builder;
524         GVariant *ret;
525         GDBusProxy *proxy;
526         GError *err = NULL;
527         int result = BLUETOOTH_ERROR_NONE;
528
529         proxy = __bt_gdbus_get_profile_proxy();
530         if (proxy == NULL) {
531                 BT_ERR("Getting profile proxy failed");
532                 return BLUETOOTH_ERROR_INTERNAL;
533         }
534
535         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
536         if (info->authentication)
537                 g_variant_builder_add(option_builder, "{sv}",
538                                                 "RequireAuthentication",
539                                                 g_variant_new_boolean(TRUE));
540         if (info->authorization)
541                 g_variant_builder_add(option_builder, "{sv}",
542                                                 "RequireAuthorization",
543                                                 g_variant_new_boolean(TRUE));
544         if (info->role)
545                 g_variant_builder_add(option_builder, "{sv}",
546                                                 "Role",
547                                                 g_variant_new_string(info->role));
548
549         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
550          * RFCOMM channels based on the availability when two services want
551          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
552          * two services use the same SPP RFCOMM channel. */
553         if (use_default_rfcomm)
554                 g_variant_builder_add(option_builder, "{sv}",
555                                                 "Channel",
556                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
557         if (info->service)
558                 g_variant_builder_add(option_builder, "{sv}",
559                                                 "Service",
560                                                 g_variant_new_string(info->service));
561
562
563         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
564                                         g_variant_new("(osa{sv})", info->obj_path,
565                                                                 info->uuid,
566                                                                 option_builder),
567                                         G_DBUS_CALL_FLAGS_NONE, -1,
568                                         NULL, &err);
569         if (err) {
570                 BT_ERR("RegisterProfile failed: %s", err->message);
571
572                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
573                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
574                 else
575                         result = BLUETOOTH_ERROR_INTERNAL;
576
577                 g_clear_error(&err);
578         }
579
580         g_variant_builder_unref(option_builder);
581
582         if (ret)
583                 g_variant_unref(ret);
584
585         return result;
586 }
587
588 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
589 {
590         GVariantBuilder *option_builder;
591         GVariant *ret;
592         GDBusProxy *proxy;
593         GError *err = NULL;
594         int result = BLUETOOTH_ERROR_NONE;
595
596         proxy = __bt_gdbus_get_profile_proxy();
597         if (proxy == NULL) {
598                 BT_ERR("Getting profile proxy failed");
599                 return BLUETOOTH_ERROR_INTERNAL;
600         }
601
602         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
603         if (info->authentication)
604                 g_variant_builder_add(option_builder, "{sv}",
605                                                 "RequireAuthentication",
606                                                 g_variant_new_boolean(TRUE));
607         if (info->authorization)
608                 g_variant_builder_add(option_builder, "{sv}",
609                                                 "RequireAuthorization",
610                                                 g_variant_new_boolean(TRUE));
611         if (info->role)
612                 g_variant_builder_add(option_builder, "{sv}",
613                                                 "Role",
614                                                 g_variant_new_string(info->role));
615
616         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
617          * RFCOMM channels based on the availability when two services want
618          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
619          * two services use the same SPP RFCOMM channel. */
620         if (use_default_rfcomm)
621                 g_variant_builder_add(option_builder, "{sv}",
622                                                 "Channel",
623                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
624         if (info->service)
625                 g_variant_builder_add(option_builder, "{sv}",
626                                                 "Service",
627                                                 g_variant_new_string(info->service));
628
629
630         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile1",
631                                         g_variant_new("(osa{sv})", info->obj_path,
632                                                                 info->uuid,
633                                                                 option_builder),
634                                         G_DBUS_CALL_FLAGS_NONE, -1,
635                                         NULL, &err);
636         if (err) {
637                 BT_ERR("RegisterProfile failed: %s", err->message);
638
639                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
640                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
641                 else
642                         result = BLUETOOTH_ERROR_INTERNAL;
643
644                 g_clear_error(&err);
645         }
646
647         g_variant_builder_unref(option_builder);
648
649         if (ret)
650                 g_variant_unref(ret);
651
652         return result;
653 }
654
655
656 void _bt_unregister_profile(char *path)
657 {
658         GVariant *ret;
659         GDBusProxy *proxy;
660         GError *err = NULL;
661
662         proxy = __bt_gdbus_get_profile_proxy();
663         if (proxy == NULL) {
664                 BT_ERR("Getting profile proxy failed");
665                 return;
666         }
667
668         ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
669                         g_variant_new("(o)", path),
670                         G_DBUS_CALL_FLAGS_NONE, -1,
671                         NULL, &err);
672         if (err) {
673                 BT_ERR("UnregisterProfile failed : %s", err->message);
674                 g_clear_error(&err);
675         }
676
677         if (ret)
678                 g_variant_unref(ret);
679
680         return;
681 }
682
683 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
684 {
685         if (bus_id == 0) {
686                 char *name = g_strdup_printf("org.bt.frwk%d", getpid());
687
688                 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
689                                                 name,
690                                                 G_BUS_NAME_OWNER_FLAGS_NONE,
691                                                 NULL,
692                                                 NULL,
693                                                 NULL,
694                                                 NULL,
695                                                 NULL);
696                 BT_DBG("Got bus id %d", bus_id);
697                 g_free(name);
698         }
699
700         return g_dbus_node_info_new_for_xml(xml_data, NULL);
701 }
702
703 int _bt_connect_profile(char *address, char *uuid, void *cb,
704                                                         gpointer func_data)
705 {
706         char *object_path;
707         DBusGProxy *proxy;
708         DBusGConnection *conn;
709         DBusGProxy *adapter_proxy;
710         GError *error = NULL;
711
712         conn = _bt_get_system_gconn();
713         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
714
715         object_path = _bt_get_device_object_path(address);
716         if (object_path == NULL) {
717                 BT_ERR("No searched device");
718
719                 adapter_proxy = _bt_get_adapter_proxy(conn);
720                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
721
722                 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
723                         G_TYPE_STRING, address,
724                         G_TYPE_INVALID, G_TYPE_INVALID);
725
726                 if (error != NULL) {
727                         BT_ERR("CreateDevice Fail: %s", error->message);
728                         g_error_free(error);
729                 }
730
731                 object_path = _bt_get_device_object_path(address);
732         }
733         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
734
735
736         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
737                                 object_path, BT_DEVICE_INTERFACE);
738         g_free(object_path);
739         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
740
741         if (!dbus_g_proxy_begin_call(proxy, "ConnectProfile",
742                         (DBusGProxyCallNotify)cb,
743                         func_data, NULL,
744                         G_TYPE_STRING, uuid,
745                         G_TYPE_INVALID)) {
746                 BT_ERR("Connect Dbus Call Error");
747                 g_object_unref(proxy);
748                 return BLUETOOTH_ERROR_INTERNAL;
749         }
750         return BLUETOOTH_ERROR_NONE;
751 }
752
753 int _bt_discover_services(char *address, char *uuid, void *cb,
754                 gpointer func_data)
755 {
756         char *object_path;
757         DBusGProxy *proxy;
758         DBusGConnection *conn;
759         DBusGProxy *adapter_proxy;
760         GError *error = NULL;
761
762         int timeout = 10000;
763         BT_DBG("+");
764         conn = _bt_get_system_gconn();
765         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
766
767         object_path = _bt_get_device_object_path(address);
768         if (object_path == NULL) {
769                 BT_ERR("No searched device");
770
771                 adapter_proxy = _bt_get_adapter_proxy(conn);
772                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
773
774                 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
775                         G_TYPE_STRING, address,
776                         G_TYPE_INVALID, G_TYPE_INVALID);
777
778                 if (error != NULL) {
779                         BT_ERR("CreateDevice Fail: %s", error->message);
780                         g_error_free(error);
781                 }
782
783                 object_path = _bt_get_device_object_path(address);
784         }
785         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
786
787         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
788                                 object_path, BT_DEVICE_INTERFACE);
789         g_free(object_path);
790         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
791
792         if (!dbus_g_proxy_begin_call_with_timeout(proxy, "DiscoverServices",
793                         (DBusGProxyCallNotify)cb,
794                         func_data, NULL, timeout,
795                         G_TYPE_STRING, uuid,
796                         G_TYPE_INVALID)) {
797                 BT_ERR("Error: While calling DiscoverServices");
798                 g_object_unref(proxy);
799                 return BLUETOOTH_ERROR_INTERNAL;
800         }
801         BT_DBG("-");
802         return BLUETOOTH_ERROR_NONE;
803 }
804
805 int _bt_cancel_discovers(char *address)
806 {
807         char *object_path;
808         DBusGProxy *proxy;
809         DBusGConnection *conn;
810         BT_DBG("+");
811         conn = _bt_get_system_gconn();
812         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
813
814         object_path = _bt_get_device_object_path(address);
815         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
816
817         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
818                                 object_path, BT_DEVICE_INTERFACE);
819         g_free(object_path);
820         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
821         if (!dbus_g_proxy_call(proxy,
822                         "CancelDiscovery",
823                         NULL,
824                         G_TYPE_INVALID, G_TYPE_INVALID)) {
825                 BT_ERR("Error: while CancelDiscovery");
826                 g_object_unref(proxy);
827                 return BLUETOOTH_ERROR_INTERNAL;
828         }
829         g_object_unref(proxy);
830         BT_DBG("-");
831         return BLUETOOTH_ERROR_NONE;
832 }
833
834 int _bt_discover_service_uuids(char *address, char *remote_uuid)
835 {
836         char *object_path;
837         DBusGProxy *proxy;
838         DBusGConnection *conn;
839         GHashTable *hash = NULL;
840         GValue *value;
841         char **uuid_value;
842         int i =0;
843         int result = BLUETOOTH_ERROR_INTERNAL;
844         retv_if(remote_uuid == NULL, BLUETOOTH_ERROR_INTERNAL);
845
846         conn = _bt_get_system_gconn();
847         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
848
849         object_path = _bt_get_device_object_path(address);
850         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
851
852         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
853                                 object_path, BT_PROPERTIES_INTERFACE);
854         g_free(object_path);
855
856         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
857
858         if (!dbus_g_proxy_call(proxy, "GetAll", NULL,
859                         G_TYPE_STRING, BT_DEVICE_INTERFACE,
860                         G_TYPE_INVALID,
861                         dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
862                         G_TYPE_VALUE), &hash, G_TYPE_INVALID)) {
863                         BT_ERR("Dbus error while GetAll");
864                         g_object_unref(proxy);
865                         return BLUETOOTH_ERROR_INTERNAL;
866         }
867         g_object_unref(proxy);
868         BT_DBG("Remote uuids %s", remote_uuid);
869         value = g_hash_table_lookup(hash, "UUIDs");
870         if (value == NULL) {
871                 BT_ERR("No uuids found");
872                 goto done;
873         }
874
875         uuid_value = g_value_get_boxed(value);
876         if(uuid_value == NULL) {
877                 BT_ERR("Error: while obtaining uuids");
878                 goto done;
879         }
880
881         for (i = 0; uuid_value[i] != NULL; i++) {
882                 BT_DBG("Remote uuids %s", uuid_value[i]);
883                 if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
884                         result = BLUETOOTH_ERROR_NONE;
885                         goto done;
886                 }
887         }
888         BT_INFO("Specified uuid not found on remote device");
889 done:
890         if (hash)
891                 g_hash_table_destroy(hash);
892
893         return result;
894 }
895
896 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
897                                                         gpointer func_data)
898 {
899         char *object_path;
900         DBusGProxy *proxy;
901         DBusGConnection *conn;
902
903         conn = _bt_get_system_gconn();
904         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
905
906         object_path = _bt_get_device_object_path(address);
907         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
908
909
910         proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
911                                 object_path, BT_DEVICE_INTERFACE);
912         g_free(object_path);
913         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
914
915         if (!dbus_g_proxy_begin_call(proxy, "DisconnectProfile",
916                         (DBusGProxyCallNotify)cb,
917                         func_data, NULL,
918                         G_TYPE_STRING, uuid,
919                         G_TYPE_INVALID)) {
920                 BT_ERR("Connect Dbus Call Error");
921                 g_object_unref(proxy);
922                 return BLUETOOTH_ERROR_INTERNAL;
923         }
924         return BLUETOOTH_ERROR_NONE;
925 }
926
927 int _bt_get_adapter_path(GDBusConnection *conn, char *path)
928 {
929         GError *err = NULL;
930         GDBusProxy *manager_proxy = NULL;
931         GVariant *result = NULL;
932         char *adapter_path = NULL;
933
934         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
935
936         manager_proxy =  g_dbus_proxy_new_sync(conn,
937                         G_DBUS_PROXY_FLAGS_NONE, NULL,
938                         BT_BLUEZ_NAME,
939                         BT_MANAGER_PATH,
940                         BT_MANAGER_INTERFACE,
941                         NULL, &err);
942
943         if (!manager_proxy) {
944                 BT_ERR("Unable to create proxy: %s", err->message);
945                 goto fail;
946         }
947
948         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
949                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
950         if (!result) {
951                 if (err != NULL)
952                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
953                 else
954                         BT_ERR("Fail to get DefaultAdapter");
955
956                 goto fail;
957         }
958
959         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
960                 BT_ERR("Incorrect result\n");
961                 goto fail;
962         }
963
964         g_variant_get(result, "(&o)", &adapter_path);
965
966         if (adapter_path == NULL ||
967                 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
968                 BT_ERR("Adapter path is inproper\n");
969                 goto fail;
970         }
971
972         if (path)
973                 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
974
975         g_variant_unref(result);
976         g_object_unref(manager_proxy);
977
978         return BLUETOOTH_ERROR_NONE;
979
980 fail:
981         g_clear_error(&err);
982
983         if (result)
984                 g_variant_unref(result);
985
986         if (manager_proxy)
987                 g_object_unref(manager_proxy);
988
989         return BLUETOOTH_ERROR_INTERNAL;
990
991 }
992
993 void _bt_convert_device_path_to_address(const char *device_path,
994                                                 char *device_address)
995 {
996         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
997         char *dev_addr;
998
999         ret_if(device_path == NULL);
1000         ret_if(device_address == NULL);
1001
1002         dev_addr = strstr(device_path, "dev_");
1003         if (dev_addr != NULL) {
1004                 char *pos = NULL;
1005                 dev_addr += 4;
1006                 g_strlcpy(address, dev_addr, sizeof(address));
1007
1008                 while ((pos = strchr(address, '_')) != NULL) {
1009                         *pos = ':';
1010                 }
1011
1012                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1013         }
1014 }
1015
1016 static char *__bt_extract_adapter_path(DBusMessageIter *msg_iter)
1017 {
1018         char *object_path = NULL;
1019         DBusMessageIter value_iter;
1020
1021         /* Parse the signature:  oa{sa{sv}}} */
1022         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
1023                                 DBUS_TYPE_OBJECT_PATH, NULL);
1024
1025         dbus_message_iter_get_basic(msg_iter, &object_path);
1026         retv_if(object_path == NULL, NULL);
1027
1028         /* object array (oa) */
1029         retv_if(dbus_message_iter_next(msg_iter) == FALSE, NULL);
1030         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
1031                                 DBUS_TYPE_ARRAY, NULL);
1032
1033         dbus_message_iter_recurse(msg_iter, &value_iter);
1034
1035         /* string array (sa) */
1036         while (dbus_message_iter_get_arg_type(&value_iter) ==
1037                                         DBUS_TYPE_DICT_ENTRY) {
1038                 char *interface_name = NULL;
1039                 DBusMessageIter interface_iter;
1040
1041                 dbus_message_iter_recurse(&value_iter, &interface_iter);
1042
1043                 retv_if(dbus_message_iter_get_arg_type(&interface_iter) !=
1044                         DBUS_TYPE_STRING, NULL);
1045
1046                 dbus_message_iter_get_basic(&interface_iter, &interface_name);
1047
1048                 if (g_strcmp0(interface_name, "org.bluez.Adapter1") == 0) {
1049                         /* Tizen don't allow the multi-adapter */
1050                         BT_DBG("Found an adapter: %s", object_path);
1051                         return g_strdup(object_path);
1052                 }
1053
1054                 dbus_message_iter_next(&value_iter);
1055         }
1056
1057         return NULL;
1058 }
1059
1060 static char *__bt_extract_device_path(DBusMessageIter *msg_iter, char *address)
1061 {
1062         char *object_path = NULL;
1063         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1064
1065         /* Parse the signature:  oa{sa{sv}}} */
1066         retv_if(dbus_message_iter_get_arg_type(msg_iter) !=
1067                                 DBUS_TYPE_OBJECT_PATH, NULL);
1068
1069         dbus_message_iter_get_basic(msg_iter, &object_path);
1070         retv_if(object_path == NULL, NULL);
1071
1072         _bt_convert_device_path_to_address(object_path, device_address);
1073
1074         if (g_strcmp0(address, device_address) == 0) {
1075                 return g_strdup(object_path);
1076         }
1077
1078         return NULL;
1079 }
1080
1081 char *_bt_get_device_object_path(char *address)
1082 {
1083         DBusMessage *msg;
1084         DBusMessage *reply;
1085         DBusMessageIter reply_iter;
1086         DBusMessageIter value_iter;
1087         DBusError err;
1088         DBusConnection *conn;
1089         char *object_path = NULL;
1090
1091         conn = _bt_get_system_conn();
1092         retv_if(conn == NULL, NULL);
1093
1094         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
1095                                                 BT_MANAGER_INTERFACE,
1096                                                 "GetManagedObjects");
1097
1098         retv_if(msg == NULL, NULL);
1099
1100         /* Synchronous call */
1101         dbus_error_init(&err);
1102         reply = dbus_connection_send_with_reply_and_block(
1103                                         conn, msg,
1104                                         -1, &err);
1105         dbus_message_unref(msg);
1106
1107         if (!reply) {
1108                 BT_ERR("Can't get managed objects");
1109
1110                 if (dbus_error_is_set(&err)) {
1111                         BT_ERR("%s", err.message);
1112                         dbus_error_free(&err);
1113                 }
1114                 return NULL;
1115         }
1116
1117         if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
1118             BT_ERR("Fail to iterate the reply");
1119             dbus_message_unref(reply);
1120             return NULL;
1121         }
1122
1123         dbus_message_iter_recurse(&reply_iter, &value_iter);
1124
1125         /* signature of GetManagedObjects:  a{oa{sa{sv}}} */
1126         while (dbus_message_iter_get_arg_type(&value_iter) ==
1127                                                 DBUS_TYPE_DICT_ENTRY) {
1128                 DBusMessageIter msg_iter;
1129
1130                 dbus_message_iter_recurse(&value_iter, &msg_iter);
1131
1132                 object_path = __bt_extract_device_path(&msg_iter, address);
1133                 if (object_path != NULL) {
1134                         BT_DBG("Found the device path %s", object_path);
1135                         break;
1136                 }
1137
1138                 dbus_message_iter_next(&value_iter);
1139         }
1140
1141         dbus_message_unref(reply);
1142
1143         return object_path;
1144 }
1145
1146
1147 DBusGProxy *_bt_get_adapter_proxy(DBusGConnection *conn)
1148 {
1149         DBusGProxy *adapter_proxy = NULL;
1150
1151         retv_if(conn == NULL, NULL);
1152
1153         adapter_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1154                         BT_BLUEZ_HCI_PATH, BT_PROPERTIES_INTERFACE);
1155
1156         return adapter_proxy;
1157 }
1158
1159 void _bt_device_path_to_address(const char *device_path, char *device_address)
1160 {
1161         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1162         char *dev_addr = NULL;
1163
1164         if (!device_path || !device_address)
1165                 return;
1166
1167         dev_addr = strstr(device_path, "dev_");
1168         if (dev_addr != NULL) {
1169                 char *pos = NULL;
1170                 dev_addr += 4;
1171                 g_strlcpy(address, dev_addr, sizeof(address));
1172
1173                 while ((pos = strchr(address, '_')) != NULL) {
1174                         *pos = ':';
1175                 }
1176
1177                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1178         }
1179 }
1180
1181 DBusGConnection *__bt_init_system_gconn(void)
1182 {
1183         g_type_init();
1184
1185         if (system_conn == NULL)
1186                 system_conn = dbus_g_bus_get(DBUS_BUS_SYSTEM, NULL);
1187
1188         return system_conn;
1189 }
1190
1191 DBusGConnection *_bt_get_system_gconn(void)
1192 {
1193         return (system_conn) ? system_conn : __bt_init_system_gconn();
1194 }
1195
1196 GDBusConnection *_bt_init_system_gdbus_conn(void)
1197 {
1198         g_type_init();
1199         GError *error = NULL;
1200         if (system_gdbus_conn == NULL) {
1201                 system_gdbus_conn =
1202                 g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1203                 if (error) {
1204                         BT_ERR("GDBus connection Error : %s \n",
1205                                 error->message);
1206                         g_clear_error(&error);
1207                         return NULL;
1208                 }
1209         }
1210         return system_gdbus_conn;
1211 }
1212
1213 DBusConnection *_bt_get_system_conn(void)
1214 {
1215         DBusGConnection *g_conn;
1216
1217         if (system_conn == NULL) {
1218                 g_conn = __bt_init_system_gconn();
1219         } else {
1220                 g_conn = system_conn;
1221         }
1222
1223         retv_if(g_conn == NULL, NULL);
1224
1225         return dbus_g_connection_get_connection(g_conn);
1226 }
1227
1228 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
1229 {
1230         int ret;
1231         char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1232         char path_str[BLUETOOTH_PATH_STRING] = { 0, };
1233
1234         BT_DBG("+");
1235         BT_INIT_PARAMS();
1236         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1237
1238         g_array_append_vals(in_param1, &type, sizeof(int));
1239         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1240         g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1241         g_strlcpy(path_str, path, sizeof(path_str));
1242         g_array_append_vals(in_param3, &path_str, BLUETOOTH_PATH_STRING);
1243         g_array_append_vals(in_param4, &fd, sizeof(int));
1244
1245         ret =  _bt_send_request(BT_AGENT_SERVICE, BT_SET_AUTHORIZATION,
1246                 in_param1, in_param2, in_param3, in_param4, &out_param);
1247
1248         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1249         BT_DBG("-");
1250         return ret;
1251 }
1252
1253 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
1254 {
1255         int ret;
1256         char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1257         char path_str[BLUETOOTH_PATH_STRING] = { 0, };
1258
1259         BT_DBG("+");
1260         BT_INIT_PARAMS();
1261         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1262
1263         g_array_append_vals(in_param1, &type, sizeof(int));
1264         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1265         g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1266
1267         ret =  _bt_send_request(BT_AGENT_SERVICE, BT_UNSET_AUTHORIZATION,
1268                 in_param1, in_param2, in_param3, in_param4, &out_param);
1269
1270         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1271         BT_DBG("-");
1272         return ret;
1273 }
1274
1275 int _bt_check_privilege(int service_type, int service_function)
1276 {
1277         int result;
1278
1279         BT_INIT_PARAMS();
1280         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1281
1282         result = _bt_send_request(service_type, service_function,
1283                 in_param1, in_param2, in_param3, in_param4, &out_param);
1284
1285         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1286
1287         return result;
1288 }
1289
1290 GVariant *_bt_get_managed_objects(void)
1291 {
1292         GDBusConnection *g_conn;
1293         GDBusProxy *manager_proxy = NULL;
1294         GVariant *result = NULL;
1295         GError *error = NULL;
1296
1297         BT_DBG("+");
1298
1299         g_conn = _bt_gdbus_get_system_gconn();
1300         retv_if(g_conn == NULL, NULL);
1301
1302         manager_proxy = g_dbus_proxy_new_sync(g_conn,
1303                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1304                         BT_BLUEZ_NAME,
1305                         BT_MANAGER_PATH,
1306                         BT_MANAGER_INTERFACE,
1307                         NULL, &error);
1308
1309         if (error) {
1310                 BT_ERR("Unable to create proxy: %s", error->message);
1311                 g_clear_error(&error);
1312                 return NULL;
1313         }
1314
1315         result = g_dbus_proxy_call_sync (manager_proxy,
1316                         "GetManagedObjects", NULL,
1317                         G_DBUS_CALL_FLAGS_NONE, -1,
1318                         NULL, &error);
1319
1320         if (error) {
1321                 BT_ERR("Fail to get ManagedObjects (Error: %s)", error->message);
1322                 g_clear_error(&error);
1323         }
1324
1325         g_object_unref(manager_proxy);
1326
1327         BT_DBG("-");
1328         return result;
1329 }
1330
1331 BT_EXPORT_API int bluetooth_is_supported(void)
1332 {
1333         int is_supported = 0;
1334         int len = 0;
1335         int fd = -1;
1336         rfkill_event event;
1337
1338         fd = open(RFKILL_NODE, O_RDONLY);
1339         if (fd < 0) {
1340                 BT_ERR("Fail to open RFKILL node");
1341                 return BLUETOOTH_ERROR_INTERNAL;
1342         }
1343
1344         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
1345                 BT_ERR("Fail to set RFKILL node to non-blocking");
1346                 close(fd);
1347                 return BLUETOOTH_ERROR_INTERNAL;
1348         }
1349
1350         while (1) {
1351                 len = read(fd, &event, sizeof(event));
1352                 if (len < 0) {
1353                         BT_ERR("Fail to read events");
1354                         break;
1355                 }
1356
1357                 if (len != RFKILL_EVENT_SIZE) {
1358                         BT_ERR("The size is wrong\n");
1359                         continue;
1360                 }
1361
1362                 if (event.type == RFKILL_TYPE_BLUETOOTH) {
1363                         is_supported = 1;
1364                         break;
1365                 }
1366         }
1367
1368         close(fd);
1369
1370         BT_DBG("supported: %d", is_supported);
1371
1372         return is_supported;
1373 }
1374
1375 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
1376 {
1377         int ret;
1378
1379         _bt_gdbus_init_system_gconn();
1380         __bt_init_system_gconn();
1381
1382         ret = _bt_init_event_handler();
1383         if (ret != BLUETOOTH_ERROR_NONE &&
1384              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
1385                 BT_ERR("Fail to init the event handler");
1386                 return ret;
1387         }
1388
1389
1390         _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
1391
1392         /* Register All events */
1393         ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
1394         if (ret != BLUETOOTH_ERROR_NONE)
1395                 goto fail;
1396         ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
1397         if (ret != BLUETOOTH_ERROR_NONE)
1398                 goto fail;
1399         ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
1400         if (ret != BLUETOOTH_ERROR_NONE)
1401                 goto fail;
1402         ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
1403         if (ret != BLUETOOTH_ERROR_NONE)
1404                 goto fail;
1405         ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
1406         if (ret != BLUETOOTH_ERROR_NONE)
1407                 goto fail;
1408
1409         _bt_register_name_owner_changed();
1410
1411         return BLUETOOTH_ERROR_NONE;
1412 fail:
1413         BT_ERR("Fail to do _bt_register_event()");
1414         bluetooth_unregister_callback();
1415         return ret;
1416 }
1417
1418 BT_EXPORT_API int bluetooth_unregister_callback(void)
1419 {
1420         int ret;
1421
1422         ret = _bt_deinit_event_handler();
1423         if (ret != BLUETOOTH_ERROR_NONE) {
1424                 BT_ERR("Fail to deinit the event handler");
1425         }
1426
1427         _bt_unregister_name_owner_changed();
1428
1429         _bt_set_user_data(BT_COMMON, NULL, NULL);
1430
1431         if (system_conn) {
1432                 dbus_g_connection_unref(system_conn);
1433                 system_conn = NULL;
1434         }
1435         if (system_gconn) {
1436                 g_object_unref(system_gconn);
1437                 system_gconn = NULL;
1438         }
1439         return BLUETOOTH_ERROR_NONE;
1440 }
1441