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 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
678 g_variant_new("(s)", bonding_info->addr),
679 G_DBUS_CALL_FLAGS_NONE,
684 return BLUETOOTH_ERROR_INTERNAL;
686 g_variant_get(result , "(o)", &device_path);
687 g_variant_unref(result);
689 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
691 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
692 g_variant_new("(o)", device_path),
693 G_DBUS_CALL_FLAGS_NONE,
699 BT_ERR("UnpairDevice Fail: %s", err->message);
701 return BLUETOOTH_ERROR_INTERNAL;
704 return __bt_retry_bond();
707 static int __bt_cancel_and_bond(void)
709 int ret = BLUETOOTH_ERROR_NONE;
711 ret = _bt_agent_reply_cancellation();
712 if (ret != BLUETOOTH_ERROR_NONE) {
713 BT_ERR("Fail to call reply cancellation");
716 return __bt_retry_bond();
720 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
723 int result = BLUETOOTH_ERROR_NONE;
726 GVariant *out_param1;
727 request_info_t *req_info;
728 bluetooth_device_info_t dev_info;
729 bt_remote_dev_info_t *remote_dev_info;
730 GVariant *manufacture_data;
733 /* Terminate ALL system popup */
734 if (headed_plugin_info->plugin_headed_enabled)
735 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
737 reply = g_dbus_proxy_call_finish(proxy, res, &err);
739 g_variant_unref(reply);
741 is_device_creating = FALSE;
743 if (bonding_info == NULL) {
745 BT_ERR("bonding_info == NULL");
751 req_info = _bt_get_request_info(bonding_info->req_id);
752 if (req_info == NULL) {
753 BT_ERR("req_info == NULL");
758 g_dbus_error_strip_remote_error(err);
759 BT_ERR("Error occured in CreateBonding [%s]", err->message);
761 if (g_strrstr(err->message, "Already Exists")) {
762 BT_INFO("Existing Bond, remove and retry");
763 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
765 result = BLUETOOTH_ERROR_PARING_FAILED;
766 } else if (_bt_agent_is_canceled() ||
767 g_strrstr(err->message, "Authentication Canceled")) {
768 BT_INFO("Cancelled by USER");
769 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
770 } else if (g_strrstr(err->message, "Authentication Rejected")) {
772 result = BLUETOOTH_ERROR_ACCESS_DENIED;
773 } else if (g_strrstr(err->message, "In Progress")) {
774 BT_INFO("Bond in progress, cancel and retry");
775 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
777 result = BLUETOOTH_ERROR_PARING_FAILED;
778 } else if (g_strrstr(err->message, "Authentication Failed")) {
779 BT_INFO("Authentication Failed");
780 if (bonding_info->is_autopair == TRUE) {
781 _bt_set_autopair_status_in_bonding_info(FALSE);
782 __ignore_auto_pairing_request(bonding_info->addr);
784 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
785 } else if (g_strrstr(err->message, "Page Timeout")) {
786 BT_INFO("Page Timeout");
787 /* This is the special case
788 As soon as call bluetooth_bond_device, try to cancel bonding.
789 In this case, before completing to call 'CreatePairedDevice' method
790 the procedure is stopped. So 'Cancle' error is not return.
792 result = BLUETOOTH_ERROR_HOST_DOWN;
793 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
794 BT_INFO("Cancel already running bonding");
795 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
796 BT_INFO("Error while Cancelling bonding");
797 /* we need to unref proxy so continue */
799 result = BLUETOOTH_ERROR_INTERNAL;
800 } else if (g_strrstr(err->message, "Connection Timeout")) {
801 /* Pairing request timeout */
802 result = BLUETOOTH_ERROR_TIMEOUT;
803 } else if (g_strrstr(err->message, "Authentication Timeout")) {
804 /* Pairing request timeout */
805 result = BLUETOOTH_ERROR_TIMEOUT;
807 BT_DBG("Default case");
808 result = BLUETOOTH_ERROR_PARING_FAILED;
812 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
813 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
814 result == BLUETOOTH_ERROR_TIMEOUT ||
815 result == BLUETOOTH_ERROR_HOST_DOWN) {
816 if (TIZEN_PROFILE_WEARABLE) {
817 int is_sw_running = 0;
819 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
820 BT_ERR("vconf_get_int for setup wizard state failed");
822 if (!is_sw_running) {
823 if (headed_plugin_info->plugin_headed_enabled)
824 headed_plugin_info->headed_plugin->bt_launch_unable_to_pairing_syspopup(result);
826 BT_ERR("Unable to pair");
829 bonding_info->result = result;
832 g_object_unref(proxy);
833 bonding_info->device_proxy = NULL;
835 if (result != BLUETOOTH_ERROR_NONE) {
836 if (bonding_info->bonding_wo_discovery) {
837 GDBusProxy *adapter_proxy;
838 GVariant *ret = NULL;
839 GError *error = NULL;
842 BT_ERR("Bond was tried without discovery. Remove it");
844 adapter_proxy = _bt_get_adapter_proxy();
845 if (adapter_proxy == NULL) {
846 BT_ERR("Cannot get adapter_proxy");
850 device_path = _bt_get_device_object_path(bonding_info->addr);
851 if (device_path == NULL) {
852 BT_ERR("Cannot get device path");
856 ret = g_dbus_proxy_call_sync(adapter_proxy,
858 g_variant_new("(o)", device_path),
859 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
862 BT_ERR("RemoveDevice Fail: %s", error->message);
863 g_clear_error(&error);
867 g_variant_unref(ret);
875 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
876 if (!remote_dev_info)
879 GVariant *uuids = NULL;
880 GVariantBuilder *builder = NULL;
882 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
883 for (i = 0; i < remote_dev_info->uuid_count; i++) {
884 g_variant_builder_add(builder, "s",
885 remote_dev_info->uuids[i]);
887 uuids = g_variant_new("as", builder);
888 g_variant_builder_unref(builder);
889 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
890 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
893 param = g_variant_new("(isunsbub@asn@ay)",
896 remote_dev_info->class,
897 remote_dev_info->rssi,
898 remote_dev_info->name,
899 remote_dev_info->paired,
900 remote_dev_info->connected,
901 remote_dev_info->trust,
903 remote_dev_info->manufacturer_data_len,
907 /* Send the event to application */
908 _bt_send_event(BT_ADAPTER_EVENT,
909 BLUETOOTH_EVENT_BONDING_FINISHED,
912 _bt_free_device_info(remote_dev_info);
915 if (req_info->context == NULL)
918 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
919 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
922 if (_bt_adapter_get_status() != BT_ACTIVATED)
923 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
925 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
926 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
928 g_dbus_method_invocation_return_value(req_info->context,
929 g_variant_new("(iv)", result, out_param1));
931 _bt_delete_request_list(req_info->req_id);
936 _bt_agent_set_canceled(FALSE);
939 g_free(bonding_info->addr);
940 g_free(bonding_info);
944 int _bt_bond_device(int request_id,
945 bluetooth_device_address_t *device_address,
946 unsigned short conn_type, GArray **out_param1)
949 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
950 bluetooth_device_info_t dev_info;
952 GDBusConnection *conn;
953 char *device_path = NULL;
954 GDBusProxy *adapter_proxy;
955 GError *error = NULL;
956 gboolean bonding_wo_discovery = FALSE;
958 BT_CHECK_PARAMETER(device_address, return);
961 BT_ERR("Bonding in progress");
963 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
964 memcpy(dev_info.device_address.addr, device_address->addr,
965 BLUETOOTH_ADDRESS_LENGTH);
967 g_array_append_vals(*out_param1, &dev_info,
968 sizeof(bluetooth_device_info_t));
970 return BLUETOOTH_ERROR_DEVICE_BUSY;
973 conn = _bt_gdbus_get_system_gconn();
974 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
976 _bt_convert_addr_type_to_string(address, device_address->addr);
978 device_path = _bt_get_device_object_path(address);
980 if (device_path == NULL) {
981 BT_ERR("No searched device");
982 GVariant *ret = NULL;
983 adapter_proxy = _bt_get_adapter_proxy();
984 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
986 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
987 g_variant_new("(s)", address),
988 G_DBUS_CALL_FLAGS_NONE,
994 BT_ERR("CreateDevice Fail: %s", error->message);
995 g_clear_error(&error);
998 g_variant_unref(ret);
999 device_path = _bt_get_device_object_path(address);
1000 if (device_path == NULL) {
1001 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1002 memcpy(dev_info.device_address.addr, device_address->addr,
1003 BLUETOOTH_ADDRESS_LENGTH);
1005 g_array_append_vals(*out_param1, &dev_info,
1006 sizeof(bluetooth_device_info_t));
1008 return BLUETOOTH_ERROR_NOT_PAIRED;
1010 BT_INFO("device_path is created[%s]", device_path);
1012 bonding_wo_discovery = TRUE;
1015 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1016 NULL, BT_BLUEZ_NAME,
1017 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1019 g_free(device_path);
1020 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1022 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1023 bonding_info->addr = g_strdup(address);
1024 bonding_info->req_id = request_id;
1026 bonding_info->device_proxy = proxy;
1027 bonding_info->conn_type = conn_type;
1028 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1030 is_device_creating = TRUE;
1032 g_dbus_proxy_call(proxy, "Pair",
1033 g_variant_new("(y)", conn_type),
1034 G_DBUS_CALL_FLAGS_NONE,
1035 BT_MAX_DBUS_TIMEOUT,
1037 (GAsyncReadyCallback)__bt_bond_device_cb,
1040 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1042 return BLUETOOTH_ERROR_NONE;
1044 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1045 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1046 bonding_info->addr);
1048 g_array_append_vals(*out_param1, &dev_info,
1049 sizeof(bluetooth_device_info_t));
1051 is_device_creating = FALSE;
1053 g_free(bonding_info->addr);
1054 g_free(bonding_info);
1055 bonding_info = NULL;
1057 return BLUETOOTH_ERROR_INTERNAL;*/
1060 int _bt_cancel_bonding(void)
1062 int ret = BLUETOOTH_ERROR_NONE;
1064 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1066 ret = _bt_agent_reply_cancellation();
1067 if (ret != BLUETOOTH_ERROR_NONE) {
1068 BT_ERR("Fail to call reply cancellation");
1072 _bt_agent_set_canceled(TRUE);
1074 return BLUETOOTH_ERROR_NONE;
1077 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1082 GVariant *out_param1;
1083 int result = BLUETOOTH_ERROR_NONE;
1084 bt_funcion_data_t *unbonding_info;
1085 bluetooth_device_info_t dev_info;
1086 request_info_t *req_info;
1088 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1090 g_variant_unref(reply);
1092 unbonding_info = user_data;
1094 if (unbonding_info == NULL) {
1096 BT_ERR("unbonding_info == NULL");
1100 req_info = _bt_get_request_info(unbonding_info->req_id);
1101 if (req_info == NULL) {
1102 BT_ERR("req_info == NULL");
1107 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1108 result = BLUETOOTH_ERROR_INTERNAL;
1111 if (req_info->context == NULL)
1114 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1115 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1116 unbonding_info->addr);
1118 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1119 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1121 g_dbus_method_invocation_return_value(req_info->context,
1122 g_variant_new("(iv)", result, out_param1));
1125 _bt_delete_request_list(req_info->req_id);
1131 if (unbonding_info) {
1132 g_free(unbonding_info->addr);
1133 g_free(unbonding_info);
1137 int _bt_unbond_device(int request_id,
1138 bluetooth_device_address_t *device_address,
1139 GArray **out_param1)
1141 char *device_path = NULL;
1142 bt_funcion_data_t *unbonding_info;
1143 GDBusProxy *adapter_proxy = NULL;
1144 GDBusProxy *device_proxy = NULL;
1145 GDBusConnection *conn;
1146 int result = BLUETOOTH_ERROR_INTERNAL;
1147 bluetooth_device_info_t dev_info;
1148 GError *error = NULL;
1149 GVariant *ret = NULL;
1151 BT_CHECK_PARAMETER(device_address, return);
1153 adapter_proxy = _bt_get_adapter_proxy();
1154 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1156 /* allocate user data so that it can be retrieved in callback */
1157 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1158 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1159 unbonding_info->req_id = request_id;
1161 _bt_convert_addr_type_to_string(unbonding_info->addr,
1162 device_address->addr);
1164 device_path = _bt_get_device_object_path(unbonding_info->addr);
1166 if (device_path == NULL) {
1167 BT_ERR("No paired device");
1168 result = BLUETOOTH_ERROR_NOT_PAIRED;
1172 conn = _bt_gdbus_get_system_gconn();
1174 BT_ERR("conn is NULL");
1175 result = BLUETOOTH_ERROR_INTERNAL;
1179 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1180 NULL, BT_BLUEZ_NAME,
1181 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1183 if (device_proxy != NULL) {
1185 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1186 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1187 G_DBUS_CALL_FLAGS_NONE,
1192 BT_ERR("Getting property failed: [%s]\n", error->message);
1193 g_error_free(error);
1194 result = BLUETOOTH_ERROR_NOT_PAIRED;
1198 BT_ERR("No paired device");
1199 g_object_unref(device_proxy);
1200 result = BLUETOOTH_ERROR_NOT_PAIRED;
1203 g_variant_unref(ret);
1205 g_object_unref(device_proxy);
1208 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1209 g_variant_new("(o)", device_path),
1210 G_DBUS_CALL_FLAGS_NONE,
1211 BT_MAX_DBUS_TIMEOUT,
1213 (GAsyncReadyCallback)__bt_unbond_cb,
1216 g_free(device_path);
1217 return BLUETOOTH_ERROR_NONE;
1220 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1221 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1222 unbonding_info->addr);
1224 g_array_append_vals(*out_param1, &dev_info,
1225 sizeof(bluetooth_device_info_t));
1226 g_free(device_path);
1227 g_free(unbonding_info->addr);
1228 g_free(unbonding_info);
1232 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1237 GVariant *out_param1;
1238 int result = BLUETOOTH_ERROR_NONE;
1239 bluetooth_device_info_t dev_info;
1240 bt_remote_dev_info_t *remote_dev_info;
1241 request_info_t *req_info;
1242 GVariant *uuid_list, *manufacture_data;
1244 GVariantBuilder *builder = NULL;
1247 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1249 g_variant_unref(reply);
1251 g_object_unref(proxy);
1253 if (searching_info == NULL) {
1255 BT_ERR("unbonding_info == NULL");
1259 req_info = _bt_get_request_info(searching_info->req_id);
1260 if (req_info == NULL) {
1261 BT_ERR("req_info == NULL");
1266 g_dbus_error_strip_remote_error(err);
1267 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1269 if (g_strrstr("Operation canceled", err->message))
1270 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1271 else if (g_strrstr("In Progress", err->message))
1272 result = BLUETOOTH_ERROR_IN_PROGRESS;
1273 else if (g_strrstr("Host is down", err->message))
1274 result = BLUETOOTH_ERROR_HOST_DOWN;
1276 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1278 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1279 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1280 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1281 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1282 remote_dev_info->uuid_count > 0) {
1283 result = BLUETOOTH_ERROR_NONE;
1286 _bt_free_device_info(remote_dev_info);
1291 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1292 if (!remote_dev_info)
1296 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1297 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1298 g_variant_builder_add(builder, "s",
1299 remote_dev_info->uuids[i]);
1301 uuid_list = g_variant_new("as", builder);
1302 g_variant_builder_unref(builder);
1303 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1304 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1307 param = g_variant_new("(isunsbub@asn@ay)",
1309 searching_info->addr,
1310 remote_dev_info->class,
1311 remote_dev_info->rssi,
1312 remote_dev_info->name,
1313 remote_dev_info->paired,
1314 remote_dev_info->connected,
1315 remote_dev_info->trust,
1317 remote_dev_info->manufacturer_data_len,
1320 /* Send the event to application */
1321 _bt_send_event(BT_ADAPTER_EVENT,
1322 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1325 _bt_free_device_info(remote_dev_info);
1328 if (req_info->context == NULL)
1331 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1332 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1333 searching_info->addr);
1335 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1336 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1338 g_dbus_method_invocation_return_value(req_info->context,
1339 g_variant_new("(iv)", result, out_param1));
1342 _bt_delete_request_list(req_info->req_id);
1347 if (searching_info) {
1348 g_free(searching_info->addr);
1349 g_free(searching_info);
1350 searching_info = NULL;
1354 int _bt_search_device(int request_id,
1355 bluetooth_device_address_t *device_address)
1357 char *device_path = NULL;
1358 GDBusProxy *device_proxy = NULL;
1359 GDBusConnection *conn;
1361 GDBusProxy *adapter_proxy;
1362 int result = BLUETOOTH_ERROR_INTERNAL;
1364 BT_CHECK_PARAMETER(device_address, return);
1367 BT_ERR("Bonding in progress");
1368 return BLUETOOTH_ERROR_DEVICE_BUSY;
1371 if (searching_info) {
1372 BT_ERR("Service searching in progress");
1373 return BLUETOOTH_ERROR_DEVICE_BUSY;
1376 adapter_proxy = _bt_get_adapter_proxy();
1377 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1379 /* allocate user data so that it can be retrieved in callback */
1380 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1381 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1382 searching_info->req_id = request_id;
1384 _bt_convert_addr_type_to_string(searching_info->addr,
1385 device_address->addr);
1387 conn = _bt_gdbus_get_system_gconn();
1388 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1391 device_path = _bt_get_device_object_path(searching_info->addr);
1393 if (device_path == NULL) {
1394 BT_ERR("No paired device");
1395 result = BLUETOOTH_ERROR_NOT_PAIRED;
1399 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1400 NULL, BT_BLUEZ_NAME,
1401 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1402 g_free(device_path);
1403 if (device_proxy == NULL) {
1404 result = BLUETOOTH_ERROR_INTERNAL;
1408 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1409 g_variant_new("(s)", ""),
1410 G_DBUS_CALL_FLAGS_NONE,
1411 BT_MAX_DBUS_TIMEOUT,
1413 (GAsyncReadyCallback)__bt_discover_cb,
1416 searching_info->device_proxy = device_proxy;
1418 return BLUETOOTH_ERROR_NONE;
1421 g_free(searching_info->addr);
1422 g_free(searching_info);
1423 searching_info = NULL;
1427 int _bt_cancel_search_device(void)
1429 GVariant *ret = NULL;
1432 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1434 if (searching_info->device_proxy) {
1435 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1437 G_DBUS_CALL_FLAGS_NONE,
1442 g_variant_unref(ret);
1444 __bt_cancel_search_service_done();
1446 return BLUETOOTH_ERROR_NONE;
1449 int _bt_set_alias(bluetooth_device_address_t *device_address,
1452 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1453 gchar *device_path = NULL;
1454 GDBusProxy *adapter_proxy;
1455 GDBusProxy *device_proxy;
1456 GVariant *ret = NULL;
1457 GError *error = NULL;
1458 GDBusConnection *conn;
1460 BT_CHECK_PARAMETER(device_address, return);
1461 BT_CHECK_PARAMETER(alias, return);
1463 adapter_proxy = _bt_get_adapter_proxy();
1464 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1466 conn = _bt_gdbus_get_system_gconn();
1467 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1469 _bt_convert_addr_type_to_string(address, device_address->addr);
1471 device_path = _bt_get_device_object_path(address);
1473 if (device_path == NULL) {
1474 BT_ERR("No paired device");
1475 return BLUETOOTH_ERROR_NOT_PAIRED;
1478 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1479 NULL, BT_BLUEZ_NAME,
1480 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1482 g_free(device_path);
1483 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1485 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1486 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1487 G_DBUS_CALL_FLAGS_NONE,
1492 g_variant_unref(ret);
1494 g_object_unref(device_proxy);
1497 BT_ERR("SetProperty error: [%s]", error->message);
1498 g_error_free(error);
1499 return BLUETOOTH_ERROR_INTERNAL;
1502 return BLUETOOTH_ERROR_NONE;
1505 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1508 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1509 gchar *device_path = NULL;
1510 GDBusProxy *device_proxy;
1511 gboolean previous_value;
1512 GError *error = NULL;
1513 GDBusConnection *conn;
1514 GVariant *result = NULL;
1515 GVariant *temp = NULL;
1516 int ret = BLUETOOTH_ERROR_NONE;
1518 BT_CHECK_PARAMETER(device_address, return);
1520 conn = _bt_gdbus_get_system_gconn();
1521 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1523 _bt_convert_addr_type_to_string(address, device_address->addr);
1525 device_path = _bt_get_device_object_path(address);
1527 if (device_path == NULL) {
1528 BT_ERR("No paired device");
1529 return BLUETOOTH_ERROR_NOT_PAIRED;
1532 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1533 NULL, BT_BLUEZ_NAME,
1534 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1536 g_free(device_path);
1537 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1539 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1540 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1541 G_DBUS_CALL_FLAGS_NONE,
1545 if (error != NULL) {
1546 BT_ERR("Getting property failed: [%s]\n", error->message);
1547 g_error_free(error);
1548 g_object_unref(device_proxy);
1549 return BLUETOOTH_ERROR_INTERNAL;
1552 g_variant_get(result, "(v)", &temp);
1553 previous_value = g_variant_get_boolean(temp);
1554 g_variant_unref(temp);
1555 g_variant_unref(result);
1556 /* If the input is same with previous value, return error. */
1557 if (previous_value == authorize) {
1558 BT_ERR("Same value: %d", previous_value);
1559 g_object_unref(device_proxy);
1560 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1564 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1565 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1566 G_DBUS_CALL_FLAGS_NONE,
1571 g_object_unref(device_proxy);
1573 BT_ERR("SetProperty error: [%s]", error->message);
1574 g_error_free(error);
1575 ret = BLUETOOTH_ERROR_INTERNAL;
1579 g_variant_unref(result);
1584 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1585 gboolean *is_connected)
1587 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1588 char *object_path = NULL;
1590 GDBusProxy *device_proxy;
1591 GError *error = NULL;
1593 GDBusConnection *conn;
1594 GVariant *result = NULL;
1595 int ret = BLUETOOTH_ERROR_NONE;
1597 BT_CHECK_PARAMETER(device_address, return);
1599 conn = _bt_gdbus_get_system_gconn();
1600 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1602 _bt_convert_addr_type_to_string(address, device_address->addr);
1604 object_path = _bt_get_device_object_path(address);
1605 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1607 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1608 NULL, BT_BLUEZ_NAME, object_path,
1609 BT_PROPERTIES_INTERFACE, NULL, NULL);
1610 g_free(object_path);
1611 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1613 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1614 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1616 G_DBUS_CALL_FLAGS_NONE,
1620 if (result == NULL) {
1621 if (error != NULL) {
1622 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1623 g_error_free(error);
1625 g_object_unref(device_proxy);
1626 return BLUETOOTH_ERROR_INTERNAL;
1629 g_variant_get(result, "(v)", &value);
1630 *is_connected = g_variant_get_boolean(value);
1631 g_variant_unref(result);
1633 BT_DBG("gatt is connected : %d", *is_connected);
1635 g_variant_unref(value);
1636 g_object_unref(device_proxy);
1641 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1642 int connection_type, gboolean *is_connected)
1644 char *object_path = NULL;
1645 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1646 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1647 GDBusProxy *device_proxy = NULL;
1648 GDBusProxy *adapter_proxy = NULL;
1649 GDBusConnection *conn;
1650 GError *error = NULL;
1651 GVariant *tmp_value = NULL;
1652 GVariant *value = NULL;
1653 GVariant *result = NULL;
1656 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1657 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1659 _bt_convert_addr_type_to_string(address, device_address->addr);
1660 _bt_convert_addr_string_to_secure_string(secure_address, address);
1662 *is_connected = FALSE;
1663 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1665 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1666 return _bt_rfcomm_is_device_connected(device_address,
1668 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1669 return _bt_is_gatt_connected(device_address, is_connected);
1670 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1671 return _bt_pbap_is_connected(device_address, is_connected);
1673 adapter_proxy = _bt_get_adapter_proxy();
1674 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1676 conn = _bt_gdbus_get_system_gconn();
1677 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1679 _bt_convert_addr_type_to_string(address, device_address->addr);
1681 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1682 object_path = _bt_get_adapter_path();
1683 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1684 NULL, BT_BLUEZ_NAME,
1685 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1686 g_free(object_path);
1687 if (device_proxy == NULL) {
1688 BT_DBG("Device don't have this service");
1689 return BLUETOOTH_ERROR_INTERNAL;
1692 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1693 g_variant_new("(s)", address),
1694 G_DBUS_CALL_FLAGS_NONE,
1699 if (result == NULL) {
1700 BT_ERR("[GetProperties] Error occured in Proxy call");
1702 BT_ERR("%s", error->message);
1703 g_error_free(error);
1705 *is_connected = FALSE;
1706 g_object_unref(device_proxy);
1707 return BLUETOOTH_ERROR_NONE;
1709 g_variant_get(result , "(@a{sv})", &value);
1710 g_variant_unref(result);
1713 tmp_value = g_variant_lookup_value(value,
1715 G_VARIANT_TYPE_BOOLEAN);
1717 *is_connected = g_variant_get_boolean(tmp_value);
1718 g_variant_unref(tmp_value);
1720 g_variant_unref(value);
1722 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1723 return _bt_is_network_connected(_bt_get_net_conn(),
1724 device_address->addr, is_connected);
1726 uuid = _bt_get_profile_uuid128(connection_type);
1728 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1729 return BLUETOOTH_ERROR_INTERNAL;
1731 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1733 object_path = _bt_get_device_object_path(address);
1735 BT_ERR("object_path is NULL");
1737 return BLUETOOTH_ERROR_NOT_PAIRED;
1740 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1741 NULL, BT_BLUEZ_NAME,
1742 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1743 g_free(object_path);
1744 if (device_proxy == NULL) {
1745 BT_DBG("Device don't have this service");
1747 return BLUETOOTH_ERROR_INTERNAL;
1750 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1751 g_variant_new("(s)", uuid),
1752 G_DBUS_CALL_FLAGS_NONE,
1757 if (result == NULL) {
1758 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1760 BT_ERR("%s", error->message);
1761 if (g_strrstr(error->message, "Not Connected"))
1762 BT_DBG("Not connected");
1763 g_error_free(error);
1765 *is_connected = FALSE;
1766 g_object_unref(device_proxy);
1768 return BLUETOOTH_ERROR_NONE;
1770 g_variant_get(result, "(b)", is_connected);
1772 g_variant_unref(result);
1775 g_object_unref(device_proxy);
1776 return BLUETOOTH_ERROR_NONE;
1779 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1780 bluetooth_connected_link_t *connected)
1782 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1783 char *object_path = NULL;
1785 GDBusProxy *device_proxy;
1786 GError *error = NULL;
1787 GDBusConnection *conn;
1788 GVariant *value = NULL;
1789 GVariant *result = NULL;
1791 BT_CHECK_PARAMETER(device_address, return);
1793 conn = _bt_gdbus_get_system_gconn();
1794 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1796 _bt_convert_addr_type_to_string(address, device_address->addr);
1798 object_path = _bt_get_device_object_path(address);
1799 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1801 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1802 NULL, BT_BLUEZ_NAME,
1803 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1804 g_free(object_path);
1805 if (device_proxy == NULL) {
1806 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1807 return BLUETOOTH_ERROR_NONE;
1810 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1811 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1812 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1814 if (error != NULL) {
1815 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1816 g_error_free(error);
1817 g_object_unref(device_proxy);
1818 return BLUETOOTH_ERROR_INTERNAL;
1820 g_variant_get(result, "(v)", &value);
1821 *connected = g_variant_get_byte(value);
1822 g_variant_unref(value);
1823 g_variant_unref(result);
1826 g_object_unref(device_proxy);
1827 return BLUETOOTH_ERROR_NONE;
1830 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1834 GVariant *out_param1;
1836 int result = BLUETOOTH_ERROR_NONE;
1837 bt_function_data_t *func_data = user_data;
1838 request_info_t *req_info = NULL;
1839 bluetooth_device_address_t device_addr = { {0} };
1841 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1842 g_object_unref(proxy);
1844 if (reply == NULL) {
1845 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1847 BT_ERR("Error: %s", err->message);
1848 g_clear_error(&err);
1850 result = BLUETOOTH_ERROR_INTERNAL;
1852 g_variant_unref(reply);
1855 if (func_data == NULL) {
1856 BT_ERR("func_data is NULL");
1860 req_info = _bt_get_request_info(func_data->req_id);
1861 if (req_info == NULL) {
1862 BT_ERR("req_info is NULL");
1866 if (req_info->context == NULL) {
1867 BT_ERR("req_info->context is NULL");
1871 _bt_convert_addr_string_to_type(device_addr.addr,
1872 (const char *)func_data->address);
1874 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1875 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1878 g_dbus_method_invocation_return_value(req_info->context,
1879 g_variant_new("(iv)", result, out_param1));
1883 _bt_delete_request_list(req_info->req_id);
1886 g_free(func_data->address);
1891 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1892 gboolean auto_connect)
1894 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1895 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1896 gchar *device_path = NULL;
1897 GDBusProxy *device_proxy = NULL;
1898 GDBusConnection *conn;
1899 int ret = BLUETOOTH_ERROR_NONE;
1900 bt_function_data_t *func_data;
1902 BT_CHECK_PARAMETER(bd_addr, return);
1904 conn = _bt_gdbus_get_system_gconn();
1905 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1907 _bt_convert_addr_type_to_string(device_address,
1908 (unsigned char *)bd_addr->addr);
1909 device_path = _bt_get_device_object_path(device_address);
1910 if (device_path == NULL) {
1911 BT_ERR_C("device_path NULL : [%s]", device_address);
1912 ret = BLUETOOTH_ERROR_INTERNAL;
1916 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1917 NULL, BT_BLUEZ_NAME,
1918 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1919 g_free(device_path);
1920 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1922 func_data = g_malloc0(sizeof(bt_function_data_t));
1923 func_data->address = g_strdup(device_address);
1924 if (func_data->address == NULL) {
1925 BT_ERR("Unable to allocate memory for address");
1926 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1930 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1931 BT_INFO("Connect LE [%s]", secure_address);
1933 func_data->req_id = req_id;
1935 g_dbus_proxy_call(device_proxy, "ConnectLE",
1936 g_variant_new("(b)", auto_connect),
1937 G_DBUS_CALL_FLAGS_NONE,
1938 BT_MAX_DBUS_TIMEOUT,
1940 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1946 g_object_unref(device_proxy);
1948 g_free(func_data->address);
1954 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1958 BT_INFO("Try to initiate pending LE connection");
1960 pending_le_conn_timer_id = 0;
1962 ret = __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1963 &pending_le_conn_info->bd_addr,
1964 pending_le_conn_info->auto_connect);
1965 if (ret != BLUETOOTH_ERROR_NONE) {
1966 request_info_t *req_info = NULL;
1967 GVariant *out_param1;
1969 req_info = _bt_get_request_info(pending_le_conn_info->req_id);
1970 if (req_info == NULL) {
1971 BT_ERR("req_info is NULL");
1975 if (req_info->context == NULL) {
1976 BT_ERR("req_info->context is NULL");
1977 _bt_delete_request_list(req_info->req_id);
1981 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1982 &pending_le_conn_info->bd_addr,
1983 sizeof(bluetooth_device_address_t), TRUE,
1986 g_dbus_method_invocation_return_value(req_info->context,
1987 g_variant_new("(iv)", ret, out_param1));
1991 g_free(pending_le_conn_info);
1992 pending_le_conn_info = NULL;
1997 void _bt_pending_connect_le_device(void)
1999 if (pending_le_conn_timer_id > 0) {
2000 g_source_remove(pending_le_conn_timer_id);
2001 __bt_connect_le_timer_cb(NULL);
2005 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
2006 gboolean auto_connect)
2008 int ret = BLUETOOTH_ERROR_NONE;
2010 BT_CHECK_PARAMETER(bd_addr, return);
2012 ret = _bt_hold_current_advertising();
2013 if (ret == BLUETOOTH_ERROR_NONE) {
2014 BT_INFO("Current advertising is held");
2015 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2016 pending_le_conn_info->req_id = req_id;
2017 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2018 BLUETOOTH_ADDRESS_LENGTH);
2019 pending_le_conn_info->auto_connect = auto_connect;
2021 pending_le_conn_timer_id =
2022 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2024 return BLUETOOTH_ERROR_NONE;
2025 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
2026 BT_ERR("Unable to hold advertising");
2029 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2032 int _bt_disconnect_le_device(int req_id,
2033 const bluetooth_device_address_t *bd_addr)
2035 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2036 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2038 GDBusProxy *device_proxy;
2039 GDBusConnection *conn;
2040 int ret = BLUETOOTH_ERROR_NONE;
2041 bt_function_data_t *func_data;
2043 BT_CHECK_PARAMETER(bd_addr, return);
2045 conn = _bt_gdbus_get_system_gconn();
2046 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2048 _bt_convert_addr_type_to_string(device_address,
2049 (unsigned char *)bd_addr->addr);
2050 device_path = _bt_get_device_object_path(device_address);
2051 if (device_path == NULL) {
2052 BT_DBG("device_path NULL");
2053 ret = BLUETOOTH_ERROR_INTERNAL;
2057 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2059 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2060 NULL, BT_BLUEZ_NAME,
2061 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2062 g_free(device_path);
2063 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2065 func_data = g_malloc0(sizeof(bt_function_data_t));
2066 func_data->address = g_strdup(device_address);
2067 if (func_data->address == NULL) {
2068 BT_ERR("Unable to allocate memory for address");
2069 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2073 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2074 BT_INFO("Disconnect LE [%s]", secure_address);
2076 func_data->req_id = req_id;
2078 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2080 G_DBUS_CALL_FLAGS_NONE,
2081 BT_MAX_DBUS_TIMEOUT,
2083 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2088 g_object_unref(device_proxy);
2090 g_free(func_data->address);
2096 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2098 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2099 gchar *device_path = NULL;
2100 GError *error = NULL;
2101 GDBusProxy *device_proxy = NULL;
2102 GDBusProxy *adapter_proxy;
2103 GDBusConnection *conn;
2104 int ret = BLUETOOTH_ERROR_NONE;
2106 BT_CHECK_PARAMETER(bd_addr, return);
2108 _bt_convert_addr_type_to_string(device_address,
2109 (unsigned char *)bd_addr->addr);
2111 conn = _bt_gdbus_get_system_gconn();
2112 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2114 adapter_proxy = _bt_get_adapter_proxy();
2115 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2117 device_path = _bt_get_device_object_path(device_address);
2118 if (device_path == NULL) {
2119 BT_DBG("device_path NULL");
2120 ret = BLUETOOTH_ERROR_INTERNAL;
2124 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2126 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2127 NULL, BT_BLUEZ_NAME,
2128 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2129 g_free(device_path);
2130 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2132 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2134 G_DBUS_CALL_FLAGS_NONE,
2139 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2140 g_error_free(error);
2141 g_object_unref(device_proxy);
2142 return BLUETOOTH_ERROR_INTERNAL;
2144 g_object_unref(device_proxy);
2146 /* IPSP daemon launch */
2147 GDBusProxy *ipsp_proxy;
2149 ipsp_proxy = _bt_get_ipsp_proxy();
2150 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2152 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2153 NULL, G_DBUS_CALL_FLAGS_NONE,
2154 -1, NULL, NULL, NULL);
2159 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2161 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2162 gchar *device_path = NULL;
2163 GError *error = NULL;
2164 GDBusProxy *device_proxy = NULL;
2165 GDBusProxy *adapter_proxy;
2166 GDBusConnection *conn;
2167 int ret = BLUETOOTH_ERROR_NONE;
2169 BT_CHECK_PARAMETER(bd_addr, return);
2171 _bt_convert_addr_type_to_string(device_address,
2172 (unsigned char *)bd_addr->addr);
2174 conn = _bt_gdbus_get_system_gconn();
2175 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2177 adapter_proxy = _bt_get_adapter_proxy();
2178 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2180 device_path = _bt_get_device_object_path(device_address);
2181 if (device_path == NULL) {
2182 BT_DBG("device_path NULL");
2183 ret = BLUETOOTH_ERROR_INTERNAL;
2187 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2189 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2190 NULL, BT_BLUEZ_NAME,
2191 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2192 g_free(device_path);
2193 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2195 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2197 G_DBUS_CALL_FLAGS_NONE,
2202 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2203 g_error_free(error);
2204 g_object_unref(device_proxy);
2205 return BLUETOOTH_ERROR_INTERNAL;
2208 g_object_unref(device_proxy);
2213 int _bt_connect_profile(char *address, char *uuid,
2214 void *cb, gpointer func_data)
2218 GDBusConnection *conn;
2219 GDBusProxy *adapter_proxy;
2220 GVariant *result = NULL;
2221 GError *error = NULL;
2223 conn = _bt_gdbus_get_system_gconn();
2224 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2226 object_path = _bt_get_device_object_path(address);
2227 if (object_path == NULL) {
2228 BT_ERR("No searched device");
2230 adapter_proxy = _bt_get_adapter_proxy();
2231 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2233 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2234 g_variant_new("(s)", address),
2235 G_DBUS_CALL_FLAGS_NONE,
2240 if (error != NULL) {
2241 BT_ERR("CreateDevice Fail: %s", error->message);
2242 g_error_free(error);
2245 g_variant_unref(result);
2247 object_path = _bt_get_device_object_path(address);
2249 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2251 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2252 NULL, BT_BLUEZ_NAME,
2253 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2254 g_free(object_path);
2255 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2257 g_dbus_proxy_call(proxy, "ConnectProfile",
2258 g_variant_new("(s)", uuid),
2259 G_DBUS_CALL_FLAGS_NONE,
2260 BT_MAX_DBUS_TIMEOUT,
2262 (GAsyncReadyCallback)cb,
2265 return BLUETOOTH_ERROR_NONE;
2268 int _bt_disconnect_all(char *address)
2270 int ret = BLUETOOTH_ERROR_NONE;
2273 GDBusConnection *conn;
2274 GVariant *result = NULL;
2278 conn = _bt_gdbus_get_system_gconn();
2279 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2281 object_path = _bt_get_device_object_path(address);
2282 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2284 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2285 NULL, BT_BLUEZ_NAME,
2286 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2287 g_free(object_path);
2288 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2290 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2292 G_DBUS_CALL_FLAGS_NONE,
2297 BT_ERR("Dbus Call Error:[%s]", err->message);
2299 ret = BLUETOOTH_ERROR_INTERNAL;
2302 g_object_unref(proxy);
2304 g_variant_unref(result);
2309 int _bt_disconnect_profile(char *address, char *uuid,
2310 void *cb, gpointer func_data)
2314 GDBusConnection *conn;
2316 conn = _bt_gdbus_get_system_gconn();
2317 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2319 object_path = _bt_get_device_object_path(address);
2320 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2322 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2323 NULL, BT_BLUEZ_NAME,
2324 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2325 g_free(object_path);
2326 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2328 g_dbus_proxy_call(proxy, "DisconnectProfile",
2329 g_variant_new("(s)", uuid),
2330 G_DBUS_CALL_FLAGS_NONE,
2331 BT_MAX_DBUS_TIMEOUT,
2333 (GAsyncReadyCallback)cb,
2336 return BLUETOOTH_ERROR_NONE;
2339 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2340 int low_threshold, int in_range_threshold, int high_threshold)
2342 int ret = BLUETOOTH_ERROR_NONE;
2344 GVariant *result = NULL;
2345 GError *error = NULL;
2346 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2348 BT_CHECK_PARAMETER(bd_addr, return);
2349 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2350 bd_addr->addr[0], bd_addr->addr[1],
2351 bd_addr->addr[2], bd_addr->addr[3],
2352 bd_addr->addr[4], bd_addr->addr[5],
2354 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2355 in_range_threshold, high_threshold);
2357 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2359 proxy = _bt_get_adapter_proxy();
2360 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2362 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2363 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2364 G_DBUS_CALL_FLAGS_NONE,
2368 if (error != NULL) {
2369 BT_ERR("Dbus Call Error:[%s]", error->message);
2370 g_error_free(error);
2371 ret = BLUETOOTH_ERROR_INTERNAL;
2375 g_variant_unref(result);
2380 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2383 int ret = BLUETOOTH_ERROR_NONE;
2385 GVariant *result = NULL;
2386 GError *error = NULL;
2387 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2389 BT_CHECK_PARAMETER(bd_addr, return);
2390 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2391 bd_addr->addr[0], bd_addr->addr[1],
2392 bd_addr->addr[2], bd_addr->addr[3],
2393 bd_addr->addr[4], bd_addr->addr[5],
2396 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2398 proxy = _bt_get_adapter_proxy();
2399 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2401 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2402 g_variant_new("(si)", address, link_type),
2403 G_DBUS_CALL_FLAGS_NONE,
2408 if (error != NULL) {
2409 BT_ERR("Dbus Call Error:[%s]", error->message);
2410 g_error_free(error);
2411 ret = BLUETOOTH_ERROR_INTERNAL;
2415 g_variant_unref(result);
2420 static int __bt_le_set_conn_parameter(const char *address,
2421 float interval_min, float interval_max,
2422 guint16 latency, guint16 time_out)
2424 gchar *device_path = NULL;
2425 GError *error = NULL;
2426 GDBusProxy *device_proxy = NULL;
2427 GDBusConnection *conn;
2429 guint32 min, max, to;
2430 int ret = BLUETOOTH_ERROR_NONE;
2432 BT_CHECK_PARAMETER(address, return);
2434 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2435 interval_min, interval_max, latency, time_out);
2437 device_path = _bt_get_device_object_path((char *)address);
2439 if (device_path == NULL) {
2440 BT_ERR("device_path NULL");
2441 ret = BLUETOOTH_ERROR_INTERNAL;
2445 conn = _bt_gdbus_get_system_gconn();
2447 BT_ERR("conn NULL");
2448 ret = BLUETOOTH_ERROR_INTERNAL;
2452 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2453 NULL, BT_BLUEZ_NAME,
2454 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2456 g_free(device_path);
2457 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2459 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2460 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2461 to = time_out / BT_LE_CONN_TO_SPLIT;
2463 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2464 g_variant_new("(uuuu)", min, max, latency, to),
2465 G_DBUS_CALL_FLAGS_NONE,
2470 g_object_unref(device_proxy);
2471 if (reply == NULL) {
2473 BT_ERR("Error %s[%s]", error->message, address);
2474 if (g_strrstr(error->message, "In Progress"))
2475 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2477 ret = BLUETOOTH_ERROR_INTERNAL;
2478 g_error_free(error);
2482 g_variant_unref(reply);
2488 static void __bt_le_conn_param_free(void *data)
2490 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2492 BT_DBG("%s", param->sender);
2493 g_free(param->sender);
2497 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2499 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2500 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2502 return parama->key > paramb->key;
2505 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2508 bt_connected_le_dev_t *dev;
2513 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2516 if (g_strcmp0(dev->address, address) == 0)
2522 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2525 bt_le_conn_param_t *param = NULL;
2527 if (!dev || !sender)
2530 for (l = dev->senders; l; l = g_slist_next(l)) {
2532 if (g_strcmp0(param->sender, sender) == 0)
2539 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2540 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2542 bt_connected_le_dev_t *dev = NULL;
2543 bt_le_conn_param_t *param = NULL;
2544 bt_le_conn_param_t *data = NULL;
2546 if (!address || !sender)
2547 return BLUETOOTH_ERROR_INVALID_PARAM;
2549 dev = __bt_get_le_connected_dev_info(address);
2551 return BLUETOOTH_ERROR_INTERNAL;
2553 param = __bt_get_le_conn_param_info(dev, sender);
2555 data = g_malloc0(sizeof(bt_le_conn_param_t));
2556 data->sender = g_strdup(sender);
2557 data->interval_min = interval_min;
2558 data->interval_max = interval_max;
2559 data->latency = latency;
2560 data->time_out = time_out;
2561 data->key = interval_min + (interval_max - interval_min)/2;
2563 if (param == NULL) {
2564 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2565 dev->senders = g_slist_append(dev->senders, data);
2567 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2568 dev->senders = g_slist_remove(dev->senders, param);
2569 g_free(param->sender);
2571 dev->senders = g_slist_append(dev->senders, data);
2574 /* Sorting. First element have the minimum interval */
2575 dev->senders = g_slist_sort(dev->senders,
2576 (GCompareFunc)__bt_compare_le_conn_param_key);
2578 return BLUETOOTH_ERROR_NONE;
2581 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2583 bt_connected_le_dev_t *dev = NULL;
2584 bt_le_conn_param_t *param = NULL;
2586 if (!address || !sender)
2587 return BLUETOOTH_ERROR_INVALID_PARAM;
2589 dev = __bt_get_le_connected_dev_info(address);
2591 return BLUETOOTH_ERROR_INTERNAL;
2593 param = __bt_get_le_conn_param_info(dev, sender);
2595 BT_DBG("Remove param %s %s ", address, sender);
2596 dev->senders = g_slist_remove(dev->senders, param);
2597 g_free(param->sender);
2601 *is_removed = FALSE;
2603 return BLUETOOTH_ERROR_NONE;
2606 int _bt_remove_all_le_conn_param_info(const char *sender)
2609 bt_connected_le_dev_t *dev = NULL;
2610 bt_le_conn_param_t *param = NULL;
2611 gboolean is_removed = FALSE;
2612 char *sender_new = NULL;
2613 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2614 int ret = BLUETOOTH_ERROR_NONE;
2617 return BLUETOOTH_ERROR_INVALID_PARAM;
2619 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2621 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2624 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2625 g_slist_length(dev->senders));
2626 if (g_slist_length(dev->senders) > 0) {
2627 param = dev->senders->data;
2628 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2629 param->interval_min, param->interval_max);
2631 if (dev->interval_min != param->interval_min ||
2632 dev->interval_max != param->interval_max) {
2633 sender_new = g_strdup(param->sender);
2635 _bt_convert_addr_string_to_type(addr, dev->address);
2636 ret = _bt_le_conn_update(sender_new, addr,
2637 param->interval_min, param->interval_max,
2638 param->latency, param->time_out);
2641 if (ret != BLUETOOTH_ERROR_NONE)
2642 BT_ERR("Unable to set le connection parameter");
2645 BT_INFO("Set the default interval");
2647 bluetooth_le_connection_param_t param = { 0 };
2648 _bt_get_le_connection_parameter(
2649 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2652 ret = __bt_le_set_conn_parameter(dev->address,
2653 param.interval_min, param.interval_max,
2654 param.latency, param.timeout);
2655 if (ret == BLUETOOTH_ERROR_NONE) {
2656 dev->interval_min = param.interval_min;
2657 dev->interval_max = param.interval_max;
2666 void _bt_add_le_connected_dev_info(const char *address)
2668 bt_connected_le_dev_t *dev = NULL;
2673 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2674 dev->address = g_strdup(address);
2676 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2681 void _bt_remove_le_connected_dev_info(const char *address)
2683 bt_connected_le_dev_t *dev = NULL;
2688 dev = __bt_get_le_connected_dev_info(address);
2692 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2693 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2694 g_free(dev->address);
2700 int _bt_le_conn_update(const char *sender,
2701 unsigned char *device_address,
2702 float interval_min, float interval_max,
2703 guint16 latency, guint16 time_out)
2705 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2706 guint32 min_supervision_to;
2707 bt_connected_le_dev_t *dev = NULL;
2708 bt_le_conn_param_t *param = NULL;
2709 gboolean is_removed = FALSE;
2710 int ret = BLUETOOTH_ERROR_NONE;
2712 BT_CHECK_PARAMETER(device_address, return);
2714 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2715 sender, interval_min, interval_max, latency, time_out);
2717 if (interval_min > interval_max ||
2718 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2719 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2720 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2724 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2725 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2726 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2730 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2731 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2736 * The Supervision_Timeout in milliseconds shall be larger than
2737 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2738 * where Conn_Interval_Max is given in milliseconds.
2740 min_supervision_to = (1 + latency) * interval_max * 2;
2741 if (time_out <= min_supervision_to) {
2742 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2746 _bt_convert_addr_type_to_string(address, device_address);
2747 BT_DBG("Remote device address: %s", address);
2749 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2751 dev = __bt_get_le_connected_dev_info(address);
2753 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2757 if (g_slist_length(dev->senders) == 1)
2760 param = dev->senders->data;
2762 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2764 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2765 BT_DBG("Skip due to same interval");
2769 interval_min = param->interval_min;
2770 interval_max = param->interval_max;
2774 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2776 if (ret != BLUETOOTH_ERROR_NONE) {
2777 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2781 dev->interval_min = interval_min;
2782 dev->interval_max = interval_max;
2788 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2789 bluetooth_device_pin_code_t *pin_code)
2791 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2793 bt_pin_code_info_t *pin_info = NULL;
2795 BT_CHECK_PARAMETER(device_address, return);
2796 BT_CHECK_PARAMETER(pin_code, return);
2797 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2798 BLUETOOTH_ERROR_NO_RESOURCES);
2800 _bt_convert_addr_type_to_string(address, device_address->addr);
2802 for (l = pin_info_list; l != NULL; l = l->next) {
2805 if (g_strcmp0(pin_info->address, address) == 0) {
2806 g_free(pin_info->pin_code);
2807 pin_info->pin_code = g_strdup(pin_code->pin_code);
2808 return BLUETOOTH_ERROR_NONE;
2812 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2813 pin_info->address = g_strdup(address);
2814 pin_info->pin_code = g_strdup(pin_code->pin_code);
2815 pin_info_list = g_slist_append(pin_info_list, pin_info);
2817 return BLUETOOTH_ERROR_NONE;
2820 gint __bt_compare_address(gpointer *a, gpointer *b)
2822 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2823 char *address = (char *)b;
2824 return g_strcmp0(pin_info->address, address);
2827 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2829 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2831 bt_pin_code_info_t *pin_info = NULL;
2833 BT_CHECK_PARAMETER(device_address, return);
2835 _bt_convert_addr_type_to_string(address, device_address->addr);
2837 l = g_slist_find_custom(pin_info_list, address,
2838 (GCompareFunc)__bt_compare_address);
2842 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2843 g_free(pin_info->address);
2844 g_free(pin_info->pin_code);
2848 return BLUETOOTH_ERROR_NONE;
2851 int _bt_get_device_pin_code(const char *address, char *pin_code)
2855 BT_CHECK_PARAMETER(address, return);
2856 BT_CHECK_PARAMETER(pin_code, return);
2858 for (l = pin_info_list; l != NULL; l = l->next) {
2859 bt_pin_code_info_t *pin_info = l->data;
2861 if (g_strcmp0(pin_info->address, address) == 0) {
2862 g_strlcpy(pin_code, pin_info->pin_code,
2863 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2865 return BLUETOOTH_ERROR_NONE;
2869 return BLUETOOTH_ERROR_NOT_FOUND;
2872 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2873 bluetooth_le_connection_param_t *param)
2876 return BLUETOOTH_ERROR_INVALID_PARAM;
2878 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2879 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2880 return BLUETOOTH_ERROR_INVALID_PARAM;
2882 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2885 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2886 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2887 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2888 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2889 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2892 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2893 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2894 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2895 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2896 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2899 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2900 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2901 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2902 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2903 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2907 BT_ERR("Unhandled mode : %d", mode);
2911 return BLUETOOTH_ERROR_NONE;
2914 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2915 guint trusted_profile_flag, guint *trusted)
2917 int trust_profile = FALSE;
2921 case TRUSTED_PROFILE_PBAP:
2922 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2923 trust_profile = trusted_profile_flag & (1 << 1);
2925 return BLUETOOTH_ERROR_NOT_SUPPORT;
2927 case TRUSTED_PROFILE_MAP:
2928 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2929 trust_profile = trusted_profile_flag & (1 << 3);
2931 return BLUETOOTH_ERROR_NOT_SUPPORT;
2933 case TRUSTED_PROFILE_SAP:
2934 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2935 trust_profile = trusted_profile_flag & (1 << 5);
2937 return BLUETOOTH_ERROR_NOT_SUPPORT;
2939 case TRUSTED_PROFILE_HFP_HF: {
2940 guint flag = (trusted_profile_flag & (PROFILE_SUPPORTED << 6)) >> 6;
2941 BT_DBG("trusted_profile_flag %x", trusted_profile_flag);
2942 if (flag != PROFILE_BLOCKED)
2943 trust_profile = TRUE;
2948 case TRUSTED_PROFILE_A2DP: {
2949 guint flag = (trusted_profile_flag & (PROFILE_SUPPORTED << 8)) >> 8;
2950 BT_DBG("trusted_profile_flag %x", trusted_profile_flag);
2951 if (flag != PROFILE_BLOCKED)
2952 trust_profile = TRUE;
2957 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2958 *trusted = trusted_profile_flag;
2959 return BLUETOOTH_ERROR_NONE;
2961 return BLUETOOTH_ERROR_NOT_SUPPORT;
2967 return BLUETOOTH_ERROR_NONE;
2970 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2973 case TRUSTED_PROFILE_PBAP:
2974 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2975 case TRUSTED_PROFILE_MAP:
2976 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2977 case TRUSTED_PROFILE_SAP:
2978 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2979 case TRUSTED_PROFILE_HFP_HF:
2980 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2981 case TRUSTED_PROFILE_A2DP:
2982 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2983 case TRUSTED_PROFILE_ALL:
2990 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2992 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2993 return TRUSTED_PROFILE_PBAP;
2994 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2995 return TRUSTED_PROFILE_MAP;
2996 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2997 return TRUSTED_PROFILE_SAP;
2999 return 0; /* 0 - Unknown Profile */
3002 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
3003 bluetooth_trusted_profile_t profile, gboolean trust)
3005 int ret = BLUETOOTH_ERROR_NONE;
3006 GDBusConnection *conn;
3008 GError *error = NULL;
3009 char *device_path = NULL;
3011 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3014 BT_CHECK_PARAMETER(bd_addr, return);
3015 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3016 bd_addr->addr[0], bd_addr->addr[1],
3017 bd_addr->addr[2], bd_addr->addr[3],
3018 bd_addr->addr[4], bd_addr->addr[5],
3021 conn = _bt_gdbus_get_system_gconn();
3022 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3024 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3026 device_path = _bt_get_device_object_path(address);
3027 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3029 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3030 NULL, BT_BLUEZ_NAME, device_path,
3031 BT_DEVICE_INTERFACE, NULL, NULL);
3033 g_free(device_path);
3034 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3036 uuid = _bt_get_trusted_profile_uuid(profile);
3038 g_object_unref(proxy);
3039 return BLUETOOTH_ERROR_NOT_SUPPORT;
3042 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3043 g_variant_new("(sb)", uuid, trust),
3044 G_DBUS_CALL_FLAGS_NONE, -1,
3046 g_object_unref(proxy);
3048 if (reply == NULL) {
3049 BT_ERR("Failed to Set Profile Trusted");
3050 ret = BLUETOOTH_ERROR_INTERNAL;
3052 BT_ERR("Error %s[%s]", error->message, address);
3053 g_error_free(error);
3057 g_variant_unref(reply);
3064 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3065 bluetooth_trusted_profile_t profile, guint *trust)
3067 int ret = BLUETOOTH_ERROR_NONE;
3068 GDBusConnection *conn;
3070 GError *error = NULL;
3071 char *device_path = NULL;
3072 guint trusted_profile_flag;
3073 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3076 BT_CHECK_PARAMETER(bd_addr, return);
3077 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3078 bd_addr->addr[0], bd_addr->addr[1],
3079 bd_addr->addr[2], bd_addr->addr[3],
3080 bd_addr->addr[4], bd_addr->addr[5],
3083 conn = _bt_gdbus_get_system_gconn();
3084 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3086 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3088 device_path = _bt_get_device_object_path(address);
3089 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3091 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3092 NULL, BT_BLUEZ_NAME, device_path,
3093 BT_PROPERTIES_INTERFACE, NULL, NULL);
3095 g_free(device_path);
3096 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3098 reply = g_dbus_proxy_call_sync(proxy, "Get",
3099 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3100 G_DBUS_CALL_FLAGS_NONE, -1,
3102 g_object_unref(proxy);
3104 if (reply == NULL) {
3105 BT_ERR("Failed to Get Profile Trusted");
3106 ret = BLUETOOTH_ERROR_INTERNAL;
3108 BT_ERR("Error %s[%s]", error->message, address);
3109 g_error_free(error);
3114 g_variant_get(reply, "(v)", &temp);
3115 trusted_profile_flag = g_variant_get_uint32(temp);
3116 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3118 ret = _bt_get_trusted_profile_from_flag(profile,
3119 trusted_profile_flag, trust);
3120 g_variant_unref(temp);
3121 g_variant_unref(reply);
3124 BT_DBG("TRUST %d", *trust);
3128 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3132 GVariant *out_param1;
3133 request_info_t *req_info;
3134 GVariant *val = NULL;
3135 GVariant *param = NULL;
3138 bluetooth_device_address_t device_addr = { {0} };
3139 int result = BLUETOOTH_ERROR_NONE;
3142 val = g_dbus_proxy_call_finish(proxy, res, &err);
3144 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3145 if (req_info == NULL) {
3146 BT_ERR("req_info == NULL");
3147 g_object_unref(proxy);
3148 att_mtu_req_info->device_proxy = NULL;
3153 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3155 if (g_strrstr(err->message, "NotSupported")) {
3156 BT_INFO("Connection Not Supported");
3157 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3158 } else if (g_strrstr(err->message, "NotConnected")) {
3159 BT_INFO("Not connected");
3160 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3161 } else if (g_strrstr(err->message, "InvalidArguments")) {
3162 BT_INFO("Not connected");
3163 result = BLUETOOTH_ERROR_INVALID_PARAM;
3165 BT_DBG("Default case");
3166 result = BLUETOOTH_ERROR_INTERNAL;
3170 g_object_unref(proxy);
3171 att_mtu_req_info->device_proxy = NULL;
3173 if (result != BLUETOOTH_ERROR_NONE)
3177 g_variant_get(val, "(qy)", &mtu, &status);
3178 g_variant_unref(val);
3181 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3183 param = g_variant_new("(isqy)",
3185 att_mtu_req_info->addr,
3189 /* Send the event to application */
3190 _bt_send_event(BT_DEVICE_EVENT,
3191 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3195 if (req_info->context == NULL)
3198 _bt_convert_addr_string_to_type(device_addr.addr,
3199 (const char *)att_mtu_req_info->addr);
3201 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3202 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3203 g_dbus_method_invocation_return_value(req_info->context,
3204 g_variant_new("(iv)", result, out_param1));
3206 _bt_delete_request_list(req_info->req_id);
3211 g_free(att_mtu_req_info->addr);
3212 g_free(att_mtu_req_info);
3213 att_mtu_req_info = NULL;
3218 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3221 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3222 gchar *device_path = NULL;
3223 GDBusProxy *adapter_proxy;
3224 GDBusProxy *device_proxy;
3225 GDBusConnection *conn;
3226 int ret = BLUETOOTH_ERROR_NONE;
3228 BT_CHECK_PARAMETER(device_address, return);
3230 if (att_mtu_req_info) {
3231 BT_ERR("ATT MTU request in progress");
3232 return BLUETOOTH_ERROR_DEVICE_BUSY;
3235 conn = _bt_gdbus_get_system_gconn();
3236 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3238 adapter_proxy = _bt_get_adapter_proxy();
3239 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3241 _bt_convert_addr_type_to_string(address, device_address->addr);
3243 BT_DBG("Remote device address: %s", address);
3245 device_path = _bt_get_device_object_path(address);
3247 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3249 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3250 NULL, BT_BLUEZ_NAME,
3251 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3252 g_free(device_path);
3253 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3255 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3256 att_mtu_req_info->addr = (char *)g_strdup(address);
3257 att_mtu_req_info->req_id = request_id;
3258 att_mtu_req_info->device_proxy = device_proxy;
3260 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3261 g_variant_new("(q)", mtu),
3262 G_DBUS_CALL_FLAGS_NONE,
3263 BT_MAX_DBUS_TIMEOUT,
3265 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3271 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3274 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3275 char *object_path = NULL;
3277 GDBusProxy *device_proxy;
3278 GError *error = NULL;
3280 GDBusConnection *conn;
3281 GVariant *result = NULL;
3283 BT_CHECK_PARAMETER(device_address, return);
3285 conn = _bt_gdbus_get_system_gconn();
3286 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3288 _bt_convert_addr_type_to_string(address, device_address->addr);
3290 object_path = _bt_get_device_object_path(address);
3291 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3293 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3294 NULL, BT_BLUEZ_NAME, object_path,
3295 BT_PROPERTIES_INTERFACE, NULL, NULL);
3296 g_free(object_path);
3297 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3299 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3300 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3301 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3303 ERR("DBus Error : %s", error->message);
3304 g_clear_error(&error);
3305 g_object_unref(device_proxy);
3306 return BLUETOOTH_ERROR_INTERNAL;
3308 g_variant_get(result, "(v)", &value);
3309 *mtu = g_variant_get_uint16(value);
3310 BT_DBG("ATT MTU : %d", *mtu);
3311 g_variant_unref(value);
3312 g_variant_unref(result);
3315 g_object_unref(device_proxy);
3316 return BLUETOOTH_ERROR_NONE;
3319 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3320 bluetooth_device_address_t *id_address)
3322 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3323 gchar *device_path = NULL;
3324 const gchar *idaddress = NULL;
3325 GDBusProxy *device_proxy;
3326 GError *error = NULL;
3327 GVariant *result = NULL;
3328 GDBusConnection *conn;
3329 int ret = BLUETOOTH_ERROR_NONE;
3331 BT_CHECK_PARAMETER(device_address, return);
3333 conn = _bt_gdbus_get_system_gconn();
3334 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3336 _bt_convert_addr_type_to_string(address, device_address->addr);
3338 device_path = _bt_get_device_object_path(address);
3339 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3341 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3342 NULL, BT_BLUEZ_NAME,
3343 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3344 g_free(device_path);
3345 if (!device_proxy) {
3346 BT_ERR("Unable to get proxy");
3347 return BLUETOOTH_ERROR_INTERNAL;
3350 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3352 G_DBUS_CALL_FLAGS_NONE,
3357 if (result == NULL) {
3358 BT_ERR("Failed to get device ID address");
3359 if (error != NULL) {
3360 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3361 g_error_free(error);
3363 g_object_unref(device_proxy);
3364 return BLUETOOTH_ERROR_INTERNAL;
3367 g_variant_get(result , "(s)", &idaddress);
3368 g_variant_unref(result);
3370 if (idaddress == NULL) {
3371 BT_ERR("No paired device");
3372 g_object_unref(device_proxy);
3373 return BLUETOOTH_ERROR_NOT_PAIRED;
3376 BT_DBG("ID Address:%s", idaddress);
3379 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3381 ret = BLUETOOTH_ERROR_INTERNAL;
3383 g_object_unref(device_proxy);
3388 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3390 GapAgentPrivate *agent = _bt_get_adapter_agent();
3391 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3393 if (authentication_reply)
3394 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3396 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3398 BT_DBG("BT_PASSKEY_REPLY");
3399 return BLUETOOTH_ERROR_NONE;
3402 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3404 GapAgentPrivate *agent = _bt_get_adapter_agent();
3405 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3407 if (confirmation_reply)
3408 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3410 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3412 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3413 return BLUETOOTH_ERROR_NONE;