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 #include "bt-service-adapter-le.h"
48 #ifdef TIZEN_DPM_ENABLE
49 #include "bt-service-dpm.h"
59 bt_adapter_timer_t visible_timer = {0, };
63 bt_set_alarm_cb callback;
68 gboolean is_alarm_initialized;
70 } bt_service_alarm_mgr_t;
72 static bt_service_alarm_mgr_t alarm_mgr = {0, };
74 static gboolean is_discovering;
75 static gboolean cancel_by_user;
76 static bt_status_t adapter_status = BT_DEACTIVATED;
77 static bt_le_status_t adapter_le_status = BT_LE_DEACTIVATED;
78 static gboolean is_le_intended = FALSE;
79 static void *adapter_agent = NULL;
80 static GDBusProxy *core_proxy = NULL;
81 static guint timer_id = 0;
82 static guint le_timer_id = 0;
83 static gboolean is_recovery_mode;
85 static uint status_reg_id;
87 #define BT_CORE_NAME "org.projectx.bt_core"
88 #define BT_CORE_PATH "/org/projectx/bt_core"
89 #define BT_CORE_INTERFACE "org.projectx.btcore"
91 #define BT_DISABLE_TIME 500 /* 500 ms */
93 static int alarm_cb(alarm_id_t alarm_id, void* user_param);
94 static void alarm_data_free(void *data);
96 GDBusProxy *_bt_init_core_proxy(void)
99 GDBusConnection *conn;
101 conn = _bt_gdbus_get_system_gconn();
105 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
120 static GDBusProxy *__bt_get_core_proxy(void)
122 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
125 static gboolean __bt_is_factory_test_mode(void)
129 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
130 BT_ERR("Get the DUT Mode fail");
135 BT_INFO("DUT Test Mode !!");
142 static gboolean __bt_timeout_handler(gpointer user_data)
144 int result = BLUETOOTH_ERROR_NONE;
148 /* Take current time */
150 time_diff = difftime(current_time, visible_timer.start_time);
152 /* Send event to application */
153 _bt_send_event(BT_ADAPTER_EVENT,
154 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
155 g_variant_new("(in)", result, time_diff));
157 if (visible_timer.timeout <= time_diff) {
158 g_source_remove(visible_timer.event_id);
159 visible_timer.event_id = 0;
160 visible_timer.timeout = 0;
162 #ifndef TIZEN_PROFILE_WEARABLE
163 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
164 BT_ERR("Set vconf failed\n");
172 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
175 int result = BLUETOOTH_ERROR_NONE;
178 if (alarm_id != visible_timer.alarm_id)
181 if (visible_timer.event_id) {
182 _bt_send_event(BT_ADAPTER_EVENT,
183 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
184 g_variant_new("(in)", result, timeout));
185 g_source_remove(visible_timer.event_id);
186 visible_timer.event_id = 0;
187 visible_timer.timeout = 0;
189 #ifndef TIZEN_PROFILE_WEARABLE
190 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
191 BT_ERR("Set vconf failed\n");
194 /* Switch Off visibility in Bluez */
195 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
196 visible_timer.alarm_id = 0;
200 static void __bt_visibility_alarm_remove()
202 if (visible_timer.event_id > 0) {
203 g_source_remove(visible_timer.event_id);
204 visible_timer.event_id = 0;
207 if (visible_timer.alarm_id > 0) {
208 _bt_service_remove_alarm(visible_timer.alarm_id);
209 visible_timer.alarm_id = 0;
213 int __bt_set_visible_time(int timeout)
218 __bt_visibility_alarm_remove();
220 visible_timer.timeout = timeout;
222 #ifndef TIZEN_PROFILE_WEARABLE
223 #ifdef TIZEN_DPM_ENABLE
224 if (_bt_dpm_get_bluetooth_limited_discoverable_state() != DPM_RESTRICTED) {
226 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
227 BT_ERR("Set vconf failed");
228 #ifdef TIZEN_DPM_ENABLE
235 return BLUETOOTH_ERROR_NONE;
237 result = _bt_service_set_alarm(visible_timer.timeout,
238 __bt_visibility_alarm_cb, NULL, &alarm_id);
239 if (result != BLUETOOTH_ERROR_NONE)
240 return BLUETOOTH_ERROR_INTERNAL;
241 visible_timer.alarm_id = alarm_id;
242 /* Take start time */
243 time(&(visible_timer.start_time));
244 visible_timer.event_id = g_timeout_add_seconds(1,
245 __bt_timeout_handler, NULL);
247 return BLUETOOTH_ERROR_NONE;
250 static void __bt_get_service_list(GVariant *value, bluetooth_device_info_t *dev)
257 ret_if(value == NULL);
260 dev->service_index = 0;
262 g_variant_get(value, "as", &iter);
263 while (g_variant_iter_loop(iter, "s", &uuid)) {
264 g_strlcpy(dev->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
265 parts = g_strsplit(uuid, "-", -1);
267 if (parts == NULL || parts[0] == NULL) {
272 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
275 dev->service_index++;
278 g_variant_iter_free(iter);
281 static int __bt_get_bonded_device_info(gchar *device_path,
282 bluetooth_device_info_t *dev_info)
284 GError *error = NULL;
285 GDBusProxy *device_proxy;
286 gchar *address = NULL;
289 unsigned int cod = 0;
291 gboolean trust = FALSE;
292 gboolean paired = FALSE;
293 guchar connected = 0;
294 GByteArray *manufacturer_data = NULL;
296 GDBusConnection *conn;
298 GVariantIter *property_iter;
302 GVariantIter *char_value_iter;
304 BT_CHECK_PARAMETER(device_path, return);
305 BT_CHECK_PARAMETER(dev_info, return);
307 conn = _bt_gdbus_get_system_gconn();
308 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
310 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
314 BT_PROPERTIES_INTERFACE,
317 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
319 result = g_dbus_proxy_call_sync(device_proxy,
321 g_variant_new("(s)", BT_DEVICE_INTERFACE),
322 G_DBUS_CALL_FLAGS_NONE,
328 BT_ERR("Error occured in Proxy call");
330 BT_ERR("Error occured in Proxy call (Error: %s)", error->message);
331 g_clear_error(&error);
333 g_object_unref(device_proxy);
334 return BLUETOOTH_ERROR_INTERNAL;
337 g_object_unref(device_proxy);
339 g_variant_get(result, "(a{sv})", &property_iter);
341 while (g_variant_iter_loop(property_iter, "{sv}", &key, &value)) {
342 if (!g_strcmp0(key, "Paired")) {
343 paired = g_variant_get_boolean(value);
344 } else if (!g_strcmp0(key, "Address")) {
345 g_variant_get(value, "s", &address);
346 } else if (!g_strcmp0(key, "Alias")) {
347 g_variant_get(value, "s", &alias);
348 } else if (!g_strcmp0(key, "Name")) {
349 g_variant_get(value, "s", &name);
350 } else if (!g_strcmp0(key, "Class")) {
351 cod = g_variant_get_uint32(value);
352 } else if (!g_strcmp0(key, "Connected")) {
353 connected = g_variant_get_byte(value);
354 } else if (!g_strcmp0(key, "Trusted")) {
355 trust = g_variant_get_boolean(value);
356 } else if (!g_strcmp0(key, "RSSI")) {
357 rssi = g_variant_get_int16(value);
358 } else if (!g_strcmp0(key, "UUIDs")) {
359 __bt_get_service_list(value, dev_info);
360 } else if (!g_strcmp0(key, "LegacyManufacturerDataLen")) {
361 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
362 } else if (!g_strcmp0(key, "LegacyManufacturerData")) {
363 manufacturer_data = g_byte_array_new();
364 g_variant_get(value, "ay", &char_value_iter);
365 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
366 g_byte_array_append(manufacturer_data, &char_value, 1);
368 g_variant_iter_free(char_value_iter);
370 if (manufacturer_data) {
371 if (manufacturer_data->len > 0) {
372 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data,
373 manufacturer_data->len);
376 g_byte_array_free(manufacturer_data, TRUE);
379 g_variant_iter_free(property_iter);
381 BT_DBG("trust: %d, paired: %d", trust, paired);
383 g_variant_unref(result);
385 if ((paired == FALSE) && (trust == FALSE)) {
389 return BLUETOOTH_ERROR_NOT_PAIRED;
392 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
395 _bt_divide_device_class(&dev_info->device_class, cod);
397 g_strlcpy(dev_info->device_name.name, alias ? alias : name,
398 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
400 dev_info->rssi = rssi;
401 dev_info->trust = trust;
402 dev_info->paired = paired;
403 dev_info->connected = connected;
404 ret = BLUETOOTH_ERROR_NONE;
412 void _bt_set_discovery_status(gboolean mode)
414 is_discovering = mode;
417 void _bt_set_cancel_by_user(gboolean value)
419 cancel_by_user = value;
422 gboolean _bt_get_cancel_by_user(void)
424 return cancel_by_user;
427 void _bt_adapter_set_status(bt_status_t status)
429 BT_INFO("adapter_status changed [%d] -> [%d]", adapter_status, status);
430 adapter_status = status;
433 bt_status_t _bt_adapter_get_status(void)
435 return adapter_status;
438 void _bt_adapter_set_le_status(bt_le_status_t status)
440 BT_INFO("adapter_le_status changed [%d] -> [%d]", adapter_le_status, status);
441 adapter_le_status = status;
444 bt_le_status_t _bt_adapter_get_le_status(void)
446 return adapter_le_status;
450 void _bt_set_le_intended_status(gboolean value)
452 is_le_intended = value;
455 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
457 char *phone_name = NULL;
463 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
464 phone_name = vconf_keynode_get_str(node);
466 if (phone_name && strlen(phone_name) != 0) {
467 if (!g_utf8_validate(phone_name, -1,
468 (const char **)&ptr))
471 BT_INFO("device_name is changed to %s", phone_name);
472 _bt_set_local_name(phone_name);
474 BT_ERR("phone_name is NOT valid");
477 BT_ERR("vconf type is NOT string");
481 static void __bt_set_local_name(void)
483 bluetooth_device_name_t local_name;
484 char *phone_name = NULL;
488 if (_bt_get_local_name(&local_name) != BLUETOOTH_ERROR_NONE ||
489 (temp = strstr(local_name.name, "BlueZ")) != NULL) {
490 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
495 if (strlen(phone_name) != 0) {
496 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
499 _bt_set_local_name(phone_name);
505 static int __bt_set_enabled(void)
507 int adapter_status = BT_ADAPTER_DISABLED;
508 int result = BLUETOOTH_ERROR_NONE;
511 BT_DBG("g_source is removed");
512 g_source_remove(timer_id);
516 _bt_check_adapter(&adapter_status);
518 if (adapter_status == BT_ADAPTER_DISABLED) {
519 BT_ERR("Bluetoothd is not running");
520 return BLUETOOTH_ERROR_INTERNAL;
523 #ifdef TIZEN_PROFILE_MOBILE || defined(TIZEN_PROFILE_IVI)
524 /* BT setting UI will control Mobile's visible mode. So in the FRWK...set the visible mode as off: */
525 if (_bt_set_discoverable_mode(
526 BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0) != BLUETOOTH_ERROR_NONE)
527 BT_ERR("Set connectable mode failed");
530 if (_bt_set_discoverable_mode(
531 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE, 0) != BLUETOOTH_ERROR_NONE)
532 BT_ERR("Fail to set discoverable mode");
536 /* Update Bluetooth Status to notify other modules */
537 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
538 BT_ERR("Set vconf failed\n");
540 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
541 BT_ERR("Set vconf failed\n");
543 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
544 EVT_VAL_BT_ON) != ES_R_OK)
545 BT_ERR("Fail to set value");
547 /* Send enabled event to API */
548 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
549 g_variant_new("(i)", result));
551 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
552 _bt_audio_start_auto_connect(FALSE);
555 __bt_set_local_name();
556 _bt_set_discovery_status(FALSE);
558 return BLUETOOTH_ERROR_NONE;
561 void _bt_set_disabled(int result)
563 int power_off_status = 0;
566 int pm_ignore_mode = 0;
568 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
569 BT_DBG("ret : %d, power_off_status : %d", ret, power_off_status);
571 ret_pm_ignore = vconf_get_int(VCONFKEY_PM_KEY_IGNORE, &pm_ignore_mode);
573 /* Update the vconf BT status in normal Deactivation case only */
574 if (ret == 0 && power_off_status == VCONFKEY_SYSMAN_POWER_OFF_NONE &&
575 ret_pm_ignore == 0 && pm_ignore_mode != VCONFKEY_PM_KEY_LOCK) {
577 BT_DBG("Update vconf for BT normal Deactivation");
579 if (result == BLUETOOTH_ERROR_TIMEOUT)
580 if (vconf_set_int(BT_OFF_DUE_TO_TIMEOUT, 1) != 0)
581 BT_ERR("Set vconf failed");
583 /* Update Bluetooth Status to notify other modules */
584 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
585 BT_ERR("Set vconf failed");
587 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_STATE,
588 EVT_VAL_BT_OFF) != ES_R_OK)
589 BT_ERR("Fail to set value");
592 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
593 BT_ERR("Set vconf failed\n");
595 _bt_cancel_queued_transfers();
596 _bt_adapter_set_status(BT_DEACTIVATED);
597 _bt_set_discovery_status(FALSE);
599 #ifndef USB_BLUETOOTH
600 if (_bt_adapter_get_le_status() != BT_LE_DEACTIVATED) {
602 /* Send disabled event */
603 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
604 g_variant_new("(i)", result));
605 #ifndef USB_BLUETOOTH
609 BT_INFO("Adapter disabled");
612 static int __bt_set_le_enabled(void)
615 int result = BLUETOOTH_ERROR_NONE;
618 /* Update Bluetooth Status to notify other modules */
619 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_ON) != 0)
620 BT_ERR("Set vconf failed");
622 /* set packet length to max size to enable packet length extension */
623 if (BLUETOOTH_ERROR_NONE != _bt_le_set_max_packet_len())
624 BT_ERR("Fail to set max packet length");
626 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
627 EVT_VAL_BT_LE_ON) != ES_R_OK)
628 BT_ERR("Fail to set value");
630 /* Send enabled event to API */
632 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
633 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
635 status = _bt_adapter_get_status();
636 if (status == BT_DEACTIVATED) {
637 BT_INFO("BREDR is off, turn off PSCAN");
638 _bt_set_connectable(FALSE);
640 if (le_timer_id > 0) {
641 g_source_remove(le_timer_id);
645 /* Send enabled event to API */
646 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_ENABLED,
647 g_variant_new("(i)", result));
649 __bt_set_local_name();
652 return BLUETOOTH_ERROR_NONE;
655 void _bt_set_le_disabled(int result)
657 int power_off_status;
660 ret = vconf_get_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, &power_off_status);
661 BT_DBG("ret : %d", ret);
662 BT_DBG("power_off_status : %d", power_off_status);
664 /* Update Bluetooth Status to notify other modules */
665 BT_DBG("Update vconf for BT LE normal Deactivation");
666 if (vconf_set_int(VCONFKEY_BT_LE_STATUS, VCONFKEY_BT_LE_STATUS_OFF) != 0)
667 BT_ERR("Set vconf failed\n");
668 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
670 if (_bt_eventsystem_set_value(SYS_EVENT_BT_STATE, EVT_KEY_BT_LE_STATE,
671 EVT_VAL_BT_LE_OFF) != ES_R_OK)
672 BT_ERR("Fail to set value");
674 /* Send disabled event */
675 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_LE_DISABLED,
676 g_variant_new("(i)", result));
679 void *_bt_get_adapter_agent(void)
681 return adapter_agent;
684 int _bt_enable_core(void)
688 GError *error = NULL;
690 proxy = __bt_get_core_proxy();
691 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
693 /* Clean up the process */
694 result = g_dbus_proxy_call_sync(proxy,
697 G_DBUS_CALL_FLAGS_NONE,
704 BT_ERR("Bt core call failed(Error: %s)", error->message);
705 g_clear_error(&error);
707 BT_ERR("Bt core call failed");
708 return BLUETOOTH_ERROR_INTERNAL;
711 g_variant_unref(result);
712 return BLUETOOTH_ERROR_NONE;
715 #if defined(TIZEN_FEATURE_FLIGHTMODE_ENABLED) || (!defined(TIZEN_PROFILE_WEARABLE))
716 static void __bt_service_flight_ps_mode_cb(keynode_t *node, void *data)
718 gboolean flight_mode = FALSE;
719 int power_saving_mode = 0;
722 DBG_SECURE("key=%s", vconf_keynode_get_name(node));
723 type = vconf_keynode_get_type(node);
724 if (type == VCONF_TYPE_BOOL) {
725 flight_mode = vconf_keynode_get_bool(node);
726 if (flight_mode != TRUE) {
727 BT_ERR("Ignore the event");
730 } else if (type == VCONF_TYPE_INT) {
731 power_saving_mode = vconf_keynode_get_int(node);
732 if (power_saving_mode != 2) {
733 BT_ERR("Ignore the event");
737 BT_ERR("Invaild vconf key type : %d", type);
745 void _bt_service_register_vconf_handler(void)
749 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
750 if (vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
751 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
752 BT_ERR("Unable to register key handler");
754 BT_DBG("Telephony is disabled");
757 #ifndef TIZEN_PROFILE_WEARABLE
758 if (vconf_notify_key_changed(VCONFKEY_SETAPPL_PSMODE,
759 (vconf_callback_fn)__bt_service_flight_ps_mode_cb, NULL) < 0)
760 BT_ERR("Unable to register key handler");
764 void _bt_service_unregister_vconf_handler(void)
768 #ifdef TIZEN_FEATURE_FLIGHTMODE_ENABLED
769 vconf_ignore_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
770 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
773 #ifndef TIZEN_PROFILE_WEARABLE
774 vconf_ignore_key_changed(VCONFKEY_SETAPPL_PSMODE,
775 (vconf_callback_fn)__bt_service_flight_ps_mode_cb);
779 static void __bt_state_event_handler(const char *event_name, bundle *data, void *user_data)
781 const char *bt_status = NULL;
782 const char *bt_le_status = NULL;
783 BT_DBG("bt state set event(%s) received", event_name);
785 bt_status = bundle_get_val(data, EVT_KEY_BT_STATE);
786 BT_DBG("bt_state: (%s)", bt_status);
788 bt_le_status = bundle_get_val(data, EVT_KEY_BT_LE_STATE);
789 BT_DBG("bt_state: (%s)", bt_le_status);
792 void _bt_handle_adapter_added(void)
796 bt_le_status_t le_status;
800 BT_DBG("g_source is removed");
801 g_source_remove(timer_id);
806 status = _bt_adapter_get_status();
807 le_status = _bt_adapter_get_le_status();
808 BT_INFO("status : %d", status);
809 BT_INFO("le_status : %d", le_status);
811 #ifndef USB_BLUETOOTH
812 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
813 if (!adapter_agent) {
814 BT_ERR("Fail to register agent");
818 if (adapter_agent == NULL) {
819 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
820 if (!adapter_agent) {
821 BT_ERR("Fail to register agent");
827 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
828 BT_ERR("Fail to register media player");
830 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
831 BT_ERR("Fail to init obex server");
833 #ifdef TIZEN_BT_PAN_NAP_ENABLED
834 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
835 BT_ERR("Fail to activate network");
838 /* add the vconf noti handler */
839 ret = vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
840 __bt_phone_name_changed_cb, NULL);
842 BT_ERR("Unable to register key handler");
844 if (le_status == BT_LE_ACTIVATING ||
845 status == BT_ACTIVATING) {
846 __bt_set_le_enabled();
847 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
850 if (status == BT_ACTIVATING) {
852 _bt_adapter_set_status(BT_ACTIVATED);
856 if (eventsystem_register_event(SYS_EVENT_BT_STATE, &status_reg_id,
857 (eventsystem_handler)__bt_state_event_handler, NULL) != ES_R_OK) {
858 BT_ERR("Fail to register system event");
862 void _bt_handle_adapter_removed(void)
866 _bt_adapter_set_status(BT_DEACTIVATED);
868 __bt_visibility_alarm_remove();
870 if (alarm_mgr.is_alarm_initialized == TRUE) {
872 alarm_mgr.is_alarm_initialized = FALSE;
873 g_list_free_full(alarm_mgr.g_alarm_list, alarm_data_free);
874 alarm_mgr.g_alarm_list = NULL;
877 #ifdef TIZEN_BT_A2DP_SINK_AUTO_CONNECT
878 _bt_audio_stop_auto_connect();
881 ret = vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
882 (vconf_callback_fn)__bt_phone_name_changed_cb);
884 ERR("vconf_ignore_key_changed failed\n");
886 #ifndef USB_BLUETOOTH
887 _bt_destroy_agent(adapter_agent);
888 adapter_agent = NULL;
890 if (is_recovery_mode == TRUE) {
891 /* Send disabled event */
892 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
894 /* Will recover BT by bt-core, so set the mode as activating */
895 _bt_adapter_set_status(BT_ACTIVATING);
896 is_recovery_mode = FALSE;
898 _bt_reliable_terminate_service(NULL);
901 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
904 if (eventsystem_unregister_event(status_reg_id) != ES_R_OK)
905 BT_ERR("Fail to unregister system event");
908 static gboolean __bt_enable_timeout_cb(gpointer user_data)
912 GError *error = NULL;
916 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
918 BT_ERR("EnableAdapter is failed");
920 proxy = __bt_get_core_proxy();
924 /* Clean up the process */
925 result = g_dbus_proxy_call_sync(proxy,
928 G_DBUS_CALL_FLAGS_NONE,
935 BT_ERR("Bt core call failed(Error: %s)", error->message);
936 g_clear_error(&error);
938 BT_ERR("Bt core call failed");
943 g_variant_unref(result);
944 _bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
946 #ifndef USB_BLUETOOTH
947 _bt_terminate_service(NULL);
953 static gboolean __bt_enable_le_timeout_cb(gpointer user_data)
957 GError *error = NULL;
961 retv_if(_bt_adapter_get_le_status() == BT_LE_ACTIVATED, FALSE);
963 BT_ERR("EnableAdapterLE is failed");
965 proxy = __bt_get_core_proxy();
969 /* Clean up the process */
970 result = g_dbus_proxy_call_sync(proxy,
973 G_DBUS_CALL_FLAGS_NONE,
980 BT_ERR("Bt core call failed(Error: %s)", error->message);
981 g_clear_error(&error);
983 BT_ERR("Bt core call failed");
987 g_variant_unref(result);
988 _bt_adapter_set_le_status(BT_LE_DEACTIVATED);
990 _bt_set_le_disabled(BLUETOOTH_ERROR_TIMEOUT);
992 if (_bt_adapter_get_status() == BT_DEACTIVATED)
993 _bt_terminate_service(NULL);
998 void _bt_adapter_start_le_enable_timer(void)
1000 if (le_timer_id > 0) {
1001 g_source_remove(le_timer_id);
1005 le_timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1006 __bt_enable_le_timeout_cb, NULL);
1011 void _bt_adapter_start_enable_timer(void)
1014 g_source_remove(timer_id);
1018 timer_id = g_timeout_add(BT_ENABLE_TIMEOUT,
1019 __bt_enable_timeout_cb, NULL);
1025 static gboolean __bt_adapter_enabled_cb(gpointer user_data)
1030 _bt_adapter_set_status(BT_ACTIVATED);
1036 int _bt_enable_adapter_check_status(void)
1038 bt_status_t status = _bt_adapter_get_status();
1039 bt_le_status_t le_status = _bt_adapter_get_le_status();
1043 if (status == BT_ACTIVATING) {
1044 BT_ERR("Enabling in progress");
1045 return BLUETOOTH_ERROR_IN_PROGRESS;
1048 if (status == BT_ACTIVATED) {
1049 BT_ERR("Already enabled");
1050 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1053 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1054 BT_ERR("Disabling in progress");
1055 return BLUETOOTH_ERROR_DEVICE_BUSY;
1058 return BLUETOOTH_ERROR_NONE;
1061 int _bt_enable_adapter(void)
1064 GError *error = NULL;
1066 GVariant *result = NULL;
1067 bt_status_t status = _bt_adapter_get_status();
1068 bt_le_status_t le_status = _bt_adapter_get_le_status();
1072 if (status == BT_ACTIVATING) {
1073 BT_ERR("Enabling in progress");
1074 return BLUETOOTH_ERROR_IN_PROGRESS;
1077 if (status == BT_ACTIVATED) {
1078 BT_ERR("Already enabled");
1079 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1082 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1083 BT_ERR("Disabling in progress");
1084 return BLUETOOTH_ERROR_DEVICE_BUSY;
1087 _bt_adapter_set_status(BT_ACTIVATING);
1091 int adapter_status = BT_ADAPTER_DISABLED;
1093 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
1094 BT_ERR("Set vconf failed");
1096 _bt_check_adapter(&adapter_status);
1097 if (adapter_status == BT_ADAPTER_ENABLED) {
1098 g_idle_add(__bt_adapter_enabled_cb, NULL);
1099 _bt_adapter_start_enable_timer();
1100 return BLUETOOTH_ERROR_NONE;
1105 proxy = __bt_get_core_proxy();
1107 return BLUETOOTH_ERROR_INTERNAL;
1109 if (le_status == BT_LE_ACTIVATED) {
1110 BT_INFO("LE Already enabled. Just turn on PSCAN");
1111 ret = _bt_set_connectable(TRUE);
1112 if (ret == BLUETOOTH_ERROR_NONE)
1113 _bt_adapter_set_status(BT_ACTIVATED);
1115 return BLUETOOTH_ERROR_INTERNAL;
1118 result = g_dbus_proxy_call_sync(proxy, "EnableAdapter",
1120 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1123 BT_ERR("EnableAdapterLe failed: %s", error->message);
1124 _bt_adapter_set_status(BT_DEACTIVATED);
1125 g_clear_error(&error);
1127 result = g_dbus_proxy_call_sync(proxy,
1130 G_DBUS_CALL_FLAGS_NONE,
1135 if (error != NULL) {
1136 BT_ERR("Bt core call failed(Error: %s)", error->message);
1137 g_clear_error(&error);
1139 g_variant_unref(result);
1140 #ifndef USB_BLUETOOTH
1141 /* Terminate myself */
1142 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1144 return BLUETOOTH_ERROR_INTERNAL;
1146 g_variant_unref(result);
1147 if (le_status == BT_LE_ACTIVATED)
1150 _bt_adapter_start_enable_timer();
1152 return BLUETOOTH_ERROR_NONE;
1155 static gboolean __bt_disconnect_all(void)
1158 GDBusConnection *conn;
1159 GDBusProxy *dev_proxy;
1160 gboolean ret = FALSE;
1162 GError *error = NULL;
1163 GArray *device_list;
1164 bluetooth_device_info_t info;
1166 char *device_path = NULL;
1167 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1172 conn = _bt_gdbus_get_system_gconn();
1174 device_list = g_array_new(FALSE, FALSE, sizeof(gchar));
1176 if (_bt_get_bonded_devices(&device_list)
1177 != BLUETOOTH_ERROR_NONE) {
1178 g_array_free(device_list, TRUE);
1182 size = (device_list->len) / sizeof(bluetooth_device_info_t);
1184 for (i = 0; i < size; i++) {
1186 info = g_array_index(device_list,
1187 bluetooth_device_info_t, i);
1189 if (info.connected != BLUETOOTH_CONNECTED_LINK_NONE) {
1190 BT_DBG("Found Connected device");
1191 _bt_convert_addr_type_to_string(address, info.device_address.addr);
1192 device_path = _bt_get_device_object_path(address);
1193 if (device_path == NULL)
1196 BT_DBG("Disconnecting : %s", device_path);
1198 dev_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1202 BT_DEVICE_INTERFACE,
1205 if (dev_proxy == NULL)
1208 result = g_dbus_proxy_call_sync(dev_proxy,
1211 G_DBUS_CALL_FLAGS_NONE,
1217 if (error != NULL) {
1218 BT_ERR("Disconnect call failed(Error: %s)", error->message);
1219 g_clear_error(&error);
1221 BT_ERR("Disconnect call failed");
1222 g_object_unref(dev_proxy);
1226 g_variant_unref(result);
1227 g_object_unref(dev_proxy);
1231 g_array_free(device_list, TRUE);
1237 static gboolean __bt_set_disabled_timeout_cb(gpointer user_data)
1240 _bt_set_disabled(BLUETOOTH_ERROR_NONE);
1246 int _bt_disable_cb(void)
1254 GError *error = NULL;
1256 _bt_adapter_set_status(BT_DEACTIVATING);
1258 bt_le_status_t le_status;
1259 le_status = _bt_adapter_get_le_status();
1260 BT_DBG("le_status : %d", le_status);
1261 if (le_status == BT_LE_ACTIVATED) {
1262 BT_INFO("LE is enabled. Just turn off PSCAN");
1264 if (_bt_is_discovering())
1265 _bt_cancel_discovery();
1267 if (_bt_is_connectable() == FALSE) {
1268 g_timeout_add(100, (GSourceFunc)__bt_set_disabled_timeout_cb, NULL);
1270 ret = _bt_set_connectable(FALSE);
1271 if (ret != BLUETOOTH_ERROR_NONE) {
1272 BT_ERR("_bt_set_connectable fail!");
1273 _bt_adapter_set_status(BT_ACTIVATED);
1274 return BLUETOOTH_ERROR_INTERNAL;
1279 proxy = __bt_get_core_proxy();
1280 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1282 result = g_dbus_proxy_call_sync(proxy,
1285 G_DBUS_CALL_FLAGS_NONE,
1291 if (error != NULL) {
1292 BT_ERR("Failed to DisableAdapter (Error: %s)", error->message);
1293 g_clear_error(&error);
1295 BT_ERR("Failed to DisableAdapter");
1296 _bt_adapter_set_status(BT_ACTIVATED);
1297 return BLUETOOTH_ERROR_INTERNAL;
1300 g_variant_unref(result);
1301 return BLUETOOTH_ERROR_NONE;
1304 int _bt_disable_adapter_check_status(void)
1306 bt_status_t status = _bt_adapter_get_status();
1310 if (status == BT_DEACTIVATING) {
1311 BT_DBG("Disabling in progress");
1312 return BLUETOOTH_ERROR_IN_PROGRESS;
1315 if (status == BT_DEACTIVATED) {
1316 BT_DBG("Already disabled");
1317 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1320 return BLUETOOTH_ERROR_NONE;
1323 int _bt_disable_adapter(void)
1328 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1329 BT_DBG("Disabling in progress");
1330 return BLUETOOTH_ERROR_IN_PROGRESS;
1333 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1334 BT_DBG("Already disabled");
1335 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1339 g_source_remove(timer_id);
1343 __bt_disconnect_all();
1344 ret = _bt_disable_cb();
1350 int _bt_recover_adapter(void)
1355 GError *error = NULL;
1357 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
1358 BT_ERR("Disabling in progress");
1359 return BLUETOOTH_ERROR_IN_PROGRESS;
1362 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
1363 BT_ERR("Already disabled");
1364 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1367 _bt_adapter_set_status(BT_DEACTIVATING);
1369 proxy = __bt_get_core_proxy();
1370 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1372 result = g_dbus_proxy_call_sync(proxy,
1375 G_DBUS_CALL_FLAGS_NONE,
1381 if (error != NULL) {
1382 BT_ERR("Failed to RecoverAdapter (Error: %s)", error->message);
1383 g_clear_error(&error);
1385 BT_ERR("Failed to RecoverAdapter");
1386 return BLUETOOTH_ERROR_INTERNAL;
1389 is_recovery_mode = TRUE;
1391 g_variant_unref(result);
1392 __bt_disconnect_all();
1395 return BLUETOOTH_ERROR_NONE;
1398 int _bt_reset_adapter(void)
1402 GError *error = NULL;
1406 proxy = __bt_get_core_proxy();
1408 return BLUETOOTH_ERROR_INTERNAL;
1410 result = g_dbus_proxy_call_sync(proxy,
1413 G_DBUS_CALL_FLAGS_NONE,
1419 if (error != NULL) {
1420 BT_ERR("Failed to ResetAdapter (Error: %s)", error->message);
1421 g_clear_error(&error);
1423 BT_ERR("Failed to ResetAdapter");
1424 return BLUETOOTH_ERROR_INTERNAL;
1427 g_variant_unref(result);
1428 /* Terminate myself */
1429 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1430 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1432 return BLUETOOTH_ERROR_NONE;
1436 int _bt_check_adapter(int *status)
1439 char *adapter_path = NULL;
1441 BT_CHECK_PARAMETER(status, return);
1443 *status = BT_ADAPTER_DISABLED;
1445 adapter_path = _bt_get_adapter_path();
1448 if (adapter_path != NULL)
1449 *status = BT_ADAPTER_ENABLED;
1451 g_free(adapter_path);
1452 return BLUETOOTH_ERROR_NONE;
1455 int _bt_check_adapter(int *status)
1458 GError *error = NULL;
1461 gboolean powered = FALSE;
1463 BT_CHECK_PARAMETER(status, return);
1465 *status = BT_ADAPTER_DISABLED;
1467 proxy = _bt_get_adapter_properties_proxy();
1468 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1470 result = g_dbus_proxy_call_sync(proxy,
1472 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1474 G_DBUS_CALL_FLAGS_NONE,
1480 BT_ERR("Failed to get local address");
1481 if (error != NULL) {
1482 BT_ERR("Failed to get local address (Error: %s)", error->message);
1483 g_clear_error(&error);
1485 return BLUETOOTH_ERROR_INTERNAL;
1488 g_variant_get(result, "(v)", &temp);
1489 powered = g_variant_get_boolean(temp);
1490 BT_DBG("powered: %d", powered);
1493 *status = BT_ADAPTER_ENABLED;
1495 g_variant_unref(result);
1496 g_variant_unref(temp);
1497 return BLUETOOTH_ERROR_NONE;
1501 int _bt_enable_adapter_le(void)
1505 GError *error = NULL;
1506 bt_status_t status = _bt_adapter_get_status();
1507 bt_le_status_t le_status = _bt_adapter_get_le_status();
1510 if (le_status == BT_LE_ACTIVATING) {
1511 BT_ERR("Enabling in progress");
1512 return BLUETOOTH_ERROR_IN_PROGRESS;
1515 if (le_status == BT_LE_ACTIVATED) {
1516 BT_ERR("Already enabled");
1517 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
1520 if (status == BT_DEACTIVATING || le_status == BT_LE_DEACTIVATING) {
1521 BT_ERR("Disabling in progress");
1522 return BLUETOOTH_ERROR_DEVICE_BUSY;
1525 _bt_adapter_set_le_status(BT_LE_ACTIVATING);
1527 proxy = __bt_get_core_proxy();
1528 retv_if(!proxy, BLUETOOTH_ERROR_INTERNAL);
1530 result = g_dbus_proxy_call_sync(proxy, "EnableAdapterLe",
1532 G_DBUS_CALL_FLAGS_NONE, BT_ENABLE_TIMEOUT,
1535 BT_ERR("EnableAdapterLe failed: %s", error->message);
1536 _bt_adapter_set_le_status(BT_DEACTIVATED);
1537 g_clear_error(&error);
1539 /* Clean up the process */
1540 result = g_dbus_proxy_call_sync(proxy,
1543 G_DBUS_CALL_FLAGS_NONE,
1549 BT_ERR("Bt core call failed");
1551 BT_ERR("EnableAdapterLE Failed %s", error->message);
1552 g_clear_error(&error);
1555 g_variant_unref(result);
1556 /* Terminate myself */
1557 if (_bt_adapter_get_status() == BT_DEACTIVATED)
1558 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
1559 return BLUETOOTH_ERROR_INTERNAL;
1563 g_variant_unref(result);
1565 _bt_adapter_start_le_enable_timer();
1567 if (status == BT_ACTIVATED) {
1568 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1569 __bt_set_le_enabled();
1571 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1573 return BLUETOOTH_ERROR_NONE;
1576 int _bt_disable_adapter_le(void)
1580 bt_le_status_t bt_le_state;
1582 GError *error = NULL;
1584 bt_le_state = _bt_adapter_get_le_status();
1585 if (bt_le_state == BT_LE_DEACTIVATING) {
1586 BT_DBG("Disabling in progress");
1587 return BLUETOOTH_ERROR_IN_PROGRESS;
1590 if (bt_le_state == BT_LE_DEACTIVATED) {
1591 BT_DBG("Already disabled");
1592 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
1595 _bt_adapter_set_le_status(BT_LE_DEACTIVATING);
1597 proxy = __bt_get_core_proxy();
1599 return BLUETOOTH_ERROR_INTERNAL;
1601 result = g_dbus_proxy_call_sync(proxy,
1604 G_DBUS_CALL_FLAGS_NONE,
1610 if (error != NULL) {
1611 BT_ERR("Bt core call failed (Error: %s)", error->message);
1612 g_clear_error(&error);
1614 BT_ERR("Bt core call failed");
1615 _bt_adapter_set_le_status(BT_LE_ACTIVATED);
1616 return BLUETOOTH_ERROR_INTERNAL;
1619 g_variant_unref(result);
1620 _bt_set_le_disabled(BLUETOOTH_ERROR_NONE);
1621 BT_DBG("le status : %d", _bt_adapter_get_le_status());
1623 return BLUETOOTH_ERROR_NONE;
1626 int _bt_get_local_address(bluetooth_device_address_t *local_address)
1630 GError *error = NULL;
1631 const char *address;
1635 BT_CHECK_PARAMETER(local_address, return);
1637 proxy = _bt_get_adapter_properties_proxy();
1638 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1640 result = g_dbus_proxy_call_sync(proxy,
1642 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1644 G_DBUS_CALL_FLAGS_NONE,
1650 BT_ERR("Failed to get local address");
1651 if (error != NULL) {
1652 BT_ERR("Failed to get local address (Error: %s)", error->message);
1653 g_clear_error(&error);
1655 return BLUETOOTH_ERROR_INTERNAL;
1658 g_variant_get(result, "(v)", &temp);
1659 address = g_variant_get_string(temp, NULL);
1660 BT_DBG("Address:%s", address);
1663 _bt_convert_addr_string_to_type(local_address->addr, address);
1665 return BLUETOOTH_ERROR_INTERNAL;
1667 g_variant_unref(result);
1668 g_variant_unref(temp);
1669 return BLUETOOTH_ERROR_NONE;
1672 int _bt_get_local_version(bluetooth_version_t *local_version)
1675 const char *ver = NULL;
1677 int ret = BLUETOOTH_ERROR_NONE;
1681 BT_CHECK_PARAMETER(local_version, return);
1683 GError *error = NULL;
1685 proxy = _bt_get_adapter_properties_proxy();
1686 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1688 result = g_dbus_proxy_call_sync(proxy,
1690 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1692 G_DBUS_CALL_FLAGS_NONE,
1698 if (error != NULL) {
1699 BT_ERR("Failed to get local version (Error: %s)", error->message);
1700 g_clear_error(&error);
1702 BT_ERR("Failed to get local version");
1703 return BLUETOOTH_ERROR_INTERNAL;
1706 g_variant_get(result, "(v)", &temp);
1707 ver = g_variant_get_string(temp, NULL);
1708 BT_DBG("VERSION: %s", ver);
1710 if (ver && (strlen(ver) > 0)) {
1711 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1712 if (!g_utf8_validate(ver, -1, (const char **)&ptr))
1715 g_strlcpy(local_version->version, ver,
1716 BLUETOOTH_VERSION_LENGTH_MAX + 1);
1719 ret = BLUETOOTH_ERROR_INTERNAL;
1722 g_variant_unref(result);
1723 g_variant_unref(temp);
1727 int _bt_get_local_name(bluetooth_device_name_t *local_name)
1730 const char *name = NULL;
1732 int ret = BLUETOOTH_ERROR_NONE;
1735 GError *error = NULL;
1737 BT_CHECK_PARAMETER(local_name, return);
1739 proxy = _bt_get_adapter_properties_proxy();
1740 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1742 result = g_dbus_proxy_call_sync(proxy,
1744 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1746 G_DBUS_CALL_FLAGS_NONE,
1752 if (error != NULL) {
1753 BT_ERR("Failed to get local name (Error: %s)", error->message);
1754 g_clear_error(&error);
1756 BT_ERR("Failed to get local name");
1757 return BLUETOOTH_ERROR_INTERNAL;
1760 g_variant_get(result, "(v)", &temp);
1761 name = g_variant_get_string(temp, NULL);
1762 BT_DBG("LOCAL NAME:%s", name);
1764 if (name && (strlen(name) > 0)) {
1765 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
1766 if (!g_utf8_validate(name, -1, (const char **)&ptr))
1769 g_strlcpy(local_name->name, name,
1770 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
1772 ret = BLUETOOTH_ERROR_INTERNAL;
1774 g_variant_unref(result);
1775 g_variant_unref(temp);
1779 int _bt_set_local_name(char *local_name)
1782 GError *error = NULL;
1786 BT_CHECK_PARAMETER(local_name, return);
1788 proxy = _bt_get_adapter_properties_proxy();
1790 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1792 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
1795 result = g_dbus_proxy_call_sync(proxy,
1797 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1798 "Alias", g_variant_new("s", local_name)),
1799 G_DBUS_CALL_FLAGS_NONE,
1805 if (error != NULL) {
1806 BT_ERR("Failed to set Alias (Error: %s)", error->message);
1807 g_clear_error(&error);
1809 BT_ERR("Failed to set Alias");
1810 return BLUETOOTH_ERROR_INTERNAL;
1813 g_variant_unref(result);
1814 return BLUETOOTH_ERROR_NONE;
1817 int _bt_is_service_used(char *service_uuid, gboolean *used)
1820 GError *error = NULL;
1821 int ret = BLUETOOTH_ERROR_NONE;
1823 GVariant *temp = NULL;
1824 GVariantIter *iter = NULL;
1828 BT_CHECK_PARAMETER(service_uuid, return);
1829 BT_CHECK_PARAMETER(used, return);
1831 proxy = _bt_get_adapter_properties_proxy();
1832 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1834 result = g_dbus_proxy_call_sync(proxy,
1836 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1838 G_DBUS_CALL_FLAGS_NONE,
1844 if (error != NULL) {
1845 BT_ERR("Failed to get UUIDs (Error: %s)", error->message);
1846 g_clear_error(&error);
1848 BT_ERR("Failed to get UUIDs");
1849 return BLUETOOTH_ERROR_INTERNAL;
1852 g_variant_get(result, "(v)", &temp);
1853 g_variant_get(temp, "as", &iter);
1856 while (g_variant_iter_loop(iter, "&s", &uuid)) {
1857 if (strcasecmp(uuid, service_uuid) == 0) {
1862 g_variant_iter_free(iter);
1863 g_variant_unref(result);
1865 BT_DBG("Service Used? %d", *used);
1870 static gboolean __bt_get_discoverable_property(void)
1873 gboolean discoverable_v;
1874 GError *error = NULL;
1878 proxy = _bt_get_adapter_properties_proxy();
1879 retv_if(proxy == NULL, FALSE);
1881 result = g_dbus_proxy_call_sync(proxy,
1883 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
1885 G_DBUS_CALL_FLAGS_NONE,
1891 if (error != NULL) {
1892 BT_ERR("Failed to get Discoverable property (Error: %s)", error->message);
1893 g_clear_error(&error);
1895 BT_ERR("Failed to get Discoverable property");
1896 return BLUETOOTH_ERROR_INTERNAL;
1899 g_variant_get(result, "(v)", &temp);
1900 discoverable_v = g_variant_get_boolean(temp);
1901 BT_DBG("discoverable_v:%d", discoverable_v);
1903 g_variant_unref(result);
1904 g_variant_unref(temp);
1906 return discoverable_v;
1909 int _bt_get_discoverable_mode(int *mode)
1911 gboolean discoverable;
1912 unsigned int timeout;
1914 BT_CHECK_PARAMETER(mode, return);
1916 discoverable = __bt_get_discoverable_property();
1917 timeout = _bt_get_discoverable_timeout_property();
1919 if (discoverable == TRUE) {
1921 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
1923 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
1925 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
1927 return BLUETOOTH_ERROR_NONE;
1931 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
1933 int ret = BLUETOOTH_ERROR_NONE;
1936 GError *error = NULL;
1940 proxy = _bt_get_adapter_properties_proxy();
1942 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1944 #ifdef TIZEN_DPM_ENABLE
1945 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE &&
1946 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1947 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT_HANDSFREE");
1948 return BLUETOOTH_ERROR_ACCESS_DENIED;
1950 if (discoverable_mode != BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE &&
1951 _bt_dpm_get_bluetooth_limited_discoverable_state() == DPM_RESTRICTED) {
1952 _bt_launch_dpm_popup("DPM_POLICY_DISABLE_BT");
1953 return BLUETOOTH_ERROR_ACCESS_DENIED;
1957 switch (discoverable_mode) {
1958 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
1963 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
1968 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
1973 return BLUETOOTH_ERROR_INVALID_PARAM;
1976 BT_INFO("Req. discoverable_mode : %d, timeout : %d",
1977 discoverable_mode, timeout);
1979 result = g_dbus_proxy_call_sync(proxy,
1981 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
1982 "Connectable", g_variant_new("b", pg_scan)),
1983 G_DBUS_CALL_FLAGS_NONE,
1989 if (error != NULL) {
1990 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
1991 g_clear_error(&error);
1993 BT_ERR("Failed to set connectable property");
1994 return BLUETOOTH_ERROR_INTERNAL;
1996 g_variant_unref(result);
1997 result = g_dbus_proxy_call_sync(proxy,
1999 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Discoverable",
2000 g_variant_new("b", inq_scan)),
2001 G_DBUS_CALL_FLAGS_NONE,
2007 if (error != NULL) {
2008 BT_ERR("Failed to set Discoverable property (Error: %s)", error->message);
2009 g_clear_error(&error);
2011 BT_ERR("Failed to set Discoverable property");
2012 return BLUETOOTH_ERROR_INTERNAL;
2014 g_variant_unref(result);
2015 result = g_dbus_proxy_call_sync(proxy,
2017 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE,
2018 "DiscoverableTimeout", g_variant_new("u", timeout)),
2019 G_DBUS_CALL_FLAGS_NONE,
2025 if (error != NULL) {
2026 BT_ERR("Failed to set DiscoverableTimeout property (Error: %s)", error->message);
2027 g_clear_error(&error);
2029 BT_ERR("Failed to set DiscoverableTimeout property");
2030 return BLUETOOTH_ERROR_INTERNAL;
2033 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
2036 ret = __bt_set_visible_time(timeout);
2038 g_variant_unref(result);
2043 int _bt_start_discovery(void)
2046 GError *error = NULL;
2049 if (_bt_is_discovering() == TRUE) {
2050 BT_ERR("BT is already in discovering");
2051 return BLUETOOTH_ERROR_IN_PROGRESS;
2052 } else if (_bt_is_device_creating() == TRUE) {
2053 BT_ERR("Bonding device is going on");
2054 return BLUETOOTH_ERROR_DEVICE_BUSY;
2057 proxy = _bt_get_adapter_proxy();
2058 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2060 result = g_dbus_proxy_call_sync(proxy,
2063 G_DBUS_CALL_FLAGS_NONE,
2069 if (error != NULL) {
2070 BT_ERR("StartDiscovery failed (Error: %s)", error->message);
2071 g_clear_error(&error);
2073 BT_ERR("StartDiscovery failed");
2074 return BLUETOOTH_ERROR_INTERNAL;
2077 is_discovering = TRUE;
2078 cancel_by_user = FALSE;
2079 /* discovery status will be change in event */
2080 g_variant_unref(result);
2081 return BLUETOOTH_ERROR_NONE;
2084 int _bt_start_custom_discovery(bt_discovery_role_type_t role)
2088 GError *error = NULL;
2089 const gchar *disc_type;
2091 if (_bt_is_discovering() == TRUE) {
2092 BT_ERR("BT is already in discovering");
2093 return BLUETOOTH_ERROR_IN_PROGRESS;
2096 proxy = _bt_get_adapter_proxy();
2097 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2099 if (role == DISCOVERY_ROLE_BREDR)
2100 disc_type = "BREDR";
2101 else if (role == DISCOVERY_ROLE_LE)
2103 else if (role == DISCOVERY_ROLE_LE_BREDR)
2104 disc_type = "LE_BREDR";
2106 return BLUETOOTH_ERROR_INVALID_PARAM;
2108 result = g_dbus_proxy_call_sync(proxy,
2109 "StartCustomDiscovery",
2110 g_variant_new("s", disc_type),
2111 G_DBUS_CALL_FLAGS_NONE,
2117 if (error != NULL) {
2118 BT_ERR("StartCustomDiscovery failed (Error: %s)", error->message);
2119 g_clear_error(&error);
2121 BT_ERR("StartCustomDiscovery failed");
2122 return BLUETOOTH_ERROR_INTERNAL;
2125 is_discovering = TRUE;
2126 cancel_by_user = FALSE;
2127 /* discovery status will be change in event */
2128 g_variant_unref(result);
2129 return BLUETOOTH_ERROR_NONE;
2132 int _bt_cancel_discovery(void)
2135 GError *error = NULL;
2138 if (_bt_is_discovering() == FALSE) {
2139 BT_ERR("BT is not in discovering");
2140 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
2143 proxy = _bt_get_adapter_proxy();
2144 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2146 result = g_dbus_proxy_call_sync(proxy,
2149 G_DBUS_CALL_FLAGS_NONE,
2155 int ret = BLUETOOTH_ERROR_INTERNAL;
2156 if (error != NULL) {
2157 BT_ERR("StopDiscovery failed (Error: %s)", error->message);
2159 if (g_strrstr(error->message, "No discovery started"))
2160 ret = BLUETOOTH_ERROR_NOT_IN_OPERATION;
2162 g_clear_error(&error);
2164 BT_ERR("StopDiscovery failed");
2170 cancel_by_user = TRUE;
2171 /* discovery status will be change in event */
2172 g_variant_unref(result);
2173 return BLUETOOTH_ERROR_NONE;
2176 gboolean _bt_is_discovering(void)
2178 return is_discovering;
2181 gboolean _bt_is_connectable(void)
2184 GError *error = NULL;
2185 gboolean is_connectable = FALSE;
2189 proxy = _bt_get_adapter_properties_proxy();
2190 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2192 result = g_dbus_proxy_call_sync(proxy,
2194 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2196 G_DBUS_CALL_FLAGS_NONE,
2202 if (error != NULL) {
2203 BT_ERR("Failed to get connectable property (Error: %s)", error->message);
2204 g_clear_error(&error);
2206 BT_ERR("Failed to get connectable property");
2207 return BLUETOOTH_ERROR_INTERNAL;
2210 g_variant_get(result, "(v)", &temp);
2211 is_connectable = g_variant_get_boolean(temp);
2212 BT_DBG("discoverable_v:%d", is_connectable);
2214 g_variant_unref(result);
2215 g_variant_unref(temp);
2217 BT_INFO("Get connectable [%d]", is_connectable);
2218 return is_connectable;
2221 int _bt_set_connectable(gboolean is_connectable)
2224 GError *error = NULL;
2227 if (__bt_is_factory_test_mode()) {
2228 BT_ERR("Unable to set connectable in factory binary !!");
2229 return BLUETOOTH_ERROR_NOT_SUPPORT;
2232 proxy = _bt_get_adapter_properties_proxy();
2234 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2236 result = g_dbus_proxy_call_sync(proxy,
2238 g_variant_new("(ssv)", BT_ADAPTER_INTERFACE, "Connectable",
2239 g_variant_new("b", is_connectable)),
2240 G_DBUS_CALL_FLAGS_NONE,
2246 if (error != NULL) {
2247 BT_ERR("Failed to set connectable property (Error: %s)", error->message);
2248 g_clear_error(&error);
2250 BT_ERR("Failed to set connectable property");
2251 return BLUETOOTH_ERROR_INTERNAL;
2254 BT_INFO_C("### Set connectable [%d]", is_connectable);
2255 g_variant_unref(result);
2256 return BLUETOOTH_ERROR_NONE;
2259 gboolean _bt_get_discovering_property(bt_discovery_role_type_t discovery_type)
2262 gboolean discovering_v;
2263 GError *error = NULL;
2264 char *discovering_type = NULL;
2268 proxy = _bt_get_adapter_properties_proxy();
2269 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2271 if (discovery_type == DISCOVERY_ROLE_BREDR)
2272 discovering_type = "Discovering";
2273 else if (discovery_type == DISCOVERY_ROLE_LE)
2274 discovering_type = "LEDiscovering";
2276 result = g_dbus_proxy_call_sync(proxy,
2278 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2280 G_DBUS_CALL_FLAGS_NONE,
2286 if (error != NULL) {
2287 BT_ERR("Failed to get discovering property (Error: %s)", error->message);
2288 g_clear_error(&error);
2290 BT_ERR("Failed to get discovering property");
2291 return BLUETOOTH_ERROR_INTERNAL;
2294 g_variant_get(result, "(v)", &temp);
2295 discovering_v = g_variant_get_boolean(temp);
2296 BT_DBG("discoverable_v:%d", discovering_v);
2298 g_variant_unref(result);
2299 g_variant_unref(temp);
2301 return discovering_v;
2304 unsigned int _bt_get_discoverable_timeout_property(void)
2307 unsigned int timeout_v;
2308 GError *error = NULL;
2312 proxy = _bt_get_adapter_properties_proxy();
2313 retv_if(proxy == NULL, 0);
2315 result = g_dbus_proxy_call_sync(proxy,
2317 g_variant_new("(ss)", BT_ADAPTER_INTERFACE,
2318 "DiscoverableTimeout"),
2319 G_DBUS_CALL_FLAGS_NONE,
2325 BT_ERR("Fail to get discoverable timeout");
2326 if (error != NULL) {
2327 BT_ERR("Fail to get discoverable timeout (Error: %s)", error->message);
2328 g_clear_error(&error);
2333 g_variant_get(result, "(v)", &temp);
2334 timeout_v = g_variant_get_uint32(temp);
2335 BT_DBG("discoverable_v:%d", timeout_v);
2337 g_variant_unref(result);
2338 g_variant_unref(temp);
2343 static bluetooth_device_info_t *__bt_parse_device_info(GVariantIter *item_iter)
2345 bluetooth_device_info_t *dev_info;
2348 GByteArray *manufacturer_data = NULL;
2350 GVariantIter *char_value_iter;
2352 dev_info = g_malloc0(sizeof(bluetooth_device_info_t));
2354 while (g_variant_iter_loop(item_iter, "{sv}", &key, &value)) {
2359 if (!g_strcmp0(key, "Address")) {
2360 const char *address = NULL;
2361 address = g_variant_get_string(value, NULL);
2362 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
2364 } else if (!g_strcmp0(key, "Class")) {
2366 cod = g_variant_get_uint32(value);
2367 _bt_divide_device_class(&dev_info->device_class, cod);
2368 } else if (!g_strcmp0(key, "Name")) {
2369 const char *name = NULL;
2370 name = g_variant_get_string(value, NULL);
2371 /* If there is no Alias */
2372 if (strlen(dev_info->device_name.name) == 0) {
2373 g_strlcpy(dev_info->device_name.name, name,
2374 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2376 } else if (!g_strcmp0(key, "Alias")) {
2377 const char *alias = NULL;
2378 alias = g_variant_get_string(value, NULL);
2379 /* Overwrite the name */
2381 memset(dev_info->device_name.name, 0x00,
2382 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2383 g_strlcpy(dev_info->device_name.name, alias,
2384 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
2386 } else if (!g_strcmp0(key, "IsAliasSet")) {
2387 dev_info->is_alias_set = g_variant_get_boolean(value);
2388 } else if (!g_strcmp0(key, "Connected")) {
2389 dev_info->connected = g_variant_get_byte(value);
2390 } else if (!g_strcmp0(key, "Paired")) {
2391 dev_info->paired = g_variant_get_boolean(value);
2392 } else if (!g_strcmp0(key, "Trusted")) {
2393 dev_info->trust = g_variant_get_boolean(value);
2394 } else if (!g_strcmp0(key, "RSSI")) {
2395 dev_info->rssi = g_variant_get_int16(value);
2396 } else if (!g_strcmp0(key, "UUIDs")) {
2402 dev_info->service_index = 0;
2403 g_variant_get(value, "as", &iter);
2404 while (g_variant_iter_loop(iter, "s", &uuid)) {
2405 g_strlcpy(dev_info->uuids[i], uuid, BLUETOOTH_UUID_STRING_MAX);
2406 parts = g_strsplit(uuid, "-", -1);
2408 if (parts == NULL || parts[0] == NULL) {
2413 dev_info->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
2418 dev_info->service_index = i;
2419 g_variant_iter_free(iter);
2420 } else if (strcasecmp(key, "LegacyManufacturerDataLen") == 0) {
2421 dev_info->manufacturer_data.data_len = g_variant_get_uint16(value);
2422 } else if (strcasecmp(key, "LegacyManufacturerData") == 0) {
2423 manufacturer_data = g_byte_array_new();
2424 g_variant_get(value, "ay", &char_value_iter);
2425 while (g_variant_iter_loop(char_value_iter, "y", &char_value))
2426 g_byte_array_append(manufacturer_data, &char_value, 1);
2428 if (manufacturer_data) {
2429 if (manufacturer_data->len > 0)
2430 memcpy(dev_info->manufacturer_data.data, manufacturer_data->data, manufacturer_data->len);
2432 g_variant_iter_free(char_value_iter);
2433 g_byte_array_free(manufacturer_data, TRUE);
2440 static void __bt_extract_device_info(GVariantIter *iter,
2443 bluetooth_device_info_t *dev_info = NULL;
2444 char *object_path = NULL;
2445 GVariantIter *interface_iter;
2446 GVariantIter *svc_iter;
2447 char *interface_str = NULL;
2449 /* Parse the signature: oa{sa{sv}}} */
2450 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path,
2453 if (object_path == NULL)
2456 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2457 &interface_str, &svc_iter)) {
2458 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2459 BT_DBG("Found a device: %s", object_path);
2460 dev_info = __bt_parse_device_info(svc_iter);
2462 if (dev_info->paired == TRUE) {
2463 g_array_append_vals(*dev_list, dev_info,
2464 sizeof(bluetooth_device_info_t));
2468 g_free(interface_str);
2469 g_variant_iter_free(svc_iter);
2477 int _bt_get_bonded_devices(GArray **dev_list)
2480 GDBusConnection *conn;
2481 GDBusProxy *manager_proxy;
2482 GVariant *result = NULL;
2483 GVariantIter *iter = NULL;
2484 GError *error = NULL;
2486 conn = _bt_gdbus_get_system_gconn();
2487 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2489 manager_proxy = _bt_get_manager_proxy();
2490 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2492 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2494 G_DBUS_CALL_FLAGS_NONE,
2500 if (error != NULL) {
2501 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2502 g_clear_error(&error);
2504 BT_ERR("Failed to Failed to GetManagedObjects");
2505 return BLUETOOTH_ERROR_INTERNAL;
2508 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2509 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2511 __bt_extract_device_info(iter, dev_list);
2512 g_variant_iter_free(iter);
2513 g_variant_unref(result);
2516 return BLUETOOTH_ERROR_NONE;
2519 int _bt_get_profile_connected_devices(char *profile_uuid, GArray **addr_list)
2522 GDBusConnection *conn;
2523 GDBusProxy *manager_proxy;
2524 GVariant *result = NULL;
2525 GVariant *result1 = NULL;
2526 GVariantIter *iter = NULL;
2527 GError *error = NULL;
2528 char *object_path = NULL;
2529 GVariantIter *interface_iter;
2530 char *interface_str = NULL;
2531 GDBusProxy *device_proxy = NULL;
2532 gboolean is_connected = FALSE;
2534 conn = _bt_gdbus_get_system_gconn();
2535 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2537 manager_proxy = _bt_get_manager_proxy();
2538 retv_if(manager_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2540 result = g_dbus_proxy_call_sync(manager_proxy, "GetManagedObjects",
2542 G_DBUS_CALL_FLAGS_NONE,
2548 if (error != NULL) {
2549 BT_ERR("Failed to GetManagedObjects (Error: %s)", error->message);
2550 g_clear_error(&error);
2553 BT_ERR("Failed to Failed to GetManagedObjects");
2554 return BLUETOOTH_ERROR_INTERNAL;
2557 /* signature of GetManagedObjects: a{oa{sa{sv}}} */
2558 g_variant_get(result, "(a{oa{sa{sv}}})", &iter);
2560 /* Parse the signature: oa{sa{sv}}} */
2561 while (g_variant_iter_loop(iter, "{&oa{sa{sv}}}", &object_path, &interface_iter)) {
2562 if (object_path == NULL)
2565 while (g_variant_iter_loop(interface_iter, "{sa{sv}}",
2566 &interface_str, NULL)) {
2567 if (g_strcmp0(interface_str, "org.bluez.Device1") == 0) {
2568 BT_DBG("Found a device: %s", object_path);
2569 g_free(interface_str);
2571 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2572 NULL, BT_BLUEZ_NAME,
2573 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2575 if (device_proxy == NULL) {
2576 BT_DBG("Device don't have this service");
2580 result1 = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
2581 g_variant_new("(s)", profile_uuid),
2582 G_DBUS_CALL_FLAGS_NONE,
2587 if (result1 == NULL) {
2588 BT_ERR("Error occured in Proxy call");
2590 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
2591 g_error_free(error);
2594 g_object_unref(device_proxy);
2597 g_variant_get(result1, "(b)", &is_connected);
2599 if (is_connected == TRUE) {
2600 char address[BT_ADDRESS_STRING_SIZE];
2601 bluetooth_device_address_t *addr = NULL;
2603 _bt_convert_device_path_to_address(object_path, address);
2605 addr = g_malloc0(sizeof(bluetooth_device_address_t));
2606 _bt_convert_addr_string_to_type(addr->addr, address);
2608 g_array_append_vals(*addr_list, addr,
2609 sizeof(bluetooth_device_address_t));
2612 g_variant_unref(result1);
2613 g_object_unref(device_proxy);
2620 g_variant_unref(result);
2621 g_variant_iter_free(iter);
2624 return BLUETOOTH_ERROR_NONE;
2627 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
2628 bluetooth_device_info_t *dev_info)
2630 char *object_path = NULL;
2631 GDBusProxy *adapter_proxy;
2632 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2633 int ret = BLUETOOTH_ERROR_NONE;
2635 BT_CHECK_PARAMETER(device_address, return);
2636 BT_CHECK_PARAMETER(dev_info, return);
2638 adapter_proxy = _bt_get_adapter_proxy();
2639 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2641 _bt_convert_addr_type_to_string(address, device_address->addr);
2643 object_path = _bt_get_device_object_path(address);
2645 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2647 ret = __bt_get_bonded_device_info(object_path, dev_info);
2648 g_free(object_path);
2653 int _bt_is_alias_set(bluetooth_device_address_t *device_address, gboolean *is_alias_set)
2655 char *object_path = NULL;
2656 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2657 gboolean alias_set = FALSE;
2659 GDBusConnection *conn;
2660 GDBusProxy *device_proxy;
2661 GError *error = NULL;
2662 GVariant *result = NULL;
2663 GVariant *temp = NULL;
2666 BT_CHECK_PARAMETER(device_address, return);
2667 BT_CHECK_PARAMETER(is_alias_set, return);
2669 _bt_convert_addr_type_to_string(address, device_address->addr);
2671 object_path = _bt_get_device_object_path(address);
2672 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
2674 conn = _bt_gdbus_get_system_gconn();
2676 g_free(object_path);
2677 return BLUETOOTH_ERROR_INTERNAL;
2680 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2684 BT_PROPERTIES_INTERFACE,
2686 if (device_proxy == NULL) {
2687 g_free(object_path);
2688 return BLUETOOTH_ERROR_INTERNAL;
2691 result = g_dbus_proxy_call_sync(device_proxy, "Get",
2692 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "IsAliasSet"),
2693 G_DBUS_CALL_FLAGS_NONE,
2699 BT_ERR("Error occured in Proxy call");
2700 if (error != NULL) {
2701 BT_ERR("Getting is_alias_set property failed: [%s]\n", error->message);
2702 g_error_free(error);
2704 g_object_unref(device_proxy);
2705 g_free(object_path);
2706 return BLUETOOTH_ERROR_INTERNAL;
2709 g_variant_get(result, "(v)", &temp);
2710 alias_set = g_variant_get_boolean(temp);
2711 *is_alias_set = alias_set;
2712 BT_DBG("address: [%s] | *is_alias_set: %s", address, *is_alias_set ? "TRUE" : "FALSE");
2713 g_variant_unref(temp);
2714 g_variant_unref(result);
2715 g_object_unref(device_proxy);
2717 g_free(object_path);
2719 return BLUETOOTH_ERROR_NONE;
2722 int _bt_get_timeout_value(int *timeout)
2724 time_t current_time;
2727 /* Take current time */
2728 time(¤t_time);
2729 time_diff = difftime(current_time, visible_timer.start_time);
2731 BT_DBG("Time diff = %d\n", time_diff);
2733 *timeout = visible_timer.timeout - time_diff;
2735 return BLUETOOTH_ERROR_NONE;
2738 int _bt_set_le_privacy(gboolean set_privacy)
2741 GError *error = NULL;
2742 GVariant *result = NULL;
2744 if (__bt_is_factory_test_mode()) {
2745 BT_ERR("Unable to set le privacy in factory binary !!");
2746 return BLUETOOTH_ERROR_NOT_SUPPORT;
2749 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2750 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2751 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2754 proxy = _bt_get_adapter_proxy();
2755 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2757 result = g_dbus_proxy_call_sync(proxy,
2759 g_variant_new("(b)", set_privacy),
2760 G_DBUS_CALL_FLAGS_NONE,
2766 if (error != NULL) {
2767 BT_ERR("Failed to SetLePrivacy (Error: %s)", error->message);
2768 g_clear_error(&error);
2770 BT_ERR("Failed to SetLePrivacy");
2771 return BLUETOOTH_ERROR_INTERNAL;
2774 g_variant_unref(result);
2775 BT_INFO("SetLePrivacy as %d", set_privacy);
2776 return BLUETOOTH_ERROR_NONE;
2779 int _bt_set_le_static_random_address(gboolean is_enable)
2782 GError *error = NULL;
2783 GVariant *result = NULL;
2785 if (__bt_is_factory_test_mode()) {
2786 BT_ERR("Unable to set le random address in factory binary !!");
2787 return BLUETOOTH_ERROR_NOT_SUPPORT;
2790 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2791 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2792 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2795 proxy = _bt_get_adapter_proxy();
2796 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2798 result = g_dbus_proxy_call_sync(proxy,
2799 "SetLeStaticRandomAddress",
2800 g_variant_new("(b)", is_enable),
2801 G_DBUS_CALL_FLAGS_NONE,
2807 if (error != NULL) {
2808 BT_ERR("Failed to SetLeStaticRandomAddress (Error: %s)", error->message);
2809 g_clear_error(&error);
2811 BT_ERR("Failed to SetLeStaticRandomAddress");
2812 return BLUETOOTH_ERROR_INTERNAL;
2815 g_variant_unref(result);
2816 BT_INFO("SetLeStaticRandomAddress as %d", is_enable);
2817 return BLUETOOTH_ERROR_NONE;
2820 int _bt_set_manufacturer_data(bluetooth_manufacturer_data_t *m_data)
2823 GError *error = NULL;
2827 GVariantBuilder *builder;
2829 BT_CHECK_PARAMETER(m_data, return);
2831 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2832 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2833 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2836 proxy = _bt_get_adapter_proxy();
2837 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2839 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2841 for (i = 0; i < (m_data->data_len) + 2; i++)
2842 g_variant_builder_add(builder, "y", m_data->data[i]);
2844 val = g_variant_new("(ay)", builder);
2846 result = g_dbus_proxy_call_sync(proxy,
2847 "SetManufacturerData",
2849 G_DBUS_CALL_FLAGS_NONE,
2853 g_variant_builder_unref(builder);
2855 if (error != NULL) {
2856 BT_ERR("Failed to SetManufacturerData (Error: %s)", error->message);
2857 g_clear_error(&error);
2859 BT_ERR("Failed to SetManufacturerData");
2861 return BLUETOOTH_ERROR_INTERNAL;
2863 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
2865 for (i = 0; i < (m_data->data_len) + 2; i++)
2866 g_variant_builder_add(builder, "y", m_data->data[i]);
2868 val = g_variant_new("(ay)", builder);
2870 _bt_send_event(BT_ADAPTER_EVENT,
2871 BLUETOOTH_EVENT_MANUFACTURER_DATA_CHANGED,
2874 BT_INFO("Set manufacturer data");
2876 g_variant_builder_unref(builder);
2877 g_variant_unref(result);
2879 return BLUETOOTH_ERROR_NONE;
2883 int _bt_service_set_alarm(int timeout, bt_set_alarm_cb call_back, void *user_data, alarm_id_t *alarm_id)
2885 int result = BLUETOOTH_ERROR_NONE;
2886 bt_service_alarm_t *alarm = NULL;
2888 if (!call_back || !alarm_id)
2889 return BLUETOOTH_ERROR_INVALID_PARAM;
2891 if (!alarm_mgr.is_alarm_initialized) {
2892 result = alarmmgr_init("bt-service");
2894 BT_ERR("Failed to initialize alarm = %d", result);
2895 result = BLUETOOTH_ERROR_INTERNAL;
2898 result = alarmmgr_set_cb(alarm_cb, NULL);
2900 BT_ERR("Failed to set the callback = %d", result);
2901 result = BLUETOOTH_ERROR_INTERNAL;
2904 alarm_mgr.is_alarm_initialized = TRUE;
2907 alarm = g_malloc0(sizeof(bt_service_alarm_t));
2909 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2911 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, timeout,
2914 BT_ERR("Failed to create alarm error = %d", result);
2915 result = BLUETOOTH_ERROR_INTERNAL;
2919 alarm->alarm_id = *alarm_id;
2920 alarm->callback = call_back;
2921 alarm->user_data = user_data;
2923 alarm_mgr.g_alarm_list = g_list_append(alarm_mgr.g_alarm_list, alarm);
2924 result = BLUETOOTH_ERROR_NONE;
2929 static int alarm_cb(alarm_id_t alarm_id, void* user_param)
2932 bt_service_alarm_t *p_data;
2933 bt_set_alarm_cb callback = NULL;
2934 void *user_data = NULL;
2936 node = g_list_find_custom(alarm_mgr.g_alarm_list,
2937 GINT_TO_POINTER(alarm_id), compare_alarm);
2941 p_data = (bt_service_alarm_t *)node->data;
2942 alarm_mgr.g_alarm_list = g_list_delete_link(alarm_mgr.g_alarm_list,
2948 callback = p_data->callback;
2949 user_data = p_data->user_data;
2953 callback(alarm_id, user_data);
2958 int _bt_service_remove_alarm(alarm_id_t alarm_id)
2961 bt_service_alarm_t *p_data;
2962 list = g_list_find_custom(alarm_mgr.g_alarm_list, GINT_TO_POINTER(alarm_id), compare_alarm);
2965 alarmmgr_remove_alarm(alarm_id);
2966 p_data = (bt_service_alarm_t *)list->data;
2967 alarm_mgr.g_alarm_list = g_list_remove(alarm_mgr.g_alarm_list, list->data);
2974 gint compare_alarm(gconstpointer list_data, gconstpointer data)
2976 alarm_id_t alarm_id = (alarm_id_t)data;
2977 bt_service_alarm_t *p_data = (bt_service_alarm_t *)list_data;
2979 if (p_data->alarm_id == alarm_id)
2985 static void alarm_data_free(void *data)
2987 bt_service_alarm_t *p_data = (bt_service_alarm_t *)data;
2992 static gboolean _bt_adapter_request_delayed_cb(gpointer user_data)
2995 int function = (int)user_data;
2998 case BT_ENABLE_ADAPTER:
2999 result = _bt_enable_adapter();
3000 if (result != BLUETOOTH_ERROR_NONE) {
3001 BT_ERR("_bt_enable_adapter is failed");
3002 /* Send enabled event to API */
3003 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
3004 g_variant_new("(i)", result));
3007 case BT_DISABLE_ADAPTER:
3008 result = _bt_disable_adapter();
3009 if (result != BLUETOOTH_ERROR_NONE) {
3010 BT_ERR("_bt_disable_adapter is failed");
3011 /* Send disabled event to API */
3012 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
3013 g_variant_new("(i)", result));
3017 BT_ERR("function is NOT matched");
3024 int _bt_adapter_request_delayed(int function)
3029 case BT_ENABLE_ADAPTER:
3030 ret = _bt_enable_adapter_check_status();
3031 if (ret == BLUETOOTH_ERROR_NONE)
3032 _bt_adapter_set_status(BT_ACTIVATING);
3037 case BT_DISABLE_ADAPTER:
3038 ret = _bt_disable_adapter_check_status();
3039 if (ret == BLUETOOTH_ERROR_NONE)
3040 _bt_adapter_set_status(BT_DEACTIVATING);
3046 BT_ERR("function is NOT matched");
3047 return BLUETOOTH_ERROR_INTERNAL;
3050 g_idle_add((GSourceFunc)_bt_adapter_request_delayed_cb, (void*)function);
3052 return BLUETOOTH_ERROR_NONE;
3056 int _bt_get_enable_timer_id(void)