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 info->uuid_count = g_variant_get_size(value);
321 info->uuids = g_variant_dup_strv(value, (gsize *)&info->uuid_count);
324 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
326 char *object_path = NULL;
327 bt_remote_dev_info_t *dev_info;
329 retv_if(address == NULL, NULL);
331 object_path = _bt_get_device_object_path(address);
332 retv_if(object_path == NULL, NULL);
334 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
340 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
341 const char *object_path)
343 bt_remote_dev_info_t *dev_info;
344 GDBusProxy *adapter_proxy;
345 GDBusProxy *device_proxy;
350 GDBusConnection *conn;
351 GError *error = NULL;
352 GVariant *result = NULL;
353 GVariantIter *value_iter;
357 adapter_proxy = _bt_get_adapter_proxy();
358 retv_if(adapter_proxy == NULL, NULL);
360 retv_if(object_path == NULL, NULL);
362 conn = _bt_gdbus_get_system_gconn();
364 BT_ERR("conn == NULL");
368 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
369 NULL, BT_BLUEZ_NAME, object_path,
370 BT_PROPERTIES_INTERFACE, NULL, NULL);
372 retv_if(device_proxy == NULL, NULL);
374 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
375 g_variant_new("(s)", BT_DEVICE_INTERFACE),
376 G_DBUS_CALL_FLAGS_NONE,
381 g_object_unref(device_proxy);
383 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
385 if (result != NULL) {
386 g_variant_get(result , "(@a{sv})", &value);
387 g_variant_unref(result);
389 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
391 g_variant_get(tmp_value, "s", &name);
392 g_variant_unref(tmp_value);
394 DBG_SECURE("Alias Name [%s]", name);
396 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
397 g_variant_get(tmp_value, "s", &name);
398 g_variant_unref(tmp_value);
401 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
403 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
404 g_variant_unref(tmp_value);
406 dev_info->is_alias_set = FALSE;
408 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
410 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
412 dev_info->class = g_variant_get_uint32(tmp_value);
413 g_variant_unref(tmp_value);
417 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
419 dev_info->connected = g_variant_get_byte(tmp_value);
420 g_variant_unref(tmp_value);
422 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
423 BT_DBG("connected link : %d", dev_info->connected);
425 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
427 dev_info->trust = g_variant_get_boolean(tmp_value);
428 g_variant_unref(tmp_value);
430 dev_info->trust = FALSE;
432 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
434 dev_info->paired = g_variant_get_boolean(tmp_value);
435 g_variant_unref(tmp_value);
437 dev_info->paired = FALSE;
439 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
441 dev_info->rssi = g_variant_get_int16(tmp_value);
442 g_variant_unref(tmp_value);
446 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
448 dev_info->addr_type = g_variant_get_byte(tmp_value);
449 g_variant_unref(tmp_value);
451 dev_info->addr_type = 0;
453 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
455 __bt_get_uuids(tmp_value, dev_info);
456 g_variant_unref(tmp_value);
459 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
461 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
462 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
463 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
464 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
466 g_variant_unref(tmp_value);
468 dev_info->manufacturer_data_len = 0;
470 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
472 if ((dev_info->manufacturer_data_len == 0) ||
473 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
474 BT_ERR("manufacturer data length doesn't match");
475 dev_info->manufacturer_data_len = 0;
476 dev_info->manufacturer_data = NULL;
478 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
479 g_variant_get(tmp_value, "ay", &value_iter);
480 while (g_variant_iter_loop(value_iter, "y", &m_value))
481 dev_info->manufacturer_data[i++] = m_value;
483 g_variant_unref(tmp_value);
485 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
486 dev_info->manufacturer_data_len = 0;
487 dev_info->manufacturer_data = NULL;
490 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
491 g_variant_get(tmp_value, "s", &address);
492 g_variant_unref(tmp_value);
494 dev_info->address = g_strdup(address);
495 dev_info->name = g_strdup(name);
497 g_variant_unref(value);
499 BT_ERR("result is NULL\n");
507 char *_bt_get_bonded_device_name(char *address)
509 bluetooth_device_address_t device_address = { {0} };
510 bluetooth_device_info_t dev_info;
512 retv_if(address == NULL, strdup(""));
514 _bt_convert_addr_string_to_type(device_address.addr, address);
516 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
518 _bt_get_bonded_device_info(&device_address, &dev_info);
520 return g_strdup(dev_info.device_name.name);
523 static gboolean __ignore_auto_pairing_request(const char *address)
528 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
539 /* Get the LAP(Lower Address part) */
540 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
542 snprintf(lap_address, sizeof(lap_address), ",%s", address);
544 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
547 BT_ERR("fopen failed \n");
551 fseek(fp, 0, SEEK_END);
556 BT_ERR("Get file size failed \n");
561 buffer = g_malloc0(sizeof(char) * size);
562 result = fread((char *)buffer, 1, size, fp);
564 if (result != size) {
565 BT_ERR("Read Error\n");
570 BT_DBG("Buffer = %s\n", buffer);
572 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
578 /* Write the data and insert new device data */
579 for (i = 0; lines[i] != NULL; i++) {
580 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
581 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
583 lines[i] = temp_buffer;
586 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
588 /* Fix : NULL_RETURNS */
589 retv_if(buffer == NULL, FALSE);
591 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
594 BT_ERR("fopen failed \n");
599 BT_DBG("Buffer = %s\n", buffer);
600 fwrite(buffer, 1, strlen(buffer), fp);
610 static int __bt_cancel_bonding(void)
612 BT_CHECK_PARAMETER(bonding_info, return);
613 BT_CHECK_PARAMETER(bonding_info->addr, return);
614 /* First Cancel the ongoing pairing in bluez otherwise if we send
615 * pair request bluez will send inprogress and we again retry bond so
616 * this cycle continues */
618 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
619 NULL, G_DBUS_CALL_FLAGS_NONE,
622 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
624 return BLUETOOTH_ERROR_PARING_FAILED;
626 return BLUETOOTH_ERROR_NONE;
629 static int __bt_retry_bond(void)
631 BT_CHECK_PARAMETER(bonding_info, return);
632 BT_CHECK_PARAMETER(bonding_info->addr, return);
634 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
635 g_variant_new("(y)", bonding_info->conn_type),
636 G_DBUS_CALL_FLAGS_NONE,
639 (GAsyncReadyCallback)__bt_bond_device_cb,
642 return BLUETOOTH_ERROR_NONE;
646 static int __bt_remove_and_bond(void)
648 GDBusProxy *adapter_proxy;
649 GVariant *result = NULL;
651 char *device_path = NULL;
653 BT_CHECK_PARAMETER(bonding_info, return);
654 BT_CHECK_PARAMETER(bonding_info->addr, return);
656 adapter_proxy = _bt_get_adapter_proxy();
657 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
659 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
660 g_variant_new("(s)", bonding_info->addr),
661 G_DBUS_CALL_FLAGS_NONE,
666 return BLUETOOTH_ERROR_INTERNAL;
668 g_variant_get(result , "(o)", &device_path);
669 g_variant_unref(result);
671 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
673 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
674 g_variant_new("(o)", device_path),
675 G_DBUS_CALL_FLAGS_NONE,
681 BT_ERR("UnpairDevice Fail: %s", err->message);
683 return BLUETOOTH_ERROR_INTERNAL;
686 return __bt_retry_bond();
689 static int __bt_cancel_and_bond(void)
691 int ret = BLUETOOTH_ERROR_NONE;
693 ret = _bt_agent_reply_cancellation();
694 if (ret != BLUETOOTH_ERROR_NONE) {
695 BT_ERR("Fail to call reply cancellation");
698 return __bt_retry_bond();
702 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
705 int result = BLUETOOTH_ERROR_NONE;
708 GVariant *out_param1;
709 request_info_t *req_info;
710 bluetooth_device_info_t dev_info;
711 bt_remote_dev_info_t *remote_dev_info;
712 GVariant *manufacture_data;
715 /* Terminate ALL system popup */
716 syspopup_destroy_all();
718 reply = g_dbus_proxy_call_finish(proxy, res, &err);
720 g_variant_unref(reply);
722 is_device_creating = FALSE;
724 if (bonding_info == NULL) {
726 BT_ERR("bonding_info == NULL");
732 req_info = _bt_get_request_info(bonding_info->req_id);
733 if (req_info == NULL) {
734 BT_ERR("req_info == NULL");
739 g_dbus_error_strip_remote_error(err);
740 BT_ERR("Error occured in CreateBonding [%s]", err->message);
742 if (g_strrstr(err->message, "Already Exists")) {
743 BT_INFO("Existing Bond, remove and retry");
744 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
746 result = BLUETOOTH_ERROR_PARING_FAILED;
747 } else if (_bt_agent_is_canceled() ||
748 g_strrstr(err->message, "Authentication Canceled")) {
749 BT_INFO("Cancelled by USER");
750 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
751 } else if (g_strrstr(err->message, "Authentication Rejected")) {
753 result = BLUETOOTH_ERROR_ACCESS_DENIED;
754 } else if (g_strrstr(err->message, "In Progress")) {
755 BT_INFO("Bond in progress, cancel and retry");
756 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
758 result = BLUETOOTH_ERROR_PARING_FAILED;
759 } else if (g_strrstr(err->message, "Authentication Failed")) {
760 BT_INFO("Authentication Failed");
761 if (bonding_info->is_autopair == TRUE) {
762 _bt_set_autopair_status_in_bonding_info(FALSE);
763 __ignore_auto_pairing_request(bonding_info->addr);
765 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
766 } else if (g_strrstr(err->message, "Page Timeout")) {
767 BT_INFO("Page Timeout");
768 /* This is the special case
769 As soon as call bluetooth_bond_device, try to cancel bonding.
770 In this case, before completing to call 'CreatePairedDevice' method
771 the procedure is stopped. So 'Cancle' error is not return.
773 result = BLUETOOTH_ERROR_HOST_DOWN;
774 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
775 BT_INFO("Cancel already running bonding");
776 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
777 BT_INFO("Error while Cancelling bonding");
778 /* we need to unref proxy so continue */
780 result = BLUETOOTH_ERROR_INTERNAL;
781 } else if (g_strrstr(err->message, "Connection Timeout")) {
782 /* Pairing request timeout */
783 result = BLUETOOTH_ERROR_TIMEOUT;
784 } else if (g_strrstr(err->message, "Authentication Timeout")) {
785 /* Pairing request timeout */
786 result = BLUETOOTH_ERROR_TIMEOUT;
788 BT_DBG("Default case");
789 result = BLUETOOTH_ERROR_PARING_FAILED;
793 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
794 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
795 result == BLUETOOTH_ERROR_TIMEOUT ||
796 result == BLUETOOTH_ERROR_HOST_DOWN) {
797 #ifdef TIZEN_PROFILE_WEARABLE
798 int is_sw_running = 0;
800 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
801 BT_ERR("vconf_get_int for setup wizard state failed");
804 __bt_launch_unable_to_pairing_syspopup(result);
806 BT_ERR("Unable to pair");
808 bonding_info->result = result;
811 g_object_unref(proxy);
812 bonding_info->device_proxy = NULL;
814 if (result != BLUETOOTH_ERROR_NONE) {
815 if (bonding_info->bonding_wo_discovery) {
816 GDBusProxy *adapter_proxy;
817 GVariant *ret = NULL;
818 GError *error = NULL;
821 BT_ERR("Bond was tried without discovery. Remove it");
823 adapter_proxy = _bt_get_adapter_proxy();
824 if (adapter_proxy == NULL) {
825 BT_ERR("Cannot get adapter_proxy");
829 device_path = _bt_get_device_object_path(bonding_info->addr);
830 if (device_path == NULL) {
831 BT_ERR("Cannot get device path");
835 ret = g_dbus_proxy_call_sync(adapter_proxy,
837 g_variant_new("(o)", device_path),
838 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
841 BT_ERR("RemoveDevice Fail: %s", error->message);
842 g_clear_error(&error);
846 g_variant_unref(ret);
854 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
855 if (!remote_dev_info)
858 GVariant *uuids = NULL;
859 GVariantBuilder *builder = NULL;
861 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
862 for (i = 0; i < remote_dev_info->uuid_count; i++) {
863 g_variant_builder_add(builder, "s",
864 remote_dev_info->uuids[i]);
866 uuids = g_variant_new("as", builder);
867 g_variant_builder_unref(builder);
868 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
869 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
872 param = g_variant_new("(isunsbub@asn@ay)",
875 remote_dev_info->class,
876 remote_dev_info->rssi,
877 remote_dev_info->name,
878 remote_dev_info->paired,
879 remote_dev_info->connected,
880 remote_dev_info->trust,
882 remote_dev_info->manufacturer_data_len,
886 /* Send the event to application */
887 _bt_send_event(BT_ADAPTER_EVENT,
888 BLUETOOTH_EVENT_BONDING_FINISHED,
891 _bt_free_device_info(remote_dev_info);
894 if (req_info->context == NULL)
897 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
898 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
901 if (_bt_adapter_get_status() != BT_ACTIVATED)
902 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
904 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
905 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
907 g_dbus_method_invocation_return_value(req_info->context,
908 g_variant_new("(iv)", result, out_param1));
910 g_variant_unref(out_param1);
912 _bt_delete_request_list(req_info->req_id);
917 _bt_agent_set_canceled(FALSE);
920 g_free(bonding_info->addr);
921 g_free(bonding_info);
925 int _bt_bond_device(int request_id,
926 bluetooth_device_address_t *device_address,
927 unsigned short conn_type, GArray **out_param1)
930 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
931 bluetooth_device_info_t dev_info;
933 GDBusConnection *conn;
934 char *device_path = NULL;
935 GDBusProxy *adapter_proxy;
936 GError *error = NULL;
937 gboolean bonding_wo_discovery = FALSE;
939 BT_CHECK_PARAMETER(device_address, return);
942 BT_ERR("Bonding in progress");
944 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
945 memcpy(dev_info.device_address.addr, device_address->addr,
946 BLUETOOTH_ADDRESS_LENGTH);
948 g_array_append_vals(*out_param1, &dev_info,
949 sizeof(bluetooth_device_info_t));
951 return BLUETOOTH_ERROR_DEVICE_BUSY;
954 conn = _bt_gdbus_get_system_gconn();
955 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
957 _bt_convert_addr_type_to_string(address, device_address->addr);
959 device_path = _bt_get_device_object_path(address);
961 if (device_path == NULL) {
962 BT_ERR("No searched device");
963 GVariant *ret = NULL;
964 adapter_proxy = _bt_get_adapter_proxy();
965 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
967 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
968 g_variant_new("(s)", address),
969 G_DBUS_CALL_FLAGS_NONE,
975 BT_ERR("CreateDevice Fail: %s", error->message);
976 g_clear_error(&error);
979 g_variant_unref(ret);
980 device_path = _bt_get_device_object_path(address);
981 if (device_path == NULL) {
982 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
983 memcpy(dev_info.device_address.addr, device_address->addr,
984 BLUETOOTH_ADDRESS_LENGTH);
986 g_array_append_vals(*out_param1, &dev_info,
987 sizeof(bluetooth_device_info_t));
989 return BLUETOOTH_ERROR_NOT_PAIRED;
991 BT_INFO("device_path is created[%s]", device_path);
993 bonding_wo_discovery = TRUE;
996 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
998 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1000 g_free(device_path);
1001 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1003 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1004 bonding_info->addr = g_strdup(address);
1005 bonding_info->req_id = request_id;
1007 bonding_info->device_proxy = proxy;
1008 bonding_info->conn_type = conn_type;
1009 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1011 is_device_creating = TRUE;
1013 g_dbus_proxy_call(proxy, "Pair",
1014 g_variant_new("(y)", conn_type),
1015 G_DBUS_CALL_FLAGS_NONE,
1016 BT_MAX_DBUS_TIMEOUT,
1018 (GAsyncReadyCallback)__bt_bond_device_cb,
1021 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1023 return BLUETOOTH_ERROR_NONE;
1025 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1026 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1027 bonding_info->addr);
1029 g_array_append_vals(*out_param1, &dev_info,
1030 sizeof(bluetooth_device_info_t));
1032 is_device_creating = FALSE;
1034 g_free(bonding_info->addr);
1035 g_free(bonding_info);
1036 bonding_info = NULL;
1038 return BLUETOOTH_ERROR_INTERNAL;*/
1041 int _bt_cancel_bonding(void)
1043 int ret = BLUETOOTH_ERROR_NONE;
1045 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1047 ret = _bt_agent_reply_cancellation();
1048 if (ret != BLUETOOTH_ERROR_NONE) {
1049 BT_ERR("Fail to call reply cancellation");
1053 _bt_agent_set_canceled(TRUE);
1055 return BLUETOOTH_ERROR_NONE;
1058 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1063 GVariant *out_param1;
1064 int result = BLUETOOTH_ERROR_NONE;
1065 bt_funcion_data_t *unbonding_info;
1066 bluetooth_device_info_t dev_info;
1067 request_info_t *req_info;
1069 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1071 g_variant_unref(reply);
1073 unbonding_info = user_data;
1075 if (unbonding_info == NULL) {
1077 BT_ERR("unbonding_info == NULL");
1081 req_info = _bt_get_request_info(unbonding_info->req_id);
1082 if (req_info == NULL) {
1083 BT_ERR("req_info == NULL");
1088 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1089 result = BLUETOOTH_ERROR_INTERNAL;
1092 if (req_info->context == NULL)
1095 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1096 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1097 unbonding_info->addr);
1099 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1100 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1102 g_dbus_method_invocation_return_value(req_info->context,
1103 g_variant_new("(iv)", result, out_param1));
1106 _bt_delete_request_list(req_info->req_id);
1112 if (unbonding_info) {
1113 g_free(unbonding_info->addr);
1114 g_free(unbonding_info);
1118 int _bt_unbond_device(int request_id,
1119 bluetooth_device_address_t *device_address,
1120 GArray **out_param1)
1122 char *device_path = NULL;
1123 bt_funcion_data_t *unbonding_info;
1124 GDBusProxy *adapter_proxy = NULL;
1125 GDBusProxy *device_proxy = NULL;
1126 GDBusConnection *conn;
1127 int result = BLUETOOTH_ERROR_INTERNAL;
1128 bluetooth_device_info_t dev_info;
1129 GError *error = NULL;
1130 GVariant *ret = NULL;
1132 BT_CHECK_PARAMETER(device_address, return);
1134 adapter_proxy = _bt_get_adapter_proxy();
1135 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1137 /* allocate user data so that it can be retrieved in callback */
1138 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1139 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1140 unbonding_info->req_id = request_id;
1142 _bt_convert_addr_type_to_string(unbonding_info->addr,
1143 device_address->addr);
1145 device_path = _bt_get_device_object_path(unbonding_info->addr);
1147 if (device_path == NULL) {
1148 BT_ERR("No paired device");
1149 result = BLUETOOTH_ERROR_NOT_PAIRED;
1153 conn = _bt_gdbus_get_system_gconn();
1155 BT_ERR("conn is NULL");
1156 result = BLUETOOTH_ERROR_INTERNAL;
1160 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1161 NULL, BT_BLUEZ_NAME,
1162 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1164 if (device_proxy != NULL) {
1166 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1167 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1168 G_DBUS_CALL_FLAGS_NONE,
1173 BT_ERR("Getting property failed: [%s]\n", error->message);
1174 g_error_free(error);
1175 result = BLUETOOTH_ERROR_NOT_PAIRED;
1179 BT_ERR("No paired device");
1180 g_object_unref(device_proxy);
1181 result = BLUETOOTH_ERROR_NOT_PAIRED;
1184 g_variant_unref(ret);
1186 g_object_unref(device_proxy);
1189 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1190 g_variant_new("(o)", device_path),
1191 G_DBUS_CALL_FLAGS_NONE,
1192 BT_MAX_DBUS_TIMEOUT,
1194 (GAsyncReadyCallback)__bt_unbond_cb,
1197 g_free(device_path);
1198 return BLUETOOTH_ERROR_NONE;
1201 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1202 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1203 unbonding_info->addr);
1205 g_array_append_vals(*out_param1, &dev_info,
1206 sizeof(bluetooth_device_info_t));
1207 g_free(device_path);
1208 g_free(unbonding_info->addr);
1209 g_free(unbonding_info);
1213 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1218 GVariant *out_param1;
1219 int result = BLUETOOTH_ERROR_NONE;
1220 bluetooth_device_info_t dev_info;
1221 bt_remote_dev_info_t *remote_dev_info;
1222 request_info_t *req_info;
1223 GVariant *uuid_list, *manufacture_data;
1225 GVariantBuilder *builder = NULL;
1228 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1230 g_variant_unref(reply);
1232 g_object_unref(proxy);
1234 if (searching_info == NULL) {
1236 BT_ERR("unbonding_info == NULL");
1240 req_info = _bt_get_request_info(searching_info->req_id);
1241 if (req_info == NULL) {
1242 BT_ERR("req_info == NULL");
1247 g_dbus_error_strip_remote_error(err);
1248 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1250 if (g_strrstr("Operation canceled", err->message))
1251 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1252 else if (g_strrstr("In Progress", err->message))
1253 result = BLUETOOTH_ERROR_IN_PROGRESS;
1254 else if (g_strrstr("Host is down", err->message))
1255 result = BLUETOOTH_ERROR_HOST_DOWN;
1257 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1259 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1260 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1261 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1262 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1263 remote_dev_info->uuid_count > 0) {
1264 result = BLUETOOTH_ERROR_NONE;
1267 _bt_free_device_info(remote_dev_info);
1272 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1273 if (!remote_dev_info)
1277 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1278 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1279 g_variant_builder_add(builder, "s",
1280 remote_dev_info->uuids[i]);
1282 uuid_list = g_variant_new("as", builder);
1283 g_variant_builder_unref(builder);
1284 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1285 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1288 param = g_variant_new("(isunsbub@asn@ay)",
1290 searching_info->addr,
1291 remote_dev_info->class,
1292 remote_dev_info->rssi,
1293 remote_dev_info->name,
1294 remote_dev_info->paired,
1295 remote_dev_info->connected,
1296 remote_dev_info->trust,
1298 remote_dev_info->manufacturer_data_len,
1301 /* Send the event to application */
1302 _bt_send_event(BT_ADAPTER_EVENT,
1303 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1306 _bt_free_device_info(remote_dev_info);
1309 if (req_info->context == NULL)
1312 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1313 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1314 searching_info->addr);
1316 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1317 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1319 g_dbus_method_invocation_return_value(req_info->context,
1320 g_variant_new("(iv)", result, out_param1));
1323 _bt_delete_request_list(req_info->req_id);
1328 if (searching_info) {
1329 g_free(searching_info->addr);
1330 g_free(searching_info);
1331 searching_info = NULL;
1335 int _bt_search_device(int request_id,
1336 bluetooth_device_address_t *device_address)
1338 char *device_path = NULL;
1339 GDBusProxy *device_proxy = NULL;
1340 GDBusConnection *conn;
1342 GDBusProxy *adapter_proxy;
1343 int result = BLUETOOTH_ERROR_INTERNAL;
1345 BT_CHECK_PARAMETER(device_address, return);
1348 BT_ERR("Bonding in progress");
1349 return BLUETOOTH_ERROR_DEVICE_BUSY;
1352 if (searching_info) {
1353 BT_ERR("Service searching in progress");
1354 return BLUETOOTH_ERROR_DEVICE_BUSY;
1357 adapter_proxy = _bt_get_adapter_proxy();
1358 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1360 /* allocate user data so that it can be retrieved in callback */
1361 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1362 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1363 searching_info->req_id = request_id;
1365 _bt_convert_addr_type_to_string(searching_info->addr,
1366 device_address->addr);
1368 conn = _bt_gdbus_get_system_gconn();
1369 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1372 device_path = _bt_get_device_object_path(searching_info->addr);
1374 if (device_path == NULL) {
1375 BT_ERR("No paired device");
1376 result = BLUETOOTH_ERROR_NOT_PAIRED;
1380 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1381 NULL, BT_BLUEZ_NAME,
1382 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1383 g_free(device_path);
1384 if (device_proxy == NULL) {
1385 result = BLUETOOTH_ERROR_INTERNAL;
1389 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1390 g_variant_new("(s)", ""),
1391 G_DBUS_CALL_FLAGS_NONE,
1392 BT_MAX_DBUS_TIMEOUT,
1394 (GAsyncReadyCallback)__bt_discover_cb,
1397 searching_info->device_proxy = device_proxy;
1399 return BLUETOOTH_ERROR_NONE;
1402 g_free(searching_info->addr);
1403 g_free(searching_info);
1404 searching_info = NULL;
1408 int _bt_cancel_search_device(void)
1410 GVariant *ret = NULL;
1413 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1415 if (searching_info->device_proxy) {
1416 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1418 G_DBUS_CALL_FLAGS_NONE,
1423 g_variant_unref(ret);
1425 __bt_cancel_search_service_done();
1427 return BLUETOOTH_ERROR_NONE;
1430 int _bt_set_alias(bluetooth_device_address_t *device_address,
1433 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1434 gchar *device_path = NULL;
1435 GDBusProxy *adapter_proxy;
1436 GDBusProxy *device_proxy;
1437 GVariant *ret = NULL;
1438 GError *error = NULL;
1439 GDBusConnection *conn;
1441 BT_CHECK_PARAMETER(device_address, return);
1442 BT_CHECK_PARAMETER(alias, return);
1444 adapter_proxy = _bt_get_adapter_proxy();
1445 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1447 conn = _bt_gdbus_get_system_gconn();
1448 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1450 _bt_convert_addr_type_to_string(address, device_address->addr);
1452 device_path = _bt_get_device_object_path(address);
1454 if (device_path == NULL) {
1455 BT_ERR("No paired device");
1456 return BLUETOOTH_ERROR_NOT_PAIRED;
1459 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1460 NULL, BT_BLUEZ_NAME,
1461 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1463 g_free(device_path);
1464 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1466 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1467 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1468 G_DBUS_CALL_FLAGS_NONE,
1473 g_variant_unref(ret);
1475 g_object_unref(device_proxy);
1478 BT_ERR("SetProperty error: [%s]", error->message);
1479 g_error_free(error);
1480 return BLUETOOTH_ERROR_INTERNAL;
1483 return BLUETOOTH_ERROR_NONE;
1486 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1489 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1490 gchar *device_path = NULL;
1491 GDBusProxy *device_proxy;
1492 gboolean previous_value;
1493 GError *error = NULL;
1494 GDBusConnection *conn;
1495 GVariant *result = NULL;
1496 GVariant *temp = NULL;
1497 int ret = BLUETOOTH_ERROR_NONE;
1499 BT_CHECK_PARAMETER(device_address, return);
1501 conn = _bt_gdbus_get_system_gconn();
1502 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1504 _bt_convert_addr_type_to_string(address, device_address->addr);
1506 device_path = _bt_get_device_object_path(address);
1508 if (device_path == NULL) {
1509 BT_ERR("No paired device");
1510 return BLUETOOTH_ERROR_NOT_PAIRED;
1513 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1514 NULL, BT_BLUEZ_NAME,
1515 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1517 g_free(device_path);
1518 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1520 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1521 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1522 G_DBUS_CALL_FLAGS_NONE,
1526 if (error != NULL) {
1527 BT_ERR("Getting property failed: [%s]\n", error->message);
1528 g_error_free(error);
1529 g_object_unref(device_proxy);
1530 return BLUETOOTH_ERROR_INTERNAL;
1533 g_variant_get(result, "(v)", &temp);
1534 previous_value = g_variant_get_boolean(temp);
1535 g_variant_unref(temp);
1536 g_variant_unref(result);
1537 /* If the input is same with previous value, return error. */
1538 if (previous_value == authorize) {
1539 BT_ERR("Same value: %d", previous_value);
1540 g_object_unref(device_proxy);
1541 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1545 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1546 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1547 G_DBUS_CALL_FLAGS_NONE,
1552 g_object_unref(device_proxy);
1554 BT_ERR("SetProperty error: [%s]", error->message);
1555 g_error_free(error);
1556 ret = BLUETOOTH_ERROR_INTERNAL;
1560 g_variant_unref(result);
1565 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1566 gboolean *is_connected)
1568 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1569 char *object_path = NULL;
1571 GDBusProxy *device_proxy;
1572 GError *error = NULL;
1574 GVariant *tmp_value;
1575 GDBusConnection *conn;
1576 GVariant *result = NULL;
1577 int ret = BLUETOOTH_ERROR_NONE;
1579 BT_CHECK_PARAMETER(device_address, return);
1581 conn = _bt_gdbus_get_system_gconn();
1582 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1584 _bt_convert_addr_type_to_string(address, device_address->addr);
1586 object_path = _bt_get_device_object_path(address);
1587 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1589 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1590 NULL, BT_BLUEZ_NAME, object_path,
1591 BT_PROPERTIES_INTERFACE, NULL, NULL);
1592 g_free(object_path);
1593 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1595 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1596 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1597 G_DBUS_CALL_FLAGS_NONE,
1601 if (result == NULL) {
1602 if (error != NULL) {
1603 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1604 g_error_free(error);
1606 g_object_unref(device_proxy);
1607 return BLUETOOTH_ERROR_INTERNAL;
1610 g_variant_get(result , "(@a{sv})", &value);
1611 g_variant_unref(result);
1613 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1614 if (tmp_value == NULL) {
1615 g_object_unref(device_proxy);
1616 g_variant_unref(value);
1617 return BLUETOOTH_ERROR_INTERNAL;
1620 *is_connected = g_variant_get_boolean(tmp_value);
1622 BT_DBG("gatt is connected : %d", *is_connected);
1624 g_variant_unref(tmp_value);
1625 g_variant_unref(value);
1626 g_object_unref(device_proxy);
1631 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1632 int connection_type, gboolean *is_connected)
1634 char *object_path = NULL;
1635 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1636 GDBusProxy *device_proxy = NULL;
1637 GDBusProxy *adapter_proxy = NULL;
1638 GDBusConnection *conn;
1639 GError *error = NULL;
1640 GVariant *tmp_value = NULL;
1641 GVariant *value = NULL;
1642 GVariant *result = NULL;
1645 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1646 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1648 *is_connected = FALSE;
1649 BT_DBG("connection_type: %d", connection_type);
1650 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1651 return _bt_rfcomm_is_device_connected(device_address,
1653 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1654 return _bt_is_gatt_connected(device_address, is_connected);
1655 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1656 return _bt_pbap_is_connected(device_address, is_connected);
1658 adapter_proxy = _bt_get_adapter_proxy();
1659 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1661 conn = _bt_gdbus_get_system_gconn();
1662 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1664 _bt_convert_addr_type_to_string(address, device_address->addr);
1666 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1667 object_path = _bt_get_adapter_path();
1668 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1669 NULL, BT_BLUEZ_NAME,
1670 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1671 g_free(object_path);
1672 if (device_proxy == NULL) {
1673 BT_DBG("Device don't have this service");
1674 return BLUETOOTH_ERROR_INTERNAL;
1677 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1678 g_variant_new("(s)", address),
1679 G_DBUS_CALL_FLAGS_NONE,
1684 if (result == NULL) {
1685 BT_ERR("[GetProperties] Error occured in Proxy call");
1687 BT_ERR("%s", error->message);
1688 g_error_free(error);
1690 *is_connected = FALSE;
1691 g_object_unref(device_proxy);
1692 return BLUETOOTH_ERROR_NONE;
1694 g_variant_get(result , "(@a{sv})", &value);
1695 g_variant_unref(result);
1698 tmp_value = g_variant_lookup_value(value,
1700 G_VARIANT_TYPE_BOOLEAN);
1702 *is_connected = g_variant_get_boolean(tmp_value);
1703 g_variant_unref(tmp_value);
1705 g_variant_unref(value);
1707 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1708 return _bt_is_network_connected(_bt_get_net_conn(),
1709 device_address->addr, is_connected);
1711 uuid = _bt_get_profile_uuid128(connection_type);
1713 BT_ERR("uuid is NULL");
1714 return BLUETOOTH_ERROR_INTERNAL;
1717 BT_DBG("uuid: %s", uuid);
1719 object_path = _bt_get_device_object_path(address);
1720 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1721 BT_DBG("object_path: %s", object_path);
1722 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1723 NULL, BT_BLUEZ_NAME,
1724 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1725 g_free(object_path);
1726 if (device_proxy == NULL) {
1727 BT_DBG("Device don't have this service");
1729 return BLUETOOTH_ERROR_INTERNAL;
1732 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1733 g_variant_new("(s)", uuid),
1734 G_DBUS_CALL_FLAGS_NONE,
1739 if (result == NULL) {
1740 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1742 BT_ERR("%s", error->message);
1743 if (g_strrstr(error->message, "Not Connected"))
1744 BT_DBG("Not connected");
1745 g_error_free(error);
1747 *is_connected = FALSE;
1748 g_object_unref(device_proxy);
1750 return BLUETOOTH_ERROR_NONE;
1752 g_variant_get(result, "(b)", is_connected);
1754 g_variant_unref(result);
1757 g_object_unref(device_proxy);
1758 return BLUETOOTH_ERROR_NONE;
1761 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1762 bluetooth_connected_link_t *connected)
1764 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1765 char *object_path = NULL;
1767 GDBusProxy *device_proxy;
1768 GError *error = NULL;
1769 GDBusConnection *conn;
1770 GVariant *tmp_value = NULL;
1771 GVariant *value = NULL;
1772 GVariant *result = NULL;
1774 BT_CHECK_PARAMETER(device_address, return);
1776 conn = _bt_gdbus_get_system_gconn();
1777 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1779 _bt_convert_addr_type_to_string(address, device_address->addr);
1781 object_path = _bt_get_device_object_path(address);
1782 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1784 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1785 NULL, BT_BLUEZ_NAME,
1786 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1787 g_free(object_path);
1788 if (device_proxy == NULL) {
1789 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1790 return BLUETOOTH_ERROR_NONE;
1793 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1794 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1795 G_DBUS_CALL_FLAGS_NONE,
1800 if (error != NULL) {
1801 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1802 g_error_free(error);
1803 g_object_unref(device_proxy);
1804 return BLUETOOTH_ERROR_INTERNAL;
1807 g_variant_get(result , "(@a{sv})", &value);
1808 g_variant_unref(result);
1810 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1811 if (tmp_value != NULL) {
1812 *connected = g_variant_get_byte(tmp_value);
1813 g_variant_unref(tmp_value);
1814 g_object_unref(device_proxy);
1815 return BLUETOOTH_ERROR_NONE;
1817 BT_ERR("g_variant value is NULL");
1818 return BLUETOOTH_ERROR_INTERNAL;
1822 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1826 GVariant *out_param1;
1828 int result = BLUETOOTH_ERROR_NONE;
1829 bt_function_data_t *func_data = user_data;
1830 request_info_t *req_info = NULL;
1831 bluetooth_device_address_t device_addr = { {0} };
1833 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1834 g_object_unref(proxy);
1836 if (reply == NULL) {
1837 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1839 BT_ERR("Error: %s", err->message);
1840 g_clear_error(&err);
1842 result = BLUETOOTH_ERROR_INTERNAL;
1844 g_variant_unref(reply);
1847 if (func_data == NULL) {
1848 BT_ERR("func_data is NULL");
1852 req_info = _bt_get_request_info(func_data->req_id);
1853 if (req_info == NULL) {
1854 BT_ERR("req_info is NULL");
1858 if (req_info->context == NULL) {
1859 BT_ERR("req_info->context is NULL");
1863 _bt_convert_addr_string_to_type(device_addr.addr,
1864 (const char *)func_data->address);
1866 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1867 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1870 g_dbus_method_invocation_return_value(req_info->context,
1871 g_variant_new("(iv)", result, out_param1));
1875 _bt_delete_request_list(req_info->req_id);
1878 g_free(func_data->address);
1883 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1884 gboolean auto_connect)
1886 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1887 gchar *device_path = NULL;
1888 GDBusProxy *device_proxy = NULL;
1889 GDBusConnection *conn;
1890 int ret = BLUETOOTH_ERROR_NONE;
1891 bt_function_data_t *func_data;
1893 BT_CHECK_PARAMETER(bd_addr, return);
1895 conn = _bt_gdbus_get_system_gconn();
1896 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1898 _bt_convert_addr_type_to_string(device_address,
1899 (unsigned char *)bd_addr->addr);
1900 device_path = _bt_get_device_object_path(device_address);
1901 if (device_path == NULL) {
1902 BT_ERR_C("device_path NULL : [%s]", device_address);
1903 ret = BLUETOOTH_ERROR_INTERNAL;
1907 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1908 NULL, BT_BLUEZ_NAME,
1909 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1910 g_free(device_path);
1911 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1913 func_data = g_malloc0(sizeof(bt_function_data_t));
1914 func_data->address = g_strdup(device_address);
1915 if (func_data->address == NULL) {
1916 BT_ERR("Unable to allocate memory for address");
1917 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1921 func_data->req_id = req_id;
1923 g_dbus_proxy_call(device_proxy, "ConnectLE",
1924 g_variant_new("(b)", auto_connect),
1925 G_DBUS_CALL_FLAGS_NONE,
1926 BT_MAX_DBUS_TIMEOUT,
1928 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1934 g_object_unref(device_proxy);
1936 g_free(func_data->address);
1942 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1944 BT_INFO("Try to initiate pending LE connection");
1946 pending_le_conn_timer_id = 0;
1948 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1949 &pending_le_conn_info->bd_addr,
1950 pending_le_conn_info->auto_connect);
1952 g_free(pending_le_conn_info);
1953 pending_le_conn_info = NULL;
1958 void _bt_pending_connect_le_device(void)
1960 if (pending_le_conn_timer_id > 0) {
1961 g_source_remove(pending_le_conn_timer_id);
1962 __bt_connect_le_timer_cb(NULL);
1966 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1967 gboolean auto_connect)
1969 int ret = BLUETOOTH_ERROR_NONE;
1971 BT_CHECK_PARAMETER(bd_addr, return);
1973 ret = _bt_hold_current_advertising();
1974 if (ret == BLUETOOTH_ERROR_NONE) {
1975 BT_INFO("Current advertising is held");
1976 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1977 pending_le_conn_info->req_id = req_id;
1978 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1979 BLUETOOTH_ADDRESS_LENGTH);
1980 pending_le_conn_info->auto_connect = auto_connect;
1982 pending_le_conn_timer_id =
1983 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1985 return BLUETOOTH_ERROR_NONE;
1988 BT_ERR("Unable to hold advertising");
1990 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1993 int _bt_disconnect_le_device(int req_id,
1994 const bluetooth_device_address_t *bd_addr)
1996 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1998 GDBusProxy *device_proxy;
1999 GDBusConnection *conn;
2000 int ret = BLUETOOTH_ERROR_NONE;
2001 bt_function_data_t *func_data;
2003 BT_CHECK_PARAMETER(bd_addr, return);
2005 conn = _bt_gdbus_get_system_gconn();
2006 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2008 _bt_convert_addr_type_to_string(device_address,
2009 (unsigned char *)bd_addr->addr);
2010 device_path = _bt_get_device_object_path(device_address);
2011 if (device_path == NULL) {
2012 BT_DBG("device_path NULL");
2013 ret = BLUETOOTH_ERROR_INTERNAL;
2017 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2019 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2020 NULL, BT_BLUEZ_NAME,
2021 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2022 g_free(device_path);
2023 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2025 func_data = g_malloc0(sizeof(bt_function_data_t));
2026 func_data->address = g_strdup(device_address);
2027 if (func_data->address == NULL) {
2028 BT_ERR("Unable to allocate memory for address");
2029 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2033 func_data->req_id = req_id;
2035 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2037 G_DBUS_CALL_FLAGS_NONE,
2038 BT_MAX_DBUS_TIMEOUT,
2040 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2045 g_object_unref(device_proxy);
2047 g_free(func_data->address);
2053 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2055 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2056 gchar *device_path = NULL;
2057 GError *error = NULL;
2058 GDBusProxy *device_proxy = NULL;
2059 GDBusProxy *adapter_proxy;
2060 GDBusConnection *conn;
2061 int ret = BLUETOOTH_ERROR_NONE;
2063 BT_CHECK_PARAMETER(bd_addr, return);
2065 _bt_convert_addr_type_to_string(device_address,
2066 (unsigned char *)bd_addr->addr);
2068 conn = _bt_gdbus_get_system_gconn();
2069 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2071 adapter_proxy = _bt_get_adapter_proxy();
2072 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2074 device_path = _bt_get_device_object_path(device_address);
2075 if (device_path == NULL) {
2076 BT_DBG("device_path NULL");
2077 ret = BLUETOOTH_ERROR_INTERNAL;
2081 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2083 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2084 NULL, BT_BLUEZ_NAME,
2085 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2086 g_free(device_path);
2087 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2089 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2091 G_DBUS_CALL_FLAGS_NONE,
2096 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2097 g_error_free(error);
2098 g_object_unref(device_proxy);
2099 return BLUETOOTH_ERROR_INTERNAL;
2102 g_object_unref(device_proxy);
2107 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2109 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2110 gchar *device_path = NULL;
2111 GError *error = NULL;
2112 GDBusProxy *device_proxy = NULL;
2113 GDBusProxy *adapter_proxy;
2114 GDBusConnection *conn;
2115 int ret = BLUETOOTH_ERROR_NONE;
2117 BT_CHECK_PARAMETER(bd_addr, return);
2119 _bt_convert_addr_type_to_string(device_address,
2120 (unsigned char *)bd_addr->addr);
2122 conn = _bt_gdbus_get_system_gconn();
2123 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2125 adapter_proxy = _bt_get_adapter_proxy();
2126 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2128 device_path = _bt_get_device_object_path(device_address);
2129 if (device_path == NULL) {
2130 BT_DBG("device_path NULL");
2131 ret = BLUETOOTH_ERROR_INTERNAL;
2135 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2137 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2138 NULL, BT_BLUEZ_NAME,
2139 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2140 g_free(device_path);
2141 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2143 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2145 G_DBUS_CALL_FLAGS_NONE,
2150 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2151 g_error_free(error);
2152 g_object_unref(device_proxy);
2153 return BLUETOOTH_ERROR_INTERNAL;
2156 g_object_unref(device_proxy);
2161 int _bt_connect_profile(char *address, char *uuid,
2162 void *cb, gpointer func_data)
2166 GDBusConnection *conn;
2167 GDBusProxy *adapter_proxy;
2168 GVariant *result = NULL;
2169 GError *error = NULL;
2171 conn = _bt_gdbus_get_system_gconn();
2172 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2174 object_path = _bt_get_device_object_path(address);
2175 if (object_path == NULL) {
2176 BT_ERR("No searched device");
2178 adapter_proxy = _bt_get_adapter_proxy();
2179 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2181 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2182 g_variant_new("(s)", address),
2183 G_DBUS_CALL_FLAGS_NONE,
2188 if (error != NULL) {
2189 BT_ERR("CreateDevice Fail: %s", error->message);
2190 g_error_free(error);
2193 g_variant_unref(result);
2195 object_path = _bt_get_device_object_path(address);
2197 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2199 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2200 NULL, BT_BLUEZ_NAME,
2201 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2202 g_free(object_path);
2203 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2205 g_dbus_proxy_call(proxy, "ConnectProfile",
2206 g_variant_new("(s)", uuid),
2207 G_DBUS_CALL_FLAGS_NONE,
2208 BT_MAX_DBUS_TIMEOUT,
2210 (GAsyncReadyCallback)cb,
2213 return BLUETOOTH_ERROR_NONE;
2216 int _bt_disconnect_all(char *address)
2218 int ret = BLUETOOTH_ERROR_NONE;
2221 GDBusConnection *conn;
2222 GVariant *result = NULL;
2226 conn = _bt_gdbus_get_system_gconn();
2227 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2229 object_path = _bt_get_device_object_path(address);
2230 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2232 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2233 NULL, BT_BLUEZ_NAME,
2234 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2235 g_free(object_path);
2236 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2238 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2240 G_DBUS_CALL_FLAGS_NONE,
2245 BT_ERR("Dbus Call Error:[%s]", err->message);
2247 ret = BLUETOOTH_ERROR_INTERNAL;
2250 g_object_unref(proxy);
2252 g_variant_unref(result);
2257 int _bt_disconnect_profile(char *address, char *uuid,
2258 void *cb, gpointer func_data)
2262 GDBusConnection *conn;
2264 conn = _bt_gdbus_get_system_gconn();
2265 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2267 object_path = _bt_get_device_object_path(address);
2268 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2270 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2271 NULL, BT_BLUEZ_NAME,
2272 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2273 g_free(object_path);
2274 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2276 g_dbus_proxy_call(proxy, "DisconnectProfile",
2277 g_variant_new("(s)", uuid),
2278 G_DBUS_CALL_FLAGS_NONE,
2279 BT_MAX_DBUS_TIMEOUT,
2281 (GAsyncReadyCallback)cb,
2284 return BLUETOOTH_ERROR_NONE;
2287 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2288 int low_threshold, int in_range_threshold, int high_threshold)
2290 int ret = BLUETOOTH_ERROR_NONE;
2292 GVariant *result = NULL;
2293 GError *error = NULL;
2294 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2296 BT_CHECK_PARAMETER(bd_addr, return);
2297 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2298 bd_addr->addr[0], bd_addr->addr[1],
2299 bd_addr->addr[2], bd_addr->addr[3],
2300 bd_addr->addr[4], bd_addr->addr[5],
2302 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2303 in_range_threshold, high_threshold);
2305 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2307 proxy = _bt_get_adapter_proxy();
2308 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2310 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2311 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2312 G_DBUS_CALL_FLAGS_NONE,
2316 if (error != NULL) {
2317 BT_ERR("Dbus Call Error:[%s]", error->message);
2318 g_error_free(error);
2319 ret = BLUETOOTH_ERROR_INTERNAL;
2323 g_variant_unref(result);
2328 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2331 int ret = BLUETOOTH_ERROR_NONE;
2333 GVariant *result = NULL;
2334 GError *error = NULL;
2335 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2337 BT_CHECK_PARAMETER(bd_addr, return);
2338 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2339 bd_addr->addr[0], bd_addr->addr[1],
2340 bd_addr->addr[2], bd_addr->addr[3],
2341 bd_addr->addr[4], bd_addr->addr[5],
2344 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2346 proxy = _bt_get_adapter_proxy();
2347 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2349 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2350 g_variant_new("(si)", address, link_type),
2351 G_DBUS_CALL_FLAGS_NONE,
2356 if (error != NULL) {
2357 BT_ERR("Dbus Call Error:[%s]", error->message);
2358 g_error_free(error);
2359 ret = BLUETOOTH_ERROR_INTERNAL;
2363 g_variant_unref(result);
2368 int _bt_le_conn_update(unsigned char *device_address,
2369 float interval_min, float interval_max,
2370 guint16 latency, guint16 time_out)
2372 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2373 gchar *device_path = NULL;
2374 GError *error = NULL;
2375 GDBusProxy *device_proxy = NULL;
2376 GDBusConnection *conn;
2378 guint32 min, max, to;
2379 guint32 min_supervision_to;
2380 int ret = BLUETOOTH_ERROR_NONE;
2382 BT_CHECK_PARAMETER(device_address, return);
2384 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2385 interval_min, interval_max, latency, time_out);
2387 if (interval_min > interval_max ||
2388 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2389 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2390 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2394 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2395 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2396 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2400 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2401 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2406 * The Supervision_Timeout in milliseconds shall be larger than
2407 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2408 * where Conn_Interval_Max is given in milliseconds.
2410 min_supervision_to = (1 + latency) * interval_max * 2;
2411 if (time_out <= min_supervision_to) {
2412 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2416 _bt_convert_addr_type_to_string(address, device_address);
2418 BT_DBG("Remote device address: %s", address);
2420 device_path = _bt_get_device_object_path(address);
2422 if (device_path == NULL) {
2423 BT_ERR("device_path NULL");
2424 ret = BLUETOOTH_ERROR_INTERNAL;
2428 conn = _bt_gdbus_get_system_gconn();
2430 BT_ERR("conn NULL");
2431 ret = BLUETOOTH_ERROR_INTERNAL;
2435 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2436 NULL, BT_BLUEZ_NAME,
2437 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2439 g_free(device_path);
2440 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2442 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2443 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2444 to = time_out / BT_LE_CONN_TO_SPLIT;
2446 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2447 g_variant_new("(uuuu)", min, max, latency, to),
2448 G_DBUS_CALL_FLAGS_NONE,
2453 g_object_unref(device_proxy);
2454 if (reply == NULL) {
2456 BT_ERR("Error %s[%s]", error->message, address);
2457 if (g_strrstr(error->message, "In Progress"))
2458 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2460 ret = BLUETOOTH_ERROR_INTERNAL;
2461 g_error_free(error);
2465 g_variant_unref(reply);
2471 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2472 bluetooth_device_pin_code_t *pin_code)
2474 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2476 bt_pin_code_info_t *pin_info = NULL;
2478 BT_CHECK_PARAMETER(device_address, return);
2479 BT_CHECK_PARAMETER(pin_code, return);
2480 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2481 BLUETOOTH_ERROR_NO_RESOURCES);
2483 _bt_convert_addr_type_to_string(address, device_address->addr);
2485 for (l = pin_info_list; l != NULL; l = l->next) {
2488 if (g_strcmp0(pin_info->address, address) == 0) {
2489 g_free(pin_info->pin_code);
2490 pin_info->pin_code = g_strdup(pin_code->pin_code);
2491 return BLUETOOTH_ERROR_NONE;
2495 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2496 pin_info->address = g_strdup(address);
2497 pin_info->pin_code = g_strdup(pin_code->pin_code);
2498 pin_info_list = g_slist_append(pin_info_list, pin_info);
2500 return BLUETOOTH_ERROR_NONE;
2503 gint __bt_compare_address(gpointer *a, gpointer *b)
2505 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2506 char *address = (char *)b;
2507 return g_strcmp0(pin_info->address, address);
2510 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2512 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2514 bt_pin_code_info_t *pin_info = NULL;
2516 BT_CHECK_PARAMETER(device_address, return);
2518 _bt_convert_addr_type_to_string(address, device_address->addr);
2520 l = g_slist_find_custom(pin_info_list, address,
2521 (GCompareFunc)__bt_compare_address);
2525 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2526 g_free(pin_info->address);
2527 g_free(pin_info->pin_code);
2531 return BLUETOOTH_ERROR_NONE;
2534 int _bt_get_device_pin_code(const char *address, char *pin_code)
2538 BT_CHECK_PARAMETER(address, return);
2539 BT_CHECK_PARAMETER(pin_code, return);
2541 for (l = pin_info_list; l != NULL; l = l->next) {
2542 bt_pin_code_info_t *pin_info = l->data;
2544 if (g_strcmp0(pin_info->address, address) == 0) {
2545 g_strlcpy(pin_code, pin_info->pin_code,
2546 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2548 return BLUETOOTH_ERROR_NONE;
2552 return BLUETOOTH_ERROR_NOT_FOUND;
2555 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2556 bluetooth_le_connection_param_t *param)
2559 return BLUETOOTH_ERROR_INVALID_PARAM;
2561 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2562 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2563 return BLUETOOTH_ERROR_INVALID_PARAM;
2565 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2568 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2569 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2570 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2571 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2572 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2575 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2576 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2577 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2578 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2579 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2582 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2583 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2584 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2585 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2586 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2590 BT_ERR("Unhandled mode : %d", mode);
2594 return BLUETOOTH_ERROR_NONE;
2597 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2598 guint trusted_profile_flag, guint *trusted)
2604 case TRUSTED_PROFILE_PBAP:
2605 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2606 trust_profile = trusted_profile_flag & (1 << 1);
2608 return BLUETOOTH_ERROR_NOT_SUPPORT;
2610 case TRUSTED_PROFILE_MAP:
2611 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2612 trust_profile = trusted_profile_flag & (1 << 3);
2614 return BLUETOOTH_ERROR_NOT_SUPPORT;
2616 case TRUSTED_PROFILE_SAP:
2617 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2618 trust_profile = trusted_profile_flag & (1 << 5);
2620 return BLUETOOTH_ERROR_NOT_SUPPORT;
2622 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2623 *trusted = trusted_profile_flag;
2624 return BLUETOOTH_ERROR_NONE;
2626 return BLUETOOTH_ERROR_NOT_SUPPORT;
2632 return BLUETOOTH_ERROR_NONE;
2635 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2636 guint restricted_profile_flag, guint *restricted)
2638 int restrict_profile;
2639 *restricted = FALSE;
2642 case RESTRICTED_PROFILE_HFP_HS:
2643 restrict_profile = restricted_profile_flag & (1 << 0);
2645 case RESTRICTED_PROFILE_A2DP:
2646 restrict_profile = restricted_profile_flag & (1 << 2);
2649 return BLUETOOTH_ERROR_NOT_SUPPORT;
2652 if (restrict_profile)
2655 return BLUETOOTH_ERROR_NONE;
2658 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2661 case TRUSTED_PROFILE_PBAP:
2662 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2663 case TRUSTED_PROFILE_MAP:
2664 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2665 case TRUSTED_PROFILE_SAP:
2666 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2667 case TRUSTED_PROFILE_ALL:
2674 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2677 case RESTRICTED_PROFILE_HFP_HS:
2678 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2679 case RESTRICTED_PROFILE_A2DP:
2680 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2686 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2688 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2689 return TRUSTED_PROFILE_PBAP;
2690 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2691 return TRUSTED_PROFILE_MAP;
2692 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2693 return TRUSTED_PROFILE_SAP;
2695 return 0; /* 0 - Unknown Profile */
2698 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2699 bluetooth_trusted_profile_t profile, gboolean trust)
2701 int ret = BLUETOOTH_ERROR_NONE;
2702 GDBusConnection *conn;
2704 GError *error = NULL;
2705 char *device_path = NULL;
2707 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2710 BT_CHECK_PARAMETER(bd_addr, return);
2711 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2712 bd_addr->addr[0], bd_addr->addr[1],
2713 bd_addr->addr[2], bd_addr->addr[3],
2714 bd_addr->addr[4], bd_addr->addr[5],
2717 conn = _bt_gdbus_get_system_gconn();
2718 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2720 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2722 device_path = _bt_get_device_object_path(address);
2723 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2725 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2726 NULL, BT_BLUEZ_NAME, device_path,
2727 BT_DEVICE_INTERFACE, NULL, NULL);
2729 g_free(device_path);
2730 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2732 uuid = _bt_get_trusted_profile_uuid(profile);
2734 g_object_unref(proxy);
2735 return BLUETOOTH_ERROR_NOT_SUPPORT;
2738 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2739 g_variant_new("(sb)", uuid, trust),
2740 G_DBUS_CALL_FLAGS_NONE, -1,
2742 g_object_unref(proxy);
2744 if (reply == NULL) {
2745 BT_ERR("Failed to Set Profile Trusted");
2746 ret = BLUETOOTH_ERROR_INTERNAL;
2748 BT_ERR("Error %s[%s]", error->message, address);
2749 g_error_free(error);
2753 g_variant_unref(reply);
2760 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2761 bluetooth_trusted_profile_t profile, guint *trust)
2763 int ret = BLUETOOTH_ERROR_NONE;
2764 GDBusConnection *conn;
2766 GError *error = NULL;
2767 char *device_path = NULL;
2768 guint trusted_profile_flag;
2769 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2772 BT_CHECK_PARAMETER(bd_addr, return);
2773 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2774 bd_addr->addr[0], bd_addr->addr[1],
2775 bd_addr->addr[2], bd_addr->addr[3],
2776 bd_addr->addr[4], bd_addr->addr[5],
2779 conn = _bt_gdbus_get_system_gconn();
2780 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2782 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2784 device_path = _bt_get_device_object_path(address);
2785 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2787 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2788 NULL, BT_BLUEZ_NAME, device_path,
2789 BT_PROPERTIES_INTERFACE, NULL, NULL);
2791 g_free(device_path);
2792 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2794 reply = g_dbus_proxy_call_sync(proxy, "Get",
2795 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2796 G_DBUS_CALL_FLAGS_NONE, -1,
2798 g_object_unref(proxy);
2800 if (reply == NULL) {
2801 BT_ERR("Failed to Get Profile Trusted");
2802 ret = BLUETOOTH_ERROR_INTERNAL;
2804 BT_ERR("Error %s[%s]", error->message, address);
2805 g_error_free(error);
2810 g_variant_get(reply, "(v)", &temp);
2811 trusted_profile_flag = g_variant_get_uint32(temp);
2812 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2814 ret = _bt_get_trusted_profile_from_flag(profile,
2815 trusted_profile_flag, trust);
2816 g_variant_unref(temp);
2817 g_variant_unref(reply);
2820 BT_DBG("TRUST %d", *trust);
2824 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2825 bluetooth_restricted_profile_t profile, gboolean restricted)
2827 int ret = BLUETOOTH_ERROR_NONE;
2828 GDBusConnection *conn;
2830 GError *error = NULL;
2831 char *device_path = NULL;
2833 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2836 BT_CHECK_PARAMETER(bd_addr, return);
2837 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2838 bd_addr->addr[0], bd_addr->addr[1],
2839 bd_addr->addr[2], bd_addr->addr[3],
2840 bd_addr->addr[4], bd_addr->addr[5],
2841 profile, restricted);
2843 conn = _bt_gdbus_get_system_gconn();
2844 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2846 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2848 device_path = _bt_get_device_object_path(address);
2849 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2851 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2852 NULL, BT_BLUEZ_NAME, device_path,
2853 BT_DEVICE_INTERFACE, NULL, NULL);
2855 g_free(device_path);
2856 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2858 uuid = _bt_get_restricted_profile_uuid(profile);
2860 g_object_unref(proxy);
2861 return BLUETOOTH_ERROR_NOT_SUPPORT;
2864 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2865 g_variant_new("(sb)", uuid, restricted),
2866 G_DBUS_CALL_FLAGS_NONE, -1,
2868 g_object_unref(proxy);
2870 if (reply == NULL) {
2871 BT_ERR("Failed to Set Profile Restricted");
2872 ret = BLUETOOTH_ERROR_INTERNAL;
2874 BT_ERR("Error %s[%s]", error->message, address);
2875 g_error_free(error);
2879 g_variant_unref(reply);
2886 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2887 bluetooth_restricted_profile_t profile, guint *restricted)
2889 int ret = BLUETOOTH_ERROR_NONE;
2890 GDBusConnection *conn;
2892 GError *error = NULL;
2893 char *device_path = NULL;
2894 guint restricted_profile_flag;
2895 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2898 BT_CHECK_PARAMETER(bd_addr, return);
2899 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2900 bd_addr->addr[0], bd_addr->addr[1],
2901 bd_addr->addr[2], bd_addr->addr[3],
2902 bd_addr->addr[4], bd_addr->addr[5],
2903 profile, *restricted);
2905 conn = _bt_gdbus_get_system_gconn();
2906 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2908 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2910 device_path = _bt_get_device_object_path(address);
2911 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2913 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2914 NULL, BT_BLUEZ_NAME, device_path,
2915 BT_PROPERTIES_INTERFACE, NULL, NULL);
2917 g_free(device_path);
2918 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2920 reply = g_dbus_proxy_call_sync(proxy, "Get",
2921 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2922 G_DBUS_CALL_FLAGS_NONE, -1,
2924 g_object_unref(proxy);
2926 if (reply == NULL) {
2927 BT_ERR("Failed to Get Profile Restricted");
2928 ret = BLUETOOTH_ERROR_INTERNAL;
2930 BT_ERR("Error %s[%s]", error->message, address);
2931 g_error_free(error);
2936 g_variant_get(reply, "(v)", &temp);
2937 restricted_profile_flag = g_variant_get_uint32(temp);
2938 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2940 ret = _bt_get_restricted_profile_from_flag(profile,
2941 restricted_profile_flag, restricted);
2942 g_variant_unref(temp);
2943 g_variant_unref(reply);
2946 BT_DBG("TRUST %d", *restricted);
2950 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2954 GVariant *out_param1;
2955 request_info_t *req_info;
2956 GVariant *val = NULL;
2957 GVariant *param = NULL;
2960 bluetooth_device_address_t device_addr = { {0} };
2961 int result = BLUETOOTH_ERROR_NONE;
2964 val = g_dbus_proxy_call_finish(proxy, res, &err);
2966 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2967 if (req_info == NULL) {
2968 BT_ERR("req_info == NULL");
2969 g_object_unref(proxy);
2970 att_mtu_req_info->device_proxy = NULL;
2975 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2977 if (g_strrstr(err->message, "NotSupported")) {
2978 BT_INFO("Connection Not Supported");
2979 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2980 } else if (g_strrstr(err->message, "NotConnected")) {
2981 BT_INFO("Not connected");
2982 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2983 } else if (g_strrstr(err->message, "InvalidArguments")) {
2984 BT_INFO("Not connected");
2985 result = BLUETOOTH_ERROR_INVALID_PARAM;
2987 BT_DBG("Default case");
2988 result = BLUETOOTH_ERROR_INTERNAL;
2992 g_object_unref(proxy);
2993 att_mtu_req_info->device_proxy = NULL;
2995 if (result != BLUETOOTH_ERROR_NONE)
2999 g_variant_get(val, "(qy)", &mtu, &status);
3000 g_variant_unref(val);
3003 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3005 param = g_variant_new("(isqy)",
3007 att_mtu_req_info->addr,
3011 /* Send the event to application */
3012 _bt_send_event(BT_DEVICE_EVENT,
3013 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3017 if (req_info->context == NULL)
3020 _bt_convert_addr_string_to_type(device_addr.addr,
3021 (const char *)att_mtu_req_info->addr);
3023 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3024 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3025 g_dbus_method_invocation_return_value(req_info->context,
3026 g_variant_new("(iv)", result, out_param1));
3028 _bt_delete_request_list(req_info->req_id);
3033 g_free(att_mtu_req_info->addr);
3034 g_free(att_mtu_req_info);
3035 att_mtu_req_info = NULL;
3040 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3043 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3044 gchar *device_path = NULL;
3045 GDBusProxy *adapter_proxy;
3046 GDBusProxy *device_proxy;
3047 GDBusConnection *conn;
3048 int ret = BLUETOOTH_ERROR_NONE;
3050 BT_CHECK_PARAMETER(device_address, return);
3052 if (att_mtu_req_info) {
3053 BT_ERR("ATT MTU request in progress");
3054 return BLUETOOTH_ERROR_DEVICE_BUSY;
3057 conn = _bt_gdbus_get_system_gconn();
3058 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3060 adapter_proxy = _bt_get_adapter_proxy();
3061 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3063 _bt_convert_addr_type_to_string(address, device_address->addr);
3065 BT_DBG("Remote device address: %s", address);
3067 device_path = _bt_get_device_object_path(address);
3069 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3071 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3072 NULL, BT_BLUEZ_NAME,
3073 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3074 g_free(device_path);
3075 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3077 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3078 att_mtu_req_info->addr = (char *)g_strdup(address);
3079 att_mtu_req_info->req_id = request_id;
3080 att_mtu_req_info->device_proxy = device_proxy;
3082 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3083 g_variant_new("(q)", mtu),
3084 G_DBUS_CALL_FLAGS_NONE,
3085 BT_MAX_DBUS_TIMEOUT,
3087 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3093 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3096 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3097 char *object_path = NULL;
3099 GDBusProxy *device_proxy;
3100 GError *error = NULL;
3102 GVariant *tmp_value;
3103 GDBusConnection *conn;
3104 GVariant *result = NULL;
3105 int ret = BLUETOOTH_ERROR_NONE;
3107 BT_CHECK_PARAMETER(device_address, return);
3109 conn = _bt_gdbus_get_system_gconn();
3110 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3112 _bt_convert_addr_type_to_string(address, device_address->addr);
3114 object_path = _bt_get_device_object_path(address);
3115 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3117 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3118 NULL, BT_BLUEZ_NAME, object_path,
3119 BT_PROPERTIES_INTERFACE, NULL, NULL);
3120 g_free(object_path);
3121 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3123 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3124 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3125 G_DBUS_CALL_FLAGS_NONE,
3129 if (result == NULL) {
3130 if (error != NULL) {
3131 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3132 g_error_free(error);
3134 g_object_unref(device_proxy);
3135 return BLUETOOTH_ERROR_INTERNAL;
3138 g_variant_get(result , "(@a{sv})", &value);
3139 g_variant_unref(result);
3141 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3142 if (tmp_value == NULL) {
3143 g_object_unref(device_proxy);
3144 g_variant_unref(value);
3145 return BLUETOOTH_ERROR_INTERNAL;
3148 *mtu = g_variant_get_uint16(tmp_value);
3150 BT_DBG("ATT MTU : %d", *mtu);
3152 g_variant_unref(tmp_value);
3153 g_variant_unref(value);
3154 g_object_unref(device_proxy);
3159 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3160 bluetooth_device_address_t *id_address)
3162 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3163 gchar *device_path = NULL;
3164 const gchar *idaddress = NULL;
3165 GDBusProxy *device_proxy;
3166 GError *error = NULL;
3167 GVariant *result = NULL;
3168 GDBusConnection *conn;
3169 int ret = BLUETOOTH_ERROR_NONE;
3171 BT_CHECK_PARAMETER(device_address, return);
3173 conn = _bt_gdbus_get_system_gconn();
3174 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3176 _bt_convert_addr_type_to_string(address, device_address->addr);
3178 device_path = _bt_get_device_object_path(address);
3179 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3181 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3182 NULL, BT_BLUEZ_NAME,
3183 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3184 g_free(device_path);
3185 if (!device_proxy) {
3186 BT_ERR("Unable to get proxy");
3187 return BLUETOOTH_ERROR_INTERNAL;
3190 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3192 G_DBUS_CALL_FLAGS_NONE,
3197 if (result == NULL) {
3198 BT_ERR("Failed to get device ID address");
3199 if (error != NULL) {
3200 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3201 g_error_free(error);
3203 g_object_unref(device_proxy);
3204 return BLUETOOTH_ERROR_INTERNAL;
3207 g_variant_get(result , "(s)", &idaddress);
3208 g_variant_unref(result);
3210 if (idaddress == NULL) {
3211 BT_ERR("No paired device");
3212 g_object_unref(device_proxy);
3213 return BLUETOOTH_ERROR_NOT_PAIRED;
3216 BT_DBG("ID Address:%s", idaddress);
3219 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3221 ret = BLUETOOTH_ERROR_INTERNAL;
3223 g_object_unref(device_proxy);
3228 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3230 GapAgentPrivate *agent = _bt_get_adapter_agent();
3231 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3233 if (authentication_reply)
3234 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3236 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3238 BT_DBG("BT_PASSKEY_REPLY");
3239 return BLUETOOTH_ERROR_NONE;
3242 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3244 GapAgentPrivate *agent = _bt_get_adapter_agent();
3245 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3247 if (confirmation_reply)
3248 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3250 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3252 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3253 return BLUETOOTH_ERROR_NONE;