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 #ifdef TIZEN_FEATURE_BT_DPM
43 #include "bt-service-dpm.h"
46 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
47 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
48 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
50 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
51 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
52 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
53 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
54 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
55 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
56 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
57 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
59 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
61 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
62 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
63 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
65 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
67 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
69 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
70 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
71 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
73 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
80 GDBusProxy *device_proxy;
81 GDBusProxy *adapter_proxy;
83 unsigned short conn_type;
84 gboolean bonding_wo_discovery;
94 bluetooth_device_address_t bd_addr;
95 gboolean auto_connect;
96 } bt_pending_le_conn_info_s;
103 } bt_connected_le_dev_t;
112 } bt_le_conn_param_t;
114 gboolean is_device_creating;
115 bt_funcion_data_t *bonding_info;
116 bt_funcion_data_t *searching_info;
117 bt_funcion_data_t *att_mtu_req_info;
119 static GSList *le_connected_dev_list = NULL;
120 static GSList *pin_info_list = NULL;
121 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
122 static guint pending_le_conn_timer_id = 0;
124 /* This HID Mouse does not support pairing precedure. need to skip it. */
125 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
127 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
130 static int __bt_retry_bond(void);
133 /*static void __bt_decline_pair_request()
135 GVariant *out_param1;
136 GVariant *out_param2;
137 request_info_t *req_info;
138 bluetooth_device_info_t dev_info;
139 bt_remote_dev_info_t *remote_dev_info;
140 GVariant *uuid_list, *manufacture_data;
145 req_info = _bt_get_request_info(bonding_info->req_id);
146 if (req_info == NULL) {
147 BT_ERR("req_info == NULL");
150 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
152 BT_DBG("bonding_info is NULL");
157 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
158 remote_dev_info->uuids, remote_dev_info->uuid_count,
161 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
162 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
165 param = g_variant_new("isunsbuba{s}na{y})",
166 bonding_info->result,
168 remote_dev_info->class,
169 remote_dev_info->rssi,
170 remote_dev_info->name,
171 remote_dev_info->paired,
172 remote_dev_info->connected,
173 remote_dev_info->trust,
175 remote_dev_info->manufacturer_data_len,
179 //Send the event to application
180 if (remote_dev_info != NULL) {
181 _bt_send_event(BT_ADAPTER_EVENT,
182 BLUETOOTH_EVENT_BONDING_FINISHED,
185 _bt_free_device_info(remote_dev_info);
188 if (req_info->context == NULL)
191 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
192 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
195 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
196 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
198 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
199 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
202 g_dbus_method_invocation_return_value(req_info->context,
203 g_variant_new("@ayi", out_param1, out_param2));
206 _bt_delete_request_list(req_info->req_id);
209 g_free(bonding_info->addr);
210 g_free(bonding_info);
216 #ifdef TIZEN_PROFILE_WEARABLE
217 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
221 retv_if(user_data == NULL, FALSE);
223 b = (bundle *)user_data;
225 ret = syspopup_launch("bt-syspopup", b);
227 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
229 BT_DBG("Hurray!!! Finally Popup launched");
232 return (ret < 0) ? TRUE : FALSE;
235 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
240 GDBusConnection *conn;
242 conn = _bt_gdbus_get_system_gconn();
250 bundle_add(b, "event-type", "unable-to-pairing");
252 if (result == BLUETOOTH_ERROR_TIMEOUT)
253 bundle_add(b, "error", "timeout");
254 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
255 bundle_add(b, "error", "authfailed");
257 bundle_add(b, "error", "error");
259 ret = syspopup_launch("bt-syspopup", b);
261 BT_ERR("Popup launch failed...retry %d \n", ret);
262 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
273 gboolean _bt_is_device_creating(void)
275 return is_device_creating;
278 gboolean _bt_is_bonding_device_address(const char *address)
280 if (bonding_info == NULL || bonding_info->addr == NULL)
283 if (g_strcmp0(bonding_info->addr, address) == 0) {
284 BT_DBG("[%s] is bonding device", address);
288 BT_DBG("[%s] is NOT bonding device", address);
292 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
294 ret_if(bonding_info == NULL);
295 bonding_info->is_autopair = is_autopair;
298 void __bt_cancel_search_service_done(void)
300 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
301 request_info_t *req_info;
302 bluetooth_device_info_t dev_info;
303 GVariant *out_param1;
305 ret_if(searching_info == NULL);
307 req_info = _bt_get_request_info(searching_info->req_id);
308 if (req_info == NULL) {
309 BT_ERR("req_info == NULL");
313 if (req_info->context == NULL)
316 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
317 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
318 searching_info->addr);
320 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
321 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
324 g_dbus_method_invocation_return_value(req_info->context,
325 g_variant_new("(iv)", result, out_param1));
327 _bt_delete_request_list(req_info->req_id);
331 g_free(searching_info->addr);
332 g_free(searching_info);
333 searching_info = NULL;
336 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
338 ret_if(value == NULL);
339 ret_if(info == NULL);
341 gsize uuid_count = 0;
343 info->uuids = g_variant_dup_strv(value, &uuid_count);
344 info->uuid_count = (unsigned int)uuid_count;
346 BT_DBG("uuid count : %d", uuid_count);
349 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
351 char *object_path = NULL;
352 bt_remote_dev_info_t *dev_info;
354 retv_if(address == NULL, NULL);
356 object_path = _bt_get_device_object_path(address);
357 retv_if(object_path == NULL, NULL);
359 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
365 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
366 const char *object_path)
368 bt_remote_dev_info_t *dev_info;
369 GDBusProxy *adapter_proxy;
370 GDBusProxy *device_proxy;
375 GDBusConnection *conn;
376 GError *error = NULL;
377 GVariant *result = NULL;
378 GVariantIter *value_iter;
382 adapter_proxy = _bt_get_adapter_proxy();
383 retv_if(adapter_proxy == NULL, NULL);
385 retv_if(object_path == NULL, NULL);
387 conn = _bt_gdbus_get_system_gconn();
389 BT_ERR("conn == NULL");
393 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
394 NULL, BT_BLUEZ_NAME, object_path,
395 BT_PROPERTIES_INTERFACE, NULL, NULL);
397 retv_if(device_proxy == NULL, NULL);
399 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
400 g_variant_new("(s)", BT_DEVICE_INTERFACE),
401 G_DBUS_CALL_FLAGS_NONE,
406 g_object_unref(device_proxy);
408 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
410 if (result != NULL) {
411 g_variant_get(result , "(@a{sv})", &value);
412 g_variant_unref(result);
414 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
416 g_variant_get(tmp_value, "s", &name);
417 g_variant_unref(tmp_value);
419 DBG_SECURE("Alias Name [%s]", name);
421 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
422 g_variant_get(tmp_value, "s", &name);
423 g_variant_unref(tmp_value);
426 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
428 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
429 g_variant_unref(tmp_value);
431 dev_info->is_alias_set = FALSE;
433 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
435 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
437 dev_info->class = g_variant_get_uint32(tmp_value);
438 g_variant_unref(tmp_value);
442 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
444 dev_info->connected = g_variant_get_byte(tmp_value);
445 g_variant_unref(tmp_value);
447 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
448 BT_DBG("connected link : %d", dev_info->connected);
450 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
452 dev_info->trust = g_variant_get_boolean(tmp_value);
453 g_variant_unref(tmp_value);
455 dev_info->trust = FALSE;
457 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
459 dev_info->paired = g_variant_get_boolean(tmp_value);
460 g_variant_unref(tmp_value);
462 dev_info->paired = FALSE;
464 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
466 dev_info->rssi = g_variant_get_int16(tmp_value);
467 g_variant_unref(tmp_value);
471 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
473 dev_info->addr_type = g_variant_get_byte(tmp_value);
474 g_variant_unref(tmp_value);
476 dev_info->addr_type = 0;
478 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
480 __bt_get_uuids(tmp_value, dev_info);
481 g_variant_unref(tmp_value);
484 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
486 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
487 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
488 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
489 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
491 g_variant_unref(tmp_value);
493 dev_info->manufacturer_data_len = 0;
495 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
497 if ((dev_info->manufacturer_data_len == 0) ||
498 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
499 BT_ERR("manufacturer data length doesn't match");
500 dev_info->manufacturer_data_len = 0;
501 dev_info->manufacturer_data = NULL;
503 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
504 g_variant_get(tmp_value, "ay", &value_iter);
505 while (g_variant_iter_loop(value_iter, "y", &m_value))
506 dev_info->manufacturer_data[i++] = m_value;
508 g_variant_unref(tmp_value);
510 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
511 dev_info->manufacturer_data_len = 0;
512 dev_info->manufacturer_data = NULL;
515 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
516 g_variant_get(tmp_value, "s", &address);
517 g_variant_unref(tmp_value);
519 dev_info->address = g_strdup(address);
520 dev_info->name = g_strdup(name);
522 g_variant_unref(value);
524 BT_ERR("result is NULL\n");
532 char *_bt_get_bonded_device_name(char *address)
534 bluetooth_device_address_t device_address = { {0} };
535 bluetooth_device_info_t dev_info;
537 retv_if(address == NULL, strdup(""));
539 _bt_convert_addr_string_to_type(device_address.addr, address);
541 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
543 _bt_get_bonded_device_info(&device_address, &dev_info);
545 return g_strdup(dev_info.device_name.name);
548 static gboolean __ignore_auto_pairing_request(const char *address)
553 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
564 /* Get the LAP(Lower Address part) */
565 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
567 snprintf(lap_address, sizeof(lap_address), ",%s", address);
569 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
572 BT_ERR("fopen failed \n");
576 fseek(fp, 0, SEEK_END);
581 BT_ERR("Get file size failed \n");
586 buffer = g_malloc0(sizeof(char) * size);
587 result = fread((char *)buffer, 1, size, fp);
589 if (result != size) {
590 BT_ERR("Read Error\n");
595 BT_DBG("Buffer = %s\n", buffer);
597 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
603 /* Write the data and insert new device data */
604 for (i = 0; lines[i] != NULL; i++) {
605 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
606 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
608 lines[i] = temp_buffer;
611 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
613 /* Fix : NULL_RETURNS */
614 retv_if(buffer == NULL, FALSE);
616 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
619 BT_ERR("fopen failed \n");
624 BT_DBG("Buffer = %s\n", buffer);
625 fwrite(buffer, 1, strlen(buffer), fp);
635 static int __bt_cancel_bonding(void)
637 BT_CHECK_PARAMETER(bonding_info, return);
638 BT_CHECK_PARAMETER(bonding_info->addr, return);
639 /* First Cancel the ongoing pairing in bluez otherwise if we send
640 * pair request bluez will send inprogress and we again retry bond so
641 * this cycle continues */
643 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
644 NULL, G_DBUS_CALL_FLAGS_NONE,
647 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
649 return BLUETOOTH_ERROR_PARING_FAILED;
651 return BLUETOOTH_ERROR_NONE;
654 static int __bt_retry_bond(void)
656 BT_CHECK_PARAMETER(bonding_info, return);
657 BT_CHECK_PARAMETER(bonding_info->addr, return);
659 is_device_creating = TRUE;
661 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
662 g_variant_new("(y)", bonding_info->conn_type),
663 G_DBUS_CALL_FLAGS_NONE,
666 (GAsyncReadyCallback)__bt_bond_device_cb,
669 return BLUETOOTH_ERROR_NONE;
673 static int __bt_remove_and_bond(void)
675 GDBusProxy *adapter_proxy;
676 GVariant *result = NULL;
678 char *device_path = NULL;
680 BT_CHECK_PARAMETER(bonding_info, return);
681 BT_CHECK_PARAMETER(bonding_info->addr, return);
683 adapter_proxy = _bt_get_adapter_proxy();
684 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
686 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
687 g_variant_new("(s)", bonding_info->addr),
688 G_DBUS_CALL_FLAGS_NONE,
693 return BLUETOOTH_ERROR_INTERNAL;
695 g_variant_get(result , "(o)", &device_path);
696 g_variant_unref(result);
698 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
700 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
701 g_variant_new("(o)", device_path),
702 G_DBUS_CALL_FLAGS_NONE,
708 BT_ERR("UnpairDevice Fail: %s", err->message);
710 return BLUETOOTH_ERROR_INTERNAL;
713 return __bt_retry_bond();
716 static int __bt_cancel_and_bond(void)
718 int ret = BLUETOOTH_ERROR_NONE;
720 ret = _bt_agent_reply_cancellation();
721 if (ret != BLUETOOTH_ERROR_NONE) {
722 BT_ERR("Fail to call reply cancellation");
725 return __bt_retry_bond();
729 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
732 int result = BLUETOOTH_ERROR_NONE;
735 GVariant *out_param1;
736 request_info_t *req_info;
737 bluetooth_device_info_t dev_info;
738 bt_remote_dev_info_t *remote_dev_info;
739 GVariant *manufacture_data;
742 /* Terminate ALL system popup */
743 syspopup_destroy_all();
745 reply = g_dbus_proxy_call_finish(proxy, res, &err);
747 g_variant_unref(reply);
749 is_device_creating = FALSE;
751 if (bonding_info == NULL) {
753 BT_ERR("bonding_info == NULL");
759 req_info = _bt_get_request_info(bonding_info->req_id);
760 if (req_info == NULL) {
761 BT_ERR("req_info == NULL");
766 g_dbus_error_strip_remote_error(err);
767 BT_ERR("Error occured in CreateBonding [%s]", err->message);
769 if (g_strrstr(err->message, "Already Exists")) {
770 BT_INFO("Existing Bond, remove and retry");
771 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
773 result = BLUETOOTH_ERROR_PARING_FAILED;
774 } else if (_bt_agent_is_canceled() ||
775 g_strrstr(err->message, "Authentication Canceled")) {
776 BT_INFO("Cancelled by USER");
777 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
778 } else if (g_strrstr(err->message, "Authentication Rejected")) {
780 result = BLUETOOTH_ERROR_ACCESS_DENIED;
781 } else if (g_strrstr(err->message, "In Progress")) {
782 BT_INFO("Bond in progress, cancel and retry");
783 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
785 result = BLUETOOTH_ERROR_PARING_FAILED;
786 } else if (g_strrstr(err->message, "Authentication Failed")) {
787 BT_INFO("Authentication Failed");
788 if (bonding_info->is_autopair == TRUE) {
789 _bt_set_autopair_status_in_bonding_info(FALSE);
790 __ignore_auto_pairing_request(bonding_info->addr);
792 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
793 } else if (g_strrstr(err->message, "Page Timeout")) {
794 BT_INFO("Page Timeout");
795 /* This is the special case
796 As soon as call bluetooth_bond_device, try to cancel bonding.
797 In this case, before completing to call 'CreatePairedDevice' method
798 the procedure is stopped. So 'Cancle' error is not return.
800 result = BLUETOOTH_ERROR_HOST_DOWN;
801 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
802 BT_INFO("Cancel already running bonding");
803 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
804 BT_INFO("Error while Cancelling bonding");
805 /* we need to unref proxy so continue */
807 result = BLUETOOTH_ERROR_INTERNAL;
808 } else if (g_strrstr(err->message, "Connection Timeout")) {
809 /* Pairing request timeout */
810 result = BLUETOOTH_ERROR_TIMEOUT;
811 } else if (g_strrstr(err->message, "Authentication Timeout")) {
812 /* Pairing request timeout */
813 result = BLUETOOTH_ERROR_TIMEOUT;
815 BT_DBG("Default case");
816 result = BLUETOOTH_ERROR_PARING_FAILED;
820 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
821 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
822 result == BLUETOOTH_ERROR_TIMEOUT ||
823 result == BLUETOOTH_ERROR_HOST_DOWN) {
824 #ifdef TIZEN_PROFILE_WEARABLE
825 int is_sw_running = 0;
827 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
828 BT_ERR("vconf_get_int for setup wizard state failed");
831 __bt_launch_unable_to_pairing_syspopup(result);
833 BT_ERR("Unable to pair");
835 bonding_info->result = result;
838 g_object_unref(proxy);
839 bonding_info->device_proxy = NULL;
841 if (result != BLUETOOTH_ERROR_NONE) {
842 if (bonding_info->bonding_wo_discovery) {
843 GDBusProxy *adapter_proxy;
844 GVariant *ret = NULL;
845 GError *error = NULL;
848 BT_ERR("Bond was tried without discovery. Remove it");
850 adapter_proxy = _bt_get_adapter_proxy();
851 if (adapter_proxy == NULL) {
852 BT_ERR("Cannot get adapter_proxy");
856 device_path = _bt_get_device_object_path(bonding_info->addr);
857 if (device_path == NULL) {
858 BT_ERR("Cannot get device path");
862 ret = g_dbus_proxy_call_sync(adapter_proxy,
864 g_variant_new("(o)", device_path),
865 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
868 BT_ERR("RemoveDevice Fail: %s", error->message);
869 g_clear_error(&error);
873 g_variant_unref(ret);
881 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
882 if (!remote_dev_info)
885 GVariant *uuids = NULL;
886 GVariantBuilder *builder = NULL;
888 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
889 for (i = 0; i < remote_dev_info->uuid_count; i++) {
890 g_variant_builder_add(builder, "s",
891 remote_dev_info->uuids[i]);
893 uuids = g_variant_new("as", builder);
894 g_variant_builder_unref(builder);
895 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
896 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
899 param = g_variant_new("(isunsbub@asn@ay)",
902 remote_dev_info->class,
903 remote_dev_info->rssi,
904 remote_dev_info->name,
905 remote_dev_info->paired,
906 remote_dev_info->connected,
907 remote_dev_info->trust,
909 remote_dev_info->manufacturer_data_len,
913 /* Send the event to application */
914 _bt_send_event(BT_ADAPTER_EVENT,
915 BLUETOOTH_EVENT_BONDING_FINISHED,
918 _bt_free_device_info(remote_dev_info);
921 if (req_info->context == NULL)
924 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
925 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
928 if (_bt_adapter_get_status() != BT_ACTIVATED)
929 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
931 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
932 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
934 g_dbus_method_invocation_return_value(req_info->context,
935 g_variant_new("(iv)", result, out_param1));
937 g_variant_unref(out_param1);
939 _bt_delete_request_list(req_info->req_id);
944 _bt_agent_set_canceled(FALSE);
947 g_free(bonding_info->addr);
948 g_free(bonding_info);
952 int _bt_bond_device(int request_id,
953 bluetooth_device_address_t *device_address,
954 unsigned short conn_type, GArray **out_param1)
957 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
958 bluetooth_device_info_t dev_info;
960 GDBusConnection *conn;
961 char *device_path = NULL;
962 GDBusProxy *adapter_proxy;
963 GError *error = NULL;
964 gboolean bonding_wo_discovery = FALSE;
966 BT_CHECK_PARAMETER(device_address, return);
968 #ifdef TIZEN_FEATURE_BT_DPM
969 int pairing_state = DPM_STATUS_ERROR;
971 _bt_dpm_get_bluetooth_pairing_state(&pairing_state);
972 if (pairing_state == DPM_RESTRICTED) {
973 BT_ERR("Not allow to pair the device");
974 return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
979 BT_ERR("Bonding in progress");
981 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
982 memcpy(dev_info.device_address.addr, device_address->addr,
983 BLUETOOTH_ADDRESS_LENGTH);
985 g_array_append_vals(*out_param1, &dev_info,
986 sizeof(bluetooth_device_info_t));
988 return BLUETOOTH_ERROR_DEVICE_BUSY;
991 conn = _bt_gdbus_get_system_gconn();
992 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
994 _bt_convert_addr_type_to_string(address, device_address->addr);
996 device_path = _bt_get_device_object_path(address);
998 if (device_path == NULL) {
999 BT_ERR("No searched device");
1000 GVariant *ret = NULL;
1001 adapter_proxy = _bt_get_adapter_proxy();
1002 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1004 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1005 g_variant_new("(s)", address),
1006 G_DBUS_CALL_FLAGS_NONE,
1011 if (error != NULL) {
1012 BT_ERR("CreateDevice Fail: %s", error->message);
1013 g_clear_error(&error);
1016 g_variant_unref(ret);
1017 device_path = _bt_get_device_object_path(address);
1018 if (device_path == NULL) {
1019 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1020 memcpy(dev_info.device_address.addr, device_address->addr,
1021 BLUETOOTH_ADDRESS_LENGTH);
1023 g_array_append_vals(*out_param1, &dev_info,
1024 sizeof(bluetooth_device_info_t));
1026 return BLUETOOTH_ERROR_NOT_PAIRED;
1028 BT_INFO("device_path is created[%s]", device_path);
1030 bonding_wo_discovery = TRUE;
1033 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1034 NULL, BT_BLUEZ_NAME,
1035 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1037 g_free(device_path);
1038 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1040 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1041 bonding_info->addr = g_strdup(address);
1042 bonding_info->req_id = request_id;
1044 bonding_info->device_proxy = proxy;
1045 bonding_info->conn_type = conn_type;
1046 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1048 is_device_creating = TRUE;
1050 g_dbus_proxy_call(proxy, "Pair",
1051 g_variant_new("(y)", conn_type),
1052 G_DBUS_CALL_FLAGS_NONE,
1053 BT_MAX_DBUS_TIMEOUT,
1055 (GAsyncReadyCallback)__bt_bond_device_cb,
1058 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1060 return BLUETOOTH_ERROR_NONE;
1062 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1063 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1064 bonding_info->addr);
1066 g_array_append_vals(*out_param1, &dev_info,
1067 sizeof(bluetooth_device_info_t));
1069 is_device_creating = FALSE;
1071 g_free(bonding_info->addr);
1072 g_free(bonding_info);
1073 bonding_info = NULL;
1075 return BLUETOOTH_ERROR_INTERNAL;*/
1078 int _bt_cancel_bonding(void)
1080 int ret = BLUETOOTH_ERROR_NONE;
1082 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1084 ret = _bt_agent_reply_cancellation();
1085 if (ret != BLUETOOTH_ERROR_NONE) {
1086 BT_ERR("Fail to call reply cancellation");
1090 _bt_agent_set_canceled(TRUE);
1092 return BLUETOOTH_ERROR_NONE;
1095 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1100 GVariant *out_param1;
1101 int result = BLUETOOTH_ERROR_NONE;
1102 bt_funcion_data_t *unbonding_info;
1103 bluetooth_device_info_t dev_info;
1104 request_info_t *req_info;
1106 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1108 g_variant_unref(reply);
1110 unbonding_info = user_data;
1112 if (unbonding_info == NULL) {
1114 BT_ERR("unbonding_info == NULL");
1118 req_info = _bt_get_request_info(unbonding_info->req_id);
1119 if (req_info == NULL) {
1120 BT_ERR("req_info == NULL");
1125 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1126 result = BLUETOOTH_ERROR_INTERNAL;
1129 if (req_info->context == NULL)
1132 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1133 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1134 unbonding_info->addr);
1136 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1137 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1139 g_dbus_method_invocation_return_value(req_info->context,
1140 g_variant_new("(iv)", result, out_param1));
1143 _bt_delete_request_list(req_info->req_id);
1149 if (unbonding_info) {
1150 g_free(unbonding_info->addr);
1151 g_free(unbonding_info);
1155 int _bt_unbond_device(int request_id,
1156 bluetooth_device_address_t *device_address,
1157 GArray **out_param1)
1159 char *device_path = NULL;
1160 bt_funcion_data_t *unbonding_info;
1161 GDBusProxy *adapter_proxy = NULL;
1162 GDBusProxy *device_proxy = NULL;
1163 GDBusConnection *conn;
1164 int result = BLUETOOTH_ERROR_INTERNAL;
1165 bluetooth_device_info_t dev_info;
1166 GError *error = NULL;
1167 GVariant *ret = NULL;
1169 BT_CHECK_PARAMETER(device_address, return);
1171 adapter_proxy = _bt_get_adapter_proxy();
1172 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1174 /* allocate user data so that it can be retrieved in callback */
1175 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1176 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1177 unbonding_info->req_id = request_id;
1179 _bt_convert_addr_type_to_string(unbonding_info->addr,
1180 device_address->addr);
1182 device_path = _bt_get_device_object_path(unbonding_info->addr);
1184 if (device_path == NULL) {
1185 BT_ERR("No paired device");
1186 result = BLUETOOTH_ERROR_NOT_PAIRED;
1190 conn = _bt_gdbus_get_system_gconn();
1192 BT_ERR("conn is NULL");
1193 result = BLUETOOTH_ERROR_INTERNAL;
1197 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1198 NULL, BT_BLUEZ_NAME,
1199 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1201 if (device_proxy != NULL) {
1203 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1204 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1205 G_DBUS_CALL_FLAGS_NONE,
1210 BT_ERR("Getting property failed: [%s]\n", error->message);
1211 g_error_free(error);
1212 result = BLUETOOTH_ERROR_NOT_PAIRED;
1216 BT_ERR("No paired device");
1217 g_object_unref(device_proxy);
1218 result = BLUETOOTH_ERROR_NOT_PAIRED;
1221 g_variant_unref(ret);
1223 g_object_unref(device_proxy);
1226 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1227 g_variant_new("(o)", device_path),
1228 G_DBUS_CALL_FLAGS_NONE,
1229 BT_MAX_DBUS_TIMEOUT,
1231 (GAsyncReadyCallback)__bt_unbond_cb,
1234 g_free(device_path);
1235 return BLUETOOTH_ERROR_NONE;
1238 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1239 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1240 unbonding_info->addr);
1242 g_array_append_vals(*out_param1, &dev_info,
1243 sizeof(bluetooth_device_info_t));
1244 g_free(device_path);
1245 g_free(unbonding_info->addr);
1246 g_free(unbonding_info);
1250 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1255 GVariant *out_param1;
1256 int result = BLUETOOTH_ERROR_NONE;
1257 bluetooth_device_info_t dev_info;
1258 bt_remote_dev_info_t *remote_dev_info;
1259 request_info_t *req_info;
1260 GVariant *uuid_list, *manufacture_data;
1262 GVariantBuilder *builder = NULL;
1265 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1267 g_variant_unref(reply);
1269 g_object_unref(proxy);
1271 if (searching_info == NULL) {
1273 BT_ERR("unbonding_info == NULL");
1277 req_info = _bt_get_request_info(searching_info->req_id);
1278 if (req_info == NULL) {
1279 BT_ERR("req_info == NULL");
1284 g_dbus_error_strip_remote_error(err);
1285 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1287 if (g_strrstr("Operation canceled", err->message))
1288 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1289 else if (g_strrstr("In Progress", err->message))
1290 result = BLUETOOTH_ERROR_IN_PROGRESS;
1291 else if (g_strrstr("Host is down", err->message))
1292 result = BLUETOOTH_ERROR_HOST_DOWN;
1294 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1296 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1297 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1298 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1299 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1300 remote_dev_info->uuid_count > 0) {
1301 result = BLUETOOTH_ERROR_NONE;
1304 _bt_free_device_info(remote_dev_info);
1309 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1310 if (!remote_dev_info)
1314 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1315 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1316 g_variant_builder_add(builder, "s",
1317 remote_dev_info->uuids[i]);
1319 uuid_list = g_variant_new("as", builder);
1320 g_variant_builder_unref(builder);
1321 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1322 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1325 param = g_variant_new("(isunsbub@asn@ay)",
1327 searching_info->addr,
1328 remote_dev_info->class,
1329 remote_dev_info->rssi,
1330 remote_dev_info->name,
1331 remote_dev_info->paired,
1332 remote_dev_info->connected,
1333 remote_dev_info->trust,
1335 remote_dev_info->manufacturer_data_len,
1338 /* Send the event to application */
1339 _bt_send_event(BT_ADAPTER_EVENT,
1340 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1343 _bt_free_device_info(remote_dev_info);
1346 if (req_info->context == NULL)
1349 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1350 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1351 searching_info->addr);
1353 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1354 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1356 g_dbus_method_invocation_return_value(req_info->context,
1357 g_variant_new("(iv)", result, out_param1));
1360 _bt_delete_request_list(req_info->req_id);
1365 if (searching_info) {
1366 g_free(searching_info->addr);
1367 g_free(searching_info);
1368 searching_info = NULL;
1372 int _bt_search_device(int request_id,
1373 bluetooth_device_address_t *device_address)
1375 char *device_path = NULL;
1376 GDBusProxy *device_proxy = NULL;
1377 GDBusConnection *conn;
1379 GDBusProxy *adapter_proxy;
1380 int result = BLUETOOTH_ERROR_INTERNAL;
1382 BT_CHECK_PARAMETER(device_address, return);
1385 BT_ERR("Bonding in progress");
1386 return BLUETOOTH_ERROR_DEVICE_BUSY;
1389 if (searching_info) {
1390 BT_ERR("Service searching in progress");
1391 return BLUETOOTH_ERROR_DEVICE_BUSY;
1394 adapter_proxy = _bt_get_adapter_proxy();
1395 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1397 /* allocate user data so that it can be retrieved in callback */
1398 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1399 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1400 searching_info->req_id = request_id;
1402 _bt_convert_addr_type_to_string(searching_info->addr,
1403 device_address->addr);
1405 conn = _bt_gdbus_get_system_gconn();
1406 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1409 device_path = _bt_get_device_object_path(searching_info->addr);
1411 if (device_path == NULL) {
1412 BT_ERR("No paired device");
1413 result = BLUETOOTH_ERROR_NOT_PAIRED;
1417 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1418 NULL, BT_BLUEZ_NAME,
1419 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1420 g_free(device_path);
1421 if (device_proxy == NULL) {
1422 result = BLUETOOTH_ERROR_INTERNAL;
1426 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1427 g_variant_new("(s)", ""),
1428 G_DBUS_CALL_FLAGS_NONE,
1429 BT_MAX_DBUS_TIMEOUT,
1431 (GAsyncReadyCallback)__bt_discover_cb,
1434 searching_info->device_proxy = device_proxy;
1436 return BLUETOOTH_ERROR_NONE;
1439 g_free(searching_info->addr);
1440 g_free(searching_info);
1441 searching_info = NULL;
1445 int _bt_cancel_search_device(void)
1447 GVariant *ret = NULL;
1450 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1452 if (searching_info->device_proxy) {
1453 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1455 G_DBUS_CALL_FLAGS_NONE,
1460 g_variant_unref(ret);
1462 __bt_cancel_search_service_done();
1464 return BLUETOOTH_ERROR_NONE;
1467 int _bt_set_alias(bluetooth_device_address_t *device_address,
1470 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1471 gchar *device_path = NULL;
1472 GDBusProxy *adapter_proxy;
1473 GDBusProxy *device_proxy;
1474 GVariant *ret = NULL;
1475 GError *error = NULL;
1476 GDBusConnection *conn;
1478 BT_CHECK_PARAMETER(device_address, return);
1479 BT_CHECK_PARAMETER(alias, return);
1481 adapter_proxy = _bt_get_adapter_proxy();
1482 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1484 conn = _bt_gdbus_get_system_gconn();
1485 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1487 _bt_convert_addr_type_to_string(address, device_address->addr);
1489 device_path = _bt_get_device_object_path(address);
1491 if (device_path == NULL) {
1492 BT_ERR("No paired device");
1493 return BLUETOOTH_ERROR_NOT_PAIRED;
1496 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1497 NULL, BT_BLUEZ_NAME,
1498 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1500 g_free(device_path);
1501 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1503 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1504 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1505 G_DBUS_CALL_FLAGS_NONE,
1510 g_variant_unref(ret);
1512 g_object_unref(device_proxy);
1515 BT_ERR("SetProperty error: [%s]", error->message);
1516 g_error_free(error);
1517 return BLUETOOTH_ERROR_INTERNAL;
1520 return BLUETOOTH_ERROR_NONE;
1523 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1526 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1527 gchar *device_path = NULL;
1528 GDBusProxy *device_proxy;
1529 gboolean previous_value;
1530 GError *error = NULL;
1531 GDBusConnection *conn;
1532 GVariant *result = NULL;
1533 GVariant *temp = NULL;
1534 int ret = BLUETOOTH_ERROR_NONE;
1536 BT_CHECK_PARAMETER(device_address, return);
1538 conn = _bt_gdbus_get_system_gconn();
1539 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1541 _bt_convert_addr_type_to_string(address, device_address->addr);
1543 device_path = _bt_get_device_object_path(address);
1545 if (device_path == NULL) {
1546 BT_ERR("No paired device");
1547 return BLUETOOTH_ERROR_NOT_PAIRED;
1550 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1551 NULL, BT_BLUEZ_NAME,
1552 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1554 g_free(device_path);
1555 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1557 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1558 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1559 G_DBUS_CALL_FLAGS_NONE,
1563 if (error != NULL) {
1564 BT_ERR("Getting property failed: [%s]\n", error->message);
1565 g_error_free(error);
1566 g_object_unref(device_proxy);
1567 return BLUETOOTH_ERROR_INTERNAL;
1570 g_variant_get(result, "(v)", &temp);
1571 previous_value = g_variant_get_boolean(temp);
1572 g_variant_unref(temp);
1573 g_variant_unref(result);
1574 /* If the input is same with previous value, return error. */
1575 if (previous_value == authorize) {
1576 BT_ERR("Same value: %d", previous_value);
1577 g_object_unref(device_proxy);
1578 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1582 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1583 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1584 G_DBUS_CALL_FLAGS_NONE,
1589 g_object_unref(device_proxy);
1591 BT_ERR("SetProperty error: [%s]", error->message);
1592 g_error_free(error);
1593 ret = BLUETOOTH_ERROR_INTERNAL;
1597 g_variant_unref(result);
1602 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1603 gboolean *is_connected)
1605 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1606 char *object_path = NULL;
1608 GDBusProxy *device_proxy;
1609 GError *error = NULL;
1611 GVariant *tmp_value;
1612 GDBusConnection *conn;
1613 GVariant *result = NULL;
1614 int ret = BLUETOOTH_ERROR_NONE;
1616 BT_CHECK_PARAMETER(device_address, return);
1618 conn = _bt_gdbus_get_system_gconn();
1619 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1621 _bt_convert_addr_type_to_string(address, device_address->addr);
1623 object_path = _bt_get_device_object_path(address);
1624 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1626 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1627 NULL, BT_BLUEZ_NAME, object_path,
1628 BT_PROPERTIES_INTERFACE, NULL, NULL);
1629 g_free(object_path);
1630 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1632 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1633 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1634 G_DBUS_CALL_FLAGS_NONE,
1638 if (result == NULL) {
1639 if (error != NULL) {
1640 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1641 g_error_free(error);
1643 g_object_unref(device_proxy);
1644 return BLUETOOTH_ERROR_INTERNAL;
1647 g_variant_get(result , "(@a{sv})", &value);
1648 g_variant_unref(result);
1650 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1651 if (tmp_value == NULL) {
1652 g_object_unref(device_proxy);
1653 g_variant_unref(value);
1654 return BLUETOOTH_ERROR_INTERNAL;
1657 *is_connected = g_variant_get_boolean(tmp_value);
1659 BT_DBG("gatt is connected : %d", *is_connected);
1661 g_variant_unref(tmp_value);
1662 g_variant_unref(value);
1663 g_object_unref(device_proxy);
1668 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1669 int connection_type, gboolean *is_connected)
1671 char *object_path = NULL;
1672 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1673 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1674 GDBusProxy *device_proxy = NULL;
1675 GDBusProxy *adapter_proxy = NULL;
1676 GDBusConnection *conn;
1677 GError *error = NULL;
1678 GVariant *tmp_value = NULL;
1679 GVariant *value = NULL;
1680 GVariant *result = NULL;
1683 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1684 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1686 _bt_convert_addr_type_to_string(address, device_address->addr);
1687 _bt_convert_addr_string_to_secure_string(secure_address, address);
1689 *is_connected = FALSE;
1690 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1692 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1693 return _bt_rfcomm_is_device_connected(device_address,
1695 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1696 return _bt_is_gatt_connected(device_address, is_connected);
1697 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1698 return _bt_pbap_is_connected(device_address, is_connected);
1700 adapter_proxy = _bt_get_adapter_proxy();
1701 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1703 conn = _bt_gdbus_get_system_gconn();
1704 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1706 _bt_convert_addr_type_to_string(address, device_address->addr);
1708 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1709 object_path = _bt_get_adapter_path();
1710 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1711 NULL, BT_BLUEZ_NAME,
1712 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1713 g_free(object_path);
1714 if (device_proxy == NULL) {
1715 BT_DBG("Device don't have this service");
1716 return BLUETOOTH_ERROR_INTERNAL;
1719 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1720 g_variant_new("(s)", address),
1721 G_DBUS_CALL_FLAGS_NONE,
1726 if (result == NULL) {
1727 BT_ERR("[GetProperties] Error occured in Proxy call");
1729 BT_ERR("%s", error->message);
1730 g_error_free(error);
1732 *is_connected = FALSE;
1733 g_object_unref(device_proxy);
1734 return BLUETOOTH_ERROR_NONE;
1736 g_variant_get(result , "(@a{sv})", &value);
1737 g_variant_unref(result);
1740 tmp_value = g_variant_lookup_value(value,
1742 G_VARIANT_TYPE_BOOLEAN);
1744 *is_connected = g_variant_get_boolean(tmp_value);
1745 g_variant_unref(tmp_value);
1747 g_variant_unref(value);
1749 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1750 return _bt_is_network_connected(_bt_get_net_conn(),
1751 device_address->addr, is_connected);
1753 uuid = _bt_get_profile_uuid128(connection_type);
1755 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1756 return BLUETOOTH_ERROR_INTERNAL;
1758 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1760 object_path = _bt_get_device_object_path(address);
1762 BT_ERR("object_path is NULL");
1764 return BLUETOOTH_ERROR_NOT_PAIRED;
1767 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1768 NULL, BT_BLUEZ_NAME,
1769 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1770 g_free(object_path);
1771 if (device_proxy == NULL) {
1772 BT_DBG("Device don't have this service");
1774 return BLUETOOTH_ERROR_INTERNAL;
1777 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1778 g_variant_new("(s)", uuid),
1779 G_DBUS_CALL_FLAGS_NONE,
1784 if (result == NULL) {
1785 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1787 BT_ERR("%s", error->message);
1788 if (g_strrstr(error->message, "Not Connected"))
1789 BT_DBG("Not connected");
1790 g_error_free(error);
1792 *is_connected = FALSE;
1793 g_object_unref(device_proxy);
1795 return BLUETOOTH_ERROR_NONE;
1797 g_variant_get(result, "(b)", is_connected);
1799 g_variant_unref(result);
1802 g_object_unref(device_proxy);
1803 return BLUETOOTH_ERROR_NONE;
1806 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1807 bluetooth_connected_link_t *connected)
1809 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1810 char *object_path = NULL;
1812 GDBusProxy *device_proxy;
1813 GError *error = NULL;
1814 GDBusConnection *conn;
1815 GVariant *tmp_value = NULL;
1816 GVariant *value = NULL;
1817 GVariant *result = NULL;
1819 BT_CHECK_PARAMETER(device_address, return);
1821 conn = _bt_gdbus_get_system_gconn();
1822 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1824 _bt_convert_addr_type_to_string(address, device_address->addr);
1826 object_path = _bt_get_device_object_path(address);
1827 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1829 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1830 NULL, BT_BLUEZ_NAME,
1831 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1832 g_free(object_path);
1833 if (device_proxy == NULL) {
1834 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1835 return BLUETOOTH_ERROR_NONE;
1838 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1839 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1840 G_DBUS_CALL_FLAGS_NONE,
1845 if (error != NULL) {
1846 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1847 g_error_free(error);
1848 g_object_unref(device_proxy);
1849 return BLUETOOTH_ERROR_INTERNAL;
1852 g_variant_get(result , "(@a{sv})", &value);
1853 g_variant_unref(result);
1855 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1856 if (tmp_value != NULL) {
1857 *connected = g_variant_get_byte(tmp_value);
1858 g_variant_unref(tmp_value);
1859 g_object_unref(device_proxy);
1860 return BLUETOOTH_ERROR_NONE;
1862 BT_ERR("g_variant value is NULL");
1863 return BLUETOOTH_ERROR_INTERNAL;
1867 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1871 GVariant *out_param1;
1873 int result = BLUETOOTH_ERROR_NONE;
1874 bt_function_data_t *func_data = user_data;
1875 request_info_t *req_info = NULL;
1876 bluetooth_device_address_t device_addr = { {0} };
1878 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1879 g_object_unref(proxy);
1881 if (reply == NULL) {
1882 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1884 BT_ERR("Error: %s", err->message);
1885 g_clear_error(&err);
1887 result = BLUETOOTH_ERROR_INTERNAL;
1889 g_variant_unref(reply);
1892 if (func_data == NULL) {
1893 BT_ERR("func_data is NULL");
1897 req_info = _bt_get_request_info(func_data->req_id);
1898 if (req_info == NULL) {
1899 BT_ERR("req_info is NULL");
1903 if (req_info->context == NULL) {
1904 BT_ERR("req_info->context is NULL");
1908 _bt_convert_addr_string_to_type(device_addr.addr,
1909 (const char *)func_data->address);
1911 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1912 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1915 g_dbus_method_invocation_return_value(req_info->context,
1916 g_variant_new("(iv)", result, out_param1));
1920 _bt_delete_request_list(req_info->req_id);
1923 g_free(func_data->address);
1928 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1929 gboolean auto_connect)
1931 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1932 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1933 gchar *device_path = NULL;
1934 GDBusProxy *device_proxy = NULL;
1935 GDBusConnection *conn;
1936 int ret = BLUETOOTH_ERROR_NONE;
1937 bt_function_data_t *func_data;
1939 BT_CHECK_PARAMETER(bd_addr, return);
1941 conn = _bt_gdbus_get_system_gconn();
1942 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1944 _bt_convert_addr_type_to_string(device_address,
1945 (unsigned char *)bd_addr->addr);
1946 device_path = _bt_get_device_object_path(device_address);
1947 if (device_path == NULL) {
1948 BT_ERR_C("device_path NULL : [%s]", device_address);
1949 ret = BLUETOOTH_ERROR_INTERNAL;
1953 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1954 NULL, BT_BLUEZ_NAME,
1955 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1956 g_free(device_path);
1957 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1959 func_data = g_malloc0(sizeof(bt_function_data_t));
1960 func_data->address = g_strdup(device_address);
1961 if (func_data->address == NULL) {
1962 BT_ERR("Unable to allocate memory for address");
1963 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1967 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1968 BT_INFO("Connect LE [%s]", secure_address);
1970 func_data->req_id = req_id;
1972 g_dbus_proxy_call(device_proxy, "ConnectLE",
1973 g_variant_new("(b)", auto_connect),
1974 G_DBUS_CALL_FLAGS_NONE,
1975 BT_MAX_DBUS_TIMEOUT,
1977 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1983 g_object_unref(device_proxy);
1985 g_free(func_data->address);
1991 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1993 BT_INFO("Try to initiate pending LE connection");
1995 pending_le_conn_timer_id = 0;
1997 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1998 &pending_le_conn_info->bd_addr,
1999 pending_le_conn_info->auto_connect);
2001 g_free(pending_le_conn_info);
2002 pending_le_conn_info = NULL;
2007 void _bt_pending_connect_le_device(void)
2009 if (pending_le_conn_timer_id > 0) {
2010 g_source_remove(pending_le_conn_timer_id);
2011 __bt_connect_le_timer_cb(NULL);
2015 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
2016 gboolean auto_connect)
2018 int ret = BLUETOOTH_ERROR_NONE;
2020 BT_CHECK_PARAMETER(bd_addr, return);
2022 ret = _bt_hold_current_advertising();
2023 if (ret == BLUETOOTH_ERROR_NONE) {
2024 BT_INFO("Current advertising is held");
2025 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2026 pending_le_conn_info->req_id = req_id;
2027 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2028 BLUETOOTH_ADDRESS_LENGTH);
2029 pending_le_conn_info->auto_connect = auto_connect;
2031 pending_le_conn_timer_id =
2032 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2034 return BLUETOOTH_ERROR_NONE;
2035 } else if (ret != BLUETOOTH_ERROR_NOT_IN_OPERATION) {
2036 BT_ERR("Unable to hold advertising");
2039 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2042 int _bt_disconnect_le_device(int req_id,
2043 const bluetooth_device_address_t *bd_addr)
2045 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2046 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2048 GDBusProxy *device_proxy;
2049 GDBusConnection *conn;
2050 int ret = BLUETOOTH_ERROR_NONE;
2051 bt_function_data_t *func_data;
2053 BT_CHECK_PARAMETER(bd_addr, return);
2055 conn = _bt_gdbus_get_system_gconn();
2056 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2058 _bt_convert_addr_type_to_string(device_address,
2059 (unsigned char *)bd_addr->addr);
2060 device_path = _bt_get_device_object_path(device_address);
2061 if (device_path == NULL) {
2062 BT_DBG("device_path NULL");
2063 ret = BLUETOOTH_ERROR_INTERNAL;
2067 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2069 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2070 NULL, BT_BLUEZ_NAME,
2071 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2072 g_free(device_path);
2073 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2075 func_data = g_malloc0(sizeof(bt_function_data_t));
2076 func_data->address = g_strdup(device_address);
2077 if (func_data->address == NULL) {
2078 BT_ERR("Unable to allocate memory for address");
2079 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2083 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2084 BT_INFO("Disconnect LE [%s]", secure_address);
2086 func_data->req_id = req_id;
2088 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2090 G_DBUS_CALL_FLAGS_NONE,
2091 BT_MAX_DBUS_TIMEOUT,
2093 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2098 g_object_unref(device_proxy);
2100 g_free(func_data->address);
2106 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2108 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2109 gchar *device_path = NULL;
2110 GError *error = NULL;
2111 GDBusProxy *device_proxy = NULL;
2112 GDBusProxy *adapter_proxy;
2113 GDBusConnection *conn;
2114 int ret = BLUETOOTH_ERROR_NONE;
2116 BT_CHECK_PARAMETER(bd_addr, return);
2118 _bt_convert_addr_type_to_string(device_address,
2119 (unsigned char *)bd_addr->addr);
2121 conn = _bt_gdbus_get_system_gconn();
2122 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2124 adapter_proxy = _bt_get_adapter_proxy();
2125 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2127 device_path = _bt_get_device_object_path(device_address);
2128 if (device_path == NULL) {
2129 BT_DBG("device_path NULL");
2130 ret = BLUETOOTH_ERROR_INTERNAL;
2134 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2136 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2137 NULL, BT_BLUEZ_NAME,
2138 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2139 g_free(device_path);
2140 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2142 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2144 G_DBUS_CALL_FLAGS_NONE,
2149 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2150 g_error_free(error);
2151 g_object_unref(device_proxy);
2152 return BLUETOOTH_ERROR_INTERNAL;
2154 g_object_unref(device_proxy);
2156 /* IPSP daemon launch */
2157 GDBusProxy *ipsp_proxy;
2159 ipsp_proxy = _bt_get_ipsp_proxy();
2160 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2162 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2163 NULL, G_DBUS_CALL_FLAGS_NONE,
2164 -1, NULL, NULL, NULL);
2169 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2171 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2172 gchar *device_path = NULL;
2173 GError *error = NULL;
2174 GDBusProxy *device_proxy = NULL;
2175 GDBusProxy *adapter_proxy;
2176 GDBusConnection *conn;
2177 int ret = BLUETOOTH_ERROR_NONE;
2179 BT_CHECK_PARAMETER(bd_addr, return);
2181 _bt_convert_addr_type_to_string(device_address,
2182 (unsigned char *)bd_addr->addr);
2184 conn = _bt_gdbus_get_system_gconn();
2185 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2187 adapter_proxy = _bt_get_adapter_proxy();
2188 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2190 device_path = _bt_get_device_object_path(device_address);
2191 if (device_path == NULL) {
2192 BT_DBG("device_path NULL");
2193 ret = BLUETOOTH_ERROR_INTERNAL;
2197 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2199 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2200 NULL, BT_BLUEZ_NAME,
2201 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2202 g_free(device_path);
2203 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2205 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2207 G_DBUS_CALL_FLAGS_NONE,
2212 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2213 g_error_free(error);
2214 g_object_unref(device_proxy);
2215 return BLUETOOTH_ERROR_INTERNAL;
2218 g_object_unref(device_proxy);
2223 int _bt_connect_profile(char *address, char *uuid,
2224 void *cb, gpointer func_data)
2228 GDBusConnection *conn;
2229 GDBusProxy *adapter_proxy;
2230 GVariant *result = NULL;
2231 GError *error = NULL;
2233 conn = _bt_gdbus_get_system_gconn();
2234 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2236 object_path = _bt_get_device_object_path(address);
2237 if (object_path == NULL) {
2238 BT_ERR("No searched device");
2240 adapter_proxy = _bt_get_adapter_proxy();
2241 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2243 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2244 g_variant_new("(s)", address),
2245 G_DBUS_CALL_FLAGS_NONE,
2250 if (error != NULL) {
2251 BT_ERR("CreateDevice Fail: %s", error->message);
2252 g_error_free(error);
2255 g_variant_unref(result);
2257 object_path = _bt_get_device_object_path(address);
2259 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2261 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2262 NULL, BT_BLUEZ_NAME,
2263 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2264 g_free(object_path);
2265 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2267 g_dbus_proxy_call(proxy, "ConnectProfile",
2268 g_variant_new("(s)", uuid),
2269 G_DBUS_CALL_FLAGS_NONE,
2270 BT_MAX_DBUS_TIMEOUT,
2272 (GAsyncReadyCallback)cb,
2275 return BLUETOOTH_ERROR_NONE;
2278 int _bt_disconnect_all(char *address)
2280 int ret = BLUETOOTH_ERROR_NONE;
2283 GDBusConnection *conn;
2284 GVariant *result = NULL;
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 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2302 G_DBUS_CALL_FLAGS_NONE,
2307 BT_ERR("Dbus Call Error:[%s]", err->message);
2309 ret = BLUETOOTH_ERROR_INTERNAL;
2312 g_object_unref(proxy);
2314 g_variant_unref(result);
2319 int _bt_disconnect_profile(char *address, char *uuid,
2320 void *cb, gpointer func_data)
2324 GDBusConnection *conn;
2326 conn = _bt_gdbus_get_system_gconn();
2327 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2329 object_path = _bt_get_device_object_path(address);
2330 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2332 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2333 NULL, BT_BLUEZ_NAME,
2334 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2335 g_free(object_path);
2336 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2338 g_dbus_proxy_call(proxy, "DisconnectProfile",
2339 g_variant_new("(s)", uuid),
2340 G_DBUS_CALL_FLAGS_NONE,
2341 BT_MAX_DBUS_TIMEOUT,
2343 (GAsyncReadyCallback)cb,
2346 return BLUETOOTH_ERROR_NONE;
2349 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2350 int low_threshold, int in_range_threshold, int high_threshold)
2352 int ret = BLUETOOTH_ERROR_NONE;
2354 GVariant *result = NULL;
2355 GError *error = NULL;
2356 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2358 BT_CHECK_PARAMETER(bd_addr, return);
2359 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2360 bd_addr->addr[0], bd_addr->addr[1],
2361 bd_addr->addr[2], bd_addr->addr[3],
2362 bd_addr->addr[4], bd_addr->addr[5],
2364 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2365 in_range_threshold, high_threshold);
2367 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2369 proxy = _bt_get_adapter_proxy();
2370 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2372 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2373 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2374 G_DBUS_CALL_FLAGS_NONE,
2378 if (error != NULL) {
2379 BT_ERR("Dbus Call Error:[%s]", error->message);
2380 g_error_free(error);
2381 ret = BLUETOOTH_ERROR_INTERNAL;
2385 g_variant_unref(result);
2390 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2393 int ret = BLUETOOTH_ERROR_NONE;
2395 GVariant *result = NULL;
2396 GError *error = NULL;
2397 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2399 BT_CHECK_PARAMETER(bd_addr, return);
2400 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2401 bd_addr->addr[0], bd_addr->addr[1],
2402 bd_addr->addr[2], bd_addr->addr[3],
2403 bd_addr->addr[4], bd_addr->addr[5],
2406 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2408 proxy = _bt_get_adapter_proxy();
2409 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2411 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2412 g_variant_new("(si)", address, link_type),
2413 G_DBUS_CALL_FLAGS_NONE,
2418 if (error != NULL) {
2419 BT_ERR("Dbus Call Error:[%s]", error->message);
2420 g_error_free(error);
2421 ret = BLUETOOTH_ERROR_INTERNAL;
2425 g_variant_unref(result);
2430 static int __bt_le_set_conn_parameter(const char *address,
2431 float interval_min, float interval_max,
2432 guint16 latency, guint16 time_out)
2434 gchar *device_path = NULL;
2435 GError *error = NULL;
2436 GDBusProxy *device_proxy = NULL;
2437 GDBusConnection *conn;
2439 guint32 min, max, to;
2440 int ret = BLUETOOTH_ERROR_NONE;
2442 BT_CHECK_PARAMETER(address, return);
2444 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2445 interval_min, interval_max, latency, time_out);
2447 device_path = _bt_get_device_object_path((char *)address);
2449 if (device_path == NULL) {
2450 BT_ERR("device_path NULL");
2451 ret = BLUETOOTH_ERROR_INTERNAL;
2455 conn = _bt_gdbus_get_system_gconn();
2457 BT_ERR("conn NULL");
2458 ret = BLUETOOTH_ERROR_INTERNAL;
2462 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2463 NULL, BT_BLUEZ_NAME,
2464 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2466 g_free(device_path);
2467 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2469 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2470 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2471 to = time_out / BT_LE_CONN_TO_SPLIT;
2473 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2474 g_variant_new("(uuuu)", min, max, latency, to),
2475 G_DBUS_CALL_FLAGS_NONE,
2480 g_object_unref(device_proxy);
2481 if (reply == NULL) {
2483 BT_ERR("Error %s[%s]", error->message, address);
2484 if (g_strrstr(error->message, "In Progress"))
2485 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2487 ret = BLUETOOTH_ERROR_INTERNAL;
2488 g_error_free(error);
2492 g_variant_unref(reply);
2498 static void __bt_le_conn_param_free(void *data)
2500 bt_le_conn_param_t *param = (bt_le_conn_param_t *)data;
2502 BT_DBG("%s", param->sender);
2503 g_free(param->sender);
2507 static gint __bt_compare_le_conn_param_key(gpointer *a, gpointer *b)
2509 bt_le_conn_param_t *parama= (bt_le_conn_param_t *)a;
2510 bt_le_conn_param_t *paramb= (bt_le_conn_param_t *)b;
2512 return parama->key > paramb->key;
2515 static bt_connected_le_dev_t *__bt_get_le_connected_dev_info(const char *address)
2518 bt_connected_le_dev_t *dev;
2523 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2526 if (g_strcmp0(dev->address, address) == 0)
2532 static bt_le_conn_param_t *__bt_get_le_conn_param_info(bt_connected_le_dev_t *dev, const char *sender)
2535 bt_le_conn_param_t *param = NULL;
2537 if (!dev || !sender)
2540 for (l = dev->senders; l; l = g_slist_next(l)) {
2542 if (g_strcmp0(param->sender, sender) == 0)
2549 int _bt_add_le_conn_param_info(const char *address, const char *sender,
2550 float interval_min, float interval_max, guint16 latency, guint16 time_out)
2552 bt_connected_le_dev_t *dev = NULL;
2553 bt_le_conn_param_t *param = NULL;
2554 bt_le_conn_param_t *data = NULL;
2556 if (!address || !sender)
2557 return BLUETOOTH_ERROR_INVALID_PARAM;
2559 dev = __bt_get_le_connected_dev_info(address);
2561 return BLUETOOTH_ERROR_INTERNAL;
2563 param = __bt_get_le_conn_param_info(dev, sender);
2565 data = g_malloc0(sizeof(bt_le_conn_param_t));
2566 data->sender = g_strdup(sender);
2567 data->interval_min = interval_min;
2568 data->interval_max = interval_max;
2569 data->latency = latency;
2570 data->time_out = time_out;
2571 data->key = interval_min + (interval_max - interval_min)/2;
2573 if (param == NULL) {
2574 BT_DBG("Add param %s %s %f %f", address, sender, interval_min, interval_max);
2575 dev->senders = g_slist_append(dev->senders, data);
2577 BT_DBG("Update param %s %s %f %f", address, sender, interval_min, interval_max);
2578 dev->senders = g_slist_remove(dev->senders, param);
2579 g_free(param->sender);
2581 dev->senders = g_slist_append(dev->senders, data);
2584 /* Sorting. First element have the minimum interval */
2585 dev->senders = g_slist_sort(dev->senders,
2586 (GCompareFunc)__bt_compare_le_conn_param_key);
2588 return BLUETOOTH_ERROR_NONE;
2591 int _bt_remove_le_conn_param_info(const char *address, const char *sender, gboolean *is_removed)
2593 bt_connected_le_dev_t *dev = NULL;
2594 bt_le_conn_param_t *param = NULL;
2596 if (!address || !sender)
2597 return BLUETOOTH_ERROR_INVALID_PARAM;
2599 dev = __bt_get_le_connected_dev_info(address);
2601 return BLUETOOTH_ERROR_INTERNAL;
2603 param = __bt_get_le_conn_param_info(dev, sender);
2605 BT_DBG("Remove param %s %s ", address, sender);
2606 dev->senders = g_slist_remove(dev->senders, param);
2607 g_free(param->sender);
2611 *is_removed = FALSE;
2613 return BLUETOOTH_ERROR_NONE;
2616 int _bt_remove_all_le_conn_param_info(const char *sender)
2619 bt_connected_le_dev_t *dev = NULL;
2620 bt_le_conn_param_t *param = NULL;
2621 gboolean is_removed = FALSE;
2622 char *sender_new = NULL;
2623 unsigned char addr[BLUETOOTH_ADDRESS_LENGTH];
2624 int ret = BLUETOOTH_ERROR_NONE;
2627 return BLUETOOTH_ERROR_INVALID_PARAM;
2629 for (l = le_connected_dev_list; l; l = g_slist_next(l)) {
2631 _bt_remove_le_conn_param_info(dev->address, sender, &is_removed);
2634 BT_INFO("Sender terminated. Update le conn interval [senders %d]",
2635 g_slist_length(dev->senders));
2636 if (g_slist_length(dev->senders) > 0) {
2637 param = dev->senders->data;
2638 BT_DBG("dev %f %f, param %f %f", dev->interval_min, dev->interval_max,
2639 param->interval_min, param->interval_max);
2641 if (dev->interval_min != param->interval_min ||
2642 dev->interval_max != param->interval_max) {
2643 sender_new = g_strdup(param->sender);
2645 _bt_convert_addr_string_to_type(addr, dev->address);
2646 ret = _bt_le_conn_update(sender_new, addr,
2647 param->interval_min, param->interval_max,
2648 param->latency, param->time_out);
2651 if (ret != BLUETOOTH_ERROR_NONE)
2652 BT_ERR("Unable to set le connection parameter");
2655 BT_INFO("Set the default interval");
2657 bluetooth_le_connection_param_t param = { 0 };
2658 _bt_get_le_connection_parameter(
2659 BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER,
2662 ret = __bt_le_set_conn_parameter(dev->address,
2663 param.interval_min, param.interval_max,
2664 param.latency, param.timeout);
2665 if (ret == BLUETOOTH_ERROR_NONE) {
2666 dev->interval_min = param.interval_min;
2667 dev->interval_max = param.interval_max;
2676 void _bt_add_le_connected_dev_info(const char *address)
2678 bt_connected_le_dev_t *dev = NULL;
2683 dev = g_malloc0(sizeof(bt_connected_le_dev_t));
2684 dev->address = g_strdup(address);
2686 le_connected_dev_list = g_slist_append(le_connected_dev_list, dev);
2691 void _bt_remove_le_connected_dev_info(const char *address)
2693 bt_connected_le_dev_t *dev = NULL;
2698 dev = __bt_get_le_connected_dev_info(address);
2702 g_slist_free_full(dev->senders, __bt_le_conn_param_free);
2703 le_connected_dev_list = g_slist_remove(le_connected_dev_list, dev);
2704 g_free(dev->address);
2710 int _bt_le_conn_update(const char *sender,
2711 unsigned char *device_address,
2712 float interval_min, float interval_max,
2713 guint16 latency, guint16 time_out)
2715 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2716 guint32 min_supervision_to;
2717 bt_connected_le_dev_t *dev = NULL;
2718 bt_le_conn_param_t *param = NULL;
2719 gboolean is_removed = FALSE;
2720 int ret = BLUETOOTH_ERROR_NONE;
2722 BT_CHECK_PARAMETER(device_address, return);
2724 BT_INFO("Sender %s, Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2725 sender, interval_min, interval_max, latency, time_out);
2727 if (interval_min > interval_max ||
2728 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2729 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2730 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2734 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2735 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2736 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2740 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2741 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2746 * The Supervision_Timeout in milliseconds shall be larger than
2747 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2748 * where Conn_Interval_Max is given in milliseconds.
2750 min_supervision_to = (1 + latency) * interval_max * 2;
2751 if (time_out <= min_supervision_to) {
2752 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2756 _bt_convert_addr_type_to_string(address, device_address);
2757 BT_DBG("Remote device address: %s", address);
2759 _bt_add_le_conn_param_info(address, sender, interval_min, interval_max, 0, 2000);
2761 dev = __bt_get_le_connected_dev_info(address);
2763 ret = BLUETOOTH_ERROR_NOT_CONNECTED;
2767 if (g_slist_length(dev->senders) == 1)
2770 param = dev->senders->data;
2772 BT_DBG("dev %f, param %f, input %f", dev->interval_min, param->interval_min, interval_min);
2774 if (dev->interval_min == param->interval_min && dev->interval_max == param->interval_max) {
2775 BT_DBG("Skip due to same interval");
2779 interval_min = param->interval_min;
2780 interval_max = param->interval_max;
2784 ret = __bt_le_set_conn_parameter(address, interval_min, interval_max, latency, time_out);
2786 if (ret != BLUETOOTH_ERROR_NONE) {
2787 _bt_remove_le_conn_param_info(address, sender, &is_removed);
2791 dev->interval_min = interval_min;
2792 dev->interval_max = interval_max;
2798 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2799 bluetooth_device_pin_code_t *pin_code)
2801 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2803 bt_pin_code_info_t *pin_info = NULL;
2805 BT_CHECK_PARAMETER(device_address, return);
2806 BT_CHECK_PARAMETER(pin_code, return);
2807 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2808 BLUETOOTH_ERROR_NO_RESOURCES);
2810 _bt_convert_addr_type_to_string(address, device_address->addr);
2812 for (l = pin_info_list; l != NULL; l = l->next) {
2815 if (g_strcmp0(pin_info->address, address) == 0) {
2816 g_free(pin_info->pin_code);
2817 pin_info->pin_code = g_strdup(pin_code->pin_code);
2818 return BLUETOOTH_ERROR_NONE;
2822 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2823 pin_info->address = g_strdup(address);
2824 pin_info->pin_code = g_strdup(pin_code->pin_code);
2825 pin_info_list = g_slist_append(pin_info_list, pin_info);
2827 return BLUETOOTH_ERROR_NONE;
2830 gint __bt_compare_address(gpointer *a, gpointer *b)
2832 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2833 char *address = (char *)b;
2834 return g_strcmp0(pin_info->address, address);
2837 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2839 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2841 bt_pin_code_info_t *pin_info = NULL;
2843 BT_CHECK_PARAMETER(device_address, return);
2845 _bt_convert_addr_type_to_string(address, device_address->addr);
2847 l = g_slist_find_custom(pin_info_list, address,
2848 (GCompareFunc)__bt_compare_address);
2852 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2853 g_free(pin_info->address);
2854 g_free(pin_info->pin_code);
2858 return BLUETOOTH_ERROR_NONE;
2861 int _bt_get_device_pin_code(const char *address, char *pin_code)
2865 BT_CHECK_PARAMETER(address, return);
2866 BT_CHECK_PARAMETER(pin_code, return);
2868 for (l = pin_info_list; l != NULL; l = l->next) {
2869 bt_pin_code_info_t *pin_info = l->data;
2871 if (g_strcmp0(pin_info->address, address) == 0) {
2872 g_strlcpy(pin_code, pin_info->pin_code,
2873 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2875 return BLUETOOTH_ERROR_NONE;
2879 return BLUETOOTH_ERROR_NOT_FOUND;
2882 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2883 bluetooth_le_connection_param_t *param)
2886 return BLUETOOTH_ERROR_INVALID_PARAM;
2888 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2889 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2890 return BLUETOOTH_ERROR_INVALID_PARAM;
2892 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2895 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2896 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2897 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2898 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2899 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2902 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2903 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2904 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2905 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2906 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2909 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2910 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2911 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2912 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2913 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2917 BT_ERR("Unhandled mode : %d", mode);
2921 return BLUETOOTH_ERROR_NONE;
2924 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2925 guint trusted_profile_flag, guint *trusted)
2931 case TRUSTED_PROFILE_PBAP:
2932 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2933 trust_profile = trusted_profile_flag & (1 << 1);
2935 return BLUETOOTH_ERROR_NOT_SUPPORT;
2937 case TRUSTED_PROFILE_MAP:
2938 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2939 trust_profile = trusted_profile_flag & (1 << 3);
2941 return BLUETOOTH_ERROR_NOT_SUPPORT;
2943 case TRUSTED_PROFILE_SAP:
2944 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2945 trust_profile = trusted_profile_flag & (1 << 5);
2947 return BLUETOOTH_ERROR_NOT_SUPPORT;
2949 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2950 *trusted = trusted_profile_flag;
2951 return BLUETOOTH_ERROR_NONE;
2953 return BLUETOOTH_ERROR_NOT_SUPPORT;
2959 return BLUETOOTH_ERROR_NONE;
2962 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2963 guint restricted_profile_flag, guint *restricted)
2965 int restrict_profile;
2966 *restricted = FALSE;
2969 case RESTRICTED_PROFILE_HFP_HS:
2970 restrict_profile = restricted_profile_flag & (1 << 0);
2972 case RESTRICTED_PROFILE_A2DP:
2973 restrict_profile = restricted_profile_flag & (1 << 2);
2976 return BLUETOOTH_ERROR_NOT_SUPPORT;
2979 if (restrict_profile)
2982 return BLUETOOTH_ERROR_NONE;
2985 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2988 case TRUSTED_PROFILE_PBAP:
2989 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2990 case TRUSTED_PROFILE_MAP:
2991 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2992 case TRUSTED_PROFILE_SAP:
2993 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2994 case TRUSTED_PROFILE_ALL:
3001 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
3004 case RESTRICTED_PROFILE_HFP_HS:
3005 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
3006 case RESTRICTED_PROFILE_A2DP:
3007 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
3013 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
3015 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
3016 return TRUSTED_PROFILE_PBAP;
3017 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
3018 return TRUSTED_PROFILE_MAP;
3019 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
3020 return TRUSTED_PROFILE_SAP;
3022 return 0; /* 0 - Unknown Profile */
3025 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
3026 bluetooth_trusted_profile_t profile, gboolean trust)
3028 int ret = BLUETOOTH_ERROR_NONE;
3029 GDBusConnection *conn;
3031 GError *error = NULL;
3032 char *device_path = NULL;
3034 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3037 BT_CHECK_PARAMETER(bd_addr, return);
3038 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3039 bd_addr->addr[0], bd_addr->addr[1],
3040 bd_addr->addr[2], bd_addr->addr[3],
3041 bd_addr->addr[4], bd_addr->addr[5],
3044 conn = _bt_gdbus_get_system_gconn();
3045 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3047 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3049 device_path = _bt_get_device_object_path(address);
3050 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3052 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3053 NULL, BT_BLUEZ_NAME, device_path,
3054 BT_DEVICE_INTERFACE, NULL, NULL);
3056 g_free(device_path);
3057 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3059 uuid = _bt_get_trusted_profile_uuid(profile);
3061 g_object_unref(proxy);
3062 return BLUETOOTH_ERROR_NOT_SUPPORT;
3065 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
3066 g_variant_new("(sb)", uuid, trust),
3067 G_DBUS_CALL_FLAGS_NONE, -1,
3069 g_object_unref(proxy);
3071 if (reply == NULL) {
3072 BT_ERR("Failed to Set Profile Trusted");
3073 ret = BLUETOOTH_ERROR_INTERNAL;
3075 BT_ERR("Error %s[%s]", error->message, address);
3076 g_error_free(error);
3080 g_variant_unref(reply);
3087 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
3088 bluetooth_trusted_profile_t profile, guint *trust)
3090 int ret = BLUETOOTH_ERROR_NONE;
3091 GDBusConnection *conn;
3093 GError *error = NULL;
3094 char *device_path = NULL;
3095 guint trusted_profile_flag;
3096 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3099 BT_CHECK_PARAMETER(bd_addr, return);
3100 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
3101 bd_addr->addr[0], bd_addr->addr[1],
3102 bd_addr->addr[2], bd_addr->addr[3],
3103 bd_addr->addr[4], bd_addr->addr[5],
3106 conn = _bt_gdbus_get_system_gconn();
3107 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3109 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3111 device_path = _bt_get_device_object_path(address);
3112 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3114 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3115 NULL, BT_BLUEZ_NAME, device_path,
3116 BT_PROPERTIES_INTERFACE, NULL, NULL);
3118 g_free(device_path);
3119 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3121 reply = g_dbus_proxy_call_sync(proxy, "Get",
3122 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
3123 G_DBUS_CALL_FLAGS_NONE, -1,
3125 g_object_unref(proxy);
3127 if (reply == NULL) {
3128 BT_ERR("Failed to Get Profile Trusted");
3129 ret = BLUETOOTH_ERROR_INTERNAL;
3131 BT_ERR("Error %s[%s]", error->message, address);
3132 g_error_free(error);
3137 g_variant_get(reply, "(v)", &temp);
3138 trusted_profile_flag = g_variant_get_uint32(temp);
3139 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
3141 ret = _bt_get_trusted_profile_from_flag(profile,
3142 trusted_profile_flag, trust);
3143 g_variant_unref(temp);
3144 g_variant_unref(reply);
3147 BT_DBG("TRUST %d", *trust);
3151 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
3152 bluetooth_restricted_profile_t profile, gboolean restricted)
3154 int ret = BLUETOOTH_ERROR_NONE;
3155 GDBusConnection *conn;
3157 GError *error = NULL;
3158 char *device_path = NULL;
3160 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3163 BT_CHECK_PARAMETER(bd_addr, return);
3164 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3165 bd_addr->addr[0], bd_addr->addr[1],
3166 bd_addr->addr[2], bd_addr->addr[3],
3167 bd_addr->addr[4], bd_addr->addr[5],
3168 profile, restricted);
3170 conn = _bt_gdbus_get_system_gconn();
3171 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3173 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3175 device_path = _bt_get_device_object_path(address);
3176 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3178 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3179 NULL, BT_BLUEZ_NAME, device_path,
3180 BT_DEVICE_INTERFACE, NULL, NULL);
3182 g_free(device_path);
3183 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3185 uuid = _bt_get_restricted_profile_uuid(profile);
3187 g_object_unref(proxy);
3188 return BLUETOOTH_ERROR_NOT_SUPPORT;
3191 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
3192 g_variant_new("(sb)", uuid, restricted),
3193 G_DBUS_CALL_FLAGS_NONE, -1,
3195 g_object_unref(proxy);
3197 if (reply == NULL) {
3198 BT_ERR("Failed to Set Profile Restricted");
3199 ret = BLUETOOTH_ERROR_INTERNAL;
3201 BT_ERR("Error %s[%s]", error->message, address);
3202 g_error_free(error);
3206 g_variant_unref(reply);
3213 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
3214 bluetooth_restricted_profile_t profile, guint *restricted)
3216 int ret = BLUETOOTH_ERROR_NONE;
3217 GDBusConnection *conn;
3219 GError *error = NULL;
3220 char *device_path = NULL;
3221 guint restricted_profile_flag;
3222 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3225 BT_CHECK_PARAMETER(bd_addr, return);
3226 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
3227 bd_addr->addr[0], bd_addr->addr[1],
3228 bd_addr->addr[2], bd_addr->addr[3],
3229 bd_addr->addr[4], bd_addr->addr[5],
3230 profile, *restricted);
3232 conn = _bt_gdbus_get_system_gconn();
3233 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3235 _bt_convert_addr_type_to_string(address, bd_addr->addr);
3237 device_path = _bt_get_device_object_path(address);
3238 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3240 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3241 NULL, BT_BLUEZ_NAME, device_path,
3242 BT_PROPERTIES_INTERFACE, NULL, NULL);
3244 g_free(device_path);
3245 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3247 reply = g_dbus_proxy_call_sync(proxy, "Get",
3248 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
3249 G_DBUS_CALL_FLAGS_NONE, -1,
3251 g_object_unref(proxy);
3253 if (reply == NULL) {
3254 BT_ERR("Failed to Get Profile Restricted");
3255 ret = BLUETOOTH_ERROR_INTERNAL;
3257 BT_ERR("Error %s[%s]", error->message, address);
3258 g_error_free(error);
3263 g_variant_get(reply, "(v)", &temp);
3264 restricted_profile_flag = g_variant_get_uint32(temp);
3265 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
3267 ret = _bt_get_restricted_profile_from_flag(profile,
3268 restricted_profile_flag, restricted);
3269 g_variant_unref(temp);
3270 g_variant_unref(reply);
3273 BT_DBG("TRUST %d", *restricted);
3277 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
3281 GVariant *out_param1;
3282 request_info_t *req_info;
3283 GVariant *val = NULL;
3284 GVariant *param = NULL;
3287 bluetooth_device_address_t device_addr = { {0} };
3288 int result = BLUETOOTH_ERROR_NONE;
3291 val = g_dbus_proxy_call_finish(proxy, res, &err);
3293 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
3294 if (req_info == NULL) {
3295 BT_ERR("req_info == NULL");
3296 g_object_unref(proxy);
3297 att_mtu_req_info->device_proxy = NULL;
3302 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
3304 if (g_strrstr(err->message, "NotSupported")) {
3305 BT_INFO("Connection Not Supported");
3306 result = BLUETOOTH_ERROR_NOT_SUPPORT;
3307 } else if (g_strrstr(err->message, "NotConnected")) {
3308 BT_INFO("Not connected");
3309 result = BLUETOOTH_ERROR_NOT_CONNECTED;
3310 } else if (g_strrstr(err->message, "InvalidArguments")) {
3311 BT_INFO("Not connected");
3312 result = BLUETOOTH_ERROR_INVALID_PARAM;
3314 BT_DBG("Default case");
3315 result = BLUETOOTH_ERROR_INTERNAL;
3319 g_object_unref(proxy);
3320 att_mtu_req_info->device_proxy = NULL;
3322 if (result != BLUETOOTH_ERROR_NONE)
3326 g_variant_get(val, "(qy)", &mtu, &status);
3327 g_variant_unref(val);
3330 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
3332 param = g_variant_new("(isqy)",
3334 att_mtu_req_info->addr,
3338 /* Send the event to application */
3339 _bt_send_event(BT_DEVICE_EVENT,
3340 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3344 if (req_info->context == NULL)
3347 _bt_convert_addr_string_to_type(device_addr.addr,
3348 (const char *)att_mtu_req_info->addr);
3350 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3351 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3352 g_dbus_method_invocation_return_value(req_info->context,
3353 g_variant_new("(iv)", result, out_param1));
3355 _bt_delete_request_list(req_info->req_id);
3360 g_free(att_mtu_req_info->addr);
3361 g_free(att_mtu_req_info);
3362 att_mtu_req_info = NULL;
3367 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3370 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3371 gchar *device_path = NULL;
3372 GDBusProxy *adapter_proxy;
3373 GDBusProxy *device_proxy;
3374 GDBusConnection *conn;
3375 int ret = BLUETOOTH_ERROR_NONE;
3377 BT_CHECK_PARAMETER(device_address, return);
3379 if (att_mtu_req_info) {
3380 BT_ERR("ATT MTU request in progress");
3381 return BLUETOOTH_ERROR_DEVICE_BUSY;
3384 conn = _bt_gdbus_get_system_gconn();
3385 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3387 adapter_proxy = _bt_get_adapter_proxy();
3388 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3390 _bt_convert_addr_type_to_string(address, device_address->addr);
3392 BT_DBG("Remote device address: %s", address);
3394 device_path = _bt_get_device_object_path(address);
3396 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3398 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3399 NULL, BT_BLUEZ_NAME,
3400 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3401 g_free(device_path);
3402 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3404 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3405 att_mtu_req_info->addr = (char *)g_strdup(address);
3406 att_mtu_req_info->req_id = request_id;
3407 att_mtu_req_info->device_proxy = device_proxy;
3409 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3410 g_variant_new("(q)", mtu),
3411 G_DBUS_CALL_FLAGS_NONE,
3412 BT_MAX_DBUS_TIMEOUT,
3414 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3420 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3423 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3424 char *object_path = NULL;
3426 GDBusProxy *device_proxy;
3427 GError *error = NULL;
3429 GVariant *tmp_value;
3430 GDBusConnection *conn;
3431 GVariant *result = NULL;
3432 int ret = BLUETOOTH_ERROR_NONE;
3434 BT_CHECK_PARAMETER(device_address, return);
3436 conn = _bt_gdbus_get_system_gconn();
3437 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3439 _bt_convert_addr_type_to_string(address, device_address->addr);
3441 object_path = _bt_get_device_object_path(address);
3442 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3444 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3445 NULL, BT_BLUEZ_NAME, object_path,
3446 BT_PROPERTIES_INTERFACE, NULL, NULL);
3447 g_free(object_path);
3448 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3450 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3451 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3452 G_DBUS_CALL_FLAGS_NONE,
3456 if (result == NULL) {
3457 if (error != NULL) {
3458 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3459 g_error_free(error);
3461 g_object_unref(device_proxy);
3462 return BLUETOOTH_ERROR_INTERNAL;
3465 g_variant_get(result , "(@a{sv})", &value);
3466 g_variant_unref(result);
3468 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3469 if (tmp_value == NULL) {
3470 g_object_unref(device_proxy);
3471 g_variant_unref(value);
3472 return BLUETOOTH_ERROR_INTERNAL;
3475 *mtu = g_variant_get_uint16(tmp_value);
3477 BT_DBG("ATT MTU : %d", *mtu);
3479 g_variant_unref(tmp_value);
3480 g_variant_unref(value);
3481 g_object_unref(device_proxy);
3486 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3487 bluetooth_device_address_t *id_address)
3489 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3490 gchar *device_path = NULL;
3491 const gchar *idaddress = NULL;
3492 GDBusProxy *device_proxy;
3493 GError *error = NULL;
3494 GVariant *result = NULL;
3495 GDBusConnection *conn;
3496 int ret = BLUETOOTH_ERROR_NONE;
3498 BT_CHECK_PARAMETER(device_address, return);
3500 conn = _bt_gdbus_get_system_gconn();
3501 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3503 _bt_convert_addr_type_to_string(address, device_address->addr);
3505 device_path = _bt_get_device_object_path(address);
3506 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3508 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3509 NULL, BT_BLUEZ_NAME,
3510 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3511 g_free(device_path);
3512 if (!device_proxy) {
3513 BT_ERR("Unable to get proxy");
3514 return BLUETOOTH_ERROR_INTERNAL;
3517 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3519 G_DBUS_CALL_FLAGS_NONE,
3524 if (result == NULL) {
3525 BT_ERR("Failed to get device ID address");
3526 if (error != NULL) {
3527 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3528 g_error_free(error);
3530 g_object_unref(device_proxy);
3531 return BLUETOOTH_ERROR_INTERNAL;
3534 g_variant_get(result , "(s)", &idaddress);
3535 g_variant_unref(result);
3537 if (idaddress == NULL) {
3538 BT_ERR("No paired device");
3539 g_object_unref(device_proxy);
3540 return BLUETOOTH_ERROR_NOT_PAIRED;
3543 BT_DBG("ID Address:%s", idaddress);
3546 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3548 ret = BLUETOOTH_ERROR_INTERNAL;
3550 g_object_unref(device_proxy);
3555 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3557 GapAgentPrivate *agent = _bt_get_adapter_agent();
3558 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3560 if (authentication_reply)
3561 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3563 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3565 BT_DBG("BT_PASSKEY_REPLY");
3566 return BLUETOOTH_ERROR_NONE;
3569 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3571 GapAgentPrivate *agent = _bt_get_adapter_agent();
3572 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3574 if (confirmation_reply)
3575 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3577 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3579 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3580 return BLUETOOTH_ERROR_NONE;