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 = g_strdup(address);
461 dev_info->name = g_strdup(name);
463 g_variant_unref(value);
465 BT_ERR("result is NULL\n");
473 char *_bt_get_device_name(const char *bdaddress)
475 char *device_path = NULL;
476 const gchar *name = NULL;
477 gchar *dev_name = NULL;
479 GVariant *result = NULL;
481 GDBusProxy *device_proxy;
482 GDBusConnection *conn;
484 retv_if(bdaddress == NULL, NULL);
486 device_path = _bt_get_device_object_path((char *)bdaddress);
487 retv_if(device_path == NULL, NULL);
489 conn = _bt_gdbus_get_system_gconn();
490 retv_if(conn == NULL, NULL);
491 INFO_SECURE("Device_path %s", device_path);
492 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
493 NULL, BT_BLUEZ_NAME, device_path,
494 BT_PROPERTIES_INTERFACE, NULL, &err);
497 retv_if(device_proxy == NULL, NULL);
499 result = g_dbus_proxy_call_sync(device_proxy, "Get",
500 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
501 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
503 BT_ERR("DBus Error : %s", err->message);
507 g_variant_get(result, "(v)", &value);
508 name = g_variant_get_string(value, &name_len);
509 INFO_SECURE("Alias Name [%s]", name);
511 dev_name = g_strdup(name);
512 g_variant_unref(value);
513 g_variant_unref(result);
518 result = g_dbus_proxy_call_sync(device_proxy, "Get",
519 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
520 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
522 ERR("DBus Error : %s", err->message);
525 g_variant_get(result, "(v)", &value);
526 name = g_variant_get_string(value, &name_len);
527 INFO_SECURE("Name = %s", name);
529 dev_name = g_strdup(name);
530 g_variant_unref(value);
531 g_variant_unref(result);
535 g_object_unref(device_proxy);
539 static gboolean __ignore_auto_pairing_request(const char *address)
544 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
555 /* Get the LAP(Lower Address part) */
556 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
558 snprintf(lap_address, sizeof(lap_address), ",%s", address);
560 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
563 BT_ERR("fopen failed \n");
567 fseek(fp, 0, SEEK_END);
572 BT_ERR("Get file size failed \n");
577 buffer = g_malloc0(sizeof(char) * size);
578 result = fread((char *)buffer, 1, size, fp);
580 if (result != size) {
581 BT_ERR("Read Error\n");
586 BT_DBG("Buffer = %s\n", buffer);
588 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
594 /* Write the data and insert new device data */
595 for (i = 0; lines[i] != NULL; i++) {
596 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
597 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
599 lines[i] = temp_buffer;
602 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
604 /* Fix : NULL_RETURNS */
605 retv_if(buffer == NULL, FALSE);
607 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
610 BT_ERR("fopen failed \n");
615 BT_DBG("Buffer = %s\n", buffer);
616 fwrite(buffer, 1, strlen(buffer), fp);
626 static int __bt_cancel_bonding(void)
628 BT_CHECK_PARAMETER(bonding_info, return);
629 BT_CHECK_PARAMETER(bonding_info->addr, return);
630 /* First Cancel the ongoing pairing in bluez otherwise if we send
631 * pair request bluez will send inprogress and we again retry bond so
632 * this cycle continues */
634 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
635 NULL, G_DBUS_CALL_FLAGS_NONE,
638 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
640 return BLUETOOTH_ERROR_PARING_FAILED;
642 return BLUETOOTH_ERROR_NONE;
645 static int __bt_retry_bond(void)
647 BT_CHECK_PARAMETER(bonding_info, return);
648 BT_CHECK_PARAMETER(bonding_info->addr, return);
650 is_device_creating = TRUE;
652 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
653 g_variant_new("(y)", bonding_info->conn_type),
654 G_DBUS_CALL_FLAGS_NONE,
657 (GAsyncReadyCallback)__bt_bond_device_cb,
660 return BLUETOOTH_ERROR_NONE;
664 static int __bt_remove_and_bond(void)
666 GDBusProxy *adapter_proxy;
667 GVariant *result = NULL;
669 char *device_path = NULL;
671 BT_CHECK_PARAMETER(bonding_info, return);
672 BT_CHECK_PARAMETER(bonding_info->addr, return);
674 adapter_proxy = _bt_get_adapter_proxy();
675 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
677 device_path = _bt_get_device_object_path(bonding_info->addr);
678 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
680 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
681 g_variant_new("(o)", device_path),
682 G_DBUS_CALL_FLAGS_NONE,
687 if (result == NULL) {
689 BT_ERR("UnpairDevice Fail: %s", err->message);
692 return BLUETOOTH_ERROR_INTERNAL;
695 g_variant_unref(result);
696 return __bt_retry_bond();
699 static int __bt_cancel_and_bond(void)
701 int ret = BLUETOOTH_ERROR_NONE;
703 ret = _bt_agent_reply_cancellation();
704 if (ret != BLUETOOTH_ERROR_NONE) {
705 BT_ERR("Fail to call reply cancellation");
708 return __bt_retry_bond();
712 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
715 int result = BLUETOOTH_ERROR_NONE;
718 GVariant *out_param1;
719 request_info_t *req_info;
720 bluetooth_device_info_t dev_info;
721 bt_remote_dev_info_t *remote_dev_info;
722 GVariant *manufacture_data;
725 /* Terminate ALL system popup */
726 if (headed_plugin_info->plugin_headed_enabled)
727 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
729 reply = g_dbus_proxy_call_finish(proxy, res, &err);
731 g_variant_unref(reply);
733 is_device_creating = FALSE;
735 if (bonding_info == NULL) {
737 BT_ERR("bonding_info == NULL");
743 req_info = _bt_get_request_info(bonding_info->req_id);
744 if (req_info == NULL) {
745 BT_ERR("req_info == NULL");
750 g_dbus_error_strip_remote_error(err);
751 BT_ERR("Error occured in CreateBonding [%s]", err->message);
753 if (g_strrstr(err->message, "Already Exists")) {
754 BT_INFO("Existing Bond, remove and retry");
755 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
757 result = BLUETOOTH_ERROR_PARING_FAILED;
758 } else if (_bt_agent_is_canceled() ||
759 g_strrstr(err->message, "Authentication Canceled")) {
760 BT_INFO("Cancelled by USER");
761 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
762 } else if (g_strrstr(err->message, "Authentication Rejected")) {
764 result = BLUETOOTH_ERROR_ACCESS_DENIED;
765 } else if (g_strrstr(err->message, "In Progress")) {
766 BT_INFO("Bond in progress, cancel and retry");
767 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
769 result = BLUETOOTH_ERROR_PARING_FAILED;
770 } else if (g_strrstr(err->message, "Authentication Failed")) {
771 BT_INFO("Authentication Failed");
772 if (bonding_info->is_autopair == TRUE) {
773 _bt_set_autopair_status_in_bonding_info(FALSE);
774 __ignore_auto_pairing_request(bonding_info->addr);
776 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
777 } else if (g_strrstr(err->message, "Page Timeout")) {
778 BT_INFO("Page Timeout");
779 /* This is the special case
780 As soon as call bluetooth_bond_device, try to cancel bonding.
781 In this case, before completing to call 'CreatePairedDevice' method
782 the procedure is stopped. So 'Cancle' error is not return.
784 result = BLUETOOTH_ERROR_HOST_DOWN;
785 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
786 BT_INFO("Cancel already running bonding");
787 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
788 BT_INFO("Error while Cancelling bonding");
789 /* we need to unref proxy so continue */
791 result = BLUETOOTH_ERROR_INTERNAL;
792 } else if (g_strrstr(err->message, "Connection Timeout")) {
793 /* Pairing request timeout */
794 result = BLUETOOTH_ERROR_TIMEOUT;
795 } else if (g_strrstr(err->message, "Authentication Timeout")) {
796 /* Pairing request timeout */
797 result = BLUETOOTH_ERROR_TIMEOUT;
799 BT_DBG("Default case");
800 result = BLUETOOTH_ERROR_PARING_FAILED;
804 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
805 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
806 result == BLUETOOTH_ERROR_TIMEOUT ||
807 result == BLUETOOTH_ERROR_HOST_DOWN) {
808 if (TIZEN_PROFILE_WEARABLE) {
809 int is_sw_running = 0;
811 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
812 BT_ERR("vconf_get_int for setup wizard state failed");
814 if (!is_sw_running) {
815 if (headed_plugin_info->plugin_headed_enabled)
816 headed_plugin_info->headed_plugin->bt_launch_unable_to_pairing_syspopup(result);
818 BT_ERR("Unable to pair");
821 bonding_info->result = result;
824 g_object_unref(proxy);
825 bonding_info->device_proxy = NULL;
827 if (result != BLUETOOTH_ERROR_NONE) {
828 if (bonding_info->bonding_wo_discovery) {
829 GDBusProxy *adapter_proxy;
830 GVariant *ret = NULL;
831 GError *error = NULL;
834 BT_ERR("Bond was tried without discovery. Remove it");
836 adapter_proxy = _bt_get_adapter_proxy();
837 if (adapter_proxy == NULL) {
838 BT_ERR("Cannot get adapter_proxy");
842 device_path = _bt_get_device_object_path(bonding_info->addr);
843 if (device_path == NULL) {
844 BT_ERR("Cannot get device path");
848 ret = g_dbus_proxy_call_sync(adapter_proxy,
850 g_variant_new("(o)", device_path),
851 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
854 BT_ERR("RemoveDevice Fail: %s", error->message);
855 g_clear_error(&error);
859 g_variant_unref(ret);
867 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
868 if (!remote_dev_info)
871 GVariant *uuids = NULL;
872 GVariantBuilder *builder = NULL;
874 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
875 for (i = 0; i < remote_dev_info->uuid_count; i++) {
876 g_variant_builder_add(builder, "s",
877 remote_dev_info->uuids[i]);
879 uuids = g_variant_new("as", builder);
880 g_variant_builder_unref(builder);
881 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
882 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
885 param = g_variant_new("(isunsbub@asn@ay)",
888 remote_dev_info->class,
889 remote_dev_info->rssi,
890 remote_dev_info->name,
891 remote_dev_info->paired,
892 remote_dev_info->connected,
893 remote_dev_info->trust,
895 remote_dev_info->manufacturer_data_len,
899 /* Send the event to application */
900 _bt_send_event(BT_ADAPTER_EVENT,
901 BLUETOOTH_EVENT_BONDING_FINISHED,
904 _bt_free_device_info(remote_dev_info);
907 if (req_info->context == NULL)
910 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
911 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
914 if (_bt_adapter_get_status() != BT_ACTIVATED)
915 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
917 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
918 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
920 g_dbus_method_invocation_return_value(req_info->context,
921 g_variant_new("(iv)", result, out_param1));
923 _bt_delete_request_list(req_info->req_id);
928 _bt_agent_set_canceled(FALSE);
931 g_free(bonding_info->addr);
932 g_free(bonding_info);
936 int _bt_bond_device(int request_id,
937 bluetooth_device_address_t *device_address,
938 unsigned short conn_type, GArray **out_param1)
941 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
942 bluetooth_device_info_t dev_info;
944 GDBusConnection *conn;
945 char *device_path = NULL;
946 GDBusProxy *adapter_proxy;
947 GError *error = NULL;
948 gboolean bonding_wo_discovery = FALSE;
950 BT_CHECK_PARAMETER(device_address, return);
953 BT_ERR("Bonding in progress");
955 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
956 memcpy(dev_info.device_address.addr, device_address->addr,
957 BLUETOOTH_ADDRESS_LENGTH);
959 g_array_append_vals(*out_param1, &dev_info,
960 sizeof(bluetooth_device_info_t));
962 return BLUETOOTH_ERROR_DEVICE_BUSY;
965 conn = _bt_gdbus_get_system_gconn();
966 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
968 _bt_convert_addr_type_to_string(address, device_address->addr);
970 device_path = _bt_get_device_object_path(address);
972 if (device_path == NULL) {
973 BT_ERR("No searched device");
974 GVariant *ret = NULL;
975 adapter_proxy = _bt_get_adapter_proxy();
976 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
978 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
979 g_variant_new("(s)", address),
980 G_DBUS_CALL_FLAGS_NONE,
986 BT_ERR("CreateDevice Fail: %s", error->message);
987 g_clear_error(&error);
990 g_variant_unref(ret);
991 device_path = _bt_get_device_object_path(address);
992 if (device_path == NULL) {
993 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
994 memcpy(dev_info.device_address.addr, device_address->addr,
995 BLUETOOTH_ADDRESS_LENGTH);
997 g_array_append_vals(*out_param1, &dev_info,
998 sizeof(bluetooth_device_info_t));
1000 return BLUETOOTH_ERROR_NOT_PAIRED;
1002 BT_INFO("device_path is created[%s]", device_path);
1004 bonding_wo_discovery = TRUE;
1007 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1008 NULL, BT_BLUEZ_NAME,
1009 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1011 g_free(device_path);
1012 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1014 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1015 bonding_info->addr = g_strdup(address);
1016 bonding_info->req_id = request_id;
1018 bonding_info->device_proxy = proxy;
1019 bonding_info->conn_type = conn_type;
1020 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1022 is_device_creating = TRUE;
1024 g_dbus_proxy_call(proxy, "Pair",
1025 g_variant_new("(y)", conn_type),
1026 G_DBUS_CALL_FLAGS_NONE,
1027 BT_MAX_DBUS_TIMEOUT,
1029 (GAsyncReadyCallback)__bt_bond_device_cb,
1032 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1034 return BLUETOOTH_ERROR_NONE;
1036 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1037 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1038 bonding_info->addr);
1040 g_array_append_vals(*out_param1, &dev_info,
1041 sizeof(bluetooth_device_info_t));
1043 is_device_creating = FALSE;
1045 g_free(bonding_info->addr);
1046 g_free(bonding_info);
1047 bonding_info = NULL;
1049 return BLUETOOTH_ERROR_INTERNAL;*/
1052 int _bt_cancel_bonding(void)
1054 int ret = BLUETOOTH_ERROR_NONE;
1056 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1058 ret = _bt_agent_reply_cancellation();
1059 if (ret != BLUETOOTH_ERROR_NONE) {
1060 BT_ERR("Fail to call reply cancellation");
1064 _bt_agent_set_canceled(TRUE);
1066 return BLUETOOTH_ERROR_NONE;
1069 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1074 GVariant *out_param1;
1075 int result = BLUETOOTH_ERROR_NONE;
1076 bt_funcion_data_t *unbonding_info;
1077 bluetooth_device_info_t dev_info;
1078 request_info_t *req_info;
1080 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1082 g_variant_unref(reply);
1084 unbonding_info = user_data;
1086 if (unbonding_info == NULL) {
1088 BT_ERR("unbonding_info == NULL");
1092 req_info = _bt_get_request_info(unbonding_info->req_id);
1093 if (req_info == NULL) {
1094 BT_ERR("req_info == NULL");
1099 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1100 result = BLUETOOTH_ERROR_INTERNAL;
1103 if (req_info->context == NULL)
1106 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1107 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1108 unbonding_info->addr);
1110 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1111 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1113 g_dbus_method_invocation_return_value(req_info->context,
1114 g_variant_new("(iv)", result, out_param1));
1117 _bt_delete_request_list(req_info->req_id);
1123 if (unbonding_info) {
1124 g_free(unbonding_info->addr);
1125 g_free(unbonding_info);
1129 int _bt_unbond_device(int request_id,
1130 bluetooth_device_address_t *device_address,
1131 GArray **out_param1)
1133 char *device_path = NULL;
1134 bt_funcion_data_t *unbonding_info;
1135 GDBusProxy *adapter_proxy = NULL;
1136 GDBusProxy *device_proxy = NULL;
1137 GDBusConnection *conn;
1138 int result = BLUETOOTH_ERROR_INTERNAL;
1139 bluetooth_device_info_t dev_info;
1140 GError *error = NULL;
1141 GVariant *ret = NULL;
1143 BT_CHECK_PARAMETER(device_address, return);
1145 adapter_proxy = _bt_get_adapter_proxy();
1146 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1148 /* allocate user data so that it can be retrieved in callback */
1149 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1150 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1151 unbonding_info->req_id = request_id;
1153 _bt_convert_addr_type_to_string(unbonding_info->addr,
1154 device_address->addr);
1156 device_path = _bt_get_device_object_path(unbonding_info->addr);
1158 if (device_path == NULL) {
1159 BT_ERR("No paired device");
1160 result = BLUETOOTH_ERROR_NOT_PAIRED;
1164 conn = _bt_gdbus_get_system_gconn();
1166 BT_ERR("conn is NULL");
1167 result = BLUETOOTH_ERROR_INTERNAL;
1171 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1172 NULL, BT_BLUEZ_NAME,
1173 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1175 if (device_proxy != NULL) {
1177 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1178 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1179 G_DBUS_CALL_FLAGS_NONE,
1184 BT_ERR("Getting property failed: [%s]\n", error->message);
1185 g_error_free(error);
1186 result = BLUETOOTH_ERROR_NOT_PAIRED;
1190 BT_ERR("No paired device");
1191 g_object_unref(device_proxy);
1192 result = BLUETOOTH_ERROR_NOT_PAIRED;
1195 g_variant_unref(ret);
1197 g_object_unref(device_proxy);
1200 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1201 g_variant_new("(o)", device_path),
1202 G_DBUS_CALL_FLAGS_NONE,
1203 BT_MAX_DBUS_TIMEOUT,
1205 (GAsyncReadyCallback)__bt_unbond_cb,
1208 g_free(device_path);
1209 return BLUETOOTH_ERROR_NONE;
1212 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1213 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1214 unbonding_info->addr);
1216 g_array_append_vals(*out_param1, &dev_info,
1217 sizeof(bluetooth_device_info_t));
1218 g_free(device_path);
1219 g_free(unbonding_info->addr);
1220 g_free(unbonding_info);
1224 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1229 GVariant *out_param1;
1230 int result = BLUETOOTH_ERROR_NONE;
1231 bluetooth_device_info_t dev_info;
1232 bt_remote_dev_info_t *remote_dev_info;
1233 request_info_t *req_info;
1234 GVariant *uuid_list, *manufacture_data;
1236 GVariantBuilder *builder = NULL;
1239 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1241 g_variant_unref(reply);
1243 g_object_unref(proxy);
1245 if (searching_info == NULL) {
1247 BT_ERR("unbonding_info == NULL");
1251 req_info = _bt_get_request_info(searching_info->req_id);
1252 if (req_info == NULL) {
1253 BT_ERR("req_info == NULL");
1258 g_dbus_error_strip_remote_error(err);
1259 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1261 if (g_strrstr("Operation canceled", err->message))
1262 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1263 else if (g_strrstr("In Progress", err->message))
1264 result = BLUETOOTH_ERROR_IN_PROGRESS;
1265 else if (g_strrstr("Host is down", err->message))
1266 result = BLUETOOTH_ERROR_HOST_DOWN;
1268 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1270 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1271 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1272 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1273 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1274 remote_dev_info->uuid_count > 0) {
1275 result = BLUETOOTH_ERROR_NONE;
1278 _bt_free_device_info(remote_dev_info);
1283 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1284 if (!remote_dev_info)
1288 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1289 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1290 g_variant_builder_add(builder, "s",
1291 remote_dev_info->uuids[i]);
1293 uuid_list = g_variant_new("as", builder);
1294 g_variant_builder_unref(builder);
1295 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1296 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1299 param = g_variant_new("(isunsbub@asn@ay)",
1301 searching_info->addr,
1302 remote_dev_info->class,
1303 remote_dev_info->rssi,
1304 remote_dev_info->name,
1305 remote_dev_info->paired,
1306 remote_dev_info->connected,
1307 remote_dev_info->trust,
1309 remote_dev_info->manufacturer_data_len,
1312 /* Send the event to application */
1313 _bt_send_event(BT_ADAPTER_EVENT,
1314 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1317 _bt_free_device_info(remote_dev_info);
1320 if (req_info->context == NULL)
1323 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1324 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1325 searching_info->addr);
1327 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1328 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1330 g_dbus_method_invocation_return_value(req_info->context,
1331 g_variant_new("(iv)", result, out_param1));
1334 _bt_delete_request_list(req_info->req_id);
1339 if (searching_info) {
1340 g_free(searching_info->addr);
1341 g_free(searching_info);
1342 searching_info = NULL;
1346 int _bt_search_device(int request_id,
1347 bluetooth_device_address_t *device_address)
1349 char *device_path = NULL;
1350 GDBusProxy *device_proxy = NULL;
1351 GDBusConnection *conn;
1353 GDBusProxy *adapter_proxy;
1354 int result = BLUETOOTH_ERROR_INTERNAL;
1356 BT_CHECK_PARAMETER(device_address, return);
1359 BT_ERR("Bonding in progress");
1360 return BLUETOOTH_ERROR_DEVICE_BUSY;
1363 if (searching_info) {
1364 BT_ERR("Service searching in progress");
1365 return BLUETOOTH_ERROR_DEVICE_BUSY;
1368 adapter_proxy = _bt_get_adapter_proxy();
1369 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1371 /* allocate user data so that it can be retrieved in callback */
1372 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1373 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1374 searching_info->req_id = request_id;
1376 _bt_convert_addr_type_to_string(searching_info->addr,
1377 device_address->addr);
1379 conn = _bt_gdbus_get_system_gconn();
1380 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1383 device_path = _bt_get_device_object_path(searching_info->addr);
1385 if (device_path == NULL) {
1386 BT_ERR("No paired device");
1387 result = BLUETOOTH_ERROR_NOT_PAIRED;
1391 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1392 NULL, BT_BLUEZ_NAME,
1393 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1394 g_free(device_path);
1395 if (device_proxy == NULL) {
1396 result = BLUETOOTH_ERROR_INTERNAL;
1400 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1401 g_variant_new("(s)", ""),
1402 G_DBUS_CALL_FLAGS_NONE,
1403 BT_MAX_DBUS_TIMEOUT,
1405 (GAsyncReadyCallback)__bt_discover_cb,
1408 searching_info->device_proxy = device_proxy;
1410 return BLUETOOTH_ERROR_NONE;
1413 g_free(searching_info->addr);
1414 g_free(searching_info);
1415 searching_info = NULL;
1419 int _bt_cancel_search_device(void)
1421 GVariant *ret = NULL;
1424 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1426 if (searching_info->device_proxy) {
1427 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1429 G_DBUS_CALL_FLAGS_NONE,
1434 g_variant_unref(ret);
1436 __bt_cancel_search_service_done();
1438 return BLUETOOTH_ERROR_NONE;
1441 int _bt_set_alias(bluetooth_device_address_t *device_address,
1444 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1445 gchar *device_path = NULL;
1446 GDBusProxy *adapter_proxy;
1447 GDBusProxy *device_proxy;
1448 GVariant *ret = NULL;
1449 GError *error = NULL;
1450 GDBusConnection *conn;
1452 BT_CHECK_PARAMETER(device_address, return);
1453 BT_CHECK_PARAMETER(alias, return);
1455 adapter_proxy = _bt_get_adapter_proxy();
1456 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1458 conn = _bt_gdbus_get_system_gconn();
1459 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1461 _bt_convert_addr_type_to_string(address, device_address->addr);
1463 device_path = _bt_get_device_object_path(address);
1465 if (device_path == NULL) {
1466 BT_ERR("No paired device");
1467 return BLUETOOTH_ERROR_NOT_PAIRED;
1470 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1471 NULL, BT_BLUEZ_NAME,
1472 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1474 g_free(device_path);
1475 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1477 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1478 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1479 G_DBUS_CALL_FLAGS_NONE,
1484 g_variant_unref(ret);
1486 g_object_unref(device_proxy);
1489 BT_ERR("SetProperty error: [%s]", error->message);
1490 g_error_free(error);
1491 return BLUETOOTH_ERROR_INTERNAL;
1494 return BLUETOOTH_ERROR_NONE;
1497 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1500 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1501 gchar *device_path = NULL;
1502 GDBusProxy *device_proxy;
1503 gboolean previous_value;
1504 GError *error = NULL;
1505 GDBusConnection *conn;
1506 GVariant *result = NULL;
1507 GVariant *temp = NULL;
1508 int ret = BLUETOOTH_ERROR_NONE;
1510 BT_CHECK_PARAMETER(device_address, return);
1512 conn = _bt_gdbus_get_system_gconn();
1513 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1515 _bt_convert_addr_type_to_string(address, device_address->addr);
1517 device_path = _bt_get_device_object_path(address);
1519 if (device_path == NULL) {
1520 BT_ERR("No paired device");
1521 return BLUETOOTH_ERROR_NOT_PAIRED;
1524 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1525 NULL, BT_BLUEZ_NAME,
1526 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1528 g_free(device_path);
1529 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1531 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1532 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1533 G_DBUS_CALL_FLAGS_NONE,
1537 if (error != NULL) {
1538 BT_ERR("Getting property failed: [%s]\n", error->message);
1539 g_error_free(error);
1540 g_object_unref(device_proxy);
1541 return BLUETOOTH_ERROR_INTERNAL;
1544 g_variant_get(result, "(v)", &temp);
1545 previous_value = g_variant_get_boolean(temp);
1546 g_variant_unref(temp);
1547 g_variant_unref(result);
1548 /* If the input is same with previous value, return error. */
1549 if (previous_value == authorize) {
1550 BT_ERR("Same value: %d", previous_value);
1551 g_object_unref(device_proxy);
1552 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1556 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1557 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1558 G_DBUS_CALL_FLAGS_NONE,
1563 g_object_unref(device_proxy);
1565 BT_ERR("SetProperty error: [%s]", error->message);
1566 g_error_free(error);
1567 ret = BLUETOOTH_ERROR_INTERNAL;
1571 g_variant_unref(result);
1576 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1577 gboolean *is_connected)
1579 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1580 char *object_path = NULL;
1582 GDBusProxy *device_proxy;
1583 GError *error = NULL;
1585 GDBusConnection *conn;
1586 GVariant *result = NULL;
1587 int ret = BLUETOOTH_ERROR_NONE;
1589 BT_CHECK_PARAMETER(device_address, return);
1591 conn = _bt_gdbus_get_system_gconn();
1592 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1594 _bt_convert_addr_type_to_string(address, device_address->addr);
1596 object_path = _bt_get_device_object_path(address);
1597 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1599 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1600 NULL, BT_BLUEZ_NAME, object_path,
1601 BT_PROPERTIES_INTERFACE, NULL, NULL);
1602 g_free(object_path);
1603 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1605 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1606 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1608 G_DBUS_CALL_FLAGS_NONE,
1612 if (result == NULL) {
1613 if (error != NULL) {
1614 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1615 g_error_free(error);
1617 g_object_unref(device_proxy);
1618 return BLUETOOTH_ERROR_INTERNAL;
1621 g_variant_get(result, "(v)", &value);
1622 *is_connected = g_variant_get_boolean(value);
1623 g_variant_unref(result);
1625 BT_DBG("gatt is connected : %d", *is_connected);
1627 g_variant_unref(value);
1628 g_object_unref(device_proxy);
1633 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1634 int connection_type, gboolean *is_connected)
1636 char *object_path = NULL;
1637 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1638 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1639 GDBusProxy *device_proxy = NULL;
1640 GDBusProxy *adapter_proxy = NULL;
1641 GDBusConnection *conn;
1642 GError *error = NULL;
1643 GVariant *tmp_value = NULL;
1644 GVariant *value = NULL;
1645 GVariant *result = NULL;
1648 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1649 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1651 _bt_convert_addr_type_to_string(address, device_address->addr);
1652 _bt_convert_addr_string_to_secure_string(secure_address, address);
1654 *is_connected = FALSE;
1655 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1657 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1658 return _bt_rfcomm_is_device_connected(device_address,
1660 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1661 return _bt_is_gatt_connected(device_address, is_connected);
1662 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1663 return _bt_pbap_is_connected(device_address, is_connected);
1665 adapter_proxy = _bt_get_adapter_proxy();
1666 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1668 conn = _bt_gdbus_get_system_gconn();
1669 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1671 _bt_convert_addr_type_to_string(address, device_address->addr);
1673 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1674 object_path = _bt_get_adapter_path();
1675 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1676 NULL, BT_BLUEZ_NAME,
1677 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1678 g_free(object_path);
1679 if (device_proxy == NULL) {
1680 BT_DBG("Device don't have this service");
1681 return BLUETOOTH_ERROR_INTERNAL;
1684 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1685 g_variant_new("(s)", address),
1686 G_DBUS_CALL_FLAGS_NONE,
1691 if (result == NULL) {
1692 BT_ERR("[GetProperties] Error occured in Proxy call");
1694 BT_ERR("%s", error->message);
1695 g_error_free(error);
1697 *is_connected = FALSE;
1698 g_object_unref(device_proxy);
1699 return BLUETOOTH_ERROR_NONE;
1701 g_variant_get(result , "(@a{sv})", &value);
1702 g_variant_unref(result);
1705 tmp_value = g_variant_lookup_value(value,
1707 G_VARIANT_TYPE_BOOLEAN);
1709 *is_connected = g_variant_get_boolean(tmp_value);
1710 g_variant_unref(tmp_value);
1712 g_variant_unref(value);
1714 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1715 return _bt_is_network_connected(_bt_get_net_conn(),
1716 device_address->addr, is_connected);
1718 uuid = _bt_get_profile_uuid128(connection_type);
1720 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1721 return BLUETOOTH_ERROR_INTERNAL;
1723 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1725 object_path = _bt_get_device_object_path(address);
1727 BT_ERR("object_path is NULL");
1729 return BLUETOOTH_ERROR_NOT_PAIRED;
1732 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1733 NULL, BT_BLUEZ_NAME,
1734 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1735 g_free(object_path);
1736 if (device_proxy == NULL) {
1737 BT_DBG("Device don't have this service");
1739 return BLUETOOTH_ERROR_INTERNAL;
1742 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1743 g_variant_new("(s)", uuid),
1744 G_DBUS_CALL_FLAGS_NONE,
1749 if (result == NULL) {
1750 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1752 BT_ERR("%s", error->message);
1753 if (g_strrstr(error->message, "Not Connected"))
1754 BT_DBG("Not connected");
1755 g_error_free(error);
1757 *is_connected = FALSE;
1758 g_object_unref(device_proxy);
1760 return BLUETOOTH_ERROR_NONE;
1762 g_variant_get(result, "(b)", is_connected);
1764 g_variant_unref(result);
1767 g_object_unref(device_proxy);
1768 return BLUETOOTH_ERROR_NONE;
1771 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1772 bluetooth_connected_link_t *connected)
1774 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1775 char *object_path = NULL;
1777 GDBusProxy *device_proxy;
1778 GError *error = NULL;
1779 GDBusConnection *conn;
1780 GVariant *value = NULL;
1781 GVariant *result = NULL;
1783 BT_CHECK_PARAMETER(device_address, return);
1785 conn = _bt_gdbus_get_system_gconn();
1786 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1788 _bt_convert_addr_type_to_string(address, device_address->addr);
1790 object_path = _bt_get_device_object_path(address);
1791 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1793 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1794 NULL, BT_BLUEZ_NAME,
1795 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1796 g_free(object_path);
1797 if (device_proxy == NULL) {
1798 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1799 return BLUETOOTH_ERROR_NONE;
1802 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1803 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1804 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1806 if (error != NULL) {
1807 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1808 g_error_free(error);
1809 g_object_unref(device_proxy);
1810 return BLUETOOTH_ERROR_INTERNAL;
1812 g_variant_get(result, "(v)", &value);
1813 *connected = g_variant_get_byte(value);
1814 g_variant_unref(value);
1815 g_variant_unref(result);
1818 g_object_unref(device_proxy);
1819 return BLUETOOTH_ERROR_NONE;
1822 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1826 GVariant *out_param1;
1828 int result = BLUETOOTH_ERROR_NONE;
1829 bt_function_data_t *func_data = user_data;
1830 request_info_t *req_info = NULL;
1831 bluetooth_device_address_t device_addr = { {0} };
1833 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1834 g_object_unref(proxy);
1836 if (reply == NULL) {
1837 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1839 BT_ERR("Error: %s", err->message);
1840 g_clear_error(&err);
1842 result = BLUETOOTH_ERROR_INTERNAL;
1844 g_variant_unref(reply);
1847 if (func_data == NULL) {
1848 BT_ERR("func_data is NULL");
1852 req_info = _bt_get_request_info(func_data->req_id);
1853 if (req_info == NULL) {
1854 BT_ERR("req_info is NULL");
1858 if (req_info->context == NULL) {
1859 BT_ERR("req_info->context is NULL");
1863 _bt_convert_addr_string_to_type(device_addr.addr,
1864 (const char *)func_data->address);
1866 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1867 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1870 g_dbus_method_invocation_return_value(req_info->context,
1871 g_variant_new("(iv)", result, out_param1));
1875 _bt_delete_request_list(req_info->req_id);
1878 g_free(func_data->address);
1883 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1884 gboolean auto_connect)
1886 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1887 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1888 gchar *device_path = NULL;
1889 GDBusProxy *device_proxy = NULL;
1890 GDBusConnection *conn;
1891 int ret = BLUETOOTH_ERROR_NONE;
1892 bt_function_data_t *func_data;
1894 BT_CHECK_PARAMETER(bd_addr, return);
1896 conn = _bt_gdbus_get_system_gconn();
1897 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1899 _bt_convert_addr_type_to_string(device_address,
1900 (unsigned char *)bd_addr->addr);
1901 device_path = _bt_get_device_object_path(device_address);
1902 if (device_path == NULL) {
1903 BT_ERR_C("device_path NULL : [%s]", device_address);
1904 ret = BLUETOOTH_ERROR_INTERNAL;
1908 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1909 NULL, BT_BLUEZ_NAME,
1910 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1911 g_free(device_path);
1912 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1914 func_data = g_malloc0(sizeof(bt_function_data_t));
1915 func_data->address = g_strdup(device_address);
1916 if (func_data->address == NULL) {
1917 BT_ERR("Unable to allocate memory for address");
1918 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1922 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1923 BT_INFO("Connect LE [%s]", secure_address);
1925 func_data->req_id = req_id;
1927 g_dbus_proxy_call(device_proxy, "ConnectLE",
1928 g_variant_new("(b)", auto_connect),
1929 G_DBUS_CALL_FLAGS_NONE,
1930 BT_MAX_DBUS_TIMEOUT,
1932 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1938 g_object_unref(device_proxy);
1940 g_free(func_data->address);
1946 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1950 BT_INFO("Try to initiate pending LE connection");
1952 pending_le_conn_timer_id = 0;
1954 ret = __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1955 &pending_le_conn_info->bd_addr,
1956 pending_le_conn_info->auto_connect);
1957 if (ret != BLUETOOTH_ERROR_NONE) {
1958 request_info_t *req_info = NULL;
1959 GVariant *out_param1;
1961 req_info = _bt_get_request_info(pending_le_conn_info->req_id);
1962 if (req_info == NULL) {
1963 BT_ERR("req_info is NULL");
1967 if (req_info->context == NULL) {
1968 BT_ERR("req_info->context is NULL");
1969 _bt_delete_request_list(req_info->req_id);
1973 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1974 &pending_le_conn_info->bd_addr,
1975 sizeof(bluetooth_device_address_t), TRUE,
1978 g_dbus_method_invocation_return_value(req_info->context,
1979 g_variant_new("(iv)", ret, out_param1));
1983 g_free(pending_le_conn_info);
1984 pending_le_conn_info = NULL;
1989 void _bt_pending_connect_le_device(void)
1991 if (pending_le_conn_timer_id > 0) {
1992 g_source_remove(pending_le_conn_timer_id);
1993 __bt_connect_le_timer_cb(NULL);
1997 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1998 gboolean auto_connect)
2000 int ret = BLUETOOTH_ERROR_NONE;
2002 BT_CHECK_PARAMETER(bd_addr, return);
2004 ret = _bt_hold_current_advertising();
2005 if (ret == BLUETOOTH_ERROR_NONE) {
2006 BT_INFO("Current advertising is held");
2007 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2008 pending_le_conn_info->req_id = req_id;
2009 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2010 BLUETOOTH_ADDRESS_LENGTH);
2011 pending_le_conn_info->auto_connect = auto_connect;
2013 pending_le_conn_timer_id =
2014 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2016 return BLUETOOTH_ERROR_NONE;
2017 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
2018 BT_ERR("Unable to hold advertising");
2021 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2024 int _bt_disconnect_le_device(int req_id,
2025 const bluetooth_device_address_t *bd_addr)
2027 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2028 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2030 GDBusProxy *device_proxy;
2031 GDBusConnection *conn;
2032 int ret = BLUETOOTH_ERROR_NONE;
2033 bt_function_data_t *func_data;
2035 BT_CHECK_PARAMETER(bd_addr, return);
2037 conn = _bt_gdbus_get_system_gconn();
2038 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2040 _bt_convert_addr_type_to_string(device_address,
2041 (unsigned char *)bd_addr->addr);
2042 device_path = _bt_get_device_object_path(device_address);
2043 if (device_path == NULL) {
2044 BT_DBG("device_path NULL");
2045 ret = BLUETOOTH_ERROR_INTERNAL;
2049 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2051 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2052 NULL, BT_BLUEZ_NAME,
2053 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2054 g_free(device_path);
2055 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2057 func_data = g_malloc0(sizeof(bt_function_data_t));
2058 func_data->address = g_strdup(device_address);
2059 if (func_data->address == NULL) {
2060 BT_ERR("Unable to allocate memory for address");
2061 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2065 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2066 BT_INFO("Disconnect LE [%s]", secure_address);
2068 func_data->req_id = req_id;
2070 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2072 G_DBUS_CALL_FLAGS_NONE,
2073 BT_MAX_DBUS_TIMEOUT,
2075 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2080 g_object_unref(device_proxy);
2082 g_free(func_data->address);
2088 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2090 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2091 gchar *device_path = NULL;
2092 GError *error = NULL;
2093 GDBusProxy *device_proxy = NULL;
2094 GDBusProxy *adapter_proxy;
2095 GDBusConnection *conn;
2096 int ret = BLUETOOTH_ERROR_NONE;
2098 BT_CHECK_PARAMETER(bd_addr, return);
2100 _bt_convert_addr_type_to_string(device_address,
2101 (unsigned char *)bd_addr->addr);
2103 conn = _bt_gdbus_get_system_gconn();
2104 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2106 adapter_proxy = _bt_get_adapter_proxy();
2107 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2109 device_path = _bt_get_device_object_path(device_address);
2110 if (device_path == NULL) {
2111 BT_DBG("device_path NULL");
2112 ret = BLUETOOTH_ERROR_INTERNAL;
2116 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2118 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2119 NULL, BT_BLUEZ_NAME,
2120 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2121 g_free(device_path);
2122 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2124 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2126 G_DBUS_CALL_FLAGS_NONE,
2131 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2132 g_error_free(error);
2133 g_object_unref(device_proxy);
2134 return BLUETOOTH_ERROR_INTERNAL;
2136 g_object_unref(device_proxy);
2138 /* IPSP daemon launch */
2139 GDBusProxy *ipsp_proxy;
2141 ipsp_proxy = _bt_get_ipsp_proxy();
2142 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2144 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2145 NULL, G_DBUS_CALL_FLAGS_NONE,
2146 -1, NULL, NULL, NULL);
2151 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2153 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2154 gchar *device_path = NULL;
2155 GError *error = NULL;
2156 GDBusProxy *device_proxy = NULL;
2157 GDBusProxy *adapter_proxy;
2158 GDBusConnection *conn;
2159 int ret = BLUETOOTH_ERROR_NONE;
2161 BT_CHECK_PARAMETER(bd_addr, return);
2163 _bt_convert_addr_type_to_string(device_address,
2164 (unsigned char *)bd_addr->addr);
2166 conn = _bt_gdbus_get_system_gconn();
2167 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2169 adapter_proxy = _bt_get_adapter_proxy();
2170 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2172 device_path = _bt_get_device_object_path(device_address);
2173 if (device_path == NULL) {
2174 BT_DBG("device_path NULL");
2175 ret = BLUETOOTH_ERROR_INTERNAL;
2179 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2181 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2182 NULL, BT_BLUEZ_NAME,
2183 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2184 g_free(device_path);
2185 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2187 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2189 G_DBUS_CALL_FLAGS_NONE,
2194 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2195 g_error_free(error);
2196 g_object_unref(device_proxy);
2197 return BLUETOOTH_ERROR_INTERNAL;
2200 g_object_unref(device_proxy);
2205 int _bt_connect_profile(char *address, char *uuid,
2206 void *cb, gpointer func_data)
2210 GDBusConnection *conn;
2211 GDBusProxy *adapter_proxy;
2212 GVariant *result = NULL;
2213 GError *error = NULL;
2215 conn = _bt_gdbus_get_system_gconn();
2216 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2218 object_path = _bt_get_device_object_path(address);
2219 if (object_path == NULL) {
2220 BT_ERR("No searched device");
2222 adapter_proxy = _bt_get_adapter_proxy();
2223 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2225 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2226 g_variant_new("(s)", address),
2227 G_DBUS_CALL_FLAGS_NONE,
2232 if (error != NULL) {
2233 BT_ERR("CreateDevice Fail: %s", error->message);
2234 g_error_free(error);
2237 g_variant_unref(result);
2239 object_path = _bt_get_device_object_path(address);
2241 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2243 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2244 NULL, BT_BLUEZ_NAME,
2245 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2246 g_free(object_path);
2247 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2249 g_dbus_proxy_call(proxy, "ConnectProfile",
2250 g_variant_new("(s)", uuid),
2251 G_DBUS_CALL_FLAGS_NONE,
2252 BT_MAX_DBUS_TIMEOUT,
2254 (GAsyncReadyCallback)cb,
2257 return BLUETOOTH_ERROR_NONE;
2260 int _bt_disconnect_all(char *address)
2262 int ret = BLUETOOTH_ERROR_NONE;
2265 GDBusConnection *conn;
2266 GVariant *result = NULL;
2270 conn = _bt_gdbus_get_system_gconn();
2271 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2273 object_path = _bt_get_device_object_path(address);
2274 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2276 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2277 NULL, BT_BLUEZ_NAME,
2278 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2279 g_free(object_path);
2280 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2282 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2284 G_DBUS_CALL_FLAGS_NONE,
2289 BT_ERR("Dbus Call Error:[%s]", err->message);
2291 ret = BLUETOOTH_ERROR_INTERNAL;
2294 g_object_unref(proxy);
2296 g_variant_unref(result);
2301 int _bt_disconnect_profile(char *address, char *uuid,
2302 void *cb, gpointer func_data)
2306 GDBusConnection *conn;
2308 conn = _bt_gdbus_get_system_gconn();
2309 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2311 object_path = _bt_get_device_object_path(address);
2312 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2314 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2315 NULL, BT_BLUEZ_NAME,
2316 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2317 g_free(object_path);
2318 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2320 g_dbus_proxy_call(proxy, "DisconnectProfile",
2321 g_variant_new("(s)", uuid),
2322 G_DBUS_CALL_FLAGS_NONE,
2323 BT_MAX_DBUS_TIMEOUT,
2325 (GAsyncReadyCallback)cb,
2328 return BLUETOOTH_ERROR_NONE;
2331 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2332 int low_threshold, int in_range_threshold, int high_threshold)
2334 int ret = BLUETOOTH_ERROR_NONE;
2336 GVariant *result = NULL;
2337 GError *error = NULL;
2338 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2340 BT_CHECK_PARAMETER(bd_addr, return);
2341 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2342 bd_addr->addr[0], bd_addr->addr[1],
2343 bd_addr->addr[2], bd_addr->addr[3],
2344 bd_addr->addr[4], bd_addr->addr[5],
2346 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2347 in_range_threshold, high_threshold);
2349 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2351 proxy = _bt_get_adapter_proxy();
2352 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2354 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2355 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2356 G_DBUS_CALL_FLAGS_NONE,
2360 if (error != NULL) {
2361 BT_ERR("Dbus Call Error:[%s]", error->message);
2362 g_error_free(error);
2363 ret = BLUETOOTH_ERROR_INTERNAL;
2367 g_variant_unref(result);
2372 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2375 int ret = BLUETOOTH_ERROR_NONE;
2377 GVariant *result = NULL;
2378 GError *error = NULL;
2379 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2381 BT_CHECK_PARAMETER(bd_addr, return);
2382 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2383 bd_addr->addr[0], bd_addr->addr[1],
2384 bd_addr->addr[2], bd_addr->addr[3],
2385 bd_addr->addr[4], bd_addr->addr[5],
2388 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2390 proxy = _bt_get_adapter_proxy();
2391 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2393 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2394 g_variant_new("(si)", address, link_type),
2395 G_DBUS_CALL_FLAGS_NONE,
2400 if (error != NULL) {
2401 BT_ERR("Dbus Call Error:[%s]", error->message);
2402 g_error_free(error);
2403 ret = BLUETOOTH_ERROR_INTERNAL;
2407 g_variant_unref(result);
2412 static int __bt_le_set_conn_parameter(const char *address,
2413 float interval_min, float interval_max,
2414 guint16 latency, guint16 time_out)
2416 gchar *device_path = NULL;
2417 GError *error = NULL;
2418 GDBusProxy *device_proxy = NULL;
2419 GDBusConnection *conn;
2421 guint32 min, max, to;
2422 int ret = BLUETOOTH_ERROR_NONE;
2424 BT_CHECK_PARAMETER(address, return);
2426 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2427 interval_min, interval_max, latency, time_out);
2429 device_path = _bt_get_device_object_path((char *)address);
2431 if (device_path == NULL) {
2432 BT_ERR("device_path NULL");
2433 ret = BLUETOOTH_ERROR_INTERNAL;
2437 conn = _bt_gdbus_get_system_gconn();
2439 BT_ERR("conn NULL");
2440 ret = BLUETOOTH_ERROR_INTERNAL;
2444 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2445 NULL, BT_BLUEZ_NAME,
2446 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2448 g_free(device_path);
2449 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2451 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2452 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2453 to = time_out / BT_LE_CONN_TO_SPLIT;
2455 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2456 g_variant_new("(uuuu)", min, max, latency, to),
2457 G_DBUS_CALL_FLAGS_NONE,
2462 g_object_unref(device_proxy);
2463 if (reply == NULL) {
2465 BT_ERR("Error %s[%s]", error->message, address);
2466 if (g_strrstr(error->message, "In Progress"))
2467 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2469 ret = BLUETOOTH_ERROR_INTERNAL;
2470 g_error_free(error);
2474 g_variant_unref(reply);
2480 static void __bt_le_conn_param_free(void *data)
2482 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2484 BT_DBG("%s", param->sender);
2485 g_free(param->sender);
2489 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2491 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2492 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2494 return parama->key > paramb->key;
2497 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2500 bt_connected_le_dev_t *dev;
2505 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2508 if (g_strcmp0(dev->address, address) == 0)
2514 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2517 bt_le_conn_param_t *param = NULL;
2519 if (!dev || !sender)
2522 for (l = dev->senders; l; l = g_slist_next(l)) {
2524 if (g_strcmp0(param->sender, sender) == 0)
2531 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2532 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2534 bt_connected_le_dev_t *dev = NULL;
2535 bt_le_conn_param_t *param = NULL;
2536 bt_le_conn_param_t *data = NULL;
2538 if (!address || !sender)
2539 return BLUETOOTH_ERROR_INVALID_PARAM;
2541 dev = __bt_get_le_connected_dev_info(address);
2543 return BLUETOOTH_ERROR_INTERNAL;
2545 param = __bt_get_le_conn_param_info(dev, sender);
2547 data = g_malloc0(sizeof(bt_le_conn_param_t));
2548 data->sender = g_strdup(sender);
2549 data->interval_min = interval_min;
2550 data->interval_max = interval_max;
2551 data->latency = latency;
2552 data->time_out = time_out;
2553 data->key = interval_min + (interval_max - interval_min)/2;
2555 if (param == NULL) {
2556 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2557 dev->senders = g_slist_append(dev->senders, data);
2559 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2560 dev->senders = g_slist_remove(dev->senders, param);
2561 g_free(param->sender);
2563 dev->senders = g_slist_append(dev->senders, data);
2566 /* Sorting. First element have the minimum interval */
2567 dev->senders = g_slist_sort(dev->senders,
2568 (GCompareFunc)__bt_compare_le_conn_param_key);
2570 return BLUETOOTH_ERROR_NONE;
2573 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2575 bt_connected_le_dev_t *dev = NULL;
2576 bt_le_conn_param_t *param = NULL;
2578 if (!address || !sender)
2579 return BLUETOOTH_ERROR_INVALID_PARAM;
2581 dev = __bt_get_le_connected_dev_info(address);
2583 return BLUETOOTH_ERROR_INTERNAL;
2585 param = __bt_get_le_conn_param_info(dev, sender);
2587 BT_DBG("Remove param %s %s ", address, sender);
2588 dev->senders = g_slist_remove(dev->senders, param);
2589 g_free(param->sender);
2593 *is_removed = FALSE;
2595 return BLUETOOTH_ERROR_NONE;
2598 int _bt_remove_all_le_conn_param_info(const char *sender)
2601 bt_connected_le_dev_t *dev = NULL;
2602 bt_le_conn_param_t *param = NULL;
2603 gboolean is_removed = FALSE;
2604 char *sender_new = NULL;
2605 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2606 int ret = BLUETOOTH_ERROR_NONE;
2609 return BLUETOOTH_ERROR_INVALID_PARAM;
2611 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2613 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2616 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2617 g_slist_length(dev->senders));
2618 if (g_slist_length(dev->senders) > 0) {
2619 param = dev->senders->data;
2620 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2621 param->interval_min, param->interval_max);
2623 if (dev->interval_min != param->interval_min ||
2624 dev->interval_max != param->interval_max) {
2625 sender_new = g_strdup(param->sender);
2627 _bt_convert_addr_string_to_type(addr, dev->address);
2628 ret = _bt_le_conn_update(sender_new, addr,
2629 param->interval_min, param->interval_max,
2630 param->latency, param->time_out);
2633 if (ret != BLUETOOTH_ERROR_NONE)
2634 BT_ERR("Unable to set le connection parameter");
2637 BT_INFO("Set the default interval");
2639 bluetooth_le_connection_param_t param = { 0 };
2640 _bt_get_le_connection_parameter(
2641 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2644 ret = __bt_le_set_conn_parameter(dev->address,
2645 param.interval_min, param.interval_max,
2646 param.latency, param.timeout);
2647 if (ret == BLUETOOTH_ERROR_NONE) {
2648 dev->interval_min = param.interval_min;
2649 dev->interval_max = param.interval_max;
2658 void _bt_add_le_connected_dev_info(const char *address)
2660 bt_connected_le_dev_t *dev = NULL;
2665 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2666 dev->address = g_strdup(address);
2668 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2673 void _bt_remove_le_connected_dev_info(const char *address)
2675 bt_connected_le_dev_t *dev = NULL;
2680 dev = __bt_get_le_connected_dev_info(address);
2684 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2685 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2686 g_free(dev->address);
2692 int _bt_le_conn_update(const char *sender,
2693 unsigned char *device_address,
2694 float interval_min, float interval_max,
2695 guint16 latency, guint16 time_out)
2697 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2698 guint32 min_supervision_to;
2699 bt_connected_le_dev_t *dev = NULL;
2700 bt_le_conn_param_t *param = NULL;
2701 gboolean is_removed = FALSE;
2702 int ret = BLUETOOTH_ERROR_NONE;
2704 BT_CHECK_PARAMETER(device_address, return);
2706 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2707 sender, interval_min, interval_max, latency, time_out);
2709 if (interval_min > interval_max ||
2710 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2711 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2712 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2716 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2717 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2718 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2722 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2723 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2728 * The Supervision_Timeout in milliseconds shall be larger than
2729 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2730 * where Conn_Interval_Max is given in milliseconds.
2732 min_supervision_to = (1 + latency) * interval_max * 2;
2733 if (time_out <= min_supervision_to) {
2734 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2738 _bt_convert_addr_type_to_string(address, device_address);
2739 BT_DBG("Remote device address: %s", address);
2741 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2743 dev = __bt_get_le_connected_dev_info(address);
2745 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2749 if (g_slist_length(dev->senders) == 1)
2752 param = dev->senders->data;
2754 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2756 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2757 BT_DBG("Skip due to same interval");
2761 interval_min = param->interval_min;
2762 interval_max = param->interval_max;
2766 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2768 if (ret != BLUETOOTH_ERROR_NONE) {
2769 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2773 dev->interval_min = interval_min;
2774 dev->interval_max = interval_max;
2780 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2781 bluetooth_device_pin_code_t *pin_code)
2783 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2785 bt_pin_code_info_t *pin_info = NULL;
2787 BT_CHECK_PARAMETER(device_address, return);
2788 BT_CHECK_PARAMETER(pin_code, return);
2789 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2790 BLUETOOTH_ERROR_NO_RESOURCES);
2792 _bt_convert_addr_type_to_string(address, device_address->addr);
2794 for (l = pin_info_list; l != NULL; l = l->next) {
2797 if (g_strcmp0(pin_info->address, address) == 0) {
2798 g_free(pin_info->pin_code);
2799 pin_info->pin_code = g_strdup(pin_code->pin_code);
2800 return BLUETOOTH_ERROR_NONE;
2804 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2805 pin_info->address = g_strdup(address);
2806 pin_info->pin_code = g_strdup(pin_code->pin_code);
2807 pin_info_list = g_slist_append(pin_info_list, pin_info);
2809 return BLUETOOTH_ERROR_NONE;
2812 gint __bt_compare_address(gpointer *a, gpointer *b)
2814 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2815 char *address = (char *)b;
2816 return g_strcmp0(pin_info->address, address);
2819 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2821 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2823 bt_pin_code_info_t *pin_info = NULL;
2825 BT_CHECK_PARAMETER(device_address, return);
2827 _bt_convert_addr_type_to_string(address, device_address->addr);
2829 l = g_slist_find_custom(pin_info_list, address,
2830 (GCompareFunc)__bt_compare_address);
2834 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2835 g_free(pin_info->address);
2836 g_free(pin_info->pin_code);
2840 return BLUETOOTH_ERROR_NONE;
2843 int _bt_get_device_pin_code(const char *address, char *pin_code)
2847 BT_CHECK_PARAMETER(address, return);
2848 BT_CHECK_PARAMETER(pin_code, return);
2850 for (l = pin_info_list; l != NULL; l = l->next) {
2851 bt_pin_code_info_t *pin_info = l->data;
2853 if (g_strcmp0(pin_info->address, address) == 0) {
2854 g_strlcpy(pin_code, pin_info->pin_code,
2855 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2857 return BLUETOOTH_ERROR_NONE;
2861 return BLUETOOTH_ERROR_NOT_FOUND;
2864 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2865 bluetooth_le_connection_param_t *param)
2868 return BLUETOOTH_ERROR_INVALID_PARAM;
2870 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2871 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2872 return BLUETOOTH_ERROR_INVALID_PARAM;
2874 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2877 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2878 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2879 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2880 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2881 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2884 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2885 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2886 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2887 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2888 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2891 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2892 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2893 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2894 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2895 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2899 BT_ERR("Unhandled mode : %d", mode);
2903 return BLUETOOTH_ERROR_NONE;
2906 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2907 guint trusted_profile_flag, guint *trusted)
2909 int trust_profile = FALSE;
2913 case TRUSTED_PROFILE_PBAP:
2914 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2915 trust_profile = trusted_profile_flag & (1 << 1);
2917 return BLUETOOTH_ERROR_NOT_SUPPORT;
2919 case TRUSTED_PROFILE_MAP:
2920 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2921 trust_profile = trusted_profile_flag & (1 << 3);
2923 return BLUETOOTH_ERROR_NOT_SUPPORT;
2925 case TRUSTED_PROFILE_SAP:
2926 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2927 trust_profile = trusted_profile_flag & (1 << 5);
2929 return BLUETOOTH_ERROR_NOT_SUPPORT;
2931 case TRUSTED_PROFILE_HFP_HF: {
2932 guint flag = (trusted_profile_flag & (PROFILE_SUPPORTED << 6)) >> 6;
2933 BT_DBG("trusted_profile_flag %x", trusted_profile_flag);
2934 if (flag != PROFILE_BLOCKED)
2935 trust_profile = TRUE;
2940 case TRUSTED_PROFILE_A2DP: {
2941 guint flag = (trusted_profile_flag & (PROFILE_SUPPORTED << 8)) >> 8;
2942 BT_DBG("trusted_profile_flag %x", trusted_profile_flag);
2943 if (flag != PROFILE_BLOCKED)
2944 trust_profile = TRUE;
2949 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2950 *trusted = trusted_profile_flag;
2951 return BLUETOOTH_ERROR_NONE;
2953 return BLUETOOTH_ERROR_NOT_SUPPORT;
2959 return BLUETOOTH_ERROR_NONE;
2962 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2965 case TRUSTED_PROFILE_PBAP:
2966 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2967 case TRUSTED_PROFILE_MAP:
2968 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2969 case TRUSTED_PROFILE_SAP:
2970 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2971 case TRUSTED_PROFILE_HFP_HF:
2972 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2973 case TRUSTED_PROFILE_A2DP:
2974 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2975 case TRUSTED_PROFILE_ALL:
2982 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2984 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2985 return TRUSTED_PROFILE_PBAP;
2986 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2987 return TRUSTED_PROFILE_MAP;
2988 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2989 return TRUSTED_PROFILE_SAP;
2991 return 0; /* 0 - Unknown Profile */
2994 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2995 bluetooth_trusted_profile_t profile, gboolean trust)
2997 int ret = BLUETOOTH_ERROR_NONE;
2998 GDBusConnection *conn;
3000 GError *error = NULL;
3001 char *device_path = NULL;
3003 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3006 BT_CHECK_PARAMETER(bd_addr, return);
3007 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3008 bd_addr->addr[0], bd_addr->addr[1],
3009 bd_addr->addr[2], bd_addr->addr[3],
3010 bd_addr->addr[4], bd_addr->addr[5],
3013 conn = _bt_gdbus_get_system_gconn();
3014 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3016 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3018 device_path = _bt_get_device_object_path(address);
3019 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3021 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3022 NULL, BT_BLUEZ_NAME, device_path,
3023 BT_DEVICE_INTERFACE, NULL, NULL);
3025 g_free(device_path);
3026 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3028 uuid = _bt_get_trusted_profile_uuid(profile);
3030 g_object_unref(proxy);
3031 return BLUETOOTH_ERROR_NOT_SUPPORT;
3034 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3035 g_variant_new("(sb)", uuid, trust),
3036 G_DBUS_CALL_FLAGS_NONE, -1,
3038 g_object_unref(proxy);
3040 if (reply == NULL) {
3041 BT_ERR("Failed to Set Profile Trusted");
3042 ret = BLUETOOTH_ERROR_INTERNAL;
3044 BT_ERR("Error %s[%s]", error->message, address);
3045 g_error_free(error);
3049 g_variant_unref(reply);
3056 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3057 bluetooth_trusted_profile_t profile, guint *trust)
3059 int ret = BLUETOOTH_ERROR_NONE;
3060 GDBusConnection *conn;
3062 GError *error = NULL;
3063 char *device_path = NULL;
3064 guint trusted_profile_flag;
3065 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3068 BT_CHECK_PARAMETER(bd_addr, return);
3069 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3070 bd_addr->addr[0], bd_addr->addr[1],
3071 bd_addr->addr[2], bd_addr->addr[3],
3072 bd_addr->addr[4], bd_addr->addr[5],
3075 conn = _bt_gdbus_get_system_gconn();
3076 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3078 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3080 device_path = _bt_get_device_object_path(address);
3081 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3083 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3084 NULL, BT_BLUEZ_NAME, device_path,
3085 BT_PROPERTIES_INTERFACE, NULL, NULL);
3087 g_free(device_path);
3088 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3090 reply = g_dbus_proxy_call_sync(proxy, "Get",
3091 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3092 G_DBUS_CALL_FLAGS_NONE, -1,
3094 g_object_unref(proxy);
3096 if (reply == NULL) {
3097 BT_ERR("Failed to Get Profile Trusted");
3098 ret = BLUETOOTH_ERROR_INTERNAL;
3100 BT_ERR("Error %s[%s]", error->message, address);
3101 g_error_free(error);
3106 g_variant_get(reply, "(v)", &temp);
3107 trusted_profile_flag = g_variant_get_uint32(temp);
3108 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3110 ret = _bt_get_trusted_profile_from_flag(profile,
3111 trusted_profile_flag, trust);
3112 g_variant_unref(temp);
3113 g_variant_unref(reply);
3116 BT_DBG("TRUST %d", *trust);
3120 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3124 GVariant *out_param1;
3125 request_info_t *req_info;
3126 GVariant *val = NULL;
3127 GVariant *param = NULL;
3130 bluetooth_device_address_t device_addr = { {0} };
3131 int result = BLUETOOTH_ERROR_NONE;
3134 val = g_dbus_proxy_call_finish(proxy, res, &err);
3136 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3137 if (req_info == NULL) {
3138 BT_ERR("req_info == NULL");
3139 g_object_unref(proxy);
3140 att_mtu_req_info->device_proxy = NULL;
3145 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3147 if (g_strrstr(err->message, "NotSupported")) {
3148 BT_INFO("Connection Not Supported");
3149 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3150 } else if (g_strrstr(err->message, "NotConnected")) {
3151 BT_INFO("Not connected");
3152 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3153 } else if (g_strrstr(err->message, "InvalidArguments")) {
3154 BT_INFO("Not connected");
3155 result = BLUETOOTH_ERROR_INVALID_PARAM;
3157 BT_DBG("Default case");
3158 result = BLUETOOTH_ERROR_INTERNAL;
3162 g_object_unref(proxy);
3163 att_mtu_req_info->device_proxy = NULL;
3165 if (result != BLUETOOTH_ERROR_NONE)
3169 g_variant_get(val, "(qy)", &mtu, &status);
3170 g_variant_unref(val);
3173 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3175 param = g_variant_new("(isqy)",
3177 att_mtu_req_info->addr,
3181 /* Send the event to application */
3182 _bt_send_event(BT_DEVICE_EVENT,
3183 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3187 if (req_info->context == NULL)
3190 _bt_convert_addr_string_to_type(device_addr.addr,
3191 (const char *)att_mtu_req_info->addr);
3193 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3194 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3195 g_dbus_method_invocation_return_value(req_info->context,
3196 g_variant_new("(iv)", result, out_param1));
3198 _bt_delete_request_list(req_info->req_id);
3203 g_free(att_mtu_req_info->addr);
3204 g_free(att_mtu_req_info);
3205 att_mtu_req_info = NULL;
3210 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3213 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3214 gchar *device_path = NULL;
3215 GDBusProxy *adapter_proxy;
3216 GDBusProxy *device_proxy;
3217 GDBusConnection *conn;
3218 int ret = BLUETOOTH_ERROR_NONE;
3220 BT_CHECK_PARAMETER(device_address, return);
3222 if (att_mtu_req_info) {
3223 BT_ERR("ATT MTU request in progress");
3224 return BLUETOOTH_ERROR_DEVICE_BUSY;
3227 conn = _bt_gdbus_get_system_gconn();
3228 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3230 adapter_proxy = _bt_get_adapter_proxy();
3231 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3233 _bt_convert_addr_type_to_string(address, device_address->addr);
3235 BT_DBG("Remote device address: %s", address);
3237 device_path = _bt_get_device_object_path(address);
3239 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3241 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3242 NULL, BT_BLUEZ_NAME,
3243 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3244 g_free(device_path);
3245 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3247 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3248 att_mtu_req_info->addr = (char *)g_strdup(address);
3249 att_mtu_req_info->req_id = request_id;
3250 att_mtu_req_info->device_proxy = device_proxy;
3252 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3253 g_variant_new("(q)", mtu),
3254 G_DBUS_CALL_FLAGS_NONE,
3255 BT_MAX_DBUS_TIMEOUT,
3257 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3263 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3266 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3267 char *object_path = NULL;
3269 GDBusProxy *device_proxy;
3270 GError *error = NULL;
3272 GDBusConnection *conn;
3273 GVariant *result = NULL;
3275 BT_CHECK_PARAMETER(device_address, return);
3277 conn = _bt_gdbus_get_system_gconn();
3278 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3280 _bt_convert_addr_type_to_string(address, device_address->addr);
3282 object_path = _bt_get_device_object_path(address);
3283 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3285 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3286 NULL, BT_BLUEZ_NAME, object_path,
3287 BT_PROPERTIES_INTERFACE, NULL, NULL);
3288 g_free(object_path);
3289 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3291 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3292 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3293 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3295 ERR("DBus Error : %s", error->message);
3296 g_clear_error(&error);
3297 g_object_unref(device_proxy);
3298 return BLUETOOTH_ERROR_INTERNAL;
3300 g_variant_get(result, "(v)", &value);
3301 *mtu = g_variant_get_uint16(value);
3302 BT_DBG("ATT MTU : %d", *mtu);
3303 g_variant_unref(value);
3304 g_variant_unref(result);
3307 g_object_unref(device_proxy);
3308 return BLUETOOTH_ERROR_NONE;
3311 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3312 bluetooth_device_address_t *id_address)
3314 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3315 gchar *device_path = NULL;
3316 const gchar *idaddress = NULL;
3317 GDBusProxy *device_proxy;
3318 GError *error = NULL;
3319 GVariant *result = NULL;
3320 GDBusConnection *conn;
3321 int ret = BLUETOOTH_ERROR_NONE;
3323 BT_CHECK_PARAMETER(device_address, return);
3325 conn = _bt_gdbus_get_system_gconn();
3326 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3328 _bt_convert_addr_type_to_string(address, device_address->addr);
3330 device_path = _bt_get_device_object_path(address);
3331 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3333 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3334 NULL, BT_BLUEZ_NAME,
3335 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3336 g_free(device_path);
3337 if (!device_proxy) {
3338 BT_ERR("Unable to get proxy");
3339 return BLUETOOTH_ERROR_INTERNAL;
3342 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3344 G_DBUS_CALL_FLAGS_NONE,
3349 if (result == NULL) {
3350 BT_ERR("Failed to get device ID address");
3351 if (error != NULL) {
3352 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3353 g_error_free(error);
3355 g_object_unref(device_proxy);
3356 return BLUETOOTH_ERROR_INTERNAL;
3359 g_variant_get(result , "(s)", &idaddress);
3360 g_variant_unref(result);
3362 if (idaddress == NULL) {
3363 BT_ERR("No paired device");
3364 g_object_unref(device_proxy);
3365 return BLUETOOTH_ERROR_NOT_PAIRED;
3368 BT_DBG("ID Address:%s", idaddress);
3371 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3373 ret = BLUETOOTH_ERROR_INTERNAL;
3375 g_object_unref(device_proxy);
3380 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3382 GapAgentPrivate *agent = _bt_get_adapter_agent();
3383 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3385 if (authentication_reply)
3386 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3388 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3390 BT_DBG("BT_PASSKEY_REPLY");
3391 return BLUETOOTH_ERROR_NONE;
3394 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3396 GapAgentPrivate *agent = _bt_get_adapter_agent();
3397 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3399 if (confirmation_reply)
3400 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3402 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3404 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3405 return BLUETOOTH_ERROR_NONE;