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 void __bt_decline_pair_request()
127 GVariant *out_param1;
128 GVariant *out_param2;
129 request_info_t *req_info;
130 bluetooth_device_info_t dev_info;
131 bt_remote_dev_info_t *remote_dev_info;
132 GVariant *uuid_list, *manufacture_data;
137 req_info = _bt_get_request_info(bonding_info->req_id);
138 if (req_info == NULL) {
139 BT_ERR("req_info == NULL");
142 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
144 BT_DBG("bonding_info is NULL");
149 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
150 remote_dev_info->uuids, remote_dev_info->uuid_count,
153 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
154 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
157 param = g_variant_new("isunsbuba{s}na{y})",
158 bonding_info->result,
160 remote_dev_info->class,
161 remote_dev_info->rssi,
162 remote_dev_info->name,
163 remote_dev_info->paired,
164 remote_dev_info->connected,
165 remote_dev_info->trust,
167 remote_dev_info->manufacturer_data_len,
171 //Send the event to application
172 if (remote_dev_info != NULL) {
173 _bt_send_event(BT_ADAPTER_EVENT,
174 BLUETOOTH_EVENT_BONDING_FINISHED,
177 _bt_free_device_info(remote_dev_info);
180 if (req_info->context == NULL)
183 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
184 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
187 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
188 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
190 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
191 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
194 g_dbus_method_invocation_return_value(req_info->context,
195 g_variant_new("@ayi", out_param1, out_param2));
198 _bt_delete_request_list(req_info->req_id);
201 g_free(bonding_info->addr);
202 g_free(bonding_info);
208 #ifdef TIZEN_PROFILE_WEARABLE
209 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
213 retv_if(user_data == NULL, FALSE);
215 b = (bundle *)user_data;
217 ret = syspopup_launch("bt-syspopup", b);
219 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
221 BT_DBG("Hurray!!! Finally Popup launched");
224 return (ret < 0) ? TRUE : FALSE;
227 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
232 GDBusConnection *conn;
234 conn = _bt_gdbus_get_system_gconn();
242 bundle_add(b, "event-type", "unable-to-pairing");
244 if (result == BLUETOOTH_ERROR_TIMEOUT)
245 bundle_add(b, "error", "timeout");
246 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
247 bundle_add(b, "error", "authfailed");
249 bundle_add(b, "error", "error");
251 ret = syspopup_launch("bt-syspopup", b);
253 BT_ERR("Popup launch failed...retry %d \n", ret);
254 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
265 gboolean _bt_is_device_creating(void)
267 return is_device_creating;
270 gboolean _bt_is_bonding_device_address(const char *address)
272 if (bonding_info == NULL || bonding_info->addr == NULL)
275 if (g_strcmp0(bonding_info->addr, address) == 0) {
276 BT_DBG("[%s] is bonding device", address);
280 BT_DBG("[%s] is NOT bonding device", address);
284 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
286 ret_if(bonding_info == NULL);
287 bonding_info->is_autopair = is_autopair;
290 void __bt_cancel_search_service_done(void)
292 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
293 request_info_t *req_info;
294 bluetooth_device_info_t dev_info;
295 GVariant *out_param1;
297 ret_if(searching_info == NULL);
299 req_info = _bt_get_request_info(searching_info->req_id);
300 if (req_info == NULL) {
301 BT_ERR("req_info == NULL");
305 if (req_info->context == NULL)
308 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
309 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
310 searching_info->addr);
312 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
313 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
316 g_dbus_method_invocation_return_value(req_info->context,
317 g_variant_new("(iv)", result, out_param1));
319 _bt_delete_request_list(req_info->req_id);
323 g_free(searching_info->addr);
324 g_free(searching_info);
325 searching_info = NULL;
328 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
330 ret_if(value == NULL);
331 ret_if(info == NULL);
333 info->uuid_count = g_variant_get_size(value);
334 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
337 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
339 char *object_path = NULL;
340 bt_remote_dev_info_t *dev_info;
342 retv_if(address == NULL, NULL);
344 object_path = _bt_get_device_object_path(address);
345 retv_if(object_path == NULL, NULL);
347 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
353 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
354 const char *object_path)
356 bt_remote_dev_info_t *dev_info;
357 GDBusProxy *adapter_proxy;
358 GDBusProxy *device_proxy;
363 GDBusConnection *conn;
364 GError *error = NULL;
365 GVariant *result = NULL;
366 GVariantIter *value_iter;
370 adapter_proxy = _bt_get_adapter_proxy();
371 retv_if(adapter_proxy == NULL, NULL);
373 retv_if(object_path == NULL, NULL);
375 conn = _bt_gdbus_get_system_gconn();
377 BT_ERR("conn == NULL");
381 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
382 NULL, BT_BLUEZ_NAME, object_path,
383 BT_PROPERTIES_INTERFACE, NULL, NULL);
385 retv_if(device_proxy == NULL, NULL);
387 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
388 g_variant_new("(s)", BT_DEVICE_INTERFACE),
389 G_DBUS_CALL_FLAGS_NONE,
394 g_object_unref(device_proxy);
396 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
398 if (result != NULL) {
399 g_variant_get(result , "(@a{sv})", &value);
400 g_variant_unref(result);
402 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
404 g_variant_get(tmp_value, "s", &name);
405 g_variant_unref(tmp_value);
407 DBG_SECURE("Alias Name [%s]", name);
409 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
410 g_variant_get(tmp_value, "s", &name);
411 g_variant_unref(tmp_value);
414 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
416 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
417 g_variant_unref(tmp_value);
419 dev_info->is_alias_set = FALSE;
421 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
423 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
425 dev_info->class = g_variant_get_uint32(tmp_value);
426 g_variant_unref(tmp_value);
430 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
432 dev_info->connected = g_variant_get_byte(tmp_value);
433 g_variant_unref(tmp_value);
435 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
436 BT_DBG("connected link : %d", dev_info->connected);
438 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
440 dev_info->trust = g_variant_get_boolean(tmp_value);
441 g_variant_unref(tmp_value);
443 dev_info->trust = FALSE;
445 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
447 dev_info->paired = g_variant_get_boolean(tmp_value);
448 g_variant_unref(tmp_value);
450 dev_info->paired = FALSE;
452 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
454 dev_info->rssi = g_variant_get_int16(tmp_value);
455 g_variant_unref(tmp_value);
459 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
461 dev_info->addr_type = g_variant_get_byte(tmp_value);
462 g_variant_unref(tmp_value);
464 dev_info->addr_type = 0;
466 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
468 __bt_get_uuids(tmp_value, dev_info);
469 g_variant_unref(tmp_value);
472 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerDataLen", G_VARIANT_TYPE_UINT16);
474 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
475 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
476 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
477 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
479 g_variant_unref(tmp_value);
481 dev_info->manufacturer_data_len = 0;
483 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerData", G_VARIANT_TYPE_ARRAY);
485 if ((dev_info->manufacturer_data_len == 0) ||
486 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
487 BT_ERR("manufacturer data length doesn't match");
488 dev_info->manufacturer_data_len = 0;
489 dev_info->manufacturer_data = NULL;
491 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
492 g_variant_get(tmp_value, "ay", &value_iter);
493 while (g_variant_iter_loop(value_iter, "y", &m_value))
494 dev_info->manufacturer_data[i++] = m_value;
496 g_variant_unref(tmp_value);
498 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
499 dev_info->manufacturer_data_len = 0;
500 dev_info->manufacturer_data = NULL;
503 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
504 g_variant_get(tmp_value, "s", &address);
505 g_variant_unref(tmp_value);
507 dev_info->address = address;
508 dev_info->name = name;
509 g_variant_unref(value);
511 BT_ERR("result is NULL\n");
519 char *_bt_get_device_name(const char *bdaddress)
521 char *device_path = NULL;
522 const gchar *name = NULL;
523 gchar *dev_name = NULL;
525 GVariant *result = NULL;
527 GDBusProxy *device_proxy;
528 GDBusConnection *conn;
530 retv_if(bdaddress == NULL, NULL);
532 device_path = _bt_get_device_object_path((char *)bdaddress);
533 retv_if(device_path == NULL, NULL);
535 conn = _bt_gdbus_get_system_gconn();
536 retv_if(conn == NULL, NULL);
537 INFO_SECURE("Device_path %s", device_path);
538 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
539 NULL, BT_BLUEZ_NAME, device_path,
540 BT_PROPERTIES_INTERFACE, NULL, &err);
543 retv_if(device_proxy == NULL, NULL);
545 result = g_dbus_proxy_call_sync(device_proxy, "Get",
546 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
547 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
549 BT_ERR("DBus Error : %s", err->message);
553 g_variant_get(result, "(v)", &value);
554 name = g_variant_get_string(value, &name_len);
555 INFO_SECURE("Alias Name [%s]", name);
557 dev_name = g_strdup(name);
558 g_variant_unref(value);
559 g_variant_unref(result);
564 result = g_dbus_proxy_call_sync(device_proxy, "Get",
565 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
566 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
568 ERR("DBus Error : %s", err->message);
571 g_variant_get(result, "(v)", &value);
572 name = g_variant_get_string(value, &name_len);
573 INFO_SECURE("Name = %s", name);
575 dev_name = g_strdup(name);
576 g_variant_unref(value);
577 g_variant_unref(result);
581 g_object_unref(device_proxy);
585 static gboolean __ignore_auto_pairing_request(const char *address)
590 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
601 /* Get the LAP(Lower Address part) */
602 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
604 snprintf(lap_address, sizeof(lap_address), ",%s", address);
606 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
609 BT_ERR("fopen failed \n");
613 fseek(fp, 0, SEEK_END);
618 BT_ERR("Get file size failed \n");
623 buffer = g_malloc0(sizeof(char) * size);
624 result = fread((char *)buffer, 1, size, fp);
626 if (result != size) {
627 BT_ERR("Read Error\n");
632 BT_DBG("Buffer = %s\n", buffer);
634 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
640 /* Write the data and insert new device data */
641 for (i = 0; lines[i] != NULL; i++) {
642 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
643 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
645 lines[i] = temp_buffer;
648 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
650 /* Fix : NULL_RETURNS */
651 retv_if(buffer == NULL, FALSE);
653 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
656 BT_ERR("fopen failed \n");
661 BT_DBG("Buffer = %s\n", buffer);
662 fwrite(buffer, 1, strlen(buffer), fp);
672 static int __bt_cancel_bonding(void)
674 BT_CHECK_PARAMETER(bonding_info, return);
675 BT_CHECK_PARAMETER(bonding_info->addr, return);
676 /* First Cancel the ongoing pairing in bluez otherwise if we send
677 * pair request bluez will send inprogress and we again retry bond so
678 * this cycle continues */
680 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
681 NULL, G_DBUS_CALL_FLAGS_NONE,
684 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
686 return BLUETOOTH_ERROR_PARING_FAILED;
688 return BLUETOOTH_ERROR_NONE;
691 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
694 int result = BLUETOOTH_ERROR_NONE;
697 GVariant *out_param1;
698 request_info_t *req_info;
699 bluetooth_device_info_t dev_info;
701 /* Terminate ALL system popup */
702 syspopup_destroy_all();
704 reply = g_dbus_proxy_call_finish(proxy, res, &err);
706 g_variant_unref(reply);
708 is_device_creating = FALSE;
710 if (bonding_info == NULL) {
712 BT_ERR("bonding_info == NULL");
718 req_info = _bt_get_request_info(bonding_info->req_id);
719 if (req_info == NULL) {
720 BT_ERR("req_info == NULL");
725 g_dbus_error_strip_remote_error(err);
726 BT_ERR("Error occured in CreateBonding [%s]", err->message);
728 if (g_strrstr(err->message, "Already Exists")) {
729 BT_INFO("Existing Bond");
730 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
731 } else if (_bt_agent_is_canceled() ||
732 g_strrstr(err->message, "Authentication Canceled")) {
733 BT_INFO("Cancelled by USER");
734 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
735 } else if (g_strrstr(err->message, "Authentication Rejected")) {
737 result = BLUETOOTH_ERROR_ACCESS_DENIED;
738 } else if (g_strrstr(err->message, "In Progress")) {
739 BT_INFO("Bond in progress");
740 result = BLUETOOTH_ERROR_IN_PROGRESS;
741 } else if (g_strrstr(err->message, "Authentication Failed")) {
742 BT_INFO("Authentication Failed");
743 if (bonding_info->is_autopair == TRUE) {
744 _bt_set_autopair_status_in_bonding_info(FALSE);
745 __ignore_auto_pairing_request(bonding_info->addr);
747 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
748 } else if (g_strrstr(err->message, "Page Timeout")) {
749 BT_INFO("Page Timeout");
750 /* This is the special case
751 As soon as call bluetooth_bond_device, try to cancel bonding.
752 In this case, before completing to call 'CreatePairedDevice' method
753 the procedure is stopped. So 'Cancle' error is not return.
755 result = BLUETOOTH_ERROR_HOST_DOWN;
756 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
757 BT_INFO("Cancel already running bonding");
758 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
759 BT_INFO("Error while Cancelling bonding");
760 /* we need to unref proxy so continue */
762 result = BLUETOOTH_ERROR_INTERNAL;
763 } else if (g_strrstr(err->message, "Connection Timeout")) {
764 /* Pairing request timeout */
765 result = BLUETOOTH_ERROR_TIMEOUT;
766 } else if (g_strrstr(err->message, "Authentication Timeout")) {
767 /* Pairing request timeout */
768 result = BLUETOOTH_ERROR_TIMEOUT;
770 BT_DBG("Default case");
771 result = BLUETOOTH_ERROR_PARING_FAILED;
775 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
776 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
777 result == BLUETOOTH_ERROR_TIMEOUT ||
778 result == BLUETOOTH_ERROR_HOST_DOWN) {
779 #ifdef TIZEN_PROFILE_WEARABLE
780 int is_sw_running = 0;
782 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
783 BT_ERR("vconf_get_int for setup wizard state failed");
786 __bt_launch_unable_to_pairing_syspopup(result);
788 BT_ERR("Unable to pair");
790 bonding_info->result = result;
793 g_object_unref(proxy);
794 bonding_info->device_proxy = NULL;
796 if (result != BLUETOOTH_ERROR_NONE) {
797 if (bonding_info->bonding_wo_discovery) {
798 GDBusProxy *adapter_proxy;
799 GVariant *ret = NULL;
800 GError *error = NULL;
803 BT_ERR("Bond was tried without discovery. Remove it");
805 adapter_proxy = _bt_get_adapter_proxy();
806 if (adapter_proxy == NULL) {
807 BT_ERR("Cannot get adapter_proxy");
811 device_path = _bt_get_device_object_path(bonding_info->addr);
812 if (device_path == NULL) {
813 BT_ERR("Cannot get device path");
817 ret = g_dbus_proxy_call_sync(adapter_proxy,
819 g_variant_new("(o)", device_path),
820 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
823 BT_ERR("RemoveDevice Fail: %s", error->message);
824 g_clear_error(&error);
828 g_variant_unref(ret);
837 if (req_info->context == NULL)
840 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
841 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
844 if (_bt_adapter_get_status() != BT_ACTIVATED)
845 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
847 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
848 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
850 g_dbus_method_invocation_return_value(req_info->context,
851 g_variant_new("(iv)", result, out_param1));
853 g_variant_unref(out_param1);
855 _bt_delete_request_list(req_info->req_id);
860 _bt_agent_set_canceled(FALSE);
863 g_free(bonding_info->addr);
864 g_free(bonding_info);
868 int _bt_bond_device(int request_id,
869 bluetooth_device_address_t *device_address,
870 unsigned short conn_type, GArray **out_param1)
873 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
874 bluetooth_device_info_t dev_info;
876 GDBusConnection *conn;
877 char *device_path = NULL;
878 GDBusProxy *adapter_proxy;
879 GError *error = NULL;
880 gboolean bonding_wo_discovery = FALSE;
882 BT_CHECK_PARAMETER(device_address, return);
885 BT_ERR("Bonding in progress");
887 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
888 memcpy(dev_info.device_address.addr, device_address->addr,
889 BLUETOOTH_ADDRESS_LENGTH);
891 g_array_append_vals(*out_param1, &dev_info,
892 sizeof(bluetooth_device_info_t));
894 return BLUETOOTH_ERROR_DEVICE_BUSY;
897 conn = _bt_gdbus_get_system_gconn();
898 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
900 _bt_convert_addr_type_to_string(address, device_address->addr);
902 device_path = _bt_get_device_object_path(address);
904 if (device_path == NULL) {
905 BT_ERR("No searched device");
906 GVariant *ret = NULL;
907 adapter_proxy = _bt_get_adapter_proxy();
908 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
910 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
911 g_variant_new("(s)", address),
912 G_DBUS_CALL_FLAGS_NONE,
918 BT_ERR("CreateDevice Fail: %s", error->message);
919 g_clear_error(&error);
922 g_variant_unref(ret);
923 device_path = _bt_get_device_object_path(address);
924 if (device_path == NULL) {
925 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
926 memcpy(dev_info.device_address.addr, device_address->addr,
927 BLUETOOTH_ADDRESS_LENGTH);
929 g_array_append_vals(*out_param1, &dev_info,
930 sizeof(bluetooth_device_info_t));
932 return BLUETOOTH_ERROR_NOT_PAIRED;
934 BT_INFO("device_path is created[%s]", device_path);
936 bonding_wo_discovery = TRUE;
939 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
941 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
944 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
946 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
947 bonding_info->addr = g_strdup(address);
948 bonding_info->req_id = request_id;
950 bonding_info->device_proxy = proxy;
951 bonding_info->conn_type = conn_type;
952 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
954 is_device_creating = TRUE;
956 g_dbus_proxy_call(proxy, "Pair",
957 g_variant_new("(y)", conn_type),
958 G_DBUS_CALL_FLAGS_NONE,
961 (GAsyncReadyCallback)__bt_bond_device_cb,
964 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
966 return BLUETOOTH_ERROR_NONE;
968 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
969 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
972 g_array_append_vals(*out_param1, &dev_info,
973 sizeof(bluetooth_device_info_t));
975 is_device_creating = FALSE;
977 g_free(bonding_info->addr);
978 g_free(bonding_info);
981 return BLUETOOTH_ERROR_INTERNAL;*/
984 int _bt_cancel_bonding(void)
986 int ret = BLUETOOTH_ERROR_NONE;
988 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
990 ret = _bt_agent_reply_cancellation();
991 if (ret != BLUETOOTH_ERROR_NONE) {
992 BT_ERR("Fail to call reply cancellation");
996 _bt_agent_set_canceled(TRUE);
998 return BLUETOOTH_ERROR_NONE;
1001 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1006 GVariant *out_param1;
1007 int result = BLUETOOTH_ERROR_NONE;
1008 bt_funcion_data_t *unbonding_info;
1009 bluetooth_device_info_t dev_info;
1010 request_info_t *req_info;
1012 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1014 g_variant_unref(reply);
1016 unbonding_info = user_data;
1018 if (unbonding_info == NULL) {
1020 BT_ERR("unbonding_info == NULL");
1024 req_info = _bt_get_request_info(unbonding_info->req_id);
1025 if (req_info == NULL) {
1026 BT_ERR("req_info == NULL");
1031 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1032 result = BLUETOOTH_ERROR_INTERNAL;
1035 if (req_info->context == NULL)
1038 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1039 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1040 unbonding_info->addr);
1042 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1043 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1045 g_dbus_method_invocation_return_value(req_info->context,
1046 g_variant_new("(iv)", result, out_param1));
1049 _bt_delete_request_list(req_info->req_id);
1055 if (unbonding_info) {
1056 g_free(unbonding_info->addr);
1057 g_free(unbonding_info);
1061 int _bt_unbond_device(int request_id,
1062 bluetooth_device_address_t *device_address,
1063 GArray **out_param1)
1065 char *device_path = NULL;
1066 bt_funcion_data_t *unbonding_info;
1067 GDBusProxy *adapter_proxy = NULL;
1068 GDBusProxy *device_proxy = NULL;
1069 GDBusConnection *conn;
1070 int result = BLUETOOTH_ERROR_INTERNAL;
1071 bluetooth_device_info_t dev_info;
1072 GError *error = NULL;
1073 GVariant *ret = NULL;
1075 BT_CHECK_PARAMETER(device_address, return);
1077 adapter_proxy = _bt_get_adapter_proxy();
1078 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1080 /* allocate user data so that it can be retrieved in callback */
1081 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1082 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1083 unbonding_info->req_id = request_id;
1085 _bt_convert_addr_type_to_string(unbonding_info->addr,
1086 device_address->addr);
1088 device_path = _bt_get_device_object_path(unbonding_info->addr);
1090 if (device_path == NULL) {
1091 BT_ERR("No paired device");
1092 result = BLUETOOTH_ERROR_NOT_PAIRED;
1096 conn = _bt_gdbus_get_system_gconn();
1098 BT_ERR("conn is NULL");
1099 result = BLUETOOTH_ERROR_INTERNAL;
1103 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1104 NULL, BT_BLUEZ_NAME,
1105 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1107 if (device_proxy != NULL) {
1109 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1110 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1111 G_DBUS_CALL_FLAGS_NONE,
1116 BT_ERR("Getting property failed: [%s]\n", error->message);
1117 g_error_free(error);
1118 result = BLUETOOTH_ERROR_NOT_PAIRED;
1122 BT_ERR("No paired device");
1123 g_object_unref(device_proxy);
1124 result = BLUETOOTH_ERROR_NOT_PAIRED;
1127 g_variant_unref(ret);
1129 g_object_unref(device_proxy);
1132 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1133 g_variant_new("(o)", device_path),
1134 G_DBUS_CALL_FLAGS_NONE,
1135 BT_MAX_DBUS_TIMEOUT,
1137 (GAsyncReadyCallback)__bt_unbond_cb,
1140 g_free(device_path);
1141 return BLUETOOTH_ERROR_NONE;
1144 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1145 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1146 unbonding_info->addr);
1148 g_array_append_vals(*out_param1, &dev_info,
1149 sizeof(bluetooth_device_info_t));
1150 g_free(device_path);
1151 g_free(unbonding_info->addr);
1152 g_free(unbonding_info);
1156 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1161 GVariant *out_param1;
1162 int result = BLUETOOTH_ERROR_NONE;
1163 bluetooth_device_info_t dev_info;
1164 bt_remote_dev_info_t *remote_dev_info;
1165 request_info_t *req_info;
1166 GVariant *uuid_list, *manufacture_data;
1168 GVariantBuilder *builder = NULL;
1171 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1173 g_variant_unref(reply);
1175 g_object_unref(proxy);
1177 if (searching_info == NULL) {
1179 BT_ERR("unbonding_info == NULL");
1183 req_info = _bt_get_request_info(searching_info->req_id);
1184 if (req_info == NULL) {
1185 BT_ERR("req_info == NULL");
1190 g_dbus_error_strip_remote_error(err);
1191 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1193 if (g_strrstr("Operation canceled", err->message))
1194 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1195 else if (g_strrstr("In Progress", err->message))
1196 result = BLUETOOTH_ERROR_IN_PROGRESS;
1197 else if (g_strrstr("Host is down", err->message))
1198 result = BLUETOOTH_ERROR_HOST_DOWN;
1200 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1202 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1203 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1204 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1205 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1206 remote_dev_info->uuid_count > 0) {
1207 result = BLUETOOTH_ERROR_NONE;
1210 _bt_free_device_info(remote_dev_info);
1215 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1216 if (!remote_dev_info)
1220 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1221 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1222 g_variant_builder_add(builder, "s",
1223 remote_dev_info->uuids[i]);
1225 uuid_list = g_variant_new("as", builder);
1226 g_variant_builder_unref(builder);
1227 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1228 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1231 param = g_variant_new("(isunsbub@asn@ay)",
1233 searching_info->addr,
1234 remote_dev_info->class,
1235 remote_dev_info->rssi,
1236 remote_dev_info->name,
1237 remote_dev_info->paired,
1238 remote_dev_info->connected,
1239 remote_dev_info->trust,
1241 remote_dev_info->manufacturer_data_len,
1244 /* Send the event to application */
1245 _bt_send_event(BT_ADAPTER_EVENT,
1246 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1249 _bt_free_device_info(remote_dev_info);
1252 if (req_info->context == NULL)
1255 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1256 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1257 searching_info->addr);
1259 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1260 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1262 g_dbus_method_invocation_return_value(req_info->context,
1263 g_variant_new("(iv)", result, out_param1));
1266 _bt_delete_request_list(req_info->req_id);
1271 if (searching_info) {
1272 g_free(searching_info->addr);
1273 g_free(searching_info);
1274 searching_info = NULL;
1278 int _bt_search_device(int request_id,
1279 bluetooth_device_address_t *device_address)
1281 char *device_path = NULL;
1282 GDBusProxy *device_proxy = NULL;
1283 GDBusConnection *conn;
1285 GDBusProxy *adapter_proxy;
1286 int result = BLUETOOTH_ERROR_INTERNAL;
1288 BT_CHECK_PARAMETER(device_address, return);
1291 BT_ERR("Bonding in progress");
1292 return BLUETOOTH_ERROR_DEVICE_BUSY;
1295 if (searching_info) {
1296 BT_ERR("Service searching in progress");
1297 return BLUETOOTH_ERROR_DEVICE_BUSY;
1300 adapter_proxy = _bt_get_adapter_proxy();
1301 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1303 /* allocate user data so that it can be retrieved in callback */
1304 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1305 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1306 searching_info->req_id = request_id;
1308 _bt_convert_addr_type_to_string(searching_info->addr,
1309 device_address->addr);
1311 conn = _bt_gdbus_get_system_gconn();
1312 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1315 device_path = _bt_get_device_object_path(searching_info->addr);
1317 if (device_path == NULL) {
1318 BT_ERR("No paired device");
1319 result = BLUETOOTH_ERROR_NOT_PAIRED;
1323 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1324 NULL, BT_BLUEZ_NAME,
1325 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1326 g_free(device_path);
1327 if (device_proxy == NULL) {
1328 result = BLUETOOTH_ERROR_INTERNAL;
1332 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1333 g_variant_new("(s)", ""),
1334 G_DBUS_CALL_FLAGS_NONE,
1335 BT_MAX_DBUS_TIMEOUT,
1337 (GAsyncReadyCallback)__bt_discover_cb,
1340 searching_info->device_proxy = device_proxy;
1342 return BLUETOOTH_ERROR_NONE;
1345 g_free(searching_info->addr);
1346 g_free(searching_info);
1347 searching_info = NULL;
1351 int _bt_cancel_search_device(void)
1353 GVariant *ret = NULL;
1356 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1358 if (searching_info->device_proxy) {
1359 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1361 G_DBUS_CALL_FLAGS_NONE,
1366 g_variant_unref(ret);
1368 __bt_cancel_search_service_done();
1370 return BLUETOOTH_ERROR_NONE;
1373 int _bt_set_alias(bluetooth_device_address_t *device_address,
1376 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1377 gchar *device_path = NULL;
1378 GDBusProxy *adapter_proxy;
1379 GDBusProxy *device_proxy;
1380 GVariant *ret = NULL;
1381 GError *error = NULL;
1382 GDBusConnection *conn;
1384 BT_CHECK_PARAMETER(device_address, return);
1385 BT_CHECK_PARAMETER(alias, return);
1387 adapter_proxy = _bt_get_adapter_proxy();
1388 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1390 conn = _bt_gdbus_get_system_gconn();
1391 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1393 _bt_convert_addr_type_to_string(address, device_address->addr);
1395 device_path = _bt_get_device_object_path(address);
1397 if (device_path == NULL) {
1398 BT_ERR("No paired device");
1399 return BLUETOOTH_ERROR_NOT_PAIRED;
1402 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1403 NULL, BT_BLUEZ_NAME,
1404 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1406 g_free(device_path);
1407 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1409 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1410 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1411 G_DBUS_CALL_FLAGS_NONE,
1416 g_variant_unref(ret);
1418 g_object_unref(device_proxy);
1421 BT_ERR("SetProperty error: [%s]", error->message);
1422 g_error_free(error);
1423 return BLUETOOTH_ERROR_INTERNAL;
1426 return BLUETOOTH_ERROR_NONE;
1429 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1432 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1433 gchar *device_path = NULL;
1434 GDBusProxy *device_proxy;
1435 gboolean previous_value;
1436 GError *error = NULL;
1437 GDBusConnection *conn;
1438 GVariant *result = NULL;
1439 GVariant *temp = NULL;
1440 int ret = BLUETOOTH_ERROR_NONE;
1442 BT_CHECK_PARAMETER(device_address, return);
1444 conn = _bt_gdbus_get_system_gconn();
1445 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1447 _bt_convert_addr_type_to_string(address, device_address->addr);
1449 device_path = _bt_get_device_object_path(address);
1451 if (device_path == NULL) {
1452 BT_ERR("No paired device");
1453 return BLUETOOTH_ERROR_NOT_PAIRED;
1456 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1457 NULL, BT_BLUEZ_NAME,
1458 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1460 g_free(device_path);
1461 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1463 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1464 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1465 G_DBUS_CALL_FLAGS_NONE,
1469 if (error != NULL) {
1470 BT_ERR("Getting property failed: [%s]\n", error->message);
1471 g_error_free(error);
1472 g_object_unref(device_proxy);
1473 return BLUETOOTH_ERROR_INTERNAL;
1476 g_variant_get(result, "(v)", &temp);
1477 previous_value = g_variant_get_boolean(temp);
1478 g_variant_unref(temp);
1479 g_variant_unref(result);
1480 /* If the input is same with previous value, return error. */
1481 if (previous_value == authorize) {
1482 BT_ERR("Same value: %d", previous_value);
1483 g_object_unref(device_proxy);
1484 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1488 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1489 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1490 G_DBUS_CALL_FLAGS_NONE,
1495 g_object_unref(device_proxy);
1497 BT_ERR("SetProperty error: [%s]", error->message);
1498 g_error_free(error);
1499 ret = BLUETOOTH_ERROR_INTERNAL;
1503 g_variant_unref(result);
1508 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1509 gboolean *is_connected)
1511 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1512 char *object_path = NULL;
1514 GDBusProxy *device_proxy;
1515 GError *error = NULL;
1517 GDBusConnection *conn;
1518 GVariant *result = NULL;
1519 int ret = BLUETOOTH_ERROR_NONE;
1521 BT_CHECK_PARAMETER(device_address, return);
1523 conn = _bt_gdbus_get_system_gconn();
1524 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1526 _bt_convert_addr_type_to_string(address, device_address->addr);
1528 object_path = _bt_get_device_object_path(address);
1529 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1531 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1532 NULL, BT_BLUEZ_NAME, object_path,
1533 BT_PROPERTIES_INTERFACE, NULL, NULL);
1534 g_free(object_path);
1535 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1537 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1538 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1540 G_DBUS_CALL_FLAGS_NONE,
1544 if (result == NULL) {
1545 if (error != NULL) {
1546 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1547 g_error_free(error);
1549 g_object_unref(device_proxy);
1550 return BLUETOOTH_ERROR_INTERNAL;
1553 g_variant_get(result, "(v)", &value);
1554 *is_connected = g_variant_get_boolean(value);
1555 g_variant_unref(result);
1557 BT_DBG("gatt is connected : %d", *is_connected);
1559 g_variant_unref(value);
1560 g_object_unref(device_proxy);
1565 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1566 int connection_type, gboolean *is_connected)
1568 char *object_path = NULL;
1569 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1570 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1571 GDBusProxy *device_proxy = NULL;
1572 GDBusProxy *adapter_proxy = NULL;
1573 GDBusConnection *conn;
1574 GError *error = NULL;
1575 GVariant *tmp_value = NULL;
1576 GVariant *value = NULL;
1577 GVariant *result = NULL;
1580 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1581 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1583 _bt_convert_addr_type_to_string(address, device_address->addr);
1584 _bt_convert_addr_string_to_secure_string(secure_address, address);
1586 *is_connected = FALSE;
1587 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1589 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1590 return _bt_rfcomm_is_device_connected(device_address,
1592 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1593 return _bt_is_gatt_connected(device_address, is_connected);
1594 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1595 return _bt_pbap_is_connected(device_address, is_connected);
1597 adapter_proxy = _bt_get_adapter_proxy();
1598 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1600 conn = _bt_gdbus_get_system_gconn();
1601 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1603 _bt_convert_addr_type_to_string(address, device_address->addr);
1605 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1606 object_path = _bt_get_adapter_path();
1607 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1608 NULL, BT_BLUEZ_NAME,
1609 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1610 g_free(object_path);
1611 if (device_proxy == NULL) {
1612 BT_DBG("Device don't have this service");
1613 return BLUETOOTH_ERROR_INTERNAL;
1616 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1617 g_variant_new("(s)", address),
1618 G_DBUS_CALL_FLAGS_NONE,
1623 if (result == NULL) {
1624 BT_ERR("[GetProperties] Error occured in Proxy call");
1626 BT_ERR("%s", error->message);
1627 g_error_free(error);
1629 *is_connected = FALSE;
1630 g_object_unref(device_proxy);
1631 return BLUETOOTH_ERROR_NONE;
1633 g_variant_get(result , "(@a{sv})", &value);
1634 g_variant_unref(result);
1637 tmp_value = g_variant_lookup_value(value,
1639 G_VARIANT_TYPE_BOOLEAN);
1641 *is_connected = g_variant_get_boolean(tmp_value);
1642 g_variant_unref(tmp_value);
1644 g_variant_unref(value);
1646 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1647 return _bt_is_network_connected(_bt_get_net_conn(),
1648 device_address->addr, is_connected);
1650 uuid = _bt_get_profile_uuid128(connection_type);
1652 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1653 return BLUETOOTH_ERROR_INTERNAL;
1655 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1657 object_path = _bt_get_device_object_path(address);
1658 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1659 BT_DBG("object_path: %s", object_path);
1660 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1661 NULL, BT_BLUEZ_NAME,
1662 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1663 g_free(object_path);
1664 if (device_proxy == NULL) {
1665 BT_DBG("Device don't have this service");
1667 return BLUETOOTH_ERROR_INTERNAL;
1670 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1671 g_variant_new("(s)", uuid),
1672 G_DBUS_CALL_FLAGS_NONE,
1677 if (result == NULL) {
1678 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1680 BT_ERR("%s", error->message);
1681 if (g_strrstr(error->message, "Not Connected"))
1682 BT_DBG("Not connected");
1683 g_error_free(error);
1685 *is_connected = FALSE;
1686 g_object_unref(device_proxy);
1688 return BLUETOOTH_ERROR_NONE;
1690 g_variant_get(result, "(b)", is_connected);
1692 g_variant_unref(result);
1695 g_object_unref(device_proxy);
1696 return BLUETOOTH_ERROR_NONE;
1699 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1700 bluetooth_connected_link_t *connected)
1702 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1703 char *object_path = NULL;
1705 GDBusProxy *device_proxy;
1706 GError *error = NULL;
1707 GDBusConnection *conn;
1708 GVariant *value = NULL;
1709 GVariant *result = NULL;
1711 BT_CHECK_PARAMETER(device_address, return);
1713 conn = _bt_gdbus_get_system_gconn();
1714 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1716 _bt_convert_addr_type_to_string(address, device_address->addr);
1718 object_path = _bt_get_device_object_path(address);
1719 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1721 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1722 NULL, BT_BLUEZ_NAME,
1723 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1724 g_free(object_path);
1725 if (device_proxy == NULL) {
1726 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1727 return BLUETOOTH_ERROR_NONE;
1730 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1731 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1732 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1734 if (error != NULL) {
1735 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1736 g_error_free(error);
1737 g_object_unref(device_proxy);
1738 return BLUETOOTH_ERROR_INTERNAL;
1740 g_variant_get(result, "(v)", &value);
1741 *connected = g_variant_get_byte(value);
1742 g_variant_unref(value);
1743 g_variant_unref(result);
1746 g_object_unref(device_proxy);
1747 return BLUETOOTH_ERROR_NONE;
1750 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1754 GVariant *out_param1;
1756 int result = BLUETOOTH_ERROR_NONE;
1757 bt_function_data_t *func_data = user_data;
1758 request_info_t *req_info = NULL;
1759 bluetooth_device_address_t device_addr = { {0} };
1761 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1762 g_object_unref(proxy);
1764 if (reply == NULL) {
1765 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1767 BT_ERR("Error: %s", err->message);
1768 g_clear_error(&err);
1770 result = BLUETOOTH_ERROR_INTERNAL;
1772 g_variant_unref(reply);
1775 if (func_data == NULL) {
1776 BT_ERR("func_data is NULL");
1780 req_info = _bt_get_request_info(func_data->req_id);
1781 if (req_info == NULL) {
1782 BT_ERR("req_info is NULL");
1786 if (req_info->context == NULL) {
1787 BT_ERR("req_info->context is NULL");
1791 _bt_convert_addr_string_to_type(device_addr.addr,
1792 (const char *)func_data->address);
1794 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1795 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1798 g_dbus_method_invocation_return_value(req_info->context,
1799 g_variant_new("(iv)", result, out_param1));
1803 _bt_delete_request_list(req_info->req_id);
1806 g_free(func_data->address);
1811 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1812 gboolean auto_connect)
1814 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1815 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1816 gchar *device_path = NULL;
1817 GDBusProxy *device_proxy = NULL;
1818 GDBusConnection *conn;
1819 int ret = BLUETOOTH_ERROR_NONE;
1820 bt_function_data_t *func_data;
1822 BT_CHECK_PARAMETER(bd_addr, return);
1824 conn = _bt_gdbus_get_system_gconn();
1825 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1827 _bt_convert_addr_type_to_string(device_address,
1828 (unsigned char *)bd_addr->addr);
1829 device_path = _bt_get_device_object_path(device_address);
1830 if (device_path == NULL) {
1831 BT_ERR_C("device_path NULL : [%s]", device_address);
1832 ret = BLUETOOTH_ERROR_INTERNAL;
1836 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1837 NULL, BT_BLUEZ_NAME,
1838 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1839 g_free(device_path);
1840 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1842 func_data = g_malloc0(sizeof(bt_function_data_t));
1843 func_data->address = g_strdup(device_address);
1844 if (func_data->address == NULL) {
1845 BT_ERR("Unable to allocate memory for address");
1846 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1850 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1851 BT_INFO("Connect LE [%s]", secure_address);
1853 func_data->req_id = req_id;
1855 g_dbus_proxy_call(device_proxy, "ConnectLE",
1856 g_variant_new("(b)", auto_connect),
1857 G_DBUS_CALL_FLAGS_NONE,
1858 BT_MAX_DBUS_TIMEOUT,
1860 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1866 g_object_unref(device_proxy);
1868 g_free(func_data->address);
1874 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1876 BT_INFO("Try to initiate pending LE connection");
1878 pending_le_conn_timer_id = 0;
1880 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1881 &pending_le_conn_info->bd_addr,
1882 pending_le_conn_info->auto_connect);
1884 g_free(pending_le_conn_info);
1885 pending_le_conn_info = NULL;
1890 void _bt_pending_connect_le_device(void)
1892 if (pending_le_conn_timer_id > 0) {
1893 g_source_remove(pending_le_conn_timer_id);
1894 __bt_connect_le_timer_cb(NULL);
1898 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1899 gboolean auto_connect)
1901 int ret = BLUETOOTH_ERROR_NONE;
1903 BT_CHECK_PARAMETER(bd_addr, return);
1905 ret = _bt_hold_current_advertising();
1906 if (ret == BLUETOOTH_ERROR_NONE) {
1907 BT_INFO("Current advertising is held");
1908 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1909 pending_le_conn_info->req_id = req_id;
1910 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1911 BLUETOOTH_ADDRESS_LENGTH);
1912 pending_le_conn_info->auto_connect = auto_connect;
1914 pending_le_conn_timer_id =
1915 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1917 return BLUETOOTH_ERROR_NONE;
1918 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
1919 BT_ERR("Unable to hold advertising");
1922 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1925 int _bt_disconnect_le_device(int req_id,
1926 const bluetooth_device_address_t *bd_addr)
1928 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1929 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1931 GDBusProxy *device_proxy;
1932 GDBusConnection *conn;
1933 int ret = BLUETOOTH_ERROR_NONE;
1934 bt_function_data_t *func_data;
1936 BT_CHECK_PARAMETER(bd_addr, return);
1938 conn = _bt_gdbus_get_system_gconn();
1939 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1941 _bt_convert_addr_type_to_string(device_address,
1942 (unsigned char *)bd_addr->addr);
1943 device_path = _bt_get_device_object_path(device_address);
1944 if (device_path == NULL) {
1945 BT_DBG("device_path NULL");
1946 ret = BLUETOOTH_ERROR_INTERNAL;
1950 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1952 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1953 NULL, BT_BLUEZ_NAME,
1954 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1955 g_free(device_path);
1956 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1958 func_data = g_malloc0(sizeof(bt_function_data_t));
1959 func_data->address = g_strdup(device_address);
1960 if (func_data->address == NULL) {
1961 BT_ERR("Unable to allocate memory for address");
1962 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1966 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1967 BT_INFO("Disconnect LE [%s]", secure_address);
1969 func_data->req_id = req_id;
1971 g_dbus_proxy_call(device_proxy, "DisconnectLE",
1973 G_DBUS_CALL_FLAGS_NONE,
1974 BT_MAX_DBUS_TIMEOUT,
1976 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1981 g_object_unref(device_proxy);
1983 g_free(func_data->address);
1989 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
1991 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1992 gchar *device_path = NULL;
1993 GError *error = NULL;
1994 GDBusProxy *device_proxy = NULL;
1995 GDBusProxy *adapter_proxy;
1996 GDBusConnection *conn;
1997 int ret = BLUETOOTH_ERROR_NONE;
1999 BT_CHECK_PARAMETER(bd_addr, return);
2001 _bt_convert_addr_type_to_string(device_address,
2002 (unsigned char *)bd_addr->addr);
2004 conn = _bt_gdbus_get_system_gconn();
2005 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2007 adapter_proxy = _bt_get_adapter_proxy();
2008 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2010 device_path = _bt_get_device_object_path(device_address);
2011 if (device_path == NULL) {
2012 BT_DBG("device_path NULL");
2013 ret = BLUETOOTH_ERROR_INTERNAL;
2017 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2019 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2020 NULL, BT_BLUEZ_NAME,
2021 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2022 g_free(device_path);
2023 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2025 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2027 G_DBUS_CALL_FLAGS_NONE,
2032 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2033 g_error_free(error);
2034 g_object_unref(device_proxy);
2035 return BLUETOOTH_ERROR_INTERNAL;
2038 g_object_unref(device_proxy);
2043 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2045 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2046 gchar *device_path = NULL;
2047 GError *error = NULL;
2048 GDBusProxy *device_proxy = NULL;
2049 GDBusProxy *adapter_proxy;
2050 GDBusConnection *conn;
2051 int ret = BLUETOOTH_ERROR_NONE;
2053 BT_CHECK_PARAMETER(bd_addr, return);
2055 _bt_convert_addr_type_to_string(device_address,
2056 (unsigned char *)bd_addr->addr);
2058 conn = _bt_gdbus_get_system_gconn();
2059 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2061 adapter_proxy = _bt_get_adapter_proxy();
2062 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2064 device_path = _bt_get_device_object_path(device_address);
2065 if (device_path == NULL) {
2066 BT_DBG("device_path NULL");
2067 ret = BLUETOOTH_ERROR_INTERNAL;
2071 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2073 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2074 NULL, BT_BLUEZ_NAME,
2075 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2076 g_free(device_path);
2077 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2079 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2081 G_DBUS_CALL_FLAGS_NONE,
2086 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2087 g_error_free(error);
2088 g_object_unref(device_proxy);
2089 return BLUETOOTH_ERROR_INTERNAL;
2092 g_object_unref(device_proxy);
2097 int _bt_connect_profile(char *address, char *uuid,
2098 void *cb, gpointer func_data)
2102 GDBusConnection *conn;
2103 GDBusProxy *adapter_proxy;
2104 GVariant *result = NULL;
2105 GError *error = NULL;
2107 conn = _bt_gdbus_get_system_gconn();
2108 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2110 object_path = _bt_get_device_object_path(address);
2111 if (object_path == NULL) {
2112 BT_ERR("No searched device");
2114 adapter_proxy = _bt_get_adapter_proxy();
2115 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2117 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2118 g_variant_new("(s)", address),
2119 G_DBUS_CALL_FLAGS_NONE,
2124 if (error != NULL) {
2125 BT_ERR("CreateDevice Fail: %s", error->message);
2126 g_error_free(error);
2129 g_variant_unref(result);
2131 object_path = _bt_get_device_object_path(address);
2133 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2135 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2136 NULL, BT_BLUEZ_NAME,
2137 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2138 g_free(object_path);
2139 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2141 g_dbus_proxy_call(proxy, "ConnectProfile",
2142 g_variant_new("(s)", uuid),
2143 G_DBUS_CALL_FLAGS_NONE,
2144 BT_MAX_DBUS_TIMEOUT,
2146 (GAsyncReadyCallback)cb,
2149 return BLUETOOTH_ERROR_NONE;
2152 int _bt_disconnect_all(char *address)
2154 int ret = BLUETOOTH_ERROR_NONE;
2157 GDBusConnection *conn;
2158 GVariant *result = NULL;
2162 conn = _bt_gdbus_get_system_gconn();
2163 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2165 object_path = _bt_get_device_object_path(address);
2166 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2168 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2169 NULL, BT_BLUEZ_NAME,
2170 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2171 g_free(object_path);
2172 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2174 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2176 G_DBUS_CALL_FLAGS_NONE,
2181 BT_ERR("Dbus Call Error:[%s]", err->message);
2183 ret = BLUETOOTH_ERROR_INTERNAL;
2186 g_object_unref(proxy);
2188 g_variant_unref(result);
2193 int _bt_disconnect_profile(char *address, char *uuid,
2194 void *cb, gpointer func_data)
2198 GDBusConnection *conn;
2200 conn = _bt_gdbus_get_system_gconn();
2201 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2203 object_path = _bt_get_device_object_path(address);
2204 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2206 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2207 NULL, BT_BLUEZ_NAME,
2208 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2209 g_free(object_path);
2210 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2212 g_dbus_proxy_call(proxy, "DisconnectProfile",
2213 g_variant_new("(s)", uuid),
2214 G_DBUS_CALL_FLAGS_NONE,
2215 BT_MAX_DBUS_TIMEOUT,
2217 (GAsyncReadyCallback)cb,
2220 return BLUETOOTH_ERROR_NONE;
2223 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2224 int low_threshold, int in_range_threshold, int high_threshold)
2226 int ret = BLUETOOTH_ERROR_NONE;
2228 GVariant *result = NULL;
2229 GError *error = NULL;
2230 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2232 BT_CHECK_PARAMETER(bd_addr, return);
2233 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2234 bd_addr->addr[0], bd_addr->addr[1],
2235 bd_addr->addr[2], bd_addr->addr[3],
2236 bd_addr->addr[4], bd_addr->addr[5],
2238 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2239 in_range_threshold, high_threshold);
2241 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2243 proxy = _bt_get_adapter_proxy();
2244 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2246 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2247 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2248 G_DBUS_CALL_FLAGS_NONE,
2252 if (error != NULL) {
2253 BT_ERR("Dbus Call Error:[%s]", error->message);
2254 g_error_free(error);
2255 ret = BLUETOOTH_ERROR_INTERNAL;
2259 g_variant_unref(result);
2264 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2267 int ret = BLUETOOTH_ERROR_NONE;
2269 GVariant *result = NULL;
2270 GError *error = NULL;
2271 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2273 BT_CHECK_PARAMETER(bd_addr, return);
2274 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2275 bd_addr->addr[0], bd_addr->addr[1],
2276 bd_addr->addr[2], bd_addr->addr[3],
2277 bd_addr->addr[4], bd_addr->addr[5],
2280 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2282 proxy = _bt_get_adapter_proxy();
2283 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2285 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2286 g_variant_new("(si)", address, link_type),
2287 G_DBUS_CALL_FLAGS_NONE,
2292 if (error != NULL) {
2293 BT_ERR("Dbus Call Error:[%s]", error->message);
2294 g_error_free(error);
2295 ret = BLUETOOTH_ERROR_INTERNAL;
2299 g_variant_unref(result);
2304 static int __bt_le_set_conn_parameter(const char *address,
2305 float interval_min, float interval_max,
2306 guint16 latency, guint16 time_out)
2308 gchar *device_path = NULL;
2309 GError *error = NULL;
2310 GDBusProxy *device_proxy = NULL;
2311 GDBusConnection *conn;
2313 guint32 min, max, to;
2314 int ret = BLUETOOTH_ERROR_NONE;
2316 BT_CHECK_PARAMETER(address, return);
2318 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2319 interval_min, interval_max, latency, time_out);
2321 device_path = _bt_get_device_object_path((char *)address);
2323 if (device_path == NULL) {
2324 BT_ERR("device_path NULL");
2325 ret = BLUETOOTH_ERROR_INTERNAL;
2329 conn = _bt_gdbus_get_system_gconn();
2331 BT_ERR("conn NULL");
2332 ret = BLUETOOTH_ERROR_INTERNAL;
2336 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2337 NULL, BT_BLUEZ_NAME,
2338 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2340 g_free(device_path);
2341 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2343 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2344 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2345 to = time_out / BT_LE_CONN_TO_SPLIT;
2347 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2348 g_variant_new("(uuuu)", min, max, latency, to),
2349 G_DBUS_CALL_FLAGS_NONE,
2354 g_object_unref(device_proxy);
2355 if (reply == NULL) {
2357 BT_ERR("Error %s[%s]", error->message, address);
2358 if (g_strrstr(error->message, "In Progress"))
2359 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2361 ret = BLUETOOTH_ERROR_INTERNAL;
2362 g_error_free(error);
2366 g_variant_unref(reply);
2372 static void __bt_le_conn_param_free(void *data)
2374 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2376 BT_DBG("%s", param->sender);
2377 g_free(param->sender);
2381 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2383 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2384 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2386 return parama->key > paramb->key;
2389 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2392 bt_connected_le_dev_t *dev;
2397 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2400 if (g_strcmp0(dev->address, address) == 0)
2406 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2409 bt_le_conn_param_t *param = NULL;
2411 if (!dev || !sender)
2414 for (l = dev->senders; l; l = g_slist_next(l)) {
2416 if (g_strcmp0(param->sender, sender) == 0)
2423 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2424 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2426 bt_connected_le_dev_t *dev = NULL;
2427 bt_le_conn_param_t *param = NULL;
2428 bt_le_conn_param_t *data = NULL;
2430 if (!address || !sender)
2431 return BLUETOOTH_ERROR_INVALID_PARAM;
2433 dev = __bt_get_le_connected_dev_info(address);
2435 return BLUETOOTH_ERROR_INTERNAL;
2437 param = __bt_get_le_conn_param_info(dev, sender);
2439 data = g_malloc0(sizeof(bt_le_conn_param_t));
2440 data->sender = g_strdup(sender);
2441 data->interval_min = interval_min;
2442 data->interval_max = interval_max;
2443 data->latency = latency;
2444 data->time_out = time_out;
2445 data->key = interval_min + (interval_max - interval_min)/2;
2447 if (param == NULL) {
2448 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2449 dev->senders = g_slist_append(dev->senders, data);
2451 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2452 dev->senders = g_slist_remove(dev->senders, param);
2453 g_free(param->sender);
2455 dev->senders = g_slist_append(dev->senders, data);
2458 /* Sorting. First element have the minimum interval */
2459 dev->senders = g_slist_sort(dev->senders,
2460 (GCompareFunc)__bt_compare_le_conn_param_key);
2462 return BLUETOOTH_ERROR_NONE;
2465 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2467 bt_connected_le_dev_t *dev = NULL;
2468 bt_le_conn_param_t *param = NULL;
2470 if (!address || !sender)
2471 return BLUETOOTH_ERROR_INVALID_PARAM;
2473 dev = __bt_get_le_connected_dev_info(address);
2475 return BLUETOOTH_ERROR_INTERNAL;
2477 param = __bt_get_le_conn_param_info(dev, sender);
2479 BT_DBG("Remove param %s %s ", address, sender);
2480 dev->senders = g_slist_remove(dev->senders, param);
2481 g_free(param->sender);
2485 *is_removed = FALSE;
2487 return BLUETOOTH_ERROR_NONE;
2490 int _bt_remove_all_le_conn_param_info(const char *sender)
2493 bt_connected_le_dev_t *dev = NULL;
2494 bt_le_conn_param_t *param = NULL;
2495 gboolean is_removed = FALSE;
2496 char *sender_new = NULL;
2497 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2498 int ret = BLUETOOTH_ERROR_NONE;
2501 return BLUETOOTH_ERROR_INVALID_PARAM;
2503 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2505 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2508 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2509 g_slist_length(dev->senders));
2510 if (g_slist_length(dev->senders) > 0) {
2511 param = dev->senders->data;
2512 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2513 param->interval_min, param->interval_max);
2515 if (dev->interval_min != param->interval_min ||
2516 dev->interval_max != param->interval_max) {
2517 sender_new = g_strdup(param->sender);
2519 _bt_convert_addr_string_to_type(addr, dev->address);
2520 ret = _bt_le_conn_update(sender_new, addr,
2521 param->interval_min, param->interval_max,
2522 param->latency, param->time_out);
2525 if (ret != BLUETOOTH_ERROR_NONE)
2526 BT_ERR("Unable to set le connection parameter");
2529 BT_INFO("Set the default interval");
2531 bluetooth_le_connection_param_t param = { 0 };
2532 _bt_get_le_connection_parameter(
2533 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2536 ret = __bt_le_set_conn_parameter(dev->address,
2537 param.interval_min, param.interval_max,
2538 param.latency, param.timeout);
2539 if (ret == BLUETOOTH_ERROR_NONE) {
2540 dev->interval_min = param.interval_min;
2541 dev->interval_max = param.interval_max;
2550 void _bt_add_le_connected_dev_info(const char *address)
2552 bt_connected_le_dev_t *dev = NULL;
2557 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2558 dev->address = g_strdup(address);
2560 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2565 void _bt_remove_le_connected_dev_info(const char *address)
2567 bt_connected_le_dev_t *dev = NULL;
2572 dev = __bt_get_le_connected_dev_info(address);
2576 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2577 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2578 g_free(dev->address);
2584 int _bt_le_conn_update(const char *sender,
2585 unsigned char *device_address,
2586 float interval_min, float interval_max,
2587 guint16 latency, guint16 time_out)
2589 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2590 guint32 min_supervision_to;
2591 bt_connected_le_dev_t *dev = NULL;
2592 bt_le_conn_param_t *param = NULL;
2593 gboolean is_removed = FALSE;
2594 int ret = BLUETOOTH_ERROR_NONE;
2596 BT_CHECK_PARAMETER(device_address, return);
2598 BT_DBG("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2599 sender, interval_min, interval_max, latency, time_out);
2601 if (interval_min > interval_max ||
2602 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2603 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2604 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2608 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2609 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2610 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2614 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2615 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2620 * The Supervision_Timeout in milliseconds shall be larger than
2621 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2622 * where Conn_Interval_Max is given in milliseconds.
2624 min_supervision_to = (1 + latency) * interval_max * 2;
2625 if (time_out <= min_supervision_to) {
2626 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2630 _bt_convert_addr_type_to_string(address, device_address);
2631 BT_DBG("Remote device address: %s", address);
2633 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2635 dev = __bt_get_le_connected_dev_info(address);
2637 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2641 if (g_slist_length(dev->senders) == 1)
2644 param = dev->senders->data;
2646 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2648 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2649 BT_DBG("Skip due to same interval");
2653 interval_min = param->interval_min;
2654 interval_max = param->interval_max;
2658 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2660 if (ret != BLUETOOTH_ERROR_NONE) {
2661 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2665 dev->interval_min = interval_min;
2666 dev->interval_max = interval_max;
2672 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2673 bluetooth_device_pin_code_t *pin_code)
2675 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2677 bt_pin_code_info_t *pin_info = NULL;
2679 BT_CHECK_PARAMETER(device_address, return);
2680 BT_CHECK_PARAMETER(pin_code, return);
2681 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2682 BLUETOOTH_ERROR_NO_RESOURCES);
2684 _bt_convert_addr_type_to_string(address, device_address->addr);
2686 for (l = pin_info_list; l != NULL; l = l->next) {
2689 if (g_strcmp0(pin_info->address, address) == 0) {
2690 g_free(pin_info->pin_code);
2691 pin_info->pin_code = g_strdup(pin_code->pin_code);
2692 return BLUETOOTH_ERROR_NONE;
2696 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2697 pin_info->address = g_strdup(address);
2698 pin_info->pin_code = g_strdup(pin_code->pin_code);
2699 pin_info_list = g_slist_append(pin_info_list, pin_info);
2701 return BLUETOOTH_ERROR_NONE;
2704 gint __bt_compare_address(gpointer *a, gpointer *b)
2706 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2707 char *address = (char *)b;
2708 return g_strcmp0(pin_info->address, address);
2711 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2713 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2715 bt_pin_code_info_t *pin_info = NULL;
2717 BT_CHECK_PARAMETER(device_address, return);
2719 _bt_convert_addr_type_to_string(address, device_address->addr);
2721 l = g_slist_find_custom(pin_info_list, address,
2722 (GCompareFunc)__bt_compare_address);
2726 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2727 g_free(pin_info->address);
2728 g_free(pin_info->pin_code);
2732 return BLUETOOTH_ERROR_NONE;
2735 int _bt_get_device_pin_code(const char *address, char *pin_code)
2739 BT_CHECK_PARAMETER(address, return);
2740 BT_CHECK_PARAMETER(pin_code, return);
2742 for (l = pin_info_list; l != NULL; l = l->next) {
2743 bt_pin_code_info_t *pin_info = l->data;
2745 if (g_strcmp0(pin_info->address, address) == 0) {
2746 g_strlcpy(pin_code, pin_info->pin_code,
2747 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2749 return BLUETOOTH_ERROR_NONE;
2753 return BLUETOOTH_ERROR_NOT_FOUND;
2756 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2757 bluetooth_le_connection_param_t *param)
2760 return BLUETOOTH_ERROR_INVALID_PARAM;
2762 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2763 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2764 return BLUETOOTH_ERROR_INVALID_PARAM;
2766 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2769 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2770 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2771 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2772 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2773 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2776 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2777 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2778 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2779 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2780 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2783 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2784 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2785 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2786 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2787 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2791 BT_ERR("Unhandled mode : %d", mode);
2795 return BLUETOOTH_ERROR_NONE;
2798 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2799 guint trusted_profile_flag, guint *trusted)
2805 case TRUSTED_PROFILE_PBAP:
2806 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2807 trust_profile = trusted_profile_flag & (1 << 1);
2809 return BLUETOOTH_ERROR_NOT_SUPPORT;
2811 case TRUSTED_PROFILE_MAP:
2812 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2813 trust_profile = trusted_profile_flag & (1 << 3);
2815 return BLUETOOTH_ERROR_NOT_SUPPORT;
2817 case TRUSTED_PROFILE_SAP:
2818 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2819 trust_profile = trusted_profile_flag & (1 << 5);
2821 return BLUETOOTH_ERROR_NOT_SUPPORT;
2823 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2824 *trusted = trusted_profile_flag;
2825 return BLUETOOTH_ERROR_NONE;
2827 return BLUETOOTH_ERROR_NOT_SUPPORT;
2833 return BLUETOOTH_ERROR_NONE;
2836 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2837 guint restricted_profile_flag, guint *restricted)
2839 int restrict_profile;
2840 *restricted = FALSE;
2843 case RESTRICTED_PROFILE_HFP_HS:
2844 restrict_profile = restricted_profile_flag & (1 << 0);
2846 case RESTRICTED_PROFILE_A2DP:
2847 restrict_profile = restricted_profile_flag & (1 << 2);
2850 return BLUETOOTH_ERROR_NOT_SUPPORT;
2853 if (restrict_profile)
2856 return BLUETOOTH_ERROR_NONE;
2859 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2862 case TRUSTED_PROFILE_PBAP:
2863 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2864 case TRUSTED_PROFILE_MAP:
2865 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2866 case TRUSTED_PROFILE_SAP:
2867 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2868 case TRUSTED_PROFILE_ALL:
2875 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2878 case RESTRICTED_PROFILE_HFP_HS:
2879 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2880 case RESTRICTED_PROFILE_A2DP:
2881 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2887 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2889 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2890 return TRUSTED_PROFILE_PBAP;
2891 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2892 return TRUSTED_PROFILE_MAP;
2893 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2894 return TRUSTED_PROFILE_SAP;
2896 return 0; /* 0 - Unknown Profile */
2899 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2900 bluetooth_trusted_profile_t profile, gboolean trust)
2902 int ret = BLUETOOTH_ERROR_NONE;
2903 GDBusConnection *conn;
2905 GError *error = NULL;
2906 char *device_path = NULL;
2908 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2911 BT_CHECK_PARAMETER(bd_addr, return);
2912 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2913 bd_addr->addr[0], bd_addr->addr[1],
2914 bd_addr->addr[2], bd_addr->addr[3],
2915 bd_addr->addr[4], bd_addr->addr[5],
2918 conn = _bt_gdbus_get_system_gconn();
2919 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2921 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2923 device_path = _bt_get_device_object_path(address);
2924 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2926 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2927 NULL, BT_BLUEZ_NAME, device_path,
2928 BT_DEVICE_INTERFACE, NULL, NULL);
2930 g_free(device_path);
2931 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2933 uuid = _bt_get_trusted_profile_uuid(profile);
2935 g_object_unref(proxy);
2936 return BLUETOOTH_ERROR_NOT_SUPPORT;
2939 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2940 g_variant_new("(sb)", uuid, trust),
2941 G_DBUS_CALL_FLAGS_NONE, -1,
2943 g_object_unref(proxy);
2945 if (reply == NULL) {
2946 BT_ERR("Failed to Set Profile Trusted");
2947 ret = BLUETOOTH_ERROR_INTERNAL;
2949 BT_ERR("Error %s[%s]", error->message, address);
2950 g_error_free(error);
2954 g_variant_unref(reply);
2961 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2962 bluetooth_trusted_profile_t profile, guint *trust)
2964 int ret = BLUETOOTH_ERROR_NONE;
2965 GDBusConnection *conn;
2967 GError *error = NULL;
2968 char *device_path = NULL;
2969 guint trusted_profile_flag;
2970 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2973 BT_CHECK_PARAMETER(bd_addr, return);
2974 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2975 bd_addr->addr[0], bd_addr->addr[1],
2976 bd_addr->addr[2], bd_addr->addr[3],
2977 bd_addr->addr[4], bd_addr->addr[5],
2980 conn = _bt_gdbus_get_system_gconn();
2981 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2983 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2985 device_path = _bt_get_device_object_path(address);
2986 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2988 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2989 NULL, BT_BLUEZ_NAME, device_path,
2990 BT_PROPERTIES_INTERFACE, NULL, NULL);
2992 g_free(device_path);
2993 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2995 reply = g_dbus_proxy_call_sync(proxy, "Get",
2996 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2997 G_DBUS_CALL_FLAGS_NONE, -1,
2999 g_object_unref(proxy);
3001 if (reply == NULL) {
3002 BT_ERR("Failed to Get Profile Trusted");
3003 ret = BLUETOOTH_ERROR_INTERNAL;
3005 BT_ERR("Error %s[%s]", error->message, address);
3006 g_error_free(error);
3011 g_variant_get(reply, "(v)", &temp);
3012 trusted_profile_flag = g_variant_get_uint32(temp);
3013 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3015 ret = _bt_get_trusted_profile_from_flag(profile,
3016 trusted_profile_flag, trust);
3017 g_variant_unref(temp);
3018 g_variant_unref(reply);
3021 BT_DBG("TRUST %d", *trust);
3025 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3026 bluetooth_restricted_profile_t profile, gboolean restricted)
3028 int ret = BLUETOOTH_ERROR_NONE;
3029 GDBusConnection *conn;
3031 GError *error = NULL;
3032 char *device_path = NULL;
3034 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3037 BT_CHECK_PARAMETER(bd_addr, return);
3038 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3039 bd_addr->addr[0], bd_addr->addr[1],
3040 bd_addr->addr[2], bd_addr->addr[3],
3041 bd_addr->addr[4], bd_addr->addr[5],
3042 profile, restricted);
3044 conn = _bt_gdbus_get_system_gconn();
3045 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3047 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3049 device_path = _bt_get_device_object_path(address);
3050 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3052 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3053 NULL, BT_BLUEZ_NAME, device_path,
3054 BT_DEVICE_INTERFACE, NULL, NULL);
3056 g_free(device_path);
3057 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3059 uuid = _bt_get_restricted_profile_uuid(profile);
3061 g_object_unref(proxy);
3062 return BLUETOOTH_ERROR_NOT_SUPPORT;
3065 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3066 g_variant_new("(sb)", uuid, restricted),
3067 G_DBUS_CALL_FLAGS_NONE, -1,
3069 g_object_unref(proxy);
3071 if (reply == NULL) {
3072 BT_ERR("Failed to Set Profile Restricted");
3073 ret = BLUETOOTH_ERROR_INTERNAL;
3075 BT_ERR("Error %s[%s]", error->message, address);
3076 g_error_free(error);
3080 g_variant_unref(reply);
3087 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3088 bluetooth_restricted_profile_t profile, guint *restricted)
3090 int ret = BLUETOOTH_ERROR_NONE;
3091 GDBusConnection *conn;
3093 GError *error = NULL;
3094 char *device_path = NULL;
3095 guint restricted_profile_flag;
3096 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3099 BT_CHECK_PARAMETER(bd_addr, return);
3100 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3101 bd_addr->addr[0], bd_addr->addr[1],
3102 bd_addr->addr[2], bd_addr->addr[3],
3103 bd_addr->addr[4], bd_addr->addr[5],
3104 profile, *restricted);
3106 conn = _bt_gdbus_get_system_gconn();
3107 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3109 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3111 device_path = _bt_get_device_object_path(address);
3112 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3114 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3115 NULL, BT_BLUEZ_NAME, device_path,
3116 BT_PROPERTIES_INTERFACE, NULL, NULL);
3118 g_free(device_path);
3119 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3121 reply = g_dbus_proxy_call_sync(proxy, "Get",
3122 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3123 G_DBUS_CALL_FLAGS_NONE, -1,
3125 g_object_unref(proxy);
3127 if (reply == NULL) {
3128 BT_ERR("Failed to Get Profile Restricted");
3129 ret = BLUETOOTH_ERROR_INTERNAL;
3131 BT_ERR("Error %s[%s]", error->message, address);
3132 g_error_free(error);
3137 g_variant_get(reply, "(v)", &temp);
3138 restricted_profile_flag = g_variant_get_uint32(temp);
3139 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3141 ret = _bt_get_restricted_profile_from_flag(profile,
3142 restricted_profile_flag, restricted);
3143 g_variant_unref(temp);
3144 g_variant_unref(reply);
3147 BT_DBG("TRUST %d", *restricted);
3151 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3155 GVariant *out_param1;
3156 request_info_t *req_info;
3157 GVariant *val = NULL;
3158 GVariant *param = NULL;
3161 bluetooth_device_address_t device_addr = { {0} };
3162 int result = BLUETOOTH_ERROR_NONE;
3165 val = g_dbus_proxy_call_finish(proxy, res, &err);
3167 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3168 if (req_info == NULL) {
3169 BT_ERR("req_info == NULL");
3170 g_object_unref(proxy);
3171 att_mtu_req_info->device_proxy = NULL;
3176 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3178 if (g_strrstr(err->message, "NotSupported")) {
3179 BT_INFO("Connection Not Supported");
3180 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3181 } else if (g_strrstr(err->message, "NotConnected")) {
3182 BT_INFO("Not connected");
3183 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3184 } else if (g_strrstr(err->message, "InvalidArguments")) {
3185 BT_INFO("Not connected");
3186 result = BLUETOOTH_ERROR_INVALID_PARAM;
3188 BT_DBG("Default case");
3189 result = BLUETOOTH_ERROR_INTERNAL;
3193 g_object_unref(proxy);
3194 att_mtu_req_info->device_proxy = NULL;
3196 if (result != BLUETOOTH_ERROR_NONE)
3200 g_variant_get(val, "(qy)", &mtu, &status);
3201 g_variant_unref(val);
3204 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3206 param = g_variant_new("(isqy)",
3208 att_mtu_req_info->addr,
3212 /* Send the event to application */
3213 _bt_send_event(BT_DEVICE_EVENT,
3214 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3218 if (req_info->context == NULL)
3221 _bt_convert_addr_string_to_type(device_addr.addr,
3222 (const char *)att_mtu_req_info->addr);
3224 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3225 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3226 g_dbus_method_invocation_return_value(req_info->context,
3227 g_variant_new("(iv)", result, out_param1));
3229 _bt_delete_request_list(req_info->req_id);
3234 g_free(att_mtu_req_info->addr);
3235 g_free(att_mtu_req_info);
3236 att_mtu_req_info = NULL;
3241 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3244 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3245 gchar *device_path = NULL;
3246 GDBusProxy *adapter_proxy;
3247 GDBusProxy *device_proxy;
3248 GDBusConnection *conn;
3249 int ret = BLUETOOTH_ERROR_NONE;
3251 BT_CHECK_PARAMETER(device_address, return);
3253 if (att_mtu_req_info) {
3254 BT_ERR("ATT MTU request in progress");
3255 return BLUETOOTH_ERROR_DEVICE_BUSY;
3258 conn = _bt_gdbus_get_system_gconn();
3259 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3261 adapter_proxy = _bt_get_adapter_proxy();
3262 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3264 _bt_convert_addr_type_to_string(address, device_address->addr);
3266 BT_DBG("Remote device address: %s", address);
3268 device_path = _bt_get_device_object_path(address);
3270 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3272 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3273 NULL, BT_BLUEZ_NAME,
3274 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3275 g_free(device_path);
3276 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3278 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3279 att_mtu_req_info->addr = (char *)g_strdup(address);
3280 att_mtu_req_info->req_id = request_id;
3281 att_mtu_req_info->device_proxy = device_proxy;
3283 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3284 g_variant_new("(q)", mtu),
3285 G_DBUS_CALL_FLAGS_NONE,
3286 BT_MAX_DBUS_TIMEOUT,
3288 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3294 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3297 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3298 char *object_path = NULL;
3300 GDBusProxy *device_proxy;
3301 GError *error = NULL;
3303 GDBusConnection *conn;
3304 GVariant *result = NULL;
3306 BT_CHECK_PARAMETER(device_address, return);
3308 conn = _bt_gdbus_get_system_gconn();
3309 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3311 _bt_convert_addr_type_to_string(address, device_address->addr);
3313 object_path = _bt_get_device_object_path(address);
3314 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3316 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3317 NULL, BT_BLUEZ_NAME, object_path,
3318 BT_PROPERTIES_INTERFACE, NULL, NULL);
3319 g_free(object_path);
3320 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3322 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3323 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3324 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3326 ERR("DBus Error : %s", error->message);
3327 g_clear_error(&error);
3328 g_object_unref(device_proxy);
3329 return BLUETOOTH_ERROR_INTERNAL;
3331 g_variant_get(result, "(v)", &value);
3332 *mtu = g_variant_get_uint16(value);
3333 BT_DBG("ATT MTU : %d", *mtu);
3334 g_variant_unref(value);
3335 g_variant_unref(result);
3338 g_object_unref(device_proxy);
3339 return BLUETOOTH_ERROR_NOT_CONNECTED;
3343 g_object_unref(device_proxy);
3344 return BLUETOOTH_ERROR_NONE;
3347 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3348 bluetooth_device_address_t *id_address)
3350 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3351 gchar *device_path = NULL;
3352 const gchar *idaddress = NULL;
3353 GDBusProxy *device_proxy;
3354 GError *error = NULL;
3355 GVariant *result = NULL;
3356 GDBusConnection *conn;
3357 int ret = BLUETOOTH_ERROR_NONE;
3359 BT_CHECK_PARAMETER(device_address, return);
3361 conn = _bt_gdbus_get_system_gconn();
3362 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3364 _bt_convert_addr_type_to_string(address, device_address->addr);
3366 device_path = _bt_get_device_object_path(address);
3367 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3369 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3370 NULL, BT_BLUEZ_NAME,
3371 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3372 g_free(device_path);
3373 if (!device_proxy) {
3374 BT_ERR("Unable to get proxy");
3375 return BLUETOOTH_ERROR_INTERNAL;
3378 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3379 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3380 if (result == NULL) {
3381 BT_ERR("Failed to get device ID address");
3383 ret = BLUETOOTH_ERROR_INTERNAL;
3384 if (error != NULL) {
3385 g_dbus_error_strip_remote_error(error);
3386 BT_ERR("Error occured in Proxy call [%s]", error->message);
3387 if (g_strcmp0(error->message, "Does Not Exist") == 0)
3388 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3389 g_error_free(error);
3391 g_object_unref(device_proxy);
3395 g_variant_get(result , "(&s)", &idaddress);
3397 DBG_SECURE("ID Address : %s", idaddress);
3398 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3400 g_variant_unref(result);
3401 g_object_unref(device_proxy);
3406 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3408 GapAgentPrivate *agent = _bt_get_adapter_agent();
3409 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3411 if (authentication_reply)
3412 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3414 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3416 BT_DBG("BT_PASSKEY_REPLY");
3417 return BLUETOOTH_ERROR_NONE;
3420 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3422 GapAgentPrivate *agent = _bt_get_adapter_agent();
3423 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3425 if (confirmation_reply)
3426 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3428 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3430 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3431 return BLUETOOTH_ERROR_NONE;