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"
40 #include "bt-service-pbap.h"
42 #ifdef TIZEN_FEATURE_BT_DPM
43 #include "bt-service-dpm.h"
46 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
47 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
48 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
50 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
51 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
52 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
53 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
54 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
55 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
56 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
57 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
59 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
61 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
62 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
63 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
65 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
67 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
69 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
70 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
71 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
73 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
80 GDBusProxy *device_proxy;
81 GDBusProxy *adapter_proxy;
83 unsigned short conn_type;
84 gboolean bonding_wo_discovery;
94 bluetooth_device_address_t bd_addr;
95 gboolean auto_connect;
96 } bt_pending_le_conn_info_s;
98 gboolean is_device_creating;
99 bt_funcion_data_t *bonding_info;
100 bt_funcion_data_t *searching_info;
101 bt_funcion_data_t *att_mtu_req_info;
103 static GSList *pin_info_list = NULL;
104 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
105 static guint pending_le_conn_timer_id = 0;
107 /* This HID Mouse does not support pairing precedure. need to skip it. */
108 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
110 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
113 static int __bt_retry_bond(void);
116 /*static void __bt_decline_pair_request()
118 GVariant *out_param1;
119 GVariant *out_param2;
120 request_info_t *req_info;
121 bluetooth_device_info_t dev_info;
122 bt_remote_dev_info_t *remote_dev_info;
123 GVariant *uuid_list, *manufacture_data;
128 req_info = _bt_get_request_info(bonding_info->req_id);
129 if (req_info == NULL) {
130 BT_ERR("req_info == NULL");
133 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
135 BT_DBG("bonding_info is NULL");
140 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
141 remote_dev_info->uuids, remote_dev_info->uuid_count,
144 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
145 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
148 param = g_variant_new("isunsbuba{s}na{y})",
149 bonding_info->result,
151 remote_dev_info->class,
152 remote_dev_info->rssi,
153 remote_dev_info->name,
154 remote_dev_info->paired,
155 remote_dev_info->connected,
156 remote_dev_info->trust,
158 remote_dev_info->manufacturer_data_len,
162 //Send the event to application
163 if (remote_dev_info != NULL) {
164 _bt_send_event(BT_ADAPTER_EVENT,
165 BLUETOOTH_EVENT_BONDING_FINISHED,
168 _bt_free_device_info(remote_dev_info);
171 if (req_info->context == NULL)
174 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
175 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
178 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
179 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
181 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
182 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
185 g_dbus_method_invocation_return_value(req_info->context,
186 g_variant_new("@ayi", out_param1, out_param2));
189 _bt_delete_request_list(req_info->req_id);
192 g_free(bonding_info->addr);
193 g_free(bonding_info);
199 #ifdef TIZEN_PROFILE_WEARABLE
200 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
204 retv_if(user_data == NULL, FALSE);
206 b = (bundle *)user_data;
208 ret = syspopup_launch("bt-syspopup", b);
210 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
212 BT_DBG("Hurray!!! Finally Popup launched");
215 return (ret < 0) ? TRUE : FALSE;
218 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
223 GDBusConnection *conn;
225 conn = _bt_gdbus_get_system_gconn();
233 bundle_add(b, "event-type", "unable-to-pairing");
235 if (result == BLUETOOTH_ERROR_TIMEOUT)
236 bundle_add(b, "error", "timeout");
237 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
238 bundle_add(b, "error", "authfailed");
240 bundle_add(b, "error", "error");
242 ret = syspopup_launch("bt-syspopup", b);
244 BT_ERR("Popup launch failed...retry %d \n", ret);
245 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
256 gboolean _bt_is_device_creating(void)
258 return is_device_creating;
261 gboolean _bt_is_bonding_device_address(const char *address)
263 if (bonding_info == NULL || bonding_info->addr == NULL)
266 if (g_strcmp0(bonding_info->addr, address) == 0) {
267 BT_DBG("[%s] is bonding device", address);
271 BT_DBG("[%s] is NOT bonding device", address);
275 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
277 ret_if(bonding_info == NULL);
278 bonding_info->is_autopair = is_autopair;
281 void __bt_cancel_search_service_done(void)
283 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
284 request_info_t *req_info;
285 bluetooth_device_info_t dev_info;
286 GVariant *out_param1;
288 ret_if(searching_info == NULL);
290 req_info = _bt_get_request_info(searching_info->req_id);
291 if (req_info == NULL) {
292 BT_ERR("req_info == NULL");
296 if (req_info->context == NULL)
299 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
300 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
301 searching_info->addr);
303 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
304 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
307 g_dbus_method_invocation_return_value(req_info->context,
308 g_variant_new("(iv)", result, out_param1));
310 _bt_delete_request_list(req_info->req_id);
314 g_free(searching_info->addr);
315 g_free(searching_info);
316 searching_info = NULL;
319 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
321 ret_if(value == NULL);
322 ret_if(info == NULL);
324 gsize uuid_count = 0;
326 info->uuids = g_variant_dup_strv(value, &uuid_count);
327 info->uuid_count = (unsigned int)uuid_count;
329 BT_DBG("uuid count : %d", uuid_count);
332 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
334 char *object_path = NULL;
335 bt_remote_dev_info_t *dev_info;
337 retv_if(address == NULL, NULL);
339 object_path = _bt_get_device_object_path(address);
340 retv_if(object_path == NULL, NULL);
342 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
348 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
349 const char *object_path)
351 bt_remote_dev_info_t *dev_info;
352 GDBusProxy *adapter_proxy;
353 GDBusProxy *device_proxy;
358 GDBusConnection *conn;
359 GError *error = NULL;
360 GVariant *result = NULL;
361 GVariantIter *value_iter;
365 adapter_proxy = _bt_get_adapter_proxy();
366 retv_if(adapter_proxy == NULL, NULL);
368 retv_if(object_path == NULL, NULL);
370 conn = _bt_gdbus_get_system_gconn();
372 BT_ERR("conn == NULL");
376 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
377 NULL, BT_BLUEZ_NAME, object_path,
378 BT_PROPERTIES_INTERFACE, NULL, NULL);
380 retv_if(device_proxy == NULL, NULL);
382 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
383 g_variant_new("(s)", BT_DEVICE_INTERFACE),
384 G_DBUS_CALL_FLAGS_NONE,
389 g_object_unref(device_proxy);
391 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
393 if (result != NULL) {
394 g_variant_get(result , "(@a{sv})", &value);
395 g_variant_unref(result);
397 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
399 g_variant_get(tmp_value, "s", &name);
400 g_variant_unref(tmp_value);
402 DBG_SECURE("Alias Name [%s]", name);
404 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
405 g_variant_get(tmp_value, "s", &name);
406 g_variant_unref(tmp_value);
409 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
411 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
412 g_variant_unref(tmp_value);
414 dev_info->is_alias_set = FALSE;
416 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
418 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
420 dev_info->class = g_variant_get_uint32(tmp_value);
421 g_variant_unref(tmp_value);
425 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
427 dev_info->connected = g_variant_get_byte(tmp_value);
428 g_variant_unref(tmp_value);
430 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
431 BT_DBG("connected link : %d", dev_info->connected);
433 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
435 dev_info->trust = g_variant_get_boolean(tmp_value);
436 g_variant_unref(tmp_value);
438 dev_info->trust = FALSE;
440 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
442 dev_info->paired = g_variant_get_boolean(tmp_value);
443 g_variant_unref(tmp_value);
445 dev_info->paired = FALSE;
447 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
449 dev_info->rssi = g_variant_get_int16(tmp_value);
450 g_variant_unref(tmp_value);
454 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
456 dev_info->addr_type = g_variant_get_byte(tmp_value);
457 g_variant_unref(tmp_value);
459 dev_info->addr_type = 0;
461 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
463 __bt_get_uuids(tmp_value, dev_info);
464 g_variant_unref(tmp_value);
467 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
469 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
470 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
471 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
472 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
474 g_variant_unref(tmp_value);
476 dev_info->manufacturer_data_len = 0;
478 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
480 if ((dev_info->manufacturer_data_len == 0) ||
481 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
482 BT_ERR("manufacturer data length doesn't match");
483 dev_info->manufacturer_data_len = 0;
484 dev_info->manufacturer_data = NULL;
486 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
487 g_variant_get(tmp_value, "ay", &value_iter);
488 while (g_variant_iter_loop(value_iter, "y", &m_value))
489 dev_info->manufacturer_data[i++] = m_value;
491 g_variant_unref(tmp_value);
493 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
494 dev_info->manufacturer_data_len = 0;
495 dev_info->manufacturer_data = NULL;
498 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
499 g_variant_get(tmp_value, "s", &address);
500 g_variant_unref(tmp_value);
502 dev_info->address = g_strdup(address);
503 dev_info->name = g_strdup(name);
505 g_variant_unref(value);
507 BT_ERR("result is NULL\n");
515 char *_bt_get_bonded_device_name(char *address)
517 bluetooth_device_address_t device_address = { {0} };
518 bluetooth_device_info_t dev_info;
520 retv_if(address == NULL, strdup(""));
522 _bt_convert_addr_string_to_type(device_address.addr, address);
524 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
526 _bt_get_bonded_device_info(&device_address, &dev_info);
528 return g_strdup(dev_info.device_name.name);
531 static gboolean __ignore_auto_pairing_request(const char *address)
536 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
547 /* Get the LAP(Lower Address part) */
548 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
550 snprintf(lap_address, sizeof(lap_address), ",%s", address);
552 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
555 BT_ERR("fopen failed \n");
559 fseek(fp, 0, SEEK_END);
564 BT_ERR("Get file size failed \n");
569 buffer = g_malloc0(sizeof(char) * size);
570 result = fread((char *)buffer, 1, size, fp);
572 if (result != size) {
573 BT_ERR("Read Error\n");
578 BT_DBG("Buffer = %s\n", buffer);
580 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
586 /* Write the data and insert new device data */
587 for (i = 0; lines[i] != NULL; i++) {
588 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
589 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
591 lines[i] = temp_buffer;
594 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
596 /* Fix : NULL_RETURNS */
597 retv_if(buffer == NULL, FALSE);
599 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
602 BT_ERR("fopen failed \n");
607 BT_DBG("Buffer = %s\n", buffer);
608 fwrite(buffer, 1, strlen(buffer), fp);
618 static int __bt_cancel_bonding(void)
620 BT_CHECK_PARAMETER(bonding_info, return);
621 BT_CHECK_PARAMETER(bonding_info->addr, return);
622 /* First Cancel the ongoing pairing in bluez otherwise if we send
623 * pair request bluez will send inprogress and we again retry bond so
624 * this cycle continues */
626 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
627 NULL, G_DBUS_CALL_FLAGS_NONE,
630 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
632 return BLUETOOTH_ERROR_PARING_FAILED;
634 return BLUETOOTH_ERROR_NONE;
637 static int __bt_retry_bond(void)
639 BT_CHECK_PARAMETER(bonding_info, return);
640 BT_CHECK_PARAMETER(bonding_info->addr, return);
642 is_device_creating = TRUE;
644 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
645 g_variant_new("(y)", bonding_info->conn_type),
646 G_DBUS_CALL_FLAGS_NONE,
649 (GAsyncReadyCallback)__bt_bond_device_cb,
652 return BLUETOOTH_ERROR_NONE;
656 static int __bt_remove_and_bond(void)
658 GDBusProxy *adapter_proxy;
659 GVariant *result = NULL;
661 char *device_path = NULL;
663 BT_CHECK_PARAMETER(bonding_info, return);
664 BT_CHECK_PARAMETER(bonding_info->addr, return);
666 adapter_proxy = _bt_get_adapter_proxy();
667 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
669 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
670 g_variant_new("(s)", bonding_info->addr),
671 G_DBUS_CALL_FLAGS_NONE,
676 return BLUETOOTH_ERROR_INTERNAL;
678 g_variant_get(result , "(o)", &device_path);
679 g_variant_unref(result);
681 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
683 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
684 g_variant_new("(o)", device_path),
685 G_DBUS_CALL_FLAGS_NONE,
691 BT_ERR("UnpairDevice Fail: %s", err->message);
693 return BLUETOOTH_ERROR_INTERNAL;
696 return __bt_retry_bond();
699 static int __bt_cancel_and_bond(void)
701 int ret = BLUETOOTH_ERROR_NONE;
703 ret = _bt_agent_reply_cancellation();
704 if (ret != BLUETOOTH_ERROR_NONE) {
705 BT_ERR("Fail to call reply cancellation");
708 return __bt_retry_bond();
712 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
715 int result = BLUETOOTH_ERROR_NONE;
718 GVariant *out_param1;
719 request_info_t *req_info;
720 bluetooth_device_info_t dev_info;
721 bt_remote_dev_info_t *remote_dev_info;
722 GVariant *manufacture_data;
725 /* Terminate ALL system popup */
726 syspopup_destroy_all();
728 reply = g_dbus_proxy_call_finish(proxy, res, &err);
730 g_variant_unref(reply);
732 is_device_creating = FALSE;
734 if (bonding_info == NULL) {
736 BT_ERR("bonding_info == NULL");
742 req_info = _bt_get_request_info(bonding_info->req_id);
743 if (req_info == NULL) {
744 BT_ERR("req_info == NULL");
749 g_dbus_error_strip_remote_error(err);
750 BT_ERR("Error occured in CreateBonding [%s]", err->message);
752 if (g_strrstr(err->message, "Already Exists")) {
753 BT_INFO("Existing Bond, remove and retry");
754 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
756 result = BLUETOOTH_ERROR_PARING_FAILED;
757 } else if (_bt_agent_is_canceled() ||
758 g_strrstr(err->message, "Authentication Canceled")) {
759 BT_INFO("Cancelled by USER");
760 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
761 } else if (g_strrstr(err->message, "Authentication Rejected")) {
763 result = BLUETOOTH_ERROR_ACCESS_DENIED;
764 } else if (g_strrstr(err->message, "In Progress")) {
765 BT_INFO("Bond in progress, cancel and retry");
766 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
768 result = BLUETOOTH_ERROR_PARING_FAILED;
769 } else if (g_strrstr(err->message, "Authentication Failed")) {
770 BT_INFO("Authentication Failed");
771 if (bonding_info->is_autopair == TRUE) {
772 _bt_set_autopair_status_in_bonding_info(FALSE);
773 __ignore_auto_pairing_request(bonding_info->addr);
775 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
776 } else if (g_strrstr(err->message, "Page Timeout")) {
777 BT_INFO("Page Timeout");
778 /* This is the special case
779 As soon as call bluetooth_bond_device, try to cancel bonding.
780 In this case, before completing to call 'CreatePairedDevice' method
781 the procedure is stopped. So 'Cancle' error is not return.
783 result = BLUETOOTH_ERROR_HOST_DOWN;
784 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
785 BT_INFO("Cancel already running bonding");
786 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
787 BT_INFO("Error while Cancelling bonding");
788 /* we need to unref proxy so continue */
790 result = BLUETOOTH_ERROR_INTERNAL;
791 } else if (g_strrstr(err->message, "Connection Timeout")) {
792 /* Pairing request timeout */
793 result = BLUETOOTH_ERROR_TIMEOUT;
794 } else if (g_strrstr(err->message, "Authentication Timeout")) {
795 /* Pairing request timeout */
796 result = BLUETOOTH_ERROR_TIMEOUT;
798 BT_DBG("Default case");
799 result = BLUETOOTH_ERROR_PARING_FAILED;
803 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
804 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
805 result == BLUETOOTH_ERROR_TIMEOUT ||
806 result == BLUETOOTH_ERROR_HOST_DOWN) {
807 #ifdef TIZEN_PROFILE_WEARABLE
808 int is_sw_running = 0;
810 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
811 BT_ERR("vconf_get_int for setup wizard state failed");
814 __bt_launch_unable_to_pairing_syspopup(result);
816 BT_ERR("Unable to pair");
818 bonding_info->result = result;
821 g_object_unref(proxy);
822 bonding_info->device_proxy = NULL;
824 if (result != BLUETOOTH_ERROR_NONE) {
825 if (bonding_info->bonding_wo_discovery) {
826 GDBusProxy *adapter_proxy;
827 GVariant *ret = NULL;
828 GError *error = NULL;
831 BT_ERR("Bond was tried without discovery. Remove it");
833 adapter_proxy = _bt_get_adapter_proxy();
834 if (adapter_proxy == NULL) {
835 BT_ERR("Cannot get adapter_proxy");
839 device_path = _bt_get_device_object_path(bonding_info->addr);
840 if (device_path == NULL) {
841 BT_ERR("Cannot get device path");
845 ret = g_dbus_proxy_call_sync(adapter_proxy,
847 g_variant_new("(o)", device_path),
848 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
851 BT_ERR("RemoveDevice Fail: %s", error->message);
852 g_clear_error(&error);
856 g_variant_unref(ret);
864 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
865 if (!remote_dev_info)
868 GVariant *uuids = NULL;
869 GVariantBuilder *builder = NULL;
871 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
872 for (i = 0; i < remote_dev_info->uuid_count; i++) {
873 g_variant_builder_add(builder, "s",
874 remote_dev_info->uuids[i]);
876 uuids = g_variant_new("as", builder);
877 g_variant_builder_unref(builder);
878 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
879 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
882 param = g_variant_new("(isunsbub@asn@ay)",
885 remote_dev_info->class,
886 remote_dev_info->rssi,
887 remote_dev_info->name,
888 remote_dev_info->paired,
889 remote_dev_info->connected,
890 remote_dev_info->trust,
892 remote_dev_info->manufacturer_data_len,
896 /* Send the event to application */
897 _bt_send_event(BT_ADAPTER_EVENT,
898 BLUETOOTH_EVENT_BONDING_FINISHED,
901 _bt_free_device_info(remote_dev_info);
904 if (req_info->context == NULL)
907 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
908 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
911 if (_bt_adapter_get_status() != BT_ACTIVATED)
912 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
914 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
915 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
917 g_dbus_method_invocation_return_value(req_info->context,
918 g_variant_new("(iv)", result, out_param1));
920 g_variant_unref(out_param1);
922 _bt_delete_request_list(req_info->req_id);
927 _bt_agent_set_canceled(FALSE);
930 g_free(bonding_info->addr);
931 g_free(bonding_info);
935 int _bt_bond_device(int request_id,
936 bluetooth_device_address_t *device_address,
937 unsigned short conn_type, GArray **out_param1)
940 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
941 bluetooth_device_info_t dev_info;
943 GDBusConnection *conn;
944 char *device_path = NULL;
945 GDBusProxy *adapter_proxy;
946 GError *error = NULL;
947 gboolean bonding_wo_discovery = FALSE;
949 BT_CHECK_PARAMETER(device_address, return);
951 #ifdef TIZEN_FEATURE_BT_DPM
952 int pairing_state = DPM_STATUS_ERROR;
954 _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
955 if (pairing_state == DPM_RESTRICTED) {
956 BT_ERR("Not allow to pair the device");
957 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
962 BT_ERR("Bonding in progress");
964 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
965 memcpy(dev_info.device_address.addr, device_address->addr,
966 BLUETOOTH_ADDRESS_LENGTH);
968 g_array_append_vals(*out_param1, &dev_info,
969 sizeof(bluetooth_device_info_t));
971 return BLUETOOTH_ERROR_DEVICE_BUSY;
974 conn = _bt_gdbus_get_system_gconn();
975 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
977 _bt_convert_addr_type_to_string(address, device_address->addr);
979 device_path = _bt_get_device_object_path(address);
981 if (device_path == NULL) {
982 BT_ERR("No searched device");
983 GVariant *ret = NULL;
984 adapter_proxy = _bt_get_adapter_proxy();
985 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
987 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
988 g_variant_new("(s)", address),
989 G_DBUS_CALL_FLAGS_NONE,
995 BT_ERR("CreateDevice Fail: %s", error->message);
996 g_clear_error(&error);
999 g_variant_unref(ret);
1000 device_path = _bt_get_device_object_path(address);
1001 if (device_path == NULL) {
1002 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1003 memcpy(dev_info.device_address.addr, device_address->addr,
1004 BLUETOOTH_ADDRESS_LENGTH);
1006 g_array_append_vals(*out_param1, &dev_info,
1007 sizeof(bluetooth_device_info_t));
1009 return BLUETOOTH_ERROR_NOT_PAIRED;
1011 BT_INFO("device_path is created[%s]", device_path);
1013 bonding_wo_discovery = TRUE;
1016 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1017 NULL, BT_BLUEZ_NAME,
1018 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1020 g_free(device_path);
1021 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1023 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1024 bonding_info->addr = g_strdup(address);
1025 bonding_info->req_id = request_id;
1027 bonding_info->device_proxy = proxy;
1028 bonding_info->conn_type = conn_type;
1029 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1031 is_device_creating = TRUE;
1033 g_dbus_proxy_call(proxy, "Pair",
1034 g_variant_new("(y)", conn_type),
1035 G_DBUS_CALL_FLAGS_NONE,
1036 BT_MAX_DBUS_TIMEOUT,
1038 (GAsyncReadyCallback)__bt_bond_device_cb,
1041 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1043 return BLUETOOTH_ERROR_NONE;
1045 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1046 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1047 bonding_info->addr);
1049 g_array_append_vals(*out_param1, &dev_info,
1050 sizeof(bluetooth_device_info_t));
1052 is_device_creating = FALSE;
1054 g_free(bonding_info->addr);
1055 g_free(bonding_info);
1056 bonding_info = NULL;
1058 return BLUETOOTH_ERROR_INTERNAL;*/
1061 int _bt_cancel_bonding(void)
1063 int ret = BLUETOOTH_ERROR_NONE;
1065 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1067 ret = _bt_agent_reply_cancellation();
1068 if (ret != BLUETOOTH_ERROR_NONE) {
1069 BT_ERR("Fail to call reply cancellation");
1073 _bt_agent_set_canceled(TRUE);
1075 return BLUETOOTH_ERROR_NONE;
1078 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1083 GVariant *out_param1;
1084 int result = BLUETOOTH_ERROR_NONE;
1085 bt_funcion_data_t *unbonding_info;
1086 bluetooth_device_info_t dev_info;
1087 request_info_t *req_info;
1089 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1091 g_variant_unref(reply);
1093 unbonding_info = user_data;
1095 if (unbonding_info == NULL) {
1097 BT_ERR("unbonding_info == NULL");
1101 req_info = _bt_get_request_info(unbonding_info->req_id);
1102 if (req_info == NULL) {
1103 BT_ERR("req_info == NULL");
1108 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1109 result = BLUETOOTH_ERROR_INTERNAL;
1112 if (req_info->context == NULL)
1115 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1116 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1117 unbonding_info->addr);
1119 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1120 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1122 g_dbus_method_invocation_return_value(req_info->context,
1123 g_variant_new("(iv)", result, out_param1));
1126 _bt_delete_request_list(req_info->req_id);
1132 if (unbonding_info) {
1133 g_free(unbonding_info->addr);
1134 g_free(unbonding_info);
1138 int _bt_unbond_device(int request_id,
1139 bluetooth_device_address_t *device_address,
1140 GArray **out_param1)
1142 char *device_path = NULL;
1143 bt_funcion_data_t *unbonding_info;
1144 GDBusProxy *adapter_proxy = NULL;
1145 GDBusProxy *device_proxy = NULL;
1146 GDBusConnection *conn;
1147 int result = BLUETOOTH_ERROR_INTERNAL;
1148 bluetooth_device_info_t dev_info;
1149 GError *error = NULL;
1150 GVariant *ret = NULL;
1152 BT_CHECK_PARAMETER(device_address, return);
1154 adapter_proxy = _bt_get_adapter_proxy();
1155 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1157 /* allocate user data so that it can be retrieved in callback */
1158 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1159 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1160 unbonding_info->req_id = request_id;
1162 _bt_convert_addr_type_to_string(unbonding_info->addr,
1163 device_address->addr);
1165 device_path = _bt_get_device_object_path(unbonding_info->addr);
1167 if (device_path == NULL) {
1168 BT_ERR("No paired device");
1169 result = BLUETOOTH_ERROR_NOT_PAIRED;
1173 conn = _bt_gdbus_get_system_gconn();
1175 BT_ERR("conn is NULL");
1176 result = BLUETOOTH_ERROR_INTERNAL;
1180 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1181 NULL, BT_BLUEZ_NAME,
1182 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1184 if (device_proxy != NULL) {
1186 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1187 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1188 G_DBUS_CALL_FLAGS_NONE,
1193 BT_ERR("Getting property failed: [%s]\n", error->message);
1194 g_error_free(error);
1195 result = BLUETOOTH_ERROR_NOT_PAIRED;
1199 BT_ERR("No paired device");
1200 g_object_unref(device_proxy);
1201 result = BLUETOOTH_ERROR_NOT_PAIRED;
1204 g_variant_unref(ret);
1206 g_object_unref(device_proxy);
1209 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1210 g_variant_new("(o)", device_path),
1211 G_DBUS_CALL_FLAGS_NONE,
1212 BT_MAX_DBUS_TIMEOUT,
1214 (GAsyncReadyCallback)__bt_unbond_cb,
1217 g_free(device_path);
1218 return BLUETOOTH_ERROR_NONE;
1221 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1222 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1223 unbonding_info->addr);
1225 g_array_append_vals(*out_param1, &dev_info,
1226 sizeof(bluetooth_device_info_t));
1227 g_free(device_path);
1228 g_free(unbonding_info->addr);
1229 g_free(unbonding_info);
1233 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1238 GVariant *out_param1;
1239 int result = BLUETOOTH_ERROR_NONE;
1240 bluetooth_device_info_t dev_info;
1241 bt_remote_dev_info_t *remote_dev_info;
1242 request_info_t *req_info;
1243 GVariant *uuid_list, *manufacture_data;
1245 GVariantBuilder *builder = NULL;
1248 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1250 g_variant_unref(reply);
1252 g_object_unref(proxy);
1254 if (searching_info == NULL) {
1256 BT_ERR("unbonding_info == NULL");
1260 req_info = _bt_get_request_info(searching_info->req_id);
1261 if (req_info == NULL) {
1262 BT_ERR("req_info == NULL");
1267 g_dbus_error_strip_remote_error(err);
1268 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1270 if (g_strrstr("Operation canceled", err->message))
1271 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1272 else if (g_strrstr("In Progress", err->message))
1273 result = BLUETOOTH_ERROR_IN_PROGRESS;
1274 else if (g_strrstr("Host is down", err->message))
1275 result = BLUETOOTH_ERROR_HOST_DOWN;
1277 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1279 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1280 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1281 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1282 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1283 remote_dev_info->uuid_count > 0) {
1284 result = BLUETOOTH_ERROR_NONE;
1287 _bt_free_device_info(remote_dev_info);
1292 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1293 if (!remote_dev_info)
1297 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1298 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1299 g_variant_builder_add(builder, "s",
1300 remote_dev_info->uuids[i]);
1302 uuid_list = g_variant_new("as", builder);
1303 g_variant_builder_unref(builder);
1304 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1305 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1308 param = g_variant_new("(isunsbub@asn@ay)",
1310 searching_info->addr,
1311 remote_dev_info->class,
1312 remote_dev_info->rssi,
1313 remote_dev_info->name,
1314 remote_dev_info->paired,
1315 remote_dev_info->connected,
1316 remote_dev_info->trust,
1318 remote_dev_info->manufacturer_data_len,
1321 /* Send the event to application */
1322 _bt_send_event(BT_ADAPTER_EVENT,
1323 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1326 _bt_free_device_info(remote_dev_info);
1329 if (req_info->context == NULL)
1332 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1333 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1334 searching_info->addr);
1336 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1337 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1339 g_dbus_method_invocation_return_value(req_info->context,
1340 g_variant_new("(iv)", result, out_param1));
1343 _bt_delete_request_list(req_info->req_id);
1348 if (searching_info) {
1349 g_free(searching_info->addr);
1350 g_free(searching_info);
1351 searching_info = NULL;
1355 int _bt_search_device(int request_id,
1356 bluetooth_device_address_t *device_address)
1358 char *device_path = NULL;
1359 GDBusProxy *device_proxy = NULL;
1360 GDBusConnection *conn;
1362 GDBusProxy *adapter_proxy;
1363 int result = BLUETOOTH_ERROR_INTERNAL;
1365 BT_CHECK_PARAMETER(device_address, return);
1368 BT_ERR("Bonding in progress");
1369 return BLUETOOTH_ERROR_DEVICE_BUSY;
1372 if (searching_info) {
1373 BT_ERR("Service searching in progress");
1374 return BLUETOOTH_ERROR_DEVICE_BUSY;
1377 adapter_proxy = _bt_get_adapter_proxy();
1378 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1380 /* allocate user data so that it can be retrieved in callback */
1381 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1382 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1383 searching_info->req_id = request_id;
1385 _bt_convert_addr_type_to_string(searching_info->addr,
1386 device_address->addr);
1388 conn = _bt_gdbus_get_system_gconn();
1389 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1392 device_path = _bt_get_device_object_path(searching_info->addr);
1394 if (device_path == NULL) {
1395 BT_ERR("No paired device");
1396 result = BLUETOOTH_ERROR_NOT_PAIRED;
1400 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1401 NULL, BT_BLUEZ_NAME,
1402 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1403 g_free(device_path);
1404 if (device_proxy == NULL) {
1405 result = BLUETOOTH_ERROR_INTERNAL;
1409 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1410 g_variant_new("(s)", ""),
1411 G_DBUS_CALL_FLAGS_NONE,
1412 BT_MAX_DBUS_TIMEOUT,
1414 (GAsyncReadyCallback)__bt_discover_cb,
1417 searching_info->device_proxy = device_proxy;
1419 return BLUETOOTH_ERROR_NONE;
1422 g_free(searching_info->addr);
1423 g_free(searching_info);
1424 searching_info = NULL;
1428 int _bt_cancel_search_device(void)
1430 GVariant *ret = NULL;
1433 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1435 if (searching_info->device_proxy) {
1436 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1438 G_DBUS_CALL_FLAGS_NONE,
1443 g_variant_unref(ret);
1445 __bt_cancel_search_service_done();
1447 return BLUETOOTH_ERROR_NONE;
1450 int _bt_set_alias(bluetooth_device_address_t *device_address,
1453 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1454 gchar *device_path = NULL;
1455 GDBusProxy *adapter_proxy;
1456 GDBusProxy *device_proxy;
1457 GVariant *ret = NULL;
1458 GError *error = NULL;
1459 GDBusConnection *conn;
1461 BT_CHECK_PARAMETER(device_address, return);
1462 BT_CHECK_PARAMETER(alias, return);
1464 adapter_proxy = _bt_get_adapter_proxy();
1465 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1467 conn = _bt_gdbus_get_system_gconn();
1468 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1470 _bt_convert_addr_type_to_string(address, device_address->addr);
1472 device_path = _bt_get_device_object_path(address);
1474 if (device_path == NULL) {
1475 BT_ERR("No paired device");
1476 return BLUETOOTH_ERROR_NOT_PAIRED;
1479 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1480 NULL, BT_BLUEZ_NAME,
1481 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1483 g_free(device_path);
1484 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1486 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1487 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1488 G_DBUS_CALL_FLAGS_NONE,
1493 g_variant_unref(ret);
1495 g_object_unref(device_proxy);
1498 BT_ERR("SetProperty error: [%s]", error->message);
1499 g_error_free(error);
1500 return BLUETOOTH_ERROR_INTERNAL;
1503 return BLUETOOTH_ERROR_NONE;
1506 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1509 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1510 gchar *device_path = NULL;
1511 GDBusProxy *device_proxy;
1512 gboolean previous_value;
1513 GError *error = NULL;
1514 GDBusConnection *conn;
1515 GVariant *result = NULL;
1516 GVariant *temp = NULL;
1517 int ret = BLUETOOTH_ERROR_NONE;
1519 BT_CHECK_PARAMETER(device_address, return);
1521 conn = _bt_gdbus_get_system_gconn();
1522 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1524 _bt_convert_addr_type_to_string(address, device_address->addr);
1526 device_path = _bt_get_device_object_path(address);
1528 if (device_path == NULL) {
1529 BT_ERR("No paired device");
1530 return BLUETOOTH_ERROR_NOT_PAIRED;
1533 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1534 NULL, BT_BLUEZ_NAME,
1535 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1537 g_free(device_path);
1538 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1540 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1541 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1542 G_DBUS_CALL_FLAGS_NONE,
1546 if (error != NULL) {
1547 BT_ERR("Getting property failed: [%s]\n", error->message);
1548 g_error_free(error);
1549 g_object_unref(device_proxy);
1550 return BLUETOOTH_ERROR_INTERNAL;
1553 g_variant_get(result, "(v)", &temp);
1554 previous_value = g_variant_get_boolean(temp);
1555 g_variant_unref(temp);
1556 g_variant_unref(result);
1557 /* If the input is same with previous value, return error. */
1558 if (previous_value == authorize) {
1559 BT_ERR("Same value: %d", previous_value);
1560 g_object_unref(device_proxy);
1561 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1565 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1566 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1567 G_DBUS_CALL_FLAGS_NONE,
1572 g_object_unref(device_proxy);
1574 BT_ERR("SetProperty error: [%s]", error->message);
1575 g_error_free(error);
1576 ret = BLUETOOTH_ERROR_INTERNAL;
1580 g_variant_unref(result);
1585 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1586 gboolean *is_connected)
1588 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1589 char *object_path = NULL;
1591 GDBusProxy *device_proxy;
1592 GError *error = NULL;
1594 GVariant *tmp_value;
1595 GDBusConnection *conn;
1596 GVariant *result = NULL;
1597 int ret = BLUETOOTH_ERROR_NONE;
1599 BT_CHECK_PARAMETER(device_address, return);
1601 conn = _bt_gdbus_get_system_gconn();
1602 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1604 _bt_convert_addr_type_to_string(address, device_address->addr);
1606 object_path = _bt_get_device_object_path(address);
1607 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1609 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1610 NULL, BT_BLUEZ_NAME, object_path,
1611 BT_PROPERTIES_INTERFACE, NULL, NULL);
1612 g_free(object_path);
1613 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1615 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1616 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1617 G_DBUS_CALL_FLAGS_NONE,
1621 if (result == NULL) {
1622 if (error != NULL) {
1623 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1624 g_error_free(error);
1626 g_object_unref(device_proxy);
1627 return BLUETOOTH_ERROR_INTERNAL;
1630 g_variant_get(result , "(@a{sv})", &value);
1631 g_variant_unref(result);
1633 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1634 if (tmp_value == NULL) {
1635 g_object_unref(device_proxy);
1636 g_variant_unref(value);
1637 return BLUETOOTH_ERROR_INTERNAL;
1640 *is_connected = g_variant_get_boolean(tmp_value);
1642 BT_DBG("gatt is connected : %d", *is_connected);
1644 g_variant_unref(tmp_value);
1645 g_variant_unref(value);
1646 g_object_unref(device_proxy);
1651 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1652 int connection_type, gboolean *is_connected)
1654 char *object_path = NULL;
1655 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1656 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1657 GDBusProxy *device_proxy = NULL;
1658 GDBusProxy *adapter_proxy = NULL;
1659 GDBusConnection *conn;
1660 GError *error = NULL;
1661 GVariant *tmp_value = NULL;
1662 GVariant *value = NULL;
1663 GVariant *result = NULL;
1666 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1667 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1669 _bt_convert_addr_type_to_string(address, device_address->addr);
1670 _bt_convert_addr_string_to_secure_string(secure_address, address);
1672 *is_connected = FALSE;
1673 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1675 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1676 return _bt_rfcomm_is_device_connected(device_address,
1678 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1679 return _bt_is_gatt_connected(device_address, is_connected);
1680 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1681 return _bt_pbap_is_connected(device_address, is_connected);
1683 adapter_proxy = _bt_get_adapter_proxy();
1684 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1686 conn = _bt_gdbus_get_system_gconn();
1687 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1689 _bt_convert_addr_type_to_string(address, device_address->addr);
1691 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1692 object_path = _bt_get_adapter_path();
1693 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1694 NULL, BT_BLUEZ_NAME,
1695 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1696 g_free(object_path);
1697 if (device_proxy == NULL) {
1698 BT_DBG("Device don't have this service");
1699 return BLUETOOTH_ERROR_INTERNAL;
1702 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1703 g_variant_new("(s)", address),
1704 G_DBUS_CALL_FLAGS_NONE,
1709 if (result == NULL) {
1710 BT_ERR("[GetProperties] Error occured in Proxy call");
1712 BT_ERR("%s", error->message);
1713 g_error_free(error);
1715 *is_connected = FALSE;
1716 g_object_unref(device_proxy);
1717 return BLUETOOTH_ERROR_NONE;
1719 g_variant_get(result , "(@a{sv})", &value);
1720 g_variant_unref(result);
1723 tmp_value = g_variant_lookup_value(value,
1725 G_VARIANT_TYPE_BOOLEAN);
1727 *is_connected = g_variant_get_boolean(tmp_value);
1728 g_variant_unref(tmp_value);
1730 g_variant_unref(value);
1732 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1733 return _bt_is_network_connected(_bt_get_net_conn(),
1734 device_address->addr, is_connected);
1736 uuid = _bt_get_profile_uuid128(connection_type);
1738 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1739 return BLUETOOTH_ERROR_INTERNAL;
1741 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1743 object_path = _bt_get_device_object_path(address);
1745 BT_ERR("object_path is NULL");
1747 return BLUETOOTH_ERROR_NOT_PAIRED;
1750 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1751 NULL, BT_BLUEZ_NAME,
1752 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1753 g_free(object_path);
1754 if (device_proxy == NULL) {
1755 BT_DBG("Device don't have this service");
1757 return BLUETOOTH_ERROR_INTERNAL;
1760 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1761 g_variant_new("(s)", uuid),
1762 G_DBUS_CALL_FLAGS_NONE,
1767 if (result == NULL) {
1768 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1770 BT_ERR("%s", error->message);
1771 if (g_strrstr(error->message, "Not Connected"))
1772 BT_DBG("Not connected");
1773 g_error_free(error);
1775 *is_connected = FALSE;
1776 g_object_unref(device_proxy);
1778 return BLUETOOTH_ERROR_NONE;
1780 g_variant_get(result, "(b)", is_connected);
1782 g_variant_unref(result);
1785 g_object_unref(device_proxy);
1786 return BLUETOOTH_ERROR_NONE;
1789 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1790 bluetooth_connected_link_t *connected)
1792 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1793 char *object_path = NULL;
1795 GDBusProxy *device_proxy;
1796 GError *error = NULL;
1797 GDBusConnection *conn;
1798 GVariant *tmp_value = NULL;
1799 GVariant *value = NULL;
1800 GVariant *result = NULL;
1802 BT_CHECK_PARAMETER(device_address, return);
1804 conn = _bt_gdbus_get_system_gconn();
1805 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1807 _bt_convert_addr_type_to_string(address, device_address->addr);
1809 object_path = _bt_get_device_object_path(address);
1810 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1812 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1813 NULL, BT_BLUEZ_NAME,
1814 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1815 g_free(object_path);
1816 if (device_proxy == NULL) {
1817 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1818 return BLUETOOTH_ERROR_NONE;
1821 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1822 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1823 G_DBUS_CALL_FLAGS_NONE,
1828 if (error != NULL) {
1829 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1830 g_error_free(error);
1831 g_object_unref(device_proxy);
1832 return BLUETOOTH_ERROR_INTERNAL;
1835 g_variant_get(result , "(@a{sv})", &value);
1836 g_variant_unref(result);
1838 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1839 if (tmp_value != NULL) {
1840 *connected = g_variant_get_byte(tmp_value);
1841 g_variant_unref(tmp_value);
1842 g_object_unref(device_proxy);
1843 return BLUETOOTH_ERROR_NONE;
1845 BT_ERR("g_variant value is NULL");
1846 return BLUETOOTH_ERROR_INTERNAL;
1850 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1854 GVariant *out_param1;
1856 int result = BLUETOOTH_ERROR_NONE;
1857 bt_function_data_t *func_data = user_data;
1858 request_info_t *req_info = NULL;
1859 bluetooth_device_address_t device_addr = { {0} };
1861 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1862 g_object_unref(proxy);
1864 if (reply == NULL) {
1865 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1867 BT_ERR("Error: %s", err->message);
1868 g_clear_error(&err);
1870 result = BLUETOOTH_ERROR_INTERNAL;
1872 g_variant_unref(reply);
1875 if (func_data == NULL) {
1876 BT_ERR("func_data is NULL");
1880 req_info = _bt_get_request_info(func_data->req_id);
1881 if (req_info == NULL) {
1882 BT_ERR("req_info is NULL");
1886 if (req_info->context == NULL) {
1887 BT_ERR("req_info->context is NULL");
1891 _bt_convert_addr_string_to_type(device_addr.addr,
1892 (const char *)func_data->address);
1894 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1895 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1898 g_dbus_method_invocation_return_value(req_info->context,
1899 g_variant_new("(iv)", result, out_param1));
1903 _bt_delete_request_list(req_info->req_id);
1906 g_free(func_data->address);
1911 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1912 gboolean auto_connect)
1914 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1915 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1916 gchar *device_path = NULL;
1917 GDBusProxy *device_proxy = NULL;
1918 GDBusConnection *conn;
1919 int ret = BLUETOOTH_ERROR_NONE;
1920 bt_function_data_t *func_data;
1922 BT_CHECK_PARAMETER(bd_addr, return);
1924 conn = _bt_gdbus_get_system_gconn();
1925 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1927 _bt_convert_addr_type_to_string(device_address,
1928 (unsigned char *)bd_addr->addr);
1929 device_path = _bt_get_device_object_path(device_address);
1930 if (device_path == NULL) {
1931 BT_ERR_C("device_path NULL : [%s]", device_address);
1932 ret = BLUETOOTH_ERROR_INTERNAL;
1936 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1937 NULL, BT_BLUEZ_NAME,
1938 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1939 g_free(device_path);
1940 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1942 func_data = g_malloc0(sizeof(bt_function_data_t));
1943 func_data->address = g_strdup(device_address);
1944 if (func_data->address == NULL) {
1945 BT_ERR("Unable to allocate memory for address");
1946 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1950 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1951 BT_INFO("Connect LE [%s]", secure_address);
1953 func_data->req_id = req_id;
1955 g_dbus_proxy_call(device_proxy, "ConnectLE",
1956 g_variant_new("(b)", auto_connect),
1957 G_DBUS_CALL_FLAGS_NONE,
1958 BT_MAX_DBUS_TIMEOUT,
1960 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1966 g_object_unref(device_proxy);
1968 g_free(func_data->address);
1974 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1976 BT_INFO("Try to initiate pending LE connection");
1978 pending_le_conn_timer_id = 0;
1980 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1981 &pending_le_conn_info->bd_addr,
1982 pending_le_conn_info->auto_connect);
1984 g_free(pending_le_conn_info);
1985 pending_le_conn_info = NULL;
1990 void _bt_pending_connect_le_device(void)
1992 if (pending_le_conn_timer_id > 0) {
1993 g_source_remove(pending_le_conn_timer_id);
1994 __bt_connect_le_timer_cb(NULL);
1998 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1999 gboolean auto_connect)
2001 int ret = BLUETOOTH_ERROR_NONE;
2003 BT_CHECK_PARAMETER(bd_addr, return);
2005 ret = _bt_hold_current_advertising();
2006 if (ret == BLUETOOTH_ERROR_NONE) {
2007 BT_INFO("Current advertising is held");
2008 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2009 pending_le_conn_info->req_id = req_id;
2010 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2011 BLUETOOTH_ADDRESS_LENGTH);
2012 pending_le_conn_info->auto_connect = auto_connect;
2014 pending_le_conn_timer_id =
2015 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2017 return BLUETOOTH_ERROR_NONE;
2020 BT_ERR("Unable to hold advertising");
2022 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2025 int _bt_disconnect_le_device(int req_id,
2026 const bluetooth_device_address_t *bd_addr)
2028 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2029 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2031 GDBusProxy *device_proxy;
2032 GDBusConnection *conn;
2033 int ret = BLUETOOTH_ERROR_NONE;
2034 bt_function_data_t *func_data;
2036 BT_CHECK_PARAMETER(bd_addr, return);
2038 conn = _bt_gdbus_get_system_gconn();
2039 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2041 _bt_convert_addr_type_to_string(device_address,
2042 (unsigned char *)bd_addr->addr);
2043 device_path = _bt_get_device_object_path(device_address);
2044 if (device_path == NULL) {
2045 BT_DBG("device_path NULL");
2046 ret = BLUETOOTH_ERROR_INTERNAL;
2050 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2052 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2053 NULL, BT_BLUEZ_NAME,
2054 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2055 g_free(device_path);
2056 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2058 func_data = g_malloc0(sizeof(bt_function_data_t));
2059 func_data->address = g_strdup(device_address);
2060 if (func_data->address == NULL) {
2061 BT_ERR("Unable to allocate memory for address");
2062 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2066 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2067 BT_INFO("Disconnect LE [%s]", secure_address);
2069 func_data->req_id = req_id;
2071 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2073 G_DBUS_CALL_FLAGS_NONE,
2074 BT_MAX_DBUS_TIMEOUT,
2076 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2081 g_object_unref(device_proxy);
2083 g_free(func_data->address);
2089 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2091 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2092 gchar *device_path = NULL;
2093 GError *error = NULL;
2094 GDBusProxy *device_proxy = NULL;
2095 GDBusProxy *adapter_proxy;
2096 GDBusConnection *conn;
2097 int ret = BLUETOOTH_ERROR_NONE;
2099 BT_CHECK_PARAMETER(bd_addr, return);
2101 _bt_convert_addr_type_to_string(device_address,
2102 (unsigned char *)bd_addr->addr);
2104 conn = _bt_gdbus_get_system_gconn();
2105 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2107 adapter_proxy = _bt_get_adapter_proxy();
2108 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2110 device_path = _bt_get_device_object_path(device_address);
2111 if (device_path == NULL) {
2112 BT_DBG("device_path NULL");
2113 ret = BLUETOOTH_ERROR_INTERNAL;
2117 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2119 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2120 NULL, BT_BLUEZ_NAME,
2121 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2122 g_free(device_path);
2123 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2125 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2127 G_DBUS_CALL_FLAGS_NONE,
2132 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2133 g_error_free(error);
2134 g_object_unref(device_proxy);
2135 return BLUETOOTH_ERROR_INTERNAL;
2137 g_object_unref(device_proxy);
2139 /* IPSP daemon launch */
2140 GDBusProxy *ipsp_proxy;
2142 ipsp_proxy = _bt_get_ipsp_proxy();
2143 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2145 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2146 NULL, G_DBUS_CALL_FLAGS_NONE,
2147 -1, NULL, NULL, NULL);
2152 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2154 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2155 gchar *device_path = NULL;
2156 GError *error = NULL;
2157 GDBusProxy *device_proxy = NULL;
2158 GDBusProxy *adapter_proxy;
2159 GDBusConnection *conn;
2160 int ret = BLUETOOTH_ERROR_NONE;
2162 BT_CHECK_PARAMETER(bd_addr, return);
2164 _bt_convert_addr_type_to_string(device_address,
2165 (unsigned char *)bd_addr->addr);
2167 conn = _bt_gdbus_get_system_gconn();
2168 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2170 adapter_proxy = _bt_get_adapter_proxy();
2171 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2173 device_path = _bt_get_device_object_path(device_address);
2174 if (device_path == NULL) {
2175 BT_DBG("device_path NULL");
2176 ret = BLUETOOTH_ERROR_INTERNAL;
2180 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2182 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2183 NULL, BT_BLUEZ_NAME,
2184 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2185 g_free(device_path);
2186 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2188 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2190 G_DBUS_CALL_FLAGS_NONE,
2195 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2196 g_error_free(error);
2197 g_object_unref(device_proxy);
2198 return BLUETOOTH_ERROR_INTERNAL;
2201 g_object_unref(device_proxy);
2206 int _bt_connect_profile(char *address, char *uuid,
2207 void *cb, gpointer func_data)
2211 GDBusConnection *conn;
2212 GDBusProxy *adapter_proxy;
2213 GVariant *result = NULL;
2214 GError *error = NULL;
2216 conn = _bt_gdbus_get_system_gconn();
2217 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2219 object_path = _bt_get_device_object_path(address);
2220 if (object_path == NULL) {
2221 BT_ERR("No searched device");
2223 adapter_proxy = _bt_get_adapter_proxy();
2224 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2226 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2227 g_variant_new("(s)", address),
2228 G_DBUS_CALL_FLAGS_NONE,
2233 if (error != NULL) {
2234 BT_ERR("CreateDevice Fail: %s", error->message);
2235 g_error_free(error);
2238 g_variant_unref(result);
2240 object_path = _bt_get_device_object_path(address);
2242 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2244 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2245 NULL, BT_BLUEZ_NAME,
2246 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2247 g_free(object_path);
2248 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2250 g_dbus_proxy_call(proxy, "ConnectProfile",
2251 g_variant_new("(s)", uuid),
2252 G_DBUS_CALL_FLAGS_NONE,
2253 BT_MAX_DBUS_TIMEOUT,
2255 (GAsyncReadyCallback)cb,
2258 return BLUETOOTH_ERROR_NONE;
2261 int _bt_disconnect_all(char *address)
2263 int ret = BLUETOOTH_ERROR_NONE;
2266 GDBusConnection *conn;
2267 GVariant *result = NULL;
2271 conn = _bt_gdbus_get_system_gconn();
2272 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2274 object_path = _bt_get_device_object_path(address);
2275 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2277 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2278 NULL, BT_BLUEZ_NAME,
2279 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2280 g_free(object_path);
2281 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2283 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2285 G_DBUS_CALL_FLAGS_NONE,
2290 BT_ERR("Dbus Call Error:[%s]", err->message);
2292 ret = BLUETOOTH_ERROR_INTERNAL;
2295 g_object_unref(proxy);
2297 g_variant_unref(result);
2302 int _bt_disconnect_profile(char *address, char *uuid,
2303 void *cb, gpointer func_data)
2307 GDBusConnection *conn;
2309 conn = _bt_gdbus_get_system_gconn();
2310 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2312 object_path = _bt_get_device_object_path(address);
2313 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2315 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2316 NULL, BT_BLUEZ_NAME,
2317 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2318 g_free(object_path);
2319 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2321 g_dbus_proxy_call(proxy, "DisconnectProfile",
2322 g_variant_new("(s)", uuid),
2323 G_DBUS_CALL_FLAGS_NONE,
2324 BT_MAX_DBUS_TIMEOUT,
2326 (GAsyncReadyCallback)cb,
2329 return BLUETOOTH_ERROR_NONE;
2332 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2333 int low_threshold, int in_range_threshold, int high_threshold)
2335 int ret = BLUETOOTH_ERROR_NONE;
2337 GVariant *result = NULL;
2338 GError *error = NULL;
2339 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2341 BT_CHECK_PARAMETER(bd_addr, return);
2342 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2343 bd_addr->addr[0], bd_addr->addr[1],
2344 bd_addr->addr[2], bd_addr->addr[3],
2345 bd_addr->addr[4], bd_addr->addr[5],
2347 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2348 in_range_threshold, high_threshold);
2350 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2352 proxy = _bt_get_adapter_proxy();
2353 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2355 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2356 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2357 G_DBUS_CALL_FLAGS_NONE,
2361 if (error != NULL) {
2362 BT_ERR("Dbus Call Error:[%s]", error->message);
2363 g_error_free(error);
2364 ret = BLUETOOTH_ERROR_INTERNAL;
2368 g_variant_unref(result);
2373 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2376 int ret = BLUETOOTH_ERROR_NONE;
2378 GVariant *result = NULL;
2379 GError *error = NULL;
2380 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2382 BT_CHECK_PARAMETER(bd_addr, return);
2383 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2384 bd_addr->addr[0], bd_addr->addr[1],
2385 bd_addr->addr[2], bd_addr->addr[3],
2386 bd_addr->addr[4], bd_addr->addr[5],
2389 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2391 proxy = _bt_get_adapter_proxy();
2392 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2394 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2395 g_variant_new("(si)", address, link_type),
2396 G_DBUS_CALL_FLAGS_NONE,
2401 if (error != NULL) {
2402 BT_ERR("Dbus Call Error:[%s]", error->message);
2403 g_error_free(error);
2404 ret = BLUETOOTH_ERROR_INTERNAL;
2408 g_variant_unref(result);
2413 int _bt_le_conn_update(unsigned char *device_address,
2414 float interval_min, float interval_max,
2415 guint16 latency, guint16 time_out)
2417 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2418 gchar *device_path = NULL;
2419 GError *error = NULL;
2420 GDBusProxy *device_proxy = NULL;
2421 GDBusConnection *conn;
2423 guint32 min, max, to;
2424 guint32 min_supervision_to;
2425 int ret = BLUETOOTH_ERROR_NONE;
2427 BT_CHECK_PARAMETER(device_address, return);
2429 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2430 interval_min, interval_max, latency, time_out);
2432 if (interval_min > interval_max ||
2433 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2434 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2435 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2439 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2440 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2441 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2445 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2446 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2451 * The Supervision_Timeout in milliseconds shall be larger than
2452 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2453 * where Conn_Interval_Max is given in milliseconds.
2455 min_supervision_to = (1 + latency) * interval_max * 2;
2456 if (time_out <= min_supervision_to) {
2457 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2461 _bt_convert_addr_type_to_string(address, device_address);
2463 BT_DBG("Remote device address: %s", address);
2465 device_path = _bt_get_device_object_path(address);
2467 if (device_path == NULL) {
2468 BT_ERR("device_path NULL");
2469 ret = BLUETOOTH_ERROR_INTERNAL;
2473 conn = _bt_gdbus_get_system_gconn();
2475 BT_ERR("conn NULL");
2476 ret = BLUETOOTH_ERROR_INTERNAL;
2480 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2481 NULL, BT_BLUEZ_NAME,
2482 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2484 g_free(device_path);
2485 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2487 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2488 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2489 to = time_out / BT_LE_CONN_TO_SPLIT;
2491 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2492 g_variant_new("(uuuu)", min, max, latency, to),
2493 G_DBUS_CALL_FLAGS_NONE,
2498 g_object_unref(device_proxy);
2499 if (reply == NULL) {
2501 BT_ERR("Error %s[%s]", error->message, address);
2502 if (g_strrstr(error->message, "In Progress"))
2503 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2505 ret = BLUETOOTH_ERROR_INTERNAL;
2506 g_error_free(error);
2510 g_variant_unref(reply);
2516 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2517 bluetooth_device_pin_code_t *pin_code)
2519 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2521 bt_pin_code_info_t *pin_info = NULL;
2523 BT_CHECK_PARAMETER(device_address, return);
2524 BT_CHECK_PARAMETER(pin_code, return);
2525 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2526 BLUETOOTH_ERROR_NO_RESOURCES);
2528 _bt_convert_addr_type_to_string(address, device_address->addr);
2530 for (l = pin_info_list; l != NULL; l = l->next) {
2533 if (g_strcmp0(pin_info->address, address) == 0) {
2534 g_free(pin_info->pin_code);
2535 pin_info->pin_code = g_strdup(pin_code->pin_code);
2536 return BLUETOOTH_ERROR_NONE;
2540 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2541 pin_info->address = g_strdup(address);
2542 pin_info->pin_code = g_strdup(pin_code->pin_code);
2543 pin_info_list = g_slist_append(pin_info_list, pin_info);
2545 return BLUETOOTH_ERROR_NONE;
2548 gint __bt_compare_address(gpointer *a, gpointer *b)
2550 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2551 char *address = (char *)b;
2552 return g_strcmp0(pin_info->address, address);
2555 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2557 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2559 bt_pin_code_info_t *pin_info = NULL;
2561 BT_CHECK_PARAMETER(device_address, return);
2563 _bt_convert_addr_type_to_string(address, device_address->addr);
2565 l = g_slist_find_custom(pin_info_list, address,
2566 (GCompareFunc)__bt_compare_address);
2570 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2571 g_free(pin_info->address);
2572 g_free(pin_info->pin_code);
2576 return BLUETOOTH_ERROR_NONE;
2579 int _bt_get_device_pin_code(const char *address, char *pin_code)
2583 BT_CHECK_PARAMETER(address, return);
2584 BT_CHECK_PARAMETER(pin_code, return);
2586 for (l = pin_info_list; l != NULL; l = l->next) {
2587 bt_pin_code_info_t *pin_info = l->data;
2589 if (g_strcmp0(pin_info->address, address) == 0) {
2590 g_strlcpy(pin_code, pin_info->pin_code,
2591 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2593 return BLUETOOTH_ERROR_NONE;
2597 return BLUETOOTH_ERROR_NOT_FOUND;
2600 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2601 bluetooth_le_connection_param_t *param)
2604 return BLUETOOTH_ERROR_INVALID_PARAM;
2606 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2607 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2608 return BLUETOOTH_ERROR_INVALID_PARAM;
2610 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2613 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2614 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2615 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2616 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2617 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2620 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2621 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2622 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2623 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2624 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2627 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2628 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2629 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2630 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2631 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2635 BT_ERR("Unhandled mode : %d", mode);
2639 return BLUETOOTH_ERROR_NONE;
2642 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2643 guint trusted_profile_flag, guint *trusted)
2649 case TRUSTED_PROFILE_PBAP:
2650 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2651 trust_profile = trusted_profile_flag & (1 << 1);
2653 return BLUETOOTH_ERROR_NOT_SUPPORT;
2655 case TRUSTED_PROFILE_MAP:
2656 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2657 trust_profile = trusted_profile_flag & (1 << 3);
2659 return BLUETOOTH_ERROR_NOT_SUPPORT;
2661 case TRUSTED_PROFILE_SAP:
2662 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2663 trust_profile = trusted_profile_flag & (1 << 5);
2665 return BLUETOOTH_ERROR_NOT_SUPPORT;
2667 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2668 *trusted = trusted_profile_flag;
2669 return BLUETOOTH_ERROR_NONE;
2671 return BLUETOOTH_ERROR_NOT_SUPPORT;
2677 return BLUETOOTH_ERROR_NONE;
2680 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2681 guint restricted_profile_flag, guint *restricted)
2683 int restrict_profile;
2684 *restricted = FALSE;
2687 case RESTRICTED_PROFILE_HFP_HS:
2688 restrict_profile = restricted_profile_flag & (1 << 0);
2690 case RESTRICTED_PROFILE_A2DP:
2691 restrict_profile = restricted_profile_flag & (1 << 2);
2694 return BLUETOOTH_ERROR_NOT_SUPPORT;
2697 if (restrict_profile)
2700 return BLUETOOTH_ERROR_NONE;
2703 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2706 case TRUSTED_PROFILE_PBAP:
2707 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2708 case TRUSTED_PROFILE_MAP:
2709 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2710 case TRUSTED_PROFILE_SAP:
2711 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2712 case TRUSTED_PROFILE_ALL:
2719 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2722 case RESTRICTED_PROFILE_HFP_HS:
2723 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2724 case RESTRICTED_PROFILE_A2DP:
2725 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2731 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2733 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2734 return TRUSTED_PROFILE_PBAP;
2735 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2736 return TRUSTED_PROFILE_MAP;
2737 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2738 return TRUSTED_PROFILE_SAP;
2740 return 0; /* 0 - Unknown Profile */
2743 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2744 bluetooth_trusted_profile_t profile, gboolean trust)
2746 int ret = BLUETOOTH_ERROR_NONE;
2747 GDBusConnection *conn;
2749 GError *error = NULL;
2750 char *device_path = NULL;
2752 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2755 BT_CHECK_PARAMETER(bd_addr, return);
2756 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2757 bd_addr->addr[0], bd_addr->addr[1],
2758 bd_addr->addr[2], bd_addr->addr[3],
2759 bd_addr->addr[4], bd_addr->addr[5],
2762 conn = _bt_gdbus_get_system_gconn();
2763 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2765 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2767 device_path = _bt_get_device_object_path(address);
2768 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2770 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2771 NULL, BT_BLUEZ_NAME, device_path,
2772 BT_DEVICE_INTERFACE, NULL, NULL);
2774 g_free(device_path);
2775 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2777 uuid = _bt_get_trusted_profile_uuid(profile);
2779 g_object_unref(proxy);
2780 return BLUETOOTH_ERROR_NOT_SUPPORT;
2783 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2784 g_variant_new("(sb)", uuid, trust),
2785 G_DBUS_CALL_FLAGS_NONE, -1,
2787 g_object_unref(proxy);
2789 if (reply == NULL) {
2790 BT_ERR("Failed to Set Profile Trusted");
2791 ret = BLUETOOTH_ERROR_INTERNAL;
2793 BT_ERR("Error %s[%s]", error->message, address);
2794 g_error_free(error);
2798 g_variant_unref(reply);
2805 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2806 bluetooth_trusted_profile_t profile, guint *trust)
2808 int ret = BLUETOOTH_ERROR_NONE;
2809 GDBusConnection *conn;
2811 GError *error = NULL;
2812 char *device_path = NULL;
2813 guint trusted_profile_flag;
2814 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2817 BT_CHECK_PARAMETER(bd_addr, return);
2818 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2819 bd_addr->addr[0], bd_addr->addr[1],
2820 bd_addr->addr[2], bd_addr->addr[3],
2821 bd_addr->addr[4], bd_addr->addr[5],
2824 conn = _bt_gdbus_get_system_gconn();
2825 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2827 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2829 device_path = _bt_get_device_object_path(address);
2830 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2832 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2833 NULL, BT_BLUEZ_NAME, device_path,
2834 BT_PROPERTIES_INTERFACE, NULL, NULL);
2836 g_free(device_path);
2837 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2839 reply = g_dbus_proxy_call_sync(proxy, "Get",
2840 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2841 G_DBUS_CALL_FLAGS_NONE, -1,
2843 g_object_unref(proxy);
2845 if (reply == NULL) {
2846 BT_ERR("Failed to Get Profile Trusted");
2847 ret = BLUETOOTH_ERROR_INTERNAL;
2849 BT_ERR("Error %s[%s]", error->message, address);
2850 g_error_free(error);
2855 g_variant_get(reply, "(v)", &temp);
2856 trusted_profile_flag = g_variant_get_uint32(temp);
2857 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2859 ret = _bt_get_trusted_profile_from_flag(profile,
2860 trusted_profile_flag, trust);
2861 g_variant_unref(temp);
2862 g_variant_unref(reply);
2865 BT_DBG("TRUST %d", *trust);
2869 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2870 bluetooth_restricted_profile_t profile, gboolean restricted)
2872 int ret = BLUETOOTH_ERROR_NONE;
2873 GDBusConnection *conn;
2875 GError *error = NULL;
2876 char *device_path = NULL;
2878 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2881 BT_CHECK_PARAMETER(bd_addr, return);
2882 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2883 bd_addr->addr[0], bd_addr->addr[1],
2884 bd_addr->addr[2], bd_addr->addr[3],
2885 bd_addr->addr[4], bd_addr->addr[5],
2886 profile, restricted);
2888 conn = _bt_gdbus_get_system_gconn();
2889 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2891 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2893 device_path = _bt_get_device_object_path(address);
2894 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2896 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2897 NULL, BT_BLUEZ_NAME, device_path,
2898 BT_DEVICE_INTERFACE, NULL, NULL);
2900 g_free(device_path);
2901 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2903 uuid = _bt_get_restricted_profile_uuid(profile);
2905 g_object_unref(proxy);
2906 return BLUETOOTH_ERROR_NOT_SUPPORT;
2909 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2910 g_variant_new("(sb)", uuid, restricted),
2911 G_DBUS_CALL_FLAGS_NONE, -1,
2913 g_object_unref(proxy);
2915 if (reply == NULL) {
2916 BT_ERR("Failed to Set Profile Restricted");
2917 ret = BLUETOOTH_ERROR_INTERNAL;
2919 BT_ERR("Error %s[%s]", error->message, address);
2920 g_error_free(error);
2924 g_variant_unref(reply);
2931 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2932 bluetooth_restricted_profile_t profile, guint *restricted)
2934 int ret = BLUETOOTH_ERROR_NONE;
2935 GDBusConnection *conn;
2937 GError *error = NULL;
2938 char *device_path = NULL;
2939 guint restricted_profile_flag;
2940 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2943 BT_CHECK_PARAMETER(bd_addr, return);
2944 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2945 bd_addr->addr[0], bd_addr->addr[1],
2946 bd_addr->addr[2], bd_addr->addr[3],
2947 bd_addr->addr[4], bd_addr->addr[5],
2948 profile, *restricted);
2950 conn = _bt_gdbus_get_system_gconn();
2951 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2953 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2955 device_path = _bt_get_device_object_path(address);
2956 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2958 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2959 NULL, BT_BLUEZ_NAME, device_path,
2960 BT_PROPERTIES_INTERFACE, NULL, NULL);
2962 g_free(device_path);
2963 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2965 reply = g_dbus_proxy_call_sync(proxy, "Get",
2966 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2967 G_DBUS_CALL_FLAGS_NONE, -1,
2969 g_object_unref(proxy);
2971 if (reply == NULL) {
2972 BT_ERR("Failed to Get Profile Restricted");
2973 ret = BLUETOOTH_ERROR_INTERNAL;
2975 BT_ERR("Error %s[%s]", error->message, address);
2976 g_error_free(error);
2981 g_variant_get(reply, "(v)", &temp);
2982 restricted_profile_flag = g_variant_get_uint32(temp);
2983 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2985 ret = _bt_get_restricted_profile_from_flag(profile,
2986 restricted_profile_flag, restricted);
2987 g_variant_unref(temp);
2988 g_variant_unref(reply);
2991 BT_DBG("TRUST %d", *restricted);
2995 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2999 GVariant *out_param1;
3000 request_info_t *req_info;
3001 GVariant *val = NULL;
3002 GVariant *param = NULL;
3005 bluetooth_device_address_t device_addr = { {0} };
3006 int result = BLUETOOTH_ERROR_NONE;
3009 val = g_dbus_proxy_call_finish(proxy, res, &err);
3011 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3012 if (req_info == NULL) {
3013 BT_ERR("req_info == NULL");
3014 g_object_unref(proxy);
3015 att_mtu_req_info->device_proxy = NULL;
3020 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3022 if (g_strrstr(err->message, "NotSupported")) {
3023 BT_INFO("Connection Not Supported");
3024 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3025 } else if (g_strrstr(err->message, "NotConnected")) {
3026 BT_INFO("Not connected");
3027 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3028 } else if (g_strrstr(err->message, "InvalidArguments")) {
3029 BT_INFO("Not connected");
3030 result = BLUETOOTH_ERROR_INVALID_PARAM;
3032 BT_DBG("Default case");
3033 result = BLUETOOTH_ERROR_INTERNAL;
3037 g_object_unref(proxy);
3038 att_mtu_req_info->device_proxy = NULL;
3040 if (result != BLUETOOTH_ERROR_NONE)
3044 g_variant_get(val, "(qy)", &mtu, &status);
3045 g_variant_unref(val);
3048 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3050 param = g_variant_new("(isqy)",
3052 att_mtu_req_info->addr,
3056 /* Send the event to application */
3057 _bt_send_event(BT_DEVICE_EVENT,
3058 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3062 if (req_info->context == NULL)
3065 _bt_convert_addr_string_to_type(device_addr.addr,
3066 (const char *)att_mtu_req_info->addr);
3068 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3069 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3070 g_dbus_method_invocation_return_value(req_info->context,
3071 g_variant_new("(iv)", result, out_param1));
3073 _bt_delete_request_list(req_info->req_id);
3078 g_free(att_mtu_req_info->addr);
3079 g_free(att_mtu_req_info);
3080 att_mtu_req_info = NULL;
3085 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3088 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3089 gchar *device_path = NULL;
3090 GDBusProxy *adapter_proxy;
3091 GDBusProxy *device_proxy;
3092 GDBusConnection *conn;
3093 int ret = BLUETOOTH_ERROR_NONE;
3095 BT_CHECK_PARAMETER(device_address, return);
3097 if (att_mtu_req_info) {
3098 BT_ERR("ATT MTU request in progress");
3099 return BLUETOOTH_ERROR_DEVICE_BUSY;
3102 conn = _bt_gdbus_get_system_gconn();
3103 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3105 adapter_proxy = _bt_get_adapter_proxy();
3106 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3108 _bt_convert_addr_type_to_string(address, device_address->addr);
3110 BT_DBG("Remote device address: %s", address);
3112 device_path = _bt_get_device_object_path(address);
3114 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3116 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3117 NULL, BT_BLUEZ_NAME,
3118 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3119 g_free(device_path);
3120 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3122 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3123 att_mtu_req_info->addr = (char *)g_strdup(address);
3124 att_mtu_req_info->req_id = request_id;
3125 att_mtu_req_info->device_proxy = device_proxy;
3127 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3128 g_variant_new("(q)", mtu),
3129 G_DBUS_CALL_FLAGS_NONE,
3130 BT_MAX_DBUS_TIMEOUT,
3132 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3138 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3141 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3142 char *object_path = NULL;
3144 GDBusProxy *device_proxy;
3145 GError *error = NULL;
3147 GVariant *tmp_value;
3148 GDBusConnection *conn;
3149 GVariant *result = NULL;
3150 int ret = BLUETOOTH_ERROR_NONE;
3152 BT_CHECK_PARAMETER(device_address, return);
3154 conn = _bt_gdbus_get_system_gconn();
3155 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3157 _bt_convert_addr_type_to_string(address, device_address->addr);
3159 object_path = _bt_get_device_object_path(address);
3160 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3162 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3163 NULL, BT_BLUEZ_NAME, object_path,
3164 BT_PROPERTIES_INTERFACE, NULL, NULL);
3165 g_free(object_path);
3166 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3168 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3169 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3170 G_DBUS_CALL_FLAGS_NONE,
3174 if (result == NULL) {
3175 if (error != NULL) {
3176 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3177 g_error_free(error);
3179 g_object_unref(device_proxy);
3180 return BLUETOOTH_ERROR_INTERNAL;
3183 g_variant_get(result , "(@a{sv})", &value);
3184 g_variant_unref(result);
3186 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3187 if (tmp_value == NULL) {
3188 g_object_unref(device_proxy);
3189 g_variant_unref(value);
3190 return BLUETOOTH_ERROR_INTERNAL;
3193 *mtu = g_variant_get_uint16(tmp_value);
3195 BT_DBG("ATT MTU : %d", *mtu);
3197 g_variant_unref(tmp_value);
3198 g_variant_unref(value);
3199 g_object_unref(device_proxy);
3204 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3205 bluetooth_device_address_t *id_address)
3207 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3208 gchar *device_path = NULL;
3209 const gchar *idaddress = NULL;
3210 GDBusProxy *device_proxy;
3211 GError *error = NULL;
3212 GVariant *result = NULL;
3213 GDBusConnection *conn;
3214 int ret = BLUETOOTH_ERROR_NONE;
3216 BT_CHECK_PARAMETER(device_address, return);
3218 conn = _bt_gdbus_get_system_gconn();
3219 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3221 _bt_convert_addr_type_to_string(address, device_address->addr);
3223 device_path = _bt_get_device_object_path(address);
3224 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3226 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3227 NULL, BT_BLUEZ_NAME,
3228 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3229 g_free(device_path);
3230 if (!device_proxy) {
3231 BT_ERR("Unable to get proxy");
3232 return BLUETOOTH_ERROR_INTERNAL;
3235 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3237 G_DBUS_CALL_FLAGS_NONE,
3242 if (result == NULL) {
3243 BT_ERR("Failed to get device ID address");
3244 if (error != NULL) {
3245 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3246 g_error_free(error);
3248 g_object_unref(device_proxy);
3249 return BLUETOOTH_ERROR_INTERNAL;
3252 g_variant_get(result , "(s)", &idaddress);
3253 g_variant_unref(result);
3255 if (idaddress == NULL) {
3256 BT_ERR("No paired device");
3257 g_object_unref(device_proxy);
3258 return BLUETOOTH_ERROR_NOT_PAIRED;
3261 BT_DBG("ID Address:%s", idaddress);
3264 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3266 ret = BLUETOOTH_ERROR_INTERNAL;
3268 g_object_unref(device_proxy);
3273 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3275 GapAgentPrivate *agent = _bt_get_adapter_agent();
3276 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3278 if (authentication_reply)
3279 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3281 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3283 BT_DBG("BT_PASSKEY_REPLY");
3284 return BLUETOOTH_ERROR_NONE;
3287 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3289 GapAgentPrivate *agent = _bt_get_adapter_agent();
3290 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3292 if (confirmation_reply)
3293 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3295 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3297 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3298 return BLUETOOTH_ERROR_NONE;