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 = g_strdup(address);
494 dev_info->name = g_strdup(name);
496 g_variant_unref(value);
498 BT_ERR("result is NULL\n");
506 char *_bt_get_bonded_device_name(char *address)
508 bluetooth_device_address_t device_address = { {0} };
509 bluetooth_device_info_t dev_info;
511 retv_if(address == NULL, strdup(""));
513 _bt_convert_addr_string_to_type(device_address.addr, address);
515 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
517 _bt_get_bonded_device_info(&device_address, &dev_info);
519 return g_strdup(dev_info.device_name.name);
522 static gboolean __ignore_auto_pairing_request(const char *address)
527 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
538 /* Get the LAP(Lower Address part) */
539 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
541 snprintf(lap_address, sizeof(lap_address), ",%s", address);
543 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
546 BT_ERR("fopen failed \n");
550 fseek(fp, 0, SEEK_END);
555 BT_ERR("Get file size failed \n");
560 buffer = g_malloc0(sizeof(char) * size);
561 result = fread((char *)buffer, 1, size, fp);
563 if (result != size) {
564 BT_ERR("Read Error\n");
569 BT_DBG("Buffer = %s\n", buffer);
571 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
577 /* Write the data and insert new device data */
578 for (i = 0; lines[i] != NULL; i++) {
579 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
580 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
582 lines[i] = temp_buffer;
585 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
587 /* Fix : NULL_RETURNS */
588 retv_if(buffer == NULL, FALSE);
590 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
593 BT_ERR("fopen failed \n");
598 BT_DBG("Buffer = %s\n", buffer);
599 fwrite(buffer, 1, strlen(buffer), fp);
609 static int __bt_cancel_bonding(void)
611 BT_CHECK_PARAMETER(bonding_info, return);
612 BT_CHECK_PARAMETER(bonding_info->addr, return);
613 /* First Cancel the ongoing pairing in bluez otherwise if we send
614 * pair request bluez will send inprogress and we again retry bond so
615 * this cycle continues */
617 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
618 NULL, G_DBUS_CALL_FLAGS_NONE,
621 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
623 return BLUETOOTH_ERROR_PARING_FAILED;
625 return BLUETOOTH_ERROR_NONE;
628 static int __bt_retry_bond(void)
630 BT_CHECK_PARAMETER(bonding_info, return);
631 BT_CHECK_PARAMETER(bonding_info->addr, return);
633 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
634 g_variant_new("(y)", bonding_info->conn_type),
635 G_DBUS_CALL_FLAGS_NONE,
638 (GAsyncReadyCallback)__bt_bond_device_cb,
641 return BLUETOOTH_ERROR_NONE;
645 static int __bt_remove_and_bond(void)
647 GDBusProxy *adapter_proxy;
648 GVariant *result = NULL;
650 char *device_path = NULL;
652 BT_CHECK_PARAMETER(bonding_info, return);
653 BT_CHECK_PARAMETER(bonding_info->addr, return);
655 adapter_proxy = _bt_get_adapter_proxy();
656 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
658 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
659 g_variant_new("(s)", bonding_info->addr),
660 G_DBUS_CALL_FLAGS_NONE,
665 return BLUETOOTH_ERROR_INTERNAL;
667 g_variant_get(result , "(o)", &device_path);
668 g_variant_unref(result);
670 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
672 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
673 g_variant_new("(o)", device_path),
674 G_DBUS_CALL_FLAGS_NONE,
680 BT_ERR("UnpairDevice Fail: %s", err->message);
682 return BLUETOOTH_ERROR_INTERNAL;
685 return __bt_retry_bond();
688 static int __bt_cancel_and_bond(void)
690 int ret = BLUETOOTH_ERROR_NONE;
692 ret = _bt_agent_reply_cancellation();
693 if (ret != BLUETOOTH_ERROR_NONE) {
694 BT_ERR("Fail to call reply cancellation");
697 return __bt_retry_bond();
701 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
704 int result = BLUETOOTH_ERROR_NONE;
707 GVariant *out_param1;
708 request_info_t *req_info;
709 bluetooth_device_info_t dev_info;
710 bt_remote_dev_info_t *remote_dev_info;
711 GVariant *manufacture_data;
714 /* Terminate ALL system popup */
715 syspopup_destroy_all();
717 reply = g_dbus_proxy_call_finish(proxy, res, &err);
719 g_variant_unref(reply);
721 is_device_creating = FALSE;
723 if (bonding_info == NULL) {
725 BT_ERR("bonding_info == NULL");
731 req_info = _bt_get_request_info(bonding_info->req_id);
732 if (req_info == NULL) {
733 BT_ERR("req_info == NULL");
738 g_dbus_error_strip_remote_error(err);
739 BT_ERR("Error occured in CreateBonding [%s]", err->message);
741 if (g_strrstr(err->message, "Already Exists")) {
742 BT_INFO("Existing Bond, remove and retry");
743 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
745 result = BLUETOOTH_ERROR_PARING_FAILED;
746 } else if (_bt_agent_is_canceled() ||
747 g_strrstr(err->message, "Authentication Canceled")) {
748 BT_INFO("Cancelled by USER");
749 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
750 } else if (g_strrstr(err->message, "Authentication Rejected")) {
752 result = BLUETOOTH_ERROR_ACCESS_DENIED;
753 } else if (g_strrstr(err->message, "In Progress")) {
754 BT_INFO("Bond in progress, cancel and retry");
755 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
757 result = BLUETOOTH_ERROR_PARING_FAILED;
758 } else if (g_strrstr(err->message, "Authentication Failed")) {
759 BT_INFO("Authentication Failed");
760 if (bonding_info->is_autopair == TRUE) {
761 _bt_set_autopair_status_in_bonding_info(FALSE);
762 __ignore_auto_pairing_request(bonding_info->addr);
764 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
765 } else if (g_strrstr(err->message, "Page Timeout")) {
766 BT_INFO("Page Timeout");
767 /* This is the special case
768 As soon as call bluetooth_bond_device, try to cancel bonding.
769 In this case, before completing to call 'CreatePairedDevice' method
770 the procedure is stopped. So 'Cancle' error is not return.
772 result = BLUETOOTH_ERROR_HOST_DOWN;
773 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
774 BT_INFO("Cancel already running bonding");
775 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
776 BT_INFO("Error while Cancelling bonding");
777 /* we need to unref proxy so continue */
779 result = BLUETOOTH_ERROR_INTERNAL;
780 } else if (g_strrstr(err->message, "Connection Timeout")) {
781 /* Pairing request timeout */
782 result = BLUETOOTH_ERROR_TIMEOUT;
783 } else if (g_strrstr(err->message, "Authentication Timeout")) {
784 /* Pairing request timeout */
785 result = BLUETOOTH_ERROR_TIMEOUT;
787 BT_DBG("Default case");
788 result = BLUETOOTH_ERROR_PARING_FAILED;
792 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
793 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
794 result == BLUETOOTH_ERROR_TIMEOUT ||
795 result == BLUETOOTH_ERROR_HOST_DOWN) {
796 #ifdef TIZEN_PROFILE_WEARABLE
797 int is_sw_running = 0;
799 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
800 BT_ERR("vconf_get_int for setup wizard state failed");
803 __bt_launch_unable_to_pairing_syspopup(result);
805 BT_ERR("Unable to pair");
807 bonding_info->result = result;
810 g_object_unref(proxy);
811 bonding_info->device_proxy = NULL;
813 if (result != BLUETOOTH_ERROR_NONE) {
814 if (bonding_info->bonding_wo_discovery) {
815 GDBusProxy *adapter_proxy;
816 GVariant *ret = NULL;
817 GError *error = NULL;
820 BT_ERR("Bond was tried without discovery. Remove it");
822 adapter_proxy = _bt_get_adapter_proxy();
823 if (adapter_proxy == NULL) {
824 BT_ERR("Cannot get adapter_proxy");
828 device_path = _bt_get_device_object_path(bonding_info->addr);
829 if (device_path == NULL) {
830 BT_ERR("Cannot get device path");
834 ret = g_dbus_proxy_call_sync(adapter_proxy,
836 g_variant_new("(o)", device_path),
837 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
840 BT_ERR("RemoveDevice Fail: %s", error->message);
841 g_clear_error(&error);
845 g_variant_unref(ret);
853 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
854 if (!remote_dev_info)
857 GVariant *uuids = NULL;
858 GVariantBuilder *builder = NULL;
860 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
861 for (i = 0; i < remote_dev_info->uuid_count; i++) {
862 g_variant_builder_add(builder, "s",
863 remote_dev_info->uuids[i]);
865 uuids = g_variant_new("as", builder);
866 g_variant_builder_unref(builder);
867 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
868 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
871 param = g_variant_new("(isunsbub@asn@ay)",
874 remote_dev_info->class,
875 remote_dev_info->rssi,
876 remote_dev_info->name,
877 remote_dev_info->paired,
878 remote_dev_info->connected,
879 remote_dev_info->trust,
881 remote_dev_info->manufacturer_data_len,
885 /* Send the event to application */
886 _bt_send_event(BT_ADAPTER_EVENT,
887 BLUETOOTH_EVENT_BONDING_FINISHED,
890 _bt_free_device_info(remote_dev_info);
893 if (req_info->context == NULL)
896 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
897 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
900 if (_bt_adapter_get_status() != BT_ACTIVATED)
901 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
903 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
904 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
906 g_dbus_method_invocation_return_value(req_info->context,
907 g_variant_new("(iv)", result, out_param1));
909 g_variant_unref(out_param1);
911 _bt_delete_request_list(req_info->req_id);
916 _bt_agent_set_canceled(FALSE);
919 g_free(bonding_info->addr);
920 g_free(bonding_info);
924 int _bt_bond_device(int request_id,
925 bluetooth_device_address_t *device_address,
926 unsigned short conn_type, GArray **out_param1)
929 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
930 bluetooth_device_info_t dev_info;
932 GDBusConnection *conn;
933 char *device_path = NULL;
934 GDBusProxy *adapter_proxy;
935 GError *error = NULL;
936 gboolean bonding_wo_discovery = FALSE;
938 BT_CHECK_PARAMETER(device_address, return);
941 BT_ERR("Bonding in progress");
943 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
944 memcpy(dev_info.device_address.addr, device_address->addr,
945 BLUETOOTH_ADDRESS_LENGTH);
947 g_array_append_vals(*out_param1, &dev_info,
948 sizeof(bluetooth_device_info_t));
950 return BLUETOOTH_ERROR_DEVICE_BUSY;
953 conn = _bt_gdbus_get_system_gconn();
954 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
956 _bt_convert_addr_type_to_string(address, device_address->addr);
958 device_path = _bt_get_device_object_path(address);
960 if (device_path == NULL) {
961 BT_ERR("No searched device");
962 GVariant *ret = NULL;
963 adapter_proxy = _bt_get_adapter_proxy();
964 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
966 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
967 g_variant_new("(s)", address),
968 G_DBUS_CALL_FLAGS_NONE,
974 BT_ERR("CreateDevice Fail: %s", error->message);
975 g_clear_error(&error);
978 g_variant_unref(ret);
979 device_path = _bt_get_device_object_path(address);
980 if (device_path == NULL) {
981 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
982 memcpy(dev_info.device_address.addr, device_address->addr,
983 BLUETOOTH_ADDRESS_LENGTH);
985 g_array_append_vals(*out_param1, &dev_info,
986 sizeof(bluetooth_device_info_t));
988 return BLUETOOTH_ERROR_NOT_PAIRED;
990 BT_INFO("device_path is created[%s]", device_path);
992 bonding_wo_discovery = TRUE;
995 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
997 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1000 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1002 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1003 bonding_info->addr = g_strdup(address);
1004 bonding_info->req_id = request_id;
1006 bonding_info->device_proxy = proxy;
1007 bonding_info->conn_type = conn_type;
1008 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1010 is_device_creating = TRUE;
1012 g_dbus_proxy_call(proxy, "Pair",
1013 g_variant_new("(y)", conn_type),
1014 G_DBUS_CALL_FLAGS_NONE,
1015 BT_MAX_DBUS_TIMEOUT,
1017 (GAsyncReadyCallback)__bt_bond_device_cb,
1020 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1022 return BLUETOOTH_ERROR_NONE;
1024 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1025 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1026 bonding_info->addr);
1028 g_array_append_vals(*out_param1, &dev_info,
1029 sizeof(bluetooth_device_info_t));
1031 is_device_creating = FALSE;
1033 g_free(bonding_info->addr);
1034 g_free(bonding_info);
1035 bonding_info = NULL;
1037 return BLUETOOTH_ERROR_INTERNAL;*/
1040 int _bt_cancel_bonding(void)
1042 int ret = BLUETOOTH_ERROR_NONE;
1044 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1046 ret = _bt_agent_reply_cancellation();
1047 if (ret != BLUETOOTH_ERROR_NONE) {
1048 BT_ERR("Fail to call reply cancellation");
1052 _bt_agent_set_canceled(TRUE);
1054 return BLUETOOTH_ERROR_NONE;
1057 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1062 GVariant *out_param1;
1063 int result = BLUETOOTH_ERROR_NONE;
1064 bt_funcion_data_t *unbonding_info;
1065 bluetooth_device_info_t dev_info;
1066 request_info_t *req_info;
1068 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1070 g_variant_unref(reply);
1072 unbonding_info = user_data;
1074 if (unbonding_info == NULL) {
1076 BT_ERR("unbonding_info == NULL");
1080 req_info = _bt_get_request_info(unbonding_info->req_id);
1081 if (req_info == NULL) {
1082 BT_ERR("req_info == NULL");
1087 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1088 result = BLUETOOTH_ERROR_INTERNAL;
1091 if (req_info->context == NULL)
1094 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1095 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1096 unbonding_info->addr);
1098 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1099 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1101 g_dbus_method_invocation_return_value(req_info->context,
1102 g_variant_new("(iv)", result, out_param1));
1105 _bt_delete_request_list(req_info->req_id);
1111 if (unbonding_info) {
1112 g_free(unbonding_info->addr);
1113 g_free(unbonding_info);
1117 int _bt_unbond_device(int request_id,
1118 bluetooth_device_address_t *device_address,
1119 GArray **out_param1)
1121 char *device_path = NULL;
1122 bt_funcion_data_t *unbonding_info;
1123 GDBusProxy *adapter_proxy = NULL;
1124 GDBusProxy *device_proxy = NULL;
1125 GDBusConnection *conn;
1126 int result = BLUETOOTH_ERROR_INTERNAL;
1127 bluetooth_device_info_t dev_info;
1128 GError *error = NULL;
1129 GVariant *ret = NULL;
1131 BT_CHECK_PARAMETER(device_address, return);
1133 adapter_proxy = _bt_get_adapter_proxy();
1134 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1136 /* allocate user data so that it can be retrieved in callback */
1137 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1138 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1139 unbonding_info->req_id = request_id;
1141 _bt_convert_addr_type_to_string(unbonding_info->addr,
1142 device_address->addr);
1144 device_path = _bt_get_device_object_path(unbonding_info->addr);
1146 if (device_path == NULL) {
1147 BT_ERR("No paired device");
1148 result = BLUETOOTH_ERROR_NOT_PAIRED;
1152 conn = _bt_gdbus_get_system_gconn();
1154 BT_ERR("conn is NULL");
1155 result = BLUETOOTH_ERROR_INTERNAL;
1159 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1160 NULL, BT_BLUEZ_NAME,
1161 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1163 if (device_proxy != NULL) {
1165 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1166 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1167 G_DBUS_CALL_FLAGS_NONE,
1172 BT_ERR("Getting property failed: [%s]\n", error->message);
1173 g_error_free(error);
1174 result = BLUETOOTH_ERROR_NOT_PAIRED;
1178 BT_ERR("No paired device");
1179 g_object_unref(device_proxy);
1180 result = BLUETOOTH_ERROR_NOT_PAIRED;
1183 g_variant_unref(ret);
1185 g_object_unref(device_proxy);
1188 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1189 g_variant_new("(o)", device_path),
1190 G_DBUS_CALL_FLAGS_NONE,
1191 BT_MAX_DBUS_TIMEOUT,
1193 (GAsyncReadyCallback)__bt_unbond_cb,
1196 g_free(device_path);
1197 return BLUETOOTH_ERROR_NONE;
1200 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1201 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1202 unbonding_info->addr);
1204 g_array_append_vals(*out_param1, &dev_info,
1205 sizeof(bluetooth_device_info_t));
1206 g_free(device_path);
1207 g_free(unbonding_info->addr);
1208 g_free(unbonding_info);
1212 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1217 GVariant *out_param1;
1218 int result = BLUETOOTH_ERROR_NONE;
1219 bluetooth_device_info_t dev_info;
1220 bt_remote_dev_info_t *remote_dev_info;
1221 request_info_t *req_info;
1222 GVariant *uuid_list, *manufacture_data;
1224 GVariantBuilder *builder = NULL;
1227 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1229 g_variant_unref(reply);
1231 g_object_unref(proxy);
1233 if (searching_info == NULL) {
1235 BT_ERR("unbonding_info == NULL");
1239 req_info = _bt_get_request_info(searching_info->req_id);
1240 if (req_info == NULL) {
1241 BT_ERR("req_info == NULL");
1246 g_dbus_error_strip_remote_error(err);
1247 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1249 if (g_strrstr("Operation canceled", err->message))
1250 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1251 else if (g_strrstr("In Progress", err->message))
1252 result = BLUETOOTH_ERROR_IN_PROGRESS;
1253 else if (g_strrstr("Host is down", err->message))
1254 result = BLUETOOTH_ERROR_HOST_DOWN;
1256 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1258 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1259 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1260 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1261 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1262 remote_dev_info->uuid_count > 0) {
1263 result = BLUETOOTH_ERROR_NONE;
1266 _bt_free_device_info(remote_dev_info);
1271 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1272 if (!remote_dev_info)
1276 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1277 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1278 g_variant_builder_add(builder, "s",
1279 remote_dev_info->uuids[i]);
1281 uuid_list = g_variant_new("as", builder);
1282 g_variant_builder_unref(builder);
1283 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1284 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1287 param = g_variant_new("(isunsbub@asn@ay)",
1289 searching_info->addr,
1290 remote_dev_info->class,
1291 remote_dev_info->rssi,
1292 remote_dev_info->name,
1293 remote_dev_info->paired,
1294 remote_dev_info->connected,
1295 remote_dev_info->trust,
1297 remote_dev_info->manufacturer_data_len,
1300 /* Send the event to application */
1301 _bt_send_event(BT_ADAPTER_EVENT,
1302 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1305 _bt_free_device_info(remote_dev_info);
1308 if (req_info->context == NULL)
1311 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1312 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1313 searching_info->addr);
1315 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1316 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1318 g_dbus_method_invocation_return_value(req_info->context,
1319 g_variant_new("(iv)", result, out_param1));
1322 _bt_delete_request_list(req_info->req_id);
1327 if (searching_info) {
1328 g_free(searching_info->addr);
1329 g_free(searching_info);
1330 searching_info = NULL;
1334 int _bt_search_device(int request_id,
1335 bluetooth_device_address_t *device_address)
1337 char *device_path = NULL;
1338 GDBusProxy *device_proxy = NULL;
1339 GDBusConnection *conn;
1341 GDBusProxy *adapter_proxy;
1342 int result = BLUETOOTH_ERROR_INTERNAL;
1344 BT_CHECK_PARAMETER(device_address, return);
1347 BT_ERR("Bonding in progress");
1348 return BLUETOOTH_ERROR_DEVICE_BUSY;
1351 if (searching_info) {
1352 BT_ERR("Service searching in progress");
1353 return BLUETOOTH_ERROR_DEVICE_BUSY;
1356 adapter_proxy = _bt_get_adapter_proxy();
1357 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1359 /* allocate user data so that it can be retrieved in callback */
1360 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1361 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1362 searching_info->req_id = request_id;
1364 _bt_convert_addr_type_to_string(searching_info->addr,
1365 device_address->addr);
1367 conn = _bt_gdbus_get_system_gconn();
1368 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1371 device_path = _bt_get_device_object_path(searching_info->addr);
1373 if (device_path == NULL) {
1374 BT_ERR("No paired device");
1375 result = BLUETOOTH_ERROR_NOT_PAIRED;
1379 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1380 NULL, BT_BLUEZ_NAME,
1381 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1382 g_free(device_path);
1383 if (device_proxy == NULL) {
1384 result = BLUETOOTH_ERROR_INTERNAL;
1388 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1389 g_variant_new("(s)", ""),
1390 G_DBUS_CALL_FLAGS_NONE,
1391 BT_MAX_DBUS_TIMEOUT,
1393 (GAsyncReadyCallback)__bt_discover_cb,
1396 searching_info->device_proxy = device_proxy;
1398 return BLUETOOTH_ERROR_NONE;
1401 g_free(searching_info->addr);
1402 g_free(searching_info);
1403 searching_info = NULL;
1407 int _bt_cancel_search_device(void)
1409 GVariant *ret = NULL;
1412 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1414 if (searching_info->device_proxy) {
1415 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1417 G_DBUS_CALL_FLAGS_NONE,
1422 g_variant_unref(ret);
1424 __bt_cancel_search_service_done();
1426 return BLUETOOTH_ERROR_NONE;
1429 int _bt_set_alias(bluetooth_device_address_t *device_address,
1432 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1433 gchar *device_path = NULL;
1434 GDBusProxy *adapter_proxy;
1435 GDBusProxy *device_proxy;
1436 GVariant *ret = NULL;
1437 GError *error = NULL;
1438 GDBusConnection *conn;
1440 BT_CHECK_PARAMETER(device_address, return);
1441 BT_CHECK_PARAMETER(alias, return);
1443 adapter_proxy = _bt_get_adapter_proxy();
1444 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1446 conn = _bt_gdbus_get_system_gconn();
1447 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1449 _bt_convert_addr_type_to_string(address, device_address->addr);
1451 device_path = _bt_get_device_object_path(address);
1453 if (device_path == NULL) {
1454 BT_ERR("No paired device");
1455 return BLUETOOTH_ERROR_NOT_PAIRED;
1458 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1459 NULL, BT_BLUEZ_NAME,
1460 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1462 g_free(device_path);
1463 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1465 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1466 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1467 G_DBUS_CALL_FLAGS_NONE,
1472 g_variant_unref(ret);
1474 g_object_unref(device_proxy);
1477 BT_ERR("SetProperty error: [%s]", error->message);
1478 g_error_free(error);
1479 return BLUETOOTH_ERROR_INTERNAL;
1482 return BLUETOOTH_ERROR_NONE;
1485 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1488 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1489 gchar *device_path = NULL;
1490 GDBusProxy *device_proxy;
1491 gboolean previous_value;
1492 GError *error = NULL;
1493 GDBusConnection *conn;
1494 GVariant *result = NULL;
1495 GVariant *temp = NULL;
1496 int ret = BLUETOOTH_ERROR_NONE;
1498 BT_CHECK_PARAMETER(device_address, return);
1500 conn = _bt_gdbus_get_system_gconn();
1501 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1503 _bt_convert_addr_type_to_string(address, device_address->addr);
1505 device_path = _bt_get_device_object_path(address);
1507 if (device_path == NULL) {
1508 BT_ERR("No paired device");
1509 return BLUETOOTH_ERROR_NOT_PAIRED;
1512 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1513 NULL, BT_BLUEZ_NAME,
1514 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1516 g_free(device_path);
1517 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1519 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1520 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1521 G_DBUS_CALL_FLAGS_NONE,
1525 if (error != NULL) {
1526 BT_ERR("Getting property failed: [%s]\n", error->message);
1527 g_error_free(error);
1528 g_object_unref(device_proxy);
1529 return BLUETOOTH_ERROR_INTERNAL;
1532 g_variant_get(result, "(v)", &temp);
1533 previous_value = g_variant_get_boolean(temp);
1534 g_variant_unref(temp);
1535 g_variant_unref(result);
1536 /* If the input is same with previous value, return error. */
1537 if (previous_value == authorize) {
1538 BT_ERR("Same value: %d", previous_value);
1539 g_object_unref(device_proxy);
1540 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1544 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1545 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1546 G_DBUS_CALL_FLAGS_NONE,
1551 g_object_unref(device_proxy);
1553 BT_ERR("SetProperty error: [%s]", error->message);
1554 g_error_free(error);
1555 ret = BLUETOOTH_ERROR_INTERNAL;
1559 g_variant_unref(result);
1564 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1565 gboolean *is_connected)
1567 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1568 char *object_path = NULL;
1570 GDBusProxy *device_proxy;
1571 GError *error = NULL;
1573 GDBusConnection *conn;
1574 GVariant *result = NULL;
1575 int ret = BLUETOOTH_ERROR_NONE;
1577 BT_CHECK_PARAMETER(device_address, return);
1579 conn = _bt_gdbus_get_system_gconn();
1580 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1582 _bt_convert_addr_type_to_string(address, device_address->addr);
1584 object_path = _bt_get_device_object_path(address);
1585 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1587 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1588 NULL, BT_BLUEZ_NAME, object_path,
1589 BT_PROPERTIES_INTERFACE, NULL, NULL);
1590 g_free(object_path);
1591 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1593 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1594 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1596 G_DBUS_CALL_FLAGS_NONE,
1600 if (result == NULL) {
1601 if (error != NULL) {
1602 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1603 g_error_free(error);
1605 g_object_unref(device_proxy);
1606 return BLUETOOTH_ERROR_INTERNAL;
1609 g_variant_get(result, "(v)", &value);
1610 *is_connected = g_variant_get_boolean(value);
1611 g_variant_unref(result);
1613 BT_DBG("gatt is connected : %d", *is_connected);
1615 g_variant_unref(value);
1616 g_object_unref(device_proxy);
1621 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1622 int connection_type, gboolean *is_connected)
1624 char *object_path = NULL;
1625 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1626 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1627 GDBusProxy *device_proxy = NULL;
1628 GDBusProxy *adapter_proxy = NULL;
1629 GDBusConnection *conn;
1630 GError *error = NULL;
1631 GVariant *tmp_value = NULL;
1632 GVariant *value = NULL;
1633 GVariant *result = NULL;
1636 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1637 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1639 _bt_convert_addr_type_to_string(address, device_address->addr);
1640 _bt_convert_addr_string_to_secure_string(secure_address, address);
1642 *is_connected = FALSE;
1643 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1645 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1646 return _bt_rfcomm_is_device_connected(device_address,
1648 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1649 return _bt_is_gatt_connected(device_address, is_connected);
1650 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1651 return _bt_pbap_is_connected(device_address, is_connected);
1653 adapter_proxy = _bt_get_adapter_proxy();
1654 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1656 conn = _bt_gdbus_get_system_gconn();
1657 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1659 _bt_convert_addr_type_to_string(address, device_address->addr);
1661 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1662 object_path = _bt_get_adapter_path();
1663 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1664 NULL, BT_BLUEZ_NAME,
1665 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1666 g_free(object_path);
1667 if (device_proxy == NULL) {
1668 BT_DBG("Device don't have this service");
1669 return BLUETOOTH_ERROR_INTERNAL;
1672 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1673 g_variant_new("(s)", address),
1674 G_DBUS_CALL_FLAGS_NONE,
1679 if (result == NULL) {
1680 BT_ERR("[GetProperties] Error occured in Proxy call");
1682 BT_ERR("%s", error->message);
1683 g_error_free(error);
1685 *is_connected = FALSE;
1686 g_object_unref(device_proxy);
1687 return BLUETOOTH_ERROR_NONE;
1689 g_variant_get(result , "(@a{sv})", &value);
1690 g_variant_unref(result);
1693 tmp_value = g_variant_lookup_value(value,
1695 G_VARIANT_TYPE_BOOLEAN);
1697 *is_connected = g_variant_get_boolean(tmp_value);
1698 g_variant_unref(tmp_value);
1700 g_variant_unref(value);
1702 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1703 return _bt_is_network_connected(_bt_get_net_conn(),
1704 device_address->addr, is_connected);
1706 uuid = _bt_get_profile_uuid128(connection_type);
1708 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1709 return BLUETOOTH_ERROR_INTERNAL;
1711 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1713 object_path = _bt_get_device_object_path(address);
1714 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1715 BT_DBG("object_path: %s", object_path);
1716 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1717 NULL, BT_BLUEZ_NAME,
1718 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1719 g_free(object_path);
1720 if (device_proxy == NULL) {
1721 BT_DBG("Device don't have this service");
1723 return BLUETOOTH_ERROR_INTERNAL;
1726 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1727 g_variant_new("(s)", uuid),
1728 G_DBUS_CALL_FLAGS_NONE,
1733 if (result == NULL) {
1734 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1736 BT_ERR("%s", error->message);
1737 if (g_strrstr(error->message, "Not Connected"))
1738 BT_DBG("Not connected");
1739 g_error_free(error);
1741 *is_connected = FALSE;
1742 g_object_unref(device_proxy);
1744 return BLUETOOTH_ERROR_NONE;
1746 g_variant_get(result, "(b)", is_connected);
1748 g_variant_unref(result);
1751 g_object_unref(device_proxy);
1752 return BLUETOOTH_ERROR_NONE;
1755 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1756 bluetooth_connected_link_t *connected)
1758 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1759 char *object_path = NULL;
1761 GDBusProxy *device_proxy;
1762 GError *error = NULL;
1763 GDBusConnection *conn;
1764 GVariant *value = NULL;
1765 GVariant *result = NULL;
1767 BT_CHECK_PARAMETER(device_address, return);
1769 conn = _bt_gdbus_get_system_gconn();
1770 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1772 _bt_convert_addr_type_to_string(address, device_address->addr);
1774 object_path = _bt_get_device_object_path(address);
1775 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1777 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1778 NULL, BT_BLUEZ_NAME,
1779 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1780 g_free(object_path);
1781 if (device_proxy == NULL) {
1782 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1783 return BLUETOOTH_ERROR_NONE;
1786 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1787 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1788 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1790 if (error != NULL) {
1791 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1792 g_error_free(error);
1793 g_object_unref(device_proxy);
1794 return BLUETOOTH_ERROR_INTERNAL;
1796 g_variant_get(result, "(v)", &value);
1797 *connected = g_variant_get_byte(value);
1798 g_variant_unref(value);
1799 g_variant_unref(result);
1802 g_object_unref(device_proxy);
1803 return BLUETOOTH_ERROR_NONE;
1806 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1810 GVariant *out_param1;
1812 int result = BLUETOOTH_ERROR_NONE;
1813 bt_function_data_t *func_data = user_data;
1814 request_info_t *req_info = NULL;
1815 bluetooth_device_address_t device_addr = { {0} };
1817 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1818 g_object_unref(proxy);
1820 if (reply == NULL) {
1821 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1823 BT_ERR("Error: %s", err->message);
1824 g_clear_error(&err);
1826 result = BLUETOOTH_ERROR_INTERNAL;
1828 g_variant_unref(reply);
1831 if (func_data == NULL) {
1832 BT_ERR("func_data is NULL");
1836 req_info = _bt_get_request_info(func_data->req_id);
1837 if (req_info == NULL) {
1838 BT_ERR("req_info is NULL");
1842 if (req_info->context == NULL) {
1843 BT_ERR("req_info->context is NULL");
1847 _bt_convert_addr_string_to_type(device_addr.addr,
1848 (const char *)func_data->address);
1850 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1851 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1854 g_dbus_method_invocation_return_value(req_info->context,
1855 g_variant_new("(iv)", result, out_param1));
1859 _bt_delete_request_list(req_info->req_id);
1862 g_free(func_data->address);
1867 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1868 gboolean auto_connect)
1870 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1871 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1872 gchar *device_path = NULL;
1873 GDBusProxy *device_proxy = NULL;
1874 GDBusConnection *conn;
1875 int ret = BLUETOOTH_ERROR_NONE;
1876 bt_function_data_t *func_data;
1878 BT_CHECK_PARAMETER(bd_addr, return);
1880 conn = _bt_gdbus_get_system_gconn();
1881 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1883 _bt_convert_addr_type_to_string(device_address,
1884 (unsigned char *)bd_addr->addr);
1885 device_path = _bt_get_device_object_path(device_address);
1886 if (device_path == NULL) {
1887 BT_ERR_C("device_path NULL : [%s]", device_address);
1888 ret = BLUETOOTH_ERROR_INTERNAL;
1892 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1893 NULL, BT_BLUEZ_NAME,
1894 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1895 g_free(device_path);
1896 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1898 func_data = g_malloc0(sizeof(bt_function_data_t));
1899 func_data->address = g_strdup(device_address);
1900 if (func_data->address == NULL) {
1901 BT_ERR("Unable to allocate memory for address");
1902 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1906 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1907 BT_INFO("Connect LE [%s]", secure_address);
1909 func_data->req_id = req_id;
1911 g_dbus_proxy_call(device_proxy, "ConnectLE",
1912 g_variant_new("(b)", auto_connect),
1913 G_DBUS_CALL_FLAGS_NONE,
1914 BT_MAX_DBUS_TIMEOUT,
1916 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1922 g_object_unref(device_proxy);
1924 g_free(func_data->address);
1930 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1932 BT_INFO("Try to initiate pending LE connection");
1934 pending_le_conn_timer_id = 0;
1936 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1937 &pending_le_conn_info->bd_addr,
1938 pending_le_conn_info->auto_connect);
1940 g_free(pending_le_conn_info);
1941 pending_le_conn_info = NULL;
1946 void _bt_pending_connect_le_device(void)
1948 if (pending_le_conn_timer_id > 0) {
1949 g_source_remove(pending_le_conn_timer_id);
1950 __bt_connect_le_timer_cb(NULL);
1954 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1955 gboolean auto_connect)
1957 int ret = BLUETOOTH_ERROR_NONE;
1959 BT_CHECK_PARAMETER(bd_addr, return);
1961 ret = _bt_hold_current_advertising();
1962 if (ret == BLUETOOTH_ERROR_NONE) {
1963 BT_INFO("Current advertising is held");
1964 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1965 pending_le_conn_info->req_id = req_id;
1966 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1967 BLUETOOTH_ADDRESS_LENGTH);
1968 pending_le_conn_info->auto_connect = auto_connect;
1970 pending_le_conn_timer_id =
1971 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1973 return BLUETOOTH_ERROR_NONE;
1976 BT_ERR("Unable to hold advertising");
1978 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1981 int _bt_disconnect_le_device(int req_id,
1982 const bluetooth_device_address_t *bd_addr)
1984 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1985 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1987 GDBusProxy *device_proxy;
1988 GDBusConnection *conn;
1989 int ret = BLUETOOTH_ERROR_NONE;
1990 bt_function_data_t *func_data;
1992 BT_CHECK_PARAMETER(bd_addr, return);
1994 conn = _bt_gdbus_get_system_gconn();
1995 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1997 _bt_convert_addr_type_to_string(device_address,
1998 (unsigned char *)bd_addr->addr);
1999 device_path = _bt_get_device_object_path(device_address);
2000 if (device_path == NULL) {
2001 BT_DBG("device_path NULL");
2002 ret = BLUETOOTH_ERROR_INTERNAL;
2006 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2008 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2009 NULL, BT_BLUEZ_NAME,
2010 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2011 g_free(device_path);
2012 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2014 func_data = g_malloc0(sizeof(bt_function_data_t));
2015 func_data->address = g_strdup(device_address);
2016 if (func_data->address == NULL) {
2017 BT_ERR("Unable to allocate memory for address");
2018 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2022 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2023 BT_INFO("Disconnect LE [%s]", secure_address);
2025 func_data->req_id = req_id;
2027 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2029 G_DBUS_CALL_FLAGS_NONE,
2030 BT_MAX_DBUS_TIMEOUT,
2032 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2037 g_object_unref(device_proxy);
2039 g_free(func_data->address);
2045 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2047 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2048 gchar *device_path = NULL;
2049 GError *error = NULL;
2050 GDBusProxy *device_proxy = NULL;
2051 GDBusProxy *adapter_proxy;
2052 GDBusConnection *conn;
2053 int ret = BLUETOOTH_ERROR_NONE;
2055 BT_CHECK_PARAMETER(bd_addr, return);
2057 _bt_convert_addr_type_to_string(device_address,
2058 (unsigned char *)bd_addr->addr);
2060 conn = _bt_gdbus_get_system_gconn();
2061 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2063 adapter_proxy = _bt_get_adapter_proxy();
2064 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2066 device_path = _bt_get_device_object_path(device_address);
2067 if (device_path == NULL) {
2068 BT_DBG("device_path NULL");
2069 ret = BLUETOOTH_ERROR_INTERNAL;
2073 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2075 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2076 NULL, BT_BLUEZ_NAME,
2077 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2078 g_free(device_path);
2079 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2081 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2083 G_DBUS_CALL_FLAGS_NONE,
2088 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2089 g_error_free(error);
2090 g_object_unref(device_proxy);
2091 return BLUETOOTH_ERROR_INTERNAL;
2094 g_object_unref(device_proxy);
2099 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2101 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2102 gchar *device_path = NULL;
2103 GError *error = NULL;
2104 GDBusProxy *device_proxy = NULL;
2105 GDBusProxy *adapter_proxy;
2106 GDBusConnection *conn;
2107 int ret = BLUETOOTH_ERROR_NONE;
2109 BT_CHECK_PARAMETER(bd_addr, return);
2111 _bt_convert_addr_type_to_string(device_address,
2112 (unsigned char *)bd_addr->addr);
2114 conn = _bt_gdbus_get_system_gconn();
2115 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2117 adapter_proxy = _bt_get_adapter_proxy();
2118 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2120 device_path = _bt_get_device_object_path(device_address);
2121 if (device_path == NULL) {
2122 BT_DBG("device_path NULL");
2123 ret = BLUETOOTH_ERROR_INTERNAL;
2127 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2129 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2130 NULL, BT_BLUEZ_NAME,
2131 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2132 g_free(device_path);
2133 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2135 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2137 G_DBUS_CALL_FLAGS_NONE,
2142 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2143 g_error_free(error);
2144 g_object_unref(device_proxy);
2145 return BLUETOOTH_ERROR_INTERNAL;
2148 g_object_unref(device_proxy);
2153 int _bt_connect_profile(char *address, char *uuid,
2154 void *cb, gpointer func_data)
2158 GDBusConnection *conn;
2159 GDBusProxy *adapter_proxy;
2160 GVariant *result = NULL;
2161 GError *error = NULL;
2163 conn = _bt_gdbus_get_system_gconn();
2164 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2166 object_path = _bt_get_device_object_path(address);
2167 if (object_path == NULL) {
2168 BT_ERR("No searched device");
2170 adapter_proxy = _bt_get_adapter_proxy();
2171 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2173 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2174 g_variant_new("(s)", address),
2175 G_DBUS_CALL_FLAGS_NONE,
2180 if (error != NULL) {
2181 BT_ERR("CreateDevice Fail: %s", error->message);
2182 g_error_free(error);
2185 g_variant_unref(result);
2187 object_path = _bt_get_device_object_path(address);
2189 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2191 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2192 NULL, BT_BLUEZ_NAME,
2193 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2194 g_free(object_path);
2195 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2197 g_dbus_proxy_call(proxy, "ConnectProfile",
2198 g_variant_new("(s)", uuid),
2199 G_DBUS_CALL_FLAGS_NONE,
2200 BT_MAX_DBUS_TIMEOUT,
2202 (GAsyncReadyCallback)cb,
2205 return BLUETOOTH_ERROR_NONE;
2208 int _bt_disconnect_profile(char *address, char *uuid,
2209 void *cb, gpointer func_data)
2213 GDBusConnection *conn;
2215 conn = _bt_gdbus_get_system_gconn();
2216 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2218 object_path = _bt_get_device_object_path(address);
2219 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2221 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2222 NULL, BT_BLUEZ_NAME,
2223 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2224 g_free(object_path);
2225 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2227 g_dbus_proxy_call(proxy, "DisconnectProfile",
2228 g_variant_new("(s)", uuid),
2229 G_DBUS_CALL_FLAGS_NONE,
2230 BT_MAX_DBUS_TIMEOUT,
2232 (GAsyncReadyCallback)cb,
2235 return BLUETOOTH_ERROR_NONE;
2238 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2239 int low_threshold, int in_range_threshold, int high_threshold)
2241 int ret = BLUETOOTH_ERROR_NONE;
2243 GVariant *result = NULL;
2244 GError *error = NULL;
2245 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2247 BT_CHECK_PARAMETER(bd_addr, return);
2248 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2249 bd_addr->addr[0], bd_addr->addr[1],
2250 bd_addr->addr[2], bd_addr->addr[3],
2251 bd_addr->addr[4], bd_addr->addr[5],
2253 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2254 in_range_threshold, high_threshold);
2256 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2258 proxy = _bt_get_adapter_proxy();
2259 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2261 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2262 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2263 G_DBUS_CALL_FLAGS_NONE,
2267 if (error != NULL) {
2268 BT_ERR("Dbus Call Error:[%s]", error->message);
2269 g_error_free(error);
2270 ret = BLUETOOTH_ERROR_INTERNAL;
2274 g_variant_unref(result);
2279 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2282 int ret = BLUETOOTH_ERROR_NONE;
2284 GVariant *result = NULL;
2285 GError *error = NULL;
2286 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2288 BT_CHECK_PARAMETER(bd_addr, return);
2289 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2290 bd_addr->addr[0], bd_addr->addr[1],
2291 bd_addr->addr[2], bd_addr->addr[3],
2292 bd_addr->addr[4], bd_addr->addr[5],
2295 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2297 proxy = _bt_get_adapter_proxy();
2298 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2300 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2301 g_variant_new("(si)", address, link_type),
2302 G_DBUS_CALL_FLAGS_NONE,
2307 if (error != NULL) {
2308 BT_ERR("Dbus Call Error:[%s]", error->message);
2309 g_error_free(error);
2310 ret = BLUETOOTH_ERROR_INTERNAL;
2314 g_variant_unref(result);
2319 int _bt_le_conn_update(unsigned char *device_address,
2320 float interval_min, float interval_max,
2321 guint16 latency, guint16 time_out)
2323 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2324 gchar *device_path = NULL;
2325 GError *error = NULL;
2326 GDBusProxy *device_proxy = NULL;
2327 GDBusConnection *conn;
2329 guint32 min, max, to;
2330 guint32 min_supervision_to;
2331 int ret = BLUETOOTH_ERROR_NONE;
2333 BT_CHECK_PARAMETER(device_address, return);
2335 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2336 interval_min, interval_max, latency, time_out);
2338 if (interval_min > interval_max ||
2339 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2340 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2341 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2345 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2346 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2347 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2351 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2352 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2357 * The Supervision_Timeout in milliseconds shall be larger than
2358 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2359 * where Conn_Interval_Max is given in milliseconds.
2361 min_supervision_to = (1 + latency) * interval_max * 2;
2362 if (time_out <= min_supervision_to) {
2363 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2367 _bt_convert_addr_type_to_string(address, device_address);
2369 BT_DBG("Remote device address: %s", address);
2371 device_path = _bt_get_device_object_path(address);
2373 if (device_path == NULL) {
2374 BT_ERR("device_path NULL");
2375 ret = BLUETOOTH_ERROR_INTERNAL;
2379 conn = _bt_gdbus_get_system_gconn();
2381 BT_ERR("conn NULL");
2382 ret = BLUETOOTH_ERROR_INTERNAL;
2386 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2387 NULL, BT_BLUEZ_NAME,
2388 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2390 g_free(device_path);
2391 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2393 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2394 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2395 to = time_out / BT_LE_CONN_TO_SPLIT;
2397 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2398 g_variant_new("(uuuu)", min, max, latency, to),
2399 G_DBUS_CALL_FLAGS_NONE,
2404 g_object_unref(device_proxy);
2405 if (reply == NULL) {
2407 BT_ERR("Error %s[%s]", error->message, address);
2408 if (g_strrstr(error->message, "In Progress"))
2409 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2411 ret = BLUETOOTH_ERROR_INTERNAL;
2412 g_error_free(error);
2416 g_variant_unref(reply);
2422 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2423 bluetooth_device_pin_code_t *pin_code)
2425 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2427 bt_pin_code_info_t *pin_info = NULL;
2429 BT_CHECK_PARAMETER(device_address, return);
2430 BT_CHECK_PARAMETER(pin_code, return);
2431 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2432 BLUETOOTH_ERROR_NO_RESOURCES);
2434 _bt_convert_addr_type_to_string(address, device_address->addr);
2436 for (l = pin_info_list; l != NULL; l = l->next) {
2439 if (g_strcmp0(pin_info->address, address) == 0) {
2440 g_free(pin_info->pin_code);
2441 pin_info->pin_code = g_strdup(pin_code->pin_code);
2442 return BLUETOOTH_ERROR_NONE;
2446 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2447 pin_info->address = g_strdup(address);
2448 pin_info->pin_code = g_strdup(pin_code->pin_code);
2449 pin_info_list = g_slist_append(pin_info_list, pin_info);
2451 return BLUETOOTH_ERROR_NONE;
2454 gint __bt_compare_address(gpointer *a, gpointer *b)
2456 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2457 char *address = (char *)b;
2458 return g_strcmp0(pin_info->address, address);
2461 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2463 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2465 bt_pin_code_info_t *pin_info = NULL;
2467 BT_CHECK_PARAMETER(device_address, return);
2469 _bt_convert_addr_type_to_string(address, device_address->addr);
2471 l = g_slist_find_custom(pin_info_list, address,
2472 (GCompareFunc)__bt_compare_address);
2476 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2477 g_free(pin_info->address);
2478 g_free(pin_info->pin_code);
2482 return BLUETOOTH_ERROR_NONE;
2485 int _bt_get_device_pin_code(const char *address, char *pin_code)
2489 BT_CHECK_PARAMETER(address, return);
2490 BT_CHECK_PARAMETER(pin_code, return);
2492 for (l = pin_info_list; l != NULL; l = l->next) {
2493 bt_pin_code_info_t *pin_info = l->data;
2495 if (g_strcmp0(pin_info->address, address) == 0) {
2496 g_strlcpy(pin_code, pin_info->pin_code,
2497 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2499 return BLUETOOTH_ERROR_NONE;
2503 return BLUETOOTH_ERROR_NOT_FOUND;
2506 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2507 bluetooth_le_connection_param_t *param)
2510 return BLUETOOTH_ERROR_INVALID_PARAM;
2512 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2513 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2514 return BLUETOOTH_ERROR_INVALID_PARAM;
2516 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2519 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2520 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2521 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2522 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2523 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2526 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2527 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2528 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2529 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2530 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2533 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2534 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2535 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2536 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2537 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2541 BT_ERR("Unhandled mode : %d", mode);
2545 return BLUETOOTH_ERROR_NONE;
2548 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2549 guint trusted_profile_flag, guint *trusted)
2555 case TRUSTED_PROFILE_PBAP:
2556 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2557 trust_profile = trusted_profile_flag & (1 << 1);
2559 return BLUETOOTH_ERROR_NOT_SUPPORT;
2561 case TRUSTED_PROFILE_MAP:
2562 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2563 trust_profile = trusted_profile_flag & (1 << 3);
2565 return BLUETOOTH_ERROR_NOT_SUPPORT;
2567 case TRUSTED_PROFILE_SAP:
2568 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2569 trust_profile = trusted_profile_flag & (1 << 5);
2571 return BLUETOOTH_ERROR_NOT_SUPPORT;
2573 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2574 *trusted = trusted_profile_flag;
2575 return BLUETOOTH_ERROR_NONE;
2577 return BLUETOOTH_ERROR_NOT_SUPPORT;
2583 return BLUETOOTH_ERROR_NONE;
2586 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2587 guint restricted_profile_flag, guint *restricted)
2589 int restrict_profile;
2590 *restricted = FALSE;
2593 case RESTRICTED_PROFILE_HFP_HS:
2594 restrict_profile = restricted_profile_flag & (1 << 0);
2596 case RESTRICTED_PROFILE_A2DP:
2597 restrict_profile = restricted_profile_flag & (1 << 2);
2600 return BLUETOOTH_ERROR_NOT_SUPPORT;
2603 if (restrict_profile)
2606 return BLUETOOTH_ERROR_NONE;
2609 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2612 case TRUSTED_PROFILE_PBAP:
2613 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2614 case TRUSTED_PROFILE_MAP:
2615 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2616 case TRUSTED_PROFILE_SAP:
2617 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2618 case TRUSTED_PROFILE_ALL:
2625 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2628 case RESTRICTED_PROFILE_HFP_HS:
2629 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2630 case RESTRICTED_PROFILE_A2DP:
2631 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2637 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2639 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2640 return TRUSTED_PROFILE_PBAP;
2641 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2642 return TRUSTED_PROFILE_MAP;
2643 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2644 return TRUSTED_PROFILE_SAP;
2646 return 0; /* 0 - Unknown Profile */
2649 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2650 bluetooth_trusted_profile_t profile, gboolean trust)
2652 int ret = BLUETOOTH_ERROR_NONE;
2653 GDBusConnection *conn;
2655 GError *error = NULL;
2656 char *device_path = NULL;
2658 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2661 BT_CHECK_PARAMETER(bd_addr, return);
2662 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2663 bd_addr->addr[0], bd_addr->addr[1],
2664 bd_addr->addr[2], bd_addr->addr[3],
2665 bd_addr->addr[4], bd_addr->addr[5],
2668 conn = _bt_gdbus_get_system_gconn();
2669 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2671 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2673 device_path = _bt_get_device_object_path(address);
2674 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2676 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2677 NULL, BT_BLUEZ_NAME, device_path,
2678 BT_DEVICE_INTERFACE, NULL, NULL);
2680 g_free(device_path);
2681 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2683 uuid = _bt_get_trusted_profile_uuid(profile);
2685 g_object_unref(proxy);
2686 return BLUETOOTH_ERROR_NOT_SUPPORT;
2689 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2690 g_variant_new("(sb)", uuid, trust),
2691 G_DBUS_CALL_FLAGS_NONE, -1,
2693 g_object_unref(proxy);
2695 if (reply == NULL) {
2696 BT_ERR("Failed to Set Profile Trusted");
2697 ret = BLUETOOTH_ERROR_INTERNAL;
2699 BT_ERR("Error %s[%s]", error->message, address);
2700 g_error_free(error);
2704 g_variant_unref(reply);
2711 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2712 bluetooth_trusted_profile_t profile, guint *trust)
2714 int ret = BLUETOOTH_ERROR_NONE;
2715 GDBusConnection *conn;
2717 GError *error = NULL;
2718 char *device_path = NULL;
2719 guint trusted_profile_flag;
2720 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2723 BT_CHECK_PARAMETER(bd_addr, return);
2724 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2725 bd_addr->addr[0], bd_addr->addr[1],
2726 bd_addr->addr[2], bd_addr->addr[3],
2727 bd_addr->addr[4], bd_addr->addr[5],
2730 conn = _bt_gdbus_get_system_gconn();
2731 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2733 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2735 device_path = _bt_get_device_object_path(address);
2736 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2738 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2739 NULL, BT_BLUEZ_NAME, device_path,
2740 BT_PROPERTIES_INTERFACE, NULL, NULL);
2742 g_free(device_path);
2743 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2745 reply = g_dbus_proxy_call_sync(proxy, "Get",
2746 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2747 G_DBUS_CALL_FLAGS_NONE, -1,
2749 g_object_unref(proxy);
2751 if (reply == NULL) {
2752 BT_ERR("Failed to Get Profile Trusted");
2753 ret = BLUETOOTH_ERROR_INTERNAL;
2755 BT_ERR("Error %s[%s]", error->message, address);
2756 g_error_free(error);
2761 g_variant_get(reply, "(v)", &temp);
2762 trusted_profile_flag = g_variant_get_uint32(temp);
2763 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2765 ret = _bt_get_trusted_profile_from_flag(profile,
2766 trusted_profile_flag, trust);
2767 g_variant_unref(temp);
2768 g_variant_unref(reply);
2771 BT_DBG("TRUST %d", *trust);
2775 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2776 bluetooth_restricted_profile_t profile, gboolean restricted)
2778 int ret = BLUETOOTH_ERROR_NONE;
2779 GDBusConnection *conn;
2781 GError *error = NULL;
2782 char *device_path = NULL;
2784 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2787 BT_CHECK_PARAMETER(bd_addr, return);
2788 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2789 bd_addr->addr[0], bd_addr->addr[1],
2790 bd_addr->addr[2], bd_addr->addr[3],
2791 bd_addr->addr[4], bd_addr->addr[5],
2792 profile, restricted);
2794 conn = _bt_gdbus_get_system_gconn();
2795 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2797 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2799 device_path = _bt_get_device_object_path(address);
2800 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2802 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2803 NULL, BT_BLUEZ_NAME, device_path,
2804 BT_DEVICE_INTERFACE, NULL, NULL);
2806 g_free(device_path);
2807 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2809 uuid = _bt_get_restricted_profile_uuid(profile);
2811 g_object_unref(proxy);
2812 return BLUETOOTH_ERROR_NOT_SUPPORT;
2815 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2816 g_variant_new("(sb)", uuid, restricted),
2817 G_DBUS_CALL_FLAGS_NONE, -1,
2819 g_object_unref(proxy);
2821 if (reply == NULL) {
2822 BT_ERR("Failed to Set Profile Restricted");
2823 ret = BLUETOOTH_ERROR_INTERNAL;
2825 BT_ERR("Error %s[%s]", error->message, address);
2826 g_error_free(error);
2830 g_variant_unref(reply);
2837 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2838 bluetooth_restricted_profile_t profile, guint *restricted)
2840 int ret = BLUETOOTH_ERROR_NONE;
2841 GDBusConnection *conn;
2843 GError *error = NULL;
2844 char *device_path = NULL;
2845 guint restricted_profile_flag;
2846 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2849 BT_CHECK_PARAMETER(bd_addr, return);
2850 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2851 bd_addr->addr[0], bd_addr->addr[1],
2852 bd_addr->addr[2], bd_addr->addr[3],
2853 bd_addr->addr[4], bd_addr->addr[5],
2854 profile, *restricted);
2856 conn = _bt_gdbus_get_system_gconn();
2857 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2859 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2861 device_path = _bt_get_device_object_path(address);
2862 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2864 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2865 NULL, BT_BLUEZ_NAME, device_path,
2866 BT_PROPERTIES_INTERFACE, NULL, NULL);
2868 g_free(device_path);
2869 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2871 reply = g_dbus_proxy_call_sync(proxy, "Get",
2872 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2873 G_DBUS_CALL_FLAGS_NONE, -1,
2875 g_object_unref(proxy);
2877 if (reply == NULL) {
2878 BT_ERR("Failed to Get Profile Restricted");
2879 ret = BLUETOOTH_ERROR_INTERNAL;
2881 BT_ERR("Error %s[%s]", error->message, address);
2882 g_error_free(error);
2887 g_variant_get(reply, "(v)", &temp);
2888 restricted_profile_flag = g_variant_get_uint32(temp);
2889 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2891 ret = _bt_get_restricted_profile_from_flag(profile,
2892 restricted_profile_flag, restricted);
2893 g_variant_unref(temp);
2894 g_variant_unref(reply);
2897 BT_DBG("TRUST %d", *restricted);
2901 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2905 GVariant *out_param1;
2906 request_info_t *req_info;
2907 GVariant *val = NULL;
2908 GVariant *param = NULL;
2911 bluetooth_device_address_t device_addr = { {0} };
2912 int result = BLUETOOTH_ERROR_NONE;
2915 val = g_dbus_proxy_call_finish(proxy, res, &err);
2917 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2918 if (req_info == NULL) {
2919 BT_ERR("req_info == NULL");
2920 g_object_unref(proxy);
2921 att_mtu_req_info->device_proxy = NULL;
2926 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2928 if (g_strrstr(err->message, "NotSupported")) {
2929 BT_INFO("Connection Not Supported");
2930 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2931 } else if (g_strrstr(err->message, "NotConnected")) {
2932 BT_INFO("Not connected");
2933 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2934 } else if (g_strrstr(err->message, "InvalidArguments")) {
2935 BT_INFO("Not connected");
2936 result = BLUETOOTH_ERROR_INVALID_PARAM;
2938 BT_DBG("Default case");
2939 result = BLUETOOTH_ERROR_INTERNAL;
2943 g_object_unref(proxy);
2944 att_mtu_req_info->device_proxy = NULL;
2946 if (result != BLUETOOTH_ERROR_NONE)
2950 g_variant_get(val, "(qy)", &mtu, &status);
2951 g_variant_unref(val);
2954 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
2956 param = g_variant_new("(isqy)",
2958 att_mtu_req_info->addr,
2962 /* Send the event to application */
2963 _bt_send_event(BT_DEVICE_EVENT,
2964 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2968 if (req_info->context == NULL)
2971 _bt_convert_addr_string_to_type(device_addr.addr,
2972 (const char *)att_mtu_req_info->addr);
2974 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
2975 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
2976 g_dbus_method_invocation_return_value(req_info->context,
2977 g_variant_new("(iv)", result, out_param1));
2979 _bt_delete_request_list(req_info->req_id);
2984 g_free(att_mtu_req_info->addr);
2985 g_free(att_mtu_req_info);
2986 att_mtu_req_info = NULL;
2991 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
2994 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2995 gchar *device_path = NULL;
2996 GDBusProxy *adapter_proxy;
2997 GDBusProxy *device_proxy;
2998 GDBusConnection *conn;
2999 int ret = BLUETOOTH_ERROR_NONE;
3001 BT_CHECK_PARAMETER(device_address, return);
3003 if (att_mtu_req_info) {
3004 BT_ERR("ATT MTU request in progress");
3005 return BLUETOOTH_ERROR_DEVICE_BUSY;
3008 conn = _bt_gdbus_get_system_gconn();
3009 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3011 adapter_proxy = _bt_get_adapter_proxy();
3012 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3014 _bt_convert_addr_type_to_string(address, device_address->addr);
3016 BT_DBG("Remote device address: %s", address);
3018 device_path = _bt_get_device_object_path(address);
3020 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3022 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3023 NULL, BT_BLUEZ_NAME,
3024 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3025 g_free(device_path);
3026 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3028 if (device_path == NULL) {
3029 BT_DBG("device_path NULL");
3030 ret = BLUETOOTH_ERROR_INTERNAL;
3034 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3035 att_mtu_req_info->addr = (char *)g_strdup(address);
3036 att_mtu_req_info->req_id = request_id;
3037 att_mtu_req_info->device_proxy = device_proxy;
3039 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3040 g_variant_new("(q)", mtu),
3041 G_DBUS_CALL_FLAGS_NONE,
3042 BT_MAX_DBUS_TIMEOUT,
3044 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3050 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3053 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3054 char *object_path = NULL;
3056 GDBusProxy *device_proxy;
3057 GError *error = NULL;
3059 GDBusConnection *conn;
3060 GVariant *result = NULL;
3062 BT_CHECK_PARAMETER(device_address, return);
3064 conn = _bt_gdbus_get_system_gconn();
3065 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3067 _bt_convert_addr_type_to_string(address, device_address->addr);
3069 object_path = _bt_get_device_object_path(address);
3070 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3072 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3073 NULL, BT_BLUEZ_NAME, object_path,
3074 BT_PROPERTIES_INTERFACE, NULL, NULL);
3075 g_free(object_path);
3076 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3078 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3079 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3080 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3082 ERR("DBus Error : %s", error->message);
3083 g_clear_error(&error);
3084 g_object_unref(device_proxy);
3085 return BLUETOOTH_ERROR_INTERNAL;
3087 g_variant_get(result, "(v)", &value);
3088 *mtu = g_variant_get_uint16(value);
3089 BT_DBG("ATT MTU : %d", *mtu);
3090 g_variant_unref(value);
3091 g_variant_unref(result);
3094 g_object_unref(device_proxy);
3095 return BLUETOOTH_ERROR_NONE;
3098 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3099 bluetooth_device_address_t *id_address)
3101 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3102 gchar *device_path = NULL;
3103 const gchar *idaddress = NULL;
3104 GDBusProxy *device_proxy;
3105 GError *error = NULL;
3106 GVariant *result = NULL;
3107 GDBusConnection *conn;
3108 int ret = BLUETOOTH_ERROR_NONE;
3110 BT_CHECK_PARAMETER(device_address, return);
3112 conn = _bt_gdbus_get_system_gconn();
3113 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3115 _bt_convert_addr_type_to_string(address, device_address->addr);
3117 device_path = _bt_get_device_object_path(address);
3118 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3120 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3121 NULL, BT_BLUEZ_NAME,
3122 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3123 g_free(device_path);
3124 if (!device_proxy) {
3125 BT_ERR("Unable to get proxy");
3126 return BLUETOOTH_ERROR_INTERNAL;
3129 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3131 G_DBUS_CALL_FLAGS_NONE,
3136 if (result == NULL) {
3137 BT_ERR("Failed to get device ID address");
3138 if (error != NULL) {
3139 BT_ERR("Error occured in Proxy call [%s]", error->message);
3140 g_error_free(error);
3142 g_object_unref(device_proxy);
3143 return BLUETOOTH_ERROR_INTERNAL;
3146 g_variant_get(result , "(s)", &idaddress);
3147 g_variant_unref(result);
3149 if (idaddress == NULL) {
3150 BT_ERR("No paired device");
3151 g_object_unref(device_proxy);
3152 return BLUETOOTH_ERROR_NOT_PAIRED;
3155 BT_DBG("ID Address:%s", idaddress);
3158 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3160 ret = BLUETOOTH_ERROR_INTERNAL;
3162 g_object_unref(device_proxy);
3167 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3169 GapAgentPrivate *agent = _bt_get_adapter_agent();
3170 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3172 if (authentication_reply)
3173 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3175 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3177 BT_DBG("BT_PASSKEY_REPLY");
3178 return BLUETOOTH_ERROR_NONE;
3181 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3183 GapAgentPrivate *agent = _bt_get_adapter_agent();
3184 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3186 if (confirmation_reply)
3187 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3189 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3191 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3192 return BLUETOOTH_ERROR_NONE;