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>
27 #include <eventsystem.h>
28 #include <bundle_internal.h>
31 #include "bluetooth-api.h"
32 #include "bt-internal-types.h"
33 #include "bt-service-common.h"
34 #include "bt-service-event.h"
35 #include "bt-service-adapter.h"
36 #include "bt-service-util.h"
37 #include "bt-service-network.h"
38 #include "bt-service-obex-server.h"
39 #include "bt-service-opp-client.h"
40 #include "bt-service-map-client.h"
41 #include "bt-service-agent.h"
42 #include "bt-service-main.h"
43 #include "bt-service-avrcp.h"
44 #include "bt-service-device.h"
45 #ifdef TIZEN_FEATURE_BT_DPM
46 #include "bt-service-dpm.h"
56 bt_adapter_timer_t visible_timer = {0, };
60 bt_set_alarm_cb callback;
65 gboolean is_alarm_initialized;
67 } bt_service_alarm_mgr_t;
69 static bt_service_alarm_mgr_t alarm_mgr = {0, };
71 static gboolean is_discovering;
72 static gboolean cancel_by_user;
73 static bt_status_t adapter_status = BT_DEACTIVATED;
74 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
75 static gboolean is_le_intended = FALSE;
76 static void *adapter_agent = NULL;
77 static GDBusProxy *core_proxy = NULL;
78 static guint timer_id = 0;
79 static guint le_timer_id = 0;
80 static gboolean is_recovery_mode;
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_gdbus_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)
214 #ifndef TIZEN_PROFILE_WEARABLE
215 #ifdef TIZEN_FEATURE_BT_DPM
216 int discoverable_state = DPM_BT_ERROR;
220 __bt_visibility_alarm_remove();
222 visible_timer.timeout = timeout;
224 #ifndef TIZEN_PROFILE_WEARABLE
225 #ifdef TIZEN_FEATURE_BT_DPM
226 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
227 if (discoverable_state != DPM_RESTRICTED) {
229 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
230 BT_ERR("Set vconf failed");
231 #ifdef TIZEN_FEATURE_BT_DPM
238 return BLUETOOTH_ERROR_NONE;
240 result = _bt_service_set_alarm(visible_timer.timeout,
241 __bt_visibility_alarm_cb, NULL, &alarm_id);
242 if (result != BLUETOOTH_ERROR_NONE)
243 return BLUETOOTH_ERROR_INTERNAL;
244 visible_timer.alarm_id = alarm_id;
245 /* Take start time */
246 time(&(visible_timer.start_time));
247 visible_timer.event_id = g_timeout_add_seconds(1,
248 __bt_timeout_handler, NULL);
250 return BLUETOOTH_ERROR_NONE;
253 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
260 ret_if(value == NULL);
263 dev->service_index = 0;
265 g_variant_get(value, "as", &iter);
266 while (g_variant_iter_loop(iter, "s", &uuid)) {
267 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
268 parts = g_strsplit(uuid, "-", -1);
270 if (parts == NULL || parts[0] == NULL) {
275 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
278 dev->service_index++;
281 g_variant_iter_free(iter);
284 static int __bt_get_bonded_device_info(gchar *device_path,
285 bluetooth_device_info_t *dev_info)
287 GError *error = NULL;
288 GDBusProxy *device_proxy;
289 gchar *address = NULL;
291 unsigned int cod = 0;
293 gboolean trust = FALSE;
294 gboolean paired = FALSE;
295 guchar connected = 0;
296 GByteArray *manufacturer_data = NULL;
298 GDBusConnection *conn;
300 GVariantIter *property_iter;
304 GVariantIter *char_value_iter;
306 BT_CHECK_PARAMETER(device_path, return);
307 BT_CHECK_PARAMETER(dev_info, return);
309 conn = _bt_gdbus_get_system_gconn();
310 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
312 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
316 BT_PROPERTIES_INTERFACE,
319 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
321 result = g_dbus_proxy_call_sync(device_proxy,
323 g_variant_new("(s)", BT_DEVICE_INTERFACE),
324 G_DBUS_CALL_FLAGS_NONE,
330 BT_ERR("Error occured in Proxy call");
332 BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
333 g_clear_error(&error);
335 g_object_unref(device_proxy);
336 return BLUETOOTH_ERROR_INTERNAL;
339 g_object_unref(device_proxy);
341 g_variant_get(result, "(a{sv})", &property_iter);
343 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
344 if (!g_strcmp0(key, "Paired")) {
345 paired = g_variant_get_boolean(value);
346 } else if (!g_strcmp0(key, "Address")) {
347 g_variant_get(value, "s", &address);
348 } else if (!g_strcmp0(key, "Alias")) {
349 g_variant_get(value, "s", &name);
350 } else if (!g_strcmp0(key, "Name")) {
352 g_variant_get(value, "s", &name);
353 } else if (!g_strcmp0(key, "Class")) {
354 cod = g_variant_get_uint32(value);
355 } else if (!g_strcmp0(key, "Connected")) {
356 connected = g_variant_get_byte(value);
357 } else if (!g_strcmp0(key, "Trusted")) {
358 trust = g_variant_get_boolean(value);
359 } else if (!g_strcmp0(key, "RSSI")) {
360 rssi = g_variant_get_int16(value);
361 } else if (!g_strcmp0(key, "UUIDs")) {
362 __bt_get_service_list(value, dev_info);
363 } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
364 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
365 } else if (!g_strcmp0(key, "ManufacturerData")) {
366 manufacturer_data = g_byte_array_new();
367 g_variant_get(value, "ay", &char_value_iter);
368 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
369 g_byte_array_append(manufacturer_data, &char_value, 1);
371 g_variant_iter_free(char_value_iter);
373 if (manufacturer_data) {
374 if (manufacturer_data->len > 0) {
375 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
376 manufacturer_data->len);
379 g_byte_array_free(manufacturer_data, TRUE);
382 g_variant_iter_free(property_iter);
384 BT_DBG("trust: %d, paired: %d", trust, paired);
386 g_variant_unref(result);
388 if ((paired == FALSE) && (trust == FALSE)) {
391 return BLUETOOTH_ERROR_NOT_PAIRED;
394 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
397 _bt_divide_device_class(&dev_info->device_class, cod);
399 g_strlcpy(dev_info->device_name.name, name,
400 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
402 dev_info->rssi = rssi;
403 dev_info->trust = trust;
404 dev_info->paired = paired;
405 dev_info->connected = connected;
406 ret = BLUETOOTH_ERROR_NONE;
413 void _bt_set_discovery_status(gboolean mode)
415 is_discovering = mode;
418 void _bt_set_cancel_by_user(gboolean value)
420 cancel_by_user = value;
423 gboolean _bt_get_cancel_by_user(void)
425 return cancel_by_user;
428 void _bt_adapter_set_status(bt_status_t status)
430 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
431 adapter_status = status;
434 bt_status_t _bt_adapter_get_status(void)
436 return adapter_status;
439 void _bt_adapter_set_le_status(bt_le_status_t status)
441 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
442 adapter_le_status = status;
445 bt_le_status_t _bt_adapter_get_le_status(void)
447 return adapter_le_status;
451 void _bt_set_le_intended_status(gboolean value)
453 is_le_intended = value;
456 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
458 char *phone_name = NULL;
464 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
465 phone_name = vconf_keynode_get_str(node);
467 if (phone_name && strlen(phone_name) != 0) {
468 if (!g_utf8_validate(phone_name, -1,
469 (const char **)&ptr))
472 _bt_set_local_name(phone_name);
477 static void __bt_set_local_name(void)
479 bluetooth_device_name_t local_name;
480 char *phone_name = NULL;
484 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
485 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
486 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
491 if (strlen(phone_name) != 0) {
492 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
495 _bt_set_local_name(phone_name);
501 static int __bt_set_enabled(void)
503 int adapter_status = BT_ADAPTER_DISABLED;
504 int result = BLUETOOTH_ERROR_NONE;
507 BT_DBG("g_source is removed");
508 g_source_remove(timer_id);
512 _bt_check_adapter(&adapter_status);
514 if (adapter_status == BT_ADAPTER_DISABLED) {
515 BT_ERR("Bluetoothd is not running");
516 return BLUETOOTH_ERROR_INTERNAL;
519 #if defined(TIZEN_PROFILE_MOBILE) || defined(TIZEN_PROFILE_IVI)
520 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
521 if (_bt_set_discoverable_mode(
522 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
523 BT_ERR("Set connectable mode failed");
525 #ifdef TIZEN_PROFILE_TV
526 if (_bt_set_discoverable_mode(
527 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
528 BT_ERR("Fail to set discoverable mode");
532 /* Update Bluetooth Status to notify other modules */
533 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
534 BT_ERR("Set vconf failed\n");
536 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
537 BT_ERR("Set vconf failed\n");
539 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
540 EVT_VAL_BT_ON) != ES_R_OK)
541 BT_ERR("Fail to set value");
543 /* Send enabled event to API */
544 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
545 g_variant_new("(i)", result));
547 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
548 _bt_audio_start_auto_connect(FALSE);
551 __bt_set_local_name();
552 _bt_set_discovery_status(FALSE);
554 return BLUETOOTH_ERROR_NONE;
557 void _bt_set_disabled(int result)
559 int power_off_status = 0;
562 int pm_ignore_mode = 0;
564 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
565 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
567 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
569 /* Update the vconf BT status in normal Deactivation case only */
570 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
571 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
573 BT_DBG("Update vconf for BT normal Deactivation");
575 if (result == BLUETOOTH_ERROR_TIMEOUT)
576 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
577 BT_ERR("Set vconf failed");
579 /* Update Bluetooth Status to notify other modules */
580 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
581 BT_ERR("Set vconf failed");
583 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
584 EVT_VAL_BT_OFF) != ES_R_OK)
585 BT_ERR("Fail to set value");
588 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
589 BT_ERR("Set vconf failed\n");
591 _bt_cancel_queued_transfers();
592 _bt_adapter_set_status(BT_DEACTIVATED);
593 _bt_set_discovery_status(FALSE);
595 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
596 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
598 /* Send disabled event */
599 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
600 g_variant_new("(i)", result));
601 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
605 BT_INFO("Adapter disabled");
608 static int __bt_set_le_enabled(void)
611 int result = BLUETOOTH_ERROR_NONE;
614 /* Update Bluetooth Status to notify other modules */
615 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
616 BT_ERR("Set vconf failed\n");
618 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
619 EVT_VAL_BT_LE_ON) != ES_R_OK)
620 BT_ERR("Fail to set value");
622 /* Send enabled event to API */
624 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
625 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
627 status = _bt_adapter_get_status();
628 if (status == BT_DEACTIVATED) {
629 BT_INFO("BREDR is off, turn off PSCAN");
630 _bt_set_connectable(FALSE);
632 if (le_timer_id > 0) {
633 g_source_remove(le_timer_id);
637 /* Send enabled event to API */
638 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
639 g_variant_new("(i)", result));
641 __bt_set_local_name();
644 return BLUETOOTH_ERROR_NONE;
647 void _bt_set_le_disabled(int result)
649 int power_off_status;
652 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
653 BT_DBG("ret : %d", ret);
654 BT_DBG("power_off_status : %d", power_off_status);
656 /* Update Bluetooth Status to notify other modules */
657 BT_DBG("Update vconf for BT LE normal Deactivation");
658 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
659 BT_ERR("Set vconf failed\n");
660 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
662 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
663 EVT_VAL_BT_LE_OFF) != ES_R_OK)
664 BT_ERR("Fail to set value");
666 /* Send disabled event */
667 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
668 g_variant_new("(i)", result));
671 void *_bt_get_adapter_agent(void)
673 return adapter_agent;
676 int _bt_enable_core(void)
680 GError *error = NULL;
682 proxy = __bt_get_core_proxy();
683 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
685 /* Clean up the process */
686 result = g_dbus_proxy_call_sync(proxy,
689 G_DBUS_CALL_FLAGS_NONE,
696 BT_ERR("Bt core call failed(Error: %s)", error->message);
697 g_clear_error(&error);
699 BT_ERR("Bt core call failed");
700 return BLUETOOTH_ERROR_INTERNAL;
703 g_variant_unref(result);
704 return BLUETOOTH_ERROR_NONE;
707 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
708 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
710 gboolean flight_mode = FALSE;
711 int power_saving_mode = 0;
714 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
715 type = vconf_keynode_get_type(node);
716 if (type == VCONF_TYPE_BOOL) {
717 flight_mode = vconf_keynode_get_bool(node);
718 if (flight_mode != TRUE) {
719 BT_ERR("Ignore the event");
722 } else if (type == VCONF_TYPE_INT) {
723 power_saving_mode = vconf_keynode_get_int(node);
724 if (power_saving_mode != 2) {
725 BT_ERR("Ignore the event");
729 BT_ERR("Invaild vconf key type : %d", type);
737 void _bt_service_register_vconf_handler(void)
741 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
742 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
743 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
744 BT_ERR("Unable to register key handler");
746 BT_DBG("Telephony is disabled");
749 #ifndef TIZEN_PROFILE_WEARABLE
750 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
751 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
752 BT_ERR("Unable to register key handler");
756 void _bt_service_unregister_vconf_handler(void)
760 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
761 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
762 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
765 #ifndef TIZEN_PROFILE_WEARABLE
766 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
767 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
771 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
773 const char *bt_status = NULL;
774 const char *bt_le_status = NULL;
775 BT_DBG("bt state set event(%s) received", event_name);
777 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
778 BT_DBG("bt_state: (%s)", bt_status);
780 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
781 BT_DBG("bt_state: (%s)", bt_le_status);
784 void _bt_handle_adapter_added(void)
788 bt_le_status_t le_status;
792 BT_DBG("g_source is removed");
793 g_source_remove(timer_id);
798 status = _bt_adapter_get_status();
799 le_status = _bt_adapter_get_le_status();
800 BT_DBG("status : %d", status);
801 BT_DBG("le_status : %d", le_status);
803 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
804 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
805 if (!adapter_agent) {
806 BT_ERR("Fail to register agent");
810 if (adapter_agent == NULL) {
811 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
812 if (!adapter_agent) {
813 BT_ERR("Fail to register agent");
819 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
820 BT_ERR("Fail to register media player");
822 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
823 BT_ERR("Fail to init obex server");
825 #ifdef TIZEN_BT_PAN_NAP_ENABLED
826 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
827 BT_ERR("Fail to activate network");
830 /* add the vconf noti handler */
831 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
832 __bt_phone_name_changed_cb, NULL);
834 BT_ERR("Unable to register key handler");
836 if (le_status == BT_LE_ACTIVATING ||
837 status == BT_ACTIVATING) {
838 __bt_set_le_enabled();
839 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
842 if (status == BT_ACTIVATING) {
844 _bt_adapter_set_status(BT_ACTIVATED);
848 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
849 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
850 BT_ERR("Fail to register system event");
854 void _bt_handle_adapter_removed(void)
858 _bt_adapter_set_status(BT_DEACTIVATED);
860 __bt_visibility_alarm_remove();
862 if (alarm_mgr.is_alarm_initialized == TRUE) {
864 alarm_mgr.is_alarm_initialized = FALSE;
865 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
866 alarm_mgr.g_alarm_list = NULL;
869 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
870 _bt_audio_stop_auto_connect();
873 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
874 (vconf_callback_fn)__bt_phone_name_changed_cb);
876 ERR("vconf_ignore_key_changed failed\n");
878 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
879 _bt_destroy_agent(adapter_agent);
880 adapter_agent = NULL;
882 if (is_recovery_mode == TRUE) {
883 /* Send disabled event */
884 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
886 /* Will recover BT by bt-core, so set the mode as activating */
887 _bt_adapter_set_status(BT_ACTIVATING);
888 is_recovery_mode = FALSE;
890 _bt_reliable_terminate_service(NULL);
893 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
896 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
897 BT_ERR("Fail to unregister system event");
900 static gboolean __bt_enable_timeout_cb(gpointer user_data)
904 GError *error = NULL;
908 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
910 BT_ERR("EnableAdapter is failed");
912 proxy = __bt_get_core_proxy();
916 /* Clean up the process */
917 result = g_dbus_proxy_call_sync(proxy,
920 G_DBUS_CALL_FLAGS_NONE,
927 BT_ERR("Bt core call failed(Error: %s)", error->message);
928 g_clear_error(&error);
930 BT_ERR("Bt core call failed");
935 g_variant_unref(result);
936 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
938 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
939 _bt_terminate_service(NULL);
945 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
949 GError *error = NULL;
953 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
955 BT_ERR("EnableAdapterLE is failed");
957 proxy = __bt_get_core_proxy();
961 /* Clean up the process */
962 result = g_dbus_proxy_call_sync(proxy,
965 G_DBUS_CALL_FLAGS_NONE,
972 BT_ERR("Bt core call failed(Error: %s)", error->message);
973 g_clear_error(&error);
975 BT_ERR("Bt core call failed");
979 g_variant_unref(result);
980 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
982 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
984 if (_bt_adapter_get_status() == BT_DEACTIVATED)
985 _bt_terminate_service(NULL);
990 void _bt_adapter_start_le_enable_timer(void)
992 if (le_timer_id > 0) {
993 g_source_remove(le_timer_id);
997 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
998 __bt_enable_le_timeout_cb, NULL);
1003 void _bt_adapter_start_enable_timer(void)
1006 g_source_remove(timer_id);
1010 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1011 __bt_enable_timeout_cb, NULL);
1016 #ifdef TIZEN_PROFILE_TV
1017 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
1022 _bt_adapter_set_status(BT_ACTIVATED);
1028 int _bt_enable_adapter_check_status(void)
1030 bt_status_t status = _bt_adapter_get_status();
1031 bt_le_status_t le_status = _bt_adapter_get_le_status();
1035 if (status == BT_ACTIVATING) {
1036 BT_ERR("Enabling in progress");
1037 return BLUETOOTH_ERROR_IN_PROGRESS;
1040 if (status == BT_ACTIVATED) {
1041 BT_ERR("Already enabled");
1042 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1045 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1046 BT_ERR("Disabling in progress");
1047 return BLUETOOTH_ERROR_DEVICE_BUSY;
1050 return BLUETOOTH_ERROR_NONE;
1053 int _bt_enable_adapter(void)
1056 GError *error = NULL;
1058 GVariant *result = NULL;
1059 bt_status_t status = _bt_adapter_get_status();
1060 bt_le_status_t le_status = _bt_adapter_get_le_status();
1064 if (status == BT_ACTIVATING) {
1065 BT_ERR("Enabling in progress");
1066 return BLUETOOTH_ERROR_IN_PROGRESS;
1069 if (status == BT_ACTIVATED) {
1070 BT_ERR("Already enabled");
1071 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1074 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1075 BT_ERR("Disabling in progress");
1076 return BLUETOOTH_ERROR_DEVICE_BUSY;
1079 _bt_adapter_set_status(BT_ACTIVATING);
1081 #ifdef TIZEN_PROFILE_TV
1083 int adapter_status = BT_ADAPTER_DISABLED;
1085 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1086 BT_ERR("Set vconf failed");
1088 _bt_check_adapter(&adapter_status);
1089 if (adapter_status == BT_ADAPTER_ENABLED) {
1090 g_idle_add(__bt_adapter_enabled_cb, NULL);
1091 _bt_adapter_start_enable_timer();
1092 return BLUETOOTH_ERROR_NONE;
1097 proxy = __bt_get_core_proxy();
1099 return BLUETOOTH_ERROR_INTERNAL;
1101 if (le_status == BT_LE_ACTIVATED) {
1102 BT_INFO("LE Already enabled. Just turn on PSCAN");
1103 ret = _bt_set_connectable(TRUE);
1104 if (ret == BLUETOOTH_ERROR_NONE)
1105 _bt_adapter_set_status(BT_ACTIVATED);
1107 return BLUETOOTH_ERROR_INTERNAL;
1110 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1112 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1115 BT_ERR("EnableAdapterLe failed: %s", error->message);
1116 _bt_adapter_set_status(BT_DEACTIVATED);
1117 g_clear_error(&error);
1119 result = g_dbus_proxy_call_sync(proxy,
1122 G_DBUS_CALL_FLAGS_NONE,
1127 if (error != NULL) {
1128 BT_ERR("Bt core call failed(Error: %s)", error->message);
1129 g_clear_error(&error);
1131 g_variant_unref(result);
1132 #ifndef TIZEN_FEATURE_BT_USB_DONGLE
1133 /* Terminate myself */
1134 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1136 return BLUETOOTH_ERROR_INTERNAL;
1138 g_variant_unref(result);
1139 if (le_status == BT_LE_ACTIVATED)
1142 _bt_adapter_start_enable_timer();
1144 return BLUETOOTH_ERROR_NONE;
1147 static gboolean __bt_disconnect_all(void)
1150 GDBusConnection *conn;
1151 GDBusProxy *dev_proxy;
1152 gboolean ret = FALSE;
1154 GError *error = NULL;
1155 GArray *device_list;
1156 bluetooth_device_info_t info;
1158 char *device_path = NULL;
1159 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1164 conn = _bt_gdbus_get_system_gconn();
1166 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1168 if (_bt_get_bonded_devices(&device_list)
1169 != BLUETOOTH_ERROR_NONE) {
1170 g_array_free(device_list, TRUE);
1174 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1176 for (i = 0; i < size; i++) {
1178 info = g_array_index(device_list,
1179 bluetooth_device_info_t, i);
1181 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1182 BT_DBG("Found Connected device");
1183 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1184 device_path = _bt_get_device_object_path(address);
1185 if (device_path == NULL)
1188 BT_DBG("Disconnecting : %s", device_path);
1190 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1194 BT_DEVICE_INTERFACE,
1197 if (dev_proxy == NULL)
1200 result = g_dbus_proxy_call_sync(dev_proxy,
1203 G_DBUS_CALL_FLAGS_NONE,
1209 if (error != NULL) {
1210 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1211 g_clear_error(&error);
1213 BT_ERR("Disconnect call failed");
1214 g_object_unref(dev_proxy);
1218 g_variant_unref(result);
1219 g_object_unref(dev_proxy);
1223 g_array_free(device_list, TRUE);
1229 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1232 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1238 int _bt_disable_cb(void)
1246 GError *error = NULL;
1248 _bt_adapter_set_status(BT_DEACTIVATING);
1250 bt_le_status_t le_status;
1251 le_status = _bt_adapter_get_le_status();
1252 BT_DBG("le_status : %d", le_status);
1253 if (le_status == BT_LE_ACTIVATED) {
1254 BT_INFO("LE is enabled. Just turn off PSCAN");
1256 if (_bt_is_discovering())
1257 _bt_cancel_discovery();
1259 if (_bt_is_connectable() == FALSE) {
1260 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1262 ret = _bt_set_connectable(FALSE);
1263 if (ret != BLUETOOTH_ERROR_NONE) {
1264 BT_ERR("_bt_set_connectable fail!");
1265 _bt_adapter_set_status(BT_ACTIVATED);
1266 return BLUETOOTH_ERROR_INTERNAL;
1271 proxy = __bt_get_core_proxy();
1272 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1274 result = g_dbus_proxy_call_sync(proxy,
1277 G_DBUS_CALL_FLAGS_NONE,
1283 if (error != NULL) {
1284 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1285 g_clear_error(&error);
1287 BT_ERR("Failed to DisableAdapter");
1288 _bt_adapter_set_status(BT_ACTIVATED);
1289 return BLUETOOTH_ERROR_INTERNAL;
1292 g_variant_unref(result);
1293 return BLUETOOTH_ERROR_NONE;
1296 int _bt_disable_adapter_check_status(void)
1298 bt_status_t status = _bt_adapter_get_status();
1302 if (status == BT_DEACTIVATING) {
1303 BT_DBG("Disabling in progress");
1304 return BLUETOOTH_ERROR_IN_PROGRESS;
1307 if (status == BT_DEACTIVATED) {
1308 BT_DBG("Already disabled");
1309 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1312 return BLUETOOTH_ERROR_NONE;
1315 int _bt_disable_adapter(void)
1320 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1321 BT_DBG("Disabling in progress");
1322 return BLUETOOTH_ERROR_IN_PROGRESS;
1325 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1326 BT_DBG("Already disabled");
1327 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1331 g_source_remove(timer_id);
1335 __bt_disconnect_all();
1336 ret = _bt_disable_cb();
1342 int _bt_recover_adapter(void)
1347 GError *error = NULL;
1349 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1350 BT_DBG("Disabling in progress");
1351 return BLUETOOTH_ERROR_IN_PROGRESS;
1354 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1355 BT_DBG("Already disabled");
1356 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1359 _bt_adapter_set_status(BT_DEACTIVATING);
1361 proxy = __bt_get_core_proxy();
1362 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1364 result = g_dbus_proxy_call_sync(proxy,
1367 G_DBUS_CALL_FLAGS_NONE,
1373 if (error != NULL) {
1374 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1375 g_clear_error(&error);
1377 BT_ERR("Failed to RecoverAdapter");
1378 return BLUETOOTH_ERROR_INTERNAL;
1381 is_recovery_mode = TRUE;
1383 g_variant_unref(result);
1384 __bt_disconnect_all();
1387 return BLUETOOTH_ERROR_NONE;
1390 int _bt_reset_adapter(void)
1394 GError *error = NULL;
1398 proxy = __bt_get_core_proxy();
1400 return BLUETOOTH_ERROR_INTERNAL;
1402 result = g_dbus_proxy_call_sync(proxy,
1405 G_DBUS_CALL_FLAGS_NONE,
1411 if (error != NULL) {
1412 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1413 g_clear_error(&error);
1415 BT_ERR("Failed to ResetAdapter");
1416 return BLUETOOTH_ERROR_INTERNAL;
1419 g_variant_unref(result);
1420 /* Terminate myself */
1421 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1422 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1424 return BLUETOOTH_ERROR_NONE;
1427 #ifndef TIZEN_PROFILE_TV
1428 int _bt_check_adapter(int *status)
1431 char *adapter_path = NULL;
1433 BT_CHECK_PARAMETER(status, return);
1435 *status = BT_ADAPTER_DISABLED;
1437 adapter_path = _bt_get_adapter_path();
1440 if (adapter_path != NULL)
1441 *status = BT_ADAPTER_ENABLED;
1443 g_free(adapter_path);
1444 return BLUETOOTH_ERROR_NONE;
1447 int _bt_check_adapter(int *status)
1450 GError *error = NULL;
1453 gboolean powered = FALSE;
1455 BT_CHECK_PARAMETER(status, return);
1457 *status = BT_ADAPTER_DISABLED;
1459 proxy = _bt_get_adapter_properties_proxy();
1460 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1462 result = g_dbus_proxy_call_sync(proxy,
1464 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1466 G_DBUS_CALL_FLAGS_NONE,
1472 BT_ERR("Failed to get local address");
1473 if (error != NULL) {
1474 BT_ERR("Failed to get local address (Error: %s)", error->message);
1475 g_clear_error(&error);
1477 return BLUETOOTH_ERROR_INTERNAL;
1480 g_variant_get(result, "(v)", &temp);
1481 powered = g_variant_get_boolean(temp);
1482 BT_DBG("powered: %d", powered);
1485 *status = BT_ADAPTER_ENABLED;
1487 g_variant_unref(result);
1488 g_variant_unref(temp);
1489 return BLUETOOTH_ERROR_NONE;
1493 int _bt_enable_adapter_le(void)
1497 GError *error = NULL;
1498 bt_status_t status = _bt_adapter_get_status();
1499 bt_le_status_t le_status = _bt_adapter_get_le_status();
1502 if (le_status == BT_LE_ACTIVATING) {
1503 BT_ERR("Enabling in progress");
1504 return BLUETOOTH_ERROR_IN_PROGRESS;
1507 if (le_status == BT_LE_ACTIVATED) {
1508 BT_ERR("Already enabled");
1509 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1512 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1513 BT_ERR("Disabling in progress");
1514 return BLUETOOTH_ERROR_DEVICE_BUSY;
1517 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1519 proxy = __bt_get_core_proxy();
1520 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1522 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1524 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1527 BT_ERR("EnableAdapterLe failed: %s", error->message);
1528 _bt_adapter_set_le_status(BT_DEACTIVATED);
1529 g_clear_error(&error);
1531 /* Clean up the process */
1532 result = g_dbus_proxy_call_sync(proxy,
1535 G_DBUS_CALL_FLAGS_NONE,
1541 BT_ERR("Bt core call failed");
1543 BT_ERR("EnableAdapterLE Failed %s", error->message);
1544 g_clear_error(&error);
1547 g_variant_unref(result);
1548 /* Terminate myself */
1549 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1550 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1551 return BLUETOOTH_ERROR_INTERNAL;
1555 g_variant_unref(result);
1557 _bt_adapter_start_le_enable_timer();
1559 if (status == BT_ACTIVATED) {
1560 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1561 __bt_set_le_enabled();
1563 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1565 return BLUETOOTH_ERROR_NONE;
1568 int _bt_disable_adapter_le(void)
1572 bt_le_status_t bt_le_state;
1574 GError *error = NULL;
1576 bt_le_state = _bt_adapter_get_le_status();
1577 if (bt_le_state == BT_LE_DEACTIVATING) {
1578 BT_DBG("Disabling in progress");
1579 return BLUETOOTH_ERROR_IN_PROGRESS;
1582 if (bt_le_state == BT_LE_DEACTIVATED) {
1583 BT_DBG("Already disabled");
1584 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1587 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1589 proxy = __bt_get_core_proxy();
1591 return BLUETOOTH_ERROR_INTERNAL;
1593 result = g_dbus_proxy_call_sync(proxy,
1596 G_DBUS_CALL_FLAGS_NONE,
1602 if (error != NULL) {
1603 BT_ERR("Bt core call failed (Error: %s)", error->message);
1604 g_clear_error(&error);
1606 BT_ERR("Bt core call failed");
1607 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1608 return BLUETOOTH_ERROR_INTERNAL;
1611 g_variant_unref(result);
1612 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1613 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1615 return BLUETOOTH_ERROR_NONE;
1618 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1622 GError *error = NULL;
1623 const char *address;
1627 BT_CHECK_PARAMETER(local_address, return);
1629 proxy = _bt_get_adapter_properties_proxy();
1630 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1632 result = g_dbus_proxy_call_sync(proxy,
1634 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1636 G_DBUS_CALL_FLAGS_NONE,
1642 BT_ERR("Failed to get local address");
1643 if (error != NULL) {
1644 BT_ERR("Failed to get local address (Error: %s)", error->message);
1645 g_clear_error(&error);
1647 return BLUETOOTH_ERROR_INTERNAL;
1650 g_variant_get(result, "(v)", &temp);
1651 address = g_variant_get_string(temp, NULL);
1652 BT_DBG("Address:%s", address);
1655 _bt_convert_addr_string_to_type(local_address->addr, address);
1657 return BLUETOOTH_ERROR_INTERNAL;
1659 g_variant_unref(result);
1660 g_variant_unref(temp);
1661 return BLUETOOTH_ERROR_NONE;
1664 int _bt_get_local_version(bluetooth_version_t *local_version)
1667 const char *ver = NULL;
1669 int ret = BLUETOOTH_ERROR_NONE;
1673 BT_CHECK_PARAMETER(local_version, return);
1675 GError *error = NULL;
1677 proxy = _bt_get_adapter_properties_proxy();
1678 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1680 result = g_dbus_proxy_call_sync(proxy,
1682 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1684 G_DBUS_CALL_FLAGS_NONE,
1690 if (error != NULL) {
1691 BT_ERR("Failed to get local version (Error: %s)", error->message);
1692 g_clear_error(&error);
1694 BT_ERR("Failed to get local version");
1695 return BLUETOOTH_ERROR_INTERNAL;
1698 g_variant_get(result, "(v)", &temp);
1699 ver = g_variant_get_string(temp, NULL);
1700 BT_DBG("VERSION: %s", ver);
1702 if (ver && (strlen(ver) > 0)) {
1703 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1704 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1707 g_strlcpy(local_version->version, ver,
1708 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1711 ret = BLUETOOTH_ERROR_INTERNAL;
1714 g_variant_unref(result);
1715 g_variant_unref(temp);
1719 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1722 const char *name = NULL;
1724 int ret = BLUETOOTH_ERROR_NONE;
1727 GError *error = NULL;
1729 BT_CHECK_PARAMETER(local_name, return);
1731 proxy = _bt_get_adapter_properties_proxy();
1732 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1734 result = g_dbus_proxy_call_sync(proxy,
1736 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1738 G_DBUS_CALL_FLAGS_NONE,
1744 if (error != NULL) {
1745 BT_ERR("Failed to get local name (Error: %s)", error->message);
1746 g_clear_error(&error);
1748 BT_ERR("Failed to get local name");
1749 return BLUETOOTH_ERROR_INTERNAL;
1752 g_variant_get(result, "(v)", &temp);
1753 name = g_variant_get_string(temp, NULL);
1754 BT_DBG("LOCAL NAME:%s", name);
1756 if (name && (strlen(name) > 0)) {
1757 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1758 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1761 g_strlcpy(local_name->name, name,
1762 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1764 ret = BLUETOOTH_ERROR_INTERNAL;
1766 g_variant_unref(result);
1767 g_variant_unref(temp);
1771 int _bt_set_local_name(char *local_name)
1774 GError *error = NULL;
1778 BT_CHECK_PARAMETER(local_name, return);
1780 proxy = _bt_get_adapter_properties_proxy();
1782 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1784 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1787 result = g_dbus_proxy_call_sync(proxy,
1789 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1790 "Alias", g_variant_new("s", local_name)),
1791 G_DBUS_CALL_FLAGS_NONE,
1797 if (error != NULL) {
1798 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1799 g_clear_error(&error);
1801 BT_ERR("Failed to set Alias");
1802 return BLUETOOTH_ERROR_INTERNAL;
1805 g_variant_unref(result);
1806 return BLUETOOTH_ERROR_NONE;
1809 int _bt_is_service_used(char *service_uuid, gboolean *used)
1812 GError *error = NULL;
1813 int ret = BLUETOOTH_ERROR_NONE;
1815 GVariant *temp = NULL;
1816 GVariantIter *iter = NULL;
1820 BT_CHECK_PARAMETER(service_uuid, return);
1821 BT_CHECK_PARAMETER(used, return);
1823 proxy = _bt_get_adapter_properties_proxy();
1824 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1826 result = g_dbus_proxy_call_sync(proxy,
1828 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1830 G_DBUS_CALL_FLAGS_NONE,
1836 if (error != NULL) {
1837 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1838 g_clear_error(&error);
1840 BT_ERR("Failed to get UUIDs");
1841 return BLUETOOTH_ERROR_INTERNAL;
1844 g_variant_get(result, "(v)", &temp);
1845 g_variant_get(temp, "as", &iter);
1848 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1849 if (strcasecmp(uuid, service_uuid) == 0) {
1854 g_variant_iter_free(iter);
1855 g_variant_unref(result);
1857 BT_DBG("Service Used? %d", *used);
1862 static gboolean __bt_get_discoverable_property(void)
1865 gboolean discoverable_v;
1866 GError *error = NULL;
1870 proxy = _bt_get_adapter_properties_proxy();
1871 retv_if(proxy == NULL, FALSE);
1873 result = g_dbus_proxy_call_sync(proxy,
1875 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1877 G_DBUS_CALL_FLAGS_NONE,
1883 if (error != NULL) {
1884 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1885 g_clear_error(&error);
1887 BT_ERR("Failed to get Discoverable property");
1888 return BLUETOOTH_ERROR_INTERNAL;
1891 g_variant_get(result, "(v)", &temp);
1892 discoverable_v = g_variant_get_boolean(temp);
1893 BT_DBG("discoverable_v:%d", discoverable_v);
1895 g_variant_unref(result);
1896 g_variant_unref(temp);
1898 return discoverable_v;
1901 int _bt_get_discoverable_mode(int *mode)
1903 gboolean discoverable;
1904 unsigned int timeout;
1906 BT_CHECK_PARAMETER(mode, return);
1908 discoverable = __bt_get_discoverable_property();
1909 timeout = _bt_get_discoverable_timeout_property();
1911 if (discoverable == TRUE) {
1913 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1915 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1917 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1919 return BLUETOOTH_ERROR_NONE;
1923 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1925 int ret = BLUETOOTH_ERROR_NONE;
1928 GError *error = NULL;
1931 #ifdef TIZEN_FEATURE_BT_DPM
1932 int discoverable_state = DPM_BT_ERROR;
1935 proxy = _bt_get_adapter_properties_proxy();
1937 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1939 #ifdef TIZEN_FEATURE_BT_DPM
1940 _bt_dpm_get_bluetooth_limited_discoverable_state(&discoverable_state);
1941 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1942 discoverable_state == DPM_RESTRICTED) {
1943 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1944 return BLUETOOTH_ERROR_ACCESS_DENIED;
1946 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1947 discoverable_state == DPM_RESTRICTED) {
1948 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1949 return BLUETOOTH_ERROR_ACCESS_DENIED;
1953 switch (discoverable_mode) {
1954 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1959 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1964 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1969 return BLUETOOTH_ERROR_INVALID_PARAM;
1972 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1973 discoverable_mode, timeout);
1975 result = g_dbus_proxy_call_sync(proxy,
1977 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1978 "Connectable", g_variant_new("b", pg_scan)),
1979 G_DBUS_CALL_FLAGS_NONE,
1985 if (error != NULL) {
1986 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1987 g_clear_error(&error);
1989 BT_ERR("Failed to set connectable property");
1990 return BLUETOOTH_ERROR_INTERNAL;
1992 g_variant_unref(result);
1993 result = g_dbus_proxy_call_sync(proxy,
1995 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1996 g_variant_new("b", inq_scan)),
1997 G_DBUS_CALL_FLAGS_NONE,
2003 if (error != NULL) {
2004 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
2005 g_clear_error(&error);
2007 BT_ERR("Failed to set Discoverable property");
2008 return BLUETOOTH_ERROR_INTERNAL;
2010 g_variant_unref(result);
2011 result = g_dbus_proxy_call_sync(proxy,
2013 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
2014 "DiscoverableTimeout", g_variant_new("u", timeout)),
2015 G_DBUS_CALL_FLAGS_NONE,
2021 if (error != NULL) {
2022 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
2023 g_clear_error(&error);
2025 BT_ERR("Failed to set DiscoverableTimeout property");
2026 return BLUETOOTH_ERROR_INTERNAL;
2029 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
2032 ret = __bt_set_visible_time(timeout);
2034 g_variant_unref(result);
2039 int _bt_start_discovery(void)
2042 GError *error = NULL;
2045 if (_bt_is_discovering() == TRUE) {
2046 BT_ERR("BT is already in discovering");
2047 return BLUETOOTH_ERROR_IN_PROGRESS;
2048 } else if (_bt_is_device_creating() == TRUE) {
2049 BT_ERR("Bonding device is going on");
2050 return BLUETOOTH_ERROR_DEVICE_BUSY;
2053 proxy = _bt_get_adapter_proxy();
2054 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2056 result = g_dbus_proxy_call_sync(proxy,
2059 G_DBUS_CALL_FLAGS_NONE,
2065 if (error != NULL) {
2066 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
2067 g_clear_error(&error);
2069 BT_ERR("StartDiscovery failed");
2070 return BLUETOOTH_ERROR_INTERNAL;
2073 is_discovering = TRUE;
2074 cancel_by_user = FALSE;
2075 /* discovery status will be change in event */
2076 g_variant_unref(result);
2077 return BLUETOOTH_ERROR_NONE;
2080 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2084 GError *error = NULL;
2085 const gchar *disc_type;
2087 if (_bt_is_discovering() == TRUE) {
2088 BT_ERR("BT is already in discovering");
2089 return BLUETOOTH_ERROR_IN_PROGRESS;
2092 proxy = _bt_get_adapter_proxy();
2093 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2095 if (role == DISCOVERY_ROLE_BREDR)
2096 disc_type = "BREDR";
2097 else if (role == DISCOVERY_ROLE_LE)
2099 else if (role == DISCOVERY_ROLE_LE_BREDR)
2100 disc_type = "LE_BREDR";
2102 return BLUETOOTH_ERROR_INVALID_PARAM;
2104 result = g_dbus_proxy_call_sync(proxy,
2105 "StartCustomDiscovery",
2106 g_variant_new("s", disc_type),
2107 G_DBUS_CALL_FLAGS_NONE,
2113 if (error != NULL) {
2114 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2115 g_clear_error(&error);
2117 BT_ERR("StartCustomDiscovery failed");
2118 return BLUETOOTH_ERROR_INTERNAL;
2121 is_discovering = TRUE;
2122 cancel_by_user = FALSE;
2123 /* discovery status will be change in event */
2124 g_variant_unref(result);
2125 return BLUETOOTH_ERROR_NONE;
2128 int _bt_cancel_discovery(void)
2131 GError *error = NULL;
2134 if (_bt_is_discovering() == FALSE) {
2135 BT_ERR("BT is not in discovering");
2136 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2139 proxy = _bt_get_adapter_proxy();
2140 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2142 result = g_dbus_proxy_call_sync(proxy,
2145 G_DBUS_CALL_FLAGS_NONE,
2151 int ret = BLUETOOTH_ERROR_INTERNAL;
2152 if (error != NULL) {
2153 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2155 if (g_strrstr(error->message, "No discovery started"))
2156 ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
2158 g_clear_error(&error);
2160 BT_ERR("StopDiscovery failed");
2166 cancel_by_user = TRUE;
2167 /* discovery status will be change in event */
2168 g_variant_unref(result);
2169 return BLUETOOTH_ERROR_NONE;
2172 gboolean _bt_is_discovering(void)
2174 return is_discovering;
2177 gboolean _bt_is_connectable(void)
2180 GError *error = NULL;
2181 gboolean is_connectable = FALSE;
2185 proxy = _bt_get_adapter_properties_proxy();
2186 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2188 result = g_dbus_proxy_call_sync(proxy,
2190 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2192 G_DBUS_CALL_FLAGS_NONE,
2198 if (error != NULL) {
2199 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2200 g_clear_error(&error);
2202 BT_ERR("Failed to get connectable property");
2203 return BLUETOOTH_ERROR_INTERNAL;
2206 g_variant_get(result, "(v)", &temp);
2207 is_connectable = g_variant_get_boolean(temp);
2208 BT_DBG("discoverable_v:%d", is_connectable);
2210 g_variant_unref(result);
2211 g_variant_unref(temp);
2213 BT_INFO("Get connectable [%d]", is_connectable);
2214 return is_connectable;
2217 int _bt_set_connectable(gboolean is_connectable)
2220 GError *error = NULL;
2223 if (__bt_is_factory_test_mode()) {
2224 BT_ERR("Unable to set connectable in factory binary !!");
2225 return BLUETOOTH_ERROR_NOT_SUPPORT;
2228 proxy = _bt_get_adapter_properties_proxy();
2230 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2232 result = g_dbus_proxy_call_sync(proxy,
2234 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2235 g_variant_new("b", is_connectable)),
2236 G_DBUS_CALL_FLAGS_NONE,
2242 if (error != NULL) {
2243 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2244 g_clear_error(&error);
2246 BT_ERR("Failed to set connectable property");
2247 return BLUETOOTH_ERROR_INTERNAL;
2250 BT_INFO_C("### Set connectable [%d]", is_connectable);
2251 g_variant_unref(result);
2252 return BLUETOOTH_ERROR_NONE;
2255 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2258 gboolean discovering_v;
2259 GError *error = NULL;
2260 char *discovering_type = NULL;
2264 proxy = _bt_get_adapter_properties_proxy();
2265 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2267 if (discovery_type == DISCOVERY_ROLE_BREDR)
2268 discovering_type = "Discovering";
2269 else if (discovery_type == DISCOVERY_ROLE_LE)
2270 discovering_type = "LEDiscovering";
2272 result = g_dbus_proxy_call_sync(proxy,
2274 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2276 G_DBUS_CALL_FLAGS_NONE,
2282 if (error != NULL) {
2283 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2284 g_clear_error(&error);
2286 BT_ERR("Failed to get discovering property");
2287 return BLUETOOTH_ERROR_INTERNAL;
2290 g_variant_get(result, "(v)", &temp);
2291 discovering_v = g_variant_get_boolean(temp);
2292 BT_DBG("discoverable_v:%d", discovering_v);
2294 g_variant_unref(result);
2295 g_variant_unref(temp);
2297 return discovering_v;
2300 unsigned int _bt_get_discoverable_timeout_property(void)
2303 unsigned int timeout_v;
2304 GError *error = NULL;
2308 proxy = _bt_get_adapter_properties_proxy();
2309 retv_if(proxy == NULL, 0);
2311 result = g_dbus_proxy_call_sync(proxy,
2313 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2314 "DiscoverableTimeout"),
2315 G_DBUS_CALL_FLAGS_NONE,
2321 BT_ERR("Fail to get discoverable timeout");
2322 if (error != NULL) {
2323 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2324 g_clear_error(&error);
2329 g_variant_get(result, "(v)", &temp);
2330 timeout_v = g_variant_get_uint32(temp);
2331 BT_DBG("discoverable_v:%d", timeout_v);
2333 g_variant_unref(result);
2334 g_variant_unref(temp);
2339 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2341 bluetooth_device_info_t *dev_info;
2344 GByteArray *manufacturer_data = NULL;
2346 GVariantIter *char_value_iter;
2348 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2350 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2355 if (!g_strcmp0(key, "Address")) {
2356 const char *address = NULL;
2357 address = g_variant_get_string(value, NULL);
2358 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2360 } else if (!g_strcmp0(key, "Class")) {
2362 cod = g_variant_get_uint32(value);
2363 _bt_divide_device_class(&dev_info->device_class, cod);
2364 } else if (!g_strcmp0(key, "Name")) {
2365 const char *name = NULL;
2366 name = g_variant_get_string(value, NULL);
2367 /* If there is no Alias */
2368 if (strlen(dev_info->device_name.name) == 0) {
2369 g_strlcpy(dev_info->device_name.name, name,
2370 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2372 } else if (!g_strcmp0(key, "Alias")) {
2373 const char *alias = NULL;
2374 alias = g_variant_get_string(value, NULL);
2375 /* Overwrite the name */
2377 memset(dev_info->device_name.name, 0x00,
2378 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2379 g_strlcpy(dev_info->device_name.name, alias,
2380 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2382 } else if (!g_strcmp0(key, "IsAliasSet")) {
2383 dev_info->is_alias_set = g_variant_get_boolean(value);
2384 } else if (!g_strcmp0(key, "Connected")) {
2385 dev_info->connected = g_variant_get_byte(value);
2386 } else if (!g_strcmp0(key, "Paired")) {
2387 dev_info->paired = g_variant_get_boolean(value);
2388 } else if (!g_strcmp0(key, "Trusted")) {
2389 dev_info->trust = g_variant_get_boolean(value);
2390 } else if (!g_strcmp0(key, "RSSI")) {
2391 dev_info->rssi = g_variant_get_int16(value);
2392 } else if (!g_strcmp0(key, "UUIDs")) {
2398 dev_info->service_index = 0;
2399 g_variant_get(value, "as", &iter);
2400 while (g_variant_iter_loop(iter, "s", &uuid)) {
2401 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2402 parts = g_strsplit(uuid, "-", -1);
2404 if (parts == NULL || parts[0] == NULL) {
2409 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2414 dev_info->service_index = i;
2415 g_variant_iter_free(iter);
2416 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
2417 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2418 } else if (strcasecmp(key, "ManufacturerData") == 0) {
2419 manufacturer_data = g_byte_array_new();
2420 g_variant_get(value, "ay", &char_value_iter);
2421 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
2422 g_byte_array_append(manufacturer_data, &char_value, 1);
2424 if (manufacturer_data) {
2425 if (manufacturer_data->len > 0)
2426 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2428 g_variant_iter_free(char_value_iter);
2429 g_byte_array_free(manufacturer_data, TRUE);
2436 static void __bt_extract_device_info(GVariantIter *iter,
2439 bluetooth_device_info_t *dev_info = NULL;
2440 char *object_path = NULL;
2441 GVariantIter *interface_iter;
2442 GVariantIter *svc_iter;
2443 char *interface_str = NULL;
2445 /* Parse the signature: oa{sa{sv}}} */
2446 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2449 if (object_path == NULL)
2452 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2453 &interface_str, &svc_iter)) {
2454 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2455 BT_DBG("Found a device: %s", object_path);
2456 dev_info = __bt_parse_device_info(svc_iter);
2458 if (dev_info->paired == TRUE) {
2459 g_array_append_vals(*dev_list, dev_info,
2460 sizeof(bluetooth_device_info_t));
2464 g_free(interface_str);
2465 g_variant_iter_free(svc_iter);
2473 int _bt_get_bonded_devices(GArray **dev_list)
2476 GDBusConnection *conn;
2477 GDBusProxy *manager_proxy;
2478 GVariant *result = NULL;
2479 GVariantIter *iter = NULL;
2480 GError *error = NULL;
2482 conn = _bt_gdbus_get_system_gconn();
2483 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2485 manager_proxy = _bt_get_manager_proxy();
2486 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2488 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2490 G_DBUS_CALL_FLAGS_NONE,
2496 if (error != NULL) {
2497 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2498 g_clear_error(&error);
2500 BT_ERR("Failed to Failed to GetManagedObjects");
2501 return BLUETOOTH_ERROR_INTERNAL;
2504 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2505 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2507 __bt_extract_device_info(iter, dev_list);
2508 g_variant_iter_free(iter);
2509 g_variant_unref(result);
2512 return BLUETOOTH_ERROR_NONE;
2515 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2518 GDBusConnection *conn;
2519 GDBusProxy *manager_proxy;
2520 GVariant *result = NULL;
2521 GVariant *result1 = NULL;
2522 GVariantIter *iter = NULL;
2523 GError *error = NULL;
2524 char *object_path = NULL;
2525 GVariantIter *interface_iter;
2526 char *interface_str = NULL;
2527 GDBusProxy *device_proxy = NULL;
2528 gboolean is_connected = FALSE;
2530 conn = _bt_gdbus_get_system_gconn();
2531 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2533 manager_proxy = _bt_get_manager_proxy();
2534 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2536 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2538 G_DBUS_CALL_FLAGS_NONE,
2544 if (error != NULL) {
2545 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2546 g_clear_error(&error);
2549 BT_ERR("Failed to Failed to GetManagedObjects");
2550 return BLUETOOTH_ERROR_INTERNAL;
2553 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2554 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2556 /* Parse the signature: oa{sa{sv}}} */
2557 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2558 if (object_path == NULL)
2561 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2562 &interface_str, NULL)) {
2563 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2564 BT_DBG("Found a device: %s", object_path);
2565 g_free(interface_str);
2567 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2568 NULL, BT_BLUEZ_NAME,
2569 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2571 if (device_proxy == NULL) {
2572 BT_DBG("Device don't have this service");
2576 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2577 g_variant_new("(s)", profile_uuid),
2578 G_DBUS_CALL_FLAGS_NONE,
2583 if (result1 == NULL) {
2584 BT_ERR("Error occured in Proxy call");
2586 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2587 g_error_free(error);
2590 g_object_unref(device_proxy);
2593 g_variant_get(result1, "(b)", &is_connected);
2595 if (is_connected == TRUE) {
2596 char address[BT_ADDRESS_STRING_SIZE];
2597 bluetooth_device_address_t *addr = NULL;
2599 _bt_convert_device_path_to_address(object_path, address);
2601 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2602 _bt_convert_addr_string_to_type(addr->addr, address);
2604 g_array_append_vals(*addr_list, addr,
2605 sizeof(bluetooth_device_address_t));
2608 g_variant_unref(result1);
2609 g_object_unref(device_proxy);
2616 g_variant_unref(result);
2617 g_variant_iter_free(iter);
2620 return BLUETOOTH_ERROR_NONE;
2623 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2624 bluetooth_device_info_t *dev_info)
2626 char *object_path = NULL;
2627 GDBusProxy *adapter_proxy;
2628 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2629 int ret = BLUETOOTH_ERROR_NONE;
2631 BT_CHECK_PARAMETER(device_address, return);
2632 BT_CHECK_PARAMETER(dev_info, return);
2634 adapter_proxy = _bt_get_adapter_proxy();
2635 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2637 _bt_convert_addr_type_to_string(address, device_address->addr);
2639 object_path = _bt_get_device_object_path(address);
2641 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2643 ret = __bt_get_bonded_device_info(object_path, dev_info);
2644 g_free(object_path);
2649 int _bt_is_alias_set(bluetooth_device_address_t *device_address, gboolean *is_alias_set)
2651 char *object_path = NULL;
2652 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2653 gboolean alias_set = FALSE;
2655 GDBusConnection *conn;
2656 GDBusProxy *device_proxy;
2657 GError *error = NULL;
2658 GVariant *result = NULL;
2659 GVariant *temp = NULL;
2662 BT_CHECK_PARAMETER(device_address, return);
2663 BT_CHECK_PARAMETER(is_alias_set, return);
2665 _bt_convert_addr_type_to_string(address, device_address->addr);
2667 object_path = _bt_get_device_object_path(address);
2668 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2670 conn = _bt_gdbus_get_system_gconn();
2672 g_free(object_path);
2673 return BLUETOOTH_ERROR_INTERNAL;
2676 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2680 BT_PROPERTIES_INTERFACE,
2682 if (device_proxy == NULL) {
2683 g_free(object_path);
2684 return BLUETOOTH_ERROR_INTERNAL;
2687 result = g_dbus_proxy_call_sync(device_proxy, "Get",
2688 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "IsAliasSet"),
2689 G_DBUS_CALL_FLAGS_NONE,
2695 BT_ERR("Error occured in Proxy call");
2696 if (error != NULL) {
2697 BT_ERR("Getting is_alias_set property failed: [%s]\n", error->message);
2698 g_error_free(error);
2700 g_object_unref(device_proxy);
2701 g_free(object_path);
2702 return BLUETOOTH_ERROR_INTERNAL;
2705 g_variant_get(result, "(v)", &temp);
2706 alias_set = g_variant_get_boolean(temp);
2707 *is_alias_set = alias_set;
2708 BT_DBG("address: [%s] | *is_alias_set: %s", address, *is_alias_set ? "TRUE" : "FALSE");
2709 g_variant_unref(temp);
2710 g_variant_unref(result);
2711 g_object_unref(device_proxy);
2713 g_free(object_path);
2715 return BLUETOOTH_ERROR_NONE;
2718 int _bt_get_timeout_value(int *timeout)
2720 time_t current_time;
2723 /* Take current time */
2724 time(¤t_time);
2725 time_diff = difftime(current_time, visible_timer.start_time);
2727 BT_DBG("Time diff = %d\n", time_diff);
2729 *timeout = visible_timer.timeout - time_diff;
2731 return BLUETOOTH_ERROR_NONE;
2734 int _bt_set_le_privacy(gboolean set_privacy)
2737 GError *error = NULL;
2738 GVariant *result = NULL;
2740 if (__bt_is_factory_test_mode()) {
2741 BT_ERR("Unable to set le privacy in factory binary !!");
2742 return BLUETOOTH_ERROR_NOT_SUPPORT;
2745 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2746 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2747 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2750 proxy = _bt_get_adapter_proxy();
2751 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2753 result = g_dbus_proxy_call_sync(proxy,
2755 g_variant_new("(b)", set_privacy),
2756 G_DBUS_CALL_FLAGS_NONE,
2762 if (error != NULL) {
2763 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2764 g_clear_error(&error);
2766 BT_ERR("Failed to SetLePrivacy");
2767 return BLUETOOTH_ERROR_INTERNAL;
2770 g_variant_unref(result);
2771 BT_INFO("SetLePrivacy as %d", set_privacy);
2772 return BLUETOOTH_ERROR_NONE;
2775 int _bt_set_le_static_random_address(gboolean is_enable)
2778 GError *error = NULL;
2779 GVariant *result = NULL;
2781 if (__bt_is_factory_test_mode()) {
2782 BT_ERR("Unable to set le random address in factory binary !!");
2783 return BLUETOOTH_ERROR_NOT_SUPPORT;
2786 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2787 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2788 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2791 proxy = _bt_get_adapter_proxy();
2792 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2794 result = g_dbus_proxy_call_sync(proxy,
2795 "SetLeStaticRandomAddress",
2796 g_variant_new("(b)", is_enable),
2797 G_DBUS_CALL_FLAGS_NONE,
2803 if (error != NULL) {
2804 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2805 g_clear_error(&error);
2807 BT_ERR("Failed to SetLeStaticRandomAddress");
2808 return BLUETOOTH_ERROR_INTERNAL;
2811 g_variant_unref(result);
2812 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2813 return BLUETOOTH_ERROR_NONE;
2816 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2819 GError *error = NULL;
2823 GVariantBuilder *builder;
2825 BT_CHECK_PARAMETER(m_data, return);
2827 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2828 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2829 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2832 proxy = _bt_get_adapter_proxy();
2833 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2835 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2837 for (i = 0; i < (m_data->data_len) + 2; i++)
2838 g_variant_builder_add(builder, "y", m_data->data[i]);
2840 val = g_variant_new("(ay)", builder);
2842 result = g_dbus_proxy_call_sync(proxy,
2843 "SetManufacturerData",
2845 G_DBUS_CALL_FLAGS_NONE,
2849 g_variant_builder_unref(builder);
2851 if (error != NULL) {
2852 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2853 g_clear_error(&error);
2855 BT_ERR("Failed to SetManufacturerData");
2857 return BLUETOOTH_ERROR_INTERNAL;
2859 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2861 for (i = 0; i < (m_data->data_len) + 2; i++)
2862 g_variant_builder_add(builder, "y", m_data->data[i]);
2864 val = g_variant_new("(ay)", builder);
2866 _bt_send_event(BT_ADAPTER_EVENT,
2867 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2870 BT_INFO("Set manufacturer data");
2872 g_variant_builder_unref(builder);
2873 g_variant_unref(result);
2875 return BLUETOOTH_ERROR_NONE;
2879 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2881 int result = BLUETOOTH_ERROR_NONE;
2882 bt_service_alarm_t *alarm = NULL;
2884 if (!call_back || !alarm_id)
2885 return BLUETOOTH_ERROR_INVALID_PARAM;
2887 if (!alarm_mgr.is_alarm_initialized) {
2888 result = alarmmgr_init("bt-service");
2890 BT_ERR("Failed to initialize alarm = %d", result);
2891 result = BLUETOOTH_ERROR_INTERNAL;
2894 result = alarmmgr_set_cb(alarm_cb, NULL);
2896 BT_ERR("Failed to set the callback = %d", result);
2897 result = BLUETOOTH_ERROR_INTERNAL;
2900 alarm_mgr.is_alarm_initialized = TRUE;
2903 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2905 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2907 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2910 BT_ERR("Failed to create alarm error = %d", result);
2911 result = BLUETOOTH_ERROR_INTERNAL;
2915 alarm->alarm_id = *alarm_id;
2916 alarm->callback = call_back;
2917 alarm->user_data = user_data;
2919 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2920 result = BLUETOOTH_ERROR_NONE;
2925 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2928 bt_service_alarm_t *p_data;
2929 bt_set_alarm_cb callback = NULL;
2930 void *user_data = NULL;
2932 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2933 GINT_TO_POINTER(alarm_id), compare_alarm);
2937 p_data = (bt_service_alarm_t *)node->data;
2938 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2944 callback = p_data->callback;
2945 user_data = p_data->user_data;
2949 callback(alarm_id, user_data);
2954 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2957 bt_service_alarm_t *p_data;
2958 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2961 alarmmgr_remove_alarm(alarm_id);
2962 p_data = (bt_service_alarm_t *)list->data;
2963 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2970 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2973 alarm_id_t alarm_id = (alarm_id_t)(uintptr_t)data;
2975 alarm_id_t alarm_id = (alarm_id_t)data;
2977 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2979 if (p_data->alarm_id == alarm_id)
2985 static void alarm_data_free(void *data)
2987 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2992 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2996 int function = (int)(uintptr_t)user_data;
2998 int function = (int)user_data;
3002 case BT_ENABLE_ADAPTER:
3003 result = _bt_enable_adapter();
3004 if (result != BLUETOOTH_ERROR_NONE) {
3005 BT_ERR("_bt_enable_adapter is failed");
3006 /* Send enabled event to API */
3007 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
3008 g_variant_new("(i)", result));
3011 case BT_DISABLE_ADAPTER:
3012 result = _bt_disable_adapter();
3013 if (result != BLUETOOTH_ERROR_NONE) {
3014 BT_ERR("_bt_disable_adapter is failed");
3015 /* Send disabled event to API */
3016 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
3017 g_variant_new("(i)", result));
3021 BT_ERR("function is NOT matched");
3028 int _bt_adapter_request_delayed(int function)
3033 case BT_ENABLE_ADAPTER:
3034 ret = _bt_enable_adapter_check_status();
3035 if (ret == BLUETOOTH_ERROR_NONE)
3036 _bt_adapter_set_status(BT_ACTIVATING);
3041 case BT_DISABLE_ADAPTER:
3042 ret = _bt_disable_adapter_check_status();
3043 if (ret == BLUETOOTH_ERROR_NONE)
3044 _bt_adapter_set_status(BT_DEACTIVATING);
3050 BT_ERR("function is NOT matched");
3051 return BLUETOOTH_ERROR_INTERNAL;
3055 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void *)(uintptr_t)function);
3057 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void *)function);
3060 return BLUETOOTH_ERROR_NONE;
3063 #ifdef TIZEN_PROFILE_TV
3064 int _bt_get_enable_timer_id(void)