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.
24 #include <syspopup_caller.h>
26 #include <dbus/dbus-glib.h>
27 #include <dbus/dbus.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
33 #include "bluetooth-api.h"
34 #include "bt-internal-types.h"
35 #include "bt-service-common.h"
36 #include "bt-service-event.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-util.h"
39 #include "bt-service-network.h"
40 #include "bt-service-obex-server.h"
41 #include "bt-service-opp-client.h"
42 #include "bt-service-agent.h"
43 #include "bt-service-main.h"
44 #include "bt-service-avrcp.h"
45 #include "bt-service-device.h"
46 #ifdef TIZEN_DPM_ENABLE
47 #include "bt-service-dpm.h"
57 bt_adapter_timer_t visible_timer = {0, };
61 bt_set_alarm_cb callback;
66 gboolean is_alarm_initialized;
68 } bt_service_alarm_mgr_t;
70 static bt_service_alarm_mgr_t alarm_mgr = {0, };
72 static gboolean is_discovering;
73 static gboolean cancel_by_user;
74 static bt_status_t adapter_status = BT_DEACTIVATED;
75 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
76 static gboolean is_le_intended = FALSE;
77 static void *adapter_agent = NULL;
78 static GDBusProxy *core_proxy = NULL;
79 static guint timer_id = 0;
80 static guint le_timer_id = 0;
82 static uint status_reg_id;
84 #define BT_CORE_NAME "org.projectx.bt_core"
85 #define BT_CORE_PATH "/org/projectx/bt_core"
86 #define BT_CORE_INTERFACE "org.projectx.btcore"
88 #define BT_DISABLE_TIME 500 /* 500 ms */
90 static int alarm_cb(alarm_id_t alarm_id, void* user_param);
91 static void alarm_data_free(void *data);
93 GDBusProxy *_bt_init_core_proxy(void)
96 GDBusConnection *conn;
98 conn = _bt_get_system_gconn();
102 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
117 static GDBusProxy *__bt_get_core_proxy(void)
119 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
122 static gboolean __bt_is_factory_test_mode(void)
126 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
127 BT_ERR("Get the DUT Mode fail");
132 BT_INFO("DUT Test Mode !!");
139 static gboolean __bt_timeout_handler(gpointer user_data)
141 int result = BLUETOOTH_ERROR_NONE;
145 /* Take current time */
147 time_diff = difftime(current_time, visible_timer.start_time);
149 /* Send event to application */
150 _bt_send_event(BT_ADAPTER_EVENT,
151 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
152 g_variant_new("(in)", result, time_diff));
154 if (visible_timer.timeout <= time_diff) {
155 g_source_remove(visible_timer.event_id);
156 visible_timer.event_id = 0;
157 visible_timer.timeout = 0;
159 #ifndef TIZEN_PROFILE_WEARABLE
160 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
161 BT_ERR("Set vconf failed\n");
169 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
172 int result = BLUETOOTH_ERROR_NONE;
175 if (alarm_id != visible_timer.alarm_id)
178 if (visible_timer.event_id) {
179 _bt_send_event(BT_ADAPTER_EVENT,
180 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
181 g_variant_new("(in)", result, timeout));
182 g_source_remove(visible_timer.event_id);
183 visible_timer.event_id = 0;
184 visible_timer.timeout = 0;
186 #ifndef TIZEN_PROFILE_WEARABLE
187 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
188 BT_ERR("Set vconf failed\n");
191 /* Switch Off visibility in Bluez */
192 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
193 visible_timer.alarm_id = 0;
197 static void __bt_visibility_alarm_remove()
199 if (visible_timer.event_id > 0) {
200 g_source_remove(visible_timer.event_id);
201 visible_timer.event_id = 0;
204 if (visible_timer.alarm_id > 0) {
205 _bt_service_remove_alarm(visible_timer.alarm_id);
206 visible_timer.alarm_id = 0;
210 int __bt_set_visible_time(int timeout)
215 __bt_visibility_alarm_remove();
217 visible_timer.timeout = timeout;
219 #ifndef TIZEN_PROFILE_WEARABLE
220 #ifdef TIZEN_DPM_ENABLE
221 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
223 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
224 BT_ERR("Set vconf failed");
225 #ifdef TIZEN_DPM_ENABLE
232 return BLUETOOTH_ERROR_NONE;
234 result = _bt_service_set_alarm(visible_timer.timeout,
235 __bt_visibility_alarm_cb, NULL, &alarm_id);
236 if (result != BLUETOOTH_ERROR_NONE)
237 return BLUETOOTH_ERROR_INTERNAL;
238 visible_timer.alarm_id = alarm_id;
239 /* Take start time */
240 time(&(visible_timer.start_time));
241 visible_timer.event_id = g_timeout_add_seconds(1,
242 __bt_timeout_handler, NULL);
244 return BLUETOOTH_ERROR_NONE;
247 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
254 ret_if(value == NULL);
257 dev->service_index = 0;
259 g_variant_get(value, "as", &iter);
260 while (g_variant_iter_loop(iter, "s", &uuid)) {
261 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
262 parts = g_strsplit(uuid, "-", -1);
264 if (parts == NULL || parts[0] == NULL) {
269 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
272 dev->service_index++;
275 g_variant_iter_free(iter);
278 static int __bt_get_bonded_device_info(gchar *device_path,
279 bluetooth_device_info_t *dev_info)
281 GError *error = NULL;
282 GDBusProxy *device_proxy;
283 gchar *address = NULL;
285 unsigned int cod = 0;
287 gboolean trust = FALSE;
288 gboolean paired = FALSE;
289 guchar connected = 0;
290 GByteArray *manufacturer_data = NULL;
292 GDBusConnection *conn;
294 GVariantIter *property_iter;
298 GVariantIter *char_value_iter;
300 BT_CHECK_PARAMETER(device_path, return);
301 BT_CHECK_PARAMETER(dev_info, return);
303 conn = _bt_get_system_gconn();
304 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
306 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
310 BT_PROPERTIES_INTERFACE,
313 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
315 result = g_dbus_proxy_call_sync(device_proxy,
317 g_variant_new("(s)", BT_DEVICE_INTERFACE),
318 G_DBUS_CALL_FLAGS_NONE,
324 BT_ERR("Error occured in Proxy call");
326 BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
327 g_clear_error(&error);
329 g_object_unref(device_proxy);
330 return BLUETOOTH_ERROR_INTERNAL;
333 g_object_unref(device_proxy);
335 g_variant_get(result, "(a{sv})", &property_iter);
337 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
338 if (!g_strcmp0(key, "Paired")) {
339 paired = g_variant_get_boolean(value);
340 } else if (!g_strcmp0(key, "Address")) {
341 g_variant_get(value, "s", &address);
342 } else if (!g_strcmp0(key, "Alias")) {
343 g_variant_get(value, "s", &name);
344 } else if (!g_strcmp0(key, "Name")) {
346 g_variant_get(value, "s", &name);
347 } else if(!g_strcmp0(key, "IsAliasSet")) {
348 dev_info->is_alias_set = g_variant_get_boolean(value);
349 } else if (!g_strcmp0(key, "Class")) {
350 cod = g_variant_get_uint32(value);
351 } else if (!g_strcmp0(key, "Connected")) {
352 connected = g_variant_get_byte(value);
353 } else if (!g_strcmp0(key, "Trusted")) {
354 trust = g_variant_get_boolean(value);
355 } else if (!g_strcmp0(key, "RSSI")) {
356 rssi = g_variant_get_int16(value);
357 } else if (!g_strcmp0(key, "UUIDs")) {
358 __bt_get_service_list(value, dev_info);
359 } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
360 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
361 } else if (!g_strcmp0(key, "ManufacturerData")) {
362 manufacturer_data = g_byte_array_new();
363 g_variant_get(value, "ay", &char_value_iter);
364 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
365 g_byte_array_append(manufacturer_data, &char_value, 1);
367 g_variant_iter_free(char_value_iter);
369 if (manufacturer_data) {
370 if (manufacturer_data->len > 0) {
371 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
372 manufacturer_data->len);
375 g_byte_array_free(manufacturer_data, TRUE);
378 g_variant_iter_free(property_iter);
380 BT_DBG("trust: %d, paired: %d", trust, paired);
382 g_variant_unref(result);
384 if ((paired == FALSE) && (trust == FALSE)) {
387 return BLUETOOTH_ERROR_NOT_PAIRED;
390 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
393 _bt_divide_device_class(&dev_info->device_class, cod);
395 g_strlcpy(dev_info->device_name.name, name,
396 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
398 dev_info->rssi = rssi;
399 dev_info->trust = trust;
400 dev_info->paired = paired;
401 dev_info->connected = connected;
402 ret = BLUETOOTH_ERROR_NONE;
409 void _bt_set_discovery_status(gboolean mode)
411 is_discovering = mode;
414 void _bt_set_cancel_by_user(gboolean value)
416 cancel_by_user = value;
419 gboolean _bt_get_cancel_by_user(void)
421 return cancel_by_user;
424 void _bt_adapter_set_status(bt_status_t status)
426 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
427 adapter_status = status;
430 bt_status_t _bt_adapter_get_status(void)
432 return adapter_status;
435 void _bt_adapter_set_le_status(bt_le_status_t status)
437 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
438 adapter_le_status = status;
441 bt_le_status_t _bt_adapter_get_le_status(void)
443 return adapter_le_status;
447 void _bt_set_le_intended_status(gboolean value)
449 is_le_intended = value;
452 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
454 char *phone_name = NULL;
460 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
461 phone_name = vconf_keynode_get_str(node);
463 if (phone_name && strlen(phone_name) != 0) {
464 if (!g_utf8_validate(phone_name, -1,
465 (const char **)&ptr))
468 _bt_set_local_name(phone_name);
473 static void __bt_set_local_name(void)
475 bluetooth_device_name_t local_name;
476 char *phone_name = NULL;
480 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
481 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
482 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
487 if (strlen(phone_name) != 0) {
488 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
491 _bt_set_local_name(phone_name);
497 static int __bt_set_enabled(void)
499 int adapter_status = BT_ADAPTER_DISABLED;
500 int result = BLUETOOTH_ERROR_NONE;
503 BT_DBG("g_source is removed");
504 g_source_remove(timer_id);
508 _bt_check_adapter(&adapter_status);
510 if (adapter_status == BT_ADAPTER_DISABLED) {
511 BT_ERR("Bluetoothd is not running");
512 return BLUETOOTH_ERROR_INTERNAL;
515 #ifdef TIZEN_PROFILE_MOBILE
516 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
517 if (_bt_set_discoverable_mode(
518 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
519 BT_ERR("Set connectable mode failed");
522 if (_bt_set_discoverable_mode(
523 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
524 BT_ERR("Fail to set discoverable mode");
528 /* Update Bluetooth Status to notify other modules */
529 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
530 BT_ERR("Set vconf failed\n");
532 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
533 BT_ERR("Set vconf failed\n");
535 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
536 EVT_VAL_BT_ON) != ES_R_OK)
537 BT_ERR("Fail to set value");
539 /* Send enabled event to API */
540 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
541 g_variant_new("(i)", result));
543 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
544 _bt_audio_start_auto_connect(FALSE);
547 __bt_set_local_name();
548 _bt_set_discovery_status(FALSE);
550 return BLUETOOTH_ERROR_NONE;
553 void _bt_set_disabled(int result)
555 int power_off_status = 0;
558 int pm_ignore_mode = 0;
560 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
561 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
563 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
565 /* Update the vconf BT status in normal Deactivation case only */
566 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
567 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
569 BT_DBG("Update vconf for BT normal Deactivation");
571 if (result == BLUETOOTH_ERROR_TIMEOUT)
572 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
573 BT_ERR("Set vconf failed");
575 /* Update Bluetooth Status to notify other modules */
576 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
577 BT_ERR("Set vconf failed");
579 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
580 EVT_VAL_BT_OFF) != ES_R_OK)
581 BT_ERR("Fail to set value");
584 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
585 BT_ERR("Set vconf failed\n");
587 _bt_cancel_queued_transfers();
588 _bt_adapter_set_status(BT_DEACTIVATED);
589 _bt_set_discovery_status(FALSE);
591 #ifndef USB_BLUETOOTH
592 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
594 /* Send disabled event */
595 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
596 g_variant_new("(i)", result));
597 #ifndef USB_BLUETOOTH
601 BT_INFO("Adapter disabled");
604 static int __bt_set_le_enabled(void)
607 int result = BLUETOOTH_ERROR_NONE;
610 /* Update Bluetooth Status to notify other modules */
611 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
612 BT_ERR("Set vconf failed\n");
614 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
615 EVT_VAL_BT_LE_ON) != ES_R_OK)
616 BT_ERR("Fail to set value");
618 /* Send enabled event to API */
620 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
621 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
623 status = _bt_adapter_get_status();
624 if (status == BT_DEACTIVATED) {
625 BT_INFO("BREDR is off, turn off PSCAN");
626 _bt_set_connectable(FALSE);
628 if (le_timer_id > 0) {
629 g_source_remove(le_timer_id);
633 /* Send enabled event to API */
634 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
635 g_variant_new("(i)", result));
637 __bt_set_local_name();
640 return BLUETOOTH_ERROR_NONE;
643 void _bt_set_le_disabled(int result)
645 int power_off_status;
648 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
649 BT_DBG("ret : %d", ret);
650 BT_DBG("power_off_status : %d", power_off_status);
652 /* Update Bluetooth Status to notify other modules */
653 BT_DBG("Update vconf for BT LE normal Deactivation");
654 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
655 BT_ERR("Set vconf failed\n");
656 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
658 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
659 EVT_VAL_BT_LE_OFF) != ES_R_OK)
660 BT_ERR("Fail to set value");
662 /* Send disabled event */
663 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
664 g_variant_new_int32(result));
667 void *_bt_get_adapter_agent(void)
669 return adapter_agent;
672 int _bt_enable_core(void)
676 GError *error = NULL;
678 proxy = __bt_get_core_proxy();
679 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
681 /* Clean up the process */
682 result = g_dbus_proxy_call_sync(proxy,
685 G_DBUS_CALL_FLAGS_NONE,
692 BT_ERR("Bt core call failed(Error: %s)", error->message);
693 g_clear_error(&error);
695 BT_ERR("Bt core call failed");
696 return BLUETOOTH_ERROR_INTERNAL;
699 g_variant_unref(result);
700 return BLUETOOTH_ERROR_NONE;
703 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
704 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
706 gboolean flight_mode = FALSE;
707 int power_saving_mode = 0;
710 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
711 type = vconf_keynode_get_type(node);
712 if (type == VCONF_TYPE_BOOL) {
713 flight_mode = vconf_keynode_get_bool(node);
714 if (flight_mode != TRUE) {
715 BT_ERR("Ignore the event");
718 } else if (type == VCONF_TYPE_INT) {
719 power_saving_mode = vconf_keynode_get_int(node);
720 if (power_saving_mode != 2) {
721 BT_ERR("Ignore the event");
725 BT_ERR("Invaild vconf key type : %d", type);
733 void _bt_service_register_vconf_handler(void)
737 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
738 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
739 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
740 BT_ERR("Unable to register key handler");
742 BT_DBG("Telephony is disabled");
745 #ifndef TIZEN_PROFILE_WEARABLE
746 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
747 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
748 BT_ERR("Unable to register key handler");
752 void _bt_service_unregister_vconf_handler(void)
756 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
757 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
758 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
761 #ifndef TIZEN_PROFILE_WEARABLE
762 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
763 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
767 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
769 const char *bt_status = NULL;
770 const char *bt_le_status = NULL;
771 BT_DBG("bt state set event(%s) received", event_name);
773 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
774 BT_DBG("bt_state: (%s)", bt_status);
776 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
777 BT_DBG("bt_state: (%s)", bt_le_status);
780 void _bt_handle_adapter_added(void)
784 bt_le_status_t le_status;
788 BT_DBG("g_source is removed");
789 g_source_remove(timer_id);
794 status = _bt_adapter_get_status();
795 le_status = _bt_adapter_get_le_status();
796 BT_DBG("status : %d", status);
797 BT_DBG("le_status : %d", le_status);
799 #ifndef USB_BLUETOOTH
800 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
801 if (!adapter_agent) {
802 BT_ERR("Fail to register agent");
806 if (adapter_agent == NULL) {
807 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
808 if (!adapter_agent) {
809 BT_ERR("Fail to register agent");
815 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
816 BT_ERR("Fail to register media player");
818 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
819 BT_ERR("Fail to init obex server");
821 #ifdef TIZEN_BT_PAN_NAP_ENABLED
822 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
823 BT_ERR("Fail to activate network");
826 /* add the vconf noti handler */
827 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
828 __bt_phone_name_changed_cb, NULL);
830 BT_ERR("Unable to register key handler");
832 if (le_status == BT_LE_ACTIVATING ||
833 status == BT_ACTIVATING) {
834 __bt_set_le_enabled();
835 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
838 if (status == BT_ACTIVATING) {
840 _bt_adapter_set_status(BT_ACTIVATED);
843 _bt_service_register_vconf_handler();
846 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
847 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
848 BT_ERR("Fail to register system event");
852 void _bt_handle_adapter_removed(void)
856 _bt_adapter_set_status(BT_DEACTIVATED);
858 __bt_visibility_alarm_remove();
860 if (alarm_mgr.is_alarm_initialized == TRUE) {
862 alarm_mgr.is_alarm_initialized = FALSE;
863 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
864 alarm_mgr.g_alarm_list = NULL;
867 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
868 _bt_audio_stop_auto_connect();
871 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
872 (vconf_callback_fn)__bt_phone_name_changed_cb);
874 ERR("vconf_ignore_key_changed failed\n");
877 #ifndef USB_BLUETOOTH
878 _bt_destroy_agent(adapter_agent);
879 adapter_agent = NULL;
881 _bt_reliable_terminate_service(NULL);
883 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
886 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK) {
887 BT_ERR("Fail to unregister system event");
892 static gboolean __bt_enable_timeout_cb(gpointer user_data)
896 GError *error = NULL;
900 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
902 BT_ERR("EnableAdapter is failed");
904 proxy = __bt_get_core_proxy();
908 /* Clean up the process */
909 result = g_dbus_proxy_call_sync(proxy,
912 G_DBUS_CALL_FLAGS_NONE,
919 BT_ERR("Bt core call failed(Error: %s)", error->message);
920 g_clear_error(&error);
922 BT_ERR("Bt core call failed");
927 g_variant_unref(result);
928 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
930 #ifndef USB_BLUETOOTH
931 _bt_terminate_service(NULL);
937 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
941 GError *error = NULL;
945 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
947 BT_ERR("EnableAdapterLE is failed");
949 proxy = __bt_get_core_proxy();
953 /* Clean up the process */
954 result = g_dbus_proxy_call_sync(proxy,
957 G_DBUS_CALL_FLAGS_NONE,
964 BT_ERR("Bt core call failed(Error: %s)", error->message);
965 g_clear_error(&error);
967 BT_ERR("Bt core call failed");
971 g_variant_unref(result);
972 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
974 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
976 if (_bt_adapter_get_status() == BT_DEACTIVATED)
977 _bt_terminate_service(NULL);
982 void _bt_adapter_start_le_enable_timer(void)
984 if (le_timer_id > 0) {
985 g_source_remove(le_timer_id);
989 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
990 __bt_enable_le_timeout_cb, NULL);
995 void _bt_adapter_start_enable_timer(void)
998 g_source_remove(timer_id);
1002 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1003 __bt_enable_timeout_cb, NULL);
1009 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
1014 _bt_adapter_set_status(BT_ACTIVATED);
1020 int _bt_enable_adapter(void)
1023 GError *error = NULL;
1025 GVariant *result = NULL;
1026 bt_status_t status = _bt_adapter_get_status();
1027 bt_le_status_t le_status = _bt_adapter_get_le_status();
1031 if (status == BT_ACTIVATING) {
1032 BT_ERR("Enabling in progress");
1033 return BLUETOOTH_ERROR_IN_PROGRESS;
1036 if (status == BT_ACTIVATED) {
1037 BT_ERR("Already enabled");
1038 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1041 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1042 BT_ERR("Disabling in progress");
1043 return BLUETOOTH_ERROR_DEVICE_BUSY;
1046 _bt_adapter_set_status(BT_ACTIVATING);
1050 int adapter_status = BT_ADAPTER_DISABLED;
1052 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1053 BT_ERR("Set vconf failed");
1055 _bt_check_adapter(&adapter_status);
1056 if (adapter_status == BT_ADAPTER_ENABLED) {
1057 g_idle_add(__bt_adapter_enabled_cb, NULL);
1058 _bt_adapter_start_enable_timer();
1059 return BLUETOOTH_ERROR_NONE;
1064 proxy = __bt_get_core_proxy();
1066 return BLUETOOTH_ERROR_INTERNAL;
1068 if (le_status == BT_LE_ACTIVATED) {
1069 BT_INFO("LE Already enabled. Just turn on PSCAN");
1070 ret = _bt_set_connectable(TRUE);
1071 if (ret == BLUETOOTH_ERROR_NONE) {
1072 _bt_adapter_set_status(BT_ACTIVATED);
1074 return BLUETOOTH_ERROR_INTERNAL;
1078 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1080 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1083 BT_ERR("EnableAdapterLe failed: %s", error->message);
1084 _bt_adapter_set_status(BT_DEACTIVATED);
1085 g_clear_error(&error);
1087 result = g_dbus_proxy_call_sync(proxy,
1090 G_DBUS_CALL_FLAGS_NONE,
1095 if (error != NULL) {
1096 BT_ERR("Bt core call failed(Error: %s)", error->message);
1097 g_clear_error(&error);
1099 g_variant_unref(result);
1100 #ifndef USB_BLUETOOTH
1101 /* Terminate myself */
1102 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1104 return BLUETOOTH_ERROR_INTERNAL;
1106 g_variant_unref(result);
1107 if (le_status == BT_LE_ACTIVATED) {
1110 _bt_adapter_start_enable_timer();
1113 return BLUETOOTH_ERROR_NONE;
1116 static gboolean __bt_disconnect_all(void)
1119 GDBusConnection *conn;
1120 GDBusProxy *dev_proxy;
1121 gboolean ret = FALSE;
1123 GError *error = NULL;
1124 GArray *device_list;
1125 bluetooth_device_info_t info;
1127 char *device_path = NULL;
1128 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1133 conn = _bt_get_system_gconn();
1135 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1137 if (_bt_get_bonded_devices(&device_list)
1138 != BLUETOOTH_ERROR_NONE) {
1139 g_array_free(device_list, TRUE);
1143 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1145 for (i = 0; i < size; i++) {
1147 info = g_array_index(device_list,
1148 bluetooth_device_info_t, i);
1150 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1151 BT_DBG("Found Connected device");
1152 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1153 device_path = _bt_get_device_object_path(address);
1154 if (device_path == NULL)
1157 BT_DBG("Disconnecting : %s", device_path);
1159 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1163 BT_DEVICE_INTERFACE,
1166 if (dev_proxy == NULL)
1169 result = g_dbus_proxy_call_sync(dev_proxy,
1172 G_DBUS_CALL_FLAGS_NONE,
1178 if (error != NULL) {
1179 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1180 g_clear_error(&error);
1182 BT_ERR("Disconnect call failed");
1183 g_object_unref(dev_proxy);
1187 g_variant_unref(result);
1188 g_object_unref(dev_proxy);
1192 g_array_free(device_list, TRUE);
1198 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1201 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1207 int _bt_disable_cb(void)
1215 GError *error = NULL;
1217 _bt_adapter_set_status(BT_DEACTIVATING);
1219 bt_le_status_t le_status;
1220 le_status = _bt_adapter_get_le_status();
1221 BT_DBG("le_status : %d", le_status);
1222 if (le_status == BT_LE_ACTIVATED) {
1223 BT_INFO("LE is enabled. Just turn off PSCAN");
1225 if (_bt_is_discovering())
1226 _bt_cancel_discovery();
1228 if (_bt_is_connectable() == FALSE) {
1229 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1231 ret = _bt_set_connectable(FALSE);
1232 if (ret != BLUETOOTH_ERROR_NONE) {
1233 BT_ERR("_bt_set_connectable fail!");
1234 _bt_adapter_set_status(BT_ACTIVATED);
1235 return BLUETOOTH_ERROR_INTERNAL;
1240 proxy = __bt_get_core_proxy();
1241 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1243 result = g_dbus_proxy_call_sync(proxy,
1246 G_DBUS_CALL_FLAGS_NONE,
1252 if (error != NULL) {
1253 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1254 g_clear_error(&error);
1256 BT_ERR("Failed to DisableAdapter");
1257 _bt_adapter_set_status(BT_ACTIVATED);
1258 return BLUETOOTH_ERROR_INTERNAL;
1261 g_variant_unref(result);
1262 return BLUETOOTH_ERROR_NONE;
1265 int _bt_disable_adapter(void)
1270 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1271 BT_DBG("Disabling in progress");
1272 return BLUETOOTH_ERROR_IN_PROGRESS;
1275 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1276 BT_DBG("Already disabled");
1277 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1281 g_source_remove(timer_id);
1285 __bt_disconnect_all();
1286 ret = _bt_disable_cb();
1292 int _bt_recover_adapter(void)
1297 GError *error = NULL;
1299 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1300 BT_DBG("Disabling in progress");
1301 return BLUETOOTH_ERROR_IN_PROGRESS;
1304 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1305 BT_DBG("Already disabled");
1306 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1309 _bt_adapter_set_status(BT_DEACTIVATING);
1311 proxy = __bt_get_core_proxy();
1312 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1314 result = g_dbus_proxy_call_sync(proxy,
1317 G_DBUS_CALL_FLAGS_NONE,
1323 if (error != NULL) {
1324 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1325 g_clear_error(&error);
1327 BT_ERR("Failed to RecoverAdapter");
1328 return BLUETOOTH_ERROR_INTERNAL;
1331 g_variant_unref(result);
1332 __bt_disconnect_all();
1335 return BLUETOOTH_ERROR_NONE;
1338 int _bt_reset_adapter(void)
1342 GError *error = NULL;
1346 proxy = __bt_get_core_proxy();
1348 return BLUETOOTH_ERROR_INTERNAL;
1350 result = g_dbus_proxy_call_sync(proxy,
1353 G_DBUS_CALL_FLAGS_NONE,
1359 if (error != NULL) {
1360 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1361 g_clear_error(&error);
1363 BT_ERR("Failed to ResetAdapter");
1364 return BLUETOOTH_ERROR_INTERNAL;
1367 g_variant_unref(result);
1368 /* Terminate myself */
1369 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1370 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1373 return BLUETOOTH_ERROR_NONE;
1377 int _bt_check_adapter(int *status)
1380 char *adapter_path = NULL;
1382 BT_CHECK_PARAMETER(status, return);
1384 *status = BT_ADAPTER_DISABLED;
1386 adapter_path = _bt_get_adapter_path();
1389 if (adapter_path != NULL)
1390 *status = BT_ADAPTER_ENABLED;
1392 g_free(adapter_path);
1393 return BLUETOOTH_ERROR_NONE;
1396 int _bt_check_adapter(int *status)
1399 GError *error = NULL;
1402 gboolean powered = FALSE;
1404 BT_CHECK_PARAMETER(status, return);
1406 *status = BT_ADAPTER_DISABLED;
1408 proxy = _bt_get_adapter_properties_proxy();
1409 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1411 result = g_dbus_proxy_call_sync(proxy,
1413 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1415 G_DBUS_CALL_FLAGS_NONE,
1421 BT_ERR("Failed to get local address");
1422 if (error != NULL) {
1423 BT_ERR("Failed to get local address (Error: %s)", error->message);
1424 g_clear_error(&error);
1426 return BLUETOOTH_ERROR_INTERNAL;
1429 g_variant_get(result, "(v)", &temp);
1430 powered = g_variant_get_boolean(temp);
1431 BT_DBG("powered: %d", powered);
1434 *status = BT_ADAPTER_ENABLED;
1436 g_variant_unref(result);
1437 g_variant_unref(temp);
1438 return BLUETOOTH_ERROR_NONE;
1442 int _bt_enable_adapter_le(void)
1446 GError *error = NULL;
1447 bt_status_t status = _bt_adapter_get_status();
1448 bt_le_status_t le_status = _bt_adapter_get_le_status();
1451 if (le_status == BT_LE_ACTIVATING) {
1452 BT_ERR("Enabling in progress");
1453 return BLUETOOTH_ERROR_IN_PROGRESS;
1456 if (le_status == BT_LE_ACTIVATED) {
1457 BT_ERR("Already enabled");
1458 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1461 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1462 BT_ERR("Disabling in progress");
1463 return BLUETOOTH_ERROR_DEVICE_BUSY;
1466 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1468 proxy = __bt_get_core_proxy();
1469 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1471 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1473 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1476 BT_ERR("EnableAdapterLe failed: %s", error->message);
1477 _bt_adapter_set_le_status(BT_DEACTIVATED);
1478 g_clear_error(&error);
1480 /* Clean up the process */
1481 result = g_dbus_proxy_call_sync(proxy,
1484 G_DBUS_CALL_FLAGS_NONE,
1490 BT_ERR("Bt core call failed");
1492 BT_ERR("EnableAdapterLE Failed %s", error->message);
1493 g_clear_error(&error);
1496 g_variant_unref(result);
1497 /* Terminate myself */
1498 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1499 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1500 return BLUETOOTH_ERROR_INTERNAL;
1504 g_variant_unref(result);
1506 _bt_adapter_start_le_enable_timer();
1508 if (status == BT_ACTIVATED) {
1509 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1510 __bt_set_le_enabled();
1512 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1514 return BLUETOOTH_ERROR_NONE;
1517 int _bt_disable_adapter_le(void)
1521 bt_le_status_t bt_le_state;
1523 GError *error = NULL;
1525 bt_le_state = _bt_adapter_get_le_status();
1526 if (bt_le_state == BT_LE_DEACTIVATING) {
1527 BT_DBG("Disabling in progress");
1528 return BLUETOOTH_ERROR_IN_PROGRESS;
1531 if (bt_le_state == BT_LE_DEACTIVATED) {
1532 BT_DBG("Already disabled");
1533 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1536 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1538 proxy = __bt_get_core_proxy();
1540 return BLUETOOTH_ERROR_INTERNAL;
1542 result = g_dbus_proxy_call_sync(proxy,
1545 G_DBUS_CALL_FLAGS_NONE,
1551 if (error != NULL) {
1552 BT_ERR("Bt core call failed (Error: %s)", error->message);
1553 g_clear_error(&error);
1555 BT_ERR("Bt core call failed");
1556 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1557 return BLUETOOTH_ERROR_INTERNAL;
1560 g_variant_unref(result);
1561 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1562 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1564 return BLUETOOTH_ERROR_NONE;
1567 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1571 GError *error = NULL;
1572 const char *address;
1576 BT_CHECK_PARAMETER(local_address, return);
1578 proxy = _bt_get_adapter_properties_proxy();
1579 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1581 result = g_dbus_proxy_call_sync(proxy,
1583 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1585 G_DBUS_CALL_FLAGS_NONE,
1591 BT_ERR("Failed to get local address");
1592 if (error != NULL) {
1593 BT_ERR("Failed to get local address (Error: %s)", error->message);
1594 g_clear_error(&error);
1596 return BLUETOOTH_ERROR_INTERNAL;
1599 g_variant_get(result, "(v)", &temp);
1600 address = g_variant_get_string(temp, NULL);
1601 BT_DBG("Address:%s", address);
1604 _bt_convert_addr_string_to_type(local_address->addr, address);
1606 return BLUETOOTH_ERROR_INTERNAL;
1609 g_variant_unref(result);
1610 g_variant_unref(temp);
1611 return BLUETOOTH_ERROR_NONE;
1614 int _bt_get_local_version(bluetooth_version_t *local_version)
1617 const char *ver = NULL;
1619 int ret = BLUETOOTH_ERROR_NONE;
1623 BT_CHECK_PARAMETER(local_version, return);
1625 GError *error = NULL;
1627 proxy = _bt_get_adapter_properties_proxy();
1628 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1630 result = g_dbus_proxy_call_sync(proxy,
1632 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1634 G_DBUS_CALL_FLAGS_NONE,
1640 if (error != NULL) {
1641 BT_ERR("Failed to get local version (Error: %s)", error->message);
1642 g_clear_error(&error);
1644 BT_ERR("Failed to get local version");
1645 return BLUETOOTH_ERROR_INTERNAL;
1648 g_variant_get(result, "(v)", &temp);
1649 ver = g_variant_get_string(temp, NULL);
1650 BT_DBG("VERSION: %s", ver);
1652 if (ver && (strlen(ver) > 0)) {
1653 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1654 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1657 g_strlcpy(local_version->version, ver,
1658 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1661 ret = BLUETOOTH_ERROR_INTERNAL;
1664 g_variant_unref(result);
1665 g_variant_unref(temp);
1669 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1672 const char *name = NULL;
1674 int ret = BLUETOOTH_ERROR_NONE;
1677 GError *error = NULL;
1679 BT_CHECK_PARAMETER(local_name, return);
1681 proxy = _bt_get_adapter_properties_proxy();
1682 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1684 result = g_dbus_proxy_call_sync(proxy,
1686 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1688 G_DBUS_CALL_FLAGS_NONE,
1694 if (error != NULL) {
1695 BT_ERR("Failed to get local name (Error: %s)", error->message);
1696 g_clear_error(&error);
1698 BT_ERR("Failed to get local name");
1699 return BLUETOOTH_ERROR_INTERNAL;
1702 g_variant_get(result, "(v)", &temp);
1703 name = g_variant_get_string(temp, NULL);
1704 BT_DBG("LOCAL NAME:%s", name);
1706 if (name && (strlen(name) > 0)) {
1707 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1708 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1711 g_strlcpy(local_name->name, name,
1712 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1714 ret = BLUETOOTH_ERROR_INTERNAL;
1716 g_variant_unref(result);
1717 g_variant_unref(temp);
1721 int _bt_set_local_name(char *local_name)
1724 GError *error = NULL;
1728 BT_CHECK_PARAMETER(local_name, return);
1730 proxy = _bt_get_adapter_properties_proxy();
1732 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1734 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1737 result = g_dbus_proxy_call_sync(proxy,
1739 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1740 "Alias", g_variant_new("s", local_name)),
1741 G_DBUS_CALL_FLAGS_NONE,
1747 if (error != NULL) {
1748 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1749 g_clear_error(&error);
1751 BT_ERR("Failed to set Alias");
1752 return BLUETOOTH_ERROR_INTERNAL;
1755 g_variant_unref(result);
1756 return BLUETOOTH_ERROR_NONE;
1759 int _bt_is_service_used(char *service_uuid, gboolean *used)
1762 GError *error = NULL;
1763 int ret = BLUETOOTH_ERROR_NONE;
1765 GVariant *temp = NULL;
1766 GVariantIter *iter = NULL;
1770 BT_CHECK_PARAMETER(service_uuid, return);
1771 BT_CHECK_PARAMETER(used, return);
1773 proxy = _bt_get_adapter_properties_proxy();
1774 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1776 result = g_dbus_proxy_call_sync(proxy,
1778 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1780 G_DBUS_CALL_FLAGS_NONE,
1786 if (error != NULL) {
1787 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1788 g_clear_error(&error);
1790 BT_ERR("Failed to get UUIDs");
1791 return BLUETOOTH_ERROR_INTERNAL;
1794 g_variant_get(result, "(v)", &temp);
1795 g_variant_get(temp, "as", &iter);
1798 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1799 if (strcasecmp(uuid, service_uuid) == 0) {
1804 g_variant_iter_free(iter);
1805 g_variant_unref(result);
1807 BT_DBG("Service Used? %d", *used);
1812 static gboolean __bt_get_discoverable_property(void)
1815 gboolean discoverable_v;
1816 GError *error = NULL;
1820 proxy = _bt_get_adapter_properties_proxy();
1821 retv_if(proxy == NULL, FALSE);
1823 result = g_dbus_proxy_call_sync(proxy,
1825 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1827 G_DBUS_CALL_FLAGS_NONE,
1833 if (error != NULL) {
1834 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1835 g_clear_error(&error);
1837 BT_ERR("Failed to get Discoverable property");
1838 return BLUETOOTH_ERROR_INTERNAL;
1841 g_variant_get(result, "(v)", &temp);
1842 discoverable_v = g_variant_get_boolean(temp);
1843 BT_DBG("discoverable_v:%d", discoverable_v);
1845 g_variant_unref(result);
1846 g_variant_unref(temp);
1848 return discoverable_v;
1851 int _bt_get_discoverable_mode(int *mode)
1853 gboolean discoverable;
1854 unsigned int timeout;
1856 BT_CHECK_PARAMETER(mode, return);
1858 discoverable = __bt_get_discoverable_property();
1859 timeout = _bt_get_discoverable_timeout_property();
1861 if (discoverable == TRUE) {
1863 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1865 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1867 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1869 return BLUETOOTH_ERROR_NONE;
1873 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1875 int ret = BLUETOOTH_ERROR_NONE;
1878 GError *error = NULL;
1882 proxy = _bt_get_adapter_properties_proxy();
1884 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1886 #ifdef TIZEN_DPM_ENABLE
1887 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1888 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1889 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1890 return BLUETOOTH_ERROR_ACCESS_DENIED;
1892 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1893 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1894 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1895 return BLUETOOTH_ERROR_ACCESS_DENIED;
1899 switch (discoverable_mode) {
1900 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1905 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1910 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1915 return BLUETOOTH_ERROR_INVALID_PARAM;
1918 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1919 discoverable_mode, timeout);
1921 result = g_dbus_proxy_call_sync(proxy,
1923 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1924 "Connectable", g_variant_new("b", pg_scan)),
1925 G_DBUS_CALL_FLAGS_NONE,
1931 if (error != NULL) {
1932 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1933 g_clear_error(&error);
1935 BT_ERR("Failed to set connectable property");
1936 return BLUETOOTH_ERROR_INTERNAL;
1938 g_variant_unref(result);
1939 result = g_dbus_proxy_call_sync(proxy,
1941 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1942 g_variant_new("b", inq_scan)),
1943 G_DBUS_CALL_FLAGS_NONE,
1949 if (error != NULL) {
1950 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
1951 g_clear_error(&error);
1953 BT_ERR("Failed to set Discoverable property");
1954 return BLUETOOTH_ERROR_INTERNAL;
1956 g_variant_unref(result);
1957 result = g_dbus_proxy_call_sync(proxy,
1959 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1960 "DiscoverableTimeout", g_variant_new("u", timeout)),
1961 G_DBUS_CALL_FLAGS_NONE,
1967 if (error != NULL) {
1968 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
1969 g_clear_error(&error);
1971 BT_ERR("Failed to set DiscoverableTimeout property");
1972 return BLUETOOTH_ERROR_INTERNAL;
1975 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
1978 ret = __bt_set_visible_time(timeout);
1980 g_variant_unref(result);
1985 int _bt_start_discovery(void)
1988 GError *error = NULL;
1991 if (_bt_is_discovering() == TRUE) {
1992 BT_ERR("BT is already in discovering");
1993 return BLUETOOTH_ERROR_IN_PROGRESS;
1994 } else if (_bt_is_device_creating() == TRUE) {
1995 BT_ERR("Bonding device is going on");
1996 return BLUETOOTH_ERROR_DEVICE_BUSY;
1999 proxy = _bt_get_adapter_proxy();
2000 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2002 result = g_dbus_proxy_call_sync(proxy,
2005 G_DBUS_CALL_FLAGS_NONE,
2011 if (error != NULL) {
2012 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
2013 g_clear_error(&error);
2015 BT_ERR("StartDiscovery failed");
2016 return BLUETOOTH_ERROR_INTERNAL;
2019 is_discovering = TRUE;
2020 cancel_by_user = FALSE;
2021 /* discovery status will be change in event */
2022 g_variant_unref(result);
2023 return BLUETOOTH_ERROR_NONE;
2026 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2030 GError *error = NULL;
2031 const gchar *disc_type;
2033 if (_bt_is_discovering() == TRUE) {
2034 BT_ERR("BT is already in discovering");
2035 return BLUETOOTH_ERROR_IN_PROGRESS;
2038 proxy = _bt_get_adapter_proxy();
2039 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2041 if (role == DISCOVERY_ROLE_BREDR)
2042 disc_type = "BREDR";
2043 else if (role == DISCOVERY_ROLE_LE)
2045 else if (role == DISCOVERY_ROLE_LE_BREDR)
2046 disc_type = "LE_BREDR";
2048 return BLUETOOTH_ERROR_INVALID_PARAM;
2050 result = g_dbus_proxy_call_sync(proxy,
2051 "StartCustomDiscovery",
2052 g_variant_new("s", disc_type),
2053 G_DBUS_CALL_FLAGS_NONE,
2059 if (error != NULL) {
2060 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2061 g_clear_error(&error);
2063 BT_ERR("StartCustomDiscovery failed");
2064 return BLUETOOTH_ERROR_INTERNAL;
2067 is_discovering = TRUE;
2068 cancel_by_user = FALSE;
2069 /* discovery status will be change in event */
2070 g_variant_unref(result);
2071 return BLUETOOTH_ERROR_NONE;
2074 int _bt_cancel_discovery(void)
2077 GError *error = NULL;
2080 if (_bt_is_discovering() == FALSE) {
2081 BT_ERR("BT is not in discovering");
2082 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2085 proxy = _bt_get_adapter_proxy();
2086 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2088 result = g_dbus_proxy_call_sync(proxy,
2091 G_DBUS_CALL_FLAGS_NONE,
2097 int ret = BLUETOOTH_ERROR_INTERNAL;
2098 if (error != NULL) {
2099 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2101 if (g_strrstr(error->message, "No discovery started"))
2102 ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
2104 g_clear_error(&error);
2106 BT_ERR("StopDiscovery failed");
2112 cancel_by_user = TRUE;
2113 /* discovery status will be change in event */
2114 g_variant_unref(result);
2115 return BLUETOOTH_ERROR_NONE;
2118 gboolean _bt_is_discovering(void)
2120 return is_discovering;
2123 gboolean _bt_is_connectable(void)
2126 GError *error = NULL;
2127 gboolean is_connectable = FALSE;
2131 proxy = _bt_get_adapter_properties_proxy();
2132 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2134 result = g_dbus_proxy_call_sync(proxy,
2136 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2138 G_DBUS_CALL_FLAGS_NONE,
2144 if (error != NULL) {
2145 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2146 g_clear_error(&error);
2148 BT_ERR("Failed to get connectable property");
2149 return BLUETOOTH_ERROR_INTERNAL;
2152 g_variant_get(result, "(v)", &temp);
2153 is_connectable = g_variant_get_boolean(temp);
2154 BT_DBG("discoverable_v:%d", is_connectable);
2156 g_variant_unref(result);
2157 g_variant_unref(temp);
2159 BT_INFO("Get connectable [%d]", is_connectable);
2160 return is_connectable;
2163 int _bt_set_connectable(gboolean is_connectable)
2166 GError *error = NULL;
2169 if (__bt_is_factory_test_mode()) {
2170 BT_ERR("Unable to set connectable in factory binary !!");
2171 return BLUETOOTH_ERROR_NOT_SUPPORT;
2174 proxy = _bt_get_adapter_properties_proxy();
2176 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2178 result = g_dbus_proxy_call_sync(proxy,
2180 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2181 g_variant_new("b", is_connectable)),
2182 G_DBUS_CALL_FLAGS_NONE,
2188 if (error != NULL) {
2189 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2190 g_clear_error(&error);
2192 BT_ERR("Failed to set connectable property");
2193 return BLUETOOTH_ERROR_INTERNAL;
2196 BT_INFO("### Set connectable [%d]", is_connectable);
2197 g_variant_unref(result);
2198 return BLUETOOTH_ERROR_NONE;
2201 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2204 gboolean discovering_v;
2205 GError *error = NULL;
2206 char *discovering_type = NULL;
2210 proxy = _bt_get_adapter_properties_proxy();
2211 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2213 if (discovery_type == DISCOVERY_ROLE_BREDR)
2214 discovering_type = "Discovering";
2215 else if (discovery_type == DISCOVERY_ROLE_LE)
2216 discovering_type = "LEDiscovering";
2218 result = g_dbus_proxy_call_sync(proxy,
2220 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2222 G_DBUS_CALL_FLAGS_NONE,
2228 if (error != NULL) {
2229 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2230 g_clear_error(&error);
2232 BT_ERR("Failed to get discovering property");
2233 return BLUETOOTH_ERROR_INTERNAL;
2236 g_variant_get(result, "(v)", &temp);
2237 discovering_v = g_variant_get_boolean(temp);
2238 BT_DBG("discoverable_v:%d", discovering_v);
2240 g_variant_unref(result);
2241 g_variant_unref(temp);
2243 return discovering_v;
2246 unsigned int _bt_get_discoverable_timeout_property(void)
2249 unsigned int timeout_v;
2250 GError *error = NULL;
2254 proxy = _bt_get_adapter_properties_proxy();
2255 retv_if(proxy == NULL, 0);
2257 result = g_dbus_proxy_call_sync(proxy,
2259 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2260 "DiscoverableTimeout"),
2261 G_DBUS_CALL_FLAGS_NONE,
2267 BT_ERR("Fail to get discoverable timeout");
2268 if (error != NULL) {
2269 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2270 g_clear_error(&error);
2275 g_variant_get(result, "(v)", &temp);
2276 timeout_v = g_variant_get_uint32(temp);
2277 BT_DBG("discoverable_v:%d", timeout_v);
2279 g_variant_unref(result);
2280 g_variant_unref(temp);
2285 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2287 bluetooth_device_info_t *dev_info;
2290 GByteArray *manufacturer_data = NULL;
2292 GVariantIter *char_value_iter;
2294 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2296 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2301 if (!g_strcmp0(key, "Address")) {
2302 const char *address = NULL;
2303 address = g_variant_get_string(value, NULL);
2304 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2306 } else if (!g_strcmp0(key, "Class")) {
2308 cod = g_variant_get_uint32(value);
2309 _bt_divide_device_class(&dev_info->device_class, cod);
2310 } else if (!g_strcmp0(key, "Name")) {
2311 const char *name = NULL;
2312 name = g_variant_get_string(value, NULL);
2313 /* If there is no Alias */
2314 if (strlen(dev_info->device_name.name) == 0) {
2315 g_strlcpy(dev_info->device_name.name, name,
2316 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2318 } else if (!g_strcmp0(key, "Alias")) {
2319 const char *alias = NULL;
2320 alias = g_variant_get_string(value, NULL);
2321 /* Overwrite the name */
2323 memset(dev_info->device_name.name, 0x00,
2324 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2325 g_strlcpy(dev_info->device_name.name, alias,
2326 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2328 } else if (!g_strcmp0(key, "IsAliasSet")) {
2329 dev_info->is_alias_set = g_variant_get_boolean(value);
2330 } else if (!g_strcmp0(key, "Connected")) {
2331 dev_info->connected = g_variant_get_byte(value);
2332 } else if (!g_strcmp0(key, "Paired")) {
2333 dev_info->paired = g_variant_get_boolean(value);
2334 } else if (!g_strcmp0(key, "Trusted")) {
2335 dev_info->trust = g_variant_get_boolean(value);
2336 } else if (!g_strcmp0(key, "RSSI")) {
2337 dev_info->rssi = g_variant_get_int16(value);
2338 } else if (!g_strcmp0(key, "UUIDs")) {
2344 dev_info->service_index = 0;
2345 g_variant_get(value, "as", &iter);
2346 while (g_variant_iter_loop(iter, "s", &uuid)) {
2347 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2348 parts = g_strsplit(uuid, "-", -1);
2350 if (parts == NULL || parts[0] == NULL) {
2355 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2360 dev_info->service_index = i;
2361 g_variant_iter_free(iter);
2362 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
2363 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2364 } else if (strcasecmp(key, "ManufacturerData") == 0) {
2365 manufacturer_data = g_byte_array_new();
2366 g_variant_get(value, "ay", &char_value_iter);
2367 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
2368 g_byte_array_append(manufacturer_data, &char_value, 1);
2370 if (manufacturer_data) {
2371 if (manufacturer_data->len > 0) {
2372 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2375 g_variant_iter_free(char_value_iter);
2376 g_byte_array_free(manufacturer_data, TRUE);
2383 static void __bt_extract_device_info(GVariantIter *iter,
2386 bluetooth_device_info_t *dev_info = NULL;
2387 char *object_path = NULL;
2388 GVariantIter *interface_iter;
2389 GVariantIter *svc_iter;
2390 char *interface_str = NULL;
2392 /* Parse the signature: oa{sa{sv}}} */
2393 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2396 if (object_path == NULL)
2399 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2400 &interface_str, &svc_iter)) {
2401 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2402 BT_DBG("Found a device: %s", object_path);
2403 dev_info = __bt_parse_device_info(svc_iter);
2405 if (dev_info->paired == TRUE) {
2406 g_array_append_vals(*dev_list, dev_info,
2407 sizeof(bluetooth_device_info_t));
2411 g_free(interface_str);
2412 g_variant_iter_free(svc_iter);
2420 int _bt_get_bonded_devices(GArray **dev_list)
2423 GDBusConnection *conn;
2424 GDBusProxy *manager_proxy;
2425 GVariant *result = NULL;
2426 GVariantIter *iter = NULL;
2427 GError *error = NULL;
2429 conn = _bt_get_system_conn();
2430 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2432 manager_proxy = _bt_get_manager_proxy();
2433 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2435 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2437 G_DBUS_CALL_FLAGS_NONE,
2443 if (error != NULL) {
2444 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2445 g_clear_error(&error);
2447 BT_ERR("Failed to Failed to GetManagedObjects");
2448 return BLUETOOTH_ERROR_INTERNAL;
2451 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2452 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2454 __bt_extract_device_info(iter, dev_list);
2455 g_variant_iter_free(iter);
2456 g_variant_unref(result);
2459 return BLUETOOTH_ERROR_NONE;
2462 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2465 GDBusConnection *conn;
2466 GDBusProxy *manager_proxy;
2467 GVariant *result = NULL;
2468 GVariant *result1 = NULL;
2469 GVariantIter *iter = NULL;
2470 GError *error = NULL;
2471 char *object_path = NULL;
2472 GVariantIter *interface_iter;
2473 char *interface_str = NULL;
2474 GDBusProxy *device_proxy = NULL;
2475 gboolean is_connected = FALSE;
2477 conn = _bt_get_system_conn();
2478 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2480 manager_proxy = _bt_get_manager_proxy();
2481 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2483 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2485 G_DBUS_CALL_FLAGS_NONE,
2491 if (error != NULL) {
2492 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2493 g_clear_error(&error);
2496 BT_ERR("Failed to Failed to GetManagedObjects");
2497 return BLUETOOTH_ERROR_INTERNAL;
2500 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2501 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2503 /* Parse the signature: oa{sa{sv}}} */
2504 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2505 if (object_path == NULL)
2508 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2509 &interface_str, NULL)) {
2510 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2511 BT_DBG("Found a device: %s", object_path);
2512 g_free(interface_str);
2514 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2515 NULL, BT_BLUEZ_NAME,
2516 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2518 if (device_proxy == NULL) {
2519 BT_DBG("Device don't have this service");
2523 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2524 g_variant_new("(s)", profile_uuid),
2525 G_DBUS_CALL_FLAGS_NONE,
2530 if (result1 == NULL) {
2531 BT_ERR("Error occured in Proxy call");
2533 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2534 g_error_free(error);
2537 g_object_unref(device_proxy);
2540 g_variant_get(result1, "(b)", &is_connected);
2542 if (is_connected == TRUE) {
2543 char address[BT_ADDRESS_STRING_SIZE];
2544 bluetooth_device_address_t *addr = NULL;
2546 _bt_convert_device_path_to_address(object_path, address);
2548 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2549 _bt_convert_addr_string_to_type(addr->addr, address);
2551 g_array_append_vals(*addr_list, addr,
2552 sizeof(bluetooth_device_address_t));
2555 g_variant_unref(result1);
2556 g_object_unref(device_proxy);
2563 g_variant_unref(result);
2564 g_variant_iter_free(iter);
2567 return BLUETOOTH_ERROR_NONE;
2570 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2571 bluetooth_device_info_t *dev_info)
2573 char *object_path = NULL;
2574 GDBusProxy *adapter_proxy;
2575 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2576 int ret = BLUETOOTH_ERROR_NONE;
2578 BT_CHECK_PARAMETER(device_address, return);
2579 BT_CHECK_PARAMETER(dev_info, return);
2581 adapter_proxy = _bt_get_adapter_proxy();
2582 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2584 _bt_convert_addr_type_to_string(address, device_address->addr);
2586 object_path = _bt_get_device_object_path(address);
2588 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2590 ret = __bt_get_bonded_device_info(object_path, dev_info);
2591 g_free(object_path);
2596 int _bt_get_timeout_value(int *timeout)
2598 time_t current_time;
2601 /* Take current time */
2602 time(¤t_time);
2603 time_diff = difftime(current_time, visible_timer.start_time);
2605 BT_DBG("Time diff = %d\n", time_diff);
2607 *timeout = visible_timer.timeout - time_diff;
2609 return BLUETOOTH_ERROR_NONE;
2612 int _bt_set_le_privacy(gboolean set_privacy)
2615 GError *error = NULL;
2616 GVariant *result = NULL;
2618 if (__bt_is_factory_test_mode()) {
2619 BT_ERR("Unable to set le privacy in factory binary !!");
2620 return BLUETOOTH_ERROR_NOT_SUPPORT;
2623 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2624 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2625 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2628 proxy = _bt_get_adapter_proxy();
2629 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2631 result = g_dbus_proxy_call_sync(proxy,
2633 g_variant_new("(b)", set_privacy),
2634 G_DBUS_CALL_FLAGS_NONE,
2640 if (error != NULL) {
2641 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2642 g_clear_error(&error);
2644 BT_ERR("Failed to SetLePrivacy");
2645 return BLUETOOTH_ERROR_INTERNAL;
2648 g_variant_unref(result);
2649 BT_INFO("SetLePrivacy as %d", set_privacy);
2650 return BLUETOOTH_ERROR_NONE;
2653 int _bt_set_le_static_random_address(gboolean is_enable)
2656 GError *error = NULL;
2657 GVariant *result = NULL;
2659 if (__bt_is_factory_test_mode()) {
2660 BT_ERR("Unable to set le random address in factory binary !!");
2661 return BLUETOOTH_ERROR_NOT_SUPPORT;
2664 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2665 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2666 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2669 proxy = _bt_get_adapter_proxy();
2670 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2672 result = g_dbus_proxy_call_sync(proxy,
2673 "SetLeStaticRandomAddress",
2674 g_variant_new("(b)", is_enable),
2675 G_DBUS_CALL_FLAGS_NONE,
2681 if (error != NULL) {
2682 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2683 g_clear_error(&error);
2685 BT_ERR("Failed to SetLeStaticRandomAddress");
2686 return BLUETOOTH_ERROR_INTERNAL;
2689 g_variant_unref(result);
2690 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2691 return BLUETOOTH_ERROR_NONE;
2694 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2697 GError *error = NULL;
2701 GVariantBuilder *builder;
2703 BT_CHECK_PARAMETER(m_data, return);
2705 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2706 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2707 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2710 proxy = _bt_get_adapter_proxy();
2711 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2713 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2715 for (i = 0; i < (m_data->data_len) + 2; i++) {
2716 g_variant_builder_add(builder, "y", m_data->data[i]);
2719 val = g_variant_new("(ay)", builder);
2721 result = g_dbus_proxy_call_sync(proxy,
2722 "SetManufacturerData",
2724 G_DBUS_CALL_FLAGS_NONE,
2728 g_variant_builder_unref(builder);
2730 if (error != NULL) {
2731 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2732 g_clear_error(&error);
2734 BT_ERR("Failed to SetManufacturerData");
2736 return BLUETOOTH_ERROR_INTERNAL;
2738 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2740 for (i = 0; i < (m_data->data_len) + 2; i++) {
2741 g_variant_builder_add(builder, "y", m_data->data[i]);
2744 val = g_variant_new("(ay)", builder);
2746 _bt_send_event(BT_ADAPTER_EVENT,
2747 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2750 BT_INFO("Set manufacturer data");
2752 g_variant_builder_unref(builder);
2753 g_variant_unref(result);
2755 return BLUETOOTH_ERROR_NONE;
2759 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2761 int result = BLUETOOTH_ERROR_NONE;
2762 bt_service_alarm_t *alarm = NULL;
2764 if(!call_back || !alarm_id)
2765 return BLUETOOTH_ERROR_INVALID_PARAM;
2767 if (!alarm_mgr.is_alarm_initialized) {
2768 result = alarmmgr_init("bt-service");
2770 BT_ERR("Failed to initialize alarm = %d",result);
2771 result = BLUETOOTH_ERROR_INTERNAL;
2774 result = alarmmgr_set_cb(alarm_cb, NULL);
2776 BT_ERR("Failed to set the callback = %d",result);
2777 result = BLUETOOTH_ERROR_INTERNAL;
2780 alarm_mgr.is_alarm_initialized = TRUE;
2783 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2785 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2787 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2790 BT_ERR("Failed to create alarm error = %d",result);
2791 result = BLUETOOTH_ERROR_INTERNAL;
2795 alarm->alarm_id = *alarm_id;
2796 alarm->callback = call_back;
2797 alarm->user_data = user_data;
2799 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2800 result = BLUETOOTH_ERROR_NONE;
2805 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2808 bt_service_alarm_t *p_data;
2809 bt_set_alarm_cb callback = NULL;
2810 void *user_data = NULL;
2812 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2813 GINT_TO_POINTER(alarm_id), compare_alarm);
2817 p_data = (bt_service_alarm_t *)node->data;
2818 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2824 callback = p_data->callback;
2825 user_data = p_data->user_data;
2829 callback(alarm_id, user_data);
2834 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2837 bt_service_alarm_t *p_data;
2838 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2841 alarmmgr_remove_alarm(alarm_id);
2842 p_data = (bt_service_alarm_t *)list->data;
2843 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2850 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2852 alarm_id_t alarm_id = (alarm_id_t)data;
2853 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2855 if (p_data->alarm_id == alarm_id) {
2861 static void alarm_data_free(void *data)
2863 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2868 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2871 int function = (int)user_data;
2874 case BT_ENABLE_ADAPTER:
2875 result = _bt_enable_adapter();
2876 if (result != BLUETOOTH_ERROR_NONE) {
2877 BT_ERR("_bt_enable_adapter is failed");
2878 /* Send enabled event to API */
2879 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
2880 g_variant_new("(i)", result));
2883 case BT_DISABLE_ADAPTER:
2884 result = _bt_disable_adapter();
2885 if (result != BLUETOOTH_ERROR_NONE) {
2886 BT_ERR("_bt_disable_adapter is failed");
2887 /* Send disabled event to API */
2888 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
2889 g_variant_new("(i)", result));
2893 BT_ERR("function is NOT matched");
2900 int _bt_adapter_request_delayed(int function)
2902 bt_status_t status = _bt_adapter_get_status();
2903 bt_le_status_t le_status = _bt_adapter_get_le_status();
2906 case BT_ENABLE_ADAPTER:
2907 if (status == BT_ACTIVATING) {
2908 BT_ERR("Enabling in progress");
2909 return BLUETOOTH_ERROR_IN_PROGRESS;
2912 if (status == BT_ACTIVATED) {
2913 BT_ERR("Already enabled");
2914 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
2917 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
2918 BT_ERR("Disabling in progress");
2919 return BLUETOOTH_ERROR_DEVICE_BUSY;
2923 case BT_DISABLE_ADAPTER:
2924 if (status == BT_DEACTIVATING) {
2925 BT_DBG("Disabling in progress");
2926 return BLUETOOTH_ERROR_IN_PROGRESS;
2929 if (status == BT_DEACTIVATED) {
2930 BT_DBG("Already disabled");
2931 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2936 BT_ERR("function is NOT matched");
2937 return BLUETOOTH_ERROR_INTERNAL;
2940 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
2942 return BLUETOOTH_ERROR_NONE;
2946 int _bt_get_enable_timer_id(void)