2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
24 #include <syspopup_caller.h>
26 #include <dbus/dbus-glib.h>
27 #include <dbus/dbus.h>
29 #include <eventsystem.h>
30 #include <bundle_internal.h>
33 #include "bluetooth-api.h"
34 #include "bt-internal-types.h"
35 #include "bt-service-common.h"
36 #include "bt-service-event.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-util.h"
39 #include "bt-service-network.h"
40 #include "bt-service-obex-server.h"
41 #include "bt-service-opp-client.h"
42 #include "bt-service-agent.h"
43 #include "bt-service-main.h"
44 #include "bt-service-avrcp.h"
45 #include "bt-service-device.h"
46 #ifdef TIZEN_DPM_ENABLE
47 #include "bt-service-dpm.h"
57 bt_adapter_timer_t visible_timer = {0, };
61 bt_set_alarm_cb callback;
66 gboolean is_alarm_initialized;
68 } bt_service_alarm_mgr_t;
70 static bt_service_alarm_mgr_t alarm_mgr = {0, };
72 static gboolean is_discovering;
73 static gboolean cancel_by_user;
74 static bt_status_t adapter_status = BT_DEACTIVATED;
75 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
76 static gboolean is_le_intended = FALSE;
77 static void *adapter_agent = NULL;
78 static GDBusProxy *core_proxy = NULL;
79 static guint timer_id = 0;
80 static guint le_timer_id = 0;
81 static gboolean is_recovery_mode;
83 static uint status_reg_id;
85 #define BT_CORE_NAME "org.projectx.bt_core"
86 #define BT_CORE_PATH "/org/projectx/bt_core"
87 #define BT_CORE_INTERFACE "org.projectx.btcore"
89 #define BT_DISABLE_TIME 500 /* 500 ms */
91 static int alarm_cb(alarm_id_t alarm_id, void* user_param);
92 static void alarm_data_free(void *data);
94 GDBusProxy *_bt_init_core_proxy(void)
97 GDBusConnection *conn;
99 conn = _bt_gdbus_get_system_gconn();
103 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
118 static GDBusProxy *__bt_get_core_proxy(void)
120 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
123 static gboolean __bt_is_factory_test_mode(void)
127 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
128 BT_ERR("Get the DUT Mode fail");
133 BT_INFO("DUT Test Mode !!");
140 static gboolean __bt_timeout_handler(gpointer user_data)
142 int result = BLUETOOTH_ERROR_NONE;
146 /* Take current time */
148 time_diff = difftime(current_time, visible_timer.start_time);
150 /* Send event to application */
151 _bt_send_event(BT_ADAPTER_EVENT,
152 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
153 g_variant_new("(in)", result, time_diff));
155 if (visible_timer.timeout <= time_diff) {
156 g_source_remove(visible_timer.event_id);
157 visible_timer.event_id = 0;
158 visible_timer.timeout = 0;
160 #ifndef TIZEN_PROFILE_WEARABLE
161 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
162 BT_ERR("Set vconf failed\n");
170 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
173 int result = BLUETOOTH_ERROR_NONE;
176 if (alarm_id != visible_timer.alarm_id)
179 if (visible_timer.event_id) {
180 _bt_send_event(BT_ADAPTER_EVENT,
181 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
182 g_variant_new("(in)", result, timeout));
183 g_source_remove(visible_timer.event_id);
184 visible_timer.event_id = 0;
185 visible_timer.timeout = 0;
187 #ifndef TIZEN_PROFILE_WEARABLE
188 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
189 BT_ERR("Set vconf failed\n");
192 /* Switch Off visibility in Bluez */
193 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
194 visible_timer.alarm_id = 0;
198 static void __bt_visibility_alarm_remove()
200 if (visible_timer.event_id > 0) {
201 g_source_remove(visible_timer.event_id);
202 visible_timer.event_id = 0;
205 if (visible_timer.alarm_id > 0) {
206 _bt_service_remove_alarm(visible_timer.alarm_id);
207 visible_timer.alarm_id = 0;
211 int __bt_set_visible_time(int timeout)
216 __bt_visibility_alarm_remove();
218 visible_timer.timeout = timeout;
220 #ifndef TIZEN_PROFILE_WEARABLE
221 #ifdef TIZEN_DPM_ENABLE
222 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
224 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
225 BT_ERR("Set vconf failed");
226 #ifdef TIZEN_DPM_ENABLE
233 return BLUETOOTH_ERROR_NONE;
235 result = _bt_service_set_alarm(visible_timer.timeout,
236 __bt_visibility_alarm_cb, NULL, &alarm_id);
237 if (result != BLUETOOTH_ERROR_NONE)
238 return BLUETOOTH_ERROR_INTERNAL;
239 visible_timer.alarm_id = alarm_id;
240 /* Take start time */
241 time(&(visible_timer.start_time));
242 visible_timer.event_id = g_timeout_add_seconds(1,
243 __bt_timeout_handler, NULL);
245 return BLUETOOTH_ERROR_NONE;
248 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
255 ret_if(value == NULL);
258 dev->service_index = 0;
260 g_variant_get(value, "as", &iter);
261 while (g_variant_iter_loop(iter, "s", &uuid)) {
262 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
263 parts = g_strsplit(uuid, "-", -1);
265 if (parts == NULL || parts[0] == NULL) {
270 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
273 dev->service_index++;
276 g_variant_iter_free(iter);
279 static int __bt_get_bonded_device_info(gchar *device_path,
280 bluetooth_device_info_t *dev_info)
282 GError *error = NULL;
283 GDBusProxy *device_proxy;
284 gchar *address = NULL;
286 unsigned int cod = 0;
288 gboolean trust = FALSE;
289 gboolean paired = FALSE;
290 guchar connected = 0;
291 GByteArray *manufacturer_data = NULL;
293 GDBusConnection *conn;
295 GVariantIter *property_iter;
299 GVariantIter *char_value_iter;
301 BT_CHECK_PARAMETER(device_path, return);
302 BT_CHECK_PARAMETER(dev_info, return);
304 conn = _bt_gdbus_get_system_gconn();
305 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
307 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
311 BT_PROPERTIES_INTERFACE,
314 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
316 result = g_dbus_proxy_call_sync(device_proxy,
318 g_variant_new("(s)", BT_DEVICE_INTERFACE),
319 G_DBUS_CALL_FLAGS_NONE,
325 BT_ERR("Error occured in Proxy call");
327 BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
328 g_clear_error(&error);
330 g_object_unref(device_proxy);
331 return BLUETOOTH_ERROR_INTERNAL;
334 g_object_unref(device_proxy);
336 g_variant_get(result, "(a{sv})", &property_iter);
338 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
339 if (!g_strcmp0(key, "Paired")) {
340 paired = g_variant_get_boolean(value);
341 } else if (!g_strcmp0(key, "Address")) {
342 g_variant_get(value, "s", &address);
343 } else if (!g_strcmp0(key, "Alias")) {
344 g_variant_get(value, "s", &name);
345 } else if (!g_strcmp0(key, "Name")) {
347 g_variant_get(value, "s", &name);
348 } else if (!g_strcmp0(key, "Class")) {
349 cod = g_variant_get_uint32(value);
350 } else if (!g_strcmp0(key, "Connected")) {
351 connected = g_variant_get_byte(value);
352 } else if (!g_strcmp0(key, "Trusted")) {
353 trust = g_variant_get_boolean(value);
354 } else if (!g_strcmp0(key, "RSSI")) {
355 rssi = g_variant_get_int16(value);
356 } else if (!g_strcmp0(key, "UUIDs")) {
357 __bt_get_service_list(value, dev_info);
358 } else if (!g_strcmp0(key, "ManufacturerDataLen")) {
359 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
360 } else if (!g_strcmp0(key, "ManufacturerData")) {
361 manufacturer_data = g_byte_array_new();
362 g_variant_get(value, "ay", &char_value_iter);
363 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
364 g_byte_array_append(manufacturer_data, &char_value, 1);
366 g_variant_iter_free(char_value_iter);
368 if (manufacturer_data) {
369 if (manufacturer_data->len > 0) {
370 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
371 manufacturer_data->len);
374 g_byte_array_free(manufacturer_data, TRUE);
377 g_variant_iter_free(property_iter);
379 BT_DBG("trust: %d, paired: %d", trust, paired);
381 g_variant_unref(result);
383 if ((paired == FALSE) && (trust == FALSE)) {
386 return BLUETOOTH_ERROR_NOT_PAIRED;
389 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
392 _bt_divide_device_class(&dev_info->device_class, cod);
394 g_strlcpy(dev_info->device_name.name, name,
395 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
397 dev_info->rssi = rssi;
398 dev_info->trust = trust;
399 dev_info->paired = paired;
400 dev_info->connected = connected;
401 ret = BLUETOOTH_ERROR_NONE;
408 void _bt_set_discovery_status(gboolean mode)
410 is_discovering = mode;
413 void _bt_set_cancel_by_user(gboolean value)
415 cancel_by_user = value;
418 gboolean _bt_get_cancel_by_user(void)
420 return cancel_by_user;
423 void _bt_adapter_set_status(bt_status_t status)
425 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
426 adapter_status = status;
429 bt_status_t _bt_adapter_get_status(void)
431 return adapter_status;
434 void _bt_adapter_set_le_status(bt_le_status_t status)
436 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
437 adapter_le_status = status;
440 bt_le_status_t _bt_adapter_get_le_status(void)
442 return adapter_le_status;
446 void _bt_set_le_intended_status(gboolean value)
448 is_le_intended = value;
451 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
453 char *phone_name = NULL;
459 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
460 phone_name = vconf_keynode_get_str(node);
462 if (phone_name && strlen(phone_name) != 0) {
463 if (!g_utf8_validate(phone_name, -1,
464 (const char **)&ptr))
467 _bt_set_local_name(phone_name);
472 static void __bt_set_local_name(void)
474 bluetooth_device_name_t local_name;
475 char *phone_name = NULL;
479 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
480 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
481 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
486 if (strlen(phone_name) != 0) {
487 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
490 _bt_set_local_name(phone_name);
496 static int __bt_set_enabled(void)
498 int adapter_status = BT_ADAPTER_DISABLED;
499 int result = BLUETOOTH_ERROR_NONE;
502 BT_DBG("g_source is removed");
503 g_source_remove(timer_id);
507 _bt_check_adapter(&adapter_status);
509 if (adapter_status == BT_ADAPTER_DISABLED) {
510 BT_ERR("Bluetoothd is not running");
511 return BLUETOOTH_ERROR_INTERNAL;
514 #ifdef TIZEN_PROFILE_MOBILE || defined(TIZEN_PROFILE_IVI)
515 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
516 if (_bt_set_discoverable_mode(
517 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
518 BT_ERR("Set connectable mode failed");
521 if (_bt_set_discoverable_mode(
522 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
523 BT_ERR("Fail to set discoverable mode");
527 /* Update Bluetooth Status to notify other modules */
528 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
529 BT_ERR("Set vconf failed\n");
531 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
532 BT_ERR("Set vconf failed\n");
534 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
535 EVT_VAL_BT_ON) != ES_R_OK)
536 BT_ERR("Fail to set value");
538 /* Send enabled event to API */
539 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
540 g_variant_new("(i)", result));
542 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
543 _bt_audio_start_auto_connect(FALSE);
546 __bt_set_local_name();
547 _bt_set_discovery_status(FALSE);
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);
588 _bt_set_discovery_status(FALSE);
590 #ifndef USB_BLUETOOTH
591 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
593 /* Send disabled event */
594 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
595 g_variant_new("(i)", result));
596 #ifndef USB_BLUETOOTH
600 BT_INFO("Adapter disabled");
603 static int __bt_set_le_enabled(void)
606 int result = BLUETOOTH_ERROR_NONE;
609 /* Update Bluetooth Status to notify other modules */
610 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
611 BT_ERR("Set vconf failed\n");
613 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
614 EVT_VAL_BT_LE_ON) != ES_R_OK)
615 BT_ERR("Fail to set value");
617 /* Send enabled event to API */
619 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
620 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
622 status = _bt_adapter_get_status();
623 if (status == BT_DEACTIVATED) {
624 BT_INFO("BREDR is off, turn off PSCAN");
625 _bt_set_connectable(FALSE);
627 if (le_timer_id > 0) {
628 g_source_remove(le_timer_id);
632 /* Send enabled event to API */
633 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
634 g_variant_new("(i)", result));
636 __bt_set_local_name();
639 return BLUETOOTH_ERROR_NONE;
642 void _bt_set_le_disabled(int result)
644 int power_off_status;
647 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
648 BT_DBG("ret : %d", ret);
649 BT_DBG("power_off_status : %d", power_off_status);
651 /* Update Bluetooth Status to notify other modules */
652 BT_DBG("Update vconf for BT LE normal Deactivation");
653 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
654 BT_ERR("Set vconf failed\n");
655 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
657 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
658 EVT_VAL_BT_LE_OFF) != ES_R_OK)
659 BT_ERR("Fail to set value");
661 /* Send disabled event */
662 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
663 g_variant_new("(i)", result));
666 void *_bt_get_adapter_agent(void)
668 return adapter_agent;
671 int _bt_enable_core(void)
675 GError *error = NULL;
677 proxy = __bt_get_core_proxy();
678 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
680 /* Clean up the process */
681 result = g_dbus_proxy_call_sync(proxy,
684 G_DBUS_CALL_FLAGS_NONE,
691 BT_ERR("Bt core call failed(Error: %s)", error->message);
692 g_clear_error(&error);
694 BT_ERR("Bt core call failed");
695 return BLUETOOTH_ERROR_INTERNAL;
698 g_variant_unref(result);
699 return BLUETOOTH_ERROR_NONE;
702 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
703 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
705 gboolean flight_mode = FALSE;
706 int power_saving_mode = 0;
709 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
710 type = vconf_keynode_get_type(node);
711 if (type == VCONF_TYPE_BOOL) {
712 flight_mode = vconf_keynode_get_bool(node);
713 if (flight_mode != TRUE) {
714 BT_ERR("Ignore the event");
717 } else if (type == VCONF_TYPE_INT) {
718 power_saving_mode = vconf_keynode_get_int(node);
719 if (power_saving_mode != 2) {
720 BT_ERR("Ignore the event");
724 BT_ERR("Invaild vconf key type : %d", type);
732 void _bt_service_register_vconf_handler(void)
736 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
737 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
738 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
739 BT_ERR("Unable to register key handler");
741 BT_DBG("Telephony is disabled");
744 #ifndef TIZEN_PROFILE_WEARABLE
745 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
746 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
747 BT_ERR("Unable to register key handler");
751 void _bt_service_unregister_vconf_handler(void)
755 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
756 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
757 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
760 #ifndef TIZEN_PROFILE_WEARABLE
761 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
762 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
766 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
768 const char *bt_status = NULL;
769 const char *bt_le_status = NULL;
770 BT_DBG("bt state set event(%s) received", event_name);
772 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
773 BT_DBG("bt_state: (%s)", bt_status);
775 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
776 BT_DBG("bt_state: (%s)", bt_le_status);
779 void _bt_handle_adapter_added(void)
783 bt_le_status_t le_status;
787 BT_DBG("g_source is removed");
788 g_source_remove(timer_id);
793 status = _bt_adapter_get_status();
794 le_status = _bt_adapter_get_le_status();
795 BT_DBG("status : %d", status);
796 BT_DBG("le_status : %d", le_status);
798 #ifndef USB_BLUETOOTH
799 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
800 if (!adapter_agent) {
801 BT_ERR("Fail to register agent");
805 if (adapter_agent == NULL) {
806 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
807 if (!adapter_agent) {
808 BT_ERR("Fail to register agent");
814 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
815 BT_ERR("Fail to register media player");
817 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
818 BT_ERR("Fail to init obex server");
820 #ifdef TIZEN_BT_PAN_NAP_ENABLED
821 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
822 BT_ERR("Fail to activate network");
825 /* add the vconf noti handler */
826 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
827 __bt_phone_name_changed_cb, NULL);
829 BT_ERR("Unable to register key handler");
831 if (le_status == BT_LE_ACTIVATING ||
832 status == BT_ACTIVATING) {
833 __bt_set_le_enabled();
834 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
837 if (status == BT_ACTIVATING) {
839 _bt_adapter_set_status(BT_ACTIVATED);
842 _bt_service_register_vconf_handler();
845 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
846 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
847 BT_ERR("Fail to register system event");
851 void _bt_handle_adapter_removed(void)
855 _bt_adapter_set_status(BT_DEACTIVATED);
857 __bt_visibility_alarm_remove();
859 if (alarm_mgr.is_alarm_initialized == TRUE) {
861 alarm_mgr.is_alarm_initialized = FALSE;
862 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
863 alarm_mgr.g_alarm_list = NULL;
866 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
867 _bt_audio_stop_auto_connect();
870 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
871 (vconf_callback_fn)__bt_phone_name_changed_cb);
873 ERR("vconf_ignore_key_changed failed\n");
875 #ifndef USB_BLUETOOTH
876 _bt_destroy_agent(adapter_agent);
877 adapter_agent = NULL;
879 if (is_recovery_mode == TRUE) {
880 /* Send disabled event */
881 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
883 /* Will recover BT by bt-core, so set the mode as activating */
884 _bt_adapter_set_status(BT_ACTIVATING);
885 is_recovery_mode = FALSE;
887 _bt_reliable_terminate_service(NULL);
890 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
893 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
894 BT_ERR("Fail to unregister system event");
897 static gboolean __bt_enable_timeout_cb(gpointer user_data)
901 GError *error = NULL;
905 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
907 BT_ERR("EnableAdapter is failed");
909 proxy = __bt_get_core_proxy();
913 /* Clean up the process */
914 result = g_dbus_proxy_call_sync(proxy,
917 G_DBUS_CALL_FLAGS_NONE,
924 BT_ERR("Bt core call failed(Error: %s)", error->message);
925 g_clear_error(&error);
927 BT_ERR("Bt core call failed");
932 g_variant_unref(result);
933 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
935 #ifndef USB_BLUETOOTH
936 _bt_terminate_service(NULL);
942 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
946 GError *error = NULL;
950 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
952 BT_ERR("EnableAdapterLE is failed");
954 proxy = __bt_get_core_proxy();
958 /* Clean up the process */
959 result = g_dbus_proxy_call_sync(proxy,
962 G_DBUS_CALL_FLAGS_NONE,
969 BT_ERR("Bt core call failed(Error: %s)", error->message);
970 g_clear_error(&error);
972 BT_ERR("Bt core call failed");
976 g_variant_unref(result);
977 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
979 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
981 if (_bt_adapter_get_status() == BT_DEACTIVATED)
982 _bt_terminate_service(NULL);
987 void _bt_adapter_start_le_enable_timer(void)
989 if (le_timer_id > 0) {
990 g_source_remove(le_timer_id);
994 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
995 __bt_enable_le_timeout_cb, NULL);
1000 void _bt_adapter_start_enable_timer(void)
1003 g_source_remove(timer_id);
1007 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1008 __bt_enable_timeout_cb, NULL);
1014 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
1019 _bt_adapter_set_status(BT_ACTIVATED);
1025 int _bt_enable_adapter_check_status(void)
1027 bt_status_t status = _bt_adapter_get_status();
1028 bt_le_status_t le_status = _bt_adapter_get_le_status();
1032 if (status == BT_ACTIVATING) {
1033 BT_ERR("Enabling in progress");
1034 return BLUETOOTH_ERROR_IN_PROGRESS;
1037 if (status == BT_ACTIVATED) {
1038 BT_ERR("Already enabled");
1039 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1042 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1043 BT_ERR("Disabling in progress");
1044 return BLUETOOTH_ERROR_DEVICE_BUSY;
1047 return BLUETOOTH_ERROR_NONE;
1050 int _bt_enable_adapter(void)
1053 GError *error = NULL;
1055 GVariant *result = NULL;
1056 bt_status_t status = _bt_adapter_get_status();
1057 bt_le_status_t le_status = _bt_adapter_get_le_status();
1061 if (status == BT_ACTIVATING) {
1062 BT_ERR("Enabling in progress");
1063 return BLUETOOTH_ERROR_IN_PROGRESS;
1066 if (status == BT_ACTIVATED) {
1067 BT_ERR("Already enabled");
1068 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1071 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1072 BT_ERR("Disabling in progress");
1073 return BLUETOOTH_ERROR_DEVICE_BUSY;
1076 _bt_adapter_set_status(BT_ACTIVATING);
1080 int adapter_status = BT_ADAPTER_DISABLED;
1082 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1083 BT_ERR("Set vconf failed");
1085 _bt_check_adapter(&adapter_status);
1086 if (adapter_status == BT_ADAPTER_ENABLED) {
1087 g_idle_add(__bt_adapter_enabled_cb, NULL);
1088 _bt_adapter_start_enable_timer();
1089 return BLUETOOTH_ERROR_NONE;
1094 proxy = __bt_get_core_proxy();
1096 return BLUETOOTH_ERROR_INTERNAL;
1098 if (le_status == BT_LE_ACTIVATED) {
1099 BT_INFO("LE Already enabled. Just turn on PSCAN");
1100 ret = _bt_set_connectable(TRUE);
1101 if (ret == BLUETOOTH_ERROR_NONE)
1102 _bt_adapter_set_status(BT_ACTIVATED);
1104 return BLUETOOTH_ERROR_INTERNAL;
1107 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1109 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1112 BT_ERR("EnableAdapterLe failed: %s", error->message);
1113 _bt_adapter_set_status(BT_DEACTIVATED);
1114 g_clear_error(&error);
1116 result = g_dbus_proxy_call_sync(proxy,
1119 G_DBUS_CALL_FLAGS_NONE,
1124 if (error != NULL) {
1125 BT_ERR("Bt core call failed(Error: %s)", error->message);
1126 g_clear_error(&error);
1128 g_variant_unref(result);
1129 #ifndef USB_BLUETOOTH
1130 /* Terminate myself */
1131 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1133 return BLUETOOTH_ERROR_INTERNAL;
1135 g_variant_unref(result);
1136 if (le_status == BT_LE_ACTIVATED)
1139 _bt_adapter_start_enable_timer();
1141 return BLUETOOTH_ERROR_NONE;
1144 static gboolean __bt_disconnect_all(void)
1147 GDBusConnection *conn;
1148 GDBusProxy *dev_proxy;
1149 gboolean ret = FALSE;
1151 GError *error = NULL;
1152 GArray *device_list;
1153 bluetooth_device_info_t info;
1155 char *device_path = NULL;
1156 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1161 conn = _bt_gdbus_get_system_gconn();
1163 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1165 if (_bt_get_bonded_devices(&device_list)
1166 != BLUETOOTH_ERROR_NONE) {
1167 g_array_free(device_list, TRUE);
1171 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1173 for (i = 0; i < size; i++) {
1175 info = g_array_index(device_list,
1176 bluetooth_device_info_t, i);
1178 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1179 BT_DBG("Found Connected device");
1180 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1181 device_path = _bt_get_device_object_path(address);
1182 if (device_path == NULL)
1185 BT_DBG("Disconnecting : %s", device_path);
1187 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1191 BT_DEVICE_INTERFACE,
1194 if (dev_proxy == NULL)
1197 result = g_dbus_proxy_call_sync(dev_proxy,
1200 G_DBUS_CALL_FLAGS_NONE,
1206 if (error != NULL) {
1207 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1208 g_clear_error(&error);
1210 BT_ERR("Disconnect call failed");
1211 g_object_unref(dev_proxy);
1215 g_variant_unref(result);
1216 g_object_unref(dev_proxy);
1220 g_array_free(device_list, TRUE);
1226 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1229 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1235 int _bt_disable_cb(void)
1243 GError *error = NULL;
1245 _bt_adapter_set_status(BT_DEACTIVATING);
1247 bt_le_status_t le_status;
1248 le_status = _bt_adapter_get_le_status();
1249 BT_DBG("le_status : %d", le_status);
1250 if (le_status == BT_LE_ACTIVATED) {
1251 BT_INFO("LE is enabled. Just turn off PSCAN");
1253 if (_bt_is_discovering())
1254 _bt_cancel_discovery();
1256 if (_bt_is_connectable() == FALSE) {
1257 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1259 ret = _bt_set_connectable(FALSE);
1260 if (ret != BLUETOOTH_ERROR_NONE) {
1261 BT_ERR("_bt_set_connectable fail!");
1262 _bt_adapter_set_status(BT_ACTIVATED);
1263 return BLUETOOTH_ERROR_INTERNAL;
1268 proxy = __bt_get_core_proxy();
1269 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1271 result = g_dbus_proxy_call_sync(proxy,
1274 G_DBUS_CALL_FLAGS_NONE,
1280 if (error != NULL) {
1281 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1282 g_clear_error(&error);
1284 BT_ERR("Failed to DisableAdapter");
1285 _bt_adapter_set_status(BT_ACTIVATED);
1286 return BLUETOOTH_ERROR_INTERNAL;
1289 g_variant_unref(result);
1290 return BLUETOOTH_ERROR_NONE;
1293 int _bt_disable_adapter_check_status(void)
1295 bt_status_t status = _bt_adapter_get_status();
1299 if (status == BT_DEACTIVATING) {
1300 BT_DBG("Disabling in progress");
1301 return BLUETOOTH_ERROR_IN_PROGRESS;
1304 if (status == BT_DEACTIVATED) {
1305 BT_DBG("Already disabled");
1306 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1309 return BLUETOOTH_ERROR_NONE;
1312 int _bt_disable_adapter(void)
1317 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1318 BT_DBG("Disabling in progress");
1319 return BLUETOOTH_ERROR_IN_PROGRESS;
1322 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1323 BT_DBG("Already disabled");
1324 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1328 g_source_remove(timer_id);
1332 __bt_disconnect_all();
1333 ret = _bt_disable_cb();
1339 int _bt_recover_adapter(void)
1344 GError *error = NULL;
1346 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1347 BT_DBG("Disabling in progress");
1348 return BLUETOOTH_ERROR_IN_PROGRESS;
1351 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1352 BT_DBG("Already disabled");
1353 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1356 _bt_adapter_set_status(BT_DEACTIVATING);
1358 proxy = __bt_get_core_proxy();
1359 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1361 result = g_dbus_proxy_call_sync(proxy,
1364 G_DBUS_CALL_FLAGS_NONE,
1370 if (error != NULL) {
1371 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1372 g_clear_error(&error);
1374 BT_ERR("Failed to RecoverAdapter");
1375 return BLUETOOTH_ERROR_INTERNAL;
1378 is_recovery_mode = TRUE;
1380 g_variant_unref(result);
1381 __bt_disconnect_all();
1384 return BLUETOOTH_ERROR_NONE;
1387 int _bt_reset_adapter(void)
1391 GError *error = NULL;
1395 proxy = __bt_get_core_proxy();
1397 return BLUETOOTH_ERROR_INTERNAL;
1399 result = g_dbus_proxy_call_sync(proxy,
1402 G_DBUS_CALL_FLAGS_NONE,
1408 if (error != NULL) {
1409 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1410 g_clear_error(&error);
1412 BT_ERR("Failed to ResetAdapter");
1413 return BLUETOOTH_ERROR_INTERNAL;
1416 g_variant_unref(result);
1417 /* Terminate myself */
1418 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1419 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1421 return BLUETOOTH_ERROR_NONE;
1425 int _bt_check_adapter(int *status)
1428 char *adapter_path = NULL;
1430 BT_CHECK_PARAMETER(status, return);
1432 *status = BT_ADAPTER_DISABLED;
1434 adapter_path = _bt_get_adapter_path();
1437 if (adapter_path != NULL)
1438 *status = BT_ADAPTER_ENABLED;
1440 g_free(adapter_path);
1441 return BLUETOOTH_ERROR_NONE;
1444 int _bt_check_adapter(int *status)
1447 GError *error = NULL;
1450 gboolean powered = FALSE;
1452 BT_CHECK_PARAMETER(status, return);
1454 *status = BT_ADAPTER_DISABLED;
1456 proxy = _bt_get_adapter_properties_proxy();
1457 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1459 result = g_dbus_proxy_call_sync(proxy,
1461 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1463 G_DBUS_CALL_FLAGS_NONE,
1469 BT_ERR("Failed to get local address");
1470 if (error != NULL) {
1471 BT_ERR("Failed to get local address (Error: %s)", error->message);
1472 g_clear_error(&error);
1474 return BLUETOOTH_ERROR_INTERNAL;
1477 g_variant_get(result, "(v)", &temp);
1478 powered = g_variant_get_boolean(temp);
1479 BT_DBG("powered: %d", powered);
1482 *status = BT_ADAPTER_ENABLED;
1484 g_variant_unref(result);
1485 g_variant_unref(temp);
1486 return BLUETOOTH_ERROR_NONE;
1490 int _bt_enable_adapter_le(void)
1494 GError *error = NULL;
1495 bt_status_t status = _bt_adapter_get_status();
1496 bt_le_status_t le_status = _bt_adapter_get_le_status();
1499 if (le_status == BT_LE_ACTIVATING) {
1500 BT_ERR("Enabling in progress");
1501 return BLUETOOTH_ERROR_IN_PROGRESS;
1504 if (le_status == BT_LE_ACTIVATED) {
1505 BT_ERR("Already enabled");
1506 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1509 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1510 BT_ERR("Disabling in progress");
1511 return BLUETOOTH_ERROR_DEVICE_BUSY;
1514 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1516 proxy = __bt_get_core_proxy();
1517 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1519 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1521 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1524 BT_ERR("EnableAdapterLe failed: %s", error->message);
1525 _bt_adapter_set_le_status(BT_DEACTIVATED);
1526 g_clear_error(&error);
1528 /* Clean up the process */
1529 result = g_dbus_proxy_call_sync(proxy,
1532 G_DBUS_CALL_FLAGS_NONE,
1538 BT_ERR("Bt core call failed");
1540 BT_ERR("EnableAdapterLE Failed %s", error->message);
1541 g_clear_error(&error);
1544 g_variant_unref(result);
1545 /* Terminate myself */
1546 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1547 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1548 return BLUETOOTH_ERROR_INTERNAL;
1552 g_variant_unref(result);
1554 _bt_adapter_start_le_enable_timer();
1556 if (status == BT_ACTIVATED) {
1557 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1558 __bt_set_le_enabled();
1560 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1562 return BLUETOOTH_ERROR_NONE;
1565 int _bt_disable_adapter_le(void)
1569 bt_le_status_t bt_le_state;
1571 GError *error = NULL;
1573 bt_le_state = _bt_adapter_get_le_status();
1574 if (bt_le_state == BT_LE_DEACTIVATING) {
1575 BT_DBG("Disabling in progress");
1576 return BLUETOOTH_ERROR_IN_PROGRESS;
1579 if (bt_le_state == BT_LE_DEACTIVATED) {
1580 BT_DBG("Already disabled");
1581 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1584 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1586 proxy = __bt_get_core_proxy();
1588 return BLUETOOTH_ERROR_INTERNAL;
1590 result = g_dbus_proxy_call_sync(proxy,
1593 G_DBUS_CALL_FLAGS_NONE,
1599 if (error != NULL) {
1600 BT_ERR("Bt core call failed (Error: %s)", error->message);
1601 g_clear_error(&error);
1603 BT_ERR("Bt core call failed");
1604 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1605 return BLUETOOTH_ERROR_INTERNAL;
1608 g_variant_unref(result);
1609 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1610 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1612 return BLUETOOTH_ERROR_NONE;
1615 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1619 GError *error = NULL;
1620 const char *address;
1624 BT_CHECK_PARAMETER(local_address, return);
1626 proxy = _bt_get_adapter_properties_proxy();
1627 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1629 result = g_dbus_proxy_call_sync(proxy,
1631 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1633 G_DBUS_CALL_FLAGS_NONE,
1639 BT_ERR("Failed to get local address");
1640 if (error != NULL) {
1641 BT_ERR("Failed to get local address (Error: %s)", error->message);
1642 g_clear_error(&error);
1644 return BLUETOOTH_ERROR_INTERNAL;
1647 g_variant_get(result, "(v)", &temp);
1648 address = g_variant_get_string(temp, NULL);
1649 BT_DBG("Address:%s", address);
1652 _bt_convert_addr_string_to_type(local_address->addr, address);
1654 return BLUETOOTH_ERROR_INTERNAL;
1656 g_variant_unref(result);
1657 g_variant_unref(temp);
1658 return BLUETOOTH_ERROR_NONE;
1661 int _bt_get_local_version(bluetooth_version_t *local_version)
1664 const char *ver = NULL;
1666 int ret = BLUETOOTH_ERROR_NONE;
1670 BT_CHECK_PARAMETER(local_version, return);
1672 GError *error = NULL;
1674 proxy = _bt_get_adapter_properties_proxy();
1675 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1677 result = g_dbus_proxy_call_sync(proxy,
1679 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1681 G_DBUS_CALL_FLAGS_NONE,
1687 if (error != NULL) {
1688 BT_ERR("Failed to get local version (Error: %s)", error->message);
1689 g_clear_error(&error);
1691 BT_ERR("Failed to get local version");
1692 return BLUETOOTH_ERROR_INTERNAL;
1695 g_variant_get(result, "(v)", &temp);
1696 ver = g_variant_get_string(temp, NULL);
1697 BT_DBG("VERSION: %s", ver);
1699 if (ver && (strlen(ver) > 0)) {
1700 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1701 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1704 g_strlcpy(local_version->version, ver,
1705 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1708 ret = BLUETOOTH_ERROR_INTERNAL;
1711 g_variant_unref(result);
1712 g_variant_unref(temp);
1716 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1719 const char *name = NULL;
1721 int ret = BLUETOOTH_ERROR_NONE;
1724 GError *error = NULL;
1726 BT_CHECK_PARAMETER(local_name, return);
1728 proxy = _bt_get_adapter_properties_proxy();
1729 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1731 result = g_dbus_proxy_call_sync(proxy,
1733 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1735 G_DBUS_CALL_FLAGS_NONE,
1741 if (error != NULL) {
1742 BT_ERR("Failed to get local name (Error: %s)", error->message);
1743 g_clear_error(&error);
1745 BT_ERR("Failed to get local name");
1746 return BLUETOOTH_ERROR_INTERNAL;
1749 g_variant_get(result, "(v)", &temp);
1750 name = g_variant_get_string(temp, NULL);
1751 BT_DBG("LOCAL NAME:%s", name);
1753 if (name && (strlen(name) > 0)) {
1754 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1755 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1758 g_strlcpy(local_name->name, name,
1759 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1761 ret = BLUETOOTH_ERROR_INTERNAL;
1763 g_variant_unref(result);
1764 g_variant_unref(temp);
1768 int _bt_set_local_name(char *local_name)
1771 GError *error = NULL;
1775 BT_CHECK_PARAMETER(local_name, return);
1777 proxy = _bt_get_adapter_properties_proxy();
1779 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1781 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1784 result = g_dbus_proxy_call_sync(proxy,
1786 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1787 "Alias", g_variant_new("s", local_name)),
1788 G_DBUS_CALL_FLAGS_NONE,
1794 if (error != NULL) {
1795 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1796 g_clear_error(&error);
1798 BT_ERR("Failed to set Alias");
1799 return BLUETOOTH_ERROR_INTERNAL;
1802 g_variant_unref(result);
1803 return BLUETOOTH_ERROR_NONE;
1806 int _bt_is_service_used(char *service_uuid, gboolean *used)
1809 GError *error = NULL;
1810 int ret = BLUETOOTH_ERROR_NONE;
1812 GVariant *temp = NULL;
1813 GVariantIter *iter = NULL;
1817 BT_CHECK_PARAMETER(service_uuid, return);
1818 BT_CHECK_PARAMETER(used, return);
1820 proxy = _bt_get_adapter_properties_proxy();
1821 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1823 result = g_dbus_proxy_call_sync(proxy,
1825 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1827 G_DBUS_CALL_FLAGS_NONE,
1833 if (error != NULL) {
1834 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1835 g_clear_error(&error);
1837 BT_ERR("Failed to get UUIDs");
1838 return BLUETOOTH_ERROR_INTERNAL;
1841 g_variant_get(result, "(v)", &temp);
1842 g_variant_get(temp, "as", &iter);
1845 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1846 if (strcasecmp(uuid, service_uuid) == 0) {
1851 g_variant_iter_free(iter);
1852 g_variant_unref(result);
1854 BT_DBG("Service Used? %d", *used);
1859 static gboolean __bt_get_discoverable_property(void)
1862 gboolean discoverable_v;
1863 GError *error = NULL;
1867 proxy = _bt_get_adapter_properties_proxy();
1868 retv_if(proxy == NULL, FALSE);
1870 result = g_dbus_proxy_call_sync(proxy,
1872 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1874 G_DBUS_CALL_FLAGS_NONE,
1880 if (error != NULL) {
1881 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1882 g_clear_error(&error);
1884 BT_ERR("Failed to get Discoverable property");
1885 return BLUETOOTH_ERROR_INTERNAL;
1888 g_variant_get(result, "(v)", &temp);
1889 discoverable_v = g_variant_get_boolean(temp);
1890 BT_DBG("discoverable_v:%d", discoverable_v);
1892 g_variant_unref(result);
1893 g_variant_unref(temp);
1895 return discoverable_v;
1898 int _bt_get_discoverable_mode(int *mode)
1900 gboolean discoverable;
1901 unsigned int timeout;
1903 BT_CHECK_PARAMETER(mode, return);
1905 discoverable = __bt_get_discoverable_property();
1906 timeout = _bt_get_discoverable_timeout_property();
1908 if (discoverable == TRUE) {
1910 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1912 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1914 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1916 return BLUETOOTH_ERROR_NONE;
1920 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1922 int ret = BLUETOOTH_ERROR_NONE;
1925 GError *error = NULL;
1929 proxy = _bt_get_adapter_properties_proxy();
1931 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1933 #ifdef TIZEN_DPM_ENABLE
1934 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1935 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1936 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1937 return BLUETOOTH_ERROR_ACCESS_DENIED;
1939 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1940 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1941 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1942 return BLUETOOTH_ERROR_ACCESS_DENIED;
1946 switch (discoverable_mode) {
1947 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1952 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1957 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1962 return BLUETOOTH_ERROR_INVALID_PARAM;
1965 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1966 discoverable_mode, timeout);
1968 result = g_dbus_proxy_call_sync(proxy,
1970 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1971 "Connectable", g_variant_new("b", pg_scan)),
1972 G_DBUS_CALL_FLAGS_NONE,
1978 if (error != NULL) {
1979 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1980 g_clear_error(&error);
1982 BT_ERR("Failed to set connectable property");
1983 return BLUETOOTH_ERROR_INTERNAL;
1985 g_variant_unref(result);
1986 result = g_dbus_proxy_call_sync(proxy,
1988 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1989 g_variant_new("b", inq_scan)),
1990 G_DBUS_CALL_FLAGS_NONE,
1996 if (error != NULL) {
1997 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
1998 g_clear_error(&error);
2000 BT_ERR("Failed to set Discoverable property");
2001 return BLUETOOTH_ERROR_INTERNAL;
2003 g_variant_unref(result);
2004 result = g_dbus_proxy_call_sync(proxy,
2006 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
2007 "DiscoverableTimeout", g_variant_new("u", timeout)),
2008 G_DBUS_CALL_FLAGS_NONE,
2014 if (error != NULL) {
2015 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
2016 g_clear_error(&error);
2018 BT_ERR("Failed to set DiscoverableTimeout property");
2019 return BLUETOOTH_ERROR_INTERNAL;
2022 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
2025 ret = __bt_set_visible_time(timeout);
2027 g_variant_unref(result);
2032 int _bt_start_discovery(void)
2035 GError *error = NULL;
2038 if (_bt_is_discovering() == TRUE) {
2039 BT_ERR("BT is already in discovering");
2040 return BLUETOOTH_ERROR_IN_PROGRESS;
2041 } else if (_bt_is_device_creating() == TRUE) {
2042 BT_ERR("Bonding device is going on");
2043 return BLUETOOTH_ERROR_DEVICE_BUSY;
2046 proxy = _bt_get_adapter_proxy();
2047 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2049 result = g_dbus_proxy_call_sync(proxy,
2052 G_DBUS_CALL_FLAGS_NONE,
2058 if (error != NULL) {
2059 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
2060 g_clear_error(&error);
2062 BT_ERR("StartDiscovery failed");
2063 return BLUETOOTH_ERROR_INTERNAL;
2066 is_discovering = TRUE;
2067 cancel_by_user = FALSE;
2068 /* discovery status will be change in event */
2069 g_variant_unref(result);
2070 return BLUETOOTH_ERROR_NONE;
2073 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2077 GError *error = NULL;
2078 const gchar *disc_type;
2080 if (_bt_is_discovering() == TRUE) {
2081 BT_ERR("BT is already in discovering");
2082 return BLUETOOTH_ERROR_IN_PROGRESS;
2085 proxy = _bt_get_adapter_proxy();
2086 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2088 if (role == DISCOVERY_ROLE_BREDR)
2089 disc_type = "BREDR";
2090 else if (role == DISCOVERY_ROLE_LE)
2092 else if (role == DISCOVERY_ROLE_LE_BREDR)
2093 disc_type = "LE_BREDR";
2095 return BLUETOOTH_ERROR_INVALID_PARAM;
2097 result = g_dbus_proxy_call_sync(proxy,
2098 "StartCustomDiscovery",
2099 g_variant_new("s", disc_type),
2100 G_DBUS_CALL_FLAGS_NONE,
2106 if (error != NULL) {
2107 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2108 g_clear_error(&error);
2110 BT_ERR("StartCustomDiscovery failed");
2111 return BLUETOOTH_ERROR_INTERNAL;
2114 is_discovering = TRUE;
2115 cancel_by_user = FALSE;
2116 /* discovery status will be change in event */
2117 g_variant_unref(result);
2118 return BLUETOOTH_ERROR_NONE;
2121 int _bt_cancel_discovery(void)
2124 GError *error = NULL;
2127 if (_bt_is_discovering() == FALSE) {
2128 BT_ERR("BT is not in discovering");
2129 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2132 proxy = _bt_get_adapter_proxy();
2133 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2135 result = g_dbus_proxy_call_sync(proxy,
2138 G_DBUS_CALL_FLAGS_NONE,
2144 int ret = BLUETOOTH_ERROR_INTERNAL;
2145 if (error != NULL) {
2146 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2148 if (g_strrstr(error->message, "No discovery started"))
2149 ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
2151 g_clear_error(&error);
2153 BT_ERR("StopDiscovery failed");
2159 cancel_by_user = TRUE;
2160 /* discovery status will be change in event */
2161 g_variant_unref(result);
2162 return BLUETOOTH_ERROR_NONE;
2165 gboolean _bt_is_discovering(void)
2167 return is_discovering;
2170 gboolean _bt_is_connectable(void)
2173 GError *error = NULL;
2174 gboolean is_connectable = FALSE;
2178 proxy = _bt_get_adapter_properties_proxy();
2179 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2181 result = g_dbus_proxy_call_sync(proxy,
2183 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2185 G_DBUS_CALL_FLAGS_NONE,
2191 if (error != NULL) {
2192 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2193 g_clear_error(&error);
2195 BT_ERR("Failed to get connectable property");
2196 return BLUETOOTH_ERROR_INTERNAL;
2199 g_variant_get(result, "(v)", &temp);
2200 is_connectable = g_variant_get_boolean(temp);
2201 BT_DBG("discoverable_v:%d", is_connectable);
2203 g_variant_unref(result);
2204 g_variant_unref(temp);
2206 BT_INFO("Get connectable [%d]", is_connectable);
2207 return is_connectable;
2210 int _bt_set_connectable(gboolean is_connectable)
2213 GError *error = NULL;
2216 if (__bt_is_factory_test_mode()) {
2217 BT_ERR("Unable to set connectable in factory binary !!");
2218 return BLUETOOTH_ERROR_NOT_SUPPORT;
2221 proxy = _bt_get_adapter_properties_proxy();
2223 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2225 result = g_dbus_proxy_call_sync(proxy,
2227 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2228 g_variant_new("b", is_connectable)),
2229 G_DBUS_CALL_FLAGS_NONE,
2235 if (error != NULL) {
2236 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2237 g_clear_error(&error);
2239 BT_ERR("Failed to set connectable property");
2240 return BLUETOOTH_ERROR_INTERNAL;
2243 BT_INFO("### Set connectable [%d]", is_connectable);
2244 g_variant_unref(result);
2245 return BLUETOOTH_ERROR_NONE;
2248 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2251 gboolean discovering_v;
2252 GError *error = NULL;
2253 char *discovering_type = NULL;
2257 proxy = _bt_get_adapter_properties_proxy();
2258 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2260 if (discovery_type == DISCOVERY_ROLE_BREDR)
2261 discovering_type = "Discovering";
2262 else if (discovery_type == DISCOVERY_ROLE_LE)
2263 discovering_type = "LEDiscovering";
2265 result = g_dbus_proxy_call_sync(proxy,
2267 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2269 G_DBUS_CALL_FLAGS_NONE,
2275 if (error != NULL) {
2276 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2277 g_clear_error(&error);
2279 BT_ERR("Failed to get discovering property");
2280 return BLUETOOTH_ERROR_INTERNAL;
2283 g_variant_get(result, "(v)", &temp);
2284 discovering_v = g_variant_get_boolean(temp);
2285 BT_DBG("discoverable_v:%d", discovering_v);
2287 g_variant_unref(result);
2288 g_variant_unref(temp);
2290 return discovering_v;
2293 unsigned int _bt_get_discoverable_timeout_property(void)
2296 unsigned int timeout_v;
2297 GError *error = NULL;
2301 proxy = _bt_get_adapter_properties_proxy();
2302 retv_if(proxy == NULL, 0);
2304 result = g_dbus_proxy_call_sync(proxy,
2306 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2307 "DiscoverableTimeout"),
2308 G_DBUS_CALL_FLAGS_NONE,
2314 BT_ERR("Fail to get discoverable timeout");
2315 if (error != NULL) {
2316 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2317 g_clear_error(&error);
2322 g_variant_get(result, "(v)", &temp);
2323 timeout_v = g_variant_get_uint32(temp);
2324 BT_DBG("discoverable_v:%d", timeout_v);
2326 g_variant_unref(result);
2327 g_variant_unref(temp);
2332 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2334 bluetooth_device_info_t *dev_info;
2337 GByteArray *manufacturer_data = NULL;
2339 GVariantIter *char_value_iter;
2341 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2343 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2348 if (!g_strcmp0(key, "Address")) {
2349 const char *address = NULL;
2350 address = g_variant_get_string(value, NULL);
2351 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2353 } else if (!g_strcmp0(key, "Class")) {
2355 cod = g_variant_get_uint32(value);
2356 _bt_divide_device_class(&dev_info->device_class, cod);
2357 } else if (!g_strcmp0(key, "Name")) {
2358 const char *name = NULL;
2359 name = g_variant_get_string(value, NULL);
2360 /* If there is no Alias */
2361 if (strlen(dev_info->device_name.name) == 0) {
2362 g_strlcpy(dev_info->device_name.name, name,
2363 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2365 } else if (!g_strcmp0(key, "Alias")) {
2366 const char *alias = NULL;
2367 alias = g_variant_get_string(value, NULL);
2368 /* Overwrite the name */
2370 memset(dev_info->device_name.name, 0x00,
2371 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2372 g_strlcpy(dev_info->device_name.name, alias,
2373 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2375 } else if (!g_strcmp0(key, "IsAliasSet")) {
2376 dev_info->is_alias_set = g_variant_get_boolean(value);
2377 } else if (!g_strcmp0(key, "Connected")) {
2378 dev_info->connected = g_variant_get_byte(value);
2379 } else if (!g_strcmp0(key, "Paired")) {
2380 dev_info->paired = g_variant_get_boolean(value);
2381 } else if (!g_strcmp0(key, "Trusted")) {
2382 dev_info->trust = g_variant_get_boolean(value);
2383 } else if (!g_strcmp0(key, "RSSI")) {
2384 dev_info->rssi = g_variant_get_int16(value);
2385 } else if (!g_strcmp0(key, "UUIDs")) {
2391 dev_info->service_index = 0;
2392 g_variant_get(value, "as", &iter);
2393 while (g_variant_iter_loop(iter, "s", &uuid)) {
2394 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2395 parts = g_strsplit(uuid, "-", -1);
2397 if (parts == NULL || parts[0] == NULL) {
2402 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2407 dev_info->service_index = i;
2408 g_variant_iter_free(iter);
2409 } else if (strcasecmp(key, "ManufacturerDataLen") == 0) {
2410 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2411 } else if (strcasecmp(key, "ManufacturerData") == 0) {
2412 manufacturer_data = g_byte_array_new();
2413 g_variant_get(value, "ay", &char_value_iter);
2414 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
2415 g_byte_array_append(manufacturer_data, &char_value, 1);
2417 if (manufacturer_data) {
2418 if (manufacturer_data->len > 0)
2419 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2421 g_variant_iter_free(char_value_iter);
2422 g_byte_array_free(manufacturer_data, TRUE);
2429 static void __bt_extract_device_info(GVariantIter *iter,
2432 bluetooth_device_info_t *dev_info = NULL;
2433 char *object_path = NULL;
2434 GVariantIter *interface_iter;
2435 GVariantIter *svc_iter;
2436 char *interface_str = NULL;
2438 /* Parse the signature: oa{sa{sv}}} */
2439 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2442 if (object_path == NULL)
2445 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2446 &interface_str, &svc_iter)) {
2447 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2448 BT_DBG("Found a device: %s", object_path);
2449 dev_info = __bt_parse_device_info(svc_iter);
2451 if (dev_info->paired == TRUE) {
2452 g_array_append_vals(*dev_list, dev_info,
2453 sizeof(bluetooth_device_info_t));
2457 g_free(interface_str);
2458 g_variant_iter_free(svc_iter);
2466 int _bt_get_bonded_devices(GArray **dev_list)
2469 GDBusConnection *conn;
2470 GDBusProxy *manager_proxy;
2471 GVariant *result = NULL;
2472 GVariantIter *iter = NULL;
2473 GError *error = NULL;
2475 conn = _bt_gdbus_get_system_gconn();
2476 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2478 manager_proxy = _bt_get_manager_proxy();
2479 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2481 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2483 G_DBUS_CALL_FLAGS_NONE,
2489 if (error != NULL) {
2490 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2491 g_clear_error(&error);
2493 BT_ERR("Failed to Failed to GetManagedObjects");
2494 return BLUETOOTH_ERROR_INTERNAL;
2497 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2498 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2500 __bt_extract_device_info(iter, dev_list);
2501 g_variant_iter_free(iter);
2502 g_variant_unref(result);
2505 return BLUETOOTH_ERROR_NONE;
2508 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2511 GDBusConnection *conn;
2512 GDBusProxy *manager_proxy;
2513 GVariant *result = NULL;
2514 GVariant *result1 = NULL;
2515 GVariantIter *iter = NULL;
2516 GError *error = NULL;
2517 char *object_path = NULL;
2518 GVariantIter *interface_iter;
2519 char *interface_str = NULL;
2520 GDBusProxy *device_proxy = NULL;
2521 gboolean is_connected = FALSE;
2523 conn = _bt_gdbus_get_system_gconn();
2524 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2526 manager_proxy = _bt_get_manager_proxy();
2527 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2529 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2531 G_DBUS_CALL_FLAGS_NONE,
2537 if (error != NULL) {
2538 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2539 g_clear_error(&error);
2542 BT_ERR("Failed to Failed to GetManagedObjects");
2543 return BLUETOOTH_ERROR_INTERNAL;
2546 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2547 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2549 /* Parse the signature: oa{sa{sv}}} */
2550 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2551 if (object_path == NULL)
2554 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2555 &interface_str, NULL)) {
2556 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2557 BT_DBG("Found a device: %s", object_path);
2558 g_free(interface_str);
2560 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2561 NULL, BT_BLUEZ_NAME,
2562 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2564 if (device_proxy == NULL) {
2565 BT_DBG("Device don't have this service");
2569 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2570 g_variant_new("(s)", profile_uuid),
2571 G_DBUS_CALL_FLAGS_NONE,
2576 if (result1 == NULL) {
2577 BT_ERR("Error occured in Proxy call");
2579 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2580 g_error_free(error);
2583 g_object_unref(device_proxy);
2586 g_variant_get(result1, "(b)", &is_connected);
2588 if (is_connected == TRUE) {
2589 char address[BT_ADDRESS_STRING_SIZE];
2590 bluetooth_device_address_t *addr = NULL;
2592 _bt_convert_device_path_to_address(object_path, address);
2594 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2595 _bt_convert_addr_string_to_type(addr->addr, address);
2597 g_array_append_vals(*addr_list, addr,
2598 sizeof(bluetooth_device_address_t));
2601 g_variant_unref(result1);
2602 g_object_unref(device_proxy);
2609 g_variant_unref(result);
2610 g_variant_iter_free(iter);
2613 return BLUETOOTH_ERROR_NONE;
2616 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2617 bluetooth_device_info_t *dev_info)
2619 char *object_path = NULL;
2620 GDBusProxy *adapter_proxy;
2621 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2622 int ret = BLUETOOTH_ERROR_NONE;
2624 BT_CHECK_PARAMETER(device_address, return);
2625 BT_CHECK_PARAMETER(dev_info, return);
2627 adapter_proxy = _bt_get_adapter_proxy();
2628 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2630 _bt_convert_addr_type_to_string(address, device_address->addr);
2632 object_path = _bt_get_device_object_path(address);
2634 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2636 ret = __bt_get_bonded_device_info(object_path, dev_info);
2637 g_free(object_path);
2642 int _bt_is_alias_set(bluetooth_device_address_t *device_address, gboolean *is_alias_set)
2644 char *object_path = NULL;
2645 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2646 gboolean alias_set = FALSE;
2648 GDBusConnection *conn;
2649 GDBusProxy *device_proxy;
2650 GError *error = NULL;
2651 GVariant *result = NULL;
2652 GVariant *temp = NULL;
2655 BT_CHECK_PARAMETER(device_address, return);
2656 BT_CHECK_PARAMETER(is_alias_set, return);
2658 _bt_convert_addr_type_to_string(address, device_address->addr);
2660 object_path = _bt_get_device_object_path(address);
2661 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2663 conn = _bt_gdbus_get_system_gconn();
2665 g_free(object_path);
2666 return BLUETOOTH_ERROR_INTERNAL;
2669 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2673 BT_PROPERTIES_INTERFACE,
2675 if (device_proxy == NULL) {
2676 g_free(object_path);
2677 return BLUETOOTH_ERROR_INTERNAL;
2680 result = g_dbus_proxy_call_sync(device_proxy, "Get",
2681 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "IsAliasSet"),
2682 G_DBUS_CALL_FLAGS_NONE,
2688 BT_ERR("Error occured in Proxy call");
2689 if (error != NULL) {
2690 BT_ERR("Getting is_alias_set property failed: [%s]\n", error->message);
2691 g_error_free(error);
2693 g_object_unref(device_proxy);
2694 g_free(object_path);
2695 return BLUETOOTH_ERROR_INTERNAL;
2698 g_variant_get(result, "(v)", &temp);
2699 alias_set = g_variant_get_boolean(temp);
2700 *is_alias_set = alias_set;
2701 BT_DBG("address: [%s] | *is_alias_set: %s", address, *is_alias_set ? "TRUE" : "FALSE");
2702 g_variant_unref(temp);
2703 g_variant_unref(result);
2704 g_object_unref(device_proxy);
2706 g_free(object_path);
2708 return BLUETOOTH_ERROR_NONE;
2711 int _bt_get_timeout_value(int *timeout)
2713 time_t current_time;
2716 /* Take current time */
2717 time(¤t_time);
2718 time_diff = difftime(current_time, visible_timer.start_time);
2720 BT_DBG("Time diff = %d\n", time_diff);
2722 *timeout = visible_timer.timeout - time_diff;
2724 return BLUETOOTH_ERROR_NONE;
2727 int _bt_set_le_privacy(gboolean set_privacy)
2730 GError *error = NULL;
2731 GVariant *result = NULL;
2733 if (__bt_is_factory_test_mode()) {
2734 BT_ERR("Unable to set le privacy in factory binary !!");
2735 return BLUETOOTH_ERROR_NOT_SUPPORT;
2738 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2739 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2740 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2743 proxy = _bt_get_adapter_proxy();
2744 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2746 result = g_dbus_proxy_call_sync(proxy,
2748 g_variant_new("(b)", set_privacy),
2749 G_DBUS_CALL_FLAGS_NONE,
2755 if (error != NULL) {
2756 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2757 g_clear_error(&error);
2759 BT_ERR("Failed to SetLePrivacy");
2760 return BLUETOOTH_ERROR_INTERNAL;
2763 g_variant_unref(result);
2764 BT_INFO("SetLePrivacy as %d", set_privacy);
2765 return BLUETOOTH_ERROR_NONE;
2768 int _bt_set_le_static_random_address(gboolean is_enable)
2771 GError *error = NULL;
2772 GVariant *result = NULL;
2774 if (__bt_is_factory_test_mode()) {
2775 BT_ERR("Unable to set le random address in factory binary !!");
2776 return BLUETOOTH_ERROR_NOT_SUPPORT;
2779 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2780 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2781 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2784 proxy = _bt_get_adapter_proxy();
2785 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2787 result = g_dbus_proxy_call_sync(proxy,
2788 "SetLeStaticRandomAddress",
2789 g_variant_new("(b)", is_enable),
2790 G_DBUS_CALL_FLAGS_NONE,
2796 if (error != NULL) {
2797 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2798 g_clear_error(&error);
2800 BT_ERR("Failed to SetLeStaticRandomAddress");
2801 return BLUETOOTH_ERROR_INTERNAL;
2804 g_variant_unref(result);
2805 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2806 return BLUETOOTH_ERROR_NONE;
2809 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2812 GError *error = NULL;
2816 GVariantBuilder *builder;
2818 BT_CHECK_PARAMETER(m_data, return);
2820 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2821 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2822 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2825 proxy = _bt_get_adapter_proxy();
2826 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2828 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2830 for (i = 0; i < (m_data->data_len) + 2; i++)
2831 g_variant_builder_add(builder, "y", m_data->data[i]);
2833 val = g_variant_new("(ay)", builder);
2835 result = g_dbus_proxy_call_sync(proxy,
2836 "SetManufacturerData",
2838 G_DBUS_CALL_FLAGS_NONE,
2842 g_variant_builder_unref(builder);
2844 if (error != NULL) {
2845 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2846 g_clear_error(&error);
2848 BT_ERR("Failed to SetManufacturerData");
2850 return BLUETOOTH_ERROR_INTERNAL;
2852 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2854 for (i = 0; i < (m_data->data_len) + 2; i++)
2855 g_variant_builder_add(builder, "y", m_data->data[i]);
2857 val = g_variant_new("(ay)", builder);
2859 _bt_send_event(BT_ADAPTER_EVENT,
2860 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2863 BT_INFO("Set manufacturer data");
2865 g_variant_builder_unref(builder);
2866 g_variant_unref(result);
2868 return BLUETOOTH_ERROR_NONE;
2872 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2874 int result = BLUETOOTH_ERROR_NONE;
2875 bt_service_alarm_t *alarm = NULL;
2877 if (!call_back || !alarm_id)
2878 return BLUETOOTH_ERROR_INVALID_PARAM;
2880 if (!alarm_mgr.is_alarm_initialized) {
2881 result = alarmmgr_init("bt-service");
2883 BT_ERR("Failed to initialize alarm = %d", result);
2884 result = BLUETOOTH_ERROR_INTERNAL;
2887 result = alarmmgr_set_cb(alarm_cb, NULL);
2889 BT_ERR("Failed to set the callback = %d", result);
2890 result = BLUETOOTH_ERROR_INTERNAL;
2893 alarm_mgr.is_alarm_initialized = TRUE;
2896 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2898 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2900 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2903 BT_ERR("Failed to create alarm error = %d", result);
2904 result = BLUETOOTH_ERROR_INTERNAL;
2908 alarm->alarm_id = *alarm_id;
2909 alarm->callback = call_back;
2910 alarm->user_data = user_data;
2912 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2913 result = BLUETOOTH_ERROR_NONE;
2918 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2921 bt_service_alarm_t *p_data;
2922 bt_set_alarm_cb callback = NULL;
2923 void *user_data = NULL;
2925 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2926 GINT_TO_POINTER(alarm_id), compare_alarm);
2930 p_data = (bt_service_alarm_t *)node->data;
2931 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2937 callback = p_data->callback;
2938 user_data = p_data->user_data;
2942 callback(alarm_id, user_data);
2947 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2950 bt_service_alarm_t *p_data;
2951 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2954 alarmmgr_remove_alarm(alarm_id);
2955 p_data = (bt_service_alarm_t *)list->data;
2956 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2963 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2965 alarm_id_t alarm_id = (alarm_id_t)data;
2966 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2968 if (p_data->alarm_id == alarm_id)
2974 static void alarm_data_free(void *data)
2976 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2981 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2984 int function = (int)user_data;
2987 case BT_ENABLE_ADAPTER:
2988 result = _bt_enable_adapter();
2989 if (result != BLUETOOTH_ERROR_NONE) {
2990 BT_ERR("_bt_enable_adapter is failed");
2991 /* Send enabled event to API */
2992 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
2993 g_variant_new("(i)", result));
2996 case BT_DISABLE_ADAPTER:
2997 result = _bt_disable_adapter();
2998 if (result != BLUETOOTH_ERROR_NONE) {
2999 BT_ERR("_bt_disable_adapter is failed");
3000 /* Send disabled event to API */
3001 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
3002 g_variant_new("(i)", result));
3006 BT_ERR("function is NOT matched");
3013 int _bt_adapter_request_delayed(int function)
3018 case BT_ENABLE_ADAPTER:
3019 ret = _bt_enable_adapter_check_status();
3020 if (ret == BLUETOOTH_ERROR_NONE)
3021 _bt_adapter_set_status(BT_ACTIVATING);
3026 case BT_DISABLE_ADAPTER:
3027 ret = _bt_disable_adapter_check_status();
3028 if (ret == BLUETOOTH_ERROR_NONE)
3029 _bt_adapter_set_status(BT_DEACTIVATING);
3035 BT_ERR("function is NOT matched");
3036 return BLUETOOTH_ERROR_INTERNAL;
3039 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
3041 return BLUETOOTH_ERROR_NONE;
3045 int _bt_get_enable_timer_id(void)