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;
81 static gboolean is_recovery_mode;
83 static uint status_reg_id;
85 #define BT_CORE_NAME "org.projectx.bt_core"
86 #define BT_CORE_PATH "/org/projectx/bt_core"
87 #define BT_CORE_INTERFACE "org.projectx.btcore"
89 #define BT_DISABLE_TIME 500 /* 500 ms */
91 static int alarm_cb(alarm_id_t alarm_id, void* user_param);
92 static void alarm_data_free(void *data);
94 GDBusProxy *_bt_init_core_proxy(void)
97 GDBusConnection *conn;
99 conn = _bt_gdbus_get_system_gconn();
103 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
118 static GDBusProxy *__bt_get_core_proxy(void)
120 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
123 static gboolean __bt_is_factory_test_mode(void)
127 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
128 BT_ERR("Get the DUT Mode fail");
133 BT_INFO("DUT Test Mode !!");
140 static gboolean __bt_timeout_handler(gpointer user_data)
142 int result = BLUETOOTH_ERROR_NONE;
146 /* Take current time */
148 time_diff = difftime(current_time, visible_timer.start_time);
150 /* Send event to application */
151 _bt_send_event(BT_ADAPTER_EVENT,
152 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
153 g_variant_new("(in)", result, time_diff));
155 if (visible_timer.timeout <= time_diff) {
156 g_source_remove(visible_timer.event_id);
157 visible_timer.event_id = 0;
158 visible_timer.timeout = 0;
160 #ifndef TIZEN_PROFILE_WEARABLE
161 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
162 BT_ERR("Set vconf failed\n");
170 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
173 int result = BLUETOOTH_ERROR_NONE;
176 if (alarm_id != visible_timer.alarm_id)
179 if (visible_timer.event_id) {
180 _bt_send_event(BT_ADAPTER_EVENT,
181 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
182 g_variant_new("(in)", result, timeout));
183 g_source_remove(visible_timer.event_id);
184 visible_timer.event_id = 0;
185 visible_timer.timeout = 0;
187 #ifndef TIZEN_PROFILE_WEARABLE
188 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
189 BT_ERR("Set vconf failed\n");
192 /* Switch Off visibility in Bluez */
193 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
194 visible_timer.alarm_id = 0;
198 static void __bt_visibility_alarm_remove()
200 if (visible_timer.event_id > 0) {
201 g_source_remove(visible_timer.event_id);
202 visible_timer.event_id = 0;
205 if (visible_timer.alarm_id > 0) {
206 _bt_service_remove_alarm(visible_timer.alarm_id);
207 visible_timer.alarm_id = 0;
211 int __bt_set_visible_time(int timeout)
216 __bt_visibility_alarm_remove();
218 visible_timer.timeout = timeout;
220 #ifndef TIZEN_PROFILE_WEARABLE
221 #ifdef TIZEN_DPM_ENABLE
222 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
224 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
225 BT_ERR("Set vconf failed");
226 #ifdef TIZEN_DPM_ENABLE
233 return BLUETOOTH_ERROR_NONE;
235 result = _bt_service_set_alarm(visible_timer.timeout,
236 __bt_visibility_alarm_cb, NULL, &alarm_id);
237 if (result != BLUETOOTH_ERROR_NONE)
238 return BLUETOOTH_ERROR_INTERNAL;
239 visible_timer.alarm_id = alarm_id;
240 /* Take start time */
241 time(&(visible_timer.start_time));
242 visible_timer.event_id = g_timeout_add_seconds(1,
243 __bt_timeout_handler, NULL);
245 return BLUETOOTH_ERROR_NONE;
248 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
255 ret_if(value == NULL);
258 dev->service_index = 0;
260 g_variant_get(value, "as", &iter);
261 while (g_variant_iter_loop(iter, "s", &uuid)) {
262 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
263 parts = g_strsplit(uuid, "-", -1);
265 if (parts == NULL || parts[0] == NULL) {
270 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
273 dev->service_index++;
276 g_variant_iter_free(iter);
279 static int __bt_get_bonded_device_info(gchar *device_path,
280 bluetooth_device_info_t *dev_info)
282 GError *error = NULL;
283 GDBusProxy *device_proxy;
284 gchar *address = NULL;
287 unsigned int cod = 0;
289 gboolean trust = FALSE;
290 gboolean paired = FALSE;
291 guchar connected = 0;
292 GByteArray *manufacturer_data = NULL;
294 GDBusConnection *conn;
296 GVariantIter *property_iter;
300 GVariantIter *char_value_iter;
302 BT_CHECK_PARAMETER(device_path, return);
303 BT_CHECK_PARAMETER(dev_info, return);
305 conn = _bt_gdbus_get_system_gconn();
306 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
308 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
312 BT_PROPERTIES_INTERFACE,
315 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
317 result = g_dbus_proxy_call_sync(device_proxy,
319 g_variant_new("(s)", BT_DEVICE_INTERFACE),
320 G_DBUS_CALL_FLAGS_NONE,
326 BT_ERR("Error occured in Proxy call");
328 BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
329 g_clear_error(&error);
331 g_object_unref(device_proxy);
332 return BLUETOOTH_ERROR_INTERNAL;
335 g_object_unref(device_proxy);
337 g_variant_get(result, "(a{sv})", &property_iter);
339 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
340 if (!g_strcmp0(key, "Paired")) {
341 paired = g_variant_get_boolean(value);
342 } else if (!g_strcmp0(key, "Address")) {
343 g_variant_get(value, "s", &address);
344 } else if (!g_strcmp0(key, "Alias")) {
345 g_variant_get(value, "s", &alias);
346 } else if (!g_strcmp0(key, "Name")) {
347 g_variant_get(value, "s", &name);
348 } else if (!g_strcmp0(key, "Class")) {
349 cod = g_variant_get_uint32(value);
350 } else if (!g_strcmp0(key, "Connected")) {
351 connected = g_variant_get_byte(value);
352 } else if (!g_strcmp0(key, "Trusted")) {
353 trust = g_variant_get_boolean(value);
354 } else if (!g_strcmp0(key, "RSSI")) {
355 rssi = g_variant_get_int16(value);
356 } else if (!g_strcmp0(key, "UUIDs")) {
357 __bt_get_service_list(value, dev_info);
358 } else if (!g_strcmp0(key, "LegacyManufacturerDataLen")) {
359 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
360 } else if (!g_strcmp0(key, "LegacyManufacturerData")) {
361 manufacturer_data = g_byte_array_new();
362 g_variant_get(value, "ay", &char_value_iter);
363 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
364 g_byte_array_append(manufacturer_data, &char_value, 1);
366 g_variant_iter_free(char_value_iter);
368 if (manufacturer_data) {
369 if (manufacturer_data->len > 0) {
370 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
371 manufacturer_data->len);
374 g_byte_array_free(manufacturer_data, TRUE);
377 g_variant_iter_free(property_iter);
379 BT_DBG("trust: %d, paired: %d", trust, paired);
381 g_variant_unref(result);
383 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, alias ? alias : 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;
410 void _bt_set_discovery_status(gboolean mode)
412 is_discovering = mode;
415 void _bt_set_cancel_by_user(gboolean value)
417 cancel_by_user = value;
420 gboolean _bt_get_cancel_by_user(void)
422 return cancel_by_user;
425 void _bt_adapter_set_status(bt_status_t status)
427 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
428 adapter_status = status;
431 bt_status_t _bt_adapter_get_status(void)
433 return adapter_status;
436 void _bt_adapter_set_le_status(bt_le_status_t status)
438 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
439 adapter_le_status = status;
442 bt_le_status_t _bt_adapter_get_le_status(void)
444 return adapter_le_status;
448 void _bt_set_le_intended_status(gboolean value)
450 is_le_intended = value;
453 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
455 char *phone_name = NULL;
461 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
462 phone_name = vconf_keynode_get_str(node);
464 if (phone_name && strlen(phone_name) != 0) {
465 if (!g_utf8_validate(phone_name, -1,
466 (const char **)&ptr))
469 BT_INFO("device_name is changed to %s", phone_name);
470 _bt_set_local_name(phone_name);
472 BT_ERR("phone_name is NOT valid");
475 BT_ERR("vconf type is NOT string");
479 static void __bt_set_local_name(void)
481 bluetooth_device_name_t local_name;
482 char *phone_name = NULL;
486 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
487 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
488 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
493 if (strlen(phone_name) != 0) {
494 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
497 _bt_set_local_name(phone_name);
503 static int __bt_set_enabled(void)
505 int adapter_status = BT_ADAPTER_DISABLED;
506 int result = BLUETOOTH_ERROR_NONE;
509 BT_DBG("g_source is removed");
510 g_source_remove(timer_id);
514 _bt_check_adapter(&adapter_status);
516 if (adapter_status == BT_ADAPTER_DISABLED) {
517 BT_ERR("Bluetoothd is not running");
518 return BLUETOOTH_ERROR_INTERNAL;
521 #ifdef TIZEN_PROFILE_MOBILE || defined(TIZEN_PROFILE_IVI)
522 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
523 if (_bt_set_discoverable_mode(
524 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
525 BT_ERR("Set connectable mode failed");
528 if (_bt_set_discoverable_mode(
529 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
530 BT_ERR("Fail to set discoverable mode");
534 /* Update Bluetooth Status to notify other modules */
535 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
536 BT_ERR("Set vconf failed\n");
538 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
539 BT_ERR("Set vconf failed\n");
541 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
542 EVT_VAL_BT_ON) != ES_R_OK)
543 BT_ERR("Fail to set value");
545 /* Send enabled event to API */
546 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
547 g_variant_new("(i)", result));
549 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
550 _bt_audio_start_auto_connect(FALSE);
553 __bt_set_local_name();
554 _bt_set_discovery_status(FALSE);
556 return BLUETOOTH_ERROR_NONE;
559 void _bt_set_disabled(int result)
561 int power_off_status = 0;
564 int pm_ignore_mode = 0;
566 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
567 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
569 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
571 /* Update the vconf BT status in normal Deactivation case only */
572 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
573 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
575 BT_DBG("Update vconf for BT normal Deactivation");
577 if (result == BLUETOOTH_ERROR_TIMEOUT)
578 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
579 BT_ERR("Set vconf failed");
581 /* Update Bluetooth Status to notify other modules */
582 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
583 BT_ERR("Set vconf failed");
585 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
586 EVT_VAL_BT_OFF) != ES_R_OK)
587 BT_ERR("Fail to set value");
590 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
591 BT_ERR("Set vconf failed\n");
593 _bt_cancel_queued_transfers();
594 _bt_adapter_set_status(BT_DEACTIVATED);
595 _bt_set_discovery_status(FALSE);
597 #ifndef USB_BLUETOOTH
598 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
600 /* Send disabled event */
601 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
602 g_variant_new("(i)", result));
603 #ifndef USB_BLUETOOTH
607 BT_INFO("Adapter disabled");
610 static int __bt_set_le_enabled(void)
613 int result = BLUETOOTH_ERROR_NONE;
616 /* Update Bluetooth Status to notify other modules */
617 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
618 BT_ERR("Set vconf failed\n");
620 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
621 EVT_VAL_BT_LE_ON) != ES_R_OK)
622 BT_ERR("Fail to set value");
624 /* Send enabled event to API */
626 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
627 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
629 status = _bt_adapter_get_status();
630 if (status == BT_DEACTIVATED) {
631 BT_INFO("BREDR is off, turn off PSCAN");
632 _bt_set_connectable(FALSE);
634 if (le_timer_id > 0) {
635 g_source_remove(le_timer_id);
639 /* Send enabled event to API */
640 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
641 g_variant_new("(i)", result));
643 __bt_set_local_name();
646 return BLUETOOTH_ERROR_NONE;
649 void _bt_set_le_disabled(int result)
651 int power_off_status;
654 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
655 BT_DBG("ret : %d", ret);
656 BT_DBG("power_off_status : %d", power_off_status);
658 /* Update Bluetooth Status to notify other modules */
659 BT_DBG("Update vconf for BT LE normal Deactivation");
660 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
661 BT_ERR("Set vconf failed\n");
662 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
664 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
665 EVT_VAL_BT_LE_OFF) != ES_R_OK)
666 BT_ERR("Fail to set value");
668 /* Send disabled event */
669 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
670 g_variant_new("(i)", result));
673 void *_bt_get_adapter_agent(void)
675 return adapter_agent;
678 int _bt_enable_core(void)
682 GError *error = NULL;
684 proxy = __bt_get_core_proxy();
685 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
687 /* Clean up the process */
688 result = g_dbus_proxy_call_sync(proxy,
691 G_DBUS_CALL_FLAGS_NONE,
698 BT_ERR("Bt core call failed(Error: %s)", error->message);
699 g_clear_error(&error);
701 BT_ERR("Bt core call failed");
702 return BLUETOOTH_ERROR_INTERNAL;
705 g_variant_unref(result);
706 return BLUETOOTH_ERROR_NONE;
709 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
710 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
712 gboolean flight_mode = FALSE;
713 int power_saving_mode = 0;
716 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
717 type = vconf_keynode_get_type(node);
718 if (type == VCONF_TYPE_BOOL) {
719 flight_mode = vconf_keynode_get_bool(node);
720 if (flight_mode != TRUE) {
721 BT_ERR("Ignore the event");
724 } else if (type == VCONF_TYPE_INT) {
725 power_saving_mode = vconf_keynode_get_int(node);
726 if (power_saving_mode != 2) {
727 BT_ERR("Ignore the event");
731 BT_ERR("Invaild vconf key type : %d", type);
739 void _bt_service_register_vconf_handler(void)
743 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
744 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
745 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
746 BT_ERR("Unable to register key handler");
748 BT_DBG("Telephony is disabled");
751 #ifndef TIZEN_PROFILE_WEARABLE
752 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
753 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
754 BT_ERR("Unable to register key handler");
758 void _bt_service_unregister_vconf_handler(void)
762 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
763 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
764 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
767 #ifndef TIZEN_PROFILE_WEARABLE
768 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
769 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
773 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
775 const char *bt_status = NULL;
776 const char *bt_le_status = NULL;
777 BT_DBG("bt state set event(%s) received", event_name);
779 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
780 BT_DBG("bt_state: (%s)", bt_status);
782 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
783 BT_DBG("bt_state: (%s)", bt_le_status);
786 void _bt_handle_adapter_added(void)
790 bt_le_status_t le_status;
794 BT_DBG("g_source is removed");
795 g_source_remove(timer_id);
800 status = _bt_adapter_get_status();
801 le_status = _bt_adapter_get_le_status();
802 BT_INFO("status : %d", status);
803 BT_INFO("le_status : %d", le_status);
805 #ifndef USB_BLUETOOTH
806 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
807 if (!adapter_agent) {
808 BT_ERR("Fail to register agent");
812 if (adapter_agent == NULL) {
813 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
814 if (!adapter_agent) {
815 BT_ERR("Fail to register agent");
821 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
822 BT_ERR("Fail to register media player");
824 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
825 BT_ERR("Fail to init obex server");
827 #ifdef TIZEN_BT_PAN_NAP_ENABLED
828 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
829 BT_ERR("Fail to activate network");
832 /* add the vconf noti handler */
833 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
834 __bt_phone_name_changed_cb, NULL);
836 BT_ERR("Unable to register key handler");
838 if (le_status == BT_LE_ACTIVATING ||
839 status == BT_ACTIVATING) {
840 __bt_set_le_enabled();
841 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
844 if (status == BT_ACTIVATING) {
846 _bt_adapter_set_status(BT_ACTIVATED);
850 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
851 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
852 BT_ERR("Fail to register system event");
856 void _bt_handle_adapter_removed(void)
860 _bt_adapter_set_status(BT_DEACTIVATED);
862 __bt_visibility_alarm_remove();
864 if (alarm_mgr.is_alarm_initialized == TRUE) {
866 alarm_mgr.is_alarm_initialized = FALSE;
867 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
868 alarm_mgr.g_alarm_list = NULL;
871 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
872 _bt_audio_stop_auto_connect();
875 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
876 (vconf_callback_fn)__bt_phone_name_changed_cb);
878 ERR("vconf_ignore_key_changed failed\n");
880 #ifndef USB_BLUETOOTH
881 _bt_destroy_agent(adapter_agent);
882 adapter_agent = NULL;
884 if (is_recovery_mode == TRUE) {
885 /* Send disabled event */
886 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
888 /* Will recover BT by bt-core, so set the mode as activating */
889 _bt_adapter_set_status(BT_ACTIVATING);
890 is_recovery_mode = FALSE;
892 _bt_reliable_terminate_service(NULL);
895 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
898 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
899 BT_ERR("Fail to unregister system event");
902 static gboolean __bt_enable_timeout_cb(gpointer user_data)
906 GError *error = NULL;
910 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
912 BT_ERR("EnableAdapter is failed");
914 proxy = __bt_get_core_proxy();
918 /* Clean up the process */
919 result = g_dbus_proxy_call_sync(proxy,
922 G_DBUS_CALL_FLAGS_NONE,
929 BT_ERR("Bt core call failed(Error: %s)", error->message);
930 g_clear_error(&error);
932 BT_ERR("Bt core call failed");
937 g_variant_unref(result);
938 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
940 #ifndef USB_BLUETOOTH
941 _bt_terminate_service(NULL);
947 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
951 GError *error = NULL;
955 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
957 BT_ERR("EnableAdapterLE is failed");
959 proxy = __bt_get_core_proxy();
963 /* Clean up the process */
964 result = g_dbus_proxy_call_sync(proxy,
967 G_DBUS_CALL_FLAGS_NONE,
974 BT_ERR("Bt core call failed(Error: %s)", error->message);
975 g_clear_error(&error);
977 BT_ERR("Bt core call failed");
981 g_variant_unref(result);
982 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
984 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
986 if (_bt_adapter_get_status() == BT_DEACTIVATED)
987 _bt_terminate_service(NULL);
992 void _bt_adapter_start_le_enable_timer(void)
994 if (le_timer_id > 0) {
995 g_source_remove(le_timer_id);
999 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1000 __bt_enable_le_timeout_cb, NULL);
1005 void _bt_adapter_start_enable_timer(void)
1008 g_source_remove(timer_id);
1012 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1013 __bt_enable_timeout_cb, NULL);
1019 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
1024 _bt_adapter_set_status(BT_ACTIVATED);
1030 int _bt_enable_adapter_check_status(void)
1032 bt_status_t status = _bt_adapter_get_status();
1033 bt_le_status_t le_status = _bt_adapter_get_le_status();
1037 if (status == BT_ACTIVATING) {
1038 BT_ERR("Enabling in progress");
1039 return BLUETOOTH_ERROR_IN_PROGRESS;
1042 if (status == BT_ACTIVATED) {
1043 BT_ERR("Already enabled");
1044 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1047 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1048 BT_ERR("Disabling in progress");
1049 return BLUETOOTH_ERROR_DEVICE_BUSY;
1052 return BLUETOOTH_ERROR_NONE;
1055 int _bt_enable_adapter(void)
1058 GError *error = NULL;
1060 GVariant *result = NULL;
1061 bt_status_t status = _bt_adapter_get_status();
1062 bt_le_status_t le_status = _bt_adapter_get_le_status();
1066 if (status == BT_ACTIVATING) {
1067 BT_ERR("Enabling in progress");
1068 return BLUETOOTH_ERROR_IN_PROGRESS;
1071 if (status == BT_ACTIVATED) {
1072 BT_ERR("Already enabled");
1073 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1076 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1077 BT_ERR("Disabling in progress");
1078 return BLUETOOTH_ERROR_DEVICE_BUSY;
1081 _bt_adapter_set_status(BT_ACTIVATING);
1085 int adapter_status = BT_ADAPTER_DISABLED;
1087 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1088 BT_ERR("Set vconf failed");
1090 _bt_check_adapter(&adapter_status);
1091 if (adapter_status == BT_ADAPTER_ENABLED) {
1092 g_idle_add(__bt_adapter_enabled_cb, NULL);
1093 _bt_adapter_start_enable_timer();
1094 return BLUETOOTH_ERROR_NONE;
1099 proxy = __bt_get_core_proxy();
1101 return BLUETOOTH_ERROR_INTERNAL;
1103 if (le_status == BT_LE_ACTIVATED) {
1104 BT_INFO("LE Already enabled. Just turn on PSCAN");
1105 ret = _bt_set_connectable(TRUE);
1106 if (ret == BLUETOOTH_ERROR_NONE)
1107 _bt_adapter_set_status(BT_ACTIVATED);
1109 return BLUETOOTH_ERROR_INTERNAL;
1112 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1114 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1117 BT_ERR("EnableAdapterLe failed: %s", error->message);
1118 _bt_adapter_set_status(BT_DEACTIVATED);
1119 g_clear_error(&error);
1121 result = g_dbus_proxy_call_sync(proxy,
1124 G_DBUS_CALL_FLAGS_NONE,
1129 if (error != NULL) {
1130 BT_ERR("Bt core call failed(Error: %s)", error->message);
1131 g_clear_error(&error);
1133 g_variant_unref(result);
1134 #ifndef USB_BLUETOOTH
1135 /* Terminate myself */
1136 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1138 return BLUETOOTH_ERROR_INTERNAL;
1140 g_variant_unref(result);
1141 if (le_status == BT_LE_ACTIVATED)
1144 _bt_adapter_start_enable_timer();
1146 return BLUETOOTH_ERROR_NONE;
1149 static gboolean __bt_disconnect_all(void)
1152 GDBusConnection *conn;
1153 GDBusProxy *dev_proxy;
1154 gboolean ret = FALSE;
1156 GError *error = NULL;
1157 GArray *device_list;
1158 bluetooth_device_info_t info;
1160 char *device_path = NULL;
1161 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1166 conn = _bt_gdbus_get_system_gconn();
1168 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1170 if (_bt_get_bonded_devices(&device_list)
1171 != BLUETOOTH_ERROR_NONE) {
1172 g_array_free(device_list, TRUE);
1176 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1178 for (i = 0; i < size; i++) {
1180 info = g_array_index(device_list,
1181 bluetooth_device_info_t, i);
1183 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1184 BT_DBG("Found Connected device");
1185 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1186 device_path = _bt_get_device_object_path(address);
1187 if (device_path == NULL)
1190 BT_DBG("Disconnecting : %s", device_path);
1192 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1196 BT_DEVICE_INTERFACE,
1199 if (dev_proxy == NULL)
1202 result = g_dbus_proxy_call_sync(dev_proxy,
1205 G_DBUS_CALL_FLAGS_NONE,
1211 if (error != NULL) {
1212 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1213 g_clear_error(&error);
1215 BT_ERR("Disconnect call failed");
1216 g_object_unref(dev_proxy);
1220 g_variant_unref(result);
1221 g_object_unref(dev_proxy);
1225 g_array_free(device_list, TRUE);
1231 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1234 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1240 int _bt_disable_cb(void)
1248 GError *error = NULL;
1250 _bt_adapter_set_status(BT_DEACTIVATING);
1252 bt_le_status_t le_status;
1253 le_status = _bt_adapter_get_le_status();
1254 BT_DBG("le_status : %d", le_status);
1255 if (le_status == BT_LE_ACTIVATED) {
1256 BT_INFO("LE is enabled. Just turn off PSCAN");
1258 if (_bt_is_discovering())
1259 _bt_cancel_discovery();
1261 if (_bt_is_connectable() == FALSE) {
1262 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1264 ret = _bt_set_connectable(FALSE);
1265 if (ret != BLUETOOTH_ERROR_NONE) {
1266 BT_ERR("_bt_set_connectable fail!");
1267 _bt_adapter_set_status(BT_ACTIVATED);
1268 return BLUETOOTH_ERROR_INTERNAL;
1273 proxy = __bt_get_core_proxy();
1274 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1276 result = g_dbus_proxy_call_sync(proxy,
1279 G_DBUS_CALL_FLAGS_NONE,
1285 if (error != NULL) {
1286 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1287 g_clear_error(&error);
1289 BT_ERR("Failed to DisableAdapter");
1290 _bt_adapter_set_status(BT_ACTIVATED);
1291 return BLUETOOTH_ERROR_INTERNAL;
1294 g_variant_unref(result);
1295 return BLUETOOTH_ERROR_NONE;
1298 int _bt_disable_adapter_check_status(void)
1300 bt_status_t status = _bt_adapter_get_status();
1304 if (status == BT_DEACTIVATING) {
1305 BT_DBG("Disabling in progress");
1306 return BLUETOOTH_ERROR_IN_PROGRESS;
1309 if (status == BT_DEACTIVATED) {
1310 BT_DBG("Already disabled");
1311 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1314 return BLUETOOTH_ERROR_NONE;
1317 int _bt_disable_adapter(void)
1322 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1323 BT_DBG("Disabling in progress");
1324 return BLUETOOTH_ERROR_IN_PROGRESS;
1327 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1328 BT_DBG("Already disabled");
1329 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1333 g_source_remove(timer_id);
1337 __bt_disconnect_all();
1338 ret = _bt_disable_cb();
1344 int _bt_recover_adapter(void)
1349 GError *error = NULL;
1351 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1352 BT_ERR("Disabling in progress");
1353 return BLUETOOTH_ERROR_IN_PROGRESS;
1356 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1357 BT_ERR("Already disabled");
1358 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1361 _bt_adapter_set_status(BT_DEACTIVATING);
1363 proxy = __bt_get_core_proxy();
1364 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1366 result = g_dbus_proxy_call_sync(proxy,
1369 G_DBUS_CALL_FLAGS_NONE,
1375 if (error != NULL) {
1376 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1377 g_clear_error(&error);
1379 BT_ERR("Failed to RecoverAdapter");
1380 return BLUETOOTH_ERROR_INTERNAL;
1383 is_recovery_mode = TRUE;
1385 g_variant_unref(result);
1386 __bt_disconnect_all();
1389 return BLUETOOTH_ERROR_NONE;
1392 int _bt_reset_adapter(void)
1396 GError *error = NULL;
1400 proxy = __bt_get_core_proxy();
1402 return BLUETOOTH_ERROR_INTERNAL;
1404 result = g_dbus_proxy_call_sync(proxy,
1407 G_DBUS_CALL_FLAGS_NONE,
1413 if (error != NULL) {
1414 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1415 g_clear_error(&error);
1417 BT_ERR("Failed to ResetAdapter");
1418 return BLUETOOTH_ERROR_INTERNAL;
1421 g_variant_unref(result);
1422 /* Terminate myself */
1423 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1424 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1426 return BLUETOOTH_ERROR_NONE;
1430 int _bt_check_adapter(int *status)
1433 char *adapter_path = NULL;
1435 BT_CHECK_PARAMETER(status, return);
1437 *status = BT_ADAPTER_DISABLED;
1439 adapter_path = _bt_get_adapter_path();
1442 if (adapter_path != NULL)
1443 *status = BT_ADAPTER_ENABLED;
1445 g_free(adapter_path);
1446 return BLUETOOTH_ERROR_NONE;
1449 int _bt_check_adapter(int *status)
1452 GError *error = NULL;
1455 gboolean powered = FALSE;
1457 BT_CHECK_PARAMETER(status, return);
1459 *status = BT_ADAPTER_DISABLED;
1461 proxy = _bt_get_adapter_properties_proxy();
1462 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1464 result = g_dbus_proxy_call_sync(proxy,
1466 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1468 G_DBUS_CALL_FLAGS_NONE,
1474 BT_ERR("Failed to get local address");
1475 if (error != NULL) {
1476 BT_ERR("Failed to get local address (Error: %s)", error->message);
1477 g_clear_error(&error);
1479 return BLUETOOTH_ERROR_INTERNAL;
1482 g_variant_get(result, "(v)", &temp);
1483 powered = g_variant_get_boolean(temp);
1484 BT_DBG("powered: %d", powered);
1487 *status = BT_ADAPTER_ENABLED;
1489 g_variant_unref(result);
1490 g_variant_unref(temp);
1491 return BLUETOOTH_ERROR_NONE;
1495 int _bt_enable_adapter_le(void)
1499 GError *error = NULL;
1500 bt_status_t status = _bt_adapter_get_status();
1501 bt_le_status_t le_status = _bt_adapter_get_le_status();
1504 if (le_status == BT_LE_ACTIVATING) {
1505 BT_ERR("Enabling in progress");
1506 return BLUETOOTH_ERROR_IN_PROGRESS;
1509 if (le_status == BT_LE_ACTIVATED) {
1510 BT_ERR("Already enabled");
1511 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1514 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1515 BT_ERR("Disabling in progress");
1516 return BLUETOOTH_ERROR_DEVICE_BUSY;
1519 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1521 proxy = __bt_get_core_proxy();
1522 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1524 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1526 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1529 BT_ERR("EnableAdapterLe failed: %s", error->message);
1530 _bt_adapter_set_le_status(BT_DEACTIVATED);
1531 g_clear_error(&error);
1533 /* Clean up the process */
1534 result = g_dbus_proxy_call_sync(proxy,
1537 G_DBUS_CALL_FLAGS_NONE,
1543 BT_ERR("Bt core call failed");
1545 BT_ERR("EnableAdapterLE Failed %s", error->message);
1546 g_clear_error(&error);
1549 g_variant_unref(result);
1550 /* Terminate myself */
1551 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1552 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1553 return BLUETOOTH_ERROR_INTERNAL;
1557 g_variant_unref(result);
1559 _bt_adapter_start_le_enable_timer();
1561 if (status == BT_ACTIVATED) {
1562 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1563 __bt_set_le_enabled();
1565 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1567 return BLUETOOTH_ERROR_NONE;
1570 int _bt_disable_adapter_le(void)
1574 bt_le_status_t bt_le_state;
1576 GError *error = NULL;
1578 bt_le_state = _bt_adapter_get_le_status();
1579 if (bt_le_state == BT_LE_DEACTIVATING) {
1580 BT_DBG("Disabling in progress");
1581 return BLUETOOTH_ERROR_IN_PROGRESS;
1584 if (bt_le_state == BT_LE_DEACTIVATED) {
1585 BT_DBG("Already disabled");
1586 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1589 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1591 proxy = __bt_get_core_proxy();
1593 return BLUETOOTH_ERROR_INTERNAL;
1595 result = g_dbus_proxy_call_sync(proxy,
1598 G_DBUS_CALL_FLAGS_NONE,
1604 if (error != NULL) {
1605 BT_ERR("Bt core call failed (Error: %s)", error->message);
1606 g_clear_error(&error);
1608 BT_ERR("Bt core call failed");
1609 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1610 return BLUETOOTH_ERROR_INTERNAL;
1613 g_variant_unref(result);
1614 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1615 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1617 return BLUETOOTH_ERROR_NONE;
1620 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1624 GError *error = NULL;
1625 const char *address;
1629 BT_CHECK_PARAMETER(local_address, return);
1631 proxy = _bt_get_adapter_properties_proxy();
1632 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1634 result = g_dbus_proxy_call_sync(proxy,
1636 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1638 G_DBUS_CALL_FLAGS_NONE,
1644 BT_ERR("Failed to get local address");
1645 if (error != NULL) {
1646 BT_ERR("Failed to get local address (Error: %s)", error->message);
1647 g_clear_error(&error);
1649 return BLUETOOTH_ERROR_INTERNAL;
1652 g_variant_get(result, "(v)", &temp);
1653 address = g_variant_get_string(temp, NULL);
1654 BT_DBG("Address:%s", address);
1657 _bt_convert_addr_string_to_type(local_address->addr, address);
1659 return BLUETOOTH_ERROR_INTERNAL;
1661 g_variant_unref(result);
1662 g_variant_unref(temp);
1663 return BLUETOOTH_ERROR_NONE;
1666 int _bt_get_local_version(bluetooth_version_t *local_version)
1669 const char *ver = NULL;
1671 int ret = BLUETOOTH_ERROR_NONE;
1675 BT_CHECK_PARAMETER(local_version, return);
1677 GError *error = NULL;
1679 proxy = _bt_get_adapter_properties_proxy();
1680 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1682 result = g_dbus_proxy_call_sync(proxy,
1684 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1686 G_DBUS_CALL_FLAGS_NONE,
1692 if (error != NULL) {
1693 BT_ERR("Failed to get local version (Error: %s)", error->message);
1694 g_clear_error(&error);
1696 BT_ERR("Failed to get local version");
1697 return BLUETOOTH_ERROR_INTERNAL;
1700 g_variant_get(result, "(v)", &temp);
1701 ver = g_variant_get_string(temp, NULL);
1702 BT_DBG("VERSION: %s", ver);
1704 if (ver && (strlen(ver) > 0)) {
1705 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1706 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1709 g_strlcpy(local_version->version, ver,
1710 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1713 ret = BLUETOOTH_ERROR_INTERNAL;
1716 g_variant_unref(result);
1717 g_variant_unref(temp);
1721 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1724 const char *name = NULL;
1726 int ret = BLUETOOTH_ERROR_NONE;
1729 GError *error = NULL;
1731 BT_CHECK_PARAMETER(local_name, return);
1733 proxy = _bt_get_adapter_properties_proxy();
1734 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1736 result = g_dbus_proxy_call_sync(proxy,
1738 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1740 G_DBUS_CALL_FLAGS_NONE,
1746 if (error != NULL) {
1747 BT_ERR("Failed to get local name (Error: %s)", error->message);
1748 g_clear_error(&error);
1750 BT_ERR("Failed to get local name");
1751 return BLUETOOTH_ERROR_INTERNAL;
1754 g_variant_get(result, "(v)", &temp);
1755 name = g_variant_get_string(temp, NULL);
1756 BT_DBG("LOCAL NAME:%s", name);
1758 if (name && (strlen(name) > 0)) {
1759 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1760 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1763 g_strlcpy(local_name->name, name,
1764 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1766 ret = BLUETOOTH_ERROR_INTERNAL;
1768 g_variant_unref(result);
1769 g_variant_unref(temp);
1773 int _bt_set_local_name(char *local_name)
1776 GError *error = NULL;
1780 BT_CHECK_PARAMETER(local_name, return);
1782 proxy = _bt_get_adapter_properties_proxy();
1784 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1786 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1789 result = g_dbus_proxy_call_sync(proxy,
1791 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1792 "Alias", g_variant_new("s", local_name)),
1793 G_DBUS_CALL_FLAGS_NONE,
1799 if (error != NULL) {
1800 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1801 g_clear_error(&error);
1803 BT_ERR("Failed to set Alias");
1804 return BLUETOOTH_ERROR_INTERNAL;
1807 g_variant_unref(result);
1808 return BLUETOOTH_ERROR_NONE;
1811 int _bt_is_service_used(char *service_uuid, gboolean *used)
1814 GError *error = NULL;
1815 int ret = BLUETOOTH_ERROR_NONE;
1817 GVariant *temp = NULL;
1818 GVariantIter *iter = NULL;
1822 BT_CHECK_PARAMETER(service_uuid, return);
1823 BT_CHECK_PARAMETER(used, return);
1825 proxy = _bt_get_adapter_properties_proxy();
1826 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1828 result = g_dbus_proxy_call_sync(proxy,
1830 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1832 G_DBUS_CALL_FLAGS_NONE,
1838 if (error != NULL) {
1839 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1840 g_clear_error(&error);
1842 BT_ERR("Failed to get UUIDs");
1843 return BLUETOOTH_ERROR_INTERNAL;
1846 g_variant_get(result, "(v)", &temp);
1847 g_variant_get(temp, "as", &iter);
1850 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1851 if (strcasecmp(uuid, service_uuid) == 0) {
1856 g_variant_iter_free(iter);
1857 g_variant_unref(result);
1859 BT_DBG("Service Used? %d", *used);
1864 static gboolean __bt_get_discoverable_property(void)
1867 gboolean discoverable_v;
1868 GError *error = NULL;
1872 proxy = _bt_get_adapter_properties_proxy();
1873 retv_if(proxy == NULL, FALSE);
1875 result = g_dbus_proxy_call_sync(proxy,
1877 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1879 G_DBUS_CALL_FLAGS_NONE,
1885 if (error != NULL) {
1886 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1887 g_clear_error(&error);
1889 BT_ERR("Failed to get Discoverable property");
1890 return BLUETOOTH_ERROR_INTERNAL;
1893 g_variant_get(result, "(v)", &temp);
1894 discoverable_v = g_variant_get_boolean(temp);
1895 BT_DBG("discoverable_v:%d", discoverable_v);
1897 g_variant_unref(result);
1898 g_variant_unref(temp);
1900 return discoverable_v;
1903 int _bt_get_discoverable_mode(int *mode)
1905 gboolean discoverable;
1906 unsigned int timeout;
1908 BT_CHECK_PARAMETER(mode, return);
1910 discoverable = __bt_get_discoverable_property();
1911 timeout = _bt_get_discoverable_timeout_property();
1913 if (discoverable == TRUE) {
1915 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1917 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1919 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1921 return BLUETOOTH_ERROR_NONE;
1925 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1927 int ret = BLUETOOTH_ERROR_NONE;
1930 GError *error = NULL;
1934 proxy = _bt_get_adapter_properties_proxy();
1936 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1938 #ifdef TIZEN_DPM_ENABLE
1939 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1940 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1941 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1942 return BLUETOOTH_ERROR_ACCESS_DENIED;
1944 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1945 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1946 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1947 return BLUETOOTH_ERROR_ACCESS_DENIED;
1951 switch (discoverable_mode) {
1952 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1957 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1962 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1967 return BLUETOOTH_ERROR_INVALID_PARAM;
1970 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1971 discoverable_mode, timeout);
1973 result = g_dbus_proxy_call_sync(proxy,
1975 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1976 "Connectable", g_variant_new("b", pg_scan)),
1977 G_DBUS_CALL_FLAGS_NONE,
1983 if (error != NULL) {
1984 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1985 g_clear_error(&error);
1987 BT_ERR("Failed to set connectable property");
1988 return BLUETOOTH_ERROR_INTERNAL;
1990 g_variant_unref(result);
1991 result = g_dbus_proxy_call_sync(proxy,
1993 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1994 g_variant_new("b", inq_scan)),
1995 G_DBUS_CALL_FLAGS_NONE,
2001 if (error != NULL) {
2002 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
2003 g_clear_error(&error);
2005 BT_ERR("Failed to set Discoverable property");
2006 return BLUETOOTH_ERROR_INTERNAL;
2008 g_variant_unref(result);
2009 result = g_dbus_proxy_call_sync(proxy,
2011 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
2012 "DiscoverableTimeout", g_variant_new("u", timeout)),
2013 G_DBUS_CALL_FLAGS_NONE,
2019 if (error != NULL) {
2020 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
2021 g_clear_error(&error);
2023 BT_ERR("Failed to set DiscoverableTimeout property");
2024 return BLUETOOTH_ERROR_INTERNAL;
2027 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
2030 ret = __bt_set_visible_time(timeout);
2032 g_variant_unref(result);
2037 int _bt_start_discovery(void)
2040 GError *error = NULL;
2043 if (_bt_is_discovering() == TRUE) {
2044 BT_ERR("BT is already in discovering");
2045 return BLUETOOTH_ERROR_IN_PROGRESS;
2046 } else if (_bt_is_device_creating() == TRUE) {
2047 BT_ERR("Bonding device is going on");
2048 return BLUETOOTH_ERROR_DEVICE_BUSY;
2051 proxy = _bt_get_adapter_proxy();
2052 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2054 result = g_dbus_proxy_call_sync(proxy,
2057 G_DBUS_CALL_FLAGS_NONE,
2063 if (error != NULL) {
2064 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
2065 g_clear_error(&error);
2067 BT_ERR("StartDiscovery failed");
2068 return BLUETOOTH_ERROR_INTERNAL;
2071 is_discovering = TRUE;
2072 cancel_by_user = FALSE;
2073 /* discovery status will be change in event */
2074 g_variant_unref(result);
2075 return BLUETOOTH_ERROR_NONE;
2078 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2082 GError *error = NULL;
2083 const gchar *disc_type;
2085 if (_bt_is_discovering() == TRUE) {
2086 BT_ERR("BT is already in discovering");
2087 return BLUETOOTH_ERROR_IN_PROGRESS;
2090 proxy = _bt_get_adapter_proxy();
2091 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2093 if (role == DISCOVERY_ROLE_BREDR)
2094 disc_type = "BREDR";
2095 else if (role == DISCOVERY_ROLE_LE)
2097 else if (role == DISCOVERY_ROLE_LE_BREDR)
2098 disc_type = "LE_BREDR";
2100 return BLUETOOTH_ERROR_INVALID_PARAM;
2102 result = g_dbus_proxy_call_sync(proxy,
2103 "StartCustomDiscovery",
2104 g_variant_new("s", disc_type),
2105 G_DBUS_CALL_FLAGS_NONE,
2111 if (error != NULL) {
2112 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2113 g_clear_error(&error);
2115 BT_ERR("StartCustomDiscovery failed");
2116 return BLUETOOTH_ERROR_INTERNAL;
2119 is_discovering = TRUE;
2120 cancel_by_user = FALSE;
2121 /* discovery status will be change in event */
2122 g_variant_unref(result);
2123 return BLUETOOTH_ERROR_NONE;
2126 int _bt_cancel_discovery(void)
2129 GError *error = NULL;
2132 if (_bt_is_discovering() == FALSE) {
2133 BT_ERR("BT is not in discovering");
2134 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2137 proxy = _bt_get_adapter_proxy();
2138 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2140 result = g_dbus_proxy_call_sync(proxy,
2143 G_DBUS_CALL_FLAGS_NONE,
2149 int ret = BLUETOOTH_ERROR_INTERNAL;
2150 if (error != NULL) {
2151 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2153 if (g_strrstr(error->message, "No discovery started"))
2154 ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
2156 g_clear_error(&error);
2158 BT_ERR("StopDiscovery failed");
2164 cancel_by_user = TRUE;
2165 /* discovery status will be change in event */
2166 g_variant_unref(result);
2167 return BLUETOOTH_ERROR_NONE;
2170 gboolean _bt_is_discovering(void)
2172 return is_discovering;
2175 gboolean _bt_is_connectable(void)
2178 GError *error = NULL;
2179 gboolean is_connectable = FALSE;
2183 proxy = _bt_get_adapter_properties_proxy();
2184 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2186 result = g_dbus_proxy_call_sync(proxy,
2188 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2190 G_DBUS_CALL_FLAGS_NONE,
2196 if (error != NULL) {
2197 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2198 g_clear_error(&error);
2200 BT_ERR("Failed to get connectable property");
2201 return BLUETOOTH_ERROR_INTERNAL;
2204 g_variant_get(result, "(v)", &temp);
2205 is_connectable = g_variant_get_boolean(temp);
2206 BT_DBG("discoverable_v:%d", is_connectable);
2208 g_variant_unref(result);
2209 g_variant_unref(temp);
2211 BT_INFO("Get connectable [%d]", is_connectable);
2212 return is_connectable;
2215 int _bt_set_connectable(gboolean is_connectable)
2218 GError *error = NULL;
2221 if (__bt_is_factory_test_mode()) {
2222 BT_ERR("Unable to set connectable in factory binary !!");
2223 return BLUETOOTH_ERROR_NOT_SUPPORT;
2226 proxy = _bt_get_adapter_properties_proxy();
2228 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2230 result = g_dbus_proxy_call_sync(proxy,
2232 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2233 g_variant_new("b", is_connectable)),
2234 G_DBUS_CALL_FLAGS_NONE,
2240 if (error != NULL) {
2241 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2242 g_clear_error(&error);
2244 BT_ERR("Failed to set connectable property");
2245 return BLUETOOTH_ERROR_INTERNAL;
2248 BT_INFO_C("### Set connectable [%d]", is_connectable);
2249 g_variant_unref(result);
2250 return BLUETOOTH_ERROR_NONE;
2253 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2256 gboolean discovering_v;
2257 GError *error = NULL;
2258 char *discovering_type = NULL;
2262 proxy = _bt_get_adapter_properties_proxy();
2263 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2265 if (discovery_type == DISCOVERY_ROLE_BREDR)
2266 discovering_type = "Discovering";
2267 else if (discovery_type == DISCOVERY_ROLE_LE)
2268 discovering_type = "LEDiscovering";
2270 result = g_dbus_proxy_call_sync(proxy,
2272 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2274 G_DBUS_CALL_FLAGS_NONE,
2280 if (error != NULL) {
2281 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2282 g_clear_error(&error);
2284 BT_ERR("Failed to get discovering property");
2285 return BLUETOOTH_ERROR_INTERNAL;
2288 g_variant_get(result, "(v)", &temp);
2289 discovering_v = g_variant_get_boolean(temp);
2290 BT_DBG("discoverable_v:%d", discovering_v);
2292 g_variant_unref(result);
2293 g_variant_unref(temp);
2295 return discovering_v;
2298 unsigned int _bt_get_discoverable_timeout_property(void)
2301 unsigned int timeout_v;
2302 GError *error = NULL;
2306 proxy = _bt_get_adapter_properties_proxy();
2307 retv_if(proxy == NULL, 0);
2309 result = g_dbus_proxy_call_sync(proxy,
2311 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2312 "DiscoverableTimeout"),
2313 G_DBUS_CALL_FLAGS_NONE,
2319 BT_ERR("Fail to get discoverable timeout");
2320 if (error != NULL) {
2321 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2322 g_clear_error(&error);
2327 g_variant_get(result, "(v)", &temp);
2328 timeout_v = g_variant_get_uint32(temp);
2329 BT_DBG("discoverable_v:%d", timeout_v);
2331 g_variant_unref(result);
2332 g_variant_unref(temp);
2337 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2339 bluetooth_device_info_t *dev_info;
2342 GByteArray *manufacturer_data = NULL;
2344 GVariantIter *char_value_iter;
2346 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2348 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2353 if (!g_strcmp0(key, "Address")) {
2354 const char *address = NULL;
2355 address = g_variant_get_string(value, NULL);
2356 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2358 } else if (!g_strcmp0(key, "Class")) {
2360 cod = g_variant_get_uint32(value);
2361 _bt_divide_device_class(&dev_info->device_class, cod);
2362 } else if (!g_strcmp0(key, "Name")) {
2363 const char *name = NULL;
2364 name = g_variant_get_string(value, NULL);
2365 /* If there is no Alias */
2366 if (strlen(dev_info->device_name.name) == 0) {
2367 g_strlcpy(dev_info->device_name.name, name,
2368 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2370 } else if (!g_strcmp0(key, "Alias")) {
2371 const char *alias = NULL;
2372 alias = g_variant_get_string(value, NULL);
2373 /* Overwrite the name */
2375 memset(dev_info->device_name.name, 0x00,
2376 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2377 g_strlcpy(dev_info->device_name.name, alias,
2378 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2380 } else if (!g_strcmp0(key, "IsAliasSet")) {
2381 dev_info->is_alias_set = g_variant_get_boolean(value);
2382 } else if (!g_strcmp0(key, "Connected")) {
2383 dev_info->connected = g_variant_get_byte(value);
2384 } else if (!g_strcmp0(key, "Paired")) {
2385 dev_info->paired = g_variant_get_boolean(value);
2386 } else if (!g_strcmp0(key, "Trusted")) {
2387 dev_info->trust = g_variant_get_boolean(value);
2388 } else if (!g_strcmp0(key, "RSSI")) {
2389 dev_info->rssi = g_variant_get_int16(value);
2390 } else if (!g_strcmp0(key, "UUIDs")) {
2396 dev_info->service_index = 0;
2397 g_variant_get(value, "as", &iter);
2398 while (g_variant_iter_loop(iter, "s", &uuid)) {
2399 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2400 parts = g_strsplit(uuid, "-", -1);
2402 if (parts == NULL || parts[0] == NULL) {
2407 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2412 dev_info->service_index = i;
2413 g_variant_iter_free(iter);
2414 } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
2415 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2416 } else if (strcasecmp(key, "LegacyManufacturerData") == 0) {
2417 manufacturer_data = g_byte_array_new();
2418 g_variant_get(value, "ay", &char_value_iter);
2419 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
2420 g_byte_array_append(manufacturer_data, &char_value, 1);
2422 if (manufacturer_data) {
2423 if (manufacturer_data->len > 0)
2424 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2426 g_variant_iter_free(char_value_iter);
2427 g_byte_array_free(manufacturer_data, TRUE);
2434 static void __bt_extract_device_info(GVariantIter *iter,
2437 bluetooth_device_info_t *dev_info = NULL;
2438 char *object_path = NULL;
2439 GVariantIter *interface_iter;
2440 GVariantIter *svc_iter;
2441 char *interface_str = NULL;
2443 /* Parse the signature: oa{sa{sv}}} */
2444 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2447 if (object_path == NULL)
2450 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2451 &interface_str, &svc_iter)) {
2452 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2453 BT_DBG("Found a device: %s", object_path);
2454 dev_info = __bt_parse_device_info(svc_iter);
2456 if (dev_info->paired == TRUE) {
2457 g_array_append_vals(*dev_list, dev_info,
2458 sizeof(bluetooth_device_info_t));
2462 g_free(interface_str);
2463 g_variant_iter_free(svc_iter);
2471 int _bt_get_bonded_devices(GArray **dev_list)
2474 GDBusConnection *conn;
2475 GDBusProxy *manager_proxy;
2476 GVariant *result = NULL;
2477 GVariantIter *iter = NULL;
2478 GError *error = NULL;
2480 conn = _bt_gdbus_get_system_gconn();
2481 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2483 manager_proxy = _bt_get_manager_proxy();
2484 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2486 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2488 G_DBUS_CALL_FLAGS_NONE,
2494 if (error != NULL) {
2495 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2496 g_clear_error(&error);
2498 BT_ERR("Failed to Failed to GetManagedObjects");
2499 return BLUETOOTH_ERROR_INTERNAL;
2502 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2503 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2505 __bt_extract_device_info(iter, dev_list);
2506 g_variant_iter_free(iter);
2507 g_variant_unref(result);
2510 return BLUETOOTH_ERROR_NONE;
2513 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2516 GDBusConnection *conn;
2517 GDBusProxy *manager_proxy;
2518 GVariant *result = NULL;
2519 GVariant *result1 = NULL;
2520 GVariantIter *iter = NULL;
2521 GError *error = NULL;
2522 char *object_path = NULL;
2523 GVariantIter *interface_iter;
2524 char *interface_str = NULL;
2525 GDBusProxy *device_proxy = NULL;
2526 gboolean is_connected = FALSE;
2528 conn = _bt_gdbus_get_system_gconn();
2529 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2531 manager_proxy = _bt_get_manager_proxy();
2532 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2534 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2536 G_DBUS_CALL_FLAGS_NONE,
2542 if (error != NULL) {
2543 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2544 g_clear_error(&error);
2547 BT_ERR("Failed to Failed to GetManagedObjects");
2548 return BLUETOOTH_ERROR_INTERNAL;
2551 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2552 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2554 /* Parse the signature: oa{sa{sv}}} */
2555 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2556 if (object_path == NULL)
2559 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2560 &interface_str, NULL)) {
2561 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2562 BT_DBG("Found a device: %s", object_path);
2563 g_free(interface_str);
2565 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2566 NULL, BT_BLUEZ_NAME,
2567 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2569 if (device_proxy == NULL) {
2570 BT_DBG("Device don't have this service");
2574 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2575 g_variant_new("(s)", profile_uuid),
2576 G_DBUS_CALL_FLAGS_NONE,
2581 if (result1 == NULL) {
2582 BT_ERR("Error occured in Proxy call");
2584 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2585 g_error_free(error);
2588 g_object_unref(device_proxy);
2591 g_variant_get(result1, "(b)", &is_connected);
2593 if (is_connected == TRUE) {
2594 char address[BT_ADDRESS_STRING_SIZE];
2595 bluetooth_device_address_t *addr = NULL;
2597 _bt_convert_device_path_to_address(object_path, address);
2599 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2600 _bt_convert_addr_string_to_type(addr->addr, address);
2602 g_array_append_vals(*addr_list, addr,
2603 sizeof(bluetooth_device_address_t));
2606 g_variant_unref(result1);
2607 g_object_unref(device_proxy);
2614 g_variant_unref(result);
2615 g_variant_iter_free(iter);
2618 return BLUETOOTH_ERROR_NONE;
2621 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2622 bluetooth_device_info_t *dev_info)
2624 char *object_path = NULL;
2625 GDBusProxy *adapter_proxy;
2626 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2627 int ret = BLUETOOTH_ERROR_NONE;
2629 BT_CHECK_PARAMETER(device_address, return);
2630 BT_CHECK_PARAMETER(dev_info, return);
2632 adapter_proxy = _bt_get_adapter_proxy();
2633 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2635 _bt_convert_addr_type_to_string(address, device_address->addr);
2637 object_path = _bt_get_device_object_path(address);
2639 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2641 ret = __bt_get_bonded_device_info(object_path, dev_info);
2642 g_free(object_path);
2647 int _bt_is_alias_set(bluetooth_device_address_t *device_address, gboolean *is_alias_set)
2649 char *object_path = NULL;
2650 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2651 gboolean alias_set = FALSE;
2653 GDBusConnection *conn;
2654 GDBusProxy *device_proxy;
2655 GError *error = NULL;
2656 GVariant *result = NULL;
2657 GVariant *temp = NULL;
2660 BT_CHECK_PARAMETER(device_address, return);
2661 BT_CHECK_PARAMETER(is_alias_set, return);
2663 _bt_convert_addr_type_to_string(address, device_address->addr);
2665 object_path = _bt_get_device_object_path(address);
2666 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2668 conn = _bt_gdbus_get_system_gconn();
2670 g_free(object_path);
2671 return BLUETOOTH_ERROR_INTERNAL;
2674 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2678 BT_PROPERTIES_INTERFACE,
2680 if (device_proxy == NULL) {
2681 g_free(object_path);
2682 return BLUETOOTH_ERROR_INTERNAL;
2685 result = g_dbus_proxy_call_sync(device_proxy, "Get",
2686 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "IsAliasSet"),
2687 G_DBUS_CALL_FLAGS_NONE,
2693 BT_ERR("Error occured in Proxy call");
2694 if (error != NULL) {
2695 BT_ERR("Getting is_alias_set property failed: [%s]\n", error->message);
2696 g_error_free(error);
2698 g_object_unref(device_proxy);
2699 g_free(object_path);
2700 return BLUETOOTH_ERROR_INTERNAL;
2703 g_variant_get(result, "(v)", &temp);
2704 alias_set = g_variant_get_boolean(temp);
2705 *is_alias_set = alias_set;
2706 BT_DBG("address: [%s] | *is_alias_set: %s", address, *is_alias_set ? "TRUE" : "FALSE");
2707 g_variant_unref(temp);
2708 g_variant_unref(result);
2709 g_object_unref(device_proxy);
2711 g_free(object_path);
2713 return BLUETOOTH_ERROR_NONE;
2716 int _bt_get_timeout_value(int *timeout)
2718 time_t current_time;
2721 /* Take current time */
2722 time(¤t_time);
2723 time_diff = difftime(current_time, visible_timer.start_time);
2725 BT_DBG("Time diff = %d\n", time_diff);
2727 *timeout = visible_timer.timeout - time_diff;
2729 return BLUETOOTH_ERROR_NONE;
2732 int _bt_set_le_privacy(gboolean set_privacy)
2735 GError *error = NULL;
2736 GVariant *result = NULL;
2738 if (__bt_is_factory_test_mode()) {
2739 BT_ERR("Unable to set le privacy in factory binary !!");
2740 return BLUETOOTH_ERROR_NOT_SUPPORT;
2743 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2744 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2745 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2748 proxy = _bt_get_adapter_proxy();
2749 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2751 result = g_dbus_proxy_call_sync(proxy,
2753 g_variant_new("(b)", set_privacy),
2754 G_DBUS_CALL_FLAGS_NONE,
2760 if (error != NULL) {
2761 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2762 g_clear_error(&error);
2764 BT_ERR("Failed to SetLePrivacy");
2765 return BLUETOOTH_ERROR_INTERNAL;
2768 g_variant_unref(result);
2769 BT_INFO("SetLePrivacy as %d", set_privacy);
2770 return BLUETOOTH_ERROR_NONE;
2773 int _bt_set_le_static_random_address(gboolean is_enable)
2776 GError *error = NULL;
2777 GVariant *result = NULL;
2779 if (__bt_is_factory_test_mode()) {
2780 BT_ERR("Unable to set le random address in factory binary !!");
2781 return BLUETOOTH_ERROR_NOT_SUPPORT;
2784 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2785 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2786 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2789 proxy = _bt_get_adapter_proxy();
2790 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2792 result = g_dbus_proxy_call_sync(proxy,
2793 "SetLeStaticRandomAddress",
2794 g_variant_new("(b)", is_enable),
2795 G_DBUS_CALL_FLAGS_NONE,
2801 if (error != NULL) {
2802 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2803 g_clear_error(&error);
2805 BT_ERR("Failed to SetLeStaticRandomAddress");
2806 return BLUETOOTH_ERROR_INTERNAL;
2809 g_variant_unref(result);
2810 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2811 return BLUETOOTH_ERROR_NONE;
2814 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2817 GError *error = NULL;
2821 GVariantBuilder *builder;
2823 BT_CHECK_PARAMETER(m_data, return);
2825 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2826 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2827 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2830 proxy = _bt_get_adapter_proxy();
2831 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2833 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2835 for (i = 0; i < (m_data->data_len) + 2; i++)
2836 g_variant_builder_add(builder, "y", m_data->data[i]);
2838 val = g_variant_new("(ay)", builder);
2840 result = g_dbus_proxy_call_sync(proxy,
2841 "SetManufacturerData",
2843 G_DBUS_CALL_FLAGS_NONE,
2847 g_variant_builder_unref(builder);
2849 if (error != NULL) {
2850 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2851 g_clear_error(&error);
2853 BT_ERR("Failed to SetManufacturerData");
2855 return BLUETOOTH_ERROR_INTERNAL;
2857 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2859 for (i = 0; i < (m_data->data_len) + 2; i++)
2860 g_variant_builder_add(builder, "y", m_data->data[i]);
2862 val = g_variant_new("(ay)", builder);
2864 _bt_send_event(BT_ADAPTER_EVENT,
2865 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2868 BT_INFO("Set manufacturer data");
2870 g_variant_builder_unref(builder);
2871 g_variant_unref(result);
2873 return BLUETOOTH_ERROR_NONE;
2877 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2879 int result = BLUETOOTH_ERROR_NONE;
2880 bt_service_alarm_t *alarm = NULL;
2882 if (!call_back || !alarm_id)
2883 return BLUETOOTH_ERROR_INVALID_PARAM;
2885 if (!alarm_mgr.is_alarm_initialized) {
2886 result = alarmmgr_init("bt-service");
2888 BT_ERR("Failed to initialize alarm = %d", result);
2889 result = BLUETOOTH_ERROR_INTERNAL;
2892 result = alarmmgr_set_cb(alarm_cb, NULL);
2894 BT_ERR("Failed to set the callback = %d", result);
2895 result = BLUETOOTH_ERROR_INTERNAL;
2898 alarm_mgr.is_alarm_initialized = TRUE;
2901 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2903 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2905 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2908 BT_ERR("Failed to create alarm error = %d", result);
2909 result = BLUETOOTH_ERROR_INTERNAL;
2913 alarm->alarm_id = *alarm_id;
2914 alarm->callback = call_back;
2915 alarm->user_data = user_data;
2917 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2918 result = BLUETOOTH_ERROR_NONE;
2923 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2926 bt_service_alarm_t *p_data;
2927 bt_set_alarm_cb callback = NULL;
2928 void *user_data = NULL;
2930 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2931 GINT_TO_POINTER(alarm_id), compare_alarm);
2935 p_data = (bt_service_alarm_t *)node->data;
2936 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2942 callback = p_data->callback;
2943 user_data = p_data->user_data;
2947 callback(alarm_id, user_data);
2952 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2955 bt_service_alarm_t *p_data;
2956 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2959 alarmmgr_remove_alarm(alarm_id);
2960 p_data = (bt_service_alarm_t *)list->data;
2961 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2968 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2970 alarm_id_t alarm_id = (alarm_id_t)data;
2971 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2973 if (p_data->alarm_id == alarm_id)
2979 static void alarm_data_free(void *data)
2981 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2986 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2989 int function = (int)user_data;
2992 case BT_ENABLE_ADAPTER:
2993 result = _bt_enable_adapter();
2994 if (result != BLUETOOTH_ERROR_NONE) {
2995 BT_ERR("_bt_enable_adapter is failed");
2996 /* Send enabled event to API */
2997 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
2998 g_variant_new("(i)", result));
3001 case BT_DISABLE_ADAPTER:
3002 result = _bt_disable_adapter();
3003 if (result != BLUETOOTH_ERROR_NONE) {
3004 BT_ERR("_bt_disable_adapter is failed");
3005 /* Send disabled event to API */
3006 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
3007 g_variant_new("(i)", result));
3011 BT_ERR("function is NOT matched");
3018 int _bt_adapter_request_delayed(int function)
3023 case BT_ENABLE_ADAPTER:
3024 ret = _bt_enable_adapter_check_status();
3025 if (ret == BLUETOOTH_ERROR_NONE)
3026 _bt_adapter_set_status(BT_ACTIVATING);
3031 case BT_DISABLE_ADAPTER:
3032 ret = _bt_disable_adapter_check_status();
3033 if (ret == BLUETOOTH_ERROR_NONE)
3034 _bt_adapter_set_status(BT_DEACTIVATING);
3040 BT_ERR("function is NOT matched");
3041 return BLUETOOTH_ERROR_INTERNAL;
3044 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
3046 return BLUETOOTH_ERROR_NONE;
3050 int _bt_get_enable_timer_id(void)