2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
21 #include <sys/types.h>
23 #include <sys/socket.h>
26 #include <gio/gunixfdlist.h>
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"
36 #include "bt-common.h"
37 #include "bt-request-sender.h"
38 #include "bt-event-handler.h"
40 #ifdef TIZEN_FEATURE_BT_DPM
45 static bt_user_info_t user_info[BT_MAX_USER_INFO];
46 static GDBusConnection *system_gdbus_conn = NULL;
51 static GDBusConnection *system_gconn = NULL;
53 static gboolean bt_enabled = FALSE;
55 #define DBUS_TIMEOUT 20 * 1000 /* 20 Seconds */
57 GDBusConnection *g_bus_get_private_conn(void)
61 GDBusConnection *private_gconn = NULL;
63 address = g_dbus_address_get_for_bus_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
64 if (address == NULL) {
66 BT_ERR("Failed to get bus address: %s", error->message);
67 g_clear_error(&error);
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 */
80 BT_ERR("Unable to connect to dbus: %s", error->message);
81 g_clear_error(&error);
89 GDBusConnection *_bt_gdbus_init_system_gconn(void)
91 if (system_gconn != NULL)
94 system_gconn = g_bus_get_private_conn();
99 GDBusConnection *_bt_gdbus_get_system_gconn(void)
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();
109 void _bt_print_device_address_t(const bluetooth_device_address_t *addr)
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]);
115 void _bt_set_user_data(int type, void *callback, void *user_data)
117 user_info[type].cb = callback;
118 user_info[type].user_data = user_data;
121 bt_user_info_t *_bt_get_user_data(int type)
123 return &user_info[type];
126 void _bt_common_event_cb(int event, int result, void *param,
127 void *callback, void *user_data)
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;
136 ((bluetooth_cb_func_ptr)callback)(bt_event.event, &bt_event,
140 void _bt_input_event_cb(int event, int result, void *param,
141 void *callback, void *user_data)
143 hid_event_param_t bt_event = { 0, };
144 bt_event.event = event;
145 bt_event.result = result;
146 bt_event.param_data = param;
149 ((hid_cb_func_ptr)callback)(bt_event.event, &bt_event,
153 void _bt_headset_event_cb(int event, int result, void *param,
154 void *callback, void *user_data)
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;
162 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
166 void _bt_a2dp_source_event_cb(int event, int result, void *param,
167 void *callback, void *user_data)
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;
174 ((bt_audio_func_ptr)callback)(bt_event.event, &bt_event,
178 void _bt_hf_event_cb(int event, int result, void *param,
179 void *callback, void *user_data)
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;
187 ((bt_hf_func_ptr)callback)(bt_event.event, &bt_event,
192 void _bt_avrcp_event_cb(int event, int result, void *param,
193 void *callback, void *user_data)
195 media_event_param_t bt_event = { 0, };
196 bt_event.event = event;
197 bt_event.result = result;
198 bt_event.param_data = param;
201 ((media_cb_func_ptr)callback)(bt_event.event, &bt_event,
205 void _bt_divide_device_class(bluetooth_device_class_t *device_class,
208 ret_if(device_class == NULL);
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));
214 if (cod & 0x002000) {
215 device_class->service_class |=
216 BLUETOOTH_DEVICE_SERVICE_CLASS_LIMITED_DISCOVERABLE_MODE;
220 void _bt_convert_addr_string_to_type(unsigned char *addr,
226 ret_if(address == NULL);
227 ret_if(addr == NULL);
229 for (i = 0; i < BT_ADDRESS_LENGTH_MAX; i++) {
230 addr[i] = strtol(address, &ptr, 16);
231 if (ptr[0] != '\0') {
240 void _bt_convert_addr_string_to_secure_string(char *addr,
245 ret_if(address == NULL);
246 ret_if(addr == NULL);
248 len = strlen(address);
249 ret_if(len != BT_ADDRESS_STRING_SIZE - 1);
251 strncpy(addr, address, len);
259 void _bt_convert_addr_type_to_string(char *address,
262 ret_if(address == NULL);
263 ret_if(addr == NULL);
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]);
271 const char *_bt_convert_error_to_string(int error)
274 case BLUETOOTH_ERROR_CANCEL:
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:
285 case BLUETOOTH_ERROR_NO_RESOURCES:
286 return "NO_RESOURCES";
287 case BLUETOOTH_ERROR_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:
301 case BLUETOOTH_ERROR_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:
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:
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";
354 const char *_bt_convert_service_function_to_string(int function)
360 const char *function_name;
361 } bt_function_name_t;
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"},
607 for (i = 0; bt_functions[i].function != -1; i++) {
608 if (bt_functions[i].function == function)
609 return bt_functions[i].function_name;
615 int _bt_copy_utf8_string(char *dest, const char *src, unsigned int length)
622 if (dest == NULL || src == NULL)
623 return BLUETOOTH_ERROR_INVALID_PARAM;
626 while (*p != '\0' && i < length) {
627 next = g_utf8_next_char(p);
630 while (count > 0 && ((i + count) < length)) {
637 return BLUETOOTH_ERROR_NONE;
640 gboolean _bt_utf8_validate(char *name)
644 glong items_written = 0;
646 if (FALSE == g_utf8_validate(name, -1, NULL))
649 u16 = g_utf8_to_utf16(name, -1, NULL, &items_written, NULL);
655 if (items_written != g_utf8_strlen(name, -1))
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;
670 static const gchar rfcomm_agent_xml[] =
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'/>"
678 " <method name='RequestDisconnection'>"
679 " <arg type='o' name='device' direction='in'/>"
684 static void __new_connection_method(GDBusConnection *connection,
686 const gchar *object_path,
687 const gchar *interface_name,
688 const gchar *method_name,
689 GVariant *parameters,
690 GDBusMethodInvocation *invocation,
693 BT_DBG("method %s", method_name);
694 if (g_strcmp0(method_name, "NewConnection") == 0) {
697 GUnixFDList *fd_list;
698 GVariantBuilder *properties;
701 bluetooth_device_address_t remote_addr1;
702 bt_new_connection_cb cb = user_data;
703 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
706 g_variant_get(parameters, "(oha{sv})", &obj_path, &index,
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);
721 fd = g_unix_fd_list_get(fd_list, index, NULL);
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);
731 _bt_convert_device_path_to_address(obj_path, addr);
732 _bt_convert_addr_string_to_type(remote_addr1.addr, (const char *)addr);
734 _bt_convert_addr_string_to_secure_string(secure_address, addr);
735 BT_INFO("fd: %d, address %s", fd, secure_address);
737 g_dbus_method_invocation_return_value(invocation, NULL);
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);
747 static const GDBusInterfaceVTable method_table = {
748 __new_connection_method,
753 void _bt_swap_addr(unsigned char *dst, const unsigned char *src)
757 for (i = 0; i < 6; i++)
761 int __rfcomm_assign_id(void)
765 BT_DBG("latest_id: %d", latest_id);
767 index = latest_id + 1;
769 if (index >= BT_RFCOMM_ID_MAX)
772 BT_DBG("index: %d", index);
774 while (id_used[index] == TRUE) {
775 if (index == latest_id) {
776 /* No available ID */
777 BT_ERR("All request ID is used");
783 if (index >= BT_RFCOMM_ID_MAX)
788 id_used[index] = TRUE;
790 BT_DBG("Assigned Id: %d", latest_id);
795 void __rfcomm_delete_id(int id)
797 ret_if(id >= BT_RFCOMM_ID_MAX);
802 /* Next server will use this ID */
806 static GDBusConnection *__get_gdbus_connection()
809 gconn = g_bus_get_private_conn();
814 static GDBusProxy *__bt_gdbus_get_profile_proxy(void)
816 GDBusConnection *gconn;
820 return profile_gproxy;
822 gconn = __get_gdbus_connection();
826 profile_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
829 "org.bluez.ProfileManager1",
832 BT_ERR("Unable to create proxy: %s", err->message);
837 return profile_gproxy;
840 static GDBusProxy *__bt_gdbus_get_device_proxy(char *object_path)
842 GDBusConnection *gconn;
844 GDBusProxy *device_gproxy;
846 gconn = __get_gdbus_connection();
850 device_gproxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE,
857 BT_ERR("Unable to create proxy: %s", err->message);
862 return device_gproxy;
865 void _bt_unregister_gdbus(int object_id)
867 GDBusConnection *gconn;
869 gconn = __get_gdbus_connection();
873 g_dbus_connection_unregister_object(gconn, object_id);
876 int _bt_register_new_conn(const char *path, bt_new_connection_cb cb)
878 GDBusConnection *gconn;
880 GError *error = NULL;
882 gconn = __get_gdbus_connection();
886 if (new_conn_node == NULL)
887 new_conn_node = _bt_get_gdbus_node(rfcomm_agent_xml);
889 if (new_conn_node == NULL)
892 id = g_dbus_connection_register_object(gconn, path,
893 new_conn_node->interfaces[0],
897 BT_ERR("Failed to register: %s", error->message);
902 BT_DBG("NEW CONNECTION ID %d", id);
907 static GDBusProxy * __bt_gdbus_get_adapter_proxy()
910 GDBusProxy *manager_proxy = NULL;
911 GDBusProxy *adapter_proxy = NULL;
912 GDBusConnection *conn;
913 GVariant *result = NULL;
914 char *adapter_path = NULL;
916 conn = __get_gdbus_connection();
917 retv_if(conn == NULL, NULL);
919 manager_proxy = g_dbus_proxy_new_sync(conn,
920 G_DBUS_PROXY_FLAGS_NONE, NULL,
923 BT_MANAGER_INTERFACE,
926 if (!manager_proxy) {
927 BT_ERR("Unable to create proxy: %s", err->message);
931 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
932 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
935 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
937 BT_ERR("Fail to get DefaultAdapter");
942 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
943 BT_ERR("Incorrect result\n");
947 g_variant_get(result, "(&o)", &adapter_path);
949 if (adapter_path == NULL ||
950 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
951 BT_ERR("Adapter path is inproper\n");
955 BT_INFO("Adapter Path %s", adapter_path);
957 adapter_proxy = g_dbus_proxy_new_sync(conn,
958 G_DBUS_PROXY_FLAGS_NONE, NULL,
961 BT_ADAPTER_INTERFACE,
964 BT_ERR("DBus Error message: [%s]", err->message);
970 g_object_unref(manager_proxy);
972 g_variant_unref(result);
973 return adapter_proxy;
976 int _bt_register_new_conn_ex(const char *path, const char *bus_name, bt_new_connection_cb cb)
978 GDBusConnection *gconn;
980 GError *error = NULL;
982 gconn = __get_gdbus_connection();
986 if (new_conn_node == NULL)
987 new_conn_node = _bt_get_gdbus_node_ex(rfcomm_agent_xml, bus_name);
989 if (new_conn_node == NULL)
992 id = g_dbus_connection_register_object(gconn, path,
993 new_conn_node->interfaces[0],
997 BT_ERR("Failed to register: %s", error->message);
1002 BT_DBG("NEW CONNECTION ID %d", id);
1007 int _bt_register_profile(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
1009 GVariantBuilder *option_builder;
1013 int result = BLUETOOTH_ERROR_NONE;
1015 proxy = __bt_gdbus_get_profile_proxy();
1016 if (proxy == NULL) {
1017 BT_ERR("Getting profile proxy failed");
1018 return BLUETOOTH_ERROR_INTERNAL;
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));
1031 g_variant_builder_add(option_builder, "{sv}",
1033 g_variant_new_string(info->role));
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}",
1042 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1044 g_variant_builder_add(option_builder, "{sv}",
1046 g_variant_new_string(info->service));
1048 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile",
1049 g_variant_new("(osa{sv})", info->obj_path,
1052 G_DBUS_CALL_FLAGS_NONE, -1,
1055 g_dbus_error_strip_remote_error(err);
1056 BT_ERR("RegisterProfile failed: %s", err->message);
1058 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
1059 result = BLUETOOTH_ERROR_ACCESS_DENIED;
1061 result = BLUETOOTH_ERROR_INTERNAL;
1063 g_clear_error(&err);
1066 g_variant_builder_unref(option_builder);
1069 g_variant_unref(ret);
1074 int _bt_register_profile_ex(bt_register_profile_info_t *info, gboolean use_default_rfcomm, const char *name, const char *path)
1076 GVariantBuilder *option_builder;
1080 int result = BLUETOOTH_ERROR_NONE;
1082 proxy = __bt_gdbus_get_profile_proxy();
1083 if (proxy == NULL) {
1084 BT_ERR("Getting profile proxy failed");
1085 return BLUETOOTH_ERROR_INTERNAL;
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));
1098 g_variant_builder_add(option_builder, "{sv}",
1100 g_variant_new_string(info->role));
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}",
1109 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1111 g_variant_builder_add(option_builder, "{sv}",
1113 g_variant_new_string(info->service));
1115 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile2",
1116 g_variant_new("(osssa{sv})", info->obj_path,
1121 G_DBUS_CALL_FLAGS_NONE, -1,
1124 g_dbus_error_strip_remote_error(err);
1125 BT_ERR("RegisterProfile failed: %s", err->message);
1127 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
1128 result = BLUETOOTH_ERROR_ACCESS_DENIED;
1130 result = BLUETOOTH_ERROR_INTERNAL;
1132 g_clear_error(&err);
1135 g_variant_builder_unref(option_builder);
1138 g_variant_unref(ret);
1143 int _bt_register_profile_platform(bt_register_profile_info_t *info, gboolean use_default_rfcomm)
1145 GVariantBuilder *option_builder;
1149 int result = BLUETOOTH_ERROR_NONE;
1151 proxy = __bt_gdbus_get_profile_proxy();
1152 if (proxy == NULL) {
1153 BT_ERR("Getting profile proxy failed");
1154 return BLUETOOTH_ERROR_INTERNAL;
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));
1167 g_variant_builder_add(option_builder, "{sv}",
1169 g_variant_new_string(info->role));
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}",
1178 g_variant_new_uint16(RFCOMM_DEFAULT_PROFILE_CHANNEL));
1180 g_variant_builder_add(option_builder, "{sv}",
1182 g_variant_new_string(info->service));
1184 ret = g_dbus_proxy_call_sync(proxy, "RegisterProfile1",
1185 g_variant_new("(osa{sv})", info->obj_path,
1188 G_DBUS_CALL_FLAGS_NONE, -1,
1192 g_dbus_error_strip_remote_error(err);
1193 BT_ERR("RegisterProfile failed: %s", err->message);
1195 if (g_strrstr(err->message, BT_ACCESS_DENIED_MSG))
1196 result = BLUETOOTH_ERROR_ACCESS_DENIED;
1198 result = BLUETOOTH_ERROR_INTERNAL;
1200 g_clear_error(&err);
1203 g_variant_builder_unref(option_builder);
1206 g_variant_unref(ret);
1212 void _bt_unregister_profile(char *path)
1218 proxy = __bt_gdbus_get_profile_proxy();
1219 if (proxy == NULL) {
1220 BT_ERR("Getting profile proxy failed");
1224 ret = g_dbus_proxy_call_sync(proxy, "UnregisterProfile",
1225 g_variant_new("(o)", path),
1226 G_DBUS_CALL_FLAGS_NONE, -1,
1229 BT_ERR("UnregisterProfile failed : %s", err->message);
1230 g_clear_error(&err);
1234 g_variant_unref(ret);
1239 GDBusNodeInfo * _bt_get_gdbus_node(const gchar *xml_data)
1242 char *name = g_strdup_printf("org.bt.frwk%d", getpid());
1244 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1246 G_BUS_NAME_OWNER_FLAGS_NONE,
1252 BT_DBG("Got bus id %d", bus_id);
1256 return g_dbus_node_info_new_for_xml(xml_data, NULL);
1259 GDBusNodeInfo * _bt_get_gdbus_node_ex(const gchar *xml_data, const char *bus_name)
1262 char *name = g_strdup(bus_name);
1263 bus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
1265 G_BUS_NAME_OWNER_FLAGS_NONE,
1271 BT_DBG("Got bus id %d", bus_id);
1275 return g_dbus_node_info_new_for_xml(xml_data, NULL);
1278 int _bt_connect_profile(char *address, char *uuid, void *cb,
1282 GDBusProxy *adapter_proxy;
1286 object_path = _bt_get_device_object_path(address);
1288 if (object_path == NULL) {
1289 GVariant *ret = NULL;
1290 BT_ERR("No searched device");
1291 adapter_proxy = __bt_gdbus_get_adapter_proxy();
1293 if (adapter_proxy == NULL) {
1294 BT_ERR("adapter proxy is NULL");
1295 return BLUETOOTH_ERROR_INTERNAL;
1298 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1299 g_variant_new("(s)", address),
1300 G_DBUS_CALL_FLAGS_NONE,
1305 BT_ERR("CreateDevice Failed: %s", err->message);
1306 g_clear_error(&err);
1309 g_variant_unref(ret);
1310 g_object_unref(adapter_proxy);
1311 object_path = _bt_get_device_object_path(address);
1313 if (object_path == NULL)
1314 return BLUETOOTH_ERROR_INTERNAL;
1317 proxy = __bt_gdbus_get_device_proxy(object_path);
1318 g_free(object_path);
1320 if (proxy == NULL) {
1321 BT_ERR("Error while getting proxy");
1322 return BLUETOOTH_ERROR_INTERNAL;
1325 g_dbus_proxy_call(proxy, "ConnectProfile",
1326 g_variant_new("(s)", uuid),
1327 G_DBUS_CALL_FLAGS_NONE,
1329 (GAsyncReadyCallback)cb,
1332 return BLUETOOTH_ERROR_NONE;
1335 int _bt_discover_services(char *address, char *uuid, void *cb,
1340 GDBusProxy *adapter_proxy;
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,
1354 BT_ERR("CreateDevice Failed: %s", err->message);
1355 g_clear_error(&err);
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;
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;
1370 g_dbus_proxy_call(proxy, "DiscoverServices",
1371 g_variant_new("(s)", uuid),
1372 G_DBUS_CALL_FLAGS_NONE,
1374 (GAsyncReadyCallback)cb,
1377 return BLUETOOTH_ERROR_NONE;
1380 int _bt_cancel_discovers(char *address)
1384 GDBusProxy *adapter_proxy;
1385 GVariant *ret = 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,
1399 BT_ERR("CreateDevice Failed: %s", err->message);
1400 g_clear_error(&err);
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;
1409 proxy = __bt_gdbus_get_device_proxy(object_path);
1410 g_free(object_path);
1411 ret = g_dbus_proxy_call_sync(proxy, "CancelDiscovery",
1413 G_DBUS_CALL_FLAGS_NONE,
1417 BT_ERR("DBus Error message: [%s]", err->message);
1418 g_clear_error(&err);
1419 return BLUETOOTH_ERROR_INTERNAL;
1422 g_variant_unref(ret);
1424 g_object_unref(proxy);
1425 return BLUETOOTH_ERROR_NONE;
1428 int _bt_discover_service_uuids(char *address, char *remote_uuid)
1432 GDBusConnection *gconn;
1434 char **uuid_value = NULL;
1437 GVariant *value = NULL;
1438 GVariant *ret = NULL;
1439 int result = BLUETOOTH_ERROR_INTERNAL;
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);
1447 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1448 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1450 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1452 BT_ERR("DBus Error: [%s]", err->message);
1453 g_clear_error(&err);
1455 ret = g_dbus_proxy_call_sync(proxy, "GetAll",
1456 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1457 G_DBUS_CALL_FLAGS_NONE,
1461 result = BLUETOOTH_ERROR_INTERNAL;
1462 BT_ERR("DBus Error : %s", err->message);
1463 g_clear_error(&err);
1467 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1468 result = BLUETOOTH_ERROR_INTERNAL;
1472 g_variant_get(ret, "(@a{sv})", &value);
1473 g_variant_unref(ret);
1475 GVariant *temp_value = g_variant_lookup_value(value, "UUIDs",
1476 G_VARIANT_TYPE_STRING_ARRAY);
1479 size = g_variant_get_size(temp_value);
1481 uuid_value = (char **)g_variant_get_strv(temp_value, &size);
1482 BT_DBG("Size items %d", size);
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);
1495 g_variant_unref(temp_value);
1500 g_object_unref(proxy);
1502 g_variant_unref(value);
1510 int _bt_get_cod_by_address(char *address, bluetooth_device_class_t *dev_class)
1514 GDBusConnection *gconn;
1516 GVariant *value = NULL;
1517 GVariant *result = NULL;
1518 unsigned int class = 0x00;
1519 int ret = BLUETOOTH_ERROR_INTERNAL;
1521 gconn = __get_gdbus_connection();
1522 retv_if(gconn == NULL, BLUETOOTH_ERROR_INTERNAL);
1523 object_path = _bt_get_device_object_path(address);
1525 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1527 proxy = g_dbus_proxy_new_sync(gconn, G_DBUS_PROXY_FLAGS_NONE, NULL,
1528 BT_BLUEZ_NAME, object_path, BT_PROPERTIES_INTERFACE, NULL,
1530 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1532 BT_ERR("DBus Error: [%s]", err->message);
1533 g_clear_error(&err);
1536 result = g_dbus_proxy_call_sync(proxy, "GetAll",
1537 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1538 G_DBUS_CALL_FLAGS_NONE,
1542 ret = BLUETOOTH_ERROR_INTERNAL;
1543 BT_ERR("DBus Error : %s", err->message);
1544 g_clear_error(&err);
1547 if (result == NULL) {
1548 BT_ERR("g_dbus_proxy_call_sync function return NULL");
1549 ret = BLUETOOTH_ERROR_INTERNAL;
1552 g_variant_get(result, "(@a{sv})", &value);
1553 g_variant_unref(result);
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);
1560 g_variant_unref(temp_value);
1565 g_object_unref(proxy);
1567 g_variant_unref(value);
1573 int _bt_disconnect_profile(char *address, char *uuid, void *cb,
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,
1592 BT_ERR("CreateDevice Failed: %s", err->message);
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;
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;
1608 g_dbus_proxy_call(proxy, "DisconnectProfile",
1609 g_variant_new("(s)", uuid),
1610 G_DBUS_CALL_FLAGS_NONE,
1612 (GAsyncReadyCallback)cb,
1615 return BLUETOOTH_ERROR_NONE;
1618 int _bt_get_adapter_path(GDBusConnection *conn, char *path)
1621 GDBusProxy *manager_proxy = NULL;
1622 GVariant *result = NULL;
1623 char *adapter_path = NULL;
1625 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1627 manager_proxy = g_dbus_proxy_new_sync(conn,
1628 G_DBUS_PROXY_FLAGS_NONE, NULL,
1631 BT_MANAGER_INTERFACE,
1634 if (!manager_proxy) {
1635 BT_ERR("Unable to create proxy: %s", err->message);
1639 result = g_dbus_proxy_call_sync(manager_proxy, "DefaultAdapter", NULL,
1640 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1643 if (!g_strrstr(err->message, "ServiceUnknown"))
1644 BT_ERR("Fail to get DefaultAdapter (Error: %s)", err->message);
1646 BT_ERR("Fail to get DefaultAdapter");
1652 if (g_strcmp0(g_variant_get_type_string(result), "(o)")) {
1653 BT_ERR("Incorrect result\n");
1657 g_variant_get(result, "(&o)", &adapter_path);
1659 if (adapter_path == NULL ||
1660 strlen(adapter_path) >= BT_ADAPTER_OBJECT_PATH_MAX) {
1661 BT_ERR("Adapter path is inproper\n");
1666 g_strlcpy(path, adapter_path, BT_ADAPTER_OBJECT_PATH_MAX);
1668 g_variant_unref(result);
1669 g_object_unref(manager_proxy);
1671 return BLUETOOTH_ERROR_NONE;
1674 g_clear_error(&err);
1677 g_variant_unref(result);
1680 g_object_unref(manager_proxy);
1682 return BLUETOOTH_ERROR_INTERNAL;
1686 void _bt_convert_device_path_to_address(const char *device_path,
1687 char *device_address)
1689 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1692 ret_if(device_path == NULL);
1693 ret_if(device_address == NULL);
1695 dev_addr = strstr(device_path, "dev_");
1696 if (dev_addr != NULL) {
1699 g_strlcpy(address, dev_addr, sizeof(address));
1701 while ((pos = strchr(address, '_')) != NULL)
1704 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
1708 static char *__bt_extract_device_path(GVariantIter *iter, char *address)
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,
1715 retv_if(object_path == NULL, NULL);
1716 _bt_convert_device_path_to_address(object_path, device_address);
1718 if (g_strcmp0(address, device_address) == 0)
1719 return g_strdup(object_path);
1724 char *_bt_get_device_object_path(char *address)
1727 GDBusProxy *proxy = NULL;
1728 GVariant *result = NULL;
1729 GVariantIter *iter = NULL;
1730 GDBusConnection *conn = NULL;
1731 char *object_path = NULL;
1733 conn = _bt_gdbus_get_system_gconn();
1734 retv_if(conn == NULL, NULL);
1736 proxy = g_dbus_proxy_new_sync(conn,
1737 G_DBUS_PROXY_FLAGS_NONE, NULL,
1740 BT_MANAGER_INTERFACE,
1744 BT_ERR("Unable to create proxy: %s", err->message);
1748 result = g_dbus_proxy_call_sync(proxy, "GetManagedObjects", NULL,
1749 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
1752 BT_ERR("Fail to get GetManagedObjects (Error: %s)", err->message);
1754 BT_ERR("Fail to get GetManagedObjects");
1759 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
1760 object_path = __bt_extract_device_path(iter, address);
1762 g_variant_unref(result);
1763 g_object_unref(proxy);
1764 g_variant_iter_free(iter);
1768 g_clear_error(&err);
1771 g_object_unref(proxy);
1776 GDBusConnection *_bt_init_system_gdbus_conn(void)
1778 GError *error = NULL;
1779 if (system_gdbus_conn == NULL) {
1781 g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
1783 BT_ERR("GDBus connection Error : %s \n",
1785 g_clear_error(&error);
1789 return system_gdbus_conn;
1793 int _bt_register_osp_server_in_agent(int type, char *uuid, char *path, int fd)
1796 char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1797 char path_str[BLUETOOTH_PATH_STRING] = { 0, };
1801 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
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));
1810 ret = _bt_send_request(BT_AGENT_SERVICE, BT_SET_AUTHORIZATION,
1811 in_param1, in_param2, in_param3, in_param4, &out_param);
1813 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1818 int _bt_unregister_osp_server_in_agent(int type, char *uuid)
1821 char uuid_str[BLUETOOTH_UUID_STRING_MAX] = { 0, };
1825 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
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);
1831 ret = _bt_send_request(BT_AGENT_SERVICE, BT_UNSET_AUTHORIZATION,
1832 in_param1, in_param2, in_param3, in_param4, &out_param);
1834 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1839 int _bt_check_privilege(int service_type, int service_function)
1843 BT_CHECK_ENABLED(return);
1846 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1848 result = _bt_sync_send_request(service_type, service_function,
1849 in_param1, in_param2, in_param3, in_param4, &out_param);
1851 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
1856 GVariant *_bt_get_managed_objects(void)
1858 GDBusConnection *g_conn;
1859 GDBusProxy *manager_proxy = NULL;
1860 GVariant *result = NULL;
1861 GError *error = NULL;
1865 g_conn = _bt_gdbus_get_system_gconn();
1866 retv_if(g_conn == NULL, NULL);
1868 manager_proxy = g_dbus_proxy_new_sync(g_conn,
1869 G_DBUS_PROXY_FLAGS_NONE, NULL,
1872 BT_MANAGER_INTERFACE,
1876 BT_ERR("Unable to create proxy: %s", error->message);
1877 g_clear_error(&error);
1881 result = g_dbus_proxy_call_sync(manager_proxy,
1882 "GetManagedObjects", NULL,
1883 G_DBUS_CALL_FLAGS_NONE, -1,
1887 BT_ERR("Fail to get ManagedObjects (Error: %s)", error->message);
1888 g_clear_error(&error);
1891 g_object_unref(manager_proxy);
1897 gboolean _bt_check_enabled_internal(void)
1899 if (bt_enabled == TRUE)
1902 if (bluetooth_check_adapter() == BLUETOOTH_ADAPTER_ENABLED)
1908 void _bt_set_adapter_internal_status(gboolean enabled)
1910 bt_enabled = enabled;
1913 int _bt_get_uuid_specification_name(const char *uuid, char **name)
1920 const char *specification_name;
1921 } bt_uuid_name[] = {
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"},
1940 /* GATT Declarations */
1941 {"2800", "Primary Service Declaration"},
1942 {"2801", "Secondary Service Declaration"},
1943 {"2802", "Include Declaration"},
1944 {"2803", "Characteristic Declaration"},
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"},
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"},
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"},
2004 return BLUETOOTH_ERROR_INVALID_PARAM;
2005 if (strlen(uuid) == 36) {
2006 if (!g_ascii_strncasecmp(uuid + 9, "0000-1000-8000-00805F9B34FB", 27))
2012 } else if (strlen(uuid) >= 8)
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;
2022 *name = g_strdup("Unknown");
2023 return BLUETOOTH_ERROR_NONE;
2026 BT_EXPORT_API int bluetooth_is_supported(void)
2028 int is_supported = 0;
2033 fd = open(RFKILL_NODE, O_RDONLY);
2035 BT_ERR("Fail to open RFKILL node");
2036 return BLUETOOTH_ERROR_INTERNAL;
2039 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) {
2040 BT_ERR("Fail to set RFKILL node to non-blocking");
2042 return BLUETOOTH_ERROR_INTERNAL;
2046 len = read(fd, &event, sizeof(event));
2048 BT_ERR("Fail to read events");
2052 if (len != RFKILL_EVENT_SIZE) {
2053 BT_ERR("The size is wrong\n");
2057 if (event.type == RFKILL_TYPE_BLUETOOTH) {
2065 BT_DBG("supported: %d", is_supported);
2067 return is_supported;
2070 BT_EXPORT_API int bluetooth_register_callback(bluetooth_cb_func_ptr callback_ptr, void *user_data)
2074 _bt_gdbus_init_system_gconn();
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");
2084 _bt_set_user_data(BT_COMMON, (void *)callback_ptr, user_data);
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)
2091 ret = _bt_register_event(BT_DEVICE_EVENT, (void *)callback_ptr, user_data);
2092 if (ret != BLUETOOTH_ERROR_NONE)
2094 ret = _bt_register_event(BT_NETWORK_EVENT, (void *)callback_ptr, user_data);
2095 if (ret != BLUETOOTH_ERROR_NONE)
2097 ret = _bt_register_event(BT_RFCOMM_CLIENT_EVENT, (void *)callback_ptr, user_data);
2098 if (ret != BLUETOOTH_ERROR_NONE)
2100 ret = _bt_register_event(BT_RFCOMM_SERVER_EVENT, (void *)callback_ptr, user_data);
2101 if (ret != BLUETOOTH_ERROR_NONE)
2103 ret = _bt_register_event(BT_GATT_BLUEZ_EVENT, (void *)callback_ptr, user_data);
2104 if (ret != BLUETOOTH_ERROR_NONE)
2106 ret = _bt_register_event(BT_TDS_EVENT, (void *)callback_ptr, user_data);
2107 if (ret != BLUETOOTH_ERROR_NONE)
2111 _bt_register_name_owner_changed();
2113 return BLUETOOTH_ERROR_NONE;
2115 BT_ERR("Fail to do _bt_register_event()");
2116 bluetooth_unregister_callback();
2120 BT_EXPORT_API int bluetooth_unregister_callback(void)
2125 ret = _bt_deinit_event_handler();
2126 if (ret != BLUETOOTH_ERROR_NONE)
2127 BT_ERR("Fail to deinit the event handler");
2129 _bt_unregister_name_owner_changed();
2131 _bt_set_user_data(BT_COMMON, NULL, NULL);
2133 _bt_set_obex_server_id(BT_NO_SERVER);
2135 _bt_gdbus_deinit_proxys();
2138 g_object_unref(system_gconn);
2139 system_gconn = NULL;
2142 return BLUETOOTH_ERROR_NONE;