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_iter_free(value_iter);
498 g_variant_unref(tmp_value);
500 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
501 dev_info->manufacturer_data_len = 0;
502 dev_info->manufacturer_data = NULL;
505 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
506 g_variant_get(tmp_value, "s", &address);
507 g_variant_unref(tmp_value);
509 dev_info->address = address;
510 dev_info->name = name;
511 g_variant_unref(value);
513 BT_ERR("result is NULL\n");
521 char *_bt_get_device_name(const char *bdaddress)
523 char *device_path = NULL;
524 const gchar *name = NULL;
525 gchar *dev_name = NULL;
527 GVariant *result = NULL;
529 GDBusProxy *device_proxy;
530 GDBusConnection *conn;
532 retv_if(bdaddress == NULL, NULL);
534 device_path = _bt_get_device_object_path((char *)bdaddress);
535 retv_if(device_path == NULL, NULL);
537 conn = _bt_gdbus_get_system_gconn();
538 retv_if(conn == NULL, NULL);
539 INFO_SECURE("Device_path %s", device_path);
540 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
541 NULL, BT_BLUEZ_NAME, device_path,
542 BT_PROPERTIES_INTERFACE, NULL, &err);
545 retv_if(device_proxy == NULL, NULL);
547 result = g_dbus_proxy_call_sync(device_proxy, "Get",
548 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Alias"),
549 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
551 BT_ERR("DBus Error : %s", err->message);
555 g_variant_get(result, "(v)", &value);
556 name = g_variant_get_string(value, &name_len);
557 INFO_SECURE("Alias Name [%s]", name);
559 dev_name = g_strdup(name);
560 g_variant_unref(value);
561 g_variant_unref(result);
566 result = g_dbus_proxy_call_sync(device_proxy, "Get",
567 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Name"),
568 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &err);
570 ERR("DBus Error : %s", err->message);
573 g_variant_get(result, "(v)", &value);
574 name = g_variant_get_string(value, &name_len);
575 INFO_SECURE("Name = %s", name);
577 dev_name = g_strdup(name);
578 g_variant_unref(value);
579 g_variant_unref(result);
583 g_object_unref(device_proxy);
587 static gboolean __ignore_auto_pairing_request(const char *address)
592 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
603 /* Get the LAP(Lower Address part) */
604 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
606 snprintf(lap_address, sizeof(lap_address), ",%s", address);
608 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
611 BT_ERR("fopen failed \n");
615 fseek(fp, 0, SEEK_END);
620 BT_ERR("Get file size failed \n");
625 buffer = g_malloc0(sizeof(char) * size);
626 result = fread((char *)buffer, 1, size, fp);
628 if (result != size) {
629 BT_ERR("Read Error\n");
634 BT_DBG("Buffer = %s\n", buffer);
636 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
642 /* Write the data and insert new device data */
643 for (i = 0; lines[i] != NULL; i++) {
644 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
645 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
647 lines[i] = temp_buffer;
650 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
652 /* Fix : NULL_RETURNS */
653 retv_if(buffer == NULL, FALSE);
655 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
658 BT_ERR("fopen failed \n");
663 BT_DBG("Buffer = %s\n", buffer);
664 fwrite(buffer, 1, strlen(buffer), fp);
674 static int __bt_cancel_bonding(void)
676 BT_CHECK_PARAMETER(bonding_info, return);
677 BT_CHECK_PARAMETER(bonding_info->addr, return);
678 /* First Cancel the ongoing pairing in bluez otherwise if we send
679 * pair request bluez will send inprogress and we again retry bond so
680 * this cycle continues */
683 result = g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
684 NULL, G_DBUS_CALL_FLAGS_NONE,
688 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
690 return BLUETOOTH_ERROR_PARING_FAILED;
693 g_variant_unref(result);
695 return BLUETOOTH_ERROR_NONE;
698 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
701 int result = BLUETOOTH_ERROR_NONE;
704 GVariant *out_param1;
705 request_info_t *req_info;
706 bluetooth_device_info_t dev_info;
708 /* Terminate ALL system popup */
709 syspopup_destroy_all();
711 reply = g_dbus_proxy_call_finish(proxy, res, &err);
713 g_variant_unref(reply);
715 is_device_creating = FALSE;
717 if (bonding_info == NULL) {
719 BT_ERR("bonding_info == NULL");
725 req_info = _bt_get_request_info(bonding_info->req_id);
726 if (req_info == NULL) {
727 BT_ERR("req_info == NULL");
732 g_dbus_error_strip_remote_error(err);
733 BT_ERR("Error occured in CreateBonding [%s]", err->message);
735 if (g_strrstr(err->message, "Already Exists")) {
736 BT_INFO("Existing Bond");
737 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
738 } else if (_bt_agent_is_canceled() ||
739 g_strrstr(err->message, "Authentication Canceled")) {
740 BT_INFO("Cancelled by USER");
741 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
742 } else if (g_strrstr(err->message, "Authentication Rejected")) {
744 result = BLUETOOTH_ERROR_ACCESS_DENIED;
745 } else if (g_strrstr(err->message, "In Progress")) {
746 BT_INFO("Bond in progress");
747 result = BLUETOOTH_ERROR_IN_PROGRESS;
748 } else if (g_strrstr(err->message, "Authentication Failed")) {
749 BT_INFO("Authentication Failed");
750 if (bonding_info->is_autopair == TRUE) {
751 _bt_set_autopair_status_in_bonding_info(FALSE);
752 __ignore_auto_pairing_request(bonding_info->addr);
754 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
755 } else if (g_strrstr(err->message, "Page Timeout")) {
756 BT_INFO("Page Timeout");
757 /* This is the special case
758 As soon as call bluetooth_bond_device, try to cancel bonding.
759 In this case, before completing to call 'CreatePairedDevice' method
760 the procedure is stopped. So 'Cancle' error is not return.
762 result = BLUETOOTH_ERROR_HOST_DOWN;
763 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
764 BT_INFO("Cancel already running bonding");
765 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
766 BT_INFO("Error while Cancelling bonding");
767 /* we need to unref proxy so continue */
769 result = BLUETOOTH_ERROR_INTERNAL;
770 } else if (g_strrstr(err->message, "Connection Timeout")) {
771 /* Pairing request timeout */
772 result = BLUETOOTH_ERROR_TIMEOUT;
773 } else if (g_strrstr(err->message, "Authentication Timeout")) {
774 /* Pairing request timeout */
775 result = BLUETOOTH_ERROR_TIMEOUT;
777 BT_DBG("Default case");
778 result = BLUETOOTH_ERROR_PARING_FAILED;
782 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
783 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
784 result == BLUETOOTH_ERROR_TIMEOUT ||
785 result == BLUETOOTH_ERROR_HOST_DOWN) {
786 #ifdef TIZEN_PROFILE_WEARABLE
787 int is_sw_running = 0;
789 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
790 BT_ERR("vconf_get_int for setup wizard state failed");
793 __bt_launch_unable_to_pairing_syspopup(result);
795 BT_ERR("Unable to pair");
797 bonding_info->result = result;
800 g_object_unref(proxy);
801 bonding_info->device_proxy = NULL;
803 if (result != BLUETOOTH_ERROR_NONE) {
804 if (bonding_info->bonding_wo_discovery) {
805 GDBusProxy *adapter_proxy;
806 GVariant *ret = NULL;
807 GError *error = NULL;
810 BT_ERR("Bond was tried without discovery. Remove it");
812 adapter_proxy = _bt_get_adapter_proxy();
813 if (adapter_proxy == NULL) {
814 BT_ERR("Cannot get adapter_proxy");
818 device_path = _bt_get_device_object_path(bonding_info->addr);
819 if (device_path == NULL) {
820 BT_ERR("Cannot get device path");
824 ret = g_dbus_proxy_call_sync(adapter_proxy,
826 g_variant_new("(o)", device_path),
827 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
830 BT_ERR("RemoveDevice Fail: %s", error->message);
831 g_clear_error(&error);
835 g_variant_unref(ret);
844 if (req_info->context == NULL)
847 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
848 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
851 if (_bt_adapter_get_status() != BT_ACTIVATED)
852 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
854 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
855 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
857 g_dbus_method_invocation_return_value(req_info->context,
858 g_variant_new("(iv)", result, out_param1));
860 g_variant_unref(out_param1);
862 _bt_delete_request_list(req_info->req_id);
867 _bt_agent_set_canceled(FALSE);
870 g_free(bonding_info->addr);
871 g_free(bonding_info);
875 int _bt_bond_device(int request_id,
876 bluetooth_device_address_t *device_address,
877 unsigned short conn_type, GArray **out_param1)
880 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
881 bluetooth_device_info_t dev_info;
883 GDBusConnection *conn;
884 char *device_path = NULL;
885 GDBusProxy *adapter_proxy;
886 GError *error = NULL;
887 gboolean bonding_wo_discovery = FALSE;
889 BT_CHECK_PARAMETER(device_address, return);
892 BT_ERR("Bonding in progress");
894 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
895 memcpy(dev_info.device_address.addr, device_address->addr,
896 BLUETOOTH_ADDRESS_LENGTH);
898 g_array_append_vals(*out_param1, &dev_info,
899 sizeof(bluetooth_device_info_t));
901 return BLUETOOTH_ERROR_DEVICE_BUSY;
904 conn = _bt_gdbus_get_system_gconn();
905 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
907 _bt_convert_addr_type_to_string(address, device_address->addr);
909 device_path = _bt_get_device_object_path(address);
911 if (device_path == NULL) {
912 BT_ERR("No searched device");
913 GVariant *ret = NULL;
914 adapter_proxy = _bt_get_adapter_proxy();
915 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
917 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
918 g_variant_new("(s)", address),
919 G_DBUS_CALL_FLAGS_NONE,
925 BT_ERR("CreateDevice Fail: %s", error->message);
926 g_clear_error(&error);
929 g_variant_unref(ret);
930 device_path = _bt_get_device_object_path(address);
931 if (device_path == NULL) {
932 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
933 memcpy(dev_info.device_address.addr, device_address->addr,
934 BLUETOOTH_ADDRESS_LENGTH);
936 g_array_append_vals(*out_param1, &dev_info,
937 sizeof(bluetooth_device_info_t));
939 return BLUETOOTH_ERROR_NOT_PAIRED;
941 BT_INFO("device_path is created[%s]", device_path);
943 bonding_wo_discovery = TRUE;
946 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
948 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
951 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
953 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
954 bonding_info->addr = g_strdup(address);
955 bonding_info->req_id = request_id;
957 bonding_info->device_proxy = proxy;
958 bonding_info->conn_type = conn_type;
959 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
961 is_device_creating = TRUE;
963 g_dbus_proxy_call(proxy, "Pair",
964 g_variant_new("(y)", conn_type),
965 G_DBUS_CALL_FLAGS_NONE,
968 (GAsyncReadyCallback)__bt_bond_device_cb,
971 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
973 return BLUETOOTH_ERROR_NONE;
975 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
976 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
979 g_array_append_vals(*out_param1, &dev_info,
980 sizeof(bluetooth_device_info_t));
982 is_device_creating = FALSE;
984 g_free(bonding_info->addr);
985 g_free(bonding_info);
988 return BLUETOOTH_ERROR_INTERNAL;*/
991 int _bt_cancel_bonding(void)
993 int ret = BLUETOOTH_ERROR_NONE;
995 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
997 ret = _bt_agent_reply_cancellation();
998 if (ret != BLUETOOTH_ERROR_NONE) {
999 BT_ERR("Fail to call reply cancellation");
1003 _bt_agent_set_canceled(TRUE);
1005 return BLUETOOTH_ERROR_NONE;
1008 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1013 GVariant *out_param1;
1014 int result = BLUETOOTH_ERROR_NONE;
1015 bt_funcion_data_t *unbonding_info;
1016 bluetooth_device_info_t dev_info;
1017 request_info_t *req_info;
1019 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1021 g_variant_unref(reply);
1023 unbonding_info = user_data;
1025 if (unbonding_info == NULL) {
1027 BT_ERR("unbonding_info == NULL");
1031 req_info = _bt_get_request_info(unbonding_info->req_id);
1032 if (req_info == NULL) {
1033 BT_ERR("req_info == NULL");
1038 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1039 result = BLUETOOTH_ERROR_INTERNAL;
1042 if (req_info->context == NULL)
1045 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1046 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1047 unbonding_info->addr);
1049 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1050 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1052 g_dbus_method_invocation_return_value(req_info->context,
1053 g_variant_new("(iv)", result, out_param1));
1056 _bt_delete_request_list(req_info->req_id);
1062 if (unbonding_info) {
1063 g_free(unbonding_info->addr);
1064 g_free(unbonding_info);
1068 int _bt_unbond_device(int request_id,
1069 bluetooth_device_address_t *device_address,
1070 GArray **out_param1)
1072 char *device_path = NULL;
1073 bt_funcion_data_t *unbonding_info;
1074 GDBusProxy *adapter_proxy = NULL;
1075 GDBusProxy *device_proxy = NULL;
1076 GDBusConnection *conn;
1077 int result = BLUETOOTH_ERROR_INTERNAL;
1078 bluetooth_device_info_t dev_info;
1079 GError *error = NULL;
1080 GVariant *ret = NULL;
1082 BT_CHECK_PARAMETER(device_address, return);
1084 adapter_proxy = _bt_get_adapter_proxy();
1085 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1087 /* allocate user data so that it can be retrieved in callback */
1088 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1089 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1090 unbonding_info->req_id = request_id;
1092 _bt_convert_addr_type_to_string(unbonding_info->addr,
1093 device_address->addr);
1095 device_path = _bt_get_device_object_path(unbonding_info->addr);
1097 if (device_path == NULL) {
1098 BT_ERR("No paired device");
1099 result = BLUETOOTH_ERROR_NOT_PAIRED;
1103 conn = _bt_gdbus_get_system_gconn();
1105 BT_ERR("conn is NULL");
1106 result = BLUETOOTH_ERROR_INTERNAL;
1110 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1111 NULL, BT_BLUEZ_NAME,
1112 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1114 if (device_proxy != NULL) {
1116 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1117 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1118 G_DBUS_CALL_FLAGS_NONE,
1123 BT_ERR("Getting property failed: [%s]\n", error->message);
1124 g_error_free(error);
1125 result = BLUETOOTH_ERROR_NOT_PAIRED;
1129 BT_ERR("No paired device");
1130 g_object_unref(device_proxy);
1131 result = BLUETOOTH_ERROR_NOT_PAIRED;
1134 g_variant_unref(ret);
1136 g_object_unref(device_proxy);
1139 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1140 g_variant_new("(o)", device_path),
1141 G_DBUS_CALL_FLAGS_NONE,
1142 BT_MAX_DBUS_TIMEOUT,
1144 (GAsyncReadyCallback)__bt_unbond_cb,
1147 g_free(device_path);
1148 return BLUETOOTH_ERROR_NONE;
1151 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1152 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1153 unbonding_info->addr);
1155 g_array_append_vals(*out_param1, &dev_info,
1156 sizeof(bluetooth_device_info_t));
1157 g_free(device_path);
1158 g_free(unbonding_info->addr);
1159 g_free(unbonding_info);
1163 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1168 GVariant *out_param1;
1169 int result = BLUETOOTH_ERROR_NONE;
1170 bluetooth_device_info_t dev_info;
1171 bt_remote_dev_info_t *remote_dev_info;
1172 request_info_t *req_info;
1173 GVariant *uuid_list, *manufacture_data;
1175 GVariantBuilder *builder = NULL;
1178 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1180 g_variant_unref(reply);
1182 g_object_unref(proxy);
1184 if (searching_info == NULL) {
1186 BT_ERR("unbonding_info == NULL");
1190 req_info = _bt_get_request_info(searching_info->req_id);
1191 if (req_info == NULL) {
1192 BT_ERR("req_info == NULL");
1197 g_dbus_error_strip_remote_error(err);
1198 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1200 if (g_strrstr("Operation canceled", err->message))
1201 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1202 else if (g_strrstr("In Progress", err->message))
1203 result = BLUETOOTH_ERROR_IN_PROGRESS;
1204 else if (g_strrstr("Host is down", err->message))
1205 result = BLUETOOTH_ERROR_HOST_DOWN;
1207 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1209 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1210 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1211 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1212 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1213 remote_dev_info->uuid_count > 0) {
1214 result = BLUETOOTH_ERROR_NONE;
1217 _bt_free_device_info(remote_dev_info);
1222 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1223 if (!remote_dev_info)
1227 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1228 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1229 g_variant_builder_add(builder, "s",
1230 remote_dev_info->uuids[i]);
1232 uuid_list = g_variant_new("as", builder);
1233 g_variant_builder_unref(builder);
1234 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1235 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1238 param = g_variant_new("(isunsbub@asn@ay)",
1240 searching_info->addr,
1241 remote_dev_info->class,
1242 remote_dev_info->rssi,
1243 remote_dev_info->name,
1244 remote_dev_info->paired,
1245 remote_dev_info->connected,
1246 remote_dev_info->trust,
1248 remote_dev_info->manufacturer_data_len,
1251 /* Send the event to application */
1252 _bt_send_event(BT_ADAPTER_EVENT,
1253 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1256 _bt_free_device_info(remote_dev_info);
1259 if (req_info->context == NULL)
1262 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1263 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1264 searching_info->addr);
1266 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1267 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1269 g_dbus_method_invocation_return_value(req_info->context,
1270 g_variant_new("(iv)", result, out_param1));
1273 _bt_delete_request_list(req_info->req_id);
1278 if (searching_info) {
1279 g_free(searching_info->addr);
1280 g_free(searching_info);
1281 searching_info = NULL;
1285 int _bt_search_device(int request_id,
1286 bluetooth_device_address_t *device_address)
1288 char *device_path = NULL;
1289 GDBusProxy *device_proxy = NULL;
1290 GDBusConnection *conn;
1292 GDBusProxy *adapter_proxy;
1293 int result = BLUETOOTH_ERROR_INTERNAL;
1295 BT_CHECK_PARAMETER(device_address, return);
1298 BT_ERR("Bonding in progress");
1299 return BLUETOOTH_ERROR_DEVICE_BUSY;
1302 if (searching_info) {
1303 BT_ERR("Service searching in progress");
1304 return BLUETOOTH_ERROR_DEVICE_BUSY;
1307 adapter_proxy = _bt_get_adapter_proxy();
1308 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1310 /* allocate user data so that it can be retrieved in callback */
1311 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1312 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1313 searching_info->req_id = request_id;
1315 _bt_convert_addr_type_to_string(searching_info->addr,
1316 device_address->addr);
1318 conn = _bt_gdbus_get_system_gconn();
1319 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1322 device_path = _bt_get_device_object_path(searching_info->addr);
1324 if (device_path == NULL) {
1325 BT_ERR("No paired device");
1326 result = BLUETOOTH_ERROR_NOT_PAIRED;
1330 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1331 NULL, BT_BLUEZ_NAME,
1332 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1333 g_free(device_path);
1334 if (device_proxy == NULL) {
1335 result = BLUETOOTH_ERROR_INTERNAL;
1339 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1340 g_variant_new("(s)", ""),
1341 G_DBUS_CALL_FLAGS_NONE,
1342 BT_MAX_DBUS_TIMEOUT,
1344 (GAsyncReadyCallback)__bt_discover_cb,
1347 searching_info->device_proxy = device_proxy;
1349 return BLUETOOTH_ERROR_NONE;
1352 g_free(searching_info->addr);
1353 g_free(searching_info);
1354 searching_info = NULL;
1358 int _bt_cancel_search_device(void)
1360 GVariant *ret = NULL;
1363 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1365 if (searching_info->device_proxy) {
1366 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1368 G_DBUS_CALL_FLAGS_NONE,
1373 g_variant_unref(ret);
1375 __bt_cancel_search_service_done();
1377 return BLUETOOTH_ERROR_NONE;
1380 int _bt_set_alias(bluetooth_device_address_t *device_address,
1383 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1384 gchar *device_path = NULL;
1385 GDBusProxy *adapter_proxy;
1386 GDBusProxy *device_proxy;
1387 GVariant *ret = NULL;
1388 GError *error = NULL;
1389 GDBusConnection *conn;
1391 BT_CHECK_PARAMETER(device_address, return);
1392 BT_CHECK_PARAMETER(alias, return);
1394 adapter_proxy = _bt_get_adapter_proxy();
1395 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1397 conn = _bt_gdbus_get_system_gconn();
1398 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1400 _bt_convert_addr_type_to_string(address, device_address->addr);
1402 device_path = _bt_get_device_object_path(address);
1404 if (device_path == NULL) {
1405 BT_ERR("No paired device");
1406 return BLUETOOTH_ERROR_NOT_PAIRED;
1409 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1410 NULL, BT_BLUEZ_NAME,
1411 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1413 g_free(device_path);
1414 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1416 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1417 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1418 G_DBUS_CALL_FLAGS_NONE,
1423 g_variant_unref(ret);
1425 g_object_unref(device_proxy);
1428 BT_ERR("SetProperty error: [%s]", error->message);
1429 g_error_free(error);
1430 return BLUETOOTH_ERROR_INTERNAL;
1433 return BLUETOOTH_ERROR_NONE;
1436 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1439 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1440 gchar *device_path = NULL;
1441 GDBusProxy *device_proxy;
1442 gboolean previous_value;
1443 GError *error = NULL;
1444 GDBusConnection *conn;
1445 GVariant *result = NULL;
1446 GVariant *temp = NULL;
1447 int ret = BLUETOOTH_ERROR_NONE;
1449 BT_CHECK_PARAMETER(device_address, return);
1451 conn = _bt_gdbus_get_system_gconn();
1452 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1454 _bt_convert_addr_type_to_string(address, device_address->addr);
1456 device_path = _bt_get_device_object_path(address);
1458 if (device_path == NULL) {
1459 BT_ERR("No paired device");
1460 return BLUETOOTH_ERROR_NOT_PAIRED;
1463 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1464 NULL, BT_BLUEZ_NAME,
1465 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1467 g_free(device_path);
1468 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1470 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1471 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1472 G_DBUS_CALL_FLAGS_NONE,
1476 if (error != NULL) {
1477 BT_ERR("Getting property failed: [%s]\n", error->message);
1478 g_error_free(error);
1479 g_object_unref(device_proxy);
1480 return BLUETOOTH_ERROR_INTERNAL;
1483 g_variant_get(result, "(v)", &temp);
1484 previous_value = g_variant_get_boolean(temp);
1485 g_variant_unref(temp);
1486 g_variant_unref(result);
1487 /* If the input is same with previous value, return error. */
1488 if (previous_value == authorize) {
1489 BT_ERR("Same value: %d", previous_value);
1490 g_object_unref(device_proxy);
1491 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1495 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1496 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1497 G_DBUS_CALL_FLAGS_NONE,
1502 g_object_unref(device_proxy);
1504 BT_ERR("SetProperty error: [%s]", error->message);
1505 g_error_free(error);
1506 ret = BLUETOOTH_ERROR_INTERNAL;
1510 g_variant_unref(result);
1515 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1516 gboolean *is_connected)
1518 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1519 char *object_path = NULL;
1521 GDBusProxy *device_proxy;
1522 GError *error = NULL;
1524 GDBusConnection *conn;
1525 GVariant *result = NULL;
1526 int ret = BLUETOOTH_ERROR_NONE;
1528 BT_CHECK_PARAMETER(device_address, return);
1530 conn = _bt_gdbus_get_system_gconn();
1531 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1533 _bt_convert_addr_type_to_string(address, device_address->addr);
1535 object_path = _bt_get_device_object_path(address);
1536 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1538 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1539 NULL, BT_BLUEZ_NAME, object_path,
1540 BT_PROPERTIES_INTERFACE, NULL, NULL);
1541 g_free(object_path);
1542 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1544 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1545 g_variant_new("(ss)", BT_DEVICE_INTERFACE,
1547 G_DBUS_CALL_FLAGS_NONE,
1551 if (result == NULL) {
1552 if (error != NULL) {
1553 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1554 g_error_free(error);
1556 g_object_unref(device_proxy);
1557 return BLUETOOTH_ERROR_INTERNAL;
1560 g_variant_get(result, "(v)", &value);
1561 *is_connected = g_variant_get_boolean(value);
1562 g_variant_unref(result);
1564 BT_DBG("gatt is connected : %d", *is_connected);
1566 g_variant_unref(value);
1567 g_object_unref(device_proxy);
1572 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1573 int connection_type, gboolean *is_connected)
1575 char *object_path = NULL;
1576 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1577 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1578 GDBusProxy *device_proxy = NULL;
1579 GDBusProxy *adapter_proxy = NULL;
1580 GDBusConnection *conn;
1581 GError *error = NULL;
1582 GVariant *tmp_value = NULL;
1583 GVariant *value = NULL;
1584 GVariant *result = NULL;
1587 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1588 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1590 _bt_convert_addr_type_to_string(address, device_address->addr);
1591 _bt_convert_addr_string_to_secure_string(secure_address, address);
1593 *is_connected = FALSE;
1594 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1596 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1597 return _bt_rfcomm_is_device_connected(device_address,
1599 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1600 return _bt_is_gatt_connected(device_address, is_connected);
1601 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1602 return _bt_pbap_is_connected(device_address, is_connected);
1604 adapter_proxy = _bt_get_adapter_proxy();
1605 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1607 conn = _bt_gdbus_get_system_gconn();
1608 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1610 _bt_convert_addr_type_to_string(address, device_address->addr);
1612 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1613 object_path = _bt_get_adapter_path();
1614 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1615 NULL, BT_BLUEZ_NAME,
1616 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1617 g_free(object_path);
1618 if (device_proxy == NULL) {
1619 BT_DBG("Device don't have this service");
1620 return BLUETOOTH_ERROR_INTERNAL;
1623 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1624 g_variant_new("(s)", address),
1625 G_DBUS_CALL_FLAGS_NONE,
1630 if (result == NULL) {
1631 BT_ERR("[GetProperties] Error occured in Proxy call");
1633 BT_ERR("%s", error->message);
1634 g_error_free(error);
1636 *is_connected = FALSE;
1637 g_object_unref(device_proxy);
1638 return BLUETOOTH_ERROR_NONE;
1640 g_variant_get(result , "(@a{sv})", &value);
1641 g_variant_unref(result);
1644 tmp_value = g_variant_lookup_value(value,
1646 G_VARIANT_TYPE_BOOLEAN);
1648 *is_connected = g_variant_get_boolean(tmp_value);
1649 g_variant_unref(tmp_value);
1651 g_variant_unref(value);
1653 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1654 return _bt_is_network_connected(_bt_get_net_conn(),
1655 device_address->addr, is_connected);
1657 uuid = _bt_get_profile_uuid128(connection_type);
1659 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1660 return BLUETOOTH_ERROR_INTERNAL;
1662 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1664 object_path = _bt_get_device_object_path(address);
1665 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1666 BT_DBG("object_path: %s", object_path);
1667 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1668 NULL, BT_BLUEZ_NAME,
1669 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1670 g_free(object_path);
1671 if (device_proxy == NULL) {
1672 BT_DBG("Device don't have this service");
1674 return BLUETOOTH_ERROR_INTERNAL;
1677 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1678 g_variant_new("(s)", uuid),
1679 G_DBUS_CALL_FLAGS_NONE,
1684 if (result == NULL) {
1685 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1687 BT_ERR("%s", error->message);
1688 if (g_strrstr(error->message, "Not Connected"))
1689 BT_DBG("Not connected");
1690 g_error_free(error);
1692 *is_connected = FALSE;
1693 g_object_unref(device_proxy);
1695 return BLUETOOTH_ERROR_NONE;
1697 g_variant_get(result, "(b)", is_connected);
1699 g_variant_unref(result);
1702 g_object_unref(device_proxy);
1703 return BLUETOOTH_ERROR_NONE;
1706 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1707 bluetooth_connected_link_t *connected)
1709 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1710 char *object_path = NULL;
1712 GDBusProxy *device_proxy;
1713 GError *error = NULL;
1714 GDBusConnection *conn;
1715 GVariant *value = NULL;
1716 GVariant *result = NULL;
1718 BT_CHECK_PARAMETER(device_address, return);
1720 conn = _bt_gdbus_get_system_gconn();
1721 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1723 _bt_convert_addr_type_to_string(address, device_address->addr);
1725 object_path = _bt_get_device_object_path(address);
1726 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1728 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1729 NULL, BT_BLUEZ_NAME,
1730 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1731 g_free(object_path);
1732 if (device_proxy == NULL) {
1733 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1734 return BLUETOOTH_ERROR_NONE;
1737 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1738 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Connected"),
1739 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1741 if (error != NULL) {
1742 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1743 g_error_free(error);
1744 g_object_unref(device_proxy);
1745 return BLUETOOTH_ERROR_INTERNAL;
1747 g_variant_get(result, "(v)", &value);
1748 *connected = g_variant_get_byte(value);
1749 g_variant_unref(value);
1750 g_variant_unref(result);
1753 g_object_unref(device_proxy);
1754 return BLUETOOTH_ERROR_NONE;
1757 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1761 GVariant *out_param1;
1763 int result = BLUETOOTH_ERROR_NONE;
1764 bt_function_data_t *func_data = user_data;
1765 request_info_t *req_info = NULL;
1766 bluetooth_device_address_t device_addr = { {0} };
1768 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1769 g_object_unref(proxy);
1771 if (reply == NULL) {
1772 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1774 BT_ERR("Error: %s", err->message);
1775 g_clear_error(&err);
1777 result = BLUETOOTH_ERROR_INTERNAL;
1779 g_variant_unref(reply);
1782 if (func_data == NULL) {
1783 BT_ERR("func_data is NULL");
1787 req_info = _bt_get_request_info(func_data->req_id);
1788 if (req_info == NULL) {
1789 BT_ERR("req_info is NULL");
1793 if (req_info->context == NULL) {
1794 BT_ERR("req_info->context is NULL");
1798 _bt_convert_addr_string_to_type(device_addr.addr,
1799 (const char *)func_data->address);
1801 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1802 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1805 g_dbus_method_invocation_return_value(req_info->context,
1806 g_variant_new("(iv)", result, out_param1));
1810 _bt_delete_request_list(req_info->req_id);
1813 g_free(func_data->address);
1818 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1819 gboolean auto_connect)
1821 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1822 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1823 gchar *device_path = NULL;
1824 GDBusProxy *device_proxy = NULL;
1825 GDBusConnection *conn;
1826 int ret = BLUETOOTH_ERROR_NONE;
1827 bt_function_data_t *func_data;
1829 BT_CHECK_PARAMETER(bd_addr, return);
1831 conn = _bt_gdbus_get_system_gconn();
1832 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1834 _bt_convert_addr_type_to_string(device_address,
1835 (unsigned char *)bd_addr->addr);
1836 device_path = _bt_get_device_object_path(device_address);
1837 if (device_path == NULL) {
1838 BT_ERR_C("device_path NULL : [%s]", device_address);
1839 ret = BLUETOOTH_ERROR_INTERNAL;
1843 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1844 NULL, BT_BLUEZ_NAME,
1845 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1846 g_free(device_path);
1847 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1849 func_data = g_malloc0(sizeof(bt_function_data_t));
1850 func_data->address = g_strdup(device_address);
1851 if (func_data->address == NULL) {
1852 BT_ERR("Unable to allocate memory for address");
1853 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1857 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1858 BT_INFO("Connect LE [%s]", secure_address);
1860 func_data->req_id = req_id;
1862 g_dbus_proxy_call(device_proxy, "ConnectLE",
1863 g_variant_new("(b)", auto_connect),
1864 G_DBUS_CALL_FLAGS_NONE,
1865 BT_MAX_DBUS_TIMEOUT,
1867 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1873 g_object_unref(device_proxy);
1875 g_free(func_data->address);
1881 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1883 BT_INFO("Try to initiate pending LE connection");
1885 pending_le_conn_timer_id = 0;
1887 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1888 &pending_le_conn_info->bd_addr,
1889 pending_le_conn_info->auto_connect);
1891 g_free(pending_le_conn_info);
1892 pending_le_conn_info = NULL;
1897 void _bt_pending_connect_le_device(void)
1899 if (pending_le_conn_timer_id > 0) {
1900 g_source_remove(pending_le_conn_timer_id);
1901 __bt_connect_le_timer_cb(NULL);
1905 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1906 gboolean auto_connect)
1908 int ret = BLUETOOTH_ERROR_NONE;
1910 BT_CHECK_PARAMETER(bd_addr, return);
1912 ret = _bt_hold_current_advertising();
1913 if (ret == BLUETOOTH_ERROR_NONE) {
1914 BT_INFO("Current advertising is held");
1915 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1916 pending_le_conn_info->req_id = req_id;
1917 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1918 BLUETOOTH_ADDRESS_LENGTH);
1919 pending_le_conn_info->auto_connect = auto_connect;
1921 pending_le_conn_timer_id =
1922 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1924 return BLUETOOTH_ERROR_NONE;
1925 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
1926 BT_ERR("Unable to hold advertising");
1929 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1932 int _bt_disconnect_le_device(int req_id,
1933 const bluetooth_device_address_t *bd_addr)
1935 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1936 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1938 GDBusProxy *device_proxy;
1939 GDBusConnection *conn;
1940 int ret = BLUETOOTH_ERROR_NONE;
1941 bt_function_data_t *func_data;
1943 BT_CHECK_PARAMETER(bd_addr, return);
1945 conn = _bt_gdbus_get_system_gconn();
1946 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1948 _bt_convert_addr_type_to_string(device_address,
1949 (unsigned char *)bd_addr->addr);
1950 device_path = _bt_get_device_object_path(device_address);
1951 if (device_path == NULL) {
1952 BT_DBG("device_path NULL");
1953 ret = BLUETOOTH_ERROR_INTERNAL;
1957 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1959 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1960 NULL, BT_BLUEZ_NAME,
1961 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1962 g_free(device_path);
1963 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1965 func_data = g_malloc0(sizeof(bt_function_data_t));
1966 func_data->address = g_strdup(device_address);
1967 if (func_data->address == NULL) {
1968 BT_ERR("Unable to allocate memory for address");
1969 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1973 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1974 BT_INFO("Disconnect LE [%s]", secure_address);
1976 func_data->req_id = req_id;
1978 g_dbus_proxy_call(device_proxy, "DisconnectLE",
1980 G_DBUS_CALL_FLAGS_NONE,
1981 BT_MAX_DBUS_TIMEOUT,
1983 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1988 g_object_unref(device_proxy);
1990 g_free(func_data->address);
1996 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
1998 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1999 gchar *device_path = NULL;
2000 GError *error = NULL;
2001 GDBusProxy *device_proxy = NULL;
2002 GDBusProxy *adapter_proxy;
2003 GDBusConnection *conn;
2004 int ret = BLUETOOTH_ERROR_NONE;
2007 BT_CHECK_PARAMETER(bd_addr, return);
2009 _bt_convert_addr_type_to_string(device_address,
2010 (unsigned char *)bd_addr->addr);
2012 conn = _bt_gdbus_get_system_gconn();
2013 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2015 adapter_proxy = _bt_get_adapter_proxy();
2016 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2018 device_path = _bt_get_device_object_path(device_address);
2019 if (device_path == NULL) {
2020 BT_DBG("device_path NULL");
2021 ret = BLUETOOTH_ERROR_INTERNAL;
2025 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2027 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2028 NULL, BT_BLUEZ_NAME,
2029 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2030 g_free(device_path);
2031 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2033 result = g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2035 G_DBUS_CALL_FLAGS_NONE,
2041 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2042 g_error_free(error);
2043 g_object_unref(device_proxy);
2044 return BLUETOOTH_ERROR_INTERNAL;
2047 g_variant_unref(result);
2050 g_object_unref(device_proxy);
2055 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2057 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2058 gchar *device_path = NULL;
2059 GError *error = NULL;
2060 GDBusProxy *device_proxy = NULL;
2061 GDBusProxy *adapter_proxy;
2062 GDBusConnection *conn;
2063 int ret = BLUETOOTH_ERROR_NONE;
2066 BT_CHECK_PARAMETER(bd_addr, return);
2068 _bt_convert_addr_type_to_string(device_address,
2069 (unsigned char *)bd_addr->addr);
2071 conn = _bt_gdbus_get_system_gconn();
2072 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2074 adapter_proxy = _bt_get_adapter_proxy();
2075 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2077 device_path = _bt_get_device_object_path(device_address);
2078 if (device_path == NULL) {
2079 BT_DBG("device_path NULL");
2080 ret = BLUETOOTH_ERROR_INTERNAL;
2084 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2086 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2087 NULL, BT_BLUEZ_NAME,
2088 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2089 g_free(device_path);
2090 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2092 result = g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2094 G_DBUS_CALL_FLAGS_NONE,
2100 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2101 g_error_free(error);
2102 g_object_unref(device_proxy);
2103 return BLUETOOTH_ERROR_INTERNAL;
2106 g_variant_unref(result);
2109 g_object_unref(device_proxy);
2114 int _bt_connect_profile(char *address, char *uuid,
2115 void *cb, gpointer func_data)
2119 GDBusConnection *conn;
2120 GDBusProxy *adapter_proxy;
2121 GVariant *result = NULL;
2122 GError *error = NULL;
2124 conn = _bt_gdbus_get_system_gconn();
2125 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2127 object_path = _bt_get_device_object_path(address);
2128 if (object_path == NULL) {
2129 BT_ERR("No searched device");
2131 adapter_proxy = _bt_get_adapter_proxy();
2132 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2134 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2135 g_variant_new("(s)", address),
2136 G_DBUS_CALL_FLAGS_NONE,
2141 if (error != NULL) {
2142 BT_ERR("CreateDevice Fail: %s", error->message);
2143 g_error_free(error);
2146 g_variant_unref(result);
2148 object_path = _bt_get_device_object_path(address);
2150 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2152 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2153 NULL, BT_BLUEZ_NAME,
2154 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2155 g_free(object_path);
2156 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2158 g_dbus_proxy_call(proxy, "ConnectProfile",
2159 g_variant_new("(s)", uuid),
2160 G_DBUS_CALL_FLAGS_NONE,
2161 BT_MAX_DBUS_TIMEOUT,
2163 (GAsyncReadyCallback)cb,
2166 return BLUETOOTH_ERROR_NONE;
2169 int _bt_disconnect_all(char *address)
2171 int ret = BLUETOOTH_ERROR_NONE;
2174 GDBusConnection *conn;
2175 GVariant *result = NULL;
2179 conn = _bt_gdbus_get_system_gconn();
2180 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2182 object_path = _bt_get_device_object_path(address);
2183 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2185 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2186 NULL, BT_BLUEZ_NAME,
2187 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2188 g_free(object_path);
2189 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2191 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2193 G_DBUS_CALL_FLAGS_NONE,
2198 BT_ERR("Dbus Call Error:[%s]", err->message);
2200 ret = BLUETOOTH_ERROR_INTERNAL;
2203 g_object_unref(proxy);
2205 g_variant_unref(result);
2210 int _bt_disconnect_profile(char *address, char *uuid,
2211 void *cb, gpointer func_data)
2215 GDBusConnection *conn;
2217 conn = _bt_gdbus_get_system_gconn();
2218 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2220 object_path = _bt_get_device_object_path(address);
2221 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2223 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2224 NULL, BT_BLUEZ_NAME,
2225 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2226 g_free(object_path);
2227 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2229 g_dbus_proxy_call(proxy, "DisconnectProfile",
2230 g_variant_new("(s)", uuid),
2231 G_DBUS_CALL_FLAGS_NONE,
2232 BT_MAX_DBUS_TIMEOUT,
2234 (GAsyncReadyCallback)cb,
2237 return BLUETOOTH_ERROR_NONE;
2240 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2241 int low_threshold, int in_range_threshold, int high_threshold)
2243 int ret = BLUETOOTH_ERROR_NONE;
2245 GVariant *result = NULL;
2246 GError *error = NULL;
2247 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2249 BT_CHECK_PARAMETER(bd_addr, return);
2250 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2251 bd_addr->addr[0], bd_addr->addr[1],
2252 bd_addr->addr[2], bd_addr->addr[3],
2253 bd_addr->addr[4], bd_addr->addr[5],
2255 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2256 in_range_threshold, high_threshold);
2258 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2260 proxy = _bt_get_adapter_proxy();
2261 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2263 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2264 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2265 G_DBUS_CALL_FLAGS_NONE,
2269 if (error != NULL) {
2270 BT_ERR("Dbus Call Error:[%s]", error->message);
2271 g_error_free(error);
2272 ret = BLUETOOTH_ERROR_INTERNAL;
2276 g_variant_unref(result);
2281 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2284 int ret = BLUETOOTH_ERROR_NONE;
2286 GVariant *result = NULL;
2287 GError *error = NULL;
2288 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2290 BT_CHECK_PARAMETER(bd_addr, return);
2291 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2292 bd_addr->addr[0], bd_addr->addr[1],
2293 bd_addr->addr[2], bd_addr->addr[3],
2294 bd_addr->addr[4], bd_addr->addr[5],
2297 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2299 proxy = _bt_get_adapter_proxy();
2300 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2302 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2303 g_variant_new("(si)", address, link_type),
2304 G_DBUS_CALL_FLAGS_NONE,
2309 if (error != NULL) {
2310 BT_ERR("Dbus Call Error:[%s]", error->message);
2311 g_error_free(error);
2312 ret = BLUETOOTH_ERROR_INTERNAL;
2316 g_variant_unref(result);
2321 static int __bt_le_set_conn_parameter(const char *address,
2322 float interval_min, float interval_max,
2323 guint16 latency, guint16 time_out)
2325 gchar *device_path = NULL;
2326 GError *error = NULL;
2327 GDBusProxy *device_proxy = NULL;
2328 GDBusConnection *conn;
2330 guint32 min, max, to;
2331 int ret = BLUETOOTH_ERROR_NONE;
2333 BT_CHECK_PARAMETER(address, return);
2335 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2336 interval_min, interval_max, latency, time_out);
2338 device_path = _bt_get_device_object_path((char *)address);
2340 if (device_path == NULL) {
2341 BT_ERR("device_path NULL");
2342 ret = BLUETOOTH_ERROR_INTERNAL;
2346 conn = _bt_gdbus_get_system_gconn();
2348 BT_ERR("conn NULL");
2349 ret = BLUETOOTH_ERROR_INTERNAL;
2353 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2354 NULL, BT_BLUEZ_NAME,
2355 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2357 g_free(device_path);
2358 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2360 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2361 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2362 to = time_out / BT_LE_CONN_TO_SPLIT;
2364 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2365 g_variant_new("(uuuu)", min, max, latency, to),
2366 G_DBUS_CALL_FLAGS_NONE,
2371 g_object_unref(device_proxy);
2372 if (reply == NULL) {
2374 BT_ERR("Error %s[%s]", error->message, address);
2375 if (g_strrstr(error->message, "In Progress"))
2376 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2378 ret = BLUETOOTH_ERROR_INTERNAL;
2379 g_error_free(error);
2383 g_variant_unref(reply);
2389 static void __bt_le_conn_param_free(void *data)
2391 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2393 BT_DBG("%s", param->sender);
2394 g_free(param->sender);
2398 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2400 bt_le_conn_param_t *parama = (bt_le_conn_param_t *)a;
2401 bt_le_conn_param_t *paramb = (bt_le_conn_param_t *)b;
2403 return parama->key > paramb->key;
2406 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2409 bt_connected_le_dev_t *dev;
2414 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2417 if (g_strcmp0(dev->address, address) == 0)
2423 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2426 bt_le_conn_param_t *param = NULL;
2428 if (!dev || !sender)
2431 for (l = dev->senders; l; l = g_slist_next(l)) {
2433 if (g_strcmp0(param->sender, sender) == 0)
2440 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2441 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2443 bt_connected_le_dev_t *dev = NULL;
2444 bt_le_conn_param_t *param = NULL;
2445 bt_le_conn_param_t *data = NULL;
2447 if (!address || !sender)
2448 return BLUETOOTH_ERROR_INVALID_PARAM;
2450 dev = __bt_get_le_connected_dev_info(address);
2452 return BLUETOOTH_ERROR_INTERNAL;
2454 param = __bt_get_le_conn_param_info(dev, sender);
2456 data = g_malloc0(sizeof(bt_le_conn_param_t));
2457 data->sender = g_strdup(sender);
2458 data->interval_min = interval_min;
2459 data->interval_max = interval_max;
2460 data->latency = latency;
2461 data->time_out = time_out;
2462 data->key = interval_min + (interval_max - interval_min)/2;
2464 if (param == NULL) {
2465 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2466 dev->senders = g_slist_append(dev->senders, data);
2468 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2469 dev->senders = g_slist_remove(dev->senders, param);
2470 g_free(param->sender);
2472 dev->senders = g_slist_append(dev->senders, data);
2475 /* Sorting. First element have the minimum interval */
2476 dev->senders = g_slist_sort(dev->senders,
2477 (GCompareFunc)__bt_compare_le_conn_param_key);
2479 return BLUETOOTH_ERROR_NONE;
2482 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2484 bt_connected_le_dev_t *dev = NULL;
2485 bt_le_conn_param_t *param = NULL;
2487 if (!address || !sender)
2488 return BLUETOOTH_ERROR_INVALID_PARAM;
2490 dev = __bt_get_le_connected_dev_info(address);
2492 return BLUETOOTH_ERROR_INTERNAL;
2494 param = __bt_get_le_conn_param_info(dev, sender);
2496 BT_DBG("Remove param %s %s ", address, sender);
2497 dev->senders = g_slist_remove(dev->senders, param);
2498 g_free(param->sender);
2502 *is_removed = FALSE;
2504 return BLUETOOTH_ERROR_NONE;
2507 int _bt_remove_all_le_conn_param_info(const char *sender)
2510 bt_connected_le_dev_t *dev = NULL;
2511 bt_le_conn_param_t *param = NULL;
2512 gboolean is_removed = FALSE;
2513 char *sender_new = NULL;
2514 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2515 int ret = BLUETOOTH_ERROR_NONE;
2518 return BLUETOOTH_ERROR_INVALID_PARAM;
2520 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2522 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2525 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2526 g_slist_length(dev->senders));
2527 if (g_slist_length(dev->senders) > 0) {
2528 param = dev->senders->data;
2529 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2530 param->interval_min, param->interval_max);
2532 if (dev->interval_min != param->interval_min ||
2533 dev->interval_max != param->interval_max) {
2534 sender_new = g_strdup(param->sender);
2536 _bt_convert_addr_string_to_type(addr, dev->address);
2537 ret = _bt_le_conn_update(sender_new, addr,
2538 param->interval_min, param->interval_max,
2539 param->latency, param->time_out);
2542 if (ret != BLUETOOTH_ERROR_NONE)
2543 BT_ERR("Unable to set le connection parameter");
2546 BT_INFO("Set the default interval");
2548 bluetooth_le_connection_param_t param = { 0 };
2549 _bt_get_le_connection_parameter(
2550 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2553 ret = __bt_le_set_conn_parameter(dev->address,
2554 param.interval_min, param.interval_max,
2555 param.latency, param.timeout);
2556 if (ret == BLUETOOTH_ERROR_NONE) {
2557 dev->interval_min = param.interval_min;
2558 dev->interval_max = param.interval_max;
2567 void _bt_add_le_connected_dev_info(const char *address)
2569 bt_connected_le_dev_t *dev = NULL;
2574 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2575 dev->address = g_strdup(address);
2577 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2582 void _bt_remove_le_connected_dev_info(const char *address)
2584 bt_connected_le_dev_t *dev = NULL;
2589 dev = __bt_get_le_connected_dev_info(address);
2593 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2594 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2595 g_free(dev->address);
2601 int _bt_le_conn_update(const char *sender,
2602 unsigned char *device_address,
2603 float interval_min, float interval_max,
2604 guint16 latency, guint16 time_out)
2606 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2607 guint32 min_supervision_to;
2608 bt_connected_le_dev_t *dev = NULL;
2609 bt_le_conn_param_t *param = NULL;
2610 gboolean is_removed = FALSE;
2611 int ret = BLUETOOTH_ERROR_NONE;
2613 BT_CHECK_PARAMETER(device_address, return);
2615 BT_DBG("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2616 sender, interval_min, interval_max, latency, time_out);
2618 if (interval_min > interval_max ||
2619 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2620 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2621 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2625 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2626 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2627 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2631 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2632 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2637 * The Supervision_Timeout in milliseconds shall be larger than
2638 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2639 * where Conn_Interval_Max is given in milliseconds.
2641 min_supervision_to = (1 + latency) * interval_max * 2;
2642 if (time_out <= min_supervision_to) {
2643 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2647 _bt_convert_addr_type_to_string(address, device_address);
2648 BT_DBG("Remote device address: %s", address);
2650 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2652 dev = __bt_get_le_connected_dev_info(address);
2654 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2658 if (g_slist_length(dev->senders) == 1)
2661 param = dev->senders->data;
2663 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2665 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2666 BT_DBG("Skip due to same interval");
2670 interval_min = param->interval_min;
2671 interval_max = param->interval_max;
2675 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2677 if (ret != BLUETOOTH_ERROR_NONE) {
2678 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2682 dev->interval_min = interval_min;
2683 dev->interval_max = interval_max;
2689 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2690 bluetooth_device_pin_code_t *pin_code)
2692 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2694 bt_pin_code_info_t *pin_info = NULL;
2696 BT_CHECK_PARAMETER(device_address, return);
2697 BT_CHECK_PARAMETER(pin_code, return);
2698 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2699 BLUETOOTH_ERROR_NO_RESOURCES);
2701 _bt_convert_addr_type_to_string(address, device_address->addr);
2703 for (l = pin_info_list; l != NULL; l = l->next) {
2706 if (g_strcmp0(pin_info->address, address) == 0) {
2707 g_free(pin_info->pin_code);
2708 pin_info->pin_code = g_strdup(pin_code->pin_code);
2709 return BLUETOOTH_ERROR_NONE;
2713 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2714 pin_info->address = g_strdup(address);
2715 pin_info->pin_code = g_strdup(pin_code->pin_code);
2716 pin_info_list = g_slist_append(pin_info_list, pin_info);
2718 return BLUETOOTH_ERROR_NONE;
2721 gint __bt_compare_address(gpointer *a, gpointer *b)
2723 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2724 char *address = (char *)b;
2725 return g_strcmp0(pin_info->address, address);
2728 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2730 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2732 bt_pin_code_info_t *pin_info = NULL;
2734 BT_CHECK_PARAMETER(device_address, return);
2736 _bt_convert_addr_type_to_string(address, device_address->addr);
2738 l = g_slist_find_custom(pin_info_list, address,
2739 (GCompareFunc)__bt_compare_address);
2743 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2744 g_free(pin_info->address);
2745 g_free(pin_info->pin_code);
2749 return BLUETOOTH_ERROR_NONE;
2752 int _bt_get_device_pin_code(const char *address, char *pin_code)
2756 BT_CHECK_PARAMETER(address, return);
2757 BT_CHECK_PARAMETER(pin_code, return);
2759 for (l = pin_info_list; l != NULL; l = l->next) {
2760 bt_pin_code_info_t *pin_info = l->data;
2762 if (g_strcmp0(pin_info->address, address) == 0) {
2763 g_strlcpy(pin_code, pin_info->pin_code,
2764 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2766 return BLUETOOTH_ERROR_NONE;
2770 return BLUETOOTH_ERROR_NOT_FOUND;
2773 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2774 bluetooth_le_connection_param_t *param)
2777 return BLUETOOTH_ERROR_INVALID_PARAM;
2779 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2780 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2781 return BLUETOOTH_ERROR_INVALID_PARAM;
2783 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2786 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2787 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2788 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2789 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2790 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2793 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2794 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2795 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2796 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2797 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2800 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2801 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2802 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2803 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2804 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2808 BT_ERR("Unhandled mode : %d", mode);
2812 return BLUETOOTH_ERROR_NONE;
2815 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2816 guint trusted_profile_flag, guint *trusted)
2822 case TRUSTED_PROFILE_PBAP:
2823 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2824 trust_profile = trusted_profile_flag & (1 << 1);
2826 return BLUETOOTH_ERROR_NOT_SUPPORT;
2828 case TRUSTED_PROFILE_MAP:
2829 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2830 trust_profile = trusted_profile_flag & (1 << 3);
2832 return BLUETOOTH_ERROR_NOT_SUPPORT;
2834 case TRUSTED_PROFILE_SAP:
2835 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2836 trust_profile = trusted_profile_flag & (1 << 5);
2838 return BLUETOOTH_ERROR_NOT_SUPPORT;
2840 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2841 *trusted = trusted_profile_flag;
2842 return BLUETOOTH_ERROR_NONE;
2844 return BLUETOOTH_ERROR_NOT_SUPPORT;
2850 return BLUETOOTH_ERROR_NONE;
2853 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2854 guint restricted_profile_flag, guint *restricted)
2856 int restrict_profile;
2857 *restricted = FALSE;
2860 case RESTRICTED_PROFILE_HFP_HS:
2861 restrict_profile = restricted_profile_flag & (1 << 0);
2863 case RESTRICTED_PROFILE_A2DP:
2864 restrict_profile = restricted_profile_flag & (1 << 2);
2867 return BLUETOOTH_ERROR_NOT_SUPPORT;
2870 if (restrict_profile)
2873 return BLUETOOTH_ERROR_NONE;
2876 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2879 case TRUSTED_PROFILE_PBAP:
2880 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2881 case TRUSTED_PROFILE_MAP:
2882 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2883 case TRUSTED_PROFILE_SAP:
2884 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2885 case TRUSTED_PROFILE_ALL:
2892 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2895 case RESTRICTED_PROFILE_HFP_HS:
2896 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2897 case RESTRICTED_PROFILE_A2DP:
2898 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2904 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2906 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2907 return TRUSTED_PROFILE_PBAP;
2908 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2909 return TRUSTED_PROFILE_MAP;
2910 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2911 return TRUSTED_PROFILE_SAP;
2913 return 0; /* 0 - Unknown Profile */
2916 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2917 bluetooth_trusted_profile_t profile, gboolean trust)
2919 int ret = BLUETOOTH_ERROR_NONE;
2920 GDBusConnection *conn;
2922 GError *error = NULL;
2923 char *device_path = NULL;
2925 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2928 BT_CHECK_PARAMETER(bd_addr, return);
2929 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2930 bd_addr->addr[0], bd_addr->addr[1],
2931 bd_addr->addr[2], bd_addr->addr[3],
2932 bd_addr->addr[4], bd_addr->addr[5],
2935 conn = _bt_gdbus_get_system_gconn();
2936 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2938 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2940 device_path = _bt_get_device_object_path(address);
2941 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2943 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2944 NULL, BT_BLUEZ_NAME, device_path,
2945 BT_DEVICE_INTERFACE, NULL, NULL);
2947 g_free(device_path);
2948 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2950 uuid = _bt_get_trusted_profile_uuid(profile);
2952 g_object_unref(proxy);
2953 return BLUETOOTH_ERROR_NOT_SUPPORT;
2956 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2957 g_variant_new("(sb)", uuid, trust),
2958 G_DBUS_CALL_FLAGS_NONE, -1,
2960 g_object_unref(proxy);
2962 if (reply == NULL) {
2963 BT_ERR("Failed to Set Profile Trusted");
2964 ret = BLUETOOTH_ERROR_INTERNAL;
2966 BT_ERR("Error %s[%s]", error->message, address);
2967 g_error_free(error);
2971 g_variant_unref(reply);
2978 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2979 bluetooth_trusted_profile_t profile, guint *trust)
2981 int ret = BLUETOOTH_ERROR_NONE;
2982 GDBusConnection *conn;
2984 GError *error = NULL;
2985 char *device_path = NULL;
2986 guint trusted_profile_flag;
2987 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2990 BT_CHECK_PARAMETER(bd_addr, return);
2991 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2992 bd_addr->addr[0], bd_addr->addr[1],
2993 bd_addr->addr[2], bd_addr->addr[3],
2994 bd_addr->addr[4], bd_addr->addr[5],
2997 conn = _bt_gdbus_get_system_gconn();
2998 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3000 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3002 device_path = _bt_get_device_object_path(address);
3003 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3005 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3006 NULL, BT_BLUEZ_NAME, device_path,
3007 BT_PROPERTIES_INTERFACE, NULL, NULL);
3009 g_free(device_path);
3010 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3012 reply = g_dbus_proxy_call_sync(proxy, "Get",
3013 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3014 G_DBUS_CALL_FLAGS_NONE, -1,
3016 g_object_unref(proxy);
3018 if (reply == NULL) {
3019 BT_ERR("Failed to Get Profile Trusted");
3020 ret = BLUETOOTH_ERROR_INTERNAL;
3022 BT_ERR("Error %s[%s]", error->message, address);
3023 g_error_free(error);
3028 g_variant_get(reply, "(v)", &temp);
3029 trusted_profile_flag = g_variant_get_uint32(temp);
3030 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3032 ret = _bt_get_trusted_profile_from_flag(profile,
3033 trusted_profile_flag, trust);
3034 g_variant_unref(temp);
3035 g_variant_unref(reply);
3038 BT_DBG("TRUST %d", *trust);
3042 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3043 bluetooth_restricted_profile_t profile, gboolean restricted)
3045 int ret = BLUETOOTH_ERROR_NONE;
3046 GDBusConnection *conn;
3048 GError *error = NULL;
3049 char *device_path = NULL;
3051 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3054 BT_CHECK_PARAMETER(bd_addr, return);
3055 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3056 bd_addr->addr[0], bd_addr->addr[1],
3057 bd_addr->addr[2], bd_addr->addr[3],
3058 bd_addr->addr[4], bd_addr->addr[5],
3059 profile, restricted);
3061 conn = _bt_gdbus_get_system_gconn();
3062 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3064 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3066 device_path = _bt_get_device_object_path(address);
3067 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3069 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3070 NULL, BT_BLUEZ_NAME, device_path,
3071 BT_DEVICE_INTERFACE, NULL, NULL);
3073 g_free(device_path);
3074 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3076 uuid = _bt_get_restricted_profile_uuid(profile);
3078 g_object_unref(proxy);
3079 return BLUETOOTH_ERROR_NOT_SUPPORT;
3082 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3083 g_variant_new("(sb)", uuid, restricted),
3084 G_DBUS_CALL_FLAGS_NONE, -1,
3086 g_object_unref(proxy);
3088 if (reply == NULL) {
3089 BT_ERR("Failed to Set Profile Restricted");
3090 ret = BLUETOOTH_ERROR_INTERNAL;
3092 BT_ERR("Error %s[%s]", error->message, address);
3093 g_error_free(error);
3097 g_variant_unref(reply);
3104 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3105 bluetooth_restricted_profile_t profile, guint *restricted)
3107 int ret = BLUETOOTH_ERROR_NONE;
3108 GDBusConnection *conn;
3110 GError *error = NULL;
3111 char *device_path = NULL;
3112 guint restricted_profile_flag;
3113 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3116 BT_CHECK_PARAMETER(bd_addr, return);
3117 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3118 bd_addr->addr[0], bd_addr->addr[1],
3119 bd_addr->addr[2], bd_addr->addr[3],
3120 bd_addr->addr[4], bd_addr->addr[5],
3121 profile, *restricted);
3123 conn = _bt_gdbus_get_system_gconn();
3124 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3126 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3128 device_path = _bt_get_device_object_path(address);
3129 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3131 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3132 NULL, BT_BLUEZ_NAME, device_path,
3133 BT_PROPERTIES_INTERFACE, NULL, NULL);
3135 g_free(device_path);
3136 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3138 reply = g_dbus_proxy_call_sync(proxy, "Get",
3139 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3140 G_DBUS_CALL_FLAGS_NONE, -1,
3142 g_object_unref(proxy);
3144 if (reply == NULL) {
3145 BT_ERR("Failed to Get Profile Restricted");
3146 ret = BLUETOOTH_ERROR_INTERNAL;
3148 BT_ERR("Error %s[%s]", error->message, address);
3149 g_error_free(error);
3154 g_variant_get(reply, "(v)", &temp);
3155 restricted_profile_flag = g_variant_get_uint32(temp);
3156 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3158 ret = _bt_get_restricted_profile_from_flag(profile,
3159 restricted_profile_flag, restricted);
3160 g_variant_unref(temp);
3161 g_variant_unref(reply);
3164 BT_DBG("TRUST %d", *restricted);
3168 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3172 GVariant *out_param1;
3173 request_info_t *req_info;
3174 GVariant *val = NULL;
3175 GVariant *param = NULL;
3178 bluetooth_device_address_t device_addr = { {0} };
3179 int result = BLUETOOTH_ERROR_NONE;
3182 val = g_dbus_proxy_call_finish(proxy, res, &err);
3184 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3185 if (req_info == NULL) {
3186 BT_ERR("req_info == NULL");
3187 g_object_unref(proxy);
3188 att_mtu_req_info->device_proxy = NULL;
3193 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3195 if (g_strrstr(err->message, "NotSupported")) {
3196 BT_INFO("Connection Not Supported");
3197 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3198 } else if (g_strrstr(err->message, "NotConnected")) {
3199 BT_INFO("Not connected");
3200 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3201 } else if (g_strrstr(err->message, "InvalidArguments")) {
3202 BT_INFO("Not connected");
3203 result = BLUETOOTH_ERROR_INVALID_PARAM;
3205 BT_DBG("Default case");
3206 result = BLUETOOTH_ERROR_INTERNAL;
3210 g_object_unref(proxy);
3211 att_mtu_req_info->device_proxy = NULL;
3213 if (result != BLUETOOTH_ERROR_NONE)
3217 g_variant_get(val, "(qy)", &mtu, &status);
3218 g_variant_unref(val);
3221 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3223 param = g_variant_new("(isqy)",
3225 att_mtu_req_info->addr,
3229 /* Send the event to application */
3230 _bt_send_event(BT_DEVICE_EVENT,
3231 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3235 if (req_info->context == NULL)
3238 _bt_convert_addr_string_to_type(device_addr.addr,
3239 (const char *)att_mtu_req_info->addr);
3241 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3242 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3243 g_dbus_method_invocation_return_value(req_info->context,
3244 g_variant_new("(iv)", result, out_param1));
3246 _bt_delete_request_list(req_info->req_id);
3251 g_free(att_mtu_req_info->addr);
3252 g_free(att_mtu_req_info);
3253 att_mtu_req_info = NULL;
3258 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3261 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3262 gchar *device_path = NULL;
3263 GDBusProxy *adapter_proxy;
3264 GDBusProxy *device_proxy;
3265 GDBusConnection *conn;
3266 int ret = BLUETOOTH_ERROR_NONE;
3268 BT_CHECK_PARAMETER(device_address, return);
3270 if (att_mtu_req_info) {
3271 BT_ERR("ATT MTU request in progress");
3272 return BLUETOOTH_ERROR_DEVICE_BUSY;
3275 conn = _bt_gdbus_get_system_gconn();
3276 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3278 adapter_proxy = _bt_get_adapter_proxy();
3279 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3281 _bt_convert_addr_type_to_string(address, device_address->addr);
3283 BT_DBG("Remote device address: %s", address);
3285 device_path = _bt_get_device_object_path(address);
3287 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3289 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3290 NULL, BT_BLUEZ_NAME,
3291 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3292 g_free(device_path);
3293 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3295 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3296 att_mtu_req_info->addr = (char *)g_strdup(address);
3297 att_mtu_req_info->req_id = request_id;
3298 att_mtu_req_info->device_proxy = device_proxy;
3300 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3301 g_variant_new("(q)", mtu),
3302 G_DBUS_CALL_FLAGS_NONE,
3303 BT_MAX_DBUS_TIMEOUT,
3305 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3311 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3314 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3315 char *object_path = NULL;
3317 GDBusProxy *device_proxy;
3318 GError *error = NULL;
3320 GDBusConnection *conn;
3321 GVariant *result = NULL;
3323 BT_CHECK_PARAMETER(device_address, return);
3325 conn = _bt_gdbus_get_system_gconn();
3326 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3328 _bt_convert_addr_type_to_string(address, device_address->addr);
3330 object_path = _bt_get_device_object_path(address);
3331 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3333 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3334 NULL, BT_BLUEZ_NAME, object_path,
3335 BT_PROPERTIES_INTERFACE, NULL, NULL);
3336 g_free(object_path);
3337 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3339 result = g_dbus_proxy_call_sync(device_proxy, "Get",
3340 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "AttMtu"),
3341 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3343 ERR("DBus Error : %s", error->message);
3344 g_clear_error(&error);
3345 g_object_unref(device_proxy);
3346 return BLUETOOTH_ERROR_INTERNAL;
3348 g_variant_get(result, "(v)", &value);
3349 *mtu = g_variant_get_uint16(value);
3350 BT_DBG("ATT MTU : %d", *mtu);
3351 g_variant_unref(value);
3352 g_variant_unref(result);
3355 g_object_unref(device_proxy);
3356 return BLUETOOTH_ERROR_NOT_CONNECTED;
3360 g_object_unref(device_proxy);
3361 return BLUETOOTH_ERROR_NONE;
3364 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3365 bluetooth_device_address_t *id_address)
3367 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3368 gchar *device_path = NULL;
3369 const gchar *idaddress = NULL;
3370 GDBusProxy *device_proxy;
3371 GError *error = NULL;
3372 GVariant *result = NULL;
3373 GDBusConnection *conn;
3374 int ret = BLUETOOTH_ERROR_NONE;
3376 BT_CHECK_PARAMETER(device_address, return);
3378 conn = _bt_gdbus_get_system_gconn();
3379 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3381 _bt_convert_addr_type_to_string(address, device_address->addr);
3383 device_path = _bt_get_device_object_path(address);
3384 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3386 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3387 NULL, BT_BLUEZ_NAME,
3388 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3389 g_free(device_path);
3390 if (!device_proxy) {
3391 BT_ERR("Unable to get proxy");
3392 return BLUETOOTH_ERROR_INTERNAL;
3395 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress", NULL,
3396 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
3397 if (result == NULL) {
3398 BT_ERR("Failed to get device ID address");
3400 ret = BLUETOOTH_ERROR_INTERNAL;
3401 if (error != NULL) {
3402 g_dbus_error_strip_remote_error(error);
3403 BT_ERR("Error occured in Proxy call [%s]", error->message);
3404 if (g_strcmp0(error->message, "Does Not Exist") == 0)
3405 ret = BLUETOOTH_ERROR_NOT_PAIRED;
3406 g_error_free(error);
3408 g_object_unref(device_proxy);
3412 g_variant_get(result , "(&s)", &idaddress);
3414 DBG_SECURE("ID Address : %s", idaddress);
3415 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3417 g_variant_unref(result);
3418 g_object_unref(device_proxy);
3423 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3425 GapAgentPrivate *agent = _bt_get_adapter_agent();
3426 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3428 if (authentication_reply)
3429 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3431 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3433 BT_DBG("BT_PASSKEY_REPLY");
3434 return BLUETOOTH_ERROR_NONE;
3437 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3439 GapAgentPrivate *agent = _bt_get_adapter_agent();
3440 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3442 if (confirmation_reply)
3443 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3445 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3447 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3448 return BLUETOOTH_ERROR_NONE;