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;
93 gboolean is_device_creating;
94 bt_funcion_data_t *bonding_info;
95 bt_funcion_data_t *searching_info;
96 bt_funcion_data_t *att_mtu_req_info;
98 static GSList *pin_info_list = NULL;
99 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
100 static guint pending_le_conn_timer_id = 0;
102 /* This HID Mouse does not support pairing precedure. need to skip it. */
103 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
105 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
108 static int __bt_retry_bond(void);
111 /*static void __bt_decline_pair_request()
113 GVariant *out_param1;
114 GVariant *out_param2;
115 request_info_t *req_info;
116 bluetooth_device_info_t dev_info;
117 bt_remote_dev_info_t *remote_dev_info;
118 GVariant *uuid_list, *manufacture_data;
123 req_info = _bt_get_request_info(bonding_info->req_id);
124 if (req_info == NULL) {
125 BT_ERR("req_info == NULL");
128 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
130 BT_DBG("bonding_info is NULL");
135 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
136 remote_dev_info->uuids, remote_dev_info->uuid_count,
139 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
140 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
143 param = g_variant_new("isunsbuba{s}na{y})",
144 bonding_info->result,
146 remote_dev_info->class,
147 remote_dev_info->rssi,
148 remote_dev_info->name,
149 remote_dev_info->paired,
150 remote_dev_info->connected,
151 remote_dev_info->trust,
153 remote_dev_info->manufacturer_data_len,
157 //Send the event to application
158 if (remote_dev_info != NULL) {
159 _bt_send_event(BT_ADAPTER_EVENT,
160 BLUETOOTH_EVENT_BONDING_FINISHED,
163 _bt_free_device_info(remote_dev_info);
166 if (req_info->context == NULL)
169 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
170 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
173 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
174 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
176 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
177 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
180 g_dbus_method_invocation_return_value(req_info->context,
181 g_variant_new("@ayi", out_param1, out_param2));
184 _bt_delete_request_list(req_info->req_id);
187 g_free(bonding_info->addr);
188 g_free(bonding_info);
194 #ifdef TIZEN_PROFILE_WEARABLE
195 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
199 retv_if(user_data == NULL, FALSE);
201 b = (bundle *)user_data;
203 ret = syspopup_launch("bt-syspopup", b);
205 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
207 BT_DBG("Hurray!!! Finally Popup launched");
210 return (ret < 0) ? TRUE : FALSE;
213 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
218 GDBusConnection *conn;
220 conn = _bt_gdbus_get_system_gconn();
228 bundle_add(b, "event-type", "unable-to-pairing");
230 if (result == BLUETOOTH_ERROR_TIMEOUT)
231 bundle_add(b, "error", "timeout");
232 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
233 bundle_add(b, "error", "authfailed");
235 bundle_add(b, "error", "error");
237 ret = syspopup_launch("bt-syspopup", b);
239 BT_ERR("Popup launch failed...retry %d \n", ret);
240 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
251 gboolean _bt_is_device_creating(void)
253 return is_device_creating;
256 gboolean _bt_is_bonding_device_address(const char *address)
258 if (bonding_info == NULL || bonding_info->addr == NULL)
261 if (g_strcmp0(bonding_info->addr, address) == 0) {
262 BT_DBG("[%s] is bonding device", address);
266 BT_DBG("[%s] is NOT bonding device", address);
270 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
272 ret_if(bonding_info == NULL);
273 bonding_info->is_autopair = is_autopair;
276 void __bt_cancel_search_service_done(void)
278 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
279 request_info_t *req_info;
280 bluetooth_device_info_t dev_info;
281 GVariant *out_param1;
283 ret_if(searching_info == NULL);
285 req_info = _bt_get_request_info(searching_info->req_id);
286 if (req_info == NULL) {
287 BT_ERR("req_info == NULL");
291 if (req_info->context == NULL)
294 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
295 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
296 searching_info->addr);
298 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
299 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
302 g_dbus_method_invocation_return_value(req_info->context,
303 g_variant_new("(iv)", result, out_param1));
305 _bt_delete_request_list(req_info->req_id);
309 g_free(searching_info->addr);
310 g_free(searching_info);
311 searching_info = NULL;
314 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
316 ret_if(value == NULL);
317 ret_if(info == NULL);
319 info->uuid_count = g_variant_get_size(value);
320 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
323 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
325 char *object_path = NULL;
326 bt_remote_dev_info_t *dev_info;
328 retv_if(address == NULL, NULL);
330 object_path = _bt_get_device_object_path(address);
331 retv_if(object_path == NULL, NULL);
333 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
339 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
340 const char *object_path)
342 bt_remote_dev_info_t *dev_info;
343 GDBusProxy *adapter_proxy;
344 GDBusProxy *device_proxy;
349 GDBusConnection *conn;
350 GError *error = NULL;
351 GVariant *result = NULL;
352 GVariantIter *value_iter;
356 adapter_proxy = _bt_get_adapter_proxy();
357 retv_if(adapter_proxy == NULL, NULL);
359 retv_if(object_path == NULL, NULL);
361 conn = _bt_gdbus_get_system_gconn();
363 BT_ERR("conn == NULL");
367 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
368 NULL, BT_BLUEZ_NAME, object_path,
369 BT_PROPERTIES_INTERFACE, NULL, NULL);
371 retv_if(device_proxy == NULL, NULL);
373 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
374 g_variant_new("(s)", BT_DEVICE_INTERFACE),
375 G_DBUS_CALL_FLAGS_NONE,
380 g_object_unref(device_proxy);
382 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
384 if (result != NULL) {
385 g_variant_get(result , "(@a{sv})", &value);
386 g_variant_unref(result);
388 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
390 g_variant_get(tmp_value, "s", &name);
391 g_variant_unref(tmp_value);
393 DBG_SECURE("Alias Name [%s]", name);
395 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
396 g_variant_get(tmp_value, "s", &name);
397 g_variant_unref(tmp_value);
400 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
402 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
403 g_variant_unref(tmp_value);
405 dev_info->is_alias_set = FALSE;
407 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
409 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
411 dev_info->class = g_variant_get_uint32(tmp_value);
412 g_variant_unref(tmp_value);
416 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
418 dev_info->connected = g_variant_get_byte(tmp_value);
419 g_variant_unref(tmp_value);
421 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
422 BT_DBG("connected link : %d", dev_info->connected);
424 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
426 dev_info->trust = g_variant_get_boolean(tmp_value);
427 g_variant_unref(tmp_value);
429 dev_info->trust = FALSE;
431 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
433 dev_info->paired = g_variant_get_boolean(tmp_value);
434 g_variant_unref(tmp_value);
436 dev_info->paired = FALSE;
438 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
440 dev_info->rssi = g_variant_get_int16(tmp_value);
441 g_variant_unref(tmp_value);
445 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
447 dev_info->addr_type = g_variant_get_byte(tmp_value);
448 g_variant_unref(tmp_value);
450 dev_info->addr_type = 0;
452 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
454 __bt_get_uuids(tmp_value, dev_info);
455 g_variant_unref(tmp_value);
458 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerDataLen", G_VARIANT_TYPE_UINT16);
460 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
461 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
462 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
463 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
465 g_variant_unref(tmp_value);
467 dev_info->manufacturer_data_len = 0;
469 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerData", G_VARIANT_TYPE_ARRAY);
471 if ((dev_info->manufacturer_data_len == 0) ||
472 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
473 BT_ERR("manufacturer data length doesn't match");
474 dev_info->manufacturer_data_len = 0;
475 dev_info->manufacturer_data = NULL;
477 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
478 g_variant_get(tmp_value, "ay", &value_iter);
479 while (g_variant_iter_loop(value_iter, "y", &m_value))
480 dev_info->manufacturer_data[i++] = m_value;
482 g_variant_unref(tmp_value);
484 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
485 dev_info->manufacturer_data_len = 0;
486 dev_info->manufacturer_data = NULL;
489 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
490 g_variant_get(tmp_value, "s", &address);
491 g_variant_unref(tmp_value);
493 dev_info->address = address;
494 dev_info->name = name;
495 g_variant_unref(value);
497 BT_ERR("result is NULL\n");
505 char *_bt_get_bonded_device_name(char *address)
507 bluetooth_device_address_t device_address = { {0} };
508 bluetooth_device_info_t dev_info;
510 retv_if(address == NULL, strdup(""));
512 _bt_convert_addr_string_to_type(device_address.addr, address);
514 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
516 _bt_get_bonded_device_info(&device_address, &dev_info);
518 return g_strdup(dev_info.device_name.name);
521 static gboolean __ignore_auto_pairing_request(const char *address)
526 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
537 /* Get the LAP(Lower Address part) */
538 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
540 snprintf(lap_address, sizeof(lap_address), ",%s", address);
542 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
545 BT_ERR("fopen failed \n");
549 fseek(fp, 0, SEEK_END);
554 BT_ERR("Get file size failed \n");
559 buffer = g_malloc0(sizeof(char) * size);
560 result = fread((char *)buffer, 1, size, fp);
562 if (result != size) {
563 BT_ERR("Read Error\n");
568 BT_DBG("Buffer = %s\n", buffer);
570 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
576 /* Write the data and insert new device data */
577 for (i = 0; lines[i] != NULL; i++) {
578 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
579 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
581 lines[i] = temp_buffer;
584 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
586 /* Fix : NULL_RETURNS */
587 retv_if(buffer == NULL, FALSE);
589 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
592 BT_ERR("fopen failed \n");
597 BT_DBG("Buffer = %s\n", buffer);
598 fwrite(buffer, 1, strlen(buffer), fp);
608 static int __bt_cancel_bonding(void)
610 BT_CHECK_PARAMETER(bonding_info, return);
611 BT_CHECK_PARAMETER(bonding_info->addr, return);
612 /* First Cancel the ongoing pairing in bluez otherwise if we send
613 * pair request bluez will send inprogress and we again retry bond so
614 * this cycle continues */
616 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
617 NULL, G_DBUS_CALL_FLAGS_NONE,
620 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
622 return BLUETOOTH_ERROR_PARING_FAILED;
624 return BLUETOOTH_ERROR_NONE;
627 static int __bt_retry_bond(void)
629 BT_CHECK_PARAMETER(bonding_info, return);
630 BT_CHECK_PARAMETER(bonding_info->addr, return);
632 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
633 g_variant_new("(y)", bonding_info->conn_type),
634 G_DBUS_CALL_FLAGS_NONE,
637 (GAsyncReadyCallback)__bt_bond_device_cb,
640 return BLUETOOTH_ERROR_NONE;
644 static int __bt_remove_and_bond(void)
646 GDBusProxy *adapter_proxy;
647 GVariant *result = NULL;
649 char *device_path = NULL;
651 BT_CHECK_PARAMETER(bonding_info, return);
652 BT_CHECK_PARAMETER(bonding_info->addr, return);
654 adapter_proxy = _bt_get_adapter_proxy();
655 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
657 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
658 g_variant_new("(s)", bonding_info->addr),
659 G_DBUS_CALL_FLAGS_NONE,
664 return BLUETOOTH_ERROR_INTERNAL;
666 g_variant_get(result , "(o)", &device_path);
667 g_variant_unref(result);
669 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
671 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
672 g_variant_new("(o)", device_path),
673 G_DBUS_CALL_FLAGS_NONE,
679 BT_ERR("UnpairDevice Fail: %s", err->message);
681 return BLUETOOTH_ERROR_INTERNAL;
684 return __bt_retry_bond();
687 static int __bt_cancel_and_bond(void)
689 int ret = BLUETOOTH_ERROR_NONE;
691 ret = _bt_agent_reply_cancellation();
692 if (ret != BLUETOOTH_ERROR_NONE) {
693 BT_ERR("Fail to call reply cancellation");
696 return __bt_retry_bond();
700 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
703 int result = BLUETOOTH_ERROR_NONE;
706 GVariant *out_param1;
707 request_info_t *req_info;
708 bluetooth_device_info_t dev_info;
709 bt_remote_dev_info_t *remote_dev_info;
710 GVariant *manufacture_data;
713 /* Terminate ALL system popup */
714 syspopup_destroy_all();
716 reply = g_dbus_proxy_call_finish(proxy, res, &err);
718 g_variant_unref(reply);
720 is_device_creating = FALSE;
722 if (bonding_info == NULL) {
724 BT_ERR("bonding_info == NULL");
730 req_info = _bt_get_request_info(bonding_info->req_id);
731 if (req_info == NULL) {
732 BT_ERR("req_info == NULL");
737 g_dbus_error_strip_remote_error(err);
738 BT_ERR("Error occured in CreateBonding [%s]", err->message);
740 if (g_strrstr(err->message, "Already Exists")) {
741 BT_INFO("Existing Bond, remove and retry");
742 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
744 result = BLUETOOTH_ERROR_PARING_FAILED;
745 } else if (_bt_agent_is_canceled() ||
746 g_strrstr(err->message, "Authentication Canceled")) {
747 BT_INFO("Cancelled by USER");
748 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
749 } else if (g_strrstr(err->message, "Authentication Rejected")) {
751 result = BLUETOOTH_ERROR_ACCESS_DENIED;
752 } else if (g_strrstr(err->message, "In Progress")) {
753 BT_INFO("Bond in progress, cancel and retry");
754 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
756 result = BLUETOOTH_ERROR_PARING_FAILED;
757 } else if (g_strrstr(err->message, "Authentication Failed")) {
758 BT_INFO("Authentication Failed");
759 if (bonding_info->is_autopair == TRUE) {
760 _bt_set_autopair_status_in_bonding_info(FALSE);
761 __ignore_auto_pairing_request(bonding_info->addr);
763 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
764 } else if (g_strrstr(err->message, "Page Timeout")) {
765 BT_INFO("Page Timeout");
766 /* This is the special case
767 As soon as call bluetooth_bond_device, try to cancel bonding.
768 In this case, before completing to call 'CreatePairedDevice' method
769 the procedure is stopped. So 'Cancle' error is not return.
771 result = BLUETOOTH_ERROR_HOST_DOWN;
772 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
773 BT_INFO("Cancel already running bonding");
774 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
775 BT_INFO("Error while Cancelling bonding");
776 /* we need to unref proxy so continue */
778 result = BLUETOOTH_ERROR_INTERNAL;
779 } else if (g_strrstr(err->message, "Connection Timeout")) {
780 /* Pairing request timeout */
781 result = BLUETOOTH_ERROR_TIMEOUT;
782 } else if (g_strrstr(err->message, "Authentication Timeout")) {
783 /* Pairing request timeout */
784 result = BLUETOOTH_ERROR_TIMEOUT;
786 BT_DBG("Default case");
787 result = BLUETOOTH_ERROR_PARING_FAILED;
791 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
792 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
793 result == BLUETOOTH_ERROR_TIMEOUT ||
794 result == BLUETOOTH_ERROR_HOST_DOWN) {
795 #ifdef TIZEN_PROFILE_WEARABLE
796 int is_sw_running = 0;
798 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
799 BT_ERR("vconf_get_int for setup wizard state failed");
802 __bt_launch_unable_to_pairing_syspopup(result);
804 BT_ERR("Unable to pair");
806 bonding_info->result = result;
809 g_object_unref(proxy);
810 bonding_info->device_proxy = NULL;
812 if (result != BLUETOOTH_ERROR_NONE) {
813 if (bonding_info->bonding_wo_discovery) {
814 GDBusProxy *adapter_proxy;
815 GVariant *ret = NULL;
816 GError *error = NULL;
819 BT_ERR("Bond was tried without discovery. Remove it");
821 adapter_proxy = _bt_get_adapter_proxy();
822 if (adapter_proxy == NULL) {
823 BT_ERR("Cannot get adapter_proxy");
827 device_path = _bt_get_device_object_path(bonding_info->addr);
828 if (device_path == NULL) {
829 BT_ERR("Cannot get device path");
833 ret = g_dbus_proxy_call_sync(adapter_proxy,
835 g_variant_new("(o)", device_path),
836 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
839 BT_ERR("RemoveDevice Fail: %s", error->message);
840 g_clear_error(&error);
844 g_variant_unref(ret);
852 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
853 if (!remote_dev_info)
856 GVariant *uuids = NULL;
857 GVariantBuilder *builder = NULL;
859 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
860 for (i = 0; i < remote_dev_info->uuid_count; i++) {
861 g_variant_builder_add(builder, "s",
862 remote_dev_info->uuids[i]);
864 uuids = g_variant_new("as", builder);
865 g_variant_builder_unref(builder);
866 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
867 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
870 param = g_variant_new("(isunsbub@asn@ay)",
873 remote_dev_info->class,
874 remote_dev_info->rssi,
875 remote_dev_info->name,
876 remote_dev_info->paired,
877 remote_dev_info->connected,
878 remote_dev_info->trust,
880 remote_dev_info->manufacturer_data_len,
884 /* Send the event to application */
885 _bt_send_event(BT_ADAPTER_EVENT,
886 BLUETOOTH_EVENT_BONDING_FINISHED,
889 _bt_free_device_info(remote_dev_info);
892 if (req_info->context == NULL)
895 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
896 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
899 if (_bt_adapter_get_status() != BT_ACTIVATED)
900 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
902 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
903 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
905 g_dbus_method_invocation_return_value(req_info->context,
906 g_variant_new("(iv)", result, out_param1));
908 g_variant_unref(out_param1);
910 _bt_delete_request_list(req_info->req_id);
915 _bt_agent_set_canceled(FALSE);
918 g_free(bonding_info->addr);
919 g_free(bonding_info);
923 int _bt_bond_device(int request_id,
924 bluetooth_device_address_t *device_address,
925 unsigned short conn_type, GArray **out_param1)
928 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
929 bluetooth_device_info_t dev_info;
931 GDBusConnection *conn;
932 char *device_path = NULL;
933 GDBusProxy *adapter_proxy;
934 GError *error = NULL;
935 gboolean bonding_wo_discovery = FALSE;
937 BT_CHECK_PARAMETER(device_address, return);
940 BT_ERR("Bonding in progress");
942 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
943 memcpy(dev_info.device_address.addr, device_address->addr,
944 BLUETOOTH_ADDRESS_LENGTH);
946 g_array_append_vals(*out_param1, &dev_info,
947 sizeof(bluetooth_device_info_t));
949 return BLUETOOTH_ERROR_DEVICE_BUSY;
952 conn = _bt_gdbus_get_system_gconn();
953 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
955 _bt_convert_addr_type_to_string(address, device_address->addr);
957 device_path = _bt_get_device_object_path(address);
959 if (device_path == NULL) {
960 BT_ERR("No searched device");
961 GVariant *ret = NULL;
962 adapter_proxy = _bt_get_adapter_proxy();
963 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
965 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
966 g_variant_new("(s)", address),
967 G_DBUS_CALL_FLAGS_NONE,
973 BT_ERR("CreateDevice Fail: %s", error->message);
974 g_clear_error(&error);
977 g_variant_unref(ret);
978 device_path = _bt_get_device_object_path(address);
979 if (device_path == NULL) {
980 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
981 memcpy(dev_info.device_address.addr, device_address->addr,
982 BLUETOOTH_ADDRESS_LENGTH);
984 g_array_append_vals(*out_param1, &dev_info,
985 sizeof(bluetooth_device_info_t));
987 return BLUETOOTH_ERROR_NOT_PAIRED;
989 BT_INFO("device_path is created[%s]", device_path);
991 bonding_wo_discovery = TRUE;
994 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
996 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
999 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1001 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1002 bonding_info->addr = g_strdup(address);
1003 bonding_info->req_id = request_id;
1005 bonding_info->device_proxy = proxy;
1006 bonding_info->conn_type = conn_type;
1007 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1009 is_device_creating = TRUE;
1011 g_dbus_proxy_call(proxy, "Pair",
1012 g_variant_new("(y)", conn_type),
1013 G_DBUS_CALL_FLAGS_NONE,
1014 BT_MAX_DBUS_TIMEOUT,
1016 (GAsyncReadyCallback)__bt_bond_device_cb,
1019 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1021 return BLUETOOTH_ERROR_NONE;
1023 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1024 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1025 bonding_info->addr);
1027 g_array_append_vals(*out_param1, &dev_info,
1028 sizeof(bluetooth_device_info_t));
1030 is_device_creating = FALSE;
1032 g_free(bonding_info->addr);
1033 g_free(bonding_info);
1034 bonding_info = NULL;
1036 return BLUETOOTH_ERROR_INTERNAL;*/
1039 int _bt_cancel_bonding(void)
1041 int ret = BLUETOOTH_ERROR_NONE;
1043 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1045 ret = _bt_agent_reply_cancellation();
1046 if (ret != BLUETOOTH_ERROR_NONE) {
1047 BT_ERR("Fail to call reply cancellation");
1051 _bt_agent_set_canceled(TRUE);
1053 return BLUETOOTH_ERROR_NONE;
1056 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1061 GVariant *out_param1;
1062 int result = BLUETOOTH_ERROR_NONE;
1063 bt_funcion_data_t *unbonding_info;
1064 bluetooth_device_info_t dev_info;
1065 request_info_t *req_info;
1067 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1069 g_variant_unref(reply);
1071 unbonding_info = user_data;
1073 if (unbonding_info == NULL) {
1075 BT_ERR("unbonding_info == NULL");
1079 req_info = _bt_get_request_info(unbonding_info->req_id);
1080 if (req_info == NULL) {
1081 BT_ERR("req_info == NULL");
1086 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1087 result = BLUETOOTH_ERROR_INTERNAL;
1090 if (req_info->context == NULL)
1093 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1094 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1095 unbonding_info->addr);
1097 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1098 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1100 g_dbus_method_invocation_return_value(req_info->context,
1101 g_variant_new("(iv)", result, out_param1));
1104 _bt_delete_request_list(req_info->req_id);
1110 if (unbonding_info) {
1111 g_free(unbonding_info->addr);
1112 g_free(unbonding_info);
1116 int _bt_unbond_device(int request_id,
1117 bluetooth_device_address_t *device_address,
1118 GArray **out_param1)
1120 char *device_path = NULL;
1121 bt_funcion_data_t *unbonding_info;
1122 GDBusProxy *adapter_proxy = NULL;
1123 GDBusProxy *device_proxy = NULL;
1124 GDBusConnection *conn;
1125 int result = BLUETOOTH_ERROR_INTERNAL;
1126 bluetooth_device_info_t dev_info;
1127 GError *error = NULL;
1128 GVariant *ret = NULL;
1130 BT_CHECK_PARAMETER(device_address, return);
1132 adapter_proxy = _bt_get_adapter_proxy();
1133 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1135 /* allocate user data so that it can be retrieved in callback */
1136 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1137 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1138 unbonding_info->req_id = request_id;
1140 _bt_convert_addr_type_to_string(unbonding_info->addr,
1141 device_address->addr);
1143 device_path = _bt_get_device_object_path(unbonding_info->addr);
1145 if (device_path == NULL) {
1146 BT_ERR("No paired device");
1147 result = BLUETOOTH_ERROR_NOT_PAIRED;
1151 conn = _bt_gdbus_get_system_gconn();
1153 BT_ERR("conn is NULL");
1154 result = BLUETOOTH_ERROR_INTERNAL;
1158 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1159 NULL, BT_BLUEZ_NAME,
1160 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1162 if (device_proxy != NULL) {
1164 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1165 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1166 G_DBUS_CALL_FLAGS_NONE,
1171 BT_ERR("Getting property failed: [%s]\n", error->message);
1172 g_error_free(error);
1173 result = BLUETOOTH_ERROR_NOT_PAIRED;
1177 BT_ERR("No paired device");
1178 g_object_unref(device_proxy);
1179 result = BLUETOOTH_ERROR_NOT_PAIRED;
1182 g_variant_unref(ret);
1184 g_object_unref(device_proxy);
1187 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1188 g_variant_new("(o)", device_path),
1189 G_DBUS_CALL_FLAGS_NONE,
1190 BT_MAX_DBUS_TIMEOUT,
1192 (GAsyncReadyCallback)__bt_unbond_cb,
1195 g_free(device_path);
1196 return BLUETOOTH_ERROR_NONE;
1199 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1200 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1201 unbonding_info->addr);
1203 g_array_append_vals(*out_param1, &dev_info,
1204 sizeof(bluetooth_device_info_t));
1205 g_free(device_path);
1206 g_free(unbonding_info->addr);
1207 g_free(unbonding_info);
1211 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1216 GVariant *out_param1;
1217 int result = BLUETOOTH_ERROR_NONE;
1218 bluetooth_device_info_t dev_info;
1219 bt_remote_dev_info_t *remote_dev_info;
1220 request_info_t *req_info;
1221 GVariant *uuid_list, *manufacture_data;
1223 GVariantBuilder *builder = NULL;
1226 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1228 g_variant_unref(reply);
1230 g_object_unref(proxy);
1232 if (searching_info == NULL) {
1234 BT_ERR("unbonding_info == NULL");
1238 req_info = _bt_get_request_info(searching_info->req_id);
1239 if (req_info == NULL) {
1240 BT_ERR("req_info == NULL");
1245 g_dbus_error_strip_remote_error(err);
1246 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1248 if (g_strrstr("Operation canceled", err->message))
1249 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1250 else if (g_strrstr("In Progress", err->message))
1251 result = BLUETOOTH_ERROR_IN_PROGRESS;
1252 else if (g_strrstr("Host is down", err->message))
1253 result = BLUETOOTH_ERROR_HOST_DOWN;
1255 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1257 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1258 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1259 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1260 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1261 remote_dev_info->uuid_count > 0) {
1262 result = BLUETOOTH_ERROR_NONE;
1265 _bt_free_device_info(remote_dev_info);
1270 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1271 if (!remote_dev_info)
1275 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1276 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1277 g_variant_builder_add(builder, "s",
1278 remote_dev_info->uuids[i]);
1280 uuid_list = g_variant_new("as", builder);
1281 g_variant_builder_unref(builder);
1282 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1283 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1286 param = g_variant_new("(isunsbub@asn@ay)",
1288 searching_info->addr,
1289 remote_dev_info->class,
1290 remote_dev_info->rssi,
1291 remote_dev_info->name,
1292 remote_dev_info->paired,
1293 remote_dev_info->connected,
1294 remote_dev_info->trust,
1296 remote_dev_info->manufacturer_data_len,
1299 /* Send the event to application */
1300 _bt_send_event(BT_ADAPTER_EVENT,
1301 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1304 _bt_free_device_info(remote_dev_info);
1307 if (req_info->context == NULL)
1310 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1311 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1312 searching_info->addr);
1314 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1315 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1317 g_dbus_method_invocation_return_value(req_info->context,
1318 g_variant_new("(iv)", result, out_param1));
1321 _bt_delete_request_list(req_info->req_id);
1326 if (searching_info) {
1327 g_free(searching_info->addr);
1328 g_free(searching_info);
1329 searching_info = NULL;
1333 int _bt_search_device(int request_id,
1334 bluetooth_device_address_t *device_address)
1336 char *device_path = NULL;
1337 GDBusProxy *device_proxy = NULL;
1338 GDBusConnection *conn;
1340 GDBusProxy *adapter_proxy;
1341 int result = BLUETOOTH_ERROR_INTERNAL;
1343 BT_CHECK_PARAMETER(device_address, return);
1346 BT_ERR("Bonding in progress");
1347 return BLUETOOTH_ERROR_DEVICE_BUSY;
1350 if (searching_info) {
1351 BT_ERR("Service searching in progress");
1352 return BLUETOOTH_ERROR_DEVICE_BUSY;
1355 adapter_proxy = _bt_get_adapter_proxy();
1356 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1358 /* allocate user data so that it can be retrieved in callback */
1359 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1360 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1361 searching_info->req_id = request_id;
1363 _bt_convert_addr_type_to_string(searching_info->addr,
1364 device_address->addr);
1366 conn = _bt_gdbus_get_system_gconn();
1367 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1370 device_path = _bt_get_device_object_path(searching_info->addr);
1372 if (device_path == NULL) {
1373 BT_ERR("No paired device");
1374 result = BLUETOOTH_ERROR_NOT_PAIRED;
1378 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1379 NULL, BT_BLUEZ_NAME,
1380 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1381 g_free(device_path);
1382 if (device_proxy == NULL) {
1383 result = BLUETOOTH_ERROR_INTERNAL;
1387 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1388 g_variant_new("(s)", ""),
1389 G_DBUS_CALL_FLAGS_NONE,
1390 BT_MAX_DBUS_TIMEOUT,
1392 (GAsyncReadyCallback)__bt_discover_cb,
1395 searching_info->device_proxy = device_proxy;
1397 return BLUETOOTH_ERROR_NONE;
1400 g_free(searching_info->addr);
1401 g_free(searching_info);
1402 searching_info = NULL;
1406 int _bt_cancel_search_device(void)
1408 GVariant *ret = NULL;
1411 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1413 if (searching_info->device_proxy) {
1414 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1416 G_DBUS_CALL_FLAGS_NONE,
1421 g_variant_unref(ret);
1423 __bt_cancel_search_service_done();
1425 return BLUETOOTH_ERROR_NONE;
1428 int _bt_set_alias(bluetooth_device_address_t *device_address,
1431 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1432 gchar *device_path = NULL;
1433 GDBusProxy *adapter_proxy;
1434 GDBusProxy *device_proxy;
1435 GVariant *ret = NULL;
1436 GError *error = NULL;
1437 GDBusConnection *conn;
1439 BT_CHECK_PARAMETER(device_address, return);
1440 BT_CHECK_PARAMETER(alias, return);
1442 adapter_proxy = _bt_get_adapter_proxy();
1443 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1445 conn = _bt_gdbus_get_system_gconn();
1446 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1448 _bt_convert_addr_type_to_string(address, device_address->addr);
1450 device_path = _bt_get_device_object_path(address);
1452 if (device_path == NULL) {
1453 BT_ERR("No paired device");
1454 return BLUETOOTH_ERROR_NOT_PAIRED;
1457 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1458 NULL, BT_BLUEZ_NAME,
1459 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1461 g_free(device_path);
1462 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1464 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1465 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1466 G_DBUS_CALL_FLAGS_NONE,
1471 g_variant_unref(ret);
1473 g_object_unref(device_proxy);
1476 BT_ERR("SetProperty error: [%s]", error->message);
1477 g_error_free(error);
1478 return BLUETOOTH_ERROR_INTERNAL;
1481 return BLUETOOTH_ERROR_NONE;
1484 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1487 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1488 gchar *device_path = NULL;
1489 GDBusProxy *device_proxy;
1490 gboolean previous_value;
1491 GError *error = NULL;
1492 GDBusConnection *conn;
1493 GVariant *result = NULL;
1494 GVariant *temp = NULL;
1495 int ret = BLUETOOTH_ERROR_NONE;
1497 BT_CHECK_PARAMETER(device_address, return);
1499 conn = _bt_gdbus_get_system_gconn();
1500 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1502 _bt_convert_addr_type_to_string(address, device_address->addr);
1504 device_path = _bt_get_device_object_path(address);
1506 if (device_path == NULL) {
1507 BT_ERR("No paired device");
1508 return BLUETOOTH_ERROR_NOT_PAIRED;
1511 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1512 NULL, BT_BLUEZ_NAME,
1513 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1515 g_free(device_path);
1516 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1518 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1519 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1520 G_DBUS_CALL_FLAGS_NONE,
1524 if (error != NULL) {
1525 BT_ERR("Getting property failed: [%s]\n", error->message);
1526 g_error_free(error);
1527 g_object_unref(device_proxy);
1528 return BLUETOOTH_ERROR_INTERNAL;
1531 g_variant_get(result, "(v)", &temp);
1532 previous_value = g_variant_get_boolean(temp);
1533 g_variant_unref(temp);
1534 g_variant_unref(result);
1535 /* If the input is same with previous value, return error. */
1536 if (previous_value == authorize) {
1537 BT_ERR("Same value: %d", previous_value);
1538 g_object_unref(device_proxy);
1539 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1543 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1544 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1545 G_DBUS_CALL_FLAGS_NONE,
1550 g_object_unref(device_proxy);
1552 BT_ERR("SetProperty error: [%s]", error->message);
1553 g_error_free(error);
1554 ret = BLUETOOTH_ERROR_INTERNAL;
1558 g_variant_unref(result);
1563 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1564 gboolean *is_connected)
1566 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1567 char *object_path = NULL;
1569 GDBusProxy *device_proxy;
1570 GError *error = NULL;
1572 GDBusConnection *conn;
1573 GVariant *result = NULL;
1574 int ret = BLUETOOTH_ERROR_NONE;
1576 BT_CHECK_PARAMETER(device_address, return);
1578 conn = _bt_gdbus_get_system_gconn();
1579 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1581 _bt_convert_addr_type_to_string(address, device_address->addr);
1583 object_path = _bt_get_device_object_path(address);
1584 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1586 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1587 NULL, BT_BLUEZ_NAME, object_path,
1588 BT_PROPERTIES_INTERFACE, NULL, NULL);
1589 g_free(object_path);
1590 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1592 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1593 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1595 G_DBUS_CALL_FLAGS_NONE,
1599 if (result == NULL) {
1600 if (error != NULL) {
1601 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1602 g_error_free(error);
1604 g_object_unref(device_proxy);
1605 return BLUETOOTH_ERROR_INTERNAL;
1608 g_variant_get(result, "(v)", &value);
1609 *is_connected = g_variant_get_boolean(value);
1610 g_variant_unref(result);
1612 BT_DBG("gatt is connected : %d", *is_connected);
1614 g_variant_unref(value);
1615 g_object_unref(device_proxy);
1620 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1621 int connection_type, gboolean *is_connected)
1623 char *object_path = NULL;
1624 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1625 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1626 GDBusProxy *device_proxy = NULL;
1627 GDBusProxy *adapter_proxy = NULL;
1628 GDBusConnection *conn;
1629 GError *error = NULL;
1630 GVariant *tmp_value = NULL;
1631 GVariant *value = NULL;
1632 GVariant *result = NULL;
1635 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1636 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1638 _bt_convert_addr_type_to_string(address, device_address->addr);
1639 _bt_convert_addr_string_to_secure_string(secure_address, address);
1641 *is_connected = FALSE;
1642 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1644 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1645 return _bt_rfcomm_is_device_connected(device_address,
1647 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1648 return _bt_is_gatt_connected(device_address, is_connected);
1649 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1650 return _bt_pbap_is_connected(device_address, is_connected);
1652 adapter_proxy = _bt_get_adapter_proxy();
1653 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1655 conn = _bt_gdbus_get_system_gconn();
1656 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1658 _bt_convert_addr_type_to_string(address, device_address->addr);
1660 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1661 object_path = _bt_get_adapter_path();
1662 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1663 NULL, BT_BLUEZ_NAME,
1664 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1665 g_free(object_path);
1666 if (device_proxy == NULL) {
1667 BT_DBG("Device don't have this service");
1668 return BLUETOOTH_ERROR_INTERNAL;
1671 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1672 g_variant_new("(s)", address),
1673 G_DBUS_CALL_FLAGS_NONE,
1678 if (result == NULL) {
1679 BT_ERR("[GetProperties] Error occured in Proxy call");
1681 BT_ERR("%s", error->message);
1682 g_error_free(error);
1684 *is_connected = FALSE;
1685 g_object_unref(device_proxy);
1686 return BLUETOOTH_ERROR_NONE;
1688 g_variant_get(result , "(@a{sv})", &value);
1689 g_variant_unref(result);
1692 tmp_value = g_variant_lookup_value(value,
1694 G_VARIANT_TYPE_BOOLEAN);
1696 *is_connected = g_variant_get_boolean(tmp_value);
1697 g_variant_unref(tmp_value);
1699 g_variant_unref(value);
1701 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1702 return _bt_is_network_connected(_bt_get_net_conn(),
1703 device_address->addr, is_connected);
1705 uuid = _bt_get_profile_uuid128(connection_type);
1707 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1708 return BLUETOOTH_ERROR_INTERNAL;
1710 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1712 object_path = _bt_get_device_object_path(address);
1713 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1714 BT_DBG("object_path: %s", object_path);
1715 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1716 NULL, BT_BLUEZ_NAME,
1717 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1718 g_free(object_path);
1719 if (device_proxy == NULL) {
1720 BT_DBG("Device don't have this service");
1722 return BLUETOOTH_ERROR_INTERNAL;
1725 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1726 g_variant_new("(s)", uuid),
1727 G_DBUS_CALL_FLAGS_NONE,
1732 if (result == NULL) {
1733 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1735 BT_ERR("%s", error->message);
1736 if (g_strrstr(error->message, "Not Connected"))
1737 BT_DBG("Not connected");
1738 g_error_free(error);
1740 *is_connected = FALSE;
1741 g_object_unref(device_proxy);
1743 return BLUETOOTH_ERROR_NONE;
1745 g_variant_get(result, "(b)", is_connected);
1747 g_variant_unref(result);
1750 g_object_unref(device_proxy);
1751 return BLUETOOTH_ERROR_NONE;
1754 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1755 bluetooth_connected_link_t *connected)
1757 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1758 char *object_path = NULL;
1760 GDBusProxy *device_proxy;
1761 GError *error = NULL;
1762 GDBusConnection *conn;
1763 GVariant *value = NULL;
1764 GVariant *result = NULL;
1766 BT_CHECK_PARAMETER(device_address, return);
1768 conn = _bt_gdbus_get_system_gconn();
1769 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1771 _bt_convert_addr_type_to_string(address, device_address->addr);
1773 object_path = _bt_get_device_object_path(address);
1774 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1776 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1777 NULL, BT_BLUEZ_NAME,
1778 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1779 g_free(object_path);
1780 if (device_proxy == NULL) {
1781 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1782 return BLUETOOTH_ERROR_NONE;
1785 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1786 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1787 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1789 if (error != NULL) {
1790 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1791 g_error_free(error);
1792 g_object_unref(device_proxy);
1793 return BLUETOOTH_ERROR_INTERNAL;
1795 g_variant_get(result, "(v)", &value);
1796 *connected = g_variant_get_byte(value);
1797 g_variant_unref(value);
1798 g_variant_unref(result);
1801 g_object_unref(device_proxy);
1802 return BLUETOOTH_ERROR_NONE;
1805 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1809 GVariant *out_param1;
1811 int result = BLUETOOTH_ERROR_NONE;
1812 bt_function_data_t *func_data = user_data;
1813 request_info_t *req_info = NULL;
1814 bluetooth_device_address_t device_addr = { {0} };
1816 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1817 g_object_unref(proxy);
1819 if (reply == NULL) {
1820 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1822 BT_ERR("Error: %s", err->message);
1823 g_clear_error(&err);
1825 result = BLUETOOTH_ERROR_INTERNAL;
1827 g_variant_unref(reply);
1830 if (func_data == NULL) {
1831 BT_ERR("func_data is NULL");
1835 req_info = _bt_get_request_info(func_data->req_id);
1836 if (req_info == NULL) {
1837 BT_ERR("req_info is NULL");
1841 if (req_info->context == NULL) {
1842 BT_ERR("req_info->context is NULL");
1846 _bt_convert_addr_string_to_type(device_addr.addr,
1847 (const char *)func_data->address);
1849 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1850 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1853 g_dbus_method_invocation_return_value(req_info->context,
1854 g_variant_new("(iv)", result, out_param1));
1858 _bt_delete_request_list(req_info->req_id);
1861 g_free(func_data->address);
1866 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1867 gboolean auto_connect)
1869 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1870 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1871 gchar *device_path = NULL;
1872 GDBusProxy *device_proxy = NULL;
1873 GDBusConnection *conn;
1874 int ret = BLUETOOTH_ERROR_NONE;
1875 bt_function_data_t *func_data;
1877 BT_CHECK_PARAMETER(bd_addr, return);
1879 conn = _bt_gdbus_get_system_gconn();
1880 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1882 _bt_convert_addr_type_to_string(device_address,
1883 (unsigned char *)bd_addr->addr);
1884 device_path = _bt_get_device_object_path(device_address);
1885 if (device_path == NULL) {
1886 BT_ERR_C("device_path NULL : [%s]", device_address);
1887 ret = BLUETOOTH_ERROR_INTERNAL;
1891 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1892 NULL, BT_BLUEZ_NAME,
1893 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1894 g_free(device_path);
1895 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1897 func_data = g_malloc0(sizeof(bt_function_data_t));
1898 func_data->address = g_strdup(device_address);
1899 if (func_data->address == NULL) {
1900 BT_ERR("Unable to allocate memory for address");
1901 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1905 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1906 BT_INFO("Connect LE [%s]", secure_address);
1908 func_data->req_id = req_id;
1910 g_dbus_proxy_call(device_proxy, "ConnectLE",
1911 g_variant_new("(b)", auto_connect),
1912 G_DBUS_CALL_FLAGS_NONE,
1913 BT_MAX_DBUS_TIMEOUT,
1915 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1921 g_object_unref(device_proxy);
1923 g_free(func_data->address);
1929 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1931 BT_INFO("Try to initiate pending LE connection");
1933 pending_le_conn_timer_id = 0;
1935 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1936 &pending_le_conn_info->bd_addr,
1937 pending_le_conn_info->auto_connect);
1939 g_free(pending_le_conn_info);
1940 pending_le_conn_info = NULL;
1945 void _bt_pending_connect_le_device(void)
1947 if (pending_le_conn_timer_id > 0) {
1948 g_source_remove(pending_le_conn_timer_id);
1949 __bt_connect_le_timer_cb(NULL);
1953 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1954 gboolean auto_connect)
1956 int ret = BLUETOOTH_ERROR_NONE;
1958 BT_CHECK_PARAMETER(bd_addr, return);
1960 ret = _bt_hold_current_advertising();
1961 if (ret == BLUETOOTH_ERROR_NONE) {
1962 BT_INFO("Current advertising is held");
1963 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1964 pending_le_conn_info->req_id = req_id;
1965 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1966 BLUETOOTH_ADDRESS_LENGTH);
1967 pending_le_conn_info->auto_connect = auto_connect;
1969 pending_le_conn_timer_id =
1970 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1972 return BLUETOOTH_ERROR_NONE;
1975 BT_ERR("Unable to hold advertising");
1977 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1980 int _bt_disconnect_le_device(int req_id,
1981 const bluetooth_device_address_t *bd_addr)
1983 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1984 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1986 GDBusProxy *device_proxy;
1987 GDBusConnection *conn;
1988 int ret = BLUETOOTH_ERROR_NONE;
1989 bt_function_data_t *func_data;
1991 BT_CHECK_PARAMETER(bd_addr, return);
1993 conn = _bt_gdbus_get_system_gconn();
1994 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1996 _bt_convert_addr_type_to_string(device_address,
1997 (unsigned char *)bd_addr->addr);
1998 device_path = _bt_get_device_object_path(device_address);
1999 if (device_path == NULL) {
2000 BT_DBG("device_path NULL");
2001 ret = BLUETOOTH_ERROR_INTERNAL;
2005 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2007 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2008 NULL, BT_BLUEZ_NAME,
2009 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2010 g_free(device_path);
2011 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2013 func_data = g_malloc0(sizeof(bt_function_data_t));
2014 func_data->address = g_strdup(device_address);
2015 if (func_data->address == NULL) {
2016 BT_ERR("Unable to allocate memory for address");
2017 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2021 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2022 BT_INFO("Disconnect LE [%s]", secure_address);
2024 func_data->req_id = req_id;
2026 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2028 G_DBUS_CALL_FLAGS_NONE,
2029 BT_MAX_DBUS_TIMEOUT,
2031 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2036 g_object_unref(device_proxy);
2038 g_free(func_data->address);
2044 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2046 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2047 gchar *device_path = NULL;
2048 GError *error = NULL;
2049 GDBusProxy *device_proxy = NULL;
2050 GDBusProxy *adapter_proxy;
2051 GDBusConnection *conn;
2052 int ret = BLUETOOTH_ERROR_NONE;
2054 BT_CHECK_PARAMETER(bd_addr, return);
2056 _bt_convert_addr_type_to_string(device_address,
2057 (unsigned char *)bd_addr->addr);
2059 conn = _bt_gdbus_get_system_gconn();
2060 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2062 adapter_proxy = _bt_get_adapter_proxy();
2063 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2065 device_path = _bt_get_device_object_path(device_address);
2066 if (device_path == NULL) {
2067 BT_DBG("device_path NULL");
2068 ret = BLUETOOTH_ERROR_INTERNAL;
2072 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2074 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2075 NULL, BT_BLUEZ_NAME,
2076 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2077 g_free(device_path);
2078 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2080 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2082 G_DBUS_CALL_FLAGS_NONE,
2087 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2088 g_error_free(error);
2089 g_object_unref(device_proxy);
2090 return BLUETOOTH_ERROR_INTERNAL;
2093 g_object_unref(device_proxy);
2098 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2100 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2101 gchar *device_path = NULL;
2102 GError *error = NULL;
2103 GDBusProxy *device_proxy = NULL;
2104 GDBusProxy *adapter_proxy;
2105 GDBusConnection *conn;
2106 int ret = BLUETOOTH_ERROR_NONE;
2108 BT_CHECK_PARAMETER(bd_addr, return);
2110 _bt_convert_addr_type_to_string(device_address,
2111 (unsigned char *)bd_addr->addr);
2113 conn = _bt_gdbus_get_system_gconn();
2114 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2116 adapter_proxy = _bt_get_adapter_proxy();
2117 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2119 device_path = _bt_get_device_object_path(device_address);
2120 if (device_path == NULL) {
2121 BT_DBG("device_path NULL");
2122 ret = BLUETOOTH_ERROR_INTERNAL;
2126 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2128 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2129 NULL, BT_BLUEZ_NAME,
2130 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2131 g_free(device_path);
2132 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2134 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2136 G_DBUS_CALL_FLAGS_NONE,
2141 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2142 g_error_free(error);
2143 g_object_unref(device_proxy);
2144 return BLUETOOTH_ERROR_INTERNAL;
2147 g_object_unref(device_proxy);
2152 int _bt_connect_profile(char *address, char *uuid,
2153 void *cb, gpointer func_data)
2157 GDBusConnection *conn;
2158 GDBusProxy *adapter_proxy;
2159 GVariant *result = NULL;
2160 GError *error = NULL;
2162 conn = _bt_gdbus_get_system_gconn();
2163 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2165 object_path = _bt_get_device_object_path(address);
2166 if (object_path == NULL) {
2167 BT_ERR("No searched device");
2169 adapter_proxy = _bt_get_adapter_proxy();
2170 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2172 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2173 g_variant_new("(s)", address),
2174 G_DBUS_CALL_FLAGS_NONE,
2179 if (error != NULL) {
2180 BT_ERR("CreateDevice Fail: %s", error->message);
2181 g_error_free(error);
2184 g_variant_unref(result);
2186 object_path = _bt_get_device_object_path(address);
2188 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2190 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2191 NULL, BT_BLUEZ_NAME,
2192 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2193 g_free(object_path);
2194 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2196 g_dbus_proxy_call(proxy, "ConnectProfile",
2197 g_variant_new("(s)", uuid),
2198 G_DBUS_CALL_FLAGS_NONE,
2199 BT_MAX_DBUS_TIMEOUT,
2201 (GAsyncReadyCallback)cb,
2204 return BLUETOOTH_ERROR_NONE;
2207 int _bt_disconnect_profile(char *address, char *uuid,
2208 void *cb, gpointer func_data)
2212 GDBusConnection *conn;
2214 conn = _bt_gdbus_get_system_gconn();
2215 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2217 object_path = _bt_get_device_object_path(address);
2218 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2220 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2221 NULL, BT_BLUEZ_NAME,
2222 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2223 g_free(object_path);
2224 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2226 g_dbus_proxy_call(proxy, "DisconnectProfile",
2227 g_variant_new("(s)", uuid),
2228 G_DBUS_CALL_FLAGS_NONE,
2229 BT_MAX_DBUS_TIMEOUT,
2231 (GAsyncReadyCallback)cb,
2234 return BLUETOOTH_ERROR_NONE;
2237 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2238 int low_threshold, int in_range_threshold, int high_threshold)
2240 int ret = BLUETOOTH_ERROR_NONE;
2242 GVariant *result = NULL;
2243 GError *error = NULL;
2244 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2246 BT_CHECK_PARAMETER(bd_addr, return);
2247 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2248 bd_addr->addr[0], bd_addr->addr[1],
2249 bd_addr->addr[2], bd_addr->addr[3],
2250 bd_addr->addr[4], bd_addr->addr[5],
2252 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2253 in_range_threshold, high_threshold);
2255 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2257 proxy = _bt_get_adapter_proxy();
2258 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2260 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2261 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2262 G_DBUS_CALL_FLAGS_NONE,
2266 if (error != NULL) {
2267 BT_ERR("Dbus Call Error:[%s]", error->message);
2268 g_error_free(error);
2269 ret = BLUETOOTH_ERROR_INTERNAL;
2273 g_variant_unref(result);
2278 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2281 int ret = BLUETOOTH_ERROR_NONE;
2283 GVariant *result = NULL;
2284 GError *error = NULL;
2285 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2287 BT_CHECK_PARAMETER(bd_addr, return);
2288 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2289 bd_addr->addr[0], bd_addr->addr[1],
2290 bd_addr->addr[2], bd_addr->addr[3],
2291 bd_addr->addr[4], bd_addr->addr[5],
2294 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2296 proxy = _bt_get_adapter_proxy();
2297 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2299 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2300 g_variant_new("(si)", address, link_type),
2301 G_DBUS_CALL_FLAGS_NONE,
2306 if (error != NULL) {
2307 BT_ERR("Dbus Call Error:[%s]", error->message);
2308 g_error_free(error);
2309 ret = BLUETOOTH_ERROR_INTERNAL;
2313 g_variant_unref(result);
2318 int _bt_le_conn_update(unsigned char *device_address,
2319 float interval_min, float interval_max,
2320 guint16 latency, guint16 time_out)
2322 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2323 gchar *device_path = NULL;
2324 GError *error = NULL;
2325 GDBusProxy *device_proxy = NULL;
2326 GDBusConnection *conn;
2328 guint32 min, max, to;
2329 guint32 min_supervision_to;
2330 int ret = BLUETOOTH_ERROR_NONE;
2332 BT_CHECK_PARAMETER(device_address, return);
2334 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2335 interval_min, interval_max, latency, time_out);
2337 if (interval_min > interval_max ||
2338 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2339 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2340 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2344 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2345 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2346 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2350 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2351 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2356 * The Supervision_Timeout in milliseconds shall be larger than
2357 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2358 * where Conn_Interval_Max is given in milliseconds.
2360 min_supervision_to = (1 + latency) * interval_max * 2;
2361 if (time_out <= min_supervision_to) {
2362 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2366 _bt_convert_addr_type_to_string(address, device_address);
2368 BT_DBG("Remote device address: %s", address);
2370 device_path = _bt_get_device_object_path(address);
2372 if (device_path == NULL) {
2373 BT_ERR("device_path NULL");
2374 ret = BLUETOOTH_ERROR_INTERNAL;
2378 conn = _bt_gdbus_get_system_gconn();
2380 BT_ERR("conn NULL");
2381 ret = BLUETOOTH_ERROR_INTERNAL;
2385 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2386 NULL, BT_BLUEZ_NAME,
2387 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2389 g_free(device_path);
2390 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2392 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2393 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2394 to = time_out / BT_LE_CONN_TO_SPLIT;
2396 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2397 g_variant_new("(uuuu)", min, max, latency, to),
2398 G_DBUS_CALL_FLAGS_NONE,
2403 g_object_unref(device_proxy);
2404 if (reply == NULL) {
2406 BT_ERR("Error %s[%s]", error->message, address);
2407 if (g_strrstr(error->message, "In Progress"))
2408 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2410 ret = BLUETOOTH_ERROR_INTERNAL;
2411 g_error_free(error);
2415 g_variant_unref(reply);
2421 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2422 bluetooth_device_pin_code_t *pin_code)
2424 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2426 bt_pin_code_info_t *pin_info = NULL;
2428 BT_CHECK_PARAMETER(device_address, return);
2429 BT_CHECK_PARAMETER(pin_code, return);
2430 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2431 BLUETOOTH_ERROR_NO_RESOURCES);
2433 _bt_convert_addr_type_to_string(address, device_address->addr);
2435 for (l = pin_info_list; l != NULL; l = l->next) {
2438 if (g_strcmp0(pin_info->address, address) == 0) {
2439 g_free(pin_info->pin_code);
2440 pin_info->pin_code = g_strdup(pin_code->pin_code);
2441 return BLUETOOTH_ERROR_NONE;
2445 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2446 pin_info->address = g_strdup(address);
2447 pin_info->pin_code = g_strdup(pin_code->pin_code);
2448 pin_info_list = g_slist_append(pin_info_list, pin_info);
2450 return BLUETOOTH_ERROR_NONE;
2453 gint __bt_compare_address(gpointer *a, gpointer *b)
2455 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2456 char *address = (char *)b;
2457 return g_strcmp0(pin_info->address, address);
2460 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2462 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2464 bt_pin_code_info_t *pin_info = NULL;
2466 BT_CHECK_PARAMETER(device_address, return);
2468 _bt_convert_addr_type_to_string(address, device_address->addr);
2470 l = g_slist_find_custom(pin_info_list, address,
2471 (GCompareFunc)__bt_compare_address);
2475 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2476 g_free(pin_info->address);
2477 g_free(pin_info->pin_code);
2481 return BLUETOOTH_ERROR_NONE;
2484 int _bt_get_device_pin_code(const char *address, char *pin_code)
2488 BT_CHECK_PARAMETER(address, return);
2489 BT_CHECK_PARAMETER(pin_code, return);
2491 for (l = pin_info_list; l != NULL; l = l->next) {
2492 bt_pin_code_info_t *pin_info = l->data;
2494 if (g_strcmp0(pin_info->address, address) == 0) {
2495 g_strlcpy(pin_code, pin_info->pin_code,
2496 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2498 return BLUETOOTH_ERROR_NONE;
2502 return BLUETOOTH_ERROR_NOT_FOUND;
2505 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2506 bluetooth_le_connection_param_t *param)
2509 return BLUETOOTH_ERROR_INVALID_PARAM;
2511 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2512 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2513 return BLUETOOTH_ERROR_INVALID_PARAM;
2515 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2518 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2519 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2520 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2521 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2522 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2525 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2526 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2527 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2528 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2529 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2532 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2533 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2534 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2535 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2536 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2540 BT_ERR("Unhandled mode : %d", mode);
2544 return BLUETOOTH_ERROR_NONE;
2547 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2548 guint trusted_profile_flag, guint *trusted)
2554 case TRUSTED_PROFILE_PBAP:
2555 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2556 trust_profile = trusted_profile_flag & (1 << 1);
2558 return BLUETOOTH_ERROR_NOT_SUPPORT;
2560 case TRUSTED_PROFILE_MAP:
2561 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2562 trust_profile = trusted_profile_flag & (1 << 3);
2564 return BLUETOOTH_ERROR_NOT_SUPPORT;
2566 case TRUSTED_PROFILE_SAP:
2567 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2568 trust_profile = trusted_profile_flag & (1 << 5);
2570 return BLUETOOTH_ERROR_NOT_SUPPORT;
2572 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2573 *trusted = trusted_profile_flag;
2574 return BLUETOOTH_ERROR_NONE;
2576 return BLUETOOTH_ERROR_NOT_SUPPORT;
2582 return BLUETOOTH_ERROR_NONE;
2585 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2586 guint restricted_profile_flag, guint *restricted)
2588 int restrict_profile;
2589 *restricted = FALSE;
2592 case RESTRICTED_PROFILE_HFP_HS:
2593 restrict_profile = restricted_profile_flag & (1 << 0);
2595 case RESTRICTED_PROFILE_A2DP:
2596 restrict_profile = restricted_profile_flag & (1 << 2);
2599 return BLUETOOTH_ERROR_NOT_SUPPORT;
2602 if (restrict_profile)
2605 return BLUETOOTH_ERROR_NONE;
2608 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2611 case TRUSTED_PROFILE_PBAP:
2612 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2613 case TRUSTED_PROFILE_MAP:
2614 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2615 case TRUSTED_PROFILE_SAP:
2616 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2617 case TRUSTED_PROFILE_ALL:
2624 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2627 case RESTRICTED_PROFILE_HFP_HS:
2628 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2629 case RESTRICTED_PROFILE_A2DP:
2630 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2636 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2638 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2639 return TRUSTED_PROFILE_PBAP;
2640 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2641 return TRUSTED_PROFILE_MAP;
2642 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2643 return TRUSTED_PROFILE_SAP;
2645 return 0; /* 0 - Unknown Profile */
2648 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2649 bluetooth_trusted_profile_t profile, gboolean trust)
2651 int ret = BLUETOOTH_ERROR_NONE;
2652 GDBusConnection *conn;
2654 GError *error = NULL;
2655 char *device_path = NULL;
2657 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2660 BT_CHECK_PARAMETER(bd_addr, return);
2661 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2662 bd_addr->addr[0], bd_addr->addr[1],
2663 bd_addr->addr[2], bd_addr->addr[3],
2664 bd_addr->addr[4], bd_addr->addr[5],
2667 conn = _bt_gdbus_get_system_gconn();
2668 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2670 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2672 device_path = _bt_get_device_object_path(address);
2673 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2675 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2676 NULL, BT_BLUEZ_NAME, device_path,
2677 BT_DEVICE_INTERFACE, NULL, NULL);
2679 g_free(device_path);
2680 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2682 uuid = _bt_get_trusted_profile_uuid(profile);
2684 g_object_unref(proxy);
2685 return BLUETOOTH_ERROR_NOT_SUPPORT;
2688 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2689 g_variant_new("(sb)", uuid, trust),
2690 G_DBUS_CALL_FLAGS_NONE, -1,
2692 g_object_unref(proxy);
2694 if (reply == NULL) {
2695 BT_ERR("Failed to Set Profile Trusted");
2696 ret = BLUETOOTH_ERROR_INTERNAL;
2698 BT_ERR("Error %s[%s]", error->message, address);
2699 g_error_free(error);
2703 g_variant_unref(reply);
2710 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2711 bluetooth_trusted_profile_t profile, guint *trust)
2713 int ret = BLUETOOTH_ERROR_NONE;
2714 GDBusConnection *conn;
2716 GError *error = NULL;
2717 char *device_path = NULL;
2718 guint trusted_profile_flag;
2719 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2722 BT_CHECK_PARAMETER(bd_addr, return);
2723 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2724 bd_addr->addr[0], bd_addr->addr[1],
2725 bd_addr->addr[2], bd_addr->addr[3],
2726 bd_addr->addr[4], bd_addr->addr[5],
2729 conn = _bt_gdbus_get_system_gconn();
2730 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2732 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2734 device_path = _bt_get_device_object_path(address);
2735 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2737 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2738 NULL, BT_BLUEZ_NAME, device_path,
2739 BT_PROPERTIES_INTERFACE, NULL, NULL);
2741 g_free(device_path);
2742 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2744 reply = g_dbus_proxy_call_sync(proxy, "Get",
2745 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2746 G_DBUS_CALL_FLAGS_NONE, -1,
2748 g_object_unref(proxy);
2750 if (reply == NULL) {
2751 BT_ERR("Failed to Get Profile Trusted");
2752 ret = BLUETOOTH_ERROR_INTERNAL;
2754 BT_ERR("Error %s[%s]", error->message, address);
2755 g_error_free(error);
2760 g_variant_get(reply, "(v)", &temp);
2761 trusted_profile_flag = g_variant_get_uint32(temp);
2762 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2764 ret = _bt_get_trusted_profile_from_flag(profile,
2765 trusted_profile_flag, trust);
2766 g_variant_unref(temp);
2767 g_variant_unref(reply);
2770 BT_DBG("TRUST %d", *trust);
2774 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2775 bluetooth_restricted_profile_t profile, gboolean restricted)
2777 int ret = BLUETOOTH_ERROR_NONE;
2778 GDBusConnection *conn;
2780 GError *error = NULL;
2781 char *device_path = NULL;
2783 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2786 BT_CHECK_PARAMETER(bd_addr, return);
2787 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2788 bd_addr->addr[0], bd_addr->addr[1],
2789 bd_addr->addr[2], bd_addr->addr[3],
2790 bd_addr->addr[4], bd_addr->addr[5],
2791 profile, restricted);
2793 conn = _bt_gdbus_get_system_gconn();
2794 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2796 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2798 device_path = _bt_get_device_object_path(address);
2799 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2801 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2802 NULL, BT_BLUEZ_NAME, device_path,
2803 BT_DEVICE_INTERFACE, NULL, NULL);
2805 g_free(device_path);
2806 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2808 uuid = _bt_get_restricted_profile_uuid(profile);
2810 g_object_unref(proxy);
2811 return BLUETOOTH_ERROR_NOT_SUPPORT;
2814 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2815 g_variant_new("(sb)", uuid, restricted),
2816 G_DBUS_CALL_FLAGS_NONE, -1,
2818 g_object_unref(proxy);
2820 if (reply == NULL) {
2821 BT_ERR("Failed to Set Profile Restricted");
2822 ret = BLUETOOTH_ERROR_INTERNAL;
2824 BT_ERR("Error %s[%s]", error->message, address);
2825 g_error_free(error);
2829 g_variant_unref(reply);
2836 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2837 bluetooth_restricted_profile_t profile, guint *restricted)
2839 int ret = BLUETOOTH_ERROR_NONE;
2840 GDBusConnection *conn;
2842 GError *error = NULL;
2843 char *device_path = NULL;
2844 guint restricted_profile_flag;
2845 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2848 BT_CHECK_PARAMETER(bd_addr, return);
2849 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2850 bd_addr->addr[0], bd_addr->addr[1],
2851 bd_addr->addr[2], bd_addr->addr[3],
2852 bd_addr->addr[4], bd_addr->addr[5],
2853 profile, *restricted);
2855 conn = _bt_gdbus_get_system_gconn();
2856 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2858 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2860 device_path = _bt_get_device_object_path(address);
2861 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2863 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2864 NULL, BT_BLUEZ_NAME, device_path,
2865 BT_PROPERTIES_INTERFACE, NULL, NULL);
2867 g_free(device_path);
2868 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2870 reply = g_dbus_proxy_call_sync(proxy, "Get",
2871 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2872 G_DBUS_CALL_FLAGS_NONE, -1,
2874 g_object_unref(proxy);
2876 if (reply == NULL) {
2877 BT_ERR("Failed to Get Profile Restricted");
2878 ret = BLUETOOTH_ERROR_INTERNAL;
2880 BT_ERR("Error %s[%s]", error->message, address);
2881 g_error_free(error);
2886 g_variant_get(reply, "(v)", &temp);
2887 restricted_profile_flag = g_variant_get_uint32(temp);
2888 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2890 ret = _bt_get_restricted_profile_from_flag(profile,
2891 restricted_profile_flag, restricted);
2892 g_variant_unref(temp);
2893 g_variant_unref(reply);
2896 BT_DBG("TRUST %d", *restricted);
2900 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2904 GVariant *out_param1;
2905 request_info_t *req_info;
2906 GVariant *val = NULL;
2907 GVariant *param = NULL;
2910 bluetooth_device_address_t device_addr = { {0} };
2911 int result = BLUETOOTH_ERROR_NONE;
2914 val = g_dbus_proxy_call_finish(proxy, res, &err);
2916 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2917 if (req_info == NULL) {
2918 BT_ERR("req_info == NULL");
2919 g_object_unref(proxy);
2920 att_mtu_req_info->device_proxy = NULL;
2925 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2927 if (g_strrstr(err->message, "NotSupported")) {
2928 BT_INFO("Connection Not Supported");
2929 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2930 } else if (g_strrstr(err->message, "NotConnected")) {
2931 BT_INFO("Not connected");
2932 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2933 } else if (g_strrstr(err->message, "InvalidArguments")) {
2934 BT_INFO("Not connected");
2935 result = BLUETOOTH_ERROR_INVALID_PARAM;
2937 BT_DBG("Default case");
2938 result = BLUETOOTH_ERROR_INTERNAL;
2942 g_object_unref(proxy);
2943 att_mtu_req_info->device_proxy = NULL;
2945 if (result != BLUETOOTH_ERROR_NONE)
2949 g_variant_get(val, "(qy)", &mtu, &status);
2950 g_variant_unref(val);
2953 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
2955 param = g_variant_new("(isqy)",
2957 att_mtu_req_info->addr,
2961 /* Send the event to application */
2962 _bt_send_event(BT_DEVICE_EVENT,
2963 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2967 if (req_info->context == NULL)
2970 _bt_convert_addr_string_to_type(device_addr.addr,
2971 (const char *)att_mtu_req_info->addr);
2973 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
2974 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
2975 g_dbus_method_invocation_return_value(req_info->context,
2976 g_variant_new("(iv)", result, out_param1));
2978 _bt_delete_request_list(req_info->req_id);
2983 g_free(att_mtu_req_info->addr);
2984 g_free(att_mtu_req_info);
2985 att_mtu_req_info = NULL;
2990 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
2993 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2994 gchar *device_path = NULL;
2995 GDBusProxy *adapter_proxy;
2996 GDBusProxy *device_proxy;
2997 GDBusConnection *conn;
2998 int ret = BLUETOOTH_ERROR_NONE;
3000 BT_CHECK_PARAMETER(device_address, return);
3002 if (att_mtu_req_info) {
3003 BT_ERR("ATT MTU request in progress");
3004 return BLUETOOTH_ERROR_DEVICE_BUSY;
3007 conn = _bt_gdbus_get_system_gconn();
3008 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3010 adapter_proxy = _bt_get_adapter_proxy();
3011 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3013 _bt_convert_addr_type_to_string(address, device_address->addr);
3015 BT_DBG("Remote device address: %s", address);
3017 device_path = _bt_get_device_object_path(address);
3019 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3021 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3022 NULL, BT_BLUEZ_NAME,
3023 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3024 g_free(device_path);
3025 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3027 if (device_path == NULL) {
3028 BT_DBG("device_path NULL");
3029 ret = BLUETOOTH_ERROR_INTERNAL;
3033 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3034 att_mtu_req_info->addr = (char *)g_strdup(address);
3035 att_mtu_req_info->req_id = request_id;
3036 att_mtu_req_info->device_proxy = device_proxy;
3038 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3039 g_variant_new("(q)", mtu),
3040 G_DBUS_CALL_FLAGS_NONE,
3041 BT_MAX_DBUS_TIMEOUT,
3043 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3049 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3052 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3053 char *object_path = NULL;
3055 GDBusProxy *device_proxy;
3056 GError *error = NULL;
3058 GDBusConnection *conn;
3059 GVariant *result = NULL;
3061 BT_CHECK_PARAMETER(device_address, return);
3063 conn = _bt_gdbus_get_system_gconn();
3064 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3066 _bt_convert_addr_type_to_string(address, device_address->addr);
3068 object_path = _bt_get_device_object_path(address);
3069 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3071 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3072 NULL, BT_BLUEZ_NAME, object_path,
3073 BT_PROPERTIES_INTERFACE, NULL, NULL);
3074 g_free(object_path);
3075 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3077 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3078 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3079 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3081 ERR("DBus Error : %s", error->message);
3082 g_clear_error(&error);
3083 g_object_unref(device_proxy);
3084 return BLUETOOTH_ERROR_INTERNAL;
3086 g_variant_get(result, "(v)", &value);
3087 *mtu = g_variant_get_uint16(value);
3088 BT_DBG("ATT MTU : %d", *mtu);
3089 g_variant_unref(value);
3090 g_variant_unref(result);
3093 g_object_unref(device_proxy);
3094 return BLUETOOTH_ERROR_NONE;
3097 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3098 bluetooth_device_address_t *id_address)
3100 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3101 gchar *device_path = NULL;
3102 const gchar *idaddress = NULL;
3103 GDBusProxy *device_proxy;
3104 GError *error = NULL;
3105 GVariant *result = NULL;
3106 GDBusConnection *conn;
3107 int ret = BLUETOOTH_ERROR_NONE;
3109 BT_CHECK_PARAMETER(device_address, return);
3111 conn = _bt_gdbus_get_system_gconn();
3112 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3114 _bt_convert_addr_type_to_string(address, device_address->addr);
3116 device_path = _bt_get_device_object_path(address);
3117 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3119 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3120 NULL, BT_BLUEZ_NAME,
3121 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3122 g_free(device_path);
3123 if (!device_proxy) {
3124 BT_ERR("Unable to get proxy");
3125 return BLUETOOTH_ERROR_INTERNAL;
3128 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3129 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3130 if (result == NULL) {
3131 BT_ERR("Failed to get device ID address");
3132 if (error != NULL) {
3133 BT_ERR("Error occured in Proxy call [%s]", error->message);
3134 g_error_free(error);
3136 g_object_unref(device_proxy);
3137 return BLUETOOTH_ERROR_INTERNAL;
3140 g_variant_get(result , "(&s)", &idaddress);
3141 if (idaddress == NULL) {
3142 BT_ERR("No paired device");
3143 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3145 DBG_SECURE("ID Address : %s", idaddress);
3146 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3149 g_variant_unref(result);
3150 g_object_unref(device_proxy);
3155 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3157 GapAgentPrivate *agent = _bt_get_adapter_agent();
3158 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3160 if (authentication_reply)
3161 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3163 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3165 BT_DBG("BT_PASSKEY_REPLY");
3166 return BLUETOOTH_ERROR_NONE;
3169 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3171 GapAgentPrivate *agent = _bt_get_adapter_agent();
3172 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3174 if (confirmation_reply)
3175 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3177 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3179 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3180 return BLUETOOTH_ERROR_NONE;