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, "LegacyManufacturerDataLen")) {
359 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
360 } else if (!g_strcmp0(key, "LegacyManufacturerData")) {
361 manufacturer_data = g_byte_array_new();
362 g_variant_get(value, "ay", &char_value_iter);
363 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
364 g_byte_array_append(manufacturer_data, &char_value, 1);
366 g_variant_iter_free(char_value_iter);
368 if (manufacturer_data) {
369 if (manufacturer_data->len > 0) {
370 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
371 manufacturer_data->len);
374 g_byte_array_free(manufacturer_data, TRUE);
377 g_variant_iter_free(property_iter);
379 BT_DBG("trust: %d, paired: %d", trust, paired);
381 g_variant_unref(result);
383 if ((paired == FALSE) && (trust == FALSE)) {
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_INFO("device_name is changed to %s", phone_name);
468 _bt_set_local_name(phone_name);
470 BT_ERR("phone_name is NOT valid");
473 BT_ERR("vconf type is NOT string");
477 static void __bt_set_local_name(void)
479 bluetooth_device_name_t local_name;
480 char *phone_name = NULL;
484 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
485 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
486 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
491 if (strlen(phone_name) != 0) {
492 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
495 _bt_set_local_name(phone_name);
501 static int __bt_set_enabled(void)
503 int adapter_status = BT_ADAPTER_DISABLED;
504 int result = BLUETOOTH_ERROR_NONE;
507 BT_DBG("g_source is removed");
508 g_source_remove(timer_id);
512 _bt_check_adapter(&adapter_status);
514 if (adapter_status == BT_ADAPTER_DISABLED) {
515 BT_ERR("Bluetoothd is not running");
516 return BLUETOOTH_ERROR_INTERNAL;
519 #ifdef TIZEN_PROFILE_MOBILE || defined(TIZEN_PROFILE_IVI)
520 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
521 if (_bt_set_discoverable_mode(
522 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
523 BT_ERR("Set connectable mode failed");
526 if (_bt_set_discoverable_mode(
527 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
528 BT_ERR("Fail to set discoverable mode");
532 /* Update Bluetooth Status to notify other modules */
533 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
534 BT_ERR("Set vconf failed\n");
536 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
537 BT_ERR("Set vconf failed\n");
539 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
540 EVT_VAL_BT_ON) != ES_R_OK)
541 BT_ERR("Fail to set value");
543 /* Send enabled event to API */
544 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
545 g_variant_new("(i)", result));
547 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
548 _bt_audio_start_auto_connect(FALSE);
551 __bt_set_local_name();
552 _bt_set_discovery_status(FALSE);
554 return BLUETOOTH_ERROR_NONE;
557 void _bt_set_disabled(int result)
559 int power_off_status = 0;
562 int pm_ignore_mode = 0;
564 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
565 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
567 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
569 /* Update the vconf BT status in normal Deactivation case only */
570 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
571 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
573 BT_DBG("Update vconf for BT normal Deactivation");
575 if (result == BLUETOOTH_ERROR_TIMEOUT)
576 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
577 BT_ERR("Set vconf failed");
579 /* Update Bluetooth Status to notify other modules */
580 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
581 BT_ERR("Set vconf failed");
583 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
584 EVT_VAL_BT_OFF) != ES_R_OK)
585 BT_ERR("Fail to set value");
588 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
589 BT_ERR("Set vconf failed\n");
591 _bt_cancel_queued_transfers();
592 _bt_adapter_set_status(BT_DEACTIVATED);
593 _bt_set_discovery_status(FALSE);
595 #ifndef USB_BLUETOOTH
596 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
598 /* Send disabled event */
599 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
600 g_variant_new("(i)", result));
601 #ifndef USB_BLUETOOTH
605 BT_INFO("Adapter disabled");
608 static int __bt_set_le_enabled(void)
611 int result = BLUETOOTH_ERROR_NONE;
614 /* Update Bluetooth Status to notify other modules */
615 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
616 BT_ERR("Set vconf failed\n");
618 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
619 EVT_VAL_BT_LE_ON) != ES_R_OK)
620 BT_ERR("Fail to set value");
622 /* Send enabled event to API */
624 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
625 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
627 status = _bt_adapter_get_status();
628 if (status == BT_DEACTIVATED) {
629 BT_INFO("BREDR is off, turn off PSCAN");
630 _bt_set_connectable(FALSE);
632 if (le_timer_id > 0) {
633 g_source_remove(le_timer_id);
637 /* Send enabled event to API */
638 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
639 g_variant_new("(i)", result));
641 __bt_set_local_name();
644 return BLUETOOTH_ERROR_NONE;
647 void _bt_set_le_disabled(int result)
649 int power_off_status;
652 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
653 BT_DBG("ret : %d", ret);
654 BT_DBG("power_off_status : %d", power_off_status);
656 /* Update Bluetooth Status to notify other modules */
657 BT_DBG("Update vconf for BT LE normal Deactivation");
658 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
659 BT_ERR("Set vconf failed\n");
660 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
662 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
663 EVT_VAL_BT_LE_OFF) != ES_R_OK)
664 BT_ERR("Fail to set value");
666 /* Send disabled event */
667 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
668 g_variant_new("(i)", result));
671 void *_bt_get_adapter_agent(void)
673 return adapter_agent;
676 int _bt_enable_core(void)
680 GError *error = NULL;
682 proxy = __bt_get_core_proxy();
683 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
685 /* Clean up the process */
686 result = g_dbus_proxy_call_sync(proxy,
689 G_DBUS_CALL_FLAGS_NONE,
696 BT_ERR("Bt core call failed(Error: %s)", error->message);
697 g_clear_error(&error);
699 BT_ERR("Bt core call failed");
700 return BLUETOOTH_ERROR_INTERNAL;
703 g_variant_unref(result);
704 return BLUETOOTH_ERROR_NONE;
707 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
708 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
710 gboolean flight_mode = FALSE;
711 int power_saving_mode = 0;
714 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
715 type = vconf_keynode_get_type(node);
716 if (type == VCONF_TYPE_BOOL) {
717 flight_mode = vconf_keynode_get_bool(node);
718 if (flight_mode != TRUE) {
719 BT_ERR("Ignore the event");
722 } else if (type == VCONF_TYPE_INT) {
723 power_saving_mode = vconf_keynode_get_int(node);
724 if (power_saving_mode != 2) {
725 BT_ERR("Ignore the event");
729 BT_ERR("Invaild vconf key type : %d", type);
737 void _bt_service_register_vconf_handler(void)
741 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
742 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
743 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
744 BT_ERR("Unable to register key handler");
746 BT_DBG("Telephony is disabled");
749 #ifndef TIZEN_PROFILE_WEARABLE
750 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
751 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
752 BT_ERR("Unable to register key handler");
756 void _bt_service_unregister_vconf_handler(void)
760 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
761 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
762 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
765 #ifndef TIZEN_PROFILE_WEARABLE
766 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
767 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
771 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
773 const char *bt_status = NULL;
774 const char *bt_le_status = NULL;
775 BT_DBG("bt state set event(%s) received", event_name);
777 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
778 BT_DBG("bt_state: (%s)", bt_status);
780 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
781 BT_DBG("bt_state: (%s)", bt_le_status);
784 void _bt_handle_adapter_added(void)
788 bt_le_status_t le_status;
792 BT_DBG("g_source is removed");
793 g_source_remove(timer_id);
798 status = _bt_adapter_get_status();
799 le_status = _bt_adapter_get_le_status();
800 BT_INFO("status : %d", status);
801 BT_INFO("le_status : %d", le_status);
803 #ifndef USB_BLUETOOTH
804 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
805 if (!adapter_agent) {
806 BT_ERR("Fail to register agent");
810 if (adapter_agent == NULL) {
811 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
812 if (!adapter_agent) {
813 BT_ERR("Fail to register agent");
819 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
820 BT_ERR("Fail to register media player");
822 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
823 BT_ERR("Fail to init obex server");
825 #ifdef TIZEN_BT_PAN_NAP_ENABLED
826 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
827 BT_ERR("Fail to activate network");
830 /* add the vconf noti handler */
831 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
832 __bt_phone_name_changed_cb, NULL);
834 BT_ERR("Unable to register key handler");
836 if (le_status == BT_LE_ACTIVATING ||
837 status == BT_ACTIVATING) {
838 __bt_set_le_enabled();
839 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
842 if (status == BT_ACTIVATING) {
844 _bt_adapter_set_status(BT_ACTIVATED);
848 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
849 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
850 BT_ERR("Fail to register system event");
854 void _bt_handle_adapter_removed(void)
858 _bt_adapter_set_status(BT_DEACTIVATED);
860 __bt_visibility_alarm_remove();
862 if (alarm_mgr.is_alarm_initialized == TRUE) {
864 alarm_mgr.is_alarm_initialized = FALSE;
865 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
866 alarm_mgr.g_alarm_list = NULL;
869 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
870 _bt_audio_stop_auto_connect();
873 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
874 (vconf_callback_fn)__bt_phone_name_changed_cb);
876 ERR("vconf_ignore_key_changed failed\n");
878 #ifndef USB_BLUETOOTH
879 _bt_destroy_agent(adapter_agent);
880 adapter_agent = NULL;
882 if (is_recovery_mode == TRUE) {
883 /* Send disabled event */
884 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
886 /* Will recover BT by bt-core, so set the mode as activating */
887 _bt_adapter_set_status(BT_ACTIVATING);
888 is_recovery_mode = FALSE;
890 _bt_reliable_terminate_service(NULL);
893 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
896 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
897 BT_ERR("Fail to unregister system event");
900 static gboolean __bt_enable_timeout_cb(gpointer user_data)
904 GError *error = NULL;
908 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
910 BT_ERR("EnableAdapter is failed");
912 proxy = __bt_get_core_proxy();
916 /* Clean up the process */
917 result = g_dbus_proxy_call_sync(proxy,
920 G_DBUS_CALL_FLAGS_NONE,
927 BT_ERR("Bt core call failed(Error: %s)", error->message);
928 g_clear_error(&error);
930 BT_ERR("Bt core call failed");
935 g_variant_unref(result);
936 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
938 #ifndef USB_BLUETOOTH
939 _bt_terminate_service(NULL);
945 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
949 GError *error = NULL;
953 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
955 BT_ERR("EnableAdapterLE is failed");
957 proxy = __bt_get_core_proxy();
961 /* Clean up the process */
962 result = g_dbus_proxy_call_sync(proxy,
965 G_DBUS_CALL_FLAGS_NONE,
972 BT_ERR("Bt core call failed(Error: %s)", error->message);
973 g_clear_error(&error);
975 BT_ERR("Bt core call failed");
979 g_variant_unref(result);
980 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
982 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
984 if (_bt_adapter_get_status() == BT_DEACTIVATED)
985 _bt_terminate_service(NULL);
990 void _bt_adapter_start_le_enable_timer(void)
992 if (le_timer_id > 0) {
993 g_source_remove(le_timer_id);
997 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
998 __bt_enable_le_timeout_cb, NULL);
1003 void _bt_adapter_start_enable_timer(void)
1006 g_source_remove(timer_id);
1010 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1011 __bt_enable_timeout_cb, NULL);
1017 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
1022 _bt_adapter_set_status(BT_ACTIVATED);
1028 int _bt_enable_adapter_check_status(void)
1030 bt_status_t status = _bt_adapter_get_status();
1031 bt_le_status_t le_status = _bt_adapter_get_le_status();
1035 if (status == BT_ACTIVATING) {
1036 BT_ERR("Enabling in progress");
1037 return BLUETOOTH_ERROR_IN_PROGRESS;
1040 if (status == BT_ACTIVATED) {
1041 BT_ERR("Already enabled");
1042 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1045 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1046 BT_ERR("Disabling in progress");
1047 return BLUETOOTH_ERROR_DEVICE_BUSY;
1050 return BLUETOOTH_ERROR_NONE;
1053 int _bt_enable_adapter(void)
1056 GError *error = NULL;
1058 GVariant *result = NULL;
1059 bt_status_t status = _bt_adapter_get_status();
1060 bt_le_status_t le_status = _bt_adapter_get_le_status();
1064 if (status == BT_ACTIVATING) {
1065 BT_ERR("Enabling in progress");
1066 return BLUETOOTH_ERROR_IN_PROGRESS;
1069 if (status == BT_ACTIVATED) {
1070 BT_ERR("Already enabled");
1071 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1074 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1075 BT_ERR("Disabling in progress");
1076 return BLUETOOTH_ERROR_DEVICE_BUSY;
1079 _bt_adapter_set_status(BT_ACTIVATING);
1083 int adapter_status = BT_ADAPTER_DISABLED;
1085 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1086 BT_ERR("Set vconf failed");
1088 _bt_check_adapter(&adapter_status);
1089 if (adapter_status == BT_ADAPTER_ENABLED) {
1090 g_idle_add(__bt_adapter_enabled_cb, NULL);
1091 _bt_adapter_start_enable_timer();
1092 return BLUETOOTH_ERROR_NONE;
1097 proxy = __bt_get_core_proxy();
1099 return BLUETOOTH_ERROR_INTERNAL;
1101 if (le_status == BT_LE_ACTIVATED) {
1102 BT_INFO("LE Already enabled. Just turn on PSCAN");
1103 ret = _bt_set_connectable(TRUE);
1104 if (ret == BLUETOOTH_ERROR_NONE)
1105 _bt_adapter_set_status(BT_ACTIVATED);
1107 return BLUETOOTH_ERROR_INTERNAL;
1110 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1112 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1115 BT_ERR("EnableAdapterLe failed: %s", error->message);
1116 _bt_adapter_set_status(BT_DEACTIVATED);
1117 g_clear_error(&error);
1119 result = g_dbus_proxy_call_sync(proxy,
1122 G_DBUS_CALL_FLAGS_NONE,
1127 if (error != NULL) {
1128 BT_ERR("Bt core call failed(Error: %s)", error->message);
1129 g_clear_error(&error);
1131 g_variant_unref(result);
1132 #ifndef USB_BLUETOOTH
1133 /* Terminate myself */
1134 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1136 return BLUETOOTH_ERROR_INTERNAL;
1138 g_variant_unref(result);
1139 if (le_status == BT_LE_ACTIVATED)
1142 _bt_adapter_start_enable_timer();
1144 return BLUETOOTH_ERROR_NONE;
1147 static gboolean __bt_disconnect_all(void)
1150 GDBusConnection *conn;
1151 GDBusProxy *dev_proxy;
1152 gboolean ret = FALSE;
1154 GError *error = NULL;
1155 GArray *device_list;
1156 bluetooth_device_info_t info;
1158 char *device_path = NULL;
1159 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1164 conn = _bt_gdbus_get_system_gconn();
1166 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1168 if (_bt_get_bonded_devices(&device_list)
1169 != BLUETOOTH_ERROR_NONE) {
1170 g_array_free(device_list, TRUE);
1174 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1176 for (i = 0; i < size; i++) {
1178 info = g_array_index(device_list,
1179 bluetooth_device_info_t, i);
1181 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1182 BT_DBG("Found Connected device");
1183 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1184 device_path = _bt_get_device_object_path(address);
1185 if (device_path == NULL)
1188 BT_DBG("Disconnecting : %s", device_path);
1190 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1194 BT_DEVICE_INTERFACE,
1197 if (dev_proxy == NULL)
1200 result = g_dbus_proxy_call_sync(dev_proxy,
1203 G_DBUS_CALL_FLAGS_NONE,
1209 if (error != NULL) {
1210 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1211 g_clear_error(&error);
1213 BT_ERR("Disconnect call failed");
1214 g_object_unref(dev_proxy);
1218 g_variant_unref(result);
1219 g_object_unref(dev_proxy);
1223 g_array_free(device_list, TRUE);
1229 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1232 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1238 int _bt_disable_cb(void)
1246 GError *error = NULL;
1248 _bt_adapter_set_status(BT_DEACTIVATING);
1250 bt_le_status_t le_status;
1251 le_status = _bt_adapter_get_le_status();
1252 BT_DBG("le_status : %d", le_status);
1253 if (le_status == BT_LE_ACTIVATED) {
1254 BT_INFO("LE is enabled. Just turn off PSCAN");
1256 if (_bt_is_discovering())
1257 _bt_cancel_discovery();
1259 if (_bt_is_connectable() == FALSE) {
1260 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1262 ret = _bt_set_connectable(FALSE);
1263 if (ret != BLUETOOTH_ERROR_NONE) {
1264 BT_ERR("_bt_set_connectable fail!");
1265 _bt_adapter_set_status(BT_ACTIVATED);
1266 return BLUETOOTH_ERROR_INTERNAL;
1271 proxy = __bt_get_core_proxy();
1272 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1274 result = g_dbus_proxy_call_sync(proxy,
1277 G_DBUS_CALL_FLAGS_NONE,
1283 if (error != NULL) {
1284 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1285 g_clear_error(&error);
1287 BT_ERR("Failed to DisableAdapter");
1288 _bt_adapter_set_status(BT_ACTIVATED);
1289 return BLUETOOTH_ERROR_INTERNAL;
1292 g_variant_unref(result);
1293 return BLUETOOTH_ERROR_NONE;
1296 int _bt_disable_adapter_check_status(void)
1298 bt_status_t status = _bt_adapter_get_status();
1302 if (status == BT_DEACTIVATING) {
1303 BT_DBG("Disabling in progress");
1304 return BLUETOOTH_ERROR_IN_PROGRESS;
1307 if (status == BT_DEACTIVATED) {
1308 BT_DBG("Already disabled");
1309 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1312 return BLUETOOTH_ERROR_NONE;
1315 int _bt_disable_adapter(void)
1320 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1321 BT_DBG("Disabling in progress");
1322 return BLUETOOTH_ERROR_IN_PROGRESS;
1325 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1326 BT_DBG("Already disabled");
1327 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1331 g_source_remove(timer_id);
1335 __bt_disconnect_all();
1336 ret = _bt_disable_cb();
1342 int _bt_recover_adapter(void)
1347 GError *error = NULL;
1349 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1350 BT_ERR("Disabling in progress");
1351 return BLUETOOTH_ERROR_IN_PROGRESS;
1354 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1355 BT_ERR("Already disabled");
1356 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1359 _bt_adapter_set_status(BT_DEACTIVATING);
1361 proxy = __bt_get_core_proxy();
1362 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1364 result = g_dbus_proxy_call_sync(proxy,
1367 G_DBUS_CALL_FLAGS_NONE,
1373 if (error != NULL) {
1374 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1375 g_clear_error(&error);
1377 BT_ERR("Failed to RecoverAdapter");
1378 return BLUETOOTH_ERROR_INTERNAL;
1381 is_recovery_mode = TRUE;
1383 g_variant_unref(result);
1384 __bt_disconnect_all();
1387 return BLUETOOTH_ERROR_NONE;
1390 int _bt_reset_adapter(void)
1394 GError *error = NULL;
1398 proxy = __bt_get_core_proxy();
1400 return BLUETOOTH_ERROR_INTERNAL;
1402 result = g_dbus_proxy_call_sync(proxy,
1405 G_DBUS_CALL_FLAGS_NONE,
1411 if (error != NULL) {
1412 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1413 g_clear_error(&error);
1415 BT_ERR("Failed to ResetAdapter");
1416 return BLUETOOTH_ERROR_INTERNAL;
1419 g_variant_unref(result);
1420 /* Terminate myself */
1421 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1422 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1424 return BLUETOOTH_ERROR_NONE;
1428 int _bt_check_adapter(int *status)
1431 char *adapter_path = NULL;
1433 BT_CHECK_PARAMETER(status, return);
1435 *status = BT_ADAPTER_DISABLED;
1437 adapter_path = _bt_get_adapter_path();
1440 if (adapter_path != NULL)
1441 *status = BT_ADAPTER_ENABLED;
1443 g_free(adapter_path);
1444 return BLUETOOTH_ERROR_NONE;
1447 int _bt_check_adapter(int *status)
1450 GError *error = NULL;
1453 gboolean powered = FALSE;
1455 BT_CHECK_PARAMETER(status, return);
1457 *status = BT_ADAPTER_DISABLED;
1459 proxy = _bt_get_adapter_properties_proxy();
1460 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1462 result = g_dbus_proxy_call_sync(proxy,
1464 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1466 G_DBUS_CALL_FLAGS_NONE,
1472 BT_ERR("Failed to get local address");
1473 if (error != NULL) {
1474 BT_ERR("Failed to get local address (Error: %s)", error->message);
1475 g_clear_error(&error);
1477 return BLUETOOTH_ERROR_INTERNAL;
1480 g_variant_get(result, "(v)", &temp);
1481 powered = g_variant_get_boolean(temp);
1482 BT_DBG("powered: %d", powered);
1485 *status = BT_ADAPTER_ENABLED;
1487 g_variant_unref(result);
1488 g_variant_unref(temp);
1489 return BLUETOOTH_ERROR_NONE;
1493 int _bt_enable_adapter_le(void)
1497 GError *error = NULL;
1498 bt_status_t status = _bt_adapter_get_status();
1499 bt_le_status_t le_status = _bt_adapter_get_le_status();
1502 if (le_status == BT_LE_ACTIVATING) {
1503 BT_ERR("Enabling in progress");
1504 return BLUETOOTH_ERROR_IN_PROGRESS;
1507 if (le_status == BT_LE_ACTIVATED) {
1508 BT_ERR("Already enabled");
1509 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1512 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1513 BT_ERR("Disabling in progress");
1514 return BLUETOOTH_ERROR_DEVICE_BUSY;
1517 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1519 proxy = __bt_get_core_proxy();
1520 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1522 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1524 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1527 BT_ERR("EnableAdapterLe failed: %s", error->message);
1528 _bt_adapter_set_le_status(BT_DEACTIVATED);
1529 g_clear_error(&error);
1531 /* Clean up the process */
1532 result = g_dbus_proxy_call_sync(proxy,
1535 G_DBUS_CALL_FLAGS_NONE,
1541 BT_ERR("Bt core call failed");
1543 BT_ERR("EnableAdapterLE Failed %s", error->message);
1544 g_clear_error(&error);
1547 g_variant_unref(result);
1548 /* Terminate myself */
1549 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1550 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1551 return BLUETOOTH_ERROR_INTERNAL;
1555 g_variant_unref(result);
1557 _bt_adapter_start_le_enable_timer();
1559 if (status == BT_ACTIVATED) {
1560 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1561 __bt_set_le_enabled();
1563 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1565 return BLUETOOTH_ERROR_NONE;
1568 int _bt_disable_adapter_le(void)
1572 bt_le_status_t bt_le_state;
1574 GError *error = NULL;
1576 bt_le_state = _bt_adapter_get_le_status();
1577 if (bt_le_state == BT_LE_DEACTIVATING) {
1578 BT_DBG("Disabling in progress");
1579 return BLUETOOTH_ERROR_IN_PROGRESS;
1582 if (bt_le_state == BT_LE_DEACTIVATED) {
1583 BT_DBG("Already disabled");
1584 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1587 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1589 proxy = __bt_get_core_proxy();
1591 return BLUETOOTH_ERROR_INTERNAL;
1593 result = g_dbus_proxy_call_sync(proxy,
1596 G_DBUS_CALL_FLAGS_NONE,
1602 if (error != NULL) {
1603 BT_ERR("Bt core call failed (Error: %s)", error->message);
1604 g_clear_error(&error);
1606 BT_ERR("Bt core call failed");
1607 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1608 return BLUETOOTH_ERROR_INTERNAL;
1611 g_variant_unref(result);
1612 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1613 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1615 return BLUETOOTH_ERROR_NONE;
1618 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1622 GError *error = NULL;
1623 const char *address;
1627 BT_CHECK_PARAMETER(local_address, return);
1629 proxy = _bt_get_adapter_properties_proxy();
1630 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1632 result = g_dbus_proxy_call_sync(proxy,
1634 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1636 G_DBUS_CALL_FLAGS_NONE,
1642 BT_ERR("Failed to get local address");
1643 if (error != NULL) {
1644 BT_ERR("Failed to get local address (Error: %s)", error->message);
1645 g_clear_error(&error);
1647 return BLUETOOTH_ERROR_INTERNAL;
1650 g_variant_get(result, "(v)", &temp);
1651 address = g_variant_get_string(temp, NULL);
1652 BT_DBG("Address:%s", address);
1655 _bt_convert_addr_string_to_type(local_address->addr, address);
1657 return BLUETOOTH_ERROR_INTERNAL;
1659 g_variant_unref(result);
1660 g_variant_unref(temp);
1661 return BLUETOOTH_ERROR_NONE;
1664 int _bt_get_local_version(bluetooth_version_t *local_version)
1667 const char *ver = NULL;
1669 int ret = BLUETOOTH_ERROR_NONE;
1673 BT_CHECK_PARAMETER(local_version, return);
1675 GError *error = NULL;
1677 proxy = _bt_get_adapter_properties_proxy();
1678 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1680 result = g_dbus_proxy_call_sync(proxy,
1682 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1684 G_DBUS_CALL_FLAGS_NONE,
1690 if (error != NULL) {
1691 BT_ERR("Failed to get local version (Error: %s)", error->message);
1692 g_clear_error(&error);
1694 BT_ERR("Failed to get local version");
1695 return BLUETOOTH_ERROR_INTERNAL;
1698 g_variant_get(result, "(v)", &temp);
1699 ver = g_variant_get_string(temp, NULL);
1700 BT_DBG("VERSION: %s", ver);
1702 if (ver && (strlen(ver) > 0)) {
1703 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1704 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1707 g_strlcpy(local_version->version, ver,
1708 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1711 ret = BLUETOOTH_ERROR_INTERNAL;
1714 g_variant_unref(result);
1715 g_variant_unref(temp);
1719 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1722 const char *name = NULL;
1724 int ret = BLUETOOTH_ERROR_NONE;
1727 GError *error = NULL;
1729 BT_CHECK_PARAMETER(local_name, return);
1731 proxy = _bt_get_adapter_properties_proxy();
1732 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1734 result = g_dbus_proxy_call_sync(proxy,
1736 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1738 G_DBUS_CALL_FLAGS_NONE,
1744 if (error != NULL) {
1745 BT_ERR("Failed to get local name (Error: %s)", error->message);
1746 g_clear_error(&error);
1748 BT_ERR("Failed to get local name");
1749 return BLUETOOTH_ERROR_INTERNAL;
1752 g_variant_get(result, "(v)", &temp);
1753 name = g_variant_get_string(temp, NULL);
1754 BT_DBG("LOCAL NAME:%s", name);
1756 if (name && (strlen(name) > 0)) {
1757 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1758 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1761 g_strlcpy(local_name->name, name,
1762 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1764 ret = BLUETOOTH_ERROR_INTERNAL;
1766 g_variant_unref(result);
1767 g_variant_unref(temp);
1771 int _bt_set_local_name(char *local_name)
1774 GError *error = NULL;
1778 BT_CHECK_PARAMETER(local_name, return);
1780 proxy = _bt_get_adapter_properties_proxy();
1782 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1784 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1787 result = g_dbus_proxy_call_sync(proxy,
1789 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1790 "Alias", g_variant_new("s", local_name)),
1791 G_DBUS_CALL_FLAGS_NONE,
1797 if (error != NULL) {
1798 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1799 g_clear_error(&error);
1801 BT_ERR("Failed to set Alias");
1802 return BLUETOOTH_ERROR_INTERNAL;
1805 g_variant_unref(result);
1806 return BLUETOOTH_ERROR_NONE;
1809 int _bt_is_service_used(char *service_uuid, gboolean *used)
1812 GError *error = NULL;
1813 int ret = BLUETOOTH_ERROR_NONE;
1815 GVariant *temp = NULL;
1816 GVariantIter *iter = NULL;
1820 BT_CHECK_PARAMETER(service_uuid, return);
1821 BT_CHECK_PARAMETER(used, return);
1823 proxy = _bt_get_adapter_properties_proxy();
1824 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1826 result = g_dbus_proxy_call_sync(proxy,
1828 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1830 G_DBUS_CALL_FLAGS_NONE,
1836 if (error != NULL) {
1837 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1838 g_clear_error(&error);
1840 BT_ERR("Failed to get UUIDs");
1841 return BLUETOOTH_ERROR_INTERNAL;
1844 g_variant_get(result, "(v)", &temp);
1845 g_variant_get(temp, "as", &iter);
1848 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1849 if (strcasecmp(uuid, service_uuid) == 0) {
1854 g_variant_iter_free(iter);
1855 g_variant_unref(result);
1857 BT_DBG("Service Used? %d", *used);
1862 static gboolean __bt_get_discoverable_property(void)
1865 gboolean discoverable_v;
1866 GError *error = NULL;
1870 proxy = _bt_get_adapter_properties_proxy();
1871 retv_if(proxy == NULL, FALSE);
1873 result = g_dbus_proxy_call_sync(proxy,
1875 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1877 G_DBUS_CALL_FLAGS_NONE,
1883 if (error != NULL) {
1884 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1885 g_clear_error(&error);
1887 BT_ERR("Failed to get Discoverable property");
1888 return BLUETOOTH_ERROR_INTERNAL;
1891 g_variant_get(result, "(v)", &temp);
1892 discoverable_v = g_variant_get_boolean(temp);
1893 BT_DBG("discoverable_v:%d", discoverable_v);
1895 g_variant_unref(result);
1896 g_variant_unref(temp);
1898 return discoverable_v;
1901 int _bt_get_discoverable_mode(int *mode)
1903 gboolean discoverable;
1904 unsigned int timeout;
1906 BT_CHECK_PARAMETER(mode, return);
1908 discoverable = __bt_get_discoverable_property();
1909 timeout = _bt_get_discoverable_timeout_property();
1911 if (discoverable == TRUE) {
1913 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1915 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1917 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1919 return BLUETOOTH_ERROR_NONE;
1923 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1925 int ret = BLUETOOTH_ERROR_NONE;
1928 GError *error = NULL;
1932 proxy = _bt_get_adapter_properties_proxy();
1934 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1936 #ifdef TIZEN_DPM_ENABLE
1937 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1938 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1939 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1940 return BLUETOOTH_ERROR_ACCESS_DENIED;
1942 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1943 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1944 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1945 return BLUETOOTH_ERROR_ACCESS_DENIED;
1949 switch (discoverable_mode) {
1950 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1955 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1960 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1965 return BLUETOOTH_ERROR_INVALID_PARAM;
1968 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1969 discoverable_mode, timeout);
1971 result = g_dbus_proxy_call_sync(proxy,
1973 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1974 "Connectable", g_variant_new("b", pg_scan)),
1975 G_DBUS_CALL_FLAGS_NONE,
1981 if (error != NULL) {
1982 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1983 g_clear_error(&error);
1985 BT_ERR("Failed to set connectable property");
1986 return BLUETOOTH_ERROR_INTERNAL;
1988 g_variant_unref(result);
1989 result = g_dbus_proxy_call_sync(proxy,
1991 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
1992 g_variant_new("b", inq_scan)),
1993 G_DBUS_CALL_FLAGS_NONE,
1999 if (error != NULL) {
2000 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
2001 g_clear_error(&error);
2003 BT_ERR("Failed to set Discoverable property");
2004 return BLUETOOTH_ERROR_INTERNAL;
2006 g_variant_unref(result);
2007 result = g_dbus_proxy_call_sync(proxy,
2009 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
2010 "DiscoverableTimeout", g_variant_new("u", timeout)),
2011 G_DBUS_CALL_FLAGS_NONE,
2017 if (error != NULL) {
2018 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
2019 g_clear_error(&error);
2021 BT_ERR("Failed to set DiscoverableTimeout property");
2022 return BLUETOOTH_ERROR_INTERNAL;
2025 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
2028 ret = __bt_set_visible_time(timeout);
2030 g_variant_unref(result);
2035 int _bt_start_discovery(void)
2038 GError *error = NULL;
2041 if (_bt_is_discovering() == TRUE) {
2042 BT_ERR("BT is already in discovering");
2043 return BLUETOOTH_ERROR_IN_PROGRESS;
2044 } else if (_bt_is_device_creating() == TRUE) {
2045 BT_ERR("Bonding device is going on");
2046 return BLUETOOTH_ERROR_DEVICE_BUSY;
2049 proxy = _bt_get_adapter_proxy();
2050 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2052 result = g_dbus_proxy_call_sync(proxy,
2055 G_DBUS_CALL_FLAGS_NONE,
2061 if (error != NULL) {
2062 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
2063 g_clear_error(&error);
2065 BT_ERR("StartDiscovery failed");
2066 return BLUETOOTH_ERROR_INTERNAL;
2069 is_discovering = TRUE;
2070 cancel_by_user = FALSE;
2071 /* discovery status will be change in event */
2072 g_variant_unref(result);
2073 return BLUETOOTH_ERROR_NONE;
2076 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2080 GError *error = NULL;
2081 const gchar *disc_type;
2083 if (_bt_is_discovering() == TRUE) {
2084 BT_ERR("BT is already in discovering");
2085 return BLUETOOTH_ERROR_IN_PROGRESS;
2088 proxy = _bt_get_adapter_proxy();
2089 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2091 if (role == DISCOVERY_ROLE_BREDR)
2092 disc_type = "BREDR";
2093 else if (role == DISCOVERY_ROLE_LE)
2095 else if (role == DISCOVERY_ROLE_LE_BREDR)
2096 disc_type = "LE_BREDR";
2098 return BLUETOOTH_ERROR_INVALID_PARAM;
2100 result = g_dbus_proxy_call_sync(proxy,
2101 "StartCustomDiscovery",
2102 g_variant_new("s", disc_type),
2103 G_DBUS_CALL_FLAGS_NONE,
2109 if (error != NULL) {
2110 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2111 g_clear_error(&error);
2113 BT_ERR("StartCustomDiscovery failed");
2114 return BLUETOOTH_ERROR_INTERNAL;
2117 is_discovering = TRUE;
2118 cancel_by_user = FALSE;
2119 /* discovery status will be change in event */
2120 g_variant_unref(result);
2121 return BLUETOOTH_ERROR_NONE;
2124 int _bt_cancel_discovery(void)
2127 GError *error = NULL;
2130 if (_bt_is_discovering() == FALSE) {
2131 BT_ERR("BT is not in discovering");
2132 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2135 proxy = _bt_get_adapter_proxy();
2136 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2138 result = g_dbus_proxy_call_sync(proxy,
2141 G_DBUS_CALL_FLAGS_NONE,
2147 int ret = BLUETOOTH_ERROR_INTERNAL;
2148 if (error != NULL) {
2149 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2151 if (g_strrstr(error->message, "No discovery started"))
2152 ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
2154 g_clear_error(&error);
2156 BT_ERR("StopDiscovery failed");
2162 cancel_by_user = TRUE;
2163 /* discovery status will be change in event */
2164 g_variant_unref(result);
2165 return BLUETOOTH_ERROR_NONE;
2168 gboolean _bt_is_discovering(void)
2170 return is_discovering;
2173 gboolean _bt_is_connectable(void)
2176 GError *error = NULL;
2177 gboolean is_connectable = FALSE;
2181 proxy = _bt_get_adapter_properties_proxy();
2182 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2184 result = g_dbus_proxy_call_sync(proxy,
2186 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2188 G_DBUS_CALL_FLAGS_NONE,
2194 if (error != NULL) {
2195 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2196 g_clear_error(&error);
2198 BT_ERR("Failed to get connectable property");
2199 return BLUETOOTH_ERROR_INTERNAL;
2202 g_variant_get(result, "(v)", &temp);
2203 is_connectable = g_variant_get_boolean(temp);
2204 BT_DBG("discoverable_v:%d", is_connectable);
2206 g_variant_unref(result);
2207 g_variant_unref(temp);
2209 BT_INFO("Get connectable [%d]", is_connectable);
2210 return is_connectable;
2213 int _bt_set_connectable(gboolean is_connectable)
2216 GError *error = NULL;
2219 if (__bt_is_factory_test_mode()) {
2220 BT_ERR("Unable to set connectable in factory binary !!");
2221 return BLUETOOTH_ERROR_NOT_SUPPORT;
2224 proxy = _bt_get_adapter_properties_proxy();
2226 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2228 result = g_dbus_proxy_call_sync(proxy,
2230 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2231 g_variant_new("b", is_connectable)),
2232 G_DBUS_CALL_FLAGS_NONE,
2238 if (error != NULL) {
2239 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2240 g_clear_error(&error);
2242 BT_ERR("Failed to set connectable property");
2243 return BLUETOOTH_ERROR_INTERNAL;
2246 BT_INFO_C("### Set connectable [%d]", is_connectable);
2247 g_variant_unref(result);
2248 return BLUETOOTH_ERROR_NONE;
2251 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2254 gboolean discovering_v;
2255 GError *error = NULL;
2256 char *discovering_type = NULL;
2260 proxy = _bt_get_adapter_properties_proxy();
2261 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2263 if (discovery_type == DISCOVERY_ROLE_BREDR)
2264 discovering_type = "Discovering";
2265 else if (discovery_type == DISCOVERY_ROLE_LE)
2266 discovering_type = "LEDiscovering";
2268 result = g_dbus_proxy_call_sync(proxy,
2270 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2272 G_DBUS_CALL_FLAGS_NONE,
2278 if (error != NULL) {
2279 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2280 g_clear_error(&error);
2282 BT_ERR("Failed to get discovering property");
2283 return BLUETOOTH_ERROR_INTERNAL;
2286 g_variant_get(result, "(v)", &temp);
2287 discovering_v = g_variant_get_boolean(temp);
2288 BT_DBG("discoverable_v:%d", discovering_v);
2290 g_variant_unref(result);
2291 g_variant_unref(temp);
2293 return discovering_v;
2296 unsigned int _bt_get_discoverable_timeout_property(void)
2299 unsigned int timeout_v;
2300 GError *error = NULL;
2304 proxy = _bt_get_adapter_properties_proxy();
2305 retv_if(proxy == NULL, 0);
2307 result = g_dbus_proxy_call_sync(proxy,
2309 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2310 "DiscoverableTimeout"),
2311 G_DBUS_CALL_FLAGS_NONE,
2317 BT_ERR("Fail to get discoverable timeout");
2318 if (error != NULL) {
2319 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2320 g_clear_error(&error);
2325 g_variant_get(result, "(v)", &temp);
2326 timeout_v = g_variant_get_uint32(temp);
2327 BT_DBG("discoverable_v:%d", timeout_v);
2329 g_variant_unref(result);
2330 g_variant_unref(temp);
2335 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2337 bluetooth_device_info_t *dev_info;
2340 GByteArray *manufacturer_data = NULL;
2342 GVariantIter *char_value_iter;
2344 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2346 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2351 if (!g_strcmp0(key, "Address")) {
2352 const char *address = NULL;
2353 address = g_variant_get_string(value, NULL);
2354 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2356 } else if (!g_strcmp0(key, "Class")) {
2358 cod = g_variant_get_uint32(value);
2359 _bt_divide_device_class(&dev_info->device_class, cod);
2360 } else if (!g_strcmp0(key, "Name")) {
2361 const char *name = NULL;
2362 name = g_variant_get_string(value, NULL);
2363 /* If there is no Alias */
2364 if (strlen(dev_info->device_name.name) == 0) {
2365 g_strlcpy(dev_info->device_name.name, name,
2366 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2368 } else if (!g_strcmp0(key, "Alias")) {
2369 const char *alias = NULL;
2370 alias = g_variant_get_string(value, NULL);
2371 /* Overwrite the name */
2373 memset(dev_info->device_name.name, 0x00,
2374 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2375 g_strlcpy(dev_info->device_name.name, alias,
2376 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2378 } else if (!g_strcmp0(key, "IsAliasSet")) {
2379 dev_info->is_alias_set = g_variant_get_boolean(value);
2380 } else if (!g_strcmp0(key, "Connected")) {
2381 dev_info->connected = g_variant_get_byte(value);
2382 } else if (!g_strcmp0(key, "Paired")) {
2383 dev_info->paired = g_variant_get_boolean(value);
2384 } else if (!g_strcmp0(key, "Trusted")) {
2385 dev_info->trust = g_variant_get_boolean(value);
2386 } else if (!g_strcmp0(key, "RSSI")) {
2387 dev_info->rssi = g_variant_get_int16(value);
2388 } else if (!g_strcmp0(key, "UUIDs")) {
2394 dev_info->service_index = 0;
2395 g_variant_get(value, "as", &iter);
2396 while (g_variant_iter_loop(iter, "s", &uuid)) {
2397 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2398 parts = g_strsplit(uuid, "-", -1);
2400 if (parts == NULL || parts[0] == NULL) {
2405 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2410 dev_info->service_index = i;
2411 g_variant_iter_free(iter);
2412 } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
2413 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2414 } else if (strcasecmp(key, "LegacyManufacturerData") == 0) {
2415 manufacturer_data = g_byte_array_new();
2416 g_variant_get(value, "ay", &char_value_iter);
2417 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
2418 g_byte_array_append(manufacturer_data, &char_value, 1);
2420 if (manufacturer_data) {
2421 if (manufacturer_data->len > 0)
2422 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2424 g_variant_iter_free(char_value_iter);
2425 g_byte_array_free(manufacturer_data, TRUE);
2432 static void __bt_extract_device_info(GVariantIter *iter,
2435 bluetooth_device_info_t *dev_info = NULL;
2436 char *object_path = NULL;
2437 GVariantIter *interface_iter;
2438 GVariantIter *svc_iter;
2439 char *interface_str = NULL;
2441 /* Parse the signature: oa{sa{sv}}} */
2442 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2445 if (object_path == NULL)
2448 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2449 &interface_str, &svc_iter)) {
2450 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2451 BT_DBG("Found a device: %s", object_path);
2452 dev_info = __bt_parse_device_info(svc_iter);
2454 if (dev_info->paired == TRUE) {
2455 g_array_append_vals(*dev_list, dev_info,
2456 sizeof(bluetooth_device_info_t));
2460 g_free(interface_str);
2461 g_variant_iter_free(svc_iter);
2469 int _bt_get_bonded_devices(GArray **dev_list)
2472 GDBusConnection *conn;
2473 GDBusProxy *manager_proxy;
2474 GVariant *result = NULL;
2475 GVariantIter *iter = NULL;
2476 GError *error = NULL;
2478 conn = _bt_gdbus_get_system_gconn();
2479 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2481 manager_proxy = _bt_get_manager_proxy();
2482 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2484 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2486 G_DBUS_CALL_FLAGS_NONE,
2492 if (error != NULL) {
2493 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2494 g_clear_error(&error);
2496 BT_ERR("Failed to Failed to GetManagedObjects");
2497 return BLUETOOTH_ERROR_INTERNAL;
2500 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2501 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2503 __bt_extract_device_info(iter, dev_list);
2504 g_variant_iter_free(iter);
2505 g_variant_unref(result);
2508 return BLUETOOTH_ERROR_NONE;
2511 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2514 GDBusConnection *conn;
2515 GDBusProxy *manager_proxy;
2516 GVariant *result = NULL;
2517 GVariant *result1 = NULL;
2518 GVariantIter *iter = NULL;
2519 GError *error = NULL;
2520 char *object_path = NULL;
2521 GVariantIter *interface_iter;
2522 char *interface_str = NULL;
2523 GDBusProxy *device_proxy = NULL;
2524 gboolean is_connected = FALSE;
2526 conn = _bt_gdbus_get_system_gconn();
2527 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2529 manager_proxy = _bt_get_manager_proxy();
2530 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2532 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2534 G_DBUS_CALL_FLAGS_NONE,
2540 if (error != NULL) {
2541 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2542 g_clear_error(&error);
2545 BT_ERR("Failed to Failed to GetManagedObjects");
2546 return BLUETOOTH_ERROR_INTERNAL;
2549 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2550 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2552 /* Parse the signature: oa{sa{sv}}} */
2553 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2554 if (object_path == NULL)
2557 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2558 &interface_str, NULL)) {
2559 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2560 BT_DBG("Found a device: %s", object_path);
2561 g_free(interface_str);
2563 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2564 NULL, BT_BLUEZ_NAME,
2565 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2567 if (device_proxy == NULL) {
2568 BT_DBG("Device don't have this service");
2572 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2573 g_variant_new("(s)", profile_uuid),
2574 G_DBUS_CALL_FLAGS_NONE,
2579 if (result1 == NULL) {
2580 BT_ERR("Error occured in Proxy call");
2582 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2583 g_error_free(error);
2586 g_object_unref(device_proxy);
2589 g_variant_get(result1, "(b)", &is_connected);
2591 if (is_connected == TRUE) {
2592 char address[BT_ADDRESS_STRING_SIZE];
2593 bluetooth_device_address_t *addr = NULL;
2595 _bt_convert_device_path_to_address(object_path, address);
2597 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2598 _bt_convert_addr_string_to_type(addr->addr, address);
2600 g_array_append_vals(*addr_list, addr,
2601 sizeof(bluetooth_device_address_t));
2604 g_variant_unref(result1);
2605 g_object_unref(device_proxy);
2612 g_variant_unref(result);
2613 g_variant_iter_free(iter);
2616 return BLUETOOTH_ERROR_NONE;
2619 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2620 bluetooth_device_info_t *dev_info)
2622 char *object_path = NULL;
2623 GDBusProxy *adapter_proxy;
2624 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2625 int ret = BLUETOOTH_ERROR_NONE;
2627 BT_CHECK_PARAMETER(device_address, return);
2628 BT_CHECK_PARAMETER(dev_info, return);
2630 adapter_proxy = _bt_get_adapter_proxy();
2631 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2633 _bt_convert_addr_type_to_string(address, device_address->addr);
2635 object_path = _bt_get_device_object_path(address);
2637 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2639 ret = __bt_get_bonded_device_info(object_path, dev_info);
2640 g_free(object_path);
2645 int _bt_is_alias_set(bluetooth_device_address_t *device_address, gboolean *is_alias_set)
2647 char *object_path = NULL;
2648 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2649 gboolean alias_set = FALSE;
2651 GDBusConnection *conn;
2652 GDBusProxy *device_proxy;
2653 GError *error = NULL;
2654 GVariant *result = NULL;
2655 GVariant *temp = NULL;
2658 BT_CHECK_PARAMETER(device_address, return);
2659 BT_CHECK_PARAMETER(is_alias_set, return);
2661 _bt_convert_addr_type_to_string(address, device_address->addr);
2663 object_path = _bt_get_device_object_path(address);
2664 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2666 conn = _bt_gdbus_get_system_gconn();
2668 g_free(object_path);
2669 return BLUETOOTH_ERROR_INTERNAL;
2672 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2676 BT_PROPERTIES_INTERFACE,
2678 if (device_proxy == NULL) {
2679 g_free(object_path);
2680 return BLUETOOTH_ERROR_INTERNAL;
2683 result = g_dbus_proxy_call_sync(device_proxy, "Get",
2684 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "IsAliasSet"),
2685 G_DBUS_CALL_FLAGS_NONE,
2691 BT_ERR("Error occured in Proxy call");
2692 if (error != NULL) {
2693 BT_ERR("Getting is_alias_set property failed: [%s]\n", error->message);
2694 g_error_free(error);
2696 g_object_unref(device_proxy);
2697 g_free(object_path);
2698 return BLUETOOTH_ERROR_INTERNAL;
2701 g_variant_get(result, "(v)", &temp);
2702 alias_set = g_variant_get_boolean(temp);
2703 *is_alias_set = alias_set;
2704 BT_DBG("address: [%s] | *is_alias_set: %s", address, *is_alias_set ? "TRUE" : "FALSE");
2705 g_variant_unref(temp);
2706 g_variant_unref(result);
2707 g_object_unref(device_proxy);
2709 g_free(object_path);
2711 return BLUETOOTH_ERROR_NONE;
2714 int _bt_get_timeout_value(int *timeout)
2716 time_t current_time;
2719 /* Take current time */
2720 time(¤t_time);
2721 time_diff = difftime(current_time, visible_timer.start_time);
2723 BT_DBG("Time diff = %d\n", time_diff);
2725 *timeout = visible_timer.timeout - time_diff;
2727 return BLUETOOTH_ERROR_NONE;
2730 int _bt_set_le_privacy(gboolean set_privacy)
2733 GError *error = NULL;
2734 GVariant *result = NULL;
2736 if (__bt_is_factory_test_mode()) {
2737 BT_ERR("Unable to set le privacy in factory binary !!");
2738 return BLUETOOTH_ERROR_NOT_SUPPORT;
2741 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2742 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2743 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2746 proxy = _bt_get_adapter_proxy();
2747 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2749 result = g_dbus_proxy_call_sync(proxy,
2751 g_variant_new("(b)", set_privacy),
2752 G_DBUS_CALL_FLAGS_NONE,
2758 if (error != NULL) {
2759 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2760 g_clear_error(&error);
2762 BT_ERR("Failed to SetLePrivacy");
2763 return BLUETOOTH_ERROR_INTERNAL;
2766 g_variant_unref(result);
2767 BT_INFO("SetLePrivacy as %d", set_privacy);
2768 return BLUETOOTH_ERROR_NONE;
2771 int _bt_set_le_static_random_address(gboolean is_enable)
2774 GError *error = NULL;
2775 GVariant *result = NULL;
2777 if (__bt_is_factory_test_mode()) {
2778 BT_ERR("Unable to set le random address in factory binary !!");
2779 return BLUETOOTH_ERROR_NOT_SUPPORT;
2782 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2783 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2784 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2787 proxy = _bt_get_adapter_proxy();
2788 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2790 result = g_dbus_proxy_call_sync(proxy,
2791 "SetLeStaticRandomAddress",
2792 g_variant_new("(b)", is_enable),
2793 G_DBUS_CALL_FLAGS_NONE,
2799 if (error != NULL) {
2800 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2801 g_clear_error(&error);
2803 BT_ERR("Failed to SetLeStaticRandomAddress");
2804 return BLUETOOTH_ERROR_INTERNAL;
2807 g_variant_unref(result);
2808 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2809 return BLUETOOTH_ERROR_NONE;
2812 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2815 GError *error = NULL;
2819 GVariantBuilder *builder;
2821 BT_CHECK_PARAMETER(m_data, return);
2823 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2824 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2825 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2828 proxy = _bt_get_adapter_proxy();
2829 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2831 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2833 for (i = 0; i < (m_data->data_len) + 2; i++)
2834 g_variant_builder_add(builder, "y", m_data->data[i]);
2836 val = g_variant_new("(ay)", builder);
2838 result = g_dbus_proxy_call_sync(proxy,
2839 "SetManufacturerData",
2841 G_DBUS_CALL_FLAGS_NONE,
2845 g_variant_builder_unref(builder);
2847 if (error != NULL) {
2848 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2849 g_clear_error(&error);
2851 BT_ERR("Failed to SetManufacturerData");
2853 return BLUETOOTH_ERROR_INTERNAL;
2855 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2857 for (i = 0; i < (m_data->data_len) + 2; i++)
2858 g_variant_builder_add(builder, "y", m_data->data[i]);
2860 val = g_variant_new("(ay)", builder);
2862 _bt_send_event(BT_ADAPTER_EVENT,
2863 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2866 BT_INFO("Set manufacturer data");
2868 g_variant_builder_unref(builder);
2869 g_variant_unref(result);
2871 return BLUETOOTH_ERROR_NONE;
2875 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2877 int result = BLUETOOTH_ERROR_NONE;
2878 bt_service_alarm_t *alarm = NULL;
2880 if (!call_back || !alarm_id)
2881 return BLUETOOTH_ERROR_INVALID_PARAM;
2883 if (!alarm_mgr.is_alarm_initialized) {
2884 result = alarmmgr_init("bt-service");
2886 BT_ERR("Failed to initialize alarm = %d", result);
2887 result = BLUETOOTH_ERROR_INTERNAL;
2890 result = alarmmgr_set_cb(alarm_cb, NULL);
2892 BT_ERR("Failed to set the callback = %d", result);
2893 result = BLUETOOTH_ERROR_INTERNAL;
2896 alarm_mgr.is_alarm_initialized = TRUE;
2899 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2901 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2903 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2906 BT_ERR("Failed to create alarm error = %d", result);
2907 result = BLUETOOTH_ERROR_INTERNAL;
2911 alarm->alarm_id = *alarm_id;
2912 alarm->callback = call_back;
2913 alarm->user_data = user_data;
2915 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2916 result = BLUETOOTH_ERROR_NONE;
2921 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2924 bt_service_alarm_t *p_data;
2925 bt_set_alarm_cb callback = NULL;
2926 void *user_data = NULL;
2928 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2929 GINT_TO_POINTER(alarm_id), compare_alarm);
2933 p_data = (bt_service_alarm_t *)node->data;
2934 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2940 callback = p_data->callback;
2941 user_data = p_data->user_data;
2945 callback(alarm_id, user_data);
2950 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2953 bt_service_alarm_t *p_data;
2954 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2957 alarmmgr_remove_alarm(alarm_id);
2958 p_data = (bt_service_alarm_t *)list->data;
2959 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2966 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2968 alarm_id_t alarm_id = (alarm_id_t)data;
2969 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2971 if (p_data->alarm_id == alarm_id)
2977 static void alarm_data_free(void *data)
2979 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2984 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2987 int function = (int)user_data;
2990 case BT_ENABLE_ADAPTER:
2991 result = _bt_enable_adapter();
2992 if (result != BLUETOOTH_ERROR_NONE) {
2993 BT_ERR("_bt_enable_adapter is failed");
2994 /* Send enabled event to API */
2995 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
2996 g_variant_new("(i)", result));
2999 case BT_DISABLE_ADAPTER:
3000 result = _bt_disable_adapter();
3001 if (result != BLUETOOTH_ERROR_NONE) {
3002 BT_ERR("_bt_disable_adapter is failed");
3003 /* Send disabled event to API */
3004 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
3005 g_variant_new("(i)", result));
3009 BT_ERR("function is NOT matched");
3016 int _bt_adapter_request_delayed(int function)
3021 case BT_ENABLE_ADAPTER:
3022 ret = _bt_enable_adapter_check_status();
3023 if (ret == BLUETOOTH_ERROR_NONE)
3024 _bt_adapter_set_status(BT_ACTIVATING);
3029 case BT_DISABLE_ADAPTER:
3030 ret = _bt_disable_adapter_check_status();
3031 if (ret == BLUETOOTH_ERROR_NONE)
3032 _bt_adapter_set_status(BT_DEACTIVATING);
3038 BT_ERR("function is NOT matched");
3039 return BLUETOOTH_ERROR_INTERNAL;
3042 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
3044 return BLUETOOTH_ERROR_NONE;
3048 int _bt_get_enable_timer_id(void)