2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-event.h"
33 #include "bt-service-device.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
41 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
42 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
43 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
45 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
46 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
47 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
49 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
50 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
51 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
52 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
54 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
56 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
60 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
64 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
68 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
75 GDBusProxy *device_proxy;
76 GDBusProxy *adapter_proxy;
78 unsigned short conn_type;
79 gboolean bonding_wo_discovery;
89 bluetooth_device_address_t bd_addr;
90 gboolean auto_connect;
91 } bt_pending_le_conn_info_s;
98 } bt_connected_le_dev_t;
107 } bt_le_conn_param_t;
109 gboolean is_device_creating;
110 bt_funcion_data_t *bonding_info;
111 bt_funcion_data_t *searching_info;
112 bt_funcion_data_t *att_mtu_req_info;
114 static GSList *le_connected_dev_list = NULL;
115 static GSList *pin_info_list = NULL;
116 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
117 static guint pending_le_conn_timer_id = 0;
119 /* This HID Mouse does not support pairing precedure. need to skip it. */
120 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
122 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
125 static int __bt_retry_bond(void);
128 /*static void __bt_decline_pair_request()
130 GVariant *out_param1;
131 GVariant *out_param2;
132 request_info_t *req_info;
133 bluetooth_device_info_t dev_info;
134 bt_remote_dev_info_t *remote_dev_info;
135 GVariant *uuid_list, *manufacture_data;
140 req_info = _bt_get_request_info(bonding_info->req_id);
141 if (req_info == NULL) {
142 BT_ERR("req_info == NULL");
145 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
147 BT_DBG("bonding_info is NULL");
152 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
153 remote_dev_info->uuids, remote_dev_info->uuid_count,
156 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
157 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
160 param = g_variant_new("isunsbuba{s}na{y})",
161 bonding_info->result,
163 remote_dev_info->class,
164 remote_dev_info->rssi,
165 remote_dev_info->name,
166 remote_dev_info->paired,
167 remote_dev_info->connected,
168 remote_dev_info->trust,
170 remote_dev_info->manufacturer_data_len,
174 //Send the event to application
175 if (remote_dev_info != NULL) {
176 _bt_send_event(BT_ADAPTER_EVENT,
177 BLUETOOTH_EVENT_BONDING_FINISHED,
180 _bt_free_device_info(remote_dev_info);
183 if (req_info->context == NULL)
186 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
187 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
190 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
191 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
193 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
194 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
197 g_dbus_method_invocation_return_value(req_info->context,
198 g_variant_new("@ayi", out_param1, out_param2));
201 _bt_delete_request_list(req_info->req_id);
204 g_free(bonding_info->addr);
205 g_free(bonding_info);
211 #ifdef TIZEN_PROFILE_WEARABLE
212 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
216 retv_if(user_data == NULL, FALSE);
218 b = (bundle *)user_data;
220 ret = syspopup_launch("bt-syspopup", b);
222 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
224 BT_DBG("Hurray!!! Finally Popup launched");
227 return (ret < 0) ? TRUE : FALSE;
230 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
235 GDBusConnection *conn;
237 conn = _bt_gdbus_get_system_gconn();
245 bundle_add(b, "event-type", "unable-to-pairing");
247 if (result == BLUETOOTH_ERROR_TIMEOUT)
248 bundle_add(b, "error", "timeout");
249 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
250 bundle_add(b, "error", "authfailed");
252 bundle_add(b, "error", "error");
254 ret = syspopup_launch("bt-syspopup", b);
256 BT_ERR("Popup launch failed...retry %d \n", ret);
257 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
268 gboolean _bt_is_device_creating(void)
270 return is_device_creating;
273 gboolean _bt_is_bonding_device_address(const char *address)
275 if (bonding_info == NULL || bonding_info->addr == NULL)
278 if (g_strcmp0(bonding_info->addr, address) == 0) {
279 BT_DBG("[%s] is bonding device", address);
283 BT_DBG("[%s] is NOT bonding device", address);
287 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
289 ret_if(bonding_info == NULL);
290 bonding_info->is_autopair = is_autopair;
293 void __bt_cancel_search_service_done(void)
295 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
296 request_info_t *req_info;
297 bluetooth_device_info_t dev_info;
298 GVariant *out_param1;
300 ret_if(searching_info == NULL);
302 req_info = _bt_get_request_info(searching_info->req_id);
303 if (req_info == NULL) {
304 BT_ERR("req_info == NULL");
308 if (req_info->context == NULL)
311 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
312 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
313 searching_info->addr);
315 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
316 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
319 g_dbus_method_invocation_return_value(req_info->context,
320 g_variant_new("(iv)", result, out_param1));
322 _bt_delete_request_list(req_info->req_id);
326 g_free(searching_info->addr);
327 g_free(searching_info);
328 searching_info = NULL;
331 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
333 ret_if(value == NULL);
334 ret_if(info == NULL);
336 info->uuid_count = g_variant_get_size(value);
337 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
340 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
342 char *object_path = NULL;
343 bt_remote_dev_info_t *dev_info;
345 retv_if(address == NULL, NULL);
347 object_path = _bt_get_device_object_path(address);
348 retv_if(object_path == NULL, NULL);
350 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
356 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
357 const char *object_path)
359 bt_remote_dev_info_t *dev_info;
360 GDBusProxy *adapter_proxy;
361 GDBusProxy *device_proxy;
366 GDBusConnection *conn;
367 GError *error = NULL;
368 GVariant *result = NULL;
369 GVariantIter *value_iter;
373 adapter_proxy = _bt_get_adapter_proxy();
374 retv_if(adapter_proxy == NULL, NULL);
376 retv_if(object_path == NULL, NULL);
378 conn = _bt_gdbus_get_system_gconn();
380 BT_ERR("conn == NULL");
384 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
385 NULL, BT_BLUEZ_NAME, object_path,
386 BT_PROPERTIES_INTERFACE, NULL, NULL);
388 retv_if(device_proxy == NULL, NULL);
390 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
391 g_variant_new("(s)", BT_DEVICE_INTERFACE),
392 G_DBUS_CALL_FLAGS_NONE,
397 g_object_unref(device_proxy);
399 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
401 if (result != NULL) {
402 g_variant_get(result , "(@a{sv})", &value);
403 g_variant_unref(result);
405 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
407 g_variant_get(tmp_value, "s", &name);
408 g_variant_unref(tmp_value);
410 DBG_SECURE("Alias Name [%s]", name);
412 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
413 g_variant_get(tmp_value, "s", &name);
414 g_variant_unref(tmp_value);
417 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
419 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
420 g_variant_unref(tmp_value);
422 dev_info->is_alias_set = FALSE;
424 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
426 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
428 dev_info->class = g_variant_get_uint32(tmp_value);
429 g_variant_unref(tmp_value);
433 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
435 dev_info->connected = g_variant_get_byte(tmp_value);
436 g_variant_unref(tmp_value);
438 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
439 BT_DBG("connected link : %d", dev_info->connected);
441 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
443 dev_info->trust = g_variant_get_boolean(tmp_value);
444 g_variant_unref(tmp_value);
446 dev_info->trust = FALSE;
448 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
450 dev_info->paired = g_variant_get_boolean(tmp_value);
451 g_variant_unref(tmp_value);
453 dev_info->paired = FALSE;
455 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
457 dev_info->rssi = g_variant_get_int16(tmp_value);
458 g_variant_unref(tmp_value);
462 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
464 dev_info->addr_type = g_variant_get_byte(tmp_value);
465 g_variant_unref(tmp_value);
467 dev_info->addr_type = 0;
469 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
471 __bt_get_uuids(tmp_value, dev_info);
472 g_variant_unref(tmp_value);
475 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerDataLen", G_VARIANT_TYPE_UINT16);
477 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
478 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
479 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
480 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
482 g_variant_unref(tmp_value);
484 dev_info->manufacturer_data_len = 0;
486 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerData", G_VARIANT_TYPE_ARRAY);
488 if ((dev_info->manufacturer_data_len == 0) ||
489 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
490 BT_ERR("manufacturer data length doesn't match");
491 dev_info->manufacturer_data_len = 0;
492 dev_info->manufacturer_data = NULL;
494 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
495 g_variant_get(tmp_value, "ay", &value_iter);
496 while (g_variant_iter_loop(value_iter, "y", &m_value))
497 dev_info->manufacturer_data[i++] = m_value;
499 g_variant_unref(tmp_value);
501 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
502 dev_info->manufacturer_data_len = 0;
503 dev_info->manufacturer_data = NULL;
506 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
507 g_variant_get(tmp_value, "s", &address);
508 g_variant_unref(tmp_value);
510 dev_info->address = address;
511 dev_info->name = name;
512 g_variant_unref(value);
514 BT_ERR("result is NULL\n");
522 char *_bt_get_device_name(const char *bdaddress)
524 char *device_path = NULL;
525 const gchar *name = NULL;
526 gchar *dev_name = NULL;
528 GVariant *result = NULL;
530 GDBusProxy *device_proxy;
531 GDBusConnection *conn;
533 retv_if(bdaddress == NULL, NULL);
535 device_path = _bt_get_device_object_path((char *)bdaddress);
536 retv_if(device_path == NULL, NULL);
538 conn = _bt_gdbus_get_system_gconn();
539 retv_if(conn == NULL, NULL);
540 INFO_SECURE("Device_path %s", device_path);
541 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
542 NULL, BT_BLUEZ_NAME, device_path,
543 BT_PROPERTIES_INTERFACE, NULL, &err);
546 retv_if(device_proxy == NULL, NULL);
548 result = g_dbus_proxy_call_sync(device_proxy, "Get",
549 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
550 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
552 BT_ERR("DBus Error : %s", err->message);
556 g_variant_get(result, "(v)", &value);
557 name = g_variant_get_string(value, &name_len);
558 INFO_SECURE("Alias Name [%s]", name);
560 dev_name = g_strdup(name);
561 g_variant_unref(value);
562 g_variant_unref(result);
567 result = g_dbus_proxy_call_sync(device_proxy, "Get",
568 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
569 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
571 ERR("DBus Error : %s", err->message);
574 g_variant_get(result, "(v)", &value);
575 name = g_variant_get_string(value, &name_len);
576 INFO_SECURE("Name = %s", name);
578 dev_name = g_strdup(name);
579 g_variant_unref(value);
580 g_variant_unref(result);
584 g_object_unref(device_proxy);
588 static gboolean __ignore_auto_pairing_request(const char *address)
593 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
604 /* Get the LAP(Lower Address part) */
605 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
607 snprintf(lap_address, sizeof(lap_address), ",%s", address);
609 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
612 BT_ERR("fopen failed \n");
616 fseek(fp, 0, SEEK_END);
621 BT_ERR("Get file size failed \n");
626 buffer = g_malloc0(sizeof(char) * size);
627 result = fread((char *)buffer, 1, size, fp);
629 if (result != size) {
630 BT_ERR("Read Error\n");
635 BT_DBG("Buffer = %s\n", buffer);
637 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
643 /* Write the data and insert new device data */
644 for (i = 0; lines[i] != NULL; i++) {
645 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
646 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
648 lines[i] = temp_buffer;
651 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
653 /* Fix : NULL_RETURNS */
654 retv_if(buffer == NULL, FALSE);
656 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
659 BT_ERR("fopen failed \n");
664 BT_DBG("Buffer = %s\n", buffer);
665 fwrite(buffer, 1, strlen(buffer), fp);
675 static int __bt_cancel_bonding(void)
677 BT_CHECK_PARAMETER(bonding_info, return);
678 BT_CHECK_PARAMETER(bonding_info->addr, return);
679 /* First Cancel the ongoing pairing in bluez otherwise if we send
680 * pair request bluez will send inprogress and we again retry bond so
681 * this cycle continues */
683 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
684 NULL, G_DBUS_CALL_FLAGS_NONE,
687 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
689 return BLUETOOTH_ERROR_PARING_FAILED;
691 return BLUETOOTH_ERROR_NONE;
694 static int __bt_retry_bond(void)
696 BT_CHECK_PARAMETER(bonding_info, return);
697 BT_CHECK_PARAMETER(bonding_info->addr, return);
699 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
700 g_variant_new("(y)", bonding_info->conn_type),
701 G_DBUS_CALL_FLAGS_NONE,
704 (GAsyncReadyCallback)__bt_bond_device_cb,
707 return BLUETOOTH_ERROR_NONE;
711 static int __bt_remove_and_bond(void)
713 GDBusProxy *adapter_proxy;
714 GVariant *result = NULL;
716 char *device_path = NULL;
718 BT_CHECK_PARAMETER(bonding_info, return);
719 BT_CHECK_PARAMETER(bonding_info->addr, return);
721 adapter_proxy = _bt_get_adapter_proxy();
722 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
724 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
725 g_variant_new("(s)", bonding_info->addr),
726 G_DBUS_CALL_FLAGS_NONE,
731 return BLUETOOTH_ERROR_INTERNAL;
733 g_variant_get(result , "(o)", &device_path);
734 g_variant_unref(result);
736 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
738 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
739 g_variant_new("(o)", device_path),
740 G_DBUS_CALL_FLAGS_NONE,
745 if (result == NULL) {
747 BT_ERR("UnpairDevice Fail: %s", err->message);
750 return BLUETOOTH_ERROR_INTERNAL;
753 return __bt_retry_bond();
756 static int __bt_cancel_and_bond(void)
758 int ret = BLUETOOTH_ERROR_NONE;
760 ret = _bt_agent_reply_cancellation();
761 if (ret != BLUETOOTH_ERROR_NONE) {
762 BT_ERR("Fail to call reply cancellation");
765 return __bt_retry_bond();
769 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
772 int result = BLUETOOTH_ERROR_NONE;
775 GVariant *out_param1;
776 request_info_t *req_info;
777 bluetooth_device_info_t dev_info;
778 bt_remote_dev_info_t *remote_dev_info;
779 GVariant *manufacture_data;
782 /* Terminate ALL system popup */
783 syspopup_destroy_all();
785 reply = g_dbus_proxy_call_finish(proxy, res, &err);
787 g_variant_unref(reply);
789 is_device_creating = FALSE;
791 if (bonding_info == NULL) {
793 BT_ERR("bonding_info == NULL");
799 req_info = _bt_get_request_info(bonding_info->req_id);
800 if (req_info == NULL) {
801 BT_ERR("req_info == NULL");
806 g_dbus_error_strip_remote_error(err);
807 BT_ERR("Error occured in CreateBonding [%s]", err->message);
809 if (g_strrstr(err->message, "Already Exists")) {
810 BT_INFO("Existing Bond, remove and retry");
811 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
813 result = BLUETOOTH_ERROR_PARING_FAILED;
814 } else if (_bt_agent_is_canceled() ||
815 g_strrstr(err->message, "Authentication Canceled")) {
816 BT_INFO("Cancelled by USER");
817 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
818 } else if (g_strrstr(err->message, "Authentication Rejected")) {
820 result = BLUETOOTH_ERROR_ACCESS_DENIED;
821 } else if (g_strrstr(err->message, "In Progress")) {
822 BT_INFO("Bond in progress, cancel and retry");
823 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
825 result = BLUETOOTH_ERROR_PARING_FAILED;
826 } else if (g_strrstr(err->message, "Authentication Failed")) {
827 BT_INFO("Authentication Failed");
828 if (bonding_info->is_autopair == TRUE) {
829 _bt_set_autopair_status_in_bonding_info(FALSE);
830 __ignore_auto_pairing_request(bonding_info->addr);
832 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
833 } else if (g_strrstr(err->message, "Page Timeout")) {
834 BT_INFO("Page Timeout");
835 /* This is the special case
836 As soon as call bluetooth_bond_device, try to cancel bonding.
837 In this case, before completing to call 'CreatePairedDevice' method
838 the procedure is stopped. So 'Cancle' error is not return.
840 result = BLUETOOTH_ERROR_HOST_DOWN;
841 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
842 BT_INFO("Cancel already running bonding");
843 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
844 BT_INFO("Error while Cancelling bonding");
845 /* we need to unref proxy so continue */
847 result = BLUETOOTH_ERROR_INTERNAL;
848 } else if (g_strrstr(err->message, "Connection Timeout")) {
849 /* Pairing request timeout */
850 result = BLUETOOTH_ERROR_TIMEOUT;
851 } else if (g_strrstr(err->message, "Authentication Timeout")) {
852 /* Pairing request timeout */
853 result = BLUETOOTH_ERROR_TIMEOUT;
855 BT_DBG("Default case");
856 result = BLUETOOTH_ERROR_PARING_FAILED;
860 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
861 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
862 result == BLUETOOTH_ERROR_TIMEOUT ||
863 result == BLUETOOTH_ERROR_HOST_DOWN) {
864 #ifdef TIZEN_PROFILE_WEARABLE
865 int is_sw_running = 0;
867 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
868 BT_ERR("vconf_get_int for setup wizard state failed");
871 __bt_launch_unable_to_pairing_syspopup(result);
873 BT_ERR("Unable to pair");
875 bonding_info->result = result;
878 g_object_unref(proxy);
879 bonding_info->device_proxy = NULL;
881 if (result != BLUETOOTH_ERROR_NONE) {
882 if (bonding_info->bonding_wo_discovery) {
883 GDBusProxy *adapter_proxy;
884 GVariant *ret = NULL;
885 GError *error = NULL;
888 BT_ERR("Bond was tried without discovery. Remove it");
890 adapter_proxy = _bt_get_adapter_proxy();
891 if (adapter_proxy == NULL) {
892 BT_ERR("Cannot get adapter_proxy");
896 device_path = _bt_get_device_object_path(bonding_info->addr);
897 if (device_path == NULL) {
898 BT_ERR("Cannot get device path");
902 ret = g_dbus_proxy_call_sync(adapter_proxy,
904 g_variant_new("(o)", device_path),
905 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
908 BT_ERR("RemoveDevice Fail: %s", error->message);
909 g_clear_error(&error);
913 g_variant_unref(ret);
921 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
922 if (!remote_dev_info)
925 GVariant *uuids = NULL;
926 GVariantBuilder *builder = NULL;
928 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
929 for (i = 0; i < remote_dev_info->uuid_count; i++) {
930 g_variant_builder_add(builder, "s",
931 remote_dev_info->uuids[i]);
933 uuids = g_variant_new("as", builder);
934 g_variant_builder_unref(builder);
935 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
936 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
939 param = g_variant_new("(isunsbub@asn@ay)",
942 remote_dev_info->class,
943 remote_dev_info->rssi,
944 remote_dev_info->name,
945 remote_dev_info->paired,
946 remote_dev_info->connected,
947 remote_dev_info->trust,
949 remote_dev_info->manufacturer_data_len,
953 /* Send the event to application */
954 _bt_send_event(BT_ADAPTER_EVENT,
955 BLUETOOTH_EVENT_BONDING_FINISHED,
958 _bt_free_device_info(remote_dev_info);
961 if (req_info->context == NULL)
964 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
965 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
968 if (_bt_adapter_get_status() != BT_ACTIVATED)
969 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
971 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
972 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
974 g_dbus_method_invocation_return_value(req_info->context,
975 g_variant_new("(iv)", result, out_param1));
977 g_variant_unref(out_param1);
979 _bt_delete_request_list(req_info->req_id);
984 _bt_agent_set_canceled(FALSE);
987 g_free(bonding_info->addr);
988 g_free(bonding_info);
992 int _bt_bond_device(int request_id,
993 bluetooth_device_address_t *device_address,
994 unsigned short conn_type, GArray **out_param1)
997 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
998 bluetooth_device_info_t dev_info;
1000 GDBusConnection *conn;
1001 char *device_path = NULL;
1002 GDBusProxy *adapter_proxy;
1003 GError *error = NULL;
1004 gboolean bonding_wo_discovery = FALSE;
1006 BT_CHECK_PARAMETER(device_address, return);
1009 BT_ERR("Bonding in progress");
1011 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1012 memcpy(dev_info.device_address.addr, device_address->addr,
1013 BLUETOOTH_ADDRESS_LENGTH);
1015 g_array_append_vals(*out_param1, &dev_info,
1016 sizeof(bluetooth_device_info_t));
1018 return BLUETOOTH_ERROR_DEVICE_BUSY;
1021 conn = _bt_gdbus_get_system_gconn();
1022 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1024 _bt_convert_addr_type_to_string(address, device_address->addr);
1026 device_path = _bt_get_device_object_path(address);
1028 if (device_path == NULL) {
1029 BT_ERR("No searched device");
1030 GVariant *ret = NULL;
1031 adapter_proxy = _bt_get_adapter_proxy();
1032 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1034 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1035 g_variant_new("(s)", address),
1036 G_DBUS_CALL_FLAGS_NONE,
1041 if (error != NULL) {
1042 BT_ERR("CreateDevice Fail: %s", error->message);
1043 g_clear_error(&error);
1046 g_variant_unref(ret);
1047 device_path = _bt_get_device_object_path(address);
1048 if (device_path == NULL) {
1049 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1050 memcpy(dev_info.device_address.addr, device_address->addr,
1051 BLUETOOTH_ADDRESS_LENGTH);
1053 g_array_append_vals(*out_param1, &dev_info,
1054 sizeof(bluetooth_device_info_t));
1056 return BLUETOOTH_ERROR_NOT_PAIRED;
1058 BT_INFO("device_path is created[%s]", device_path);
1060 bonding_wo_discovery = TRUE;
1063 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1064 NULL, BT_BLUEZ_NAME,
1065 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1067 g_free(device_path);
1068 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1070 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1071 bonding_info->addr = g_strdup(address);
1072 bonding_info->req_id = request_id;
1074 bonding_info->device_proxy = proxy;
1075 bonding_info->conn_type = conn_type;
1076 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1078 is_device_creating = TRUE;
1080 g_dbus_proxy_call(proxy, "Pair",
1081 g_variant_new("(y)", conn_type),
1082 G_DBUS_CALL_FLAGS_NONE,
1083 BT_MAX_DBUS_TIMEOUT,
1085 (GAsyncReadyCallback)__bt_bond_device_cb,
1088 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1090 return BLUETOOTH_ERROR_NONE;
1092 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1093 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1094 bonding_info->addr);
1096 g_array_append_vals(*out_param1, &dev_info,
1097 sizeof(bluetooth_device_info_t));
1099 is_device_creating = FALSE;
1101 g_free(bonding_info->addr);
1102 g_free(bonding_info);
1103 bonding_info = NULL;
1105 return BLUETOOTH_ERROR_INTERNAL;*/
1108 int _bt_cancel_bonding(void)
1110 int ret = BLUETOOTH_ERROR_NONE;
1112 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1114 ret = _bt_agent_reply_cancellation();
1115 if (ret != BLUETOOTH_ERROR_NONE) {
1116 BT_ERR("Fail to call reply cancellation");
1120 _bt_agent_set_canceled(TRUE);
1122 return BLUETOOTH_ERROR_NONE;
1125 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1130 GVariant *out_param1;
1131 int result = BLUETOOTH_ERROR_NONE;
1132 bt_funcion_data_t *unbonding_info;
1133 bluetooth_device_info_t dev_info;
1134 request_info_t *req_info;
1136 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1138 g_variant_unref(reply);
1140 unbonding_info = user_data;
1142 if (unbonding_info == NULL) {
1144 BT_ERR("unbonding_info == NULL");
1148 req_info = _bt_get_request_info(unbonding_info->req_id);
1149 if (req_info == NULL) {
1150 BT_ERR("req_info == NULL");
1155 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1156 result = BLUETOOTH_ERROR_INTERNAL;
1159 if (req_info->context == NULL)
1162 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1163 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1164 unbonding_info->addr);
1166 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1167 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1169 g_dbus_method_invocation_return_value(req_info->context,
1170 g_variant_new("(iv)", result, out_param1));
1173 _bt_delete_request_list(req_info->req_id);
1179 if (unbonding_info) {
1180 g_free(unbonding_info->addr);
1181 g_free(unbonding_info);
1185 int _bt_unbond_device(int request_id,
1186 bluetooth_device_address_t *device_address,
1187 GArray **out_param1)
1189 char *device_path = NULL;
1190 bt_funcion_data_t *unbonding_info;
1191 GDBusProxy *adapter_proxy = NULL;
1192 GDBusProxy *device_proxy = NULL;
1193 GDBusConnection *conn;
1194 int result = BLUETOOTH_ERROR_INTERNAL;
1195 bluetooth_device_info_t dev_info;
1196 GError *error = NULL;
1197 GVariant *ret = NULL;
1199 BT_CHECK_PARAMETER(device_address, return);
1201 adapter_proxy = _bt_get_adapter_proxy();
1202 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1204 /* allocate user data so that it can be retrieved in callback */
1205 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1206 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1207 unbonding_info->req_id = request_id;
1209 _bt_convert_addr_type_to_string(unbonding_info->addr,
1210 device_address->addr);
1212 device_path = _bt_get_device_object_path(unbonding_info->addr);
1214 if (device_path == NULL) {
1215 BT_ERR("No paired device");
1216 result = BLUETOOTH_ERROR_NOT_PAIRED;
1220 conn = _bt_gdbus_get_system_gconn();
1222 BT_ERR("conn is NULL");
1223 result = BLUETOOTH_ERROR_INTERNAL;
1227 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1228 NULL, BT_BLUEZ_NAME,
1229 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1231 if (device_proxy != NULL) {
1233 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1234 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1235 G_DBUS_CALL_FLAGS_NONE,
1240 BT_ERR("Getting property failed: [%s]\n", error->message);
1241 g_error_free(error);
1242 result = BLUETOOTH_ERROR_NOT_PAIRED;
1246 BT_ERR("No paired device");
1247 g_object_unref(device_proxy);
1248 result = BLUETOOTH_ERROR_NOT_PAIRED;
1251 g_variant_unref(ret);
1253 g_object_unref(device_proxy);
1256 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1257 g_variant_new("(o)", device_path),
1258 G_DBUS_CALL_FLAGS_NONE,
1259 BT_MAX_DBUS_TIMEOUT,
1261 (GAsyncReadyCallback)__bt_unbond_cb,
1264 g_free(device_path);
1265 return BLUETOOTH_ERROR_NONE;
1268 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1269 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1270 unbonding_info->addr);
1272 g_array_append_vals(*out_param1, &dev_info,
1273 sizeof(bluetooth_device_info_t));
1274 g_free(device_path);
1275 g_free(unbonding_info->addr);
1276 g_free(unbonding_info);
1280 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1285 GVariant *out_param1;
1286 int result = BLUETOOTH_ERROR_NONE;
1287 bluetooth_device_info_t dev_info;
1288 bt_remote_dev_info_t *remote_dev_info;
1289 request_info_t *req_info;
1290 GVariant *uuid_list, *manufacture_data;
1292 GVariantBuilder *builder = NULL;
1295 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1297 g_variant_unref(reply);
1299 g_object_unref(proxy);
1301 if (searching_info == NULL) {
1303 BT_ERR("unbonding_info == NULL");
1307 req_info = _bt_get_request_info(searching_info->req_id);
1308 if (req_info == NULL) {
1309 BT_ERR("req_info == NULL");
1314 g_dbus_error_strip_remote_error(err);
1315 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1317 if (g_strrstr("Operation canceled", err->message))
1318 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1319 else if (g_strrstr("In Progress", err->message))
1320 result = BLUETOOTH_ERROR_IN_PROGRESS;
1321 else if (g_strrstr("Host is down", err->message))
1322 result = BLUETOOTH_ERROR_HOST_DOWN;
1324 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1326 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1327 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1328 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1329 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1330 remote_dev_info->uuid_count > 0) {
1331 result = BLUETOOTH_ERROR_NONE;
1334 _bt_free_device_info(remote_dev_info);
1339 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1340 if (!remote_dev_info)
1344 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1345 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1346 g_variant_builder_add(builder, "s",
1347 remote_dev_info->uuids[i]);
1349 uuid_list = g_variant_new("as", builder);
1350 g_variant_builder_unref(builder);
1351 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1352 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1355 param = g_variant_new("(isunsbub@asn@ay)",
1357 searching_info->addr,
1358 remote_dev_info->class,
1359 remote_dev_info->rssi,
1360 remote_dev_info->name,
1361 remote_dev_info->paired,
1362 remote_dev_info->connected,
1363 remote_dev_info->trust,
1365 remote_dev_info->manufacturer_data_len,
1368 /* Send the event to application */
1369 _bt_send_event(BT_ADAPTER_EVENT,
1370 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1373 _bt_free_device_info(remote_dev_info);
1376 if (req_info->context == NULL)
1379 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1380 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1381 searching_info->addr);
1383 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1384 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1386 g_dbus_method_invocation_return_value(req_info->context,
1387 g_variant_new("(iv)", result, out_param1));
1390 _bt_delete_request_list(req_info->req_id);
1395 if (searching_info) {
1396 g_free(searching_info->addr);
1397 g_free(searching_info);
1398 searching_info = NULL;
1402 int _bt_search_device(int request_id,
1403 bluetooth_device_address_t *device_address)
1405 char *device_path = NULL;
1406 GDBusProxy *device_proxy = NULL;
1407 GDBusConnection *conn;
1409 GDBusProxy *adapter_proxy;
1410 int result = BLUETOOTH_ERROR_INTERNAL;
1412 BT_CHECK_PARAMETER(device_address, return);
1415 BT_ERR("Bonding in progress");
1416 return BLUETOOTH_ERROR_DEVICE_BUSY;
1419 if (searching_info) {
1420 BT_ERR("Service searching in progress");
1421 return BLUETOOTH_ERROR_DEVICE_BUSY;
1424 adapter_proxy = _bt_get_adapter_proxy();
1425 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1427 /* allocate user data so that it can be retrieved in callback */
1428 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1429 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1430 searching_info->req_id = request_id;
1432 _bt_convert_addr_type_to_string(searching_info->addr,
1433 device_address->addr);
1435 conn = _bt_gdbus_get_system_gconn();
1436 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1439 device_path = _bt_get_device_object_path(searching_info->addr);
1441 if (device_path == NULL) {
1442 BT_ERR("No paired device");
1443 result = BLUETOOTH_ERROR_NOT_PAIRED;
1447 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1448 NULL, BT_BLUEZ_NAME,
1449 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1450 g_free(device_path);
1451 if (device_proxy == NULL) {
1452 result = BLUETOOTH_ERROR_INTERNAL;
1456 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1457 g_variant_new("(s)", ""),
1458 G_DBUS_CALL_FLAGS_NONE,
1459 BT_MAX_DBUS_TIMEOUT,
1461 (GAsyncReadyCallback)__bt_discover_cb,
1464 searching_info->device_proxy = device_proxy;
1466 return BLUETOOTH_ERROR_NONE;
1469 g_free(searching_info->addr);
1470 g_free(searching_info);
1471 searching_info = NULL;
1475 int _bt_cancel_search_device(void)
1477 GVariant *ret = NULL;
1480 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1482 if (searching_info->device_proxy) {
1483 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1485 G_DBUS_CALL_FLAGS_NONE,
1490 g_variant_unref(ret);
1492 __bt_cancel_search_service_done();
1494 return BLUETOOTH_ERROR_NONE;
1497 int _bt_set_alias(bluetooth_device_address_t *device_address,
1500 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1501 gchar *device_path = NULL;
1502 GDBusProxy *adapter_proxy;
1503 GDBusProxy *device_proxy;
1504 GVariant *ret = NULL;
1505 GError *error = NULL;
1506 GDBusConnection *conn;
1508 BT_CHECK_PARAMETER(device_address, return);
1509 BT_CHECK_PARAMETER(alias, return);
1511 adapter_proxy = _bt_get_adapter_proxy();
1512 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1514 conn = _bt_gdbus_get_system_gconn();
1515 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1517 _bt_convert_addr_type_to_string(address, device_address->addr);
1519 device_path = _bt_get_device_object_path(address);
1521 if (device_path == NULL) {
1522 BT_ERR("No paired device");
1523 return BLUETOOTH_ERROR_NOT_PAIRED;
1526 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1527 NULL, BT_BLUEZ_NAME,
1528 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1530 g_free(device_path);
1531 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1533 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1534 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1535 G_DBUS_CALL_FLAGS_NONE,
1540 g_variant_unref(ret);
1542 g_object_unref(device_proxy);
1545 BT_ERR("SetProperty error: [%s]", error->message);
1546 g_error_free(error);
1547 return BLUETOOTH_ERROR_INTERNAL;
1550 return BLUETOOTH_ERROR_NONE;
1553 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1556 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1557 gchar *device_path = NULL;
1558 GDBusProxy *device_proxy;
1559 gboolean previous_value;
1560 GError *error = NULL;
1561 GDBusConnection *conn;
1562 GVariant *result = NULL;
1563 GVariant *temp = NULL;
1564 int ret = BLUETOOTH_ERROR_NONE;
1566 BT_CHECK_PARAMETER(device_address, return);
1568 conn = _bt_gdbus_get_system_gconn();
1569 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1571 _bt_convert_addr_type_to_string(address, device_address->addr);
1573 device_path = _bt_get_device_object_path(address);
1575 if (device_path == NULL) {
1576 BT_ERR("No paired device");
1577 return BLUETOOTH_ERROR_NOT_PAIRED;
1580 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1581 NULL, BT_BLUEZ_NAME,
1582 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1584 g_free(device_path);
1585 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1587 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1588 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1589 G_DBUS_CALL_FLAGS_NONE,
1593 if (error != NULL) {
1594 BT_ERR("Getting property failed: [%s]\n", error->message);
1595 g_error_free(error);
1596 g_object_unref(device_proxy);
1597 return BLUETOOTH_ERROR_INTERNAL;
1600 g_variant_get(result, "(v)", &temp);
1601 previous_value = g_variant_get_boolean(temp);
1602 g_variant_unref(temp);
1603 g_variant_unref(result);
1604 /* If the input is same with previous value, return error. */
1605 if (previous_value == authorize) {
1606 BT_ERR("Same value: %d", previous_value);
1607 g_object_unref(device_proxy);
1608 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1612 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1613 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1614 G_DBUS_CALL_FLAGS_NONE,
1619 g_object_unref(device_proxy);
1621 BT_ERR("SetProperty error: [%s]", error->message);
1622 g_error_free(error);
1623 ret = BLUETOOTH_ERROR_INTERNAL;
1627 g_variant_unref(result);
1632 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1633 gboolean *is_connected)
1635 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1636 char *object_path = NULL;
1638 GDBusProxy *device_proxy;
1639 GError *error = NULL;
1641 GDBusConnection *conn;
1642 GVariant *result = NULL;
1643 int ret = BLUETOOTH_ERROR_NONE;
1645 BT_CHECK_PARAMETER(device_address, return);
1647 conn = _bt_gdbus_get_system_gconn();
1648 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1650 _bt_convert_addr_type_to_string(address, device_address->addr);
1652 object_path = _bt_get_device_object_path(address);
1653 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1655 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1656 NULL, BT_BLUEZ_NAME, object_path,
1657 BT_PROPERTIES_INTERFACE, NULL, NULL);
1658 g_free(object_path);
1659 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1661 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1662 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1664 G_DBUS_CALL_FLAGS_NONE,
1668 if (result == NULL) {
1669 if (error != NULL) {
1670 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1671 g_error_free(error);
1673 g_object_unref(device_proxy);
1674 return BLUETOOTH_ERROR_INTERNAL;
1677 g_variant_get(result, "(v)", &value);
1678 *is_connected = g_variant_get_boolean(value);
1679 g_variant_unref(result);
1681 BT_DBG("gatt is connected : %d", *is_connected);
1683 g_variant_unref(value);
1684 g_object_unref(device_proxy);
1689 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1690 int connection_type, gboolean *is_connected)
1692 char *object_path = NULL;
1693 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1694 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1695 GDBusProxy *device_proxy = NULL;
1696 GDBusProxy *adapter_proxy = NULL;
1697 GDBusConnection *conn;
1698 GError *error = NULL;
1699 GVariant *tmp_value = NULL;
1700 GVariant *value = NULL;
1701 GVariant *result = NULL;
1704 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1705 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1707 _bt_convert_addr_type_to_string(address, device_address->addr);
1708 _bt_convert_addr_string_to_secure_string(secure_address, address);
1710 *is_connected = FALSE;
1711 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1713 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1714 return _bt_rfcomm_is_device_connected(device_address,
1716 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1717 return _bt_is_gatt_connected(device_address, is_connected);
1718 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1719 return _bt_pbap_is_connected(device_address, is_connected);
1721 adapter_proxy = _bt_get_adapter_proxy();
1722 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1724 conn = _bt_gdbus_get_system_gconn();
1725 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1727 _bt_convert_addr_type_to_string(address, device_address->addr);
1729 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1730 object_path = _bt_get_adapter_path();
1731 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1732 NULL, BT_BLUEZ_NAME,
1733 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1734 g_free(object_path);
1735 if (device_proxy == NULL) {
1736 BT_DBG("Device don't have this service");
1737 return BLUETOOTH_ERROR_INTERNAL;
1740 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1741 g_variant_new("(s)", address),
1742 G_DBUS_CALL_FLAGS_NONE,
1747 if (result == NULL) {
1748 BT_ERR("[GetProperties] Error occured in Proxy call");
1750 BT_ERR("%s", error->message);
1751 g_error_free(error);
1753 *is_connected = FALSE;
1754 g_object_unref(device_proxy);
1755 return BLUETOOTH_ERROR_NONE;
1757 g_variant_get(result , "(@a{sv})", &value);
1758 g_variant_unref(result);
1761 tmp_value = g_variant_lookup_value(value,
1763 G_VARIANT_TYPE_BOOLEAN);
1765 *is_connected = g_variant_get_boolean(tmp_value);
1766 g_variant_unref(tmp_value);
1768 g_variant_unref(value);
1770 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1771 return _bt_is_network_connected(_bt_get_net_conn(),
1772 device_address->addr, is_connected);
1774 uuid = _bt_get_profile_uuid128(connection_type);
1776 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1777 return BLUETOOTH_ERROR_INTERNAL;
1779 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1781 object_path = _bt_get_device_object_path(address);
1782 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1783 BT_DBG("object_path: %s", object_path);
1784 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1785 NULL, BT_BLUEZ_NAME,
1786 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1787 g_free(object_path);
1788 if (device_proxy == NULL) {
1789 BT_DBG("Device don't have this service");
1791 return BLUETOOTH_ERROR_INTERNAL;
1794 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1795 g_variant_new("(s)", uuid),
1796 G_DBUS_CALL_FLAGS_NONE,
1801 if (result == NULL) {
1802 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1804 BT_ERR("%s", error->message);
1805 if (g_strrstr(error->message, "Not Connected"))
1806 BT_DBG("Not connected");
1807 g_error_free(error);
1809 *is_connected = FALSE;
1810 g_object_unref(device_proxy);
1812 return BLUETOOTH_ERROR_NONE;
1814 g_variant_get(result, "(b)", is_connected);
1816 g_variant_unref(result);
1819 g_object_unref(device_proxy);
1820 return BLUETOOTH_ERROR_NONE;
1823 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1824 bluetooth_connected_link_t *connected)
1826 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1827 char *object_path = NULL;
1829 GDBusProxy *device_proxy;
1830 GError *error = NULL;
1831 GDBusConnection *conn;
1832 GVariant *value = NULL;
1833 GVariant *result = NULL;
1835 BT_CHECK_PARAMETER(device_address, return);
1837 conn = _bt_gdbus_get_system_gconn();
1838 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1840 _bt_convert_addr_type_to_string(address, device_address->addr);
1842 object_path = _bt_get_device_object_path(address);
1843 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1845 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1846 NULL, BT_BLUEZ_NAME,
1847 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1848 g_free(object_path);
1849 if (device_proxy == NULL) {
1850 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1851 return BLUETOOTH_ERROR_NONE;
1854 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1855 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1856 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1858 if (error != NULL) {
1859 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1860 g_error_free(error);
1861 g_object_unref(device_proxy);
1862 return BLUETOOTH_ERROR_INTERNAL;
1864 g_variant_get(result, "(v)", &value);
1865 *connected = g_variant_get_byte(value);
1866 g_variant_unref(value);
1867 g_variant_unref(result);
1870 g_object_unref(device_proxy);
1871 return BLUETOOTH_ERROR_NONE;
1874 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1878 GVariant *out_param1;
1880 int result = BLUETOOTH_ERROR_NONE;
1881 bt_function_data_t *func_data = user_data;
1882 request_info_t *req_info = NULL;
1883 bluetooth_device_address_t device_addr = { {0} };
1885 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1886 g_object_unref(proxy);
1888 if (reply == NULL) {
1889 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1891 BT_ERR("Error: %s", err->message);
1892 g_clear_error(&err);
1894 result = BLUETOOTH_ERROR_INTERNAL;
1896 g_variant_unref(reply);
1899 if (func_data == NULL) {
1900 BT_ERR("func_data is NULL");
1904 req_info = _bt_get_request_info(func_data->req_id);
1905 if (req_info == NULL) {
1906 BT_ERR("req_info is NULL");
1910 if (req_info->context == NULL) {
1911 BT_ERR("req_info->context is NULL");
1915 _bt_convert_addr_string_to_type(device_addr.addr,
1916 (const char *)func_data->address);
1918 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1919 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1922 g_dbus_method_invocation_return_value(req_info->context,
1923 g_variant_new("(iv)", result, out_param1));
1927 _bt_delete_request_list(req_info->req_id);
1930 g_free(func_data->address);
1935 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1936 gboolean auto_connect)
1938 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1939 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1940 gchar *device_path = NULL;
1941 GDBusProxy *device_proxy = NULL;
1942 GDBusConnection *conn;
1943 int ret = BLUETOOTH_ERROR_NONE;
1944 bt_function_data_t *func_data;
1946 BT_CHECK_PARAMETER(bd_addr, return);
1948 conn = _bt_gdbus_get_system_gconn();
1949 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1951 _bt_convert_addr_type_to_string(device_address,
1952 (unsigned char *)bd_addr->addr);
1953 device_path = _bt_get_device_object_path(device_address);
1954 if (device_path == NULL) {
1955 BT_ERR_C("device_path NULL : [%s]", device_address);
1956 ret = BLUETOOTH_ERROR_INTERNAL;
1960 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1961 NULL, BT_BLUEZ_NAME,
1962 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1963 g_free(device_path);
1964 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1966 func_data = g_malloc0(sizeof(bt_function_data_t));
1967 func_data->address = g_strdup(device_address);
1968 if (func_data->address == NULL) {
1969 BT_ERR("Unable to allocate memory for address");
1970 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1974 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1975 BT_INFO("Connect LE [%s]", secure_address);
1977 func_data->req_id = req_id;
1979 g_dbus_proxy_call(device_proxy, "ConnectLE",
1980 g_variant_new("(b)", auto_connect),
1981 G_DBUS_CALL_FLAGS_NONE,
1982 BT_MAX_DBUS_TIMEOUT,
1984 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1990 g_object_unref(device_proxy);
1992 g_free(func_data->address);
1998 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
2000 BT_INFO("Try to initiate pending LE connection");
2002 pending_le_conn_timer_id = 0;
2004 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
2005 &pending_le_conn_info->bd_addr,
2006 pending_le_conn_info->auto_connect);
2008 g_free(pending_le_conn_info);
2009 pending_le_conn_info = NULL;
2014 void _bt_pending_connect_le_device(void)
2016 if (pending_le_conn_timer_id > 0) {
2017 g_source_remove(pending_le_conn_timer_id);
2018 __bt_connect_le_timer_cb(NULL);
2022 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
2023 gboolean auto_connect)
2025 int ret = BLUETOOTH_ERROR_NONE;
2027 BT_CHECK_PARAMETER(bd_addr, return);
2029 ret = _bt_hold_current_advertising();
2030 if (ret == BLUETOOTH_ERROR_NONE) {
2031 BT_INFO("Current advertising is held");
2032 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2033 pending_le_conn_info->req_id = req_id;
2034 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2035 BLUETOOTH_ADDRESS_LENGTH);
2036 pending_le_conn_info->auto_connect = auto_connect;
2038 pending_le_conn_timer_id =
2039 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2041 return BLUETOOTH_ERROR_NONE;
2042 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
2043 BT_ERR("Unable to hold advertising");
2046 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2049 int _bt_disconnect_le_device(int req_id,
2050 const bluetooth_device_address_t *bd_addr)
2052 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2053 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2055 GDBusProxy *device_proxy;
2056 GDBusConnection *conn;
2057 int ret = BLUETOOTH_ERROR_NONE;
2058 bt_function_data_t *func_data;
2060 BT_CHECK_PARAMETER(bd_addr, return);
2062 conn = _bt_gdbus_get_system_gconn();
2063 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2065 _bt_convert_addr_type_to_string(device_address,
2066 (unsigned char *)bd_addr->addr);
2067 device_path = _bt_get_device_object_path(device_address);
2068 if (device_path == NULL) {
2069 BT_DBG("device_path NULL");
2070 ret = BLUETOOTH_ERROR_INTERNAL;
2074 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2076 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2077 NULL, BT_BLUEZ_NAME,
2078 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2079 g_free(device_path);
2080 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2082 func_data = g_malloc0(sizeof(bt_function_data_t));
2083 func_data->address = g_strdup(device_address);
2084 if (func_data->address == NULL) {
2085 BT_ERR("Unable to allocate memory for address");
2086 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2090 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2091 BT_INFO("Disconnect LE [%s]", secure_address);
2093 func_data->req_id = req_id;
2095 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2097 G_DBUS_CALL_FLAGS_NONE,
2098 BT_MAX_DBUS_TIMEOUT,
2100 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2105 g_object_unref(device_proxy);
2107 g_free(func_data->address);
2113 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2115 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2116 gchar *device_path = NULL;
2117 GError *error = NULL;
2118 GDBusProxy *device_proxy = NULL;
2119 GDBusProxy *adapter_proxy;
2120 GDBusConnection *conn;
2121 int ret = BLUETOOTH_ERROR_NONE;
2123 BT_CHECK_PARAMETER(bd_addr, return);
2125 _bt_convert_addr_type_to_string(device_address,
2126 (unsigned char *)bd_addr->addr);
2128 conn = _bt_gdbus_get_system_gconn();
2129 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2131 adapter_proxy = _bt_get_adapter_proxy();
2132 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2134 device_path = _bt_get_device_object_path(device_address);
2135 if (device_path == NULL) {
2136 BT_DBG("device_path NULL");
2137 ret = BLUETOOTH_ERROR_INTERNAL;
2141 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2143 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2144 NULL, BT_BLUEZ_NAME,
2145 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2146 g_free(device_path);
2147 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2149 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2151 G_DBUS_CALL_FLAGS_NONE,
2156 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2157 g_error_free(error);
2158 g_object_unref(device_proxy);
2159 return BLUETOOTH_ERROR_INTERNAL;
2162 g_object_unref(device_proxy);
2167 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2169 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2170 gchar *device_path = NULL;
2171 GError *error = NULL;
2172 GDBusProxy *device_proxy = NULL;
2173 GDBusProxy *adapter_proxy;
2174 GDBusConnection *conn;
2175 int ret = BLUETOOTH_ERROR_NONE;
2177 BT_CHECK_PARAMETER(bd_addr, return);
2179 _bt_convert_addr_type_to_string(device_address,
2180 (unsigned char *)bd_addr->addr);
2182 conn = _bt_gdbus_get_system_gconn();
2183 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2185 adapter_proxy = _bt_get_adapter_proxy();
2186 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2188 device_path = _bt_get_device_object_path(device_address);
2189 if (device_path == NULL) {
2190 BT_DBG("device_path NULL");
2191 ret = BLUETOOTH_ERROR_INTERNAL;
2195 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2197 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2198 NULL, BT_BLUEZ_NAME,
2199 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2200 g_free(device_path);
2201 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2203 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2205 G_DBUS_CALL_FLAGS_NONE,
2210 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2211 g_error_free(error);
2212 g_object_unref(device_proxy);
2213 return BLUETOOTH_ERROR_INTERNAL;
2216 g_object_unref(device_proxy);
2221 int _bt_connect_profile(char *address, char *uuid,
2222 void *cb, gpointer func_data)
2226 GDBusConnection *conn;
2227 GDBusProxy *adapter_proxy;
2228 GVariant *result = NULL;
2229 GError *error = NULL;
2231 conn = _bt_gdbus_get_system_gconn();
2232 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2234 object_path = _bt_get_device_object_path(address);
2235 if (object_path == NULL) {
2236 BT_ERR("No searched device");
2238 adapter_proxy = _bt_get_adapter_proxy();
2239 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2241 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2242 g_variant_new("(s)", address),
2243 G_DBUS_CALL_FLAGS_NONE,
2248 if (error != NULL) {
2249 BT_ERR("CreateDevice Fail: %s", error->message);
2250 g_error_free(error);
2253 g_variant_unref(result);
2255 object_path = _bt_get_device_object_path(address);
2257 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2259 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2260 NULL, BT_BLUEZ_NAME,
2261 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2262 g_free(object_path);
2263 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2265 g_dbus_proxy_call(proxy, "ConnectProfile",
2266 g_variant_new("(s)", uuid),
2267 G_DBUS_CALL_FLAGS_NONE,
2268 BT_MAX_DBUS_TIMEOUT,
2270 (GAsyncReadyCallback)cb,
2273 return BLUETOOTH_ERROR_NONE;
2276 int _bt_disconnect_all(char *address)
2278 int ret = BLUETOOTH_ERROR_NONE;
2281 GDBusConnection *conn;
2282 GVariant *result = NULL;
2286 conn = _bt_gdbus_get_system_gconn();
2287 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2289 object_path = _bt_get_device_object_path(address);
2290 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2292 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2293 NULL, BT_BLUEZ_NAME,
2294 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2295 g_free(object_path);
2296 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2298 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2300 G_DBUS_CALL_FLAGS_NONE,
2305 BT_ERR("Dbus Call Error:[%s]", err->message);
2307 ret = BLUETOOTH_ERROR_INTERNAL;
2310 g_object_unref(proxy);
2312 g_variant_unref(result);
2317 int _bt_disconnect_profile(char *address, char *uuid,
2318 void *cb, gpointer func_data)
2322 GDBusConnection *conn;
2324 conn = _bt_gdbus_get_system_gconn();
2325 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2327 object_path = _bt_get_device_object_path(address);
2328 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2330 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2331 NULL, BT_BLUEZ_NAME,
2332 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2333 g_free(object_path);
2334 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2336 g_dbus_proxy_call(proxy, "DisconnectProfile",
2337 g_variant_new("(s)", uuid),
2338 G_DBUS_CALL_FLAGS_NONE,
2339 BT_MAX_DBUS_TIMEOUT,
2341 (GAsyncReadyCallback)cb,
2344 return BLUETOOTH_ERROR_NONE;
2347 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2348 int low_threshold, int in_range_threshold, int high_threshold)
2350 int ret = BLUETOOTH_ERROR_NONE;
2352 GVariant *result = NULL;
2353 GError *error = NULL;
2354 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2356 BT_CHECK_PARAMETER(bd_addr, return);
2357 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2358 bd_addr->addr[0], bd_addr->addr[1],
2359 bd_addr->addr[2], bd_addr->addr[3],
2360 bd_addr->addr[4], bd_addr->addr[5],
2362 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2363 in_range_threshold, high_threshold);
2365 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2367 proxy = _bt_get_adapter_proxy();
2368 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2370 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2371 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2372 G_DBUS_CALL_FLAGS_NONE,
2376 if (error != NULL) {
2377 BT_ERR("Dbus Call Error:[%s]", error->message);
2378 g_error_free(error);
2379 ret = BLUETOOTH_ERROR_INTERNAL;
2383 g_variant_unref(result);
2388 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2391 int ret = BLUETOOTH_ERROR_NONE;
2393 GVariant *result = NULL;
2394 GError *error = NULL;
2395 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2397 BT_CHECK_PARAMETER(bd_addr, return);
2398 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2399 bd_addr->addr[0], bd_addr->addr[1],
2400 bd_addr->addr[2], bd_addr->addr[3],
2401 bd_addr->addr[4], bd_addr->addr[5],
2404 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2406 proxy = _bt_get_adapter_proxy();
2407 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2409 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2410 g_variant_new("(si)", address, link_type),
2411 G_DBUS_CALL_FLAGS_NONE,
2416 if (error != NULL) {
2417 BT_ERR("Dbus Call Error:[%s]", error->message);
2418 g_error_free(error);
2419 ret = BLUETOOTH_ERROR_INTERNAL;
2423 g_variant_unref(result);
2428 static int __bt_le_set_conn_parameter(const char *address,
2429 float interval_min, float interval_max,
2430 guint16 latency, guint16 time_out)
2432 gchar *device_path = NULL;
2433 GError *error = NULL;
2434 GDBusProxy *device_proxy = NULL;
2435 GDBusConnection *conn;
2437 guint32 min, max, to;
2438 int ret = BLUETOOTH_ERROR_NONE;
2440 BT_CHECK_PARAMETER(address, return);
2442 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2443 interval_min, interval_max, latency, time_out);
2445 device_path = _bt_get_device_object_path((char *)address);
2447 if (device_path == NULL) {
2448 BT_ERR("device_path NULL");
2449 ret = BLUETOOTH_ERROR_INTERNAL;
2453 conn = _bt_gdbus_get_system_gconn();
2455 BT_ERR("conn NULL");
2456 ret = BLUETOOTH_ERROR_INTERNAL;
2460 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2461 NULL, BT_BLUEZ_NAME,
2462 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2464 g_free(device_path);
2465 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2467 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2468 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2469 to = time_out / BT_LE_CONN_TO_SPLIT;
2471 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2472 g_variant_new("(uuuu)", min, max, latency, to),
2473 G_DBUS_CALL_FLAGS_NONE,
2478 g_object_unref(device_proxy);
2479 if (reply == NULL) {
2481 BT_ERR("Error %s[%s]", error->message, address);
2482 if (g_strrstr(error->message, "In Progress"))
2483 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2485 ret = BLUETOOTH_ERROR_INTERNAL;
2486 g_error_free(error);
2490 g_variant_unref(reply);
2496 static void __bt_le_conn_param_free(void *data)
2498 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2500 BT_DBG("%s", param->sender);
2501 g_free(param->sender);
2505 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2507 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2508 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2510 return parama->key > paramb->key;
2513 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2516 bt_connected_le_dev_t *dev;
2521 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2524 if (g_strcmp0(dev->address, address) == 0)
2530 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2533 bt_le_conn_param_t *param = NULL;
2535 if (!dev || !sender)
2538 for (l = dev->senders; l; l = g_slist_next(l)) {
2540 if (g_strcmp0(param->sender, sender) == 0)
2547 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2548 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2550 bt_connected_le_dev_t *dev = NULL;
2551 bt_le_conn_param_t *param = NULL;
2552 bt_le_conn_param_t *data = NULL;
2554 if (!address || !sender)
2555 return BLUETOOTH_ERROR_INVALID_PARAM;
2557 dev = __bt_get_le_connected_dev_info(address);
2559 return BLUETOOTH_ERROR_INTERNAL;
2561 param = __bt_get_le_conn_param_info(dev, sender);
2563 data = g_malloc0(sizeof(bt_le_conn_param_t));
2564 data->sender = g_strdup(sender);
2565 data->interval_min = interval_min;
2566 data->interval_max = interval_max;
2567 data->latency = latency;
2568 data->time_out = time_out;
2569 data->key = interval_min + (interval_max - interval_min)/2;
2571 if (param == NULL) {
2572 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2573 dev->senders = g_slist_append(dev->senders, data);
2575 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2576 dev->senders = g_slist_remove(dev->senders, param);
2577 g_free(param->sender);
2579 dev->senders = g_slist_append(dev->senders, data);
2582 /* Sorting. First element have the minimum interval */
2583 dev->senders = g_slist_sort(dev->senders,
2584 (GCompareFunc)__bt_compare_le_conn_param_key);
2586 return BLUETOOTH_ERROR_NONE;
2589 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2591 bt_connected_le_dev_t *dev = NULL;
2592 bt_le_conn_param_t *param = NULL;
2594 if (!address || !sender)
2595 return BLUETOOTH_ERROR_INVALID_PARAM;
2597 dev = __bt_get_le_connected_dev_info(address);
2599 return BLUETOOTH_ERROR_INTERNAL;
2601 param = __bt_get_le_conn_param_info(dev, sender);
2603 BT_DBG("Remove param %s %s ", address, sender);
2604 dev->senders = g_slist_remove(dev->senders, param);
2605 g_free(param->sender);
2609 *is_removed = FALSE;
2611 return BLUETOOTH_ERROR_NONE;
2614 int _bt_remove_all_le_conn_param_info(const char *sender)
2617 bt_connected_le_dev_t *dev = NULL;
2618 bt_le_conn_param_t *param = NULL;
2619 gboolean is_removed = FALSE;
2620 char *sender_new = NULL;
2621 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2622 int ret = BLUETOOTH_ERROR_NONE;
2625 return BLUETOOTH_ERROR_INVALID_PARAM;
2627 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2629 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2632 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2633 g_slist_length(dev->senders));
2634 if (g_slist_length(dev->senders) > 0) {
2635 param = dev->senders->data;
2636 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2637 param->interval_min, param->interval_max);
2639 if (dev->interval_min != param->interval_min ||
2640 dev->interval_max != param->interval_max) {
2641 sender_new = g_strdup(param->sender);
2643 _bt_convert_addr_string_to_type(addr, dev->address);
2644 ret = _bt_le_conn_update(sender_new, addr,
2645 param->interval_min, param->interval_max,
2646 param->latency, param->time_out);
2649 if (ret != BLUETOOTH_ERROR_NONE)
2650 BT_ERR("Unable to set le connection parameter");
2653 BT_INFO("Set the default interval");
2655 bluetooth_le_connection_param_t param = { 0 };
2656 _bt_get_le_connection_parameter(
2657 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2660 ret = __bt_le_set_conn_parameter(dev->address,
2661 param.interval_min, param.interval_max,
2662 param.latency, param.timeout);
2663 if (ret == BLUETOOTH_ERROR_NONE) {
2664 dev->interval_min = param.interval_min;
2665 dev->interval_max = param.interval_max;
2674 void _bt_add_le_connected_dev_info(const char *address)
2676 bt_connected_le_dev_t *dev = NULL;
2681 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2682 dev->address = g_strdup(address);
2684 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2689 void _bt_remove_le_connected_dev_info(const char *address)
2691 bt_connected_le_dev_t *dev = NULL;
2696 dev = __bt_get_le_connected_dev_info(address);
2700 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2701 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2702 g_free(dev->address);
2708 int _bt_le_conn_update(const char *sender,
2709 unsigned char *device_address,
2710 float interval_min, float interval_max,
2711 guint16 latency, guint16 time_out)
2713 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2714 guint32 min_supervision_to;
2715 bt_connected_le_dev_t *dev = NULL;
2716 bt_le_conn_param_t *param = NULL;
2717 gboolean is_removed = FALSE;
2718 int ret = BLUETOOTH_ERROR_NONE;
2720 BT_CHECK_PARAMETER(device_address, return);
2722 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2723 sender, interval_min, interval_max, latency, time_out);
2725 if (interval_min > interval_max ||
2726 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2727 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2728 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2732 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2733 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2734 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2738 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2739 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2744 * The Supervision_Timeout in milliseconds shall be larger than
2745 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2746 * where Conn_Interval_Max is given in milliseconds.
2748 min_supervision_to = (1 + latency) * interval_max * 2;
2749 if (time_out <= min_supervision_to) {
2750 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2754 _bt_convert_addr_type_to_string(address, device_address);
2755 BT_DBG("Remote device address: %s", address);
2757 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2759 dev = __bt_get_le_connected_dev_info(address);
2761 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2765 if (g_slist_length(dev->senders) == 1)
2768 param = dev->senders->data;
2770 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2772 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2773 BT_DBG("Skip due to same interval");
2777 interval_min = param->interval_min;
2778 interval_max = param->interval_max;
2782 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2784 if (ret != BLUETOOTH_ERROR_NONE) {
2785 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2789 dev->interval_min = interval_min;
2790 dev->interval_max = interval_max;
2796 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2797 bluetooth_device_pin_code_t *pin_code)
2799 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2801 bt_pin_code_info_t *pin_info = NULL;
2803 BT_CHECK_PARAMETER(device_address, return);
2804 BT_CHECK_PARAMETER(pin_code, return);
2805 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2806 BLUETOOTH_ERROR_NO_RESOURCES);
2808 _bt_convert_addr_type_to_string(address, device_address->addr);
2810 for (l = pin_info_list; l != NULL; l = l->next) {
2813 if (g_strcmp0(pin_info->address, address) == 0) {
2814 g_free(pin_info->pin_code);
2815 pin_info->pin_code = g_strdup(pin_code->pin_code);
2816 return BLUETOOTH_ERROR_NONE;
2820 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2821 pin_info->address = g_strdup(address);
2822 pin_info->pin_code = g_strdup(pin_code->pin_code);
2823 pin_info_list = g_slist_append(pin_info_list, pin_info);
2825 return BLUETOOTH_ERROR_NONE;
2828 gint __bt_compare_address(gpointer *a, gpointer *b)
2830 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2831 char *address = (char *)b;
2832 return g_strcmp0(pin_info->address, address);
2835 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2837 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2839 bt_pin_code_info_t *pin_info = NULL;
2841 BT_CHECK_PARAMETER(device_address, return);
2843 _bt_convert_addr_type_to_string(address, device_address->addr);
2845 l = g_slist_find_custom(pin_info_list, address,
2846 (GCompareFunc)__bt_compare_address);
2850 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2851 g_free(pin_info->address);
2852 g_free(pin_info->pin_code);
2856 return BLUETOOTH_ERROR_NONE;
2859 int _bt_get_device_pin_code(const char *address, char *pin_code)
2863 BT_CHECK_PARAMETER(address, return);
2864 BT_CHECK_PARAMETER(pin_code, return);
2866 for (l = pin_info_list; l != NULL; l = l->next) {
2867 bt_pin_code_info_t *pin_info = l->data;
2869 if (g_strcmp0(pin_info->address, address) == 0) {
2870 g_strlcpy(pin_code, pin_info->pin_code,
2871 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2873 return BLUETOOTH_ERROR_NONE;
2877 return BLUETOOTH_ERROR_NOT_FOUND;
2880 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2881 bluetooth_le_connection_param_t *param)
2884 return BLUETOOTH_ERROR_INVALID_PARAM;
2886 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2887 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2888 return BLUETOOTH_ERROR_INVALID_PARAM;
2890 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2893 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2894 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2895 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2896 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2897 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2900 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2901 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2902 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2903 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2904 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2907 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2908 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2909 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2910 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2911 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2915 BT_ERR("Unhandled mode : %d", mode);
2919 return BLUETOOTH_ERROR_NONE;
2922 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2923 guint trusted_profile_flag, guint *trusted)
2929 case TRUSTED_PROFILE_PBAP:
2930 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2931 trust_profile = trusted_profile_flag & (1 << 1);
2933 return BLUETOOTH_ERROR_NOT_SUPPORT;
2935 case TRUSTED_PROFILE_MAP:
2936 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2937 trust_profile = trusted_profile_flag & (1 << 3);
2939 return BLUETOOTH_ERROR_NOT_SUPPORT;
2941 case TRUSTED_PROFILE_SAP:
2942 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2943 trust_profile = trusted_profile_flag & (1 << 5);
2945 return BLUETOOTH_ERROR_NOT_SUPPORT;
2947 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2948 *trusted = trusted_profile_flag;
2949 return BLUETOOTH_ERROR_NONE;
2951 return BLUETOOTH_ERROR_NOT_SUPPORT;
2957 return BLUETOOTH_ERROR_NONE;
2960 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2961 guint restricted_profile_flag, guint *restricted)
2963 int restrict_profile;
2964 *restricted = FALSE;
2967 case RESTRICTED_PROFILE_HFP_HS:
2968 restrict_profile = restricted_profile_flag & (1 << 0);
2970 case RESTRICTED_PROFILE_A2DP:
2971 restrict_profile = restricted_profile_flag & (1 << 2);
2974 return BLUETOOTH_ERROR_NOT_SUPPORT;
2977 if (restrict_profile)
2980 return BLUETOOTH_ERROR_NONE;
2983 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2986 case TRUSTED_PROFILE_PBAP:
2987 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2988 case TRUSTED_PROFILE_MAP:
2989 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2990 case TRUSTED_PROFILE_SAP:
2991 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2992 case TRUSTED_PROFILE_ALL:
2999 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
3002 case RESTRICTED_PROFILE_HFP_HS:
3003 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
3004 case RESTRICTED_PROFILE_A2DP:
3005 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
3011 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
3013 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
3014 return TRUSTED_PROFILE_PBAP;
3015 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
3016 return TRUSTED_PROFILE_MAP;
3017 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
3018 return TRUSTED_PROFILE_SAP;
3020 return 0; /* 0 - Unknown Profile */
3023 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
3024 bluetooth_trusted_profile_t profile, gboolean trust)
3026 int ret = BLUETOOTH_ERROR_NONE;
3027 GDBusConnection *conn;
3029 GError *error = NULL;
3030 char *device_path = NULL;
3032 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3035 BT_CHECK_PARAMETER(bd_addr, return);
3036 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3037 bd_addr->addr[0], bd_addr->addr[1],
3038 bd_addr->addr[2], bd_addr->addr[3],
3039 bd_addr->addr[4], bd_addr->addr[5],
3042 conn = _bt_gdbus_get_system_gconn();
3043 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3045 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3047 device_path = _bt_get_device_object_path(address);
3048 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3050 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3051 NULL, BT_BLUEZ_NAME, device_path,
3052 BT_DEVICE_INTERFACE, NULL, NULL);
3054 g_free(device_path);
3055 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3057 uuid = _bt_get_trusted_profile_uuid(profile);
3059 g_object_unref(proxy);
3060 return BLUETOOTH_ERROR_NOT_SUPPORT;
3063 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3064 g_variant_new("(sb)", uuid, trust),
3065 G_DBUS_CALL_FLAGS_NONE, -1,
3067 g_object_unref(proxy);
3069 if (reply == NULL) {
3070 BT_ERR("Failed to Set Profile Trusted");
3071 ret = BLUETOOTH_ERROR_INTERNAL;
3073 BT_ERR("Error %s[%s]", error->message, address);
3074 g_error_free(error);
3078 g_variant_unref(reply);
3085 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3086 bluetooth_trusted_profile_t profile, guint *trust)
3088 int ret = BLUETOOTH_ERROR_NONE;
3089 GDBusConnection *conn;
3091 GError *error = NULL;
3092 char *device_path = NULL;
3093 guint trusted_profile_flag;
3094 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3097 BT_CHECK_PARAMETER(bd_addr, return);
3098 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3099 bd_addr->addr[0], bd_addr->addr[1],
3100 bd_addr->addr[2], bd_addr->addr[3],
3101 bd_addr->addr[4], bd_addr->addr[5],
3104 conn = _bt_gdbus_get_system_gconn();
3105 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3107 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3109 device_path = _bt_get_device_object_path(address);
3110 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3112 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3113 NULL, BT_BLUEZ_NAME, device_path,
3114 BT_PROPERTIES_INTERFACE, NULL, NULL);
3116 g_free(device_path);
3117 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3119 reply = g_dbus_proxy_call_sync(proxy, "Get",
3120 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3121 G_DBUS_CALL_FLAGS_NONE, -1,
3123 g_object_unref(proxy);
3125 if (reply == NULL) {
3126 BT_ERR("Failed to Get Profile Trusted");
3127 ret = BLUETOOTH_ERROR_INTERNAL;
3129 BT_ERR("Error %s[%s]", error->message, address);
3130 g_error_free(error);
3135 g_variant_get(reply, "(v)", &temp);
3136 trusted_profile_flag = g_variant_get_uint32(temp);
3137 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3139 ret = _bt_get_trusted_profile_from_flag(profile,
3140 trusted_profile_flag, trust);
3141 g_variant_unref(temp);
3142 g_variant_unref(reply);
3145 BT_DBG("TRUST %d", *trust);
3149 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3150 bluetooth_restricted_profile_t profile, gboolean restricted)
3152 int ret = BLUETOOTH_ERROR_NONE;
3153 GDBusConnection *conn;
3155 GError *error = NULL;
3156 char *device_path = NULL;
3158 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3161 BT_CHECK_PARAMETER(bd_addr, return);
3162 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3163 bd_addr->addr[0], bd_addr->addr[1],
3164 bd_addr->addr[2], bd_addr->addr[3],
3165 bd_addr->addr[4], bd_addr->addr[5],
3166 profile, restricted);
3168 conn = _bt_gdbus_get_system_gconn();
3169 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3171 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3173 device_path = _bt_get_device_object_path(address);
3174 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3176 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3177 NULL, BT_BLUEZ_NAME, device_path,
3178 BT_DEVICE_INTERFACE, NULL, NULL);
3180 g_free(device_path);
3181 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3183 uuid = _bt_get_restricted_profile_uuid(profile);
3185 g_object_unref(proxy);
3186 return BLUETOOTH_ERROR_NOT_SUPPORT;
3189 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3190 g_variant_new("(sb)", uuid, restricted),
3191 G_DBUS_CALL_FLAGS_NONE, -1,
3193 g_object_unref(proxy);
3195 if (reply == NULL) {
3196 BT_ERR("Failed to Set Profile Restricted");
3197 ret = BLUETOOTH_ERROR_INTERNAL;
3199 BT_ERR("Error %s[%s]", error->message, address);
3200 g_error_free(error);
3204 g_variant_unref(reply);
3211 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3212 bluetooth_restricted_profile_t profile, guint *restricted)
3214 int ret = BLUETOOTH_ERROR_NONE;
3215 GDBusConnection *conn;
3217 GError *error = NULL;
3218 char *device_path = NULL;
3219 guint restricted_profile_flag;
3220 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3223 BT_CHECK_PARAMETER(bd_addr, return);
3224 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3225 bd_addr->addr[0], bd_addr->addr[1],
3226 bd_addr->addr[2], bd_addr->addr[3],
3227 bd_addr->addr[4], bd_addr->addr[5],
3228 profile, *restricted);
3230 conn = _bt_gdbus_get_system_gconn();
3231 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3233 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3235 device_path = _bt_get_device_object_path(address);
3236 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3238 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3239 NULL, BT_BLUEZ_NAME, device_path,
3240 BT_PROPERTIES_INTERFACE, NULL, NULL);
3242 g_free(device_path);
3243 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3245 reply = g_dbus_proxy_call_sync(proxy, "Get",
3246 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3247 G_DBUS_CALL_FLAGS_NONE, -1,
3249 g_object_unref(proxy);
3251 if (reply == NULL) {
3252 BT_ERR("Failed to Get Profile Restricted");
3253 ret = BLUETOOTH_ERROR_INTERNAL;
3255 BT_ERR("Error %s[%s]", error->message, address);
3256 g_error_free(error);
3261 g_variant_get(reply, "(v)", &temp);
3262 restricted_profile_flag = g_variant_get_uint32(temp);
3263 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3265 ret = _bt_get_restricted_profile_from_flag(profile,
3266 restricted_profile_flag, restricted);
3267 g_variant_unref(temp);
3268 g_variant_unref(reply);
3271 BT_DBG("TRUST %d", *restricted);
3275 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3279 GVariant *out_param1;
3280 request_info_t *req_info;
3281 GVariant *val = NULL;
3282 GVariant *param = NULL;
3285 bluetooth_device_address_t device_addr = { {0} };
3286 int result = BLUETOOTH_ERROR_NONE;
3289 val = g_dbus_proxy_call_finish(proxy, res, &err);
3291 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3292 if (req_info == NULL) {
3293 BT_ERR("req_info == NULL");
3294 g_object_unref(proxy);
3295 att_mtu_req_info->device_proxy = NULL;
3300 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3302 if (g_strrstr(err->message, "NotSupported")) {
3303 BT_INFO("Connection Not Supported");
3304 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3305 } else if (g_strrstr(err->message, "NotConnected")) {
3306 BT_INFO("Not connected");
3307 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3308 } else if (g_strrstr(err->message, "InvalidArguments")) {
3309 BT_INFO("Not connected");
3310 result = BLUETOOTH_ERROR_INVALID_PARAM;
3312 BT_DBG("Default case");
3313 result = BLUETOOTH_ERROR_INTERNAL;
3317 g_object_unref(proxy);
3318 att_mtu_req_info->device_proxy = NULL;
3320 if (result != BLUETOOTH_ERROR_NONE)
3324 g_variant_get(val, "(qy)", &mtu, &status);
3325 g_variant_unref(val);
3328 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3330 param = g_variant_new("(isqy)",
3332 att_mtu_req_info->addr,
3336 /* Send the event to application */
3337 _bt_send_event(BT_DEVICE_EVENT,
3338 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3342 if (req_info->context == NULL)
3345 _bt_convert_addr_string_to_type(device_addr.addr,
3346 (const char *)att_mtu_req_info->addr);
3348 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3349 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3350 g_dbus_method_invocation_return_value(req_info->context,
3351 g_variant_new("(iv)", result, out_param1));
3353 _bt_delete_request_list(req_info->req_id);
3358 g_free(att_mtu_req_info->addr);
3359 g_free(att_mtu_req_info);
3360 att_mtu_req_info = NULL;
3365 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3368 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3369 gchar *device_path = NULL;
3370 GDBusProxy *adapter_proxy;
3371 GDBusProxy *device_proxy;
3372 GDBusConnection *conn;
3373 int ret = BLUETOOTH_ERROR_NONE;
3375 BT_CHECK_PARAMETER(device_address, return);
3377 if (att_mtu_req_info) {
3378 BT_ERR("ATT MTU request in progress");
3379 return BLUETOOTH_ERROR_DEVICE_BUSY;
3382 conn = _bt_gdbus_get_system_gconn();
3383 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3385 adapter_proxy = _bt_get_adapter_proxy();
3386 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3388 _bt_convert_addr_type_to_string(address, device_address->addr);
3390 BT_DBG("Remote device address: %s", address);
3392 device_path = _bt_get_device_object_path(address);
3394 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3396 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3397 NULL, BT_BLUEZ_NAME,
3398 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3399 g_free(device_path);
3400 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3402 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3403 att_mtu_req_info->addr = (char *)g_strdup(address);
3404 att_mtu_req_info->req_id = request_id;
3405 att_mtu_req_info->device_proxy = device_proxy;
3407 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3408 g_variant_new("(q)", mtu),
3409 G_DBUS_CALL_FLAGS_NONE,
3410 BT_MAX_DBUS_TIMEOUT,
3412 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3418 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3421 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3422 char *object_path = NULL;
3424 GDBusProxy *device_proxy;
3425 GError *error = NULL;
3427 GDBusConnection *conn;
3428 GVariant *result = NULL;
3430 BT_CHECK_PARAMETER(device_address, return);
3432 conn = _bt_gdbus_get_system_gconn();
3433 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3435 _bt_convert_addr_type_to_string(address, device_address->addr);
3437 object_path = _bt_get_device_object_path(address);
3438 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3440 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3441 NULL, BT_BLUEZ_NAME, object_path,
3442 BT_PROPERTIES_INTERFACE, NULL, NULL);
3443 g_free(object_path);
3444 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3446 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3447 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3448 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3450 ERR("DBus Error : %s", error->message);
3451 g_clear_error(&error);
3452 g_object_unref(device_proxy);
3453 return BLUETOOTH_ERROR_INTERNAL;
3455 g_variant_get(result, "(v)", &value);
3456 *mtu = g_variant_get_uint16(value);
3457 BT_DBG("ATT MTU : %d", *mtu);
3458 g_variant_unref(value);
3459 g_variant_unref(result);
3462 g_object_unref(device_proxy);
3463 return BLUETOOTH_ERROR_NONE;
3466 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3467 bluetooth_device_address_t *id_address)
3469 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3470 gchar *device_path = NULL;
3471 const gchar *idaddress = NULL;
3472 GDBusProxy *device_proxy;
3473 GError *error = NULL;
3474 GVariant *result = NULL;
3475 GDBusConnection *conn;
3476 int ret = BLUETOOTH_ERROR_NONE;
3478 BT_CHECK_PARAMETER(device_address, return);
3480 conn = _bt_gdbus_get_system_gconn();
3481 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3483 _bt_convert_addr_type_to_string(address, device_address->addr);
3485 device_path = _bt_get_device_object_path(address);
3486 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3488 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3489 NULL, BT_BLUEZ_NAME,
3490 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3491 g_free(device_path);
3492 if (!device_proxy) {
3493 BT_ERR("Unable to get proxy");
3494 return BLUETOOTH_ERROR_INTERNAL;
3497 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3498 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3499 if (result == NULL) {
3500 BT_ERR("Failed to get device ID address");
3502 ret = BLUETOOTH_ERROR_INTERNAL;
3503 if (error != NULL) {
3504 g_dbus_error_strip_remote_error(error);
3505 BT_ERR("Error occured in Proxy call [%s]", error->message);
3506 if (g_strcmp0(error->message, "Does Not Exist") == 0)
3507 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3508 g_error_free(error);
3510 g_object_unref(device_proxy);
3514 g_variant_get(result , "(&s)", &idaddress);
3516 DBG_SECURE("ID Address : %s", idaddress);
3517 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3519 g_variant_unref(result);
3520 g_object_unref(device_proxy);
3525 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3527 GapAgentPrivate *agent = _bt_get_adapter_agent();
3528 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3530 if (authentication_reply)
3531 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3533 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3535 BT_DBG("BT_PASSKEY_REPLY");
3536 return BLUETOOTH_ERROR_NONE;
3539 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3541 GapAgentPrivate *agent = _bt_get_adapter_agent();
3542 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3544 if (confirmation_reply)
3545 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3547 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3549 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3550 return BLUETOOTH_ERROR_NONE;