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;
700 bt_remote_dev_info_t *remote_dev_info;
701 GVariant *manufacture_data;
704 /* Terminate ALL system popup */
705 syspopup_destroy_all();
707 reply = g_dbus_proxy_call_finish(proxy, res, &err);
709 g_variant_unref(reply);
711 is_device_creating = FALSE;
713 if (bonding_info == NULL) {
715 BT_ERR("bonding_info == NULL");
721 req_info = _bt_get_request_info(bonding_info->req_id);
722 if (req_info == NULL) {
723 BT_ERR("req_info == NULL");
728 g_dbus_error_strip_remote_error(err);
729 BT_ERR("Error occured in CreateBonding [%s]", err->message);
731 if (g_strrstr(err->message, "Already Exists")) {
732 BT_INFO("Existing Bond");
733 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
734 } else if (_bt_agent_is_canceled() ||
735 g_strrstr(err->message, "Authentication Canceled")) {
736 BT_INFO("Cancelled by USER");
737 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
738 } else if (g_strrstr(err->message, "Authentication Rejected")) {
740 result = BLUETOOTH_ERROR_ACCESS_DENIED;
741 } else if (g_strrstr(err->message, "In Progress")) {
742 BT_INFO("Bond in progress");
743 result = BLUETOOTH_ERROR_IN_PROGRESS;
744 } else if (g_strrstr(err->message, "Authentication Failed")) {
745 BT_INFO("Authentication Failed");
746 if (bonding_info->is_autopair == TRUE) {
747 _bt_set_autopair_status_in_bonding_info(FALSE);
748 __ignore_auto_pairing_request(bonding_info->addr);
750 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
751 } else if (g_strrstr(err->message, "Page Timeout")) {
752 BT_INFO("Page Timeout");
753 /* This is the special case
754 As soon as call bluetooth_bond_device, try to cancel bonding.
755 In this case, before completing to call 'CreatePairedDevice' method
756 the procedure is stopped. So 'Cancle' error is not return.
758 result = BLUETOOTH_ERROR_HOST_DOWN;
759 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
760 BT_INFO("Cancel already running bonding");
761 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
762 BT_INFO("Error while Cancelling bonding");
763 /* we need to unref proxy so continue */
765 result = BLUETOOTH_ERROR_INTERNAL;
766 } else if (g_strrstr(err->message, "Connection Timeout")) {
767 /* Pairing request timeout */
768 result = BLUETOOTH_ERROR_TIMEOUT;
769 } else if (g_strrstr(err->message, "Authentication Timeout")) {
770 /* Pairing request timeout */
771 result = BLUETOOTH_ERROR_TIMEOUT;
773 BT_DBG("Default case");
774 result = BLUETOOTH_ERROR_PARING_FAILED;
778 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
779 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
780 result == BLUETOOTH_ERROR_TIMEOUT ||
781 result == BLUETOOTH_ERROR_HOST_DOWN) {
782 #ifdef TIZEN_PROFILE_WEARABLE
783 int is_sw_running = 0;
785 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
786 BT_ERR("vconf_get_int for setup wizard state failed");
789 __bt_launch_unable_to_pairing_syspopup(result);
791 BT_ERR("Unable to pair");
793 bonding_info->result = result;
796 g_object_unref(proxy);
797 bonding_info->device_proxy = NULL;
799 if (result != BLUETOOTH_ERROR_NONE) {
800 if (bonding_info->bonding_wo_discovery) {
801 GDBusProxy *adapter_proxy;
802 GVariant *ret = NULL;
803 GError *error = NULL;
806 BT_ERR("Bond was tried without discovery. Remove it");
808 adapter_proxy = _bt_get_adapter_proxy();
809 if (adapter_proxy == NULL) {
810 BT_ERR("Cannot get adapter_proxy");
814 device_path = _bt_get_device_object_path(bonding_info->addr);
815 if (device_path == NULL) {
816 BT_ERR("Cannot get device path");
820 ret = g_dbus_proxy_call_sync(adapter_proxy,
822 g_variant_new("(o)", device_path),
823 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
826 BT_ERR("RemoveDevice Fail: %s", error->message);
827 g_clear_error(&error);
831 g_variant_unref(ret);
839 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
840 if (!remote_dev_info)
843 GVariant *uuids = NULL;
844 GVariantBuilder *builder = NULL;
846 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
847 for (i = 0; i < remote_dev_info->uuid_count; i++) {
848 g_variant_builder_add(builder, "s",
849 remote_dev_info->uuids[i]);
851 uuids = g_variant_new("as", builder);
852 g_variant_builder_unref(builder);
853 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
854 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
857 param = g_variant_new("(isunsbub@asn@ay)",
860 remote_dev_info->class,
861 remote_dev_info->rssi,
862 remote_dev_info->name,
863 remote_dev_info->paired,
864 remote_dev_info->connected,
865 remote_dev_info->trust,
867 remote_dev_info->manufacturer_data_len,
871 /* Send the event to application */
872 _bt_send_event(BT_ADAPTER_EVENT,
873 BLUETOOTH_EVENT_BONDING_FINISHED,
876 _bt_free_device_info(remote_dev_info);
879 if (req_info->context == NULL)
882 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
883 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
886 if (_bt_adapter_get_status() != BT_ACTIVATED)
887 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
889 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
890 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
892 g_dbus_method_invocation_return_value(req_info->context,
893 g_variant_new("(iv)", result, out_param1));
895 g_variant_unref(out_param1);
897 _bt_delete_request_list(req_info->req_id);
902 _bt_agent_set_canceled(FALSE);
905 g_free(bonding_info->addr);
906 g_free(bonding_info);
910 int _bt_bond_device(int request_id,
911 bluetooth_device_address_t *device_address,
912 unsigned short conn_type, GArray **out_param1)
915 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
916 bluetooth_device_info_t dev_info;
918 GDBusConnection *conn;
919 char *device_path = NULL;
920 GDBusProxy *adapter_proxy;
921 GError *error = NULL;
922 gboolean bonding_wo_discovery = FALSE;
924 BT_CHECK_PARAMETER(device_address, return);
927 BT_ERR("Bonding in progress");
929 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
930 memcpy(dev_info.device_address.addr, device_address->addr,
931 BLUETOOTH_ADDRESS_LENGTH);
933 g_array_append_vals(*out_param1, &dev_info,
934 sizeof(bluetooth_device_info_t));
936 return BLUETOOTH_ERROR_DEVICE_BUSY;
939 conn = _bt_gdbus_get_system_gconn();
940 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
942 _bt_convert_addr_type_to_string(address, device_address->addr);
944 device_path = _bt_get_device_object_path(address);
946 if (device_path == NULL) {
947 BT_ERR("No searched device");
948 GVariant *ret = NULL;
949 adapter_proxy = _bt_get_adapter_proxy();
950 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
952 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
953 g_variant_new("(s)", address),
954 G_DBUS_CALL_FLAGS_NONE,
960 BT_ERR("CreateDevice Fail: %s", error->message);
961 g_clear_error(&error);
964 g_variant_unref(ret);
965 device_path = _bt_get_device_object_path(address);
966 if (device_path == NULL) {
967 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
968 memcpy(dev_info.device_address.addr, device_address->addr,
969 BLUETOOTH_ADDRESS_LENGTH);
971 g_array_append_vals(*out_param1, &dev_info,
972 sizeof(bluetooth_device_info_t));
974 return BLUETOOTH_ERROR_NOT_PAIRED;
976 BT_INFO("device_path is created[%s]", device_path);
978 bonding_wo_discovery = TRUE;
981 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
983 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
986 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
988 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
989 bonding_info->addr = g_strdup(address);
990 bonding_info->req_id = request_id;
992 bonding_info->device_proxy = proxy;
993 bonding_info->conn_type = conn_type;
994 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
996 is_device_creating = TRUE;
998 g_dbus_proxy_call(proxy, "Pair",
999 g_variant_new("(y)", conn_type),
1000 G_DBUS_CALL_FLAGS_NONE,
1001 BT_MAX_DBUS_TIMEOUT,
1003 (GAsyncReadyCallback)__bt_bond_device_cb,
1006 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1008 return BLUETOOTH_ERROR_NONE;
1010 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1011 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1012 bonding_info->addr);
1014 g_array_append_vals(*out_param1, &dev_info,
1015 sizeof(bluetooth_device_info_t));
1017 is_device_creating = FALSE;
1019 g_free(bonding_info->addr);
1020 g_free(bonding_info);
1021 bonding_info = NULL;
1023 return BLUETOOTH_ERROR_INTERNAL;*/
1026 int _bt_cancel_bonding(void)
1028 int ret = BLUETOOTH_ERROR_NONE;
1030 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1032 ret = _bt_agent_reply_cancellation();
1033 if (ret != BLUETOOTH_ERROR_NONE) {
1034 BT_ERR("Fail to call reply cancellation");
1038 _bt_agent_set_canceled(TRUE);
1040 return BLUETOOTH_ERROR_NONE;
1043 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1048 GVariant *out_param1;
1049 int result = BLUETOOTH_ERROR_NONE;
1050 bt_funcion_data_t *unbonding_info;
1051 bluetooth_device_info_t dev_info;
1052 request_info_t *req_info;
1054 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1056 g_variant_unref(reply);
1058 unbonding_info = user_data;
1060 if (unbonding_info == NULL) {
1062 BT_ERR("unbonding_info == NULL");
1066 req_info = _bt_get_request_info(unbonding_info->req_id);
1067 if (req_info == NULL) {
1068 BT_ERR("req_info == NULL");
1073 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1074 result = BLUETOOTH_ERROR_INTERNAL;
1077 if (req_info->context == NULL)
1080 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1081 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1082 unbonding_info->addr);
1084 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1085 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1087 g_dbus_method_invocation_return_value(req_info->context,
1088 g_variant_new("(iv)", result, out_param1));
1091 _bt_delete_request_list(req_info->req_id);
1097 if (unbonding_info) {
1098 g_free(unbonding_info->addr);
1099 g_free(unbonding_info);
1103 int _bt_unbond_device(int request_id,
1104 bluetooth_device_address_t *device_address,
1105 GArray **out_param1)
1107 char *device_path = NULL;
1108 bt_funcion_data_t *unbonding_info;
1109 GDBusProxy *adapter_proxy = NULL;
1110 GDBusProxy *device_proxy = NULL;
1111 GDBusConnection *conn;
1112 int result = BLUETOOTH_ERROR_INTERNAL;
1113 bluetooth_device_info_t dev_info;
1114 GError *error = NULL;
1115 GVariant *ret = NULL;
1117 BT_CHECK_PARAMETER(device_address, return);
1119 adapter_proxy = _bt_get_adapter_proxy();
1120 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1122 /* allocate user data so that it can be retrieved in callback */
1123 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1124 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1125 unbonding_info->req_id = request_id;
1127 _bt_convert_addr_type_to_string(unbonding_info->addr,
1128 device_address->addr);
1130 device_path = _bt_get_device_object_path(unbonding_info->addr);
1132 if (device_path == NULL) {
1133 BT_ERR("No paired device");
1134 result = BLUETOOTH_ERROR_NOT_PAIRED;
1138 conn = _bt_gdbus_get_system_gconn();
1140 BT_ERR("conn is NULL");
1141 result = BLUETOOTH_ERROR_INTERNAL;
1145 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1146 NULL, BT_BLUEZ_NAME,
1147 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1149 if (device_proxy != NULL) {
1151 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1152 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1153 G_DBUS_CALL_FLAGS_NONE,
1158 BT_ERR("Getting property failed: [%s]\n", error->message);
1159 g_error_free(error);
1160 result = BLUETOOTH_ERROR_NOT_PAIRED;
1164 BT_ERR("No paired device");
1165 g_object_unref(device_proxy);
1166 result = BLUETOOTH_ERROR_NOT_PAIRED;
1169 g_variant_unref(ret);
1171 g_object_unref(device_proxy);
1174 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1175 g_variant_new("(o)", device_path),
1176 G_DBUS_CALL_FLAGS_NONE,
1177 BT_MAX_DBUS_TIMEOUT,
1179 (GAsyncReadyCallback)__bt_unbond_cb,
1182 g_free(device_path);
1183 return BLUETOOTH_ERROR_NONE;
1186 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1187 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1188 unbonding_info->addr);
1190 g_array_append_vals(*out_param1, &dev_info,
1191 sizeof(bluetooth_device_info_t));
1192 g_free(device_path);
1193 g_free(unbonding_info->addr);
1194 g_free(unbonding_info);
1198 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1203 GVariant *out_param1;
1204 int result = BLUETOOTH_ERROR_NONE;
1205 bluetooth_device_info_t dev_info;
1206 bt_remote_dev_info_t *remote_dev_info;
1207 request_info_t *req_info;
1208 GVariant *uuid_list, *manufacture_data;
1210 GVariantBuilder *builder = NULL;
1213 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1215 g_variant_unref(reply);
1217 g_object_unref(proxy);
1219 if (searching_info == NULL) {
1221 BT_ERR("unbonding_info == NULL");
1225 req_info = _bt_get_request_info(searching_info->req_id);
1226 if (req_info == NULL) {
1227 BT_ERR("req_info == NULL");
1232 g_dbus_error_strip_remote_error(err);
1233 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1235 if (g_strrstr("Operation canceled", err->message))
1236 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1237 else if (g_strrstr("In Progress", err->message))
1238 result = BLUETOOTH_ERROR_IN_PROGRESS;
1239 else if (g_strrstr("Host is down", err->message))
1240 result = BLUETOOTH_ERROR_HOST_DOWN;
1242 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1244 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1245 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1246 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1247 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1248 remote_dev_info->uuid_count > 0) {
1249 result = BLUETOOTH_ERROR_NONE;
1252 _bt_free_device_info(remote_dev_info);
1257 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1258 if (!remote_dev_info)
1262 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1263 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1264 g_variant_builder_add(builder, "s",
1265 remote_dev_info->uuids[i]);
1267 uuid_list = g_variant_new("as", builder);
1268 g_variant_builder_unref(builder);
1269 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1270 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1273 param = g_variant_new("(isunsbub@asn@ay)",
1275 searching_info->addr,
1276 remote_dev_info->class,
1277 remote_dev_info->rssi,
1278 remote_dev_info->name,
1279 remote_dev_info->paired,
1280 remote_dev_info->connected,
1281 remote_dev_info->trust,
1283 remote_dev_info->manufacturer_data_len,
1286 /* Send the event to application */
1287 _bt_send_event(BT_ADAPTER_EVENT,
1288 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1291 _bt_free_device_info(remote_dev_info);
1294 if (req_info->context == NULL)
1297 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1298 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1299 searching_info->addr);
1301 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1302 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1304 g_dbus_method_invocation_return_value(req_info->context,
1305 g_variant_new("(iv)", result, out_param1));
1308 _bt_delete_request_list(req_info->req_id);
1313 if (searching_info) {
1314 g_free(searching_info->addr);
1315 g_free(searching_info);
1316 searching_info = NULL;
1320 int _bt_search_device(int request_id,
1321 bluetooth_device_address_t *device_address)
1323 char *device_path = NULL;
1324 GDBusProxy *device_proxy = NULL;
1325 GDBusConnection *conn;
1327 GDBusProxy *adapter_proxy;
1328 int result = BLUETOOTH_ERROR_INTERNAL;
1330 BT_CHECK_PARAMETER(device_address, return);
1333 BT_ERR("Bonding in progress");
1334 return BLUETOOTH_ERROR_DEVICE_BUSY;
1337 if (searching_info) {
1338 BT_ERR("Service searching in progress");
1339 return BLUETOOTH_ERROR_DEVICE_BUSY;
1342 adapter_proxy = _bt_get_adapter_proxy();
1343 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1345 /* allocate user data so that it can be retrieved in callback */
1346 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1347 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1348 searching_info->req_id = request_id;
1350 _bt_convert_addr_type_to_string(searching_info->addr,
1351 device_address->addr);
1353 conn = _bt_gdbus_get_system_gconn();
1354 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1357 device_path = _bt_get_device_object_path(searching_info->addr);
1359 if (device_path == NULL) {
1360 BT_ERR("No paired device");
1361 result = BLUETOOTH_ERROR_NOT_PAIRED;
1365 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1366 NULL, BT_BLUEZ_NAME,
1367 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1368 g_free(device_path);
1369 if (device_proxy == NULL) {
1370 result = BLUETOOTH_ERROR_INTERNAL;
1374 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1375 g_variant_new("(s)", ""),
1376 G_DBUS_CALL_FLAGS_NONE,
1377 BT_MAX_DBUS_TIMEOUT,
1379 (GAsyncReadyCallback)__bt_discover_cb,
1382 searching_info->device_proxy = device_proxy;
1384 return BLUETOOTH_ERROR_NONE;
1387 g_free(searching_info->addr);
1388 g_free(searching_info);
1389 searching_info = NULL;
1393 int _bt_cancel_search_device(void)
1395 GVariant *ret = NULL;
1398 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1400 if (searching_info->device_proxy) {
1401 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1403 G_DBUS_CALL_FLAGS_NONE,
1408 g_variant_unref(ret);
1410 __bt_cancel_search_service_done();
1412 return BLUETOOTH_ERROR_NONE;
1415 int _bt_set_alias(bluetooth_device_address_t *device_address,
1418 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1419 gchar *device_path = NULL;
1420 GDBusProxy *adapter_proxy;
1421 GDBusProxy *device_proxy;
1422 GVariant *ret = NULL;
1423 GError *error = NULL;
1424 GDBusConnection *conn;
1426 BT_CHECK_PARAMETER(device_address, return);
1427 BT_CHECK_PARAMETER(alias, return);
1429 adapter_proxy = _bt_get_adapter_proxy();
1430 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1432 conn = _bt_gdbus_get_system_gconn();
1433 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1435 _bt_convert_addr_type_to_string(address, device_address->addr);
1437 device_path = _bt_get_device_object_path(address);
1439 if (device_path == NULL) {
1440 BT_ERR("No paired device");
1441 return BLUETOOTH_ERROR_NOT_PAIRED;
1444 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1445 NULL, BT_BLUEZ_NAME,
1446 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1448 g_free(device_path);
1449 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1451 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1452 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1453 G_DBUS_CALL_FLAGS_NONE,
1458 g_variant_unref(ret);
1460 g_object_unref(device_proxy);
1463 BT_ERR("SetProperty error: [%s]", error->message);
1464 g_error_free(error);
1465 return BLUETOOTH_ERROR_INTERNAL;
1468 return BLUETOOTH_ERROR_NONE;
1471 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1474 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1475 gchar *device_path = NULL;
1476 GDBusProxy *device_proxy;
1477 gboolean previous_value;
1478 GError *error = NULL;
1479 GDBusConnection *conn;
1480 GVariant *result = NULL;
1481 GVariant *temp = NULL;
1482 int ret = BLUETOOTH_ERROR_NONE;
1484 BT_CHECK_PARAMETER(device_address, return);
1486 conn = _bt_gdbus_get_system_gconn();
1487 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1489 _bt_convert_addr_type_to_string(address, device_address->addr);
1491 device_path = _bt_get_device_object_path(address);
1493 if (device_path == NULL) {
1494 BT_ERR("No paired device");
1495 return BLUETOOTH_ERROR_NOT_PAIRED;
1498 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1499 NULL, BT_BLUEZ_NAME,
1500 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1502 g_free(device_path);
1503 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1505 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1506 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1507 G_DBUS_CALL_FLAGS_NONE,
1511 if (error != NULL) {
1512 BT_ERR("Getting property failed: [%s]\n", error->message);
1513 g_error_free(error);
1514 g_object_unref(device_proxy);
1515 return BLUETOOTH_ERROR_INTERNAL;
1518 g_variant_get(result, "(v)", &temp);
1519 previous_value = g_variant_get_boolean(temp);
1520 g_variant_unref(temp);
1521 g_variant_unref(result);
1522 /* If the input is same with previous value, return error. */
1523 if (previous_value == authorize) {
1524 BT_ERR("Same value: %d", previous_value);
1525 g_object_unref(device_proxy);
1526 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1530 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1531 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1532 G_DBUS_CALL_FLAGS_NONE,
1537 g_object_unref(device_proxy);
1539 BT_ERR("SetProperty error: [%s]", error->message);
1540 g_error_free(error);
1541 ret = BLUETOOTH_ERROR_INTERNAL;
1545 g_variant_unref(result);
1550 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1551 gboolean *is_connected)
1553 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1554 char *object_path = NULL;
1556 GDBusProxy *device_proxy;
1557 GError *error = NULL;
1559 GDBusConnection *conn;
1560 GVariant *result = NULL;
1561 int ret = BLUETOOTH_ERROR_NONE;
1563 BT_CHECK_PARAMETER(device_address, return);
1565 conn = _bt_gdbus_get_system_gconn();
1566 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1568 _bt_convert_addr_type_to_string(address, device_address->addr);
1570 object_path = _bt_get_device_object_path(address);
1571 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1573 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1574 NULL, BT_BLUEZ_NAME, object_path,
1575 BT_PROPERTIES_INTERFACE, NULL, NULL);
1576 g_free(object_path);
1577 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1579 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1580 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1582 G_DBUS_CALL_FLAGS_NONE,
1586 if (result == NULL) {
1587 if (error != NULL) {
1588 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1589 g_error_free(error);
1591 g_object_unref(device_proxy);
1592 return BLUETOOTH_ERROR_INTERNAL;
1595 g_variant_get(result, "(v)", &value);
1596 *is_connected = g_variant_get_boolean(value);
1597 g_variant_unref(result);
1599 BT_DBG("gatt is connected : %d", *is_connected);
1601 g_variant_unref(value);
1602 g_object_unref(device_proxy);
1607 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1608 int connection_type, gboolean *is_connected)
1610 char *object_path = NULL;
1611 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1612 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1613 GDBusProxy *device_proxy = NULL;
1614 GDBusProxy *adapter_proxy = NULL;
1615 GDBusConnection *conn;
1616 GError *error = NULL;
1617 GVariant *tmp_value = NULL;
1618 GVariant *value = NULL;
1619 GVariant *result = NULL;
1622 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1623 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1625 _bt_convert_addr_type_to_string(address, device_address->addr);
1626 _bt_convert_addr_string_to_secure_string(secure_address, address);
1628 *is_connected = FALSE;
1629 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1631 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1632 return _bt_rfcomm_is_device_connected(device_address,
1634 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1635 return _bt_is_gatt_connected(device_address, is_connected);
1636 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1637 return _bt_pbap_is_connected(device_address, is_connected);
1639 adapter_proxy = _bt_get_adapter_proxy();
1640 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1642 conn = _bt_gdbus_get_system_gconn();
1643 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1645 _bt_convert_addr_type_to_string(address, device_address->addr);
1647 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1648 object_path = _bt_get_adapter_path();
1649 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1650 NULL, BT_BLUEZ_NAME,
1651 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1652 g_free(object_path);
1653 if (device_proxy == NULL) {
1654 BT_DBG("Device don't have this service");
1655 return BLUETOOTH_ERROR_INTERNAL;
1658 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1659 g_variant_new("(s)", address),
1660 G_DBUS_CALL_FLAGS_NONE,
1665 if (result == NULL) {
1666 BT_ERR("[GetProperties] Error occured in Proxy call");
1668 BT_ERR("%s", error->message);
1669 g_error_free(error);
1671 *is_connected = FALSE;
1672 g_object_unref(device_proxy);
1673 return BLUETOOTH_ERROR_NONE;
1675 g_variant_get(result , "(@a{sv})", &value);
1676 g_variant_unref(result);
1679 tmp_value = g_variant_lookup_value(value,
1681 G_VARIANT_TYPE_BOOLEAN);
1683 *is_connected = g_variant_get_boolean(tmp_value);
1684 g_variant_unref(tmp_value);
1686 g_variant_unref(value);
1688 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1689 return _bt_is_network_connected(_bt_get_net_conn(),
1690 device_address->addr, is_connected);
1692 uuid = _bt_get_profile_uuid128(connection_type);
1694 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1695 return BLUETOOTH_ERROR_INTERNAL;
1697 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1699 object_path = _bt_get_device_object_path(address);
1700 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1701 BT_DBG("object_path: %s", object_path);
1702 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1703 NULL, BT_BLUEZ_NAME,
1704 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1705 g_free(object_path);
1706 if (device_proxy == NULL) {
1707 BT_DBG("Device don't have this service");
1709 return BLUETOOTH_ERROR_INTERNAL;
1712 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1713 g_variant_new("(s)", uuid),
1714 G_DBUS_CALL_FLAGS_NONE,
1719 if (result == NULL) {
1720 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1722 BT_ERR("%s", error->message);
1723 if (g_strrstr(error->message, "Not Connected"))
1724 BT_DBG("Not connected");
1725 g_error_free(error);
1727 *is_connected = FALSE;
1728 g_object_unref(device_proxy);
1730 return BLUETOOTH_ERROR_NONE;
1732 g_variant_get(result, "(b)", is_connected);
1734 g_variant_unref(result);
1737 g_object_unref(device_proxy);
1738 return BLUETOOTH_ERROR_NONE;
1741 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1742 bluetooth_connected_link_t *connected)
1744 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1745 char *object_path = NULL;
1747 GDBusProxy *device_proxy;
1748 GError *error = NULL;
1749 GDBusConnection *conn;
1750 GVariant *value = NULL;
1751 GVariant *result = NULL;
1753 BT_CHECK_PARAMETER(device_address, return);
1755 conn = _bt_gdbus_get_system_gconn();
1756 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1758 _bt_convert_addr_type_to_string(address, device_address->addr);
1760 object_path = _bt_get_device_object_path(address);
1761 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1763 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1764 NULL, BT_BLUEZ_NAME,
1765 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1766 g_free(object_path);
1767 if (device_proxy == NULL) {
1768 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1769 return BLUETOOTH_ERROR_NONE;
1772 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1773 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1774 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1776 if (error != NULL) {
1777 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1778 g_error_free(error);
1779 g_object_unref(device_proxy);
1780 return BLUETOOTH_ERROR_INTERNAL;
1782 g_variant_get(result, "(v)", &value);
1783 *connected = g_variant_get_byte(value);
1784 g_variant_unref(value);
1785 g_variant_unref(result);
1788 g_object_unref(device_proxy);
1789 return BLUETOOTH_ERROR_NONE;
1792 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1796 GVariant *out_param1;
1798 int result = BLUETOOTH_ERROR_NONE;
1799 bt_function_data_t *func_data = user_data;
1800 request_info_t *req_info = NULL;
1801 bluetooth_device_address_t device_addr = { {0} };
1803 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1804 g_object_unref(proxy);
1806 if (reply == NULL) {
1807 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1809 BT_ERR("Error: %s", err->message);
1810 g_clear_error(&err);
1812 result = BLUETOOTH_ERROR_INTERNAL;
1814 g_variant_unref(reply);
1817 if (func_data == NULL) {
1818 BT_ERR("func_data is NULL");
1822 req_info = _bt_get_request_info(func_data->req_id);
1823 if (req_info == NULL) {
1824 BT_ERR("req_info is NULL");
1828 if (req_info->context == NULL) {
1829 BT_ERR("req_info->context is NULL");
1833 _bt_convert_addr_string_to_type(device_addr.addr,
1834 (const char *)func_data->address);
1836 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1837 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1840 g_dbus_method_invocation_return_value(req_info->context,
1841 g_variant_new("(iv)", result, out_param1));
1845 _bt_delete_request_list(req_info->req_id);
1848 g_free(func_data->address);
1853 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1854 gboolean auto_connect)
1856 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1857 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1858 gchar *device_path = NULL;
1859 GDBusProxy *device_proxy = NULL;
1860 GDBusConnection *conn;
1861 int ret = BLUETOOTH_ERROR_NONE;
1862 bt_function_data_t *func_data;
1864 BT_CHECK_PARAMETER(bd_addr, return);
1866 conn = _bt_gdbus_get_system_gconn();
1867 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1869 _bt_convert_addr_type_to_string(device_address,
1870 (unsigned char *)bd_addr->addr);
1871 device_path = _bt_get_device_object_path(device_address);
1872 if (device_path == NULL) {
1873 BT_ERR_C("device_path NULL : [%s]", device_address);
1874 ret = BLUETOOTH_ERROR_INTERNAL;
1878 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1879 NULL, BT_BLUEZ_NAME,
1880 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1881 g_free(device_path);
1882 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1884 func_data = g_malloc0(sizeof(bt_function_data_t));
1885 func_data->address = g_strdup(device_address);
1886 if (func_data->address == NULL) {
1887 BT_ERR("Unable to allocate memory for address");
1888 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1892 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1893 BT_INFO("Connect LE [%s]", secure_address);
1895 func_data->req_id = req_id;
1897 g_dbus_proxy_call(device_proxy, "ConnectLE",
1898 g_variant_new("(b)", auto_connect),
1899 G_DBUS_CALL_FLAGS_NONE,
1900 BT_MAX_DBUS_TIMEOUT,
1902 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1908 g_object_unref(device_proxy);
1910 g_free(func_data->address);
1916 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1918 BT_INFO("Try to initiate pending LE connection");
1920 pending_le_conn_timer_id = 0;
1922 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1923 &pending_le_conn_info->bd_addr,
1924 pending_le_conn_info->auto_connect);
1926 g_free(pending_le_conn_info);
1927 pending_le_conn_info = NULL;
1932 void _bt_pending_connect_le_device(void)
1934 if (pending_le_conn_timer_id > 0) {
1935 g_source_remove(pending_le_conn_timer_id);
1936 __bt_connect_le_timer_cb(NULL);
1940 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1941 gboolean auto_connect)
1943 int ret = BLUETOOTH_ERROR_NONE;
1945 BT_CHECK_PARAMETER(bd_addr, return);
1947 ret = _bt_hold_current_advertising();
1948 if (ret == BLUETOOTH_ERROR_NONE) {
1949 BT_INFO("Current advertising is held");
1950 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1951 pending_le_conn_info->req_id = req_id;
1952 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1953 BLUETOOTH_ADDRESS_LENGTH);
1954 pending_le_conn_info->auto_connect = auto_connect;
1956 pending_le_conn_timer_id =
1957 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1959 return BLUETOOTH_ERROR_NONE;
1960 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
1961 BT_ERR("Unable to hold advertising");
1964 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1967 int _bt_disconnect_le_device(int req_id,
1968 const bluetooth_device_address_t *bd_addr)
1970 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1971 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1973 GDBusProxy *device_proxy;
1974 GDBusConnection *conn;
1975 int ret = BLUETOOTH_ERROR_NONE;
1976 bt_function_data_t *func_data;
1978 BT_CHECK_PARAMETER(bd_addr, return);
1980 conn = _bt_gdbus_get_system_gconn();
1981 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1983 _bt_convert_addr_type_to_string(device_address,
1984 (unsigned char *)bd_addr->addr);
1985 device_path = _bt_get_device_object_path(device_address);
1986 if (device_path == NULL) {
1987 BT_DBG("device_path NULL");
1988 ret = BLUETOOTH_ERROR_INTERNAL;
1992 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1994 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1995 NULL, BT_BLUEZ_NAME,
1996 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1997 g_free(device_path);
1998 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2000 func_data = g_malloc0(sizeof(bt_function_data_t));
2001 func_data->address = g_strdup(device_address);
2002 if (func_data->address == NULL) {
2003 BT_ERR("Unable to allocate memory for address");
2004 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2008 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2009 BT_INFO("Disconnect LE [%s]", secure_address);
2011 func_data->req_id = req_id;
2013 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2015 G_DBUS_CALL_FLAGS_NONE,
2016 BT_MAX_DBUS_TIMEOUT,
2018 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2023 g_object_unref(device_proxy);
2025 g_free(func_data->address);
2031 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2033 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2034 gchar *device_path = NULL;
2035 GError *error = NULL;
2036 GDBusProxy *device_proxy = NULL;
2037 GDBusProxy *adapter_proxy;
2038 GDBusConnection *conn;
2039 int ret = BLUETOOTH_ERROR_NONE;
2041 BT_CHECK_PARAMETER(bd_addr, return);
2043 _bt_convert_addr_type_to_string(device_address,
2044 (unsigned char *)bd_addr->addr);
2046 conn = _bt_gdbus_get_system_gconn();
2047 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2049 adapter_proxy = _bt_get_adapter_proxy();
2050 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2052 device_path = _bt_get_device_object_path(device_address);
2053 if (device_path == NULL) {
2054 BT_DBG("device_path NULL");
2055 ret = BLUETOOTH_ERROR_INTERNAL;
2059 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2061 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2062 NULL, BT_BLUEZ_NAME,
2063 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2064 g_free(device_path);
2065 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2067 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2069 G_DBUS_CALL_FLAGS_NONE,
2074 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2075 g_error_free(error);
2076 g_object_unref(device_proxy);
2077 return BLUETOOTH_ERROR_INTERNAL;
2080 g_object_unref(device_proxy);
2085 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2087 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2088 gchar *device_path = NULL;
2089 GError *error = NULL;
2090 GDBusProxy *device_proxy = NULL;
2091 GDBusProxy *adapter_proxy;
2092 GDBusConnection *conn;
2093 int ret = BLUETOOTH_ERROR_NONE;
2095 BT_CHECK_PARAMETER(bd_addr, return);
2097 _bt_convert_addr_type_to_string(device_address,
2098 (unsigned char *)bd_addr->addr);
2100 conn = _bt_gdbus_get_system_gconn();
2101 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2103 adapter_proxy = _bt_get_adapter_proxy();
2104 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2106 device_path = _bt_get_device_object_path(device_address);
2107 if (device_path == NULL) {
2108 BT_DBG("device_path NULL");
2109 ret = BLUETOOTH_ERROR_INTERNAL;
2113 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2115 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2116 NULL, BT_BLUEZ_NAME,
2117 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2118 g_free(device_path);
2119 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2121 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2123 G_DBUS_CALL_FLAGS_NONE,
2128 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2129 g_error_free(error);
2130 g_object_unref(device_proxy);
2131 return BLUETOOTH_ERROR_INTERNAL;
2134 g_object_unref(device_proxy);
2139 int _bt_connect_profile(char *address, char *uuid,
2140 void *cb, gpointer func_data)
2144 GDBusConnection *conn;
2145 GDBusProxy *adapter_proxy;
2146 GVariant *result = NULL;
2147 GError *error = NULL;
2149 conn = _bt_gdbus_get_system_gconn();
2150 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2152 object_path = _bt_get_device_object_path(address);
2153 if (object_path == NULL) {
2154 BT_ERR("No searched device");
2156 adapter_proxy = _bt_get_adapter_proxy();
2157 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2159 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2160 g_variant_new("(s)", address),
2161 G_DBUS_CALL_FLAGS_NONE,
2166 if (error != NULL) {
2167 BT_ERR("CreateDevice Fail: %s", error->message);
2168 g_error_free(error);
2171 g_variant_unref(result);
2173 object_path = _bt_get_device_object_path(address);
2175 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2177 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2178 NULL, BT_BLUEZ_NAME,
2179 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2180 g_free(object_path);
2181 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2183 g_dbus_proxy_call(proxy, "ConnectProfile",
2184 g_variant_new("(s)", uuid),
2185 G_DBUS_CALL_FLAGS_NONE,
2186 BT_MAX_DBUS_TIMEOUT,
2188 (GAsyncReadyCallback)cb,
2191 return BLUETOOTH_ERROR_NONE;
2194 int _bt_disconnect_all(char *address)
2196 int ret = BLUETOOTH_ERROR_NONE;
2199 GDBusConnection *conn;
2200 GVariant *result = NULL;
2204 conn = _bt_gdbus_get_system_gconn();
2205 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2207 object_path = _bt_get_device_object_path(address);
2208 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2210 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2211 NULL, BT_BLUEZ_NAME,
2212 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2213 g_free(object_path);
2214 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2216 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2218 G_DBUS_CALL_FLAGS_NONE,
2223 BT_ERR("Dbus Call Error:[%s]", err->message);
2225 ret = BLUETOOTH_ERROR_INTERNAL;
2228 g_object_unref(proxy);
2230 g_variant_unref(result);
2235 int _bt_disconnect_profile(char *address, char *uuid,
2236 void *cb, gpointer func_data)
2240 GDBusConnection *conn;
2242 conn = _bt_gdbus_get_system_gconn();
2243 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2245 object_path = _bt_get_device_object_path(address);
2246 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2248 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2249 NULL, BT_BLUEZ_NAME,
2250 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2251 g_free(object_path);
2252 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2254 g_dbus_proxy_call(proxy, "DisconnectProfile",
2255 g_variant_new("(s)", uuid),
2256 G_DBUS_CALL_FLAGS_NONE,
2257 BT_MAX_DBUS_TIMEOUT,
2259 (GAsyncReadyCallback)cb,
2262 return BLUETOOTH_ERROR_NONE;
2265 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2266 int low_threshold, int in_range_threshold, int high_threshold)
2268 int ret = BLUETOOTH_ERROR_NONE;
2270 GVariant *result = NULL;
2271 GError *error = NULL;
2272 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2274 BT_CHECK_PARAMETER(bd_addr, return);
2275 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2276 bd_addr->addr[0], bd_addr->addr[1],
2277 bd_addr->addr[2], bd_addr->addr[3],
2278 bd_addr->addr[4], bd_addr->addr[5],
2280 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2281 in_range_threshold, high_threshold);
2283 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2285 proxy = _bt_get_adapter_proxy();
2286 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2288 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2289 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2290 G_DBUS_CALL_FLAGS_NONE,
2294 if (error != NULL) {
2295 BT_ERR("Dbus Call Error:[%s]", error->message);
2296 g_error_free(error);
2297 ret = BLUETOOTH_ERROR_INTERNAL;
2301 g_variant_unref(result);
2306 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2309 int ret = BLUETOOTH_ERROR_NONE;
2311 GVariant *result = NULL;
2312 GError *error = NULL;
2313 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2315 BT_CHECK_PARAMETER(bd_addr, return);
2316 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2317 bd_addr->addr[0], bd_addr->addr[1],
2318 bd_addr->addr[2], bd_addr->addr[3],
2319 bd_addr->addr[4], bd_addr->addr[5],
2322 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2324 proxy = _bt_get_adapter_proxy();
2325 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2327 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2328 g_variant_new("(si)", address, link_type),
2329 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 static int __bt_le_set_conn_parameter(const char *address,
2347 float interval_min, float interval_max,
2348 guint16 latency, guint16 time_out)
2350 gchar *device_path = NULL;
2351 GError *error = NULL;
2352 GDBusProxy *device_proxy = NULL;
2353 GDBusConnection *conn;
2355 guint32 min, max, to;
2356 int ret = BLUETOOTH_ERROR_NONE;
2358 BT_CHECK_PARAMETER(address, return);
2360 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2361 interval_min, interval_max, latency, time_out);
2363 device_path = _bt_get_device_object_path((char *)address);
2365 if (device_path == NULL) {
2366 BT_ERR("device_path NULL");
2367 ret = BLUETOOTH_ERROR_INTERNAL;
2371 conn = _bt_gdbus_get_system_gconn();
2373 BT_ERR("conn NULL");
2374 ret = BLUETOOTH_ERROR_INTERNAL;
2378 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2379 NULL, BT_BLUEZ_NAME,
2380 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2382 g_free(device_path);
2383 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2385 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2386 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2387 to = time_out / BT_LE_CONN_TO_SPLIT;
2389 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2390 g_variant_new("(uuuu)", min, max, latency, to),
2391 G_DBUS_CALL_FLAGS_NONE,
2396 g_object_unref(device_proxy);
2397 if (reply == NULL) {
2399 BT_ERR("Error %s[%s]", error->message, address);
2400 if (g_strrstr(error->message, "In Progress"))
2401 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2403 ret = BLUETOOTH_ERROR_INTERNAL;
2404 g_error_free(error);
2408 g_variant_unref(reply);
2414 static void __bt_le_conn_param_free(void *data)
2416 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2418 BT_DBG("%s", param->sender);
2419 g_free(param->sender);
2423 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2425 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2426 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2428 return parama->key > paramb->key;
2431 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2434 bt_connected_le_dev_t *dev;
2439 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2442 if (g_strcmp0(dev->address, address) == 0)
2448 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2451 bt_le_conn_param_t *param = NULL;
2453 if (!dev || !sender)
2456 for (l = dev->senders; l; l = g_slist_next(l)) {
2458 if (g_strcmp0(param->sender, sender) == 0)
2465 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2466 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2468 bt_connected_le_dev_t *dev = NULL;
2469 bt_le_conn_param_t *param = NULL;
2470 bt_le_conn_param_t *data = NULL;
2472 if (!address || !sender)
2473 return BLUETOOTH_ERROR_INVALID_PARAM;
2475 dev = __bt_get_le_connected_dev_info(address);
2477 return BLUETOOTH_ERROR_INTERNAL;
2479 param = __bt_get_le_conn_param_info(dev, sender);
2481 data = g_malloc0(sizeof(bt_le_conn_param_t));
2482 data->sender = g_strdup(sender);
2483 data->interval_min = interval_min;
2484 data->interval_max = interval_max;
2485 data->latency = latency;
2486 data->time_out = time_out;
2487 data->key = interval_min + (interval_max - interval_min)/2;
2489 if (param == NULL) {
2490 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2491 dev->senders = g_slist_append(dev->senders, data);
2493 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2494 dev->senders = g_slist_remove(dev->senders, param);
2495 g_free(param->sender);
2497 dev->senders = g_slist_append(dev->senders, data);
2500 /* Sorting. First element have the minimum interval */
2501 dev->senders = g_slist_sort(dev->senders,
2502 (GCompareFunc)__bt_compare_le_conn_param_key);
2504 return BLUETOOTH_ERROR_NONE;
2507 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2509 bt_connected_le_dev_t *dev = NULL;
2510 bt_le_conn_param_t *param = 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 BT_DBG("Remove param %s %s ", address, sender);
2522 dev->senders = g_slist_remove(dev->senders, param);
2523 g_free(param->sender);
2527 *is_removed = FALSE;
2529 return BLUETOOTH_ERROR_NONE;
2532 int _bt_remove_all_le_conn_param_info(const char *sender)
2535 bt_connected_le_dev_t *dev = NULL;
2536 bt_le_conn_param_t *param = NULL;
2537 gboolean is_removed = FALSE;
2538 char *sender_new = NULL;
2539 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2540 int ret = BLUETOOTH_ERROR_NONE;
2543 return BLUETOOTH_ERROR_INVALID_PARAM;
2545 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2547 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2550 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2551 g_slist_length(dev->senders));
2552 if (g_slist_length(dev->senders) > 0) {
2553 param = dev->senders->data;
2554 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2555 param->interval_min, param->interval_max);
2557 if (dev->interval_min != param->interval_min ||
2558 dev->interval_max != param->interval_max) {
2559 sender_new = g_strdup(param->sender);
2561 _bt_convert_addr_string_to_type(addr, dev->address);
2562 ret = _bt_le_conn_update(sender_new, addr,
2563 param->interval_min, param->interval_max,
2564 param->latency, param->time_out);
2567 if (ret != BLUETOOTH_ERROR_NONE)
2568 BT_ERR("Unable to set le connection parameter");
2571 BT_INFO("Set the default interval");
2573 bluetooth_le_connection_param_t param = { 0 };
2574 _bt_get_le_connection_parameter(
2575 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2578 ret = __bt_le_set_conn_parameter(dev->address,
2579 param.interval_min, param.interval_max,
2580 param.latency, param.timeout);
2581 if (ret == BLUETOOTH_ERROR_NONE) {
2582 dev->interval_min = param.interval_min;
2583 dev->interval_max = param.interval_max;
2592 void _bt_add_le_connected_dev_info(const char *address)
2594 bt_connected_le_dev_t *dev = NULL;
2599 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2600 dev->address = g_strdup(address);
2602 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2607 void _bt_remove_le_connected_dev_info(const char *address)
2609 bt_connected_le_dev_t *dev = NULL;
2614 dev = __bt_get_le_connected_dev_info(address);
2618 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2619 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2620 g_free(dev->address);
2626 int _bt_le_conn_update(const char *sender,
2627 unsigned char *device_address,
2628 float interval_min, float interval_max,
2629 guint16 latency, guint16 time_out)
2631 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2632 guint32 min_supervision_to;
2633 bt_connected_le_dev_t *dev = NULL;
2634 bt_le_conn_param_t *param = NULL;
2635 gboolean is_removed = FALSE;
2636 int ret = BLUETOOTH_ERROR_NONE;
2638 BT_CHECK_PARAMETER(device_address, return);
2640 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2641 sender, interval_min, interval_max, latency, time_out);
2643 if (interval_min > interval_max ||
2644 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2645 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2646 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2650 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2651 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2652 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2656 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2657 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2662 * The Supervision_Timeout in milliseconds shall be larger than
2663 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2664 * where Conn_Interval_Max is given in milliseconds.
2666 min_supervision_to = (1 + latency) * interval_max * 2;
2667 if (time_out <= min_supervision_to) {
2668 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2672 _bt_convert_addr_type_to_string(address, device_address);
2673 BT_DBG("Remote device address: %s", address);
2675 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2677 dev = __bt_get_le_connected_dev_info(address);
2679 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2683 if (g_slist_length(dev->senders) == 1)
2686 param = dev->senders->data;
2688 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2690 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2691 BT_DBG("Skip due to same interval");
2695 interval_min = param->interval_min;
2696 interval_max = param->interval_max;
2700 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2702 if (ret != BLUETOOTH_ERROR_NONE) {
2703 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2707 dev->interval_min = interval_min;
2708 dev->interval_max = interval_max;
2714 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2715 bluetooth_device_pin_code_t *pin_code)
2717 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2719 bt_pin_code_info_t *pin_info = NULL;
2721 BT_CHECK_PARAMETER(device_address, return);
2722 BT_CHECK_PARAMETER(pin_code, return);
2723 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2724 BLUETOOTH_ERROR_NO_RESOURCES);
2726 _bt_convert_addr_type_to_string(address, device_address->addr);
2728 for (l = pin_info_list; l != NULL; l = l->next) {
2731 if (g_strcmp0(pin_info->address, address) == 0) {
2732 g_free(pin_info->pin_code);
2733 pin_info->pin_code = g_strdup(pin_code->pin_code);
2734 return BLUETOOTH_ERROR_NONE;
2738 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2739 pin_info->address = g_strdup(address);
2740 pin_info->pin_code = g_strdup(pin_code->pin_code);
2741 pin_info_list = g_slist_append(pin_info_list, pin_info);
2743 return BLUETOOTH_ERROR_NONE;
2746 gint __bt_compare_address(gpointer *a, gpointer *b)
2748 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2749 char *address = (char *)b;
2750 return g_strcmp0(pin_info->address, address);
2753 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2755 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2757 bt_pin_code_info_t *pin_info = NULL;
2759 BT_CHECK_PARAMETER(device_address, return);
2761 _bt_convert_addr_type_to_string(address, device_address->addr);
2763 l = g_slist_find_custom(pin_info_list, address,
2764 (GCompareFunc)__bt_compare_address);
2768 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2769 g_free(pin_info->address);
2770 g_free(pin_info->pin_code);
2774 return BLUETOOTH_ERROR_NONE;
2777 int _bt_get_device_pin_code(const char *address, char *pin_code)
2781 BT_CHECK_PARAMETER(address, return);
2782 BT_CHECK_PARAMETER(pin_code, return);
2784 for (l = pin_info_list; l != NULL; l = l->next) {
2785 bt_pin_code_info_t *pin_info = l->data;
2787 if (g_strcmp0(pin_info->address, address) == 0) {
2788 g_strlcpy(pin_code, pin_info->pin_code,
2789 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2791 return BLUETOOTH_ERROR_NONE;
2795 return BLUETOOTH_ERROR_NOT_FOUND;
2798 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2799 bluetooth_le_connection_param_t *param)
2802 return BLUETOOTH_ERROR_INVALID_PARAM;
2804 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2805 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2806 return BLUETOOTH_ERROR_INVALID_PARAM;
2808 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2811 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2812 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2813 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2814 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2815 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2818 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2819 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2820 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2821 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2822 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2825 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2826 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2827 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2828 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2829 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2833 BT_ERR("Unhandled mode : %d", mode);
2837 return BLUETOOTH_ERROR_NONE;
2840 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2841 guint trusted_profile_flag, guint *trusted)
2847 case TRUSTED_PROFILE_PBAP:
2848 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2849 trust_profile = trusted_profile_flag & (1 << 1);
2851 return BLUETOOTH_ERROR_NOT_SUPPORT;
2853 case TRUSTED_PROFILE_MAP:
2854 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2855 trust_profile = trusted_profile_flag & (1 << 3);
2857 return BLUETOOTH_ERROR_NOT_SUPPORT;
2859 case TRUSTED_PROFILE_SAP:
2860 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2861 trust_profile = trusted_profile_flag & (1 << 5);
2863 return BLUETOOTH_ERROR_NOT_SUPPORT;
2865 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2866 *trusted = trusted_profile_flag;
2867 return BLUETOOTH_ERROR_NONE;
2869 return BLUETOOTH_ERROR_NOT_SUPPORT;
2875 return BLUETOOTH_ERROR_NONE;
2878 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2879 guint restricted_profile_flag, guint *restricted)
2881 int restrict_profile;
2882 *restricted = FALSE;
2885 case RESTRICTED_PROFILE_HFP_HS:
2886 restrict_profile = restricted_profile_flag & (1 << 0);
2888 case RESTRICTED_PROFILE_A2DP:
2889 restrict_profile = restricted_profile_flag & (1 << 2);
2892 return BLUETOOTH_ERROR_NOT_SUPPORT;
2895 if (restrict_profile)
2898 return BLUETOOTH_ERROR_NONE;
2901 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2904 case TRUSTED_PROFILE_PBAP:
2905 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2906 case TRUSTED_PROFILE_MAP:
2907 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2908 case TRUSTED_PROFILE_SAP:
2909 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2910 case TRUSTED_PROFILE_ALL:
2917 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2920 case RESTRICTED_PROFILE_HFP_HS:
2921 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2922 case RESTRICTED_PROFILE_A2DP:
2923 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2929 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2931 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2932 return TRUSTED_PROFILE_PBAP;
2933 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2934 return TRUSTED_PROFILE_MAP;
2935 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2936 return TRUSTED_PROFILE_SAP;
2938 return 0; /* 0 - Unknown Profile */
2941 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2942 bluetooth_trusted_profile_t profile, gboolean trust)
2944 int ret = BLUETOOTH_ERROR_NONE;
2945 GDBusConnection *conn;
2947 GError *error = NULL;
2948 char *device_path = NULL;
2950 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2953 BT_CHECK_PARAMETER(bd_addr, return);
2954 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2955 bd_addr->addr[0], bd_addr->addr[1],
2956 bd_addr->addr[2], bd_addr->addr[3],
2957 bd_addr->addr[4], bd_addr->addr[5],
2960 conn = _bt_gdbus_get_system_gconn();
2961 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2963 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2965 device_path = _bt_get_device_object_path(address);
2966 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2968 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2969 NULL, BT_BLUEZ_NAME, device_path,
2970 BT_DEVICE_INTERFACE, NULL, NULL);
2972 g_free(device_path);
2973 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2975 uuid = _bt_get_trusted_profile_uuid(profile);
2977 g_object_unref(proxy);
2978 return BLUETOOTH_ERROR_NOT_SUPPORT;
2981 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2982 g_variant_new("(sb)", uuid, trust),
2983 G_DBUS_CALL_FLAGS_NONE, -1,
2985 g_object_unref(proxy);
2987 if (reply == NULL) {
2988 BT_ERR("Failed to Set Profile Trusted");
2989 ret = BLUETOOTH_ERROR_INTERNAL;
2991 BT_ERR("Error %s[%s]", error->message, address);
2992 g_error_free(error);
2996 g_variant_unref(reply);
3003 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3004 bluetooth_trusted_profile_t profile, guint *trust)
3006 int ret = BLUETOOTH_ERROR_NONE;
3007 GDBusConnection *conn;
3009 GError *error = NULL;
3010 char *device_path = NULL;
3011 guint trusted_profile_flag;
3012 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3015 BT_CHECK_PARAMETER(bd_addr, return);
3016 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3017 bd_addr->addr[0], bd_addr->addr[1],
3018 bd_addr->addr[2], bd_addr->addr[3],
3019 bd_addr->addr[4], bd_addr->addr[5],
3022 conn = _bt_gdbus_get_system_gconn();
3023 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3025 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3027 device_path = _bt_get_device_object_path(address);
3028 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3030 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3031 NULL, BT_BLUEZ_NAME, device_path,
3032 BT_PROPERTIES_INTERFACE, NULL, NULL);
3034 g_free(device_path);
3035 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3037 reply = g_dbus_proxy_call_sync(proxy, "Get",
3038 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3039 G_DBUS_CALL_FLAGS_NONE, -1,
3041 g_object_unref(proxy);
3043 if (reply == NULL) {
3044 BT_ERR("Failed to Get Profile Trusted");
3045 ret = BLUETOOTH_ERROR_INTERNAL;
3047 BT_ERR("Error %s[%s]", error->message, address);
3048 g_error_free(error);
3053 g_variant_get(reply, "(v)", &temp);
3054 trusted_profile_flag = g_variant_get_uint32(temp);
3055 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3057 ret = _bt_get_trusted_profile_from_flag(profile,
3058 trusted_profile_flag, trust);
3059 g_variant_unref(temp);
3060 g_variant_unref(reply);
3063 BT_DBG("TRUST %d", *trust);
3067 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3068 bluetooth_restricted_profile_t profile, gboolean restricted)
3070 int ret = BLUETOOTH_ERROR_NONE;
3071 GDBusConnection *conn;
3073 GError *error = NULL;
3074 char *device_path = NULL;
3076 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3079 BT_CHECK_PARAMETER(bd_addr, return);
3080 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3081 bd_addr->addr[0], bd_addr->addr[1],
3082 bd_addr->addr[2], bd_addr->addr[3],
3083 bd_addr->addr[4], bd_addr->addr[5],
3084 profile, restricted);
3086 conn = _bt_gdbus_get_system_gconn();
3087 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3089 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3091 device_path = _bt_get_device_object_path(address);
3092 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3094 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3095 NULL, BT_BLUEZ_NAME, device_path,
3096 BT_DEVICE_INTERFACE, NULL, NULL);
3098 g_free(device_path);
3099 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3101 uuid = _bt_get_restricted_profile_uuid(profile);
3103 g_object_unref(proxy);
3104 return BLUETOOTH_ERROR_NOT_SUPPORT;
3107 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3108 g_variant_new("(sb)", uuid, restricted),
3109 G_DBUS_CALL_FLAGS_NONE, -1,
3111 g_object_unref(proxy);
3113 if (reply == NULL) {
3114 BT_ERR("Failed to Set Profile Restricted");
3115 ret = BLUETOOTH_ERROR_INTERNAL;
3117 BT_ERR("Error %s[%s]", error->message, address);
3118 g_error_free(error);
3122 g_variant_unref(reply);
3129 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3130 bluetooth_restricted_profile_t profile, guint *restricted)
3132 int ret = BLUETOOTH_ERROR_NONE;
3133 GDBusConnection *conn;
3135 GError *error = NULL;
3136 char *device_path = NULL;
3137 guint restricted_profile_flag;
3138 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3141 BT_CHECK_PARAMETER(bd_addr, return);
3142 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3143 bd_addr->addr[0], bd_addr->addr[1],
3144 bd_addr->addr[2], bd_addr->addr[3],
3145 bd_addr->addr[4], bd_addr->addr[5],
3146 profile, *restricted);
3148 conn = _bt_gdbus_get_system_gconn();
3149 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3151 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3153 device_path = _bt_get_device_object_path(address);
3154 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3156 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3157 NULL, BT_BLUEZ_NAME, device_path,
3158 BT_PROPERTIES_INTERFACE, NULL, NULL);
3160 g_free(device_path);
3161 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3163 reply = g_dbus_proxy_call_sync(proxy, "Get",
3164 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3165 G_DBUS_CALL_FLAGS_NONE, -1,
3167 g_object_unref(proxy);
3169 if (reply == NULL) {
3170 BT_ERR("Failed to Get Profile Restricted");
3171 ret = BLUETOOTH_ERROR_INTERNAL;
3173 BT_ERR("Error %s[%s]", error->message, address);
3174 g_error_free(error);
3179 g_variant_get(reply, "(v)", &temp);
3180 restricted_profile_flag = g_variant_get_uint32(temp);
3181 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3183 ret = _bt_get_restricted_profile_from_flag(profile,
3184 restricted_profile_flag, restricted);
3185 g_variant_unref(temp);
3186 g_variant_unref(reply);
3189 BT_DBG("TRUST %d", *restricted);
3193 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3197 GVariant *out_param1;
3198 request_info_t *req_info;
3199 GVariant *val = NULL;
3200 GVariant *param = NULL;
3203 bluetooth_device_address_t device_addr = { {0} };
3204 int result = BLUETOOTH_ERROR_NONE;
3207 val = g_dbus_proxy_call_finish(proxy, res, &err);
3209 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3210 if (req_info == NULL) {
3211 BT_ERR("req_info == NULL");
3212 g_object_unref(proxy);
3213 att_mtu_req_info->device_proxy = NULL;
3218 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3220 if (g_strrstr(err->message, "NotSupported")) {
3221 BT_INFO("Connection Not Supported");
3222 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3223 } else if (g_strrstr(err->message, "NotConnected")) {
3224 BT_INFO("Not connected");
3225 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3226 } else if (g_strrstr(err->message, "InvalidArguments")) {
3227 BT_INFO("Not connected");
3228 result = BLUETOOTH_ERROR_INVALID_PARAM;
3230 BT_DBG("Default case");
3231 result = BLUETOOTH_ERROR_INTERNAL;
3235 g_object_unref(proxy);
3236 att_mtu_req_info->device_proxy = NULL;
3238 if (result != BLUETOOTH_ERROR_NONE)
3242 g_variant_get(val, "(qy)", &mtu, &status);
3243 g_variant_unref(val);
3246 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3248 param = g_variant_new("(isqy)",
3250 att_mtu_req_info->addr,
3254 /* Send the event to application */
3255 _bt_send_event(BT_DEVICE_EVENT,
3256 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3260 if (req_info->context == NULL)
3263 _bt_convert_addr_string_to_type(device_addr.addr,
3264 (const char *)att_mtu_req_info->addr);
3266 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3267 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3268 g_dbus_method_invocation_return_value(req_info->context,
3269 g_variant_new("(iv)", result, out_param1));
3271 _bt_delete_request_list(req_info->req_id);
3276 g_free(att_mtu_req_info->addr);
3277 g_free(att_mtu_req_info);
3278 att_mtu_req_info = NULL;
3283 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3286 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3287 gchar *device_path = NULL;
3288 GDBusProxy *adapter_proxy;
3289 GDBusProxy *device_proxy;
3290 GDBusConnection *conn;
3291 int ret = BLUETOOTH_ERROR_NONE;
3293 BT_CHECK_PARAMETER(device_address, return);
3295 if (att_mtu_req_info) {
3296 BT_ERR("ATT MTU request in progress");
3297 return BLUETOOTH_ERROR_DEVICE_BUSY;
3300 conn = _bt_gdbus_get_system_gconn();
3301 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3303 adapter_proxy = _bt_get_adapter_proxy();
3304 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3306 _bt_convert_addr_type_to_string(address, device_address->addr);
3308 BT_DBG("Remote device address: %s", address);
3310 device_path = _bt_get_device_object_path(address);
3312 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3314 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3315 NULL, BT_BLUEZ_NAME,
3316 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3317 g_free(device_path);
3318 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3320 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3321 att_mtu_req_info->addr = (char *)g_strdup(address);
3322 att_mtu_req_info->req_id = request_id;
3323 att_mtu_req_info->device_proxy = device_proxy;
3325 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3326 g_variant_new("(q)", mtu),
3327 G_DBUS_CALL_FLAGS_NONE,
3328 BT_MAX_DBUS_TIMEOUT,
3330 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3336 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3339 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3340 char *object_path = NULL;
3342 GDBusProxy *device_proxy;
3343 GError *error = NULL;
3345 GDBusConnection *conn;
3346 GVariant *result = NULL;
3348 BT_CHECK_PARAMETER(device_address, return);
3350 conn = _bt_gdbus_get_system_gconn();
3351 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3353 _bt_convert_addr_type_to_string(address, device_address->addr);
3355 object_path = _bt_get_device_object_path(address);
3356 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3358 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3359 NULL, BT_BLUEZ_NAME, object_path,
3360 BT_PROPERTIES_INTERFACE, NULL, NULL);
3361 g_free(object_path);
3362 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3364 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3365 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3366 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3368 ERR("DBus Error : %s", error->message);
3369 g_clear_error(&error);
3370 g_object_unref(device_proxy);
3371 return BLUETOOTH_ERROR_INTERNAL;
3373 g_variant_get(result, "(v)", &value);
3374 *mtu = g_variant_get_uint16(value);
3375 BT_DBG("ATT MTU : %d", *mtu);
3376 g_variant_unref(value);
3377 g_variant_unref(result);
3380 g_object_unref(device_proxy);
3381 return BLUETOOTH_ERROR_NONE;
3384 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3385 bluetooth_device_address_t *id_address)
3387 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3388 gchar *device_path = NULL;
3389 const gchar *idaddress = NULL;
3390 GDBusProxy *device_proxy;
3391 GError *error = NULL;
3392 GVariant *result = NULL;
3393 GDBusConnection *conn;
3394 int ret = BLUETOOTH_ERROR_NONE;
3396 BT_CHECK_PARAMETER(device_address, return);
3398 conn = _bt_gdbus_get_system_gconn();
3399 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3401 _bt_convert_addr_type_to_string(address, device_address->addr);
3403 device_path = _bt_get_device_object_path(address);
3404 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3406 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3407 NULL, BT_BLUEZ_NAME,
3408 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3409 g_free(device_path);
3410 if (!device_proxy) {
3411 BT_ERR("Unable to get proxy");
3412 return BLUETOOTH_ERROR_INTERNAL;
3415 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3416 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3417 if (result == NULL) {
3418 BT_ERR("Failed to get device ID address");
3420 ret = BLUETOOTH_ERROR_INTERNAL;
3421 if (error != NULL) {
3422 g_dbus_error_strip_remote_error(error);
3423 BT_ERR("Error occured in Proxy call [%s]", error->message);
3424 if (g_strcmp0(error->message, "Does Not Exist") == 0)
3425 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3426 g_error_free(error);
3428 g_object_unref(device_proxy);
3432 g_variant_get(result , "(&s)", &idaddress);
3434 DBG_SECURE("ID Address : %s", idaddress);
3435 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3437 g_variant_unref(result);
3438 g_object_unref(device_proxy);
3443 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3445 GapAgentPrivate *agent = _bt_get_adapter_agent();
3446 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3448 if (authentication_reply)
3449 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3451 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3453 BT_DBG("BT_PASSKEY_REPLY");
3454 return BLUETOOTH_ERROR_NONE;
3457 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3459 GapAgentPrivate *agent = _bt_get_adapter_agent();
3460 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3462 if (confirmation_reply)
3463 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3465 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3467 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3468 return BLUETOOTH_ERROR_NONE;