2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-event.h"
33 #include "bt-service-device.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
40 #include "bt-service-pbap.h"
42 #ifdef TIZEN_FEATURE_BT_DPM
43 #include "bt-service-dpm.h"
46 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
47 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
48 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
50 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
51 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
52 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
53 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
54 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
55 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
56 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
57 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
59 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
61 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
62 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
63 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
65 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
67 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
69 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
70 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
71 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
73 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
80 GDBusProxy *device_proxy;
81 GDBusProxy *adapter_proxy;
83 unsigned short conn_type;
84 gboolean bonding_wo_discovery;
94 bluetooth_device_address_t bd_addr;
95 gboolean auto_connect;
96 } bt_pending_le_conn_info_s;
103 } bt_connected_le_dev_t;
112 } bt_le_conn_param_t;
114 gboolean is_device_creating;
115 bt_funcion_data_t *bonding_info;
116 bt_funcion_data_t *searching_info;
117 bt_funcion_data_t *att_mtu_req_info;
119 static GSList *le_connected_dev_list = NULL;
120 static GSList *pin_info_list = NULL;
121 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
122 static guint pending_le_conn_timer_id = 0;
124 /* This HID Mouse does not support pairing precedure. need to skip it. */
125 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
127 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
130 static int __bt_retry_bond(void);
133 /*static void __bt_decline_pair_request()
135 GVariant *out_param1;
136 GVariant *out_param2;
137 request_info_t *req_info;
138 bluetooth_device_info_t dev_info;
139 bt_remote_dev_info_t *remote_dev_info;
140 GVariant *uuid_list, *manufacture_data;
145 req_info = _bt_get_request_info(bonding_info->req_id);
146 if (req_info == NULL) {
147 BT_ERR("req_info == NULL");
150 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
152 BT_DBG("bonding_info is NULL");
157 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
158 remote_dev_info->uuids, remote_dev_info->uuid_count,
161 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
162 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
165 param = g_variant_new("isunsbuba{s}na{y})",
166 bonding_info->result,
168 remote_dev_info->class,
169 remote_dev_info->rssi,
170 remote_dev_info->name,
171 remote_dev_info->paired,
172 remote_dev_info->connected,
173 remote_dev_info->trust,
175 remote_dev_info->manufacturer_data_len,
179 //Send the event to application
180 if (remote_dev_info != NULL) {
181 _bt_send_event(BT_ADAPTER_EVENT,
182 BLUETOOTH_EVENT_BONDING_FINISHED,
185 _bt_free_device_info(remote_dev_info);
188 if (req_info->context == NULL)
191 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
192 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
195 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
196 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
198 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
199 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
202 g_dbus_method_invocation_return_value(req_info->context,
203 g_variant_new("@ayi", out_param1, out_param2));
206 _bt_delete_request_list(req_info->req_id);
209 g_free(bonding_info->addr);
210 g_free(bonding_info);
216 #ifdef TIZEN_PROFILE_WEARABLE
217 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
221 retv_if(user_data == NULL, FALSE);
223 b = (bundle *)user_data;
225 ret = syspopup_launch("bt-syspopup", b);
227 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
229 BT_DBG("Hurray!!! Finally Popup launched");
232 return (ret < 0) ? TRUE : FALSE;
235 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
240 GDBusConnection *conn;
242 conn = _bt_gdbus_get_system_gconn();
250 bundle_add(b, "event-type", "unable-to-pairing");
252 if (result == BLUETOOTH_ERROR_TIMEOUT)
253 bundle_add(b, "error", "timeout");
254 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
255 bundle_add(b, "error", "authfailed");
257 bundle_add(b, "error", "error");
259 ret = syspopup_launch("bt-syspopup", b);
261 BT_ERR("Popup launch failed...retry %d \n", ret);
262 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
273 gboolean _bt_is_device_creating(void)
275 return is_device_creating;
278 gboolean _bt_is_bonding_device_address(const char *address)
280 if (bonding_info == NULL || bonding_info->addr == NULL)
283 if (g_strcmp0(bonding_info->addr, address) == 0) {
284 BT_DBG("[%s] is bonding device", address);
288 BT_DBG("[%s] is NOT bonding device", address);
292 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
294 ret_if(bonding_info == NULL);
295 bonding_info->is_autopair = is_autopair;
298 void __bt_cancel_search_service_done(void)
300 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
301 request_info_t *req_info;
302 bluetooth_device_info_t dev_info;
303 GVariant *out_param1;
305 ret_if(searching_info == NULL);
307 req_info = _bt_get_request_info(searching_info->req_id);
308 if (req_info == NULL) {
309 BT_ERR("req_info == NULL");
313 if (req_info->context == NULL)
316 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
317 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
318 searching_info->addr);
320 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
321 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
324 g_dbus_method_invocation_return_value(req_info->context,
325 g_variant_new("(iv)", result, out_param1));
327 _bt_delete_request_list(req_info->req_id);
331 g_free(searching_info->addr);
332 g_free(searching_info);
333 searching_info = NULL;
336 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
338 ret_if(value == NULL);
339 ret_if(info == NULL);
341 gsize uuid_count = 0;
343 info->uuids = g_variant_dup_strv(value, &uuid_count);
344 info->uuid_count = (unsigned int)uuid_count;
346 BT_DBG("uuid count : %d", uuid_count);
349 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
351 char *object_path = NULL;
352 bt_remote_dev_info_t *dev_info;
354 retv_if(address == NULL, NULL);
356 object_path = _bt_get_device_object_path(address);
357 retv_if(object_path == NULL, NULL);
359 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
365 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
366 const char *object_path)
368 bt_remote_dev_info_t *dev_info;
369 GDBusProxy *adapter_proxy;
370 GDBusProxy *device_proxy;
375 GDBusConnection *conn;
376 GError *error = NULL;
377 GVariant *result = NULL;
378 GVariantIter *value_iter;
382 adapter_proxy = _bt_get_adapter_proxy();
383 retv_if(adapter_proxy == NULL, NULL);
385 retv_if(object_path == NULL, NULL);
387 conn = _bt_gdbus_get_system_gconn();
389 BT_ERR("conn == NULL");
393 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
394 NULL, BT_BLUEZ_NAME, object_path,
395 BT_PROPERTIES_INTERFACE, NULL, NULL);
397 retv_if(device_proxy == NULL, NULL);
399 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
400 g_variant_new("(s)", BT_DEVICE_INTERFACE),
401 G_DBUS_CALL_FLAGS_NONE,
406 g_object_unref(device_proxy);
408 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
410 if (result != NULL) {
411 g_variant_get(result , "(@a{sv})", &value);
412 g_variant_unref(result);
414 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
416 g_variant_get(tmp_value, "s", &name);
417 g_variant_unref(tmp_value);
419 DBG_SECURE("Alias Name [%s]", name);
421 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
422 g_variant_get(tmp_value, "s", &name);
423 g_variant_unref(tmp_value);
426 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
428 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
429 g_variant_unref(tmp_value);
431 dev_info->is_alias_set = FALSE;
433 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
435 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
437 dev_info->class = g_variant_get_uint32(tmp_value);
438 g_variant_unref(tmp_value);
442 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
444 dev_info->connected = g_variant_get_byte(tmp_value);
445 g_variant_unref(tmp_value);
447 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
448 BT_DBG("connected link : %d", dev_info->connected);
450 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
452 dev_info->trust = g_variant_get_boolean(tmp_value);
453 g_variant_unref(tmp_value);
455 dev_info->trust = FALSE;
457 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
459 dev_info->paired = g_variant_get_boolean(tmp_value);
460 g_variant_unref(tmp_value);
462 dev_info->paired = FALSE;
464 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
466 dev_info->rssi = g_variant_get_int16(tmp_value);
467 g_variant_unref(tmp_value);
471 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
473 dev_info->addr_type = g_variant_get_byte(tmp_value);
474 g_variant_unref(tmp_value);
476 dev_info->addr_type = 0;
478 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
480 __bt_get_uuids(tmp_value, dev_info);
481 g_variant_unref(tmp_value);
484 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
486 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
487 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
488 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
489 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
491 g_variant_unref(tmp_value);
493 dev_info->manufacturer_data_len = 0;
495 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
497 if ((dev_info->manufacturer_data_len == 0) ||
498 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
499 BT_ERR("manufacturer data length doesn't match");
500 dev_info->manufacturer_data_len = 0;
501 dev_info->manufacturer_data = NULL;
503 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
504 g_variant_get(tmp_value, "ay", &value_iter);
505 while (g_variant_iter_loop(value_iter, "y", &m_value))
506 dev_info->manufacturer_data[i++] = m_value;
508 g_variant_unref(tmp_value);
510 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
511 dev_info->manufacturer_data_len = 0;
512 dev_info->manufacturer_data = NULL;
515 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
516 g_variant_get(tmp_value, "s", &address);
517 g_variant_unref(tmp_value);
519 dev_info->address = g_strdup(address);
520 dev_info->name = g_strdup(name);
522 g_variant_unref(value);
524 BT_ERR("result is NULL\n");
532 char *_bt_get_device_name(const char *bdaddress)
534 char *device_path = NULL;
535 const gchar *name = NULL;
536 gchar *dev_name = NULL;
538 GVariant *result = NULL;
540 GDBusProxy *device_proxy;
541 GDBusConnection *conn;
543 retv_if(bdaddress == NULL, NULL);
545 device_path = _bt_get_device_object_path((char *)bdaddress);
546 retv_if(device_path == NULL, NULL);
548 conn = _bt_gdbus_get_system_gconn();
549 retv_if(conn == NULL, NULL);
550 INFO_SECURE("Device_path %s", device_path);
551 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
552 NULL, BT_BLUEZ_NAME, device_path,
553 BT_PROPERTIES_INTERFACE, NULL, &err);
556 retv_if(device_proxy == NULL, NULL);
558 result = g_dbus_proxy_call_sync(device_proxy, "Get",
559 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
560 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
562 BT_ERR("DBus Error : %s", err->message);
566 g_variant_get(result, "(v)", &value);
567 name = g_variant_get_string(value, &name_len);
568 INFO_SECURE("Alias Name [%s]", name);
570 dev_name = g_strdup(name);
571 g_variant_unref(value);
572 g_variant_unref(result);
577 result = g_dbus_proxy_call_sync(device_proxy, "Get",
578 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
579 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
581 ERR("DBus Error : %s", err->message);
584 g_variant_get(result, "(v)", &value);
585 name = g_variant_get_string(value, &name_len);
586 INFO_SECURE("Name = %s", name);
588 dev_name = g_strdup(name);
589 g_variant_unref(value);
590 g_variant_unref(result);
594 g_object_unref(device_proxy);
598 static gboolean __ignore_auto_pairing_request(const char *address)
603 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
614 /* Get the LAP(Lower Address part) */
615 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
617 snprintf(lap_address, sizeof(lap_address), ",%s", address);
619 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
622 BT_ERR("fopen failed \n");
626 fseek(fp, 0, SEEK_END);
631 BT_ERR("Get file size failed \n");
636 buffer = g_malloc0(sizeof(char) * size);
637 result = fread((char *)buffer, 1, size, fp);
639 if (result != size) {
640 BT_ERR("Read Error\n");
645 BT_DBG("Buffer = %s\n", buffer);
647 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
653 /* Write the data and insert new device data */
654 for (i = 0; lines[i] != NULL; i++) {
655 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
656 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
658 lines[i] = temp_buffer;
661 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
663 /* Fix : NULL_RETURNS */
664 retv_if(buffer == NULL, FALSE);
666 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
669 BT_ERR("fopen failed \n");
674 BT_DBG("Buffer = %s\n", buffer);
675 fwrite(buffer, 1, strlen(buffer), fp);
685 static int __bt_cancel_bonding(void)
687 BT_CHECK_PARAMETER(bonding_info, return);
688 BT_CHECK_PARAMETER(bonding_info->addr, return);
689 /* First Cancel the ongoing pairing in bluez otherwise if we send
690 * pair request bluez will send inprogress and we again retry bond so
691 * this cycle continues */
693 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
694 NULL, G_DBUS_CALL_FLAGS_NONE,
697 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
699 return BLUETOOTH_ERROR_PARING_FAILED;
701 return BLUETOOTH_ERROR_NONE;
704 static int __bt_retry_bond(void)
706 BT_CHECK_PARAMETER(bonding_info, return);
707 BT_CHECK_PARAMETER(bonding_info->addr, return);
709 is_device_creating = TRUE;
711 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
712 g_variant_new("(y)", bonding_info->conn_type),
713 G_DBUS_CALL_FLAGS_NONE,
716 (GAsyncReadyCallback)__bt_bond_device_cb,
719 return BLUETOOTH_ERROR_NONE;
723 static int __bt_remove_and_bond(void)
725 GDBusProxy *adapter_proxy;
726 GVariant *result = NULL;
728 char *device_path = NULL;
730 BT_CHECK_PARAMETER(bonding_info, return);
731 BT_CHECK_PARAMETER(bonding_info->addr, return);
733 adapter_proxy = _bt_get_adapter_proxy();
734 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
736 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
737 g_variant_new("(s)", bonding_info->addr),
738 G_DBUS_CALL_FLAGS_NONE,
743 return BLUETOOTH_ERROR_INTERNAL;
745 g_variant_get(result , "(o)", &device_path);
746 g_variant_unref(result);
748 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
750 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
751 g_variant_new("(o)", device_path),
752 G_DBUS_CALL_FLAGS_NONE,
758 BT_ERR("UnpairDevice Fail: %s", err->message);
760 return BLUETOOTH_ERROR_INTERNAL;
763 return __bt_retry_bond();
766 static int __bt_cancel_and_bond(void)
768 int ret = BLUETOOTH_ERROR_NONE;
770 ret = _bt_agent_reply_cancellation();
771 if (ret != BLUETOOTH_ERROR_NONE) {
772 BT_ERR("Fail to call reply cancellation");
775 return __bt_retry_bond();
779 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
782 int result = BLUETOOTH_ERROR_NONE;
785 GVariant *out_param1;
786 request_info_t *req_info;
787 bluetooth_device_info_t dev_info;
788 bt_remote_dev_info_t *remote_dev_info;
789 GVariant *manufacture_data;
792 /* Terminate ALL system popup */
793 syspopup_destroy_all();
795 reply = g_dbus_proxy_call_finish(proxy, res, &err);
797 g_variant_unref(reply);
799 is_device_creating = FALSE;
801 if (bonding_info == NULL) {
803 BT_ERR("bonding_info == NULL");
809 req_info = _bt_get_request_info(bonding_info->req_id);
810 if (req_info == NULL) {
811 BT_ERR("req_info == NULL");
816 g_dbus_error_strip_remote_error(err);
817 BT_ERR("Error occured in CreateBonding [%s]", err->message);
819 if (g_strrstr(err->message, "Already Exists")) {
820 BT_INFO("Existing Bond, remove and retry");
821 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
823 result = BLUETOOTH_ERROR_PARING_FAILED;
824 } else if (_bt_agent_is_canceled() ||
825 g_strrstr(err->message, "Authentication Canceled")) {
826 BT_INFO("Cancelled by USER");
827 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
828 } else if (g_strrstr(err->message, "Authentication Rejected")) {
830 result = BLUETOOTH_ERROR_ACCESS_DENIED;
831 } else if (g_strrstr(err->message, "In Progress")) {
832 BT_INFO("Bond in progress, cancel and retry");
833 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
835 result = BLUETOOTH_ERROR_PARING_FAILED;
836 } else if (g_strrstr(err->message, "Authentication Failed")) {
837 BT_INFO("Authentication Failed");
838 if (bonding_info->is_autopair == TRUE) {
839 _bt_set_autopair_status_in_bonding_info(FALSE);
840 __ignore_auto_pairing_request(bonding_info->addr);
842 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
843 } else if (g_strrstr(err->message, "Page Timeout")) {
844 BT_INFO("Page Timeout");
845 /* This is the special case
846 As soon as call bluetooth_bond_device, try to cancel bonding.
847 In this case, before completing to call 'CreatePairedDevice' method
848 the procedure is stopped. So 'Cancle' error is not return.
850 result = BLUETOOTH_ERROR_HOST_DOWN;
851 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
852 BT_INFO("Cancel already running bonding");
853 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
854 BT_INFO("Error while Cancelling bonding");
855 /* we need to unref proxy so continue */
857 result = BLUETOOTH_ERROR_INTERNAL;
858 } else if (g_strrstr(err->message, "Connection Timeout")) {
859 /* Pairing request timeout */
860 result = BLUETOOTH_ERROR_TIMEOUT;
861 } else if (g_strrstr(err->message, "Authentication Timeout")) {
862 /* Pairing request timeout */
863 result = BLUETOOTH_ERROR_TIMEOUT;
865 BT_DBG("Default case");
866 result = BLUETOOTH_ERROR_PARING_FAILED;
870 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
871 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
872 result == BLUETOOTH_ERROR_TIMEOUT ||
873 result == BLUETOOTH_ERROR_HOST_DOWN) {
874 #ifdef TIZEN_PROFILE_WEARABLE
875 int is_sw_running = 0;
877 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
878 BT_ERR("vconf_get_int for setup wizard state failed");
881 __bt_launch_unable_to_pairing_syspopup(result);
883 BT_ERR("Unable to pair");
885 bonding_info->result = result;
888 g_object_unref(proxy);
889 bonding_info->device_proxy = NULL;
891 if (result != BLUETOOTH_ERROR_NONE) {
892 if (bonding_info->bonding_wo_discovery) {
893 GDBusProxy *adapter_proxy;
894 GVariant *ret = NULL;
895 GError *error = NULL;
898 BT_ERR("Bond was tried without discovery. Remove it");
900 adapter_proxy = _bt_get_adapter_proxy();
901 if (adapter_proxy == NULL) {
902 BT_ERR("Cannot get adapter_proxy");
906 device_path = _bt_get_device_object_path(bonding_info->addr);
907 if (device_path == NULL) {
908 BT_ERR("Cannot get device path");
912 ret = g_dbus_proxy_call_sync(adapter_proxy,
914 g_variant_new("(o)", device_path),
915 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
918 BT_ERR("RemoveDevice Fail: %s", error->message);
919 g_clear_error(&error);
923 g_variant_unref(ret);
931 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
932 if (!remote_dev_info)
935 GVariant *uuids = NULL;
936 GVariantBuilder *builder = NULL;
938 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
939 for (i = 0; i < remote_dev_info->uuid_count; i++) {
940 g_variant_builder_add(builder, "s",
941 remote_dev_info->uuids[i]);
943 uuids = g_variant_new("as", builder);
944 g_variant_builder_unref(builder);
945 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
946 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
949 param = g_variant_new("(isunsbub@asn@ay)",
952 remote_dev_info->class,
953 remote_dev_info->rssi,
954 remote_dev_info->name,
955 remote_dev_info->paired,
956 remote_dev_info->connected,
957 remote_dev_info->trust,
959 remote_dev_info->manufacturer_data_len,
963 /* Send the event to application */
964 _bt_send_event(BT_ADAPTER_EVENT,
965 BLUETOOTH_EVENT_BONDING_FINISHED,
968 _bt_free_device_info(remote_dev_info);
971 if (req_info->context == NULL)
974 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
975 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
978 if (_bt_adapter_get_status() != BT_ACTIVATED)
979 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
981 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
982 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
984 g_dbus_method_invocation_return_value(req_info->context,
985 g_variant_new("(iv)", result, out_param1));
987 _bt_delete_request_list(req_info->req_id);
992 _bt_agent_set_canceled(FALSE);
995 g_free(bonding_info->addr);
996 g_free(bonding_info);
1000 int _bt_bond_device(int request_id,
1001 bluetooth_device_address_t *device_address,
1002 unsigned short conn_type, GArray **out_param1)
1005 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1006 bluetooth_device_info_t dev_info;
1008 GDBusConnection *conn;
1009 char *device_path = NULL;
1010 GDBusProxy *adapter_proxy;
1011 GError *error = NULL;
1012 gboolean bonding_wo_discovery = FALSE;
1014 BT_CHECK_PARAMETER(device_address, return);
1016 #ifdef TIZEN_FEATURE_BT_DPM
1017 int pairing_state = DPM_STATUS_ERROR;
1019 _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
1020 if (pairing_state == DPM_RESTRICTED) {
1021 BT_ERR("Not allow to pair the device");
1022 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
1027 BT_ERR("Bonding in progress");
1029 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1030 memcpy(dev_info.device_address.addr, device_address->addr,
1031 BLUETOOTH_ADDRESS_LENGTH);
1033 g_array_append_vals(*out_param1, &dev_info,
1034 sizeof(bluetooth_device_info_t));
1036 return BLUETOOTH_ERROR_DEVICE_BUSY;
1039 conn = _bt_gdbus_get_system_gconn();
1040 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1042 _bt_convert_addr_type_to_string(address, device_address->addr);
1044 device_path = _bt_get_device_object_path(address);
1046 if (device_path == NULL) {
1047 BT_ERR("No searched device");
1048 GVariant *ret = NULL;
1049 adapter_proxy = _bt_get_adapter_proxy();
1050 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1052 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1053 g_variant_new("(s)", address),
1054 G_DBUS_CALL_FLAGS_NONE,
1059 if (error != NULL) {
1060 BT_ERR("CreateDevice Fail: %s", error->message);
1061 g_clear_error(&error);
1064 g_variant_unref(ret);
1065 device_path = _bt_get_device_object_path(address);
1066 if (device_path == NULL) {
1067 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1068 memcpy(dev_info.device_address.addr, device_address->addr,
1069 BLUETOOTH_ADDRESS_LENGTH);
1071 g_array_append_vals(*out_param1, &dev_info,
1072 sizeof(bluetooth_device_info_t));
1074 return BLUETOOTH_ERROR_NOT_PAIRED;
1076 BT_INFO("device_path is created[%s]", device_path);
1078 bonding_wo_discovery = TRUE;
1081 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1082 NULL, BT_BLUEZ_NAME,
1083 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1085 g_free(device_path);
1086 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1088 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1089 bonding_info->addr = g_strdup(address);
1090 bonding_info->req_id = request_id;
1092 bonding_info->device_proxy = proxy;
1093 bonding_info->conn_type = conn_type;
1094 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1096 is_device_creating = TRUE;
1098 g_dbus_proxy_call(proxy, "Pair",
1099 g_variant_new("(y)", conn_type),
1100 G_DBUS_CALL_FLAGS_NONE,
1101 BT_MAX_DBUS_TIMEOUT,
1103 (GAsyncReadyCallback)__bt_bond_device_cb,
1106 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1108 return BLUETOOTH_ERROR_NONE;
1110 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1111 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1112 bonding_info->addr);
1114 g_array_append_vals(*out_param1, &dev_info,
1115 sizeof(bluetooth_device_info_t));
1117 is_device_creating = FALSE;
1119 g_free(bonding_info->addr);
1120 g_free(bonding_info);
1121 bonding_info = NULL;
1123 return BLUETOOTH_ERROR_INTERNAL;*/
1126 int _bt_cancel_bonding(void)
1128 int ret = BLUETOOTH_ERROR_NONE;
1130 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1132 ret = _bt_agent_reply_cancellation();
1133 if (ret != BLUETOOTH_ERROR_NONE) {
1134 BT_ERR("Fail to call reply cancellation");
1138 _bt_agent_set_canceled(TRUE);
1140 return BLUETOOTH_ERROR_NONE;
1143 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1148 GVariant *out_param1;
1149 int result = BLUETOOTH_ERROR_NONE;
1150 bt_funcion_data_t *unbonding_info;
1151 bluetooth_device_info_t dev_info;
1152 request_info_t *req_info;
1154 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1156 g_variant_unref(reply);
1158 unbonding_info = user_data;
1160 if (unbonding_info == NULL) {
1162 BT_ERR("unbonding_info == NULL");
1166 req_info = _bt_get_request_info(unbonding_info->req_id);
1167 if (req_info == NULL) {
1168 BT_ERR("req_info == NULL");
1173 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1174 result = BLUETOOTH_ERROR_INTERNAL;
1177 if (req_info->context == NULL)
1180 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1181 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1182 unbonding_info->addr);
1184 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1185 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1187 g_dbus_method_invocation_return_value(req_info->context,
1188 g_variant_new("(iv)", result, out_param1));
1191 _bt_delete_request_list(req_info->req_id);
1197 if (unbonding_info) {
1198 g_free(unbonding_info->addr);
1199 g_free(unbonding_info);
1203 int _bt_unbond_device(int request_id,
1204 bluetooth_device_address_t *device_address,
1205 GArray **out_param1)
1207 char *device_path = NULL;
1208 bt_funcion_data_t *unbonding_info;
1209 GDBusProxy *adapter_proxy = NULL;
1210 GDBusProxy *device_proxy = NULL;
1211 GDBusConnection *conn;
1212 int result = BLUETOOTH_ERROR_INTERNAL;
1213 bluetooth_device_info_t dev_info;
1214 GError *error = NULL;
1215 GVariant *ret = NULL;
1217 BT_CHECK_PARAMETER(device_address, return);
1219 adapter_proxy = _bt_get_adapter_proxy();
1220 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1222 /* allocate user data so that it can be retrieved in callback */
1223 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1224 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1225 unbonding_info->req_id = request_id;
1227 _bt_convert_addr_type_to_string(unbonding_info->addr,
1228 device_address->addr);
1230 device_path = _bt_get_device_object_path(unbonding_info->addr);
1232 if (device_path == NULL) {
1233 BT_ERR("No paired device");
1234 result = BLUETOOTH_ERROR_NOT_PAIRED;
1238 conn = _bt_gdbus_get_system_gconn();
1240 BT_ERR("conn is NULL");
1241 result = BLUETOOTH_ERROR_INTERNAL;
1245 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1246 NULL, BT_BLUEZ_NAME,
1247 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1249 if (device_proxy != NULL) {
1251 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1252 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1253 G_DBUS_CALL_FLAGS_NONE,
1258 BT_ERR("Getting property failed: [%s]\n", error->message);
1259 g_error_free(error);
1260 result = BLUETOOTH_ERROR_NOT_PAIRED;
1264 BT_ERR("No paired device");
1265 g_object_unref(device_proxy);
1266 result = BLUETOOTH_ERROR_NOT_PAIRED;
1269 g_variant_unref(ret);
1271 g_object_unref(device_proxy);
1274 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1275 g_variant_new("(o)", device_path),
1276 G_DBUS_CALL_FLAGS_NONE,
1277 BT_MAX_DBUS_TIMEOUT,
1279 (GAsyncReadyCallback)__bt_unbond_cb,
1282 g_free(device_path);
1283 return BLUETOOTH_ERROR_NONE;
1286 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1287 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1288 unbonding_info->addr);
1290 g_array_append_vals(*out_param1, &dev_info,
1291 sizeof(bluetooth_device_info_t));
1292 g_free(device_path);
1293 g_free(unbonding_info->addr);
1294 g_free(unbonding_info);
1298 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1303 GVariant *out_param1;
1304 int result = BLUETOOTH_ERROR_NONE;
1305 bluetooth_device_info_t dev_info;
1306 bt_remote_dev_info_t *remote_dev_info;
1307 request_info_t *req_info;
1308 GVariant *uuid_list, *manufacture_data;
1310 GVariantBuilder *builder = NULL;
1313 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1315 g_variant_unref(reply);
1317 g_object_unref(proxy);
1319 if (searching_info == NULL) {
1321 BT_ERR("unbonding_info == NULL");
1325 req_info = _bt_get_request_info(searching_info->req_id);
1326 if (req_info == NULL) {
1327 BT_ERR("req_info == NULL");
1332 g_dbus_error_strip_remote_error(err);
1333 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1335 if (g_strrstr("Operation canceled", err->message))
1336 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1337 else if (g_strrstr("In Progress", err->message))
1338 result = BLUETOOTH_ERROR_IN_PROGRESS;
1339 else if (g_strrstr("Host is down", err->message))
1340 result = BLUETOOTH_ERROR_HOST_DOWN;
1342 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1344 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1345 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1346 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1347 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1348 remote_dev_info->uuid_count > 0) {
1349 result = BLUETOOTH_ERROR_NONE;
1352 _bt_free_device_info(remote_dev_info);
1357 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1358 if (!remote_dev_info)
1362 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1363 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1364 g_variant_builder_add(builder, "s",
1365 remote_dev_info->uuids[i]);
1367 uuid_list = g_variant_new("as", builder);
1368 g_variant_builder_unref(builder);
1369 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1370 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1373 param = g_variant_new("(isunsbub@asn@ay)",
1375 searching_info->addr,
1376 remote_dev_info->class,
1377 remote_dev_info->rssi,
1378 remote_dev_info->name,
1379 remote_dev_info->paired,
1380 remote_dev_info->connected,
1381 remote_dev_info->trust,
1383 remote_dev_info->manufacturer_data_len,
1386 /* Send the event to application */
1387 _bt_send_event(BT_ADAPTER_EVENT,
1388 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1391 _bt_free_device_info(remote_dev_info);
1394 if (req_info->context == NULL)
1397 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1398 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1399 searching_info->addr);
1401 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1402 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1404 g_dbus_method_invocation_return_value(req_info->context,
1405 g_variant_new("(iv)", result, out_param1));
1408 _bt_delete_request_list(req_info->req_id);
1413 if (searching_info) {
1414 g_free(searching_info->addr);
1415 g_free(searching_info);
1416 searching_info = NULL;
1420 int _bt_search_device(int request_id,
1421 bluetooth_device_address_t *device_address)
1423 char *device_path = NULL;
1424 GDBusProxy *device_proxy = NULL;
1425 GDBusConnection *conn;
1427 GDBusProxy *adapter_proxy;
1428 int result = BLUETOOTH_ERROR_INTERNAL;
1430 BT_CHECK_PARAMETER(device_address, return);
1433 BT_ERR("Bonding in progress");
1434 return BLUETOOTH_ERROR_DEVICE_BUSY;
1437 if (searching_info) {
1438 BT_ERR("Service searching in progress");
1439 return BLUETOOTH_ERROR_DEVICE_BUSY;
1442 adapter_proxy = _bt_get_adapter_proxy();
1443 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1445 /* allocate user data so that it can be retrieved in callback */
1446 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1447 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1448 searching_info->req_id = request_id;
1450 _bt_convert_addr_type_to_string(searching_info->addr,
1451 device_address->addr);
1453 conn = _bt_gdbus_get_system_gconn();
1454 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1457 device_path = _bt_get_device_object_path(searching_info->addr);
1459 if (device_path == NULL) {
1460 BT_ERR("No paired device");
1461 result = BLUETOOTH_ERROR_NOT_PAIRED;
1465 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1466 NULL, BT_BLUEZ_NAME,
1467 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1468 g_free(device_path);
1469 if (device_proxy == NULL) {
1470 result = BLUETOOTH_ERROR_INTERNAL;
1474 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1475 g_variant_new("(s)", ""),
1476 G_DBUS_CALL_FLAGS_NONE,
1477 BT_MAX_DBUS_TIMEOUT,
1479 (GAsyncReadyCallback)__bt_discover_cb,
1482 searching_info->device_proxy = device_proxy;
1484 return BLUETOOTH_ERROR_NONE;
1487 g_free(searching_info->addr);
1488 g_free(searching_info);
1489 searching_info = NULL;
1493 int _bt_cancel_search_device(void)
1495 GVariant *ret = NULL;
1498 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1500 if (searching_info->device_proxy) {
1501 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1503 G_DBUS_CALL_FLAGS_NONE,
1508 g_variant_unref(ret);
1510 __bt_cancel_search_service_done();
1512 return BLUETOOTH_ERROR_NONE;
1515 int _bt_set_alias(bluetooth_device_address_t *device_address,
1518 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1519 gchar *device_path = NULL;
1520 GDBusProxy *adapter_proxy;
1521 GDBusProxy *device_proxy;
1522 GVariant *ret = NULL;
1523 GError *error = NULL;
1524 GDBusConnection *conn;
1526 BT_CHECK_PARAMETER(device_address, return);
1527 BT_CHECK_PARAMETER(alias, return);
1529 adapter_proxy = _bt_get_adapter_proxy();
1530 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1532 conn = _bt_gdbus_get_system_gconn();
1533 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1535 _bt_convert_addr_type_to_string(address, device_address->addr);
1537 device_path = _bt_get_device_object_path(address);
1539 if (device_path == NULL) {
1540 BT_ERR("No paired device");
1541 return BLUETOOTH_ERROR_NOT_PAIRED;
1544 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1545 NULL, BT_BLUEZ_NAME,
1546 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1548 g_free(device_path);
1549 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1551 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1552 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1553 G_DBUS_CALL_FLAGS_NONE,
1558 g_variant_unref(ret);
1560 g_object_unref(device_proxy);
1563 BT_ERR("SetProperty error: [%s]", error->message);
1564 g_error_free(error);
1565 return BLUETOOTH_ERROR_INTERNAL;
1568 return BLUETOOTH_ERROR_NONE;
1571 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1574 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1575 gchar *device_path = NULL;
1576 GDBusProxy *device_proxy;
1577 gboolean previous_value;
1578 GError *error = NULL;
1579 GDBusConnection *conn;
1580 GVariant *result = NULL;
1581 GVariant *temp = NULL;
1582 int ret = BLUETOOTH_ERROR_NONE;
1584 BT_CHECK_PARAMETER(device_address, return);
1586 conn = _bt_gdbus_get_system_gconn();
1587 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1589 _bt_convert_addr_type_to_string(address, device_address->addr);
1591 device_path = _bt_get_device_object_path(address);
1593 if (device_path == NULL) {
1594 BT_ERR("No paired device");
1595 return BLUETOOTH_ERROR_NOT_PAIRED;
1598 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1599 NULL, BT_BLUEZ_NAME,
1600 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1602 g_free(device_path);
1603 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1605 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1606 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1607 G_DBUS_CALL_FLAGS_NONE,
1611 if (error != NULL) {
1612 BT_ERR("Getting property failed: [%s]\n", error->message);
1613 g_error_free(error);
1614 g_object_unref(device_proxy);
1615 return BLUETOOTH_ERROR_INTERNAL;
1618 g_variant_get(result, "(v)", &temp);
1619 previous_value = g_variant_get_boolean(temp);
1620 g_variant_unref(temp);
1621 g_variant_unref(result);
1622 /* If the input is same with previous value, return error. */
1623 if (previous_value == authorize) {
1624 BT_ERR("Same value: %d", previous_value);
1625 g_object_unref(device_proxy);
1626 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1630 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1631 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1632 G_DBUS_CALL_FLAGS_NONE,
1637 g_object_unref(device_proxy);
1639 BT_ERR("SetProperty error: [%s]", error->message);
1640 g_error_free(error);
1641 ret = BLUETOOTH_ERROR_INTERNAL;
1645 g_variant_unref(result);
1650 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1651 gboolean *is_connected)
1653 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1654 char *object_path = NULL;
1656 GDBusProxy *device_proxy;
1657 GError *error = NULL;
1659 GDBusConnection *conn;
1660 GVariant *result = NULL;
1661 int ret = BLUETOOTH_ERROR_NONE;
1663 BT_CHECK_PARAMETER(device_address, return);
1665 conn = _bt_gdbus_get_system_gconn();
1666 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1668 _bt_convert_addr_type_to_string(address, device_address->addr);
1670 object_path = _bt_get_device_object_path(address);
1671 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1673 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1674 NULL, BT_BLUEZ_NAME, object_path,
1675 BT_PROPERTIES_INTERFACE, NULL, NULL);
1676 g_free(object_path);
1677 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1679 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1680 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1682 G_DBUS_CALL_FLAGS_NONE,
1686 if (result == NULL) {
1687 if (error != NULL) {
1688 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1689 g_error_free(error);
1691 g_object_unref(device_proxy);
1692 return BLUETOOTH_ERROR_INTERNAL;
1695 g_variant_get(result, "(v)", &value);
1696 *is_connected = g_variant_get_boolean(value);
1697 g_variant_unref(result);
1699 BT_DBG("gatt is connected : %d", *is_connected);
1701 g_variant_unref(value);
1702 g_object_unref(device_proxy);
1707 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1708 int connection_type, gboolean *is_connected)
1710 char *object_path = NULL;
1711 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1712 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1713 GDBusProxy *device_proxy = NULL;
1714 GDBusProxy *adapter_proxy = NULL;
1715 GDBusConnection *conn;
1716 GError *error = NULL;
1717 GVariant *tmp_value = NULL;
1718 GVariant *value = NULL;
1719 GVariant *result = NULL;
1722 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1723 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1725 _bt_convert_addr_type_to_string(address, device_address->addr);
1726 _bt_convert_addr_string_to_secure_string(secure_address, address);
1728 *is_connected = FALSE;
1729 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1731 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1732 return _bt_rfcomm_is_device_connected(device_address,
1734 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1735 return _bt_is_gatt_connected(device_address, is_connected);
1736 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1737 return _bt_pbap_is_connected(device_address, is_connected);
1739 adapter_proxy = _bt_get_adapter_proxy();
1740 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1742 conn = _bt_gdbus_get_system_gconn();
1743 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1745 _bt_convert_addr_type_to_string(address, device_address->addr);
1747 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1748 object_path = _bt_get_adapter_path();
1749 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1750 NULL, BT_BLUEZ_NAME,
1751 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1752 g_free(object_path);
1753 if (device_proxy == NULL) {
1754 BT_DBG("Device don't have this service");
1755 return BLUETOOTH_ERROR_INTERNAL;
1758 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1759 g_variant_new("(s)", address),
1760 G_DBUS_CALL_FLAGS_NONE,
1765 if (result == NULL) {
1766 BT_ERR("[GetProperties] Error occured in Proxy call");
1768 BT_ERR("%s", error->message);
1769 g_error_free(error);
1771 *is_connected = FALSE;
1772 g_object_unref(device_proxy);
1773 return BLUETOOTH_ERROR_NONE;
1775 g_variant_get(result , "(@a{sv})", &value);
1776 g_variant_unref(result);
1779 tmp_value = g_variant_lookup_value(value,
1781 G_VARIANT_TYPE_BOOLEAN);
1783 *is_connected = g_variant_get_boolean(tmp_value);
1784 g_variant_unref(tmp_value);
1786 g_variant_unref(value);
1788 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1789 return _bt_is_network_connected(_bt_get_net_conn(),
1790 device_address->addr, is_connected);
1792 uuid = _bt_get_profile_uuid128(connection_type);
1794 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1795 return BLUETOOTH_ERROR_INTERNAL;
1797 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1799 object_path = _bt_get_device_object_path(address);
1801 BT_ERR("object_path is NULL");
1803 return BLUETOOTH_ERROR_NOT_PAIRED;
1806 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1807 NULL, BT_BLUEZ_NAME,
1808 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1809 g_free(object_path);
1810 if (device_proxy == NULL) {
1811 BT_DBG("Device don't have this service");
1813 return BLUETOOTH_ERROR_INTERNAL;
1816 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1817 g_variant_new("(s)", uuid),
1818 G_DBUS_CALL_FLAGS_NONE,
1823 if (result == NULL) {
1824 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1826 BT_ERR("%s", error->message);
1827 if (g_strrstr(error->message, "Not Connected"))
1828 BT_DBG("Not connected");
1829 g_error_free(error);
1831 *is_connected = FALSE;
1832 g_object_unref(device_proxy);
1834 return BLUETOOTH_ERROR_NONE;
1836 g_variant_get(result, "(b)", is_connected);
1838 g_variant_unref(result);
1841 g_object_unref(device_proxy);
1842 return BLUETOOTH_ERROR_NONE;
1845 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1846 bluetooth_connected_link_t *connected)
1848 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1849 char *object_path = NULL;
1851 GDBusProxy *device_proxy;
1852 GError *error = NULL;
1853 GDBusConnection *conn;
1854 GVariant *value = NULL;
1855 GVariant *result = NULL;
1857 BT_CHECK_PARAMETER(device_address, return);
1859 conn = _bt_gdbus_get_system_gconn();
1860 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1862 _bt_convert_addr_type_to_string(address, device_address->addr);
1864 object_path = _bt_get_device_object_path(address);
1865 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1867 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1868 NULL, BT_BLUEZ_NAME,
1869 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1870 g_free(object_path);
1871 if (device_proxy == NULL) {
1872 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1873 return BLUETOOTH_ERROR_NONE;
1876 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1877 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1878 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1880 if (error != NULL) {
1881 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1882 g_error_free(error);
1883 g_object_unref(device_proxy);
1884 return BLUETOOTH_ERROR_INTERNAL;
1886 g_variant_get(result, "(v)", &value);
1887 *connected = g_variant_get_byte(value);
1888 g_variant_unref(value);
1889 g_variant_unref(result);
1892 g_object_unref(device_proxy);
1893 return BLUETOOTH_ERROR_NONE;
1896 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1900 GVariant *out_param1;
1902 int result = BLUETOOTH_ERROR_NONE;
1903 bt_function_data_t *func_data = user_data;
1904 request_info_t *req_info = NULL;
1905 bluetooth_device_address_t device_addr = { {0} };
1907 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1908 g_object_unref(proxy);
1910 if (reply == NULL) {
1911 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1913 BT_ERR("Error: %s", err->message);
1914 g_clear_error(&err);
1916 result = BLUETOOTH_ERROR_INTERNAL;
1918 g_variant_unref(reply);
1921 if (func_data == NULL) {
1922 BT_ERR("func_data is NULL");
1926 req_info = _bt_get_request_info(func_data->req_id);
1927 if (req_info == NULL) {
1928 BT_ERR("req_info is NULL");
1932 if (req_info->context == NULL) {
1933 BT_ERR("req_info->context is NULL");
1937 _bt_convert_addr_string_to_type(device_addr.addr,
1938 (const char *)func_data->address);
1940 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1941 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1944 g_dbus_method_invocation_return_value(req_info->context,
1945 g_variant_new("(iv)", result, out_param1));
1949 _bt_delete_request_list(req_info->req_id);
1952 g_free(func_data->address);
1957 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1958 gboolean auto_connect)
1960 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1961 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1962 gchar *device_path = NULL;
1963 GDBusProxy *device_proxy = NULL;
1964 GDBusConnection *conn;
1965 int ret = BLUETOOTH_ERROR_NONE;
1966 bt_function_data_t *func_data;
1968 BT_CHECK_PARAMETER(bd_addr, return);
1970 conn = _bt_gdbus_get_system_gconn();
1971 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1973 _bt_convert_addr_type_to_string(device_address,
1974 (unsigned char *)bd_addr->addr);
1975 device_path = _bt_get_device_object_path(device_address);
1976 if (device_path == NULL) {
1977 BT_ERR_C("device_path NULL : [%s]", device_address);
1978 ret = BLUETOOTH_ERROR_INTERNAL;
1982 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1983 NULL, BT_BLUEZ_NAME,
1984 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1985 g_free(device_path);
1986 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1988 func_data = g_malloc0(sizeof(bt_function_data_t));
1989 func_data->address = g_strdup(device_address);
1990 if (func_data->address == NULL) {
1991 BT_ERR("Unable to allocate memory for address");
1992 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1996 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1997 BT_INFO("Connect LE [%s]", secure_address);
1999 func_data->req_id = req_id;
2001 g_dbus_proxy_call(device_proxy, "ConnectLE",
2002 g_variant_new("(b)", auto_connect),
2003 G_DBUS_CALL_FLAGS_NONE,
2004 BT_MAX_DBUS_TIMEOUT,
2006 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2012 g_object_unref(device_proxy);
2014 g_free(func_data->address);
2020 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
2024 BT_INFO("Try to initiate pending LE connection");
2026 pending_le_conn_timer_id = 0;
2028 ret = __bt_connect_le_device_internal(pending_le_conn_info->req_id,
2029 &pending_le_conn_info->bd_addr,
2030 pending_le_conn_info->auto_connect);
2031 if (ret != BLUETOOTH_ERROR_NONE) {
2032 request_info_t *req_info = NULL;
2033 GVariant *out_param1;
2035 req_info = _bt_get_request_info(pending_le_conn_info->req_id);
2036 if (req_info == NULL) {
2037 BT_ERR("req_info is NULL");
2041 if (req_info->context == NULL) {
2042 BT_ERR("req_info->context is NULL");
2043 _bt_delete_request_list(req_info->req_id);
2047 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
2048 &pending_le_conn_info->bd_addr,
2049 sizeof(bluetooth_device_address_t), TRUE,
2052 g_dbus_method_invocation_return_value(req_info->context,
2053 g_variant_new("(iv)", ret, out_param1));
2057 g_free(pending_le_conn_info);
2058 pending_le_conn_info = NULL;
2063 void _bt_pending_connect_le_device(void)
2065 if (pending_le_conn_timer_id > 0) {
2066 g_source_remove(pending_le_conn_timer_id);
2067 __bt_connect_le_timer_cb(NULL);
2071 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
2072 gboolean auto_connect)
2074 int ret = BLUETOOTH_ERROR_NONE;
2076 BT_CHECK_PARAMETER(bd_addr, return);
2078 ret = _bt_hold_current_advertising();
2079 if (ret == BLUETOOTH_ERROR_NONE) {
2080 BT_INFO("Current advertising is held");
2081 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2082 pending_le_conn_info->req_id = req_id;
2083 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2084 BLUETOOTH_ADDRESS_LENGTH);
2085 pending_le_conn_info->auto_connect = auto_connect;
2087 pending_le_conn_timer_id =
2088 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2090 return BLUETOOTH_ERROR_NONE;
2091 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
2092 BT_ERR("Unable to hold advertising");
2095 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2098 int _bt_disconnect_le_device(int req_id,
2099 const bluetooth_device_address_t *bd_addr)
2101 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2102 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2104 GDBusProxy *device_proxy;
2105 GDBusConnection *conn;
2106 int ret = BLUETOOTH_ERROR_NONE;
2107 bt_function_data_t *func_data;
2109 BT_CHECK_PARAMETER(bd_addr, return);
2111 conn = _bt_gdbus_get_system_gconn();
2112 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2114 _bt_convert_addr_type_to_string(device_address,
2115 (unsigned char *)bd_addr->addr);
2116 device_path = _bt_get_device_object_path(device_address);
2117 if (device_path == NULL) {
2118 BT_DBG("device_path NULL");
2119 ret = BLUETOOTH_ERROR_INTERNAL;
2123 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2125 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2126 NULL, BT_BLUEZ_NAME,
2127 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2128 g_free(device_path);
2129 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2131 func_data = g_malloc0(sizeof(bt_function_data_t));
2132 func_data->address = g_strdup(device_address);
2133 if (func_data->address == NULL) {
2134 BT_ERR("Unable to allocate memory for address");
2135 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2139 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2140 BT_INFO("Disconnect LE [%s]", secure_address);
2142 func_data->req_id = req_id;
2144 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2146 G_DBUS_CALL_FLAGS_NONE,
2147 BT_MAX_DBUS_TIMEOUT,
2149 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2154 g_object_unref(device_proxy);
2156 g_free(func_data->address);
2162 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2164 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2165 gchar *device_path = NULL;
2166 GError *error = NULL;
2167 GDBusProxy *device_proxy = NULL;
2168 GDBusProxy *adapter_proxy;
2169 GDBusConnection *conn;
2170 int ret = BLUETOOTH_ERROR_NONE;
2172 BT_CHECK_PARAMETER(bd_addr, return);
2174 _bt_convert_addr_type_to_string(device_address,
2175 (unsigned char *)bd_addr->addr);
2177 conn = _bt_gdbus_get_system_gconn();
2178 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2180 adapter_proxy = _bt_get_adapter_proxy();
2181 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2183 device_path = _bt_get_device_object_path(device_address);
2184 if (device_path == NULL) {
2185 BT_DBG("device_path NULL");
2186 ret = BLUETOOTH_ERROR_INTERNAL;
2190 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2192 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2193 NULL, BT_BLUEZ_NAME,
2194 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2195 g_free(device_path);
2196 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2198 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2200 G_DBUS_CALL_FLAGS_NONE,
2205 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2206 g_error_free(error);
2207 g_object_unref(device_proxy);
2208 return BLUETOOTH_ERROR_INTERNAL;
2210 g_object_unref(device_proxy);
2212 /* IPSP daemon launch */
2213 GDBusProxy *ipsp_proxy;
2215 ipsp_proxy = _bt_get_ipsp_proxy();
2216 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2218 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2219 NULL, G_DBUS_CALL_FLAGS_NONE,
2220 -1, NULL, NULL, NULL);
2225 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2227 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2228 gchar *device_path = NULL;
2229 GError *error = NULL;
2230 GDBusProxy *device_proxy = NULL;
2231 GDBusProxy *adapter_proxy;
2232 GDBusConnection *conn;
2233 int ret = BLUETOOTH_ERROR_NONE;
2235 BT_CHECK_PARAMETER(bd_addr, return);
2237 _bt_convert_addr_type_to_string(device_address,
2238 (unsigned char *)bd_addr->addr);
2240 conn = _bt_gdbus_get_system_gconn();
2241 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2243 adapter_proxy = _bt_get_adapter_proxy();
2244 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2246 device_path = _bt_get_device_object_path(device_address);
2247 if (device_path == NULL) {
2248 BT_DBG("device_path NULL");
2249 ret = BLUETOOTH_ERROR_INTERNAL;
2253 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2255 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2256 NULL, BT_BLUEZ_NAME,
2257 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2258 g_free(device_path);
2259 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2261 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2263 G_DBUS_CALL_FLAGS_NONE,
2268 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2269 g_error_free(error);
2270 g_object_unref(device_proxy);
2271 return BLUETOOTH_ERROR_INTERNAL;
2274 g_object_unref(device_proxy);
2279 int _bt_connect_profile(char *address, char *uuid,
2280 void *cb, gpointer func_data)
2284 GDBusConnection *conn;
2285 GDBusProxy *adapter_proxy;
2286 GVariant *result = NULL;
2287 GError *error = NULL;
2289 conn = _bt_gdbus_get_system_gconn();
2290 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2292 object_path = _bt_get_device_object_path(address);
2293 if (object_path == NULL) {
2294 BT_ERR("No searched device");
2296 adapter_proxy = _bt_get_adapter_proxy();
2297 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2299 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2300 g_variant_new("(s)", address),
2301 G_DBUS_CALL_FLAGS_NONE,
2306 if (error != NULL) {
2307 BT_ERR("CreateDevice Fail: %s", error->message);
2308 g_error_free(error);
2311 g_variant_unref(result);
2313 object_path = _bt_get_device_object_path(address);
2315 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2317 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2318 NULL, BT_BLUEZ_NAME,
2319 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2320 g_free(object_path);
2321 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2323 g_dbus_proxy_call(proxy, "ConnectProfile",
2324 g_variant_new("(s)", uuid),
2325 G_DBUS_CALL_FLAGS_NONE,
2326 BT_MAX_DBUS_TIMEOUT,
2328 (GAsyncReadyCallback)cb,
2331 return BLUETOOTH_ERROR_NONE;
2334 int _bt_disconnect_all(char *address)
2336 int ret = BLUETOOTH_ERROR_NONE;
2339 GDBusConnection *conn;
2340 GVariant *result = NULL;
2344 conn = _bt_gdbus_get_system_gconn();
2345 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2347 object_path = _bt_get_device_object_path(address);
2348 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2350 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2351 NULL, BT_BLUEZ_NAME,
2352 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2353 g_free(object_path);
2354 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2356 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2358 G_DBUS_CALL_FLAGS_NONE,
2363 BT_ERR("Dbus Call Error:[%s]", err->message);
2365 ret = BLUETOOTH_ERROR_INTERNAL;
2368 g_object_unref(proxy);
2370 g_variant_unref(result);
2375 int _bt_disconnect_profile(char *address, char *uuid,
2376 void *cb, gpointer func_data)
2380 GDBusConnection *conn;
2382 conn = _bt_gdbus_get_system_gconn();
2383 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2385 object_path = _bt_get_device_object_path(address);
2386 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2388 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2389 NULL, BT_BLUEZ_NAME,
2390 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2391 g_free(object_path);
2392 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2394 g_dbus_proxy_call(proxy, "DisconnectProfile",
2395 g_variant_new("(s)", uuid),
2396 G_DBUS_CALL_FLAGS_NONE,
2397 BT_MAX_DBUS_TIMEOUT,
2399 (GAsyncReadyCallback)cb,
2402 return BLUETOOTH_ERROR_NONE;
2405 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2406 int low_threshold, int in_range_threshold, int high_threshold)
2408 int ret = BLUETOOTH_ERROR_NONE;
2410 GVariant *result = NULL;
2411 GError *error = NULL;
2412 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2414 BT_CHECK_PARAMETER(bd_addr, return);
2415 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2416 bd_addr->addr[0], bd_addr->addr[1],
2417 bd_addr->addr[2], bd_addr->addr[3],
2418 bd_addr->addr[4], bd_addr->addr[5],
2420 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2421 in_range_threshold, high_threshold);
2423 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2425 proxy = _bt_get_adapter_proxy();
2426 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2428 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2429 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2430 G_DBUS_CALL_FLAGS_NONE,
2434 if (error != NULL) {
2435 BT_ERR("Dbus Call Error:[%s]", error->message);
2436 g_error_free(error);
2437 ret = BLUETOOTH_ERROR_INTERNAL;
2441 g_variant_unref(result);
2446 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2449 int ret = BLUETOOTH_ERROR_NONE;
2451 GVariant *result = NULL;
2452 GError *error = NULL;
2453 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2455 BT_CHECK_PARAMETER(bd_addr, return);
2456 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2457 bd_addr->addr[0], bd_addr->addr[1],
2458 bd_addr->addr[2], bd_addr->addr[3],
2459 bd_addr->addr[4], bd_addr->addr[5],
2462 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2464 proxy = _bt_get_adapter_proxy();
2465 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2467 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2468 g_variant_new("(si)", address, link_type),
2469 G_DBUS_CALL_FLAGS_NONE,
2474 if (error != NULL) {
2475 BT_ERR("Dbus Call Error:[%s]", error->message);
2476 g_error_free(error);
2477 ret = BLUETOOTH_ERROR_INTERNAL;
2481 g_variant_unref(result);
2486 static int __bt_le_set_conn_parameter(const char *address,
2487 float interval_min, float interval_max,
2488 guint16 latency, guint16 time_out)
2490 gchar *device_path = NULL;
2491 GError *error = NULL;
2492 GDBusProxy *device_proxy = NULL;
2493 GDBusConnection *conn;
2495 guint32 min, max, to;
2496 int ret = BLUETOOTH_ERROR_NONE;
2498 BT_CHECK_PARAMETER(address, return);
2500 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2501 interval_min, interval_max, latency, time_out);
2503 device_path = _bt_get_device_object_path((char *)address);
2505 if (device_path == NULL) {
2506 BT_ERR("device_path NULL");
2507 ret = BLUETOOTH_ERROR_INTERNAL;
2511 conn = _bt_gdbus_get_system_gconn();
2513 BT_ERR("conn NULL");
2514 ret = BLUETOOTH_ERROR_INTERNAL;
2518 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2519 NULL, BT_BLUEZ_NAME,
2520 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2522 g_free(device_path);
2523 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2525 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2526 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2527 to = time_out / BT_LE_CONN_TO_SPLIT;
2529 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2530 g_variant_new("(uuuu)", min, max, latency, to),
2531 G_DBUS_CALL_FLAGS_NONE,
2536 g_object_unref(device_proxy);
2537 if (reply == NULL) {
2539 BT_ERR("Error %s[%s]", error->message, address);
2540 if (g_strrstr(error->message, "In Progress"))
2541 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2543 ret = BLUETOOTH_ERROR_INTERNAL;
2544 g_error_free(error);
2548 g_variant_unref(reply);
2554 static void __bt_le_conn_param_free(void *data)
2556 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2558 BT_DBG("%s", param->sender);
2559 g_free(param->sender);
2563 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2565 bt_le_conn_param_t *parama= (bt_le_conn_param_t *)a;
2566 bt_le_conn_param_t *paramb= (bt_le_conn_param_t *)b;
2568 return parama->key > paramb->key;
2571 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2574 bt_connected_le_dev_t *dev;
2579 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2582 if (g_strcmp0(dev->address, address) == 0)
2588 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2591 bt_le_conn_param_t *param = NULL;
2593 if (!dev || !sender)
2596 for (l = dev->senders; l; l = g_slist_next(l)) {
2598 if (g_strcmp0(param->sender, sender) == 0)
2605 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2606 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2608 bt_connected_le_dev_t *dev = NULL;
2609 bt_le_conn_param_t *param = NULL;
2610 bt_le_conn_param_t *data = NULL;
2612 if (!address || !sender)
2613 return BLUETOOTH_ERROR_INVALID_PARAM;
2615 dev = __bt_get_le_connected_dev_info(address);
2617 return BLUETOOTH_ERROR_INTERNAL;
2619 param = __bt_get_le_conn_param_info(dev, sender);
2621 data = g_malloc0(sizeof(bt_le_conn_param_t));
2622 data->sender = g_strdup(sender);
2623 data->interval_min = interval_min;
2624 data->interval_max = interval_max;
2625 data->latency = latency;
2626 data->time_out = time_out;
2627 data->key = interval_min + (interval_max - interval_min)/2;
2629 if (param == NULL) {
2630 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2631 dev->senders = g_slist_append(dev->senders, data);
2633 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2634 dev->senders = g_slist_remove(dev->senders, param);
2635 g_free(param->sender);
2637 dev->senders = g_slist_append(dev->senders, data);
2640 /* Sorting. First element have the minimum interval */
2641 dev->senders = g_slist_sort(dev->senders,
2642 (GCompareFunc)__bt_compare_le_conn_param_key);
2644 return BLUETOOTH_ERROR_NONE;
2647 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2649 bt_connected_le_dev_t *dev = NULL;
2650 bt_le_conn_param_t *param = NULL;
2652 if (!address || !sender)
2653 return BLUETOOTH_ERROR_INVALID_PARAM;
2655 dev = __bt_get_le_connected_dev_info(address);
2657 return BLUETOOTH_ERROR_INTERNAL;
2659 param = __bt_get_le_conn_param_info(dev, sender);
2661 BT_DBG("Remove param %s %s ", address, sender);
2662 dev->senders = g_slist_remove(dev->senders, param);
2663 g_free(param->sender);
2667 *is_removed = FALSE;
2669 return BLUETOOTH_ERROR_NONE;
2672 int _bt_remove_all_le_conn_param_info(const char *sender)
2675 bt_connected_le_dev_t *dev = NULL;
2676 bt_le_conn_param_t *param = NULL;
2677 gboolean is_removed = FALSE;
2678 char *sender_new = NULL;
2679 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2680 int ret = BLUETOOTH_ERROR_NONE;
2683 return BLUETOOTH_ERROR_INVALID_PARAM;
2685 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2687 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2690 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2691 g_slist_length(dev->senders));
2692 if (g_slist_length(dev->senders) > 0) {
2693 param = dev->senders->data;
2694 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2695 param->interval_min, param->interval_max);
2697 if (dev->interval_min != param->interval_min ||
2698 dev->interval_max != param->interval_max) {
2699 sender_new = g_strdup(param->sender);
2701 _bt_convert_addr_string_to_type(addr, dev->address);
2702 ret = _bt_le_conn_update(sender_new, addr,
2703 param->interval_min, param->interval_max,
2704 param->latency, param->time_out);
2707 if (ret != BLUETOOTH_ERROR_NONE)
2708 BT_ERR("Unable to set le connection parameter");
2711 BT_INFO("Set the default interval");
2713 bluetooth_le_connection_param_t param = { 0 };
2714 _bt_get_le_connection_parameter(
2715 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2718 ret = __bt_le_set_conn_parameter(dev->address,
2719 param.interval_min, param.interval_max,
2720 param.latency, param.timeout);
2721 if (ret == BLUETOOTH_ERROR_NONE) {
2722 dev->interval_min = param.interval_min;
2723 dev->interval_max = param.interval_max;
2732 void _bt_add_le_connected_dev_info(const char *address)
2734 bt_connected_le_dev_t *dev = NULL;
2739 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2740 dev->address = g_strdup(address);
2742 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2747 void _bt_remove_le_connected_dev_info(const char *address)
2749 bt_connected_le_dev_t *dev = NULL;
2754 dev = __bt_get_le_connected_dev_info(address);
2758 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2759 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2760 g_free(dev->address);
2766 int _bt_le_conn_update(const char *sender,
2767 unsigned char *device_address,
2768 float interval_min, float interval_max,
2769 guint16 latency, guint16 time_out)
2771 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2772 guint32 min_supervision_to;
2773 bt_connected_le_dev_t *dev = NULL;
2774 bt_le_conn_param_t *param = NULL;
2775 gboolean is_removed = FALSE;
2776 int ret = BLUETOOTH_ERROR_NONE;
2778 BT_CHECK_PARAMETER(device_address, return);
2780 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2781 sender, interval_min, interval_max, latency, time_out);
2783 if (interval_min > interval_max ||
2784 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2785 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2786 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2790 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2791 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2792 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2796 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2797 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2802 * The Supervision_Timeout in milliseconds shall be larger than
2803 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2804 * where Conn_Interval_Max is given in milliseconds.
2806 min_supervision_to = (1 + latency) * interval_max * 2;
2807 if (time_out <= min_supervision_to) {
2808 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2812 _bt_convert_addr_type_to_string(address, device_address);
2813 BT_DBG("Remote device address: %s", address);
2815 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2817 dev = __bt_get_le_connected_dev_info(address);
2819 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2823 if (g_slist_length(dev->senders) == 1)
2826 param = dev->senders->data;
2828 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2830 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2831 BT_DBG("Skip due to same interval");
2835 interval_min = param->interval_min;
2836 interval_max = param->interval_max;
2840 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2842 if (ret != BLUETOOTH_ERROR_NONE) {
2843 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2847 dev->interval_min = interval_min;
2848 dev->interval_max = interval_max;
2854 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2855 bluetooth_device_pin_code_t *pin_code)
2857 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2859 bt_pin_code_info_t *pin_info = NULL;
2861 BT_CHECK_PARAMETER(device_address, return);
2862 BT_CHECK_PARAMETER(pin_code, return);
2863 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2864 BLUETOOTH_ERROR_NO_RESOURCES);
2866 _bt_convert_addr_type_to_string(address, device_address->addr);
2868 for (l = pin_info_list; l != NULL; l = l->next) {
2871 if (g_strcmp0(pin_info->address, address) == 0) {
2872 g_free(pin_info->pin_code);
2873 pin_info->pin_code = g_strdup(pin_code->pin_code);
2874 return BLUETOOTH_ERROR_NONE;
2878 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2879 pin_info->address = g_strdup(address);
2880 pin_info->pin_code = g_strdup(pin_code->pin_code);
2881 pin_info_list = g_slist_append(pin_info_list, pin_info);
2883 return BLUETOOTH_ERROR_NONE;
2886 gint __bt_compare_address(gpointer *a, gpointer *b)
2888 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2889 char *address = (char *)b;
2890 return g_strcmp0(pin_info->address, address);
2893 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2895 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2897 bt_pin_code_info_t *pin_info = NULL;
2899 BT_CHECK_PARAMETER(device_address, return);
2901 _bt_convert_addr_type_to_string(address, device_address->addr);
2903 l = g_slist_find_custom(pin_info_list, address,
2904 (GCompareFunc)__bt_compare_address);
2908 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2909 g_free(pin_info->address);
2910 g_free(pin_info->pin_code);
2914 return BLUETOOTH_ERROR_NONE;
2917 int _bt_get_device_pin_code(const char *address, char *pin_code)
2921 BT_CHECK_PARAMETER(address, return);
2922 BT_CHECK_PARAMETER(pin_code, return);
2924 for (l = pin_info_list; l != NULL; l = l->next) {
2925 bt_pin_code_info_t *pin_info = l->data;
2927 if (g_strcmp0(pin_info->address, address) == 0) {
2928 g_strlcpy(pin_code, pin_info->pin_code,
2929 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2931 return BLUETOOTH_ERROR_NONE;
2935 return BLUETOOTH_ERROR_NOT_FOUND;
2938 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2939 bluetooth_le_connection_param_t *param)
2942 return BLUETOOTH_ERROR_INVALID_PARAM;
2944 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2945 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2946 return BLUETOOTH_ERROR_INVALID_PARAM;
2948 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2951 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2952 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2953 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2954 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2955 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2958 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2959 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2960 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2961 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2962 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2965 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2966 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2967 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2968 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2969 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2973 BT_ERR("Unhandled mode : %d", mode);
2977 return BLUETOOTH_ERROR_NONE;
2980 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2981 guint trusted_profile_flag, guint *trusted)
2987 case TRUSTED_PROFILE_PBAP:
2988 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2989 trust_profile = trusted_profile_flag & (1 << 1);
2991 return BLUETOOTH_ERROR_NOT_SUPPORT;
2993 case TRUSTED_PROFILE_MAP:
2994 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2995 trust_profile = trusted_profile_flag & (1 << 3);
2997 return BLUETOOTH_ERROR_NOT_SUPPORT;
2999 case TRUSTED_PROFILE_SAP:
3000 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
3001 trust_profile = trusted_profile_flag & (1 << 5);
3003 return BLUETOOTH_ERROR_NOT_SUPPORT;
3005 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
3006 *trusted = trusted_profile_flag;
3007 return BLUETOOTH_ERROR_NONE;
3009 return BLUETOOTH_ERROR_NOT_SUPPORT;
3015 return BLUETOOTH_ERROR_NONE;
3018 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
3019 guint restricted_profile_flag, guint *restricted)
3021 int restrict_profile;
3022 *restricted = FALSE;
3025 case RESTRICTED_PROFILE_HFP_HS:
3026 restrict_profile = restricted_profile_flag & (1 << 0);
3028 case RESTRICTED_PROFILE_A2DP:
3029 restrict_profile = restricted_profile_flag & (1 << 2);
3032 return BLUETOOTH_ERROR_NOT_SUPPORT;
3035 if (restrict_profile)
3038 return BLUETOOTH_ERROR_NONE;
3041 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
3044 case TRUSTED_PROFILE_PBAP:
3045 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
3046 case TRUSTED_PROFILE_MAP:
3047 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
3048 case TRUSTED_PROFILE_SAP:
3049 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
3050 case TRUSTED_PROFILE_ALL:
3057 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
3060 case RESTRICTED_PROFILE_HFP_HS:
3061 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
3062 case RESTRICTED_PROFILE_A2DP:
3063 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
3069 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
3071 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
3072 return TRUSTED_PROFILE_PBAP;
3073 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
3074 return TRUSTED_PROFILE_MAP;
3075 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
3076 return TRUSTED_PROFILE_SAP;
3078 return 0; /* 0 - Unknown Profile */
3081 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
3082 bluetooth_trusted_profile_t profile, gboolean trust)
3084 int ret = BLUETOOTH_ERROR_NONE;
3085 GDBusConnection *conn;
3087 GError *error = NULL;
3088 char *device_path = NULL;
3090 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3093 BT_CHECK_PARAMETER(bd_addr, return);
3094 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3095 bd_addr->addr[0], bd_addr->addr[1],
3096 bd_addr->addr[2], bd_addr->addr[3],
3097 bd_addr->addr[4], bd_addr->addr[5],
3100 conn = _bt_gdbus_get_system_gconn();
3101 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3103 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3105 device_path = _bt_get_device_object_path(address);
3106 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3108 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3109 NULL, BT_BLUEZ_NAME, device_path,
3110 BT_DEVICE_INTERFACE, NULL, NULL);
3112 g_free(device_path);
3113 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3115 uuid = _bt_get_trusted_profile_uuid(profile);
3117 g_object_unref(proxy);
3118 return BLUETOOTH_ERROR_NOT_SUPPORT;
3121 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3122 g_variant_new("(sb)", uuid, trust),
3123 G_DBUS_CALL_FLAGS_NONE, -1,
3125 g_object_unref(proxy);
3127 if (reply == NULL) {
3128 BT_ERR("Failed to Set Profile Trusted");
3129 ret = BLUETOOTH_ERROR_INTERNAL;
3131 BT_ERR("Error %s[%s]", error->message, address);
3132 g_error_free(error);
3136 g_variant_unref(reply);
3143 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3144 bluetooth_trusted_profile_t profile, guint *trust)
3146 int ret = BLUETOOTH_ERROR_NONE;
3147 GDBusConnection *conn;
3149 GError *error = NULL;
3150 char *device_path = NULL;
3151 guint trusted_profile_flag;
3152 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3155 BT_CHECK_PARAMETER(bd_addr, return);
3156 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3157 bd_addr->addr[0], bd_addr->addr[1],
3158 bd_addr->addr[2], bd_addr->addr[3],
3159 bd_addr->addr[4], bd_addr->addr[5],
3162 conn = _bt_gdbus_get_system_gconn();
3163 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3165 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3167 device_path = _bt_get_device_object_path(address);
3168 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3170 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3171 NULL, BT_BLUEZ_NAME, device_path,
3172 BT_PROPERTIES_INTERFACE, NULL, NULL);
3174 g_free(device_path);
3175 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3177 reply = g_dbus_proxy_call_sync(proxy, "Get",
3178 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3179 G_DBUS_CALL_FLAGS_NONE, -1,
3181 g_object_unref(proxy);
3183 if (reply == NULL) {
3184 BT_ERR("Failed to Get Profile Trusted");
3185 ret = BLUETOOTH_ERROR_INTERNAL;
3187 BT_ERR("Error %s[%s]", error->message, address);
3188 g_error_free(error);
3193 g_variant_get(reply, "(v)", &temp);
3194 trusted_profile_flag = g_variant_get_uint32(temp);
3195 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3197 ret = _bt_get_trusted_profile_from_flag(profile,
3198 trusted_profile_flag, trust);
3199 g_variant_unref(temp);
3200 g_variant_unref(reply);
3203 BT_DBG("TRUST %d", *trust);
3207 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3208 bluetooth_restricted_profile_t profile, gboolean restricted)
3210 int ret = BLUETOOTH_ERROR_NONE;
3211 GDBusConnection *conn;
3213 GError *error = NULL;
3214 char *device_path = NULL;
3216 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3219 BT_CHECK_PARAMETER(bd_addr, return);
3220 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3221 bd_addr->addr[0], bd_addr->addr[1],
3222 bd_addr->addr[2], bd_addr->addr[3],
3223 bd_addr->addr[4], bd_addr->addr[5],
3224 profile, restricted);
3226 conn = _bt_gdbus_get_system_gconn();
3227 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3229 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3231 device_path = _bt_get_device_object_path(address);
3232 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3234 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3235 NULL, BT_BLUEZ_NAME, device_path,
3236 BT_DEVICE_INTERFACE, NULL, NULL);
3238 g_free(device_path);
3239 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3241 uuid = _bt_get_restricted_profile_uuid(profile);
3243 g_object_unref(proxy);
3244 return BLUETOOTH_ERROR_NOT_SUPPORT;
3247 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3248 g_variant_new("(sb)", uuid, restricted),
3249 G_DBUS_CALL_FLAGS_NONE, -1,
3251 g_object_unref(proxy);
3253 if (reply == NULL) {
3254 BT_ERR("Failed to Set Profile Restricted");
3255 ret = BLUETOOTH_ERROR_INTERNAL;
3257 BT_ERR("Error %s[%s]", error->message, address);
3258 g_error_free(error);
3262 g_variant_unref(reply);
3269 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3270 bluetooth_restricted_profile_t profile, guint *restricted)
3272 int ret = BLUETOOTH_ERROR_NONE;
3273 GDBusConnection *conn;
3275 GError *error = NULL;
3276 char *device_path = NULL;
3277 guint restricted_profile_flag;
3278 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3281 BT_CHECK_PARAMETER(bd_addr, return);
3282 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3283 bd_addr->addr[0], bd_addr->addr[1],
3284 bd_addr->addr[2], bd_addr->addr[3],
3285 bd_addr->addr[4], bd_addr->addr[5],
3286 profile, *restricted);
3288 conn = _bt_gdbus_get_system_gconn();
3289 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3291 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3293 device_path = _bt_get_device_object_path(address);
3294 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3296 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3297 NULL, BT_BLUEZ_NAME, device_path,
3298 BT_PROPERTIES_INTERFACE, NULL, NULL);
3300 g_free(device_path);
3301 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3303 reply = g_dbus_proxy_call_sync(proxy, "Get",
3304 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3305 G_DBUS_CALL_FLAGS_NONE, -1,
3307 g_object_unref(proxy);
3309 if (reply == NULL) {
3310 BT_ERR("Failed to Get Profile Restricted");
3311 ret = BLUETOOTH_ERROR_INTERNAL;
3313 BT_ERR("Error %s[%s]", error->message, address);
3314 g_error_free(error);
3319 g_variant_get(reply, "(v)", &temp);
3320 restricted_profile_flag = g_variant_get_uint32(temp);
3321 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3323 ret = _bt_get_restricted_profile_from_flag(profile,
3324 restricted_profile_flag, restricted);
3325 g_variant_unref(temp);
3326 g_variant_unref(reply);
3329 BT_DBG("TRUST %d", *restricted);
3333 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3337 GVariant *out_param1;
3338 request_info_t *req_info;
3339 GVariant *val = NULL;
3340 GVariant *param = NULL;
3343 bluetooth_device_address_t device_addr = { {0} };
3344 int result = BLUETOOTH_ERROR_NONE;
3347 val = g_dbus_proxy_call_finish(proxy, res, &err);
3349 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3350 if (req_info == NULL) {
3351 BT_ERR("req_info == NULL");
3352 g_object_unref(proxy);
3353 att_mtu_req_info->device_proxy = NULL;
3358 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3360 if (g_strrstr(err->message, "NotSupported")) {
3361 BT_INFO("Connection Not Supported");
3362 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3363 } else if (g_strrstr(err->message, "NotConnected")) {
3364 BT_INFO("Not connected");
3365 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3366 } else if (g_strrstr(err->message, "InvalidArguments")) {
3367 BT_INFO("Not connected");
3368 result = BLUETOOTH_ERROR_INVALID_PARAM;
3370 BT_DBG("Default case");
3371 result = BLUETOOTH_ERROR_INTERNAL;
3375 g_object_unref(proxy);
3376 att_mtu_req_info->device_proxy = NULL;
3378 if (result != BLUETOOTH_ERROR_NONE)
3382 g_variant_get(val, "(qy)", &mtu, &status);
3383 g_variant_unref(val);
3386 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3388 param = g_variant_new("(isqy)",
3390 att_mtu_req_info->addr,
3394 /* Send the event to application */
3395 _bt_send_event(BT_DEVICE_EVENT,
3396 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3400 if (req_info->context == NULL)
3403 _bt_convert_addr_string_to_type(device_addr.addr,
3404 (const char *)att_mtu_req_info->addr);
3406 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3407 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3408 g_dbus_method_invocation_return_value(req_info->context,
3409 g_variant_new("(iv)", result, out_param1));
3411 _bt_delete_request_list(req_info->req_id);
3416 g_free(att_mtu_req_info->addr);
3417 g_free(att_mtu_req_info);
3418 att_mtu_req_info = NULL;
3423 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3426 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3427 gchar *device_path = NULL;
3428 GDBusProxy *adapter_proxy;
3429 GDBusProxy *device_proxy;
3430 GDBusConnection *conn;
3431 int ret = BLUETOOTH_ERROR_NONE;
3433 BT_CHECK_PARAMETER(device_address, return);
3435 if (att_mtu_req_info) {
3436 BT_ERR("ATT MTU request in progress");
3437 return BLUETOOTH_ERROR_DEVICE_BUSY;
3440 conn = _bt_gdbus_get_system_gconn();
3441 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3443 adapter_proxy = _bt_get_adapter_proxy();
3444 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3446 _bt_convert_addr_type_to_string(address, device_address->addr);
3448 BT_DBG("Remote device address: %s", address);
3450 device_path = _bt_get_device_object_path(address);
3452 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3454 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3455 NULL, BT_BLUEZ_NAME,
3456 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3457 g_free(device_path);
3458 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3460 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3461 att_mtu_req_info->addr = (char *)g_strdup(address);
3462 att_mtu_req_info->req_id = request_id;
3463 att_mtu_req_info->device_proxy = device_proxy;
3465 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3466 g_variant_new("(q)", mtu),
3467 G_DBUS_CALL_FLAGS_NONE,
3468 BT_MAX_DBUS_TIMEOUT,
3470 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3476 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3479 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3480 char *object_path = NULL;
3482 GDBusProxy *device_proxy;
3483 GError *error = NULL;
3485 GDBusConnection *conn;
3486 GVariant *result = NULL;
3488 BT_CHECK_PARAMETER(device_address, return);
3490 conn = _bt_gdbus_get_system_gconn();
3491 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3493 _bt_convert_addr_type_to_string(address, device_address->addr);
3495 object_path = _bt_get_device_object_path(address);
3496 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3498 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3499 NULL, BT_BLUEZ_NAME, object_path,
3500 BT_PROPERTIES_INTERFACE, NULL, NULL);
3501 g_free(object_path);
3502 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3504 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3505 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3506 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3508 ERR("DBus Error : %s", error->message);
3509 g_clear_error(&error);
3510 g_object_unref(device_proxy);
3511 return BLUETOOTH_ERROR_INTERNAL;
3513 g_variant_get(result, "(v)", &value);
3514 *mtu = g_variant_get_uint16(value);
3515 BT_DBG("ATT MTU : %d", *mtu);
3516 g_variant_unref(value);
3517 g_variant_unref(result);
3520 g_object_unref(device_proxy);
3521 return BLUETOOTH_ERROR_NONE;
3524 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3525 bluetooth_device_address_t *id_address)
3527 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3528 gchar *device_path = NULL;
3529 const gchar *idaddress = NULL;
3530 GDBusProxy *device_proxy;
3531 GError *error = NULL;
3532 GVariant *result = NULL;
3533 GDBusConnection *conn;
3534 int ret = BLUETOOTH_ERROR_NONE;
3536 BT_CHECK_PARAMETER(device_address, return);
3538 conn = _bt_gdbus_get_system_gconn();
3539 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3541 _bt_convert_addr_type_to_string(address, device_address->addr);
3543 device_path = _bt_get_device_object_path(address);
3544 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3546 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3547 NULL, BT_BLUEZ_NAME,
3548 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3549 g_free(device_path);
3550 if (!device_proxy) {
3551 BT_ERR("Unable to get proxy");
3552 return BLUETOOTH_ERROR_INTERNAL;
3555 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3557 G_DBUS_CALL_FLAGS_NONE,
3562 if (result == NULL) {
3563 BT_ERR("Failed to get device ID address");
3564 if (error != NULL) {
3565 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3566 g_error_free(error);
3568 g_object_unref(device_proxy);
3569 return BLUETOOTH_ERROR_INTERNAL;
3572 g_variant_get(result , "(s)", &idaddress);
3573 g_variant_unref(result);
3575 if (idaddress == NULL) {
3576 BT_ERR("No paired device");
3577 g_object_unref(device_proxy);
3578 return BLUETOOTH_ERROR_NOT_PAIRED;
3581 BT_DBG("ID Address:%s", idaddress);
3584 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3586 ret = BLUETOOTH_ERROR_INTERNAL;
3588 g_object_unref(device_proxy);
3593 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3595 GapAgentPrivate *agent = _bt_get_adapter_agent();
3596 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3598 if (authentication_reply)
3599 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3601 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3603 BT_DBG("BT_PASSKEY_REPLY");
3604 return BLUETOOTH_ERROR_NONE;
3607 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3609 GapAgentPrivate *agent = _bt_get_adapter_agent();
3610 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3612 if (confirmation_reply)
3613 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3615 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3617 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3618 return BLUETOOTH_ERROR_NONE;