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.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-event.h"
33 #include "bt-service-device.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.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*/
75 GDBusProxy *device_proxy;
76 GDBusProxy *adapter_proxy;
78 unsigned short conn_type;
79 gboolean bonding_wo_discovery;
89 bluetooth_device_address_t bd_addr;
90 gboolean auto_connect;
91 } bt_pending_le_conn_info_s;
98 } bt_connected_le_dev_t;
107 } bt_le_conn_param_t;
109 gboolean is_device_creating;
110 bt_funcion_data_t *bonding_info;
111 bt_funcion_data_t *searching_info;
112 bt_funcion_data_t *att_mtu_req_info;
114 static GSList *le_connected_dev_list = NULL;
115 static GSList *pin_info_list = NULL;
116 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
117 static guint pending_le_conn_timer_id = 0;
119 /* This HID Mouse does not support pairing precedure. need to skip it. */
120 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
122 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
125 static int __bt_retry_bond(void);
128 /*static void __bt_decline_pair_request()
130 GVariant *out_param1;
131 GVariant *out_param2;
132 request_info_t *req_info;
133 bluetooth_device_info_t dev_info;
134 bt_remote_dev_info_t *remote_dev_info;
135 GVariant *uuid_list, *manufacture_data;
140 req_info = _bt_get_request_info(bonding_info->req_id);
141 if (req_info == NULL) {
142 BT_ERR("req_info == NULL");
145 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
147 BT_DBG("bonding_info is NULL");
152 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
153 remote_dev_info->uuids, remote_dev_info->uuid_count,
156 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
157 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
160 param = g_variant_new("isunsbuba{s}na{y})",
161 bonding_info->result,
163 remote_dev_info->class,
164 remote_dev_info->rssi,
165 remote_dev_info->name,
166 remote_dev_info->paired,
167 remote_dev_info->connected,
168 remote_dev_info->trust,
170 remote_dev_info->manufacturer_data_len,
174 //Send the event to application
175 if (remote_dev_info != NULL) {
176 _bt_send_event(BT_ADAPTER_EVENT,
177 BLUETOOTH_EVENT_BONDING_FINISHED,
180 _bt_free_device_info(remote_dev_info);
183 if (req_info->context == NULL)
186 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
187 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
190 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
191 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
193 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
194 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
197 g_dbus_method_invocation_return_value(req_info->context,
198 g_variant_new("@ayi", out_param1, out_param2));
201 _bt_delete_request_list(req_info->req_id);
204 g_free(bonding_info->addr);
205 g_free(bonding_info);
211 #ifdef TIZEN_PROFILE_WEARABLE
212 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
216 retv_if(user_data == NULL, FALSE);
218 b = (bundle *)user_data;
220 ret = syspopup_launch("bt-syspopup", b);
222 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
224 BT_DBG("Hurray!!! Finally Popup launched");
227 return (ret < 0) ? TRUE : FALSE;
230 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
235 GDBusConnection *conn;
237 conn = _bt_gdbus_get_system_gconn();
245 bundle_add(b, "event-type", "unable-to-pairing");
247 if (result == BLUETOOTH_ERROR_TIMEOUT)
248 bundle_add(b, "error", "timeout");
249 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
250 bundle_add(b, "error", "authfailed");
252 bundle_add(b, "error", "error");
254 ret = syspopup_launch("bt-syspopup", b);
256 BT_ERR("Popup launch failed...retry %d \n", ret);
257 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
268 gboolean _bt_is_device_creating(void)
270 return is_device_creating;
273 gboolean _bt_is_bonding_device_address(const char *address)
275 if (bonding_info == NULL || bonding_info->addr == NULL)
278 if (g_strcmp0(bonding_info->addr, address) == 0) {
279 BT_DBG("[%s] is bonding device", address);
283 BT_DBG("[%s] is NOT bonding device", address);
287 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
289 ret_if(bonding_info == NULL);
290 bonding_info->is_autopair = is_autopair;
293 void __bt_cancel_search_service_done(void)
295 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
296 request_info_t *req_info;
297 bluetooth_device_info_t dev_info;
298 GVariant *out_param1;
300 ret_if(searching_info == NULL);
302 req_info = _bt_get_request_info(searching_info->req_id);
303 if (req_info == NULL) {
304 BT_ERR("req_info == NULL");
308 if (req_info->context == NULL)
311 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
312 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
313 searching_info->addr);
315 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
316 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
319 g_dbus_method_invocation_return_value(req_info->context,
320 g_variant_new("(iv)", result, out_param1));
322 _bt_delete_request_list(req_info->req_id);
326 g_free(searching_info->addr);
327 g_free(searching_info);
328 searching_info = NULL;
331 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
333 ret_if(value == NULL);
334 ret_if(info == NULL);
336 info->uuid_count = g_variant_get_size(value);
337 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
340 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
342 char *object_path = NULL;
343 bt_remote_dev_info_t *dev_info;
345 retv_if(address == NULL, NULL);
347 object_path = _bt_get_device_object_path(address);
348 retv_if(object_path == NULL, NULL);
350 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
356 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
357 const char *object_path)
359 bt_remote_dev_info_t *dev_info;
360 GDBusProxy *adapter_proxy;
361 GDBusProxy *device_proxy;
366 GDBusConnection *conn;
367 GError *error = NULL;
368 GVariant *result = NULL;
369 GVariantIter *value_iter;
373 adapter_proxy = _bt_get_adapter_proxy();
374 retv_if(adapter_proxy == NULL, NULL);
376 retv_if(object_path == NULL, NULL);
378 conn = _bt_gdbus_get_system_gconn();
380 BT_ERR("conn == NULL");
384 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
385 NULL, BT_BLUEZ_NAME, object_path,
386 BT_PROPERTIES_INTERFACE, NULL, NULL);
388 retv_if(device_proxy == NULL, NULL);
390 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
391 g_variant_new("(s)", BT_DEVICE_INTERFACE),
392 G_DBUS_CALL_FLAGS_NONE,
397 g_object_unref(device_proxy);
399 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
401 if (result != NULL) {
402 g_variant_get(result , "(@a{sv})", &value);
403 g_variant_unref(result);
405 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
407 g_variant_get(tmp_value, "s", &name);
408 g_variant_unref(tmp_value);
410 DBG_SECURE("Alias Name [%s]", name);
412 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
413 g_variant_get(tmp_value, "s", &name);
414 g_variant_unref(tmp_value);
417 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
419 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
420 g_variant_unref(tmp_value);
422 dev_info->is_alias_set = FALSE;
424 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
426 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
428 dev_info->class = g_variant_get_uint32(tmp_value);
429 g_variant_unref(tmp_value);
433 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
435 dev_info->connected = g_variant_get_byte(tmp_value);
436 g_variant_unref(tmp_value);
438 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
439 BT_DBG("connected link : %d", dev_info->connected);
441 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
443 dev_info->trust = g_variant_get_boolean(tmp_value);
444 g_variant_unref(tmp_value);
446 dev_info->trust = FALSE;
448 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
450 dev_info->paired = g_variant_get_boolean(tmp_value);
451 g_variant_unref(tmp_value);
453 dev_info->paired = FALSE;
455 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
457 dev_info->rssi = g_variant_get_int16(tmp_value);
458 g_variant_unref(tmp_value);
462 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
464 dev_info->addr_type = g_variant_get_byte(tmp_value);
465 g_variant_unref(tmp_value);
467 dev_info->addr_type = 0;
469 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
471 __bt_get_uuids(tmp_value, dev_info);
472 g_variant_unref(tmp_value);
475 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerDataLen", G_VARIANT_TYPE_UINT16);
477 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
478 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
479 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
480 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
482 g_variant_unref(tmp_value);
484 dev_info->manufacturer_data_len = 0;
486 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerData", G_VARIANT_TYPE_ARRAY);
488 if ((dev_info->manufacturer_data_len == 0) ||
489 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
490 BT_ERR("manufacturer data length doesn't match");
491 dev_info->manufacturer_data_len = 0;
492 dev_info->manufacturer_data = NULL;
494 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
495 g_variant_get(tmp_value, "ay", &value_iter);
496 while (g_variant_iter_loop(value_iter, "y", &m_value))
497 dev_info->manufacturer_data[i++] = m_value;
499 g_variant_unref(tmp_value);
501 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
502 dev_info->manufacturer_data_len = 0;
503 dev_info->manufacturer_data = NULL;
506 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
507 g_variant_get(tmp_value, "s", &address);
508 g_variant_unref(tmp_value);
510 dev_info->address = address;
511 dev_info->name = name;
512 g_variant_unref(value);
514 BT_ERR("result is NULL\n");
522 char *_bt_get_bonded_device_name(char *address)
524 bluetooth_device_address_t device_address = { {0} };
525 bluetooth_device_info_t dev_info;
527 retv_if(address == NULL, strdup(""));
529 _bt_convert_addr_string_to_type(device_address.addr, address);
531 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
533 _bt_get_bonded_device_info(&device_address, &dev_info);
535 return g_strdup(dev_info.device_name.name);
538 static gboolean __ignore_auto_pairing_request(const char *address)
543 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
554 /* Get the LAP(Lower Address part) */
555 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
557 snprintf(lap_address, sizeof(lap_address), ",%s", address);
559 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
562 BT_ERR("fopen failed \n");
566 fseek(fp, 0, SEEK_END);
571 BT_ERR("Get file size failed \n");
576 buffer = g_malloc0(sizeof(char) * size);
577 result = fread((char *)buffer, 1, size, fp);
579 if (result != size) {
580 BT_ERR("Read Error\n");
585 BT_DBG("Buffer = %s\n", buffer);
587 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
593 /* Write the data and insert new device data */
594 for (i = 0; lines[i] != NULL; i++) {
595 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
596 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
598 lines[i] = temp_buffer;
601 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
603 /* Fix : NULL_RETURNS */
604 retv_if(buffer == NULL, FALSE);
606 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
609 BT_ERR("fopen failed \n");
614 BT_DBG("Buffer = %s\n", buffer);
615 fwrite(buffer, 1, strlen(buffer), fp);
625 static int __bt_cancel_bonding(void)
627 BT_CHECK_PARAMETER(bonding_info, return);
628 BT_CHECK_PARAMETER(bonding_info->addr, return);
629 /* First Cancel the ongoing pairing in bluez otherwise if we send
630 * pair request bluez will send inprogress and we again retry bond so
631 * this cycle continues */
633 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
634 NULL, G_DBUS_CALL_FLAGS_NONE,
637 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
639 return BLUETOOTH_ERROR_PARING_FAILED;
641 return BLUETOOTH_ERROR_NONE;
644 static int __bt_retry_bond(void)
646 BT_CHECK_PARAMETER(bonding_info, return);
647 BT_CHECK_PARAMETER(bonding_info->addr, return);
649 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
650 g_variant_new("(y)", bonding_info->conn_type),
651 G_DBUS_CALL_FLAGS_NONE,
654 (GAsyncReadyCallback)__bt_bond_device_cb,
657 return BLUETOOTH_ERROR_NONE;
661 static int __bt_remove_and_bond(void)
663 GDBusProxy *adapter_proxy;
664 GVariant *result = NULL;
666 char *device_path = NULL;
668 BT_CHECK_PARAMETER(bonding_info, return);
669 BT_CHECK_PARAMETER(bonding_info->addr, return);
671 adapter_proxy = _bt_get_adapter_proxy();
672 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
674 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
675 g_variant_new("(s)", bonding_info->addr),
676 G_DBUS_CALL_FLAGS_NONE,
681 return BLUETOOTH_ERROR_INTERNAL;
683 g_variant_get(result , "(o)", &device_path);
684 g_variant_unref(result);
686 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
688 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
689 g_variant_new("(o)", device_path),
690 G_DBUS_CALL_FLAGS_NONE,
696 BT_ERR("UnpairDevice Fail: %s", err->message);
698 return BLUETOOTH_ERROR_INTERNAL;
701 return __bt_retry_bond();
704 static int __bt_cancel_and_bond(void)
706 int ret = BLUETOOTH_ERROR_NONE;
708 ret = _bt_agent_reply_cancellation();
709 if (ret != BLUETOOTH_ERROR_NONE) {
710 BT_ERR("Fail to call reply cancellation");
713 return __bt_retry_bond();
717 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
720 int result = BLUETOOTH_ERROR_NONE;
723 GVariant *out_param1;
724 request_info_t *req_info;
725 bluetooth_device_info_t dev_info;
726 bt_remote_dev_info_t *remote_dev_info;
727 GVariant *manufacture_data;
730 /* Terminate ALL system popup */
731 syspopup_destroy_all();
733 reply = g_dbus_proxy_call_finish(proxy, res, &err);
735 g_variant_unref(reply);
737 is_device_creating = FALSE;
739 if (bonding_info == NULL) {
741 BT_ERR("bonding_info == NULL");
747 req_info = _bt_get_request_info(bonding_info->req_id);
748 if (req_info == NULL) {
749 BT_ERR("req_info == NULL");
754 g_dbus_error_strip_remote_error(err);
755 BT_ERR("Error occured in CreateBonding [%s]", err->message);
757 if (g_strrstr(err->message, "Already Exists")) {
758 BT_INFO("Existing Bond, remove and retry");
759 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
761 result = BLUETOOTH_ERROR_PARING_FAILED;
762 } else if (_bt_agent_is_canceled() ||
763 g_strrstr(err->message, "Authentication Canceled")) {
764 BT_INFO("Cancelled by USER");
765 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
766 } else if (g_strrstr(err->message, "Authentication Rejected")) {
768 result = BLUETOOTH_ERROR_ACCESS_DENIED;
769 } else if (g_strrstr(err->message, "In Progress")) {
770 BT_INFO("Bond in progress, cancel and retry");
771 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
773 result = BLUETOOTH_ERROR_PARING_FAILED;
774 } else if (g_strrstr(err->message, "Authentication Failed")) {
775 BT_INFO("Authentication Failed");
776 if (bonding_info->is_autopair == TRUE) {
777 _bt_set_autopair_status_in_bonding_info(FALSE);
778 __ignore_auto_pairing_request(bonding_info->addr);
780 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
781 } else if (g_strrstr(err->message, "Page Timeout")) {
782 BT_INFO("Page Timeout");
783 /* This is the special case
784 As soon as call bluetooth_bond_device, try to cancel bonding.
785 In this case, before completing to call 'CreatePairedDevice' method
786 the procedure is stopped. So 'Cancle' error is not return.
788 result = BLUETOOTH_ERROR_HOST_DOWN;
789 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
790 BT_INFO("Cancel already running bonding");
791 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
792 BT_INFO("Error while Cancelling bonding");
793 /* we need to unref proxy so continue */
795 result = BLUETOOTH_ERROR_INTERNAL;
796 } else if (g_strrstr(err->message, "Connection Timeout")) {
797 /* Pairing request timeout */
798 result = BLUETOOTH_ERROR_TIMEOUT;
799 } else if (g_strrstr(err->message, "Authentication Timeout")) {
800 /* Pairing request timeout */
801 result = BLUETOOTH_ERROR_TIMEOUT;
803 BT_DBG("Default case");
804 result = BLUETOOTH_ERROR_PARING_FAILED;
808 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
809 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
810 result == BLUETOOTH_ERROR_TIMEOUT ||
811 result == BLUETOOTH_ERROR_HOST_DOWN) {
812 #ifdef TIZEN_PROFILE_WEARABLE
813 int is_sw_running = 0;
815 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
816 BT_ERR("vconf_get_int for setup wizard state failed");
819 __bt_launch_unable_to_pairing_syspopup(result);
821 BT_ERR("Unable to pair");
823 bonding_info->result = result;
826 g_object_unref(proxy);
827 bonding_info->device_proxy = NULL;
829 if (result != BLUETOOTH_ERROR_NONE) {
830 if (bonding_info->bonding_wo_discovery) {
831 GDBusProxy *adapter_proxy;
832 GVariant *ret = NULL;
833 GError *error = NULL;
836 BT_ERR("Bond was tried without discovery. Remove it");
838 adapter_proxy = _bt_get_adapter_proxy();
839 if (adapter_proxy == NULL) {
840 BT_ERR("Cannot get adapter_proxy");
844 device_path = _bt_get_device_object_path(bonding_info->addr);
845 if (device_path == NULL) {
846 BT_ERR("Cannot get device path");
850 ret = g_dbus_proxy_call_sync(adapter_proxy,
852 g_variant_new("(o)", device_path),
853 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
856 BT_ERR("RemoveDevice Fail: %s", error->message);
857 g_clear_error(&error);
861 g_variant_unref(ret);
869 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
870 if (!remote_dev_info)
873 GVariant *uuids = NULL;
874 GVariantBuilder *builder = NULL;
876 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
877 for (i = 0; i < remote_dev_info->uuid_count; i++) {
878 g_variant_builder_add(builder, "s",
879 remote_dev_info->uuids[i]);
881 uuids = g_variant_new("as", builder);
882 g_variant_builder_unref(builder);
883 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
884 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
887 param = g_variant_new("(isunsbub@asn@ay)",
890 remote_dev_info->class,
891 remote_dev_info->rssi,
892 remote_dev_info->name,
893 remote_dev_info->paired,
894 remote_dev_info->connected,
895 remote_dev_info->trust,
897 remote_dev_info->manufacturer_data_len,
901 /* Send the event to application */
902 _bt_send_event(BT_ADAPTER_EVENT,
903 BLUETOOTH_EVENT_BONDING_FINISHED,
906 _bt_free_device_info(remote_dev_info);
909 if (req_info->context == NULL)
912 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
913 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
916 if (_bt_adapter_get_status() != BT_ACTIVATED)
917 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
919 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
920 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
922 g_dbus_method_invocation_return_value(req_info->context,
923 g_variant_new("(iv)", result, out_param1));
925 g_variant_unref(out_param1);
927 _bt_delete_request_list(req_info->req_id);
932 _bt_agent_set_canceled(FALSE);
935 g_free(bonding_info->addr);
936 g_free(bonding_info);
940 int _bt_bond_device(int request_id,
941 bluetooth_device_address_t *device_address,
942 unsigned short conn_type, GArray **out_param1)
945 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
946 bluetooth_device_info_t dev_info;
948 GDBusConnection *conn;
949 char *device_path = NULL;
950 GDBusProxy *adapter_proxy;
951 GError *error = NULL;
952 gboolean bonding_wo_discovery = FALSE;
954 BT_CHECK_PARAMETER(device_address, return);
957 BT_ERR("Bonding in progress");
959 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
960 memcpy(dev_info.device_address.addr, device_address->addr,
961 BLUETOOTH_ADDRESS_LENGTH);
963 g_array_append_vals(*out_param1, &dev_info,
964 sizeof(bluetooth_device_info_t));
966 return BLUETOOTH_ERROR_DEVICE_BUSY;
969 conn = _bt_gdbus_get_system_gconn();
970 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
972 _bt_convert_addr_type_to_string(address, device_address->addr);
974 device_path = _bt_get_device_object_path(address);
976 if (device_path == NULL) {
977 BT_ERR("No searched device");
978 GVariant *ret = NULL;
979 adapter_proxy = _bt_get_adapter_proxy();
980 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
982 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
983 g_variant_new("(s)", address),
984 G_DBUS_CALL_FLAGS_NONE,
990 BT_ERR("CreateDevice Fail: %s", error->message);
991 g_clear_error(&error);
994 g_variant_unref(ret);
995 device_path = _bt_get_device_object_path(address);
996 if (device_path == NULL) {
997 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
998 memcpy(dev_info.device_address.addr, device_address->addr,
999 BLUETOOTH_ADDRESS_LENGTH);
1001 g_array_append_vals(*out_param1, &dev_info,
1002 sizeof(bluetooth_device_info_t));
1004 return BLUETOOTH_ERROR_NOT_PAIRED;
1006 BT_INFO("device_path is created[%s]", device_path);
1008 bonding_wo_discovery = TRUE;
1011 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1012 NULL, BT_BLUEZ_NAME,
1013 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1015 g_free(device_path);
1016 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1018 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1019 bonding_info->addr = g_strdup(address);
1020 bonding_info->req_id = request_id;
1022 bonding_info->device_proxy = proxy;
1023 bonding_info->conn_type = conn_type;
1024 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1026 is_device_creating = TRUE;
1028 g_dbus_proxy_call(proxy, "Pair",
1029 g_variant_new("(y)", conn_type),
1030 G_DBUS_CALL_FLAGS_NONE,
1031 BT_MAX_DBUS_TIMEOUT,
1033 (GAsyncReadyCallback)__bt_bond_device_cb,
1036 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1038 return BLUETOOTH_ERROR_NONE;
1040 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1041 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1042 bonding_info->addr);
1044 g_array_append_vals(*out_param1, &dev_info,
1045 sizeof(bluetooth_device_info_t));
1047 is_device_creating = FALSE;
1049 g_free(bonding_info->addr);
1050 g_free(bonding_info);
1051 bonding_info = NULL;
1053 return BLUETOOTH_ERROR_INTERNAL;*/
1056 int _bt_cancel_bonding(void)
1058 int ret = BLUETOOTH_ERROR_NONE;
1060 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1062 ret = _bt_agent_reply_cancellation();
1063 if (ret != BLUETOOTH_ERROR_NONE) {
1064 BT_ERR("Fail to call reply cancellation");
1068 _bt_agent_set_canceled(TRUE);
1070 return BLUETOOTH_ERROR_NONE;
1073 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1078 GVariant *out_param1;
1079 int result = BLUETOOTH_ERROR_NONE;
1080 bt_funcion_data_t *unbonding_info;
1081 bluetooth_device_info_t dev_info;
1082 request_info_t *req_info;
1084 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1086 g_variant_unref(reply);
1088 unbonding_info = user_data;
1090 if (unbonding_info == NULL) {
1092 BT_ERR("unbonding_info == NULL");
1096 req_info = _bt_get_request_info(unbonding_info->req_id);
1097 if (req_info == NULL) {
1098 BT_ERR("req_info == NULL");
1103 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1104 result = BLUETOOTH_ERROR_INTERNAL;
1107 if (req_info->context == NULL)
1110 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1111 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1112 unbonding_info->addr);
1114 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1115 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1117 g_dbus_method_invocation_return_value(req_info->context,
1118 g_variant_new("(iv)", result, out_param1));
1121 _bt_delete_request_list(req_info->req_id);
1127 if (unbonding_info) {
1128 g_free(unbonding_info->addr);
1129 g_free(unbonding_info);
1133 int _bt_unbond_device(int request_id,
1134 bluetooth_device_address_t *device_address,
1135 GArray **out_param1)
1137 char *device_path = NULL;
1138 bt_funcion_data_t *unbonding_info;
1139 GDBusProxy *adapter_proxy = NULL;
1140 GDBusProxy *device_proxy = NULL;
1141 GDBusConnection *conn;
1142 int result = BLUETOOTH_ERROR_INTERNAL;
1143 bluetooth_device_info_t dev_info;
1144 GError *error = NULL;
1145 GVariant *ret = NULL;
1147 BT_CHECK_PARAMETER(device_address, return);
1149 adapter_proxy = _bt_get_adapter_proxy();
1150 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1152 /* allocate user data so that it can be retrieved in callback */
1153 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1154 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1155 unbonding_info->req_id = request_id;
1157 _bt_convert_addr_type_to_string(unbonding_info->addr,
1158 device_address->addr);
1160 device_path = _bt_get_device_object_path(unbonding_info->addr);
1162 if (device_path == NULL) {
1163 BT_ERR("No paired device");
1164 result = BLUETOOTH_ERROR_NOT_PAIRED;
1168 conn = _bt_gdbus_get_system_gconn();
1170 BT_ERR("conn is NULL");
1171 result = BLUETOOTH_ERROR_INTERNAL;
1175 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1176 NULL, BT_BLUEZ_NAME,
1177 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1179 if (device_proxy != NULL) {
1181 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1182 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1183 G_DBUS_CALL_FLAGS_NONE,
1188 BT_ERR("Getting property failed: [%s]\n", error->message);
1189 g_error_free(error);
1190 result = BLUETOOTH_ERROR_NOT_PAIRED;
1194 BT_ERR("No paired device");
1195 g_object_unref(device_proxy);
1196 result = BLUETOOTH_ERROR_NOT_PAIRED;
1199 g_variant_unref(ret);
1201 g_object_unref(device_proxy);
1204 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1205 g_variant_new("(o)", device_path),
1206 G_DBUS_CALL_FLAGS_NONE,
1207 BT_MAX_DBUS_TIMEOUT,
1209 (GAsyncReadyCallback)__bt_unbond_cb,
1212 g_free(device_path);
1213 return BLUETOOTH_ERROR_NONE;
1216 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1217 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1218 unbonding_info->addr);
1220 g_array_append_vals(*out_param1, &dev_info,
1221 sizeof(bluetooth_device_info_t));
1222 g_free(device_path);
1223 g_free(unbonding_info->addr);
1224 g_free(unbonding_info);
1228 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1233 GVariant *out_param1;
1234 int result = BLUETOOTH_ERROR_NONE;
1235 bluetooth_device_info_t dev_info;
1236 bt_remote_dev_info_t *remote_dev_info;
1237 request_info_t *req_info;
1238 GVariant *uuid_list, *manufacture_data;
1240 GVariantBuilder *builder = NULL;
1243 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1245 g_variant_unref(reply);
1247 g_object_unref(proxy);
1249 if (searching_info == NULL) {
1251 BT_ERR("unbonding_info == NULL");
1255 req_info = _bt_get_request_info(searching_info->req_id);
1256 if (req_info == NULL) {
1257 BT_ERR("req_info == NULL");
1262 g_dbus_error_strip_remote_error(err);
1263 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1265 if (g_strrstr("Operation canceled", err->message))
1266 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1267 else if (g_strrstr("In Progress", err->message))
1268 result = BLUETOOTH_ERROR_IN_PROGRESS;
1269 else if (g_strrstr("Host is down", err->message))
1270 result = BLUETOOTH_ERROR_HOST_DOWN;
1272 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1274 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1275 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1276 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1277 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1278 remote_dev_info->uuid_count > 0) {
1279 result = BLUETOOTH_ERROR_NONE;
1282 _bt_free_device_info(remote_dev_info);
1287 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1288 if (!remote_dev_info)
1292 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1293 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1294 g_variant_builder_add(builder, "s",
1295 remote_dev_info->uuids[i]);
1297 uuid_list = g_variant_new("as", builder);
1298 g_variant_builder_unref(builder);
1299 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1300 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1303 param = g_variant_new("(isunsbub@asn@ay)",
1305 searching_info->addr,
1306 remote_dev_info->class,
1307 remote_dev_info->rssi,
1308 remote_dev_info->name,
1309 remote_dev_info->paired,
1310 remote_dev_info->connected,
1311 remote_dev_info->trust,
1313 remote_dev_info->manufacturer_data_len,
1316 /* Send the event to application */
1317 _bt_send_event(BT_ADAPTER_EVENT,
1318 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1321 _bt_free_device_info(remote_dev_info);
1324 if (req_info->context == NULL)
1327 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1328 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1329 searching_info->addr);
1331 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1332 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1334 g_dbus_method_invocation_return_value(req_info->context,
1335 g_variant_new("(iv)", result, out_param1));
1338 _bt_delete_request_list(req_info->req_id);
1343 if (searching_info) {
1344 g_free(searching_info->addr);
1345 g_free(searching_info);
1346 searching_info = NULL;
1350 int _bt_search_device(int request_id,
1351 bluetooth_device_address_t *device_address)
1353 char *device_path = NULL;
1354 GDBusProxy *device_proxy = NULL;
1355 GDBusConnection *conn;
1357 GDBusProxy *adapter_proxy;
1358 int result = BLUETOOTH_ERROR_INTERNAL;
1360 BT_CHECK_PARAMETER(device_address, return);
1363 BT_ERR("Bonding in progress");
1364 return BLUETOOTH_ERROR_DEVICE_BUSY;
1367 if (searching_info) {
1368 BT_ERR("Service searching in progress");
1369 return BLUETOOTH_ERROR_DEVICE_BUSY;
1372 adapter_proxy = _bt_get_adapter_proxy();
1373 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1375 /* allocate user data so that it can be retrieved in callback */
1376 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1377 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1378 searching_info->req_id = request_id;
1380 _bt_convert_addr_type_to_string(searching_info->addr,
1381 device_address->addr);
1383 conn = _bt_gdbus_get_system_gconn();
1384 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1387 device_path = _bt_get_device_object_path(searching_info->addr);
1389 if (device_path == NULL) {
1390 BT_ERR("No paired device");
1391 result = BLUETOOTH_ERROR_NOT_PAIRED;
1395 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1396 NULL, BT_BLUEZ_NAME,
1397 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1398 g_free(device_path);
1399 if (device_proxy == NULL) {
1400 result = BLUETOOTH_ERROR_INTERNAL;
1404 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1405 g_variant_new("(s)", ""),
1406 G_DBUS_CALL_FLAGS_NONE,
1407 BT_MAX_DBUS_TIMEOUT,
1409 (GAsyncReadyCallback)__bt_discover_cb,
1412 searching_info->device_proxy = device_proxy;
1414 return BLUETOOTH_ERROR_NONE;
1417 g_free(searching_info->addr);
1418 g_free(searching_info);
1419 searching_info = NULL;
1423 int _bt_cancel_search_device(void)
1425 GVariant *ret = NULL;
1428 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1430 if (searching_info->device_proxy) {
1431 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1433 G_DBUS_CALL_FLAGS_NONE,
1438 g_variant_unref(ret);
1440 __bt_cancel_search_service_done();
1442 return BLUETOOTH_ERROR_NONE;
1445 int _bt_set_alias(bluetooth_device_address_t *device_address,
1448 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1449 gchar *device_path = NULL;
1450 GDBusProxy *adapter_proxy;
1451 GDBusProxy *device_proxy;
1452 GVariant *ret = NULL;
1453 GError *error = NULL;
1454 GDBusConnection *conn;
1456 BT_CHECK_PARAMETER(device_address, return);
1457 BT_CHECK_PARAMETER(alias, return);
1459 adapter_proxy = _bt_get_adapter_proxy();
1460 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1462 conn = _bt_gdbus_get_system_gconn();
1463 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1465 _bt_convert_addr_type_to_string(address, device_address->addr);
1467 device_path = _bt_get_device_object_path(address);
1469 if (device_path == NULL) {
1470 BT_ERR("No paired device");
1471 return BLUETOOTH_ERROR_NOT_PAIRED;
1474 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1475 NULL, BT_BLUEZ_NAME,
1476 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1478 g_free(device_path);
1479 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1481 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1482 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1483 G_DBUS_CALL_FLAGS_NONE,
1488 g_variant_unref(ret);
1490 g_object_unref(device_proxy);
1493 BT_ERR("SetProperty error: [%s]", error->message);
1494 g_error_free(error);
1495 return BLUETOOTH_ERROR_INTERNAL;
1498 return BLUETOOTH_ERROR_NONE;
1501 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1504 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1505 gchar *device_path = NULL;
1506 GDBusProxy *device_proxy;
1507 gboolean previous_value;
1508 GError *error = NULL;
1509 GDBusConnection *conn;
1510 GVariant *result = NULL;
1511 GVariant *temp = NULL;
1512 int ret = BLUETOOTH_ERROR_NONE;
1514 BT_CHECK_PARAMETER(device_address, return);
1516 conn = _bt_gdbus_get_system_gconn();
1517 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1519 _bt_convert_addr_type_to_string(address, device_address->addr);
1521 device_path = _bt_get_device_object_path(address);
1523 if (device_path == NULL) {
1524 BT_ERR("No paired device");
1525 return BLUETOOTH_ERROR_NOT_PAIRED;
1528 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1529 NULL, BT_BLUEZ_NAME,
1530 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1532 g_free(device_path);
1533 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1535 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1536 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1537 G_DBUS_CALL_FLAGS_NONE,
1541 if (error != NULL) {
1542 BT_ERR("Getting property failed: [%s]\n", error->message);
1543 g_error_free(error);
1544 g_object_unref(device_proxy);
1545 return BLUETOOTH_ERROR_INTERNAL;
1548 g_variant_get(result, "(v)", &temp);
1549 previous_value = g_variant_get_boolean(temp);
1550 g_variant_unref(temp);
1551 g_variant_unref(result);
1552 /* If the input is same with previous value, return error. */
1553 if (previous_value == authorize) {
1554 BT_ERR("Same value: %d", previous_value);
1555 g_object_unref(device_proxy);
1556 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1560 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1561 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1562 G_DBUS_CALL_FLAGS_NONE,
1567 g_object_unref(device_proxy);
1569 BT_ERR("SetProperty error: [%s]", error->message);
1570 g_error_free(error);
1571 ret = BLUETOOTH_ERROR_INTERNAL;
1575 g_variant_unref(result);
1580 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1581 gboolean *is_connected)
1583 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1584 char *object_path = NULL;
1586 GDBusProxy *device_proxy;
1587 GError *error = NULL;
1589 GDBusConnection *conn;
1590 GVariant *result = NULL;
1591 int ret = BLUETOOTH_ERROR_NONE;
1593 BT_CHECK_PARAMETER(device_address, return);
1595 conn = _bt_gdbus_get_system_gconn();
1596 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1598 _bt_convert_addr_type_to_string(address, device_address->addr);
1600 object_path = _bt_get_device_object_path(address);
1601 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1603 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1604 NULL, BT_BLUEZ_NAME, object_path,
1605 BT_PROPERTIES_INTERFACE, NULL, NULL);
1606 g_free(object_path);
1607 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1609 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1610 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1612 G_DBUS_CALL_FLAGS_NONE,
1616 if (result == NULL) {
1617 if (error != NULL) {
1618 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1619 g_error_free(error);
1621 g_object_unref(device_proxy);
1622 return BLUETOOTH_ERROR_INTERNAL;
1625 g_variant_get(result, "(v)", &value);
1626 *is_connected = g_variant_get_boolean(value);
1627 g_variant_unref(result);
1629 BT_DBG("gatt is connected : %d", *is_connected);
1631 g_variant_unref(value);
1632 g_object_unref(device_proxy);
1637 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1638 int connection_type, gboolean *is_connected)
1640 char *object_path = NULL;
1641 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1642 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1643 GDBusProxy *device_proxy = NULL;
1644 GDBusProxy *adapter_proxy = NULL;
1645 GDBusConnection *conn;
1646 GError *error = NULL;
1647 GVariant *tmp_value = NULL;
1648 GVariant *value = NULL;
1649 GVariant *result = NULL;
1652 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1653 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1655 _bt_convert_addr_type_to_string(address, device_address->addr);
1656 _bt_convert_addr_string_to_secure_string(secure_address, address);
1658 *is_connected = FALSE;
1659 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1661 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1662 return _bt_rfcomm_is_device_connected(device_address,
1664 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1665 return _bt_is_gatt_connected(device_address, is_connected);
1666 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1667 return _bt_pbap_is_connected(device_address, is_connected);
1669 adapter_proxy = _bt_get_adapter_proxy();
1670 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1672 conn = _bt_gdbus_get_system_gconn();
1673 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1675 _bt_convert_addr_type_to_string(address, device_address->addr);
1677 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1678 object_path = _bt_get_adapter_path();
1679 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1680 NULL, BT_BLUEZ_NAME,
1681 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1682 g_free(object_path);
1683 if (device_proxy == NULL) {
1684 BT_DBG("Device don't have this service");
1685 return BLUETOOTH_ERROR_INTERNAL;
1688 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1689 g_variant_new("(s)", address),
1690 G_DBUS_CALL_FLAGS_NONE,
1695 if (result == NULL) {
1696 BT_ERR("[GetProperties] Error occured in Proxy call");
1698 BT_ERR("%s", error->message);
1699 g_error_free(error);
1701 *is_connected = FALSE;
1702 g_object_unref(device_proxy);
1703 return BLUETOOTH_ERROR_NONE;
1705 g_variant_get(result , "(@a{sv})", &value);
1706 g_variant_unref(result);
1709 tmp_value = g_variant_lookup_value(value,
1711 G_VARIANT_TYPE_BOOLEAN);
1713 *is_connected = g_variant_get_boolean(tmp_value);
1714 g_variant_unref(tmp_value);
1716 g_variant_unref(value);
1718 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1719 return _bt_is_network_connected(_bt_get_net_conn(),
1720 device_address->addr, is_connected);
1722 uuid = _bt_get_profile_uuid128(connection_type);
1724 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1725 return BLUETOOTH_ERROR_INTERNAL;
1727 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1729 object_path = _bt_get_device_object_path(address);
1730 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1731 BT_DBG("object_path: %s", object_path);
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)
1948 BT_INFO("Try to initiate pending LE connection");
1950 pending_le_conn_timer_id = 0;
1952 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1953 &pending_le_conn_info->bd_addr,
1954 pending_le_conn_info->auto_connect);
1956 g_free(pending_le_conn_info);
1957 pending_le_conn_info = NULL;
1962 void _bt_pending_connect_le_device(void)
1964 if (pending_le_conn_timer_id > 0) {
1965 g_source_remove(pending_le_conn_timer_id);
1966 __bt_connect_le_timer_cb(NULL);
1970 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1971 gboolean auto_connect)
1973 int ret = BLUETOOTH_ERROR_NONE;
1975 BT_CHECK_PARAMETER(bd_addr, return);
1977 ret = _bt_hold_current_advertising();
1978 if (ret == BLUETOOTH_ERROR_NONE) {
1979 BT_INFO("Current advertising is held");
1980 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1981 pending_le_conn_info->req_id = req_id;
1982 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1983 BLUETOOTH_ADDRESS_LENGTH);
1984 pending_le_conn_info->auto_connect = auto_connect;
1986 pending_le_conn_timer_id =
1987 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1989 return BLUETOOTH_ERROR_NONE;
1992 BT_ERR("Unable to hold advertising");
1994 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1997 int _bt_disconnect_le_device(int req_id,
1998 const bluetooth_device_address_t *bd_addr)
2000 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2001 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2003 GDBusProxy *device_proxy;
2004 GDBusConnection *conn;
2005 int ret = BLUETOOTH_ERROR_NONE;
2006 bt_function_data_t *func_data;
2008 BT_CHECK_PARAMETER(bd_addr, return);
2010 conn = _bt_gdbus_get_system_gconn();
2011 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2013 _bt_convert_addr_type_to_string(device_address,
2014 (unsigned char *)bd_addr->addr);
2015 device_path = _bt_get_device_object_path(device_address);
2016 if (device_path == NULL) {
2017 BT_DBG("device_path NULL");
2018 ret = BLUETOOTH_ERROR_INTERNAL;
2022 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2024 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2025 NULL, BT_BLUEZ_NAME,
2026 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2027 g_free(device_path);
2028 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2030 func_data = g_malloc0(sizeof(bt_function_data_t));
2031 func_data->address = g_strdup(device_address);
2032 if (func_data->address == NULL) {
2033 BT_ERR("Unable to allocate memory for address");
2034 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2038 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2039 BT_INFO("Disconnect LE [%s]", secure_address);
2041 func_data->req_id = req_id;
2043 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2045 G_DBUS_CALL_FLAGS_NONE,
2046 BT_MAX_DBUS_TIMEOUT,
2048 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2053 g_object_unref(device_proxy);
2055 g_free(func_data->address);
2061 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2063 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2064 gchar *device_path = NULL;
2065 GError *error = NULL;
2066 GDBusProxy *device_proxy = NULL;
2067 GDBusProxy *adapter_proxy;
2068 GDBusConnection *conn;
2069 int ret = BLUETOOTH_ERROR_NONE;
2071 BT_CHECK_PARAMETER(bd_addr, return);
2073 _bt_convert_addr_type_to_string(device_address,
2074 (unsigned char *)bd_addr->addr);
2076 conn = _bt_gdbus_get_system_gconn();
2077 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2079 adapter_proxy = _bt_get_adapter_proxy();
2080 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2082 device_path = _bt_get_device_object_path(device_address);
2083 if (device_path == NULL) {
2084 BT_DBG("device_path NULL");
2085 ret = BLUETOOTH_ERROR_INTERNAL;
2089 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2091 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2092 NULL, BT_BLUEZ_NAME,
2093 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2094 g_free(device_path);
2095 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2097 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2099 G_DBUS_CALL_FLAGS_NONE,
2104 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2105 g_error_free(error);
2106 g_object_unref(device_proxy);
2107 return BLUETOOTH_ERROR_INTERNAL;
2110 g_object_unref(device_proxy);
2115 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2117 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2118 gchar *device_path = NULL;
2119 GError *error = NULL;
2120 GDBusProxy *device_proxy = NULL;
2121 GDBusProxy *adapter_proxy;
2122 GDBusConnection *conn;
2123 int ret = BLUETOOTH_ERROR_NONE;
2125 BT_CHECK_PARAMETER(bd_addr, return);
2127 _bt_convert_addr_type_to_string(device_address,
2128 (unsigned char *)bd_addr->addr);
2130 conn = _bt_gdbus_get_system_gconn();
2131 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2133 adapter_proxy = _bt_get_adapter_proxy();
2134 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2136 device_path = _bt_get_device_object_path(device_address);
2137 if (device_path == NULL) {
2138 BT_DBG("device_path NULL");
2139 ret = BLUETOOTH_ERROR_INTERNAL;
2143 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2145 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2146 NULL, BT_BLUEZ_NAME,
2147 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2148 g_free(device_path);
2149 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2151 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2153 G_DBUS_CALL_FLAGS_NONE,
2158 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2159 g_error_free(error);
2160 g_object_unref(device_proxy);
2161 return BLUETOOTH_ERROR_INTERNAL;
2164 g_object_unref(device_proxy);
2169 int _bt_connect_profile(char *address, char *uuid,
2170 void *cb, gpointer func_data)
2174 GDBusConnection *conn;
2175 GDBusProxy *adapter_proxy;
2176 GVariant *result = NULL;
2177 GError *error = NULL;
2179 conn = _bt_gdbus_get_system_gconn();
2180 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2182 object_path = _bt_get_device_object_path(address);
2183 if (object_path == NULL) {
2184 BT_ERR("No searched device");
2186 adapter_proxy = _bt_get_adapter_proxy();
2187 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2189 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2190 g_variant_new("(s)", address),
2191 G_DBUS_CALL_FLAGS_NONE,
2196 if (error != NULL) {
2197 BT_ERR("CreateDevice Fail: %s", error->message);
2198 g_error_free(error);
2201 g_variant_unref(result);
2203 object_path = _bt_get_device_object_path(address);
2205 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2207 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2208 NULL, BT_BLUEZ_NAME,
2209 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2210 g_free(object_path);
2211 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2213 g_dbus_proxy_call(proxy, "ConnectProfile",
2214 g_variant_new("(s)", uuid),
2215 G_DBUS_CALL_FLAGS_NONE,
2216 BT_MAX_DBUS_TIMEOUT,
2218 (GAsyncReadyCallback)cb,
2221 return BLUETOOTH_ERROR_NONE;
2224 int _bt_disconnect_profile(char *address, char *uuid,
2225 void *cb, gpointer func_data)
2229 GDBusConnection *conn;
2231 conn = _bt_gdbus_get_system_gconn();
2232 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2234 object_path = _bt_get_device_object_path(address);
2235 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2237 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2238 NULL, BT_BLUEZ_NAME,
2239 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2240 g_free(object_path);
2241 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2243 g_dbus_proxy_call(proxy, "DisconnectProfile",
2244 g_variant_new("(s)", uuid),
2245 G_DBUS_CALL_FLAGS_NONE,
2246 BT_MAX_DBUS_TIMEOUT,
2248 (GAsyncReadyCallback)cb,
2251 return BLUETOOTH_ERROR_NONE;
2254 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2255 int low_threshold, int in_range_threshold, int high_threshold)
2257 int ret = BLUETOOTH_ERROR_NONE;
2259 GVariant *result = NULL;
2260 GError *error = NULL;
2261 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2263 BT_CHECK_PARAMETER(bd_addr, return);
2264 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2265 bd_addr->addr[0], bd_addr->addr[1],
2266 bd_addr->addr[2], bd_addr->addr[3],
2267 bd_addr->addr[4], bd_addr->addr[5],
2269 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2270 in_range_threshold, high_threshold);
2272 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2274 proxy = _bt_get_adapter_proxy();
2275 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2277 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2278 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2279 G_DBUS_CALL_FLAGS_NONE,
2283 if (error != NULL) {
2284 BT_ERR("Dbus Call Error:[%s]", error->message);
2285 g_error_free(error);
2286 ret = BLUETOOTH_ERROR_INTERNAL;
2290 g_variant_unref(result);
2295 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2298 int ret = BLUETOOTH_ERROR_NONE;
2300 GVariant *result = NULL;
2301 GError *error = NULL;
2302 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2304 BT_CHECK_PARAMETER(bd_addr, return);
2305 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2306 bd_addr->addr[0], bd_addr->addr[1],
2307 bd_addr->addr[2], bd_addr->addr[3],
2308 bd_addr->addr[4], bd_addr->addr[5],
2311 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2313 proxy = _bt_get_adapter_proxy();
2314 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2316 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2317 g_variant_new("(si)", address, link_type),
2318 G_DBUS_CALL_FLAGS_NONE,
2323 if (error != NULL) {
2324 BT_ERR("Dbus Call Error:[%s]", error->message);
2325 g_error_free(error);
2326 ret = BLUETOOTH_ERROR_INTERNAL;
2330 g_variant_unref(result);
2335 static int __bt_le_set_conn_parameter(const char *address,
2336 float interval_min, float interval_max,
2337 guint16 latency, guint16 time_out)
2339 gchar *device_path = NULL;
2340 GError *error = NULL;
2341 GDBusProxy *device_proxy = NULL;
2342 GDBusConnection *conn;
2344 guint32 min, max, to;
2345 int ret = BLUETOOTH_ERROR_NONE;
2347 BT_CHECK_PARAMETER(address, return);
2349 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2350 interval_min, interval_max, latency, time_out);
2352 device_path = _bt_get_device_object_path((char *)address);
2354 if (device_path == NULL) {
2355 BT_ERR("device_path NULL");
2356 ret = BLUETOOTH_ERROR_INTERNAL;
2360 conn = _bt_gdbus_get_system_gconn();
2362 BT_ERR("conn NULL");
2363 ret = BLUETOOTH_ERROR_INTERNAL;
2367 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2368 NULL, BT_BLUEZ_NAME,
2369 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2371 g_free(device_path);
2372 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2374 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2375 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2376 to = time_out / BT_LE_CONN_TO_SPLIT;
2378 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2379 g_variant_new("(uuuu)", min, max, latency, to),
2380 G_DBUS_CALL_FLAGS_NONE,
2385 g_object_unref(device_proxy);
2386 if (reply == NULL) {
2388 BT_ERR("Error %s[%s]", error->message, address);
2389 if (g_strrstr(error->message, "In Progress"))
2390 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2392 ret = BLUETOOTH_ERROR_INTERNAL;
2393 g_error_free(error);
2397 g_variant_unref(reply);
2403 static void __bt_le_conn_param_free(void *data)
2405 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2407 BT_DBG("%s", param->sender);
2408 g_free(param->sender);
2412 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2414 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2415 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2417 return parama->key > paramb->key;
2420 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2423 bt_connected_le_dev_t *dev;
2428 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2431 if (g_strcmp0(dev->address, address) == 0)
2437 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2440 bt_le_conn_param_t *param = NULL;
2442 if (!dev || !sender)
2445 for (l = dev->senders; l; l = g_slist_next(l)) {
2447 if (g_strcmp0(param->sender, sender) == 0)
2454 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2455 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2457 bt_connected_le_dev_t *dev = NULL;
2458 bt_le_conn_param_t *param = NULL;
2459 bt_le_conn_param_t *data = NULL;
2461 if (!address || !sender)
2462 return BLUETOOTH_ERROR_INVALID_PARAM;
2464 dev = __bt_get_le_connected_dev_info(address);
2466 return BLUETOOTH_ERROR_INTERNAL;
2468 param = __bt_get_le_conn_param_info(dev, sender);
2470 data = g_malloc0(sizeof(bt_le_conn_param_t));
2471 data->sender = g_strdup(sender);
2472 data->interval_min = interval_min;
2473 data->interval_max = interval_max;
2474 data->latency = latency;
2475 data->time_out = time_out;
2476 data->key = interval_min + (interval_max - interval_min)/2;
2478 if (param == NULL) {
2479 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2480 dev->senders = g_slist_append(dev->senders, data);
2482 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2483 dev->senders = g_slist_remove(dev->senders, param);
2484 g_free(param->sender);
2486 dev->senders = g_slist_append(dev->senders, data);
2489 /* Sorting. First element have the minimum interval */
2490 dev->senders = g_slist_sort(dev->senders,
2491 (GCompareFunc)__bt_compare_le_conn_param_key);
2493 return BLUETOOTH_ERROR_NONE;
2496 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2498 bt_connected_le_dev_t *dev = NULL;
2499 bt_le_conn_param_t *param = NULL;
2501 if (!address || !sender)
2502 return BLUETOOTH_ERROR_INVALID_PARAM;
2504 dev = __bt_get_le_connected_dev_info(address);
2506 return BLUETOOTH_ERROR_INTERNAL;
2508 param = __bt_get_le_conn_param_info(dev, sender);
2510 BT_DBG("Remove param %s %s ", address, sender);
2511 dev->senders = g_slist_remove(dev->senders, param);
2512 g_free(param->sender);
2516 *is_removed = FALSE;
2518 return BLUETOOTH_ERROR_NONE;
2521 int _bt_remove_all_le_conn_param_info(const char *sender)
2524 bt_connected_le_dev_t *dev = NULL;
2525 bt_le_conn_param_t *param = NULL;
2526 gboolean is_removed = FALSE;
2527 char *sender_new = NULL;
2528 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2529 int ret = BLUETOOTH_ERROR_NONE;
2532 return BLUETOOTH_ERROR_INVALID_PARAM;
2534 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2536 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2539 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2540 g_slist_length(dev->senders));
2541 if (g_slist_length(dev->senders) > 0) {
2542 param = dev->senders->data;
2543 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2544 param->interval_min, param->interval_max);
2546 if (dev->interval_min != param->interval_min ||
2547 dev->interval_max != param->interval_max) {
2548 sender_new = g_strdup(param->sender);
2550 _bt_convert_addr_string_to_type(addr, dev->address);
2551 ret = _bt_le_conn_update(sender_new, addr,
2552 param->interval_min, param->interval_max,
2553 param->latency, param->time_out);
2556 if (ret != BLUETOOTH_ERROR_NONE)
2557 BT_ERR("Unable to set le connection parameter");
2560 BT_INFO("Set the default interval");
2562 bluetooth_le_connection_param_t param = { 0 };
2563 _bt_get_le_connection_parameter(
2564 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2567 ret = __bt_le_set_conn_parameter(dev->address,
2568 param.interval_min, param.interval_max,
2569 param.latency, param.timeout);
2570 if (ret == BLUETOOTH_ERROR_NONE) {
2571 dev->interval_min = param.interval_min;
2572 dev->interval_max = param.interval_max;
2581 void _bt_add_le_connected_dev_info(const char *address)
2583 bt_connected_le_dev_t *dev = NULL;
2588 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2589 dev->address = g_strdup(address);
2591 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2596 void _bt_remove_le_connected_dev_info(const char *address)
2598 bt_connected_le_dev_t *dev = NULL;
2603 dev = __bt_get_le_connected_dev_info(address);
2607 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2608 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2609 g_free(dev->address);
2615 int _bt_le_conn_update(const char *sender,
2616 unsigned char *device_address,
2617 float interval_min, float interval_max,
2618 guint16 latency, guint16 time_out)
2620 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2621 guint32 min_supervision_to;
2622 bt_connected_le_dev_t *dev = NULL;
2623 bt_le_conn_param_t *param = NULL;
2624 gboolean is_removed = FALSE;
2625 int ret = BLUETOOTH_ERROR_NONE;
2627 BT_CHECK_PARAMETER(device_address, return);
2629 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2630 sender, interval_min, interval_max, latency, time_out);
2632 if (interval_min > interval_max ||
2633 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2634 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2635 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2639 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2640 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2641 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2645 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2646 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2651 * The Supervision_Timeout in milliseconds shall be larger than
2652 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2653 * where Conn_Interval_Max is given in milliseconds.
2655 min_supervision_to = (1 + latency) * interval_max * 2;
2656 if (time_out <= min_supervision_to) {
2657 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2661 _bt_convert_addr_type_to_string(address, device_address);
2662 BT_DBG("Remote device address: %s", address);
2664 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2666 dev = __bt_get_le_connected_dev_info(address);
2668 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2672 if (g_slist_length(dev->senders) == 1)
2675 param = dev->senders->data;
2677 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2679 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2680 BT_DBG("Skip due to same interval");
2684 interval_min = param->interval_min;
2685 interval_max = param->interval_max;
2689 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2691 if (ret != BLUETOOTH_ERROR_NONE) {
2692 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2696 dev->interval_min = interval_min;
2697 dev->interval_max = interval_max;
2703 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2704 bluetooth_device_pin_code_t *pin_code)
2706 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2708 bt_pin_code_info_t *pin_info = NULL;
2710 BT_CHECK_PARAMETER(device_address, return);
2711 BT_CHECK_PARAMETER(pin_code, return);
2712 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2713 BLUETOOTH_ERROR_NO_RESOURCES);
2715 _bt_convert_addr_type_to_string(address, device_address->addr);
2717 for (l = pin_info_list; l != NULL; l = l->next) {
2720 if (g_strcmp0(pin_info->address, address) == 0) {
2721 g_free(pin_info->pin_code);
2722 pin_info->pin_code = g_strdup(pin_code->pin_code);
2723 return BLUETOOTH_ERROR_NONE;
2727 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2728 pin_info->address = g_strdup(address);
2729 pin_info->pin_code = g_strdup(pin_code->pin_code);
2730 pin_info_list = g_slist_append(pin_info_list, pin_info);
2732 return BLUETOOTH_ERROR_NONE;
2735 gint __bt_compare_address(gpointer *a, gpointer *b)
2737 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2738 char *address = (char *)b;
2739 return g_strcmp0(pin_info->address, address);
2742 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2744 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2746 bt_pin_code_info_t *pin_info = NULL;
2748 BT_CHECK_PARAMETER(device_address, return);
2750 _bt_convert_addr_type_to_string(address, device_address->addr);
2752 l = g_slist_find_custom(pin_info_list, address,
2753 (GCompareFunc)__bt_compare_address);
2757 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2758 g_free(pin_info->address);
2759 g_free(pin_info->pin_code);
2763 return BLUETOOTH_ERROR_NONE;
2766 int _bt_get_device_pin_code(const char *address, char *pin_code)
2770 BT_CHECK_PARAMETER(address, return);
2771 BT_CHECK_PARAMETER(pin_code, return);
2773 for (l = pin_info_list; l != NULL; l = l->next) {
2774 bt_pin_code_info_t *pin_info = l->data;
2776 if (g_strcmp0(pin_info->address, address) == 0) {
2777 g_strlcpy(pin_code, pin_info->pin_code,
2778 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2780 return BLUETOOTH_ERROR_NONE;
2784 return BLUETOOTH_ERROR_NOT_FOUND;
2787 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2788 bluetooth_le_connection_param_t *param)
2791 return BLUETOOTH_ERROR_INVALID_PARAM;
2793 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2794 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2795 return BLUETOOTH_ERROR_INVALID_PARAM;
2797 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2800 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2801 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2802 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2803 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2804 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2807 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2808 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2809 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2810 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2811 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2814 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2815 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2816 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2817 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2818 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2822 BT_ERR("Unhandled mode : %d", mode);
2826 return BLUETOOTH_ERROR_NONE;
2829 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2830 guint trusted_profile_flag, guint *trusted)
2836 case TRUSTED_PROFILE_PBAP:
2837 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2838 trust_profile = trusted_profile_flag & (1 << 1);
2840 return BLUETOOTH_ERROR_NOT_SUPPORT;
2842 case TRUSTED_PROFILE_MAP:
2843 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2844 trust_profile = trusted_profile_flag & (1 << 3);
2846 return BLUETOOTH_ERROR_NOT_SUPPORT;
2848 case TRUSTED_PROFILE_SAP:
2849 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2850 trust_profile = trusted_profile_flag & (1 << 5);
2852 return BLUETOOTH_ERROR_NOT_SUPPORT;
2854 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2855 *trusted = trusted_profile_flag;
2856 return BLUETOOTH_ERROR_NONE;
2858 return BLUETOOTH_ERROR_NOT_SUPPORT;
2864 return BLUETOOTH_ERROR_NONE;
2867 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2868 guint restricted_profile_flag, guint *restricted)
2870 int restrict_profile;
2871 *restricted = FALSE;
2874 case RESTRICTED_PROFILE_HFP_HS:
2875 restrict_profile = restricted_profile_flag & (1 << 0);
2877 case RESTRICTED_PROFILE_A2DP:
2878 restrict_profile = restricted_profile_flag & (1 << 2);
2881 return BLUETOOTH_ERROR_NOT_SUPPORT;
2884 if (restrict_profile)
2887 return BLUETOOTH_ERROR_NONE;
2890 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2893 case TRUSTED_PROFILE_PBAP:
2894 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2895 case TRUSTED_PROFILE_MAP:
2896 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2897 case TRUSTED_PROFILE_SAP:
2898 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2899 case TRUSTED_PROFILE_ALL:
2906 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2909 case RESTRICTED_PROFILE_HFP_HS:
2910 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2911 case RESTRICTED_PROFILE_A2DP:
2912 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2918 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2920 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2921 return TRUSTED_PROFILE_PBAP;
2922 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2923 return TRUSTED_PROFILE_MAP;
2924 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2925 return TRUSTED_PROFILE_SAP;
2927 return 0; /* 0 - Unknown Profile */
2930 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2931 bluetooth_trusted_profile_t profile, gboolean trust)
2933 int ret = BLUETOOTH_ERROR_NONE;
2934 GDBusConnection *conn;
2936 GError *error = NULL;
2937 char *device_path = NULL;
2939 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2942 BT_CHECK_PARAMETER(bd_addr, return);
2943 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2944 bd_addr->addr[0], bd_addr->addr[1],
2945 bd_addr->addr[2], bd_addr->addr[3],
2946 bd_addr->addr[4], bd_addr->addr[5],
2949 conn = _bt_gdbus_get_system_gconn();
2950 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2952 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2954 device_path = _bt_get_device_object_path(address);
2955 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2957 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2958 NULL, BT_BLUEZ_NAME, device_path,
2959 BT_DEVICE_INTERFACE, NULL, NULL);
2961 g_free(device_path);
2962 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2964 uuid = _bt_get_trusted_profile_uuid(profile);
2966 g_object_unref(proxy);
2967 return BLUETOOTH_ERROR_NOT_SUPPORT;
2970 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2971 g_variant_new("(sb)", uuid, trust),
2972 G_DBUS_CALL_FLAGS_NONE, -1,
2974 g_object_unref(proxy);
2976 if (reply == NULL) {
2977 BT_ERR("Failed to Set Profile Trusted");
2978 ret = BLUETOOTH_ERROR_INTERNAL;
2980 BT_ERR("Error %s[%s]", error->message, address);
2981 g_error_free(error);
2985 g_variant_unref(reply);
2992 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2993 bluetooth_trusted_profile_t profile, guint *trust)
2995 int ret = BLUETOOTH_ERROR_NONE;
2996 GDBusConnection *conn;
2998 GError *error = NULL;
2999 char *device_path = NULL;
3000 guint trusted_profile_flag;
3001 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3004 BT_CHECK_PARAMETER(bd_addr, return);
3005 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3006 bd_addr->addr[0], bd_addr->addr[1],
3007 bd_addr->addr[2], bd_addr->addr[3],
3008 bd_addr->addr[4], bd_addr->addr[5],
3011 conn = _bt_gdbus_get_system_gconn();
3012 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3014 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3016 device_path = _bt_get_device_object_path(address);
3017 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3019 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3020 NULL, BT_BLUEZ_NAME, device_path,
3021 BT_PROPERTIES_INTERFACE, NULL, NULL);
3023 g_free(device_path);
3024 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3026 reply = g_dbus_proxy_call_sync(proxy, "Get",
3027 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3028 G_DBUS_CALL_FLAGS_NONE, -1,
3030 g_object_unref(proxy);
3032 if (reply == NULL) {
3033 BT_ERR("Failed to Get Profile Trusted");
3034 ret = BLUETOOTH_ERROR_INTERNAL;
3036 BT_ERR("Error %s[%s]", error->message, address);
3037 g_error_free(error);
3042 g_variant_get(reply, "(v)", &temp);
3043 trusted_profile_flag = g_variant_get_uint32(temp);
3044 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3046 ret = _bt_get_trusted_profile_from_flag(profile,
3047 trusted_profile_flag, trust);
3048 g_variant_unref(temp);
3049 g_variant_unref(reply);
3052 BT_DBG("TRUST %d", *trust);
3056 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3057 bluetooth_restricted_profile_t profile, gboolean restricted)
3059 int ret = BLUETOOTH_ERROR_NONE;
3060 GDBusConnection *conn;
3062 GError *error = NULL;
3063 char *device_path = NULL;
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] restricted[%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],
3073 profile, restricted);
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_DEVICE_INTERFACE, NULL, NULL);
3087 g_free(device_path);
3088 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3090 uuid = _bt_get_restricted_profile_uuid(profile);
3092 g_object_unref(proxy);
3093 return BLUETOOTH_ERROR_NOT_SUPPORT;
3096 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3097 g_variant_new("(sb)", uuid, restricted),
3098 G_DBUS_CALL_FLAGS_NONE, -1,
3100 g_object_unref(proxy);
3102 if (reply == NULL) {
3103 BT_ERR("Failed to Set Profile Restricted");
3104 ret = BLUETOOTH_ERROR_INTERNAL;
3106 BT_ERR("Error %s[%s]", error->message, address);
3107 g_error_free(error);
3111 g_variant_unref(reply);
3118 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3119 bluetooth_restricted_profile_t profile, guint *restricted)
3121 int ret = BLUETOOTH_ERROR_NONE;
3122 GDBusConnection *conn;
3124 GError *error = NULL;
3125 char *device_path = NULL;
3126 guint restricted_profile_flag;
3127 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3130 BT_CHECK_PARAMETER(bd_addr, return);
3131 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3132 bd_addr->addr[0], bd_addr->addr[1],
3133 bd_addr->addr[2], bd_addr->addr[3],
3134 bd_addr->addr[4], bd_addr->addr[5],
3135 profile, *restricted);
3137 conn = _bt_gdbus_get_system_gconn();
3138 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3140 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3142 device_path = _bt_get_device_object_path(address);
3143 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3145 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3146 NULL, BT_BLUEZ_NAME, device_path,
3147 BT_PROPERTIES_INTERFACE, NULL, NULL);
3149 g_free(device_path);
3150 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3152 reply = g_dbus_proxy_call_sync(proxy, "Get",
3153 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3154 G_DBUS_CALL_FLAGS_NONE, -1,
3156 g_object_unref(proxy);
3158 if (reply == NULL) {
3159 BT_ERR("Failed to Get Profile Restricted");
3160 ret = BLUETOOTH_ERROR_INTERNAL;
3162 BT_ERR("Error %s[%s]", error->message, address);
3163 g_error_free(error);
3168 g_variant_get(reply, "(v)", &temp);
3169 restricted_profile_flag = g_variant_get_uint32(temp);
3170 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3172 ret = _bt_get_restricted_profile_from_flag(profile,
3173 restricted_profile_flag, restricted);
3174 g_variant_unref(temp);
3175 g_variant_unref(reply);
3178 BT_DBG("TRUST %d", *restricted);
3182 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3186 GVariant *out_param1;
3187 request_info_t *req_info;
3188 GVariant *val = NULL;
3189 GVariant *param = NULL;
3192 bluetooth_device_address_t device_addr = { {0} };
3193 int result = BLUETOOTH_ERROR_NONE;
3196 val = g_dbus_proxy_call_finish(proxy, res, &err);
3198 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3199 if (req_info == NULL) {
3200 BT_ERR("req_info == NULL");
3201 g_object_unref(proxy);
3202 att_mtu_req_info->device_proxy = NULL;
3207 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3209 if (g_strrstr(err->message, "NotSupported")) {
3210 BT_INFO("Connection Not Supported");
3211 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3212 } else if (g_strrstr(err->message, "NotConnected")) {
3213 BT_INFO("Not connected");
3214 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3215 } else if (g_strrstr(err->message, "InvalidArguments")) {
3216 BT_INFO("Not connected");
3217 result = BLUETOOTH_ERROR_INVALID_PARAM;
3219 BT_DBG("Default case");
3220 result = BLUETOOTH_ERROR_INTERNAL;
3224 g_object_unref(proxy);
3225 att_mtu_req_info->device_proxy = NULL;
3227 if (result != BLUETOOTH_ERROR_NONE)
3231 g_variant_get(val, "(qy)", &mtu, &status);
3232 g_variant_unref(val);
3235 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3237 param = g_variant_new("(isqy)",
3239 att_mtu_req_info->addr,
3243 /* Send the event to application */
3244 _bt_send_event(BT_DEVICE_EVENT,
3245 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3249 if (req_info->context == NULL)
3252 _bt_convert_addr_string_to_type(device_addr.addr,
3253 (const char *)att_mtu_req_info->addr);
3255 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3256 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3257 g_dbus_method_invocation_return_value(req_info->context,
3258 g_variant_new("(iv)", result, out_param1));
3260 _bt_delete_request_list(req_info->req_id);
3265 g_free(att_mtu_req_info->addr);
3266 g_free(att_mtu_req_info);
3267 att_mtu_req_info = NULL;
3272 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3275 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3276 gchar *device_path = NULL;
3277 GDBusProxy *adapter_proxy;
3278 GDBusProxy *device_proxy;
3279 GDBusConnection *conn;
3280 int ret = BLUETOOTH_ERROR_NONE;
3282 BT_CHECK_PARAMETER(device_address, return);
3284 if (att_mtu_req_info) {
3285 BT_ERR("ATT MTU request in progress");
3286 return BLUETOOTH_ERROR_DEVICE_BUSY;
3289 conn = _bt_gdbus_get_system_gconn();
3290 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3292 adapter_proxy = _bt_get_adapter_proxy();
3293 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3295 _bt_convert_addr_type_to_string(address, device_address->addr);
3297 BT_DBG("Remote device address: %s", address);
3299 device_path = _bt_get_device_object_path(address);
3301 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3303 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3304 NULL, BT_BLUEZ_NAME,
3305 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3306 g_free(device_path);
3307 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3309 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3310 att_mtu_req_info->addr = (char *)g_strdup(address);
3311 att_mtu_req_info->req_id = request_id;
3312 att_mtu_req_info->device_proxy = device_proxy;
3314 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3315 g_variant_new("(q)", mtu),
3316 G_DBUS_CALL_FLAGS_NONE,
3317 BT_MAX_DBUS_TIMEOUT,
3319 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3325 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3328 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3329 char *object_path = NULL;
3331 GDBusProxy *device_proxy;
3332 GError *error = NULL;
3334 GDBusConnection *conn;
3335 GVariant *result = NULL;
3337 BT_CHECK_PARAMETER(device_address, return);
3339 conn = _bt_gdbus_get_system_gconn();
3340 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3342 _bt_convert_addr_type_to_string(address, device_address->addr);
3344 object_path = _bt_get_device_object_path(address);
3345 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3347 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3348 NULL, BT_BLUEZ_NAME, object_path,
3349 BT_PROPERTIES_INTERFACE, NULL, NULL);
3350 g_free(object_path);
3351 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3353 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3354 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3355 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3357 ERR("DBus Error : %s", error->message);
3358 g_clear_error(&error);
3359 g_object_unref(device_proxy);
3360 return BLUETOOTH_ERROR_INTERNAL;
3362 g_variant_get(result, "(v)", &value);
3363 *mtu = g_variant_get_uint16(value);
3364 BT_DBG("ATT MTU : %d", *mtu);
3365 g_variant_unref(value);
3366 g_variant_unref(result);
3369 g_object_unref(device_proxy);
3370 return BLUETOOTH_ERROR_NONE;
3373 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3374 bluetooth_device_address_t *id_address)
3376 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3377 gchar *device_path = NULL;
3378 const gchar *idaddress = NULL;
3379 GDBusProxy *device_proxy;
3380 GError *error = NULL;
3381 GVariant *result = NULL;
3382 GDBusConnection *conn;
3383 int ret = BLUETOOTH_ERROR_NONE;
3385 BT_CHECK_PARAMETER(device_address, return);
3387 conn = _bt_gdbus_get_system_gconn();
3388 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3390 _bt_convert_addr_type_to_string(address, device_address->addr);
3392 device_path = _bt_get_device_object_path(address);
3393 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3395 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3396 NULL, BT_BLUEZ_NAME,
3397 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3398 g_free(device_path);
3399 if (!device_proxy) {
3400 BT_ERR("Unable to get proxy");
3401 return BLUETOOTH_ERROR_INTERNAL;
3404 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3405 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3406 if (result == NULL) {
3407 BT_ERR("Failed to get device ID address");
3408 if (error != NULL) {
3409 BT_ERR("Error occured in Proxy call [%s]", error->message);
3410 g_error_free(error);
3412 g_object_unref(device_proxy);
3413 return BLUETOOTH_ERROR_INTERNAL;
3416 g_variant_get(result , "(&s)", &idaddress);
3417 if (idaddress == NULL) {
3418 BT_ERR("No paired device");
3419 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3421 DBG_SECURE("ID Address : %s", idaddress);
3422 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3425 g_variant_unref(result);
3426 g_object_unref(device_proxy);
3431 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3433 GapAgentPrivate *agent = _bt_get_adapter_agent();
3434 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3436 if (authentication_reply)
3437 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3439 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3441 BT_DBG("BT_PASSKEY_REPLY");
3442 return BLUETOOTH_ERROR_NONE;
3445 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3447 GapAgentPrivate *agent = _bt_get_adapter_agent();
3448 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3450 if (confirmation_reply)
3451 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3453 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3455 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3456 return BLUETOOTH_ERROR_NONE;