4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
25 #include <dbus/dbus-glib.h>
26 #include <dbus/dbus.h>
32 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
33 #include <syspopup_caller.h>
38 #include <notification.h>
39 #ifdef ENABLE_TIZEN_2_4
40 #include <journal/device.h>
45 #include "bluetooth-api.h"
46 #include "bt-internal-types.h"
48 #include "bt-service-common.h"
49 #include "bt-service-event.h"
50 #include "bt-service-adapter.h"
51 #include "bt-service-util.h"
52 #include "bt-service-network.h"
53 #include "bt-service-obex-server.h"
54 #include "bt-service-agent.h"
55 #include "bt-service-main.h"
56 #include "bt-service-avrcp.h"
66 bt_adapter_timer_t visible_timer = {0, };
68 static gboolean is_discovering;
69 static gboolean is_le_discovering;
70 static bt_le_discovery_type_t le_discovery_type = BT_LE_PASSIVE_SCAN;
71 static gboolean cancel_by_user;
72 static bt_status_t adapter_status = BT_DEACTIVATED;
73 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
74 static void *adapter_agent = NULL;
75 static DBusGProxy *core_proxy = NULL;
76 static guint timer_id = 0;
77 static guint le_timer_id = 0;
79 #define BT_CORE_NAME "org.projectx.bt_core"
80 #define BT_CORE_PATH "/org/projectx/bt_core"
81 #define BT_CORE_INTERFACE "org.projectx.btcore"
83 #define BT_DISABLE_TIME 500 /* 500 ms */
85 DBusGProxy *_bt_init_core_proxy(void)
88 DBusGConnection *conn;
90 conn = _bt_get_system_gconn();
94 proxy = dbus_g_proxy_new_for_name(conn, BT_CORE_NAME,
95 BT_CORE_PATH, BT_CORE_INTERFACE);
104 static DBusGProxy *__bt_get_core_proxy(void)
106 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
109 static gboolean __bt_is_factory_test_mode(void)
113 #ifdef ENABLE_TIZEN_2_4
114 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
115 BT_ERR("Get the DUT Mode fail");
121 BT_INFO("DUT Test Mode !!");
128 static gboolean __bt_timeout_handler(gpointer user_data)
130 int result = BLUETOOTH_ERROR_NONE;
134 /* Take current time */
136 time_diff = difftime(current_time, visible_timer.start_time);
138 /* Send event to application */
139 _bt_send_event(BT_ADAPTER_EVENT,
140 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
141 DBUS_TYPE_INT32, &result,
142 DBUS_TYPE_INT16, &time_diff,
145 if (visible_timer.timeout <= time_diff) {
146 g_source_remove(visible_timer.event_id);
147 visible_timer.event_id = 0;
148 visible_timer.timeout = 0;
150 #ifndef TIZEN_WEARABLE
151 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
152 BT_ERR("Set vconf failed\n");
160 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
162 BT_DBG("__bt_visibility_alarm_cb - alram id = [%d] \n", alarm_id);
164 int result = BLUETOOTH_ERROR_NONE;
167 if (alarm_id != visible_timer.alarm_id)
170 if (visible_timer.event_id) {
171 _bt_send_event(BT_ADAPTER_EVENT,
172 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
173 DBUS_TYPE_INT32, &result,
174 DBUS_TYPE_INT16, &timeout,
176 g_source_remove(visible_timer.event_id);
177 visible_timer.event_id = 0;
178 visible_timer.timeout = 0;
180 #ifndef TIZEN_WEARABLE
181 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
182 BT_ERR("Set vconf failed\n");
185 /* Switch Off visibility in Bluez */
186 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
187 visible_timer.alarm_id = 0;
191 static void __bt_visibility_alarm_create()
196 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
199 BT_ERR("Failed to create alarm error = %d\n", result);
201 BT_DBG("Alarm created = %d\n", alarm_id);
202 visible_timer.alarm_id = alarm_id;
206 static void __bt_visibility_alarm_remove()
208 if (visible_timer.event_id > 0) {
209 g_source_remove(visible_timer.event_id);
210 visible_timer.event_id = 0;
213 if (visible_timer.alarm_id > 0) {
214 alarmmgr_remove_alarm(visible_timer.alarm_id);
215 visible_timer.alarm_id = 0;
219 int __bt_set_visible_time(int timeout)
223 __bt_visibility_alarm_remove();
225 visible_timer.timeout = timeout;
227 #ifndef TIZEN_WEARABLE
228 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
229 BT_ERR("Set vconf failed");
232 return BLUETOOTH_ERROR_NONE;
234 if (!visible_timer.alarm_init) {
235 /* Set Alarm timer to switch off BT */
236 result = alarmmgr_init("bt-service");
238 return BLUETOOTH_ERROR_INTERNAL;
240 visible_timer.alarm_init = TRUE;
243 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
245 return BLUETOOTH_ERROR_INTERNAL;
247 /* Take start time */
248 time(&(visible_timer.start_time));
249 visible_timer.event_id = g_timeout_add_seconds(1,
250 __bt_timeout_handler, NULL);
252 __bt_visibility_alarm_create();
254 return BLUETOOTH_ERROR_NONE;
257 static void __bt_get_service_list(GValue *value, bluetooth_device_info_t *dev)
263 ret_if(value == NULL);
266 uuids = g_value_get_boxed(value);
267 ret_if(uuids == NULL);
269 dev->service_index = 0;
271 for (i = 0; uuids[i] != NULL; i++) {
272 g_strlcpy(dev->uuids[i], uuids[i], BLUETOOTH_UUID_STRING_MAX);
274 parts = g_strsplit(uuids[i], "-", -1);
276 if (parts == NULL || parts[0] == NULL)
279 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
282 dev->service_index++;
286 static bt_remote_dev_info_t *__bt_parse_remote_device_info(
287 DBusMessageIter *item_iter)
289 DBusMessageIter value_iter;
290 bt_remote_dev_info_t *dev_info;
292 dbus_message_iter_recurse(item_iter, &value_iter);
294 if (dbus_message_iter_get_arg_type(&value_iter) !=
295 DBUS_TYPE_DICT_ENTRY) {
300 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
302 while (dbus_message_iter_get_arg_type(&value_iter) ==
303 DBUS_TYPE_DICT_ENTRY) {
306 DBusMessageIter dict_entry;
307 DBusMessageIter iter_dict_val;
309 dbus_message_iter_recurse(&value_iter, &dict_entry);
311 dbus_message_iter_get_basic(&dict_entry, &key);
314 dbus_message_iter_next(&value_iter);
318 if (!dbus_message_iter_next(&dict_entry)) {
319 dbus_message_iter_next(&value_iter);
322 dbus_message_iter_recurse(&dict_entry, &iter_dict_val);
324 if (strcasecmp(key, "Address") == 0) {
325 const char *address = NULL;
326 dbus_message_iter_get_basic(&iter_dict_val,
328 dev_info->address = g_strdup(address);
329 } else if (strcasecmp(key, "Class") == 0) {
330 dbus_message_iter_get_basic(&iter_dict_val,
332 } else if (strcasecmp(key, "Name") == 0) {
333 dbus_message_iter_get_basic(&iter_dict_val,
335 if (dev_info->name == NULL)
336 dev_info->name = g_strdup(value);
337 } else if (strcasecmp(key, "Connected") == 0) {
338 dbus_message_iter_get_basic(&iter_dict_val,
339 &dev_info->connected);
340 } else if (strcasecmp(key, "Paired") == 0) {
341 dbus_message_iter_get_basic(&iter_dict_val,
343 } else if (strcasecmp(key, "Trusted") == 0) {
344 dbus_message_iter_get_basic(&iter_dict_val,
346 } else if (strcasecmp(key, "RSSI") == 0) {
347 dbus_message_iter_get_basic(&iter_dict_val,
349 } else if (strcasecmp(key, "UUIDs") == 0) {
350 DBusMessageIter uuid_iter;
351 DBusMessageIter tmp_iter;
354 dbus_message_iter_recurse(&iter_dict_val,
356 tmp_iter = uuid_iter;
358 /* Store the uuid count */
359 while (dbus_message_iter_get_arg_type(&tmp_iter) !=
362 dbus_message_iter_get_basic(&tmp_iter,
365 dev_info->uuid_count++;
366 if (!dbus_message_iter_next(&tmp_iter))
370 /* Store the uuids */
371 if (dev_info->uuid_count > 0) {
372 dev_info->uuids = g_new0(char *,
373 dev_info->uuid_count + 1);
375 dbus_message_iter_next(&value_iter);
379 while (dbus_message_iter_get_arg_type(&uuid_iter) !=
381 dbus_message_iter_get_basic(&uuid_iter,
383 dev_info->uuids[i] = g_strdup(value);
385 if (!dbus_message_iter_next(&uuid_iter)) {
391 dbus_message_iter_next(&value_iter);
397 static void __bt_extract_remote_devinfo(DBusMessageIter *msg_iter,
400 bt_remote_dev_info_t *dev_info = NULL;
401 char *object_path = NULL;
402 DBusMessageIter value_iter;
404 /* Parse the signature: oa{sa{sv}}} */
405 ret_if(dbus_message_iter_get_arg_type(msg_iter) !=
406 DBUS_TYPE_OBJECT_PATH);
408 dbus_message_iter_get_basic(msg_iter, &object_path);
409 ret_if(object_path == NULL);
411 /* object array (oa) */
412 ret_if(dbus_message_iter_next(msg_iter) == FALSE);
413 ret_if(dbus_message_iter_get_arg_type(msg_iter) != DBUS_TYPE_ARRAY);
415 dbus_message_iter_recurse(msg_iter, &value_iter);
417 /* string array (sa) */
418 while (dbus_message_iter_get_arg_type(&value_iter) ==
419 DBUS_TYPE_DICT_ENTRY) {
420 char *interface_name = NULL;
421 DBusMessageIter interface_iter;
423 dbus_message_iter_recurse(&value_iter, &interface_iter);
425 ret_if(dbus_message_iter_get_arg_type(&interface_iter) !=
428 dbus_message_iter_get_basic(&interface_iter, &interface_name);
430 ret_if(dbus_message_iter_next(&interface_iter) == FALSE);
432 ret_if(dbus_message_iter_get_arg_type(&interface_iter) !=
435 if (g_strcmp0(interface_name, "org.bluez.Device1") == 0) {
436 BT_DBG("Found a device: %s", object_path);
437 dev_info = __bt_parse_remote_device_info(
441 g_array_append_vals(*dev_list, dev_info,
442 sizeof(bt_remote_dev_info_t));
446 dbus_message_iter_next(&value_iter);
450 int _bt_get_remote_found_devices(GArray **dev_list)
454 DBusMessageIter reply_iter;
455 DBusMessageIter value_iter;
457 DBusConnection *conn;
459 conn = _bt_get_system_conn();
460 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
462 msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
463 BT_MANAGER_INTERFACE,
464 "GetManagedObjects");
466 retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
468 /* Synchronous call */
469 dbus_error_init(&err);
470 reply = dbus_connection_send_with_reply_and_block(
473 dbus_message_unref(msg);
476 BT_ERR("Can't get managed objects");
478 if (dbus_error_is_set(&err)) {
479 BT_ERR("%s", err.message);
480 dbus_error_free(&err);
482 return BLUETOOTH_ERROR_INTERNAL;
485 if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
486 BT_ERR("Fail to iterate the reply");
487 return BLUETOOTH_ERROR_INTERNAL;
490 dbus_message_iter_recurse(&reply_iter, &value_iter);
492 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
493 while (dbus_message_iter_get_arg_type(&value_iter) ==
494 DBUS_TYPE_DICT_ENTRY) {
495 DBusMessageIter msg_iter;
497 dbus_message_iter_recurse(&value_iter, &msg_iter);
499 __bt_extract_remote_devinfo(&msg_iter, dev_list);
501 dbus_message_iter_next(&value_iter);
504 return BLUETOOTH_ERROR_NONE;
507 static int __bt_get_bonded_device_info(gchar *device_path,
508 bluetooth_device_info_t *dev_info)
510 GValue *value = { 0 };
512 DBusGProxy *device_proxy;
513 const gchar *address;
520 GByteArray *manufacturer_data = NULL;
521 GHashTable *hash = NULL;
523 DBusGConnection *conn;
525 BT_CHECK_PARAMETER(device_path, return);
526 BT_CHECK_PARAMETER(dev_info, return);
528 conn = _bt_get_system_gconn();
529 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
531 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
532 device_path, BT_PROPERTIES_INTERFACE);
534 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
536 dbus_g_proxy_call(device_proxy, "GetAll", &err,
537 G_TYPE_STRING, BT_DEVICE_INTERFACE,
539 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
540 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
542 g_object_unref(device_proxy);
545 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
547 return BLUETOOTH_ERROR_INTERNAL;
551 value = g_hash_table_lookup(hash, "Paired");
552 paired = g_value_get_boolean(value);
554 value = g_hash_table_lookup(hash, "Address");
555 address = value ? g_value_get_string(value) : NULL;
557 value = g_hash_table_lookup(hash, "Alias");
558 name = value ? g_value_get_string(value) : NULL;
561 BT_DBG("Alias Name [%s]", name);
563 value = g_hash_table_lookup(hash, "Name");
564 name = value ? g_value_get_string(value) : NULL;
567 value = g_hash_table_lookup(hash, "Class");
568 cod = value ? g_value_get_uint(value) : 0;
570 value = g_hash_table_lookup(hash, "Connected");
571 connected = value ? g_value_get_boolean(value) : FALSE;
573 value = g_hash_table_lookup(hash, "Trusted");
574 trust = value ? g_value_get_boolean(value) : FALSE;
576 BT_DBG("paired: %d", paired);
577 BT_DBG("trust: %d", trust);
579 if ((paired == FALSE) && (trust == FALSE)) {
580 return BLUETOOTH_ERROR_NOT_PAIRED;
583 value = g_hash_table_lookup(hash, "RSSI");
584 rssi = value ? g_value_get_int(value) : 0;
586 value = g_hash_table_lookup(hash, "UUIDs");
587 __bt_get_service_list(value, dev_info);
589 value = g_hash_table_lookup(hash, "ManufacturerDataLen");
590 dev_info->manufacturer_data.data_len = value ? g_value_get_uint(value) : 0;
592 value = g_hash_table_lookup(hash, "ManufacturerData");
593 manufacturer_data = value ? g_value_get_boxed(value) : NULL;
594 if (manufacturer_data) {
595 if (manufacturer_data->len > 0) {
596 BT_DBG("manufacturer_data->len = %d", manufacturer_data->len);
597 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
601 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
604 _bt_divide_device_class(&dev_info->device_class, cod);
606 g_strlcpy(dev_info->device_name.name, name,
607 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
609 dev_info->rssi = rssi;
610 dev_info->trust = trust;
611 dev_info->paired = paired;
612 dev_info->connected = connected;
613 g_hash_table_destroy(hash);
614 ret = BLUETOOTH_ERROR_NONE;
616 BT_ERR("Hash is NULL\n");
617 ret = BLUETOOTH_ERROR_INTERNAL;
623 void _bt_set_discovery_status(gboolean mode)
625 is_discovering = mode;
628 void _bt_set_le_discovery_status(gboolean mode)
630 is_le_discovering = mode;
633 void _bt_set_le_discovery_type(bt_le_discovery_type_t type)
635 le_discovery_type = type;
638 bt_le_discovery_type_t _bt_get_le_discovery_type(void)
640 return le_discovery_type;
643 void _bt_set_cancel_by_user(gboolean value)
645 cancel_by_user = value;
648 gboolean _bt_get_cancel_by_user(void)
650 return cancel_by_user;
653 void _bt_adapter_set_status(bt_status_t status)
655 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
656 adapter_status = status;
659 #ifdef __TIZEN_MOBILE__
660 static void __launch_bt_service(int status, int run_type)
663 char status_val[5] = { 0, };
664 char run_type_val[5] = { 0, };
666 snprintf(status_val, sizeof(status_val), "%d", status);
667 snprintf(run_type_val, sizeof(run_type_val), "%d", run_type);
669 BT_DBG("status: %s, run_type: %s", status_val, run_type_val);
671 kb = bundle_create();
673 bundle_add(kb, "launch-type", "setstate");
674 bundle_add(kb, "status", status_val);
675 bundle_add(kb, "run-type", run_type_val);
677 aul_launch_app("org.tizen.bluetooth", kb);
683 bt_status_t _bt_adapter_get_status(void)
685 return adapter_status;
688 void _bt_adapter_set_le_status(bt_le_status_t status)
690 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
691 adapter_le_status = status;
694 bt_le_status_t _bt_adapter_get_le_status(void)
696 return adapter_le_status;
699 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
701 char *phone_name = NULL;
707 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
708 phone_name = vconf_keynode_get_str(node);
710 if (phone_name && strlen(phone_name) != 0) {
711 if (!g_utf8_validate(phone_name, -1,
712 (const char **)&ptr))
715 _bt_set_local_name(phone_name);
720 #ifdef TIZEN_WEARABLE
721 static char * __bt_change_dev_name(const char *default_name)
728 if ((fp = fopen("/csa/bluetooth/.bd_addr", "r")) == NULL) {
729 BT_ERR("Unable to open bd_addr");
733 result = fseek(fp, -4, SEEK_END);
735 BT_ERR("fseek is failed");
740 buf = (char *)malloc(sizeof(char) * 5);
742 BT_ERR("malloc is failed");
748 result = fread(buf, 1, 4, fp);
750 BT_DBG("Size Read: [%d]", result);
752 BT_ERR("Error reading file: code[%d]", result);
754 name = g_strdup_printf("%s (%s)", default_name, buf);
764 static void __bt_set_visible_mode(void)
768 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
769 BT_ERR("Fail to get the timeout value");
775 static void __bt_set_local_name(void)
777 char *phone_name = NULL;
780 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
785 if (strlen(phone_name) != 0) {
786 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
789 #ifdef TIZEN_WEARABLE
790 if (strstr(phone_name, "(") == NULL) {
791 char *tmp = __bt_change_dev_name(phone_name);
798 _bt_set_local_name(phone_name);
803 int __bt_set_enabled(void)
805 int adapter_status = BT_ADAPTER_DISABLED;
806 int result = BLUETOOTH_ERROR_NONE;
808 _bt_check_adapter(&adapter_status);
809 adapter_status = BT_ADAPTER_ENABLED;
811 if (adapter_status == BT_ADAPTER_DISABLED) {
812 BT_ERR("Bluetoothd is not running");
813 return BLUETOOTH_ERROR_INTERNAL;
816 #ifndef TIZEN_WEARABLE
817 __bt_set_visible_mode();
820 __bt_set_local_name();
822 /* Update Bluetooth Status to notify other modules */
823 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
824 BT_ERR("Set vconf failed\n");
826 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
827 BT_ERR("Set vconf failed\n");
829 /* Send enabled event to API */
830 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
831 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
833 return BLUETOOTH_ERROR_NONE;
836 void _bt_set_disabled(int result)
838 int power_off_status = 0;
841 int pm_ignore_mode = 0;
843 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
844 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
846 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
848 /* Update the vconf BT status in normal Deactivation case only */
849 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
850 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
852 BT_DBG("Update vconf for BT normal Deactivation");
854 if (result == BLUETOOTH_ERROR_TIMEOUT)
855 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0 )
856 BT_ERR("Set vconf failed");
858 /* Update Bluetooth Status to notify other modules */
859 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
860 BT_ERR("Set vconf failed");
863 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
864 BT_ERR("Set vconf failed\n");
866 _bt_adapter_set_status(BT_DEACTIVATED);
868 /* Send disabled event */
869 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
870 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
873 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
874 /* Send disabled event */
875 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
876 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
881 static int __bt_set_le_enabled(void)
884 int result = BLUETOOTH_ERROR_NONE;
887 __bt_set_local_name();
889 #ifdef ENABLE_TIZEN_2_4
890 /* Update Bluetooth Status to notify other modules */
891 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
892 BT_ERR("Set vconf failed\n");
894 /* Send enabled event to API */
896 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
897 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
899 status = _bt_adapter_get_status();
900 if (status == BT_DEACTIVATED) {
901 BT_INFO("BREDR is off, turn off PSCAN");
902 _bt_set_connectable(FALSE);
904 if (le_timer_id > 0) {
905 g_source_remove(le_timer_id);
909 /* Send enabled event to API */
910 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
911 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
914 return BLUETOOTH_ERROR_NONE;
917 void _bt_set_le_disabled(int result)
919 int power_off_status;
922 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
923 BT_DBG("ret : %d", ret);
924 BT_DBG("power_off_status : %d", power_off_status);
926 /* Update Bluetooth Status to notify other modules */
927 BT_DBG("Update vconf for BT LE normal Deactivation");
928 #ifdef ENABLE_TIZEN_2_4
929 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
930 BT_ERR("Set vconf failed\n");
931 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
934 if (_bt_adapter_get_status() != BT_DEACTIVATED) {
935 /* Send disabled event */
936 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
937 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
941 void *_bt_get_adapter_agent(void)
943 return adapter_agent;
946 int _bt_enable_core(void)
950 proxy = __bt_get_core_proxy();
951 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
953 if (dbus_g_proxy_call(proxy, "EnableCore", NULL,
954 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
955 BT_ERR("Bt core call failed");
958 return BLUETOOTH_ERROR_NONE;
961 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
963 gboolean flight_mode = FALSE;
964 int power_saving_mode = 0;
967 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
968 type = vconf_keynode_get_type(node);
969 if (type == VCONF_TYPE_BOOL) {
970 flight_mode = vconf_keynode_get_bool(node);
971 if (flight_mode != TRUE) {
972 BT_ERR("Ignore the event");
975 } else if (type == VCONF_TYPE_INT) {
976 power_saving_mode = vconf_keynode_get_int(node);
977 if (power_saving_mode != 2) {
978 BT_ERR("Ignore the event");
982 BT_ERR("Invaild vconf key type : %d", type);
989 void _bt_service_register_vconf_handler(void)
994 #ifdef TIZEN_TELEPHONY_ENABLED
995 ret = vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
996 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL);
998 BT_ERR("Unable to register key handler");
1000 BT_DBG("Telephony is disabled");
1003 #ifndef TIZEN_WEARABLE
1004 #ifdef ENABLE_TIZEN_2_4
1005 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
1006 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL);
1008 BT_ERR("Unable to register key handler");
1013 void _bt_service_unregister_vconf_handler(void)
1017 #ifdef TIZEN_TELEPHONY_ENABLED
1018 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
1019 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
1022 #ifndef TIZEN_WEARABLE
1023 #ifdef ENABLE_TIZEN_2_4
1024 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
1025 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
1030 static int _bt_set_powered(gboolean is_powered)
1033 GValue powered = { 0 };
1034 GError *error = NULL;
1036 if (__bt_is_factory_test_mode()) {
1037 BT_ERR("Unable to set power in factory binary !!");
1038 return BLUETOOTH_ERROR_NOT_SUPPORT;
1041 proxy = _bt_get_adapter_properties_proxy();
1043 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1045 g_value_init(&powered, G_TYPE_BOOLEAN);
1046 g_value_set_boolean(&powered, is_powered);
1048 dbus_g_proxy_call(proxy, "Set", &error,
1049 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1050 G_TYPE_STRING, "Powered",
1051 G_TYPE_VALUE, &powered,
1052 G_TYPE_INVALID, G_TYPE_INVALID);
1054 g_value_unset(&powered);
1055 if (error != NULL) {
1056 BT_ERR("Powered set err:\n [%s]", error->message);
1057 g_error_free(error);
1058 return BLUETOOTH_ERROR_INTERNAL;
1061 BT_INFO("Set powered [%d]", is_powered);
1062 return BLUETOOTH_ERROR_NONE;
1065 void _bt_handle_adapter_added(void)
1069 bt_le_status_t le_status;
1073 BT_DBG("g_source is removed");
1074 g_source_remove(timer_id);
1078 status = _bt_adapter_get_status();
1079 le_status = _bt_adapter_get_le_status();
1080 BT_DBG("status : %d", status);
1081 BT_DBG("le_status : %d", le_status);
1083 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
1084 if (!adapter_agent) {
1085 BT_ERR("Fail to register agent");
1089 #ifdef __TIZEN_MOBILE__
1090 if (!aul_app_is_running("org.tizen.bluetooth"))
1091 __launch_bt_service(0, 0);
1093 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
1094 BT_ERR("Fail to register media player");
1098 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
1099 BT_ERR("Fail to init obex server");
1101 #ifndef TIZEN_WEARABLE
1103 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
1104 BT_ERR("Fail to activate network");
1108 /* add the vconf noti handler */
1109 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1110 __bt_phone_name_changed_cb, NULL);
1112 BT_ERR("Unable to register key handler");
1114 if (le_status == BT_LE_ACTIVATING) {
1115 __bt_set_le_enabled();
1116 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1119 BT_ERR("Activating BT ......");
1121 if (status == BT_ACTIVATING) {
1123 _bt_adapter_set_status(BT_ACTIVATED);
1125 #ifdef ENABLE_TIZEN_2_4
1129 _bt_service_register_vconf_handler();
1132 void _bt_handle_adapter_removed(void)
1136 _bt_adapter_set_status(BT_DEACTIVATED);
1137 #ifdef ENABLE_TIZEN_2_4
1141 __bt_visibility_alarm_remove();
1143 if (visible_timer.alarm_init) {
1145 visible_timer.alarm_init = FALSE;
1148 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
1149 (vconf_callback_fn)__bt_phone_name_changed_cb);
1151 ERR("vconf_ignore_key_changed failed\n");
1154 _bt_destroy_agent(adapter_agent);
1155 adapter_agent = NULL;
1157 _bt_reliable_terminate_service(NULL);
1160 static gboolean __bt_enable_timeout_cb(gpointer user_data)
1166 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
1168 BT_ERR("EnableAdapter is failed");
1170 proxy = __bt_get_core_proxy();
1174 /* Clean up the process */
1175 if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
1176 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1177 BT_ERR("Bt core call failed");
1180 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
1182 /* Display notification */
1183 notification_status_message_post(BT_STR_NOT_SUPPORT);
1185 _bt_terminate_service(NULL);
1190 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
1196 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
1198 BT_ERR("EnableAdapterLE is failed");
1200 proxy = __bt_get_core_proxy();
1204 /* Clean up the process */
1205 if (dbus_g_proxy_call(proxy, "DisableAdapterLe", NULL,
1206 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1207 BT_ERR("Bt core call failed");
1210 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
1212 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
1214 /* Display notification */
1215 notification_status_message_post(BT_STR_NOT_SUPPORT);
1217 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1218 _bt_terminate_service(NULL);
1223 void _bt_adapter_start_le_enable_timer(void)
1225 if (le_timer_id > 0) {
1226 g_source_remove(le_timer_id);
1230 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1231 __bt_enable_le_timeout_cb, NULL);
1236 void _bt_adapter_start_enable_timer(void)
1239 g_source_remove(timer_id);
1243 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1244 __bt_enable_timeout_cb, NULL);
1249 int _bt_enable_adapter(void)
1254 bt_status_t status = _bt_adapter_get_status();
1255 bt_le_status_t le_status = _bt_adapter_get_le_status();
1258 if (status == BT_ACTIVATING) {
1259 BT_ERR("Enabling in progress");
1260 return BLUETOOTH_ERROR_IN_PROGRESS;
1263 if (status == BT_ACTIVATED) {
1264 BT_ERR("Already enabled");
1265 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1268 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1269 BT_ERR("Disabling in progress");
1270 return BLUETOOTH_ERROR_DEVICE_BUSY;
1273 _bt_adapter_set_status(BT_ACTIVATING);
1275 proxy = __bt_get_core_proxy();
1277 return BLUETOOTH_ERROR_INTERNAL;
1279 if (le_status == BT_LE_ACTIVATED) {
1280 BT_INFO("LE Already enabled. Just turn on PSCAN");
1281 ret = _bt_set_connectable(TRUE);
1282 if (ret == BLUETOOTH_ERROR_NONE) {
1283 _bt_adapter_set_status(BT_ACTIVATED);
1285 return BLUETOOTH_ERROR_INTERNAL;
1288 if (dbus_g_proxy_call_with_timeout(proxy, "EnableAdapter",
1289 BT_ENABLE_TIMEOUT, &err,
1291 G_TYPE_INVALID) == FALSE) {
1293 _bt_adapter_set_status(BT_DEACTIVATED);
1296 BT_ERR("Bt core call failed: [%s]", err->message);
1300 /* Clean up the process */
1301 if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
1302 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1303 BT_ERR("Bt core call failed");
1306 /* Display notification */
1307 notification_status_message_post(BT_STR_NOT_SUPPORT);
1309 /* Terminate myself */
1310 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1311 return BLUETOOTH_ERROR_INTERNAL;
1314 if (le_status == BT_LE_ACTIVATED) {
1317 _bt_adapter_start_enable_timer();
1320 return BLUETOOTH_ERROR_NONE;
1323 static gboolean __bt_disconnect_all(void)
1326 DBusGConnection *conn;
1327 DBusGProxy *dev_proxy;
1328 gboolean ret = FALSE;
1330 GArray *device_list;
1331 bluetooth_device_info_t info;
1333 char *device_path = NULL;
1334 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1339 conn = _bt_get_system_gconn();
1341 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1343 if (_bt_get_bonded_devices(&device_list)
1344 != BLUETOOTH_ERROR_NONE) {
1345 g_array_free(device_list, TRUE);
1349 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1351 for (i = 0; i < size; i++) {
1353 info = g_array_index(device_list,
1354 bluetooth_device_info_t, i);
1356 if (info.connected == TRUE) {
1357 BT_DBG("Found Connected device");
1358 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1359 device_path = _bt_get_device_object_path(address);
1360 if (device_path == NULL)
1363 BT_DBG("Disconnecting : %s", device_path);
1365 dev_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1366 device_path, BT_DEVICE_INTERFACE);
1367 if (dev_proxy == NULL)
1370 if(!dbus_g_proxy_call(dev_proxy, "Disconnect",
1371 NULL, G_TYPE_INVALID, G_TYPE_INVALID)) {
1372 BT_ERR("Disconnect fail error.");
1373 g_object_unref(dev_proxy);
1379 g_array_free(device_list, TRUE);
1384 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1387 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1392 int __bt_disable_cb(void)
1396 bt_le_status_t le_status;
1399 _bt_adapter_set_status(BT_DEACTIVATING);
1400 le_status = _bt_adapter_get_le_status();
1401 BT_DBG("le_status : %d", le_status);
1402 if (le_status == BT_LE_ACTIVATED) {
1403 BT_INFO("LE is enabled. Just turn off PSCAN");
1405 if (_bt_is_discovering())
1406 _bt_cancel_discovery();
1408 if (_bt_is_connectable() == FALSE) {
1409 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1411 ret = _bt_set_connectable(FALSE);
1412 if (ret != BLUETOOTH_ERROR_NONE) {
1413 BT_ERR("_bt_set_connectable fail!");
1414 _bt_adapter_set_status(BT_ACTIVATED);
1415 return BLUETOOTH_ERROR_INTERNAL;
1420 proxy = __bt_get_core_proxy();
1421 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1423 if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
1424 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1425 BT_ERR("Bt core call failed");
1426 _bt_adapter_set_status(BT_ACTIVATED);
1427 return BLUETOOTH_ERROR_INTERNAL;
1430 return BLUETOOTH_ERROR_NONE;
1433 int _bt_disable_adapter(void)
1437 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1438 BT_DBG("Disabling in progress");
1439 return BLUETOOTH_ERROR_IN_PROGRESS;
1442 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1443 BT_DBG("Already disabled");
1444 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1448 g_source_remove(timer_id);
1452 __bt_disconnect_all();
1454 ret = __bt_disable_cb();
1460 int _bt_recover_adapter(void)
1465 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1466 BT_DBG("Disabling in progress");
1467 return BLUETOOTH_ERROR_IN_PROGRESS;
1470 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1471 BT_DBG("Already disabled");
1472 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1475 _bt_adapter_set_status(BT_DEACTIVATING);
1477 proxy = __bt_get_core_proxy();
1478 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1480 if (dbus_g_proxy_call(proxy, "RecoverAdapter", NULL,
1481 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1482 BT_ERR("Bt core call failed");
1483 return BLUETOOTH_ERROR_INTERNAL;
1486 __bt_disconnect_all();
1489 return BLUETOOTH_ERROR_NONE;
1492 int _bt_reset_adapter(void)
1498 proxy = __bt_get_core_proxy();
1500 return BLUETOOTH_ERROR_INTERNAL;
1502 if (dbus_g_proxy_call(proxy, "ResetAdapter", NULL,
1503 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1504 BT_ERR("Bt core call failed");
1505 return BLUETOOTH_ERROR_INTERNAL;
1508 /* Terminate myself */
1509 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1510 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1513 return BLUETOOTH_ERROR_NONE;
1516 int _bt_check_adapter(int *status)
1518 char *adapter_path = NULL;
1520 BT_CHECK_PARAMETER(status, return);
1522 *status = BT_ADAPTER_DISABLED;
1524 adapter_path = _bt_get_adapter_path();
1527 if (adapter_path != NULL)
1528 *status = BT_ADAPTER_ENABLED;
1530 g_free(adapter_path);
1532 return BLUETOOTH_ERROR_NONE;
1535 int _bt_enable_adapter_le(void)
1540 bt_status_t status = _bt_adapter_get_status();
1541 bt_le_status_t le_status = _bt_adapter_get_le_status();
1543 if (le_status == BT_LE_ACTIVATING) {
1544 BT_ERR("Enabling in progress");
1545 return BLUETOOTH_ERROR_IN_PROGRESS;
1548 if (le_status == BT_LE_ACTIVATED) {
1549 BT_ERR("Already enabled");
1550 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1553 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1554 BT_ERR("Disabling in progress");
1555 return BLUETOOTH_ERROR_DEVICE_BUSY;
1558 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1560 proxy = __bt_get_core_proxy();
1561 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1563 #if 0 // vconf key not found so commenting to resolve build issues.
1564 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
1565 BT_ERR("Set vconf failed");
1568 if (dbus_g_proxy_call_with_timeout(proxy, "EnableAdapterLe",
1569 BT_ENABLE_TIMEOUT, &err,
1571 G_TYPE_INVALID) == FALSE) {
1573 _bt_adapter_set_le_status(BT_DEACTIVATED);
1576 BT_ERR("Bt core call failed: [%s]", err->message);
1580 /* Clean up the process */
1581 if (dbus_g_proxy_call(proxy, "DisableAdapterLe", NULL,
1582 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1583 BT_ERR("Bt core call failed");
1586 /* Display notification */
1587 notification_status_message_post(BT_STR_NOT_SUPPORT);
1589 /* Terminate myself */
1590 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1591 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1592 return BLUETOOTH_ERROR_INTERNAL;
1595 _bt_adapter_start_le_enable_timer();
1597 if (status == BT_ACTIVATED) {
1598 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1599 __bt_set_le_enabled();
1601 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1603 return BLUETOOTH_ERROR_NONE;
1606 int _bt_disable_adapter_le(void)
1610 bt_le_status_t bt_le_state;
1612 bt_le_state = _bt_adapter_get_le_status();
1613 if (bt_le_state == BT_LE_DEACTIVATING) {
1614 BT_DBG("Disabling in progress");
1615 return BLUETOOTH_ERROR_IN_PROGRESS;
1618 if (bt_le_state == BT_LE_DEACTIVATED) {
1619 BT_DBG("Already disabled");
1620 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1623 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1625 proxy = __bt_get_core_proxy();
1627 return BLUETOOTH_ERROR_INTERNAL;
1628 #if 0 // vconf key not found so commenting to resolve build issues.
1629 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
1630 BT_ERR("Set vconf failed");
1633 if (dbus_g_proxy_call(proxy, "DisableAdapterLe", NULL,
1634 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
1635 BT_ERR("Bt core call failed");
1636 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1637 return BLUETOOTH_ERROR_INTERNAL;
1640 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1641 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1643 return BLUETOOTH_ERROR_NONE;
1646 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1652 GValue address_v = { 0 };
1654 BT_CHECK_PARAMETER(local_address, return);
1656 proxy = _bt_get_adapter_properties_proxy();
1657 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1659 if (!dbus_g_proxy_call(proxy, "Get", &err,
1660 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1661 G_TYPE_STRING, "Address",
1663 G_TYPE_VALUE, &address_v,
1666 BT_ERR("Getting property failed: [%s]\n", err->message);
1669 return BLUETOOTH_ERROR_INTERNAL;
1672 address = (char *)g_value_get_string(&address_v);
1675 _bt_convert_addr_string_to_type(local_address->addr, address);
1677 return BLUETOOTH_ERROR_INTERNAL;
1680 return BLUETOOTH_ERROR_NONE;
1683 int _bt_get_local_version(bluetooth_version_t *local_version)
1686 GHashTable *hash = NULL;
1689 int ret = BLUETOOTH_ERROR_NONE;
1691 BT_CHECK_PARAMETER(local_version, return);
1694 GValue version_v = { 0 };
1696 proxy = _bt_get_adapter_properties_proxy();
1697 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1699 if (!dbus_g_proxy_call(proxy, "Get", &err,
1700 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1701 G_TYPE_STRING, "Version",
1703 G_TYPE_VALUE, &version_v,
1706 BT_ERR("Getting property failed: [%s]\n", err->message);
1709 return BLUETOOTH_ERROR_INTERNAL;
1712 ver = (char *)g_value_get_string(&version_v);
1715 if (ver && (strlen(ver) > 0)) {
1716 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1717 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1720 g_strlcpy(local_version->version, ver,
1721 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1724 ret = BLUETOOTH_ERROR_INTERNAL;
1727 g_hash_table_destroy(hash);
1731 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1734 GHashTable *hash = NULL;
1737 int ret = BLUETOOTH_ERROR_NONE;
1740 GValue name_v = { 0 };
1742 BT_CHECK_PARAMETER(local_name, return);
1744 proxy = _bt_get_adapter_properties_proxy();
1745 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1747 if (!dbus_g_proxy_call(proxy, "Get", &err,
1748 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1749 G_TYPE_STRING, "Alias",
1751 G_TYPE_VALUE, &name_v,
1754 BT_ERR("Getting property failed: [%s]\n", err->message);
1757 return BLUETOOTH_ERROR_INTERNAL;
1760 name = (char *)g_value_get_string(&name_v);
1762 if (name && (strlen(name) > 0)) {
1763 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1764 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1767 g_strlcpy(local_name->name, name,
1768 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1770 ret = BLUETOOTH_ERROR_INTERNAL;
1773 g_hash_table_destroy(hash);
1777 int _bt_set_local_name(char *local_name)
1779 GValue name = { 0 };
1781 GError *error = NULL;
1784 BT_CHECK_PARAMETER(local_name, return);
1786 proxy = _bt_get_adapter_properties_proxy();
1788 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1790 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1793 g_value_init(&name, G_TYPE_STRING);
1794 g_value_set_string(&name, local_name);
1796 dbus_g_proxy_call(proxy, "Set", &error,
1797 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1798 G_TYPE_STRING, "Alias",
1799 G_TYPE_VALUE, &name,
1800 G_TYPE_INVALID, G_TYPE_INVALID);
1802 g_value_unset(&name);
1805 BT_ERR("SetProperty Fail: %s", error->message);
1806 g_error_free(error);
1807 return BLUETOOTH_ERROR_INTERNAL;
1810 return BLUETOOTH_ERROR_NONE;
1813 int _bt_is_service_used(char *service_uuid, gboolean *used)
1819 GValue uuids_v = { 0 };
1820 int ret = BLUETOOTH_ERROR_NONE;
1823 BT_CHECK_PARAMETER(service_uuid, return);
1824 BT_CHECK_PARAMETER(used, return);
1826 proxy = _bt_get_adapter_properties_proxy();
1827 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1829 if (!dbus_g_proxy_call(proxy, "Get", &err,
1830 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1831 G_TYPE_STRING, "UUIDs",
1833 G_TYPE_VALUE, &uuids_v,
1836 BT_ERR("Getting property failed: [%s]\n", err->message);
1839 return BLUETOOTH_ERROR_INTERNAL;
1842 uuids = g_value_get_boxed(&uuids_v);
1844 if (uuids == NULL) {
1850 for (i = 0; uuids[i] != NULL; i++) {
1851 if (strcasecmp(uuids[i], service_uuid) == 0) {
1859 BT_DBG("Service Used? %d", *used);
1864 static gboolean __bt_get_discoverable_property(void)
1867 GValue discoverable_v = { 0 };
1870 proxy = _bt_get_adapter_properties_proxy();
1871 retv_if(proxy == NULL, FALSE);
1873 if (!dbus_g_proxy_call(proxy, "Get", &err,
1874 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1875 G_TYPE_STRING, "Discoverable",
1877 G_TYPE_VALUE, &discoverable_v,
1880 BT_ERR("Getting property failed: [%s]\n", err->message);
1886 return g_value_get_boolean(&discoverable_v);
1889 int _bt_get_discoverable_mode(int *mode)
1891 gboolean discoverable;
1892 unsigned int timeout;
1894 BT_CHECK_PARAMETER(mode, return);
1896 discoverable = __bt_get_discoverable_property();
1897 timeout = _bt_get_discoverable_timeout_property();
1899 if (discoverable == TRUE) {
1901 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1903 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1905 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1907 return BLUETOOTH_ERROR_NONE;
1911 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1913 int ret = BLUETOOTH_ERROR_NONE;
1916 GError *error = NULL;
1917 GValue connectable = { 0 };
1918 GValue discoverable = { 0 };
1919 GValue val_timeout = { 0 };
1922 proxy = _bt_get_adapter_properties_proxy();
1924 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1926 g_value_init(&connectable, G_TYPE_BOOLEAN);
1927 g_value_init(&discoverable, G_TYPE_BOOLEAN);
1928 g_value_init(&val_timeout, G_TYPE_UINT);
1930 switch (discoverable_mode) {
1931 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1936 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1941 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1946 return BLUETOOTH_ERROR_INVALID_PARAM;
1949 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1950 discoverable_mode, timeout);
1952 g_value_set_boolean(&connectable, pg_scan);
1953 g_value_set_boolean(&discoverable, inq_scan);
1954 g_value_set_uint(&val_timeout, timeout);
1956 dbus_g_proxy_call(proxy, "Set", &error,
1957 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1958 G_TYPE_STRING, "Connectable",
1959 G_TYPE_VALUE, &connectable,
1960 G_TYPE_INVALID, G_TYPE_INVALID);
1962 if (error != NULL) {
1963 BT_ERR("Connectable set err:[%s]", error->message);
1964 g_error_free(error);
1965 ret = BLUETOOTH_ERROR_INTERNAL;
1969 dbus_g_proxy_call(proxy, "Set", &error,
1970 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1971 G_TYPE_STRING, "Discoverable",
1972 G_TYPE_VALUE, &discoverable,
1973 G_TYPE_INVALID, G_TYPE_INVALID);
1976 if (error != NULL) {
1977 BT_ERR("Discoverable set err:[%s]", error->message);
1978 g_error_free(error);
1979 ret = BLUETOOTH_ERROR_INTERNAL;
1983 dbus_g_proxy_call(proxy, "Set", &error,
1984 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
1985 G_TYPE_STRING, "DiscoverableTimeout",
1986 G_TYPE_VALUE, &val_timeout,
1987 G_TYPE_INVALID, G_TYPE_INVALID);
1989 if (error != NULL) {
1990 BT_ERR("Timeout set err:[%s]", error->message);
1991 g_error_free(error);
1992 ret = BLUETOOTH_ERROR_INTERNAL;
1996 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
1999 ret = __bt_set_visible_time(timeout);
2002 g_value_unset(&val_timeout);
2003 g_value_unset(&connectable);
2004 g_value_unset(&discoverable);
2009 int _bt_start_discovery(void)
2014 if (_bt_is_discovering() == TRUE) {
2015 BT_ERR("BT is already in discovering");
2016 return BLUETOOTH_ERROR_IN_PROGRESS;
2017 } else if (_bt_is_device_creating() == TRUE) {
2018 BT_ERR("Bonding device is going on");
2019 return BLUETOOTH_ERROR_DEVICE_BUSY;
2022 proxy = _bt_get_adapter_proxy();
2023 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2025 if (!dbus_g_proxy_call(proxy, "StartDiscovery", &err,
2026 G_TYPE_INVALID, G_TYPE_INVALID)) {
2028 BT_ERR("StartDiscovery failed: [%s]\n", err->message);
2031 BT_ERR("Discover start failed");
2032 return BLUETOOTH_ERROR_INTERNAL;
2035 is_discovering = TRUE;
2036 cancel_by_user = FALSE;
2037 /* discovery status will be change in event */
2038 BT_ERR("_bt_start_discovery 3");
2040 return BLUETOOTH_ERROR_NONE;
2043 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2047 const gchar *disc_type;
2049 if (_bt_is_discovering() == TRUE) {
2050 BT_ERR("BT is already in discovering");
2051 return BLUETOOTH_ERROR_IN_PROGRESS;
2054 proxy = _bt_get_adapter_proxy();
2055 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2057 if (role == DISCOVERY_ROLE_BREDR)
2058 disc_type = "BREDR";
2059 else if (role == DISCOVERY_ROLE_LE)
2061 else if (role == DISCOVERY_ROLE_LE_BREDR)
2062 disc_type = "LE_BREDR";
2064 return BLUETOOTH_ERROR_INVALID_PARAM;
2066 if (!dbus_g_proxy_call(proxy, "StartCustomDiscovery", NULL,
2067 G_TYPE_STRING, disc_type,
2068 G_TYPE_INVALID, G_TYPE_INVALID)) {
2069 BT_ERR("StartCustomDiscovery failed");
2070 return BLUETOOTH_ERROR_INTERNAL;
2073 is_discovering = TRUE;
2074 cancel_by_user = FALSE;
2075 /* discovery status will be change in event */
2077 return BLUETOOTH_ERROR_NONE;
2080 int _bt_cancel_discovery(void)
2085 if (_bt_is_discovering() == FALSE) {
2086 BT_ERR("BT is not in discovering");
2087 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2090 proxy = _bt_get_adapter_proxy();
2091 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2093 if (!dbus_g_proxy_call(proxy, "StopDiscovery", &err,
2094 G_TYPE_INVALID, G_TYPE_INVALID)) {
2096 BT_ERR("StopDiscovery failed: [%s]\n", err->message);
2099 return BLUETOOTH_ERROR_INTERNAL;
2102 cancel_by_user = TRUE;
2103 /* discovery status will be change in event */
2105 return BLUETOOTH_ERROR_NONE;
2108 int _bt_start_le_discovery(void)
2112 if (_bt_is_le_discovering() == TRUE) {
2113 BT_ERR("BT is already in LE discovering");
2114 return BLUETOOTH_ERROR_IN_PROGRESS;
2117 proxy = _bt_get_adapter_proxy();
2118 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2120 if (!dbus_g_proxy_call(proxy, "StartLEDiscovery", NULL,
2121 G_TYPE_INVALID, G_TYPE_INVALID)) {
2122 BT_ERR("LE Discover start failed");
2123 return BLUETOOTH_ERROR_INTERNAL;
2126 is_le_discovering = TRUE;
2128 return BLUETOOTH_ERROR_NONE;
2131 int _bt_stop_le_discovery(void)
2135 if (_bt_is_le_discovering() == FALSE) {
2136 BT_ERR("BT is not in LE discovering");
2137 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2140 proxy = _bt_get_adapter_proxy();
2141 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2143 if (!dbus_g_proxy_call(proxy, "StopLEDiscovery", NULL,
2144 G_TYPE_INVALID, G_TYPE_INVALID)) {
2145 BT_ERR("LE Discover stop failed");
2146 return BLUETOOTH_ERROR_INTERNAL;
2149 return BLUETOOTH_ERROR_NONE;
2152 gboolean _bt_is_discovering(void)
2154 return is_discovering;
2157 gboolean _bt_is_le_discovering(void)
2159 return is_le_discovering;
2162 gboolean _bt_is_connectable(void)
2165 GValue connectable_v = { 0 };
2167 gboolean is_connectable = FALSE;
2169 proxy = _bt_get_adapter_properties_proxy();
2170 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2172 if (!dbus_g_proxy_call(proxy, "Get", &err,
2173 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
2174 G_TYPE_STRING, "Connectable",
2176 G_TYPE_VALUE, &connectable_v,
2179 BT_ERR("Getting property failed: [%s]\n", err->message);
2182 return BLUETOOTH_ERROR_INTERNAL;
2185 is_connectable = g_value_get_boolean(&connectable_v);
2186 BT_INFO("Get connectable [%d]", is_connectable);
2188 return is_connectable;
2191 int _bt_set_connectable(gboolean is_connectable)
2194 GValue connectable = { 0 };
2195 GError *error = NULL;
2197 if (__bt_is_factory_test_mode()) {
2198 BT_ERR("Unable to set connectable in factory binary !!");
2199 return BLUETOOTH_ERROR_NOT_SUPPORT;
2202 proxy = _bt_get_adapter_properties_proxy();
2204 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2206 g_value_init(&connectable, G_TYPE_BOOLEAN);
2207 g_value_set_boolean(&connectable, is_connectable);
2209 dbus_g_proxy_call(proxy, "Set", &error,
2210 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
2211 G_TYPE_STRING, "Connectable",
2212 G_TYPE_VALUE, &connectable,
2213 G_TYPE_INVALID, G_TYPE_INVALID);
2215 g_value_unset(&connectable);
2216 if (error != NULL) {
2217 BT_ERR("Connectable set err:[%s]", error->message);
2218 g_error_free(error);
2219 return BLUETOOTH_ERROR_INTERNAL;
2222 BT_INFO("Set connectable [%d]", is_connectable);
2223 return BLUETOOTH_ERROR_NONE;
2226 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2229 GValue discovering_v = { 0 };
2231 char *discovering_type = NULL;
2233 proxy = _bt_get_adapter_properties_proxy();
2234 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2236 if (discovery_type == DISCOVERY_ROLE_BREDR)
2237 discovering_type = "Discovering";
2238 else if (discovery_type == DISCOVERY_ROLE_LE)
2239 discovering_type = "LEDiscovering";
2241 if (!dbus_g_proxy_call(proxy, "Get", &err,
2242 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
2243 G_TYPE_STRING, discovering_type,
2245 G_TYPE_VALUE, &discovering_v,
2248 BT_ERR("Getting property failed: [%s]\n", err->message);
2251 return BLUETOOTH_ERROR_INTERNAL;
2254 return g_value_get_boolean(&discovering_v);
2257 unsigned int _bt_get_discoverable_timeout_property(void)
2260 GValue timeout_v = { 0 };
2263 proxy = _bt_get_adapter_properties_proxy();
2264 retv_if(proxy == NULL, 0);
2266 if (!dbus_g_proxy_call(proxy, "Get", &err,
2267 G_TYPE_STRING, BT_ADAPTER_INTERFACE,
2268 G_TYPE_STRING, "DiscoverableTimeout",
2270 G_TYPE_VALUE, &timeout_v,
2273 BT_ERR("Getting property failed: [%s]\n", err->message);
2279 return g_value_get_uint(&timeout_v);
2282 static bluetooth_device_info_t *__bt_parse_device_info(DBusMessageIter *item_iter)
2284 DBusMessageIter value_iter;
2285 bluetooth_device_info_t *dev_info;
2287 dbus_message_iter_recurse(item_iter, &value_iter);
2289 if (dbus_message_iter_get_arg_type(&value_iter) != DBUS_TYPE_DICT_ENTRY) {
2294 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2296 while (dbus_message_iter_get_arg_type(&value_iter) ==
2297 DBUS_TYPE_DICT_ENTRY) {
2300 DBusMessageIter dict_entry;
2301 DBusMessageIter iter_dict_val;
2303 dbus_message_iter_recurse(&value_iter, &dict_entry);
2305 dbus_message_iter_get_basic(&dict_entry, &key);
2308 dbus_message_iter_next(&value_iter);
2312 if (!dbus_message_iter_next(&dict_entry)) {
2313 dbus_message_iter_next(&value_iter);
2316 dbus_message_iter_recurse(&dict_entry, &iter_dict_val);
2318 if (strcasecmp(key, "Address") == 0) {
2319 const char *address = NULL;
2320 dbus_message_iter_get_basic(&iter_dict_val, &address);
2321 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2324 } else if (strcasecmp(key, "Class") == 0) {
2326 dbus_message_iter_get_basic(&iter_dict_val, &cod);
2327 _bt_divide_device_class(&dev_info->device_class, cod);
2328 } else if (strcasecmp(key, "Name") == 0) {
2329 dbus_message_iter_get_basic(&iter_dict_val, &value);
2331 /* If there is no Alias */
2332 if (strlen(dev_info->device_name.name) == 0) {
2333 g_strlcpy(dev_info->device_name.name, value,
2334 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2336 } else if (strcasecmp(key, "Alias") == 0) {
2337 dbus_message_iter_get_basic(&iter_dict_val, &value);
2339 /* Overwrite the name */
2341 memset(dev_info->device_name.name, 0x00,
2342 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2343 g_strlcpy(dev_info->device_name.name, value,
2344 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2346 } else if (strcasecmp(key, "Connected") == 0) {
2347 dbus_message_iter_get_basic(&iter_dict_val,
2348 &dev_info->connected);
2349 } else if (strcasecmp(key, "Paired") == 0) {
2350 dbus_message_iter_get_basic(&iter_dict_val,
2352 } else if (strcasecmp(key, "Trusted") == 0) {
2353 dbus_message_iter_get_basic(&iter_dict_val,
2355 } else if (strcasecmp(key, "RSSI") == 0) {
2356 dbus_message_iter_get_basic(&iter_dict_val,
2358 } else if (strcasecmp(key, "UUIDs") == 0) {
2359 DBusMessageIter uuid_iter;
2363 dbus_message_iter_recurse(&iter_dict_val, &uuid_iter);
2365 while (dbus_message_iter_get_arg_type(&uuid_iter) != DBUS_TYPE_INVALID) {
2366 dbus_message_iter_get_basic(&uuid_iter,
2369 g_strlcpy(dev_info->uuids[i], value,
2370 BLUETOOTH_UUID_STRING_MAX);
2372 parts = g_strsplit(value, "-", -1);
2374 if (parts == NULL || parts[0] == NULL)
2377 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2381 if (!dbus_message_iter_next(&uuid_iter)) {
2386 dev_info->service_index = i;
2387 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
2388 dbus_message_iter_get_basic(&iter_dict_val,
2389 &dev_info->manufacturer_data.data_len);
2390 } else if (strcasecmp(key, "ManufacturerData") == 0) {
2391 DBusMessageIter manufacturer_iter;
2395 dbus_message_iter_recurse(&iter_dict_val, &manufacturer_iter);
2397 while (dbus_message_iter_get_arg_type(&manufacturer_iter) == DBUS_TYPE_BYTE) {
2398 dbus_message_iter_get_basic(&manufacturer_iter, &byte);
2399 dev_info->manufacturer_data.data[i] = byte;
2401 dbus_message_iter_next(&manufacturer_iter);
2405 dbus_message_iter_next(&value_iter);
2411 static void __bt_extract_device_info(DBusMessageIter *msg_iter,
2414 bluetooth_device_info_t *dev_info = NULL;
2415 char *object_path = NULL;
2416 DBusMessageIter value_iter;
2418 /* Parse the signature: oa{sa{sv}}} */
2419 ret_if(dbus_message_iter_get_arg_type(msg_iter) !=
2420 DBUS_TYPE_OBJECT_PATH);
2422 dbus_message_iter_get_basic(msg_iter, &object_path);
2423 ret_if(object_path == NULL);
2425 /* object array (oa) */
2426 ret_if(dbus_message_iter_next(msg_iter) == FALSE);
2427 ret_if(dbus_message_iter_get_arg_type(msg_iter) != DBUS_TYPE_ARRAY);
2429 dbus_message_iter_recurse(msg_iter, &value_iter);
2431 /* string array (sa) */
2432 while (dbus_message_iter_get_arg_type(&value_iter) ==
2433 DBUS_TYPE_DICT_ENTRY) {
2434 char *interface_name = NULL;
2435 DBusMessageIter interface_iter;
2437 dbus_message_iter_recurse(&value_iter, &interface_iter);
2439 ret_if(dbus_message_iter_get_arg_type(&interface_iter) !=
2442 dbus_message_iter_get_basic(&interface_iter, &interface_name);
2444 ret_if(dbus_message_iter_next(&interface_iter) == FALSE);
2446 ret_if(dbus_message_iter_get_arg_type(&interface_iter) !=
2449 if (g_strcmp0(interface_name, "org.bluez.Device1") == 0) {
2450 BT_DBG("Found a device: %s", object_path);
2451 dev_info = __bt_parse_device_info(&interface_iter);
2454 if (dev_info->paired == FALSE)
2457 g_array_append_vals(*dev_list, dev_info,
2458 sizeof(bluetooth_device_info_t));
2466 dbus_message_iter_next(&value_iter);
2469 BT_DBG("There is no device interface");
2472 BT_DBG("Not paired");
2476 int _bt_get_bonded_devices(GArray **dev_list)
2481 DBusMessageIter reply_iter;
2482 DBusMessageIter value_iter;
2484 DBusConnection *conn;
2486 conn = _bt_get_system_conn();
2487 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2489 msg = dbus_message_new_method_call(BT_BLUEZ_NAME, BT_MANAGER_PATH,
2490 BT_MANAGER_INTERFACE,
2491 "GetManagedObjects");
2493 retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
2495 /* Synchronous call */
2496 dbus_error_init(&err);
2497 reply = dbus_connection_send_with_reply_and_block(
2500 dbus_message_unref(msg);
2503 BT_ERR("Can't get managed objects");
2505 if (dbus_error_is_set(&err)) {
2506 BT_ERR("%s", err.message);
2507 dbus_error_free(&err);
2509 return BLUETOOTH_ERROR_INTERNAL;
2512 if (dbus_message_iter_init(reply, &reply_iter) == FALSE) {
2513 BT_ERR("Fail to iterate the reply");
2514 dbus_message_unref(reply);
2515 return BLUETOOTH_ERROR_INTERNAL;
2518 dbus_message_iter_recurse(&reply_iter, &value_iter);
2520 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2521 while (dbus_message_iter_get_arg_type(&value_iter) ==
2522 DBUS_TYPE_DICT_ENTRY) {
2523 DBusMessageIter msg_iter;
2525 dbus_message_iter_recurse(&value_iter, &msg_iter);
2527 __bt_extract_device_info(&msg_iter, dev_list);
2529 dbus_message_iter_next(&value_iter);
2531 dbus_message_unref(reply);
2533 return BLUETOOTH_ERROR_NONE;
2536 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2537 bluetooth_device_info_t *dev_info)
2539 char *object_path = NULL;
2540 DBusGProxy *adapter_proxy;
2541 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2542 int ret = BLUETOOTH_ERROR_NONE;
2544 BT_CHECK_PARAMETER(device_address, return);
2545 BT_CHECK_PARAMETER(dev_info, return);
2547 adapter_proxy = _bt_get_adapter_proxy();
2548 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2550 _bt_convert_addr_type_to_string(address, device_address->addr);
2552 object_path = _bt_get_device_object_path(address);
2554 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2556 ret = __bt_get_bonded_device_info(object_path, dev_info);
2557 g_free(object_path);
2562 int _bt_get_timeout_value(int *timeout)
2564 time_t current_time;
2567 /* Take current time */
2568 time(¤t_time);
2569 time_diff = difftime(current_time, visible_timer.start_time);
2571 BT_DBG("Time diff = %d\n", time_diff);
2573 *timeout = visible_timer.timeout - time_diff;
2575 return BLUETOOTH_ERROR_NONE;
2578 int _bt_set_le_privacy(gboolean set_privacy)
2581 GError *error = NULL;
2582 int ret = BLUETOOTH_ERROR_NONE;
2584 if (__bt_is_factory_test_mode()) {
2585 BT_ERR("Unable to set le privacy in factory binary !!");
2586 return BLUETOOTH_ERROR_NOT_SUPPORT;
2589 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2590 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2591 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2594 proxy = _bt_get_adapter_proxy();
2595 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2597 dbus_g_proxy_call(proxy, "SetLePrivacy", &error,
2598 G_TYPE_BOOLEAN, set_privacy,
2599 G_TYPE_INVALID, G_TYPE_INVALID);
2602 BT_ERR("SetLePrivacy Failed :[%s]", error->message);
2603 if (g_strrstr(error->message, BT_SERVICE_ERR_MSG_NOT_SUPPORTED))
2604 ret = BLUETOOTH_ERROR_NOT_SUPPORT;
2606 ret = BLUETOOTH_ERROR_INTERNAL;
2607 g_error_free(error);
2611 BT_INFO("SetLePrivacy as %d", set_privacy);
2613 return BLUETOOTH_ERROR_NONE;
2616 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2619 GError *error = NULL;
2623 BT_CHECK_PARAMETER(m_data, return);
2625 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2626 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2627 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2630 proxy = _bt_get_adapter_proxy();
2631 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2633 arr = g_array_new(TRUE, TRUE, sizeof(guint8));
2635 for (i = 0; i < (m_data->data_len) + 2; i++)
2636 g_array_append_vals(arr, &(m_data->data[i]), sizeof(guint8));
2638 dbus_g_proxy_call(proxy, "SetManufacturerData", &error,
2639 DBUS_TYPE_G_UCHAR_ARRAY, arr,
2640 G_TYPE_INVALID, G_TYPE_INVALID);
2642 g_array_free(arr, TRUE);
2645 BT_ERR("SetManufacturerData Fail: %s", error->message);
2646 g_error_free(error);
2647 return BLUETOOTH_ERROR_INTERNAL;
2650 _bt_send_event(BT_ADAPTER_EVENT,
2651 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2652 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
2653 &m_data, m_data->data_len,
2656 BT_INFO("Set manufacturer data");
2658 return BLUETOOTH_ERROR_NONE;