2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
23 #include <bundle_internal.h>
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-adapter-le.h"
31 #include "bt-service-event.h"
32 #include "bt-service-device.h"
33 #include "bt-service-rfcomm-client.h"
34 #include "bt-service-util.h"
35 #include "bt-service-agent.h"
36 #include "bt-service-network.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-gap-agent.h"
39 #include "bt-service-pbap.h"
41 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
42 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
43 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
45 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
46 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
47 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
49 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
50 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
51 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
52 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
54 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
56 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
60 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
64 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
68 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
69 #define PROFILE_TRUSTED 0x2 /* This corresponds to binary 0b10*/
70 #define PROFILE_BLOCKED 0x1 /* This corresponds to binary 0b01*/
71 #define PROFILE_SHOW_AUTH 0x0 /* This corresponds to binary 0b00*/
78 GDBusProxy *device_proxy;
79 GDBusProxy *adapter_proxy;
81 unsigned short conn_type;
82 gboolean bonding_wo_discovery;
92 bluetooth_device_address_t bd_addr;
93 gboolean auto_connect;
94 } bt_pending_le_conn_info_s;
101 } bt_connected_le_dev_t;
110 } bt_le_conn_param_t;
112 gboolean is_device_creating;
113 bt_funcion_data_t *bonding_info;
114 bt_funcion_data_t *searching_info;
115 bt_funcion_data_t *att_mtu_req_info;
117 static GSList *le_connected_dev_list = NULL;
118 static GSList *pin_info_list = NULL;
119 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
120 static guint pending_le_conn_timer_id = 0;
122 /* This HID Mouse does not support pairing precedure. need to skip it. */
123 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
125 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
128 static int __bt_retry_bond(void);
131 /*static void __bt_decline_pair_request()
133 GVariant *out_param1;
134 GVariant *out_param2;
135 request_info_t *req_info;
136 bluetooth_device_info_t dev_info;
137 bt_remote_dev_info_t *remote_dev_info;
138 GVariant *uuid_list, *manufacture_data;
143 req_info = _bt_get_request_info(bonding_info->req_id);
144 if (req_info == NULL) {
145 BT_ERR("req_info == NULL");
148 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
150 BT_DBG("bonding_info is NULL");
155 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
156 remote_dev_info->uuids, remote_dev_info->uuid_count,
159 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
160 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
163 param = g_variant_new("isunsbuba{s}na{y})",
164 bonding_info->result,
166 remote_dev_info->class,
167 remote_dev_info->rssi,
168 remote_dev_info->name,
169 remote_dev_info->paired,
170 remote_dev_info->connected,
171 remote_dev_info->trust,
173 remote_dev_info->manufacturer_data_len,
177 //Send the event to application
178 if (remote_dev_info != NULL) {
179 _bt_send_event(BT_ADAPTER_EVENT,
180 BLUETOOTH_EVENT_BONDING_FINISHED,
183 _bt_free_device_info(remote_dev_info);
186 if (req_info->context == NULL)
189 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
190 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
193 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
194 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
196 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
197 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
200 g_dbus_method_invocation_return_value(req_info->context,
201 g_variant_new("@ayi", out_param1, out_param2));
204 _bt_delete_request_list(req_info->req_id);
207 g_free(bonding_info->addr);
208 g_free(bonding_info);
214 gboolean _bt_is_device_creating(void)
216 return is_device_creating;
219 gboolean _bt_is_bonding_device_address(const char *address)
221 if (bonding_info == NULL || bonding_info->addr == NULL)
224 if (g_strcmp0(bonding_info->addr, address) == 0) {
225 BT_DBG("[%s] is bonding device", address);
229 BT_DBG("[%s] is NOT bonding device", address);
233 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
235 ret_if(bonding_info == NULL);
236 bonding_info->is_autopair = is_autopair;
239 void __bt_cancel_search_service_done(void)
241 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
242 request_info_t *req_info;
243 bluetooth_device_info_t dev_info;
244 GVariant *out_param1;
246 ret_if(searching_info == NULL);
248 req_info = _bt_get_request_info(searching_info->req_id);
249 if (req_info == NULL) {
250 BT_ERR("req_info == NULL");
254 if (req_info->context == NULL)
257 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
258 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
259 searching_info->addr);
261 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
262 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
265 g_dbus_method_invocation_return_value(req_info->context,
266 g_variant_new("(iv)", result, out_param1));
268 _bt_delete_request_list(req_info->req_id);
272 g_free(searching_info->addr);
273 g_free(searching_info);
274 searching_info = NULL;
277 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
279 ret_if(value == NULL);
280 ret_if(info == NULL);
282 gsize uuid_count = 0;
284 info->uuids = g_variant_dup_strv(value, &uuid_count);
285 info->uuid_count = (unsigned int)uuid_count;
287 BT_DBG("uuid count : %d", uuid_count);
290 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
292 char *object_path = NULL;
293 bt_remote_dev_info_t *dev_info;
295 retv_if(address == NULL, NULL);
297 object_path = _bt_get_device_object_path(address);
298 retv_if(object_path == NULL, NULL);
300 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
306 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
307 const char *object_path)
309 bt_remote_dev_info_t *dev_info;
310 GDBusProxy *adapter_proxy;
311 GDBusProxy *device_proxy;
316 GDBusConnection *conn;
317 GError *error = NULL;
318 GVariant *result = NULL;
319 GVariantIter *value_iter;
323 adapter_proxy = _bt_get_adapter_proxy();
324 retv_if(adapter_proxy == NULL, NULL);
326 retv_if(object_path == NULL, NULL);
328 conn = _bt_gdbus_get_system_gconn();
330 BT_ERR("conn == NULL");
334 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
335 NULL, BT_BLUEZ_NAME, object_path,
336 BT_PROPERTIES_INTERFACE, NULL, NULL);
338 retv_if(device_proxy == NULL, NULL);
340 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
341 g_variant_new("(s)", BT_DEVICE_INTERFACE),
342 G_DBUS_CALL_FLAGS_NONE,
347 g_object_unref(device_proxy);
349 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
351 if (result != NULL) {
352 g_variant_get(result , "(@a{sv})", &value);
353 g_variant_unref(result);
355 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
357 g_variant_get(tmp_value, "s", &name);
358 g_variant_unref(tmp_value);
360 DBG_SECURE("Alias Name [%s]", name);
362 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
363 g_variant_get(tmp_value, "s", &name);
364 g_variant_unref(tmp_value);
367 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
369 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
370 g_variant_unref(tmp_value);
372 dev_info->is_alias_set = FALSE;
374 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
376 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
378 dev_info->class = g_variant_get_uint32(tmp_value);
379 g_variant_unref(tmp_value);
383 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
385 dev_info->connected = g_variant_get_byte(tmp_value);
386 g_variant_unref(tmp_value);
388 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
389 BT_DBG("connected link : %d", dev_info->connected);
391 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
393 dev_info->trust = g_variant_get_boolean(tmp_value);
394 g_variant_unref(tmp_value);
396 dev_info->trust = FALSE;
398 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
400 dev_info->paired = g_variant_get_boolean(tmp_value);
401 g_variant_unref(tmp_value);
403 dev_info->paired = FALSE;
405 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
407 dev_info->rssi = g_variant_get_int16(tmp_value);
408 g_variant_unref(tmp_value);
412 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
414 dev_info->addr_type = g_variant_get_byte(tmp_value);
415 g_variant_unref(tmp_value);
417 dev_info->addr_type = 0;
419 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
421 __bt_get_uuids(tmp_value, dev_info);
422 g_variant_unref(tmp_value);
425 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
427 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
428 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
429 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
430 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
432 g_variant_unref(tmp_value);
434 dev_info->manufacturer_data_len = 0;
436 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
438 if ((dev_info->manufacturer_data_len == 0) ||
439 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
440 BT_ERR("manufacturer data length doesn't match");
441 dev_info->manufacturer_data_len = 0;
442 dev_info->manufacturer_data = NULL;
444 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
445 g_variant_get(tmp_value, "ay", &value_iter);
446 while (g_variant_iter_loop(value_iter, "y", &m_value))
447 dev_info->manufacturer_data[i++] = m_value;
449 g_variant_unref(tmp_value);
451 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
452 dev_info->manufacturer_data_len = 0;
453 dev_info->manufacturer_data = NULL;
456 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
457 g_variant_get(tmp_value, "s", &address);
458 g_variant_unref(tmp_value);
460 dev_info->address = address;
461 dev_info->name = name;
462 g_variant_unref(value);
464 BT_ERR("result is NULL\n");
472 char *_bt_get_device_name(const char *bdaddress)
474 char *device_path = NULL;
475 const gchar *name = NULL;
476 gchar *dev_name = NULL;
478 GVariant *result = NULL;
480 GDBusProxy *device_proxy;
481 GDBusConnection *conn;
483 retv_if(bdaddress == NULL, NULL);
485 device_path = _bt_get_device_object_path((char *)bdaddress);
486 retv_if(device_path == NULL, NULL);
488 conn = _bt_gdbus_get_system_gconn();
489 retv_if(conn == NULL, NULL);
490 INFO_SECURE("Device_path %s", device_path);
491 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
492 NULL, BT_BLUEZ_NAME, device_path,
493 BT_PROPERTIES_INTERFACE, NULL, &err);
496 retv_if(device_proxy == NULL, NULL);
498 result = g_dbus_proxy_call_sync(device_proxy, "Get",
499 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
500 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
502 BT_ERR("DBus Error : %s", err->message);
506 g_variant_get(result, "(v)", &value);
507 name = g_variant_get_string(value, &name_len);
508 INFO_SECURE("Alias Name [%s]", name);
510 dev_name = g_strdup(name);
511 g_variant_unref(value);
512 g_variant_unref(result);
517 result = g_dbus_proxy_call_sync(device_proxy, "Get",
518 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
519 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
521 ERR("DBus Error : %s", err->message);
524 g_variant_get(result, "(v)", &value);
525 name = g_variant_get_string(value, &name_len);
526 INFO_SECURE("Name = %s", name);
528 dev_name = g_strdup(name);
529 g_variant_unref(value);
530 g_variant_unref(result);
534 g_object_unref(device_proxy);
538 static gboolean __ignore_auto_pairing_request(const char *address)
543 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
554 /* Get the LAP(Lower Address part) */
555 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
557 snprintf(lap_address, sizeof(lap_address), ",%s", address);
559 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
562 BT_ERR("fopen failed \n");
566 fseek(fp, 0, SEEK_END);
571 BT_ERR("Get file size failed \n");
576 buffer = g_malloc0(sizeof(char) * size);
577 result = fread((char *)buffer, 1, size, fp);
579 if (result != size) {
580 BT_ERR("Read Error\n");
585 BT_DBG("Buffer = %s\n", buffer);
587 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
593 /* Write the data and insert new device data */
594 for (i = 0; lines[i] != NULL; i++) {
595 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
596 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
598 lines[i] = temp_buffer;
601 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
603 /* Fix : NULL_RETURNS */
604 retv_if(buffer == NULL, FALSE);
606 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
609 BT_ERR("fopen failed \n");
614 BT_DBG("Buffer = %s\n", buffer);
615 fwrite(buffer, 1, strlen(buffer), fp);
625 static int __bt_cancel_bonding(void)
627 BT_CHECK_PARAMETER(bonding_info, return);
628 BT_CHECK_PARAMETER(bonding_info->addr, return);
629 /* First Cancel the ongoing pairing in bluez otherwise if we send
630 * pair request bluez will send inprogress and we again retry bond so
631 * this cycle continues */
633 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
634 NULL, G_DBUS_CALL_FLAGS_NONE,
637 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
639 return BLUETOOTH_ERROR_PARING_FAILED;
641 return BLUETOOTH_ERROR_NONE;
644 static int __bt_retry_bond(void)
646 BT_CHECK_PARAMETER(bonding_info, return);
647 BT_CHECK_PARAMETER(bonding_info->addr, return);
649 is_device_creating = TRUE;
651 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
652 g_variant_new("(y)", bonding_info->conn_type),
653 G_DBUS_CALL_FLAGS_NONE,
656 (GAsyncReadyCallback)__bt_bond_device_cb,
659 return BLUETOOTH_ERROR_NONE;
663 static int __bt_remove_and_bond(void)
665 GDBusProxy *adapter_proxy;
666 GVariant *result = NULL;
668 char *device_path = NULL;
670 BT_CHECK_PARAMETER(bonding_info, return);
671 BT_CHECK_PARAMETER(bonding_info->addr, return);
673 adapter_proxy = _bt_get_adapter_proxy();
674 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
676 device_path = _bt_get_device_object_path(bonding_info->addr);
677 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
679 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
680 g_variant_new("(o)", device_path),
681 G_DBUS_CALL_FLAGS_NONE,
686 if (result == NULL) {
688 BT_ERR("UnpairDevice Fail: %s", err->message);
691 return BLUETOOTH_ERROR_INTERNAL;
694 g_variant_unref(result);
695 return __bt_retry_bond();
698 static int __bt_cancel_and_bond(void)
700 int ret = BLUETOOTH_ERROR_NONE;
702 ret = _bt_agent_reply_cancellation();
703 if (ret != BLUETOOTH_ERROR_NONE) {
704 BT_ERR("Fail to call reply cancellation");
707 return __bt_retry_bond();
711 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
714 int result = BLUETOOTH_ERROR_NONE;
717 GVariant *out_param1;
718 request_info_t *req_info;
719 bluetooth_device_info_t dev_info;
720 bt_remote_dev_info_t *remote_dev_info;
721 GVariant *manufacture_data;
724 /* Terminate ALL system popup */
725 if (headed_plugin_info->plugin_headed_enabled)
726 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
728 reply = g_dbus_proxy_call_finish(proxy, res, &err);
730 g_variant_unref(reply);
732 is_device_creating = FALSE;
734 if (bonding_info == NULL) {
736 BT_ERR("bonding_info == NULL");
742 req_info = _bt_get_request_info(bonding_info->req_id);
743 if (req_info == NULL) {
744 BT_ERR("req_info == NULL");
749 g_dbus_error_strip_remote_error(err);
750 BT_ERR("Error occured in CreateBonding [%s]", err->message);
752 if (g_strrstr(err->message, "Already Exists")) {
753 BT_INFO("Existing Bond, remove and retry");
754 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
756 result = BLUETOOTH_ERROR_PARING_FAILED;
757 } else if (_bt_agent_is_canceled() ||
758 g_strrstr(err->message, "Authentication Canceled")) {
759 BT_INFO("Cancelled by USER");
760 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
761 } else if (g_strrstr(err->message, "Authentication Rejected")) {
763 result = BLUETOOTH_ERROR_ACCESS_DENIED;
764 } else if (g_strrstr(err->message, "In Progress")) {
765 BT_INFO("Bond in progress, cancel and retry");
766 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
768 result = BLUETOOTH_ERROR_PARING_FAILED;
769 } else if (g_strrstr(err->message, "Authentication Failed")) {
770 BT_INFO("Authentication Failed");
771 if (bonding_info->is_autopair == TRUE) {
772 _bt_set_autopair_status_in_bonding_info(FALSE);
773 __ignore_auto_pairing_request(bonding_info->addr);
775 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
776 } else if (g_strrstr(err->message, "Page Timeout")) {
777 BT_INFO("Page Timeout");
778 /* This is the special case
779 As soon as call bluetooth_bond_device, try to cancel bonding.
780 In this case, before completing to call 'CreatePairedDevice' method
781 the procedure is stopped. So 'Cancle' error is not return.
783 result = BLUETOOTH_ERROR_HOST_DOWN;
784 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
785 BT_INFO("Cancel already running bonding");
786 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
787 BT_INFO("Error while Cancelling bonding");
788 /* we need to unref proxy so continue */
790 result = BLUETOOTH_ERROR_INTERNAL;
791 } else if (g_strrstr(err->message, "Connection Timeout")) {
792 /* Pairing request timeout */
793 result = BLUETOOTH_ERROR_TIMEOUT;
794 } else if (g_strrstr(err->message, "Authentication Timeout")) {
795 /* Pairing request timeout */
796 result = BLUETOOTH_ERROR_TIMEOUT;
798 BT_DBG("Default case");
799 result = BLUETOOTH_ERROR_PARING_FAILED;
803 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
804 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
805 result == BLUETOOTH_ERROR_TIMEOUT ||
806 result == BLUETOOTH_ERROR_HOST_DOWN) {
807 if (TIZEN_PROFILE_WEARABLE) {
808 int is_sw_running = 0;
810 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
811 BT_ERR("vconf_get_int for setup wizard state failed");
813 if (!is_sw_running) {
814 if (headed_plugin_info->plugin_headed_enabled)
815 headed_plugin_info->headed_plugin->bt_launch_unable_to_pairing_syspopup(result);
817 BT_ERR("Unable to pair");
820 bonding_info->result = result;
823 g_object_unref(proxy);
824 bonding_info->device_proxy = NULL;
826 if (result != BLUETOOTH_ERROR_NONE) {
827 if (bonding_info->bonding_wo_discovery) {
828 GDBusProxy *adapter_proxy;
829 GVariant *ret = NULL;
830 GError *error = NULL;
833 BT_ERR("Bond was tried without discovery. Remove it");
835 adapter_proxy = _bt_get_adapter_proxy();
836 if (adapter_proxy == NULL) {
837 BT_ERR("Cannot get adapter_proxy");
841 device_path = _bt_get_device_object_path(bonding_info->addr);
842 if (device_path == NULL) {
843 BT_ERR("Cannot get device path");
847 ret = g_dbus_proxy_call_sync(adapter_proxy,
849 g_variant_new("(o)", device_path),
850 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
853 BT_ERR("RemoveDevice Fail: %s", error->message);
854 g_clear_error(&error);
858 g_variant_unref(ret);
866 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
867 if (!remote_dev_info)
870 GVariant *uuids = NULL;
871 GVariantBuilder *builder = NULL;
873 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
874 for (i = 0; i < remote_dev_info->uuid_count; i++) {
875 g_variant_builder_add(builder, "s",
876 remote_dev_info->uuids[i]);
878 uuids = g_variant_new("as", builder);
879 g_variant_builder_unref(builder);
880 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
881 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
884 param = g_variant_new("(isunsbub@asn@ay)",
887 remote_dev_info->class,
888 remote_dev_info->rssi,
889 remote_dev_info->name,
890 remote_dev_info->paired,
891 remote_dev_info->connected,
892 remote_dev_info->trust,
894 remote_dev_info->manufacturer_data_len,
898 /* Send the event to application */
899 _bt_send_event(BT_ADAPTER_EVENT,
900 BLUETOOTH_EVENT_BONDING_FINISHED,
903 _bt_free_device_info(remote_dev_info);
906 if (req_info->context == NULL)
909 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
910 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
913 if (_bt_adapter_get_status() != BT_ACTIVATED)
914 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
916 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
917 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
919 g_dbus_method_invocation_return_value(req_info->context,
920 g_variant_new("(iv)", result, out_param1));
922 _bt_delete_request_list(req_info->req_id);
927 _bt_agent_set_canceled(FALSE);
930 g_free(bonding_info->addr);
931 g_free(bonding_info);
935 int _bt_bond_device(int request_id,
936 bluetooth_device_address_t *device_address,
937 unsigned short conn_type, GArray **out_param1)
940 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
941 bluetooth_device_info_t dev_info;
943 GDBusConnection *conn;
944 char *device_path = NULL;
945 GDBusProxy *adapter_proxy;
946 GError *error = NULL;
947 gboolean bonding_wo_discovery = FALSE;
949 BT_CHECK_PARAMETER(device_address, return);
952 BT_ERR("Bonding in progress");
954 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
955 memcpy(dev_info.device_address.addr, device_address->addr,
956 BLUETOOTH_ADDRESS_LENGTH);
958 g_array_append_vals(*out_param1, &dev_info,
959 sizeof(bluetooth_device_info_t));
961 return BLUETOOTH_ERROR_DEVICE_BUSY;
964 conn = _bt_gdbus_get_system_gconn();
965 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
967 _bt_convert_addr_type_to_string(address, device_address->addr);
969 device_path = _bt_get_device_object_path(address);
971 if (device_path == NULL) {
972 BT_ERR("No searched device");
973 GVariant *ret = NULL;
974 adapter_proxy = _bt_get_adapter_proxy();
975 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
977 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
978 g_variant_new("(s)", address),
979 G_DBUS_CALL_FLAGS_NONE,
985 BT_ERR("CreateDevice Fail: %s", error->message);
986 g_clear_error(&error);
989 g_variant_unref(ret);
990 device_path = _bt_get_device_object_path(address);
991 if (device_path == NULL) {
992 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
993 memcpy(dev_info.device_address.addr, device_address->addr,
994 BLUETOOTH_ADDRESS_LENGTH);
996 g_array_append_vals(*out_param1, &dev_info,
997 sizeof(bluetooth_device_info_t));
999 return BLUETOOTH_ERROR_NOT_PAIRED;
1001 BT_INFO("device_path is created[%s]", device_path);
1003 bonding_wo_discovery = TRUE;
1006 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1007 NULL, BT_BLUEZ_NAME,
1008 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1010 g_free(device_path);
1011 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1013 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1014 bonding_info->addr = g_strdup(address);
1015 bonding_info->req_id = request_id;
1017 bonding_info->device_proxy = proxy;
1018 bonding_info->conn_type = conn_type;
1019 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1021 is_device_creating = TRUE;
1023 g_dbus_proxy_call(proxy, "Pair",
1024 g_variant_new("(y)", conn_type),
1025 G_DBUS_CALL_FLAGS_NONE,
1026 BT_MAX_DBUS_TIMEOUT,
1028 (GAsyncReadyCallback)__bt_bond_device_cb,
1031 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1033 return BLUETOOTH_ERROR_NONE;
1035 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1036 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1037 bonding_info->addr);
1039 g_array_append_vals(*out_param1, &dev_info,
1040 sizeof(bluetooth_device_info_t));
1042 is_device_creating = FALSE;
1044 g_free(bonding_info->addr);
1045 g_free(bonding_info);
1046 bonding_info = NULL;
1048 return BLUETOOTH_ERROR_INTERNAL;*/
1051 int _bt_cancel_bonding(void)
1053 int ret = BLUETOOTH_ERROR_NONE;
1055 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1057 ret = _bt_agent_reply_cancellation();
1058 if (ret != BLUETOOTH_ERROR_NONE) {
1059 BT_ERR("Fail to call reply cancellation");
1063 _bt_agent_set_canceled(TRUE);
1065 return BLUETOOTH_ERROR_NONE;
1068 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1073 GVariant *out_param1;
1074 int result = BLUETOOTH_ERROR_NONE;
1075 bt_funcion_data_t *unbonding_info;
1076 bluetooth_device_info_t dev_info;
1077 request_info_t *req_info;
1079 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1081 g_variant_unref(reply);
1083 unbonding_info = user_data;
1085 if (unbonding_info == NULL) {
1087 BT_ERR("unbonding_info == NULL");
1091 req_info = _bt_get_request_info(unbonding_info->req_id);
1092 if (req_info == NULL) {
1093 BT_ERR("req_info == NULL");
1098 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1099 result = BLUETOOTH_ERROR_INTERNAL;
1102 if (req_info->context == NULL)
1105 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1106 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1107 unbonding_info->addr);
1109 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1110 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1112 g_dbus_method_invocation_return_value(req_info->context,
1113 g_variant_new("(iv)", result, out_param1));
1116 _bt_delete_request_list(req_info->req_id);
1122 if (unbonding_info) {
1123 g_free(unbonding_info->addr);
1124 g_free(unbonding_info);
1128 int _bt_unbond_device(int request_id,
1129 bluetooth_device_address_t *device_address,
1130 GArray **out_param1)
1132 char *device_path = NULL;
1133 bt_funcion_data_t *unbonding_info;
1134 GDBusProxy *adapter_proxy = NULL;
1135 GDBusProxy *device_proxy = NULL;
1136 GDBusConnection *conn;
1137 int result = BLUETOOTH_ERROR_INTERNAL;
1138 bluetooth_device_info_t dev_info;
1139 GError *error = NULL;
1140 GVariant *ret = NULL;
1142 BT_CHECK_PARAMETER(device_address, return);
1144 adapter_proxy = _bt_get_adapter_proxy();
1145 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1147 /* allocate user data so that it can be retrieved in callback */
1148 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1149 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1150 unbonding_info->req_id = request_id;
1152 _bt_convert_addr_type_to_string(unbonding_info->addr,
1153 device_address->addr);
1155 device_path = _bt_get_device_object_path(unbonding_info->addr);
1157 if (device_path == NULL) {
1158 BT_ERR("No paired device");
1159 result = BLUETOOTH_ERROR_NOT_PAIRED;
1163 conn = _bt_gdbus_get_system_gconn();
1165 BT_ERR("conn is NULL");
1166 result = BLUETOOTH_ERROR_INTERNAL;
1170 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1171 NULL, BT_BLUEZ_NAME,
1172 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1174 if (device_proxy != NULL) {
1176 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1177 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1178 G_DBUS_CALL_FLAGS_NONE,
1183 BT_ERR("Getting property failed: [%s]\n", error->message);
1184 g_error_free(error);
1185 result = BLUETOOTH_ERROR_NOT_PAIRED;
1189 BT_ERR("No paired device");
1190 g_object_unref(device_proxy);
1191 result = BLUETOOTH_ERROR_NOT_PAIRED;
1194 g_variant_unref(ret);
1196 g_object_unref(device_proxy);
1199 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1200 g_variant_new("(o)", device_path),
1201 G_DBUS_CALL_FLAGS_NONE,
1202 BT_MAX_DBUS_TIMEOUT,
1204 (GAsyncReadyCallback)__bt_unbond_cb,
1207 g_free(device_path);
1208 return BLUETOOTH_ERROR_NONE;
1211 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1212 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1213 unbonding_info->addr);
1215 g_array_append_vals(*out_param1, &dev_info,
1216 sizeof(bluetooth_device_info_t));
1217 g_free(device_path);
1218 g_free(unbonding_info->addr);
1219 g_free(unbonding_info);
1223 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1228 GVariant *out_param1;
1229 int result = BLUETOOTH_ERROR_NONE;
1230 bluetooth_device_info_t dev_info;
1231 bt_remote_dev_info_t *remote_dev_info;
1232 request_info_t *req_info;
1233 GVariant *uuid_list, *manufacture_data;
1235 GVariantBuilder *builder = NULL;
1238 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1240 g_variant_unref(reply);
1242 g_object_unref(proxy);
1244 if (searching_info == NULL) {
1246 BT_ERR("unbonding_info == NULL");
1250 req_info = _bt_get_request_info(searching_info->req_id);
1251 if (req_info == NULL) {
1252 BT_ERR("req_info == NULL");
1257 g_dbus_error_strip_remote_error(err);
1258 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1260 if (g_strrstr("Operation canceled", err->message))
1261 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1262 else if (g_strrstr("In Progress", err->message))
1263 result = BLUETOOTH_ERROR_IN_PROGRESS;
1264 else if (g_strrstr("Host is down", err->message))
1265 result = BLUETOOTH_ERROR_HOST_DOWN;
1267 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1269 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1270 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1271 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1272 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1273 remote_dev_info->uuid_count > 0) {
1274 result = BLUETOOTH_ERROR_NONE;
1277 _bt_free_device_info(remote_dev_info);
1282 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1283 if (!remote_dev_info)
1287 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1288 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1289 g_variant_builder_add(builder, "s",
1290 remote_dev_info->uuids[i]);
1292 uuid_list = g_variant_new("as", builder);
1293 g_variant_builder_unref(builder);
1294 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1295 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1298 param = g_variant_new("(isunsbub@asn@ay)",
1300 searching_info->addr,
1301 remote_dev_info->class,
1302 remote_dev_info->rssi,
1303 remote_dev_info->name,
1304 remote_dev_info->paired,
1305 remote_dev_info->connected,
1306 remote_dev_info->trust,
1308 remote_dev_info->manufacturer_data_len,
1311 /* Send the event to application */
1312 _bt_send_event(BT_ADAPTER_EVENT,
1313 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1316 _bt_free_device_info(remote_dev_info);
1319 if (req_info->context == NULL)
1322 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1323 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1324 searching_info->addr);
1326 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1327 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1329 g_dbus_method_invocation_return_value(req_info->context,
1330 g_variant_new("(iv)", result, out_param1));
1333 _bt_delete_request_list(req_info->req_id);
1338 if (searching_info) {
1339 g_free(searching_info->addr);
1340 g_free(searching_info);
1341 searching_info = NULL;
1345 int _bt_search_device(int request_id,
1346 bluetooth_device_address_t *device_address)
1348 char *device_path = NULL;
1349 GDBusProxy *device_proxy = NULL;
1350 GDBusConnection *conn;
1352 GDBusProxy *adapter_proxy;
1353 int result = BLUETOOTH_ERROR_INTERNAL;
1355 BT_CHECK_PARAMETER(device_address, return);
1358 BT_ERR("Bonding in progress");
1359 return BLUETOOTH_ERROR_DEVICE_BUSY;
1362 if (searching_info) {
1363 BT_ERR("Service searching in progress");
1364 return BLUETOOTH_ERROR_DEVICE_BUSY;
1367 adapter_proxy = _bt_get_adapter_proxy();
1368 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1370 /* allocate user data so that it can be retrieved in callback */
1371 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1372 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1373 searching_info->req_id = request_id;
1375 _bt_convert_addr_type_to_string(searching_info->addr,
1376 device_address->addr);
1378 conn = _bt_gdbus_get_system_gconn();
1379 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1382 device_path = _bt_get_device_object_path(searching_info->addr);
1384 if (device_path == NULL) {
1385 BT_ERR("No paired device");
1386 result = BLUETOOTH_ERROR_NOT_PAIRED;
1390 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1391 NULL, BT_BLUEZ_NAME,
1392 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1393 g_free(device_path);
1394 if (device_proxy == NULL) {
1395 result = BLUETOOTH_ERROR_INTERNAL;
1399 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1400 g_variant_new("(s)", ""),
1401 G_DBUS_CALL_FLAGS_NONE,
1402 BT_MAX_DBUS_TIMEOUT,
1404 (GAsyncReadyCallback)__bt_discover_cb,
1407 searching_info->device_proxy = device_proxy;
1409 return BLUETOOTH_ERROR_NONE;
1412 g_free(searching_info->addr);
1413 g_free(searching_info);
1414 searching_info = NULL;
1418 int _bt_cancel_search_device(void)
1420 GVariant *ret = NULL;
1423 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1425 if (searching_info->device_proxy) {
1426 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1428 G_DBUS_CALL_FLAGS_NONE,
1433 g_variant_unref(ret);
1435 __bt_cancel_search_service_done();
1437 return BLUETOOTH_ERROR_NONE;
1440 int _bt_set_alias(bluetooth_device_address_t *device_address,
1443 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1444 gchar *device_path = NULL;
1445 GDBusProxy *adapter_proxy;
1446 GDBusProxy *device_proxy;
1447 GVariant *ret = NULL;
1448 GError *error = NULL;
1449 GDBusConnection *conn;
1451 BT_CHECK_PARAMETER(device_address, return);
1452 BT_CHECK_PARAMETER(alias, return);
1454 adapter_proxy = _bt_get_adapter_proxy();
1455 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1457 conn = _bt_gdbus_get_system_gconn();
1458 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1460 _bt_convert_addr_type_to_string(address, device_address->addr);
1462 device_path = _bt_get_device_object_path(address);
1464 if (device_path == NULL) {
1465 BT_ERR("No paired device");
1466 return BLUETOOTH_ERROR_NOT_PAIRED;
1469 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1470 NULL, BT_BLUEZ_NAME,
1471 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1473 g_free(device_path);
1474 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1476 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1477 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1478 G_DBUS_CALL_FLAGS_NONE,
1483 g_variant_unref(ret);
1485 g_object_unref(device_proxy);
1488 BT_ERR("SetProperty error: [%s]", error->message);
1489 g_error_free(error);
1490 return BLUETOOTH_ERROR_INTERNAL;
1493 return BLUETOOTH_ERROR_NONE;
1496 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1499 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1500 gchar *device_path = NULL;
1501 GDBusProxy *device_proxy;
1502 gboolean previous_value;
1503 GError *error = NULL;
1504 GDBusConnection *conn;
1505 GVariant *result = NULL;
1506 GVariant *temp = NULL;
1507 int ret = BLUETOOTH_ERROR_NONE;
1509 BT_CHECK_PARAMETER(device_address, return);
1511 conn = _bt_gdbus_get_system_gconn();
1512 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1514 _bt_convert_addr_type_to_string(address, device_address->addr);
1516 device_path = _bt_get_device_object_path(address);
1518 if (device_path == NULL) {
1519 BT_ERR("No paired device");
1520 return BLUETOOTH_ERROR_NOT_PAIRED;
1523 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1524 NULL, BT_BLUEZ_NAME,
1525 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1527 g_free(device_path);
1528 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1530 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1531 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1532 G_DBUS_CALL_FLAGS_NONE,
1536 if (error != NULL) {
1537 BT_ERR("Getting property failed: [%s]\n", error->message);
1538 g_error_free(error);
1539 g_object_unref(device_proxy);
1540 return BLUETOOTH_ERROR_INTERNAL;
1543 g_variant_get(result, "(v)", &temp);
1544 previous_value = g_variant_get_boolean(temp);
1545 g_variant_unref(temp);
1546 g_variant_unref(result);
1547 /* If the input is same with previous value, return error. */
1548 if (previous_value == authorize) {
1549 BT_ERR("Same value: %d", previous_value);
1550 g_object_unref(device_proxy);
1551 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1555 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1556 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1557 G_DBUS_CALL_FLAGS_NONE,
1562 g_object_unref(device_proxy);
1564 BT_ERR("SetProperty error: [%s]", error->message);
1565 g_error_free(error);
1566 ret = BLUETOOTH_ERROR_INTERNAL;
1570 g_variant_unref(result);
1575 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1576 gboolean *is_connected)
1578 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1579 char *object_path = NULL;
1581 GDBusProxy *device_proxy;
1582 GError *error = NULL;
1584 GDBusConnection *conn;
1585 GVariant *result = NULL;
1586 int ret = BLUETOOTH_ERROR_NONE;
1588 BT_CHECK_PARAMETER(device_address, return);
1590 conn = _bt_gdbus_get_system_gconn();
1591 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1593 _bt_convert_addr_type_to_string(address, device_address->addr);
1595 object_path = _bt_get_device_object_path(address);
1596 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1598 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1599 NULL, BT_BLUEZ_NAME, object_path,
1600 BT_PROPERTIES_INTERFACE, NULL, NULL);
1601 g_free(object_path);
1602 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1604 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1605 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1607 G_DBUS_CALL_FLAGS_NONE,
1611 if (result == NULL) {
1612 if (error != NULL) {
1613 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1614 g_error_free(error);
1616 g_object_unref(device_proxy);
1617 return BLUETOOTH_ERROR_INTERNAL;
1620 g_variant_get(result, "(v)", &value);
1621 *is_connected = g_variant_get_boolean(value);
1622 g_variant_unref(result);
1624 BT_DBG("gatt is connected : %d", *is_connected);
1626 g_variant_unref(value);
1627 g_object_unref(device_proxy);
1632 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1633 int connection_type, gboolean *is_connected)
1635 char *object_path = NULL;
1636 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1637 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1638 GDBusProxy *device_proxy = NULL;
1639 GDBusProxy *adapter_proxy = NULL;
1640 GDBusConnection *conn;
1641 GError *error = NULL;
1642 GVariant *tmp_value = NULL;
1643 GVariant *value = NULL;
1644 GVariant *result = NULL;
1647 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1648 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1650 _bt_convert_addr_type_to_string(address, device_address->addr);
1651 _bt_convert_addr_string_to_secure_string(secure_address, address);
1653 *is_connected = FALSE;
1654 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1656 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1657 return _bt_rfcomm_is_device_connected(device_address,
1659 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1660 return _bt_is_gatt_connected(device_address, is_connected);
1661 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1662 return _bt_pbap_is_connected(device_address, is_connected);
1664 adapter_proxy = _bt_get_adapter_proxy();
1665 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1667 conn = _bt_gdbus_get_system_gconn();
1668 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1670 _bt_convert_addr_type_to_string(address, device_address->addr);
1672 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1673 object_path = _bt_get_adapter_path();
1674 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1675 NULL, BT_BLUEZ_NAME,
1676 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1677 g_free(object_path);
1678 if (device_proxy == NULL) {
1679 BT_DBG("Device don't have this service");
1680 return BLUETOOTH_ERROR_INTERNAL;
1683 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1684 g_variant_new("(s)", address),
1685 G_DBUS_CALL_FLAGS_NONE,
1690 if (result == NULL) {
1691 BT_ERR("[GetProperties] Error occured in Proxy call");
1693 BT_ERR("%s", error->message);
1694 g_error_free(error);
1696 *is_connected = FALSE;
1697 g_object_unref(device_proxy);
1698 return BLUETOOTH_ERROR_NONE;
1700 g_variant_get(result , "(@a{sv})", &value);
1701 g_variant_unref(result);
1704 tmp_value = g_variant_lookup_value(value,
1706 G_VARIANT_TYPE_BOOLEAN);
1708 *is_connected = g_variant_get_boolean(tmp_value);
1709 g_variant_unref(tmp_value);
1711 g_variant_unref(value);
1713 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1714 return _bt_is_network_connected(_bt_get_net_conn(),
1715 device_address->addr, is_connected);
1717 uuid = _bt_get_profile_uuid128(connection_type);
1719 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1720 return BLUETOOTH_ERROR_INTERNAL;
1722 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1724 object_path = _bt_get_device_object_path(address);
1726 BT_ERR("object_path is NULL");
1728 return BLUETOOTH_ERROR_NOT_PAIRED;
1731 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1732 NULL, BT_BLUEZ_NAME,
1733 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1734 g_free(object_path);
1735 if (device_proxy == NULL) {
1736 BT_DBG("Device don't have this service");
1738 return BLUETOOTH_ERROR_INTERNAL;
1741 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1742 g_variant_new("(s)", uuid),
1743 G_DBUS_CALL_FLAGS_NONE,
1748 if (result == NULL) {
1749 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1751 BT_ERR("%s", error->message);
1752 if (g_strrstr(error->message, "Not Connected"))
1753 BT_DBG("Not connected");
1754 g_error_free(error);
1756 *is_connected = FALSE;
1757 g_object_unref(device_proxy);
1759 return BLUETOOTH_ERROR_NONE;
1761 g_variant_get(result, "(b)", is_connected);
1763 g_variant_unref(result);
1766 g_object_unref(device_proxy);
1767 return BLUETOOTH_ERROR_NONE;
1770 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1771 bluetooth_connected_link_t *connected)
1773 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1774 char *object_path = NULL;
1776 GDBusProxy *device_proxy;
1777 GError *error = NULL;
1778 GDBusConnection *conn;
1779 GVariant *value = NULL;
1780 GVariant *result = NULL;
1782 BT_CHECK_PARAMETER(device_address, return);
1784 conn = _bt_gdbus_get_system_gconn();
1785 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1787 _bt_convert_addr_type_to_string(address, device_address->addr);
1789 object_path = _bt_get_device_object_path(address);
1790 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1792 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1793 NULL, BT_BLUEZ_NAME,
1794 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1795 g_free(object_path);
1796 if (device_proxy == NULL) {
1797 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1798 return BLUETOOTH_ERROR_NONE;
1801 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1802 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1803 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1805 if (error != NULL) {
1806 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1807 g_error_free(error);
1808 g_object_unref(device_proxy);
1809 return BLUETOOTH_ERROR_INTERNAL;
1811 g_variant_get(result, "(v)", &value);
1812 *connected = g_variant_get_byte(value);
1813 g_variant_unref(value);
1814 g_variant_unref(result);
1817 g_object_unref(device_proxy);
1818 return BLUETOOTH_ERROR_NONE;
1821 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1825 GVariant *out_param1;
1827 int result = BLUETOOTH_ERROR_NONE;
1828 bt_function_data_t *func_data = user_data;
1829 request_info_t *req_info = NULL;
1830 bluetooth_device_address_t device_addr = { {0} };
1832 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1833 g_object_unref(proxy);
1835 if (reply == NULL) {
1836 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1838 BT_ERR("Error: %s", err->message);
1839 g_clear_error(&err);
1841 result = BLUETOOTH_ERROR_INTERNAL;
1843 g_variant_unref(reply);
1846 if (func_data == NULL) {
1847 BT_ERR("func_data is NULL");
1851 req_info = _bt_get_request_info(func_data->req_id);
1852 if (req_info == NULL) {
1853 BT_ERR("req_info is NULL");
1857 if (req_info->context == NULL) {
1858 BT_ERR("req_info->context is NULL");
1862 _bt_convert_addr_string_to_type(device_addr.addr,
1863 (const char *)func_data->address);
1865 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1866 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1869 g_dbus_method_invocation_return_value(req_info->context,
1870 g_variant_new("(iv)", result, out_param1));
1874 _bt_delete_request_list(req_info->req_id);
1877 g_free(func_data->address);
1882 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1883 gboolean auto_connect)
1885 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1886 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1887 gchar *device_path = NULL;
1888 GDBusProxy *device_proxy = NULL;
1889 GDBusConnection *conn;
1890 int ret = BLUETOOTH_ERROR_NONE;
1891 bt_function_data_t *func_data;
1893 BT_CHECK_PARAMETER(bd_addr, return);
1895 conn = _bt_gdbus_get_system_gconn();
1896 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1898 _bt_convert_addr_type_to_string(device_address,
1899 (unsigned char *)bd_addr->addr);
1900 device_path = _bt_get_device_object_path(device_address);
1901 if (device_path == NULL) {
1902 BT_ERR_C("device_path NULL : [%s]", device_address);
1903 ret = BLUETOOTH_ERROR_INTERNAL;
1907 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1908 NULL, BT_BLUEZ_NAME,
1909 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1910 g_free(device_path);
1911 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1913 func_data = g_malloc0(sizeof(bt_function_data_t));
1914 func_data->address = g_strdup(device_address);
1915 if (func_data->address == NULL) {
1916 BT_ERR("Unable to allocate memory for address");
1917 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1921 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1922 BT_INFO("Connect LE [%s]", secure_address);
1924 func_data->req_id = req_id;
1926 g_dbus_proxy_call(device_proxy, "ConnectLE",
1927 g_variant_new("(b)", auto_connect),
1928 G_DBUS_CALL_FLAGS_NONE,
1929 BT_MAX_DBUS_TIMEOUT,
1931 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1937 g_object_unref(device_proxy);
1939 g_free(func_data->address);
1945 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1949 BT_INFO("Try to initiate pending LE connection");
1951 pending_le_conn_timer_id = 0;
1953 ret = __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1954 &pending_le_conn_info->bd_addr,
1955 pending_le_conn_info->auto_connect);
1956 if (ret != BLUETOOTH_ERROR_NONE) {
1957 request_info_t *req_info = NULL;
1958 GVariant *out_param1;
1960 req_info = _bt_get_request_info(pending_le_conn_info->req_id);
1961 if (req_info == NULL) {
1962 BT_ERR("req_info is NULL");
1966 if (req_info->context == NULL) {
1967 BT_ERR("req_info->context is NULL");
1968 _bt_delete_request_list(req_info->req_id);
1972 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1973 &pending_le_conn_info->bd_addr,
1974 sizeof(bluetooth_device_address_t), TRUE,
1977 g_dbus_method_invocation_return_value(req_info->context,
1978 g_variant_new("(iv)", ret, out_param1));
1982 g_free(pending_le_conn_info);
1983 pending_le_conn_info = NULL;
1988 void _bt_pending_connect_le_device(void)
1990 if (pending_le_conn_timer_id > 0) {
1991 g_source_remove(pending_le_conn_timer_id);
1992 __bt_connect_le_timer_cb(NULL);
1996 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1997 gboolean auto_connect)
1999 int ret = BLUETOOTH_ERROR_NONE;
2001 BT_CHECK_PARAMETER(bd_addr, return);
2003 ret = _bt_hold_current_advertising();
2004 if (ret == BLUETOOTH_ERROR_NONE) {
2005 BT_INFO("Current advertising is held");
2006 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2007 pending_le_conn_info->req_id = req_id;
2008 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2009 BLUETOOTH_ADDRESS_LENGTH);
2010 pending_le_conn_info->auto_connect = auto_connect;
2012 pending_le_conn_timer_id =
2013 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2015 return BLUETOOTH_ERROR_NONE;
2016 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
2017 BT_ERR("Unable to hold advertising");
2020 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2023 int _bt_disconnect_le_device(int req_id,
2024 const bluetooth_device_address_t *bd_addr)
2026 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2027 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2029 GDBusProxy *device_proxy;
2030 GDBusConnection *conn;
2031 int ret = BLUETOOTH_ERROR_NONE;
2032 bt_function_data_t *func_data;
2034 BT_CHECK_PARAMETER(bd_addr, return);
2036 conn = _bt_gdbus_get_system_gconn();
2037 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2039 _bt_convert_addr_type_to_string(device_address,
2040 (unsigned char *)bd_addr->addr);
2041 device_path = _bt_get_device_object_path(device_address);
2042 if (device_path == NULL) {
2043 BT_DBG("device_path NULL");
2044 ret = BLUETOOTH_ERROR_INTERNAL;
2048 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2050 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2051 NULL, BT_BLUEZ_NAME,
2052 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2053 g_free(device_path);
2054 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2056 func_data = g_malloc0(sizeof(bt_function_data_t));
2057 func_data->address = g_strdup(device_address);
2058 if (func_data->address == NULL) {
2059 BT_ERR("Unable to allocate memory for address");
2060 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2064 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2065 BT_INFO("Disconnect LE [%s]", secure_address);
2067 func_data->req_id = req_id;
2069 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2071 G_DBUS_CALL_FLAGS_NONE,
2072 BT_MAX_DBUS_TIMEOUT,
2074 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2079 g_object_unref(device_proxy);
2081 g_free(func_data->address);
2087 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2089 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2090 gchar *device_path = NULL;
2091 GError *error = NULL;
2092 GDBusProxy *device_proxy = NULL;
2093 GDBusProxy *adapter_proxy;
2094 GDBusConnection *conn;
2095 int ret = BLUETOOTH_ERROR_NONE;
2097 BT_CHECK_PARAMETER(bd_addr, return);
2099 _bt_convert_addr_type_to_string(device_address,
2100 (unsigned char *)bd_addr->addr);
2102 conn = _bt_gdbus_get_system_gconn();
2103 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2105 adapter_proxy = _bt_get_adapter_proxy();
2106 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2108 device_path = _bt_get_device_object_path(device_address);
2109 if (device_path == NULL) {
2110 BT_DBG("device_path NULL");
2111 ret = BLUETOOTH_ERROR_INTERNAL;
2115 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2117 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2118 NULL, BT_BLUEZ_NAME,
2119 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2120 g_free(device_path);
2121 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2123 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2125 G_DBUS_CALL_FLAGS_NONE,
2130 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2131 g_error_free(error);
2132 g_object_unref(device_proxy);
2133 return BLUETOOTH_ERROR_INTERNAL;
2135 g_object_unref(device_proxy);
2137 /* IPSP daemon launch */
2138 GDBusProxy *ipsp_proxy;
2140 ipsp_proxy = _bt_get_ipsp_proxy();
2141 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2143 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2144 NULL, G_DBUS_CALL_FLAGS_NONE,
2145 -1, NULL, NULL, NULL);
2150 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2152 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2153 gchar *device_path = NULL;
2154 GError *error = NULL;
2155 GDBusProxy *device_proxy = NULL;
2156 GDBusProxy *adapter_proxy;
2157 GDBusConnection *conn;
2158 int ret = BLUETOOTH_ERROR_NONE;
2160 BT_CHECK_PARAMETER(bd_addr, return);
2162 _bt_convert_addr_type_to_string(device_address,
2163 (unsigned char *)bd_addr->addr);
2165 conn = _bt_gdbus_get_system_gconn();
2166 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2168 adapter_proxy = _bt_get_adapter_proxy();
2169 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2171 device_path = _bt_get_device_object_path(device_address);
2172 if (device_path == NULL) {
2173 BT_DBG("device_path NULL");
2174 ret = BLUETOOTH_ERROR_INTERNAL;
2178 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2180 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2181 NULL, BT_BLUEZ_NAME,
2182 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2183 g_free(device_path);
2184 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2186 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2188 G_DBUS_CALL_FLAGS_NONE,
2193 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2194 g_error_free(error);
2195 g_object_unref(device_proxy);
2196 return BLUETOOTH_ERROR_INTERNAL;
2199 g_object_unref(device_proxy);
2204 int _bt_connect_profile(char *address, char *uuid,
2205 void *cb, gpointer func_data)
2209 GDBusConnection *conn;
2210 GDBusProxy *adapter_proxy;
2211 GVariant *result = NULL;
2212 GError *error = NULL;
2214 conn = _bt_gdbus_get_system_gconn();
2215 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2217 object_path = _bt_get_device_object_path(address);
2218 if (object_path == NULL) {
2219 BT_ERR("No searched device");
2221 adapter_proxy = _bt_get_adapter_proxy();
2222 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2224 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2225 g_variant_new("(s)", address),
2226 G_DBUS_CALL_FLAGS_NONE,
2231 if (error != NULL) {
2232 BT_ERR("CreateDevice Fail: %s", error->message);
2233 g_error_free(error);
2236 g_variant_unref(result);
2238 object_path = _bt_get_device_object_path(address);
2240 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2242 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2243 NULL, BT_BLUEZ_NAME,
2244 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2245 g_free(object_path);
2246 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2248 g_dbus_proxy_call(proxy, "ConnectProfile",
2249 g_variant_new("(s)", uuid),
2250 G_DBUS_CALL_FLAGS_NONE,
2251 BT_MAX_DBUS_TIMEOUT,
2253 (GAsyncReadyCallback)cb,
2256 return BLUETOOTH_ERROR_NONE;
2259 int _bt_disconnect_all(char *address)
2261 int ret = BLUETOOTH_ERROR_NONE;
2264 GDBusConnection *conn;
2265 GVariant *result = NULL;
2269 conn = _bt_gdbus_get_system_gconn();
2270 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2272 object_path = _bt_get_device_object_path(address);
2273 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2275 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2276 NULL, BT_BLUEZ_NAME,
2277 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2278 g_free(object_path);
2279 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2281 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2283 G_DBUS_CALL_FLAGS_NONE,
2288 BT_ERR("Dbus Call Error:[%s]", err->message);
2290 ret = BLUETOOTH_ERROR_INTERNAL;
2293 g_object_unref(proxy);
2295 g_variant_unref(result);
2300 int _bt_disconnect_profile(char *address, char *uuid,
2301 void *cb, gpointer func_data)
2305 GDBusConnection *conn;
2307 conn = _bt_gdbus_get_system_gconn();
2308 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2310 object_path = _bt_get_device_object_path(address);
2311 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2313 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2314 NULL, BT_BLUEZ_NAME,
2315 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2316 g_free(object_path);
2317 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2319 g_dbus_proxy_call(proxy, "DisconnectProfile",
2320 g_variant_new("(s)", uuid),
2321 G_DBUS_CALL_FLAGS_NONE,
2322 BT_MAX_DBUS_TIMEOUT,
2324 (GAsyncReadyCallback)cb,
2327 return BLUETOOTH_ERROR_NONE;
2330 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2331 int low_threshold, int in_range_threshold, int high_threshold)
2333 int ret = BLUETOOTH_ERROR_NONE;
2335 GVariant *result = NULL;
2336 GError *error = NULL;
2337 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2339 BT_CHECK_PARAMETER(bd_addr, return);
2340 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2341 bd_addr->addr[0], bd_addr->addr[1],
2342 bd_addr->addr[2], bd_addr->addr[3],
2343 bd_addr->addr[4], bd_addr->addr[5],
2345 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2346 in_range_threshold, high_threshold);
2348 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2350 proxy = _bt_get_adapter_proxy();
2351 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2353 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2354 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2355 G_DBUS_CALL_FLAGS_NONE,
2359 if (error != NULL) {
2360 BT_ERR("Dbus Call Error:[%s]", error->message);
2361 g_error_free(error);
2362 ret = BLUETOOTH_ERROR_INTERNAL;
2366 g_variant_unref(result);
2371 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2374 int ret = BLUETOOTH_ERROR_NONE;
2376 GVariant *result = NULL;
2377 GError *error = NULL;
2378 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2380 BT_CHECK_PARAMETER(bd_addr, return);
2381 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2382 bd_addr->addr[0], bd_addr->addr[1],
2383 bd_addr->addr[2], bd_addr->addr[3],
2384 bd_addr->addr[4], bd_addr->addr[5],
2387 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2389 proxy = _bt_get_adapter_proxy();
2390 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2392 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2393 g_variant_new("(si)", address, link_type),
2394 G_DBUS_CALL_FLAGS_NONE,
2399 if (error != NULL) {
2400 BT_ERR("Dbus Call Error:[%s]", error->message);
2401 g_error_free(error);
2402 ret = BLUETOOTH_ERROR_INTERNAL;
2406 g_variant_unref(result);
2411 static int __bt_le_set_conn_parameter(const char *address,
2412 float interval_min, float interval_max,
2413 guint16 latency, guint16 time_out)
2415 gchar *device_path = NULL;
2416 GError *error = NULL;
2417 GDBusProxy *device_proxy = NULL;
2418 GDBusConnection *conn;
2420 guint32 min, max, to;
2421 int ret = BLUETOOTH_ERROR_NONE;
2423 BT_CHECK_PARAMETER(address, return);
2425 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2426 interval_min, interval_max, latency, time_out);
2428 device_path = _bt_get_device_object_path((char *)address);
2430 if (device_path == NULL) {
2431 BT_ERR("device_path NULL");
2432 ret = BLUETOOTH_ERROR_INTERNAL;
2436 conn = _bt_gdbus_get_system_gconn();
2438 BT_ERR("conn NULL");
2439 ret = BLUETOOTH_ERROR_INTERNAL;
2443 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2444 NULL, BT_BLUEZ_NAME,
2445 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2447 g_free(device_path);
2448 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2450 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2451 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2452 to = time_out / BT_LE_CONN_TO_SPLIT;
2454 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2455 g_variant_new("(uuuu)", min, max, latency, to),
2456 G_DBUS_CALL_FLAGS_NONE,
2461 g_object_unref(device_proxy);
2462 if (reply == NULL) {
2464 BT_ERR("Error %s[%s]", error->message, address);
2465 if (g_strrstr(error->message, "In Progress"))
2466 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2468 ret = BLUETOOTH_ERROR_INTERNAL;
2469 g_error_free(error);
2473 g_variant_unref(reply);
2479 static void __bt_le_conn_param_free(void *data)
2481 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2483 BT_DBG("%s", param->sender);
2484 g_free(param->sender);
2488 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2490 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2491 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2493 return parama->key > paramb->key;
2496 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2499 bt_connected_le_dev_t *dev;
2504 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2507 if (g_strcmp0(dev->address, address) == 0)
2513 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2516 bt_le_conn_param_t *param = NULL;
2518 if (!dev || !sender)
2521 for (l = dev->senders; l; l = g_slist_next(l)) {
2523 if (g_strcmp0(param->sender, sender) == 0)
2530 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2531 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2533 bt_connected_le_dev_t *dev = NULL;
2534 bt_le_conn_param_t *param = NULL;
2535 bt_le_conn_param_t *data = NULL;
2537 if (!address || !sender)
2538 return BLUETOOTH_ERROR_INVALID_PARAM;
2540 dev = __bt_get_le_connected_dev_info(address);
2542 return BLUETOOTH_ERROR_INTERNAL;
2544 param = __bt_get_le_conn_param_info(dev, sender);
2546 data = g_malloc0(sizeof(bt_le_conn_param_t));
2547 data->sender = g_strdup(sender);
2548 data->interval_min = interval_min;
2549 data->interval_max = interval_max;
2550 data->latency = latency;
2551 data->time_out = time_out;
2552 data->key = interval_min + (interval_max - interval_min)/2;
2554 if (param == NULL) {
2555 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2556 dev->senders = g_slist_append(dev->senders, data);
2558 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2559 dev->senders = g_slist_remove(dev->senders, param);
2560 g_free(param->sender);
2562 dev->senders = g_slist_append(dev->senders, data);
2565 /* Sorting. First element have the minimum interval */
2566 dev->senders = g_slist_sort(dev->senders,
2567 (GCompareFunc)__bt_compare_le_conn_param_key);
2569 return BLUETOOTH_ERROR_NONE;
2572 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2574 bt_connected_le_dev_t *dev = NULL;
2575 bt_le_conn_param_t *param = NULL;
2577 if (!address || !sender)
2578 return BLUETOOTH_ERROR_INVALID_PARAM;
2580 dev = __bt_get_le_connected_dev_info(address);
2582 return BLUETOOTH_ERROR_INTERNAL;
2584 param = __bt_get_le_conn_param_info(dev, sender);
2586 BT_DBG("Remove param %s %s ", address, sender);
2587 dev->senders = g_slist_remove(dev->senders, param);
2588 g_free(param->sender);
2592 *is_removed = FALSE;
2594 return BLUETOOTH_ERROR_NONE;
2597 int _bt_remove_all_le_conn_param_info(const char *sender)
2600 bt_connected_le_dev_t *dev = NULL;
2601 bt_le_conn_param_t *param = NULL;
2602 gboolean is_removed = FALSE;
2603 char *sender_new = NULL;
2604 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2605 int ret = BLUETOOTH_ERROR_NONE;
2608 return BLUETOOTH_ERROR_INVALID_PARAM;
2610 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2612 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2615 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2616 g_slist_length(dev->senders));
2617 if (g_slist_length(dev->senders) > 0) {
2618 param = dev->senders->data;
2619 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2620 param->interval_min, param->interval_max);
2622 if (dev->interval_min != param->interval_min ||
2623 dev->interval_max != param->interval_max) {
2624 sender_new = g_strdup(param->sender);
2626 _bt_convert_addr_string_to_type(addr, dev->address);
2627 ret = _bt_le_conn_update(sender_new, addr,
2628 param->interval_min, param->interval_max,
2629 param->latency, param->time_out);
2632 if (ret != BLUETOOTH_ERROR_NONE)
2633 BT_ERR("Unable to set le connection parameter");
2636 BT_INFO("Set the default interval");
2638 bluetooth_le_connection_param_t param = { 0 };
2639 _bt_get_le_connection_parameter(
2640 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2643 ret = __bt_le_set_conn_parameter(dev->address,
2644 param.interval_min, param.interval_max,
2645 param.latency, param.timeout);
2646 if (ret == BLUETOOTH_ERROR_NONE) {
2647 dev->interval_min = param.interval_min;
2648 dev->interval_max = param.interval_max;
2657 void _bt_add_le_connected_dev_info(const char *address)
2659 bt_connected_le_dev_t *dev = NULL;
2664 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2665 dev->address = g_strdup(address);
2667 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2672 void _bt_remove_le_connected_dev_info(const char *address)
2674 bt_connected_le_dev_t *dev = NULL;
2679 dev = __bt_get_le_connected_dev_info(address);
2683 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2684 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2685 g_free(dev->address);
2691 int _bt_le_conn_update(const char *sender,
2692 unsigned char *device_address,
2693 float interval_min, float interval_max,
2694 guint16 latency, guint16 time_out)
2696 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2697 guint32 min_supervision_to;
2698 bt_connected_le_dev_t *dev = NULL;
2699 bt_le_conn_param_t *param = NULL;
2700 gboolean is_removed = FALSE;
2701 int ret = BLUETOOTH_ERROR_NONE;
2703 BT_CHECK_PARAMETER(device_address, return);
2705 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2706 sender, interval_min, interval_max, latency, time_out);
2708 if (interval_min > interval_max ||
2709 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2710 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2711 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2715 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2716 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2717 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2721 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2722 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2727 * The Supervision_Timeout in milliseconds shall be larger than
2728 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2729 * where Conn_Interval_Max is given in milliseconds.
2731 min_supervision_to = (1 + latency) * interval_max * 2;
2732 if (time_out <= min_supervision_to) {
2733 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2737 _bt_convert_addr_type_to_string(address, device_address);
2738 BT_DBG("Remote device address: %s", address);
2740 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2742 dev = __bt_get_le_connected_dev_info(address);
2744 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2748 if (g_slist_length(dev->senders) == 1)
2751 param = dev->senders->data;
2753 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2755 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2756 BT_DBG("Skip due to same interval");
2760 interval_min = param->interval_min;
2761 interval_max = param->interval_max;
2765 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2767 if (ret != BLUETOOTH_ERROR_NONE) {
2768 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2772 dev->interval_min = interval_min;
2773 dev->interval_max = interval_max;
2779 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2780 bluetooth_device_pin_code_t *pin_code)
2782 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2784 bt_pin_code_info_t *pin_info = NULL;
2786 BT_CHECK_PARAMETER(device_address, return);
2787 BT_CHECK_PARAMETER(pin_code, return);
2788 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2789 BLUETOOTH_ERROR_NO_RESOURCES);
2791 _bt_convert_addr_type_to_string(address, device_address->addr);
2793 for (l = pin_info_list; l != NULL; l = l->next) {
2796 if (g_strcmp0(pin_info->address, address) == 0) {
2797 g_free(pin_info->pin_code);
2798 pin_info->pin_code = g_strdup(pin_code->pin_code);
2799 return BLUETOOTH_ERROR_NONE;
2803 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2804 pin_info->address = g_strdup(address);
2805 pin_info->pin_code = g_strdup(pin_code->pin_code);
2806 pin_info_list = g_slist_append(pin_info_list, pin_info);
2808 return BLUETOOTH_ERROR_NONE;
2811 gint __bt_compare_address(gpointer *a, gpointer *b)
2813 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2814 char *address = (char *)b;
2815 return g_strcmp0(pin_info->address, address);
2818 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2820 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2822 bt_pin_code_info_t *pin_info = NULL;
2824 BT_CHECK_PARAMETER(device_address, return);
2826 _bt_convert_addr_type_to_string(address, device_address->addr);
2828 l = g_slist_find_custom(pin_info_list, address,
2829 (GCompareFunc)__bt_compare_address);
2833 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2834 g_free(pin_info->address);
2835 g_free(pin_info->pin_code);
2839 return BLUETOOTH_ERROR_NONE;
2842 int _bt_get_device_pin_code(const char *address, char *pin_code)
2846 BT_CHECK_PARAMETER(address, return);
2847 BT_CHECK_PARAMETER(pin_code, return);
2849 for (l = pin_info_list; l != NULL; l = l->next) {
2850 bt_pin_code_info_t *pin_info = l->data;
2852 if (g_strcmp0(pin_info->address, address) == 0) {
2853 g_strlcpy(pin_code, pin_info->pin_code,
2854 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2856 return BLUETOOTH_ERROR_NONE;
2860 return BLUETOOTH_ERROR_NOT_FOUND;
2863 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2864 bluetooth_le_connection_param_t *param)
2867 return BLUETOOTH_ERROR_INVALID_PARAM;
2869 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2870 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2871 return BLUETOOTH_ERROR_INVALID_PARAM;
2873 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2876 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2877 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2878 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2879 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2880 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2883 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2884 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2885 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2886 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2887 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2890 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2891 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2892 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2893 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2894 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2898 BT_ERR("Unhandled mode : %d", mode);
2902 return BLUETOOTH_ERROR_NONE;
2905 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2906 guint trusted_profile_flag, guint *trusted)
2908 int trust_profile = FALSE;
2912 case TRUSTED_PROFILE_PBAP:
2913 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2914 trust_profile = trusted_profile_flag & (1 << 1);
2916 return BLUETOOTH_ERROR_NOT_SUPPORT;
2918 case TRUSTED_PROFILE_MAP:
2919 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2920 trust_profile = trusted_profile_flag & (1 << 3);
2922 return BLUETOOTH_ERROR_NOT_SUPPORT;
2924 case TRUSTED_PROFILE_SAP:
2925 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2926 trust_profile = trusted_profile_flag & (1 << 5);
2928 return BLUETOOTH_ERROR_NOT_SUPPORT;
2930 case TRUSTED_PROFILE_HFP_HF: {
2931 guint flag = (trusted_profile_flag & (PROFILE_SUPPORTED << 6)) >> 6;
2932 BT_DBG("trusted_profile_flag %x", trusted_profile_flag);
2933 if (flag != PROFILE_BLOCKED)
2934 trust_profile = TRUE;
2939 case TRUSTED_PROFILE_A2DP: {
2940 guint flag = (trusted_profile_flag & (PROFILE_SUPPORTED << 8)) >> 8;
2941 BT_DBG("trusted_profile_flag %x", trusted_profile_flag);
2942 if (flag != PROFILE_BLOCKED)
2943 trust_profile = TRUE;
2948 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2949 *trusted = trusted_profile_flag;
2950 return BLUETOOTH_ERROR_NONE;
2952 return BLUETOOTH_ERROR_NOT_SUPPORT;
2958 return BLUETOOTH_ERROR_NONE;
2961 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2964 case TRUSTED_PROFILE_PBAP:
2965 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2966 case TRUSTED_PROFILE_MAP:
2967 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2968 case TRUSTED_PROFILE_SAP:
2969 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2970 case TRUSTED_PROFILE_HFP_HF:
2971 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2972 case TRUSTED_PROFILE_A2DP:
2973 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2974 case TRUSTED_PROFILE_ALL:
2981 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2983 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2984 return TRUSTED_PROFILE_PBAP;
2985 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2986 return TRUSTED_PROFILE_MAP;
2987 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2988 return TRUSTED_PROFILE_SAP;
2990 return 0; /* 0 - Unknown Profile */
2993 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2994 bluetooth_trusted_profile_t profile, gboolean trust)
2996 int ret = BLUETOOTH_ERROR_NONE;
2997 GDBusConnection *conn;
2999 GError *error = NULL;
3000 char *device_path = NULL;
3002 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3005 BT_CHECK_PARAMETER(bd_addr, return);
3006 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3007 bd_addr->addr[0], bd_addr->addr[1],
3008 bd_addr->addr[2], bd_addr->addr[3],
3009 bd_addr->addr[4], bd_addr->addr[5],
3012 conn = _bt_gdbus_get_system_gconn();
3013 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3015 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3017 device_path = _bt_get_device_object_path(address);
3018 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3020 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3021 NULL, BT_BLUEZ_NAME, device_path,
3022 BT_DEVICE_INTERFACE, NULL, NULL);
3024 g_free(device_path);
3025 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3027 uuid = _bt_get_trusted_profile_uuid(profile);
3029 g_object_unref(proxy);
3030 return BLUETOOTH_ERROR_NOT_SUPPORT;
3033 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3034 g_variant_new("(sb)", uuid, trust),
3035 G_DBUS_CALL_FLAGS_NONE, -1,
3037 g_object_unref(proxy);
3039 if (reply == NULL) {
3040 BT_ERR("Failed to Set Profile Trusted");
3041 ret = BLUETOOTH_ERROR_INTERNAL;
3043 BT_ERR("Error %s[%s]", error->message, address);
3044 g_error_free(error);
3048 g_variant_unref(reply);
3055 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3056 bluetooth_trusted_profile_t profile, guint *trust)
3058 int ret = BLUETOOTH_ERROR_NONE;
3059 GDBusConnection *conn;
3061 GError *error = NULL;
3062 char *device_path = NULL;
3063 guint trusted_profile_flag;
3064 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3067 BT_CHECK_PARAMETER(bd_addr, return);
3068 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3069 bd_addr->addr[0], bd_addr->addr[1],
3070 bd_addr->addr[2], bd_addr->addr[3],
3071 bd_addr->addr[4], bd_addr->addr[5],
3074 conn = _bt_gdbus_get_system_gconn();
3075 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3077 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3079 device_path = _bt_get_device_object_path(address);
3080 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3082 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3083 NULL, BT_BLUEZ_NAME, device_path,
3084 BT_PROPERTIES_INTERFACE, NULL, NULL);
3086 g_free(device_path);
3087 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3089 reply = g_dbus_proxy_call_sync(proxy, "Get",
3090 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3091 G_DBUS_CALL_FLAGS_NONE, -1,
3093 g_object_unref(proxy);
3095 if (reply == NULL) {
3096 BT_ERR("Failed to Get Profile Trusted");
3097 ret = BLUETOOTH_ERROR_INTERNAL;
3099 BT_ERR("Error %s[%s]", error->message, address);
3100 g_error_free(error);
3105 g_variant_get(reply, "(v)", &temp);
3106 trusted_profile_flag = g_variant_get_uint32(temp);
3107 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3109 ret = _bt_get_trusted_profile_from_flag(profile,
3110 trusted_profile_flag, trust);
3111 g_variant_unref(temp);
3112 g_variant_unref(reply);
3115 BT_DBG("TRUST %d", *trust);
3119 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3123 GVariant *out_param1;
3124 request_info_t *req_info;
3125 GVariant *val = NULL;
3126 GVariant *param = NULL;
3129 bluetooth_device_address_t device_addr = { {0} };
3130 int result = BLUETOOTH_ERROR_NONE;
3133 val = g_dbus_proxy_call_finish(proxy, res, &err);
3135 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3136 if (req_info == NULL) {
3137 BT_ERR("req_info == NULL");
3138 g_object_unref(proxy);
3139 att_mtu_req_info->device_proxy = NULL;
3144 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3146 if (g_strrstr(err->message, "NotSupported")) {
3147 BT_INFO("Connection Not Supported");
3148 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3149 } else if (g_strrstr(err->message, "NotConnected")) {
3150 BT_INFO("Not connected");
3151 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3152 } else if (g_strrstr(err->message, "InvalidArguments")) {
3153 BT_INFO("Not connected");
3154 result = BLUETOOTH_ERROR_INVALID_PARAM;
3156 BT_DBG("Default case");
3157 result = BLUETOOTH_ERROR_INTERNAL;
3161 g_object_unref(proxy);
3162 att_mtu_req_info->device_proxy = NULL;
3164 if (result != BLUETOOTH_ERROR_NONE)
3168 g_variant_get(val, "(qy)", &mtu, &status);
3169 g_variant_unref(val);
3172 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3174 param = g_variant_new("(isqy)",
3176 att_mtu_req_info->addr,
3180 /* Send the event to application */
3181 _bt_send_event(BT_DEVICE_EVENT,
3182 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3186 if (req_info->context == NULL)
3189 _bt_convert_addr_string_to_type(device_addr.addr,
3190 (const char *)att_mtu_req_info->addr);
3192 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3193 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3194 g_dbus_method_invocation_return_value(req_info->context,
3195 g_variant_new("(iv)", result, out_param1));
3197 _bt_delete_request_list(req_info->req_id);
3202 g_free(att_mtu_req_info->addr);
3203 g_free(att_mtu_req_info);
3204 att_mtu_req_info = NULL;
3209 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3212 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3213 gchar *device_path = NULL;
3214 GDBusProxy *adapter_proxy;
3215 GDBusProxy *device_proxy;
3216 GDBusConnection *conn;
3217 int ret = BLUETOOTH_ERROR_NONE;
3219 BT_CHECK_PARAMETER(device_address, return);
3221 if (att_mtu_req_info) {
3222 BT_ERR("ATT MTU request in progress");
3223 return BLUETOOTH_ERROR_DEVICE_BUSY;
3226 conn = _bt_gdbus_get_system_gconn();
3227 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3229 adapter_proxy = _bt_get_adapter_proxy();
3230 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3232 _bt_convert_addr_type_to_string(address, device_address->addr);
3234 BT_DBG("Remote device address: %s", address);
3236 device_path = _bt_get_device_object_path(address);
3238 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3240 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3241 NULL, BT_BLUEZ_NAME,
3242 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3243 g_free(device_path);
3244 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3246 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3247 att_mtu_req_info->addr = (char *)g_strdup(address);
3248 att_mtu_req_info->req_id = request_id;
3249 att_mtu_req_info->device_proxy = device_proxy;
3251 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3252 g_variant_new("(q)", mtu),
3253 G_DBUS_CALL_FLAGS_NONE,
3254 BT_MAX_DBUS_TIMEOUT,
3256 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3262 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3265 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3266 char *object_path = NULL;
3268 GDBusProxy *device_proxy;
3269 GError *error = NULL;
3271 GDBusConnection *conn;
3272 GVariant *result = NULL;
3274 BT_CHECK_PARAMETER(device_address, return);
3276 conn = _bt_gdbus_get_system_gconn();
3277 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3279 _bt_convert_addr_type_to_string(address, device_address->addr);
3281 object_path = _bt_get_device_object_path(address);
3282 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3284 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3285 NULL, BT_BLUEZ_NAME, object_path,
3286 BT_PROPERTIES_INTERFACE, NULL, NULL);
3287 g_free(object_path);
3288 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3290 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3291 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3292 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3294 ERR("DBus Error : %s", error->message);
3295 g_clear_error(&error);
3296 g_object_unref(device_proxy);
3297 return BLUETOOTH_ERROR_INTERNAL;
3299 g_variant_get(result, "(v)", &value);
3300 *mtu = g_variant_get_uint16(value);
3301 BT_DBG("ATT MTU : %d", *mtu);
3302 g_variant_unref(value);
3303 g_variant_unref(result);
3306 g_object_unref(device_proxy);
3307 return BLUETOOTH_ERROR_NONE;
3310 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3311 bluetooth_device_address_t *id_address)
3313 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3314 gchar *device_path = NULL;
3315 const gchar *idaddress = NULL;
3316 GDBusProxy *device_proxy;
3317 GError *error = NULL;
3318 GVariant *result = NULL;
3319 GDBusConnection *conn;
3320 int ret = BLUETOOTH_ERROR_NONE;
3322 BT_CHECK_PARAMETER(device_address, return);
3324 conn = _bt_gdbus_get_system_gconn();
3325 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3327 _bt_convert_addr_type_to_string(address, device_address->addr);
3329 device_path = _bt_get_device_object_path(address);
3330 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3332 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3333 NULL, BT_BLUEZ_NAME,
3334 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3335 g_free(device_path);
3336 if (!device_proxy) {
3337 BT_ERR("Unable to get proxy");
3338 return BLUETOOTH_ERROR_INTERNAL;
3341 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3342 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3343 if (result == NULL) {
3344 BT_ERR("Failed to get device ID address");
3345 if (error != NULL) {
3346 BT_ERR("Error occured in Proxy call [%s]", error->message);
3347 g_error_free(error);
3349 g_object_unref(device_proxy);
3350 return BLUETOOTH_ERROR_INTERNAL;
3353 g_variant_get(result , "(&s)", &idaddress);
3354 if (idaddress == NULL) {
3355 BT_ERR("No paired device");
3356 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3358 DBG_SECURE("ID Address : %s", idaddress);
3359 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3362 g_variant_unref(result);
3363 g_object_unref(device_proxy);
3368 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3370 GapAgentPrivate *agent = _bt_get_adapter_agent();
3371 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3373 if (authentication_reply)
3374 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3376 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3378 BT_DBG("BT_PASSKEY_REPLY");
3379 return BLUETOOTH_ERROR_NONE;
3382 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3384 GapAgentPrivate *agent = _bt_get_adapter_agent();
3385 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3387 if (confirmation_reply)
3388 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3390 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3392 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3393 return BLUETOOTH_ERROR_NONE;