2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-event.h"
33 #include "bt-service-device.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
40 #include "bt-service-pbap.h"
42 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
43 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
44 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
46 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
47 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
49 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
50 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
51 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
52 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
53 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
55 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
59 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
63 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
67 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
69 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
76 GDBusProxy *device_proxy;
77 GDBusProxy *adapter_proxy;
79 unsigned short conn_type;
80 gboolean bonding_wo_discovery;
90 bluetooth_device_address_t bd_addr;
91 gboolean auto_connect;
92 } bt_pending_le_conn_info_s;
94 gboolean is_device_creating;
95 bt_funcion_data_t *bonding_info;
96 bt_funcion_data_t *searching_info;
97 bt_funcion_data_t *att_mtu_req_info;
99 static GSList *pin_info_list = NULL;
100 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
101 static guint pending_le_conn_timer_id = 0;
103 /* This HID Mouse does not support pairing precedure. need to skip it. */
104 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
106 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
109 static int __bt_retry_bond(void);
112 /*static void __bt_decline_pair_request()
114 GVariant *out_param1;
115 GVariant *out_param2;
116 request_info_t *req_info;
117 bluetooth_device_info_t dev_info;
118 bt_remote_dev_info_t *remote_dev_info;
119 GVariant *uuid_list, *manufacture_data;
124 req_info = _bt_get_request_info(bonding_info->req_id);
125 if (req_info == NULL) {
126 BT_ERR("req_info == NULL");
129 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
131 BT_DBG("bonding_info is NULL");
136 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
137 remote_dev_info->uuids, remote_dev_info->uuid_count,
140 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
141 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
144 param = g_variant_new("isunsbuba{s}na{y})",
145 bonding_info->result,
147 remote_dev_info->class,
148 remote_dev_info->rssi,
149 remote_dev_info->name,
150 remote_dev_info->paired,
151 remote_dev_info->connected,
152 remote_dev_info->trust,
154 remote_dev_info->manufacturer_data_len,
158 //Send the event to application
159 if (remote_dev_info != NULL) {
160 _bt_send_event(BT_ADAPTER_EVENT,
161 BLUETOOTH_EVENT_BONDING_FINISHED,
164 _bt_free_device_info(remote_dev_info);
167 if (req_info->context == NULL)
170 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
171 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
174 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
175 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
177 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
178 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
181 g_dbus_method_invocation_return_value(req_info->context,
182 g_variant_new("@ayi", out_param1, out_param2));
185 _bt_delete_request_list(req_info->req_id);
188 g_free(bonding_info->addr);
189 g_free(bonding_info);
195 #ifdef TIZEN_PROFILE_WEARABLE
196 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
200 retv_if(user_data == NULL, FALSE);
202 b = (bundle *)user_data;
204 ret = syspopup_launch("bt-syspopup", b);
206 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
208 BT_DBG("Hurray!!! Finally Popup launched");
211 return (ret < 0) ? TRUE : FALSE;
214 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
219 GDBusConnection *conn;
221 conn = _bt_gdbus_get_system_gconn();
229 bundle_add(b, "event-type", "unable-to-pairing");
231 if (result == BLUETOOTH_ERROR_TIMEOUT)
232 bundle_add(b, "error", "timeout");
233 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
234 bundle_add(b, "error", "authfailed");
236 bundle_add(b, "error", "error");
238 ret = syspopup_launch("bt-syspopup", b);
240 BT_ERR("Popup launch failed...retry %d \n", ret);
241 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
252 gboolean _bt_is_device_creating(void)
254 return is_device_creating;
257 gboolean _bt_is_bonding_device_address(const char *address)
259 if (bonding_info == NULL || bonding_info->addr == NULL)
262 if (g_strcmp0(bonding_info->addr, address) == 0) {
263 BT_DBG("[%s] is bonding device", address);
267 BT_DBG("[%s] is NOT bonding device", address);
271 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
273 ret_if(bonding_info == NULL);
274 bonding_info->is_autopair = is_autopair;
277 void __bt_cancel_search_service_done(void)
279 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
280 request_info_t *req_info;
281 bluetooth_device_info_t dev_info;
282 GVariant *out_param1;
284 ret_if(searching_info == NULL);
286 req_info = _bt_get_request_info(searching_info->req_id);
287 if (req_info == NULL) {
288 BT_ERR("req_info == NULL");
292 if (req_info->context == NULL)
295 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
296 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
297 searching_info->addr);
299 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
300 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
303 g_dbus_method_invocation_return_value(req_info->context,
304 g_variant_new("(iv)", result, out_param1));
306 _bt_delete_request_list(req_info->req_id);
310 g_free(searching_info->addr);
311 g_free(searching_info);
312 searching_info = NULL;
315 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
317 ret_if(value == NULL);
318 ret_if(info == NULL);
320 gsize uuid_count = 0;
322 info->uuids = g_variant_dup_strv(value, &uuid_count);
323 info->uuid_count = (unsigned int)uuid_count;
325 BT_DBG("uuid count : %d", uuid_count);
328 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
330 char *object_path = NULL;
331 bt_remote_dev_info_t *dev_info;
333 retv_if(address == NULL, NULL);
335 object_path = _bt_get_device_object_path(address);
336 retv_if(object_path == NULL, NULL);
338 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
344 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
345 const char *object_path)
347 bt_remote_dev_info_t *dev_info;
348 GDBusProxy *adapter_proxy;
349 GDBusProxy *device_proxy;
354 GDBusConnection *conn;
355 GError *error = NULL;
356 GVariant *result = NULL;
357 GVariantIter *value_iter;
361 adapter_proxy = _bt_get_adapter_proxy();
362 retv_if(adapter_proxy == NULL, NULL);
364 retv_if(object_path == NULL, NULL);
366 conn = _bt_gdbus_get_system_gconn();
368 BT_ERR("conn == NULL");
372 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
373 NULL, BT_BLUEZ_NAME, object_path,
374 BT_PROPERTIES_INTERFACE, NULL, NULL);
376 retv_if(device_proxy == NULL, NULL);
378 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
379 g_variant_new("(s)", BT_DEVICE_INTERFACE),
380 G_DBUS_CALL_FLAGS_NONE,
385 g_object_unref(device_proxy);
387 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
389 if (result != NULL) {
390 g_variant_get(result , "(@a{sv})", &value);
391 g_variant_unref(result);
393 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
395 g_variant_get(tmp_value, "s", &name);
396 g_variant_unref(tmp_value);
398 DBG_SECURE("Alias Name [%s]", name);
400 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
401 g_variant_get(tmp_value, "s", &name);
402 g_variant_unref(tmp_value);
405 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
407 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
408 g_variant_unref(tmp_value);
410 dev_info->is_alias_set = FALSE;
412 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
414 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
416 dev_info->class = g_variant_get_uint32(tmp_value);
417 g_variant_unref(tmp_value);
421 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
423 dev_info->connected = g_variant_get_byte(tmp_value);
424 g_variant_unref(tmp_value);
426 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
427 BT_DBG("connected link : %d", dev_info->connected);
429 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
431 dev_info->trust = g_variant_get_boolean(tmp_value);
432 g_variant_unref(tmp_value);
434 dev_info->trust = FALSE;
436 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
438 dev_info->paired = g_variant_get_boolean(tmp_value);
439 g_variant_unref(tmp_value);
441 dev_info->paired = FALSE;
443 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
445 dev_info->rssi = g_variant_get_int16(tmp_value);
446 g_variant_unref(tmp_value);
450 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
452 dev_info->addr_type = g_variant_get_byte(tmp_value);
453 g_variant_unref(tmp_value);
455 dev_info->addr_type = 0;
457 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
459 __bt_get_uuids(tmp_value, dev_info);
460 g_variant_unref(tmp_value);
463 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
465 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
466 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
467 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
468 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
470 g_variant_unref(tmp_value);
472 dev_info->manufacturer_data_len = 0;
474 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
476 if ((dev_info->manufacturer_data_len == 0) ||
477 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
478 BT_ERR("manufacturer data length doesn't match");
479 dev_info->manufacturer_data_len = 0;
480 dev_info->manufacturer_data = NULL;
482 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
483 g_variant_get(tmp_value, "ay", &value_iter);
484 while (g_variant_iter_loop(value_iter, "y", &m_value))
485 dev_info->manufacturer_data[i++] = m_value;
487 g_variant_unref(tmp_value);
489 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
490 dev_info->manufacturer_data_len = 0;
491 dev_info->manufacturer_data = NULL;
494 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
495 g_variant_get(tmp_value, "s", &address);
496 g_variant_unref(tmp_value);
498 dev_info->address = g_strdup(address);
499 dev_info->name = g_strdup(name);
501 g_variant_unref(value);
503 BT_ERR("result is NULL\n");
511 char *_bt_get_bonded_device_name(char *address)
513 bluetooth_device_address_t device_address = { {0} };
514 bluetooth_device_info_t dev_info;
516 retv_if(address == NULL, strdup(""));
518 _bt_convert_addr_string_to_type(device_address.addr, address);
520 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
522 _bt_get_bonded_device_info(&device_address, &dev_info);
524 return g_strdup(dev_info.device_name.name);
527 static gboolean __ignore_auto_pairing_request(const char *address)
532 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
543 /* Get the LAP(Lower Address part) */
544 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
546 snprintf(lap_address, sizeof(lap_address), ",%s", address);
548 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
551 BT_ERR("fopen failed \n");
555 fseek(fp, 0, SEEK_END);
560 BT_ERR("Get file size failed \n");
565 buffer = g_malloc0(sizeof(char) * size);
566 result = fread((char *)buffer, 1, size, fp);
568 if (result != size) {
569 BT_ERR("Read Error\n");
574 BT_DBG("Buffer = %s\n", buffer);
576 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
582 /* Write the data and insert new device data */
583 for (i = 0; lines[i] != NULL; i++) {
584 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
585 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
587 lines[i] = temp_buffer;
590 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
592 /* Fix : NULL_RETURNS */
593 retv_if(buffer == NULL, FALSE);
595 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
598 BT_ERR("fopen failed \n");
603 BT_DBG("Buffer = %s\n", buffer);
604 fwrite(buffer, 1, strlen(buffer), fp);
614 static int __bt_cancel_bonding(void)
616 BT_CHECK_PARAMETER(bonding_info, return);
617 BT_CHECK_PARAMETER(bonding_info->addr, return);
618 /* First Cancel the ongoing pairing in bluez otherwise if we send
619 * pair request bluez will send inprogress and we again retry bond so
620 * this cycle continues */
622 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
623 NULL, G_DBUS_CALL_FLAGS_NONE,
626 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
628 return BLUETOOTH_ERROR_PARING_FAILED;
630 return BLUETOOTH_ERROR_NONE;
633 static int __bt_retry_bond(void)
635 BT_CHECK_PARAMETER(bonding_info, return);
636 BT_CHECK_PARAMETER(bonding_info->addr, return);
638 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
639 g_variant_new("(y)", bonding_info->conn_type),
640 G_DBUS_CALL_FLAGS_NONE,
643 (GAsyncReadyCallback)__bt_bond_device_cb,
646 return BLUETOOTH_ERROR_NONE;
650 static int __bt_remove_and_bond(void)
652 GDBusProxy *adapter_proxy;
653 GVariant *result = NULL;
655 char *device_path = NULL;
657 BT_CHECK_PARAMETER(bonding_info, return);
658 BT_CHECK_PARAMETER(bonding_info->addr, return);
660 adapter_proxy = _bt_get_adapter_proxy();
661 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
663 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
664 g_variant_new("(s)", bonding_info->addr),
665 G_DBUS_CALL_FLAGS_NONE,
670 return BLUETOOTH_ERROR_INTERNAL;
672 g_variant_get(result , "(o)", &device_path);
673 g_variant_unref(result);
675 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
677 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
678 g_variant_new("(o)", device_path),
679 G_DBUS_CALL_FLAGS_NONE,
685 BT_ERR("UnpairDevice Fail: %s", err->message);
687 return BLUETOOTH_ERROR_INTERNAL;
690 return __bt_retry_bond();
693 static int __bt_cancel_and_bond(void)
695 int ret = BLUETOOTH_ERROR_NONE;
697 ret = _bt_agent_reply_cancellation();
698 if (ret != BLUETOOTH_ERROR_NONE) {
699 BT_ERR("Fail to call reply cancellation");
702 return __bt_retry_bond();
706 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
709 int result = BLUETOOTH_ERROR_NONE;
712 GVariant *out_param1;
713 request_info_t *req_info;
714 bluetooth_device_info_t dev_info;
715 bt_remote_dev_info_t *remote_dev_info;
716 GVariant *manufacture_data;
719 /* Terminate ALL system popup */
720 syspopup_destroy_all();
722 reply = g_dbus_proxy_call_finish(proxy, res, &err);
724 g_variant_unref(reply);
726 is_device_creating = FALSE;
728 if (bonding_info == NULL) {
730 BT_ERR("bonding_info == NULL");
736 req_info = _bt_get_request_info(bonding_info->req_id);
737 if (req_info == NULL) {
738 BT_ERR("req_info == NULL");
743 g_dbus_error_strip_remote_error(err);
744 BT_ERR("Error occured in CreateBonding [%s]", err->message);
746 if (g_strrstr(err->message, "Already Exists")) {
747 BT_INFO("Existing Bond, remove and retry");
748 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
750 result = BLUETOOTH_ERROR_PARING_FAILED;
751 } else if (_bt_agent_is_canceled() ||
752 g_strrstr(err->message, "Authentication Canceled")) {
753 BT_INFO("Cancelled by USER");
754 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
755 } else if (g_strrstr(err->message, "Authentication Rejected")) {
757 result = BLUETOOTH_ERROR_ACCESS_DENIED;
758 } else if (g_strrstr(err->message, "In Progress")) {
759 BT_INFO("Bond in progress, cancel and retry");
760 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
762 result = BLUETOOTH_ERROR_PARING_FAILED;
763 } else if (g_strrstr(err->message, "Authentication Failed")) {
764 BT_INFO("Authentication Failed");
765 if (bonding_info->is_autopair == TRUE) {
766 _bt_set_autopair_status_in_bonding_info(FALSE);
767 __ignore_auto_pairing_request(bonding_info->addr);
769 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
770 } else if (g_strrstr(err->message, "Page Timeout")) {
771 BT_INFO("Page Timeout");
772 /* This is the special case
773 As soon as call bluetooth_bond_device, try to cancel bonding.
774 In this case, before completing to call 'CreatePairedDevice' method
775 the procedure is stopped. So 'Cancle' error is not return.
777 result = BLUETOOTH_ERROR_HOST_DOWN;
778 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
779 BT_INFO("Cancel already running bonding");
780 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
781 BT_INFO("Error while Cancelling bonding");
782 /* we need to unref proxy so continue */
784 result = BLUETOOTH_ERROR_INTERNAL;
785 } else if (g_strrstr(err->message, "Connection Timeout")) {
786 /* Pairing request timeout */
787 result = BLUETOOTH_ERROR_TIMEOUT;
788 } else if (g_strrstr(err->message, "Authentication Timeout")) {
789 /* Pairing request timeout */
790 result = BLUETOOTH_ERROR_TIMEOUT;
792 BT_DBG("Default case");
793 result = BLUETOOTH_ERROR_PARING_FAILED;
797 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
798 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
799 result == BLUETOOTH_ERROR_TIMEOUT ||
800 result == BLUETOOTH_ERROR_HOST_DOWN) {
801 #ifdef TIZEN_PROFILE_WEARABLE
802 int is_sw_running = 0;
804 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
805 BT_ERR("vconf_get_int for setup wizard state failed");
808 __bt_launch_unable_to_pairing_syspopup(result);
810 BT_ERR("Unable to pair");
812 bonding_info->result = result;
815 g_object_unref(proxy);
816 bonding_info->device_proxy = NULL;
818 if (result != BLUETOOTH_ERROR_NONE) {
819 if (bonding_info->bonding_wo_discovery) {
820 GDBusProxy *adapter_proxy;
821 GVariant *ret = NULL;
822 GError *error = NULL;
825 BT_ERR("Bond was tried without discovery. Remove it");
827 adapter_proxy = _bt_get_adapter_proxy();
828 if (adapter_proxy == NULL) {
829 BT_ERR("Cannot get adapter_proxy");
833 device_path = _bt_get_device_object_path(bonding_info->addr);
834 if (device_path == NULL) {
835 BT_ERR("Cannot get device path");
839 ret = g_dbus_proxy_call_sync(adapter_proxy,
841 g_variant_new("(o)", device_path),
842 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
845 BT_ERR("RemoveDevice Fail: %s", error->message);
846 g_clear_error(&error);
850 g_variant_unref(ret);
858 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
859 if (!remote_dev_info)
862 GVariant *uuids = NULL;
863 GVariantBuilder *builder = NULL;
865 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
866 for (i = 0; i < remote_dev_info->uuid_count; i++) {
867 g_variant_builder_add(builder, "s",
868 remote_dev_info->uuids[i]);
870 uuids = g_variant_new("as", builder);
871 g_variant_builder_unref(builder);
872 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
873 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
876 param = g_variant_new("(isunsbub@asn@ay)",
879 remote_dev_info->class,
880 remote_dev_info->rssi,
881 remote_dev_info->name,
882 remote_dev_info->paired,
883 remote_dev_info->connected,
884 remote_dev_info->trust,
886 remote_dev_info->manufacturer_data_len,
890 /* Send the event to application */
891 _bt_send_event(BT_ADAPTER_EVENT,
892 BLUETOOTH_EVENT_BONDING_FINISHED,
895 _bt_free_device_info(remote_dev_info);
898 if (req_info->context == NULL)
901 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
902 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
905 if (_bt_adapter_get_status() != BT_ACTIVATED)
906 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
908 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
909 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
911 g_dbus_method_invocation_return_value(req_info->context,
912 g_variant_new("(iv)", result, out_param1));
914 g_variant_unref(out_param1);
916 _bt_delete_request_list(req_info->req_id);
921 _bt_agent_set_canceled(FALSE);
924 g_free(bonding_info->addr);
925 g_free(bonding_info);
929 int _bt_bond_device(int request_id,
930 bluetooth_device_address_t *device_address,
931 unsigned short conn_type, GArray **out_param1)
934 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
935 bluetooth_device_info_t dev_info;
937 GDBusConnection *conn;
938 char *device_path = NULL;
939 GDBusProxy *adapter_proxy;
940 GError *error = NULL;
941 gboolean bonding_wo_discovery = FALSE;
943 BT_CHECK_PARAMETER(device_address, return);
946 BT_ERR("Bonding in progress");
948 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
949 memcpy(dev_info.device_address.addr, device_address->addr,
950 BLUETOOTH_ADDRESS_LENGTH);
952 g_array_append_vals(*out_param1, &dev_info,
953 sizeof(bluetooth_device_info_t));
955 return BLUETOOTH_ERROR_DEVICE_BUSY;
958 conn = _bt_gdbus_get_system_gconn();
959 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
961 _bt_convert_addr_type_to_string(address, device_address->addr);
963 device_path = _bt_get_device_object_path(address);
965 if (device_path == NULL) {
966 BT_ERR("No searched device");
967 GVariant *ret = NULL;
968 adapter_proxy = _bt_get_adapter_proxy();
969 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
971 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
972 g_variant_new("(s)", address),
973 G_DBUS_CALL_FLAGS_NONE,
979 BT_ERR("CreateDevice Fail: %s", error->message);
980 g_clear_error(&error);
983 g_variant_unref(ret);
984 device_path = _bt_get_device_object_path(address);
985 if (device_path == NULL) {
986 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
987 memcpy(dev_info.device_address.addr, device_address->addr,
988 BLUETOOTH_ADDRESS_LENGTH);
990 g_array_append_vals(*out_param1, &dev_info,
991 sizeof(bluetooth_device_info_t));
993 return BLUETOOTH_ERROR_NOT_PAIRED;
995 BT_INFO("device_path is created[%s]", device_path);
997 bonding_wo_discovery = TRUE;
1000 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1001 NULL, BT_BLUEZ_NAME,
1002 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1004 g_free(device_path);
1005 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1007 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1008 bonding_info->addr = g_strdup(address);
1009 bonding_info->req_id = request_id;
1011 bonding_info->device_proxy = proxy;
1012 bonding_info->conn_type = conn_type;
1013 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1015 is_device_creating = TRUE;
1017 g_dbus_proxy_call(proxy, "Pair",
1018 g_variant_new("(y)", conn_type),
1019 G_DBUS_CALL_FLAGS_NONE,
1020 BT_MAX_DBUS_TIMEOUT,
1022 (GAsyncReadyCallback)__bt_bond_device_cb,
1025 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1027 return BLUETOOTH_ERROR_NONE;
1029 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1030 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1031 bonding_info->addr);
1033 g_array_append_vals(*out_param1, &dev_info,
1034 sizeof(bluetooth_device_info_t));
1036 is_device_creating = FALSE;
1038 g_free(bonding_info->addr);
1039 g_free(bonding_info);
1040 bonding_info = NULL;
1042 return BLUETOOTH_ERROR_INTERNAL;*/
1045 int _bt_cancel_bonding(void)
1047 int ret = BLUETOOTH_ERROR_NONE;
1049 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1051 ret = _bt_agent_reply_cancellation();
1052 if (ret != BLUETOOTH_ERROR_NONE) {
1053 BT_ERR("Fail to call reply cancellation");
1057 _bt_agent_set_canceled(TRUE);
1059 return BLUETOOTH_ERROR_NONE;
1062 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1067 GVariant *out_param1;
1068 int result = BLUETOOTH_ERROR_NONE;
1069 bt_funcion_data_t *unbonding_info;
1070 bluetooth_device_info_t dev_info;
1071 request_info_t *req_info;
1073 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1075 g_variant_unref(reply);
1077 unbonding_info = user_data;
1079 if (unbonding_info == NULL) {
1081 BT_ERR("unbonding_info == NULL");
1085 req_info = _bt_get_request_info(unbonding_info->req_id);
1086 if (req_info == NULL) {
1087 BT_ERR("req_info == NULL");
1092 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1093 result = BLUETOOTH_ERROR_INTERNAL;
1096 if (req_info->context == NULL)
1099 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1100 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1101 unbonding_info->addr);
1103 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1104 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1106 g_dbus_method_invocation_return_value(req_info->context,
1107 g_variant_new("(iv)", result, out_param1));
1110 _bt_delete_request_list(req_info->req_id);
1116 if (unbonding_info) {
1117 g_free(unbonding_info->addr);
1118 g_free(unbonding_info);
1122 int _bt_unbond_device(int request_id,
1123 bluetooth_device_address_t *device_address,
1124 GArray **out_param1)
1126 char *device_path = NULL;
1127 bt_funcion_data_t *unbonding_info;
1128 GDBusProxy *adapter_proxy = NULL;
1129 GDBusProxy *device_proxy = NULL;
1130 GDBusConnection *conn;
1131 int result = BLUETOOTH_ERROR_INTERNAL;
1132 bluetooth_device_info_t dev_info;
1133 GError *error = NULL;
1134 GVariant *ret = NULL;
1136 BT_CHECK_PARAMETER(device_address, return);
1138 adapter_proxy = _bt_get_adapter_proxy();
1139 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1141 /* allocate user data so that it can be retrieved in callback */
1142 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1143 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1144 unbonding_info->req_id = request_id;
1146 _bt_convert_addr_type_to_string(unbonding_info->addr,
1147 device_address->addr);
1149 device_path = _bt_get_device_object_path(unbonding_info->addr);
1151 if (device_path == NULL) {
1152 BT_ERR("No paired device");
1153 result = BLUETOOTH_ERROR_NOT_PAIRED;
1157 conn = _bt_gdbus_get_system_gconn();
1159 BT_ERR("conn is NULL");
1160 result = BLUETOOTH_ERROR_INTERNAL;
1164 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1165 NULL, BT_BLUEZ_NAME,
1166 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1168 if (device_proxy != NULL) {
1170 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1171 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1172 G_DBUS_CALL_FLAGS_NONE,
1177 BT_ERR("Getting property failed: [%s]\n", error->message);
1178 g_error_free(error);
1179 result = BLUETOOTH_ERROR_NOT_PAIRED;
1183 BT_ERR("No paired device");
1184 g_object_unref(device_proxy);
1185 result = BLUETOOTH_ERROR_NOT_PAIRED;
1188 g_variant_unref(ret);
1190 g_object_unref(device_proxy);
1193 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1194 g_variant_new("(o)", device_path),
1195 G_DBUS_CALL_FLAGS_NONE,
1196 BT_MAX_DBUS_TIMEOUT,
1198 (GAsyncReadyCallback)__bt_unbond_cb,
1201 g_free(device_path);
1202 return BLUETOOTH_ERROR_NONE;
1205 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1206 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1207 unbonding_info->addr);
1209 g_array_append_vals(*out_param1, &dev_info,
1210 sizeof(bluetooth_device_info_t));
1211 g_free(device_path);
1212 g_free(unbonding_info->addr);
1213 g_free(unbonding_info);
1217 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1222 GVariant *out_param1;
1223 int result = BLUETOOTH_ERROR_NONE;
1224 bluetooth_device_info_t dev_info;
1225 bt_remote_dev_info_t *remote_dev_info;
1226 request_info_t *req_info;
1227 GVariant *uuid_list, *manufacture_data;
1229 GVariantBuilder *builder = NULL;
1232 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1234 g_variant_unref(reply);
1236 g_object_unref(proxy);
1238 if (searching_info == NULL) {
1240 BT_ERR("unbonding_info == NULL");
1244 req_info = _bt_get_request_info(searching_info->req_id);
1245 if (req_info == NULL) {
1246 BT_ERR("req_info == NULL");
1251 g_dbus_error_strip_remote_error(err);
1252 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1254 if (g_strrstr("Operation canceled", err->message))
1255 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1256 else if (g_strrstr("In Progress", err->message))
1257 result = BLUETOOTH_ERROR_IN_PROGRESS;
1258 else if (g_strrstr("Host is down", err->message))
1259 result = BLUETOOTH_ERROR_HOST_DOWN;
1261 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1263 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1264 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1265 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1266 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1267 remote_dev_info->uuid_count > 0) {
1268 result = BLUETOOTH_ERROR_NONE;
1271 _bt_free_device_info(remote_dev_info);
1276 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1277 if (!remote_dev_info)
1281 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1282 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1283 g_variant_builder_add(builder, "s",
1284 remote_dev_info->uuids[i]);
1286 uuid_list = g_variant_new("as", builder);
1287 g_variant_builder_unref(builder);
1288 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1289 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1292 param = g_variant_new("(isunsbub@asn@ay)",
1294 searching_info->addr,
1295 remote_dev_info->class,
1296 remote_dev_info->rssi,
1297 remote_dev_info->name,
1298 remote_dev_info->paired,
1299 remote_dev_info->connected,
1300 remote_dev_info->trust,
1302 remote_dev_info->manufacturer_data_len,
1305 /* Send the event to application */
1306 _bt_send_event(BT_ADAPTER_EVENT,
1307 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1310 _bt_free_device_info(remote_dev_info);
1313 if (req_info->context == NULL)
1316 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1317 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1318 searching_info->addr);
1320 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1321 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1323 g_dbus_method_invocation_return_value(req_info->context,
1324 g_variant_new("(iv)", result, out_param1));
1327 _bt_delete_request_list(req_info->req_id);
1332 if (searching_info) {
1333 g_free(searching_info->addr);
1334 g_free(searching_info);
1335 searching_info = NULL;
1339 int _bt_search_device(int request_id,
1340 bluetooth_device_address_t *device_address)
1342 char *device_path = NULL;
1343 GDBusProxy *device_proxy = NULL;
1344 GDBusConnection *conn;
1346 GDBusProxy *adapter_proxy;
1347 int result = BLUETOOTH_ERROR_INTERNAL;
1349 BT_CHECK_PARAMETER(device_address, return);
1352 BT_ERR("Bonding in progress");
1353 return BLUETOOTH_ERROR_DEVICE_BUSY;
1356 if (searching_info) {
1357 BT_ERR("Service searching in progress");
1358 return BLUETOOTH_ERROR_DEVICE_BUSY;
1361 adapter_proxy = _bt_get_adapter_proxy();
1362 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1364 /* allocate user data so that it can be retrieved in callback */
1365 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1366 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1367 searching_info->req_id = request_id;
1369 _bt_convert_addr_type_to_string(searching_info->addr,
1370 device_address->addr);
1372 conn = _bt_gdbus_get_system_gconn();
1373 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1376 device_path = _bt_get_device_object_path(searching_info->addr);
1378 if (device_path == NULL) {
1379 BT_ERR("No paired device");
1380 result = BLUETOOTH_ERROR_NOT_PAIRED;
1384 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1385 NULL, BT_BLUEZ_NAME,
1386 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1387 g_free(device_path);
1388 if (device_proxy == NULL) {
1389 result = BLUETOOTH_ERROR_INTERNAL;
1393 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1394 g_variant_new("(s)", ""),
1395 G_DBUS_CALL_FLAGS_NONE,
1396 BT_MAX_DBUS_TIMEOUT,
1398 (GAsyncReadyCallback)__bt_discover_cb,
1401 searching_info->device_proxy = device_proxy;
1403 return BLUETOOTH_ERROR_NONE;
1406 g_free(searching_info->addr);
1407 g_free(searching_info);
1408 searching_info = NULL;
1412 int _bt_cancel_search_device(void)
1414 GVariant *ret = NULL;
1417 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1419 if (searching_info->device_proxy) {
1420 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1422 G_DBUS_CALL_FLAGS_NONE,
1427 g_variant_unref(ret);
1429 __bt_cancel_search_service_done();
1431 return BLUETOOTH_ERROR_NONE;
1434 int _bt_set_alias(bluetooth_device_address_t *device_address,
1437 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1438 gchar *device_path = NULL;
1439 GDBusProxy *adapter_proxy;
1440 GDBusProxy *device_proxy;
1441 GVariant *ret = NULL;
1442 GError *error = NULL;
1443 GDBusConnection *conn;
1445 BT_CHECK_PARAMETER(device_address, return);
1446 BT_CHECK_PARAMETER(alias, return);
1448 adapter_proxy = _bt_get_adapter_proxy();
1449 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1451 conn = _bt_gdbus_get_system_gconn();
1452 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1454 _bt_convert_addr_type_to_string(address, device_address->addr);
1456 device_path = _bt_get_device_object_path(address);
1458 if (device_path == NULL) {
1459 BT_ERR("No paired device");
1460 return BLUETOOTH_ERROR_NOT_PAIRED;
1463 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1464 NULL, BT_BLUEZ_NAME,
1465 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1467 g_free(device_path);
1468 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1470 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1471 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1472 G_DBUS_CALL_FLAGS_NONE,
1477 g_variant_unref(ret);
1479 g_object_unref(device_proxy);
1482 BT_ERR("SetProperty error: [%s]", error->message);
1483 g_error_free(error);
1484 return BLUETOOTH_ERROR_INTERNAL;
1487 return BLUETOOTH_ERROR_NONE;
1490 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1493 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1494 gchar *device_path = NULL;
1495 GDBusProxy *device_proxy;
1496 gboolean previous_value;
1497 GError *error = NULL;
1498 GDBusConnection *conn;
1499 GVariant *result = NULL;
1500 GVariant *temp = NULL;
1501 int ret = BLUETOOTH_ERROR_NONE;
1503 BT_CHECK_PARAMETER(device_address, return);
1505 conn = _bt_gdbus_get_system_gconn();
1506 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1508 _bt_convert_addr_type_to_string(address, device_address->addr);
1510 device_path = _bt_get_device_object_path(address);
1512 if (device_path == NULL) {
1513 BT_ERR("No paired device");
1514 return BLUETOOTH_ERROR_NOT_PAIRED;
1517 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1518 NULL, BT_BLUEZ_NAME,
1519 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1521 g_free(device_path);
1522 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1524 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1525 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1526 G_DBUS_CALL_FLAGS_NONE,
1530 if (error != NULL) {
1531 BT_ERR("Getting property failed: [%s]\n", error->message);
1532 g_error_free(error);
1533 g_object_unref(device_proxy);
1534 return BLUETOOTH_ERROR_INTERNAL;
1537 g_variant_get(result, "(v)", &temp);
1538 previous_value = g_variant_get_boolean(temp);
1539 g_variant_unref(temp);
1540 g_variant_unref(result);
1541 /* If the input is same with previous value, return error. */
1542 if (previous_value == authorize) {
1543 BT_ERR("Same value: %d", previous_value);
1544 g_object_unref(device_proxy);
1545 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1549 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1550 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1551 G_DBUS_CALL_FLAGS_NONE,
1556 g_object_unref(device_proxy);
1558 BT_ERR("SetProperty error: [%s]", error->message);
1559 g_error_free(error);
1560 ret = BLUETOOTH_ERROR_INTERNAL;
1564 g_variant_unref(result);
1569 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1570 gboolean *is_connected)
1572 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1573 char *object_path = NULL;
1575 GDBusProxy *device_proxy;
1576 GError *error = NULL;
1578 GVariant *tmp_value;
1579 GDBusConnection *conn;
1580 GVariant *result = NULL;
1581 int ret = BLUETOOTH_ERROR_NONE;
1583 BT_CHECK_PARAMETER(device_address, return);
1585 conn = _bt_gdbus_get_system_gconn();
1586 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1588 _bt_convert_addr_type_to_string(address, device_address->addr);
1590 object_path = _bt_get_device_object_path(address);
1591 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1593 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1594 NULL, BT_BLUEZ_NAME, object_path,
1595 BT_PROPERTIES_INTERFACE, NULL, NULL);
1596 g_free(object_path);
1597 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1599 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1600 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1601 G_DBUS_CALL_FLAGS_NONE,
1605 if (result == NULL) {
1606 if (error != NULL) {
1607 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1608 g_error_free(error);
1610 g_object_unref(device_proxy);
1611 return BLUETOOTH_ERROR_INTERNAL;
1614 g_variant_get(result , "(@a{sv})", &value);
1615 g_variant_unref(result);
1617 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1618 if (tmp_value == NULL) {
1619 g_object_unref(device_proxy);
1620 g_variant_unref(value);
1621 return BLUETOOTH_ERROR_INTERNAL;
1624 *is_connected = g_variant_get_boolean(tmp_value);
1626 BT_DBG("gatt is connected : %d", *is_connected);
1628 g_variant_unref(tmp_value);
1629 g_variant_unref(value);
1630 g_object_unref(device_proxy);
1635 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1636 int connection_type, gboolean *is_connected)
1638 char *object_path = NULL;
1639 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1640 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1641 GDBusProxy *device_proxy = NULL;
1642 GDBusProxy *adapter_proxy = NULL;
1643 GDBusConnection *conn;
1644 GError *error = NULL;
1645 GVariant *tmp_value = NULL;
1646 GVariant *value = NULL;
1647 GVariant *result = NULL;
1650 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1651 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1653 _bt_convert_addr_type_to_string(address, device_address->addr);
1654 _bt_convert_addr_string_to_secure_string(secure_address, address);
1656 *is_connected = FALSE;
1657 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1659 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1660 return _bt_rfcomm_is_device_connected(device_address,
1662 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1663 return _bt_is_gatt_connected(device_address, is_connected);
1664 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1665 return _bt_pbap_is_connected(device_address, is_connected);
1667 adapter_proxy = _bt_get_adapter_proxy();
1668 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1670 conn = _bt_gdbus_get_system_gconn();
1671 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1673 _bt_convert_addr_type_to_string(address, device_address->addr);
1675 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1676 object_path = _bt_get_adapter_path();
1677 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1678 NULL, BT_BLUEZ_NAME,
1679 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1680 g_free(object_path);
1681 if (device_proxy == NULL) {
1682 BT_DBG("Device don't have this service");
1683 return BLUETOOTH_ERROR_INTERNAL;
1686 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1687 g_variant_new("(s)", address),
1688 G_DBUS_CALL_FLAGS_NONE,
1693 if (result == NULL) {
1694 BT_ERR("[GetProperties] Error occured in Proxy call");
1696 BT_ERR("%s", error->message);
1697 g_error_free(error);
1699 *is_connected = FALSE;
1700 g_object_unref(device_proxy);
1701 return BLUETOOTH_ERROR_NONE;
1703 g_variant_get(result , "(@a{sv})", &value);
1704 g_variant_unref(result);
1707 tmp_value = g_variant_lookup_value(value,
1709 G_VARIANT_TYPE_BOOLEAN);
1711 *is_connected = g_variant_get_boolean(tmp_value);
1712 g_variant_unref(tmp_value);
1714 g_variant_unref(value);
1716 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1717 return _bt_is_network_connected(_bt_get_net_conn(),
1718 device_address->addr, is_connected);
1720 uuid = _bt_get_profile_uuid128(connection_type);
1722 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1723 return BLUETOOTH_ERROR_INTERNAL;
1725 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1727 object_path = _bt_get_device_object_path(address);
1729 BT_ERR("object_path is NULL");
1731 return BLUETOOTH_ERROR_NOT_PAIRED;
1734 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1735 NULL, BT_BLUEZ_NAME,
1736 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1737 g_free(object_path);
1738 if (device_proxy == NULL) {
1739 BT_DBG("Device don't have this service");
1741 return BLUETOOTH_ERROR_INTERNAL;
1744 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1745 g_variant_new("(s)", uuid),
1746 G_DBUS_CALL_FLAGS_NONE,
1751 if (result == NULL) {
1752 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1754 BT_ERR("%s", error->message);
1755 if (g_strrstr(error->message, "Not Connected"))
1756 BT_DBG("Not connected");
1757 g_error_free(error);
1759 *is_connected = FALSE;
1760 g_object_unref(device_proxy);
1762 return BLUETOOTH_ERROR_NONE;
1764 g_variant_get(result, "(b)", is_connected);
1766 g_variant_unref(result);
1769 g_object_unref(device_proxy);
1770 return BLUETOOTH_ERROR_NONE;
1773 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1774 bluetooth_connected_link_t *connected)
1776 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1777 char *object_path = NULL;
1779 GDBusProxy *device_proxy;
1780 GError *error = NULL;
1781 GDBusConnection *conn;
1782 GVariant *tmp_value = NULL;
1783 GVariant *value = NULL;
1784 GVariant *result = NULL;
1786 BT_CHECK_PARAMETER(device_address, return);
1788 conn = _bt_gdbus_get_system_gconn();
1789 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1791 _bt_convert_addr_type_to_string(address, device_address->addr);
1793 object_path = _bt_get_device_object_path(address);
1794 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1796 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1797 NULL, BT_BLUEZ_NAME,
1798 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1799 g_free(object_path);
1800 if (device_proxy == NULL) {
1801 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1802 return BLUETOOTH_ERROR_NONE;
1805 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1806 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1807 G_DBUS_CALL_FLAGS_NONE,
1812 if (error != NULL) {
1813 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1814 g_error_free(error);
1815 g_object_unref(device_proxy);
1816 return BLUETOOTH_ERROR_INTERNAL;
1819 g_variant_get(result , "(@a{sv})", &value);
1820 g_variant_unref(result);
1822 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1823 if (tmp_value != NULL) {
1824 *connected = g_variant_get_byte(tmp_value);
1825 g_variant_unref(tmp_value);
1826 g_object_unref(device_proxy);
1827 return BLUETOOTH_ERROR_NONE;
1829 BT_ERR("g_variant value is NULL");
1830 return BLUETOOTH_ERROR_INTERNAL;
1834 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1838 GVariant *out_param1;
1840 int result = BLUETOOTH_ERROR_NONE;
1841 bt_function_data_t *func_data = user_data;
1842 request_info_t *req_info = NULL;
1843 bluetooth_device_address_t device_addr = { {0} };
1845 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1846 g_object_unref(proxy);
1848 if (reply == NULL) {
1849 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1851 BT_ERR("Error: %s", err->message);
1852 g_clear_error(&err);
1854 result = BLUETOOTH_ERROR_INTERNAL;
1856 g_variant_unref(reply);
1859 if (func_data == NULL) {
1860 BT_ERR("func_data is NULL");
1864 req_info = _bt_get_request_info(func_data->req_id);
1865 if (req_info == NULL) {
1866 BT_ERR("req_info is NULL");
1870 if (req_info->context == NULL) {
1871 BT_ERR("req_info->context is NULL");
1875 _bt_convert_addr_string_to_type(device_addr.addr,
1876 (const char *)func_data->address);
1878 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1879 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1882 g_dbus_method_invocation_return_value(req_info->context,
1883 g_variant_new("(iv)", result, out_param1));
1887 _bt_delete_request_list(req_info->req_id);
1890 g_free(func_data->address);
1895 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1896 gboolean auto_connect)
1898 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1899 gchar *device_path = NULL;
1900 GDBusProxy *device_proxy = NULL;
1901 GDBusConnection *conn;
1902 int ret = BLUETOOTH_ERROR_NONE;
1903 bt_function_data_t *func_data;
1905 BT_CHECK_PARAMETER(bd_addr, return);
1907 conn = _bt_gdbus_get_system_gconn();
1908 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1910 _bt_convert_addr_type_to_string(device_address,
1911 (unsigned char *)bd_addr->addr);
1912 device_path = _bt_get_device_object_path(device_address);
1913 if (device_path == NULL) {
1914 BT_ERR_C("device_path NULL : [%s]", device_address);
1915 ret = BLUETOOTH_ERROR_INTERNAL;
1919 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1920 NULL, BT_BLUEZ_NAME,
1921 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1922 g_free(device_path);
1923 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1925 func_data = g_malloc0(sizeof(bt_function_data_t));
1926 func_data->address = g_strdup(device_address);
1927 if (func_data->address == NULL) {
1928 BT_ERR("Unable to allocate memory for address");
1929 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1933 func_data->req_id = req_id;
1935 g_dbus_proxy_call(device_proxy, "ConnectLE",
1936 g_variant_new("(b)", auto_connect),
1937 G_DBUS_CALL_FLAGS_NONE,
1938 BT_MAX_DBUS_TIMEOUT,
1940 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1946 g_object_unref(device_proxy);
1948 g_free(func_data->address);
1954 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1956 BT_INFO("Try to initiate pending LE connection");
1958 pending_le_conn_timer_id = 0;
1960 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1961 &pending_le_conn_info->bd_addr,
1962 pending_le_conn_info->auto_connect);
1964 g_free(pending_le_conn_info);
1965 pending_le_conn_info = NULL;
1970 void _bt_pending_connect_le_device(void)
1972 if (pending_le_conn_timer_id > 0) {
1973 g_source_remove(pending_le_conn_timer_id);
1974 __bt_connect_le_timer_cb(NULL);
1978 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1979 gboolean auto_connect)
1981 int ret = BLUETOOTH_ERROR_NONE;
1983 BT_CHECK_PARAMETER(bd_addr, return);
1985 ret = _bt_hold_current_advertising();
1986 if (ret == BLUETOOTH_ERROR_NONE) {
1987 BT_INFO("Current advertising is held");
1988 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1989 pending_le_conn_info->req_id = req_id;
1990 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1991 BLUETOOTH_ADDRESS_LENGTH);
1992 pending_le_conn_info->auto_connect = auto_connect;
1994 pending_le_conn_timer_id =
1995 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1997 return BLUETOOTH_ERROR_NONE;
2000 BT_ERR("Unable to hold advertising");
2002 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2005 int _bt_disconnect_le_device(int req_id,
2006 const bluetooth_device_address_t *bd_addr)
2008 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2010 GDBusProxy *device_proxy;
2011 GDBusConnection *conn;
2012 int ret = BLUETOOTH_ERROR_NONE;
2013 bt_function_data_t *func_data;
2015 BT_CHECK_PARAMETER(bd_addr, return);
2017 conn = _bt_gdbus_get_system_gconn();
2018 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2020 _bt_convert_addr_type_to_string(device_address,
2021 (unsigned char *)bd_addr->addr);
2022 device_path = _bt_get_device_object_path(device_address);
2023 if (device_path == NULL) {
2024 BT_DBG("device_path NULL");
2025 ret = BLUETOOTH_ERROR_INTERNAL;
2029 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2031 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2032 NULL, BT_BLUEZ_NAME,
2033 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2034 g_free(device_path);
2035 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2037 func_data = g_malloc0(sizeof(bt_function_data_t));
2038 func_data->address = g_strdup(device_address);
2039 if (func_data->address == NULL) {
2040 BT_ERR("Unable to allocate memory for address");
2041 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2045 func_data->req_id = req_id;
2047 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2049 G_DBUS_CALL_FLAGS_NONE,
2050 BT_MAX_DBUS_TIMEOUT,
2052 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2057 g_object_unref(device_proxy);
2059 g_free(func_data->address);
2065 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2067 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2068 gchar *device_path = NULL;
2069 GError *error = NULL;
2070 GDBusProxy *device_proxy = NULL;
2071 GDBusProxy *adapter_proxy;
2072 GDBusConnection *conn;
2073 int ret = BLUETOOTH_ERROR_NONE;
2075 BT_CHECK_PARAMETER(bd_addr, return);
2077 _bt_convert_addr_type_to_string(device_address,
2078 (unsigned char *)bd_addr->addr);
2080 conn = _bt_gdbus_get_system_gconn();
2081 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2083 adapter_proxy = _bt_get_adapter_proxy();
2084 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2086 device_path = _bt_get_device_object_path(device_address);
2087 if (device_path == NULL) {
2088 BT_DBG("device_path NULL");
2089 ret = BLUETOOTH_ERROR_INTERNAL;
2093 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2095 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2096 NULL, BT_BLUEZ_NAME,
2097 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2098 g_free(device_path);
2099 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2101 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2103 G_DBUS_CALL_FLAGS_NONE,
2108 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2109 g_error_free(error);
2110 g_object_unref(device_proxy);
2111 return BLUETOOTH_ERROR_INTERNAL;
2114 g_object_unref(device_proxy);
2119 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2121 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2122 gchar *device_path = NULL;
2123 GError *error = NULL;
2124 GDBusProxy *device_proxy = NULL;
2125 GDBusProxy *adapter_proxy;
2126 GDBusConnection *conn;
2127 int ret = BLUETOOTH_ERROR_NONE;
2129 BT_CHECK_PARAMETER(bd_addr, return);
2131 _bt_convert_addr_type_to_string(device_address,
2132 (unsigned char *)bd_addr->addr);
2134 conn = _bt_gdbus_get_system_gconn();
2135 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2137 adapter_proxy = _bt_get_adapter_proxy();
2138 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2140 device_path = _bt_get_device_object_path(device_address);
2141 if (device_path == NULL) {
2142 BT_DBG("device_path NULL");
2143 ret = BLUETOOTH_ERROR_INTERNAL;
2147 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2149 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2150 NULL, BT_BLUEZ_NAME,
2151 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2152 g_free(device_path);
2153 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2155 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2157 G_DBUS_CALL_FLAGS_NONE,
2162 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2163 g_error_free(error);
2164 g_object_unref(device_proxy);
2165 return BLUETOOTH_ERROR_INTERNAL;
2168 g_object_unref(device_proxy);
2173 int _bt_connect_profile(char *address, char *uuid,
2174 void *cb, gpointer func_data)
2178 GDBusConnection *conn;
2179 GDBusProxy *adapter_proxy;
2180 GVariant *result = NULL;
2181 GError *error = NULL;
2183 conn = _bt_gdbus_get_system_gconn();
2184 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2186 object_path = _bt_get_device_object_path(address);
2187 if (object_path == NULL) {
2188 BT_ERR("No searched device");
2190 adapter_proxy = _bt_get_adapter_proxy();
2191 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2193 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2194 g_variant_new("(s)", address),
2195 G_DBUS_CALL_FLAGS_NONE,
2200 if (error != NULL) {
2201 BT_ERR("CreateDevice Fail: %s", error->message);
2202 g_error_free(error);
2205 g_variant_unref(result);
2207 object_path = _bt_get_device_object_path(address);
2209 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2211 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2212 NULL, BT_BLUEZ_NAME,
2213 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2214 g_free(object_path);
2215 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2217 g_dbus_proxy_call(proxy, "ConnectProfile",
2218 g_variant_new("(s)", uuid),
2219 G_DBUS_CALL_FLAGS_NONE,
2220 BT_MAX_DBUS_TIMEOUT,
2222 (GAsyncReadyCallback)cb,
2225 return BLUETOOTH_ERROR_NONE;
2228 int _bt_disconnect_all(char *address)
2230 int ret = BLUETOOTH_ERROR_NONE;
2233 GDBusConnection *conn;
2234 GVariant *result = NULL;
2238 conn = _bt_gdbus_get_system_gconn();
2239 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2241 object_path = _bt_get_device_object_path(address);
2242 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2244 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2245 NULL, BT_BLUEZ_NAME,
2246 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2247 g_free(object_path);
2248 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2250 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2252 G_DBUS_CALL_FLAGS_NONE,
2257 BT_ERR("Dbus Call Error:[%s]", err->message);
2259 ret = BLUETOOTH_ERROR_INTERNAL;
2262 g_object_unref(proxy);
2264 g_variant_unref(result);
2269 int _bt_disconnect_profile(char *address, char *uuid,
2270 void *cb, gpointer func_data)
2274 GDBusConnection *conn;
2276 conn = _bt_gdbus_get_system_gconn();
2277 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2279 object_path = _bt_get_device_object_path(address);
2280 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2282 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2283 NULL, BT_BLUEZ_NAME,
2284 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2285 g_free(object_path);
2286 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2288 g_dbus_proxy_call(proxy, "DisconnectProfile",
2289 g_variant_new("(s)", uuid),
2290 G_DBUS_CALL_FLAGS_NONE,
2291 BT_MAX_DBUS_TIMEOUT,
2293 (GAsyncReadyCallback)cb,
2296 return BLUETOOTH_ERROR_NONE;
2299 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2300 int low_threshold, int in_range_threshold, int high_threshold)
2302 int ret = BLUETOOTH_ERROR_NONE;
2304 GVariant *result = NULL;
2305 GError *error = NULL;
2306 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2308 BT_CHECK_PARAMETER(bd_addr, return);
2309 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2310 bd_addr->addr[0], bd_addr->addr[1],
2311 bd_addr->addr[2], bd_addr->addr[3],
2312 bd_addr->addr[4], bd_addr->addr[5],
2314 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2315 in_range_threshold, high_threshold);
2317 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2319 proxy = _bt_get_adapter_proxy();
2320 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2322 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2323 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2324 G_DBUS_CALL_FLAGS_NONE,
2328 if (error != NULL) {
2329 BT_ERR("Dbus Call Error:[%s]", error->message);
2330 g_error_free(error);
2331 ret = BLUETOOTH_ERROR_INTERNAL;
2335 g_variant_unref(result);
2340 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2343 int ret = BLUETOOTH_ERROR_NONE;
2345 GVariant *result = NULL;
2346 GError *error = NULL;
2347 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2349 BT_CHECK_PARAMETER(bd_addr, return);
2350 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2351 bd_addr->addr[0], bd_addr->addr[1],
2352 bd_addr->addr[2], bd_addr->addr[3],
2353 bd_addr->addr[4], bd_addr->addr[5],
2356 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2358 proxy = _bt_get_adapter_proxy();
2359 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2361 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2362 g_variant_new("(si)", address, link_type),
2363 G_DBUS_CALL_FLAGS_NONE,
2368 if (error != NULL) {
2369 BT_ERR("Dbus Call Error:[%s]", error->message);
2370 g_error_free(error);
2371 ret = BLUETOOTH_ERROR_INTERNAL;
2375 g_variant_unref(result);
2380 int _bt_le_conn_update(unsigned char *device_address,
2381 float interval_min, float interval_max,
2382 guint16 latency, guint16 time_out)
2384 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2385 gchar *device_path = NULL;
2386 GError *error = NULL;
2387 GDBusProxy *device_proxy = NULL;
2388 GDBusConnection *conn;
2390 guint32 min, max, to;
2391 guint32 min_supervision_to;
2392 int ret = BLUETOOTH_ERROR_NONE;
2394 BT_CHECK_PARAMETER(device_address, return);
2396 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2397 interval_min, interval_max, latency, time_out);
2399 if (interval_min > interval_max ||
2400 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2401 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2402 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2406 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2407 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2408 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2412 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2413 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2418 * The Supervision_Timeout in milliseconds shall be larger than
2419 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2420 * where Conn_Interval_Max is given in milliseconds.
2422 min_supervision_to = (1 + latency) * interval_max * 2;
2423 if (time_out <= min_supervision_to) {
2424 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2428 _bt_convert_addr_type_to_string(address, device_address);
2430 BT_DBG("Remote device address: %s", address);
2432 device_path = _bt_get_device_object_path(address);
2434 if (device_path == NULL) {
2435 BT_ERR("device_path NULL");
2436 ret = BLUETOOTH_ERROR_INTERNAL;
2440 conn = _bt_gdbus_get_system_gconn();
2442 BT_ERR("conn NULL");
2443 ret = BLUETOOTH_ERROR_INTERNAL;
2447 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2448 NULL, BT_BLUEZ_NAME,
2449 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2451 g_free(device_path);
2452 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2454 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2455 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2456 to = time_out / BT_LE_CONN_TO_SPLIT;
2458 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2459 g_variant_new("(uuuu)", min, max, latency, to),
2460 G_DBUS_CALL_FLAGS_NONE,
2465 g_object_unref(device_proxy);
2466 if (reply == NULL) {
2468 BT_ERR("Error %s[%s]", error->message, address);
2469 if (g_strrstr(error->message, "In Progress"))
2470 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2472 ret = BLUETOOTH_ERROR_INTERNAL;
2473 g_error_free(error);
2477 g_variant_unref(reply);
2483 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2484 bluetooth_device_pin_code_t *pin_code)
2486 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2488 bt_pin_code_info_t *pin_info = NULL;
2490 BT_CHECK_PARAMETER(device_address, return);
2491 BT_CHECK_PARAMETER(pin_code, return);
2492 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2493 BLUETOOTH_ERROR_NO_RESOURCES);
2495 _bt_convert_addr_type_to_string(address, device_address->addr);
2497 for (l = pin_info_list; l != NULL; l = l->next) {
2500 if (g_strcmp0(pin_info->address, address) == 0) {
2501 g_free(pin_info->pin_code);
2502 pin_info->pin_code = g_strdup(pin_code->pin_code);
2503 return BLUETOOTH_ERROR_NONE;
2507 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2508 pin_info->address = g_strdup(address);
2509 pin_info->pin_code = g_strdup(pin_code->pin_code);
2510 pin_info_list = g_slist_append(pin_info_list, pin_info);
2512 return BLUETOOTH_ERROR_NONE;
2515 gint __bt_compare_address(gpointer *a, gpointer *b)
2517 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2518 char *address = (char *)b;
2519 return g_strcmp0(pin_info->address, address);
2522 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2524 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2526 bt_pin_code_info_t *pin_info = NULL;
2528 BT_CHECK_PARAMETER(device_address, return);
2530 _bt_convert_addr_type_to_string(address, device_address->addr);
2532 l = g_slist_find_custom(pin_info_list, address,
2533 (GCompareFunc)__bt_compare_address);
2537 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2538 g_free(pin_info->address);
2539 g_free(pin_info->pin_code);
2543 return BLUETOOTH_ERROR_NONE;
2546 int _bt_get_device_pin_code(const char *address, char *pin_code)
2550 BT_CHECK_PARAMETER(address, return);
2551 BT_CHECK_PARAMETER(pin_code, return);
2553 for (l = pin_info_list; l != NULL; l = l->next) {
2554 bt_pin_code_info_t *pin_info = l->data;
2556 if (g_strcmp0(pin_info->address, address) == 0) {
2557 g_strlcpy(pin_code, pin_info->pin_code,
2558 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2560 return BLUETOOTH_ERROR_NONE;
2564 return BLUETOOTH_ERROR_NOT_FOUND;
2567 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2568 bluetooth_le_connection_param_t *param)
2571 return BLUETOOTH_ERROR_INVALID_PARAM;
2573 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2574 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2575 return BLUETOOTH_ERROR_INVALID_PARAM;
2577 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2580 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2581 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2582 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2583 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2584 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2587 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2588 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2589 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2590 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2591 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2594 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2595 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2596 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2597 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2598 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2602 BT_ERR("Unhandled mode : %d", mode);
2606 return BLUETOOTH_ERROR_NONE;
2609 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2610 guint trusted_profile_flag, guint *trusted)
2616 case TRUSTED_PROFILE_PBAP:
2617 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2618 trust_profile = trusted_profile_flag & (1 << 1);
2620 return BLUETOOTH_ERROR_NOT_SUPPORT;
2622 case TRUSTED_PROFILE_MAP:
2623 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2624 trust_profile = trusted_profile_flag & (1 << 3);
2626 return BLUETOOTH_ERROR_NOT_SUPPORT;
2628 case TRUSTED_PROFILE_SAP:
2629 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2630 trust_profile = trusted_profile_flag & (1 << 5);
2632 return BLUETOOTH_ERROR_NOT_SUPPORT;
2634 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2635 *trusted = trusted_profile_flag;
2636 return BLUETOOTH_ERROR_NONE;
2638 return BLUETOOTH_ERROR_NOT_SUPPORT;
2644 return BLUETOOTH_ERROR_NONE;
2647 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2648 guint restricted_profile_flag, guint *restricted)
2650 int restrict_profile;
2651 *restricted = FALSE;
2654 case RESTRICTED_PROFILE_HFP_HS:
2655 restrict_profile = restricted_profile_flag & (1 << 0);
2657 case RESTRICTED_PROFILE_A2DP:
2658 restrict_profile = restricted_profile_flag & (1 << 2);
2661 return BLUETOOTH_ERROR_NOT_SUPPORT;
2664 if (restrict_profile)
2667 return BLUETOOTH_ERROR_NONE;
2670 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2673 case TRUSTED_PROFILE_PBAP:
2674 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2675 case TRUSTED_PROFILE_MAP:
2676 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2677 case TRUSTED_PROFILE_SAP:
2678 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2679 case TRUSTED_PROFILE_ALL:
2686 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2689 case RESTRICTED_PROFILE_HFP_HS:
2690 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2691 case RESTRICTED_PROFILE_A2DP:
2692 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2698 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2700 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2701 return TRUSTED_PROFILE_PBAP;
2702 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2703 return TRUSTED_PROFILE_MAP;
2704 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2705 return TRUSTED_PROFILE_SAP;
2707 return 0; /* 0 - Unknown Profile */
2710 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2711 bluetooth_trusted_profile_t profile, gboolean trust)
2713 int ret = BLUETOOTH_ERROR_NONE;
2714 GDBusConnection *conn;
2716 GError *error = NULL;
2717 char *device_path = NULL;
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_DEVICE_INTERFACE, NULL, NULL);
2741 g_free(device_path);
2742 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2744 uuid = _bt_get_trusted_profile_uuid(profile);
2746 g_object_unref(proxy);
2747 return BLUETOOTH_ERROR_NOT_SUPPORT;
2750 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2751 g_variant_new("(sb)", uuid, trust),
2752 G_DBUS_CALL_FLAGS_NONE, -1,
2754 g_object_unref(proxy);
2756 if (reply == NULL) {
2757 BT_ERR("Failed to Set Profile Trusted");
2758 ret = BLUETOOTH_ERROR_INTERNAL;
2760 BT_ERR("Error %s[%s]", error->message, address);
2761 g_error_free(error);
2765 g_variant_unref(reply);
2772 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2773 bluetooth_trusted_profile_t profile, guint *trust)
2775 int ret = BLUETOOTH_ERROR_NONE;
2776 GDBusConnection *conn;
2778 GError *error = NULL;
2779 char *device_path = NULL;
2780 guint trusted_profile_flag;
2781 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2784 BT_CHECK_PARAMETER(bd_addr, return);
2785 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2786 bd_addr->addr[0], bd_addr->addr[1],
2787 bd_addr->addr[2], bd_addr->addr[3],
2788 bd_addr->addr[4], bd_addr->addr[5],
2791 conn = _bt_gdbus_get_system_gconn();
2792 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2794 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2796 device_path = _bt_get_device_object_path(address);
2797 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2799 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2800 NULL, BT_BLUEZ_NAME, device_path,
2801 BT_PROPERTIES_INTERFACE, NULL, NULL);
2803 g_free(device_path);
2804 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2806 reply = g_dbus_proxy_call_sync(proxy, "Get",
2807 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2808 G_DBUS_CALL_FLAGS_NONE, -1,
2810 g_object_unref(proxy);
2812 if (reply == NULL) {
2813 BT_ERR("Failed to Get Profile Trusted");
2814 ret = BLUETOOTH_ERROR_INTERNAL;
2816 BT_ERR("Error %s[%s]", error->message, address);
2817 g_error_free(error);
2822 g_variant_get(reply, "(v)", &temp);
2823 trusted_profile_flag = g_variant_get_uint32(temp);
2824 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2826 ret = _bt_get_trusted_profile_from_flag(profile,
2827 trusted_profile_flag, trust);
2828 g_variant_unref(temp);
2829 g_variant_unref(reply);
2832 BT_DBG("TRUST %d", *trust);
2836 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2837 bluetooth_restricted_profile_t profile, gboolean restricted)
2839 int ret = BLUETOOTH_ERROR_NONE;
2840 GDBusConnection *conn;
2842 GError *error = NULL;
2843 char *device_path = NULL;
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_DEVICE_INTERFACE, NULL, NULL);
2867 g_free(device_path);
2868 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2870 uuid = _bt_get_restricted_profile_uuid(profile);
2872 g_object_unref(proxy);
2873 return BLUETOOTH_ERROR_NOT_SUPPORT;
2876 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2877 g_variant_new("(sb)", uuid, restricted),
2878 G_DBUS_CALL_FLAGS_NONE, -1,
2880 g_object_unref(proxy);
2882 if (reply == NULL) {
2883 BT_ERR("Failed to Set Profile Restricted");
2884 ret = BLUETOOTH_ERROR_INTERNAL;
2886 BT_ERR("Error %s[%s]", error->message, address);
2887 g_error_free(error);
2891 g_variant_unref(reply);
2898 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2899 bluetooth_restricted_profile_t profile, guint *restricted)
2901 int ret = BLUETOOTH_ERROR_NONE;
2902 GDBusConnection *conn;
2904 GError *error = NULL;
2905 char *device_path = NULL;
2906 guint restricted_profile_flag;
2907 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2910 BT_CHECK_PARAMETER(bd_addr, return);
2911 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2912 bd_addr->addr[0], bd_addr->addr[1],
2913 bd_addr->addr[2], bd_addr->addr[3],
2914 bd_addr->addr[4], bd_addr->addr[5],
2915 profile, *restricted);
2917 conn = _bt_gdbus_get_system_gconn();
2918 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2920 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2922 device_path = _bt_get_device_object_path(address);
2923 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2925 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2926 NULL, BT_BLUEZ_NAME, device_path,
2927 BT_PROPERTIES_INTERFACE, NULL, NULL);
2929 g_free(device_path);
2930 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2932 reply = g_dbus_proxy_call_sync(proxy, "Get",
2933 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2934 G_DBUS_CALL_FLAGS_NONE, -1,
2936 g_object_unref(proxy);
2938 if (reply == NULL) {
2939 BT_ERR("Failed to Get Profile Restricted");
2940 ret = BLUETOOTH_ERROR_INTERNAL;
2942 BT_ERR("Error %s[%s]", error->message, address);
2943 g_error_free(error);
2948 g_variant_get(reply, "(v)", &temp);
2949 restricted_profile_flag = g_variant_get_uint32(temp);
2950 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2952 ret = _bt_get_restricted_profile_from_flag(profile,
2953 restricted_profile_flag, restricted);
2954 g_variant_unref(temp);
2955 g_variant_unref(reply);
2958 BT_DBG("TRUST %d", *restricted);
2962 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2966 GVariant *out_param1;
2967 request_info_t *req_info;
2968 GVariant *val = NULL;
2969 GVariant *param = NULL;
2972 bluetooth_device_address_t device_addr = { {0} };
2973 int result = BLUETOOTH_ERROR_NONE;
2976 val = g_dbus_proxy_call_finish(proxy, res, &err);
2978 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2979 if (req_info == NULL) {
2980 BT_ERR("req_info == NULL");
2981 g_object_unref(proxy);
2982 att_mtu_req_info->device_proxy = NULL;
2987 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2989 if (g_strrstr(err->message, "NotSupported")) {
2990 BT_INFO("Connection Not Supported");
2991 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2992 } else if (g_strrstr(err->message, "NotConnected")) {
2993 BT_INFO("Not connected");
2994 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2995 } else if (g_strrstr(err->message, "InvalidArguments")) {
2996 BT_INFO("Not connected");
2997 result = BLUETOOTH_ERROR_INVALID_PARAM;
2999 BT_DBG("Default case");
3000 result = BLUETOOTH_ERROR_INTERNAL;
3004 g_object_unref(proxy);
3005 att_mtu_req_info->device_proxy = NULL;
3007 if (result != BLUETOOTH_ERROR_NONE)
3011 g_variant_get(val, "(qy)", &mtu, &status);
3012 g_variant_unref(val);
3015 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3017 param = g_variant_new("(isqy)",
3019 att_mtu_req_info->addr,
3023 /* Send the event to application */
3024 _bt_send_event(BT_DEVICE_EVENT,
3025 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3029 if (req_info->context == NULL)
3032 _bt_convert_addr_string_to_type(device_addr.addr,
3033 (const char *)att_mtu_req_info->addr);
3035 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3036 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3037 g_dbus_method_invocation_return_value(req_info->context,
3038 g_variant_new("(iv)", result, out_param1));
3040 _bt_delete_request_list(req_info->req_id);
3045 g_free(att_mtu_req_info->addr);
3046 g_free(att_mtu_req_info);
3047 att_mtu_req_info = NULL;
3052 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3055 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3056 gchar *device_path = NULL;
3057 GDBusProxy *adapter_proxy;
3058 GDBusProxy *device_proxy;
3059 GDBusConnection *conn;
3060 int ret = BLUETOOTH_ERROR_NONE;
3062 BT_CHECK_PARAMETER(device_address, return);
3064 if (att_mtu_req_info) {
3065 BT_ERR("ATT MTU request in progress");
3066 return BLUETOOTH_ERROR_DEVICE_BUSY;
3069 conn = _bt_gdbus_get_system_gconn();
3070 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3072 adapter_proxy = _bt_get_adapter_proxy();
3073 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3075 _bt_convert_addr_type_to_string(address, device_address->addr);
3077 BT_DBG("Remote device address: %s", address);
3079 device_path = _bt_get_device_object_path(address);
3081 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3083 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3084 NULL, BT_BLUEZ_NAME,
3085 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3086 g_free(device_path);
3087 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3089 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3090 att_mtu_req_info->addr = (char *)g_strdup(address);
3091 att_mtu_req_info->req_id = request_id;
3092 att_mtu_req_info->device_proxy = device_proxy;
3094 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3095 g_variant_new("(q)", mtu),
3096 G_DBUS_CALL_FLAGS_NONE,
3097 BT_MAX_DBUS_TIMEOUT,
3099 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3105 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3108 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3109 char *object_path = NULL;
3111 GDBusProxy *device_proxy;
3112 GError *error = NULL;
3114 GVariant *tmp_value;
3115 GDBusConnection *conn;
3116 GVariant *result = NULL;
3117 int ret = BLUETOOTH_ERROR_NONE;
3119 BT_CHECK_PARAMETER(device_address, return);
3121 conn = _bt_gdbus_get_system_gconn();
3122 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3124 _bt_convert_addr_type_to_string(address, device_address->addr);
3126 object_path = _bt_get_device_object_path(address);
3127 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3129 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3130 NULL, BT_BLUEZ_NAME, object_path,
3131 BT_PROPERTIES_INTERFACE, NULL, NULL);
3132 g_free(object_path);
3133 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3135 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3136 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3137 G_DBUS_CALL_FLAGS_NONE,
3141 if (result == NULL) {
3142 if (error != NULL) {
3143 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3144 g_error_free(error);
3146 g_object_unref(device_proxy);
3147 return BLUETOOTH_ERROR_INTERNAL;
3150 g_variant_get(result , "(@a{sv})", &value);
3151 g_variant_unref(result);
3153 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3154 if (tmp_value == NULL) {
3155 g_object_unref(device_proxy);
3156 g_variant_unref(value);
3157 return BLUETOOTH_ERROR_INTERNAL;
3160 *mtu = g_variant_get_uint16(tmp_value);
3162 BT_DBG("ATT MTU : %d", *mtu);
3164 g_variant_unref(tmp_value);
3165 g_variant_unref(value);
3166 g_object_unref(device_proxy);
3171 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3172 bluetooth_device_address_t *id_address)
3174 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3175 gchar *device_path = NULL;
3176 const gchar *idaddress = NULL;
3177 GDBusProxy *device_proxy;
3178 GError *error = NULL;
3179 GVariant *result = NULL;
3180 GDBusConnection *conn;
3181 int ret = BLUETOOTH_ERROR_NONE;
3183 BT_CHECK_PARAMETER(device_address, return);
3185 conn = _bt_gdbus_get_system_gconn();
3186 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3188 _bt_convert_addr_type_to_string(address, device_address->addr);
3190 device_path = _bt_get_device_object_path(address);
3191 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3193 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3194 NULL, BT_BLUEZ_NAME,
3195 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3196 g_free(device_path);
3197 if (!device_proxy) {
3198 BT_ERR("Unable to get proxy");
3199 return BLUETOOTH_ERROR_INTERNAL;
3202 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3204 G_DBUS_CALL_FLAGS_NONE,
3209 if (result == NULL) {
3210 BT_ERR("Failed to get device ID address");
3211 if (error != NULL) {
3212 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3213 g_error_free(error);
3215 g_object_unref(device_proxy);
3216 return BLUETOOTH_ERROR_INTERNAL;
3219 g_variant_get(result , "(s)", &idaddress);
3220 g_variant_unref(result);
3222 if (idaddress == NULL) {
3223 BT_ERR("No paired device");
3224 g_object_unref(device_proxy);
3225 return BLUETOOTH_ERROR_NOT_PAIRED;
3228 BT_DBG("ID Address:%s", idaddress);
3231 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3233 ret = BLUETOOTH_ERROR_INTERNAL;
3235 g_object_unref(device_proxy);
3240 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3242 GapAgentPrivate *agent = _bt_get_adapter_agent();
3243 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3245 if (authentication_reply)
3246 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3248 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3250 BT_DBG("BT_PASSKEY_REPLY");
3251 return BLUETOOTH_ERROR_NONE;
3254 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3256 GapAgentPrivate *agent = _bt_get_adapter_agent();
3257 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3259 if (confirmation_reply)
3260 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3262 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3264 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3265 return BLUETOOTH_ERROR_NONE;