2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <syspopup_caller.h>
26 #include <dbus/dbus-glib.h>
27 #include <dbus/dbus.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
33 #include "bluetooth-api.h"
34 #include "bt-internal-types.h"
35 #include "bt-service-common.h"
36 #include "bt-service-event.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-util.h"
39 #include "bt-service-network.h"
40 #include "bt-service-obex-server.h"
41 #include "bt-service-opp-client.h"
42 #include "bt-service-agent.h"
43 #include "bt-service-main.h"
44 #include "bt-service-avrcp.h"
45 #include "bt-service-device.h"
46 #ifdef TIZEN_DPM_ENABLE
47 #include "bt-service-dpm.h"
57 bt_adapter_timer_t visible_timer = {0, };
61 bt_set_alarm_cb callback;
66 gboolean is_alarm_initialized;
68 } bt_service_alarm_mgr_t;
70 static bt_service_alarm_mgr_t alarm_mgr = {0, };
72 static gboolean is_discovering;
73 static gboolean cancel_by_user;
74 static bt_status_t adapter_status = BT_DEACTIVATED;
75 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
76 static gboolean is_le_intended = FALSE;
77 static void *adapter_agent = NULL;
78 static GDBusProxy *core_proxy = NULL;
79 static guint timer_id = 0;
80 static guint le_timer_id = 0;
82 static uint status_reg_id;
84 #define BT_CORE_NAME "org.projectx.bt_core"
85 #define BT_CORE_PATH "/org/projectx/bt_core"
86 #define BT_CORE_INTERFACE "org.projectx.btcore"
88 #define BT_DISABLE_TIME 500 /* 500 ms */
90 static int alarm_cb(alarm_id_t alarm_id, void* user_param);
91 static void alarm_data_free(void *data);
93 GDBusProxy *_bt_init_core_proxy(void)
96 GDBusConnection *conn;
98 conn = _bt_get_system_gconn();
102 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
117 static GDBusProxy *__bt_get_core_proxy(void)
119 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
122 static gboolean __bt_is_factory_test_mode(void)
126 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
127 BT_ERR("Get the DUT Mode fail");
132 BT_INFO("DUT Test Mode !!");
139 static gboolean __bt_timeout_handler(gpointer user_data)
141 int result = BLUETOOTH_ERROR_NONE;
145 /* Take current time */
147 time_diff = difftime(current_time, visible_timer.start_time);
149 /* Send event to application */
150 _bt_send_event(BT_ADAPTER_EVENT,
151 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
152 g_variant_new("(in)", result, time_diff));
154 if (visible_timer.timeout <= time_diff) {
155 g_source_remove(visible_timer.event_id);
156 visible_timer.event_id = 0;
157 visible_timer.timeout = 0;
159 #ifndef TIZEN_PROFILE_WEARABLE
160 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
161 BT_ERR("Set vconf failed\n");
169 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
172 int result = BLUETOOTH_ERROR_NONE;
175 if (alarm_id != visible_timer.alarm_id)
178 if (visible_timer.event_id) {
179 _bt_send_event(BT_ADAPTER_EVENT,
180 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
181 g_variant_new("(in)", result, timeout));
182 g_source_remove(visible_timer.event_id);
183 visible_timer.event_id = 0;
184 visible_timer.timeout = 0;
186 #ifndef TIZEN_PROFILE_WEARABLE
187 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
188 BT_ERR("Set vconf failed\n");
191 /* Switch Off visibility in Bluez */
192 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
193 visible_timer.alarm_id = 0;
197 static void __bt_visibility_alarm_remove()
199 if (visible_timer.event_id > 0) {
200 g_source_remove(visible_timer.event_id);
201 visible_timer.event_id = 0;
204 if (visible_timer.alarm_id > 0) {
205 _bt_service_remove_alarm(visible_timer.alarm_id);
206 visible_timer.alarm_id = 0;
210 int __bt_set_visible_time(int timeout)
215 __bt_visibility_alarm_remove();
217 visible_timer.timeout = timeout;
219 #ifndef TIZEN_PROFILE_WEARABLE
220 #ifdef TIZEN_DPM_ENABLE
221 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
223 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
224 BT_ERR("Set vconf failed");
225 #ifdef TIZEN_DPM_ENABLE
232 return BLUETOOTH_ERROR_NONE;
234 result = _bt_service_set_alarm(visible_timer.timeout,
235 __bt_visibility_alarm_cb, NULL, &alarm_id);
236 if (result != BLUETOOTH_ERROR_NONE)
237 return BLUETOOTH_ERROR_INTERNAL;
238 visible_timer.alarm_id = alarm_id;
239 /* Take start time */
240 time(&(visible_timer.start_time));
241 visible_timer.event_id = g_timeout_add_seconds(1,
242 __bt_timeout_handler, NULL);
244 return BLUETOOTH_ERROR_NONE;
247 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
254 ret_if(value == NULL);
257 dev->service_index = 0;
259 g_variant_get(value, "as", &iter);
260 while (g_variant_iter_loop(iter, "s", &uuid)) {
261 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
262 parts = g_strsplit(uuid, "-", -1);
264 if (parts == NULL || parts[0] == NULL) {
269 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
272 dev->service_index++;
275 g_variant_iter_free(iter);
278 static int __bt_get_bonded_device_info(gchar *device_path,
279 bluetooth_device_info_t *dev_info)
281 GError *error = NULL;
282 GDBusProxy *device_proxy;
283 gchar *address = NULL;
285 unsigned int cod = 0;
287 gboolean trust = FALSE;
288 gboolean paired = FALSE;
289 guchar connected = 0;
290 GByteArray *manufacturer_data = NULL;
292 GDBusConnection *conn;
294 GVariantIter *property_iter;
298 GVariantIter *char_value_iter;
300 BT_CHECK_PARAMETER(device_path, return);
301 BT_CHECK_PARAMETER(dev_info, return);
303 conn = _bt_get_system_gconn();
304 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
306 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
310 BT_PROPERTIES_INTERFACE,
313 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
315 result = g_dbus_proxy_call_sync(device_proxy,
317 g_variant_new("(s)", BT_DEVICE_INTERFACE),
318 G_DBUS_CALL_FLAGS_NONE,
324 BT_ERR("Error occured in Proxy call");
326 BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
327 g_clear_error(&error);
329 g_object_unref(device_proxy);
330 return BLUETOOTH_ERROR_INTERNAL;
333 g_object_unref(device_proxy);
335 g_variant_get(result, "(a{sv})", &property_iter);
337 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
338 if (!g_strcmp0(key, "Paired")) {
339 paired = g_variant_get_boolean(value);
340 } else if (!g_strcmp0(key, "Address")) {
341 g_variant_get(value, "s", &address);
342 } else if (!g_strcmp0(key, "Alias")) {
343 g_variant_get(value, "s", &name);
344 } else if (!g_strcmp0(key, "Name")) {
346 g_variant_get(value, "s", &name);
347 } else if(!g_strcmp0(key, "IsAliasSet")) {
348 dev_info->is_alias_set = g_variant_get_boolean(value);
349 } else if (!g_strcmp0(key, "Class")) {
350 cod = g_variant_get_uint32(value);
351 } else if (!g_strcmp0(key, "Connected")) {
352 connected = g_variant_get_byte(value);
353 } else if (!g_strcmp0(key, "Trusted")) {
354 trust = g_variant_get_boolean(value);
355 } else if (!g_strcmp0(key, "RSSI")) {
356 rssi = g_variant_get_int16(value);
357 } else if (!g_strcmp0(key, "UUIDs")) {
358 __bt_get_service_list(value, dev_info);
359 } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
360 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
361 } else if (!g_strcmp0(key, "ManufacturerData")) {
362 manufacturer_data = g_byte_array_new();
363 g_variant_get(value, "ay", &char_value_iter);
364 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
365 g_byte_array_append(manufacturer_data, &char_value, 1);
367 g_variant_iter_free(char_value_iter);
369 if (manufacturer_data) {
370 if (manufacturer_data->len > 0) {
371 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
372 manufacturer_data->len);
375 g_byte_array_free(manufacturer_data, TRUE);
378 g_variant_iter_free(property_iter);
380 BT_DBG("trust: %d, paired: %d", trust, paired);
382 g_variant_unref(result);
384 if ((paired == FALSE) && (trust == FALSE)) {
387 return BLUETOOTH_ERROR_NOT_PAIRED;
390 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
393 _bt_divide_device_class(&dev_info->device_class, cod);
395 g_strlcpy(dev_info->device_name.name, name,
396 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
398 dev_info->rssi = rssi;
399 dev_info->trust = trust;
400 dev_info->paired = paired;
401 dev_info->connected = connected;
402 ret = BLUETOOTH_ERROR_NONE;
409 void _bt_set_discovery_status(gboolean mode)
411 is_discovering = mode;
414 void _bt_set_cancel_by_user(gboolean value)
416 cancel_by_user = value;
419 gboolean _bt_get_cancel_by_user(void)
421 return cancel_by_user;
424 void _bt_adapter_set_status(bt_status_t status)
426 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
427 adapter_status = status;
430 bt_status_t _bt_adapter_get_status(void)
432 return adapter_status;
435 void _bt_adapter_set_le_status(bt_le_status_t status)
437 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
438 adapter_le_status = status;
441 bt_le_status_t _bt_adapter_get_le_status(void)
443 return adapter_le_status;
447 void _bt_set_le_intended_status(gboolean value)
449 is_le_intended = value;
452 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
454 char *phone_name = NULL;
460 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
461 phone_name = vconf_keynode_get_str(node);
463 if (phone_name && strlen(phone_name) != 0) {
464 if (!g_utf8_validate(phone_name, -1,
465 (const char **)&ptr))
468 _bt_set_local_name(phone_name);
473 static void __bt_set_local_name(void)
475 bluetooth_device_name_t local_name;
476 char *phone_name = NULL;
480 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
481 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
482 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
487 if (strlen(phone_name) != 0) {
488 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
491 _bt_set_local_name(phone_name);
497 static int __bt_set_enabled(void)
499 int adapter_status = BT_ADAPTER_DISABLED;
500 int result = BLUETOOTH_ERROR_NONE;
503 BT_DBG("g_source is removed");
504 g_source_remove(timer_id);
508 _bt_check_adapter(&adapter_status);
510 if (adapter_status == BT_ADAPTER_DISABLED) {
511 BT_ERR("Bluetoothd is not running");
512 return BLUETOOTH_ERROR_INTERNAL;
515 #ifdef TIZEN_PROFILE_MOBILE
516 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
517 if (_bt_set_discoverable_mode(
518 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
519 BT_ERR("Set connectable mode failed");
522 if (_bt_set_discoverable_mode(
523 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
524 BT_ERR("Fail to set discoverable mode");
528 /* Update Bluetooth Status to notify other modules */
529 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
530 BT_ERR("Set vconf failed\n");
532 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
533 BT_ERR("Set vconf failed\n");
535 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
536 EVT_VAL_BT_ON) != ES_R_OK)
537 BT_ERR("Fail to set value");
539 /* Send enabled event to API */
540 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
541 g_variant_new("(i)", result));
543 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
544 _bt_audio_start_auto_connect(FALSE);
547 __bt_set_local_name();
549 return BLUETOOTH_ERROR_NONE;
552 void _bt_set_disabled(int result)
554 int power_off_status = 0;
557 int pm_ignore_mode = 0;
559 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
560 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
562 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
564 /* Update the vconf BT status in normal Deactivation case only */
565 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
566 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
568 BT_DBG("Update vconf for BT normal Deactivation");
570 if (result == BLUETOOTH_ERROR_TIMEOUT)
571 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
572 BT_ERR("Set vconf failed");
574 /* Update Bluetooth Status to notify other modules */
575 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
576 BT_ERR("Set vconf failed");
578 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
579 EVT_VAL_BT_OFF) != ES_R_OK)
580 BT_ERR("Fail to set value");
583 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
584 BT_ERR("Set vconf failed\n");
586 _bt_cancel_queued_transfers();
587 _bt_adapter_set_status(BT_DEACTIVATED);
589 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
590 /* Send disabled event */
591 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
592 g_variant_new("(i)", result));
595 BT_INFO("Adapter disabled");
598 static int __bt_set_le_enabled(void)
601 int result = BLUETOOTH_ERROR_NONE;
604 /* Update Bluetooth Status to notify other modules */
605 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
606 BT_ERR("Set vconf failed\n");
608 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
609 EVT_VAL_BT_LE_ON) != ES_R_OK)
610 BT_ERR("Fail to set value");
612 /* Send enabled event to API */
614 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
615 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
617 status = _bt_adapter_get_status();
618 if (status == BT_DEACTIVATED) {
619 BT_INFO("BREDR is off, turn off PSCAN");
620 _bt_set_connectable(FALSE);
622 if (le_timer_id > 0) {
623 g_source_remove(le_timer_id);
627 /* Send enabled event to API */
628 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
629 g_variant_new("(i)", result));
631 __bt_set_local_name();
634 return BLUETOOTH_ERROR_NONE;
637 void _bt_set_le_disabled(int result)
639 int power_off_status;
642 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
643 BT_DBG("ret : %d", ret);
644 BT_DBG("power_off_status : %d", power_off_status);
646 /* Update Bluetooth Status to notify other modules */
647 BT_DBG("Update vconf for BT LE normal Deactivation");
648 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
649 BT_ERR("Set vconf failed\n");
650 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
652 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
653 EVT_VAL_BT_LE_OFF) != ES_R_OK)
654 BT_ERR("Fail to set value");
656 /* Send disabled event */
657 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
658 g_variant_new_int32(result));
661 void *_bt_get_adapter_agent(void)
663 return adapter_agent;
666 int _bt_enable_core(void)
670 GError *error = NULL;
672 proxy = __bt_get_core_proxy();
673 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
675 /* Clean up the process */
676 result = g_dbus_proxy_call_sync(proxy,
679 G_DBUS_CALL_FLAGS_NONE,
686 BT_ERR("Bt core call failed(Error: %s)", error->message);
687 g_clear_error(&error);
689 BT_ERR("Bt core call failed");
690 return BLUETOOTH_ERROR_INTERNAL;
693 g_variant_unref(result);
694 return BLUETOOTH_ERROR_NONE;
697 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
698 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
700 gboolean flight_mode = FALSE;
701 int power_saving_mode = 0;
704 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
705 type = vconf_keynode_get_type(node);
706 if (type == VCONF_TYPE_BOOL) {
707 flight_mode = vconf_keynode_get_bool(node);
708 if (flight_mode != TRUE) {
709 BT_ERR("Ignore the event");
712 } else if (type == VCONF_TYPE_INT) {
713 power_saving_mode = vconf_keynode_get_int(node);
714 if (power_saving_mode != 2) {
715 BT_ERR("Ignore the event");
719 BT_ERR("Invaild vconf key type : %d", type);
727 void _bt_service_register_vconf_handler(void)
731 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
732 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
733 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
734 BT_ERR("Unable to register key handler");
736 BT_DBG("Telephony is disabled");
739 #ifndef TIZEN_PROFILE_WEARABLE
740 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
741 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
742 BT_ERR("Unable to register key handler");
746 void _bt_service_unregister_vconf_handler(void)
750 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
751 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
752 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
755 #ifndef TIZEN_PROFILE_WEARABLE
756 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
757 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
761 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
763 const char *bt_status = NULL;
764 const char *bt_le_status = NULL;
765 BT_DBG("bt state set event(%s) received", event_name);
767 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
768 BT_DBG("bt_state: (%s)", bt_status);
770 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
771 BT_DBG("bt_state: (%s)", bt_le_status);
774 void _bt_handle_adapter_added(void)
778 bt_le_status_t le_status;
782 BT_DBG("g_source is removed");
783 g_source_remove(timer_id);
788 status = _bt_adapter_get_status();
789 le_status = _bt_adapter_get_le_status();
790 BT_DBG("status : %d", status);
791 BT_DBG("le_status : %d", le_status);
793 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
794 if (!adapter_agent) {
795 BT_ERR("Fail to register agent");
799 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
800 BT_ERR("Fail to register media player");
802 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
803 BT_ERR("Fail to init obex server");
805 #ifdef TIZEN_BT_PAN_NAP_ENABLED
806 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
807 BT_ERR("Fail to activate network");
810 /* add the vconf noti handler */
811 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
812 __bt_phone_name_changed_cb, NULL);
814 BT_ERR("Unable to register key handler");
816 if (le_status == BT_LE_ACTIVATING ||
817 status == BT_ACTIVATING) {
818 __bt_set_le_enabled();
819 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
822 if (status == BT_ACTIVATING) {
824 _bt_adapter_set_status(BT_ACTIVATED);
827 _bt_service_register_vconf_handler();
830 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
831 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
832 BT_ERR("Fail to register system event");
836 void _bt_handle_adapter_removed(void)
840 _bt_adapter_set_status(BT_DEACTIVATED);
842 __bt_visibility_alarm_remove();
844 if (alarm_mgr.is_alarm_initialized == TRUE) {
846 alarm_mgr.is_alarm_initialized = FALSE;
847 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
848 alarm_mgr.g_alarm_list = NULL;
851 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
852 _bt_audio_stop_auto_connect();
855 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
856 (vconf_callback_fn)__bt_phone_name_changed_cb);
858 ERR("vconf_ignore_key_changed failed\n");
861 _bt_destroy_agent(adapter_agent);
862 adapter_agent = NULL;
864 #ifndef USB_BLUETOOTH
865 _bt_reliable_terminate_service(NULL);
868 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK) {
869 BT_ERR("Fail to unregister system event");
874 static gboolean __bt_enable_timeout_cb(gpointer user_data)
878 GError *error = NULL;
882 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
884 BT_ERR("EnableAdapter is failed");
886 proxy = __bt_get_core_proxy();
890 /* Clean up the process */
891 result = g_dbus_proxy_call_sync(proxy,
894 G_DBUS_CALL_FLAGS_NONE,
901 BT_ERR("Bt core call failed(Error: %s)", error->message);
902 g_clear_error(&error);
904 BT_ERR("Bt core call failed");
909 g_variant_unref(result);
910 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
912 #ifndef USB_BLUETOOTH
913 _bt_terminate_service(NULL);
919 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
923 GError *error = NULL;
927 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
929 BT_ERR("EnableAdapterLE is failed");
931 proxy = __bt_get_core_proxy();
935 /* Clean up the process */
936 result = g_dbus_proxy_call_sync(proxy,
939 G_DBUS_CALL_FLAGS_NONE,
946 BT_ERR("Bt core call failed(Error: %s)", error->message);
947 g_clear_error(&error);
949 BT_ERR("Bt core call failed");
953 g_variant_unref(result);
954 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
956 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
958 if (_bt_adapter_get_status() == BT_DEACTIVATED)
959 _bt_terminate_service(NULL);
964 void _bt_adapter_start_le_enable_timer(void)
966 if (le_timer_id > 0) {
967 g_source_remove(le_timer_id);
971 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
972 __bt_enable_le_timeout_cb, NULL);
977 void _bt_adapter_start_enable_timer(void)
980 g_source_remove(timer_id);
984 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
985 __bt_enable_timeout_cb, NULL);
991 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
996 _bt_adapter_set_status(BT_ACTIVATED);
1002 int _bt_enable_adapter(void)
1005 GError *error = NULL;
1007 GVariant *result = NULL;
1008 bt_status_t status = _bt_adapter_get_status();
1009 bt_le_status_t le_status = _bt_adapter_get_le_status();
1013 if (status == BT_ACTIVATING) {
1014 BT_ERR("Enabling in progress");
1015 return BLUETOOTH_ERROR_IN_PROGRESS;
1018 if (status == BT_ACTIVATED) {
1019 BT_ERR("Already enabled");
1020 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1023 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1024 BT_ERR("Disabling in progress");
1025 return BLUETOOTH_ERROR_DEVICE_BUSY;
1028 _bt_adapter_set_status(BT_ACTIVATING);
1032 int adapter_status = BT_ADAPTER_DISABLED;
1034 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1035 BT_ERR("Set vconf failed");
1037 _bt_check_adapter(&adapter_status);
1038 if (adapter_status == BT_ADAPTER_ENABLED) {
1039 g_idle_add(__bt_adapter_enabled_cb, NULL);
1040 _bt_adapter_start_enable_timer();
1041 return BLUETOOTH_ERROR_NONE;
1046 proxy = __bt_get_core_proxy();
1048 return BLUETOOTH_ERROR_INTERNAL;
1050 if (le_status == BT_LE_ACTIVATED) {
1051 BT_INFO("LE Already enabled. Just turn on PSCAN");
1052 ret = _bt_set_connectable(TRUE);
1053 if (ret == BLUETOOTH_ERROR_NONE) {
1054 _bt_adapter_set_status(BT_ACTIVATED);
1056 return BLUETOOTH_ERROR_INTERNAL;
1060 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1062 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1065 BT_ERR("EnableAdapterLe failed: %s", error->message);
1066 _bt_adapter_set_status(BT_DEACTIVATED);
1067 g_clear_error(&error);
1069 result = g_dbus_proxy_call_sync(proxy,
1072 G_DBUS_CALL_FLAGS_NONE,
1077 if (error != NULL) {
1078 BT_ERR("Bt core call failed(Error: %s)", error->message);
1079 g_clear_error(&error);
1081 g_variant_unref(result);
1082 /* Terminate myself */
1083 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1084 return BLUETOOTH_ERROR_INTERNAL;
1086 g_variant_unref(result);
1087 if (le_status == BT_LE_ACTIVATED) {
1090 _bt_adapter_start_enable_timer();
1093 return BLUETOOTH_ERROR_NONE;
1096 static gboolean __bt_disconnect_all(void)
1099 GDBusConnection *conn;
1100 GDBusProxy *dev_proxy;
1101 gboolean ret = FALSE;
1103 GError *error = NULL;
1104 GArray *device_list;
1105 bluetooth_device_info_t info;
1107 char *device_path = NULL;
1108 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1113 conn = _bt_get_system_gconn();
1115 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1117 if (_bt_get_bonded_devices(&device_list)
1118 != BLUETOOTH_ERROR_NONE) {
1119 g_array_free(device_list, TRUE);
1123 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1125 for (i = 0; i < size; i++) {
1127 info = g_array_index(device_list,
1128 bluetooth_device_info_t, i);
1130 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1131 BT_DBG("Found Connected device");
1132 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1133 device_path = _bt_get_device_object_path(address);
1134 if (device_path == NULL)
1137 BT_DBG("Disconnecting : %s", device_path);
1139 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1143 BT_DEVICE_INTERFACE,
1146 if (dev_proxy == NULL)
1149 result = g_dbus_proxy_call_sync(dev_proxy,
1152 G_DBUS_CALL_FLAGS_NONE,
1158 if (error != NULL) {
1159 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1160 g_clear_error(&error);
1162 BT_ERR("Disconnect call failed");
1163 g_object_unref(dev_proxy);
1167 g_variant_unref(result);
1168 g_object_unref(dev_proxy);
1172 g_array_free(device_list, TRUE);
1178 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1181 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1187 int _bt_disable_cb(void)
1195 GError *error = NULL;
1197 _bt_adapter_set_status(BT_DEACTIVATING);
1199 bt_le_status_t le_status;
1200 le_status = _bt_adapter_get_le_status();
1201 BT_DBG("le_status : %d", le_status);
1202 if (le_status == BT_LE_ACTIVATED) {
1203 BT_INFO("LE is enabled. Just turn off PSCAN");
1205 if (_bt_is_discovering())
1206 _bt_cancel_discovery();
1208 if (_bt_is_connectable() == FALSE) {
1209 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1211 ret = _bt_set_connectable(FALSE);
1212 if (ret != BLUETOOTH_ERROR_NONE) {
1213 BT_ERR("_bt_set_connectable fail!");
1214 _bt_adapter_set_status(BT_ACTIVATED);
1215 return BLUETOOTH_ERROR_INTERNAL;
1220 proxy = __bt_get_core_proxy();
1221 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1223 result = g_dbus_proxy_call_sync(proxy,
1226 G_DBUS_CALL_FLAGS_NONE,
1232 if (error != NULL) {
1233 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1234 g_clear_error(&error);
1236 BT_ERR("Failed to DisableAdapter");
1237 _bt_adapter_set_status(BT_ACTIVATED);
1238 return BLUETOOTH_ERROR_INTERNAL;
1241 g_variant_unref(result);
1242 return BLUETOOTH_ERROR_NONE;
1245 int _bt_disable_adapter(void)
1250 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1251 BT_DBG("Disabling in progress");
1252 return BLUETOOTH_ERROR_IN_PROGRESS;
1255 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1256 BT_DBG("Already disabled");
1257 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1261 g_source_remove(timer_id);
1265 __bt_disconnect_all();
1266 ret = _bt_disable_cb();
1272 int _bt_recover_adapter(void)
1277 GError *error = NULL;
1279 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1280 BT_DBG("Disabling in progress");
1281 return BLUETOOTH_ERROR_IN_PROGRESS;
1284 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1285 BT_DBG("Already disabled");
1286 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1289 _bt_adapter_set_status(BT_DEACTIVATING);
1291 proxy = __bt_get_core_proxy();
1292 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1294 result = g_dbus_proxy_call_sync(proxy,
1297 G_DBUS_CALL_FLAGS_NONE,
1303 if (error != NULL) {
1304 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1305 g_clear_error(&error);
1307 BT_ERR("Failed to RecoverAdapter");
1308 return BLUETOOTH_ERROR_INTERNAL;
1311 g_variant_unref(result);
1312 __bt_disconnect_all();
1315 return BLUETOOTH_ERROR_NONE;
1318 int _bt_reset_adapter(void)
1322 GError *error = NULL;
1326 proxy = __bt_get_core_proxy();
1328 return BLUETOOTH_ERROR_INTERNAL;
1330 result = g_dbus_proxy_call_sync(proxy,
1333 G_DBUS_CALL_FLAGS_NONE,
1339 if (error != NULL) {
1340 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1341 g_clear_error(&error);
1343 BT_ERR("Failed to ResetAdapter");
1344 return BLUETOOTH_ERROR_INTERNAL;
1347 g_variant_unref(result);
1348 /* Terminate myself */
1349 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1350 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1353 return BLUETOOTH_ERROR_NONE;
1357 int _bt_check_adapter(int *status)
1360 char *adapter_path = NULL;
1362 BT_CHECK_PARAMETER(status, return);
1364 *status = BT_ADAPTER_DISABLED;
1366 adapter_path = _bt_get_adapter_path();
1369 if (adapter_path != NULL)
1370 *status = BT_ADAPTER_ENABLED;
1372 g_free(adapter_path);
1373 return BLUETOOTH_ERROR_NONE;
1376 int _bt_check_adapter(int *status)
1379 GError *error = NULL;
1382 gboolean powered = FALSE;
1384 BT_CHECK_PARAMETER(status, return);
1386 *status = BT_ADAPTER_DISABLED;
1388 proxy = _bt_get_adapter_properties_proxy();
1389 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1391 result = g_dbus_proxy_call_sync(proxy,
1393 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1395 G_DBUS_CALL_FLAGS_NONE,
1401 BT_ERR("Failed to get local address");
1402 if (error != NULL) {
1403 BT_ERR("Failed to get local address (Error: %s)", error->message);
1404 g_clear_error(&error);
1406 return BLUETOOTH_ERROR_INTERNAL;
1409 g_variant_get(result, "(v)", &temp);
1410 powered = g_variant_get_boolean(temp);
1411 BT_DBG("powered: %d", powered);
1414 *status = BT_ADAPTER_ENABLED;
1416 g_variant_unref(result);
1417 g_variant_unref(temp);
1418 return BLUETOOTH_ERROR_NONE;
1422 int _bt_enable_adapter_le(void)
1426 GError *error = NULL;
1427 bt_status_t status = _bt_adapter_get_status();
1428 bt_le_status_t le_status = _bt_adapter_get_le_status();
1431 if (le_status == BT_LE_ACTIVATING) {
1432 BT_ERR("Enabling in progress");
1433 return BLUETOOTH_ERROR_IN_PROGRESS;
1436 if (le_status == BT_LE_ACTIVATED) {
1437 BT_ERR("Already enabled");
1438 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1441 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1442 BT_ERR("Disabling in progress");
1443 return BLUETOOTH_ERROR_DEVICE_BUSY;
1446 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1448 proxy = __bt_get_core_proxy();
1449 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1451 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1453 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1456 BT_ERR("EnableAdapterLe failed: %s", error->message);
1457 _bt_adapter_set_le_status(BT_DEACTIVATED);
1458 g_clear_error(&error);
1460 /* Clean up the process */
1461 result = g_dbus_proxy_call_sync(proxy,
1464 G_DBUS_CALL_FLAGS_NONE,
1470 BT_ERR("Bt core call failed");
1472 BT_ERR("EnableAdapterLE Failed %s", error->message);
1473 g_clear_error(&error);
1476 g_variant_unref(result);
1477 /* Terminate myself */
1478 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1479 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1480 return BLUETOOTH_ERROR_INTERNAL;
1484 g_variant_unref(result);
1486 _bt_adapter_start_le_enable_timer();
1488 if (status == BT_ACTIVATED) {
1489 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1490 __bt_set_le_enabled();
1492 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1494 return BLUETOOTH_ERROR_NONE;
1497 int _bt_disable_adapter_le(void)
1501 bt_le_status_t bt_le_state;
1503 GError *error = NULL;
1505 bt_le_state = _bt_adapter_get_le_status();
1506 if (bt_le_state == BT_LE_DEACTIVATING) {
1507 BT_DBG("Disabling in progress");
1508 return BLUETOOTH_ERROR_IN_PROGRESS;
1511 if (bt_le_state == BT_LE_DEACTIVATED) {
1512 BT_DBG("Already disabled");
1513 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1516 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1518 proxy = __bt_get_core_proxy();
1520 return BLUETOOTH_ERROR_INTERNAL;
1522 result = g_dbus_proxy_call_sync(proxy,
1525 G_DBUS_CALL_FLAGS_NONE,
1531 if (error != NULL) {
1532 BT_ERR("Bt core call failed (Error: %s)", error->message);
1533 g_clear_error(&error);
1535 BT_ERR("Bt core call failed");
1536 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1537 return BLUETOOTH_ERROR_INTERNAL;
1540 g_variant_unref(result);
1541 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1542 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1544 return BLUETOOTH_ERROR_NONE;
1547 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1551 GError *error = NULL;
1552 const char *address;
1556 BT_CHECK_PARAMETER(local_address, return);
1558 proxy = _bt_get_adapter_properties_proxy();
1559 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1561 result = g_dbus_proxy_call_sync(proxy,
1563 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1565 G_DBUS_CALL_FLAGS_NONE,
1571 BT_ERR("Failed to get local address");
1572 if (error != NULL) {
1573 BT_ERR("Failed to get local address (Error: %s)", error->message);
1574 g_clear_error(&error);
1576 return BLUETOOTH_ERROR_INTERNAL;
1579 g_variant_get(result, "(v)", &temp);
1580 address = g_variant_get_string(temp, NULL);
1581 BT_DBG("Address:%s", address);
1584 _bt_convert_addr_string_to_type(local_address->addr, address);
1586 return BLUETOOTH_ERROR_INTERNAL;
1589 g_variant_unref(result);
1590 g_variant_unref(temp);
1591 return BLUETOOTH_ERROR_NONE;
1594 int _bt_get_local_version(bluetooth_version_t *local_version)
1597 const char *ver = NULL;
1599 int ret = BLUETOOTH_ERROR_NONE;
1603 BT_CHECK_PARAMETER(local_version, return);
1605 GError *error = NULL;
1607 proxy = _bt_get_adapter_properties_proxy();
1608 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1610 result = g_dbus_proxy_call_sync(proxy,
1612 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1614 G_DBUS_CALL_FLAGS_NONE,
1620 if (error != NULL) {
1621 BT_ERR("Failed to get local version (Error: %s)", error->message);
1622 g_clear_error(&error);
1624 BT_ERR("Failed to get local version");
1625 return BLUETOOTH_ERROR_INTERNAL;
1628 g_variant_get(result, "(v)", &temp);
1629 ver = g_variant_get_string(temp, NULL);
1630 BT_DBG("VERSION: %s", ver);
1632 if (ver && (strlen(ver) > 0)) {
1633 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1634 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1637 g_strlcpy(local_version->version, ver,
1638 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1641 ret = BLUETOOTH_ERROR_INTERNAL;
1644 g_variant_unref(result);
1645 g_variant_unref(temp);
1649 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1652 const char *name = NULL;
1654 int ret = BLUETOOTH_ERROR_NONE;
1657 GError *error = NULL;
1659 BT_CHECK_PARAMETER(local_name, return);
1661 proxy = _bt_get_adapter_properties_proxy();
1662 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1664 result = g_dbus_proxy_call_sync(proxy,
1666 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1668 G_DBUS_CALL_FLAGS_NONE,
1674 if (error != NULL) {
1675 BT_ERR("Failed to get local name (Error: %s)", error->message);
1676 g_clear_error(&error);
1678 BT_ERR("Failed to get local name");
1679 return BLUETOOTH_ERROR_INTERNAL;
1682 g_variant_get(result, "(v)", &temp);
1683 name = g_variant_get_string(temp, NULL);
1684 BT_DBG("LOCAL NAME:%s", name);
1686 if (name && (strlen(name) > 0)) {
1687 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1688 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1691 g_strlcpy(local_name->name, name,
1692 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1694 ret = BLUETOOTH_ERROR_INTERNAL;
1696 g_variant_unref(result);
1697 g_variant_unref(temp);
1701 int _bt_set_local_name(char *local_name)
1704 GError *error = NULL;
1708 BT_CHECK_PARAMETER(local_name, return);
1710 proxy = _bt_get_adapter_properties_proxy();
1712 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1714 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1717 result = g_dbus_proxy_call_sync(proxy,
1719 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1720 "Alias", g_variant_new("s", local_name)),
1721 G_DBUS_CALL_FLAGS_NONE,
1727 if (error != NULL) {
1728 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1729 g_clear_error(&error);
1731 BT_ERR("Failed to set Alias");
1732 return BLUETOOTH_ERROR_INTERNAL;
1735 g_variant_unref(result);
1736 return BLUETOOTH_ERROR_NONE;
1739 int _bt_is_service_used(char *service_uuid, gboolean *used)
1742 GError *error = NULL;
1743 int ret = BLUETOOTH_ERROR_NONE;
1745 GVariant *temp = NULL;
1746 GVariantIter *iter = NULL;
1750 BT_CHECK_PARAMETER(service_uuid, return);
1751 BT_CHECK_PARAMETER(used, return);
1753 proxy = _bt_get_adapter_properties_proxy();
1754 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1756 result = g_dbus_proxy_call_sync(proxy,
1758 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1760 G_DBUS_CALL_FLAGS_NONE,
1766 if (error != NULL) {
1767 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1768 g_clear_error(&error);
1770 BT_ERR("Failed to get UUIDs");
1771 return BLUETOOTH_ERROR_INTERNAL;
1774 g_variant_get(result, "(v)", &temp);
1775 g_variant_get(temp, "as", &iter);
1778 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1779 if (strcasecmp(uuid, service_uuid) == 0) {
1784 g_variant_iter_free(iter);
1785 g_variant_unref(result);
1787 BT_DBG("Service Used? %d", *used);
1792 static gboolean __bt_get_discoverable_property(void)
1795 gboolean discoverable_v;
1796 GError *error = NULL;
1800 proxy = _bt_get_adapter_properties_proxy();
1801 retv_if(proxy == NULL, FALSE);
1803 result = g_dbus_proxy_call_sync(proxy,
1805 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1807 G_DBUS_CALL_FLAGS_NONE,
1813 if (error != NULL) {
1814 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1815 g_clear_error(&error);
1817 BT_ERR("Failed to get Discoverable property");
1818 return BLUETOOTH_ERROR_INTERNAL;
1821 g_variant_get(result, "(v)", &temp);
1822 discoverable_v = g_variant_get_boolean(temp);
1823 BT_DBG("discoverable_v:%d", discoverable_v);
1825 g_variant_unref(result);
1826 g_variant_unref(temp);
1828 return discoverable_v;
1831 int _bt_get_discoverable_mode(int *mode)
1833 gboolean discoverable;
1834 unsigned int timeout;
1836 BT_CHECK_PARAMETER(mode, return);
1838 discoverable = __bt_get_discoverable_property();
1839 timeout = _bt_get_discoverable_timeout_property();
1841 if (discoverable == TRUE) {
1843 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1845 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1847 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1849 return BLUETOOTH_ERROR_NONE;
1853 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1855 int ret = BLUETOOTH_ERROR_NONE;
1858 GError *error = NULL;
1862 proxy = _bt_get_adapter_properties_proxy();
1864 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1866 #ifdef TIZEN_DPM_ENABLE
1867 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1868 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1869 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1870 return BLUETOOTH_ERROR_ACCESS_DENIED;
1872 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1873 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1874 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1875 return BLUETOOTH_ERROR_ACCESS_DENIED;
1879 switch (discoverable_mode) {
1880 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1885 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1890 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1895 return BLUETOOTH_ERROR_INVALID_PARAM;
1898 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1899 discoverable_mode, timeout);
1901 result = g_dbus_proxy_call_sync(proxy,
1903 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1904 "Connectable", g_variant_new("b", pg_scan)),
1905 G_DBUS_CALL_FLAGS_NONE,
1911 if (error != NULL) {
1912 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1913 g_clear_error(&error);
1915 BT_ERR("Failed to set connectable property");
1916 return BLUETOOTH_ERROR_INTERNAL;
1918 g_variant_unref(result);
1919 result = g_dbus_proxy_call_sync(proxy,
1921 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1922 g_variant_new("b", inq_scan)),
1923 G_DBUS_CALL_FLAGS_NONE,
1929 if (error != NULL) {
1930 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
1931 g_clear_error(&error);
1933 BT_ERR("Failed to set Discoverable property");
1934 return BLUETOOTH_ERROR_INTERNAL;
1936 g_variant_unref(result);
1937 result = g_dbus_proxy_call_sync(proxy,
1939 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1940 "DiscoverableTimeout", g_variant_new("u", timeout)),
1941 G_DBUS_CALL_FLAGS_NONE,
1947 if (error != NULL) {
1948 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
1949 g_clear_error(&error);
1951 BT_ERR("Failed to set DiscoverableTimeout property");
1952 return BLUETOOTH_ERROR_INTERNAL;
1955 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
1958 ret = __bt_set_visible_time(timeout);
1960 g_variant_unref(result);
1965 int _bt_start_discovery(void)
1968 GError *error = NULL;
1971 if (_bt_is_discovering() == TRUE) {
1972 BT_ERR("BT is already in discovering");
1973 return BLUETOOTH_ERROR_IN_PROGRESS;
1974 } else if (_bt_is_device_creating() == TRUE) {
1975 BT_ERR("Bonding device is going on");
1976 return BLUETOOTH_ERROR_DEVICE_BUSY;
1979 proxy = _bt_get_adapter_proxy();
1980 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1982 result = g_dbus_proxy_call_sync(proxy,
1985 G_DBUS_CALL_FLAGS_NONE,
1991 if (error != NULL) {
1992 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
1993 g_clear_error(&error);
1995 BT_ERR("StartDiscovery failed");
1996 return BLUETOOTH_ERROR_INTERNAL;
1999 is_discovering = TRUE;
2000 cancel_by_user = FALSE;
2001 /* discovery status will be change in event */
2002 g_variant_unref(result);
2003 return BLUETOOTH_ERROR_NONE;
2006 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2010 GError *error = NULL;
2011 const gchar *disc_type;
2013 if (_bt_is_discovering() == TRUE) {
2014 BT_ERR("BT is already in discovering");
2015 return BLUETOOTH_ERROR_IN_PROGRESS;
2018 proxy = _bt_get_adapter_proxy();
2019 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2021 if (role == DISCOVERY_ROLE_BREDR)
2022 disc_type = "BREDR";
2023 else if (role == DISCOVERY_ROLE_LE)
2025 else if (role == DISCOVERY_ROLE_LE_BREDR)
2026 disc_type = "LE_BREDR";
2028 return BLUETOOTH_ERROR_INVALID_PARAM;
2030 result = g_dbus_proxy_call_sync(proxy,
2031 "StartCustomDiscovery",
2032 g_variant_new("s", disc_type),
2033 G_DBUS_CALL_FLAGS_NONE,
2039 if (error != NULL) {
2040 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2041 g_clear_error(&error);
2043 BT_ERR("StartCustomDiscovery failed");
2044 return BLUETOOTH_ERROR_INTERNAL;
2047 is_discovering = TRUE;
2048 cancel_by_user = FALSE;
2049 /* discovery status will be change in event */
2050 g_variant_unref(result);
2051 return BLUETOOTH_ERROR_NONE;
2054 int _bt_cancel_discovery(void)
2057 GError *error = NULL;
2060 if (_bt_is_discovering() == FALSE) {
2061 BT_ERR("BT is not in discovering");
2062 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2065 proxy = _bt_get_adapter_proxy();
2066 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2068 result = g_dbus_proxy_call_sync(proxy,
2071 G_DBUS_CALL_FLAGS_NONE,
2077 if (error != NULL) {
2078 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2079 g_clear_error(&error);
2081 BT_ERR("StopDiscovery failed");
2082 return BLUETOOTH_ERROR_INTERNAL;
2085 cancel_by_user = TRUE;
2086 /* discovery status will be change in event */
2087 g_variant_unref(result);
2088 return BLUETOOTH_ERROR_NONE;
2091 gboolean _bt_is_discovering(void)
2093 return is_discovering;
2096 gboolean _bt_is_connectable(void)
2099 GError *error = NULL;
2100 gboolean is_connectable = FALSE;
2104 proxy = _bt_get_adapter_properties_proxy();
2105 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2107 result = g_dbus_proxy_call_sync(proxy,
2109 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2111 G_DBUS_CALL_FLAGS_NONE,
2117 if (error != NULL) {
2118 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2119 g_clear_error(&error);
2121 BT_ERR("Failed to get connectable property");
2122 return BLUETOOTH_ERROR_INTERNAL;
2125 g_variant_get(result, "(v)", &temp);
2126 is_connectable = g_variant_get_boolean(temp);
2127 BT_DBG("discoverable_v:%d", is_connectable);
2129 g_variant_unref(result);
2130 g_variant_unref(temp);
2132 BT_INFO("Get connectable [%d]", is_connectable);
2133 return is_connectable;
2136 int _bt_set_connectable(gboolean is_connectable)
2139 GError *error = NULL;
2142 if (__bt_is_factory_test_mode()) {
2143 BT_ERR("Unable to set connectable in factory binary !!");
2144 return BLUETOOTH_ERROR_NOT_SUPPORT;
2147 proxy = _bt_get_adapter_properties_proxy();
2149 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2151 result = g_dbus_proxy_call_sync(proxy,
2153 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2154 g_variant_new("b", is_connectable)),
2155 G_DBUS_CALL_FLAGS_NONE,
2161 if (error != NULL) {
2162 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2163 g_clear_error(&error);
2165 BT_ERR("Failed to set connectable property");
2166 return BLUETOOTH_ERROR_INTERNAL;
2169 BT_INFO("### Set connectable [%d]", is_connectable);
2170 g_variant_unref(result);
2171 return BLUETOOTH_ERROR_NONE;
2174 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2177 gboolean discovering_v;
2178 GError *error = NULL;
2179 char *discovering_type = NULL;
2183 proxy = _bt_get_adapter_properties_proxy();
2184 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2186 if (discovery_type == DISCOVERY_ROLE_BREDR)
2187 discovering_type = "Discovering";
2188 else if (discovery_type == DISCOVERY_ROLE_LE)
2189 discovering_type = "LEDiscovering";
2191 result = g_dbus_proxy_call_sync(proxy,
2193 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2195 G_DBUS_CALL_FLAGS_NONE,
2201 if (error != NULL) {
2202 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2203 g_clear_error(&error);
2205 BT_ERR("Failed to get discovering property");
2206 return BLUETOOTH_ERROR_INTERNAL;
2209 g_variant_get(result, "(v)", &temp);
2210 discovering_v = g_variant_get_boolean(temp);
2211 BT_DBG("discoverable_v:%d", discovering_v);
2213 g_variant_unref(result);
2214 g_variant_unref(temp);
2216 return discovering_v;
2219 unsigned int _bt_get_discoverable_timeout_property(void)
2222 unsigned int timeout_v;
2223 GError *error = NULL;
2227 proxy = _bt_get_adapter_properties_proxy();
2228 retv_if(proxy == NULL, 0);
2230 result = g_dbus_proxy_call_sync(proxy,
2232 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2233 "DiscoverableTimeout"),
2234 G_DBUS_CALL_FLAGS_NONE,
2240 BT_ERR("Fail to get discoverable timeout");
2241 if (error != NULL) {
2242 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2243 g_clear_error(&error);
2248 g_variant_get(result, "(v)", &temp);
2249 timeout_v = g_variant_get_uint32(temp);
2250 BT_DBG("discoverable_v:%d", timeout_v);
2252 g_variant_unref(result);
2253 g_variant_unref(temp);
2258 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2260 bluetooth_device_info_t *dev_info;
2263 GByteArray *manufacturer_data = NULL;
2265 GVariantIter *char_value_iter;
2267 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2269 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2274 if (!g_strcmp0(key, "Address")) {
2275 const char *address = NULL;
2276 address = g_variant_get_string(value, NULL);
2277 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2279 } else if (!g_strcmp0(key, "Class")) {
2281 cod = g_variant_get_uint32(value);
2282 _bt_divide_device_class(&dev_info->device_class, cod);
2283 } else if (!g_strcmp0(key, "Name")) {
2284 const char *name = NULL;
2285 name = g_variant_get_string(value, NULL);
2286 /* If there is no Alias */
2287 if (strlen(dev_info->device_name.name) == 0) {
2288 g_strlcpy(dev_info->device_name.name, name,
2289 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2291 } else if (!g_strcmp0(key, "Alias")) {
2292 const char *alias = NULL;
2293 alias = g_variant_get_string(value, NULL);
2294 /* Overwrite the name */
2296 memset(dev_info->device_name.name, 0x00,
2297 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2298 g_strlcpy(dev_info->device_name.name, alias,
2299 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2301 } else if (!g_strcmp0(key, "IsAliasSet")) {
2302 dev_info->is_alias_set = g_variant_get_boolean(value);
2303 } else if (!g_strcmp0(key, "Connected")) {
2304 dev_info->connected = g_variant_get_byte(value);
2305 } else if (!g_strcmp0(key, "Paired")) {
2306 dev_info->paired = g_variant_get_boolean(value);
2307 } else if (!g_strcmp0(key, "Trusted")) {
2308 dev_info->trust = g_variant_get_boolean(value);
2309 } else if (!g_strcmp0(key, "RSSI")) {
2310 dev_info->rssi = g_variant_get_int16(value);
2311 } else if (!g_strcmp0(key, "UUIDs")) {
2317 dev_info->service_index = 0;
2318 g_variant_get(value, "as", &iter);
2319 while (g_variant_iter_loop(iter, "s", &uuid)) {
2320 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2321 parts = g_strsplit(uuid, "-", -1);
2323 if (parts == NULL || parts[0] == NULL) {
2328 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2333 dev_info->service_index = i;
2334 g_variant_iter_free(iter);
2335 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
2336 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2337 } else if (strcasecmp(key, "ManufacturerData") == 0) {
2338 manufacturer_data = g_byte_array_new();
2339 g_variant_get(value, "ay", &char_value_iter);
2340 while (g_variant_iter_loop(char_value_iter, "y", &char_value)) {
2341 g_byte_array_append(manufacturer_data, &char_value, 1);
2343 if (manufacturer_data) {
2344 if (manufacturer_data->len > 0) {
2345 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2348 g_variant_iter_free(char_value_iter);
2349 g_byte_array_free(manufacturer_data, TRUE);
2356 static void __bt_extract_device_info(GVariantIter *iter,
2359 bluetooth_device_info_t *dev_info = NULL;
2360 char *object_path = NULL;
2361 GVariantIter *interface_iter;
2362 GVariantIter *svc_iter;
2363 char *interface_str = NULL;
2365 /* Parse the signature: oa{sa{sv}}} */
2366 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2369 if (object_path == NULL)
2372 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2373 &interface_str, &svc_iter)) {
2374 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2375 BT_DBG("Found a device: %s", object_path);
2376 dev_info = __bt_parse_device_info(svc_iter);
2378 if (dev_info->paired == TRUE) {
2379 g_array_append_vals(*dev_list, dev_info,
2380 sizeof(bluetooth_device_info_t));
2384 g_free(interface_str);
2385 g_variant_iter_free(svc_iter);
2393 int _bt_get_bonded_devices(GArray **dev_list)
2396 GDBusConnection *conn;
2397 GDBusProxy *manager_proxy;
2398 GVariant *result = NULL;
2399 GVariantIter *iter = NULL;
2400 GError *error = NULL;
2402 conn = _bt_get_system_conn();
2403 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2405 manager_proxy = _bt_get_manager_proxy();
2406 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2408 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2410 G_DBUS_CALL_FLAGS_NONE,
2416 if (error != NULL) {
2417 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2418 g_clear_error(&error);
2420 BT_ERR("Failed to Failed to GetManagedObjects");
2421 return BLUETOOTH_ERROR_INTERNAL;
2424 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2425 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2427 __bt_extract_device_info(iter, dev_list);
2428 g_variant_iter_free(iter);
2429 g_variant_unref(result);
2432 return BLUETOOTH_ERROR_NONE;
2435 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2438 GDBusConnection *conn;
2439 GDBusProxy *manager_proxy;
2440 GVariant *result = NULL;
2441 GVariant *result1 = NULL;
2442 GVariantIter *iter = NULL;
2443 GError *error = NULL;
2444 char *object_path = NULL;
2445 GVariantIter *interface_iter;
2446 char *interface_str = NULL;
2447 GDBusProxy *device_proxy = NULL;
2448 gboolean is_connected = FALSE;
2450 conn = _bt_get_system_conn();
2451 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2453 manager_proxy = _bt_get_manager_proxy();
2454 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2456 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2458 G_DBUS_CALL_FLAGS_NONE,
2464 if (error != NULL) {
2465 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2466 g_clear_error(&error);
2469 BT_ERR("Failed to Failed to GetManagedObjects");
2470 return BLUETOOTH_ERROR_INTERNAL;
2473 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2474 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2476 /* Parse the signature: oa{sa{sv}}} */
2477 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2478 if (object_path == NULL)
2481 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2482 &interface_str, NULL)) {
2483 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2484 BT_DBG("Found a device: %s", object_path);
2485 g_free(interface_str);
2487 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2488 NULL, BT_BLUEZ_NAME,
2489 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2491 if (device_proxy == NULL) {
2492 BT_DBG("Device don't have this service");
2496 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2497 g_variant_new("(s)", profile_uuid),
2498 G_DBUS_CALL_FLAGS_NONE,
2503 if (result1 == NULL) {
2504 BT_ERR("Error occured in Proxy call");
2506 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2507 g_error_free(error);
2510 g_object_unref(device_proxy);
2513 g_variant_get(result1, "(b)", &is_connected);
2515 if (is_connected == TRUE) {
2516 char address[BT_ADDRESS_STRING_SIZE];
2517 bluetooth_device_address_t *addr = NULL;
2519 _bt_convert_device_path_to_address(object_path, address);
2521 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2522 _bt_convert_addr_string_to_type(addr->addr, address);
2524 g_array_append_vals(*addr_list, addr,
2525 sizeof(bluetooth_device_address_t));
2528 g_variant_unref(result1);
2529 g_object_unref(device_proxy);
2536 g_variant_unref(result);
2537 g_variant_iter_free(iter);
2540 return BLUETOOTH_ERROR_NONE;
2543 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2544 bluetooth_device_info_t *dev_info)
2546 char *object_path = NULL;
2547 GDBusProxy *adapter_proxy;
2548 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2549 int ret = BLUETOOTH_ERROR_NONE;
2551 BT_CHECK_PARAMETER(device_address, return);
2552 BT_CHECK_PARAMETER(dev_info, return);
2554 adapter_proxy = _bt_get_adapter_proxy();
2555 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2557 _bt_convert_addr_type_to_string(address, device_address->addr);
2559 object_path = _bt_get_device_object_path(address);
2561 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2563 ret = __bt_get_bonded_device_info(object_path, dev_info);
2564 g_free(object_path);
2569 int _bt_get_timeout_value(int *timeout)
2571 time_t current_time;
2574 /* Take current time */
2575 time(¤t_time);
2576 time_diff = difftime(current_time, visible_timer.start_time);
2578 BT_DBG("Time diff = %d\n", time_diff);
2580 *timeout = visible_timer.timeout - time_diff;
2582 return BLUETOOTH_ERROR_NONE;
2585 int _bt_set_le_privacy(gboolean set_privacy)
2588 GError *error = NULL;
2589 GVariant *result = NULL;
2591 if (__bt_is_factory_test_mode()) {
2592 BT_ERR("Unable to set le privacy in factory binary !!");
2593 return BLUETOOTH_ERROR_NOT_SUPPORT;
2596 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2597 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2598 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2601 proxy = _bt_get_adapter_proxy();
2602 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2604 result = g_dbus_proxy_call_sync(proxy,
2606 g_variant_new("(b)", set_privacy),
2607 G_DBUS_CALL_FLAGS_NONE,
2613 if (error != NULL) {
2614 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2615 g_clear_error(&error);
2617 BT_ERR("Failed to SetLePrivacy");
2618 return BLUETOOTH_ERROR_INTERNAL;
2621 g_variant_unref(result);
2622 BT_INFO("SetLePrivacy as %d", set_privacy);
2623 return BLUETOOTH_ERROR_NONE;
2626 int _bt_set_le_static_random_address(gboolean is_enable)
2629 GError *error = NULL;
2630 GVariant *result = NULL;
2632 if (__bt_is_factory_test_mode()) {
2633 BT_ERR("Unable to set le random address in factory binary !!");
2634 return BLUETOOTH_ERROR_NOT_SUPPORT;
2637 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2638 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2639 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2642 proxy = _bt_get_adapter_proxy();
2643 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2645 result = g_dbus_proxy_call_sync(proxy,
2646 "SetLeStaticRandomAddress",
2647 g_variant_new("(b)", is_enable),
2648 G_DBUS_CALL_FLAGS_NONE,
2654 if (error != NULL) {
2655 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2656 g_clear_error(&error);
2658 BT_ERR("Failed to SetLeStaticRandomAddress");
2659 return BLUETOOTH_ERROR_INTERNAL;
2662 g_variant_unref(result);
2663 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2664 return BLUETOOTH_ERROR_NONE;
2667 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2670 GError *error = NULL;
2674 GVariantBuilder *builder;
2676 BT_CHECK_PARAMETER(m_data, return);
2678 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2679 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2680 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2683 proxy = _bt_get_adapter_proxy();
2684 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2686 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2688 for (i = 0; i < (m_data->data_len) + 2; i++) {
2689 g_variant_builder_add(builder, "y", m_data->data[i]);
2692 val = g_variant_new("(ay)", builder);
2694 result = g_dbus_proxy_call_sync(proxy,
2695 "SetManufacturerData",
2697 G_DBUS_CALL_FLAGS_NONE,
2701 g_variant_builder_unref(builder);
2703 if (error != NULL) {
2704 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2705 g_clear_error(&error);
2707 BT_ERR("Failed to SetManufacturerData");
2709 return BLUETOOTH_ERROR_INTERNAL;
2711 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2713 for (i = 0; i < (m_data->data_len) + 2; i++) {
2714 g_variant_builder_add(builder, "y", m_data->data[i]);
2717 val = g_variant_new("(ay)", builder);
2719 _bt_send_event(BT_ADAPTER_EVENT,
2720 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2723 BT_INFO("Set manufacturer data");
2725 g_variant_builder_unref(builder);
2726 g_variant_unref(result);
2728 return BLUETOOTH_ERROR_NONE;
2732 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2734 int result = BLUETOOTH_ERROR_NONE;
2735 bt_service_alarm_t *alarm = NULL;
2737 if(!call_back || !alarm_id)
2738 return BLUETOOTH_ERROR_INVALID_PARAM;
2740 if (!alarm_mgr.is_alarm_initialized) {
2741 result = alarmmgr_init("bt-service");
2743 BT_ERR("Failed to initialize alarm = %d",result);
2744 result = BLUETOOTH_ERROR_INTERNAL;
2747 result = alarmmgr_set_cb(alarm_cb, NULL);
2749 BT_ERR("Failed to set the callback = %d",result);
2750 result = BLUETOOTH_ERROR_INTERNAL;
2753 alarm_mgr.is_alarm_initialized = TRUE;
2756 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2758 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2760 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2763 BT_ERR("Failed to create alarm error = %d",result);
2764 result = BLUETOOTH_ERROR_INTERNAL;
2768 alarm->alarm_id = *alarm_id;
2769 alarm->callback = call_back;
2770 alarm->user_data = user_data;
2772 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2773 result = BLUETOOTH_ERROR_NONE;
2778 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2781 bt_service_alarm_t *p_data;
2782 bt_set_alarm_cb callback = NULL;
2783 void *user_data = NULL;
2785 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2786 GINT_TO_POINTER(alarm_id), compare_alarm);
2790 p_data = (bt_service_alarm_t *)node->data;
2791 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2797 callback = p_data->callback;
2798 user_data = p_data->user_data;
2802 callback(alarm_id, user_data);
2807 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2810 bt_service_alarm_t *p_data;
2811 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2814 alarmmgr_remove_alarm(alarm_id);
2815 p_data = (bt_service_alarm_t *)list->data;
2816 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2823 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2825 alarm_id_t alarm_id = (alarm_id_t)data;
2826 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2828 if (p_data->alarm_id == alarm_id) {
2834 static void alarm_data_free(void *data)
2836 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2841 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2844 int function = (int)user_data;
2847 case BT_ENABLE_ADAPTER:
2848 result = _bt_enable_adapter();
2849 if (result != BLUETOOTH_ERROR_NONE) {
2850 BT_ERR("_bt_enable_adapter is failed");
2851 /* Send enabled event to API */
2852 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
2853 g_variant_new("(i)", result));
2856 case BT_DISABLE_ADAPTER:
2857 result = _bt_disable_adapter();
2858 if (result != BLUETOOTH_ERROR_NONE) {
2859 BT_ERR("_bt_disable_adapter is failed");
2860 /* Send disabled event to API */
2861 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
2862 g_variant_new("(i)", result));
2866 BT_ERR("function is NOT matched");
2873 int _bt_adapter_request_delayed(int function)
2875 bt_status_t status = _bt_adapter_get_status();
2876 bt_le_status_t le_status = _bt_adapter_get_le_status();
2879 case BT_ENABLE_ADAPTER:
2880 if (status == BT_ACTIVATING) {
2881 BT_ERR("Enabling in progress");
2882 return BLUETOOTH_ERROR_IN_PROGRESS;
2885 if (status == BT_ACTIVATED) {
2886 BT_ERR("Already enabled");
2887 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
2890 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
2891 BT_ERR("Disabling in progress");
2892 return BLUETOOTH_ERROR_DEVICE_BUSY;
2896 case BT_DISABLE_ADAPTER:
2897 if (status == BT_DEACTIVATING) {
2898 BT_DBG("Disabling in progress");
2899 return BLUETOOTH_ERROR_IN_PROGRESS;
2902 if (status == BT_DEACTIVATED) {
2903 BT_DBG("Already disabled");
2904 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2909 BT_ERR("function is NOT matched");
2910 return BLUETOOTH_ERROR_INTERNAL;
2913 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
2915 return BLUETOOTH_ERROR_NONE;
2919 int _bt_get_enable_timer_id(void)