4 * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dbus/dbus-glib.h>
22 #include <dbus/dbus.h>
28 #include <syspopup_caller.h>
33 #include "bluetooth-api.h"
34 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-event.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-util.h"
40 #include "bt-service-network.h"
41 #include "bt-service-obex-server.h"
42 #include "bt-service-agent.h"
43 #include "bt-service-main.h"
44 #include "bt-service-avrcp.h"
53 bt_adapter_timer_t visible_timer;
54 static gboolean is_discovering;
55 static gboolean cancel_by_user;
56 static bt_status_t adapter_status = BT_DEACTIVATED;
57 static void *adapter_agent = NULL;
58 static DBusGProxy *core_proxy = NULL;
60 #define BT_CORE_NAME "org.projectx.bt_core"
61 #define BT_CORE_PATH "/org/projectx/bt_core"
62 #define BT_CORE_INTERFACE "org.projectx.btcore"
64 static gboolean __bt_timeout_handler(gpointer user_data)
66 int result = BLUETOOTH_ERROR_NONE;
70 /* Take current time */
72 time_diff = difftime(current_time, visible_timer.start_time);
74 /* Send event to application */
75 _bt_send_event(BT_ADAPTER_EVENT,
76 BLUETOOTH_EVENT_DISCOVERABLE_TIMEOUT_CHANGED,
77 DBUS_TYPE_INT32, &result,
78 DBUS_TYPE_INT16, &time_diff,
81 if (visible_timer.timeout <= time_diff) {
82 g_source_remove(visible_timer.event_id);
83 visible_timer.event_id = 0;
84 visible_timer.timeout = 0;
86 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
87 BT_DBG("Set vconf failed\n");
94 static int __bt_visibility_alarm_cb(alarm_id_t alarm_id, void* user_param)
96 BT_DBG("__bt_visibility_alarm_cb \n");
98 /* Switch Off visibility in Bluez */
99 _bt_set_discoverable_mode(BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE, 0);
100 visible_timer.alarm_id = 0;
105 static void __bt_visibility_alarm_create()
110 result = alarmmgr_add_alarm(ALARM_TYPE_VOLATILE, visible_timer.timeout,
113 BT_DBG("Failed to create alarm error = %d\n", result);
116 BT_DBG("Alarm created = %d\n", alarm_id);
117 visible_timer.alarm_id = alarm_id;
121 int __bt_set_visible_time(int timeout)
125 if (visible_timer.event_id > 0) {
126 g_source_remove(visible_timer.event_id);
127 visible_timer.event_id = 0;
130 if (visible_timer.alarm_id > 0) {
131 alarmmgr_remove_alarm(visible_timer.alarm_id);
132 visible_timer.alarm_id = 0;
135 visible_timer.timeout = timeout;
137 if (vconf_set_int(BT_FILE_VISIBLE_TIME, timeout) != 0)
138 BT_ERR("Set vconf failed\n");
141 return BLUETOOTH_ERROR_NONE;
143 /* Take start time */
144 time(&(visible_timer.start_time));
145 visible_timer.event_id = g_timeout_add_seconds(1,
146 __bt_timeout_handler, NULL);
148 /* Set Alarm timer to switch off BT */
149 result = alarmmgr_init("bt-service");
151 return BLUETOOTH_ERROR_INTERNAL;
153 result = alarmmgr_set_cb(__bt_visibility_alarm_cb, NULL);
155 return BLUETOOTH_ERROR_INTERNAL;
157 __bt_visibility_alarm_create();
159 return BLUETOOTH_ERROR_NONE;
162 static void __bt_get_service_list(GValue *value, bluetooth_device_info_t *dev)
168 ret_if(value == NULL);
171 uuids = g_value_get_boxed(value);
172 ret_if(uuids == NULL);
174 dev->service_index = 0;
176 for (i = 0; uuids[i] != NULL; i++) {
177 g_strlcpy(dev->uuids[i], uuids[i], BLUETOOTH_UUID_STRING_MAX);
179 parts = g_strsplit(uuids[i], "-", -1);
181 if (parts == NULL || parts[0] == NULL)
184 dev->service_list_array[i] = g_ascii_strtoull(parts[0], NULL, 16);
187 dev->service_index++;
191 static int __bt_get_bonded_device_info(gchar *device_path,
192 bluetooth_device_info_t *dev_info)
194 GValue *value = { 0 };
196 DBusGProxy *device_proxy;
197 const gchar *address;
204 GHashTable *hash = NULL;
206 DBusGConnection *conn;
208 BT_CHECK_PARAMETER(device_path, return);
209 BT_CHECK_PARAMETER(dev_info, return);
211 conn = _bt_get_system_gconn();
212 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
214 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
215 device_path, BT_DEVICE_INTERFACE);
217 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
219 dbus_g_proxy_call(device_proxy, "GetProperties", &err,
221 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
222 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
224 g_object_unref(device_proxy);
227 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
229 return BLUETOOTH_ERROR_INTERNAL;
233 value = g_hash_table_lookup(hash, "Paired");
234 paired = g_value_get_boolean(value);
236 value = g_hash_table_lookup(hash, "Address");
237 address = value ? g_value_get_string(value) : NULL;
239 value = g_hash_table_lookup(hash, "Alias");
240 name = value ? g_value_get_string(value) : NULL;
243 BT_DBG("Alias Name [%s]", name);
245 value = g_hash_table_lookup(hash, "Name");
246 name = value ? g_value_get_string(value) : NULL;
249 value = g_hash_table_lookup(hash, "Class");
250 cod = value ? g_value_get_uint(value) : 0;
252 value = g_hash_table_lookup(hash, "Connected");
253 connected = value ? g_value_get_boolean(value) : FALSE;
255 value = g_hash_table_lookup(hash, "Trusted");
256 trust = value ? g_value_get_boolean(value) : FALSE;
258 if ((paired == FALSE) && (trust == FALSE)) {
259 return BLUETOOTH_ERROR_NOT_PAIRED;
262 value = g_hash_table_lookup(hash, "RSSI");
263 rssi = value ? g_value_get_int(value) : 0;
265 value = g_hash_table_lookup(hash, "UUIDs");
266 __bt_get_service_list(value, dev_info);
268 _bt_convert_addr_string_to_type(dev_info->device_address.addr,
271 _bt_divide_device_class(&dev_info->device_class, cod);
273 g_strlcpy(dev_info->device_name.name, name,
274 BLUETOOTH_DEVICE_NAME_LENGTH_MAX+1);
276 dev_info->rssi = rssi;
277 dev_info->trust = trust;
278 dev_info->paired = paired;
279 dev_info->connected = connected;
280 g_hash_table_destroy(hash);
281 ret = BLUETOOTH_ERROR_NONE;
283 BT_ERR("Hash is NULL\n");
284 ret = BLUETOOTH_ERROR_INTERNAL;
290 void _bt_set_discovery_status(gboolean mode)
292 is_discovering = mode;
295 void _bt_set_cancel_by_user(gboolean value)
297 cancel_by_user = value;
300 gboolean _bt_get_cancel_by_user(void)
302 return cancel_by_user;
305 static void __bt_flight_mode_cb(keynode_t *node, void *data)
307 gboolean flight_mode = FALSE;
310 BT_DBG("key=%s\n", vconf_keynode_get_name(node));
312 bt_status = _bt_adapter_get_status();
314 if (vconf_keynode_get_type(node) == VCONF_TYPE_BOOL) {
315 flight_mode = vconf_keynode_get_bool(node);
317 BT_DBG("value=%d\n", flight_mode);
319 if (flight_mode == TRUE) {
320 BT_DBG("Deactivate Bluetooth Service\n");
321 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 1) != 0)
322 BT_DBG("Set vconf failed+\n");
324 if (bt_status == BT_ACTIVATED)
325 _bt_disable_adapter();
330 if (vconf_get_int(BT_OFF_DUE_TO_FLIGHT_MODE, &value))
331 BT_ERR("Fail get flight mode value");
336 BT_DBG("Activate Bluetooth Service\n");
337 if (vconf_set_int(BT_OFF_DUE_TO_FLIGHT_MODE, 0))
338 BT_DBG("Set vconf failed\n");
340 if (bt_status == BT_DEACTIVATED)
341 _bt_enable_adapter();
346 static void __launch_bt_service(int status, int run_type)
349 char status_val[5] = { 0, };
350 char run_type_val[5] = { 0, };
352 snprintf(status_val, sizeof(status_val), "%d", status);
353 snprintf(run_type_val, sizeof(run_type_val), "%d", run_type);
355 BT_DBG("status: %s, run_type: %s", status_val, run_type_val);
357 kb = bundle_create();
359 bundle_add(kb, "launch-type", "setstate");
360 bundle_add(kb, "status", status_val);
361 bundle_add(kb, "run-type", run_type_val);
363 aul_launch_app("com.samsung.bluetooth", kb);
368 static void __bt_adapter_set_status(bt_status_t status)
370 adapter_status = status;
373 bt_status_t _bt_adapter_get_status(void)
375 return adapter_status;
378 static void __bt_phone_name_changed_cb(keynode_t *node, void *data)
380 char *phone_name = NULL;
386 if (vconf_keynode_get_type(node) == VCONF_TYPE_STRING) {
387 phone_name = vconf_keynode_get_str(node);
388 if (phone_name && strlen(phone_name) != 0) {
389 if (!g_utf8_validate(phone_name, -1,
390 (const char **)&ptr))
393 _bt_set_local_name(phone_name);
398 static void __bt_set_visible_mode(void)
402 if (vconf_get_int(BT_FILE_VISIBLE_TIME, &timeout) != 0)
403 BT_ERR("Fail to get the timeout value");
407 if (_bt_set_discoverable_mode(
408 BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE,
409 timeout) != BLUETOOTH_ERROR_NONE) {
410 if (vconf_set_int(BT_FILE_VISIBLE_TIME, 0) != 0)
411 BT_ERR("Set vconf failed");
416 static void __bt_set_local_name(void)
418 char *phone_name = NULL;
421 phone_name = vconf_get_str(VCONFKEY_SETAPPL_DEVICE_NAME_STR);
426 if (strlen(phone_name) != 0) {
427 if (!g_utf8_validate(phone_name, -1, (const char **)&ptr))
429 _bt_set_local_name(phone_name);
434 static int __bt_set_enabled(void)
437 int result = BLUETOOTH_ERROR_NONE;
439 _bt_check_adapter(&enabled);
441 if (enabled == FALSE) {
442 BT_ERR("Bluetoothd is not running");
443 return BLUETOOTH_ERROR_INTERNAL;
446 __bt_set_visible_mode();
448 __bt_set_local_name();
450 /* Update Bluetooth Status to notify other modules */
451 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_ON) != 0)
452 BT_ERR("Set vconf failed\n");
454 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
455 BT_ERR("Set vconf failed\n");
457 /* Send enabled event to API */
458 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_ENABLED,
459 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
461 return BLUETOOTH_ERROR_NONE;
464 static void __bt_set_disabled(int result)
466 /* Update Bluetooth Status to notify other modules */
467 if (vconf_set_int(VCONFKEY_BT_STATUS, VCONFKEY_BT_STATUS_OFF) != 0)
468 BT_ERR("Set vconf failed\n");
470 if (vconf_set_int(VCONFKEY_BT_DEVICE, VCONFKEY_BT_DEVICE_NONE) != 0)
471 BT_ERR("Set vconf failed\n");
473 /* Send disabled event */
474 _bt_send_event(BT_ADAPTER_EVENT, BLUETOOTH_EVENT_DISABLED,
475 DBUS_TYPE_INT32, &result, DBUS_TYPE_INVALID);
478 void *_bt_get_adapter_agent(void)
480 return adapter_agent;
483 void _bt_handle_flight_mode_noti(void)
486 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
487 __bt_flight_mode_cb, NULL);
491 void _bt_handle_adapter_added(void)
493 adapter_agent = _bt_create_agent(BT_ADAPTER_AGENT_PATH, TRUE);
494 if (!adapter_agent) {
495 BT_ERR("Fail to register agent");
499 if (!aul_app_is_running("com.samsung.bluetooth"))
500 __launch_bt_service(0, 0);
502 if (_bt_register_media_player() != BLUETOOTH_ERROR_NONE)
503 BT_ERR("Fail to register media player");
505 if (_bt_register_obex_server() != BLUETOOTH_ERROR_NONE)
506 BT_ERR("Fail to init obex server");
508 if (_bt_network_activate() != BLUETOOTH_ERROR_NONE)
509 BT_ERR("Fail to activate network");
511 /* add the vconf noti handler */
512 vconf_notify_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
513 __bt_phone_name_changed_cb, NULL);
515 vconf_notify_key_changed(VCONFKEY_TELEPHONY_FLIGHT_MODE,
516 __bt_flight_mode_cb, NULL);
520 __bt_adapter_set_status(BT_ACTIVATED);
523 void _bt_handle_adapter_removed(void)
525 __bt_adapter_set_status(BT_DEACTIVATED);
527 vconf_ignore_key_changed(VCONFKEY_SETAPPL_DEVICE_NAME_STR,
528 (vconf_callback_fn)__bt_phone_name_changed_cb);
530 _bt_destroy_agent(adapter_agent);
531 adapter_agent = NULL;
533 __bt_set_disabled(BLUETOOTH_ERROR_NONE);
535 _bt_terminate_service(NULL);
538 DBusGProxy *_bt_init_core_proxy(void)
541 DBusGConnection *conn;
543 conn = _bt_get_system_gconn();
547 proxy = dbus_g_proxy_new_for_name(conn, BT_CORE_NAME,
548 BT_CORE_PATH, BT_CORE_INTERFACE);
557 static DBusGProxy *__bt_get_core_proxy(void)
559 return (core_proxy) ? core_proxy : _bt_init_core_proxy();
562 gboolean __bt_enable_timeout_cb(gpointer user_data)
566 retv_if(_bt_adapter_get_status() == BT_ACTIVATED, FALSE);
568 proxy = __bt_get_core_proxy();
570 return BLUETOOTH_ERROR_INTERNAL;
572 /* Clean up the process */
573 if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
574 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
575 BT_ERR("Bt core call failed");
578 __bt_adapter_set_status(BT_DEACTIVATED);
580 __bt_set_disabled(BLUETOOTH_ERROR_TIMEOUT);
582 /* Display notification */
583 status_message_post(BT_STR_NOT_SUPPORT);
585 _bt_terminate_service(NULL);
590 int _bt_enable_adapter(void)
597 if (_bt_adapter_get_status() == BT_ACTIVATING) {
598 BT_DBG("Enabling in progress");
599 return BLUETOOTH_ERROR_IN_PROGRESS;
602 if (_bt_adapter_get_status() == BT_ACTIVATED) {
603 BT_DBG("Already enabled");
604 return BLUETOOTH_ERROR_DEVICE_ALREADY_ENABLED;
607 __bt_adapter_set_status(BT_ACTIVATING);
609 proxy = __bt_get_core_proxy();
611 return BLUETOOTH_ERROR_INTERNAL;
613 if (dbus_g_proxy_call_with_timeout(proxy, "EnableAdapter",
614 BT_ENABLE_TIMEOUT, &err,
616 G_TYPE_INVALID) == FALSE) {
618 __bt_adapter_set_status(BT_DEACTIVATED);
621 BT_ERR("Bt core call failed: [%s]", err->message);
625 /* Clean up the process */
626 if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
627 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
628 BT_ERR("Bt core call failed");
631 /* Display notification */
632 status_message_post(BT_STR_NOT_SUPPORT);
634 /* Terminate myself */
635 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
636 return BLUETOOTH_ERROR_INTERNAL;
639 g_timeout_add(BT_ENABLE_TIMEOUT,
640 (GSourceFunc)__bt_enable_timeout_cb,
643 return BLUETOOTH_ERROR_NONE;
646 int _bt_disable_adapter(void)
652 if (_bt_adapter_get_status() == BT_DEACTIVATING) {
653 BT_DBG("Disabling in progress");
654 return BLUETOOTH_ERROR_IN_PROGRESS;
657 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
658 BT_DBG("Already disabled");
659 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
662 __bt_adapter_set_status(BT_DEACTIVATING);
664 proxy = __bt_get_core_proxy();
666 return BLUETOOTH_ERROR_INTERNAL;
668 if (dbus_g_proxy_call(proxy, "DisableAdapter", NULL,
669 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
670 BT_ERR("Bt core call failed");
671 __bt_adapter_set_status(BT_ACTIVATED);
672 return BLUETOOTH_ERROR_INTERNAL;
675 return BLUETOOTH_ERROR_NONE;
678 int _bt_reset_adapter(void)
684 proxy = __bt_get_core_proxy();
686 return BLUETOOTH_ERROR_INTERNAL;
688 if (dbus_g_proxy_call(proxy, "ResetAdapter", NULL,
689 G_TYPE_INVALID, G_TYPE_INVALID) == FALSE) {
690 BT_ERR("Bt core call failed");
691 return BLUETOOTH_ERROR_INTERNAL;
694 /* Terminate myself */
695 if (_bt_adapter_get_status() == BT_DEACTIVATED) {
696 g_idle_add((GSourceFunc)_bt_terminate_service, NULL);
699 return BLUETOOTH_ERROR_NONE;
702 int _bt_check_adapter(int *status)
705 char *adapter_path = NULL;
707 BT_CHECK_PARAMETER(status, return);
709 *status = 0; /* 0: disabled */
711 proxy = _bt_get_manager_proxy();
712 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
714 if (!dbus_g_proxy_call(proxy, "DefaultAdapter", NULL,
715 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
716 &adapter_path, G_TYPE_INVALID)) {
717 BT_ERR("Fait to get DefaultAdapter");
718 return BLUETOOTH_ERROR_NONE;
721 if (adapter_path != NULL)
722 *status = 1; /* 1: enabled */
724 g_free(adapter_path);
725 return BLUETOOTH_ERROR_NONE;
728 int _bt_get_local_address(bluetooth_device_address_t *local_address)
731 GHashTable *hash = NULL;
733 char *address = NULL;
735 BT_CHECK_PARAMETER(local_address, return);
737 proxy = _bt_get_adapter_proxy();
738 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
740 dbus_g_proxy_call(proxy, "GetProperties", NULL,
742 dbus_g_type_get_map("GHashTable",
743 G_TYPE_STRING, G_TYPE_VALUE),
744 &hash, G_TYPE_INVALID);
747 value = g_hash_table_lookup(hash, "Address");
748 address = (char *)(value ? g_value_dup_string(value) : NULL);
749 g_hash_table_destroy(hash);
753 _bt_convert_addr_string_to_type(local_address->addr, address);
756 return BLUETOOTH_ERROR_INTERNAL;
759 return BLUETOOTH_ERROR_NONE;
762 int _bt_get_local_name(bluetooth_device_name_t *local_name)
765 GHashTable *hash = NULL;
769 int ret = BLUETOOTH_ERROR_NONE;
771 BT_CHECK_PARAMETER(local_name, return);
773 proxy = _bt_get_adapter_proxy();
774 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
776 dbus_g_proxy_call(proxy, "GetProperties", NULL,
778 dbus_g_type_get_map("GHashTable",
779 G_TYPE_STRING, G_TYPE_VALUE),
780 &hash, G_TYPE_INVALID);
783 value = g_hash_table_lookup(hash, "Name");
784 name = (char *)(value ? g_value_get_string(value) : NULL);
787 if (name && (strlen(name) > 0)) {
788 /* Check the utf8 valitation & Fill the NULL in the invalid location*/
789 if (!g_utf8_validate(name, -1, (const char **)&ptr))
792 g_strlcpy(local_name->name, name,
793 BLUETOOTH_DEVICE_NAME_LENGTH_MAX + 1);
795 ret = BLUETOOTH_ERROR_INTERNAL;
798 g_hash_table_destroy(hash);
802 int _bt_set_local_name(char *local_name)
806 GError *error = NULL;
809 BT_CHECK_PARAMETER(local_name, return);
811 proxy = _bt_get_adapter_proxy();
812 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
814 if (!g_utf8_validate(local_name, -1, (const char **)&ptr))
817 g_value_init(&name, G_TYPE_STRING);
818 g_value_set_string(&name, local_name);
820 dbus_g_proxy_call(proxy, "SetProperty",
821 &error, G_TYPE_STRING, "Name",
822 G_TYPE_VALUE, &name, G_TYPE_INVALID, G_TYPE_INVALID);
824 g_value_unset(&name);
827 BT_ERR("SetProperty Fail: %s", error->message);
829 return BLUETOOTH_ERROR_INTERNAL;
832 return BLUETOOTH_ERROR_NONE;
835 int _bt_is_service_used(char *service_uuid, gboolean *used)
840 GHashTable *hash = NULL;
842 int ret = BLUETOOTH_ERROR_NONE;
844 BT_CHECK_PARAMETER(service_uuid, return);
845 BT_CHECK_PARAMETER(used, return);
847 proxy = _bt_get_adapter_proxy();
848 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
850 dbus_g_proxy_call(proxy, "GetProperties", NULL,
852 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
853 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
855 retv_if(hash == NULL, BLUETOOTH_ERROR_INTERNAL);
857 value = g_hash_table_lookup(hash, "UUIDs");
858 uuids = g_value_get_boxed(value);
866 for (i = 0; uuids[i] != NULL; i++) {
867 if (strcasecmp(uuids[i], service_uuid) == 0) {
875 g_hash_table_destroy(hash);
879 int _bt_get_discoverable_mode(int *mode)
882 GHashTable *hash = NULL;
884 GValue *timeout_value;
886 BT_CHECK_PARAMETER(mode, return);
888 proxy = _bt_get_adapter_proxy();
889 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
891 dbus_g_proxy_call(proxy, "GetProperties", NULL,
893 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
894 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
896 retv_if(hash == NULL, BLUETOOTH_ERROR_INTERNAL);
898 value = g_hash_table_lookup(hash, "Discoverable");
899 timeout_value = g_hash_table_lookup(hash, "DiscoverableTimeout");
901 retv_if(value == NULL, BLUETOOTH_ERROR_INTERNAL);
903 if (g_value_get_boolean(value)) {
904 if (g_value_get_uint(timeout_value) == 0)
905 *mode = BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE;
907 *mode = BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE;
909 *mode = BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE;
911 g_hash_table_destroy(hash);
912 return BLUETOOTH_ERROR_NONE;
915 int _bt_set_discoverable_mode(int discoverable_mode, int timeout)
917 int ret = BLUETOOTH_ERROR_NONE;
920 GError *error = NULL;
921 GValue connectable = { 0 };
922 GValue discoverable = { 0 };
923 GValue val_timeout = { 0 };
926 proxy = _bt_get_adapter_proxy();
927 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
929 g_value_init(&connectable, G_TYPE_BOOLEAN);
930 g_value_init(&discoverable, G_TYPE_BOOLEAN);
931 g_value_init(&val_timeout, G_TYPE_UINT);
933 switch (discoverable_mode) {
934 case BLUETOOTH_DISCOVERABLE_MODE_CONNECTABLE:
939 case BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE:
944 case BLUETOOTH_DISCOVERABLE_MODE_TIME_LIMITED_DISCOVERABLE:
949 return BLUETOOTH_ERROR_INVALID_PARAM;
952 g_value_set_boolean(&connectable, pg_scan);
953 g_value_set_boolean(&discoverable, inq_scan);
954 g_value_set_uint(&val_timeout, timeout);
956 dbus_g_proxy_call(proxy, "SetProperty", &error,
957 G_TYPE_STRING, "Powered",
958 G_TYPE_VALUE, &connectable,
959 G_TYPE_INVALID, G_TYPE_INVALID);
962 BT_ERR("Powered set err:[%s]", error->message);
964 ret = BLUETOOTH_ERROR_INTERNAL;
968 dbus_g_proxy_call(proxy, "SetProperty", &error,
969 G_TYPE_STRING, "Discoverable",
970 G_TYPE_VALUE, &discoverable,
971 G_TYPE_INVALID, G_TYPE_INVALID);
974 BT_ERR("Discoverable set err:[%s]", error->message);
976 ret = BLUETOOTH_ERROR_INTERNAL;
980 dbus_g_proxy_call(proxy, "SetProperty", &error,
981 G_TYPE_STRING, "DiscoverableTimeout",
982 G_TYPE_VALUE, &val_timeout,
983 G_TYPE_INVALID, G_TYPE_INVALID);
986 BT_ERR("Timeout set err:[%s]", error->message);
988 ret = BLUETOOTH_ERROR_INTERNAL;
992 if (discoverable_mode == BLUETOOTH_DISCOVERABLE_MODE_GENERAL_DISCOVERABLE)
995 ret = __bt_set_visible_time(timeout);
998 g_value_unset(&val_timeout);
999 g_value_unset(&connectable);
1000 g_value_unset(&discoverable);
1005 int _bt_start_discovery(void)
1009 if (_bt_is_discovering() == TRUE) {
1010 BT_ERR("BT is already in discovering");
1011 return BLUETOOTH_ERROR_IN_PROGRESS;
1014 proxy = _bt_get_adapter_proxy();
1015 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1017 if (!dbus_g_proxy_call(proxy, "StartDiscovery", NULL,
1018 G_TYPE_INVALID, G_TYPE_INVALID)) {
1019 BT_ERR("Discover start failed");
1020 return BLUETOOTH_ERROR_INTERNAL;
1023 is_discovering = TRUE;
1024 cancel_by_user = FALSE;
1025 /* discovery status will be change in event */
1027 return BLUETOOTH_ERROR_NONE;
1030 int _bt_cancel_discovery(void)
1034 if (_bt_is_discovering() == FALSE) {
1035 BT_ERR("BT is not in discovering");
1036 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1039 proxy = _bt_get_adapter_proxy();
1040 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1042 if (!dbus_g_proxy_call(proxy, "StopDiscovery", NULL,
1043 G_TYPE_INVALID, G_TYPE_INVALID)) {
1044 BT_ERR("Discover stop failed");
1045 return BLUETOOTH_ERROR_INTERNAL;
1048 cancel_by_user = TRUE;
1049 /* discovery status will be change in event */
1051 return BLUETOOTH_ERROR_NONE;
1054 gboolean _bt_is_discovering(void)
1056 return is_discovering;
1059 gboolean _bt_get_discoverying_property(void)
1062 GHashTable *hash = NULL;
1064 gboolean is_discover = FALSE;
1066 proxy = _bt_get_adapter_proxy();
1067 retv_if(proxy == NULL, FALSE);
1069 dbus_g_proxy_call(proxy, "GetProperties", NULL,
1071 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
1072 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
1074 retv_if(hash == NULL, FALSE);
1076 value = g_hash_table_lookup(hash, "Discovering");
1077 is_discover = g_value_get_boolean(value);
1078 g_hash_table_destroy(hash);
1082 int _bt_get_discoverable_timeout_property(void)
1085 GHashTable *hash = NULL;
1087 unsigned int disc_to;
1089 proxy = _bt_get_adapter_proxy();
1090 retv_if(proxy == NULL, 0);
1092 dbus_g_proxy_call(proxy, "GetProperties", NULL,
1094 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
1095 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
1097 retv_if(hash == NULL, 0);
1099 value = g_hash_table_lookup(hash, "DiscoverableTimeout");
1100 disc_to = g_value_get_uint(value);
1101 g_hash_table_destroy(hash);
1105 int _bt_get_bonded_devices(GArray **dev_list)
1108 GPtrArray *gp_array = NULL;
1109 GError *error = NULL;
1112 BT_CHECK_PARAMETER(dev_list, return);
1114 proxy = _bt_get_adapter_proxy();
1115 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1117 dbus_g_proxy_call(proxy, "ListDevices", &error,
1118 G_TYPE_INVALID, dbus_g_type_get_collection("GPtrArray",
1119 DBUS_TYPE_G_OBJECT_PATH), &gp_array, G_TYPE_INVALID);
1121 if (error != NULL) {
1122 BT_ERR("ListDevices error: [%s]\n", error->message);
1123 g_error_free(error);
1124 return BLUETOOTH_ERROR_INTERNAL;
1127 retv_if(gp_array == NULL, BLUETOOTH_ERROR_NONE);
1128 retv_if(gp_array->len == 0, BLUETOOTH_ERROR_NONE);
1130 for (i = 0; i < gp_array->len; i++) {
1131 bluetooth_device_info_t dev_info;
1132 gchar *gp_path = g_ptr_array_index(gp_array, i);
1134 if (gp_path == NULL)
1137 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1139 if (__bt_get_bonded_device_info(gp_path,
1140 &dev_info) == BLUETOOTH_ERROR_NONE) {
1142 g_array_append_vals(*dev_list, &dev_info,
1143 sizeof(bluetooth_device_info_t));
1145 BT_ERR("Can't get the paired device path \n");
1150 g_ptr_array_free(gp_array, TRUE);
1151 return BLUETOOTH_ERROR_NONE;
1154 int _bt_get_bonded_device_info(bluetooth_device_address_t *device_address,
1155 bluetooth_device_info_t *dev_info)
1157 char *object_path = NULL;
1158 DBusGProxy *adapter_proxy;
1159 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1161 BT_CHECK_PARAMETER(device_address, return);
1162 BT_CHECK_PARAMETER(dev_info, return);
1164 adapter_proxy = _bt_get_adapter_proxy();
1165 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1167 _bt_convert_addr_type_to_string(address, device_address->addr);
1169 dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
1170 G_TYPE_STRING, address, G_TYPE_INVALID,
1171 DBUS_TYPE_G_OBJECT_PATH, &object_path,
1174 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_FOUND);
1176 if (__bt_get_bonded_device_info(object_path,
1177 dev_info) != BLUETOOTH_ERROR_NONE) {
1178 BT_ERR("Can't get the paired device path \n");
1179 g_free(object_path);
1180 return BLUETOOTH_ERROR_INTERNAL;
1182 g_free(object_path);
1183 return BLUETOOTH_ERROR_NONE;
1186 int _bt_get_timeout_value(int *timeout)
1188 time_t current_time;
1191 /* Take current time */
1192 time(¤t_time);
1193 time_diff = difftime(current_time, visible_timer.start_time);
1195 BT_DBG("Time diff = %d\n", time_diff);
1197 *timeout = visible_timer.timeout - time_diff;
1199 return BLUETOOTH_ERROR_NONE;