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_profile(char *address, char *uuid,
2277 void *cb, gpointer func_data)
2281 GDBusConnection *conn;
2283 conn = _bt_gdbus_get_system_gconn();
2284 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2286 object_path = _bt_get_device_object_path(address);
2287 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2289 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2290 NULL, BT_BLUEZ_NAME,
2291 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2292 g_free(object_path);
2293 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2295 g_dbus_proxy_call(proxy, "DisconnectProfile",
2296 g_variant_new("(s)", uuid),
2297 G_DBUS_CALL_FLAGS_NONE,
2298 BT_MAX_DBUS_TIMEOUT,
2300 (GAsyncReadyCallback)cb,
2303 return BLUETOOTH_ERROR_NONE;
2306 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2307 int low_threshold, int in_range_threshold, int high_threshold)
2309 int ret = BLUETOOTH_ERROR_NONE;
2311 GVariant *result = NULL;
2312 GError *error = NULL;
2313 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2315 BT_CHECK_PARAMETER(bd_addr, return);
2316 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2317 bd_addr->addr[0], bd_addr->addr[1],
2318 bd_addr->addr[2], bd_addr->addr[3],
2319 bd_addr->addr[4], bd_addr->addr[5],
2321 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2322 in_range_threshold, high_threshold);
2324 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2326 proxy = _bt_get_adapter_proxy();
2327 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2329 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2330 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2331 G_DBUS_CALL_FLAGS_NONE,
2335 if (error != NULL) {
2336 BT_ERR("Dbus Call Error:[%s]", error->message);
2337 g_error_free(error);
2338 ret = BLUETOOTH_ERROR_INTERNAL;
2342 g_variant_unref(result);
2347 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
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],
2363 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2365 proxy = _bt_get_adapter_proxy();
2366 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2368 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2369 g_variant_new("(si)", address, link_type),
2370 G_DBUS_CALL_FLAGS_NONE,
2375 if (error != NULL) {
2376 BT_ERR("Dbus Call Error:[%s]", error->message);
2377 g_error_free(error);
2378 ret = BLUETOOTH_ERROR_INTERNAL;
2382 g_variant_unref(result);
2387 static int __bt_le_set_conn_parameter(const char *address,
2388 float interval_min, float interval_max,
2389 guint16 latency, guint16 time_out)
2391 gchar *device_path = NULL;
2392 GError *error = NULL;
2393 GDBusProxy *device_proxy = NULL;
2394 GDBusConnection *conn;
2396 guint32 min, max, to;
2397 int ret = BLUETOOTH_ERROR_NONE;
2399 BT_CHECK_PARAMETER(address, return);
2401 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2402 interval_min, interval_max, latency, time_out);
2404 device_path = _bt_get_device_object_path((char *)address);
2406 if (device_path == NULL) {
2407 BT_ERR("device_path NULL");
2408 ret = BLUETOOTH_ERROR_INTERNAL;
2412 conn = _bt_gdbus_get_system_gconn();
2414 BT_ERR("conn NULL");
2415 ret = BLUETOOTH_ERROR_INTERNAL;
2419 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2420 NULL, BT_BLUEZ_NAME,
2421 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2423 g_free(device_path);
2424 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2426 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2427 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2428 to = time_out / BT_LE_CONN_TO_SPLIT;
2430 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2431 g_variant_new("(uuuu)", min, max, latency, to),
2432 G_DBUS_CALL_FLAGS_NONE,
2437 g_object_unref(device_proxy);
2438 if (reply == NULL) {
2440 BT_ERR("Error %s[%s]", error->message, address);
2441 if (g_strrstr(error->message, "In Progress"))
2442 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2444 ret = BLUETOOTH_ERROR_INTERNAL;
2445 g_error_free(error);
2449 g_variant_unref(reply);
2455 static void __bt_le_conn_param_free(void *data)
2457 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2459 BT_DBG("%s", param->sender);
2460 g_free(param->sender);
2464 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2466 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2467 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2469 return parama->key > paramb->key;
2472 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2475 bt_connected_le_dev_t *dev;
2480 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2483 if (g_strcmp0(dev->address, address) == 0)
2489 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2492 bt_le_conn_param_t *param = NULL;
2494 if (!dev || !sender)
2497 for (l = dev->senders; l; l = g_slist_next(l)) {
2499 if (g_strcmp0(param->sender, sender) == 0)
2506 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2507 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2509 bt_connected_le_dev_t *dev = NULL;
2510 bt_le_conn_param_t *param = NULL;
2511 bt_le_conn_param_t *data = NULL;
2513 if (!address || !sender)
2514 return BLUETOOTH_ERROR_INVALID_PARAM;
2516 dev = __bt_get_le_connected_dev_info(address);
2518 return BLUETOOTH_ERROR_INTERNAL;
2520 param = __bt_get_le_conn_param_info(dev, sender);
2522 data = g_malloc0(sizeof(bt_le_conn_param_t));
2523 data->sender = g_strdup(sender);
2524 data->interval_min = interval_min;
2525 data->interval_max = interval_max;
2526 data->latency = latency;
2527 data->time_out = time_out;
2528 data->key = interval_min + (interval_max - interval_min)/2;
2530 if (param == NULL) {
2531 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2532 dev->senders = g_slist_append(dev->senders, data);
2534 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2535 dev->senders = g_slist_remove(dev->senders, param);
2536 g_free(param->sender);
2538 dev->senders = g_slist_append(dev->senders, data);
2541 /* Sorting. First element have the minimum interval */
2542 dev->senders = g_slist_sort(dev->senders,
2543 (GCompareFunc)__bt_compare_le_conn_param_key);
2545 return BLUETOOTH_ERROR_NONE;
2548 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2550 bt_connected_le_dev_t *dev = NULL;
2551 bt_le_conn_param_t *param = NULL;
2553 if (!address || !sender)
2554 return BLUETOOTH_ERROR_INVALID_PARAM;
2556 dev = __bt_get_le_connected_dev_info(address);
2558 return BLUETOOTH_ERROR_INTERNAL;
2560 param = __bt_get_le_conn_param_info(dev, sender);
2562 BT_DBG("Remove param %s %s ", address, sender);
2563 dev->senders = g_slist_remove(dev->senders, param);
2564 g_free(param->sender);
2568 *is_removed = FALSE;
2570 return BLUETOOTH_ERROR_NONE;
2573 int _bt_remove_all_le_conn_param_info(const char *sender)
2576 bt_connected_le_dev_t *dev = NULL;
2577 bt_le_conn_param_t *param = NULL;
2578 gboolean is_removed = FALSE;
2579 char *sender_new = NULL;
2580 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2581 int ret = BLUETOOTH_ERROR_NONE;
2584 return BLUETOOTH_ERROR_INVALID_PARAM;
2586 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2588 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2591 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2592 g_slist_length(dev->senders));
2593 if (g_slist_length(dev->senders) > 0) {
2594 param = dev->senders->data;
2595 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2596 param->interval_min, param->interval_max);
2598 if (dev->interval_min != param->interval_min ||
2599 dev->interval_max != param->interval_max) {
2600 sender_new = g_strdup(param->sender);
2602 _bt_convert_addr_string_to_type(addr, dev->address);
2603 ret = _bt_le_conn_update(sender_new, addr,
2604 param->interval_min, param->interval_max,
2605 param->latency, param->time_out);
2608 if (ret != BLUETOOTH_ERROR_NONE)
2609 BT_ERR("Unable to set le connection parameter");
2612 BT_INFO("Set the default interval");
2614 bluetooth_le_connection_param_t param = { 0 };
2615 _bt_get_le_connection_parameter(
2616 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2619 ret = __bt_le_set_conn_parameter(dev->address,
2620 param.interval_min, param.interval_max,
2621 param.latency, param.timeout);
2622 if (ret == BLUETOOTH_ERROR_NONE) {
2623 dev->interval_min = param.interval_min;
2624 dev->interval_max = param.interval_max;
2633 void _bt_add_le_connected_dev_info(const char *address)
2635 bt_connected_le_dev_t *dev = NULL;
2640 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2641 dev->address = g_strdup(address);
2643 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2648 void _bt_remove_le_connected_dev_info(const char *address)
2650 bt_connected_le_dev_t *dev = NULL;
2655 dev = __bt_get_le_connected_dev_info(address);
2659 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2660 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2661 g_free(dev->address);
2667 int _bt_le_conn_update(const char *sender,
2668 unsigned char *device_address,
2669 float interval_min, float interval_max,
2670 guint16 latency, guint16 time_out)
2672 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2673 guint32 min_supervision_to;
2674 bt_connected_le_dev_t *dev = NULL;
2675 bt_le_conn_param_t *param = NULL;
2676 gboolean is_removed = FALSE;
2677 int ret = BLUETOOTH_ERROR_NONE;
2679 BT_CHECK_PARAMETER(device_address, return);
2681 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2682 sender, interval_min, interval_max, latency, time_out);
2684 if (interval_min > interval_max ||
2685 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2686 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2687 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2691 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2692 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2693 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2697 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2698 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2703 * The Supervision_Timeout in milliseconds shall be larger than
2704 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2705 * where Conn_Interval_Max is given in milliseconds.
2707 min_supervision_to = (1 + latency) * interval_max * 2;
2708 if (time_out <= min_supervision_to) {
2709 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2713 _bt_convert_addr_type_to_string(address, device_address);
2714 BT_DBG("Remote device address: %s", address);
2716 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2718 dev = __bt_get_le_connected_dev_info(address);
2720 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2724 if (g_slist_length(dev->senders) == 1)
2727 param = dev->senders->data;
2729 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2731 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2732 BT_DBG("Skip due to same interval");
2736 interval_min = param->interval_min;
2737 interval_max = param->interval_max;
2741 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2743 if (ret != BLUETOOTH_ERROR_NONE) {
2744 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2748 dev->interval_min = interval_min;
2749 dev->interval_max = interval_max;
2755 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2756 bluetooth_device_pin_code_t *pin_code)
2758 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2760 bt_pin_code_info_t *pin_info = NULL;
2762 BT_CHECK_PARAMETER(device_address, return);
2763 BT_CHECK_PARAMETER(pin_code, return);
2764 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2765 BLUETOOTH_ERROR_NO_RESOURCES);
2767 _bt_convert_addr_type_to_string(address, device_address->addr);
2769 for (l = pin_info_list; l != NULL; l = l->next) {
2772 if (g_strcmp0(pin_info->address, address) == 0) {
2773 g_free(pin_info->pin_code);
2774 pin_info->pin_code = g_strdup(pin_code->pin_code);
2775 return BLUETOOTH_ERROR_NONE;
2779 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2780 pin_info->address = g_strdup(address);
2781 pin_info->pin_code = g_strdup(pin_code->pin_code);
2782 pin_info_list = g_slist_append(pin_info_list, pin_info);
2784 return BLUETOOTH_ERROR_NONE;
2787 gint __bt_compare_address(gpointer *a, gpointer *b)
2789 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2790 char *address = (char *)b;
2791 return g_strcmp0(pin_info->address, address);
2794 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2796 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2798 bt_pin_code_info_t *pin_info = NULL;
2800 BT_CHECK_PARAMETER(device_address, return);
2802 _bt_convert_addr_type_to_string(address, device_address->addr);
2804 l = g_slist_find_custom(pin_info_list, address,
2805 (GCompareFunc)__bt_compare_address);
2809 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2810 g_free(pin_info->address);
2811 g_free(pin_info->pin_code);
2815 return BLUETOOTH_ERROR_NONE;
2818 int _bt_get_device_pin_code(const char *address, char *pin_code)
2822 BT_CHECK_PARAMETER(address, return);
2823 BT_CHECK_PARAMETER(pin_code, return);
2825 for (l = pin_info_list; l != NULL; l = l->next) {
2826 bt_pin_code_info_t *pin_info = l->data;
2828 if (g_strcmp0(pin_info->address, address) == 0) {
2829 g_strlcpy(pin_code, pin_info->pin_code,
2830 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2832 return BLUETOOTH_ERROR_NONE;
2836 return BLUETOOTH_ERROR_NOT_FOUND;
2839 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2840 bluetooth_le_connection_param_t *param)
2843 return BLUETOOTH_ERROR_INVALID_PARAM;
2845 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2846 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2847 return BLUETOOTH_ERROR_INVALID_PARAM;
2849 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2852 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2853 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2854 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2855 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2856 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2859 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2860 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2861 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2862 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2863 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2866 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2867 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2868 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2869 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2870 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2874 BT_ERR("Unhandled mode : %d", mode);
2878 return BLUETOOTH_ERROR_NONE;
2881 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2882 guint trusted_profile_flag, guint *trusted)
2888 case TRUSTED_PROFILE_PBAP:
2889 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2890 trust_profile = trusted_profile_flag & (1 << 1);
2892 return BLUETOOTH_ERROR_NOT_SUPPORT;
2894 case TRUSTED_PROFILE_MAP:
2895 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2896 trust_profile = trusted_profile_flag & (1 << 3);
2898 return BLUETOOTH_ERROR_NOT_SUPPORT;
2900 case TRUSTED_PROFILE_SAP:
2901 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2902 trust_profile = trusted_profile_flag & (1 << 5);
2904 return BLUETOOTH_ERROR_NOT_SUPPORT;
2906 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2907 *trusted = trusted_profile_flag;
2908 return BLUETOOTH_ERROR_NONE;
2910 return BLUETOOTH_ERROR_NOT_SUPPORT;
2916 return BLUETOOTH_ERROR_NONE;
2919 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2920 guint restricted_profile_flag, guint *restricted)
2922 int restrict_profile;
2923 *restricted = FALSE;
2926 case RESTRICTED_PROFILE_HFP_HS:
2927 restrict_profile = restricted_profile_flag & (1 << 0);
2929 case RESTRICTED_PROFILE_A2DP:
2930 restrict_profile = restricted_profile_flag & (1 << 2);
2933 return BLUETOOTH_ERROR_NOT_SUPPORT;
2936 if (restrict_profile)
2939 return BLUETOOTH_ERROR_NONE;
2942 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2945 case TRUSTED_PROFILE_PBAP:
2946 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2947 case TRUSTED_PROFILE_MAP:
2948 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2949 case TRUSTED_PROFILE_SAP:
2950 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2951 case TRUSTED_PROFILE_ALL:
2958 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2961 case RESTRICTED_PROFILE_HFP_HS:
2962 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2963 case RESTRICTED_PROFILE_A2DP:
2964 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2970 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2972 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2973 return TRUSTED_PROFILE_PBAP;
2974 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2975 return TRUSTED_PROFILE_MAP;
2976 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2977 return TRUSTED_PROFILE_SAP;
2979 return 0; /* 0 - Unknown Profile */
2982 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2983 bluetooth_trusted_profile_t profile, gboolean trust)
2985 int ret = BLUETOOTH_ERROR_NONE;
2986 GDBusConnection *conn;
2988 GError *error = NULL;
2989 char *device_path = NULL;
2991 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2994 BT_CHECK_PARAMETER(bd_addr, return);
2995 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2996 bd_addr->addr[0], bd_addr->addr[1],
2997 bd_addr->addr[2], bd_addr->addr[3],
2998 bd_addr->addr[4], bd_addr->addr[5],
3001 conn = _bt_gdbus_get_system_gconn();
3002 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3004 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3006 device_path = _bt_get_device_object_path(address);
3007 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3009 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3010 NULL, BT_BLUEZ_NAME, device_path,
3011 BT_DEVICE_INTERFACE, NULL, NULL);
3013 g_free(device_path);
3014 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3016 uuid = _bt_get_trusted_profile_uuid(profile);
3018 g_object_unref(proxy);
3019 return BLUETOOTH_ERROR_NOT_SUPPORT;
3022 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3023 g_variant_new("(sb)", uuid, trust),
3024 G_DBUS_CALL_FLAGS_NONE, -1,
3026 g_object_unref(proxy);
3028 if (reply == NULL) {
3029 BT_ERR("Failed to Set Profile Trusted");
3030 ret = BLUETOOTH_ERROR_INTERNAL;
3032 BT_ERR("Error %s[%s]", error->message, address);
3033 g_error_free(error);
3037 g_variant_unref(reply);
3044 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3045 bluetooth_trusted_profile_t profile, guint *trust)
3047 int ret = BLUETOOTH_ERROR_NONE;
3048 GDBusConnection *conn;
3050 GError *error = NULL;
3051 char *device_path = NULL;
3052 guint trusted_profile_flag;
3053 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3056 BT_CHECK_PARAMETER(bd_addr, return);
3057 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3058 bd_addr->addr[0], bd_addr->addr[1],
3059 bd_addr->addr[2], bd_addr->addr[3],
3060 bd_addr->addr[4], bd_addr->addr[5],
3063 conn = _bt_gdbus_get_system_gconn();
3064 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3066 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3068 device_path = _bt_get_device_object_path(address);
3069 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3071 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3072 NULL, BT_BLUEZ_NAME, device_path,
3073 BT_PROPERTIES_INTERFACE, NULL, NULL);
3075 g_free(device_path);
3076 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3078 reply = g_dbus_proxy_call_sync(proxy, "Get",
3079 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3080 G_DBUS_CALL_FLAGS_NONE, -1,
3082 g_object_unref(proxy);
3084 if (reply == NULL) {
3085 BT_ERR("Failed to Get Profile Trusted");
3086 ret = BLUETOOTH_ERROR_INTERNAL;
3088 BT_ERR("Error %s[%s]", error->message, address);
3089 g_error_free(error);
3094 g_variant_get(reply, "(v)", &temp);
3095 trusted_profile_flag = g_variant_get_uint32(temp);
3096 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3098 ret = _bt_get_trusted_profile_from_flag(profile,
3099 trusted_profile_flag, trust);
3100 g_variant_unref(temp);
3101 g_variant_unref(reply);
3104 BT_DBG("TRUST %d", *trust);
3108 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3109 bluetooth_restricted_profile_t profile, gboolean restricted)
3111 int ret = BLUETOOTH_ERROR_NONE;
3112 GDBusConnection *conn;
3114 GError *error = NULL;
3115 char *device_path = NULL;
3117 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3120 BT_CHECK_PARAMETER(bd_addr, return);
3121 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3122 bd_addr->addr[0], bd_addr->addr[1],
3123 bd_addr->addr[2], bd_addr->addr[3],
3124 bd_addr->addr[4], bd_addr->addr[5],
3125 profile, restricted);
3127 conn = _bt_gdbus_get_system_gconn();
3128 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3130 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3132 device_path = _bt_get_device_object_path(address);
3133 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3135 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3136 NULL, BT_BLUEZ_NAME, device_path,
3137 BT_DEVICE_INTERFACE, NULL, NULL);
3139 g_free(device_path);
3140 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3142 uuid = _bt_get_restricted_profile_uuid(profile);
3144 g_object_unref(proxy);
3145 return BLUETOOTH_ERROR_NOT_SUPPORT;
3148 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3149 g_variant_new("(sb)", uuid, restricted),
3150 G_DBUS_CALL_FLAGS_NONE, -1,
3152 g_object_unref(proxy);
3154 if (reply == NULL) {
3155 BT_ERR("Failed to Set Profile Restricted");
3156 ret = BLUETOOTH_ERROR_INTERNAL;
3158 BT_ERR("Error %s[%s]", error->message, address);
3159 g_error_free(error);
3163 g_variant_unref(reply);
3170 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3171 bluetooth_restricted_profile_t profile, guint *restricted)
3173 int ret = BLUETOOTH_ERROR_NONE;
3174 GDBusConnection *conn;
3176 GError *error = NULL;
3177 char *device_path = NULL;
3178 guint restricted_profile_flag;
3179 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3182 BT_CHECK_PARAMETER(bd_addr, return);
3183 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3184 bd_addr->addr[0], bd_addr->addr[1],
3185 bd_addr->addr[2], bd_addr->addr[3],
3186 bd_addr->addr[4], bd_addr->addr[5],
3187 profile, *restricted);
3189 conn = _bt_gdbus_get_system_gconn();
3190 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3192 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3194 device_path = _bt_get_device_object_path(address);
3195 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3197 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3198 NULL, BT_BLUEZ_NAME, device_path,
3199 BT_PROPERTIES_INTERFACE, NULL, NULL);
3201 g_free(device_path);
3202 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3204 reply = g_dbus_proxy_call_sync(proxy, "Get",
3205 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3206 G_DBUS_CALL_FLAGS_NONE, -1,
3208 g_object_unref(proxy);
3210 if (reply == NULL) {
3211 BT_ERR("Failed to Get Profile Restricted");
3212 ret = BLUETOOTH_ERROR_INTERNAL;
3214 BT_ERR("Error %s[%s]", error->message, address);
3215 g_error_free(error);
3220 g_variant_get(reply, "(v)", &temp);
3221 restricted_profile_flag = g_variant_get_uint32(temp);
3222 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3224 ret = _bt_get_restricted_profile_from_flag(profile,
3225 restricted_profile_flag, restricted);
3226 g_variant_unref(temp);
3227 g_variant_unref(reply);
3230 BT_DBG("TRUST %d", *restricted);
3234 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3238 GVariant *out_param1;
3239 request_info_t *req_info;
3240 GVariant *val = NULL;
3241 GVariant *param = NULL;
3244 bluetooth_device_address_t device_addr = { {0} };
3245 int result = BLUETOOTH_ERROR_NONE;
3248 val = g_dbus_proxy_call_finish(proxy, res, &err);
3250 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3251 if (req_info == NULL) {
3252 BT_ERR("req_info == NULL");
3253 g_object_unref(proxy);
3254 att_mtu_req_info->device_proxy = NULL;
3259 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3261 if (g_strrstr(err->message, "NotSupported")) {
3262 BT_INFO("Connection Not Supported");
3263 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3264 } else if (g_strrstr(err->message, "NotConnected")) {
3265 BT_INFO("Not connected");
3266 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3267 } else if (g_strrstr(err->message, "InvalidArguments")) {
3268 BT_INFO("Not connected");
3269 result = BLUETOOTH_ERROR_INVALID_PARAM;
3271 BT_DBG("Default case");
3272 result = BLUETOOTH_ERROR_INTERNAL;
3276 g_object_unref(proxy);
3277 att_mtu_req_info->device_proxy = NULL;
3279 if (result != BLUETOOTH_ERROR_NONE)
3283 g_variant_get(val, "(qy)", &mtu, &status);
3284 g_variant_unref(val);
3287 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3289 param = g_variant_new("(isqy)",
3291 att_mtu_req_info->addr,
3295 /* Send the event to application */
3296 _bt_send_event(BT_DEVICE_EVENT,
3297 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3301 if (req_info->context == NULL)
3304 _bt_convert_addr_string_to_type(device_addr.addr,
3305 (const char *)att_mtu_req_info->addr);
3307 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3308 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3309 g_dbus_method_invocation_return_value(req_info->context,
3310 g_variant_new("(iv)", result, out_param1));
3312 _bt_delete_request_list(req_info->req_id);
3317 g_free(att_mtu_req_info->addr);
3318 g_free(att_mtu_req_info);
3319 att_mtu_req_info = NULL;
3324 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3327 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3328 gchar *device_path = NULL;
3329 GDBusProxy *adapter_proxy;
3330 GDBusProxy *device_proxy;
3331 GDBusConnection *conn;
3332 int ret = BLUETOOTH_ERROR_NONE;
3334 BT_CHECK_PARAMETER(device_address, return);
3336 if (att_mtu_req_info) {
3337 BT_ERR("ATT MTU request in progress");
3338 return BLUETOOTH_ERROR_DEVICE_BUSY;
3341 conn = _bt_gdbus_get_system_gconn();
3342 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3344 adapter_proxy = _bt_get_adapter_proxy();
3345 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3347 _bt_convert_addr_type_to_string(address, device_address->addr);
3349 BT_DBG("Remote device address: %s", address);
3351 device_path = _bt_get_device_object_path(address);
3353 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3355 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3356 NULL, BT_BLUEZ_NAME,
3357 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3358 g_free(device_path);
3359 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3361 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3362 att_mtu_req_info->addr = (char *)g_strdup(address);
3363 att_mtu_req_info->req_id = request_id;
3364 att_mtu_req_info->device_proxy = device_proxy;
3366 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3367 g_variant_new("(q)", mtu),
3368 G_DBUS_CALL_FLAGS_NONE,
3369 BT_MAX_DBUS_TIMEOUT,
3371 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3377 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3380 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3381 char *object_path = NULL;
3383 GDBusProxy *device_proxy;
3384 GError *error = NULL;
3386 GDBusConnection *conn;
3387 GVariant *result = NULL;
3389 BT_CHECK_PARAMETER(device_address, return);
3391 conn = _bt_gdbus_get_system_gconn();
3392 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3394 _bt_convert_addr_type_to_string(address, device_address->addr);
3396 object_path = _bt_get_device_object_path(address);
3397 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3399 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3400 NULL, BT_BLUEZ_NAME, object_path,
3401 BT_PROPERTIES_INTERFACE, NULL, NULL);
3402 g_free(object_path);
3403 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3405 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3406 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3407 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3409 ERR("DBus Error : %s", error->message);
3410 g_clear_error(&error);
3411 g_object_unref(device_proxy);
3412 return BLUETOOTH_ERROR_INTERNAL;
3414 g_variant_get(result, "(v)", &value);
3415 *mtu = g_variant_get_uint16(value);
3416 BT_DBG("ATT MTU : %d", *mtu);
3417 g_variant_unref(value);
3418 g_variant_unref(result);
3421 g_object_unref(device_proxy);
3422 return BLUETOOTH_ERROR_NONE;
3425 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3426 bluetooth_device_address_t *id_address)
3428 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3429 gchar *device_path = NULL;
3430 const gchar *idaddress = NULL;
3431 GDBusProxy *device_proxy;
3432 GError *error = NULL;
3433 GVariant *result = NULL;
3434 GDBusConnection *conn;
3435 int ret = BLUETOOTH_ERROR_NONE;
3437 BT_CHECK_PARAMETER(device_address, return);
3439 conn = _bt_gdbus_get_system_gconn();
3440 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3442 _bt_convert_addr_type_to_string(address, device_address->addr);
3444 device_path = _bt_get_device_object_path(address);
3445 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3447 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3448 NULL, BT_BLUEZ_NAME,
3449 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3450 g_free(device_path);
3451 if (!device_proxy) {
3452 BT_ERR("Unable to get proxy");
3453 return BLUETOOTH_ERROR_INTERNAL;
3456 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3457 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3458 if (result == NULL) {
3459 BT_ERR("Failed to get device ID address");
3460 if (error != NULL) {
3461 BT_ERR("Error occured in Proxy call [%s]", error->message);
3462 g_error_free(error);
3464 g_object_unref(device_proxy);
3465 return BLUETOOTH_ERROR_INTERNAL;
3468 g_variant_get(result , "(&s)", &idaddress);
3469 if (idaddress == NULL) {
3470 BT_ERR("No paired device");
3471 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3473 DBG_SECURE("ID Address : %s", idaddress);
3474 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3477 g_variant_unref(result);
3478 g_object_unref(device_proxy);
3483 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3485 GapAgentPrivate *agent = _bt_get_adapter_agent();
3486 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3488 if (authentication_reply)
3489 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3491 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3493 BT_DBG("BT_PASSKEY_REPLY");
3494 return BLUETOOTH_ERROR_NONE;
3497 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3499 GapAgentPrivate *agent = _bt_get_adapter_agent();
3500 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3502 if (confirmation_reply)
3503 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3505 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3507 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3508 return BLUETOOTH_ERROR_NONE;