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.
23 #include <bundle_internal.h>
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-adapter-le.h"
31 #include "bt-service-event.h"
32 #include "bt-service-device.h"
33 #include "bt-service-rfcomm-client.h"
34 #include "bt-service-util.h"
35 #include "bt-service-agent.h"
36 #include "bt-service-network.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-gap-agent.h"
39 #include "bt-service-pbap.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 gboolean _bt_is_device_creating(void)
213 return is_device_creating;
216 gboolean _bt_is_bonding_device_address(const char *address)
218 if (bonding_info == NULL || bonding_info->addr == NULL)
221 if (g_strcmp0(bonding_info->addr, address) == 0) {
222 BT_DBG("[%s] is bonding device", address);
226 BT_DBG("[%s] is NOT bonding device", address);
230 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
232 ret_if(bonding_info == NULL);
233 bonding_info->is_autopair = is_autopair;
236 void __bt_cancel_search_service_done(void)
238 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
239 request_info_t *req_info;
240 bluetooth_device_info_t dev_info;
241 GVariant *out_param1;
243 ret_if(searching_info == NULL);
245 req_info = _bt_get_request_info(searching_info->req_id);
246 if (req_info == NULL) {
247 BT_ERR("req_info == NULL");
251 if (req_info->context == NULL)
254 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
255 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
256 searching_info->addr);
258 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
259 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
262 g_dbus_method_invocation_return_value(req_info->context,
263 g_variant_new("(iv)", result, out_param1));
265 _bt_delete_request_list(req_info->req_id);
269 g_free(searching_info->addr);
270 g_free(searching_info);
271 searching_info = NULL;
274 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
276 ret_if(value == NULL);
277 ret_if(info == NULL);
279 gsize uuid_count = 0;
281 info->uuids = g_variant_dup_strv(value, &uuid_count);
282 info->uuid_count = (unsigned int)uuid_count;
284 BT_DBG("uuid count : %d", uuid_count);
287 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
289 char *object_path = NULL;
290 bt_remote_dev_info_t *dev_info;
292 retv_if(address == NULL, NULL);
294 object_path = _bt_get_device_object_path(address);
295 retv_if(object_path == NULL, NULL);
297 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
303 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
304 const char *object_path)
306 bt_remote_dev_info_t *dev_info;
307 GDBusProxy *adapter_proxy;
308 GDBusProxy *device_proxy;
313 GDBusConnection *conn;
314 GError *error = NULL;
315 GVariant *result = NULL;
316 GVariantIter *value_iter;
320 adapter_proxy = _bt_get_adapter_proxy();
321 retv_if(adapter_proxy == NULL, NULL);
323 retv_if(object_path == NULL, NULL);
325 conn = _bt_gdbus_get_system_gconn();
327 BT_ERR("conn == NULL");
331 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
332 NULL, BT_BLUEZ_NAME, object_path,
333 BT_PROPERTIES_INTERFACE, NULL, NULL);
335 retv_if(device_proxy == NULL, NULL);
337 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
338 g_variant_new("(s)", BT_DEVICE_INTERFACE),
339 G_DBUS_CALL_FLAGS_NONE,
344 g_object_unref(device_proxy);
346 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
348 if (result != NULL) {
349 g_variant_get(result , "(@a{sv})", &value);
350 g_variant_unref(result);
352 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
354 g_variant_get(tmp_value, "s", &name);
355 g_variant_unref(tmp_value);
357 DBG_SECURE("Alias Name [%s]", name);
359 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
360 g_variant_get(tmp_value, "s", &name);
361 g_variant_unref(tmp_value);
364 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
366 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
367 g_variant_unref(tmp_value);
369 dev_info->is_alias_set = FALSE;
371 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
373 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
375 dev_info->class = g_variant_get_uint32(tmp_value);
376 g_variant_unref(tmp_value);
380 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
382 dev_info->connected = g_variant_get_byte(tmp_value);
383 g_variant_unref(tmp_value);
385 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
386 BT_DBG("connected link : %d", dev_info->connected);
388 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
390 dev_info->trust = g_variant_get_boolean(tmp_value);
391 g_variant_unref(tmp_value);
393 dev_info->trust = FALSE;
395 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
397 dev_info->paired = g_variant_get_boolean(tmp_value);
398 g_variant_unref(tmp_value);
400 dev_info->paired = FALSE;
402 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
404 dev_info->rssi = g_variant_get_int16(tmp_value);
405 g_variant_unref(tmp_value);
409 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
411 dev_info->addr_type = g_variant_get_byte(tmp_value);
412 g_variant_unref(tmp_value);
414 dev_info->addr_type = 0;
416 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
418 __bt_get_uuids(tmp_value, dev_info);
419 g_variant_unref(tmp_value);
422 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
424 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
425 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
426 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
427 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
429 g_variant_unref(tmp_value);
431 dev_info->manufacturer_data_len = 0;
433 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
435 if ((dev_info->manufacturer_data_len == 0) ||
436 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
437 BT_ERR("manufacturer data length doesn't match");
438 dev_info->manufacturer_data_len = 0;
439 dev_info->manufacturer_data = NULL;
441 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
442 g_variant_get(tmp_value, "ay", &value_iter);
443 while (g_variant_iter_loop(value_iter, "y", &m_value))
444 dev_info->manufacturer_data[i++] = m_value;
446 g_variant_unref(tmp_value);
448 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
449 dev_info->manufacturer_data_len = 0;
450 dev_info->manufacturer_data = NULL;
453 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
454 g_variant_get(tmp_value, "s", &address);
455 g_variant_unref(tmp_value);
457 dev_info->address = g_strdup(address);
458 dev_info->name = g_strdup(name);
460 g_variant_unref(value);
462 BT_ERR("result is NULL\n");
470 char *_bt_get_bonded_device_name(char *address)
472 bluetooth_device_address_t device_address = { {0} };
473 bluetooth_device_info_t dev_info;
475 retv_if(address == NULL, strdup(""));
477 _bt_convert_addr_string_to_type(device_address.addr, address);
479 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
481 _bt_get_bonded_device_info(&device_address, &dev_info);
483 return g_strdup(dev_info.device_name.name);
486 static gboolean __ignore_auto_pairing_request(const char *address)
491 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
502 /* Get the LAP(Lower Address part) */
503 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
505 snprintf(lap_address, sizeof(lap_address), ",%s", address);
507 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
510 BT_ERR("fopen failed \n");
514 fseek(fp, 0, SEEK_END);
519 BT_ERR("Get file size failed \n");
524 buffer = g_malloc0(sizeof(char) * size);
525 result = fread((char *)buffer, 1, size, fp);
527 if (result != size) {
528 BT_ERR("Read Error\n");
533 BT_DBG("Buffer = %s\n", buffer);
535 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
541 /* Write the data and insert new device data */
542 for (i = 0; lines[i] != NULL; i++) {
543 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
544 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
546 lines[i] = temp_buffer;
549 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
551 /* Fix : NULL_RETURNS */
552 retv_if(buffer == NULL, FALSE);
554 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
557 BT_ERR("fopen failed \n");
562 BT_DBG("Buffer = %s\n", buffer);
563 fwrite(buffer, 1, strlen(buffer), fp);
573 static int __bt_cancel_bonding(void)
575 BT_CHECK_PARAMETER(bonding_info, return);
576 BT_CHECK_PARAMETER(bonding_info->addr, return);
577 /* First Cancel the ongoing pairing in bluez otherwise if we send
578 * pair request bluez will send inprogress and we again retry bond so
579 * this cycle continues */
581 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
582 NULL, G_DBUS_CALL_FLAGS_NONE,
585 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
587 return BLUETOOTH_ERROR_PARING_FAILED;
589 return BLUETOOTH_ERROR_NONE;
592 static int __bt_retry_bond(void)
594 BT_CHECK_PARAMETER(bonding_info, return);
595 BT_CHECK_PARAMETER(bonding_info->addr, return);
597 is_device_creating = TRUE;
599 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
600 g_variant_new("(y)", bonding_info->conn_type),
601 G_DBUS_CALL_FLAGS_NONE,
604 (GAsyncReadyCallback)__bt_bond_device_cb,
607 return BLUETOOTH_ERROR_NONE;
611 static int __bt_remove_and_bond(void)
613 GDBusProxy *adapter_proxy;
614 GVariant *result = NULL;
616 char *device_path = NULL;
618 BT_CHECK_PARAMETER(bonding_info, return);
619 BT_CHECK_PARAMETER(bonding_info->addr, return);
621 adapter_proxy = _bt_get_adapter_proxy();
622 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
624 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
625 g_variant_new("(s)", bonding_info->addr),
626 G_DBUS_CALL_FLAGS_NONE,
631 return BLUETOOTH_ERROR_INTERNAL;
633 g_variant_get(result , "(o)", &device_path);
634 g_variant_unref(result);
636 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
638 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
639 g_variant_new("(o)", device_path),
640 G_DBUS_CALL_FLAGS_NONE,
646 BT_ERR("UnpairDevice Fail: %s", err->message);
648 return BLUETOOTH_ERROR_INTERNAL;
651 return __bt_retry_bond();
654 static int __bt_cancel_and_bond(void)
656 int ret = BLUETOOTH_ERROR_NONE;
658 ret = _bt_agent_reply_cancellation();
659 if (ret != BLUETOOTH_ERROR_NONE) {
660 BT_ERR("Fail to call reply cancellation");
663 return __bt_retry_bond();
667 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
670 int result = BLUETOOTH_ERROR_NONE;
673 GVariant *out_param1;
674 request_info_t *req_info;
675 bluetooth_device_info_t dev_info;
676 bt_remote_dev_info_t *remote_dev_info;
677 GVariant *manufacture_data;
680 /* Terminate ALL system popup */
681 if (headed_plugin_info->plugin_headed_enabled)
682 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
684 reply = g_dbus_proxy_call_finish(proxy, res, &err);
686 g_variant_unref(reply);
688 is_device_creating = FALSE;
690 if (bonding_info == NULL) {
692 BT_ERR("bonding_info == NULL");
698 req_info = _bt_get_request_info(bonding_info->req_id);
699 if (req_info == NULL) {
700 BT_ERR("req_info == NULL");
705 g_dbus_error_strip_remote_error(err);
706 BT_ERR("Error occured in CreateBonding [%s]", err->message);
708 if (g_strrstr(err->message, "Already Exists")) {
709 BT_INFO("Existing Bond, remove and retry");
710 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
712 result = BLUETOOTH_ERROR_PARING_FAILED;
713 } else if (_bt_agent_is_canceled() ||
714 g_strrstr(err->message, "Authentication Canceled")) {
715 BT_INFO("Cancelled by USER");
716 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
717 } else if (g_strrstr(err->message, "Authentication Rejected")) {
719 result = BLUETOOTH_ERROR_ACCESS_DENIED;
720 } else if (g_strrstr(err->message, "In Progress")) {
721 BT_INFO("Bond in progress, cancel and retry");
722 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
724 result = BLUETOOTH_ERROR_PARING_FAILED;
725 } else if (g_strrstr(err->message, "Authentication Failed")) {
726 BT_INFO("Authentication Failed");
727 if (bonding_info->is_autopair == TRUE) {
728 _bt_set_autopair_status_in_bonding_info(FALSE);
729 __ignore_auto_pairing_request(bonding_info->addr);
731 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
732 } else if (g_strrstr(err->message, "Page Timeout")) {
733 BT_INFO("Page Timeout");
734 /* This is the special case
735 As soon as call bluetooth_bond_device, try to cancel bonding.
736 In this case, before completing to call 'CreatePairedDevice' method
737 the procedure is stopped. So 'Cancle' error is not return.
739 result = BLUETOOTH_ERROR_HOST_DOWN;
740 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
741 BT_INFO("Cancel already running bonding");
742 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
743 BT_INFO("Error while Cancelling bonding");
744 /* we need to unref proxy so continue */
746 result = BLUETOOTH_ERROR_INTERNAL;
747 } else if (g_strrstr(err->message, "Connection Timeout")) {
748 /* Pairing request timeout */
749 result = BLUETOOTH_ERROR_TIMEOUT;
750 } else if (g_strrstr(err->message, "Authentication Timeout")) {
751 /* Pairing request timeout */
752 result = BLUETOOTH_ERROR_TIMEOUT;
754 BT_DBG("Default case");
755 result = BLUETOOTH_ERROR_PARING_FAILED;
759 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
760 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
761 result == BLUETOOTH_ERROR_TIMEOUT ||
762 result == BLUETOOTH_ERROR_HOST_DOWN) {
763 if (TIZEN_PROFILE_WEARABLE) {
764 int is_sw_running = 0;
766 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
767 BT_ERR("vconf_get_int for setup wizard state failed");
769 if (!is_sw_running) {
770 if (headed_plugin_info->plugin_headed_enabled)
771 headed_plugin_info->headed_plugin->bt_launch_unable_to_pairing_syspopup(result);
773 BT_ERR("Unable to pair");
776 bonding_info->result = result;
779 g_object_unref(proxy);
780 bonding_info->device_proxy = NULL;
782 if (result != BLUETOOTH_ERROR_NONE) {
783 if (bonding_info->bonding_wo_discovery) {
784 GDBusProxy *adapter_proxy;
785 GVariant *ret = NULL;
786 GError *error = NULL;
789 BT_ERR("Bond was tried without discovery. Remove it");
791 adapter_proxy = _bt_get_adapter_proxy();
792 if (adapter_proxy == NULL) {
793 BT_ERR("Cannot get adapter_proxy");
797 device_path = _bt_get_device_object_path(bonding_info->addr);
798 if (device_path == NULL) {
799 BT_ERR("Cannot get device path");
803 ret = g_dbus_proxy_call_sync(adapter_proxy,
805 g_variant_new("(o)", device_path),
806 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
809 BT_ERR("RemoveDevice Fail: %s", error->message);
810 g_clear_error(&error);
814 g_variant_unref(ret);
822 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
823 if (!remote_dev_info)
826 GVariant *uuids = NULL;
827 GVariantBuilder *builder = NULL;
829 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
830 for (i = 0; i < remote_dev_info->uuid_count; i++) {
831 g_variant_builder_add(builder, "s",
832 remote_dev_info->uuids[i]);
834 uuids = g_variant_new("as", builder);
835 g_variant_builder_unref(builder);
836 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
837 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
840 param = g_variant_new("(isunsbub@asn@ay)",
843 remote_dev_info->class,
844 remote_dev_info->rssi,
845 remote_dev_info->name,
846 remote_dev_info->paired,
847 remote_dev_info->connected,
848 remote_dev_info->trust,
850 remote_dev_info->manufacturer_data_len,
854 /* Send the event to application */
855 _bt_send_event(BT_ADAPTER_EVENT,
856 BLUETOOTH_EVENT_BONDING_FINISHED,
859 _bt_free_device_info(remote_dev_info);
862 if (req_info->context == NULL)
865 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
866 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
869 if (_bt_adapter_get_status() != BT_ACTIVATED)
870 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
872 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
873 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
875 g_dbus_method_invocation_return_value(req_info->context,
876 g_variant_new("(iv)", result, out_param1));
878 _bt_delete_request_list(req_info->req_id);
883 _bt_agent_set_canceled(FALSE);
886 g_free(bonding_info->addr);
887 g_free(bonding_info);
891 int _bt_bond_device(int request_id,
892 bluetooth_device_address_t *device_address,
893 unsigned short conn_type, GArray **out_param1)
896 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
897 bluetooth_device_info_t dev_info;
899 GDBusConnection *conn;
900 char *device_path = NULL;
901 GDBusProxy *adapter_proxy;
902 GError *error = NULL;
903 gboolean bonding_wo_discovery = FALSE;
905 BT_CHECK_PARAMETER(device_address, return);
908 BT_ERR("Bonding in progress");
910 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
911 memcpy(dev_info.device_address.addr, device_address->addr,
912 BLUETOOTH_ADDRESS_LENGTH);
914 g_array_append_vals(*out_param1, &dev_info,
915 sizeof(bluetooth_device_info_t));
917 return BLUETOOTH_ERROR_DEVICE_BUSY;
920 conn = _bt_gdbus_get_system_gconn();
921 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
923 _bt_convert_addr_type_to_string(address, device_address->addr);
925 device_path = _bt_get_device_object_path(address);
927 if (device_path == NULL) {
928 BT_ERR("No searched device");
929 GVariant *ret = NULL;
930 adapter_proxy = _bt_get_adapter_proxy();
931 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
933 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
934 g_variant_new("(s)", address),
935 G_DBUS_CALL_FLAGS_NONE,
941 BT_ERR("CreateDevice Fail: %s", error->message);
942 g_clear_error(&error);
945 g_variant_unref(ret);
946 device_path = _bt_get_device_object_path(address);
947 if (device_path == NULL) {
948 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
949 memcpy(dev_info.device_address.addr, device_address->addr,
950 BLUETOOTH_ADDRESS_LENGTH);
952 g_array_append_vals(*out_param1, &dev_info,
953 sizeof(bluetooth_device_info_t));
955 return BLUETOOTH_ERROR_NOT_PAIRED;
957 BT_INFO("device_path is created[%s]", device_path);
959 bonding_wo_discovery = TRUE;
962 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
964 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
967 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
969 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
970 bonding_info->addr = g_strdup(address);
971 bonding_info->req_id = request_id;
973 bonding_info->device_proxy = proxy;
974 bonding_info->conn_type = conn_type;
975 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
977 is_device_creating = TRUE;
979 g_dbus_proxy_call(proxy, "Pair",
980 g_variant_new("(y)", conn_type),
981 G_DBUS_CALL_FLAGS_NONE,
984 (GAsyncReadyCallback)__bt_bond_device_cb,
987 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
989 return BLUETOOTH_ERROR_NONE;
991 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
992 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
995 g_array_append_vals(*out_param1, &dev_info,
996 sizeof(bluetooth_device_info_t));
998 is_device_creating = FALSE;
1000 g_free(bonding_info->addr);
1001 g_free(bonding_info);
1002 bonding_info = NULL;
1004 return BLUETOOTH_ERROR_INTERNAL;*/
1007 int _bt_cancel_bonding(void)
1009 int ret = BLUETOOTH_ERROR_NONE;
1011 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1013 ret = _bt_agent_reply_cancellation();
1014 if (ret != BLUETOOTH_ERROR_NONE) {
1015 BT_ERR("Fail to call reply cancellation");
1019 _bt_agent_set_canceled(TRUE);
1021 return BLUETOOTH_ERROR_NONE;
1024 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1029 GVariant *out_param1;
1030 int result = BLUETOOTH_ERROR_NONE;
1031 bt_funcion_data_t *unbonding_info;
1032 bluetooth_device_info_t dev_info;
1033 request_info_t *req_info;
1035 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1037 g_variant_unref(reply);
1039 unbonding_info = user_data;
1041 if (unbonding_info == NULL) {
1043 BT_ERR("unbonding_info == NULL");
1047 req_info = _bt_get_request_info(unbonding_info->req_id);
1048 if (req_info == NULL) {
1049 BT_ERR("req_info == NULL");
1054 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1055 result = BLUETOOTH_ERROR_INTERNAL;
1058 if (req_info->context == NULL)
1061 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1062 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1063 unbonding_info->addr);
1065 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1066 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1068 g_dbus_method_invocation_return_value(req_info->context,
1069 g_variant_new("(iv)", result, out_param1));
1072 _bt_delete_request_list(req_info->req_id);
1078 if (unbonding_info) {
1079 g_free(unbonding_info->addr);
1080 g_free(unbonding_info);
1084 int _bt_unbond_device(int request_id,
1085 bluetooth_device_address_t *device_address,
1086 GArray **out_param1)
1088 char *device_path = NULL;
1089 bt_funcion_data_t *unbonding_info;
1090 GDBusProxy *adapter_proxy = NULL;
1091 GDBusProxy *device_proxy = NULL;
1092 GDBusConnection *conn;
1093 int result = BLUETOOTH_ERROR_INTERNAL;
1094 bluetooth_device_info_t dev_info;
1095 GError *error = NULL;
1096 GVariant *ret = NULL;
1098 BT_CHECK_PARAMETER(device_address, return);
1100 adapter_proxy = _bt_get_adapter_proxy();
1101 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1103 /* allocate user data so that it can be retrieved in callback */
1104 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1105 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1106 unbonding_info->req_id = request_id;
1108 _bt_convert_addr_type_to_string(unbonding_info->addr,
1109 device_address->addr);
1111 device_path = _bt_get_device_object_path(unbonding_info->addr);
1113 if (device_path == NULL) {
1114 BT_ERR("No paired device");
1115 result = BLUETOOTH_ERROR_NOT_PAIRED;
1119 conn = _bt_gdbus_get_system_gconn();
1121 BT_ERR("conn is NULL");
1122 result = BLUETOOTH_ERROR_INTERNAL;
1126 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1127 NULL, BT_BLUEZ_NAME,
1128 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1130 if (device_proxy != NULL) {
1132 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1133 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1134 G_DBUS_CALL_FLAGS_NONE,
1139 BT_ERR("Getting property failed: [%s]\n", error->message);
1140 g_error_free(error);
1141 result = BLUETOOTH_ERROR_NOT_PAIRED;
1145 BT_ERR("No paired device");
1146 g_object_unref(device_proxy);
1147 result = BLUETOOTH_ERROR_NOT_PAIRED;
1150 g_variant_unref(ret);
1152 g_object_unref(device_proxy);
1155 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1156 g_variant_new("(o)", device_path),
1157 G_DBUS_CALL_FLAGS_NONE,
1158 BT_MAX_DBUS_TIMEOUT,
1160 (GAsyncReadyCallback)__bt_unbond_cb,
1163 g_free(device_path);
1164 return BLUETOOTH_ERROR_NONE;
1167 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1168 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1169 unbonding_info->addr);
1171 g_array_append_vals(*out_param1, &dev_info,
1172 sizeof(bluetooth_device_info_t));
1173 g_free(device_path);
1174 g_free(unbonding_info->addr);
1175 g_free(unbonding_info);
1179 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1184 GVariant *out_param1;
1185 int result = BLUETOOTH_ERROR_NONE;
1186 bluetooth_device_info_t dev_info;
1187 bt_remote_dev_info_t *remote_dev_info;
1188 request_info_t *req_info;
1189 GVariant *uuid_list, *manufacture_data;
1191 GVariantBuilder *builder = NULL;
1194 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1196 g_variant_unref(reply);
1198 g_object_unref(proxy);
1200 if (searching_info == NULL) {
1202 BT_ERR("unbonding_info == NULL");
1206 req_info = _bt_get_request_info(searching_info->req_id);
1207 if (req_info == NULL) {
1208 BT_ERR("req_info == NULL");
1213 g_dbus_error_strip_remote_error(err);
1214 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1216 if (g_strrstr("Operation canceled", err->message))
1217 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1218 else if (g_strrstr("In Progress", err->message))
1219 result = BLUETOOTH_ERROR_IN_PROGRESS;
1220 else if (g_strrstr("Host is down", err->message))
1221 result = BLUETOOTH_ERROR_HOST_DOWN;
1223 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1225 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1226 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1227 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1228 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1229 remote_dev_info->uuid_count > 0) {
1230 result = BLUETOOTH_ERROR_NONE;
1233 _bt_free_device_info(remote_dev_info);
1238 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1239 if (!remote_dev_info)
1243 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1244 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1245 g_variant_builder_add(builder, "s",
1246 remote_dev_info->uuids[i]);
1248 uuid_list = g_variant_new("as", builder);
1249 g_variant_builder_unref(builder);
1250 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1251 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1254 param = g_variant_new("(isunsbub@asn@ay)",
1256 searching_info->addr,
1257 remote_dev_info->class,
1258 remote_dev_info->rssi,
1259 remote_dev_info->name,
1260 remote_dev_info->paired,
1261 remote_dev_info->connected,
1262 remote_dev_info->trust,
1264 remote_dev_info->manufacturer_data_len,
1267 /* Send the event to application */
1268 _bt_send_event(BT_ADAPTER_EVENT,
1269 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1272 _bt_free_device_info(remote_dev_info);
1275 if (req_info->context == NULL)
1278 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1279 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1280 searching_info->addr);
1282 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1283 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1285 g_dbus_method_invocation_return_value(req_info->context,
1286 g_variant_new("(iv)", result, out_param1));
1289 _bt_delete_request_list(req_info->req_id);
1294 if (searching_info) {
1295 g_free(searching_info->addr);
1296 g_free(searching_info);
1297 searching_info = NULL;
1301 int _bt_search_device(int request_id,
1302 bluetooth_device_address_t *device_address)
1304 char *device_path = NULL;
1305 GDBusProxy *device_proxy = NULL;
1306 GDBusConnection *conn;
1308 GDBusProxy *adapter_proxy;
1309 int result = BLUETOOTH_ERROR_INTERNAL;
1311 BT_CHECK_PARAMETER(device_address, return);
1314 BT_ERR("Bonding in progress");
1315 return BLUETOOTH_ERROR_DEVICE_BUSY;
1318 if (searching_info) {
1319 BT_ERR("Service searching in progress");
1320 return BLUETOOTH_ERROR_DEVICE_BUSY;
1323 adapter_proxy = _bt_get_adapter_proxy();
1324 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1326 /* allocate user data so that it can be retrieved in callback */
1327 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1328 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1329 searching_info->req_id = request_id;
1331 _bt_convert_addr_type_to_string(searching_info->addr,
1332 device_address->addr);
1334 conn = _bt_gdbus_get_system_gconn();
1335 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1338 device_path = _bt_get_device_object_path(searching_info->addr);
1340 if (device_path == NULL) {
1341 BT_ERR("No paired device");
1342 result = BLUETOOTH_ERROR_NOT_PAIRED;
1346 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1347 NULL, BT_BLUEZ_NAME,
1348 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1349 g_free(device_path);
1350 if (device_proxy == NULL) {
1351 result = BLUETOOTH_ERROR_INTERNAL;
1355 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1356 g_variant_new("(s)", ""),
1357 G_DBUS_CALL_FLAGS_NONE,
1358 BT_MAX_DBUS_TIMEOUT,
1360 (GAsyncReadyCallback)__bt_discover_cb,
1363 searching_info->device_proxy = device_proxy;
1365 return BLUETOOTH_ERROR_NONE;
1368 g_free(searching_info->addr);
1369 g_free(searching_info);
1370 searching_info = NULL;
1374 int _bt_cancel_search_device(void)
1376 GVariant *ret = NULL;
1379 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1381 if (searching_info->device_proxy) {
1382 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1384 G_DBUS_CALL_FLAGS_NONE,
1389 g_variant_unref(ret);
1391 __bt_cancel_search_service_done();
1393 return BLUETOOTH_ERROR_NONE;
1396 int _bt_set_alias(bluetooth_device_address_t *device_address,
1399 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1400 gchar *device_path = NULL;
1401 GDBusProxy *adapter_proxy;
1402 GDBusProxy *device_proxy;
1403 GVariant *ret = NULL;
1404 GError *error = NULL;
1405 GDBusConnection *conn;
1407 BT_CHECK_PARAMETER(device_address, return);
1408 BT_CHECK_PARAMETER(alias, return);
1410 adapter_proxy = _bt_get_adapter_proxy();
1411 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1413 conn = _bt_gdbus_get_system_gconn();
1414 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1416 _bt_convert_addr_type_to_string(address, device_address->addr);
1418 device_path = _bt_get_device_object_path(address);
1420 if (device_path == NULL) {
1421 BT_ERR("No paired device");
1422 return BLUETOOTH_ERROR_NOT_PAIRED;
1425 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1426 NULL, BT_BLUEZ_NAME,
1427 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1429 g_free(device_path);
1430 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1432 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1433 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1434 G_DBUS_CALL_FLAGS_NONE,
1439 g_variant_unref(ret);
1441 g_object_unref(device_proxy);
1444 BT_ERR("SetProperty error: [%s]", error->message);
1445 g_error_free(error);
1446 return BLUETOOTH_ERROR_INTERNAL;
1449 return BLUETOOTH_ERROR_NONE;
1452 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1455 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1456 gchar *device_path = NULL;
1457 GDBusProxy *device_proxy;
1458 gboolean previous_value;
1459 GError *error = NULL;
1460 GDBusConnection *conn;
1461 GVariant *result = NULL;
1462 GVariant *temp = NULL;
1463 int ret = BLUETOOTH_ERROR_NONE;
1465 BT_CHECK_PARAMETER(device_address, return);
1467 conn = _bt_gdbus_get_system_gconn();
1468 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1470 _bt_convert_addr_type_to_string(address, device_address->addr);
1472 device_path = _bt_get_device_object_path(address);
1474 if (device_path == NULL) {
1475 BT_ERR("No paired device");
1476 return BLUETOOTH_ERROR_NOT_PAIRED;
1479 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1480 NULL, BT_BLUEZ_NAME,
1481 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1483 g_free(device_path);
1484 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1486 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1487 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1488 G_DBUS_CALL_FLAGS_NONE,
1492 if (error != NULL) {
1493 BT_ERR("Getting property failed: [%s]\n", error->message);
1494 g_error_free(error);
1495 g_object_unref(device_proxy);
1496 return BLUETOOTH_ERROR_INTERNAL;
1499 g_variant_get(result, "(v)", &temp);
1500 previous_value = g_variant_get_boolean(temp);
1501 g_variant_unref(temp);
1502 g_variant_unref(result);
1503 /* If the input is same with previous value, return error. */
1504 if (previous_value == authorize) {
1505 BT_ERR("Same value: %d", previous_value);
1506 g_object_unref(device_proxy);
1507 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1511 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1512 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1513 G_DBUS_CALL_FLAGS_NONE,
1518 g_object_unref(device_proxy);
1520 BT_ERR("SetProperty error: [%s]", error->message);
1521 g_error_free(error);
1522 ret = BLUETOOTH_ERROR_INTERNAL;
1526 g_variant_unref(result);
1531 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1532 gboolean *is_connected)
1534 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1535 char *object_path = NULL;
1537 GDBusProxy *device_proxy;
1538 GError *error = NULL;
1540 GVariant *tmp_value;
1541 GDBusConnection *conn;
1542 GVariant *result = NULL;
1543 int ret = BLUETOOTH_ERROR_NONE;
1545 BT_CHECK_PARAMETER(device_address, return);
1547 conn = _bt_gdbus_get_system_gconn();
1548 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1550 _bt_convert_addr_type_to_string(address, device_address->addr);
1552 object_path = _bt_get_device_object_path(address);
1553 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1555 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1556 NULL, BT_BLUEZ_NAME, object_path,
1557 BT_PROPERTIES_INTERFACE, NULL, NULL);
1558 g_free(object_path);
1559 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1561 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1562 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1563 G_DBUS_CALL_FLAGS_NONE,
1567 if (result == NULL) {
1568 if (error != NULL) {
1569 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1570 g_error_free(error);
1572 g_object_unref(device_proxy);
1573 return BLUETOOTH_ERROR_INTERNAL;
1576 g_variant_get(result , "(@a{sv})", &value);
1577 g_variant_unref(result);
1579 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1580 if (tmp_value == NULL) {
1581 g_object_unref(device_proxy);
1582 g_variant_unref(value);
1583 return BLUETOOTH_ERROR_INTERNAL;
1586 *is_connected = g_variant_get_boolean(tmp_value);
1588 BT_DBG("gatt is connected : %d", *is_connected);
1590 g_variant_unref(tmp_value);
1591 g_variant_unref(value);
1592 g_object_unref(device_proxy);
1597 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1598 int connection_type, gboolean *is_connected)
1600 char *object_path = NULL;
1601 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1602 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1603 GDBusProxy *device_proxy = NULL;
1604 GDBusProxy *adapter_proxy = NULL;
1605 GDBusConnection *conn;
1606 GError *error = NULL;
1607 GVariant *tmp_value = NULL;
1608 GVariant *value = NULL;
1609 GVariant *result = NULL;
1612 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1613 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1615 _bt_convert_addr_type_to_string(address, device_address->addr);
1616 _bt_convert_addr_string_to_secure_string(secure_address, address);
1618 *is_connected = FALSE;
1619 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1621 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1622 return _bt_rfcomm_is_device_connected(device_address,
1624 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1625 return _bt_is_gatt_connected(device_address, is_connected);
1626 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1627 return _bt_pbap_is_connected(device_address, is_connected);
1629 adapter_proxy = _bt_get_adapter_proxy();
1630 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1632 conn = _bt_gdbus_get_system_gconn();
1633 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1635 _bt_convert_addr_type_to_string(address, device_address->addr);
1637 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1638 object_path = _bt_get_adapter_path();
1639 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1640 NULL, BT_BLUEZ_NAME,
1641 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1642 g_free(object_path);
1643 if (device_proxy == NULL) {
1644 BT_DBG("Device don't have this service");
1645 return BLUETOOTH_ERROR_INTERNAL;
1648 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1649 g_variant_new("(s)", address),
1650 G_DBUS_CALL_FLAGS_NONE,
1655 if (result == NULL) {
1656 BT_ERR("[GetProperties] Error occured in Proxy call");
1658 BT_ERR("%s", error->message);
1659 g_error_free(error);
1661 *is_connected = FALSE;
1662 g_object_unref(device_proxy);
1663 return BLUETOOTH_ERROR_NONE;
1665 g_variant_get(result , "(@a{sv})", &value);
1666 g_variant_unref(result);
1669 tmp_value = g_variant_lookup_value(value,
1671 G_VARIANT_TYPE_BOOLEAN);
1673 *is_connected = g_variant_get_boolean(tmp_value);
1674 g_variant_unref(tmp_value);
1676 g_variant_unref(value);
1678 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1679 return _bt_is_network_connected(_bt_get_net_conn(),
1680 device_address->addr, is_connected);
1682 uuid = _bt_get_profile_uuid128(connection_type);
1684 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1685 return BLUETOOTH_ERROR_INTERNAL;
1687 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1689 object_path = _bt_get_device_object_path(address);
1691 BT_ERR("object_path is NULL");
1693 return BLUETOOTH_ERROR_NOT_PAIRED;
1696 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1697 NULL, BT_BLUEZ_NAME,
1698 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1699 g_free(object_path);
1700 if (device_proxy == NULL) {
1701 BT_DBG("Device don't have this service");
1703 return BLUETOOTH_ERROR_INTERNAL;
1706 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1707 g_variant_new("(s)", uuid),
1708 G_DBUS_CALL_FLAGS_NONE,
1713 if (result == NULL) {
1714 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1716 BT_ERR("%s", error->message);
1717 if (g_strrstr(error->message, "Not Connected"))
1718 BT_DBG("Not connected");
1719 g_error_free(error);
1721 *is_connected = FALSE;
1722 g_object_unref(device_proxy);
1724 return BLUETOOTH_ERROR_NONE;
1726 g_variant_get(result, "(b)", is_connected);
1728 g_variant_unref(result);
1731 g_object_unref(device_proxy);
1732 return BLUETOOTH_ERROR_NONE;
1735 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1736 bluetooth_connected_link_t *connected)
1738 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1739 char *object_path = NULL;
1741 GDBusProxy *device_proxy;
1742 GError *error = NULL;
1743 GDBusConnection *conn;
1744 GVariant *tmp_value = NULL;
1745 GVariant *value = NULL;
1746 GVariant *result = NULL;
1748 BT_CHECK_PARAMETER(device_address, return);
1750 conn = _bt_gdbus_get_system_gconn();
1751 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1753 _bt_convert_addr_type_to_string(address, device_address->addr);
1755 object_path = _bt_get_device_object_path(address);
1756 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1758 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1759 NULL, BT_BLUEZ_NAME,
1760 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1761 g_free(object_path);
1762 if (device_proxy == NULL) {
1763 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1764 return BLUETOOTH_ERROR_NONE;
1767 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1768 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1769 G_DBUS_CALL_FLAGS_NONE,
1774 if (error != NULL) {
1775 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1776 g_error_free(error);
1777 g_object_unref(device_proxy);
1778 return BLUETOOTH_ERROR_INTERNAL;
1781 g_variant_get(result , "(@a{sv})", &value);
1782 g_variant_unref(result);
1784 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1785 if (tmp_value != NULL) {
1786 *connected = g_variant_get_byte(tmp_value);
1787 g_variant_unref(tmp_value);
1788 g_object_unref(device_proxy);
1789 return BLUETOOTH_ERROR_NONE;
1791 BT_ERR("g_variant value is NULL");
1792 return BLUETOOTH_ERROR_INTERNAL;
1796 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1800 GVariant *out_param1;
1802 int result = BLUETOOTH_ERROR_NONE;
1803 bt_function_data_t *func_data = user_data;
1804 request_info_t *req_info = NULL;
1805 bluetooth_device_address_t device_addr = { {0} };
1807 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1808 g_object_unref(proxy);
1810 if (reply == NULL) {
1811 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1813 BT_ERR("Error: %s", err->message);
1814 g_clear_error(&err);
1816 result = BLUETOOTH_ERROR_INTERNAL;
1818 g_variant_unref(reply);
1821 if (func_data == NULL) {
1822 BT_ERR("func_data is NULL");
1826 req_info = _bt_get_request_info(func_data->req_id);
1827 if (req_info == NULL) {
1828 BT_ERR("req_info is NULL");
1832 if (req_info->context == NULL) {
1833 BT_ERR("req_info->context is NULL");
1837 _bt_convert_addr_string_to_type(device_addr.addr,
1838 (const char *)func_data->address);
1840 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1841 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1844 g_dbus_method_invocation_return_value(req_info->context,
1845 g_variant_new("(iv)", result, out_param1));
1849 _bt_delete_request_list(req_info->req_id);
1852 g_free(func_data->address);
1857 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1858 gboolean auto_connect)
1860 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1861 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1862 gchar *device_path = NULL;
1863 GDBusProxy *device_proxy = NULL;
1864 GDBusConnection *conn;
1865 int ret = BLUETOOTH_ERROR_NONE;
1866 bt_function_data_t *func_data;
1868 BT_CHECK_PARAMETER(bd_addr, return);
1870 conn = _bt_gdbus_get_system_gconn();
1871 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1873 _bt_convert_addr_type_to_string(device_address,
1874 (unsigned char *)bd_addr->addr);
1875 device_path = _bt_get_device_object_path(device_address);
1876 if (device_path == NULL) {
1877 BT_ERR_C("device_path NULL : [%s]", device_address);
1878 ret = BLUETOOTH_ERROR_INTERNAL;
1882 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1883 NULL, BT_BLUEZ_NAME,
1884 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1885 g_free(device_path);
1886 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1888 func_data = g_malloc0(sizeof(bt_function_data_t));
1889 func_data->address = g_strdup(device_address);
1890 if (func_data->address == NULL) {
1891 BT_ERR("Unable to allocate memory for address");
1892 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1896 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1897 BT_INFO("Connect LE [%s]", secure_address);
1899 func_data->req_id = req_id;
1901 g_dbus_proxy_call(device_proxy, "ConnectLE",
1902 g_variant_new("(b)", auto_connect),
1903 G_DBUS_CALL_FLAGS_NONE,
1904 BT_MAX_DBUS_TIMEOUT,
1906 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1912 g_object_unref(device_proxy);
1914 g_free(func_data->address);
1920 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1924 BT_INFO("Try to initiate pending LE connection");
1926 pending_le_conn_timer_id = 0;
1928 ret = __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1929 &pending_le_conn_info->bd_addr,
1930 pending_le_conn_info->auto_connect);
1931 if (ret != BLUETOOTH_ERROR_NONE) {
1932 request_info_t *req_info = NULL;
1933 GVariant *out_param1;
1935 req_info = _bt_get_request_info(pending_le_conn_info->req_id);
1936 if (req_info == NULL) {
1937 BT_ERR("req_info is NULL");
1941 if (req_info->context == NULL) {
1942 BT_ERR("req_info->context is NULL");
1943 _bt_delete_request_list(req_info->req_id);
1947 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1948 &pending_le_conn_info->bd_addr,
1949 sizeof(bluetooth_device_address_t), TRUE,
1952 g_dbus_method_invocation_return_value(req_info->context,
1953 g_variant_new("(iv)", ret, out_param1));
1957 g_free(pending_le_conn_info);
1958 pending_le_conn_info = NULL;
1963 void _bt_pending_connect_le_device(void)
1965 if (pending_le_conn_timer_id > 0) {
1966 g_source_remove(pending_le_conn_timer_id);
1967 __bt_connect_le_timer_cb(NULL);
1971 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1972 gboolean auto_connect)
1974 int ret = BLUETOOTH_ERROR_NONE;
1976 BT_CHECK_PARAMETER(bd_addr, return);
1978 ret = _bt_hold_current_advertising();
1979 if (ret == BLUETOOTH_ERROR_NONE) {
1980 BT_INFO("Current advertising is held");
1981 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1982 pending_le_conn_info->req_id = req_id;
1983 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1984 BLUETOOTH_ADDRESS_LENGTH);
1985 pending_le_conn_info->auto_connect = auto_connect;
1987 pending_le_conn_timer_id =
1988 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1990 return BLUETOOTH_ERROR_NONE;
1991 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
1992 BT_ERR("Unable to hold advertising");
1995 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1998 int _bt_disconnect_le_device(int req_id,
1999 const bluetooth_device_address_t *bd_addr)
2001 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2002 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2004 GDBusProxy *device_proxy;
2005 GDBusConnection *conn;
2006 int ret = BLUETOOTH_ERROR_NONE;
2007 bt_function_data_t *func_data;
2009 BT_CHECK_PARAMETER(bd_addr, return);
2011 conn = _bt_gdbus_get_system_gconn();
2012 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2014 _bt_convert_addr_type_to_string(device_address,
2015 (unsigned char *)bd_addr->addr);
2016 device_path = _bt_get_device_object_path(device_address);
2017 if (device_path == NULL) {
2018 BT_DBG("device_path NULL");
2019 ret = BLUETOOTH_ERROR_INTERNAL;
2023 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2025 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2026 NULL, BT_BLUEZ_NAME,
2027 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2028 g_free(device_path);
2029 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2031 func_data = g_malloc0(sizeof(bt_function_data_t));
2032 func_data->address = g_strdup(device_address);
2033 if (func_data->address == NULL) {
2034 BT_ERR("Unable to allocate memory for address");
2035 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2039 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2040 BT_INFO("Disconnect LE [%s]", secure_address);
2042 func_data->req_id = req_id;
2044 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2046 G_DBUS_CALL_FLAGS_NONE,
2047 BT_MAX_DBUS_TIMEOUT,
2049 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2054 g_object_unref(device_proxy);
2056 g_free(func_data->address);
2062 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2064 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2065 gchar *device_path = NULL;
2066 GError *error = NULL;
2067 GDBusProxy *device_proxy = NULL;
2068 GDBusProxy *adapter_proxy;
2069 GDBusConnection *conn;
2070 int ret = BLUETOOTH_ERROR_NONE;
2072 BT_CHECK_PARAMETER(bd_addr, return);
2074 _bt_convert_addr_type_to_string(device_address,
2075 (unsigned char *)bd_addr->addr);
2077 conn = _bt_gdbus_get_system_gconn();
2078 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2080 adapter_proxy = _bt_get_adapter_proxy();
2081 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2083 device_path = _bt_get_device_object_path(device_address);
2084 if (device_path == NULL) {
2085 BT_DBG("device_path NULL");
2086 ret = BLUETOOTH_ERROR_INTERNAL;
2090 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2092 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2093 NULL, BT_BLUEZ_NAME,
2094 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2095 g_free(device_path);
2096 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2098 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2100 G_DBUS_CALL_FLAGS_NONE,
2105 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2106 g_error_free(error);
2107 g_object_unref(device_proxy);
2108 return BLUETOOTH_ERROR_INTERNAL;
2110 g_object_unref(device_proxy);
2112 /* IPSP daemon launch */
2113 GDBusProxy *ipsp_proxy;
2115 ipsp_proxy = _bt_get_ipsp_proxy();
2116 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2118 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2119 NULL, G_DBUS_CALL_FLAGS_NONE,
2120 -1, NULL, NULL, NULL);
2125 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2127 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2128 gchar *device_path = NULL;
2129 GError *error = NULL;
2130 GDBusProxy *device_proxy = NULL;
2131 GDBusProxy *adapter_proxy;
2132 GDBusConnection *conn;
2133 int ret = BLUETOOTH_ERROR_NONE;
2135 BT_CHECK_PARAMETER(bd_addr, return);
2137 _bt_convert_addr_type_to_string(device_address,
2138 (unsigned char *)bd_addr->addr);
2140 conn = _bt_gdbus_get_system_gconn();
2141 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2143 adapter_proxy = _bt_get_adapter_proxy();
2144 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2146 device_path = _bt_get_device_object_path(device_address);
2147 if (device_path == NULL) {
2148 BT_DBG("device_path NULL");
2149 ret = BLUETOOTH_ERROR_INTERNAL;
2153 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2155 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2156 NULL, BT_BLUEZ_NAME,
2157 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2158 g_free(device_path);
2159 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2161 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2163 G_DBUS_CALL_FLAGS_NONE,
2168 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2169 g_error_free(error);
2170 g_object_unref(device_proxy);
2171 return BLUETOOTH_ERROR_INTERNAL;
2174 g_object_unref(device_proxy);
2179 int _bt_connect_profile(char *address, char *uuid,
2180 void *cb, gpointer func_data)
2184 GDBusConnection *conn;
2185 GDBusProxy *adapter_proxy;
2186 GVariant *result = NULL;
2187 GError *error = NULL;
2189 conn = _bt_gdbus_get_system_gconn();
2190 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2192 object_path = _bt_get_device_object_path(address);
2193 if (object_path == NULL) {
2194 BT_ERR("No searched device");
2196 adapter_proxy = _bt_get_adapter_proxy();
2197 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2199 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2200 g_variant_new("(s)", address),
2201 G_DBUS_CALL_FLAGS_NONE,
2206 if (error != NULL) {
2207 BT_ERR("CreateDevice Fail: %s", error->message);
2208 g_error_free(error);
2211 g_variant_unref(result);
2213 object_path = _bt_get_device_object_path(address);
2215 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2217 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2218 NULL, BT_BLUEZ_NAME,
2219 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2220 g_free(object_path);
2221 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2223 g_dbus_proxy_call(proxy, "ConnectProfile",
2224 g_variant_new("(s)", uuid),
2225 G_DBUS_CALL_FLAGS_NONE,
2226 BT_MAX_DBUS_TIMEOUT,
2228 (GAsyncReadyCallback)cb,
2231 return BLUETOOTH_ERROR_NONE;
2234 int _bt_disconnect_all(char *address)
2236 int ret = BLUETOOTH_ERROR_NONE;
2239 GDBusConnection *conn;
2240 GVariant *result = NULL;
2244 conn = _bt_gdbus_get_system_gconn();
2245 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2247 object_path = _bt_get_device_object_path(address);
2248 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2250 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2251 NULL, BT_BLUEZ_NAME,
2252 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2253 g_free(object_path);
2254 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2256 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2258 G_DBUS_CALL_FLAGS_NONE,
2263 BT_ERR("Dbus Call Error:[%s]", err->message);
2265 ret = BLUETOOTH_ERROR_INTERNAL;
2268 g_object_unref(proxy);
2270 g_variant_unref(result);
2275 int _bt_disconnect_profile(char *address, char *uuid,
2276 void *cb, gpointer func_data)
2280 GDBusConnection *conn;
2282 conn = _bt_gdbus_get_system_gconn();
2283 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2285 object_path = _bt_get_device_object_path(address);
2286 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2288 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2289 NULL, BT_BLUEZ_NAME,
2290 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2291 g_free(object_path);
2292 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2294 g_dbus_proxy_call(proxy, "DisconnectProfile",
2295 g_variant_new("(s)", uuid),
2296 G_DBUS_CALL_FLAGS_NONE,
2297 BT_MAX_DBUS_TIMEOUT,
2299 (GAsyncReadyCallback)cb,
2302 return BLUETOOTH_ERROR_NONE;
2305 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2306 int low_threshold, int in_range_threshold, int high_threshold)
2308 int ret = BLUETOOTH_ERROR_NONE;
2310 GVariant *result = NULL;
2311 GError *error = NULL;
2312 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2314 BT_CHECK_PARAMETER(bd_addr, return);
2315 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2316 bd_addr->addr[0], bd_addr->addr[1],
2317 bd_addr->addr[2], bd_addr->addr[3],
2318 bd_addr->addr[4], bd_addr->addr[5],
2320 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2321 in_range_threshold, high_threshold);
2323 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2325 proxy = _bt_get_adapter_proxy();
2326 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2328 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2329 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2330 G_DBUS_CALL_FLAGS_NONE,
2334 if (error != NULL) {
2335 BT_ERR("Dbus Call Error:[%s]", error->message);
2336 g_error_free(error);
2337 ret = BLUETOOTH_ERROR_INTERNAL;
2341 g_variant_unref(result);
2346 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2349 int ret = BLUETOOTH_ERROR_NONE;
2351 GVariant *result = NULL;
2352 GError *error = NULL;
2353 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2355 BT_CHECK_PARAMETER(bd_addr, return);
2356 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2357 bd_addr->addr[0], bd_addr->addr[1],
2358 bd_addr->addr[2], bd_addr->addr[3],
2359 bd_addr->addr[4], bd_addr->addr[5],
2362 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2364 proxy = _bt_get_adapter_proxy();
2365 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2367 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2368 g_variant_new("(si)", address, link_type),
2369 G_DBUS_CALL_FLAGS_NONE,
2374 if (error != NULL) {
2375 BT_ERR("Dbus Call Error:[%s]", error->message);
2376 g_error_free(error);
2377 ret = BLUETOOTH_ERROR_INTERNAL;
2381 g_variant_unref(result);
2386 static int __bt_le_set_conn_parameter(const char *address,
2387 float interval_min, float interval_max,
2388 guint16 latency, guint16 time_out)
2390 gchar *device_path = NULL;
2391 GError *error = NULL;
2392 GDBusProxy *device_proxy = NULL;
2393 GDBusConnection *conn;
2395 guint32 min, max, to;
2396 int ret = BLUETOOTH_ERROR_NONE;
2398 BT_CHECK_PARAMETER(address, return);
2400 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2401 interval_min, interval_max, latency, time_out);
2403 device_path = _bt_get_device_object_path((char *)address);
2405 if (device_path == NULL) {
2406 BT_ERR("device_path NULL");
2407 ret = BLUETOOTH_ERROR_INTERNAL;
2411 conn = _bt_gdbus_get_system_gconn();
2413 BT_ERR("conn NULL");
2414 ret = BLUETOOTH_ERROR_INTERNAL;
2418 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2419 NULL, BT_BLUEZ_NAME,
2420 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2422 g_free(device_path);
2423 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2425 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2426 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2427 to = time_out / BT_LE_CONN_TO_SPLIT;
2429 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2430 g_variant_new("(uuuu)", min, max, latency, to),
2431 G_DBUS_CALL_FLAGS_NONE,
2436 g_object_unref(device_proxy);
2437 if (reply == NULL) {
2439 BT_ERR("Error %s[%s]", error->message, address);
2440 if (g_strrstr(error->message, "In Progress"))
2441 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2443 ret = BLUETOOTH_ERROR_INTERNAL;
2444 g_error_free(error);
2448 g_variant_unref(reply);
2454 static void __bt_le_conn_param_free(void *data)
2456 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2458 BT_DBG("%s", param->sender);
2459 g_free(param->sender);
2463 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2465 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2466 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2468 return parama->key > paramb->key;
2471 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2474 bt_connected_le_dev_t *dev;
2479 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2482 if (g_strcmp0(dev->address, address) == 0)
2488 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2491 bt_le_conn_param_t *param = NULL;
2493 if (!dev || !sender)
2496 for (l = dev->senders; l; l = g_slist_next(l)) {
2498 if (g_strcmp0(param->sender, sender) == 0)
2505 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2506 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2508 bt_connected_le_dev_t *dev = NULL;
2509 bt_le_conn_param_t *param = NULL;
2510 bt_le_conn_param_t *data = NULL;
2512 if (!address || !sender)
2513 return BLUETOOTH_ERROR_INVALID_PARAM;
2515 dev = __bt_get_le_connected_dev_info(address);
2517 return BLUETOOTH_ERROR_INTERNAL;
2519 param = __bt_get_le_conn_param_info(dev, sender);
2521 data = g_malloc0(sizeof(bt_le_conn_param_t));
2522 data->sender = g_strdup(sender);
2523 data->interval_min = interval_min;
2524 data->interval_max = interval_max;
2525 data->latency = latency;
2526 data->time_out = time_out;
2527 data->key = interval_min + (interval_max - interval_min)/2;
2529 if (param == NULL) {
2530 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2531 dev->senders = g_slist_append(dev->senders, data);
2533 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2534 dev->senders = g_slist_remove(dev->senders, param);
2535 g_free(param->sender);
2537 dev->senders = g_slist_append(dev->senders, data);
2540 /* Sorting. First element have the minimum interval */
2541 dev->senders = g_slist_sort(dev->senders,
2542 (GCompareFunc)__bt_compare_le_conn_param_key);
2544 return BLUETOOTH_ERROR_NONE;
2547 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2549 bt_connected_le_dev_t *dev = NULL;
2550 bt_le_conn_param_t *param = NULL;
2552 if (!address || !sender)
2553 return BLUETOOTH_ERROR_INVALID_PARAM;
2555 dev = __bt_get_le_connected_dev_info(address);
2557 return BLUETOOTH_ERROR_INTERNAL;
2559 param = __bt_get_le_conn_param_info(dev, sender);
2561 BT_DBG("Remove param %s %s ", address, sender);
2562 dev->senders = g_slist_remove(dev->senders, param);
2563 g_free(param->sender);
2567 *is_removed = FALSE;
2569 return BLUETOOTH_ERROR_NONE;
2572 int _bt_remove_all_le_conn_param_info(const char *sender)
2575 bt_connected_le_dev_t *dev = NULL;
2576 bt_le_conn_param_t *param = NULL;
2577 gboolean is_removed = FALSE;
2578 char *sender_new = NULL;
2579 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2580 int ret = BLUETOOTH_ERROR_NONE;
2583 return BLUETOOTH_ERROR_INVALID_PARAM;
2585 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2587 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2590 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2591 g_slist_length(dev->senders));
2592 if (g_slist_length(dev->senders) > 0) {
2593 param = dev->senders->data;
2594 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2595 param->interval_min, param->interval_max);
2597 if (dev->interval_min != param->interval_min ||
2598 dev->interval_max != param->interval_max) {
2599 sender_new = g_strdup(param->sender);
2601 _bt_convert_addr_string_to_type(addr, dev->address);
2602 ret = _bt_le_conn_update(sender_new, addr,
2603 param->interval_min, param->interval_max,
2604 param->latency, param->time_out);
2607 if (ret != BLUETOOTH_ERROR_NONE)
2608 BT_ERR("Unable to set le connection parameter");
2611 BT_INFO("Set the default interval");
2613 bluetooth_le_connection_param_t param = { 0 };
2614 _bt_get_le_connection_parameter(
2615 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2618 ret = __bt_le_set_conn_parameter(dev->address,
2619 param.interval_min, param.interval_max,
2620 param.latency, param.timeout);
2621 if (ret == BLUETOOTH_ERROR_NONE) {
2622 dev->interval_min = param.interval_min;
2623 dev->interval_max = param.interval_max;
2632 void _bt_add_le_connected_dev_info(const char *address)
2634 bt_connected_le_dev_t *dev = NULL;
2639 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2640 dev->address = g_strdup(address);
2642 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2647 void _bt_remove_le_connected_dev_info(const char *address)
2649 bt_connected_le_dev_t *dev = NULL;
2654 dev = __bt_get_le_connected_dev_info(address);
2658 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2659 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2660 g_free(dev->address);
2666 int _bt_le_conn_update(const char *sender,
2667 unsigned char *device_address,
2668 float interval_min, float interval_max,
2669 guint16 latency, guint16 time_out)
2671 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2672 guint32 min_supervision_to;
2673 bt_connected_le_dev_t *dev = NULL;
2674 bt_le_conn_param_t *param = NULL;
2675 gboolean is_removed = FALSE;
2676 int ret = BLUETOOTH_ERROR_NONE;
2678 BT_CHECK_PARAMETER(device_address, return);
2680 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2681 sender, interval_min, interval_max, latency, time_out);
2683 if (interval_min > interval_max ||
2684 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2685 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2686 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2690 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2691 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2692 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2696 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2697 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2702 * The Supervision_Timeout in milliseconds shall be larger than
2703 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2704 * where Conn_Interval_Max is given in milliseconds.
2706 min_supervision_to = (1 + latency) * interval_max * 2;
2707 if (time_out <= min_supervision_to) {
2708 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2712 _bt_convert_addr_type_to_string(address, device_address);
2713 BT_DBG("Remote device address: %s", address);
2715 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2717 dev = __bt_get_le_connected_dev_info(address);
2719 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2723 if (g_slist_length(dev->senders) == 1)
2726 param = dev->senders->data;
2728 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2730 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2731 BT_DBG("Skip due to same interval");
2735 interval_min = param->interval_min;
2736 interval_max = param->interval_max;
2740 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2742 if (ret != BLUETOOTH_ERROR_NONE) {
2743 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2747 dev->interval_min = interval_min;
2748 dev->interval_max = interval_max;
2754 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2755 bluetooth_device_pin_code_t *pin_code)
2757 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2759 bt_pin_code_info_t *pin_info = NULL;
2761 BT_CHECK_PARAMETER(device_address, return);
2762 BT_CHECK_PARAMETER(pin_code, return);
2763 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2764 BLUETOOTH_ERROR_NO_RESOURCES);
2766 _bt_convert_addr_type_to_string(address, device_address->addr);
2768 for (l = pin_info_list; l != NULL; l = l->next) {
2771 if (g_strcmp0(pin_info->address, address) == 0) {
2772 g_free(pin_info->pin_code);
2773 pin_info->pin_code = g_strdup(pin_code->pin_code);
2774 return BLUETOOTH_ERROR_NONE;
2778 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2779 pin_info->address = g_strdup(address);
2780 pin_info->pin_code = g_strdup(pin_code->pin_code);
2781 pin_info_list = g_slist_append(pin_info_list, pin_info);
2783 return BLUETOOTH_ERROR_NONE;
2786 gint __bt_compare_address(gpointer *a, gpointer *b)
2788 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2789 char *address = (char *)b;
2790 return g_strcmp0(pin_info->address, address);
2793 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2795 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2797 bt_pin_code_info_t *pin_info = NULL;
2799 BT_CHECK_PARAMETER(device_address, return);
2801 _bt_convert_addr_type_to_string(address, device_address->addr);
2803 l = g_slist_find_custom(pin_info_list, address,
2804 (GCompareFunc)__bt_compare_address);
2808 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2809 g_free(pin_info->address);
2810 g_free(pin_info->pin_code);
2814 return BLUETOOTH_ERROR_NONE;
2817 int _bt_get_device_pin_code(const char *address, char *pin_code)
2821 BT_CHECK_PARAMETER(address, return);
2822 BT_CHECK_PARAMETER(pin_code, return);
2824 for (l = pin_info_list; l != NULL; l = l->next) {
2825 bt_pin_code_info_t *pin_info = l->data;
2827 if (g_strcmp0(pin_info->address, address) == 0) {
2828 g_strlcpy(pin_code, pin_info->pin_code,
2829 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2831 return BLUETOOTH_ERROR_NONE;
2835 return BLUETOOTH_ERROR_NOT_FOUND;
2838 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2839 bluetooth_le_connection_param_t *param)
2842 return BLUETOOTH_ERROR_INVALID_PARAM;
2844 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2845 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2846 return BLUETOOTH_ERROR_INVALID_PARAM;
2848 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2851 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2852 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2853 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2854 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2855 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2858 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2859 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2860 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2861 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2862 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2865 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2866 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2867 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2868 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2869 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2873 BT_ERR("Unhandled mode : %d", mode);
2877 return BLUETOOTH_ERROR_NONE;
2880 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2881 guint trusted_profile_flag, guint *trusted)
2887 case TRUSTED_PROFILE_PBAP:
2888 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2889 trust_profile = trusted_profile_flag & (1 << 1);
2891 return BLUETOOTH_ERROR_NOT_SUPPORT;
2893 case TRUSTED_PROFILE_MAP:
2894 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2895 trust_profile = trusted_profile_flag & (1 << 3);
2897 return BLUETOOTH_ERROR_NOT_SUPPORT;
2899 case TRUSTED_PROFILE_SAP:
2900 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2901 trust_profile = trusted_profile_flag & (1 << 5);
2903 return BLUETOOTH_ERROR_NOT_SUPPORT;
2905 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2906 *trusted = trusted_profile_flag;
2907 return BLUETOOTH_ERROR_NONE;
2909 return BLUETOOTH_ERROR_NOT_SUPPORT;
2915 return BLUETOOTH_ERROR_NONE;
2918 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2919 guint restricted_profile_flag, guint *restricted)
2921 int restrict_profile;
2922 *restricted = FALSE;
2925 case RESTRICTED_PROFILE_HFP_HS:
2926 restrict_profile = restricted_profile_flag & (1 << 0);
2928 case RESTRICTED_PROFILE_A2DP:
2929 restrict_profile = restricted_profile_flag & (1 << 2);
2932 return BLUETOOTH_ERROR_NOT_SUPPORT;
2935 if (restrict_profile)
2938 return BLUETOOTH_ERROR_NONE;
2941 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2944 case TRUSTED_PROFILE_PBAP:
2945 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2946 case TRUSTED_PROFILE_MAP:
2947 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2948 case TRUSTED_PROFILE_SAP:
2949 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2950 case TRUSTED_PROFILE_ALL:
2957 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2960 case RESTRICTED_PROFILE_HFP_HS:
2961 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2962 case RESTRICTED_PROFILE_A2DP:
2963 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2969 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2971 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2972 return TRUSTED_PROFILE_PBAP;
2973 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2974 return TRUSTED_PROFILE_MAP;
2975 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2976 return TRUSTED_PROFILE_SAP;
2978 return 0; /* 0 - Unknown Profile */
2981 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2982 bluetooth_trusted_profile_t profile, gboolean trust)
2984 int ret = BLUETOOTH_ERROR_NONE;
2985 GDBusConnection *conn;
2987 GError *error = NULL;
2988 char *device_path = NULL;
2990 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2993 BT_CHECK_PARAMETER(bd_addr, return);
2994 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2995 bd_addr->addr[0], bd_addr->addr[1],
2996 bd_addr->addr[2], bd_addr->addr[3],
2997 bd_addr->addr[4], bd_addr->addr[5],
3000 conn = _bt_gdbus_get_system_gconn();
3001 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3003 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3005 device_path = _bt_get_device_object_path(address);
3006 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3008 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3009 NULL, BT_BLUEZ_NAME, device_path,
3010 BT_DEVICE_INTERFACE, NULL, NULL);
3012 g_free(device_path);
3013 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3015 uuid = _bt_get_trusted_profile_uuid(profile);
3017 g_object_unref(proxy);
3018 return BLUETOOTH_ERROR_NOT_SUPPORT;
3021 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3022 g_variant_new("(sb)", uuid, trust),
3023 G_DBUS_CALL_FLAGS_NONE, -1,
3025 g_object_unref(proxy);
3027 if (reply == NULL) {
3028 BT_ERR("Failed to Set Profile Trusted");
3029 ret = BLUETOOTH_ERROR_INTERNAL;
3031 BT_ERR("Error %s[%s]", error->message, address);
3032 g_error_free(error);
3036 g_variant_unref(reply);
3043 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3044 bluetooth_trusted_profile_t profile, guint *trust)
3046 int ret = BLUETOOTH_ERROR_NONE;
3047 GDBusConnection *conn;
3049 GError *error = NULL;
3050 char *device_path = NULL;
3051 guint trusted_profile_flag;
3052 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3055 BT_CHECK_PARAMETER(bd_addr, return);
3056 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3057 bd_addr->addr[0], bd_addr->addr[1],
3058 bd_addr->addr[2], bd_addr->addr[3],
3059 bd_addr->addr[4], bd_addr->addr[5],
3062 conn = _bt_gdbus_get_system_gconn();
3063 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3065 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3067 device_path = _bt_get_device_object_path(address);
3068 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3070 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3071 NULL, BT_BLUEZ_NAME, device_path,
3072 BT_PROPERTIES_INTERFACE, NULL, NULL);
3074 g_free(device_path);
3075 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3077 reply = g_dbus_proxy_call_sync(proxy, "Get",
3078 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3079 G_DBUS_CALL_FLAGS_NONE, -1,
3081 g_object_unref(proxy);
3083 if (reply == NULL) {
3084 BT_ERR("Failed to Get Profile Trusted");
3085 ret = BLUETOOTH_ERROR_INTERNAL;
3087 BT_ERR("Error %s[%s]", error->message, address);
3088 g_error_free(error);
3093 g_variant_get(reply, "(v)", &temp);
3094 trusted_profile_flag = g_variant_get_uint32(temp);
3095 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3097 ret = _bt_get_trusted_profile_from_flag(profile,
3098 trusted_profile_flag, trust);
3099 g_variant_unref(temp);
3100 g_variant_unref(reply);
3103 BT_DBG("TRUST %d", *trust);
3107 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3108 bluetooth_restricted_profile_t profile, gboolean restricted)
3110 int ret = BLUETOOTH_ERROR_NONE;
3111 GDBusConnection *conn;
3113 GError *error = NULL;
3114 char *device_path = NULL;
3116 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3119 BT_CHECK_PARAMETER(bd_addr, return);
3120 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3121 bd_addr->addr[0], bd_addr->addr[1],
3122 bd_addr->addr[2], bd_addr->addr[3],
3123 bd_addr->addr[4], bd_addr->addr[5],
3124 profile, restricted);
3126 conn = _bt_gdbus_get_system_gconn();
3127 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3129 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3131 device_path = _bt_get_device_object_path(address);
3132 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3134 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3135 NULL, BT_BLUEZ_NAME, device_path,
3136 BT_DEVICE_INTERFACE, NULL, NULL);
3138 g_free(device_path);
3139 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3141 uuid = _bt_get_restricted_profile_uuid(profile);
3143 g_object_unref(proxy);
3144 return BLUETOOTH_ERROR_NOT_SUPPORT;
3147 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3148 g_variant_new("(sb)", uuid, restricted),
3149 G_DBUS_CALL_FLAGS_NONE, -1,
3151 g_object_unref(proxy);
3153 if (reply == NULL) {
3154 BT_ERR("Failed to Set Profile Restricted");
3155 ret = BLUETOOTH_ERROR_INTERNAL;
3157 BT_ERR("Error %s[%s]", error->message, address);
3158 g_error_free(error);
3162 g_variant_unref(reply);
3169 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3170 bluetooth_restricted_profile_t profile, guint *restricted)
3172 int ret = BLUETOOTH_ERROR_NONE;
3173 GDBusConnection *conn;
3175 GError *error = NULL;
3176 char *device_path = NULL;
3177 guint restricted_profile_flag;
3178 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3181 BT_CHECK_PARAMETER(bd_addr, return);
3182 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3183 bd_addr->addr[0], bd_addr->addr[1],
3184 bd_addr->addr[2], bd_addr->addr[3],
3185 bd_addr->addr[4], bd_addr->addr[5],
3186 profile, *restricted);
3188 conn = _bt_gdbus_get_system_gconn();
3189 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3191 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3193 device_path = _bt_get_device_object_path(address);
3194 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3196 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3197 NULL, BT_BLUEZ_NAME, device_path,
3198 BT_PROPERTIES_INTERFACE, NULL, NULL);
3200 g_free(device_path);
3201 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3203 reply = g_dbus_proxy_call_sync(proxy, "Get",
3204 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3205 G_DBUS_CALL_FLAGS_NONE, -1,
3207 g_object_unref(proxy);
3209 if (reply == NULL) {
3210 BT_ERR("Failed to Get Profile Restricted");
3211 ret = BLUETOOTH_ERROR_INTERNAL;
3213 BT_ERR("Error %s[%s]", error->message, address);
3214 g_error_free(error);
3219 g_variant_get(reply, "(v)", &temp);
3220 restricted_profile_flag = g_variant_get_uint32(temp);
3221 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3223 ret = _bt_get_restricted_profile_from_flag(profile,
3224 restricted_profile_flag, restricted);
3225 g_variant_unref(temp);
3226 g_variant_unref(reply);
3229 BT_DBG("TRUST %d", *restricted);
3233 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3237 GVariant *out_param1;
3238 request_info_t *req_info;
3239 GVariant *val = NULL;
3240 GVariant *param = NULL;
3243 bluetooth_device_address_t device_addr = { {0} };
3244 int result = BLUETOOTH_ERROR_NONE;
3247 val = g_dbus_proxy_call_finish(proxy, res, &err);
3249 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3250 if (req_info == NULL) {
3251 BT_ERR("req_info == NULL");
3252 g_object_unref(proxy);
3253 att_mtu_req_info->device_proxy = NULL;
3258 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3260 if (g_strrstr(err->message, "NotSupported")) {
3261 BT_INFO("Connection Not Supported");
3262 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3263 } else if (g_strrstr(err->message, "NotConnected")) {
3264 BT_INFO("Not connected");
3265 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3266 } else if (g_strrstr(err->message, "InvalidArguments")) {
3267 BT_INFO("Not connected");
3268 result = BLUETOOTH_ERROR_INVALID_PARAM;
3270 BT_DBG("Default case");
3271 result = BLUETOOTH_ERROR_INTERNAL;
3275 g_object_unref(proxy);
3276 att_mtu_req_info->device_proxy = NULL;
3278 if (result != BLUETOOTH_ERROR_NONE)
3282 g_variant_get(val, "(qy)", &mtu, &status);
3283 g_variant_unref(val);
3286 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3288 param = g_variant_new("(isqy)",
3290 att_mtu_req_info->addr,
3294 /* Send the event to application */
3295 _bt_send_event(BT_DEVICE_EVENT,
3296 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3300 if (req_info->context == NULL)
3303 _bt_convert_addr_string_to_type(device_addr.addr,
3304 (const char *)att_mtu_req_info->addr);
3306 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3307 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3308 g_dbus_method_invocation_return_value(req_info->context,
3309 g_variant_new("(iv)", result, out_param1));
3311 _bt_delete_request_list(req_info->req_id);
3316 g_free(att_mtu_req_info->addr);
3317 g_free(att_mtu_req_info);
3318 att_mtu_req_info = NULL;
3323 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3326 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3327 gchar *device_path = NULL;
3328 GDBusProxy *adapter_proxy;
3329 GDBusProxy *device_proxy;
3330 GDBusConnection *conn;
3331 int ret = BLUETOOTH_ERROR_NONE;
3333 BT_CHECK_PARAMETER(device_address, return);
3335 if (att_mtu_req_info) {
3336 BT_ERR("ATT MTU request in progress");
3337 return BLUETOOTH_ERROR_DEVICE_BUSY;
3340 conn = _bt_gdbus_get_system_gconn();
3341 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3343 adapter_proxy = _bt_get_adapter_proxy();
3344 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3346 _bt_convert_addr_type_to_string(address, device_address->addr);
3348 BT_DBG("Remote device address: %s", address);
3350 device_path = _bt_get_device_object_path(address);
3352 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3354 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3355 NULL, BT_BLUEZ_NAME,
3356 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3357 g_free(device_path);
3358 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3360 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3361 att_mtu_req_info->addr = (char *)g_strdup(address);
3362 att_mtu_req_info->req_id = request_id;
3363 att_mtu_req_info->device_proxy = device_proxy;
3365 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3366 g_variant_new("(q)", mtu),
3367 G_DBUS_CALL_FLAGS_NONE,
3368 BT_MAX_DBUS_TIMEOUT,
3370 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3376 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3379 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3380 char *object_path = NULL;
3382 GDBusProxy *device_proxy;
3383 GError *error = NULL;
3385 GVariant *tmp_value;
3386 GDBusConnection *conn;
3387 GVariant *result = NULL;
3388 int ret = BLUETOOTH_ERROR_NONE;
3390 BT_CHECK_PARAMETER(device_address, return);
3392 conn = _bt_gdbus_get_system_gconn();
3393 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3395 _bt_convert_addr_type_to_string(address, device_address->addr);
3397 object_path = _bt_get_device_object_path(address);
3398 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3400 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3401 NULL, BT_BLUEZ_NAME, object_path,
3402 BT_PROPERTIES_INTERFACE, NULL, NULL);
3403 g_free(object_path);
3404 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3406 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3407 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3408 G_DBUS_CALL_FLAGS_NONE,
3412 if (result == NULL) {
3413 if (error != NULL) {
3414 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3415 g_error_free(error);
3417 g_object_unref(device_proxy);
3418 return BLUETOOTH_ERROR_INTERNAL;
3421 g_variant_get(result , "(@a{sv})", &value);
3422 g_variant_unref(result);
3424 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3425 if (tmp_value == NULL) {
3426 g_object_unref(device_proxy);
3427 g_variant_unref(value);
3428 return BLUETOOTH_ERROR_INTERNAL;
3431 *mtu = g_variant_get_uint16(tmp_value);
3433 BT_DBG("ATT MTU : %d", *mtu);
3435 g_variant_unref(tmp_value);
3436 g_variant_unref(value);
3437 g_object_unref(device_proxy);
3442 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3443 bluetooth_device_address_t *id_address)
3445 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3446 gchar *device_path = NULL;
3447 const gchar *idaddress = NULL;
3448 GDBusProxy *device_proxy;
3449 GError *error = NULL;
3450 GVariant *result = NULL;
3451 GDBusConnection *conn;
3452 int ret = BLUETOOTH_ERROR_NONE;
3454 BT_CHECK_PARAMETER(device_address, return);
3456 conn = _bt_gdbus_get_system_gconn();
3457 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3459 _bt_convert_addr_type_to_string(address, device_address->addr);
3461 device_path = _bt_get_device_object_path(address);
3462 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3464 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3465 NULL, BT_BLUEZ_NAME,
3466 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3467 g_free(device_path);
3468 if (!device_proxy) {
3469 BT_ERR("Unable to get proxy");
3470 return BLUETOOTH_ERROR_INTERNAL;
3473 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3475 G_DBUS_CALL_FLAGS_NONE,
3480 if (result == NULL) {
3481 BT_ERR("Failed to get device ID address");
3482 if (error != NULL) {
3483 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3484 g_error_free(error);
3486 g_object_unref(device_proxy);
3487 return BLUETOOTH_ERROR_INTERNAL;
3490 g_variant_get(result , "(s)", &idaddress);
3491 g_variant_unref(result);
3493 if (idaddress == NULL) {
3494 BT_ERR("No paired device");
3495 g_object_unref(device_proxy);
3496 return BLUETOOTH_ERROR_NOT_PAIRED;
3499 BT_DBG("ID Address:%s", idaddress);
3502 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3504 ret = BLUETOOTH_ERROR_INTERNAL;
3506 g_object_unref(device_proxy);
3511 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3513 GapAgentPrivate *agent = _bt_get_adapter_agent();
3514 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3516 if (authentication_reply)
3517 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3519 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3521 BT_DBG("BT_PASSKEY_REPLY");
3522 return BLUETOOTH_ERROR_NONE;
3525 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3527 GapAgentPrivate *agent = _bt_get_adapter_agent();
3528 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3530 if (confirmation_reply)
3531 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3533 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3535 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3536 return BLUETOOTH_ERROR_NONE;