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 info->uuid_count = g_variant_get_size(value);
321 info->uuids = g_variant_dup_strv(value, (gsize *)&info->uuid_count);
324 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
326 char *object_path = NULL;
327 bt_remote_dev_info_t *dev_info;
329 retv_if(address == NULL, NULL);
331 object_path = _bt_get_device_object_path(address);
332 retv_if(object_path == NULL, NULL);
334 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
340 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
341 const char *object_path)
343 bt_remote_dev_info_t *dev_info;
344 GDBusProxy *adapter_proxy;
345 GDBusProxy *device_proxy;
350 GDBusConnection *conn;
351 GError *error = NULL;
352 GVariant *result = NULL;
353 GVariantIter *value_iter;
357 adapter_proxy = _bt_get_adapter_proxy();
358 retv_if(adapter_proxy == NULL, NULL);
360 retv_if(object_path == NULL, NULL);
362 conn = _bt_gdbus_get_system_gconn();
364 BT_ERR("conn == NULL");
368 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
369 NULL, BT_BLUEZ_NAME, object_path,
370 BT_PROPERTIES_INTERFACE, NULL, NULL);
372 retv_if(device_proxy == NULL, NULL);
374 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
375 g_variant_new("(s)", BT_DEVICE_INTERFACE),
376 G_DBUS_CALL_FLAGS_NONE,
381 g_object_unref(device_proxy);
383 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
385 if (result != NULL) {
386 g_variant_get(result , "(@a{sv})", &value);
387 g_variant_unref(result);
389 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
391 g_variant_get(tmp_value, "s", &name);
392 g_variant_unref(tmp_value);
394 DBG_SECURE("Alias Name [%s]", name);
396 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
397 g_variant_get(tmp_value, "s", &name);
398 g_variant_unref(tmp_value);
401 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
403 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
404 g_variant_unref(tmp_value);
406 dev_info->is_alias_set = FALSE;
408 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
410 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
412 dev_info->class = g_variant_get_uint32(tmp_value);
413 g_variant_unref(tmp_value);
417 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
419 dev_info->connected = g_variant_get_byte(tmp_value);
420 g_variant_unref(tmp_value);
422 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
423 BT_DBG("connected link : %d", dev_info->connected);
425 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
427 dev_info->trust = g_variant_get_boolean(tmp_value);
428 g_variant_unref(tmp_value);
430 dev_info->trust = FALSE;
432 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
434 dev_info->paired = g_variant_get_boolean(tmp_value);
435 g_variant_unref(tmp_value);
437 dev_info->paired = FALSE;
439 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
441 dev_info->rssi = g_variant_get_int16(tmp_value);
442 g_variant_unref(tmp_value);
446 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
448 dev_info->addr_type = g_variant_get_byte(tmp_value);
449 g_variant_unref(tmp_value);
451 dev_info->addr_type = 0;
453 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
455 __bt_get_uuids(tmp_value, dev_info);
456 g_variant_unref(tmp_value);
459 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
461 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
462 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
463 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
464 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
466 g_variant_unref(tmp_value);
468 dev_info->manufacturer_data_len = 0;
470 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
472 if ((dev_info->manufacturer_data_len == 0) ||
473 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
474 BT_ERR("manufacturer data length doesn't match");
475 dev_info->manufacturer_data_len = 0;
476 dev_info->manufacturer_data = NULL;
478 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
479 g_variant_get(tmp_value, "ay", &value_iter);
480 while (g_variant_iter_loop(value_iter, "y", &m_value))
481 dev_info->manufacturer_data[i++] = m_value;
483 g_variant_unref(tmp_value);
485 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
486 dev_info->manufacturer_data_len = 0;
487 dev_info->manufacturer_data = NULL;
490 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
491 g_variant_get(tmp_value, "s", &address);
492 g_variant_unref(tmp_value);
494 dev_info->address = g_strdup(address);
495 dev_info->name = g_strdup(name);
497 g_variant_unref(value);
499 BT_ERR("result is NULL\n");
507 char *_bt_get_bonded_device_name(char *address)
509 bluetooth_device_address_t device_address = { {0} };
510 bluetooth_device_info_t dev_info;
512 retv_if(address == NULL, strdup(""));
514 _bt_convert_addr_string_to_type(device_address.addr, address);
516 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
518 _bt_get_bonded_device_info(&device_address, &dev_info);
520 return g_strdup(dev_info.device_name.name);
523 static gboolean __ignore_auto_pairing_request(const char *address)
528 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
539 /* Get the LAP(Lower Address part) */
540 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
542 snprintf(lap_address, sizeof(lap_address), ",%s", address);
544 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
547 BT_ERR("fopen failed \n");
551 fseek(fp, 0, SEEK_END);
556 BT_ERR("Get file size failed \n");
561 buffer = g_malloc0(sizeof(char) * size);
562 /* Fix : NULL_RETURNS */
563 if (buffer == NULL) {
564 BT_ERR("Memory allocation error\n");
568 result = fread((char *)buffer, 1, size, fp);
570 if (result != size) {
571 BT_ERR("Read Error\n");
576 BT_DBG("Buffer = %s\n", buffer);
578 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
584 /* Write the data and insert new device data */
585 for (i = 0; lines[i] != NULL; i++) {
586 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
587 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
589 lines[i] = temp_buffer;
592 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
594 /* Fix : NULL_RETURNS */
595 retv_if(buffer == NULL, FALSE);
597 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
600 BT_ERR("fopen failed \n");
605 BT_DBG("Buffer = %s\n", buffer);
606 fwrite(buffer, 1, strlen(buffer), fp);
616 static int __bt_cancel_bonding(void)
618 BT_CHECK_PARAMETER(bonding_info, return);
619 BT_CHECK_PARAMETER(bonding_info->addr, return);
620 /* First Cancel the ongoing pairing in bluez otherwise if we send
621 * pair request bluez will send inprogress and we again retry bond so
622 * this cycle continues */
624 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
625 NULL, G_DBUS_CALL_FLAGS_NONE,
628 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
630 return BLUETOOTH_ERROR_PARING_FAILED;
632 return BLUETOOTH_ERROR_NONE;
635 static int __bt_retry_bond(void)
637 BT_CHECK_PARAMETER(bonding_info, return);
638 BT_CHECK_PARAMETER(bonding_info->addr, return);
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 /* Fix : NULL_RETURNS */
1146 if (unbonding_info == NULL) {
1147 BT_ERR("Memory not allocated !");
1148 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1151 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1152 unbonding_info->req_id = request_id;
1154 _bt_convert_addr_type_to_string(unbonding_info->addr,
1155 device_address->addr);
1157 device_path = _bt_get_device_object_path(unbonding_info->addr);
1159 if (device_path == NULL) {
1160 BT_ERR("No paired device");
1161 result = BLUETOOTH_ERROR_NOT_PAIRED;
1165 conn = _bt_gdbus_get_system_gconn();
1167 BT_ERR("conn is NULL");
1168 result = BLUETOOTH_ERROR_INTERNAL;
1172 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1173 NULL, BT_BLUEZ_NAME,
1174 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1176 if (device_proxy != NULL) {
1178 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1179 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1180 G_DBUS_CALL_FLAGS_NONE,
1185 BT_ERR("Getting property failed: [%s]\n", error->message);
1186 g_error_free(error);
1187 result = BLUETOOTH_ERROR_NOT_PAIRED;
1191 BT_ERR("No paired device");
1192 g_object_unref(device_proxy);
1193 result = BLUETOOTH_ERROR_NOT_PAIRED;
1196 g_variant_unref(ret);
1198 g_object_unref(device_proxy);
1201 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1202 g_variant_new("(o)", device_path),
1203 G_DBUS_CALL_FLAGS_NONE,
1204 BT_MAX_DBUS_TIMEOUT,
1206 (GAsyncReadyCallback)__bt_unbond_cb,
1209 g_free(device_path);
1210 return BLUETOOTH_ERROR_NONE;
1213 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1214 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1215 unbonding_info->addr);
1217 g_array_append_vals(*out_param1, &dev_info,
1218 sizeof(bluetooth_device_info_t));
1219 g_free(device_path);
1220 g_free(unbonding_info->addr);
1221 g_free(unbonding_info);
1225 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1230 GVariant *out_param1;
1231 int result = BLUETOOTH_ERROR_NONE;
1232 bluetooth_device_info_t dev_info;
1233 bt_remote_dev_info_t *remote_dev_info;
1234 request_info_t *req_info;
1235 GVariant *uuid_list, *manufacture_data;
1237 GVariantBuilder *builder = NULL;
1240 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1242 g_variant_unref(reply);
1244 g_object_unref(proxy);
1246 if (searching_info == NULL) {
1248 BT_ERR("unbonding_info == NULL");
1252 req_info = _bt_get_request_info(searching_info->req_id);
1253 if (req_info == NULL) {
1254 BT_ERR("req_info == NULL");
1259 g_dbus_error_strip_remote_error(err);
1260 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1262 if (g_strrstr("Operation canceled", err->message))
1263 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1264 else if (g_strrstr("In Progress", err->message))
1265 result = BLUETOOTH_ERROR_IN_PROGRESS;
1266 else if (g_strrstr("Host is down", err->message))
1267 result = BLUETOOTH_ERROR_HOST_DOWN;
1269 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1271 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1272 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1273 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1274 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1275 remote_dev_info->uuid_count > 0) {
1276 result = BLUETOOTH_ERROR_NONE;
1279 _bt_free_device_info(remote_dev_info);
1284 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1285 if (!remote_dev_info)
1289 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1290 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1291 g_variant_builder_add(builder, "s",
1292 remote_dev_info->uuids[i]);
1294 uuid_list = g_variant_new("as", builder);
1295 g_variant_builder_unref(builder);
1296 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1297 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1300 param = g_variant_new("(isunsbub@asn@ay)",
1302 searching_info->addr,
1303 remote_dev_info->class,
1304 remote_dev_info->rssi,
1305 remote_dev_info->name,
1306 remote_dev_info->paired,
1307 remote_dev_info->connected,
1308 remote_dev_info->trust,
1310 remote_dev_info->manufacturer_data_len,
1313 /* Send the event to application */
1314 _bt_send_event(BT_ADAPTER_EVENT,
1315 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1318 _bt_free_device_info(remote_dev_info);
1321 if (req_info->context == NULL)
1324 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1325 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1326 searching_info->addr);
1328 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1329 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1331 g_dbus_method_invocation_return_value(req_info->context,
1332 g_variant_new("(iv)", result, out_param1));
1335 _bt_delete_request_list(req_info->req_id);
1340 if (searching_info) {
1341 g_free(searching_info->addr);
1342 g_free(searching_info);
1343 searching_info = NULL;
1347 int _bt_search_device(int request_id,
1348 bluetooth_device_address_t *device_address)
1350 char *device_path = NULL;
1351 GDBusProxy *device_proxy = NULL;
1352 GDBusConnection *conn;
1354 GDBusProxy *adapter_proxy;
1355 int result = BLUETOOTH_ERROR_INTERNAL;
1357 BT_CHECK_PARAMETER(device_address, return);
1360 BT_ERR("Bonding in progress");
1361 return BLUETOOTH_ERROR_DEVICE_BUSY;
1364 if (searching_info) {
1365 BT_ERR("Service searching in progress");
1366 return BLUETOOTH_ERROR_DEVICE_BUSY;
1369 adapter_proxy = _bt_get_adapter_proxy();
1370 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1372 /* allocate user data so that it can be retrieved in callback */
1373 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1374 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1375 searching_info->req_id = request_id;
1377 _bt_convert_addr_type_to_string(searching_info->addr,
1378 device_address->addr);
1380 conn = _bt_gdbus_get_system_gconn();
1381 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1384 device_path = _bt_get_device_object_path(searching_info->addr);
1386 if (device_path == NULL) {
1387 BT_ERR("No paired device");
1388 result = BLUETOOTH_ERROR_NOT_PAIRED;
1392 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1393 NULL, BT_BLUEZ_NAME,
1394 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1395 g_free(device_path);
1396 if (device_proxy == NULL) {
1397 result = BLUETOOTH_ERROR_INTERNAL;
1401 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1402 g_variant_new("(s)", ""),
1403 G_DBUS_CALL_FLAGS_NONE,
1404 BT_MAX_DBUS_TIMEOUT,
1406 (GAsyncReadyCallback)__bt_discover_cb,
1409 searching_info->device_proxy = device_proxy;
1411 return BLUETOOTH_ERROR_NONE;
1414 g_free(searching_info->addr);
1415 g_free(searching_info);
1416 searching_info = NULL;
1420 int _bt_cancel_search_device(void)
1422 GVariant *ret = NULL;
1425 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1427 if (searching_info->device_proxy) {
1428 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1430 G_DBUS_CALL_FLAGS_NONE,
1435 g_variant_unref(ret);
1437 __bt_cancel_search_service_done();
1439 return BLUETOOTH_ERROR_NONE;
1442 int _bt_set_alias(bluetooth_device_address_t *device_address,
1445 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1446 gchar *device_path = NULL;
1447 GDBusProxy *adapter_proxy;
1448 GDBusProxy *device_proxy;
1449 GVariant *ret = NULL;
1450 GError *error = NULL;
1451 GDBusConnection *conn;
1453 BT_CHECK_PARAMETER(device_address, return);
1454 BT_CHECK_PARAMETER(alias, return);
1456 adapter_proxy = _bt_get_adapter_proxy();
1457 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1459 conn = _bt_gdbus_get_system_gconn();
1460 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1462 _bt_convert_addr_type_to_string(address, device_address->addr);
1464 device_path = _bt_get_device_object_path(address);
1466 if (device_path == NULL) {
1467 BT_ERR("No paired device");
1468 return BLUETOOTH_ERROR_NOT_PAIRED;
1471 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1472 NULL, BT_BLUEZ_NAME,
1473 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1475 g_free(device_path);
1476 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1478 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1479 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1480 G_DBUS_CALL_FLAGS_NONE,
1485 g_variant_unref(ret);
1487 g_object_unref(device_proxy);
1490 BT_ERR("SetProperty error: [%s]", error->message);
1491 g_error_free(error);
1492 return BLUETOOTH_ERROR_INTERNAL;
1495 return BLUETOOTH_ERROR_NONE;
1498 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1501 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1502 gchar *device_path = NULL;
1503 GDBusProxy *device_proxy;
1504 gboolean previous_value;
1505 GError *error = NULL;
1506 GDBusConnection *conn;
1507 GVariant *result = NULL;
1508 GVariant *temp = NULL;
1509 int ret = BLUETOOTH_ERROR_NONE;
1511 BT_CHECK_PARAMETER(device_address, return);
1513 conn = _bt_gdbus_get_system_gconn();
1514 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1516 _bt_convert_addr_type_to_string(address, device_address->addr);
1518 device_path = _bt_get_device_object_path(address);
1520 if (device_path == NULL) {
1521 BT_ERR("No paired device");
1522 return BLUETOOTH_ERROR_NOT_PAIRED;
1525 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1526 NULL, BT_BLUEZ_NAME,
1527 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1529 g_free(device_path);
1530 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1532 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1533 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1534 G_DBUS_CALL_FLAGS_NONE,
1538 if (error != NULL) {
1539 BT_ERR("Getting property failed: [%s]\n", error->message);
1540 g_error_free(error);
1541 g_object_unref(device_proxy);
1542 return BLUETOOTH_ERROR_INTERNAL;
1545 g_variant_get(result, "(v)", &temp);
1546 previous_value = g_variant_get_boolean(temp);
1547 g_variant_unref(temp);
1548 g_variant_unref(result);
1549 /* If the input is same with previous value, return error. */
1550 if (previous_value == authorize) {
1551 BT_ERR("Same value: %d", previous_value);
1552 g_object_unref(device_proxy);
1553 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1557 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1558 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1559 G_DBUS_CALL_FLAGS_NONE,
1564 g_object_unref(device_proxy);
1566 BT_ERR("SetProperty error: [%s]", error->message);
1567 g_error_free(error);
1568 ret = BLUETOOTH_ERROR_INTERNAL;
1572 g_variant_unref(result);
1577 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1578 gboolean *is_connected)
1580 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1581 char *object_path = NULL;
1583 GDBusProxy *device_proxy;
1584 GError *error = NULL;
1586 GVariant *tmp_value;
1587 GDBusConnection *conn;
1588 GVariant *result = NULL;
1589 int ret = BLUETOOTH_ERROR_NONE;
1591 BT_CHECK_PARAMETER(device_address, return);
1593 conn = _bt_gdbus_get_system_gconn();
1594 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1596 _bt_convert_addr_type_to_string(address, device_address->addr);
1598 object_path = _bt_get_device_object_path(address);
1599 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1601 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1602 NULL, BT_BLUEZ_NAME, object_path,
1603 BT_PROPERTIES_INTERFACE, NULL, NULL);
1604 g_free(object_path);
1605 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1607 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1608 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1609 G_DBUS_CALL_FLAGS_NONE,
1613 if (result == NULL) {
1614 if (error != NULL) {
1615 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1616 g_error_free(error);
1618 g_object_unref(device_proxy);
1619 return BLUETOOTH_ERROR_INTERNAL;
1622 g_variant_get(result , "(@a{sv})", &value);
1623 g_variant_unref(result);
1625 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1626 if (tmp_value == NULL) {
1627 g_object_unref(device_proxy);
1628 g_variant_unref(value);
1629 return BLUETOOTH_ERROR_INTERNAL;
1632 *is_connected = g_variant_get_boolean(tmp_value);
1634 BT_DBG("gatt is connected : %d", *is_connected);
1636 g_variant_unref(tmp_value);
1637 g_variant_unref(value);
1638 g_object_unref(device_proxy);
1643 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1644 int connection_type, gboolean *is_connected)
1646 char *object_path = NULL;
1647 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1648 GDBusProxy *device_proxy = NULL;
1649 GDBusProxy *adapter_proxy = NULL;
1650 GDBusConnection *conn;
1651 GError *error = NULL;
1652 GVariant *tmp_value = NULL;
1653 GVariant *value = NULL;
1654 GVariant *result = NULL;
1657 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1658 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1660 *is_connected = FALSE;
1661 BT_DBG("connection_type: %d", connection_type);
1662 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1663 return _bt_rfcomm_is_device_connected(device_address,
1665 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1666 return _bt_is_gatt_connected(device_address, is_connected);
1667 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1668 return _bt_pbap_is_connected(device_address, is_connected);
1670 adapter_proxy = _bt_get_adapter_proxy();
1671 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1673 conn = _bt_gdbus_get_system_gconn();
1674 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1676 _bt_convert_addr_type_to_string(address, device_address->addr);
1678 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1679 object_path = _bt_get_adapter_path();
1680 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1681 NULL, BT_BLUEZ_NAME,
1682 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1683 g_free(object_path);
1684 if (device_proxy == NULL) {
1685 BT_DBG("Device don't have this service");
1686 return BLUETOOTH_ERROR_INTERNAL;
1689 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1690 g_variant_new("(s)", address),
1691 G_DBUS_CALL_FLAGS_NONE,
1696 if (result == NULL) {
1697 BT_ERR("[GetProperties] Error occured in Proxy call");
1699 BT_ERR("%s", error->message);
1700 g_error_free(error);
1702 *is_connected = FALSE;
1703 g_object_unref(device_proxy);
1704 return BLUETOOTH_ERROR_NONE;
1706 g_variant_get(result , "(@a{sv})", &value);
1707 g_variant_unref(result);
1710 tmp_value = g_variant_lookup_value(value,
1712 G_VARIANT_TYPE_BOOLEAN);
1714 *is_connected = g_variant_get_boolean(tmp_value);
1715 g_variant_unref(tmp_value);
1717 g_variant_unref(value);
1719 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1720 return _bt_is_network_connected(_bt_get_net_conn(),
1721 device_address->addr, is_connected);
1723 uuid = _bt_get_profile_uuid128(connection_type);
1725 BT_ERR("uuid is NULL");
1726 return BLUETOOTH_ERROR_INTERNAL;
1729 BT_DBG("uuid: %s", uuid);
1731 object_path = _bt_get_device_object_path(address);
1732 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1733 BT_DBG("object_path: %s", object_path);
1734 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1735 NULL, BT_BLUEZ_NAME,
1736 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1737 g_free(object_path);
1738 if (device_proxy == NULL) {
1739 BT_DBG("Device don't have this service");
1741 return BLUETOOTH_ERROR_INTERNAL;
1744 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1745 g_variant_new("(s)", uuid),
1746 G_DBUS_CALL_FLAGS_NONE,
1751 if (result == NULL) {
1752 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1754 BT_ERR("%s", error->message);
1755 if (g_strrstr(error->message, "Not Connected"))
1756 BT_DBG("Not connected");
1757 g_error_free(error);
1759 *is_connected = FALSE;
1760 g_object_unref(device_proxy);
1762 return BLUETOOTH_ERROR_NONE;
1764 g_variant_get(result, "(b)", is_connected);
1766 g_variant_unref(result);
1769 g_object_unref(device_proxy);
1770 return BLUETOOTH_ERROR_NONE;
1773 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1774 bluetooth_connected_link_t *connected)
1776 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1777 char *object_path = NULL;
1779 GDBusProxy *device_proxy;
1780 GError *error = NULL;
1781 GDBusConnection *conn;
1782 GVariant *tmp_value = NULL;
1783 GVariant *value = NULL;
1784 GVariant *result = NULL;
1786 BT_CHECK_PARAMETER(device_address, return);
1788 conn = _bt_gdbus_get_system_gconn();
1789 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1791 _bt_convert_addr_type_to_string(address, device_address->addr);
1793 object_path = _bt_get_device_object_path(address);
1794 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1796 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1797 NULL, BT_BLUEZ_NAME,
1798 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1799 g_free(object_path);
1800 if (device_proxy == NULL) {
1801 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1802 return BLUETOOTH_ERROR_NONE;
1805 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1806 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1807 G_DBUS_CALL_FLAGS_NONE,
1812 if (error != NULL) {
1813 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1814 g_error_free(error);
1815 g_object_unref(device_proxy);
1816 return BLUETOOTH_ERROR_INTERNAL;
1819 g_variant_get(result , "(@a{sv})", &value);
1820 g_variant_unref(result);
1822 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1823 if (tmp_value != NULL) {
1824 *connected = g_variant_get_byte(tmp_value);
1825 g_variant_unref(tmp_value);
1826 g_object_unref(device_proxy);
1827 return BLUETOOTH_ERROR_NONE;
1829 BT_ERR("g_variant value is NULL");
1830 return BLUETOOTH_ERROR_INTERNAL;
1834 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1838 GVariant *out_param1;
1840 int result = BLUETOOTH_ERROR_NONE;
1841 bt_function_data_t *func_data = user_data;
1842 request_info_t *req_info = NULL;
1843 bluetooth_device_address_t device_addr = { {0} };
1845 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1846 g_object_unref(proxy);
1848 if (reply == NULL) {
1849 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1851 BT_ERR("Error: %s", err->message);
1852 g_clear_error(&err);
1854 result = BLUETOOTH_ERROR_INTERNAL;
1856 g_variant_unref(reply);
1859 if (func_data == NULL) {
1860 BT_ERR("func_data is NULL");
1864 req_info = _bt_get_request_info(func_data->req_id);
1865 if (req_info == NULL) {
1866 BT_ERR("req_info is NULL");
1870 if (req_info->context == NULL) {
1871 BT_ERR("req_info->context is NULL");
1875 _bt_convert_addr_string_to_type(device_addr.addr,
1876 (const char *)func_data->address);
1878 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1879 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1882 g_dbus_method_invocation_return_value(req_info->context,
1883 g_variant_new("(iv)", result, out_param1));
1887 _bt_delete_request_list(req_info->req_id);
1890 g_free(func_data->address);
1895 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1896 gboolean auto_connect)
1898 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1899 gchar *device_path = NULL;
1900 GDBusProxy *device_proxy = NULL;
1901 GDBusConnection *conn;
1902 int ret = BLUETOOTH_ERROR_NONE;
1903 bt_function_data_t *func_data;
1905 BT_CHECK_PARAMETER(bd_addr, return);
1907 conn = _bt_gdbus_get_system_gconn();
1908 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1910 _bt_convert_addr_type_to_string(device_address,
1911 (unsigned char *)bd_addr->addr);
1912 device_path = _bt_get_device_object_path(device_address);
1913 if (device_path == NULL) {
1914 BT_ERR_C("device_path NULL : [%s]", device_address);
1915 ret = BLUETOOTH_ERROR_INTERNAL;
1919 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1920 NULL, BT_BLUEZ_NAME,
1921 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1922 g_free(device_path);
1923 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1925 func_data = g_malloc0(sizeof(bt_function_data_t));
1926 if (func_data == NULL) {
1927 BT_ERR("Unable to allocate memory for address");
1928 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1932 func_data->address = g_strdup(device_address);
1933 if (func_data->address == NULL) {
1934 BT_ERR("Unable to allocate memory for address");
1935 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
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 };
2016 GDBusProxy *device_proxy;
2017 GDBusConnection *conn;
2018 int ret = BLUETOOTH_ERROR_NONE;
2019 bt_function_data_t *func_data;
2021 BT_CHECK_PARAMETER(bd_addr, return);
2023 conn = _bt_gdbus_get_system_gconn();
2024 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2026 _bt_convert_addr_type_to_string(device_address,
2027 (unsigned char *)bd_addr->addr);
2028 device_path = _bt_get_device_object_path(device_address);
2029 if (device_path == NULL) {
2030 BT_DBG("device_path NULL");
2031 ret = BLUETOOTH_ERROR_INTERNAL;
2035 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2037 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2038 NULL, BT_BLUEZ_NAME,
2039 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2040 g_free(device_path);
2041 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2043 func_data = g_malloc0(sizeof(bt_function_data_t));
2044 if (func_data == NULL) {
2045 BT_ERR("Unable to allocate memory for address");
2046 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2050 func_data->address = g_strdup(device_address);
2051 if (func_data->address == NULL) {
2052 BT_ERR("Unable to allocate memory for address");
2053 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2057 func_data->req_id = req_id;
2059 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2061 G_DBUS_CALL_FLAGS_NONE,
2062 BT_MAX_DBUS_TIMEOUT,
2064 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2069 g_object_unref(device_proxy);
2071 g_free(func_data->address);
2077 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2079 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2080 gchar *device_path = NULL;
2081 GError *error = NULL;
2082 GDBusProxy *device_proxy = NULL;
2083 GDBusProxy *adapter_proxy;
2084 GDBusConnection *conn;
2085 int ret = BLUETOOTH_ERROR_NONE;
2087 BT_CHECK_PARAMETER(bd_addr, return);
2089 _bt_convert_addr_type_to_string(device_address,
2090 (unsigned char *)bd_addr->addr);
2092 conn = _bt_gdbus_get_system_gconn();
2093 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2095 adapter_proxy = _bt_get_adapter_proxy();
2096 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2098 device_path = _bt_get_device_object_path(device_address);
2099 if (device_path == NULL) {
2100 BT_DBG("device_path NULL");
2101 ret = BLUETOOTH_ERROR_INTERNAL;
2105 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2107 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2108 NULL, BT_BLUEZ_NAME,
2109 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2110 g_free(device_path);
2111 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2113 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2115 G_DBUS_CALL_FLAGS_NONE,
2120 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2121 g_error_free(error);
2122 g_object_unref(device_proxy);
2123 return BLUETOOTH_ERROR_INTERNAL;
2126 g_object_unref(device_proxy);
2131 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2133 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2134 gchar *device_path = NULL;
2135 GError *error = NULL;
2136 GDBusProxy *device_proxy = NULL;
2137 GDBusProxy *adapter_proxy;
2138 GDBusConnection *conn;
2139 int ret = BLUETOOTH_ERROR_NONE;
2141 BT_CHECK_PARAMETER(bd_addr, return);
2143 _bt_convert_addr_type_to_string(device_address,
2144 (unsigned char *)bd_addr->addr);
2146 conn = _bt_gdbus_get_system_gconn();
2147 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2149 adapter_proxy = _bt_get_adapter_proxy();
2150 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2152 device_path = _bt_get_device_object_path(device_address);
2153 if (device_path == NULL) {
2154 BT_DBG("device_path NULL");
2155 ret = BLUETOOTH_ERROR_INTERNAL;
2159 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2161 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2162 NULL, BT_BLUEZ_NAME,
2163 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2164 g_free(device_path);
2165 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2167 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2169 G_DBUS_CALL_FLAGS_NONE,
2174 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2175 g_error_free(error);
2176 g_object_unref(device_proxy);
2177 return BLUETOOTH_ERROR_INTERNAL;
2180 g_object_unref(device_proxy);
2185 int _bt_connect_profile(char *address, char *uuid,
2186 void *cb, gpointer func_data)
2190 GDBusConnection *conn;
2191 GDBusProxy *adapter_proxy;
2192 GVariant *result = NULL;
2193 GError *error = NULL;
2195 conn = _bt_gdbus_get_system_gconn();
2196 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2198 object_path = _bt_get_device_object_path(address);
2199 if (object_path == NULL) {
2200 BT_ERR("No searched device");
2202 adapter_proxy = _bt_get_adapter_proxy();
2203 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2205 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2206 g_variant_new("(s)", address),
2207 G_DBUS_CALL_FLAGS_NONE,
2212 if (error != NULL) {
2213 BT_ERR("CreateDevice Fail: %s", error->message);
2214 g_error_free(error);
2217 g_variant_unref(result);
2219 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, "ConnectProfile",
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_disconnect_all(char *address)
2242 int ret = BLUETOOTH_ERROR_NONE;
2245 GDBusConnection *conn;
2246 GVariant *result = NULL;
2250 conn = _bt_gdbus_get_system_gconn();
2251 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2253 object_path = _bt_get_device_object_path(address);
2254 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2256 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2257 NULL, BT_BLUEZ_NAME,
2258 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2259 g_free(object_path);
2260 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2262 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2264 G_DBUS_CALL_FLAGS_NONE,
2269 BT_ERR("Dbus Call Error:[%s]", err->message);
2271 ret = BLUETOOTH_ERROR_INTERNAL;
2274 g_object_unref(proxy);
2276 g_variant_unref(result);
2281 int _bt_disconnect_profile(char *address, char *uuid,
2282 void *cb, gpointer func_data)
2286 GDBusConnection *conn;
2288 conn = _bt_gdbus_get_system_gconn();
2289 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2291 object_path = _bt_get_device_object_path(address);
2292 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2294 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2295 NULL, BT_BLUEZ_NAME,
2296 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2297 g_free(object_path);
2298 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2300 g_dbus_proxy_call(proxy, "DisconnectProfile",
2301 g_variant_new("(s)", uuid),
2302 G_DBUS_CALL_FLAGS_NONE,
2303 BT_MAX_DBUS_TIMEOUT,
2305 (GAsyncReadyCallback)cb,
2308 return BLUETOOTH_ERROR_NONE;
2311 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2312 int low_threshold, int in_range_threshold, int high_threshold)
2314 int ret = BLUETOOTH_ERROR_NONE;
2316 GVariant *result = NULL;
2317 GError *error = NULL;
2318 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2320 BT_CHECK_PARAMETER(bd_addr, return);
2321 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2322 bd_addr->addr[0], bd_addr->addr[1],
2323 bd_addr->addr[2], bd_addr->addr[3],
2324 bd_addr->addr[4], bd_addr->addr[5],
2326 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2327 in_range_threshold, high_threshold);
2329 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2331 proxy = _bt_get_adapter_proxy();
2332 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2334 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2335 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2336 G_DBUS_CALL_FLAGS_NONE,
2340 if (error != NULL) {
2341 BT_ERR("Dbus Call Error:[%s]", error->message);
2342 g_error_free(error);
2343 ret = BLUETOOTH_ERROR_INTERNAL;
2347 g_variant_unref(result);
2352 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2355 int ret = BLUETOOTH_ERROR_NONE;
2357 GVariant *result = NULL;
2358 GError *error = NULL;
2359 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2361 BT_CHECK_PARAMETER(bd_addr, return);
2362 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2363 bd_addr->addr[0], bd_addr->addr[1],
2364 bd_addr->addr[2], bd_addr->addr[3],
2365 bd_addr->addr[4], bd_addr->addr[5],
2368 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2370 proxy = _bt_get_adapter_proxy();
2371 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2373 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2374 g_variant_new("(si)", address, link_type),
2375 G_DBUS_CALL_FLAGS_NONE,
2380 if (error != NULL) {
2381 BT_ERR("Dbus Call Error:[%s]", error->message);
2382 g_error_free(error);
2383 ret = BLUETOOTH_ERROR_INTERNAL;
2387 g_variant_unref(result);
2392 int _bt_le_conn_update(unsigned char *device_address,
2393 float interval_min, float interval_max,
2394 guint16 latency, guint16 time_out)
2396 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2397 gchar *device_path = NULL;
2398 GError *error = NULL;
2399 GDBusProxy *device_proxy = NULL;
2400 GDBusConnection *conn;
2402 guint32 min, max, to;
2403 guint32 min_supervision_to;
2404 int ret = BLUETOOTH_ERROR_NONE;
2406 BT_CHECK_PARAMETER(device_address, return);
2408 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2409 interval_min, interval_max, latency, time_out);
2411 if (interval_min > interval_max ||
2412 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2413 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2414 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2418 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2419 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2420 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2424 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2425 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2430 * The Supervision_Timeout in milliseconds shall be larger than
2431 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2432 * where Conn_Interval_Max is given in milliseconds.
2434 min_supervision_to = (1 + latency) * interval_max * 2;
2435 if (time_out <= min_supervision_to) {
2436 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2440 _bt_convert_addr_type_to_string(address, device_address);
2442 BT_DBG("Remote device address: %s", address);
2444 device_path = _bt_get_device_object_path(address);
2446 if (device_path == NULL) {
2447 BT_ERR("device_path NULL");
2448 ret = BLUETOOTH_ERROR_INTERNAL;
2452 conn = _bt_gdbus_get_system_gconn();
2454 BT_ERR("conn NULL");
2455 ret = BLUETOOTH_ERROR_INTERNAL;
2459 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2460 NULL, BT_BLUEZ_NAME,
2461 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2463 g_free(device_path);
2464 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2466 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2467 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2468 to = time_out / BT_LE_CONN_TO_SPLIT;
2470 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2471 g_variant_new("(uuuu)", min, max, latency, to),
2472 G_DBUS_CALL_FLAGS_NONE,
2477 g_object_unref(device_proxy);
2478 if (reply == NULL) {
2480 BT_ERR("Error %s[%s]", error->message, address);
2481 if (g_strrstr(error->message, "In Progress"))
2482 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2484 ret = BLUETOOTH_ERROR_INTERNAL;
2485 g_error_free(error);
2489 g_variant_unref(reply);
2495 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2496 bluetooth_device_pin_code_t *pin_code)
2498 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2500 bt_pin_code_info_t *pin_info = NULL;
2502 BT_CHECK_PARAMETER(device_address, return);
2503 BT_CHECK_PARAMETER(pin_code, return);
2504 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2505 BLUETOOTH_ERROR_NO_RESOURCES);
2507 _bt_convert_addr_type_to_string(address, device_address->addr);
2509 for (l = pin_info_list; l != NULL; l = l->next) {
2512 if (g_strcmp0(pin_info->address, address) == 0) {
2513 g_free(pin_info->pin_code);
2514 pin_info->pin_code = g_strdup(pin_code->pin_code);
2515 return BLUETOOTH_ERROR_NONE;
2519 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2520 pin_info->address = g_strdup(address);
2521 pin_info->pin_code = g_strdup(pin_code->pin_code);
2522 pin_info_list = g_slist_append(pin_info_list, pin_info);
2524 return BLUETOOTH_ERROR_NONE;
2527 gint __bt_compare_address(gpointer *a, gpointer *b)
2529 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2530 char *address = (char *)b;
2531 return g_strcmp0(pin_info->address, address);
2534 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2536 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2538 bt_pin_code_info_t *pin_info = NULL;
2540 BT_CHECK_PARAMETER(device_address, return);
2542 _bt_convert_addr_type_to_string(address, device_address->addr);
2544 l = g_slist_find_custom(pin_info_list, address,
2545 (GCompareFunc)__bt_compare_address);
2549 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2550 g_free(pin_info->address);
2551 g_free(pin_info->pin_code);
2555 return BLUETOOTH_ERROR_NONE;
2558 int _bt_get_device_pin_code(const char *address, char *pin_code)
2562 BT_CHECK_PARAMETER(address, return);
2563 BT_CHECK_PARAMETER(pin_code, return);
2565 for (l = pin_info_list; l != NULL; l = l->next) {
2566 bt_pin_code_info_t *pin_info = l->data;
2568 if (g_strcmp0(pin_info->address, address) == 0) {
2569 g_strlcpy(pin_code, pin_info->pin_code,
2570 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2572 return BLUETOOTH_ERROR_NONE;
2576 return BLUETOOTH_ERROR_NOT_FOUND;
2579 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2580 bluetooth_le_connection_param_t *param)
2583 return BLUETOOTH_ERROR_INVALID_PARAM;
2585 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2586 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2587 return BLUETOOTH_ERROR_INVALID_PARAM;
2589 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2592 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2593 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2594 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2595 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2596 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2599 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2600 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2601 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2602 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2603 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2606 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2607 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2608 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2609 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2610 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2614 BT_ERR("Unhandled mode : %d", mode);
2618 return BLUETOOTH_ERROR_NONE;
2621 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2622 guint trusted_profile_flag, guint *trusted)
2628 case TRUSTED_PROFILE_PBAP:
2629 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2630 trust_profile = trusted_profile_flag & (1 << 1);
2632 return BLUETOOTH_ERROR_NOT_SUPPORT;
2634 case TRUSTED_PROFILE_MAP:
2635 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2636 trust_profile = trusted_profile_flag & (1 << 3);
2638 return BLUETOOTH_ERROR_NOT_SUPPORT;
2640 case TRUSTED_PROFILE_SAP:
2641 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2642 trust_profile = trusted_profile_flag & (1 << 5);
2644 return BLUETOOTH_ERROR_NOT_SUPPORT;
2646 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2647 *trusted = trusted_profile_flag;
2648 return BLUETOOTH_ERROR_NONE;
2650 return BLUETOOTH_ERROR_NOT_SUPPORT;
2656 return BLUETOOTH_ERROR_NONE;
2659 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2660 guint restricted_profile_flag, guint *restricted)
2662 int restrict_profile;
2663 *restricted = FALSE;
2666 case RESTRICTED_PROFILE_HFP_HS:
2667 restrict_profile = restricted_profile_flag & (1 << 0);
2669 case RESTRICTED_PROFILE_A2DP:
2670 restrict_profile = restricted_profile_flag & (1 << 2);
2673 return BLUETOOTH_ERROR_NOT_SUPPORT;
2676 if (restrict_profile)
2679 return BLUETOOTH_ERROR_NONE;
2682 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2685 case TRUSTED_PROFILE_PBAP:
2686 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2687 case TRUSTED_PROFILE_MAP:
2688 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2689 case TRUSTED_PROFILE_SAP:
2690 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2691 case TRUSTED_PROFILE_ALL:
2698 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2701 case RESTRICTED_PROFILE_HFP_HS:
2702 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2703 case RESTRICTED_PROFILE_A2DP:
2704 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2710 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2712 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2713 return TRUSTED_PROFILE_PBAP;
2714 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2715 return TRUSTED_PROFILE_MAP;
2716 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2717 return TRUSTED_PROFILE_SAP;
2719 return 0; /* 0 - Unknown Profile */
2722 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2723 bluetooth_trusted_profile_t profile, gboolean trust)
2725 int ret = BLUETOOTH_ERROR_NONE;
2726 GDBusConnection *conn;
2728 GError *error = NULL;
2729 char *device_path = NULL;
2731 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2734 BT_CHECK_PARAMETER(bd_addr, return);
2735 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2736 bd_addr->addr[0], bd_addr->addr[1],
2737 bd_addr->addr[2], bd_addr->addr[3],
2738 bd_addr->addr[4], bd_addr->addr[5],
2741 conn = _bt_gdbus_get_system_gconn();
2742 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2744 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2746 device_path = _bt_get_device_object_path(address);
2747 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2749 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2750 NULL, BT_BLUEZ_NAME, device_path,
2751 BT_DEVICE_INTERFACE, NULL, NULL);
2753 g_free(device_path);
2754 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2756 uuid = _bt_get_trusted_profile_uuid(profile);
2758 g_object_unref(proxy);
2759 return BLUETOOTH_ERROR_NOT_SUPPORT;
2762 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2763 g_variant_new("(sb)", uuid, trust),
2764 G_DBUS_CALL_FLAGS_NONE, -1,
2766 g_object_unref(proxy);
2768 if (reply == NULL) {
2769 BT_ERR("Failed to Set Profile Trusted");
2770 ret = BLUETOOTH_ERROR_INTERNAL;
2772 BT_ERR("Error %s[%s]", error->message, address);
2773 g_error_free(error);
2777 g_variant_unref(reply);
2784 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2785 bluetooth_trusted_profile_t profile, guint *trust)
2787 int ret = BLUETOOTH_ERROR_NONE;
2788 GDBusConnection *conn;
2790 GError *error = NULL;
2791 char *device_path = NULL;
2792 guint trusted_profile_flag;
2793 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2796 BT_CHECK_PARAMETER(bd_addr, return);
2797 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2798 bd_addr->addr[0], bd_addr->addr[1],
2799 bd_addr->addr[2], bd_addr->addr[3],
2800 bd_addr->addr[4], bd_addr->addr[5],
2803 conn = _bt_gdbus_get_system_gconn();
2804 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2806 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2808 device_path = _bt_get_device_object_path(address);
2809 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2811 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2812 NULL, BT_BLUEZ_NAME, device_path,
2813 BT_PROPERTIES_INTERFACE, NULL, NULL);
2815 g_free(device_path);
2816 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2818 reply = g_dbus_proxy_call_sync(proxy, "Get",
2819 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2820 G_DBUS_CALL_FLAGS_NONE, -1,
2822 g_object_unref(proxy);
2824 if (reply == NULL) {
2825 BT_ERR("Failed to Get Profile Trusted");
2826 ret = BLUETOOTH_ERROR_INTERNAL;
2828 BT_ERR("Error %s[%s]", error->message, address);
2829 g_error_free(error);
2834 g_variant_get(reply, "(v)", &temp);
2835 trusted_profile_flag = g_variant_get_uint32(temp);
2836 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2838 ret = _bt_get_trusted_profile_from_flag(profile,
2839 trusted_profile_flag, trust);
2840 g_variant_unref(temp);
2841 g_variant_unref(reply);
2844 BT_DBG("TRUST %d", *trust);
2848 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2849 bluetooth_restricted_profile_t profile, gboolean restricted)
2851 int ret = BLUETOOTH_ERROR_NONE;
2852 GDBusConnection *conn;
2854 GError *error = NULL;
2855 char *device_path = NULL;
2857 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2860 BT_CHECK_PARAMETER(bd_addr, return);
2861 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2862 bd_addr->addr[0], bd_addr->addr[1],
2863 bd_addr->addr[2], bd_addr->addr[3],
2864 bd_addr->addr[4], bd_addr->addr[5],
2865 profile, restricted);
2867 conn = _bt_gdbus_get_system_gconn();
2868 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2870 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2872 device_path = _bt_get_device_object_path(address);
2873 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2875 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2876 NULL, BT_BLUEZ_NAME, device_path,
2877 BT_DEVICE_INTERFACE, NULL, NULL);
2879 g_free(device_path);
2880 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2882 uuid = _bt_get_restricted_profile_uuid(profile);
2884 g_object_unref(proxy);
2885 return BLUETOOTH_ERROR_NOT_SUPPORT;
2888 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2889 g_variant_new("(sb)", uuid, restricted),
2890 G_DBUS_CALL_FLAGS_NONE, -1,
2892 g_object_unref(proxy);
2894 if (reply == NULL) {
2895 BT_ERR("Failed to Set Profile Restricted");
2896 ret = BLUETOOTH_ERROR_INTERNAL;
2898 BT_ERR("Error %s[%s]", error->message, address);
2899 g_error_free(error);
2903 g_variant_unref(reply);
2910 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2911 bluetooth_restricted_profile_t profile, guint *restricted)
2913 int ret = BLUETOOTH_ERROR_NONE;
2914 GDBusConnection *conn;
2916 GError *error = NULL;
2917 char *device_path = NULL;
2918 guint restricted_profile_flag;
2919 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2922 BT_CHECK_PARAMETER(bd_addr, return);
2923 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2924 bd_addr->addr[0], bd_addr->addr[1],
2925 bd_addr->addr[2], bd_addr->addr[3],
2926 bd_addr->addr[4], bd_addr->addr[5],
2927 profile, *restricted);
2929 conn = _bt_gdbus_get_system_gconn();
2930 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2932 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2934 device_path = _bt_get_device_object_path(address);
2935 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2937 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2938 NULL, BT_BLUEZ_NAME, device_path,
2939 BT_PROPERTIES_INTERFACE, NULL, NULL);
2941 g_free(device_path);
2942 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2944 reply = g_dbus_proxy_call_sync(proxy, "Get",
2945 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2946 G_DBUS_CALL_FLAGS_NONE, -1,
2948 g_object_unref(proxy);
2950 if (reply == NULL) {
2951 BT_ERR("Failed to Get Profile Restricted");
2952 ret = BLUETOOTH_ERROR_INTERNAL;
2954 BT_ERR("Error %s[%s]", error->message, address);
2955 g_error_free(error);
2960 g_variant_get(reply, "(v)", &temp);
2961 restricted_profile_flag = g_variant_get_uint32(temp);
2962 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2964 ret = _bt_get_restricted_profile_from_flag(profile,
2965 restricted_profile_flag, restricted);
2966 g_variant_unref(temp);
2967 g_variant_unref(reply);
2970 BT_DBG("TRUST %d", *restricted);
2974 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2978 GVariant *out_param1;
2979 request_info_t *req_info;
2980 GVariant *val = NULL;
2981 GVariant *param = NULL;
2984 bluetooth_device_address_t device_addr = { {0} };
2985 int result = BLUETOOTH_ERROR_NONE;
2988 val = g_dbus_proxy_call_finish(proxy, res, &err);
2990 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2991 if (req_info == NULL) {
2992 BT_ERR("req_info == NULL");
2993 g_object_unref(proxy);
2994 att_mtu_req_info->device_proxy = NULL;
2999 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3001 if (g_strrstr(err->message, "NotSupported")) {
3002 BT_INFO("Connection Not Supported");
3003 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3004 } else if (g_strrstr(err->message, "NotConnected")) {
3005 BT_INFO("Not connected");
3006 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3007 } else if (g_strrstr(err->message, "InvalidArguments")) {
3008 BT_INFO("Not connected");
3009 result = BLUETOOTH_ERROR_INVALID_PARAM;
3011 BT_DBG("Default case");
3012 result = BLUETOOTH_ERROR_INTERNAL;
3016 g_object_unref(proxy);
3017 att_mtu_req_info->device_proxy = NULL;
3019 if (result != BLUETOOTH_ERROR_NONE)
3023 g_variant_get(val, "(qy)", &mtu, &status);
3024 g_variant_unref(val);
3027 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3029 param = g_variant_new("(isqy)",
3031 att_mtu_req_info->addr,
3035 /* Send the event to application */
3036 _bt_send_event(BT_DEVICE_EVENT,
3037 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3041 if (req_info->context == NULL)
3044 _bt_convert_addr_string_to_type(device_addr.addr,
3045 (const char *)att_mtu_req_info->addr);
3047 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3048 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3049 g_dbus_method_invocation_return_value(req_info->context,
3050 g_variant_new("(iv)", result, out_param1));
3052 _bt_delete_request_list(req_info->req_id);
3057 g_free(att_mtu_req_info->addr);
3058 g_free(att_mtu_req_info);
3059 att_mtu_req_info = NULL;
3064 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3067 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3068 gchar *device_path = NULL;
3069 GDBusProxy *adapter_proxy;
3070 GDBusProxy *device_proxy;
3071 GDBusConnection *conn;
3072 int ret = BLUETOOTH_ERROR_NONE;
3074 BT_CHECK_PARAMETER(device_address, return);
3076 if (att_mtu_req_info) {
3077 BT_ERR("ATT MTU request in progress");
3078 return BLUETOOTH_ERROR_DEVICE_BUSY;
3081 conn = _bt_gdbus_get_system_gconn();
3082 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3084 adapter_proxy = _bt_get_adapter_proxy();
3085 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3087 _bt_convert_addr_type_to_string(address, device_address->addr);
3089 BT_DBG("Remote device address: %s", address);
3091 device_path = _bt_get_device_object_path(address);
3093 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3095 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3096 NULL, BT_BLUEZ_NAME,
3097 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3098 g_free(device_path);
3099 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3101 if (device_path == NULL) {
3102 BT_DBG("device_path NULL");
3103 ret = BLUETOOTH_ERROR_INTERNAL;
3107 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3108 att_mtu_req_info->addr = (char *)g_strdup(address);
3109 att_mtu_req_info->req_id = request_id;
3110 att_mtu_req_info->device_proxy = device_proxy;
3112 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3113 g_variant_new("(q)", mtu),
3114 G_DBUS_CALL_FLAGS_NONE,
3115 BT_MAX_DBUS_TIMEOUT,
3117 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3123 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3126 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3127 char *object_path = NULL;
3129 GDBusProxy *device_proxy;
3130 GError *error = NULL;
3132 GVariant *tmp_value;
3133 GDBusConnection *conn;
3134 GVariant *result = NULL;
3135 int ret = BLUETOOTH_ERROR_NONE;
3137 BT_CHECK_PARAMETER(device_address, return);
3139 conn = _bt_gdbus_get_system_gconn();
3140 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3142 _bt_convert_addr_type_to_string(address, device_address->addr);
3144 object_path = _bt_get_device_object_path(address);
3145 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3147 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3148 NULL, BT_BLUEZ_NAME, object_path,
3149 BT_PROPERTIES_INTERFACE, NULL, NULL);
3150 g_free(object_path);
3151 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3153 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3154 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3155 G_DBUS_CALL_FLAGS_NONE,
3159 if (result == NULL) {
3160 if (error != NULL) {
3161 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3162 g_error_free(error);
3164 g_object_unref(device_proxy);
3165 return BLUETOOTH_ERROR_INTERNAL;
3168 g_variant_get(result , "(@a{sv})", &value);
3169 g_variant_unref(result);
3171 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3172 if (tmp_value == NULL) {
3173 g_object_unref(device_proxy);
3174 g_variant_unref(value);
3175 return BLUETOOTH_ERROR_INTERNAL;
3178 *mtu = g_variant_get_uint16(tmp_value);
3180 BT_DBG("ATT MTU : %d", *mtu);
3182 g_variant_unref(tmp_value);
3183 g_variant_unref(value);
3184 g_object_unref(device_proxy);
3189 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3190 bluetooth_device_address_t *id_address)
3192 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3193 gchar *device_path = NULL;
3194 const gchar *idaddress = NULL;
3195 GDBusProxy *device_proxy;
3196 GError *error = NULL;
3197 GVariant *result = NULL;
3198 GDBusConnection *conn;
3199 int ret = BLUETOOTH_ERROR_NONE;
3201 BT_CHECK_PARAMETER(device_address, return);
3203 conn = _bt_gdbus_get_system_gconn();
3204 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3206 _bt_convert_addr_type_to_string(address, device_address->addr);
3208 device_path = _bt_get_device_object_path(address);
3209 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3211 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3212 NULL, BT_BLUEZ_NAME,
3213 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3214 g_free(device_path);
3215 if (!device_proxy) {
3216 BT_ERR("Unable to get proxy");
3217 return BLUETOOTH_ERROR_INTERNAL;
3220 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3222 G_DBUS_CALL_FLAGS_NONE,
3227 if (result == NULL) {
3228 BT_ERR("Failed to get device ID address");
3229 if (error != NULL) {
3230 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3231 g_error_free(error);
3233 g_object_unref(device_proxy);
3234 return BLUETOOTH_ERROR_INTERNAL;
3237 g_variant_get(result , "(s)", &idaddress);
3238 g_variant_unref(result);
3240 if (idaddress == NULL) {
3241 BT_ERR("No paired device");
3242 g_object_unref(device_proxy);
3243 return BLUETOOTH_ERROR_NOT_PAIRED;
3246 BT_DBG("ID Address:%s", idaddress);
3249 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3251 ret = BLUETOOTH_ERROR_INTERNAL;
3253 g_object_unref(device_proxy);
3258 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3260 GapAgentPrivate *agent = _bt_get_adapter_agent();
3261 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3263 if (authentication_reply)
3264 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3266 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3268 BT_DBG("BT_PASSKEY_REPLY");
3269 return BLUETOOTH_ERROR_NONE;
3272 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3274 GapAgentPrivate *agent = _bt_get_adapter_agent();
3275 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3277 if (confirmation_reply)
3278 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3280 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3282 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3283 return BLUETOOTH_ERROR_NONE;