Merge latest tizen_3.0 bug fix codes
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-common.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <string.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21 #include <sys/types.h>
22 #include <stdlib.h>
23 #include <sys/socket.h>
24
25
26 #include <gio/gunixfdlist.h>
27
28
29 #include "bluetooth-api.h"
30 #include "bluetooth-audio-api.h"
31 #include "bluetooth-hid-api.h"
32 #include "bluetooth-media-control.h"
33 #include "bt-internal-types.h"
34 #include "bluetooth-ipsp-api.h"
35
36 #include "bt-common.h"
37 #include "bt-request-sender.h"
38 #include "bt-event-handler.h"
39
40 #ifdef TIZEN_FEATURE_BT_DPM
41 #include "bt-dpm.h"
42 #endif
43
44
45 static bt_user_info_t user_info[BT_MAX_USER_INFO];
46 static GDBusConnection *system_gdbus_conn = NULL;
47
48
49 static guint bus_id;
50
51 static GDBusConnection *system_gconn = NULL;
52
53 static gboolean bt_enabled = FALSE;
54
55 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
56
57 GDBusConnection *g_bus_get_private_conn(void)
58 {
59         GError *error = NULL;
60         char *address;
61         GDBusConnection *private_gconn = NULL;
62
63         address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
64         if (address == NULL) {
65                 if (error) {
66                         BT_ERR("Failed to get bus address: %s", error->message);
67                         g_clear_error(&error);
68                 }
69                 return NULL;
70         }
71
72         private_gconn = g_dbus_connection_new_for_address_sync(address,
73                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
74                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
75                                 NULL, /* GDBusAuthObserver */
76                                 NULL,
77                                 &error);
78         if (!private_gconn) {
79                 if (error) {
80                         BT_ERR("Unable to connect to dbus: %s", error->message);
81                         g_clear_error(&error);
82                 }
83                 return NULL;
84         }
85
86         return private_gconn;
87 }
88
89 GDBusConnection *_bt_gdbus_init_system_gconn(void)
90 {
91         if (system_gconn != NULL)
92                 return system_gconn;
93
94         system_gconn = g_bus_get_private_conn();
95
96         return system_gconn;
97 }
98
99 GDBusConnection *_bt_gdbus_get_system_gconn(void)
100 {
101         if (system_gconn == NULL)
102                 system_gconn = _bt_gdbus_init_system_gconn();
103         else if (g_dbus_connection_is_closed(system_gconn))
104                 system_gconn = g_bus_get_private_conn();
105
106         return system_gconn;
107 }
108
109 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
110 {
111         BT_DBG("%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X\n", addr->addr[0], addr->addr[1], addr->addr[2],
112                                 addr->addr[3], addr->addr[4], addr->addr[5]);
113 }
114
115 void _bt_set_user_data(int type, void *callback, void *user_data)
116 {
117         user_info[type].cb = callback;
118         user_info[type].user_data = user_data;
119 }
120
121 bt_user_info_t *_bt_get_user_data(int type)
122 {
123         return &user_info[type];
124 }
125
126 void _bt_common_event_cb(int event, int result, void *param,
127                                         void *callback, void *user_data)
128 {
129         BT_DBG("bt_common_event_cb, event: %d", event);
130         bluetooth_event_param_t bt_event = { 0, };
131         bt_event.event = event;
132         bt_event.result = result;
133         bt_event.param_data = param;
134
135         if (callback)
136                 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
137                                         user_data);
138 }
139
140 void _bt_input_event_cb(int event, int result, void *param,
141                                         void *callback, void *user_data)
142 {
143         hid_event_param_t bt_event = { 0, };
144         bt_event.event = event;
145         bt_event.result = result;
146         bt_event.param_data = param;
147
148         if (callback)
149                 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
150                                         user_data);
151 }
152
153 void _bt_headset_event_cb(int event, int result, void *param,
154                                         void *callback, void *user_data)
155 {
156         bt_audio_event_param_t bt_event = { 0, };
157         bt_event.event = event;
158         bt_event.result = result;
159         bt_event.param_data = param;
160
161         if (callback)
162                 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
163                                         user_data);
164 }
165
166 void _bt_a2dp_source_event_cb(int event, int result, void *param,
167                                         void *callback, void *user_data)
168 {
169         bt_audio_event_param_t bt_event = { 0, };
170         bt_event.event = event;
171         bt_event.result = result;
172         bt_event.param_data = param;
173         if (callback)
174                 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
175                                         user_data);
176 }
177
178 void _bt_hf_event_cb(int event, int result, void *param,
179                                         void *callback, void *user_data)
180 {
181         bt_hf_event_param_t bt_event = { 0, };
182         bt_event.event = event;
183         bt_event.result = result;
184         bt_event.param_data = param;
185
186         if (callback)
187                 ((bt_hf_func_ptr)callback)(bt_event.event, &bt_event,
188                                         user_data);
189 }
190
191
192 void _bt_avrcp_event_cb(int event, int result, void *param,
193                                         void *callback, void *user_data)
194 {
195         media_event_param_t bt_event = { 0, };
196         bt_event.event = event;
197         bt_event.result = result;
198         bt_event.param_data = param;
199
200         if (callback)
201                 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
202                                         user_data);
203 }
204
205 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
206                                 unsigned int cod)
207 {
208         ret_if(device_class == NULL);
209
210         device_class->major_class = (unsigned short)(cod & 0x00001F00) >> 8;
211         device_class->minor_class = (unsigned short)((cod & 0x000000FC));
212         device_class->service_class = (unsigned long)((cod & 0x00FF0000));
213
214         if (cod & 0x002000) {
215                 device_class->service_class |=
216                 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
217         }
218 }
219
220 void _bt_convert_addr_string_to_type(unsigned char *addr,
221                                         const char *address)
222 {
223         int i;
224         char *ptr = NULL;
225
226         ret_if(address == NULL);
227         ret_if(addr == NULL);
228
229         for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
230                 addr[i] = strtol(address, &ptr, 16);
231                 if (ptr[0] != '\0') {
232                         if (ptr[0] != ':')
233                                 return;
234
235                         address = ptr + 1;
236                 }
237         }
238 }
239
240 void _bt_convert_addr_string_to_secure_string(char *addr,
241                                         const char *address)
242 {
243         int len;
244
245         ret_if(address == NULL);
246         ret_if(addr == NULL);
247
248         len = strlen(address);
249         ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
250
251         strncpy(addr, address, len);
252
253         addr[len-1] = 'X';
254         addr[len-2] = 'X';
255
256         return;
257 }
258
259 void _bt_convert_addr_type_to_string(char *address,
260                                 unsigned char *addr)
261 {
262         ret_if(address == NULL);
263         ret_if(addr == NULL);
264
265         g_snprintf(address, BT_ADDRESS_STRING_SIZE,
266                         "%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X",
267                         addr[0], addr[1], addr[2],
268                         addr[3], addr[4], addr[5]);
269 }
270
271 const char *_bt_convert_error_to_string(int error)
272 {
273         switch (error) {
274         case BLUETOOTH_ERROR_CANCEL:
275                 return "CANCELLED";
276         case BLUETOOTH_ERROR_INVALID_PARAM:
277                 return "INVALID_PARAMETER";
278         case BLUETOOTH_ERROR_INVALID_DATA:
279                 return "INVALID DATA";
280         case BLUETOOTH_ERROR_MEMORY_ALLOCATION:
281         case BLUETOOTH_ERROR_OUT_OF_MEMORY:
282                 return "OUT_OF_MEMORY";
283         case BLUETOOTH_ERROR_TIMEOUT:
284                 return "TIMEOUT";
285         case BLUETOOTH_ERROR_NO_RESOURCES:
286                 return "NO_RESOURCES";
287         case BLUETOOTH_ERROR_INTERNAL:
288                 return "INTERNAL";
289         case BLUETOOTH_ERROR_NOT_SUPPORT:
290                 return "NOT_SUPPORT";
291         case BLUETOOTH_ERROR_DEVICE_NOT_ENABLED:
292                 return "NOT_ENABLED";
293         case BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED:
294                 return "ALREADY_ENABLED";
295         case BLUETOOTH_ERROR_DEVICE_BUSY:
296                 return "DEVICE_BUSY";
297         case BLUETOOTH_ERROR_ACCESS_DENIED:
298                 return "ACCESS_DENIED";
299         case BLUETOOTH_ERROR_MAX_CLIENT:
300                 return "MAX_CLIENT";
301         case BLUETOOTH_ERROR_NOT_FOUND:
302                 return "NOT_FOUND";
303         case BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR:
304                 return "SERVICE_SEARCH_ERROR";
305         case BLUETOOTH_ERROR_PARING_FAILED:
306                 return "PARING_FAILED";
307         case BLUETOOTH_ERROR_NOT_PAIRED:
308                 return "NOT_PAIRED";
309         case BLUETOOTH_ERROR_SERVICE_NOT_FOUND:
310                 return "SERVICE_NOT_FOUND";
311         case BLUETOOTH_ERROR_NOT_CONNECTED:
312                 return "NOT_CONNECTED";
313         case BLUETOOTH_ERROR_ALREADY_CONNECT:
314                 return "ALREADY_CONNECT";
315         case BLUETOOTH_ERROR_CONNECTION_BUSY:
316                 return "CONNECTION_BUSY";
317         case BLUETOOTH_ERROR_CONNECTION_ERROR:
318                 return "CONNECTION_ERROR";
319         case BLUETOOTH_ERROR_MAX_CONNECTION:
320                 return "MAX_CONNECTION";
321         case BLUETOOTH_ERROR_NOT_IN_OPERATION:
322                 return "NOT_IN_OPERATION";
323         case BLUETOOTH_ERROR_CANCEL_BY_USER:
324                 return "CANCEL_BY_USER";
325         case BLUETOOTH_ERROR_REGISTRATION_FAILED:
326                 return "REGISTRATION_FAILED";
327         case BLUETOOTH_ERROR_IN_PROGRESS:
328                 return "IN_PROGRESS";
329         case BLUETOOTH_ERROR_AUTHENTICATION_FAILED:
330                 return "AUTHENTICATION_FAILED";
331         case BLUETOOTH_ERROR_HOST_DOWN:
332                 return "HOST_DOWN";
333         case BLUETOOTH_ERROR_END_OF_DEVICE_LIST:
334                 return "END_OF_DEVICE_LIST";
335         case BLUETOOTH_ERROR_AGENT_ALREADY_EXIST:
336                 return "AGENT_ALREADY_EXIST";
337         case BLUETOOTH_ERROR_AGENT_DOES_NOT_EXIST:
338                 return "AGENT_DOES_NOT_EXIST";
339         case BLUETOOTH_ERROR_ALREADY_INITIALIZED:
340                 return "ALREADY_INITIALIZED";
341         case BLUETOOTH_ERROR_PERMISSION_DEINED:
342                 return "PERMISSION_DEINED";
343         case BLUETOOTH_ERROR_ALREADY_DEACTIVATED:
344                 return "ALREADY_DEACTIVATED";
345         case BLUETOOTH_ERROR_NOT_INITIALIZED:
346                 return "NOT_INITIALIZED";
347         case BLUETOOTH_ERROR_AUTHENTICATION_REJECTED:
348                 return "AUTHENTICATION REJECTED";
349         default:
350                 return "UNKNOWN";
351         }
352 }
353
354 const char *_bt_convert_service_function_to_string(int function)
355 {
356         int i;
357
358         typedef struct {
359                 int function;
360                 const char *function_name;
361         } bt_function_name_t;
362
363         const bt_function_name_t bt_functions[] = {
364                 {BT_CHECK_ADAPTER, "BT_CHECK_ADAPTER"},
365                 {BT_ENABLE_ADAPTER, "BT_ENABLE_ADAPTER"},
366                 {BT_DISABLE_ADAPTER, "BT_DISABLE_ADAPTER"},
367                 {BT_RECOVER_ADAPTER, "BT_RECOVER_ADAPTER"},
368                 {BT_SET_DISCOVERABLE_TIME, "BT_SET_DISCOVERABLE_TIME"},
369                 {BT_GET_DISCOVERABLE_TIME, "BT_GET_DISCOVERABLE_TIME"},
370                 {BT_IGNORE_AUTO_PAIRING, "BT_IGNORE_AUTO_PAIRING"},
371                 {BT_GET_LOCAL_ADDRESS, "BT_GET_LOCAL_ADDRESS"},
372                 {BT_GET_LOCAL_VERSION, "BT_GET_LOCAL_VERSION"},
373                 {BT_GET_LOCAL_NAME, "BT_GET_LOCAL_NAME"},
374                 {BT_SET_LOCAL_NAME, "BT_SET_LOCAL_NAME"},
375                 {BT_IS_SERVICE_USED, "BT_IS_SERVICE_USED"},
376                 {BT_GET_DISCOVERABLE_MODE, "BT_GET_DISCOVERABLE_MODE"},
377                 {BT_SET_DISCOVERABLE_MODE, "BT_SET_DISCOVERABLE_MODE"},
378                 {BT_START_DISCOVERY, "BT_START_DISCOVERY"},
379                 {BT_START_CUSTOM_DISCOVERY, "BT_START_CUSTOM_DISCOVERY"},
380                 {BT_CANCEL_DISCOVERY, "BT_CANCEL_DISCOVERY"},
381                 {BT_START_LE_DISCOVERY, "BT_START_LE_DISCOVERY"},
382                 {BT_STOP_LE_DISCOVERY, "BT_STOP_LE_DISCOVERY"},
383                 {BT_IS_DISCOVERYING, "BT_IS_DISCOVERYING"},
384                 {BT_IS_LE_DISCOVERYING, "BT_IS_LE_DISCOVERYING"},
385                 {BT_ENABLE_RSSI, "BT_ENABLE_RSSI"},
386                 {BT_GET_RSSI, "BT_GET_RSSI"},
387                 {BT_IS_CONNECTABLE, "BT_IS_CONNECTABLE"},
388                 {BT_SET_CONNECTABLE, "BT_SET_CONNECTABLE"},
389                 {BT_GET_BONDED_DEVICES, "BT_GET_BONDED_DEVICES"},
390                 {BT_RESET_ADAPTER, "BT_RESET_ADAPTER"},
391                 {BT_SET_ADVERTISING, "BT_SET_ADVERTISING"},
392                 {BT_SET_CUSTOM_ADVERTISING, "BT_SET_CUSTOM_ADVERTISING"},
393                 {BT_SET_ADVERTISING_PARAMETERS, "BT_SET_ADVERTISING_PARAMETERS"},
394                 {BT_GET_ADVERTISING_DATA, "BT_GET_ADVERTISING_DATA"},
395                 {BT_SET_ADVERTISING_DATA, "BT_SET_ADVERTISING_DATA"},
396                 {BT_SET_SCAN_PARAMETERS, "BT_SET_SCAN_PARAMETERS"},
397                 {BT_GET_SCAN_RESPONSE_DATA, "BT_GET_SCAN_RESPONSE_DATA"},
398                 {BT_SET_SCAN_RESPONSE_DATA, "BT_SET_SCAN_RESPONSE_DATA"},
399                 {BT_IS_ADVERTISING, "BT_IS_ADVERTISING"},
400                 {BT_SET_MANUFACTURER_DATA, "BT_SET_MANUFACTURER_DATA"},
401                 {BT_LE_CONN_UPDATE, "BT_LE_CONN_UPDATE"},
402                 {BT_LE_READ_MAXIMUM_DATA_LENGTH, "BT_LE_READ_MAXIMUM_DATA_LENGTH"},
403                 {BT_LE_WRITE_HOST_SUGGESTED_DATA_LENGTH, "BT_LE_WRITE_HOST_SUGGESTED_DATA_LENGTH"},
404                 {BT_LE_READ_HOST_SUGGESTED_DATA_LENGTH, "BT_LE_READ_HOST_SUGGESTED_DATA_LENGTH"},
405                 {BT_LE_SET_DATA_LENGTH, "BT_LE_SET_DATA_LENGTH"},
406                 {BT_ADD_WHITE_LIST, "BT_ADD_WHITE_LIST"},
407                 {BT_REMOVE_WHITE_LIST, "BT_REMOVE_WHITE_LIST"},
408                 {BT_CLEAR_WHITE_LIST, "BT_CLEAR_WHITE_LIST"},
409                 {BT_REGISTER_SCAN_FILTER, "BT_REGISTER_SCAN_FILTER"},
410                 {BT_UNREGISTER_SCAN_FILTER, "BT_UNREGISTER_SCAN_FILTER"},
411                 {BT_UNREGISTER_ALL_SCAN_FILTERS, "BT_UNREGISTER_ALL_SCAN_FILTERS"},
412                 {BT_IS_SCAN_FILTER_SUPPORTED, "BT_IS_SCAN_FILTER_SUPPORTED"},
413                 {BT_GET_PROFILE_CONNECTED_DEVICES, "BT_GET_PROFILE_CONNECTED_DEVICES"},
414                 {BT_ENABLE_FORCE_HCI_DUMP, "BT_ENABLE_FORCE_HCI_DUMP"},
415                 {BT_SET_PASSKEY_NOTIFICATION, "BT_SET_PASSKEY_NOTIFICATION"},
416                 {BT_BOND_DEVICE, "BT_BOND_DEVICE"},
417                 {BT_BOND_DEVICE_BY_TYPE, "BT_BOND_DEVICE_BY_TYPE"},
418                 {BT_CANCEL_BONDING, "BT_CANCEL_BONDING"},
419                 {BT_PASSKEY_REPLY, "BT_PASSKEY_REPLY"},
420                 {BT_PASSKEY_CONFIRMATION_REPLY, "BT_PASSKEY_CONFIRMATION_REPLY"},
421                 {BT_UNBOND_DEVICE, "BT_UNBOND_DEVICE"},
422                 {BT_SEARCH_SERVICE, "BT_SEARCH_SERVICE"},
423                 {BT_CANCEL_SEARCH_SERVICE, "BT_CANCEL_SEARCH_SERVICE"},
424                 {BT_GET_BONDED_DEVICE, "BT_GET_BONDED_DEVICE"},
425                 {BT_GET_IS_ALIAS_SET, "BT_GET_IS_ALIAS_SET"},
426                 {BT_SET_ALIAS, "BT_SET_ALIAS"},
427                 {BT_SET_AUTHORIZATION, "BT_SET_AUTHORIZATION"},
428                 {BT_UNSET_AUTHORIZATION, "BT_UNSET_AUTHORIZATION"},
429                 {BT_IS_DEVICE_CONNECTED, "BT_IS_DEVICE_CONNECTED"},
430                 {BT_GET_CONNECTED_LINK_TYPE, "BT_GET_CONNECTED_LINK_TYPE"},
431                 {BT_SET_PIN_CODE, "BT_SET_PIN_CODE"},
432                 {BT_UNSET_PIN_CODE, "BT_UNSET_PIN_CODE"},
433                 {BT_UPDATE_LE_CONNECTION_MODE, "BT_UPDATE_LE_CONNECTION_MODE"},
434                 {BT_SET_PROFILE_TRUSTED, "BT_SET_PROFILE_TRUSTED"},
435                 {BT_GET_PROFILE_TRUSTED, "BT_GET_PROFILE_TRUSTED"},
436                 {BT_SET_PROFILE_RESTRICTED, "BT_SET_PROFILE_RESTRICTED"},
437                 {BT_GET_PROFILE_RESTRICTED, "BT_GET_PROFILE_RESTRICTED"},
438                 {BT_HID_CONNECT, "BT_HID_CONNECT"},
439                 {BT_HID_DISCONNECT, "BT_HID_DISCONNECT"},
440                 {BT_HID_DEVICE_ACTIVATE, "BT_HID_DEVICE_ACTIVATE"},
441                 {BT_HID_DEVICE_DEACTIVATE, "BT_HID_DEVICE_DEACTIVATE"},
442                 {BT_HID_DEVICE_CONNECT, "BT_HID_DEVICE_CONNECT"},
443                 {BT_HID_DEVICE_DISCONNECT, "BT_HID_DEVICE_DISCONNECT"},
444                 {BT_HID_DEVICE_SEND_MOUSE_EVENT, "BT_HID_DEVICE_SEND_MOUSE_EVENT"},
445                 {BT_HID_DEVICE_SEND_KEY_EVENT, "BT_HID_DEVICE_SEND_KEY_EVENT"},
446                 {BT_HID_DEVICE_SEND_CUSTOM_EVENT, "BT_HID_DEVICE_SEND_CUSTOM_EVENT"},
447                 {BT_HID_DEVICE_SEND_REPLY_TO_REPORT, "BT_HID_DEVICE_SEND_REPLY_TO_REPORT"},
448                 {BT_HID_ENABLE_BARCODE_FEATURE, "BT_HID_ENABLE_BARCODE_FEATURE"},
449                 {BT_NETWORK_ACTIVATE, "BT_NETWORK_ACTIVATE"},
450                 {BT_NETWORK_DEACTIVATE, "BT_NETWORK_DEACTIVATE"},
451                 {BT_NETWORK_CONNECT, "BT_NETWORK_CONNECT"},
452                 {BT_NETWORK_DISCONNECT, "BT_NETWORK_DISCONNECT"},
453                 {BT_NETWORK_SERVER_DISCONNECT, "BT_NETWORK_SERVER_DISCONNECT"},
454                 {BT_AUDIO_CONNECT, "BT_AUDIO_CONNECT"},
455                 {BT_AUDIO_DISCONNECT, "BT_AUDIO_DISCONNECT"},
456                 {BT_AG_CONNECT, "BT_AG_CONNECT"},
457                 {BT_AG_DISCONNECT, "BT_AG_DISCONNECT"},
458                 {BT_AV_CONNECT, "BT_AV_CONNECT"},
459                 {BT_AV_DISCONNECT, "BT_AV_DISCONNECT"},
460                 {BT_AV_SOURCE_CONNECT, "BT_AV_SOURCE_CONNECT"},
461                 {BT_AV_SOURCE_DISCONNECT, "BT_AV_SOURCE_DISCONNECT"},
462                 {BT_HF_CONNECT, "BT_HF_CONNECT"},
463                 {BT_HF_DISCONNECT, "BT_HF_DISCONNECT"},
464                 {BT_GET_SPEAKER_GAIN, "BT_GET_SPEAKER_GAIN"},
465                 {BT_SET_SPEAKER_GAIN, "BT_SET_SPEAKER_GAIN"},
466                 {BT_SET_CONTENT_PROTECT, "BT_SET_CONTENT_PROTECT"},
467                 {BT_OOB_READ_LOCAL_DATA, "BT_OOB_READ_LOCAL_DATA"},
468                 {BT_OOB_ADD_REMOTE_DATA, "BT_OOB_ADD_REMOTE_DATA"},
469                 {BT_OOB_REMOVE_REMOTE_DATA, "BT_OOB_REMOVE_REMOTE_DATA"},
470                 {BT_AVRCP_SET_TRACK_INFO, "BT_AVRCP_SET_TRACK_INFO"},
471                 {BT_AVRCP_SET_PROPERTY, "BT_AVRCP_SET_PROPERTY"},
472                 {BT_AVRCP_SET_PROPERTIES, "BT_AVRCP_SET_PROPERTIES"},
473                 {BT_AVRCP_CONTROL_CONNECT, "BT_AVRCP_CONTROL_CONNECT"},
474                 {BT_AVRCP_CONTROL_DISCONNECT, "BT_AVRCP_CONTROL_DISCONNECT"},
475                 {BT_AVRCP_TARGET_CONNECT, "BT_AVRCP_TARGET_CONNECT"},
476                 {BT_AVRCP_TARGET_DISCONNECT, "BT_AVRCP_TARGET_DISCONNECT"},
477                 {BT_AVRCP_HANDLE_CONTROL, "BT_AVRCP_HANDLE_CONTROL"},
478                 {BT_AVRCP_CONTROL_SET_PROPERTY, "BT_AVRCP_CONTROL_SET_PROPERTY"},
479                 {BT_AVRCP_CONTROL_GET_PROPERTY, "BT_AVRCP_CONTROL_GET_PROPERTY"},
480                 {BT_AVRCP_GET_TRACK_INFO, "BT_AVRCP_GET_TRACK_INFO"},
481                 {BT_OPP_PUSH_FILES, "BT_OPP_PUSH_FILES"},
482                 {BT_OPP_CANCEL_PUSH, "BT_OBT_OPP_IS_PUSHING_FILESPP_CANCEL_PUSH"},
483                 {BT_OPP_IS_PUSHING_FILES, "BT_OPP_IS_PUSHING_FILES"},
484                 {BT_OPP_GET_TRANSFER_PROGRESS, "BT_OPP_GET_TRANSFER_PROGRESS"},
485                 {BT_MAP_CREATE_SESSION, "BT_MAP_CREATE_SESSION"},
486                 {BT_MAP_DESTROY_SESSION, "BT_MAP_DESTROY_SESSION"},
487                 {BT_MAP_SET_FOLDER, "BT_MAP_SET_FOLDER"},
488                 {BT_MAP_LIST_FOLDERS, "BT_MAP_LIST_FOLDERS"},
489                 {BT_MAP_LIST_FILTER_FIELDS, "BT_MAP_LIST_FILTER_FIELDS"},
490                 {BT_MAP_LIST_MESSAGES, "BT_MAP_LIST_MESSAGES"},
491                 {BT_MAP_UPDATE_INBOX, "BT_MAP_UPDATE_INBOX"},
492                 {BT_MAP_PUSH_MESSAGE, "BT_MAP_PUSH_MESSAGE"},
493                 {BT_MAP_GET_MESSAGE, "BT_MAP_GET_MESSAGE"},
494                 {BT_OBEX_SERVER_ALLOCATE, "BT_OBEX_SERVER_ALLOCATE"},
495                 {BT_OBEX_SERVER_DEALLOCATE, "BT_OBEX_SERVER_DEALLOCATE"},
496                 {BT_OBEX_SERVER_IS_ACTIVATED, "BT_OBEX_SERVER_IS_ACTIVATED"},
497                 {BT_OBEX_SERVER_ACCEPT_CONNECTION, "BT_OBEX_SERVER_ACCEPT_CONNECTION"},
498                 {BT_OBEX_SERVER_REJECT_CONNECTION, "BT_OBEX_SERVER_REJECT_CONNECTION"},
499                 {BT_OBEX_SERVER_ACCEPT_FILE, "BT_OBEX_SERVER_ACCEPT_FILE"},
500                 {BT_OBEX_SERVER_REJECT_FILE, "BT_OBEX_SERVER_REJECT_FILE"},
501                 {BT_OBEX_SERVER_SET_PATH, "BT_OBEX_SERVER_SET_PATH"},
502                 {BT_OBEX_SERVER_SET_ROOT, "BT_OBEX_SERVER_SET_ROOT"},
503                 {BT_OBEX_SERVER_CANCEL_TRANSFER, "BT_OBEX_SERVER_CANCEL_TRANSFER"},
504                 {BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS, "BT_OBEX_SERVER_CANCEL_ALL_TRANSFERS"},
505                 {BT_OBEX_SERVER_IS_RECEIVING, "BT_OBEX_SERVER_IS_RECEIVING"},
506                 {BT_RFCOMM_CLIENT_CONNECT, "BT_RFCOMM_CLIENT_CONNECT"},
507                 {BT_RFCOMM_CLIENT_CANCEL_CONNECT, "BT_RFCOMM_CLIENT_CANCEL_CONNECT"},
508                 {BT_RFCOMM_CLIENT_IS_CONNECTED, "BT_RFCOMM_CLIENT_IS_CONNECTED"},
509                 {BT_RFCOMM_SOCKET_DISCONNECT, "BT_RFCOMM_SOCKET_DISCONNECT"},
510                 {BT_RFCOMM_SOCKET_WRITE, "BT_RFCOMM_SOCKET_WRITE"},
511                 {BT_RFCOMM_CREATE_SOCKET, "BT_RFCOMM_CREATE_SOCKET"},
512                 {BT_RFCOMM_REMOVE_SOCKET, "BT_RFCOMM_REMOVE_SOCKET"},
513                 {BT_RFCOMM_LISTEN, "BT_RFCOMM_LISTEN"},
514                 {BT_RFCOMM_IS_UUID_AVAILABLE, "BT_RFCOMM_IS_UUID_AVAILABLE"},
515                 {BT_RFCOMM_ACCEPT_CONNECTION, "BT_RFCOMM_ACCEPT_CONNECTION"},
516                 {BT_RFCOMM_REJECT_CONNECTION, "BT_RFCOMM_REJECT_CONNECTION"},
517                 {BT_RFCOMM_CREATE_SOCKET_EX, "BT_RFCOMM_CREATE_SOCKET_EX"},
518                 {BT_RFCOMM_REMOVE_SOCKET_EX, "BT_RFCOMM_REMOVE_SOCKET_EX"},
519                 {BT_PBAP_CONNECT, "BT_PBAP_CONNECT"},
520                 {BT_PBAP_DISCONNECT, "BT_PBAP_DISCONNECT"},
521                 {BT_PBAP_GET_PHONEBOOK_SIZE, "BT_PBAP_GET_PHONEBOOK_SIZE"},
522                 {BT_PBAP_GET_PHONEBOOK, "BT_PBAP_GET_PHONEBOOK"},
523                 {BT_PBAP_GET_LIST, "BT_PBAP_GET_LIST"},
524                 {BT_PBAP_PULL_VCARD, "BT_PBAP_PULL_VCARD"},
525                 {BT_PBAP_PHONEBOOK_SEARCH, "BT_PBAP_PHONEBOOK_SEARCH"},
526                 {BT_ENABLE_ADAPTER_LE, "BT_ENABLE_ADAPTER_LE"},
527                 {BT_DISABLE_ADAPTER_LE, "BT_DISABLE_ADAPTER_LE"},
528                 {BT_CONNECT_LE, "BT_CONNECT_LE"},
529                 {BT_DISCONNECT_LE, "BT_DISCONNECT_LE"},
530                 {BT_SET_LE_PRIVACY, "BT_SET_LE_PRIVACY"},
531                 {BT_REQ_ATT_MTU, "BT_REQ_ATT_MTU"},
532                 {BT_GET_ATT_MTU, "BT_GET_ATT_MTU"},
533                 {BT_GET_DEVICE_IDA, "BT_GET_DEVICE_IDA"},
534                 {BT_SET_LE_STATIC_RANDOM_ADDRESS, "BT_SET_LE_STATIC_RANDOM_ADDRESS"},
535                 {BT_HDP_CONNECT, "BT_HDP_CONNECT"},
536                 {BT_HDP_DISCONNECT, "BT_HDP_DISCONNECT"},
537                 {BT_HDP_SEND_DATA, "BT_HDP_SEND_DATA"},
538                 {BT_HDP_REGISTER_SINK_APP, "BT_HDP_REGISTER_SINK_APP"},
539                 {BT_HDP_UNREGISTER_SINK_APP, "BT_HDP_UNREGISTER_SINK_APP"},
540                 {BT_GATT_GET_PRIMARY_SERVICES, "BT_GATT_GET_PRIMARY_SERVICES"},
541                 {BT_GATT_DISCOVER_CHARACTERISTICS, "BT_GATT_DISCOVER_CHARACTERISTICS"},
542                 {BT_GATT_SET_PROPERTY_REQUEST, "BT_GATT_SET_PROPERTY_REQUEST"},
543                 {BT_GATT_READ_CHARACTERISTIC, "BT_GATT_READ_CHARACTERISTIC"},
544                 {BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR, "BT_GATT_DISCOVER_CHARACTERISTICS_DESCRIPTOR"},
545                 {BT_GATT_REGISTER_APPLICATION, "BT_GATT_REGISTER_APPLICATION"},
546                 {BT_GATT_REGISTER_SERVICE, "BT_GATT_REGISTER_SERVICE"},
547                 {BT_GATT_SEND_RESPONSE, "BT_GATT_SEND_RESPONSE"},
548                 {BT_LE_IPSP_INIT, "BT_LE_IPSP_INIT"},
549                 {BT_LE_IPSP_DEINIT, "BT_LE_IPSP_DEINIT"},
550                 {BT_LE_IPSP_CONNECT, "BT_LE_IPSP_CONNECT"},
551                 {BT_LE_IPSP_DISCONNECT, "BT_LE_IPSP_DISCONNECT"},
552                 {BT_DPM_SET_ALLOW_BT_MODE, "BT_DPM_SET_ALLOW_BT_MODE"},
553                 {BT_DPM_GET_ALLOW_BT_MODE, "BT_DPM_GET_ALLOW_BT_MODE"},
554                 {BT_DPM_SET_DEVICE_RESTRITION, "BT_DPM_SET_DEVICE_RESTRITION"},
555                 {BT_DPM_GET_DEVICE_RESTRITION, "BT_DPM_GET_DEVICE_RESTRITION"},
556                 {BT_DPM_SET_UUID_RESTRITION, "BT_DPM_SET_UUID_RESTRITION"},
557                 {BT_DPM_GET_UUID_RESTRITION, "BT_DPM_GET_UUID_RESTRITION"},
558                 {BT_DPM_ADD_DEVICES_BLACKLIST, "BT_DPM_ADD_DEVICES_BLACKLIST"},
559                 {BT_DPM_ADD_DEVICES_WHITELIST, "BT_DPM_ADD_DEVICES_WHITELIST"},
560                 {BT_DPM_ADD_UUIDS_BLACKLIST, "BT_DPM_ADD_UUIDS_BLACKLIST"},
561                 {BT_DPM_ADD_UUIDS_WHITELIST, "BT_DPM_ADD_UUIDS_WHITELIST"},
562                 {BT_DPM_CLEAR_DEVICES_BLACKLIST, "BT_DPM_CLEAR_DEVICES_BLACKLIST"},
563                 {BT_DPM_CLEAR_DEVICES_WHITELIST, "BT_DPM_CLEAR_DEVICES_WHITELIST"},
564                 {BT_DPM_CLEAR_UUIDS_BLACKLIST, "BT_DPM_CLEAR_UUIDS_BLACKLIST"},
565                 {BT_DPM_CLEAR_UUIDS_WHITELIST, "BT_DPM_CLEAR_UUIDS_WHITELIST"},
566                 {BT_DPM_REMOVE_DEVICE_BLACKLIST, "BT_DPM_REMOVE_DEVICE_BLACKLIST"},
567                 {BT_DPM_REMOVE_DEVICE_WHITELIST, "BT_DPM_REMOVE_DEVICE_WHITELIST"},
568                 {BT_DPM_REMOVE_UUID_BLACKLIST, "BT_DPM_REMOVE_UUID_BLACKLIST"},
569                 {BT_DPM_REMOVE_UUID_WHITELIST, "BT_DPM_REMOVE_UUID_WHITELIST"},
570                 {BT_DPM_GET_DEVICES_BLACKLIST, "BT_DPM_GET_DEVICES_BLACKLIST"},
571                 {BT_DPM_GET_DEVICES_WHITELIST, "BT_DPM_GET_DEVICES_WHITELIST"},
572                 {BT_DPM_GET_UUIDS_BLACKLIST, "BT_DPM_GET_UUIDS_BLACKLIST"},
573                 {BT_DPM_GET_UUIDS_WHITELIST, "BT_DPM_GET_UUIDS_WHITELIST"},
574                 {BT_DPM_SET_ALLOW_OUTGOING_CALL, "BT_DPM_SET_ALLOW_OUTGOING_CALL"},
575                 {BT_DPM_GET_ALLOW_OUTGOING_CALL, "BT_DPM_GET_ALLOW_OUTGOING_CALL"},
576                 {BT_DPM_SET_PAIRING_STATE, "BT_DPM_SET_PAIRING_STATE"},
577                 {BT_DPM_GET_PAIRING_STATE, "BT_DPM_GET_PAIRING_STATE"},
578                 {BT_DPM_SET_PROFILE_STATE, "BT_DPM_SET_PROFILE_STATE"},
579                 {BT_DPM_GET_PROFILE_STATE, "BT_DPM_GET_PROFILE_STATE"},
580                 {BT_DPM_SET_DESKROP_CONNECTIVITY_STATE, "BT_DPM_SET_DESKROP_CONNECTIVITY_STATE"},
581                 {BT_DPM_GET_DESKROP_CONNECTIVITY_STATE, "BT_DPM_GET_DESKROP_CONNECTIVITY_STATE"},
582                 {BT_DPM_SET_DISCOVERABLE_STATE, "BT_DPM_SET_DISCOVERABLE_STATE"},
583                 {BT_DPM_GET_DISCOVERABLE_STATE, "BT_DPM_GET_DISCOVERABLE_STATE"},
584                 {BT_DPM_SET_LIMITED_DISCOVERABLE_STATE, "BT_DPM_SET_LIMITED_DISCOVERABLE_STATE"},
585                 {BT_DPM_GET_LIMITED_DISCOVERABLE_STATE, "BT_DPM_GET_LIMITED_DISCOVERABLE_STATE"},
586                 {BT_DPM_SET_DATA_TRANSFER_STATE, "BT_DPM_SET_DATA_TRANSFER_STATE"},
587                 {BT_DPM_GET_DATA_TRANSFER_STATE, "BT_DPM_GET_DATA_TRANSFER_STATE"},
588                 {BT_PXP_MONITOR_SET_PROPERTY, "BT_PXP_MONITOR_SET_PROPERTY"},
589                 {BT_PXP_MONITOR_GET_PROPERTY, "BT_PXP_MONITOR_GET_PROPERTY"},
590                 {BT_PXP_MONITOR_GET_SUPPORTED_SERIVCES, "BT_PXP_MONITOR_GET_SUPPORTED_SERIVCES"},
591                 {BT_PXP_REPORTER_REGISTER, "BT_PXP_REPORTER_REGISTER"},
592                 {BT_PXP_REPORTER_UNREGISTER, "BT_PXP_REPORTER_UNREGISTER"},
593                 {BT_PXP_REPORTER_GET_PROPERTY, "BT_PXP_REPORTER_GET_PROPERTY"},
594                 {BT_TDS_PROVIDER_REGISTER, "BT_TDS_PROVIDER_REGISTER"},
595                 {BT_TDS_PROVIDER_UNREGISTER, "BT_TDS_PROVIDER_UNREGISTER"},
596                 {BT_TDS_PROVIDER_SET_MANUF_DATA, "BT_TDS_PROVIDER_SET_MANUF_DATA"},
597                 {BT_TDS_PROVIDER_CREATE, "BT_TDS_PROVIDER_CREATE"},
598                 {BT_TDS_PROVIDER_DESTROY, "BT_TDS_PROVIDER_DESTROY"},
599                 {BT_TDS_PROVIDER_SET_TRANSPORT_DATA, "BT_TDS_PROVIDER_SET_TRANSPORT_DATA"},
600                 {BT_TDS_SEND_ACTIVATION_RESPONSE, "BT_TDS_SEND_ACTIVATION_RESPONSE"},
601                 {BT_TDS_READ_TRANSPORT_DATA, "BT_TDS_READ_TRANSPORT_DATA"},
602                 {BT_TDS_ENABLE_CONTROL_POINT, "BT_TDS_ENABLE_CONTROL_POINT"},
603                 {BT_TDS_ACTIVATE_CONTROL_POINT, "BT_TDS_ACTIVATE_CONTROL_POINT"},
604                 {-1, ""},
605         };
606
607         for (i = 0; bt_functions[i].function != -1; i++) {
608                 if (bt_functions[i].function == function)
609                         return bt_functions[i].function_name;
610         }
611
612         return NULL;
613 }
614
615 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
616 {
617         int i;
618         const char *p = src;
619         char *next;
620         int count;
621
622         if (dest == NULL || src == NULL)
623                 return BLUETOOTH_ERROR_INVALID_PARAM;
624
625         i = 0;
626         while (*p != '\0' && i < length) {
627                 next = g_utf8_next_char(p);
628                 count = next - p;
629
630                 while (count > 0 && ((i + count) < length)) {
631                         dest[i++] = *p;
632                         p++;
633                         count--;
634                 }
635                 p = next;
636         }
637         return BLUETOOTH_ERROR_NONE;
638 }
639
640 gboolean _bt_utf8_validate(char *name)
641 {
642         BT_DBG("+");
643         gunichar2 *u16;
644         glong items_written = 0;
645
646         if (FALSE == g_utf8_validate(name, -1, NULL))
647                 return FALSE;
648
649         u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
650         if (u16 == NULL)
651                 return FALSE;
652
653         g_free(u16);
654
655         if (items_written != g_utf8_strlen(name, -1))
656                 return FALSE;
657
658         BT_DBG("-");
659         return TRUE;
660 }
661
662
663 static GDBusProxy *profile_gproxy;
664 static GDBusConnection *gconn;
665 static int latest_id = -1;
666 #define BT_RFCOMM_ID_MAX 245
667 static gboolean id_used[BT_RFCOMM_ID_MAX];
668 GDBusNodeInfo *new_conn_node;
669
670 static const gchar rfcomm_agent_xml[] =
671 "<node name='/'>"
672 " <interface name='org.bluez.Profile1'>"
673 "     <method name='NewConnection'>"
674 "          <arg type='o' name='object' direction='in'/>"
675 "          <arg type='h' name='fd' direction='in'/>"
676 "          <arg type='a{sv}' name='properties' direction='in'/>"
677 "     </method>"
678 "     <method name='RequestDisconnection'>"
679 "          <arg type='o' name='device' direction='in'/>"
680 "     </method>"
681 "  </interface>"
682 "</node>";
683
684 static void __new_connection_method(GDBusConnection *connection,
685                                             const gchar *sender,
686                                             const gchar *object_path,
687                                             const gchar *interface_name,
688                                             const gchar *method_name,
689                                             GVariant *parameters,
690                                             GDBusMethodInvocation *invocation,
691                                             gpointer user_data)
692 {
693         BT_DBG("method %s", method_name);
694         if (g_strcmp0(method_name, "NewConnection") == 0) {
695                 int index;
696                 GDBusMessage *msg;
697                 GUnixFDList *fd_list;
698                 GVariantBuilder *properties;
699                 char *obj_path;
700                 char addr[20];
701                 bluetooth_device_address_t  remote_addr1;
702                 bt_new_connection_cb cb = user_data;
703                 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
704                 int fd;
705
706                 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
707                                                                 &properties);
708
709                 msg = g_dbus_method_invocation_get_message(invocation);
710                 fd_list = g_dbus_message_get_unix_fd_list(msg);
711                 if (fd_list == NULL) {
712                         BT_ERR("fd_list is NULL");
713                         GQuark quark = g_quark_from_string("rfcomm-app");
714                         GError *err = g_error_new(quark, 0, "No fd in message");
715                         g_dbus_method_invocation_return_gerror(invocation, err);
716                         g_error_free(err);
717                         return;
718                 }
719
720
721                 fd = g_unix_fd_list_get(fd_list, index, NULL);
722                 if (fd == -1) {
723                         BT_ERR("Invalid fd return");
724                         GQuark quark = g_quark_from_string("rfcomm-app");
725                         GError *err = g_error_new(quark, 0, "Invalid FD return");
726                         g_dbus_method_invocation_return_gerror(invocation, err);
727                         g_error_free(err);
728                         return;
729                 }
730
731                 _bt_convert_device_path_to_address(obj_path, addr);
732                 _bt_convert_addr_string_to_type(remote_addr1.addr, (const char *)addr);
733
734                 _bt_convert_addr_string_to_secure_string(secure_address, addr);
735                 BT_INFO("fd: %d, address %s", fd, secure_address);
736
737                 g_dbus_method_invocation_return_value(invocation, NULL);
738
739                 if (cb)
740                         cb(object_path, fd, &remote_addr1);
741         } else if (g_strcmp0(method_name, "RequestDisconnection") == 0) {
742                 g_dbus_method_invocation_return_value(invocation, NULL);
743         }
744 }
745
746
747 static const GDBusInterfaceVTable method_table = {
748         __new_connection_method,
749         NULL,
750         NULL,
751 };
752
753 void _bt_swap_addr(unsigned char *dst, const unsigned char *src)
754 {
755         int i;
756
757         for (i = 0; i < 6; i++)
758                 dst[i] = src[5-i];
759 }
760
761 int __rfcomm_assign_id(void)
762 {
763         int index;
764
765         BT_DBG("latest_id: %d", latest_id);
766
767         index = latest_id + 1;
768
769         if (index >= BT_RFCOMM_ID_MAX)
770                 index = 0;
771
772         BT_DBG("index: %d", index);
773
774         while (id_used[index] == TRUE) {
775                 if (index == latest_id) {
776                         /* No available ID */
777                         BT_ERR("All request ID is used");
778                         return -1;
779                 }
780
781                 index++;
782
783                 if (index >= BT_RFCOMM_ID_MAX)
784                         index = 0;
785         }
786
787         latest_id = index;
788         id_used[index] = TRUE;
789
790         BT_DBG("Assigned Id: %d", latest_id);
791
792         return latest_id;
793 }
794
795 void __rfcomm_delete_id(int id)
796 {
797         ret_if(id >= BT_RFCOMM_ID_MAX);
798         ret_if(id < 0);
799
800         id_used[id] = FALSE;
801
802         /* Next server will use this ID */
803         latest_id = id - 1;
804 }
805
806 static GDBusConnection *__get_gdbus_connection()
807 {
808         if (gconn == NULL)
809                 gconn = g_bus_get_private_conn();
810
811         return gconn;
812 }
813
814 static GDBusProxy *__bt_gdbus_get_profile_proxy(void)
815 {
816         GDBusConnection *gconn;
817         GError *err = NULL;
818
819         if (profile_gproxy)
820                 return profile_gproxy;
821
822         gconn = __get_gdbus_connection();
823         if (gconn == NULL)
824                 return NULL;
825
826         profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
827                                                 NULL, BT_BLUEZ_NAME,
828                                                 "/org/bluez",
829                                                 "org.bluez.ProfileManager1",
830                                                 NULL, &err);
831         if (err) {
832                 BT_ERR("Unable to create proxy: %s", err->message);
833                 g_clear_error(&err);
834                 return NULL;
835         }
836
837         return profile_gproxy;
838 }
839
840 static GDBusProxy *__bt_gdbus_get_device_proxy(char *object_path)
841 {
842         GDBusConnection *gconn;
843         GError *err = NULL;
844         GDBusProxy *device_gproxy;
845
846         gconn = __get_gdbus_connection();
847         if (gconn == NULL)
848                 return NULL;
849
850         device_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
851                                                 NULL, BT_BLUEZ_NAME,
852                                                 object_path,
853                                                 BT_DEVICE_INTERFACE,
854                                                 NULL, &err);
855
856         if (err) {
857                 BT_ERR("Unable to create proxy: %s", err->message);
858                 g_clear_error(&err);
859                 return NULL;
860         }
861
862         return device_gproxy;
863 }
864
865 void _bt_unregister_gdbus(int object_id)
866 {
867         GDBusConnection *gconn;
868
869         gconn = __get_gdbus_connection();
870         if (gconn == NULL)
871                 return;
872
873         g_dbus_connection_unregister_object(gconn, object_id);
874 }
875
876 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
877 {
878         GDBusConnection *gconn;
879         int id;
880         GError *error = NULL;
881
882         gconn = __get_gdbus_connection();
883         if (gconn == NULL)
884                 return -1;
885
886         if (new_conn_node == NULL)
887                 new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
888
889         if (new_conn_node == NULL)
890                 return -1;
891
892         id = g_dbus_connection_register_object(gconn, path,
893                                                 new_conn_node->interfaces[0],
894                                                 &method_table,
895                                                 cb, NULL, &error);
896         if (id == 0) {
897                 BT_ERR("Failed to register: %s", error->message);
898                 g_error_free(error);
899                 return -1;
900         }
901
902         BT_DBG("NEW CONNECTION ID %d", id);
903
904         return id;
905 }
906
907 static GDBusProxy * __bt_gdbus_get_adapter_proxy()
908 {
909         GError *err = NULL;
910         GDBusProxy *manager_proxy = NULL;
911         GDBusProxy *adapter_proxy = NULL;
912         GDBusConnection *conn;
913         GVariant *result = NULL;
914         char *adapter_path = NULL;
915
916         conn = __get_gdbus_connection();
917         retv_if(conn == NULL, NULL);
918
919         manager_proxy =  g_dbus_proxy_new_sync(conn,
920                         G_DBUS_PROXY_FLAGS_NONE, NULL,
921                         BT_BLUEZ_NAME,
922                         BT_MANAGER_PATH,
923                         BT_MANAGER_INTERFACE,
924                         NULL, &err);
925
926         if (!manager_proxy) {
927                 BT_ERR("Unable to create proxy: %s", err->message);
928                 goto fail;
929         }
930
931         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
932                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
933         if (!result) {
934                 if (err != NULL)
935                         BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
936                 else
937                         BT_ERR("Fail to get DefaultAdapter");
938
939                 goto fail;
940         }
941
942         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
943                 BT_ERR("Incorrect result\n");
944                 goto fail;
945         }
946
947         g_variant_get(result, "(&o)", &adapter_path);
948
949         if (adapter_path == NULL ||
950                 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
951                 BT_ERR("Adapter path is inproper\n");
952                 goto fail;
953         }
954
955         BT_INFO("Adapter Path %s", adapter_path);
956
957         adapter_proxy = g_dbus_proxy_new_sync(conn,
958                                         G_DBUS_PROXY_FLAGS_NONE, NULL,
959                                         BT_BLUEZ_NAME,
960                                         adapter_path,
961                                         BT_ADAPTER_INTERFACE,
962                                         NULL, &err);
963         if (err) {
964                 BT_ERR("DBus Error message: [%s]", err->message);
965                 g_clear_error(&err);
966         }
967
968 fail:
969         if (manager_proxy)
970                 g_object_unref(manager_proxy);
971         if (result)
972                 g_variant_unref(result);
973         return adapter_proxy;
974 }
975
976 int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb)
977 {
978         GDBusConnection *gconn;
979         int id;
980         GError *error = NULL;
981
982         gconn = __get_gdbus_connection();
983         if (gconn == NULL)
984                 return -1;
985
986         if (new_conn_node == NULL)
987                 new_conn_node = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
988
989         if (new_conn_node == NULL)
990                 return -1;
991
992         id = g_dbus_connection_register_object(gconn, path,
993                                                 new_conn_node->interfaces[0],
994                                                 &method_table,
995                                                 cb, NULL, &error);
996         if (id == 0) {
997                 BT_ERR("Failed to register: %s", error->message);
998                 g_error_free(error);
999                 return -1;
1000         }
1001
1002         BT_DBG("NEW CONNECTION ID %d", id);
1003
1004         return id;
1005 }
1006
1007 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
1008 {
1009         GVariantBuilder *option_builder;
1010         GVariant *ret;
1011         GDBusProxy *proxy;
1012         GError *err = NULL;
1013         int result = BLUETOOTH_ERROR_NONE;
1014
1015         proxy = __bt_gdbus_get_profile_proxy();
1016         if (proxy == NULL) {
1017                 BT_ERR("Getting profile proxy failed");
1018                 return BLUETOOTH_ERROR_INTERNAL;
1019         }
1020
1021         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1022         if (info->authentication)
1023                 g_variant_builder_add(option_builder, "{sv}",
1024                                                 "RequireAuthentication",
1025                                                 g_variant_new_boolean(TRUE));
1026         if (info->authorization)
1027                 g_variant_builder_add(option_builder, "{sv}",
1028                                                 "RequireAuthorization",
1029                                                 g_variant_new_boolean(TRUE));
1030         if (info->role)
1031                 g_variant_builder_add(option_builder, "{sv}",
1032                                                 "Role",
1033                                                 g_variant_new_string(info->role));
1034
1035         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
1036          * RFCOMM channels based on the availability when two services want
1037          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
1038          * two services use the same SPP RFCOMM channel. */
1039         if (use_default_rfcomm)
1040                 g_variant_builder_add(option_builder, "{sv}",
1041                                                 "Channel",
1042                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1043         if (info->service)
1044                 g_variant_builder_add(option_builder, "{sv}",
1045                                                 "Service",
1046                                                 g_variant_new_string(info->service));
1047
1048         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
1049                                         g_variant_new("(osa{sv})", info->obj_path,
1050                                                                 info->uuid,
1051                                                                 option_builder),
1052                                         G_DBUS_CALL_FLAGS_NONE, -1,
1053                                         NULL, &err);
1054         if (err) {
1055                 g_dbus_error_strip_remote_error(err);
1056                 BT_ERR("RegisterProfile failed: %s", err->message);
1057
1058                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
1059                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
1060                 else
1061                         result = BLUETOOTH_ERROR_INTERNAL;
1062
1063                 g_clear_error(&err);
1064         }
1065
1066         g_variant_builder_unref(option_builder);
1067
1068         if (ret)
1069                 g_variant_unref(ret);
1070
1071         return result;
1072 }
1073
1074 int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path)
1075 {
1076         GVariantBuilder *option_builder;
1077         GVariant *ret;
1078         GDBusProxy *proxy;
1079         GError *err = NULL;
1080         int result = BLUETOOTH_ERROR_NONE;
1081
1082         proxy = __bt_gdbus_get_profile_proxy();
1083         if (proxy == NULL) {
1084                 BT_ERR("Getting profile proxy failed");
1085                 return BLUETOOTH_ERROR_INTERNAL;
1086         }
1087
1088         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1089         if (info->authentication)
1090                 g_variant_builder_add(option_builder, "{sv}",
1091                                                 "RequireAuthentication",
1092                                                 g_variant_new_boolean(TRUE));
1093         if (info->authorization)
1094                 g_variant_builder_add(option_builder, "{sv}",
1095                                                 "RequireAuthorization",
1096                                                 g_variant_new_boolean(TRUE));
1097         if (info->role)
1098                 g_variant_builder_add(option_builder, "{sv}",
1099                                                 "Role",
1100                                                 g_variant_new_string(info->role));
1101
1102         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
1103          * RFCOMM channels based on the availability when two services want
1104          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
1105          * two services use the same SPP RFCOMM channel. */
1106         if (use_default_rfcomm)
1107                 g_variant_builder_add(option_builder, "{sv}",
1108                                                 "Channel",
1109                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1110         if (info->service)
1111                 g_variant_builder_add(option_builder, "{sv}",
1112                                                 "Service",
1113                                                 g_variant_new_string(info->service));
1114
1115         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile2",
1116                                         g_variant_new("(osssa{sv})", info->obj_path,
1117                                                                 info->uuid,
1118                                                                 name,
1119                                                                 path,
1120                                                                 option_builder),
1121                                         G_DBUS_CALL_FLAGS_NONE, -1,
1122                                         NULL, &err);
1123         if (err) {
1124                 g_dbus_error_strip_remote_error(err);
1125                 BT_ERR("RegisterProfile failed: %s", err->message);
1126
1127                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
1128                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
1129                 else
1130                         result = BLUETOOTH_ERROR_INTERNAL;
1131
1132                 g_clear_error(&err);
1133         }
1134
1135         g_variant_builder_unref(option_builder);
1136
1137         if (ret)
1138                 g_variant_unref(ret);
1139
1140         return result;
1141 }
1142
1143 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
1144 {
1145         GVariantBuilder *option_builder;
1146         GVariant *ret;
1147         GDBusProxy *proxy;
1148         GError *err = NULL;
1149         int result = BLUETOOTH_ERROR_NONE;
1150
1151         proxy = __bt_gdbus_get_profile_proxy();
1152         if (proxy == NULL) {
1153                 BT_ERR("Getting profile proxy failed");
1154                 return BLUETOOTH_ERROR_INTERNAL;
1155         }
1156
1157         option_builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
1158         if (info->authentication)
1159                 g_variant_builder_add(option_builder, "{sv}",
1160                                                 "RequireAuthentication",
1161                                                 g_variant_new_boolean(TRUE));
1162         if (info->authorization)
1163                 g_variant_builder_add(option_builder, "{sv}",
1164                                                 "RequireAuthorization",
1165                                                 g_variant_new_boolean(TRUE));
1166         if (info->role)
1167                 g_variant_builder_add(option_builder, "{sv}",
1168                                                 "Role",
1169                                                 g_variant_new_string(info->role));
1170
1171         /* Setting RFCOMM channel to default value 0; would allow bluez to assign
1172          * RFCOMM channels based on the availability when two services want
1173          * to use the RFCOMM along with SPP. Hence bluez makes sure that no
1174          * two services use the same SPP RFCOMM channel. */
1175         if (use_default_rfcomm)
1176                 g_variant_builder_add(option_builder, "{sv}",
1177                                                 "Channel",
1178                                                 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1179         if (info->service)
1180                 g_variant_builder_add(option_builder, "{sv}",
1181                                                 "Service",
1182                                                 g_variant_new_string(info->service));
1183
1184         ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile1",
1185                                         g_variant_new("(osa{sv})", info->obj_path,
1186                                                                 info->uuid,
1187                                                                 option_builder),
1188                                         G_DBUS_CALL_FLAGS_NONE, -1,
1189                                         NULL, &err);
1190
1191         if (err) {
1192                 g_dbus_error_strip_remote_error(err);
1193                 BT_ERR("RegisterProfile failed: %s", err->message);
1194
1195                 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
1196                         result = BLUETOOTH_ERROR_ACCESS_DENIED;
1197                 else
1198                         result = BLUETOOTH_ERROR_INTERNAL;
1199
1200                 g_clear_error(&err);
1201         }
1202
1203         g_variant_builder_unref(option_builder);
1204
1205         if (ret)
1206                 g_variant_unref(ret);
1207
1208         return result;
1209 }
1210
1211
1212 void _bt_unregister_profile(char *path)
1213 {
1214         GVariant *ret;
1215         GDBusProxy *proxy;
1216         GError *err = NULL;
1217
1218         proxy = __bt_gdbus_get_profile_proxy();
1219         if (proxy == NULL) {
1220                 BT_ERR("Getting profile proxy failed");
1221                 return;
1222         }
1223
1224         ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
1225                         g_variant_new("(o)", path),
1226                         G_DBUS_CALL_FLAGS_NONE, -1,
1227                         NULL, &err);
1228         if (err) {
1229                 BT_ERR("UnregisterProfile failed : %s", err->message);
1230                 g_clear_error(&err);
1231         }
1232
1233         if (ret)
1234                 g_variant_unref(ret);
1235
1236         return;
1237 }
1238
1239 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
1240 {
1241         if (bus_id == 0) {
1242                 char *name = g_strdup_printf("org.bt.frwk%d", getpid());
1243
1244                 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1245                                                 name,
1246                                                 G_BUS_NAME_OWNER_FLAGS_NONE,
1247                                                 NULL,
1248                                                 NULL,
1249                                                 NULL,
1250                                                 NULL,
1251                                                 NULL);
1252                 BT_DBG("Got bus id %d", bus_id);
1253                 g_free(name);
1254         }
1255
1256         return g_dbus_node_info_new_for_xml(xml_data, NULL);
1257 }
1258
1259 GDBusNodeInfo * _bt_get_gdbus_node_ex(const gchar *xml_data, const char *bus_name)
1260 {
1261         if (bus_id == 0) {
1262                 char *name = g_strdup(bus_name);
1263                 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1264                                                 name,
1265                                                 G_BUS_NAME_OWNER_FLAGS_NONE,
1266                                                 NULL,
1267                                                 NULL,
1268                                                 NULL,
1269                                                 NULL,
1270                                                 NULL);
1271                 BT_DBG("Got bus id %d", bus_id);
1272                 g_free(name);
1273         }
1274
1275         return g_dbus_node_info_new_for_xml(xml_data, NULL);
1276 }
1277
1278 int _bt_connect_profile(char *address, char *uuid, void *cb,
1279                                                         gpointer func_data)
1280 {
1281         GDBusProxy *proxy;
1282         GDBusProxy *adapter_proxy;
1283         char *object_path;
1284         GError *err = NULL;
1285
1286         object_path = _bt_get_device_object_path(address);
1287
1288         if (object_path == NULL) {
1289                 GVariant *ret = NULL;
1290                 BT_ERR("No searched device");
1291                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1292
1293                 if (adapter_proxy == NULL) {
1294                         BT_ERR("adapter proxy is NULL");
1295                         return BLUETOOTH_ERROR_INTERNAL;
1296                 }
1297
1298                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1299                                 g_variant_new("(s)", address),
1300                                 G_DBUS_CALL_FLAGS_NONE,
1301                                 DBUS_TIMEOUT, NULL,
1302                                 &err);
1303
1304                 if (err != NULL) {
1305                         BT_ERR("CreateDevice Failed: %s", err->message);
1306                         g_clear_error(&err);
1307                 }
1308                 if (ret)
1309                         g_variant_unref(ret);
1310                 g_object_unref(adapter_proxy);
1311                 object_path = _bt_get_device_object_path(address);
1312
1313                 if (object_path == NULL)
1314                         return BLUETOOTH_ERROR_INTERNAL;
1315         }
1316
1317         proxy = __bt_gdbus_get_device_proxy(object_path);
1318         g_free(object_path);
1319
1320         if (proxy == NULL) {
1321                 BT_ERR("Error while getting proxy");
1322                 return BLUETOOTH_ERROR_INTERNAL;
1323         }
1324
1325         g_dbus_proxy_call(proxy, "ConnectProfile",
1326                         g_variant_new("(s)", uuid),
1327                         G_DBUS_CALL_FLAGS_NONE,
1328                         DBUS_TIMEOUT, NULL,
1329                         (GAsyncReadyCallback)cb,
1330                         func_data);
1331         BT_DBG("-");
1332         return BLUETOOTH_ERROR_NONE;
1333 }
1334
1335 int _bt_discover_services(char *address, char *uuid, void *cb,
1336                 gpointer func_data)
1337 {
1338         char *object_path;
1339         GDBusProxy *proxy;
1340         GDBusProxy *adapter_proxy;
1341         GError *err = NULL;
1342         object_path = _bt_get_device_object_path(address);
1343         if (object_path == NULL) {
1344                 GVariant *ret = NULL;
1345                 BT_ERR("No searched device");
1346                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1347                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1348                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1349                                 g_variant_new("(s)", address),
1350                                 G_DBUS_CALL_FLAGS_NONE,
1351                                 DBUS_TIMEOUT, NULL,
1352                                 &err);
1353                 if (err != NULL) {
1354                         BT_ERR("CreateDevice Failed: %s", err->message);
1355                         g_clear_error(&err);
1356                 }
1357                 if (ret)
1358                         g_variant_unref(ret);
1359                 g_object_unref(adapter_proxy);
1360                 object_path = _bt_get_device_object_path(address);
1361                 if (object_path == NULL)
1362                         return BLUETOOTH_ERROR_INTERNAL;
1363         }
1364         proxy = __bt_gdbus_get_device_proxy(object_path);
1365         g_free(object_path);
1366         if (proxy == NULL) {
1367                 BT_ERR("Error while getting proxy");
1368                 return BLUETOOTH_ERROR_INTERNAL;
1369         }
1370         g_dbus_proxy_call(proxy, "DiscoverServices",
1371                         g_variant_new("(s)", uuid),
1372                         G_DBUS_CALL_FLAGS_NONE,
1373                         DBUS_TIMEOUT, NULL,
1374                         (GAsyncReadyCallback)cb,
1375                         func_data);
1376         BT_DBG("-");
1377         return BLUETOOTH_ERROR_NONE;
1378 }
1379
1380 int _bt_cancel_discovers(char *address)
1381 {
1382         char *object_path;
1383         GDBusProxy *proxy;
1384         GDBusProxy *adapter_proxy;
1385         GVariant *ret = NULL;
1386         GError *err = NULL;
1387         object_path = _bt_get_device_object_path(address);
1388         if (object_path == NULL) {
1389                 GVariant *ret = NULL;
1390                 BT_ERR("No searched device");
1391                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1392                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1393                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1394                                 g_variant_new("(s)", address),
1395                                 G_DBUS_CALL_FLAGS_NONE,
1396                                 DBUS_TIMEOUT, NULL,
1397                                 &err);
1398                 if (err != NULL) {
1399                         BT_ERR("CreateDevice Failed: %s", err->message);
1400                         g_clear_error(&err);
1401                 }
1402                 if (ret)
1403                         g_variant_unref(ret);
1404                 g_object_unref(adapter_proxy);
1405                 object_path = _bt_get_device_object_path(address);
1406                 if (object_path == NULL)
1407                         return BLUETOOTH_ERROR_INTERNAL;
1408         }
1409         proxy = __bt_gdbus_get_device_proxy(object_path);
1410         g_free(object_path);
1411         ret = g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
1412                 NULL,
1413                 G_DBUS_CALL_FLAGS_NONE,
1414                 DBUS_TIMEOUT, NULL,
1415                 &err);
1416         if (err) {
1417                 BT_ERR("DBus Error message: [%s]", err->message);
1418                 g_clear_error(&err);
1419                 return BLUETOOTH_ERROR_INTERNAL;
1420         }
1421         if (ret)
1422                 g_variant_unref(ret);
1423         if (proxy)
1424                 g_object_unref(proxy);
1425         return BLUETOOTH_ERROR_NONE;
1426 }
1427
1428 int _bt_discover_service_uuids(char *address, char *remote_uuid)
1429 {
1430         char *object_path;
1431         GDBusProxy *proxy;
1432         GDBusConnection *gconn;
1433         GError *err = NULL;
1434         char **uuid_value = NULL;
1435         gsize size = 0;
1436         int i = 0;
1437         GVariant *value = NULL;
1438         GVariant *ret = NULL;
1439         int result = BLUETOOTH_ERROR_INTERNAL;
1440         BT_INFO("+");
1441         retv_if(remote_uuid == NULL, BLUETOOTH_ERROR_INTERNAL);
1442         gconn = __get_gdbus_connection();
1443         retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1444         object_path = _bt_get_device_object_path(address);
1445         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1446
1447         proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1448                                 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1449                                 &err);
1450         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1451         if (err) {
1452                 BT_ERR("DBus Error: [%s]", err->message);
1453                 g_clear_error(&err);
1454         }
1455         ret = g_dbus_proxy_call_sync(proxy, "GetAll",
1456                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1457                         G_DBUS_CALL_FLAGS_NONE,
1458                         DBUS_TIMEOUT, NULL,
1459                         &err);
1460         if (err) {
1461                 result = BLUETOOTH_ERROR_INTERNAL;
1462                 BT_ERR("DBus Error : %s", err->message);
1463                 g_clear_error(&err);
1464                 goto done;
1465         }
1466         if (ret == NULL) {
1467                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1468                 result = BLUETOOTH_ERROR_INTERNAL;
1469                 goto done;
1470         }
1471
1472         g_variant_get(ret, "(@a{sv})", &value);
1473         g_variant_unref(ret);
1474         if (value) {
1475                 GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
1476                         G_VARIANT_TYPE_STRING_ARRAY);
1477
1478                 if (temp_value) {
1479                         size = g_variant_get_size(temp_value);
1480                         if (size > 0) {
1481                                 uuid_value = (char **)g_variant_get_strv(temp_value, &size);
1482                                 BT_DBG("Size items %d", size);
1483
1484                                 if (uuid_value) {
1485                                         for (i = 0; uuid_value[i] != NULL; i++) {
1486                                                 BT_DBG("Remote uuids %s", uuid_value[i]);
1487                                                 if (strcasecmp(uuid_value[i], remote_uuid) == 0) {
1488                                                         result = BLUETOOTH_ERROR_NONE;
1489                                                         g_variant_unref(temp_value);
1490                                                         goto done;
1491                                                 }
1492                                         }
1493                                 }
1494                         }
1495                         g_variant_unref(temp_value);
1496                 }
1497         }
1498 done:
1499         if (proxy)
1500                 g_object_unref(proxy);
1501         if (value)
1502                 g_variant_unref(value);
1503         if (uuid_value)
1504                 g_free(uuid_value);
1505
1506         BT_DBG("-");
1507         return result;
1508 }
1509
1510 int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
1511 {
1512         char *object_path;
1513         GDBusProxy *proxy;
1514         GDBusConnection *gconn;
1515         GError *err = NULL;
1516         GVariant *value = NULL;
1517         GVariant *result = NULL;
1518         unsigned int  class = 0x00;
1519         int ret = BLUETOOTH_ERROR_INTERNAL;
1520
1521         gconn = __get_gdbus_connection();
1522         retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1523                 object_path = _bt_get_device_object_path(address);
1524
1525         retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1526
1527         proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1528                                 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1529                                 &err);
1530         retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1531         if (err) {
1532                 BT_ERR("DBus Error: [%s]", err->message);
1533                 g_clear_error(&err);
1534         }
1535
1536         result = g_dbus_proxy_call_sync(proxy, "GetAll",
1537                         g_variant_new("(s)", BT_DEVICE_INTERFACE),
1538                         G_DBUS_CALL_FLAGS_NONE,
1539                         DBUS_TIMEOUT, NULL,
1540                         &err);
1541         if (err) {
1542                 ret = BLUETOOTH_ERROR_INTERNAL;
1543                 BT_ERR("DBus Error : %s", err->message);
1544                 g_clear_error(&err);
1545                 goto done;
1546         }
1547         if (result == NULL) {
1548                 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1549                 ret = BLUETOOTH_ERROR_INTERNAL;
1550                 goto done;
1551         }
1552         g_variant_get(result, "(@a{sv})", &value);
1553         g_variant_unref(result);
1554         if (value) {
1555                 GVariant *temp_value = g_variant_lookup_value(value, "Class",
1556                         G_VARIANT_TYPE_UINT32);
1557                 class = g_variant_get_uint32(temp_value);
1558                 _bt_divide_device_class(dev_class, class);
1559                 if (temp_value)
1560                         g_variant_unref(temp_value);
1561         }
1562
1563 done:
1564         if (proxy)
1565                 g_object_unref(proxy);
1566         if (value)
1567                 g_variant_unref(value);
1568
1569         BT_DBG("-");
1570         return ret;
1571 }
1572
1573 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
1574                                                         gpointer func_data)
1575 {
1576         GDBusProxy *proxy;
1577         char *object_path;
1578         GError *err = NULL;
1579         GDBusProxy *adapter_proxy;
1580         object_path = _bt_get_device_object_path(address);
1581         if (object_path == NULL) {
1582                 GVariant *ret = NULL;
1583                 BT_ERR("No searched device");
1584                 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1585                 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1586                 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1587                                 g_variant_new("(s)", address),
1588                                 G_DBUS_CALL_FLAGS_NONE,
1589                                 DBUS_TIMEOUT, NULL,
1590                                 &err);
1591                 if (err != NULL) {
1592                         BT_ERR("CreateDevice Failed: %s", err->message);
1593                         g_error_free(err);
1594                 }
1595                 if (ret)
1596                         g_variant_unref(ret);
1597                 g_object_unref(adapter_proxy);
1598                 object_path = _bt_get_device_object_path(address);
1599                 if (object_path == NULL)
1600                         return BLUETOOTH_ERROR_INTERNAL;
1601         }
1602         proxy = __bt_gdbus_get_device_proxy(object_path);
1603         g_free(object_path);
1604         if (proxy == NULL) {
1605                 BT_ERR("Error while getting proxy");
1606                 return BLUETOOTH_ERROR_INTERNAL;
1607         }
1608         g_dbus_proxy_call(proxy, "DisconnectProfile",
1609                         g_variant_new("(s)", uuid),
1610                         G_DBUS_CALL_FLAGS_NONE,
1611                         DBUS_TIMEOUT, NULL,
1612                         (GAsyncReadyCallback)cb,
1613                         func_data);
1614         BT_DBG("-");
1615         return BLUETOOTH_ERROR_NONE;
1616 }
1617
1618 int _bt_get_adapter_path(GDBusConnection *conn, char *path)
1619 {
1620         GError *err = NULL;
1621         GDBusProxy *manager_proxy = NULL;
1622         GVariant *result = NULL;
1623         char *adapter_path = NULL;
1624
1625         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1626
1627         manager_proxy =  g_dbus_proxy_new_sync(conn,
1628                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1629                         BT_BLUEZ_NAME,
1630                         BT_MANAGER_PATH,
1631                         BT_MANAGER_INTERFACE,
1632                         NULL, &err);
1633
1634         if (!manager_proxy) {
1635                 BT_ERR("Unable to create proxy: %s", err->message);
1636                 goto fail;
1637         }
1638
1639         result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
1640                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1641         if (!result) {
1642                 if (err != NULL) {
1643                         if (!g_strrstr(err->message, "ServiceUnknown"))
1644                                 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
1645                 } else {
1646                         BT_ERR("Fail to get DefaultAdapter");
1647                 }
1648
1649                 goto fail;
1650         }
1651
1652         if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
1653                 BT_ERR("Incorrect result\n");
1654                 goto fail;
1655         }
1656
1657         g_variant_get(result, "(&o)", &adapter_path);
1658
1659         if (adapter_path == NULL ||
1660                 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
1661                 BT_ERR("Adapter path is inproper\n");
1662                 goto fail;
1663         }
1664
1665         if (path)
1666                 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
1667
1668         g_variant_unref(result);
1669         g_object_unref(manager_proxy);
1670
1671         return BLUETOOTH_ERROR_NONE;
1672
1673 fail:
1674         g_clear_error(&err);
1675
1676         if (result)
1677                 g_variant_unref(result);
1678
1679         if (manager_proxy)
1680                 g_object_unref(manager_proxy);
1681
1682         return BLUETOOTH_ERROR_INTERNAL;
1683
1684 }
1685
1686 void _bt_convert_device_path_to_address(const char *device_path,
1687                                                 char *device_address)
1688 {
1689         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1690         char *dev_addr;
1691
1692         ret_if(device_path == NULL);
1693         ret_if(device_address == NULL);
1694
1695         dev_addr = strstr(device_path, "dev_");
1696         if (dev_addr != NULL) {
1697                 char *pos = NULL;
1698                 dev_addr += 4;
1699                 g_strlcpy(address, dev_addr, sizeof(address));
1700
1701                 while ((pos = strchr(address, '_')) != NULL)
1702                         *pos = ':';
1703
1704                 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1705         }
1706 }
1707
1708 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
1709 {
1710         char *object_path = NULL;
1711         char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1712         /* Parse the signature:  oa{sa{sv}}} */
1713         while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
1714                         NULL)) {
1715                 retv_if(object_path == NULL, NULL);
1716                 _bt_convert_device_path_to_address(object_path, device_address);
1717
1718                 if (g_strcmp0(address, device_address) == 0)
1719                         return g_strdup(object_path);
1720         }
1721         return NULL;
1722 }
1723
1724 char *_bt_get_device_object_path(char *address)
1725 {
1726         GError *err = NULL;
1727         GDBusProxy *proxy = NULL;
1728         GVariant *result = NULL;
1729         GVariantIter *iter = NULL;
1730         GDBusConnection *conn = NULL;
1731         char *object_path = NULL;
1732
1733         conn = _bt_gdbus_get_system_gconn();
1734         retv_if(conn == NULL, NULL);
1735
1736         proxy =  g_dbus_proxy_new_sync(conn,
1737                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1738                         BT_BLUEZ_NAME,
1739                         BT_MANAGER_PATH,
1740                         BT_MANAGER_INTERFACE,
1741                         NULL, &err);
1742
1743         if (!proxy) {
1744                 BT_ERR("Unable to create proxy: %s", err->message);
1745                 goto fail;
1746         }
1747
1748         result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
1749                         G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1750         if (!result) {
1751                 if (err != NULL)
1752                         BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
1753                 else
1754                         BT_ERR("Fail to get GetManagedObjects");
1755
1756                 goto fail;
1757         }
1758
1759         g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
1760         object_path = __bt_extract_device_path(iter, address);
1761
1762         g_variant_unref(result);
1763         g_object_unref(proxy);
1764         g_variant_iter_free(iter);
1765         return object_path;
1766
1767 fail:
1768         g_clear_error(&err);
1769
1770         if (proxy)
1771                 g_object_unref(proxy);
1772
1773         return object_path;
1774 }
1775
1776 GDBusConnection *_bt_init_system_gdbus_conn(void)
1777 {
1778         GError *error = NULL;
1779         if (system_gdbus_conn == NULL) {
1780                 system_gdbus_conn =
1781                 g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1782                 if (error) {
1783                         BT_ERR("GDBus connection Error : %s \n",
1784                                 error->message);
1785                         g_clear_error(&error);
1786                         return NULL;
1787                 }
1788         }
1789         return system_gdbus_conn;
1790 }
1791
1792
1793 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
1794 {
1795         int ret;
1796         char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1797         char path_str[BLUETOOTH_PATH_STRING] = { 0, };
1798
1799         BT_DBG("+");
1800         BT_INIT_PARAMS();
1801         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1802
1803         g_array_append_vals(in_param1, &type, sizeof(int));
1804         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1805         g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1806         g_strlcpy(path_str, path, sizeof(path_str));
1807         g_array_append_vals(in_param3, &path_str, BLUETOOTH_PATH_STRING);
1808         g_array_append_vals(in_param4, &fd, sizeof(int));
1809
1810         ret =  _bt_send_request(BT_AGENT_SERVICE, BT_SET_AUTHORIZATION,
1811                 in_param1, in_param2, in_param3, in_param4, &out_param);
1812
1813         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1814         BT_DBG("-");
1815         return ret;
1816 }
1817
1818 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
1819 {
1820         int ret;
1821         char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1822
1823         BT_DBG("+");
1824         BT_INIT_PARAMS();
1825         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1826
1827         g_array_append_vals(in_param1, &type, sizeof(int));
1828         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
1829         g_array_append_vals(in_param2, &uuid_str, BLUETOOTH_UUID_STRING_MAX);
1830
1831         ret =  _bt_send_request(BT_AGENT_SERVICE, BT_UNSET_AUTHORIZATION,
1832                 in_param1, in_param2, in_param3, in_param4, &out_param);
1833
1834         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1835         BT_DBG("-");
1836         return ret;
1837 }
1838
1839 int _bt_check_privilege(int service_type, int service_function)
1840 {
1841         int result;
1842
1843         BT_CHECK_ENABLED(return);
1844
1845         BT_INIT_PARAMS();
1846         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1847
1848         result = _bt_sync_send_request(service_type, service_function,
1849                 in_param1, in_param2, in_param3, in_param4, &out_param);
1850
1851         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1852
1853         return result;
1854 }
1855
1856 GVariant *_bt_get_managed_objects(void)
1857 {
1858         GDBusConnection *g_conn;
1859         GDBusProxy *manager_proxy = NULL;
1860         GVariant *result = NULL;
1861         GError *error = NULL;
1862
1863         BT_DBG("+");
1864
1865         g_conn = _bt_gdbus_get_system_gconn();
1866         retv_if(g_conn == NULL, NULL);
1867
1868         manager_proxy = g_dbus_proxy_new_sync(g_conn,
1869                         G_DBUS_PROXY_FLAGS_NONE, NULL,
1870                         BT_BLUEZ_NAME,
1871                         BT_MANAGER_PATH,
1872                         BT_MANAGER_INTERFACE,
1873                         NULL, &error);
1874
1875         if (error) {
1876                 BT_ERR("Unable to create proxy: %s", error->message);
1877                 g_clear_error(&error);
1878                 return NULL;
1879         }
1880
1881         result = g_dbus_proxy_call_sync(manager_proxy,
1882                         "GetManagedObjects", NULL,
1883                         G_DBUS_CALL_FLAGS_NONE, -1,
1884                         NULL, &error);
1885
1886         if (error) {
1887                 BT_ERR("Fail to get ManagedObjects (Error: %s)", error->message);
1888                 g_clear_error(&error);
1889         }
1890
1891         g_object_unref(manager_proxy);
1892
1893         BT_DBG("-");
1894         return result;
1895 }
1896
1897 gboolean _bt_check_enabled_internal(void)
1898 {
1899         if (bt_enabled == TRUE)
1900                 return TRUE;
1901
1902         if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_ENABLED)
1903                 bt_enabled = TRUE;
1904
1905         return bt_enabled;
1906 }
1907
1908 void _bt_set_adapter_internal_status(gboolean enabled)
1909 {
1910         bt_enabled = enabled;
1911 }
1912
1913 int _bt_get_uuid_specification_name(const char *uuid, char **name)
1914 {
1915         int i;
1916         int offset = 0;
1917         int uuid_len = 4;
1918         static struct {
1919                 const char *uuid;
1920                 const char *specification_name;
1921         } bt_uuid_name[] = {
1922                 /* GATT Services */
1923                 {"1800", "Generic Access"},
1924                 {"1801", "Generic Attribute"},
1925                 {"1802", "Immediate Alert"},
1926                 {"1803", "Link Loss"},
1927                 {"1804", "Tx Power"},
1928                 {"1805", "Current Time Service"},
1929                 {"1806", "Reference Time Update Service"},
1930                 {"1807", "Next DST Change Service"},
1931                 {"1808", "Glucose"},
1932                 {"1809", "Health Thermometer"},
1933                 {"180A", "Device Information"},
1934                 {"180D", "Heart Rate"},
1935                 {"180F", "Battery Service"},
1936                 {"1810", "Blood Pressure"},
1937                 {"1811", "Alert Notification Service"},
1938                 {"1812", "Human Interface Device"},
1939
1940                 /* GATT Declarations */
1941                 {"2800", "Primary Service Declaration"},
1942                 {"2801", "Secondary Service Declaration"},
1943                 {"2802", "Include Declaration"},
1944                 {"2803", "Characteristic Declaration"},
1945
1946                 /* GATT Descriptors */
1947                 {"2900", "Characteristic Extended Properties"},
1948                 {"2901", "Characteristic User Description"},
1949                 {"2902", "Client Characteristic Configuration"},
1950                 {"2903", "Server Characteristic Configuration"},
1951                 {"2904", "Characteristic Format"},
1952                 {"2905", "Characteristic Aggregate Formate"},
1953                 {"2906", "Valid Range"},
1954                 {"2907", "External Report Reference"},
1955                 {"2908", "Report Reference"},
1956
1957                 /* GATT Characteristics */
1958                 {"2A00", "Device Name"},
1959                 {"2A01", "Appearance"},
1960                 {"2A02", "Peripheral Privacy Flag"},
1961                 {"2A03", "Reconnection Address"},
1962                 {"2A04", "Peripheral Preferred Connection Parameters"},
1963                 {"2A05", "Service Changed"},
1964                 {"2A06", "Alert Level"},
1965                 {"2A07", "Tx Power Level"},
1966                 {"2A08", "Date Time"},
1967                 {"2A09", "Day of Week"},
1968                 {"2A0A", "Day Date Time"},
1969                 {"2A19", "Battery Level"},
1970                 {"2A1E", "Intermediate Temperature"},
1971                 {"2A23", "System ID"},
1972                 {"2A24", "Model Number String"},
1973                 {"2A25", "Serial Number String"},
1974                 {"2A26", "Firmware Revision String"},
1975                 {"2A27", "Hardware Revision String"},
1976                 {"2A28", "Software Revision String"},
1977                 {"2A29", "Manufacturer Name String"},
1978                 {"2A2A", "IEEE 11073-20601 Regulatory Certification Data List"},
1979                 {"2A2B", "Current Time"},
1980                 {"2A37", "Heart Rate Measurement"},
1981                 {"2A38", "Body Sensor Location"},
1982                 {"2A3F", "Alert Status"},
1983                 {"2A46", "New Alert"},
1984                 {"2A4A", "HID Information"},
1985                 {"2A4C", "HID Control Point"},
1986                 {"2A50", "PnP ID"},
1987
1988                 /* Custom uuids */
1989                 {"7905F431-B5CE-4E99-A40F-4B1E122D00D0", "Apple Notification Center Service"},
1990                 {"9FBF120D-6301-42D9-8C58-25E699A21DBD", "Notifications Source"},
1991                 {"69D1D8F3-45E1-49A8-9821-9BBDFDAAD9D9", "Control Point"},
1992                 {"22EAC6E9-24D6-4BB5-BE44-B36ACE7C7BFB", "Data Source"},
1993                 {"89D3502B-0F36-433A-8EF4-C502AD55F8DC", "Apple Media Service"},
1994                 {"9B3C81D8-57B1-4A8A-B8DF-0E56F7CA51C2", "Remote Command"},
1995                 {"2F7CABCE-808D-411F-9A0C-BB92BA96C102", "Entity Update"},
1996                 {"C6B2F38C-23AB-46D8-A6AB-A3A870BBD5D7", "Entity Attribute"},
1997                 {"9A3F68E0-86CE-11E5-A309-0002A5D5C51B", "Samsung Gear Manager Service"},
1998                 {"c2f2cc0f-c085-4dd4-be5a-aca3074bbc72", "Control Point"},
1999                 {"cece518b-28d7-4171-92d5-76a1e249a3b9", "Notifications Source"},
2000                 {NULL, NULL}
2001         };
2002
2003         if (!uuid || !name)
2004                 return BLUETOOTH_ERROR_INVALID_PARAM;
2005         if (strlen(uuid) == 36) {
2006                 if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
2007                         offset = 4;
2008                 else {
2009                         offset = 0;
2010                         uuid_len = 36;
2011                 }
2012         } else if (strlen(uuid) >= 8)
2013                 offset = 4;
2014
2015         for (i = 0; bt_uuid_name[i].uuid; i++) {
2016                 if (!g_ascii_strncasecmp(uuid + offset, bt_uuid_name[i].uuid, uuid_len)) {
2017                         *name = g_strdup(bt_uuid_name[i].specification_name);
2018                         return BLUETOOTH_ERROR_NONE;
2019                 }
2020         }
2021
2022         *name = g_strdup("Unknown");
2023         return BLUETOOTH_ERROR_NONE;
2024 }
2025
2026 BT_EXPORT_API int bluetooth_is_supported(void)
2027 {
2028         int is_supported = 0;
2029         int len = 0;
2030         int fd = -1;
2031         rfkill_event event;
2032
2033         fd = open(RFKILL_NODE, O_RDONLY);
2034         if (fd < 0) {
2035                 BT_ERR("Fail to open RFKILL node");
2036                 return BLUETOOTH_ERROR_INTERNAL;
2037         }
2038
2039         if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
2040                 BT_ERR("Fail to set RFKILL node to non-blocking");
2041                 close(fd);
2042                 return BLUETOOTH_ERROR_INTERNAL;
2043         }
2044
2045         while (1) {
2046                 len = read(fd, &event, sizeof(event));
2047                 if (len < 0) {
2048                         BT_ERR("Fail to read events");
2049                         break;
2050                 }
2051
2052                 if (len != RFKILL_EVENT_SIZE) {
2053                         BT_ERR("The size is wrong\n");
2054                         continue;
2055                 }
2056
2057                 if (event.type == RFKILL_TYPE_BLUETOOTH) {
2058                         is_supported = 1;
2059                         break;
2060                 }
2061         }
2062
2063         close(fd);
2064
2065         BT_DBG("supported: %d", is_supported);
2066
2067         return is_supported;
2068 }
2069
2070 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
2071 {
2072         int ret;
2073
2074         _bt_gdbus_init_system_gconn();
2075
2076         ret = _bt_init_event_handler();
2077         if (ret != BLUETOOTH_ERROR_NONE &&
2078              ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
2079                 BT_ERR("Fail to init the event handler");
2080                 return ret;
2081         }
2082
2083
2084         _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
2085
2086         /* Register All events */
2087         if (ret != BLUETOOTH_ERROR_ALREADY_INITIALIZED) {
2088                 ret = _bt_register_event(BT_ADAPTER_EVENT, (void *)callback_ptr, user_data);
2089                 if (ret != BLUETOOTH_ERROR_NONE)
2090                         goto fail;
2091                 ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
2092                 if (ret != BLUETOOTH_ERROR_NONE)
2093                         goto fail;
2094                 ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
2095                 if (ret != BLUETOOTH_ERROR_NONE)
2096                         goto fail;
2097                 ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
2098                 if (ret != BLUETOOTH_ERROR_NONE)
2099                         goto fail;
2100                 ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
2101                 if (ret != BLUETOOTH_ERROR_NONE)
2102                         goto fail;
2103                 ret = _bt_register_event(BT_GATT_BLUEZ_EVENT, (void *)callback_ptr, user_data);
2104                 if (ret != BLUETOOTH_ERROR_NONE)
2105                         goto fail;
2106                 ret = _bt_register_event(BT_TDS_EVENT, (void *)callback_ptr, user_data);
2107                 if (ret != BLUETOOTH_ERROR_NONE)
2108                         goto fail;
2109         }
2110
2111         _bt_register_name_owner_changed();
2112
2113         return BLUETOOTH_ERROR_NONE;
2114 fail:
2115         BT_ERR("Fail to do _bt_register_event()");
2116         bluetooth_unregister_callback();
2117         return ret;
2118 }
2119
2120 BT_EXPORT_API int bluetooth_unregister_callback(void)
2121 {
2122         int ret;
2123
2124
2125         ret = _bt_deinit_event_handler();
2126         if (ret != BLUETOOTH_ERROR_NONE)
2127                 BT_ERR("Fail to deinit the event handler");
2128
2129         _bt_unregister_name_owner_changed();
2130
2131         _bt_set_user_data(BT_COMMON, NULL, NULL);
2132
2133         _bt_set_obex_server_id(BT_NO_SERVER);
2134
2135         _bt_gdbus_deinit_proxys();
2136
2137         if (system_gconn) {
2138                 g_object_unref(system_gconn);
2139                 system_gconn = NULL;
2140         }
2141
2142         return BLUETOOTH_ERROR_NONE;
2143 }
2144