4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 #include <dbus/dbus-glib.h>
25 #include <dbus/dbus.h>
29 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
30 #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-device.h"
39 #include "bt-service-rfcomm-client.h"
40 #include "bt-service-util.h"
41 #include "bt-service-agent.h"
42 #include "bt-service-network.h"
43 #include "bt-service-adapter.h"
45 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
46 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
47 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
49 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
50 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
51 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
52 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
53 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
54 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
61 DBusGProxy *device_proxy;
62 DBusGProxy *adapter_proxy;
64 unsigned short conn_type;
67 gboolean is_device_creating;
68 bt_funcion_data_t *bonding_info;
69 bt_funcion_data_t *searching_info;
71 /* This HID Mouse does not support pairing precedure. need to skip it. */
72 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
74 static void __bt_bond_device_cb(DBusGProxy *proxy, DBusGProxyCall *call,
77 static int __bt_retry_bond(void);
80 static void __bt_decline_pair_request()
84 request_info_t *req_info;
85 bluetooth_device_info_t dev_info;
86 bt_remote_dev_info_t *remote_dev_info;
90 req_info = _bt_get_request_info(bonding_info->req_id);
91 if (req_info == NULL) {
92 BT_ERR("req_info == NULL");
95 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
97 BT_DBG("bonding_info is NULL");
104 /* Send the event to application */
105 if (remote_dev_info != NULL) {
106 _bt_send_event(BT_ADAPTER_EVENT,
107 BLUETOOTH_EVENT_BONDING_FINISHED,
108 DBUS_TYPE_INT32, &bonding_info->result,
109 DBUS_TYPE_STRING, &bonding_info->addr,
110 DBUS_TYPE_UINT32, &remote_dev_info->class,
111 DBUS_TYPE_INT16, &remote_dev_info->rssi,
112 DBUS_TYPE_STRING, &remote_dev_info->name,
113 DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
114 DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
115 DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
116 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
117 &remote_dev_info->uuids, remote_dev_info->uuid_count,
118 DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
119 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
120 &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
123 _bt_free_device_info(remote_dev_info);
126 if (req_info->context == NULL)
129 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
130 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
132 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
133 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
136 g_array_append_vals(out_param1, &dev_info,
137 sizeof(bluetooth_device_info_t));
138 g_array_append_vals(out_param2, &bonding_info->result, sizeof(int));
140 dbus_g_method_return(req_info->context, out_param1, out_param2);
142 g_array_free(out_param1, TRUE);
143 g_array_free(out_param2, TRUE);
145 _bt_delete_request_list(req_info->req_id);
148 g_free(bonding_info->addr);
149 g_free(bonding_info);
155 #ifdef TIZEN_WEARABLE
156 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
160 retv_if(user_data == NULL, FALSE);
162 b = (bundle *)user_data;
164 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
165 ret = syspopup_launch("bt-syspopup", b);
168 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
170 BT_DBG("Hurray!!! Finally Popup launched");
173 return (ret < 0) ? TRUE : FALSE;
176 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
181 DBusGConnection *conn;
183 conn = _bt_get_system_gconn();
191 bundle_add(b, "event-type", "unable-to-pairing");
193 if (result == BLUETOOTH_ERROR_TIMEOUT )
194 bundle_add(b, "error", "timeout");
195 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
196 bundle_add(b, "error", "authfailed");
198 bundle_add(b, "error", "error");
200 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
201 ret = syspopup_launch("bt-syspopup", b);
204 BT_ERR("Popup launch failed...retry %d \n", ret);
205 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
216 gboolean _bt_is_device_creating(void)
218 return is_device_creating;
221 gboolean _bt_is_bonding_device_address(const char *address)
223 if (bonding_info == NULL || bonding_info->addr == NULL)
226 if (g_strcmp0(bonding_info->addr, address) == 0) {
227 BT_DBG("[%s] is bonding device", address);
231 BT_DBG("[%s] is NOT bonding device", address);
235 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
237 ret_if(bonding_info == NULL);
238 bonding_info->is_autopair = is_autopair;
241 void _bt_device_path_to_address(const char *device_path,
242 char *device_address)
244 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
248 ret_if(device_path == NULL);
249 ret_if(device_address == NULL);
251 dev_addr = strstr(device_path, "dev_");
252 ret_if(dev_addr == NULL);
255 g_strlcpy(address, dev_addr, sizeof(address));
257 while ((pos = strchr(address, '_')) != NULL) {
261 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
264 void __bt_cancel_search_service_done(void)
266 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
267 request_info_t *req_info;
268 bluetooth_device_info_t dev_info;
272 ret_if(searching_info == NULL);
274 req_info = _bt_get_request_info(searching_info->req_id);
275 if (req_info == NULL) {
276 BT_ERR("req_info == NULL");
280 if (req_info->context == NULL)
283 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
284 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
286 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
287 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
288 searching_info->addr);
290 g_array_append_vals(out_param1, &dev_info,
291 sizeof(bluetooth_device_info_t));
292 g_array_append_vals(out_param2, &result, sizeof(int));
294 dbus_g_method_return(req_info->context, out_param1, out_param2);
296 g_array_free(out_param1, TRUE);
297 g_array_free(out_param2, TRUE);
299 _bt_delete_request_list(req_info->req_id);
303 g_free(searching_info->addr);
304 g_free(searching_info);
305 searching_info = NULL;
308 static void __bt_get_uuids(GValue *value, bt_remote_dev_info_t *info)
313 ret_if(value == NULL);
314 ret_if(info == NULL);
316 info->uuid_count = 0;
318 uuid_value = g_value_get_boxed(value);
319 ret_if(uuid_value == NULL);
321 while (uuid_value[i]) {
326 info->uuid_count = i;
328 info->uuids = g_new0(char *, info->uuid_count + 1);
330 for (i = 0; uuid_value[i] != NULL; i++) {
331 info->uuids[i] = g_strdup(uuid_value[i]);
335 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
337 bt_remote_dev_info_t *dev_info;
338 char *object_path = NULL;
339 DBusGProxy *adapter_proxy;
340 DBusGProxy *device_proxy;
341 GHashTable *hash = NULL;
344 GByteArray *manufacturer_data = NULL;
345 DBusGConnection *conn;
347 retv_if(address == NULL, NULL);
349 adapter_proxy = _bt_get_adapter_proxy();
350 retv_if(adapter_proxy == NULL, NULL);
352 object_path = _bt_get_device_object_path(address);
354 retv_if(object_path == NULL, NULL);
356 conn = _bt_get_system_gconn();
358 BT_ERR("conn == NULL");
363 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
364 object_path, BT_PROPERTIES_INTERFACE);
366 retv_if(device_proxy == NULL, NULL);
368 dbus_g_proxy_call(device_proxy, "GetAll", NULL,
369 G_TYPE_STRING, BT_DEVICE_INTERFACE,
371 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
372 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
374 g_object_unref(device_proxy);
376 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
379 value = g_hash_table_lookup(hash, "Alias");
380 name = value ? g_value_get_string(value) : NULL;
383 DBG_SECURE("Alias Name [%s]", name);
385 value = g_hash_table_lookup(hash, "Name");
386 name = value ? g_value_get_string(value) : NULL;
389 value = g_hash_table_lookup(hash, "Class");
390 dev_info->class = value ? g_value_get_uint(value) : 0;
392 value = g_hash_table_lookup(hash, "Connected");
393 dev_info->connected = value ? g_value_get_boolean(value) : FALSE;
395 value = g_hash_table_lookup(hash, "Trusted");
396 dev_info->trust = value ? g_value_get_boolean(value) : FALSE;
398 value = g_hash_table_lookup(hash, "Paired");
399 dev_info->paired = value ? g_value_get_boolean(value) : FALSE;
401 BT_DBG("Paired %d", dev_info->paired );
403 value = g_hash_table_lookup(hash, "RSSI");
404 dev_info->rssi = value ? g_value_get_int(value) : 0;
406 value = g_hash_table_lookup(hash, "LastAddrType");
407 dev_info->addr_type = value ? g_value_get_uchar(value) : 0;
409 value = g_hash_table_lookup(hash, "UUIDs");
410 __bt_get_uuids(value, dev_info);
412 value = g_hash_table_lookup(hash, "ManufacturerDataLen");
413 dev_info->manufacturer_data_len = value ? g_value_get_uint(value) : 0;
414 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
415 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
416 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
419 value = g_hash_table_lookup(hash, "ManufacturerData");
420 manufacturer_data = value ? g_value_get_boxed(value) : NULL;
421 if (manufacturer_data) {
422 if (dev_info->manufacturer_data_len > 0) {
423 BT_DBG("manufacturer_data_len = %d", dev_info->manufacturer_data_len);
424 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
425 memcpy(dev_info->manufacturer_data, manufacturer_data->data, dev_info->manufacturer_data_len);
429 dev_info->address = g_strdup(address);
430 dev_info->name = g_strdup(name);
432 g_hash_table_destroy(hash);
434 BT_ERR("Hash is NULL\n");
442 static gboolean __ignore_auto_pairing_request(const char *address)
447 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
458 /* Get the LAP(Lower Address part) */
459 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
461 snprintf(lap_address, sizeof(lap_address), ",%s", address);
463 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
466 BT_ERR("fopen failed \n");
470 fseek(fp, 0, SEEK_END);
475 BT_ERR("Get file size failed \n");
480 buffer = g_malloc0(sizeof(char) * size);
481 result = fread((char *)buffer, 1, size, fp);
483 if (result != size) {
484 BT_ERR("Read Error\n");
489 BT_DBG("Buffer = %s\n", buffer);
491 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
497 /* Write the data and insert new device data */
498 for (i = 0; lines[i] != NULL; i++) {
499 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
500 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
502 lines[i] = temp_buffer;
505 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
508 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
511 BT_ERR("fopen failed \n");
516 BT_DBG("Buffer = %s\n", buffer);
517 fwrite(buffer, 1, strlen(buffer), fp);
527 static int __bt_retry_bond(void)
529 BT_CHECK_PARAMETER(bonding_info, return);
530 BT_CHECK_PARAMETER(bonding_info->addr, return);
532 if (!dbus_g_proxy_begin_call_with_timeout(bonding_info->device_proxy,
534 (DBusGProxyCallNotify) __bt_bond_device_cb,
535 NULL, NULL, BT_MAX_DBUS_TIMEOUT,
536 G_TYPE_UCHAR, bonding_info->conn_type,
538 BT_ERR("RePair call fail");
540 g_object_unref(bonding_info->device_proxy);
545 return BLUETOOTH_ERROR_NONE;
548 __bt_decline_pair_request();
549 is_device_creating = FALSE;
551 return BLUETOOTH_ERROR_INTERNAL;
555 static int __bt_remove_and_bond(void)
557 DBusGProxy *adapter_proxy;
559 char *device_path = NULL;
561 BT_CHECK_PARAMETER(bonding_info, return);
562 BT_CHECK_PARAMETER(bonding_info->addr, return);
564 adapter_proxy = _bt_get_adapter_proxy();
565 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
567 dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
568 G_TYPE_STRING, bonding_info->addr,
569 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
573 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
575 dbus_g_proxy_call(adapter_proxy, "UnpairDevice",
576 &err, DBUS_TYPE_G_OBJECT_PATH, device_path,
577 G_TYPE_INVALID, G_TYPE_INVALID);
580 BT_ERR("UnpairDevice Fail: %s", err->message);
582 return BLUETOOTH_ERROR_INTERNAL;
585 return __bt_retry_bond();
588 static int __bt_cancel_and_bond(void)
590 int ret = BLUETOOTH_ERROR_NONE;
592 ret = _bt_agent_reply_cancellation();
593 if (ret != BLUETOOTH_ERROR_NONE){
594 BT_ERR("Fail to call reply cancellation");
598 return __bt_retry_bond();
602 static void __bt_bond_device_cb(DBusGProxy *proxy, DBusGProxyCall *call,
605 int result = BLUETOOTH_ERROR_NONE;
609 request_info_t *req_info;
610 bluetooth_device_info_t dev_info;
611 bt_remote_dev_info_t *remote_dev_info;
613 /* Terminate ALL system popup */
614 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
615 //syspopup_destroy_all();
618 dbus_g_proxy_end_call(proxy, call, &err, G_TYPE_INVALID);
620 g_object_unref(proxy);
622 is_device_creating = FALSE;
624 if (bonding_info == NULL) {
626 BT_ERR("bonding_info == NULL");
632 bonding_info->device_proxy = NULL;
634 req_info = _bt_get_request_info(bonding_info->req_id);
635 if (req_info == NULL) {
636 BT_ERR("req_info == NULL");
641 BT_ERR("Error occured in Pair [%s]", err->message);
643 if (!strcmp(err->message, "Already Exists")) {
644 BT_INFO("Existing Bond, remove and retry");
645 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
647 result = BLUETOOTH_ERROR_PARING_FAILED;
648 // } else if (_bt_agent_is_canceled() ||
649 // !strcmp(err->message, "Authentication Canceled")) {
650 // result = BLUETOOTH_ERROR_CANCEL_BY_USER;
651 } else if (!strcmp(err->message, "Authentication Rejected")) {
652 result = BLUETOOTH_ERROR_ACCESS_DENIED;
653 } else if (!strcmp(err->message, "In Progress")) {
654 BT_INFO("Bond in progress, cancel and retry");
655 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
657 result = BLUETOOTH_ERROR_PARING_FAILED;
658 } else if (!strcmp(err->message, "Authentication Failed")) {
659 if (bonding_info->is_autopair == TRUE) {
660 _bt_set_autopair_status_in_bonding_info(FALSE);
661 __ignore_auto_pairing_request(bonding_info->addr);
663 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
664 } else if (!strcmp(err->message, "Page Timeout")) {
665 /* This is the special case
666 As soon as call bluetooth_bond_device, try to cancel bonding.
667 In this case, before completing to call 'CreatePairedDevice' method
668 the procedure is stopped. So 'Cancle' error is not return.
670 result = BLUETOOTH_ERROR_HOST_DOWN;
671 } else if (!strcmp(err->message, BT_TIMEOUT_MESSAGE)) {
672 dbus_g_proxy_call(proxy, "CancelDeviceCreation", NULL,
673 G_TYPE_STRING, bonding_info->addr,
674 G_TYPE_INVALID, G_TYPE_INVALID);
676 result = BLUETOOTH_ERROR_INTERNAL;
677 } else if (!strcmp(err->message, "Connection Timeout")) {
678 BT_INFO("pairing request timeout");
679 /* Pairing request timeout */
680 result = BLUETOOTH_ERROR_TIMEOUT;
681 } else if (!strcmp(err->message, "Authentication Timeout")) {
682 /* Pairing request timeout */
683 result = BLUETOOTH_ERROR_TIMEOUT;
685 BT_DBG("Default case");
686 result = BLUETOOTH_ERROR_PARING_FAILED;
690 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
691 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
692 result == BLUETOOTH_ERROR_TIMEOUT ||
693 result == BLUETOOTH_ERROR_HOST_DOWN) {
695 BT_INFO("result error %d", result);
696 bonding_info->result = result;
697 #ifdef TIZEN_WEARABLE
698 __bt_launch_unable_to_pairing_syspopup(result);
702 g_object_unref(proxy);
703 bonding_info->device_proxy = NULL;
705 if (result != BLUETOOTH_ERROR_NONE)
708 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
710 /* Send the event to application */
711 if (remote_dev_info != NULL) {
712 _bt_send_event(BT_ADAPTER_EVENT,
713 BLUETOOTH_EVENT_BONDING_FINISHED,
714 DBUS_TYPE_INT32, &result,
715 DBUS_TYPE_STRING, &bonding_info->addr,
716 DBUS_TYPE_UINT32, &remote_dev_info->class,
717 DBUS_TYPE_INT16, &remote_dev_info->rssi,
718 DBUS_TYPE_STRING, &remote_dev_info->name,
719 DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
720 DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
721 DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
722 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
723 &remote_dev_info->uuids, remote_dev_info->uuid_count,
724 DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
725 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
726 &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
729 _bt_free_device_info(remote_dev_info);
734 if (req_info->context == NULL)
737 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
738 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
740 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
741 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
745 if (_bt_adapter_get_status() != BT_ACTIVATED)
746 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
749 g_array_append_vals(out_param1, &dev_info,
750 sizeof(bluetooth_device_info_t));
751 g_array_append_vals(out_param2, &result, sizeof(int));
753 dbus_g_method_return(req_info->context, out_param1, out_param2);
755 g_array_free(out_param1, TRUE);
756 g_array_free(out_param2, TRUE);
758 _bt_delete_request_list(req_info->req_id);
765 _bt_agent_set_canceled(FALSE);
768 g_free(bonding_info->addr);
769 g_free(bonding_info);
773 int _bt_bond_device(int request_id,
774 bluetooth_device_address_t *device_address,
775 unsigned short conn_type, GArray **out_param1)
778 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
779 bluetooth_device_info_t dev_info;
781 DBusGConnection *conn;
782 char *device_path = NULL;
783 DBusGProxy *adapter_proxy;
784 GError *error = NULL;
786 BT_CHECK_PARAMETER(device_address, return);
789 BT_ERR("Bonding in progress");
791 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
792 memcpy(dev_info.device_address.addr, device_address->addr,
793 BLUETOOTH_ADDRESS_LENGTH);
795 g_array_append_vals(*out_param1, &dev_info,
796 sizeof(bluetooth_device_info_t));
798 return BLUETOOTH_ERROR_DEVICE_BUSY;
801 conn = _bt_get_system_gconn();
802 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
804 _bt_convert_addr_type_to_string(address, device_address->addr);
806 device_path = _bt_get_device_object_path(address);
808 if (device_path == NULL) {
809 BT_ERR("No searched device");
811 adapter_proxy = _bt_get_adapter_proxy();
812 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
814 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
815 G_TYPE_STRING, address,
816 G_TYPE_INVALID, G_TYPE_INVALID);
819 BT_ERR("CreateDevice Fail: %s", error->message);
823 device_path = _bt_get_device_object_path(address);
824 if (device_path == NULL) {
825 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
826 memcpy(dev_info.device_address.addr, device_address->addr,
827 BLUETOOTH_ADDRESS_LENGTH);
829 g_array_append_vals(*out_param1, &dev_info,
830 sizeof(bluetooth_device_info_t));
832 return BLUETOOTH_ERROR_NOT_PAIRED;
834 BT_INFO("device_path is created[%s]", device_path);
838 proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
839 device_path, BT_DEVICE_INTERFACE);
842 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
844 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
845 bonding_info->addr = g_strdup(address);
846 bonding_info->req_id = request_id;
848 bonding_info->device_proxy = proxy;
849 bonding_info->conn_type = conn_type;
851 is_device_creating = TRUE;
853 if (!dbus_g_proxy_begin_call_with_timeout(proxy, "Pair",
854 (DBusGProxyCallNotify) __bt_bond_device_cb,
855 NULL, NULL, BT_MAX_DBUS_TIMEOUT,
856 G_TYPE_UCHAR, conn_type,
858 BT_ERR("Pair call fail");
859 g_object_unref(proxy);
860 bonding_info->device_proxy = NULL;
863 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
865 return BLUETOOTH_ERROR_NONE;
867 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
868 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
871 g_array_append_vals(*out_param1, &dev_info,
872 sizeof(bluetooth_device_info_t));
874 is_device_creating = FALSE;
876 g_free(bonding_info->addr);
877 g_free(bonding_info);
880 return BLUETOOTH_ERROR_INTERNAL;
883 int _bt_cancel_bonding(void)
885 int ret = BLUETOOTH_ERROR_NONE;
887 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
889 ret = _bt_agent_reply_cancellation();
890 if (ret != BLUETOOTH_ERROR_NONE){
891 BT_ERR("Fail to call reply cancellation");
895 _bt_agent_set_canceled(TRUE);
897 return BLUETOOTH_ERROR_NONE;
900 static void __bt_unbond_cb(DBusGProxy *proxy, DBusGProxyCall *call,
906 int result = BLUETOOTH_ERROR_NONE;
907 bt_funcion_data_t *unbonding_info;
908 bluetooth_device_info_t dev_info;
909 request_info_t *req_info;
911 dbus_g_proxy_end_call(proxy, call, &err, G_TYPE_INVALID);
913 unbonding_info = user_data;
915 if (unbonding_info == NULL) {
917 BT_ERR("unbonding_info == NULL");
921 req_info = _bt_get_request_info(unbonding_info->req_id);
922 if (req_info == NULL) {
923 BT_ERR("req_info == NULL");
928 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
929 result = BLUETOOTH_ERROR_INTERNAL;
932 if (req_info->context == NULL)
935 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
936 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
938 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
939 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
940 unbonding_info->addr);
942 g_array_append_vals(out_param1, &dev_info,
943 sizeof(bluetooth_device_info_t));
944 g_array_append_vals(out_param2, &result, sizeof(int));
946 dbus_g_method_return(req_info->context, out_param1, out_param2);
948 _bt_delete_request_list(req_info->req_id);
950 g_array_free(out_param1, TRUE);
951 g_array_free(out_param2, TRUE);
957 if (unbonding_info) {
958 g_free(unbonding_info->addr);
959 g_free(unbonding_info);
963 int _bt_unbond_device(int request_id,
964 bluetooth_device_address_t *device_address,
967 char *device_path = NULL;
968 bt_funcion_data_t *unbonding_info;
969 DBusGProxy *adapter_proxy = NULL;
970 DBusGProxy *device_proxy = NULL;
971 DBusGConnection *conn;
972 int result = BLUETOOTH_ERROR_INTERNAL;
973 bluetooth_device_info_t dev_info;
974 GValue paired = { 0 };
975 GError *error = NULL;
977 BT_CHECK_PARAMETER(device_address, return);
979 adapter_proxy = _bt_get_adapter_proxy();
980 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
982 /* allocate user data so that it can be retrieved in callback */
983 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
984 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
985 unbonding_info->req_id = request_id;
987 _bt_convert_addr_type_to_string(unbonding_info->addr,
988 device_address->addr);
990 device_path = _bt_get_device_object_path(unbonding_info->addr);
992 if (device_path == NULL) {
993 BT_ERR("No paired device");
994 result = BLUETOOTH_ERROR_NOT_PAIRED;
998 conn = _bt_get_system_gconn();
1000 BT_ERR("conn is NULL");
1001 result = BLUETOOTH_ERROR_INTERNAL;
1005 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1006 device_path, BT_PROPERTIES_INTERFACE);
1007 if (device_proxy != NULL) {
1008 if (!dbus_g_proxy_call(device_proxy, "Get", &error,
1009 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1010 G_TYPE_STRING, "Paired",
1012 G_TYPE_VALUE, &paired,
1014 if (error != NULL) {
1015 BT_ERR("Getting property failed: [%s]\n", error->message);
1016 g_error_free(error);
1019 if (g_value_get_boolean(&paired) == FALSE) {
1020 BT_ERR("No paired device");
1021 g_object_unref(device_proxy);
1022 result = BLUETOOTH_ERROR_NOT_PAIRED;
1026 g_object_unref(device_proxy);
1029 if (!dbus_g_proxy_begin_call(adapter_proxy, "UnpairDevice",
1030 (DBusGProxyCallNotify) __bt_unbond_cb,
1031 (gpointer)unbonding_info, NULL,
1032 DBUS_TYPE_G_OBJECT_PATH, device_path,
1034 BT_ERR("RemoveBonding begin failed\n");
1037 g_free(device_path);
1038 return BLUETOOTH_ERROR_NONE;
1041 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1042 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1043 unbonding_info->addr);
1045 g_array_append_vals(*out_param1, &dev_info,
1046 sizeof(bluetooth_device_info_t));
1047 g_free(device_path);
1048 g_free(unbonding_info->addr);
1049 g_free(unbonding_info);
1053 static void __bt_discover_cb(DBusGProxy *proxy, DBusGProxyCall *call,
1057 GHashTable *hash = NULL;
1060 int result = BLUETOOTH_ERROR_NONE;
1061 bluetooth_device_info_t dev_info;
1062 bt_remote_dev_info_t *remote_dev_info;
1063 request_info_t *req_info;
1065 dbus_g_proxy_end_call(proxy, call, &err,
1066 dbus_g_type_get_map("GHashTable", G_TYPE_UINT, G_TYPE_STRING), &hash,
1069 g_object_unref(proxy);
1071 if (searching_info == NULL) {
1073 BT_ERR("unbonding_info == NULL");
1077 req_info = _bt_get_request_info(searching_info->req_id);
1078 if (req_info == NULL) {
1079 BT_ERR("req_info == NULL");
1084 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1086 if (!strcmp("Operation canceled", err->message)) {
1087 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1088 } else if (!strcmp("In Progress", err->message)) {
1089 result = BLUETOOTH_ERROR_IN_PROGRESS;
1090 } else if (!strcmp("Host is down", err->message)) {
1091 result = BLUETOOTH_ERROR_HOST_DOWN;
1093 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1096 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1097 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1098 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1099 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1100 remote_dev_info->uuid_count > 0) {
1101 result = BLUETOOTH_ERROR_NONE;
1104 _bt_free_device_info(remote_dev_info);
1109 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1112 /* Send the event to application */
1113 if (remote_dev_info != NULL) {
1114 _bt_send_event(BT_ADAPTER_EVENT,
1115 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1116 DBUS_TYPE_INT32, &result,
1117 DBUS_TYPE_STRING, &searching_info->addr,
1118 DBUS_TYPE_UINT32, &remote_dev_info->class,
1119 DBUS_TYPE_INT16, &remote_dev_info->rssi,
1120 DBUS_TYPE_STRING, &remote_dev_info->name,
1121 DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
1122 DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
1123 DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
1124 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1125 &remote_dev_info->uuids, remote_dev_info->uuid_count,
1126 DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
1127 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
1128 &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1131 _bt_free_device_info(remote_dev_info);
1135 if (req_info->context == NULL)
1138 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
1139 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
1141 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1142 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1143 searching_info->addr);
1145 g_array_append_vals(out_param1, &dev_info,
1146 sizeof(bluetooth_device_info_t));
1147 g_array_append_vals(out_param2, &result, sizeof(int));
1149 dbus_g_method_return(req_info->context, out_param1, out_param2);
1151 g_array_free(out_param1, TRUE);
1152 g_array_free(out_param2, TRUE);
1154 _bt_delete_request_list(req_info->req_id);
1159 g_hash_table_destroy(hash);
1161 if (searching_info) {
1162 g_free(searching_info->addr);
1163 g_free(searching_info);
1164 searching_info = NULL;
1168 int _bt_search_device(int request_id,
1169 bluetooth_device_address_t *device_address)
1171 char *device_path = NULL;
1172 DBusGProxy *device_proxy = NULL;
1173 DBusGConnection *conn;
1175 DBusGProxy *adapter_proxy;
1176 int result = BLUETOOTH_ERROR_INTERNAL;
1178 BT_CHECK_PARAMETER(device_address, return);
1180 if (searching_info) {
1181 BT_ERR("Service searching in progress");
1182 return BLUETOOTH_ERROR_DEVICE_BUSY;
1185 adapter_proxy = _bt_get_adapter_proxy();
1186 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1188 /* allocate user data so that it can be retrieved in callback */
1189 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1190 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1191 searching_info->req_id = request_id;
1193 _bt_convert_addr_type_to_string(searching_info->addr,
1194 device_address->addr);
1196 conn = _bt_get_system_gconn();
1197 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1200 device_path = _bt_get_device_object_path(searching_info->addr);
1202 if (device_path == NULL) {
1203 BT_ERR("No paired device");
1204 result = BLUETOOTH_ERROR_NOT_PAIRED;
1208 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1209 device_path, BT_PROPERTIES_INTERFACE);
1210 g_free(device_path);
1211 if (device_proxy == NULL) {
1212 result = BLUETOOTH_ERROR_INTERNAL;
1216 if (!dbus_g_proxy_begin_call(device_proxy, "Get",
1217 (DBusGProxyCallNotify)__bt_discover_cb,
1218 (gpointer)searching_info, NULL,
1219 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1220 G_TYPE_STRING, "UUIDs",
1222 BT_ERR("DiscoverServices failed");
1226 searching_info->device_proxy = device_proxy;
1228 return BLUETOOTH_ERROR_NONE;
1231 g_object_unref(device_proxy);
1233 g_free(searching_info->addr);
1234 g_free(searching_info);
1235 searching_info = NULL;
1239 int _bt_cancel_search_device(void)
1243 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1245 if (searching_info->device_proxy) {
1246 dbus_g_proxy_call(searching_info->device_proxy,
1249 G_TYPE_INVALID, G_TYPE_INVALID);
1251 __bt_cancel_search_service_done();
1253 return BLUETOOTH_ERROR_NONE;
1256 int _bt_set_alias(bluetooth_device_address_t *device_address,
1259 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1260 gchar *device_path = NULL;
1261 DBusGProxy *adapter_proxy;
1262 DBusGProxy *device_proxy;
1263 GError *error = NULL;
1264 GValue name = { 0 };
1265 DBusGConnection *conn;
1267 BT_CHECK_PARAMETER(device_address, return);
1268 BT_CHECK_PARAMETER(alias, return);
1270 adapter_proxy = _bt_get_adapter_proxy();
1271 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1273 conn = _bt_get_system_gconn();
1274 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1276 _bt_convert_addr_type_to_string(address, device_address->addr);
1278 device_path = _bt_get_device_object_path(address);
1280 if (device_path == NULL) {
1281 BT_ERR("No paired device");
1282 return BLUETOOTH_ERROR_NOT_PAIRED;
1285 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1286 device_path, BT_PROPERTIES_INTERFACE);
1288 g_free(device_path);
1289 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1290 g_value_init(&name, G_TYPE_STRING);
1291 g_value_set_string(&name, alias);
1293 dbus_g_proxy_call(device_proxy, "Set", &error,
1294 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1295 G_TYPE_STRING, "Alias",
1296 G_TYPE_VALUE, &name,
1297 G_TYPE_INVALID, G_TYPE_INVALID);
1299 g_object_unref(device_proxy);
1301 g_value_unset(&name);
1304 BT_ERR("SetProperty error: [%s]", error->message);
1305 g_error_free(error);
1306 return BLUETOOTH_ERROR_INTERNAL;
1309 return BLUETOOTH_ERROR_NONE;
1312 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1315 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1316 gchar *device_path = NULL;
1317 DBusGProxy *device_proxy;
1318 gboolean previous_value;
1319 GError *error = NULL;
1320 GValue trusted = { 0 };
1321 GValue trusted_v = { 0 };
1322 DBusGConnection *conn;
1323 int ret = BLUETOOTH_ERROR_NONE;
1325 BT_CHECK_PARAMETER(device_address, return);
1327 conn = _bt_get_system_gconn();
1328 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1330 _bt_convert_addr_type_to_string(address, device_address->addr);
1332 device_path = _bt_get_device_object_path(address);
1334 if (device_path == NULL) {
1335 BT_ERR("No paired device");
1336 return BLUETOOTH_ERROR_NOT_PAIRED;
1339 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1340 device_path, BT_PROPERTIES_INTERFACE);
1341 g_free(device_path);
1342 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1344 if (!dbus_g_proxy_call(device_proxy, "Get", &error,
1345 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1346 G_TYPE_STRING, "Trusted",
1348 G_TYPE_VALUE, &trusted_v,
1350 if (error != NULL) {
1351 BT_ERR("Getting property failed: [%s]\n", error->message);
1352 g_error_free(error);
1354 g_object_unref(device_proxy);
1355 return BLUETOOTH_ERROR_INTERNAL;
1358 previous_value = g_value_get_boolean(&trusted_v);
1360 /* If the input is same with previous value, return error. */
1361 if (previous_value == authorize) {
1362 BT_ERR("Same value: %d", previous_value);
1363 g_object_unref(device_proxy);
1364 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1368 g_value_init(&trusted, G_TYPE_BOOLEAN);
1369 g_value_set_boolean(&trusted, authorize);
1371 dbus_g_proxy_call(device_proxy, "Set", &error,
1372 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1373 G_TYPE_STRING, "Trusted",
1374 G_TYPE_VALUE, &trusted,
1375 G_TYPE_INVALID, G_TYPE_INVALID);
1377 g_object_unref(device_proxy);
1378 g_value_unset(&trusted);
1381 BT_ERR("SetProperty error: [%s]", error->message);
1382 g_error_free(error);
1383 ret = BLUETOOTH_ERROR_INTERNAL;
1389 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1390 gboolean *is_connected)
1392 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1393 char *object_path = NULL;
1395 DBusGProxy *device_proxy;
1396 GError *error = NULL;
1398 GHashTable *hash = NULL;
1399 DBusGConnection *conn;
1400 int ret = BLUETOOTH_ERROR_NONE;
1402 BT_CHECK_PARAMETER(device_address, return);
1404 conn = _bt_get_system_gconn();
1405 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1407 _bt_convert_addr_type_to_string(address, device_address->addr);
1409 object_path = _bt_get_device_object_path(address);
1410 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1412 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1413 object_path, BT_PROPERTIES_INTERFACE);
1414 g_free(object_path);
1415 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1417 dbus_g_proxy_call(device_proxy, "GetAll", &error,
1418 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1420 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
1421 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
1423 if (error != NULL) {
1424 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1425 g_error_free(error);
1426 g_object_unref(device_proxy);
1427 return BLUETOOTH_ERROR_INTERNAL;
1431 g_object_unref(device_proxy);
1432 return BLUETOOTH_ERROR_INTERNAL;
1435 value = g_hash_table_lookup(hash, "GattConnected");
1436 *is_connected = g_value_get_boolean(value);
1438 BT_DBG("gatt is connected : %d", *is_connected);
1440 g_hash_table_destroy(hash);
1441 g_object_unref(device_proxy);
1446 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1447 int connection_type, gboolean *is_connected)
1449 char *object_path = NULL;
1450 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1451 DBusGProxy *device_proxy = NULL;
1452 DBusGProxy *adapter_proxy = NULL;
1453 DBusGConnection *conn;
1454 GError *error = NULL;
1455 GHashTable *hash = NULL;
1456 GValue *value = NULL;
1458 dbus_bool_t val = FALSE;
1461 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1462 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1464 *is_connected = FALSE;
1466 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1467 return _bt_rfcomm_is_device_connected(device_address,
1469 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1470 return _bt_is_gatt_connected(device_address, is_connected);
1472 adapter_proxy = _bt_get_adapter_proxy();
1473 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1475 conn = _bt_get_system_gconn();
1476 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1478 _bt_convert_addr_type_to_string(address, device_address->addr);
1480 if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1481 object_path = _bt_get_adapter_path();
1482 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1483 object_path, BT_NETWORK_SERVER_INTERFACE);
1484 g_free(object_path);
1485 if (device_proxy == NULL) {
1486 BT_DBG("Device don't have this service");
1487 return BLUETOOTH_ERROR_INTERNAL;
1489 dbus_g_proxy_call(device_proxy, "GetProperties", NULL,
1490 G_TYPE_STRING, address,
1492 dbus_g_type_get_map("GHashTable",
1493 G_TYPE_STRING, G_TYPE_VALUE),
1494 &hash, G_TYPE_INVALID);
1496 value = g_hash_table_lookup(hash, "Connected");
1497 *is_connected = value ? g_value_get_boolean(value) : FALSE;
1498 g_hash_table_destroy(hash);
1500 } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1501 return _bt_is_network_connected(_bt_get_net_conn(),
1502 device_address->addr, is_connected);
1504 uuid = _bt_get_profile_uuid128(connection_type);
1506 BT_ERR("uuid is NULL");
1507 return BLUETOOTH_ERROR_INTERNAL;
1510 BT_DBG("uuid: %s", uuid);
1512 object_path = _bt_get_device_object_path(address);
1513 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1515 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1516 object_path, BT_DEVICE_INTERFACE);
1517 g_free(object_path);
1518 if (device_proxy == NULL) {
1519 BT_DBG("Device don't have this service");
1521 return BLUETOOTH_ERROR_INTERNAL;
1523 dbus_g_proxy_call(device_proxy, "IsConnectedProfile", &error,
1524 G_TYPE_STRING, uuid,
1526 G_TYPE_BOOLEAN, &val,
1529 if (error != NULL) {
1530 BT_ERR("Failed to get properties: %s\n", error->message);
1531 g_error_free(error);
1534 *is_connected = val;
1538 g_object_unref(device_proxy);
1539 return BLUETOOTH_ERROR_NONE;
1542 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1544 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1545 gchar *device_path = NULL;
1546 GError *error = NULL;
1547 DBusGProxy *device_proxy = NULL;
1548 DBusGProxy *adapter_proxy;
1549 DBusGConnection *conn;
1550 int ret = BLUETOOTH_ERROR_NONE;
1552 BT_CHECK_PARAMETER(bd_addr, return);
1554 _bt_convert_addr_type_to_string(device_address,
1555 (unsigned char *)bd_addr->addr);
1557 conn = _bt_get_system_gconn();
1558 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1560 adapter_proxy = _bt_get_adapter_proxy();
1561 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1563 device_path = _bt_get_device_object_path(device_address);
1564 if (device_path == NULL) {
1565 BT_DBG("device_path NULL");
1566 ret = BLUETOOTH_ERROR_INTERNAL;
1570 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1572 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1573 device_path, BT_DEVICE_INTERFACE);
1574 g_free(device_path);
1575 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1577 dbus_g_proxy_call(device_proxy, "ConnectLE", &error,
1578 G_TYPE_BOOLEAN, auto_connect, G_TYPE_INVALID,
1581 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1582 g_error_free(error);
1583 g_object_unref(device_proxy);
1584 return BLUETOOTH_ERROR_INTERNAL;
1587 g_object_unref(device_proxy);
1592 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1594 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1595 gchar *device_path = NULL;
1596 GError *error = NULL;
1597 DBusGProxy *device_proxy = NULL;
1598 DBusGProxy *adapter_proxy;
1599 DBusGConnection *conn;
1600 int ret = BLUETOOTH_ERROR_NONE;
1602 BT_CHECK_PARAMETER(bd_addr, return);
1604 _bt_convert_addr_type_to_string(device_address,
1605 (unsigned char *)bd_addr->addr);
1607 conn = _bt_get_system_gconn();
1608 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1610 adapter_proxy = _bt_get_adapter_proxy();
1611 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1613 device_path = _bt_get_device_object_path(device_address);
1614 if (device_path == NULL) {
1615 BT_DBG("device_path NULL");
1616 ret = BLUETOOTH_ERROR_INTERNAL;
1620 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1622 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1623 device_path, BT_DEVICE_INTERFACE);
1624 g_free(device_path);
1625 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1627 dbus_g_proxy_call(device_proxy, "DisconnectLE", &error, G_TYPE_INVALID, G_TYPE_INVALID);
1629 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1630 g_error_free(error);
1631 g_object_unref(device_proxy);
1632 return BLUETOOTH_ERROR_INTERNAL;
1635 g_object_unref(device_proxy);
1640 int _bt_connect_profile(char *address, char *uuid,
1641 void *cb, gpointer func_data)
1645 DBusGConnection *conn;
1646 DBusGProxy *adapter_proxy;
1647 GError *error = NULL;
1649 conn = _bt_get_system_gconn();
1650 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1652 object_path = _bt_get_device_object_path(address);
1653 if (object_path == NULL) {
1654 BT_ERR("No searched device");
1656 adapter_proxy = _bt_get_adapter_proxy();
1657 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1659 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
1660 G_TYPE_STRING, address,
1661 G_TYPE_INVALID, G_TYPE_INVALID);
1663 if (error != NULL) {
1664 BT_ERR("CreateDevice Fail: %s", error->message);
1665 g_error_free(error);
1668 object_path = _bt_get_device_object_path(address);
1670 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1672 proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1673 object_path, BT_DEVICE_INTERFACE);
1674 g_free(object_path);
1675 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1677 if (!dbus_g_proxy_begin_call(proxy, "ConnectProfile",
1678 (DBusGProxyCallNotify)cb,
1680 G_TYPE_STRING, uuid,
1682 BT_ERR("Connect Dbus Call Error");
1683 g_object_unref(proxy);
1684 return BLUETOOTH_ERROR_INTERNAL;
1686 return BLUETOOTH_ERROR_NONE;
1689 int _bt_disconnect_profile(char *address, char *uuid,
1690 void *cb, gpointer func_data)
1694 DBusGConnection *conn;
1696 conn = _bt_get_system_gconn();
1697 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1699 object_path = _bt_get_device_object_path(address);
1700 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1702 proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1703 object_path, BT_DEVICE_INTERFACE);
1704 g_free(object_path);
1705 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1707 if (!dbus_g_proxy_begin_call(proxy, "DisconnectProfile",
1708 (DBusGProxyCallNotify)cb,
1710 G_TYPE_STRING, uuid,
1712 BT_ERR("Connect Dbus Call Error");
1713 g_object_unref(proxy);
1714 return BLUETOOTH_ERROR_INTERNAL;
1716 return BLUETOOTH_ERROR_NONE;
1719 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1720 int low_threshold, int in_range_threshold, int high_threshold)
1722 int ret = BLUETOOTH_ERROR_NONE;
1724 GError *error = NULL;
1725 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1727 BT_CHECK_PARAMETER(bd_addr, return);
1728 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1729 bd_addr->addr[0], bd_addr->addr[1],
1730 bd_addr->addr[2], bd_addr->addr[3],
1731 bd_addr->addr[4], bd_addr->addr[5],
1733 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1734 in_range_threshold, high_threshold);
1736 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1738 proxy = _bt_get_adapter_proxy();
1739 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1741 if (!dbus_g_proxy_call(proxy, "EnableRssi", &error,
1742 G_TYPE_STRING, address,
1743 G_TYPE_INT, link_type,
1744 G_TYPE_INT, low_threshold,
1745 G_TYPE_INT, in_range_threshold,
1746 G_TYPE_INT, high_threshold,
1747 G_TYPE_INVALID, G_TYPE_INVALID)) {
1748 BT_ERR("Failed to Enable RSSI");
1749 ret = BLUETOOTH_ERROR_INTERNAL;
1750 if (error != NULL) {
1751 BT_ERR("Dbus Call Error:[%s]", error->message);
1752 g_error_free(error);
1753 ret = BLUETOOTH_ERROR_INTERNAL;
1760 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1763 int ret = BLUETOOTH_ERROR_NONE;
1765 GError *error = NULL;
1766 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1768 BT_CHECK_PARAMETER(bd_addr, return);
1769 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1770 bd_addr->addr[0], bd_addr->addr[1],
1771 bd_addr->addr[2], bd_addr->addr[3],
1772 bd_addr->addr[4], bd_addr->addr[5],
1775 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1777 proxy = _bt_get_adapter_proxy();
1778 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1780 if (!dbus_g_proxy_call(proxy, "GetRssiStrength", &error,
1781 G_TYPE_STRING, address,
1782 G_TYPE_INT, link_type,
1783 G_TYPE_INVALID, G_TYPE_INVALID)) {
1784 BT_ERR("Failed to get Raw RSSI");
1785 ret = BLUETOOTH_ERROR_INTERNAL;
1786 if (error != NULL) {
1787 BT_ERR("Dbus Call Error:[%s]", error->message);
1788 g_error_free(error);
1789 ret = BLUETOOTH_ERROR_INTERNAL;
1796 int _bt_le_conn_update(unsigned char *device_address,
1797 guint16 interval_min, guint16 interval_max,
1798 guint16 latency, guint16 time_out)
1800 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1801 gchar *device_path = NULL;
1802 GError *error = NULL;
1803 DBusGProxy *device_proxy = NULL;
1804 DBusGConnection *conn;
1805 guint32 min, max, to;
1806 int ret = BLUETOOTH_ERROR_NONE;
1810 BT_CHECK_PARAMETER(device_address, return);
1812 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1813 interval_min, interval_max, latency, time_out);
1815 if (interval_min > interval_max ||
1816 interval_min < BT_LE_CONN_INTERVAL_MIN ||
1817 interval_max > BT_LE_CONN_INTERVAL_MAX) {
1818 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1822 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
1823 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
1825 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1826 time_out > BT_LE_CONN_SUPER_TO_MAX) {
1827 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1831 to = time_out / BT_LE_CONN_TO_SPLIT;
1833 if (latency > ((to / max) - 1)) {
1834 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1838 _bt_convert_addr_type_to_string(address, device_address);
1840 BT_DBG("Remote device address: %s", address);
1842 device_path = _bt_get_device_object_path(address);
1844 if (device_path == NULL) {
1845 BT_DBG("device_path NULL");
1846 ret = BLUETOOTH_ERROR_INTERNAL;
1850 conn = _bt_get_system_gconn();
1852 BT_DBG("conn NULL");
1853 ret = BLUETOOTH_ERROR_INTERNAL;
1857 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1858 device_path, BT_DEVICE_INTERFACE);
1859 g_free(device_path);
1860 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1862 dbus_g_proxy_call(device_proxy, "LeConnUpdate", &error,
1865 G_TYPE_UINT, latency,
1867 G_TYPE_INVALID, G_TYPE_INVALID);
1869 BT_ERR("LeConnUpdate Call Error %s[%s]",
1870 error->message, address);
1871 g_error_free(error);
1872 g_object_unref(device_proxy);
1873 return BLUETOOTH_ERROR_INTERNAL;
1876 g_object_unref(device_proxy);