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 #include <syspopup_caller.h>
31 #include "bluetooth-api.h"
32 #include "bt-internal-types.h"
34 #include "bt-service-common.h"
35 #include "bt-service-event.h"
36 #include "bt-service-device.h"
37 #include "bt-service-rfcomm-client.h"
38 #include "bt-service-util.h"
39 #include "bt-service-agent.h"
40 #include "bt-service-network.h"
41 #include "bt-service-adapter.h"
43 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
44 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
45 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
47 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
48 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
49 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
50 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
51 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
52 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
59 DBusGProxy *device_proxy;
60 DBusGProxy *adapter_proxy;
62 unsigned short conn_type;
65 gboolean is_device_creating;
66 bt_funcion_data_t *bonding_info;
67 bt_funcion_data_t *searching_info;
69 /* This HID Mouse does not support pairing precedure. need to skip it. */
70 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
72 static void __bt_bond_device_cb(DBusGProxy *proxy, DBusGProxyCall *call,
75 static int __bt_retry_bond(void);
78 static void __bt_decline_pair_request()
82 request_info_t *req_info;
83 bluetooth_device_info_t dev_info;
84 bt_remote_dev_info_t *remote_dev_info;
88 req_info = _bt_get_request_info(bonding_info->req_id);
89 if (req_info == NULL) {
90 BT_ERR("req_info == NULL");
93 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
95 BT_DBG("bonding_info is NULL");
102 /* Send the event to application */
103 if (remote_dev_info != NULL) {
104 _bt_send_event(BT_ADAPTER_EVENT,
105 BLUETOOTH_EVENT_BONDING_FINISHED,
106 DBUS_TYPE_INT32, &bonding_info->result,
107 DBUS_TYPE_STRING, &bonding_info->addr,
108 DBUS_TYPE_UINT32, &remote_dev_info->class,
109 DBUS_TYPE_INT16, &remote_dev_info->rssi,
110 DBUS_TYPE_STRING, &remote_dev_info->name,
111 DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
112 DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
113 DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
114 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
115 &remote_dev_info->uuids, remote_dev_info->uuid_count,
116 DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
117 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
118 &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
121 _bt_free_device_info(remote_dev_info);
124 if (req_info->context == NULL)
127 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
128 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
130 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
131 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
134 g_array_append_vals(out_param1, &dev_info,
135 sizeof(bluetooth_device_info_t));
136 g_array_append_vals(out_param2, &bonding_info->result, sizeof(int));
138 dbus_g_method_return(req_info->context, out_param1, out_param2);
140 g_array_free(out_param1, TRUE);
141 g_array_free(out_param2, TRUE);
143 _bt_delete_request_list(req_info->req_id);
146 g_free(bonding_info->addr);
147 g_free(bonding_info);
153 #ifdef TIZEN_WEARABLE
154 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
158 retv_if(user_data == NULL, FALSE);
160 b = (bundle *)user_data;
162 ret = syspopup_launch("bt-syspopup", b);
164 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
166 BT_DBG("Hurray!!! Finally Popup launched");
169 return (ret < 0) ? TRUE : FALSE;
172 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
177 DBusGConnection *conn;
179 conn = _bt_get_system_gconn();
187 bundle_add(b, "event-type", "unable-to-pairing");
189 if (result == BLUETOOTH_ERROR_TIMEOUT )
190 bundle_add(b, "error", "timeout");
191 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
192 bundle_add(b, "error", "authfailed");
194 bundle_add(b, "error", "error");
196 ret = syspopup_launch("bt-syspopup", b);
198 BT_ERR("Popup launch failed...retry %d \n", ret);
199 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
210 gboolean _bt_is_device_creating(void)
212 return is_device_creating;
215 gboolean _bt_is_bonding_device_address(const char *address)
217 if (bonding_info == NULL || bonding_info->addr == NULL)
220 if (g_strcmp0(bonding_info->addr, address) == 0) {
221 BT_DBG("[%s] is bonding device", address);
225 BT_DBG("[%s] is NOT bonding device", address);
229 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
231 ret_if(bonding_info == NULL);
232 bonding_info->is_autopair = is_autopair;
235 void _bt_device_path_to_address(const char *device_path,
236 char *device_address)
238 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
242 ret_if(device_path == NULL);
243 ret_if(device_address == NULL);
245 dev_addr = strstr(device_path, "dev_");
246 ret_if(dev_addr == NULL);
249 g_strlcpy(address, dev_addr, sizeof(address));
251 while ((pos = strchr(address, '_')) != NULL) {
255 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
258 void __bt_cancel_search_service_done(void)
260 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
261 request_info_t *req_info;
262 bluetooth_device_info_t dev_info;
266 ret_if(searching_info == NULL);
268 req_info = _bt_get_request_info(searching_info->req_id);
269 if (req_info == NULL) {
270 BT_ERR("req_info == NULL");
274 if (req_info->context == NULL)
277 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
278 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
280 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
281 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
282 searching_info->addr);
284 g_array_append_vals(out_param1, &dev_info,
285 sizeof(bluetooth_device_info_t));
286 g_array_append_vals(out_param2, &result, sizeof(int));
288 dbus_g_method_return(req_info->context, out_param1, out_param2);
290 g_array_free(out_param1, TRUE);
291 g_array_free(out_param2, TRUE);
293 _bt_delete_request_list(req_info->req_id);
297 g_free(searching_info->addr);
298 g_free(searching_info);
299 searching_info = NULL;
302 static void __bt_get_uuids(GValue *value, bt_remote_dev_info_t *info)
307 ret_if(value == NULL);
308 ret_if(info == NULL);
310 info->uuid_count = 0;
312 uuid_value = g_value_get_boxed(value);
313 ret_if(uuid_value == NULL);
315 while (uuid_value[i]) {
320 info->uuid_count = i;
322 info->uuids = g_new0(char *, info->uuid_count + 1);
324 for (i = 0; uuid_value[i] != NULL; i++) {
325 info->uuids[i] = g_strdup(uuid_value[i]);
329 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
331 bt_remote_dev_info_t *dev_info;
332 char *object_path = NULL;
333 DBusGProxy *adapter_proxy;
334 DBusGProxy *device_proxy;
335 GHashTable *hash = NULL;
338 GByteArray *manufacturer_data = NULL;
339 DBusGConnection *conn;
341 retv_if(address == NULL, NULL);
343 adapter_proxy = _bt_get_adapter_proxy();
344 retv_if(adapter_proxy == NULL, NULL);
347 object_path = _bt_get_device_object_path(address);
349 retv_if(object_path == NULL, NULL);
351 conn = _bt_get_system_gconn();
353 BT_ERR("conn == NULL");
358 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
359 object_path, BT_PROPERTIES_INTERFACE);
361 retv_if(device_proxy == NULL, NULL);
363 dbus_g_proxy_call(device_proxy, "GetAll", NULL,
364 G_TYPE_STRING, BT_DEVICE_INTERFACE,
366 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
367 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
369 g_object_unref(device_proxy);
371 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
374 value = g_hash_table_lookup(hash, "Alias");
375 name = value ? g_value_get_string(value) : NULL;
378 DBG_SECURE("Alias Name [%s]", name);
380 value = g_hash_table_lookup(hash, "Name");
381 name = value ? g_value_get_string(value) : NULL;
384 value = g_hash_table_lookup(hash, "Class");
385 dev_info->class = value ? g_value_get_uint(value) : 0;
387 value = g_hash_table_lookup(hash, "Connected");
388 dev_info->connected = value ? g_value_get_boolean(value) : FALSE;
390 value = g_hash_table_lookup(hash, "Trusted");
391 dev_info->trust = value ? g_value_get_boolean(value) : FALSE;
393 value = g_hash_table_lookup(hash, "Paired");
394 dev_info->paired = value ? g_value_get_boolean(value) : FALSE;
396 BT_DBG("Paired %d", dev_info->paired );
398 value = g_hash_table_lookup(hash, "RSSI");
399 dev_info->rssi = value ? g_value_get_int(value) : 0;
401 value = g_hash_table_lookup(hash, "LastAddrType");
402 dev_info->addr_type = value ? g_value_get_uchar(value) : 0;
404 value = g_hash_table_lookup(hash, "UUIDs");
405 __bt_get_uuids(value, dev_info);
407 value = g_hash_table_lookup(hash, "ManufacturerDataLen");
408 dev_info->manufacturer_data_len = value ? g_value_get_uint(value) : 0;
409 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
410 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
411 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
414 value = g_hash_table_lookup(hash, "ManufacturerData");
415 manufacturer_data = value ? g_value_get_boxed(value) : NULL;
416 if (manufacturer_data) {
417 if (dev_info->manufacturer_data_len > 0) {
418 BT_DBG("manufacturer_data_len = %d", dev_info->manufacturer_data_len);
419 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
420 memcpy(dev_info->manufacturer_data, manufacturer_data->data, dev_info->manufacturer_data_len);
424 dev_info->address = g_strdup(address);
425 dev_info->name = g_strdup(name);
427 g_hash_table_destroy(hash);
429 BT_ERR("Hash is NULL\n");
437 static gboolean __ignore_auto_pairing_request(const char *address)
442 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
453 /* Get the LAP(Lower Address part) */
454 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
456 snprintf(lap_address, sizeof(lap_address), ",%s", address);
458 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
461 BT_ERR("fopen failed \n");
465 fseek(fp, 0, SEEK_END);
470 BT_ERR("Get file size failed \n");
475 buffer = g_malloc0(sizeof(char) * size);
476 result = fread((char *)buffer, 1, size, fp);
478 if (result != size) {
479 BT_ERR("Read Error\n");
484 BT_DBG("Buffer = %s\n", buffer);
486 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
492 /* Write the data and insert new device data */
493 for (i = 0; lines[i] != NULL; i++) {
494 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
495 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
497 lines[i] = temp_buffer;
500 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
503 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
506 BT_ERR("fopen failed \n");
511 BT_DBG("Buffer = %s\n", buffer);
512 fwrite(buffer, 1, strlen(buffer), fp);
522 static int __bt_retry_bond(void)
524 BT_CHECK_PARAMETER(bonding_info, return);
525 BT_CHECK_PARAMETER(bonding_info->addr, return);
527 if (!dbus_g_proxy_begin_call_with_timeout(bonding_info->device_proxy,
529 (DBusGProxyCallNotify) __bt_bond_device_cb,
530 NULL, NULL, BT_MAX_DBUS_TIMEOUT,
531 G_TYPE_UCHAR, bonding_info->conn_type,
533 BT_ERR("RePair call fail");
535 g_object_unref(bonding_info->device_proxy);
540 return BLUETOOTH_ERROR_NONE;
543 __bt_decline_pair_request();
544 is_device_creating = FALSE;
546 return BLUETOOTH_ERROR_INTERNAL;
550 static int __bt_remove_and_bond(void)
552 DBusGProxy *adapter_proxy;
554 char *device_path = NULL;
556 BT_CHECK_PARAMETER(bonding_info, return);
557 BT_CHECK_PARAMETER(bonding_info->addr, return);
559 adapter_proxy = _bt_get_adapter_proxy();
560 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
562 dbus_g_proxy_call(adapter_proxy, "FindDevice", NULL,
563 G_TYPE_STRING, bonding_info->addr,
564 G_TYPE_INVALID, DBUS_TYPE_G_OBJECT_PATH,
568 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
570 dbus_g_proxy_call(adapter_proxy, "UnpairDevice",
571 &err, DBUS_TYPE_G_OBJECT_PATH, device_path,
572 G_TYPE_INVALID, G_TYPE_INVALID);
575 BT_ERR("UnpairDevice Fail: %s", err->message);
577 return BLUETOOTH_ERROR_INTERNAL;
580 return __bt_retry_bond();
583 static int __bt_cancel_and_bond(void)
585 int ret = BLUETOOTH_ERROR_NONE;
587 ret = _bt_agent_reply_cancellation();
588 if (ret != BLUETOOTH_ERROR_NONE){
589 BT_ERR("Fail to call reply cancellation");
593 return __bt_retry_bond();
597 static void __bt_bond_device_cb(DBusGProxy *proxy, DBusGProxyCall *call,
600 int result = BLUETOOTH_ERROR_NONE;
604 request_info_t *req_info;
605 bluetooth_device_info_t dev_info;
606 bt_remote_dev_info_t *remote_dev_info;
608 /* Terminate ALL system popup */
609 syspopup_destroy_all();
611 dbus_g_proxy_end_call(proxy, call, &err, G_TYPE_INVALID);
614 is_device_creating = FALSE;
616 if (bonding_info == NULL) {
618 BT_ERR("bonding_info == NULL");
624 req_info = _bt_get_request_info(bonding_info->req_id);
625 if (req_info == NULL) {
626 BT_ERR("req_info == NULL");
631 BT_ERR("Error occured in CreateBonding [%s]", err->message);
633 if (!strcmp(err->message, "Already Exists")) {
634 BT_INFO("Existing Bond, remove and retry");
635 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
637 result = BLUETOOTH_ERROR_PARING_FAILED;
638 } else if (_bt_agent_is_canceled() ||
639 !strcmp(err->message, "Authentication Canceled")) {
640 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
641 } else if (!strcmp(err->message, "Authentication Rejected")) {
642 result = BLUETOOTH_ERROR_ACCESS_DENIED;
643 } else if (!strcmp(err->message, "In Progress")) {
644 BT_INFO("Bond in progress, cancel and retry");
645 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
647 result = BLUETOOTH_ERROR_PARING_FAILED;
648 } else if (!strcmp(err->message, "Authentication Failed")) {
649 if (bonding_info->is_autopair == TRUE) {
650 _bt_set_autopair_status_in_bonding_info(FALSE);
651 __ignore_auto_pairing_request(bonding_info->addr);
653 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
654 } else if (!strcmp(err->message, "Page Timeout")) {
655 /* This is the special case
656 As soon as call bluetooth_bond_device, try to cancel bonding.
657 In this case, before completing to call 'CreatePairedDevice' method
658 the procedure is stopped. So 'Cancle' error is not return.
660 result = BLUETOOTH_ERROR_HOST_DOWN;
661 } else if (!strcmp(err->message, BT_TIMEOUT_MESSAGE)) {
662 dbus_g_proxy_call(proxy, "CancelDeviceCreation", NULL,
663 G_TYPE_STRING, bonding_info->addr,
664 G_TYPE_INVALID, G_TYPE_INVALID);
666 result = BLUETOOTH_ERROR_INTERNAL;
667 } else if (!strcmp(err->message, "Connection Timeout")) {
668 /* Pairing request timeout */
669 result = BLUETOOTH_ERROR_TIMEOUT;
670 } else if (!strcmp(err->message, "Authentication Timeout")) {
671 /* Pairing request timeout */
672 result = BLUETOOTH_ERROR_TIMEOUT;
674 BT_DBG("Default case");
675 result = BLUETOOTH_ERROR_PARING_FAILED;
679 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
680 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
681 result == BLUETOOTH_ERROR_TIMEOUT ||
682 result == BLUETOOTH_ERROR_HOST_DOWN) {
683 bonding_info->result = result;
684 #ifdef TIZEN_WEARABLE
685 __bt_launch_unable_to_pairing_syspopup(result);
689 g_object_unref(proxy);
690 bonding_info->device_proxy = NULL;
692 if (result != BLUETOOTH_ERROR_NONE)
695 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
697 /* Send the event to application */
698 if (remote_dev_info != NULL) {
699 _bt_send_event(BT_ADAPTER_EVENT,
700 BLUETOOTH_EVENT_BONDING_FINISHED,
701 DBUS_TYPE_INT32, &result,
702 DBUS_TYPE_STRING, &bonding_info->addr,
703 DBUS_TYPE_UINT32, &remote_dev_info->class,
704 DBUS_TYPE_INT16, &remote_dev_info->rssi,
705 DBUS_TYPE_STRING, &remote_dev_info->name,
706 DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
707 DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
708 DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
709 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
710 &remote_dev_info->uuids, remote_dev_info->uuid_count,
711 DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
712 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
713 &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
716 _bt_free_device_info(remote_dev_info);
720 if (req_info->context == NULL)
723 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
724 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
726 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
727 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
730 if (_bt_adapter_get_status() != BT_ACTIVATED)
731 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
733 g_array_append_vals(out_param1, &dev_info,
734 sizeof(bluetooth_device_info_t));
735 g_array_append_vals(out_param2, &result, sizeof(int));
737 dbus_g_method_return(req_info->context, out_param1, out_param2);
739 g_array_free(out_param1, TRUE);
740 g_array_free(out_param2, TRUE);
742 _bt_delete_request_list(req_info->req_id);
747 _bt_agent_set_canceled(FALSE);
750 g_free(bonding_info->addr);
751 g_free(bonding_info);
755 int _bt_bond_device(int request_id,
756 bluetooth_device_address_t *device_address,
757 unsigned short conn_type, GArray **out_param1)
760 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
761 bluetooth_device_info_t dev_info;
763 DBusGConnection *conn;
764 char *device_path = NULL;
765 DBusGProxy *adapter_proxy;
766 GError *error = NULL;
768 BT_CHECK_PARAMETER(device_address, return);
771 BT_ERR("Bonding in progress");
773 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
774 memcpy(dev_info.device_address.addr, device_address->addr,
775 BLUETOOTH_ADDRESS_LENGTH);
777 g_array_append_vals(*out_param1, &dev_info,
778 sizeof(bluetooth_device_info_t));
780 return BLUETOOTH_ERROR_DEVICE_BUSY;
783 conn = _bt_get_system_gconn();
784 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
786 _bt_convert_addr_type_to_string(address, device_address->addr);
788 device_path = _bt_get_device_object_path(address);
790 if (device_path == NULL) {
791 BT_ERR("No searched device");
793 adapter_proxy = _bt_get_adapter_proxy();
794 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
796 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
797 G_TYPE_STRING, address,
798 G_TYPE_INVALID, G_TYPE_INVALID);
801 BT_ERR("CreateDevice Fail: %s", error->message);
805 device_path = _bt_get_device_object_path(address);
806 if (device_path == NULL) {
807 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
808 memcpy(dev_info.device_address.addr, device_address->addr,
809 BLUETOOTH_ADDRESS_LENGTH);
811 g_array_append_vals(*out_param1, &dev_info,
812 sizeof(bluetooth_device_info_t));
814 return BLUETOOTH_ERROR_NOT_PAIRED;
816 BT_INFO("device_path is created[%s]", device_path);
820 proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
821 device_path, BT_DEVICE_INTERFACE);
824 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
826 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
827 bonding_info->addr = g_strdup(address);
828 bonding_info->req_id = request_id;
830 bonding_info->device_proxy = proxy;
831 bonding_info->conn_type = conn_type;
833 is_device_creating = TRUE;
835 if (!dbus_g_proxy_begin_call_with_timeout(proxy, "Pair",
836 (DBusGProxyCallNotify) __bt_bond_device_cb,
837 NULL, NULL, BT_MAX_DBUS_TIMEOUT,
838 G_TYPE_UCHAR, conn_type,
840 BT_ERR("Pair call fail");
841 g_object_unref(proxy);
842 bonding_info->device_proxy = NULL;
845 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
847 return BLUETOOTH_ERROR_NONE;
849 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
850 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
853 g_array_append_vals(*out_param1, &dev_info,
854 sizeof(bluetooth_device_info_t));
856 is_device_creating = FALSE;
858 g_free(bonding_info->addr);
859 g_free(bonding_info);
862 return BLUETOOTH_ERROR_INTERNAL;
865 int _bt_cancel_bonding(void)
867 int ret = BLUETOOTH_ERROR_NONE;
869 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
871 ret = _bt_agent_reply_cancellation();
872 if (ret != BLUETOOTH_ERROR_NONE){
873 BT_ERR("Fail to call reply cancellation");
877 _bt_agent_set_canceled(TRUE);
879 return BLUETOOTH_ERROR_NONE;
882 static void __bt_unbond_cb(DBusGProxy *proxy, DBusGProxyCall *call,
888 int result = BLUETOOTH_ERROR_NONE;
889 bt_funcion_data_t *unbonding_info;
890 bluetooth_device_info_t dev_info;
891 request_info_t *req_info;
893 dbus_g_proxy_end_call(proxy, call, &err, G_TYPE_INVALID);
895 unbonding_info = user_data;
897 if (unbonding_info == NULL) {
899 BT_ERR("unbonding_info == NULL");
903 req_info = _bt_get_request_info(unbonding_info->req_id);
904 if (req_info == NULL) {
905 BT_ERR("req_info == NULL");
910 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
911 result = BLUETOOTH_ERROR_INTERNAL;
914 if (req_info->context == NULL)
917 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
918 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
920 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
921 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
922 unbonding_info->addr);
924 g_array_append_vals(out_param1, &dev_info,
925 sizeof(bluetooth_device_info_t));
926 g_array_append_vals(out_param2, &result, sizeof(int));
928 dbus_g_method_return(req_info->context, out_param1, out_param2);
930 _bt_delete_request_list(req_info->req_id);
932 g_array_free(out_param1, TRUE);
933 g_array_free(out_param2, TRUE);
939 if (unbonding_info) {
940 g_free(unbonding_info->addr);
941 g_free(unbonding_info);
945 int _bt_unbond_device(int request_id,
946 bluetooth_device_address_t *device_address,
949 char *device_path = NULL;
950 bt_funcion_data_t *unbonding_info;
951 DBusGProxy *adapter_proxy = NULL;
952 DBusGProxy *device_proxy = NULL;
953 DBusGConnection *conn;
954 int result = BLUETOOTH_ERROR_INTERNAL;
955 bluetooth_device_info_t dev_info;
956 GValue paired = { 0 };
957 GError *error = NULL;
959 BT_CHECK_PARAMETER(device_address, return);
961 adapter_proxy = _bt_get_adapter_proxy();
962 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
964 /* allocate user data so that it can be retrieved in callback */
965 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
966 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
967 unbonding_info->req_id = request_id;
969 _bt_convert_addr_type_to_string(unbonding_info->addr,
970 device_address->addr);
972 device_path = _bt_get_device_object_path(unbonding_info->addr);
974 if (device_path == NULL) {
975 BT_ERR("No paired device");
976 result = BLUETOOTH_ERROR_NOT_PAIRED;
980 conn = _bt_get_system_gconn();
982 BT_ERR("conn is NULL");
983 result = BLUETOOTH_ERROR_INTERNAL;
987 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
988 device_path, BT_PROPERTIES_INTERFACE);
989 if (device_proxy != NULL) {
990 if (!dbus_g_proxy_call(device_proxy, "Get", &error,
991 G_TYPE_STRING, BT_DEVICE_INTERFACE,
992 G_TYPE_STRING, "Paired",
994 G_TYPE_VALUE, &paired,
997 BT_ERR("Getting property failed: [%s]\n", error->message);
1001 if (g_value_get_boolean(&paired) == FALSE) {
1002 BT_ERR("No paired device");
1003 g_object_unref(device_proxy);
1004 result = BLUETOOTH_ERROR_NOT_PAIRED;
1008 g_object_unref(device_proxy);
1011 if (!dbus_g_proxy_begin_call(adapter_proxy, "UnpairDevice",
1012 (DBusGProxyCallNotify) __bt_unbond_cb,
1013 (gpointer)unbonding_info, NULL,
1014 DBUS_TYPE_G_OBJECT_PATH, device_path,
1016 BT_ERR("RemoveBonding begin failed\n");
1019 g_free(device_path);
1020 return BLUETOOTH_ERROR_NONE;
1023 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1024 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1025 unbonding_info->addr);
1027 g_array_append_vals(*out_param1, &dev_info,
1028 sizeof(bluetooth_device_info_t));
1029 g_free(device_path);
1030 g_free(unbonding_info->addr);
1031 g_free(unbonding_info);
1035 static void __bt_discover_cb(DBusGProxy *proxy, DBusGProxyCall *call,
1039 GHashTable *hash = NULL;
1042 int result = BLUETOOTH_ERROR_NONE;
1043 bluetooth_device_info_t dev_info;
1044 bt_remote_dev_info_t *remote_dev_info;
1045 request_info_t *req_info;
1047 dbus_g_proxy_end_call(proxy, call, &err,
1048 dbus_g_type_get_map("GHashTable", G_TYPE_UINT, G_TYPE_STRING), &hash,
1051 g_object_unref(proxy);
1053 if (searching_info == NULL) {
1055 BT_ERR("unbonding_info == NULL");
1059 req_info = _bt_get_request_info(searching_info->req_id);
1060 if (req_info == NULL) {
1061 BT_ERR("req_info == NULL");
1066 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1068 if (!strcmp("Operation canceled", err->message)) {
1069 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1070 } else if (!strcmp("In Progress", err->message)) {
1071 result = BLUETOOTH_ERROR_IN_PROGRESS;
1072 } else if (!strcmp("Host is down", err->message)) {
1073 result = BLUETOOTH_ERROR_HOST_DOWN;
1075 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1078 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1079 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1080 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1081 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1082 remote_dev_info->uuid_count > 0) {
1083 result = BLUETOOTH_ERROR_NONE;
1086 _bt_free_device_info(remote_dev_info);
1091 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1094 /* Send the event to application */
1095 if (remote_dev_info != NULL) {
1096 _bt_send_event(BT_ADAPTER_EVENT,
1097 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1098 DBUS_TYPE_INT32, &result,
1099 DBUS_TYPE_STRING, &searching_info->addr,
1100 DBUS_TYPE_UINT32, &remote_dev_info->class,
1101 DBUS_TYPE_INT16, &remote_dev_info->rssi,
1102 DBUS_TYPE_STRING, &remote_dev_info->name,
1103 DBUS_TYPE_BOOLEAN, &remote_dev_info->paired,
1104 DBUS_TYPE_BOOLEAN, &remote_dev_info->connected,
1105 DBUS_TYPE_BOOLEAN, &remote_dev_info->trust,
1106 DBUS_TYPE_ARRAY, DBUS_TYPE_STRING,
1107 &remote_dev_info->uuids, remote_dev_info->uuid_count,
1108 DBUS_TYPE_INT16, &remote_dev_info->manufacturer_data_len,
1109 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
1110 &remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1113 _bt_free_device_info(remote_dev_info);
1117 if (req_info->context == NULL)
1120 out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
1121 out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
1123 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1124 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1125 searching_info->addr);
1127 g_array_append_vals(out_param1, &dev_info,
1128 sizeof(bluetooth_device_info_t));
1129 g_array_append_vals(out_param2, &result, sizeof(int));
1131 dbus_g_method_return(req_info->context, out_param1, out_param2);
1133 g_array_free(out_param1, TRUE);
1134 g_array_free(out_param2, TRUE);
1136 _bt_delete_request_list(req_info->req_id);
1141 g_hash_table_destroy(hash);
1143 if (searching_info) {
1144 g_free(searching_info->addr);
1145 g_free(searching_info);
1146 searching_info = NULL;
1150 int _bt_search_device(int request_id,
1151 bluetooth_device_address_t *device_address)
1153 char *device_path = NULL;
1154 DBusGProxy *device_proxy = NULL;
1155 DBusGConnection *conn;
1157 DBusGProxy *adapter_proxy;
1158 int result = BLUETOOTH_ERROR_INTERNAL;
1160 BT_CHECK_PARAMETER(device_address, return);
1162 if (searching_info) {
1163 BT_ERR("Service searching in progress");
1164 return BLUETOOTH_ERROR_DEVICE_BUSY;
1167 adapter_proxy = _bt_get_adapter_proxy();
1168 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1170 /* allocate user data so that it can be retrieved in callback */
1171 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1172 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1173 searching_info->req_id = request_id;
1175 _bt_convert_addr_type_to_string(searching_info->addr,
1176 device_address->addr);
1178 conn = _bt_get_system_gconn();
1179 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1182 device_path = _bt_get_device_object_path(searching_info->addr);
1184 if (device_path == NULL) {
1185 BT_ERR("No paired device");
1186 result = BLUETOOTH_ERROR_NOT_PAIRED;
1190 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1191 device_path, BT_DEVICE_INTERFACE);
1192 g_free(device_path);
1193 if (device_proxy == NULL) {
1194 result = BLUETOOTH_ERROR_INTERNAL;
1198 if (!dbus_g_proxy_begin_call(device_proxy, "DiscoverServices",
1199 (DBusGProxyCallNotify)__bt_discover_cb,
1200 (gpointer)searching_info, NULL,
1203 BT_ERR("DiscoverServices failed");
1207 searching_info->device_proxy = device_proxy;
1209 return BLUETOOTH_ERROR_NONE;
1212 g_object_unref(device_proxy);
1214 g_free(searching_info->addr);
1215 g_free(searching_info);
1216 searching_info = NULL;
1220 int _bt_cancel_search_device(void)
1224 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1226 if (searching_info->device_proxy) {
1227 dbus_g_proxy_call(searching_info->device_proxy,
1230 G_TYPE_INVALID, G_TYPE_INVALID);
1232 __bt_cancel_search_service_done();
1234 return BLUETOOTH_ERROR_NONE;
1237 int _bt_set_alias(bluetooth_device_address_t *device_address,
1240 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1241 gchar *device_path = NULL;
1242 DBusGProxy *adapter_proxy;
1243 DBusGProxy *device_proxy;
1244 GError *error = NULL;
1245 GValue name = { 0 };
1246 DBusGConnection *conn;
1248 BT_CHECK_PARAMETER(device_address, return);
1249 BT_CHECK_PARAMETER(alias, return);
1251 adapter_proxy = _bt_get_adapter_proxy();
1252 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1254 conn = _bt_get_system_gconn();
1255 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1257 _bt_convert_addr_type_to_string(address, device_address->addr);
1259 device_path = _bt_get_device_object_path(address);
1261 if (device_path == NULL) {
1262 BT_ERR("No paired device");
1263 return BLUETOOTH_ERROR_NOT_PAIRED;
1266 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1267 device_path, BT_PROPERTIES_INTERFACE);
1269 g_free(device_path);
1270 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1271 g_value_init(&name, G_TYPE_STRING);
1272 g_value_set_string(&name, alias);
1274 dbus_g_proxy_call(device_proxy, "Set", &error,
1275 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1276 G_TYPE_STRING, "Alias",
1277 G_TYPE_VALUE, &name,
1278 G_TYPE_INVALID, G_TYPE_INVALID);
1280 g_object_unref(device_proxy);
1282 g_value_unset(&name);
1285 BT_ERR("SetProperty error: [%s]", error->message);
1286 g_error_free(error);
1287 return BLUETOOTH_ERROR_INTERNAL;
1290 return BLUETOOTH_ERROR_NONE;
1293 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1296 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1297 gchar *device_path = NULL;
1298 DBusGProxy *device_proxy;
1299 gboolean previous_value;
1300 GError *error = NULL;
1301 GValue trusted = { 0 };
1302 GValue trusted_v = { 0 };
1303 DBusGConnection *conn;
1304 int ret = BLUETOOTH_ERROR_NONE;
1306 BT_CHECK_PARAMETER(device_address, return);
1308 conn = _bt_get_system_gconn();
1309 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1311 _bt_convert_addr_type_to_string(address, device_address->addr);
1313 device_path = _bt_get_device_object_path(address);
1315 if (device_path == NULL) {
1316 BT_ERR("No paired device");
1317 return BLUETOOTH_ERROR_NOT_PAIRED;
1320 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1321 device_path, BT_PROPERTIES_INTERFACE);
1322 g_free(device_path);
1323 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1325 if (!dbus_g_proxy_call(device_proxy, "Get", &error,
1326 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1327 G_TYPE_STRING, "Trusted",
1329 G_TYPE_VALUE, &trusted_v,
1331 if (error != NULL) {
1332 BT_ERR("Getting property failed: [%s]\n", error->message);
1333 g_error_free(error);
1335 g_object_unref(device_proxy);
1336 return BLUETOOTH_ERROR_INTERNAL;
1339 previous_value = g_value_get_boolean(&trusted_v);
1341 /* If the input is same with previous value, return error. */
1342 if (previous_value == authorize) {
1343 BT_ERR("Same value: %d", previous_value);
1344 g_object_unref(device_proxy);
1345 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1349 g_value_init(&trusted, G_TYPE_BOOLEAN);
1350 g_value_set_boolean(&trusted, authorize);
1352 dbus_g_proxy_call(device_proxy, "Set", &error,
1353 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1354 G_TYPE_STRING, "Trusted",
1355 G_TYPE_VALUE, &trusted,
1356 G_TYPE_INVALID, G_TYPE_INVALID);
1358 g_object_unref(device_proxy);
1359 g_value_unset(&trusted);
1362 BT_ERR("SetProperty error: [%s]", error->message);
1363 g_error_free(error);
1364 ret = BLUETOOTH_ERROR_INTERNAL;
1370 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1371 gboolean *is_connected)
1373 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1374 char *object_path = NULL;
1376 DBusGProxy *device_proxy;
1377 GError *error = NULL;
1379 GHashTable *hash = NULL;
1380 DBusGConnection *conn;
1381 int ret = BLUETOOTH_ERROR_NONE;
1383 BT_CHECK_PARAMETER(device_address, return);
1385 conn = _bt_get_system_gconn();
1386 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1388 _bt_convert_addr_type_to_string(address, device_address->addr);
1390 object_path = _bt_get_device_object_path(address);
1391 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1393 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1394 object_path, BT_PROPERTIES_INTERFACE);
1395 g_free(object_path);
1396 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1398 dbus_g_proxy_call(device_proxy, "GetAll", &error,
1399 G_TYPE_STRING, BT_DEVICE_INTERFACE,
1401 dbus_g_type_get_map("GHashTable", G_TYPE_STRING,
1402 G_TYPE_VALUE), &hash, G_TYPE_INVALID);
1404 if (error != NULL) {
1405 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1406 g_error_free(error);
1407 g_object_unref(device_proxy);
1408 return BLUETOOTH_ERROR_INTERNAL;
1412 g_object_unref(device_proxy);
1413 return BLUETOOTH_ERROR_INTERNAL;
1416 value = g_hash_table_lookup(hash, "GattConnected");
1417 *is_connected = g_value_get_boolean(value);
1419 BT_DBG("gatt is connected : %d", *is_connected);
1421 g_hash_table_destroy(hash);
1422 g_object_unref(device_proxy);
1427 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1428 int connection_type, gboolean *is_connected)
1430 char *object_path = NULL;
1431 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1432 DBusGProxy *device_proxy = NULL;
1433 DBusGProxy *adapter_proxy = NULL;
1434 DBusGConnection *conn;
1435 GError *error = NULL;
1436 GHashTable *hash = NULL;
1437 GValue *value = NULL;
1439 dbus_bool_t val = FALSE;
1442 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1443 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1445 *is_connected = FALSE;
1447 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1448 return _bt_rfcomm_is_device_connected(device_address,
1450 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1451 return _bt_is_gatt_connected(device_address, is_connected);
1453 adapter_proxy = _bt_get_adapter_proxy();
1454 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1456 conn = _bt_get_system_gconn();
1457 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1459 _bt_convert_addr_type_to_string(address, device_address->addr);
1461 if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1462 object_path = _bt_get_adapter_path();
1463 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1464 object_path, BT_NETWORK_SERVER_INTERFACE);
1465 g_free(object_path);
1466 if (device_proxy == NULL) {
1467 BT_DBG("Device don't have this service");
1468 return BLUETOOTH_ERROR_INTERNAL;
1470 dbus_g_proxy_call(device_proxy, "GetProperties", NULL,
1471 G_TYPE_STRING, address,
1473 dbus_g_type_get_map("GHashTable",
1474 G_TYPE_STRING, G_TYPE_VALUE),
1475 &hash, G_TYPE_INVALID);
1477 value = g_hash_table_lookup(hash, "Connected");
1478 *is_connected = value ? g_value_get_boolean(value) : FALSE;
1479 g_hash_table_destroy(hash);
1481 } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1482 return _bt_is_network_connected(_bt_get_net_conn(),
1483 device_address->addr, is_connected);
1485 uuid = _bt_get_profile_uuid128(connection_type);
1487 BT_ERR("uuid is NULL");
1488 return BLUETOOTH_ERROR_INTERNAL;
1491 BT_DBG("uuid: %s", uuid);
1493 object_path = _bt_get_device_object_path(address);
1494 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1496 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1497 object_path, BT_DEVICE_INTERFACE);
1498 g_free(object_path);
1499 if (device_proxy == NULL) {
1500 BT_DBG("Device don't have this service");
1502 return BLUETOOTH_ERROR_INTERNAL;
1504 dbus_g_proxy_call(device_proxy, "IsConnectedProfile", &error,
1505 G_TYPE_STRING, uuid,
1507 G_TYPE_BOOLEAN, &val,
1510 if (error != NULL) {
1511 BT_ERR("Failed to get properties: %s\n", error->message);
1512 g_error_free(error);
1515 *is_connected = val;
1519 g_object_unref(device_proxy);
1520 return BLUETOOTH_ERROR_NONE;
1523 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1525 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1526 gchar *device_path = NULL;
1527 GError *error = NULL;
1528 DBusGProxy *device_proxy = NULL;
1529 DBusGProxy *adapter_proxy;
1530 DBusGConnection *conn;
1531 int ret = BLUETOOTH_ERROR_NONE;
1533 BT_CHECK_PARAMETER(bd_addr, return);
1535 _bt_convert_addr_type_to_string(device_address,
1536 (unsigned char *)bd_addr->addr);
1538 conn = _bt_get_system_gconn();
1539 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1541 adapter_proxy = _bt_get_adapter_proxy();
1542 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1544 device_path = _bt_get_device_object_path(device_address);
1545 if (device_path == NULL) {
1546 BT_DBG("device_path NULL");
1547 ret = BLUETOOTH_ERROR_INTERNAL;
1551 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1553 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1554 device_path, BT_DEVICE_INTERFACE);
1555 g_free(device_path);
1556 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1558 dbus_g_proxy_call(device_proxy, "ConnectLE", &error,
1559 G_TYPE_BOOLEAN, auto_connect, G_TYPE_INVALID,
1562 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1563 g_error_free(error);
1564 g_object_unref(device_proxy);
1565 return BLUETOOTH_ERROR_INTERNAL;
1568 g_object_unref(device_proxy);
1573 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1575 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1576 gchar *device_path = NULL;
1577 GError *error = NULL;
1578 DBusGProxy *device_proxy = NULL;
1579 DBusGProxy *adapter_proxy;
1580 DBusGConnection *conn;
1581 int ret = BLUETOOTH_ERROR_NONE;
1583 BT_CHECK_PARAMETER(bd_addr, return);
1585 _bt_convert_addr_type_to_string(device_address,
1586 (unsigned char *)bd_addr->addr);
1588 conn = _bt_get_system_gconn();
1589 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1591 adapter_proxy = _bt_get_adapter_proxy();
1592 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1594 device_path = _bt_get_device_object_path(device_address);
1595 if (device_path == NULL) {
1596 BT_DBG("device_path NULL");
1597 ret = BLUETOOTH_ERROR_INTERNAL;
1601 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1603 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1604 device_path, BT_DEVICE_INTERFACE);
1605 g_free(device_path);
1606 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1608 dbus_g_proxy_call(device_proxy, "DisconnectLE", &error, G_TYPE_INVALID, G_TYPE_INVALID);
1610 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1611 g_error_free(error);
1612 g_object_unref(device_proxy);
1613 return BLUETOOTH_ERROR_INTERNAL;
1616 g_object_unref(device_proxy);
1621 int _bt_connect_profile(char *address, char *uuid,
1622 void *cb, gpointer func_data)
1626 DBusGConnection *conn;
1627 DBusGProxy *adapter_proxy;
1628 GError *error = NULL;
1630 conn = _bt_get_system_gconn();
1631 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1633 object_path = _bt_get_device_object_path(address);
1634 if (object_path == NULL) {
1635 BT_ERR("No searched device");
1637 adapter_proxy = _bt_get_adapter_proxy();
1638 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1640 dbus_g_proxy_call(adapter_proxy, "CreateDevice", &error,
1641 G_TYPE_STRING, address,
1642 G_TYPE_INVALID, G_TYPE_INVALID);
1644 if (error != NULL) {
1645 BT_ERR("CreateDevice Fail: %s", error->message);
1646 g_error_free(error);
1649 object_path = _bt_get_device_object_path(address);
1651 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1653 proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1654 object_path, BT_DEVICE_INTERFACE);
1655 g_free(object_path);
1656 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1658 if (!dbus_g_proxy_begin_call(proxy, "ConnectProfile",
1659 (DBusGProxyCallNotify)cb,
1661 G_TYPE_STRING, uuid,
1663 BT_ERR("Connect Dbus Call Error");
1664 g_object_unref(proxy);
1665 return BLUETOOTH_ERROR_INTERNAL;
1667 return BLUETOOTH_ERROR_NONE;
1670 int _bt_disconnect_profile(char *address, char *uuid,
1671 void *cb, gpointer func_data)
1675 DBusGConnection *conn;
1677 conn = _bt_get_system_gconn();
1678 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1680 object_path = _bt_get_device_object_path(address);
1681 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1683 proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1684 object_path, BT_DEVICE_INTERFACE);
1685 g_free(object_path);
1686 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1688 if (!dbus_g_proxy_begin_call(proxy, "DisconnectProfile",
1689 (DBusGProxyCallNotify)cb,
1691 G_TYPE_STRING, uuid,
1693 BT_ERR("Connect Dbus Call Error");
1694 g_object_unref(proxy);
1695 return BLUETOOTH_ERROR_INTERNAL;
1697 return BLUETOOTH_ERROR_NONE;
1700 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1701 int low_threshold, int in_range_threshold, int high_threshold)
1703 int ret = BLUETOOTH_ERROR_NONE;
1705 GError *error = NULL;
1706 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1708 BT_CHECK_PARAMETER(bd_addr, return);
1709 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1710 bd_addr->addr[0], bd_addr->addr[1],
1711 bd_addr->addr[2], bd_addr->addr[3],
1712 bd_addr->addr[4], bd_addr->addr[5],
1714 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1715 in_range_threshold, high_threshold);
1717 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1719 proxy = _bt_get_adapter_proxy();
1720 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1722 if (!dbus_g_proxy_call(proxy, "EnableRssi", &error,
1723 G_TYPE_STRING, address,
1724 G_TYPE_INT, link_type,
1725 G_TYPE_INT, low_threshold,
1726 G_TYPE_INT, in_range_threshold,
1727 G_TYPE_INT, high_threshold,
1728 G_TYPE_INVALID, G_TYPE_INVALID)) {
1729 BT_ERR("Failed to Enable RSSI");
1730 ret = BLUETOOTH_ERROR_INTERNAL;
1731 if (error != NULL) {
1732 BT_ERR("Dbus Call Error:[%s]", error->message);
1733 g_error_free(error);
1734 ret = BLUETOOTH_ERROR_INTERNAL;
1741 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1744 int ret = BLUETOOTH_ERROR_NONE;
1746 GError *error = NULL;
1747 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1749 BT_CHECK_PARAMETER(bd_addr, return);
1750 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1751 bd_addr->addr[0], bd_addr->addr[1],
1752 bd_addr->addr[2], bd_addr->addr[3],
1753 bd_addr->addr[4], bd_addr->addr[5],
1756 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1758 proxy = _bt_get_adapter_proxy();
1759 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1761 if (!dbus_g_proxy_call(proxy, "GetRssiStrength", &error,
1762 G_TYPE_STRING, address,
1763 G_TYPE_INT, link_type,
1764 G_TYPE_INVALID, G_TYPE_INVALID)) {
1765 BT_ERR("Failed to get Raw RSSI");
1766 ret = BLUETOOTH_ERROR_INTERNAL;
1767 if (error != NULL) {
1768 BT_ERR("Dbus Call Error:[%s]", error->message);
1769 g_error_free(error);
1770 ret = BLUETOOTH_ERROR_INTERNAL;
1777 int _bt_le_conn_update(unsigned char *device_address,
1778 guint16 interval_min, guint16 interval_max,
1779 guint16 latency, guint16 time_out)
1781 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1782 gchar *device_path = NULL;
1783 GError *error = NULL;
1784 DBusGProxy *device_proxy = NULL;
1785 DBusGConnection *conn;
1786 guint32 min, max, to;
1787 int ret = BLUETOOTH_ERROR_NONE;
1791 BT_CHECK_PARAMETER(device_address, return);
1793 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1794 interval_min, interval_max, latency, time_out);
1796 if (interval_min > interval_max ||
1797 interval_min < BT_LE_CONN_INTERVAL_MIN ||
1798 interval_max > BT_LE_CONN_INTERVAL_MAX) {
1799 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1803 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
1804 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
1806 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1807 time_out > BT_LE_CONN_SUPER_TO_MAX) {
1808 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1812 to = time_out / BT_LE_CONN_TO_SPLIT;
1814 if (latency > ((to / max) - 1)) {
1815 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1819 _bt_convert_addr_type_to_string(address, device_address);
1821 BT_DBG("Remote device address: %s", address);
1823 device_path = _bt_get_device_object_path(address);
1825 if (device_path == NULL) {
1826 BT_DBG("device_path NULL");
1827 ret = BLUETOOTH_ERROR_INTERNAL;
1831 conn = _bt_get_system_gconn();
1833 BT_DBG("conn NULL");
1834 ret = BLUETOOTH_ERROR_INTERNAL;
1838 device_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
1839 device_path, BT_DEVICE_INTERFACE);
1840 g_free(device_path);
1841 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1843 dbus_g_proxy_call(device_proxy, "LeConnUpdate", &error,
1846 G_TYPE_UINT, latency,
1848 G_TYPE_INVALID, G_TYPE_INVALID);
1850 BT_ERR("LeConnUpdate Call Error %s[%s]",
1851 error->message, address);
1852 g_error_free(error);
1853 g_object_unref(device_proxy);
1854 return BLUETOOTH_ERROR_INTERNAL;
1857 g_object_unref(device_proxy);