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"
40 #include "bt-service-pbap.h"
42 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
43 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
44 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
46 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
47 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
49 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
50 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
51 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
52 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
53 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
55 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
59 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
63 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
67 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
69 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
76 GDBusProxy *device_proxy;
77 GDBusProxy *adapter_proxy;
79 unsigned short conn_type;
80 gboolean bonding_wo_discovery;
90 bluetooth_device_address_t bd_addr;
91 gboolean auto_connect;
92 } bt_pending_le_conn_info_s;
94 gboolean is_device_creating;
95 bt_funcion_data_t *bonding_info;
96 bt_funcion_data_t *searching_info;
97 bt_funcion_data_t *att_mtu_req_info;
99 static GSList *pin_info_list = NULL;
100 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
101 static guint pending_le_conn_timer_id = 0;
103 /* This HID Mouse does not support pairing precedure. need to skip it. */
104 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
106 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
109 static int __bt_retry_bond(void);
112 /*static void __bt_decline_pair_request()
114 GVariant *out_param1;
115 GVariant *out_param2;
116 request_info_t *req_info;
117 bluetooth_device_info_t dev_info;
118 bt_remote_dev_info_t *remote_dev_info;
119 GVariant *uuid_list, *manufacture_data;
124 req_info = _bt_get_request_info(bonding_info->req_id);
125 if (req_info == NULL) {
126 BT_ERR("req_info == NULL");
129 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
131 BT_DBG("bonding_info is NULL");
136 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
137 remote_dev_info->uuids, remote_dev_info->uuid_count,
140 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
141 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
144 param = g_variant_new("isunsbuba{s}na{y})",
145 bonding_info->result,
147 remote_dev_info->class,
148 remote_dev_info->rssi,
149 remote_dev_info->name,
150 remote_dev_info->paired,
151 remote_dev_info->connected,
152 remote_dev_info->trust,
154 remote_dev_info->manufacturer_data_len,
158 //Send the event to application
159 if (remote_dev_info != NULL) {
160 _bt_send_event(BT_ADAPTER_EVENT,
161 BLUETOOTH_EVENT_BONDING_FINISHED,
164 _bt_free_device_info(remote_dev_info);
167 if (req_info->context == NULL)
170 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
171 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
174 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
175 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
177 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
178 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
181 g_dbus_method_invocation_return_value(req_info->context,
182 g_variant_new("@ayi", out_param1, out_param2));
185 _bt_delete_request_list(req_info->req_id);
188 g_free(bonding_info->addr);
189 g_free(bonding_info);
195 #ifdef TIZEN_PROFILE_WEARABLE
196 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
200 retv_if(user_data == NULL, FALSE);
202 b = (bundle *)user_data;
204 ret = syspopup_launch("bt-syspopup", b);
206 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
208 BT_DBG("Hurray!!! Finally Popup launched");
211 return (ret < 0) ? TRUE : FALSE;
214 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
219 GDBusConnection *conn;
221 conn = _bt_gdbus_get_system_gconn();
229 bundle_add(b, "event-type", "unable-to-pairing");
231 if (result == BLUETOOTH_ERROR_TIMEOUT)
232 bundle_add(b, "error", "timeout");
233 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
234 bundle_add(b, "error", "authfailed");
236 bundle_add(b, "error", "error");
238 ret = syspopup_launch("bt-syspopup", b);
240 BT_ERR("Popup launch failed...retry %d \n", ret);
241 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
252 gboolean _bt_is_device_creating(void)
254 return is_device_creating;
257 gboolean _bt_is_bonding_device_address(const char *address)
259 if (bonding_info == NULL || bonding_info->addr == NULL)
262 if (g_strcmp0(bonding_info->addr, address) == 0) {
263 BT_DBG("[%s] is bonding device", address);
267 BT_DBG("[%s] is NOT bonding device", address);
271 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
273 ret_if(bonding_info == NULL);
274 bonding_info->is_autopair = is_autopair;
277 void __bt_cancel_search_service_done(void)
279 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
280 request_info_t *req_info;
281 bluetooth_device_info_t dev_info;
282 GVariant *out_param1;
284 ret_if(searching_info == NULL);
286 req_info = _bt_get_request_info(searching_info->req_id);
287 if (req_info == NULL) {
288 BT_ERR("req_info == NULL");
292 if (req_info->context == NULL)
295 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
296 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
297 searching_info->addr);
299 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
300 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
303 g_dbus_method_invocation_return_value(req_info->context,
304 g_variant_new("(iv)", result, out_param1));
306 _bt_delete_request_list(req_info->req_id);
310 g_free(searching_info->addr);
311 g_free(searching_info);
312 searching_info = NULL;
315 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
317 ret_if(value == NULL);
318 ret_if(info == NULL);
320 gsize uuid_count = 0;
322 info->uuids = g_variant_dup_strv(value, &uuid_count);
323 info->uuid_count = (unsigned int)uuid_count;
325 BT_DBG("uuid count : %d", uuid_count);
328 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
330 char *object_path = NULL;
331 bt_remote_dev_info_t *dev_info;
333 retv_if(address == NULL, NULL);
335 object_path = _bt_get_device_object_path(address);
336 retv_if(object_path == NULL, NULL);
338 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
344 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
345 const char *object_path)
347 bt_remote_dev_info_t *dev_info;
348 GDBusProxy *adapter_proxy;
349 GDBusProxy *device_proxy;
354 GDBusConnection *conn;
355 GError *error = NULL;
356 GVariant *result = NULL;
357 GVariantIter *value_iter;
361 adapter_proxy = _bt_get_adapter_proxy();
362 retv_if(adapter_proxy == NULL, NULL);
364 retv_if(object_path == NULL, NULL);
366 conn = _bt_gdbus_get_system_gconn();
368 BT_ERR("conn == NULL");
372 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
373 NULL, BT_BLUEZ_NAME, object_path,
374 BT_PROPERTIES_INTERFACE, NULL, NULL);
376 retv_if(device_proxy == NULL, NULL);
378 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
379 g_variant_new("(s)", BT_DEVICE_INTERFACE),
380 G_DBUS_CALL_FLAGS_NONE,
385 g_object_unref(device_proxy);
387 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
389 if (result != NULL) {
390 g_variant_get(result , "(@a{sv})", &value);
391 g_variant_unref(result);
393 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
395 g_variant_get(tmp_value, "s", &name);
396 g_variant_unref(tmp_value);
398 DBG_SECURE("Alias Name [%s]", name);
400 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
401 g_variant_get(tmp_value, "s", &name);
402 g_variant_unref(tmp_value);
405 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
407 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
408 g_variant_unref(tmp_value);
410 dev_info->is_alias_set = FALSE;
412 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
414 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
416 dev_info->class = g_variant_get_uint32(tmp_value);
417 g_variant_unref(tmp_value);
421 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
423 dev_info->connected = g_variant_get_byte(tmp_value);
424 g_variant_unref(tmp_value);
426 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
427 BT_DBG("connected link : %d", dev_info->connected);
429 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
431 dev_info->trust = g_variant_get_boolean(tmp_value);
432 g_variant_unref(tmp_value);
434 dev_info->trust = FALSE;
436 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
438 dev_info->paired = g_variant_get_boolean(tmp_value);
439 g_variant_unref(tmp_value);
441 dev_info->paired = FALSE;
443 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
445 dev_info->rssi = g_variant_get_int16(tmp_value);
446 g_variant_unref(tmp_value);
450 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
452 dev_info->addr_type = g_variant_get_byte(tmp_value);
453 g_variant_unref(tmp_value);
455 dev_info->addr_type = 0;
457 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
459 __bt_get_uuids(tmp_value, dev_info);
460 g_variant_unref(tmp_value);
463 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
465 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
466 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
467 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
468 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
470 g_variant_unref(tmp_value);
472 dev_info->manufacturer_data_len = 0;
474 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
476 if ((dev_info->manufacturer_data_len == 0) ||
477 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
478 BT_ERR("manufacturer data length doesn't match");
479 dev_info->manufacturer_data_len = 0;
480 dev_info->manufacturer_data = NULL;
482 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
483 g_variant_get(tmp_value, "ay", &value_iter);
484 while (g_variant_iter_loop(value_iter, "y", &m_value))
485 dev_info->manufacturer_data[i++] = m_value;
487 g_variant_unref(tmp_value);
489 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
490 dev_info->manufacturer_data_len = 0;
491 dev_info->manufacturer_data = NULL;
494 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
495 g_variant_get(tmp_value, "s", &address);
496 g_variant_unref(tmp_value);
498 dev_info->address = g_strdup(address);
499 dev_info->name = g_strdup(name);
501 g_variant_unref(value);
503 BT_ERR("result is NULL\n");
511 char *_bt_get_bonded_device_name(char *address)
513 bluetooth_device_address_t device_address = { {0} };
514 bluetooth_device_info_t dev_info;
516 retv_if(address == NULL, strdup(""));
518 _bt_convert_addr_string_to_type(device_address.addr, address);
520 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
522 _bt_get_bonded_device_info(&device_address, &dev_info);
524 return g_strdup(dev_info.device_name.name);
527 static gboolean __ignore_auto_pairing_request(const char *address)
532 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
543 /* Get the LAP(Lower Address part) */
544 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
546 snprintf(lap_address, sizeof(lap_address), ",%s", address);
548 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
551 BT_ERR("fopen failed \n");
555 fseek(fp, 0, SEEK_END);
560 BT_ERR("Get file size failed \n");
565 buffer = g_malloc0(sizeof(char) * size);
566 result = fread((char *)buffer, 1, size, fp);
568 if (result != size) {
569 BT_ERR("Read Error\n");
574 BT_DBG("Buffer = %s\n", buffer);
576 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
582 /* Write the data and insert new device data */
583 for (i = 0; lines[i] != NULL; i++) {
584 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
585 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
587 lines[i] = temp_buffer;
590 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
592 /* Fix : NULL_RETURNS */
593 retv_if(buffer == NULL, FALSE);
595 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
598 BT_ERR("fopen failed \n");
603 BT_DBG("Buffer = %s\n", buffer);
604 fwrite(buffer, 1, strlen(buffer), fp);
614 static int __bt_cancel_bonding(void)
616 BT_CHECK_PARAMETER(bonding_info, return);
617 BT_CHECK_PARAMETER(bonding_info->addr, return);
618 /* First Cancel the ongoing pairing in bluez otherwise if we send
619 * pair request bluez will send inprogress and we again retry bond so
620 * this cycle continues */
622 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
623 NULL, G_DBUS_CALL_FLAGS_NONE,
626 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
628 return BLUETOOTH_ERROR_PARING_FAILED;
630 return BLUETOOTH_ERROR_NONE;
633 static int __bt_retry_bond(void)
635 BT_CHECK_PARAMETER(bonding_info, return);
636 BT_CHECK_PARAMETER(bonding_info->addr, return);
638 is_device_creating = TRUE;
640 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
641 g_variant_new("(y)", bonding_info->conn_type),
642 G_DBUS_CALL_FLAGS_NONE,
645 (GAsyncReadyCallback)__bt_bond_device_cb,
648 return BLUETOOTH_ERROR_NONE;
652 static int __bt_remove_and_bond(void)
654 GDBusProxy *adapter_proxy;
655 GVariant *result = NULL;
657 char *device_path = NULL;
659 BT_CHECK_PARAMETER(bonding_info, return);
660 BT_CHECK_PARAMETER(bonding_info->addr, return);
662 adapter_proxy = _bt_get_adapter_proxy();
663 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
665 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
666 g_variant_new("(s)", bonding_info->addr),
667 G_DBUS_CALL_FLAGS_NONE,
672 return BLUETOOTH_ERROR_INTERNAL;
674 g_variant_get(result , "(o)", &device_path);
675 g_variant_unref(result);
677 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
679 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
680 g_variant_new("(o)", device_path),
681 G_DBUS_CALL_FLAGS_NONE,
687 BT_ERR("UnpairDevice Fail: %s", err->message);
689 return BLUETOOTH_ERROR_INTERNAL;
692 return __bt_retry_bond();
695 static int __bt_cancel_and_bond(void)
697 int ret = BLUETOOTH_ERROR_NONE;
699 ret = _bt_agent_reply_cancellation();
700 if (ret != BLUETOOTH_ERROR_NONE) {
701 BT_ERR("Fail to call reply cancellation");
704 return __bt_retry_bond();
708 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
711 int result = BLUETOOTH_ERROR_NONE;
714 GVariant *out_param1;
715 request_info_t *req_info;
716 bluetooth_device_info_t dev_info;
717 bt_remote_dev_info_t *remote_dev_info;
718 GVariant *manufacture_data;
721 /* Terminate ALL system popup */
722 syspopup_destroy_all();
724 reply = g_dbus_proxy_call_finish(proxy, res, &err);
726 g_variant_unref(reply);
728 is_device_creating = FALSE;
730 if (bonding_info == NULL) {
732 BT_ERR("bonding_info == NULL");
738 req_info = _bt_get_request_info(bonding_info->req_id);
739 if (req_info == NULL) {
740 BT_ERR("req_info == NULL");
745 g_dbus_error_strip_remote_error(err);
746 BT_ERR("Error occured in CreateBonding [%s]", err->message);
748 if (g_strrstr(err->message, "Already Exists")) {
749 BT_INFO("Existing Bond, remove and retry");
750 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
752 result = BLUETOOTH_ERROR_PARING_FAILED;
753 } else if (_bt_agent_is_canceled() ||
754 g_strrstr(err->message, "Authentication Canceled")) {
755 BT_INFO("Cancelled by USER");
756 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
757 } else if (g_strrstr(err->message, "Authentication Rejected")) {
759 result = BLUETOOTH_ERROR_ACCESS_DENIED;
760 } else if (g_strrstr(err->message, "In Progress")) {
761 BT_INFO("Bond in progress, cancel and retry");
762 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
764 result = BLUETOOTH_ERROR_PARING_FAILED;
765 } else if (g_strrstr(err->message, "Authentication Failed")) {
766 BT_INFO("Authentication Failed");
767 if (bonding_info->is_autopair == TRUE) {
768 _bt_set_autopair_status_in_bonding_info(FALSE);
769 __ignore_auto_pairing_request(bonding_info->addr);
771 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
772 } else if (g_strrstr(err->message, "Page Timeout")) {
773 BT_INFO("Page Timeout");
774 /* This is the special case
775 As soon as call bluetooth_bond_device, try to cancel bonding.
776 In this case, before completing to call 'CreatePairedDevice' method
777 the procedure is stopped. So 'Cancle' error is not return.
779 result = BLUETOOTH_ERROR_HOST_DOWN;
780 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
781 BT_INFO("Cancel already running bonding");
782 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
783 BT_INFO("Error while Cancelling bonding");
784 /* we need to unref proxy so continue */
786 result = BLUETOOTH_ERROR_INTERNAL;
787 } else if (g_strrstr(err->message, "Connection Timeout")) {
788 /* Pairing request timeout */
789 result = BLUETOOTH_ERROR_TIMEOUT;
790 } else if (g_strrstr(err->message, "Authentication Timeout")) {
791 /* Pairing request timeout */
792 result = BLUETOOTH_ERROR_TIMEOUT;
794 BT_DBG("Default case");
795 result = BLUETOOTH_ERROR_PARING_FAILED;
799 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
800 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
801 result == BLUETOOTH_ERROR_TIMEOUT ||
802 result == BLUETOOTH_ERROR_HOST_DOWN) {
803 #ifdef TIZEN_PROFILE_WEARABLE
804 int is_sw_running = 0;
806 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
807 BT_ERR("vconf_get_int for setup wizard state failed");
810 __bt_launch_unable_to_pairing_syspopup(result);
812 BT_ERR("Unable to pair");
814 bonding_info->result = result;
817 g_object_unref(proxy);
818 bonding_info->device_proxy = NULL;
820 if (result != BLUETOOTH_ERROR_NONE) {
821 if (bonding_info->bonding_wo_discovery) {
822 GDBusProxy *adapter_proxy;
823 GVariant *ret = NULL;
824 GError *error = NULL;
827 BT_ERR("Bond was tried without discovery. Remove it");
829 adapter_proxy = _bt_get_adapter_proxy();
830 if (adapter_proxy == NULL) {
831 BT_ERR("Cannot get adapter_proxy");
835 device_path = _bt_get_device_object_path(bonding_info->addr);
836 if (device_path == NULL) {
837 BT_ERR("Cannot get device path");
841 ret = g_dbus_proxy_call_sync(adapter_proxy,
843 g_variant_new("(o)", device_path),
844 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
847 BT_ERR("RemoveDevice Fail: %s", error->message);
848 g_clear_error(&error);
852 g_variant_unref(ret);
860 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
861 if (!remote_dev_info)
864 GVariant *uuids = NULL;
865 GVariantBuilder *builder = NULL;
867 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
868 for (i = 0; i < remote_dev_info->uuid_count; i++) {
869 g_variant_builder_add(builder, "s",
870 remote_dev_info->uuids[i]);
872 uuids = g_variant_new("as", builder);
873 g_variant_builder_unref(builder);
874 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
875 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
878 param = g_variant_new("(isunsbub@asn@ay)",
881 remote_dev_info->class,
882 remote_dev_info->rssi,
883 remote_dev_info->name,
884 remote_dev_info->paired,
885 remote_dev_info->connected,
886 remote_dev_info->trust,
888 remote_dev_info->manufacturer_data_len,
892 /* Send the event to application */
893 _bt_send_event(BT_ADAPTER_EVENT,
894 BLUETOOTH_EVENT_BONDING_FINISHED,
897 _bt_free_device_info(remote_dev_info);
900 if (req_info->context == NULL)
903 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
904 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
907 if (_bt_adapter_get_status() != BT_ACTIVATED)
908 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
910 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
911 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
913 g_dbus_method_invocation_return_value(req_info->context,
914 g_variant_new("(iv)", result, out_param1));
916 g_variant_unref(out_param1);
918 _bt_delete_request_list(req_info->req_id);
923 _bt_agent_set_canceled(FALSE);
926 g_free(bonding_info->addr);
927 g_free(bonding_info);
931 int _bt_bond_device(int request_id,
932 bluetooth_device_address_t *device_address,
933 unsigned short conn_type, GArray **out_param1)
936 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
937 bluetooth_device_info_t dev_info;
939 GDBusConnection *conn;
940 char *device_path = NULL;
941 GDBusProxy *adapter_proxy;
942 GError *error = NULL;
943 gboolean bonding_wo_discovery = FALSE;
945 BT_CHECK_PARAMETER(device_address, return);
948 BT_ERR("Bonding in progress");
950 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
951 memcpy(dev_info.device_address.addr, device_address->addr,
952 BLUETOOTH_ADDRESS_LENGTH);
954 g_array_append_vals(*out_param1, &dev_info,
955 sizeof(bluetooth_device_info_t));
957 return BLUETOOTH_ERROR_DEVICE_BUSY;
960 conn = _bt_gdbus_get_system_gconn();
961 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
963 _bt_convert_addr_type_to_string(address, device_address->addr);
965 device_path = _bt_get_device_object_path(address);
967 if (device_path == NULL) {
968 BT_ERR("No searched device");
969 GVariant *ret = NULL;
970 adapter_proxy = _bt_get_adapter_proxy();
971 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
973 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
974 g_variant_new("(s)", address),
975 G_DBUS_CALL_FLAGS_NONE,
981 BT_ERR("CreateDevice Fail: %s", error->message);
982 g_clear_error(&error);
985 g_variant_unref(ret);
986 device_path = _bt_get_device_object_path(address);
987 if (device_path == NULL) {
988 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
989 memcpy(dev_info.device_address.addr, device_address->addr,
990 BLUETOOTH_ADDRESS_LENGTH);
992 g_array_append_vals(*out_param1, &dev_info,
993 sizeof(bluetooth_device_info_t));
995 return BLUETOOTH_ERROR_NOT_PAIRED;
997 BT_INFO("device_path is created[%s]", device_path);
999 bonding_wo_discovery = TRUE;
1002 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1003 NULL, BT_BLUEZ_NAME,
1004 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1006 g_free(device_path);
1007 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1009 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1010 bonding_info->addr = g_strdup(address);
1011 bonding_info->req_id = request_id;
1013 bonding_info->device_proxy = proxy;
1014 bonding_info->conn_type = conn_type;
1015 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1017 is_device_creating = TRUE;
1019 g_dbus_proxy_call(proxy, "Pair",
1020 g_variant_new("(y)", conn_type),
1021 G_DBUS_CALL_FLAGS_NONE,
1022 BT_MAX_DBUS_TIMEOUT,
1024 (GAsyncReadyCallback)__bt_bond_device_cb,
1027 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1029 return BLUETOOTH_ERROR_NONE;
1031 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1032 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1033 bonding_info->addr);
1035 g_array_append_vals(*out_param1, &dev_info,
1036 sizeof(bluetooth_device_info_t));
1038 is_device_creating = FALSE;
1040 g_free(bonding_info->addr);
1041 g_free(bonding_info);
1042 bonding_info = NULL;
1044 return BLUETOOTH_ERROR_INTERNAL;*/
1047 int _bt_cancel_bonding(void)
1049 int ret = BLUETOOTH_ERROR_NONE;
1051 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1053 ret = _bt_agent_reply_cancellation();
1054 if (ret != BLUETOOTH_ERROR_NONE) {
1055 BT_ERR("Fail to call reply cancellation");
1059 _bt_agent_set_canceled(TRUE);
1061 return BLUETOOTH_ERROR_NONE;
1064 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1069 GVariant *out_param1;
1070 int result = BLUETOOTH_ERROR_NONE;
1071 bt_funcion_data_t *unbonding_info;
1072 bluetooth_device_info_t dev_info;
1073 request_info_t *req_info;
1075 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1077 g_variant_unref(reply);
1079 unbonding_info = user_data;
1081 if (unbonding_info == NULL) {
1083 BT_ERR("unbonding_info == NULL");
1087 req_info = _bt_get_request_info(unbonding_info->req_id);
1088 if (req_info == NULL) {
1089 BT_ERR("req_info == NULL");
1094 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1095 result = BLUETOOTH_ERROR_INTERNAL;
1098 if (req_info->context == NULL)
1101 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1102 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1103 unbonding_info->addr);
1105 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1106 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1108 g_dbus_method_invocation_return_value(req_info->context,
1109 g_variant_new("(iv)", result, out_param1));
1112 _bt_delete_request_list(req_info->req_id);
1118 if (unbonding_info) {
1119 g_free(unbonding_info->addr);
1120 g_free(unbonding_info);
1124 int _bt_unbond_device(int request_id,
1125 bluetooth_device_address_t *device_address,
1126 GArray **out_param1)
1128 char *device_path = NULL;
1129 bt_funcion_data_t *unbonding_info;
1130 GDBusProxy *adapter_proxy = NULL;
1131 GDBusProxy *device_proxy = NULL;
1132 GDBusConnection *conn;
1133 int result = BLUETOOTH_ERROR_INTERNAL;
1134 bluetooth_device_info_t dev_info;
1135 GError *error = NULL;
1136 GVariant *ret = NULL;
1138 BT_CHECK_PARAMETER(device_address, return);
1140 adapter_proxy = _bt_get_adapter_proxy();
1141 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1143 /* allocate user data so that it can be retrieved in callback */
1144 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1145 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1146 unbonding_info->req_id = request_id;
1148 _bt_convert_addr_type_to_string(unbonding_info->addr,
1149 device_address->addr);
1151 device_path = _bt_get_device_object_path(unbonding_info->addr);
1153 if (device_path == NULL) {
1154 BT_ERR("No paired device");
1155 result = BLUETOOTH_ERROR_NOT_PAIRED;
1159 conn = _bt_gdbus_get_system_gconn();
1161 BT_ERR("conn is NULL");
1162 result = BLUETOOTH_ERROR_INTERNAL;
1166 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1167 NULL, BT_BLUEZ_NAME,
1168 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1170 if (device_proxy != NULL) {
1172 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1173 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1174 G_DBUS_CALL_FLAGS_NONE,
1179 BT_ERR("Getting property failed: [%s]\n", error->message);
1180 g_error_free(error);
1181 result = BLUETOOTH_ERROR_NOT_PAIRED;
1185 BT_ERR("No paired device");
1186 g_object_unref(device_proxy);
1187 result = BLUETOOTH_ERROR_NOT_PAIRED;
1190 g_variant_unref(ret);
1192 g_object_unref(device_proxy);
1195 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1196 g_variant_new("(o)", device_path),
1197 G_DBUS_CALL_FLAGS_NONE,
1198 BT_MAX_DBUS_TIMEOUT,
1200 (GAsyncReadyCallback)__bt_unbond_cb,
1203 g_free(device_path);
1204 return BLUETOOTH_ERROR_NONE;
1207 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1208 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1209 unbonding_info->addr);
1211 g_array_append_vals(*out_param1, &dev_info,
1212 sizeof(bluetooth_device_info_t));
1213 g_free(device_path);
1214 g_free(unbonding_info->addr);
1215 g_free(unbonding_info);
1219 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1224 GVariant *out_param1;
1225 int result = BLUETOOTH_ERROR_NONE;
1226 bluetooth_device_info_t dev_info;
1227 bt_remote_dev_info_t *remote_dev_info;
1228 request_info_t *req_info;
1229 GVariant *uuid_list, *manufacture_data;
1231 GVariantBuilder *builder = NULL;
1234 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1236 g_variant_unref(reply);
1238 g_object_unref(proxy);
1240 if (searching_info == NULL) {
1242 BT_ERR("unbonding_info == NULL");
1246 req_info = _bt_get_request_info(searching_info->req_id);
1247 if (req_info == NULL) {
1248 BT_ERR("req_info == NULL");
1253 g_dbus_error_strip_remote_error(err);
1254 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1256 if (g_strrstr("Operation canceled", err->message))
1257 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1258 else if (g_strrstr("In Progress", err->message))
1259 result = BLUETOOTH_ERROR_IN_PROGRESS;
1260 else if (g_strrstr("Host is down", err->message))
1261 result = BLUETOOTH_ERROR_HOST_DOWN;
1263 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1265 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1266 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1267 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1268 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1269 remote_dev_info->uuid_count > 0) {
1270 result = BLUETOOTH_ERROR_NONE;
1273 _bt_free_device_info(remote_dev_info);
1278 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1279 if (!remote_dev_info)
1283 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1284 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1285 g_variant_builder_add(builder, "s",
1286 remote_dev_info->uuids[i]);
1288 uuid_list = g_variant_new("as", builder);
1289 g_variant_builder_unref(builder);
1290 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1291 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1294 param = g_variant_new("(isunsbub@asn@ay)",
1296 searching_info->addr,
1297 remote_dev_info->class,
1298 remote_dev_info->rssi,
1299 remote_dev_info->name,
1300 remote_dev_info->paired,
1301 remote_dev_info->connected,
1302 remote_dev_info->trust,
1304 remote_dev_info->manufacturer_data_len,
1307 /* Send the event to application */
1308 _bt_send_event(BT_ADAPTER_EVENT,
1309 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1312 _bt_free_device_info(remote_dev_info);
1315 if (req_info->context == NULL)
1318 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1319 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1320 searching_info->addr);
1322 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1323 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1325 g_dbus_method_invocation_return_value(req_info->context,
1326 g_variant_new("(iv)", result, out_param1));
1329 _bt_delete_request_list(req_info->req_id);
1334 if (searching_info) {
1335 g_free(searching_info->addr);
1336 g_free(searching_info);
1337 searching_info = NULL;
1341 int _bt_search_device(int request_id,
1342 bluetooth_device_address_t *device_address)
1344 char *device_path = NULL;
1345 GDBusProxy *device_proxy = NULL;
1346 GDBusConnection *conn;
1348 GDBusProxy *adapter_proxy;
1349 int result = BLUETOOTH_ERROR_INTERNAL;
1351 BT_CHECK_PARAMETER(device_address, return);
1354 BT_ERR("Bonding in progress");
1355 return BLUETOOTH_ERROR_DEVICE_BUSY;
1358 if (searching_info) {
1359 BT_ERR("Service searching in progress");
1360 return BLUETOOTH_ERROR_DEVICE_BUSY;
1363 adapter_proxy = _bt_get_adapter_proxy();
1364 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1366 /* allocate user data so that it can be retrieved in callback */
1367 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1368 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1369 searching_info->req_id = request_id;
1371 _bt_convert_addr_type_to_string(searching_info->addr,
1372 device_address->addr);
1374 conn = _bt_gdbus_get_system_gconn();
1375 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1378 device_path = _bt_get_device_object_path(searching_info->addr);
1380 if (device_path == NULL) {
1381 BT_ERR("No paired device");
1382 result = BLUETOOTH_ERROR_NOT_PAIRED;
1386 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1387 NULL, BT_BLUEZ_NAME,
1388 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1389 g_free(device_path);
1390 if (device_proxy == NULL) {
1391 result = BLUETOOTH_ERROR_INTERNAL;
1395 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1396 g_variant_new("(s)", ""),
1397 G_DBUS_CALL_FLAGS_NONE,
1398 BT_MAX_DBUS_TIMEOUT,
1400 (GAsyncReadyCallback)__bt_discover_cb,
1403 searching_info->device_proxy = device_proxy;
1405 return BLUETOOTH_ERROR_NONE;
1408 g_free(searching_info->addr);
1409 g_free(searching_info);
1410 searching_info = NULL;
1414 int _bt_cancel_search_device(void)
1416 GVariant *ret = NULL;
1419 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1421 if (searching_info->device_proxy) {
1422 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1424 G_DBUS_CALL_FLAGS_NONE,
1429 g_variant_unref(ret);
1431 __bt_cancel_search_service_done();
1433 return BLUETOOTH_ERROR_NONE;
1436 int _bt_set_alias(bluetooth_device_address_t *device_address,
1439 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1440 gchar *device_path = NULL;
1441 GDBusProxy *adapter_proxy;
1442 GDBusProxy *device_proxy;
1443 GVariant *ret = NULL;
1444 GError *error = NULL;
1445 GDBusConnection *conn;
1447 BT_CHECK_PARAMETER(device_address, return);
1448 BT_CHECK_PARAMETER(alias, return);
1450 adapter_proxy = _bt_get_adapter_proxy();
1451 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1453 conn = _bt_gdbus_get_system_gconn();
1454 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1456 _bt_convert_addr_type_to_string(address, device_address->addr);
1458 device_path = _bt_get_device_object_path(address);
1460 if (device_path == NULL) {
1461 BT_ERR("No paired device");
1462 return BLUETOOTH_ERROR_NOT_PAIRED;
1465 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1466 NULL, BT_BLUEZ_NAME,
1467 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1469 g_free(device_path);
1470 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1472 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1473 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1474 G_DBUS_CALL_FLAGS_NONE,
1479 g_variant_unref(ret);
1481 g_object_unref(device_proxy);
1484 BT_ERR("SetProperty error: [%s]", error->message);
1485 g_error_free(error);
1486 return BLUETOOTH_ERROR_INTERNAL;
1489 return BLUETOOTH_ERROR_NONE;
1492 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1495 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1496 gchar *device_path = NULL;
1497 GDBusProxy *device_proxy;
1498 gboolean previous_value;
1499 GError *error = NULL;
1500 GDBusConnection *conn;
1501 GVariant *result = NULL;
1502 GVariant *temp = NULL;
1503 int ret = BLUETOOTH_ERROR_NONE;
1505 BT_CHECK_PARAMETER(device_address, return);
1507 conn = _bt_gdbus_get_system_gconn();
1508 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1510 _bt_convert_addr_type_to_string(address, device_address->addr);
1512 device_path = _bt_get_device_object_path(address);
1514 if (device_path == NULL) {
1515 BT_ERR("No paired device");
1516 return BLUETOOTH_ERROR_NOT_PAIRED;
1519 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1520 NULL, BT_BLUEZ_NAME,
1521 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1523 g_free(device_path);
1524 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1526 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1527 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1528 G_DBUS_CALL_FLAGS_NONE,
1532 if (error != NULL) {
1533 BT_ERR("Getting property failed: [%s]\n", error->message);
1534 g_error_free(error);
1535 g_object_unref(device_proxy);
1536 return BLUETOOTH_ERROR_INTERNAL;
1539 g_variant_get(result, "(v)", &temp);
1540 previous_value = g_variant_get_boolean(temp);
1541 g_variant_unref(temp);
1542 g_variant_unref(result);
1543 /* If the input is same with previous value, return error. */
1544 if (previous_value == authorize) {
1545 BT_ERR("Same value: %d", previous_value);
1546 g_object_unref(device_proxy);
1547 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1551 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1552 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1553 G_DBUS_CALL_FLAGS_NONE,
1558 g_object_unref(device_proxy);
1560 BT_ERR("SetProperty error: [%s]", error->message);
1561 g_error_free(error);
1562 ret = BLUETOOTH_ERROR_INTERNAL;
1566 g_variant_unref(result);
1571 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1572 gboolean *is_connected)
1574 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1575 char *object_path = NULL;
1577 GDBusProxy *device_proxy;
1578 GError *error = NULL;
1580 GVariant *tmp_value;
1581 GDBusConnection *conn;
1582 GVariant *result = NULL;
1583 int ret = BLUETOOTH_ERROR_NONE;
1585 BT_CHECK_PARAMETER(device_address, return);
1587 conn = _bt_gdbus_get_system_gconn();
1588 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1590 _bt_convert_addr_type_to_string(address, device_address->addr);
1592 object_path = _bt_get_device_object_path(address);
1593 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1595 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1596 NULL, BT_BLUEZ_NAME, object_path,
1597 BT_PROPERTIES_INTERFACE, NULL, NULL);
1598 g_free(object_path);
1599 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1601 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1602 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1603 G_DBUS_CALL_FLAGS_NONE,
1607 if (result == NULL) {
1608 if (error != NULL) {
1609 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1610 g_error_free(error);
1612 g_object_unref(device_proxy);
1613 return BLUETOOTH_ERROR_INTERNAL;
1616 g_variant_get(result , "(@a{sv})", &value);
1617 g_variant_unref(result);
1619 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1620 if (tmp_value == NULL) {
1621 g_object_unref(device_proxy);
1622 g_variant_unref(value);
1623 return BLUETOOTH_ERROR_INTERNAL;
1626 *is_connected = g_variant_get_boolean(tmp_value);
1628 BT_DBG("gatt is connected : %d", *is_connected);
1630 g_variant_unref(tmp_value);
1631 g_variant_unref(value);
1632 g_object_unref(device_proxy);
1637 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1638 int connection_type, gboolean *is_connected)
1640 char *object_path = NULL;
1641 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1642 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1643 GDBusProxy *device_proxy = NULL;
1644 GDBusProxy *adapter_proxy = NULL;
1645 GDBusConnection *conn;
1646 GError *error = NULL;
1647 GVariant *tmp_value = NULL;
1648 GVariant *value = NULL;
1649 GVariant *result = NULL;
1652 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1653 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1655 _bt_convert_addr_type_to_string(address, device_address->addr);
1656 _bt_convert_addr_string_to_secure_string(secure_address, address);
1658 *is_connected = FALSE;
1659 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1661 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1662 return _bt_rfcomm_is_device_connected(device_address,
1664 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1665 return _bt_is_gatt_connected(device_address, is_connected);
1666 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1667 return _bt_pbap_is_connected(device_address, is_connected);
1669 adapter_proxy = _bt_get_adapter_proxy();
1670 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1672 conn = _bt_gdbus_get_system_gconn();
1673 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1675 _bt_convert_addr_type_to_string(address, device_address->addr);
1677 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1678 object_path = _bt_get_adapter_path();
1679 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1680 NULL, BT_BLUEZ_NAME,
1681 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1682 g_free(object_path);
1683 if (device_proxy == NULL) {
1684 BT_DBG("Device don't have this service");
1685 return BLUETOOTH_ERROR_INTERNAL;
1688 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1689 g_variant_new("(s)", address),
1690 G_DBUS_CALL_FLAGS_NONE,
1695 if (result == NULL) {
1696 BT_ERR("[GetProperties] Error occured in Proxy call");
1698 BT_ERR("%s", error->message);
1699 g_error_free(error);
1701 *is_connected = FALSE;
1702 g_object_unref(device_proxy);
1703 return BLUETOOTH_ERROR_NONE;
1705 g_variant_get(result , "(@a{sv})", &value);
1706 g_variant_unref(result);
1709 tmp_value = g_variant_lookup_value(value,
1711 G_VARIANT_TYPE_BOOLEAN);
1713 *is_connected = g_variant_get_boolean(tmp_value);
1714 g_variant_unref(tmp_value);
1716 g_variant_unref(value);
1718 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1719 return _bt_is_network_connected(_bt_get_net_conn(),
1720 device_address->addr, is_connected);
1722 uuid = _bt_get_profile_uuid128(connection_type);
1724 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1725 return BLUETOOTH_ERROR_INTERNAL;
1727 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1729 object_path = _bt_get_device_object_path(address);
1731 BT_ERR("object_path is NULL");
1733 return BLUETOOTH_ERROR_NOT_PAIRED;
1736 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1737 NULL, BT_BLUEZ_NAME,
1738 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1739 g_free(object_path);
1740 if (device_proxy == NULL) {
1741 BT_DBG("Device don't have this service");
1743 return BLUETOOTH_ERROR_INTERNAL;
1746 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1747 g_variant_new("(s)", uuid),
1748 G_DBUS_CALL_FLAGS_NONE,
1753 if (result == NULL) {
1754 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1756 BT_ERR("%s", error->message);
1757 if (g_strrstr(error->message, "Not Connected"))
1758 BT_DBG("Not connected");
1759 g_error_free(error);
1761 *is_connected = FALSE;
1762 g_object_unref(device_proxy);
1764 return BLUETOOTH_ERROR_NONE;
1766 g_variant_get(result, "(b)", is_connected);
1768 g_variant_unref(result);
1771 g_object_unref(device_proxy);
1772 return BLUETOOTH_ERROR_NONE;
1775 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1776 bluetooth_connected_link_t *connected)
1778 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1779 char *object_path = NULL;
1781 GDBusProxy *device_proxy;
1782 GError *error = NULL;
1783 GDBusConnection *conn;
1784 GVariant *tmp_value = NULL;
1785 GVariant *value = NULL;
1786 GVariant *result = NULL;
1788 BT_CHECK_PARAMETER(device_address, return);
1790 conn = _bt_gdbus_get_system_gconn();
1791 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1793 _bt_convert_addr_type_to_string(address, device_address->addr);
1795 object_path = _bt_get_device_object_path(address);
1796 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1798 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1799 NULL, BT_BLUEZ_NAME,
1800 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1801 g_free(object_path);
1802 if (device_proxy == NULL) {
1803 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1804 return BLUETOOTH_ERROR_NONE;
1807 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1808 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1809 G_DBUS_CALL_FLAGS_NONE,
1814 if (error != NULL) {
1815 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1816 g_error_free(error);
1817 g_object_unref(device_proxy);
1818 return BLUETOOTH_ERROR_INTERNAL;
1821 g_variant_get(result , "(@a{sv})", &value);
1822 g_variant_unref(result);
1824 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1825 if (tmp_value != NULL) {
1826 *connected = g_variant_get_byte(tmp_value);
1827 g_variant_unref(tmp_value);
1828 g_object_unref(device_proxy);
1829 return BLUETOOTH_ERROR_NONE;
1831 BT_ERR("g_variant value is NULL");
1832 return BLUETOOTH_ERROR_INTERNAL;
1836 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1840 GVariant *out_param1;
1842 int result = BLUETOOTH_ERROR_NONE;
1843 bt_function_data_t *func_data = user_data;
1844 request_info_t *req_info = NULL;
1845 bluetooth_device_address_t device_addr = { {0} };
1847 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1848 g_object_unref(proxy);
1850 if (reply == NULL) {
1851 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1853 BT_ERR("Error: %s", err->message);
1854 g_clear_error(&err);
1856 result = BLUETOOTH_ERROR_INTERNAL;
1858 g_variant_unref(reply);
1861 if (func_data == NULL) {
1862 BT_ERR("func_data is NULL");
1866 req_info = _bt_get_request_info(func_data->req_id);
1867 if (req_info == NULL) {
1868 BT_ERR("req_info is NULL");
1872 if (req_info->context == NULL) {
1873 BT_ERR("req_info->context is NULL");
1877 _bt_convert_addr_string_to_type(device_addr.addr,
1878 (const char *)func_data->address);
1880 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1881 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1884 g_dbus_method_invocation_return_value(req_info->context,
1885 g_variant_new("(iv)", result, out_param1));
1889 _bt_delete_request_list(req_info->req_id);
1892 g_free(func_data->address);
1897 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1898 gboolean auto_connect)
1900 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1901 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1902 gchar *device_path = NULL;
1903 GDBusProxy *device_proxy = NULL;
1904 GDBusConnection *conn;
1905 int ret = BLUETOOTH_ERROR_NONE;
1906 bt_function_data_t *func_data;
1908 BT_CHECK_PARAMETER(bd_addr, return);
1910 conn = _bt_gdbus_get_system_gconn();
1911 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1913 _bt_convert_addr_type_to_string(device_address,
1914 (unsigned char *)bd_addr->addr);
1915 device_path = _bt_get_device_object_path(device_address);
1916 if (device_path == NULL) {
1917 BT_ERR_C("device_path NULL : [%s]", device_address);
1918 ret = BLUETOOTH_ERROR_INTERNAL;
1922 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1923 NULL, BT_BLUEZ_NAME,
1924 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1925 g_free(device_path);
1926 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1928 func_data = g_malloc0(sizeof(bt_function_data_t));
1929 func_data->address = g_strdup(device_address);
1930 if (func_data->address == NULL) {
1931 BT_ERR("Unable to allocate memory for address");
1932 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1936 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1937 BT_INFO("Connect LE [%s]", secure_address);
1939 func_data->req_id = req_id;
1941 g_dbus_proxy_call(device_proxy, "ConnectLE",
1942 g_variant_new("(b)", auto_connect),
1943 G_DBUS_CALL_FLAGS_NONE,
1944 BT_MAX_DBUS_TIMEOUT,
1946 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1952 g_object_unref(device_proxy);
1954 g_free(func_data->address);
1960 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1962 BT_INFO("Try to initiate pending LE connection");
1964 pending_le_conn_timer_id = 0;
1966 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1967 &pending_le_conn_info->bd_addr,
1968 pending_le_conn_info->auto_connect);
1970 g_free(pending_le_conn_info);
1971 pending_le_conn_info = NULL;
1976 void _bt_pending_connect_le_device(void)
1978 if (pending_le_conn_timer_id > 0) {
1979 g_source_remove(pending_le_conn_timer_id);
1980 __bt_connect_le_timer_cb(NULL);
1984 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1985 gboolean auto_connect)
1987 int ret = BLUETOOTH_ERROR_NONE;
1989 BT_CHECK_PARAMETER(bd_addr, return);
1991 ret = _bt_hold_current_advertising();
1992 if (ret == BLUETOOTH_ERROR_NONE) {
1993 BT_INFO("Current advertising is held");
1994 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1995 pending_le_conn_info->req_id = req_id;
1996 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1997 BLUETOOTH_ADDRESS_LENGTH);
1998 pending_le_conn_info->auto_connect = auto_connect;
2000 pending_le_conn_timer_id =
2001 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2003 return BLUETOOTH_ERROR_NONE;
2006 BT_ERR("Unable to hold advertising");
2008 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2011 int _bt_disconnect_le_device(int req_id,
2012 const bluetooth_device_address_t *bd_addr)
2014 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2015 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2017 GDBusProxy *device_proxy;
2018 GDBusConnection *conn;
2019 int ret = BLUETOOTH_ERROR_NONE;
2020 bt_function_data_t *func_data;
2022 BT_CHECK_PARAMETER(bd_addr, return);
2024 conn = _bt_gdbus_get_system_gconn();
2025 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2027 _bt_convert_addr_type_to_string(device_address,
2028 (unsigned char *)bd_addr->addr);
2029 device_path = _bt_get_device_object_path(device_address);
2030 if (device_path == NULL) {
2031 BT_DBG("device_path NULL");
2032 ret = BLUETOOTH_ERROR_INTERNAL;
2036 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2038 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2039 NULL, BT_BLUEZ_NAME,
2040 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2041 g_free(device_path);
2042 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2044 func_data = g_malloc0(sizeof(bt_function_data_t));
2045 func_data->address = g_strdup(device_address);
2046 if (func_data->address == NULL) {
2047 BT_ERR("Unable to allocate memory for address");
2048 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2052 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2053 BT_INFO("Disconnect LE [%s]", secure_address);
2055 func_data->req_id = req_id;
2057 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2059 G_DBUS_CALL_FLAGS_NONE,
2060 BT_MAX_DBUS_TIMEOUT,
2062 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2067 g_object_unref(device_proxy);
2069 g_free(func_data->address);
2075 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2077 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2078 gchar *device_path = NULL;
2079 GError *error = NULL;
2080 GDBusProxy *device_proxy = NULL;
2081 GDBusProxy *adapter_proxy;
2082 GDBusConnection *conn;
2083 int ret = BLUETOOTH_ERROR_NONE;
2085 BT_CHECK_PARAMETER(bd_addr, return);
2087 _bt_convert_addr_type_to_string(device_address,
2088 (unsigned char *)bd_addr->addr);
2090 conn = _bt_gdbus_get_system_gconn();
2091 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2093 adapter_proxy = _bt_get_adapter_proxy();
2094 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2096 device_path = _bt_get_device_object_path(device_address);
2097 if (device_path == NULL) {
2098 BT_DBG("device_path NULL");
2099 ret = BLUETOOTH_ERROR_INTERNAL;
2103 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2105 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2106 NULL, BT_BLUEZ_NAME,
2107 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2108 g_free(device_path);
2109 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2111 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2113 G_DBUS_CALL_FLAGS_NONE,
2118 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2119 g_error_free(error);
2120 g_object_unref(device_proxy);
2121 return BLUETOOTH_ERROR_INTERNAL;
2123 g_object_unref(device_proxy);
2125 /* IPSP daemon launch */
2126 GDBusProxy *ipsp_proxy;
2128 ipsp_proxy = _bt_get_ipsp_proxy();
2129 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2131 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2132 NULL, G_DBUS_CALL_FLAGS_NONE,
2133 -1, NULL, NULL, NULL);
2138 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2140 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2141 gchar *device_path = NULL;
2142 GError *error = NULL;
2143 GDBusProxy *device_proxy = NULL;
2144 GDBusProxy *adapter_proxy;
2145 GDBusConnection *conn;
2146 int ret = BLUETOOTH_ERROR_NONE;
2148 BT_CHECK_PARAMETER(bd_addr, return);
2150 _bt_convert_addr_type_to_string(device_address,
2151 (unsigned char *)bd_addr->addr);
2153 conn = _bt_gdbus_get_system_gconn();
2154 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2156 adapter_proxy = _bt_get_adapter_proxy();
2157 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2159 device_path = _bt_get_device_object_path(device_address);
2160 if (device_path == NULL) {
2161 BT_DBG("device_path NULL");
2162 ret = BLUETOOTH_ERROR_INTERNAL;
2166 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2168 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2169 NULL, BT_BLUEZ_NAME,
2170 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2171 g_free(device_path);
2172 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2174 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2176 G_DBUS_CALL_FLAGS_NONE,
2181 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2182 g_error_free(error);
2183 g_object_unref(device_proxy);
2184 return BLUETOOTH_ERROR_INTERNAL;
2187 g_object_unref(device_proxy);
2192 int _bt_connect_profile(char *address, char *uuid,
2193 void *cb, gpointer func_data)
2197 GDBusConnection *conn;
2198 GDBusProxy *adapter_proxy;
2199 GVariant *result = NULL;
2200 GError *error = NULL;
2202 conn = _bt_gdbus_get_system_gconn();
2203 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2205 object_path = _bt_get_device_object_path(address);
2206 if (object_path == NULL) {
2207 BT_ERR("No searched device");
2209 adapter_proxy = _bt_get_adapter_proxy();
2210 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2212 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2213 g_variant_new("(s)", address),
2214 G_DBUS_CALL_FLAGS_NONE,
2219 if (error != NULL) {
2220 BT_ERR("CreateDevice Fail: %s", error->message);
2221 g_error_free(error);
2224 g_variant_unref(result);
2226 object_path = _bt_get_device_object_path(address);
2228 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2230 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2231 NULL, BT_BLUEZ_NAME,
2232 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2233 g_free(object_path);
2234 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2236 g_dbus_proxy_call(proxy, "ConnectProfile",
2237 g_variant_new("(s)", uuid),
2238 G_DBUS_CALL_FLAGS_NONE,
2239 BT_MAX_DBUS_TIMEOUT,
2241 (GAsyncReadyCallback)cb,
2244 return BLUETOOTH_ERROR_NONE;
2247 int _bt_disconnect_all(char *address)
2249 int ret = BLUETOOTH_ERROR_NONE;
2252 GDBusConnection *conn;
2253 GVariant *result = NULL;
2257 conn = _bt_gdbus_get_system_gconn();
2258 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2260 object_path = _bt_get_device_object_path(address);
2261 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2263 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2264 NULL, BT_BLUEZ_NAME,
2265 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2266 g_free(object_path);
2267 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2269 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2271 G_DBUS_CALL_FLAGS_NONE,
2276 BT_ERR("Dbus Call Error:[%s]", err->message);
2278 ret = BLUETOOTH_ERROR_INTERNAL;
2281 g_object_unref(proxy);
2283 g_variant_unref(result);
2288 int _bt_disconnect_profile(char *address, char *uuid,
2289 void *cb, gpointer func_data)
2293 GDBusConnection *conn;
2295 conn = _bt_gdbus_get_system_gconn();
2296 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2298 object_path = _bt_get_device_object_path(address);
2299 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2301 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2302 NULL, BT_BLUEZ_NAME,
2303 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2304 g_free(object_path);
2305 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2307 g_dbus_proxy_call(proxy, "DisconnectProfile",
2308 g_variant_new("(s)", uuid),
2309 G_DBUS_CALL_FLAGS_NONE,
2310 BT_MAX_DBUS_TIMEOUT,
2312 (GAsyncReadyCallback)cb,
2315 return BLUETOOTH_ERROR_NONE;
2318 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2319 int low_threshold, int in_range_threshold, int high_threshold)
2321 int ret = BLUETOOTH_ERROR_NONE;
2323 GVariant *result = NULL;
2324 GError *error = NULL;
2325 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2327 BT_CHECK_PARAMETER(bd_addr, return);
2328 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2329 bd_addr->addr[0], bd_addr->addr[1],
2330 bd_addr->addr[2], bd_addr->addr[3],
2331 bd_addr->addr[4], bd_addr->addr[5],
2333 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2334 in_range_threshold, high_threshold);
2336 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2338 proxy = _bt_get_adapter_proxy();
2339 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2341 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2342 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2343 G_DBUS_CALL_FLAGS_NONE,
2347 if (error != NULL) {
2348 BT_ERR("Dbus Call Error:[%s]", error->message);
2349 g_error_free(error);
2350 ret = BLUETOOTH_ERROR_INTERNAL;
2354 g_variant_unref(result);
2359 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2362 int ret = BLUETOOTH_ERROR_NONE;
2364 GVariant *result = NULL;
2365 GError *error = NULL;
2366 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2368 BT_CHECK_PARAMETER(bd_addr, return);
2369 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2370 bd_addr->addr[0], bd_addr->addr[1],
2371 bd_addr->addr[2], bd_addr->addr[3],
2372 bd_addr->addr[4], bd_addr->addr[5],
2375 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2377 proxy = _bt_get_adapter_proxy();
2378 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2380 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2381 g_variant_new("(si)", address, link_type),
2382 G_DBUS_CALL_FLAGS_NONE,
2387 if (error != NULL) {
2388 BT_ERR("Dbus Call Error:[%s]", error->message);
2389 g_error_free(error);
2390 ret = BLUETOOTH_ERROR_INTERNAL;
2394 g_variant_unref(result);
2399 int _bt_le_conn_update(unsigned char *device_address,
2400 float interval_min, float interval_max,
2401 guint16 latency, guint16 time_out)
2403 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2404 gchar *device_path = NULL;
2405 GError *error = NULL;
2406 GDBusProxy *device_proxy = NULL;
2407 GDBusConnection *conn;
2409 guint32 min, max, to;
2410 guint32 min_supervision_to;
2411 int ret = BLUETOOTH_ERROR_NONE;
2413 BT_CHECK_PARAMETER(device_address, return);
2415 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2416 interval_min, interval_max, latency, time_out);
2418 if (interval_min > interval_max ||
2419 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2420 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2421 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2425 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2426 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2427 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2431 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2432 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2437 * The Supervision_Timeout in milliseconds shall be larger than
2438 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2439 * where Conn_Interval_Max is given in milliseconds.
2441 min_supervision_to = (1 + latency) * interval_max * 2;
2442 if (time_out <= min_supervision_to) {
2443 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2447 _bt_convert_addr_type_to_string(address, device_address);
2449 BT_DBG("Remote device address: %s", address);
2451 device_path = _bt_get_device_object_path(address);
2453 if (device_path == NULL) {
2454 BT_ERR("device_path NULL");
2455 ret = BLUETOOTH_ERROR_INTERNAL;
2459 conn = _bt_gdbus_get_system_gconn();
2461 BT_ERR("conn NULL");
2462 ret = BLUETOOTH_ERROR_INTERNAL;
2466 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2467 NULL, BT_BLUEZ_NAME,
2468 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2470 g_free(device_path);
2471 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2473 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2474 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2475 to = time_out / BT_LE_CONN_TO_SPLIT;
2477 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2478 g_variant_new("(uuuu)", min, max, latency, to),
2479 G_DBUS_CALL_FLAGS_NONE,
2484 g_object_unref(device_proxy);
2485 if (reply == NULL) {
2487 BT_ERR("Error %s[%s]", error->message, address);
2488 if (g_strrstr(error->message, "In Progress"))
2489 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2491 ret = BLUETOOTH_ERROR_INTERNAL;
2492 g_error_free(error);
2496 g_variant_unref(reply);
2502 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2503 bluetooth_device_pin_code_t *pin_code)
2505 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2507 bt_pin_code_info_t *pin_info = NULL;
2509 BT_CHECK_PARAMETER(device_address, return);
2510 BT_CHECK_PARAMETER(pin_code, return);
2511 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2512 BLUETOOTH_ERROR_NO_RESOURCES);
2514 _bt_convert_addr_type_to_string(address, device_address->addr);
2516 for (l = pin_info_list; l != NULL; l = l->next) {
2519 if (g_strcmp0(pin_info->address, address) == 0) {
2520 g_free(pin_info->pin_code);
2521 pin_info->pin_code = g_strdup(pin_code->pin_code);
2522 return BLUETOOTH_ERROR_NONE;
2526 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2527 pin_info->address = g_strdup(address);
2528 pin_info->pin_code = g_strdup(pin_code->pin_code);
2529 pin_info_list = g_slist_append(pin_info_list, pin_info);
2531 return BLUETOOTH_ERROR_NONE;
2534 gint __bt_compare_address(gpointer *a, gpointer *b)
2536 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2537 char *address = (char *)b;
2538 return g_strcmp0(pin_info->address, address);
2541 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2543 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2545 bt_pin_code_info_t *pin_info = NULL;
2547 BT_CHECK_PARAMETER(device_address, return);
2549 _bt_convert_addr_type_to_string(address, device_address->addr);
2551 l = g_slist_find_custom(pin_info_list, address,
2552 (GCompareFunc)__bt_compare_address);
2556 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2557 g_free(pin_info->address);
2558 g_free(pin_info->pin_code);
2562 return BLUETOOTH_ERROR_NONE;
2565 int _bt_get_device_pin_code(const char *address, char *pin_code)
2569 BT_CHECK_PARAMETER(address, return);
2570 BT_CHECK_PARAMETER(pin_code, return);
2572 for (l = pin_info_list; l != NULL; l = l->next) {
2573 bt_pin_code_info_t *pin_info = l->data;
2575 if (g_strcmp0(pin_info->address, address) == 0) {
2576 g_strlcpy(pin_code, pin_info->pin_code,
2577 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2579 return BLUETOOTH_ERROR_NONE;
2583 return BLUETOOTH_ERROR_NOT_FOUND;
2586 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2587 bluetooth_le_connection_param_t *param)
2590 return BLUETOOTH_ERROR_INVALID_PARAM;
2592 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2593 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2594 return BLUETOOTH_ERROR_INVALID_PARAM;
2596 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2599 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2600 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2601 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2602 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2603 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2606 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2607 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2608 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2609 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2610 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2613 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2614 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2615 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2616 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2617 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2621 BT_ERR("Unhandled mode : %d", mode);
2625 return BLUETOOTH_ERROR_NONE;
2628 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2629 guint trusted_profile_flag, guint *trusted)
2635 case TRUSTED_PROFILE_PBAP:
2636 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2637 trust_profile = trusted_profile_flag & (1 << 1);
2639 return BLUETOOTH_ERROR_NOT_SUPPORT;
2641 case TRUSTED_PROFILE_MAP:
2642 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2643 trust_profile = trusted_profile_flag & (1 << 3);
2645 return BLUETOOTH_ERROR_NOT_SUPPORT;
2647 case TRUSTED_PROFILE_SAP:
2648 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2649 trust_profile = trusted_profile_flag & (1 << 5);
2651 return BLUETOOTH_ERROR_NOT_SUPPORT;
2653 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2654 *trusted = trusted_profile_flag;
2655 return BLUETOOTH_ERROR_NONE;
2657 return BLUETOOTH_ERROR_NOT_SUPPORT;
2663 return BLUETOOTH_ERROR_NONE;
2666 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2667 guint restricted_profile_flag, guint *restricted)
2669 int restrict_profile;
2670 *restricted = FALSE;
2673 case RESTRICTED_PROFILE_HFP_HS:
2674 restrict_profile = restricted_profile_flag & (1 << 0);
2676 case RESTRICTED_PROFILE_A2DP:
2677 restrict_profile = restricted_profile_flag & (1 << 2);
2680 return BLUETOOTH_ERROR_NOT_SUPPORT;
2683 if (restrict_profile)
2686 return BLUETOOTH_ERROR_NONE;
2689 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2692 case TRUSTED_PROFILE_PBAP:
2693 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2694 case TRUSTED_PROFILE_MAP:
2695 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2696 case TRUSTED_PROFILE_SAP:
2697 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2698 case TRUSTED_PROFILE_ALL:
2705 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2708 case RESTRICTED_PROFILE_HFP_HS:
2709 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2710 case RESTRICTED_PROFILE_A2DP:
2711 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2717 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2719 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2720 return TRUSTED_PROFILE_PBAP;
2721 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2722 return TRUSTED_PROFILE_MAP;
2723 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2724 return TRUSTED_PROFILE_SAP;
2726 return 0; /* 0 - Unknown Profile */
2729 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2730 bluetooth_trusted_profile_t profile, gboolean trust)
2732 int ret = BLUETOOTH_ERROR_NONE;
2733 GDBusConnection *conn;
2735 GError *error = NULL;
2736 char *device_path = NULL;
2738 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2741 BT_CHECK_PARAMETER(bd_addr, return);
2742 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2743 bd_addr->addr[0], bd_addr->addr[1],
2744 bd_addr->addr[2], bd_addr->addr[3],
2745 bd_addr->addr[4], bd_addr->addr[5],
2748 conn = _bt_gdbus_get_system_gconn();
2749 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2751 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2753 device_path = _bt_get_device_object_path(address);
2754 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2756 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2757 NULL, BT_BLUEZ_NAME, device_path,
2758 BT_DEVICE_INTERFACE, NULL, NULL);
2760 g_free(device_path);
2761 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2763 uuid = _bt_get_trusted_profile_uuid(profile);
2765 g_object_unref(proxy);
2766 return BLUETOOTH_ERROR_NOT_SUPPORT;
2769 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2770 g_variant_new("(sb)", uuid, trust),
2771 G_DBUS_CALL_FLAGS_NONE, -1,
2773 g_object_unref(proxy);
2775 if (reply == NULL) {
2776 BT_ERR("Failed to Set Profile Trusted");
2777 ret = BLUETOOTH_ERROR_INTERNAL;
2779 BT_ERR("Error %s[%s]", error->message, address);
2780 g_error_free(error);
2784 g_variant_unref(reply);
2791 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2792 bluetooth_trusted_profile_t profile, guint *trust)
2794 int ret = BLUETOOTH_ERROR_NONE;
2795 GDBusConnection *conn;
2797 GError *error = NULL;
2798 char *device_path = NULL;
2799 guint trusted_profile_flag;
2800 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2803 BT_CHECK_PARAMETER(bd_addr, return);
2804 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2805 bd_addr->addr[0], bd_addr->addr[1],
2806 bd_addr->addr[2], bd_addr->addr[3],
2807 bd_addr->addr[4], bd_addr->addr[5],
2810 conn = _bt_gdbus_get_system_gconn();
2811 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2813 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2815 device_path = _bt_get_device_object_path(address);
2816 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2818 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2819 NULL, BT_BLUEZ_NAME, device_path,
2820 BT_PROPERTIES_INTERFACE, NULL, NULL);
2822 g_free(device_path);
2823 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2825 reply = g_dbus_proxy_call_sync(proxy, "Get",
2826 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2827 G_DBUS_CALL_FLAGS_NONE, -1,
2829 g_object_unref(proxy);
2831 if (reply == NULL) {
2832 BT_ERR("Failed to Get Profile Trusted");
2833 ret = BLUETOOTH_ERROR_INTERNAL;
2835 BT_ERR("Error %s[%s]", error->message, address);
2836 g_error_free(error);
2841 g_variant_get(reply, "(v)", &temp);
2842 trusted_profile_flag = g_variant_get_uint32(temp);
2843 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2845 ret = _bt_get_trusted_profile_from_flag(profile,
2846 trusted_profile_flag, trust);
2847 g_variant_unref(temp);
2848 g_variant_unref(reply);
2851 BT_DBG("TRUST %d", *trust);
2855 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2856 bluetooth_restricted_profile_t profile, gboolean restricted)
2858 int ret = BLUETOOTH_ERROR_NONE;
2859 GDBusConnection *conn;
2861 GError *error = NULL;
2862 char *device_path = NULL;
2864 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2867 BT_CHECK_PARAMETER(bd_addr, return);
2868 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2869 bd_addr->addr[0], bd_addr->addr[1],
2870 bd_addr->addr[2], bd_addr->addr[3],
2871 bd_addr->addr[4], bd_addr->addr[5],
2872 profile, restricted);
2874 conn = _bt_gdbus_get_system_gconn();
2875 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2877 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2879 device_path = _bt_get_device_object_path(address);
2880 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2882 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2883 NULL, BT_BLUEZ_NAME, device_path,
2884 BT_DEVICE_INTERFACE, NULL, NULL);
2886 g_free(device_path);
2887 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2889 uuid = _bt_get_restricted_profile_uuid(profile);
2891 g_object_unref(proxy);
2892 return BLUETOOTH_ERROR_NOT_SUPPORT;
2895 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2896 g_variant_new("(sb)", uuid, restricted),
2897 G_DBUS_CALL_FLAGS_NONE, -1,
2899 g_object_unref(proxy);
2901 if (reply == NULL) {
2902 BT_ERR("Failed to Set Profile Restricted");
2903 ret = BLUETOOTH_ERROR_INTERNAL;
2905 BT_ERR("Error %s[%s]", error->message, address);
2906 g_error_free(error);
2910 g_variant_unref(reply);
2917 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2918 bluetooth_restricted_profile_t profile, guint *restricted)
2920 int ret = BLUETOOTH_ERROR_NONE;
2921 GDBusConnection *conn;
2923 GError *error = NULL;
2924 char *device_path = NULL;
2925 guint restricted_profile_flag;
2926 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2929 BT_CHECK_PARAMETER(bd_addr, return);
2930 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2931 bd_addr->addr[0], bd_addr->addr[1],
2932 bd_addr->addr[2], bd_addr->addr[3],
2933 bd_addr->addr[4], bd_addr->addr[5],
2934 profile, *restricted);
2936 conn = _bt_gdbus_get_system_gconn();
2937 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2939 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2941 device_path = _bt_get_device_object_path(address);
2942 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2944 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2945 NULL, BT_BLUEZ_NAME, device_path,
2946 BT_PROPERTIES_INTERFACE, NULL, NULL);
2948 g_free(device_path);
2949 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2951 reply = g_dbus_proxy_call_sync(proxy, "Get",
2952 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2953 G_DBUS_CALL_FLAGS_NONE, -1,
2955 g_object_unref(proxy);
2957 if (reply == NULL) {
2958 BT_ERR("Failed to Get Profile Restricted");
2959 ret = BLUETOOTH_ERROR_INTERNAL;
2961 BT_ERR("Error %s[%s]", error->message, address);
2962 g_error_free(error);
2967 g_variant_get(reply, "(v)", &temp);
2968 restricted_profile_flag = g_variant_get_uint32(temp);
2969 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2971 ret = _bt_get_restricted_profile_from_flag(profile,
2972 restricted_profile_flag, restricted);
2973 g_variant_unref(temp);
2974 g_variant_unref(reply);
2977 BT_DBG("TRUST %d", *restricted);
2981 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2985 GVariant *out_param1;
2986 request_info_t *req_info;
2987 GVariant *val = NULL;
2988 GVariant *param = NULL;
2991 bluetooth_device_address_t device_addr = { {0} };
2992 int result = BLUETOOTH_ERROR_NONE;
2995 val = g_dbus_proxy_call_finish(proxy, res, &err);
2997 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2998 if (req_info == NULL) {
2999 BT_ERR("req_info == NULL");
3000 g_object_unref(proxy);
3001 att_mtu_req_info->device_proxy = NULL;
3006 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3008 if (g_strrstr(err->message, "NotSupported")) {
3009 BT_INFO("Connection Not Supported");
3010 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3011 } else if (g_strrstr(err->message, "NotConnected")) {
3012 BT_INFO("Not connected");
3013 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3014 } else if (g_strrstr(err->message, "InvalidArguments")) {
3015 BT_INFO("Not connected");
3016 result = BLUETOOTH_ERROR_INVALID_PARAM;
3018 BT_DBG("Default case");
3019 result = BLUETOOTH_ERROR_INTERNAL;
3023 g_object_unref(proxy);
3024 att_mtu_req_info->device_proxy = NULL;
3026 if (result != BLUETOOTH_ERROR_NONE)
3030 g_variant_get(val, "(qy)", &mtu, &status);
3031 g_variant_unref(val);
3034 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3036 param = g_variant_new("(isqy)",
3038 att_mtu_req_info->addr,
3042 /* Send the event to application */
3043 _bt_send_event(BT_DEVICE_EVENT,
3044 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3048 if (req_info->context == NULL)
3051 _bt_convert_addr_string_to_type(device_addr.addr,
3052 (const char *)att_mtu_req_info->addr);
3054 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3055 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3056 g_dbus_method_invocation_return_value(req_info->context,
3057 g_variant_new("(iv)", result, out_param1));
3059 _bt_delete_request_list(req_info->req_id);
3064 g_free(att_mtu_req_info->addr);
3065 g_free(att_mtu_req_info);
3066 att_mtu_req_info = NULL;
3071 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3074 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3075 gchar *device_path = NULL;
3076 GDBusProxy *adapter_proxy;
3077 GDBusProxy *device_proxy;
3078 GDBusConnection *conn;
3079 int ret = BLUETOOTH_ERROR_NONE;
3081 BT_CHECK_PARAMETER(device_address, return);
3083 if (att_mtu_req_info) {
3084 BT_ERR("ATT MTU request in progress");
3085 return BLUETOOTH_ERROR_DEVICE_BUSY;
3088 conn = _bt_gdbus_get_system_gconn();
3089 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3091 adapter_proxy = _bt_get_adapter_proxy();
3092 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3094 _bt_convert_addr_type_to_string(address, device_address->addr);
3096 BT_DBG("Remote device address: %s", address);
3098 device_path = _bt_get_device_object_path(address);
3100 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3102 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3103 NULL, BT_BLUEZ_NAME,
3104 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3105 g_free(device_path);
3106 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3108 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3109 att_mtu_req_info->addr = (char *)g_strdup(address);
3110 att_mtu_req_info->req_id = request_id;
3111 att_mtu_req_info->device_proxy = device_proxy;
3113 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3114 g_variant_new("(q)", mtu),
3115 G_DBUS_CALL_FLAGS_NONE,
3116 BT_MAX_DBUS_TIMEOUT,
3118 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3124 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3127 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3128 char *object_path = NULL;
3130 GDBusProxy *device_proxy;
3131 GError *error = NULL;
3133 GVariant *tmp_value;
3134 GDBusConnection *conn;
3135 GVariant *result = NULL;
3136 int ret = BLUETOOTH_ERROR_NONE;
3138 BT_CHECK_PARAMETER(device_address, return);
3140 conn = _bt_gdbus_get_system_gconn();
3141 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3143 _bt_convert_addr_type_to_string(address, device_address->addr);
3145 object_path = _bt_get_device_object_path(address);
3146 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3148 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3149 NULL, BT_BLUEZ_NAME, object_path,
3150 BT_PROPERTIES_INTERFACE, NULL, NULL);
3151 g_free(object_path);
3152 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3154 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3155 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3156 G_DBUS_CALL_FLAGS_NONE,
3160 if (result == NULL) {
3161 if (error != NULL) {
3162 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3163 g_error_free(error);
3165 g_object_unref(device_proxy);
3166 return BLUETOOTH_ERROR_INTERNAL;
3169 g_variant_get(result , "(@a{sv})", &value);
3170 g_variant_unref(result);
3172 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3173 if (tmp_value == NULL) {
3174 g_object_unref(device_proxy);
3175 g_variant_unref(value);
3176 return BLUETOOTH_ERROR_INTERNAL;
3179 *mtu = g_variant_get_uint16(tmp_value);
3181 BT_DBG("ATT MTU : %d", *mtu);
3183 g_variant_unref(tmp_value);
3184 g_variant_unref(value);
3185 g_object_unref(device_proxy);
3190 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3191 bluetooth_device_address_t *id_address)
3193 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3194 gchar *device_path = NULL;
3195 const gchar *idaddress = NULL;
3196 GDBusProxy *device_proxy;
3197 GError *error = NULL;
3198 GVariant *result = NULL;
3199 GDBusConnection *conn;
3200 int ret = BLUETOOTH_ERROR_NONE;
3202 BT_CHECK_PARAMETER(device_address, return);
3204 conn = _bt_gdbus_get_system_gconn();
3205 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3207 _bt_convert_addr_type_to_string(address, device_address->addr);
3209 device_path = _bt_get_device_object_path(address);
3210 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3212 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3213 NULL, BT_BLUEZ_NAME,
3214 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3215 g_free(device_path);
3216 if (!device_proxy) {
3217 BT_ERR("Unable to get proxy");
3218 return BLUETOOTH_ERROR_INTERNAL;
3221 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3223 G_DBUS_CALL_FLAGS_NONE,
3228 if (result == NULL) {
3229 BT_ERR("Failed to get device ID address");
3230 if (error != NULL) {
3231 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3232 g_error_free(error);
3234 g_object_unref(device_proxy);
3235 return BLUETOOTH_ERROR_INTERNAL;
3238 g_variant_get(result , "(s)", &idaddress);
3239 g_variant_unref(result);
3241 if (idaddress == NULL) {
3242 BT_ERR("No paired device");
3243 g_object_unref(device_proxy);
3244 return BLUETOOTH_ERROR_NOT_PAIRED;
3247 BT_DBG("ID Address:%s", idaddress);
3250 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3252 ret = BLUETOOTH_ERROR_INTERNAL;
3254 g_object_unref(device_proxy);
3259 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3261 GapAgentPrivate *agent = _bt_get_adapter_agent();
3262 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3264 if (authentication_reply)
3265 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3267 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3269 BT_DBG("BT_PASSKEY_REPLY");
3270 return BLUETOOTH_ERROR_NONE;
3273 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3275 GapAgentPrivate *agent = _bt_get_adapter_agent();
3276 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3278 if (confirmation_reply)
3279 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3281 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3283 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3284 return BLUETOOTH_ERROR_NONE;