2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-adapter-le.h"
32 #include "bt-service-event.h"
33 #include "bt-service-device.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
41 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
42 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
43 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
45 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
46 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
47 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
49 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
50 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
51 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
52 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
54 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 6000 /* msec */
56 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
60 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 10 /* msec */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 30 /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
64 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
68 #define PROFILE_SUPPORTED 0x3 /* This corresponds to binary 0b11*/
75 GDBusProxy *device_proxy;
76 GDBusProxy *adapter_proxy;
78 unsigned short conn_type;
79 gboolean bonding_wo_discovery;
89 bluetooth_device_address_t bd_addr;
90 gboolean auto_connect;
91 } bt_pending_le_conn_info_s;
93 gboolean is_device_creating;
94 bt_funcion_data_t *bonding_info;
95 bt_funcion_data_t *searching_info;
96 bt_funcion_data_t *att_mtu_req_info;
98 static GSList *pin_info_list = NULL;
99 static bt_pending_le_conn_info_s *pending_le_conn_info = NULL;
100 static guint pending_le_conn_timer_id = 0;
102 /* This HID Mouse does not support pairing precedure. need to skip it. */
103 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
105 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
108 static int __bt_retry_bond(void);
111 /*static void __bt_decline_pair_request()
113 GVariant *out_param1;
114 GVariant *out_param2;
115 request_info_t *req_info;
116 bluetooth_device_info_t dev_info;
117 bt_remote_dev_info_t *remote_dev_info;
118 GVariant *uuid_list, *manufacture_data;
123 req_info = _bt_get_request_info(bonding_info->req_id);
124 if (req_info == NULL) {
125 BT_ERR("req_info == NULL");
128 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
130 BT_DBG("bonding_info is NULL");
135 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
136 remote_dev_info->uuids, remote_dev_info->uuid_count,
139 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
140 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
143 param = g_variant_new("isunsbuba{s}na{y})",
144 bonding_info->result,
146 remote_dev_info->class,
147 remote_dev_info->rssi,
148 remote_dev_info->name,
149 remote_dev_info->paired,
150 remote_dev_info->connected,
151 remote_dev_info->trust,
153 remote_dev_info->manufacturer_data_len,
157 //Send the event to application
158 if (remote_dev_info != NULL) {
159 _bt_send_event(BT_ADAPTER_EVENT,
160 BLUETOOTH_EVENT_BONDING_FINISHED,
163 _bt_free_device_info(remote_dev_info);
166 if (req_info->context == NULL)
169 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
170 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
173 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
174 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
176 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
177 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
180 g_dbus_method_invocation_return_value(req_info->context,
181 g_variant_new("@ayi", out_param1, out_param2));
184 _bt_delete_request_list(req_info->req_id);
187 g_free(bonding_info->addr);
188 g_free(bonding_info);
194 #ifdef TIZEN_PROFILE_WEARABLE
195 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
199 retv_if(user_data == NULL, FALSE);
201 b = (bundle *)user_data;
203 ret = syspopup_launch("bt-syspopup", b);
205 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
207 BT_DBG("Hurray!!! Finally Popup launched");
210 return (ret < 0) ? TRUE : FALSE;
213 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
218 GDBusConnection *conn;
220 conn = _bt_gdbus_get_system_gconn();
228 bundle_add(b, "event-type", "unable-to-pairing");
230 if (result == BLUETOOTH_ERROR_TIMEOUT)
231 bundle_add(b, "error", "timeout");
232 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
233 bundle_add(b, "error", "authfailed");
235 bundle_add(b, "error", "error");
237 ret = syspopup_launch("bt-syspopup", b);
239 BT_ERR("Popup launch failed...retry %d \n", ret);
240 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
251 gboolean _bt_is_device_creating(void)
253 return is_device_creating;
256 gboolean _bt_is_bonding_device_address(const char *address)
258 if (bonding_info == NULL || bonding_info->addr == NULL)
261 if (g_strcmp0(bonding_info->addr, address) == 0) {
262 BT_DBG("[%s] is bonding device", address);
266 BT_DBG("[%s] is NOT bonding device", address);
270 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
272 ret_if(bonding_info == NULL);
273 bonding_info->is_autopair = is_autopair;
276 void __bt_cancel_search_service_done(void)
278 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
279 request_info_t *req_info;
280 bluetooth_device_info_t dev_info;
281 GVariant *out_param1;
283 ret_if(searching_info == NULL);
285 req_info = _bt_get_request_info(searching_info->req_id);
286 if (req_info == NULL) {
287 BT_ERR("req_info == NULL");
291 if (req_info->context == NULL)
294 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
295 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
296 searching_info->addr);
298 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
299 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
302 g_dbus_method_invocation_return_value(req_info->context,
303 g_variant_new("(iv)", result, out_param1));
305 _bt_delete_request_list(req_info->req_id);
309 g_free(searching_info->addr);
310 g_free(searching_info);
311 searching_info = NULL;
314 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
316 ret_if(value == NULL);
317 ret_if(info == NULL);
319 info->uuid_count = g_variant_get_size(value);
320 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
323 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
325 char *object_path = NULL;
326 bt_remote_dev_info_t *dev_info;
328 retv_if(address == NULL, NULL);
330 object_path = _bt_get_device_object_path(address);
331 retv_if(object_path == NULL, NULL);
333 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
339 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
340 const char *object_path)
342 bt_remote_dev_info_t *dev_info;
343 GDBusProxy *adapter_proxy;
344 GDBusProxy *device_proxy;
349 GDBusConnection *conn;
350 GError *error = NULL;
351 GVariant *result = NULL;
352 GVariantIter *value_iter;
356 adapter_proxy = _bt_get_adapter_proxy();
357 retv_if(adapter_proxy == NULL, NULL);
359 retv_if(object_path == NULL, NULL);
361 conn = _bt_gdbus_get_system_gconn();
363 BT_ERR("conn == NULL");
367 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
368 NULL, BT_BLUEZ_NAME, object_path,
369 BT_PROPERTIES_INTERFACE, NULL, NULL);
371 retv_if(device_proxy == NULL, NULL);
373 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
374 g_variant_new("(s)", BT_DEVICE_INTERFACE),
375 G_DBUS_CALL_FLAGS_NONE,
380 g_object_unref(device_proxy);
382 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
384 if (result != NULL) {
385 g_variant_get(result , "(@a{sv})", &value);
386 g_variant_unref(result);
388 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
390 g_variant_get(tmp_value, "s", &name);
391 g_variant_unref(tmp_value);
393 DBG_SECURE("Alias Name [%s]", name);
395 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
396 g_variant_get(tmp_value, "s", &name);
397 g_variant_unref(tmp_value);
400 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
402 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
403 g_variant_unref(tmp_value);
405 dev_info->is_alias_set = FALSE;
407 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
409 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
411 dev_info->class = g_variant_get_uint32(tmp_value);
412 g_variant_unref(tmp_value);
416 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
418 dev_info->connected = g_variant_get_byte(tmp_value);
419 g_variant_unref(tmp_value);
421 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
422 BT_DBG("connected link : %d", dev_info->connected);
424 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
426 dev_info->trust = g_variant_get_boolean(tmp_value);
427 g_variant_unref(tmp_value);
429 dev_info->trust = FALSE;
431 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
433 dev_info->paired = g_variant_get_boolean(tmp_value);
434 g_variant_unref(tmp_value);
436 dev_info->paired = FALSE;
438 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
440 dev_info->rssi = g_variant_get_int16(tmp_value);
441 g_variant_unref(tmp_value);
445 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
447 dev_info->addr_type = g_variant_get_byte(tmp_value);
448 g_variant_unref(tmp_value);
450 dev_info->addr_type = 0;
452 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
454 __bt_get_uuids(tmp_value, dev_info);
455 g_variant_unref(tmp_value);
458 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerDataLen", G_VARIANT_TYPE_UINT16);
460 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
461 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
462 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
463 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
465 g_variant_unref(tmp_value);
467 dev_info->manufacturer_data_len = 0;
469 tmp_value = g_variant_lookup_value(value, "LegacyManufacturerData", G_VARIANT_TYPE_ARRAY);
471 if ((dev_info->manufacturer_data_len == 0) ||
472 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
473 BT_ERR("manufacturer data length doesn't match");
474 dev_info->manufacturer_data_len = 0;
475 dev_info->manufacturer_data = NULL;
477 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
478 g_variant_get(tmp_value, "ay", &value_iter);
479 while (g_variant_iter_loop(value_iter, "y", &m_value))
480 dev_info->manufacturer_data[i++] = m_value;
482 g_variant_unref(tmp_value);
484 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
485 dev_info->manufacturer_data_len = 0;
486 dev_info->manufacturer_data = NULL;
489 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
490 g_variant_get(tmp_value, "s", &address);
491 g_variant_unref(tmp_value);
493 dev_info->address = g_strdup(address);
494 dev_info->name = g_strdup(name);
496 g_variant_unref(value);
498 BT_ERR("result is NULL\n");
506 char *_bt_get_bonded_device_name(char *address)
508 bluetooth_device_address_t device_address = { {0} };
509 bluetooth_device_info_t dev_info;
511 retv_if(address == NULL, strdup(""));
513 _bt_convert_addr_string_to_type(device_address.addr, address);
515 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
517 _bt_get_bonded_device_info(&device_address, &dev_info);
519 return g_strdup(dev_info.device_name.name);
522 static gboolean __ignore_auto_pairing_request(const char *address)
527 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
538 /* Get the LAP(Lower Address part) */
539 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
541 snprintf(lap_address, sizeof(lap_address), ",%s", address);
543 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
546 BT_ERR("fopen failed \n");
550 fseek(fp, 0, SEEK_END);
555 BT_ERR("Get file size failed \n");
560 buffer = g_malloc0(sizeof(char) * size);
561 /* Fix : NULL_RETURNS */
562 if (buffer == NULL) {
563 BT_ERR("Memory allocation error\n");
567 result = fread((char *)buffer, 1, size, fp);
569 if (result != size) {
570 BT_ERR("Read Error\n");
575 BT_DBG("Buffer = %s\n", buffer);
577 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
583 /* Write the data and insert new device data */
584 for (i = 0; lines[i] != NULL; i++) {
585 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
586 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
588 lines[i] = temp_buffer;
591 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
593 /* Fix : NULL_RETURNS */
594 retv_if(buffer == NULL, FALSE);
596 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
599 BT_ERR("fopen failed \n");
604 BT_DBG("Buffer = %s\n", buffer);
605 fwrite(buffer, 1, strlen(buffer), fp);
615 static int __bt_cancel_bonding(void)
617 BT_CHECK_PARAMETER(bonding_info, return);
618 BT_CHECK_PARAMETER(bonding_info->addr, return);
619 /* First Cancel the ongoing pairing in bluez otherwise if we send
620 * pair request bluez will send inprogress and we again retry bond so
621 * this cycle continues */
623 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
624 NULL, G_DBUS_CALL_FLAGS_NONE,
627 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
629 return BLUETOOTH_ERROR_PARING_FAILED;
631 return BLUETOOTH_ERROR_NONE;
634 static int __bt_retry_bond(void)
636 BT_CHECK_PARAMETER(bonding_info, return);
637 BT_CHECK_PARAMETER(bonding_info->addr, return);
639 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
640 g_variant_new("(y)", bonding_info->conn_type),
641 G_DBUS_CALL_FLAGS_NONE,
644 (GAsyncReadyCallback)__bt_bond_device_cb,
647 return BLUETOOTH_ERROR_NONE;
651 static int __bt_remove_and_bond(void)
653 GDBusProxy *adapter_proxy;
654 GVariant *result = NULL;
656 char *device_path = NULL;
658 BT_CHECK_PARAMETER(bonding_info, return);
659 BT_CHECK_PARAMETER(bonding_info->addr, return);
661 adapter_proxy = _bt_get_adapter_proxy();
662 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
664 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
665 g_variant_new("(s)", bonding_info->addr),
666 G_DBUS_CALL_FLAGS_NONE,
671 return BLUETOOTH_ERROR_INTERNAL;
673 g_variant_get(result , "(o)", &device_path);
674 g_variant_unref(result);
676 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
678 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
679 g_variant_new("(o)", device_path),
680 G_DBUS_CALL_FLAGS_NONE,
686 BT_ERR("UnpairDevice Fail: %s", err->message);
688 return BLUETOOTH_ERROR_INTERNAL;
691 return __bt_retry_bond();
694 static int __bt_cancel_and_bond(void)
696 int ret = BLUETOOTH_ERROR_NONE;
698 ret = _bt_agent_reply_cancellation();
699 if (ret != BLUETOOTH_ERROR_NONE) {
700 BT_ERR("Fail to call reply cancellation");
703 return __bt_retry_bond();
707 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
710 int result = BLUETOOTH_ERROR_NONE;
713 GVariant *out_param1;
714 request_info_t *req_info;
715 bluetooth_device_info_t dev_info;
716 bt_remote_dev_info_t *remote_dev_info;
717 GVariant *manufacture_data;
720 /* Terminate ALL system popup */
721 syspopup_destroy_all();
723 reply = g_dbus_proxy_call_finish(proxy, res, &err);
725 g_variant_unref(reply);
727 is_device_creating = FALSE;
729 if (bonding_info == NULL) {
731 BT_ERR("bonding_info == NULL");
737 req_info = _bt_get_request_info(bonding_info->req_id);
738 if (req_info == NULL) {
739 BT_ERR("req_info == NULL");
744 g_dbus_error_strip_remote_error(err);
745 BT_ERR("Error occured in CreateBonding [%s]", err->message);
747 if (g_strrstr(err->message, "Already Exists")) {
748 BT_INFO("Existing Bond, remove and retry");
749 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
751 result = BLUETOOTH_ERROR_PARING_FAILED;
752 } else if (_bt_agent_is_canceled() ||
753 g_strrstr(err->message, "Authentication Canceled")) {
754 BT_INFO("Cancelled by USER");
755 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
756 } else if (g_strrstr(err->message, "Authentication Rejected")) {
758 result = BLUETOOTH_ERROR_ACCESS_DENIED;
759 } else if (g_strrstr(err->message, "In Progress")) {
760 BT_INFO("Bond in progress, cancel and retry");
761 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
763 result = BLUETOOTH_ERROR_PARING_FAILED;
764 } else if (g_strrstr(err->message, "Authentication Failed")) {
765 BT_INFO("Authentication Failed");
766 if (bonding_info->is_autopair == TRUE) {
767 _bt_set_autopair_status_in_bonding_info(FALSE);
768 __ignore_auto_pairing_request(bonding_info->addr);
770 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
771 } else if (g_strrstr(err->message, "Page Timeout")) {
772 BT_INFO("Page Timeout");
773 /* This is the special case
774 As soon as call bluetooth_bond_device, try to cancel bonding.
775 In this case, before completing to call 'CreatePairedDevice' method
776 the procedure is stopped. So 'Cancle' error is not return.
778 result = BLUETOOTH_ERROR_HOST_DOWN;
779 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
780 BT_INFO("Cancel already running bonding");
781 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
782 BT_INFO("Error while Cancelling bonding");
783 /* we need to unref proxy so continue */
785 result = BLUETOOTH_ERROR_INTERNAL;
786 } else if (g_strrstr(err->message, "Connection Timeout")) {
787 /* Pairing request timeout */
788 result = BLUETOOTH_ERROR_TIMEOUT;
789 } else if (g_strrstr(err->message, "Authentication Timeout")) {
790 /* Pairing request timeout */
791 result = BLUETOOTH_ERROR_TIMEOUT;
793 BT_DBG("Default case");
794 result = BLUETOOTH_ERROR_PARING_FAILED;
798 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
799 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
800 result == BLUETOOTH_ERROR_TIMEOUT ||
801 result == BLUETOOTH_ERROR_HOST_DOWN) {
802 #ifdef TIZEN_PROFILE_WEARABLE
803 int is_sw_running = 0;
805 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
806 BT_ERR("vconf_get_int for setup wizard state failed");
809 __bt_launch_unable_to_pairing_syspopup(result);
811 BT_ERR("Unable to pair");
813 bonding_info->result = result;
816 g_object_unref(proxy);
817 bonding_info->device_proxy = NULL;
819 if (result != BLUETOOTH_ERROR_NONE) {
820 if (bonding_info->bonding_wo_discovery) {
821 GDBusProxy *adapter_proxy;
822 GVariant *ret = NULL;
823 GError *error = NULL;
826 BT_ERR("Bond was tried without discovery. Remove it");
828 adapter_proxy = _bt_get_adapter_proxy();
829 if (adapter_proxy == NULL) {
830 BT_ERR("Cannot get adapter_proxy");
834 device_path = _bt_get_device_object_path(bonding_info->addr);
835 if (device_path == NULL) {
836 BT_ERR("Cannot get device path");
840 ret = g_dbus_proxy_call_sync(adapter_proxy,
842 g_variant_new("(o)", device_path),
843 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
846 BT_ERR("RemoveDevice Fail: %s", error->message);
847 g_clear_error(&error);
851 g_variant_unref(ret);
859 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
860 if (!remote_dev_info)
863 GVariant *uuids = NULL;
864 GVariantBuilder *builder = NULL;
866 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
867 for (i = 0; i < remote_dev_info->uuid_count; i++) {
868 g_variant_builder_add(builder, "s",
869 remote_dev_info->uuids[i]);
871 uuids = g_variant_new("as", builder);
872 g_variant_builder_unref(builder);
873 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
874 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
877 param = g_variant_new("(isunsbub@asn@ay)",
880 remote_dev_info->class,
881 remote_dev_info->rssi,
882 remote_dev_info->name,
883 remote_dev_info->paired,
884 remote_dev_info->connected,
885 remote_dev_info->trust,
887 remote_dev_info->manufacturer_data_len,
891 /* Send the event to application */
892 _bt_send_event(BT_ADAPTER_EVENT,
893 BLUETOOTH_EVENT_BONDING_FINISHED,
896 _bt_free_device_info(remote_dev_info);
899 if (req_info->context == NULL)
902 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
903 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
906 if (_bt_adapter_get_status() != BT_ACTIVATED)
907 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
909 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
910 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
912 g_dbus_method_invocation_return_value(req_info->context,
913 g_variant_new("(iv)", result, out_param1));
915 g_variant_unref(out_param1);
917 _bt_delete_request_list(req_info->req_id);
922 _bt_agent_set_canceled(FALSE);
925 g_free(bonding_info->addr);
926 g_free(bonding_info);
930 int _bt_bond_device(int request_id,
931 bluetooth_device_address_t *device_address,
932 unsigned short conn_type, GArray **out_param1)
935 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
936 bluetooth_device_info_t dev_info;
938 GDBusConnection *conn;
939 char *device_path = NULL;
940 GDBusProxy *adapter_proxy;
941 GError *error = NULL;
942 gboolean bonding_wo_discovery = FALSE;
944 BT_CHECK_PARAMETER(device_address, return);
947 BT_ERR("Bonding in progress");
949 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
950 memcpy(dev_info.device_address.addr, device_address->addr,
951 BLUETOOTH_ADDRESS_LENGTH);
953 g_array_append_vals(*out_param1, &dev_info,
954 sizeof(bluetooth_device_info_t));
956 return BLUETOOTH_ERROR_DEVICE_BUSY;
959 conn = _bt_gdbus_get_system_gconn();
960 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
962 _bt_convert_addr_type_to_string(address, device_address->addr);
964 device_path = _bt_get_device_object_path(address);
966 if (device_path == NULL) {
967 BT_ERR("No searched device");
968 GVariant *ret = NULL;
969 adapter_proxy = _bt_get_adapter_proxy();
970 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
972 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
973 g_variant_new("(s)", address),
974 G_DBUS_CALL_FLAGS_NONE,
980 BT_ERR("CreateDevice Fail: %s", error->message);
981 g_clear_error(&error);
984 g_variant_unref(ret);
985 device_path = _bt_get_device_object_path(address);
986 if (device_path == NULL) {
987 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
988 memcpy(dev_info.device_address.addr, device_address->addr,
989 BLUETOOTH_ADDRESS_LENGTH);
991 g_array_append_vals(*out_param1, &dev_info,
992 sizeof(bluetooth_device_info_t));
994 return BLUETOOTH_ERROR_NOT_PAIRED;
996 BT_INFO("device_path is created[%s]", device_path);
998 bonding_wo_discovery = TRUE;
1001 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1002 NULL, BT_BLUEZ_NAME,
1003 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1005 g_free(device_path);
1006 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1008 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1009 bonding_info->addr = g_strdup(address);
1010 bonding_info->req_id = request_id;
1012 bonding_info->device_proxy = proxy;
1013 bonding_info->conn_type = conn_type;
1014 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1016 is_device_creating = TRUE;
1018 g_dbus_proxy_call(proxy, "Pair",
1019 g_variant_new("(y)", conn_type),
1020 G_DBUS_CALL_FLAGS_NONE,
1021 BT_MAX_DBUS_TIMEOUT,
1023 (GAsyncReadyCallback)__bt_bond_device_cb,
1026 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1028 return BLUETOOTH_ERROR_NONE;
1030 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1031 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1032 bonding_info->addr);
1034 g_array_append_vals(*out_param1, &dev_info,
1035 sizeof(bluetooth_device_info_t));
1037 is_device_creating = FALSE;
1039 g_free(bonding_info->addr);
1040 g_free(bonding_info);
1041 bonding_info = NULL;
1043 return BLUETOOTH_ERROR_INTERNAL;*/
1046 int _bt_cancel_bonding(void)
1048 int ret = BLUETOOTH_ERROR_NONE;
1050 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1052 ret = _bt_agent_reply_cancellation();
1053 if (ret != BLUETOOTH_ERROR_NONE) {
1054 BT_ERR("Fail to call reply cancellation");
1058 _bt_agent_set_canceled(TRUE);
1060 return BLUETOOTH_ERROR_NONE;
1063 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1068 GVariant *out_param1;
1069 int result = BLUETOOTH_ERROR_NONE;
1070 bt_funcion_data_t *unbonding_info;
1071 bluetooth_device_info_t dev_info;
1072 request_info_t *req_info;
1074 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1076 g_variant_unref(reply);
1078 unbonding_info = user_data;
1080 if (unbonding_info == NULL) {
1082 BT_ERR("unbonding_info == NULL");
1086 req_info = _bt_get_request_info(unbonding_info->req_id);
1087 if (req_info == NULL) {
1088 BT_ERR("req_info == NULL");
1093 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1094 result = BLUETOOTH_ERROR_INTERNAL;
1097 if (req_info->context == NULL)
1100 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1101 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1102 unbonding_info->addr);
1104 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1105 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1107 g_dbus_method_invocation_return_value(req_info->context,
1108 g_variant_new("(iv)", result, out_param1));
1111 _bt_delete_request_list(req_info->req_id);
1117 if (unbonding_info) {
1118 g_free(unbonding_info->addr);
1119 g_free(unbonding_info);
1123 int _bt_unbond_device(int request_id,
1124 bluetooth_device_address_t *device_address,
1125 GArray **out_param1)
1127 char *device_path = NULL;
1128 bt_funcion_data_t *unbonding_info;
1129 GDBusProxy *adapter_proxy = NULL;
1130 GDBusProxy *device_proxy = NULL;
1131 GDBusConnection *conn;
1132 int result = BLUETOOTH_ERROR_INTERNAL;
1133 bluetooth_device_info_t dev_info;
1134 GError *error = NULL;
1135 GVariant *ret = NULL;
1137 BT_CHECK_PARAMETER(device_address, return);
1139 adapter_proxy = _bt_get_adapter_proxy();
1140 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1142 /* allocate user data so that it can be retrieved in callback */
1143 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1144 /* Fix : NULL_RETURNS */
1145 if (unbonding_info == NULL) {
1146 BT_ERR("Memory not allocated !");
1147 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1150 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1151 unbonding_info->req_id = request_id;
1153 _bt_convert_addr_type_to_string(unbonding_info->addr,
1154 device_address->addr);
1156 device_path = _bt_get_device_object_path(unbonding_info->addr);
1158 if (device_path == NULL) {
1159 BT_ERR("No paired device");
1160 result = BLUETOOTH_ERROR_NOT_PAIRED;
1164 conn = _bt_gdbus_get_system_gconn();
1166 BT_ERR("conn is NULL");
1167 result = BLUETOOTH_ERROR_INTERNAL;
1171 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1172 NULL, BT_BLUEZ_NAME,
1173 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1175 if (device_proxy != NULL) {
1177 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1178 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1179 G_DBUS_CALL_FLAGS_NONE,
1184 BT_ERR("Getting property failed: [%s]\n", error->message);
1185 g_error_free(error);
1186 result = BLUETOOTH_ERROR_NOT_PAIRED;
1190 BT_ERR("No paired device");
1191 g_object_unref(device_proxy);
1192 result = BLUETOOTH_ERROR_NOT_PAIRED;
1195 g_variant_unref(ret);
1197 g_object_unref(device_proxy);
1200 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1201 g_variant_new("(o)", device_path),
1202 G_DBUS_CALL_FLAGS_NONE,
1203 BT_MAX_DBUS_TIMEOUT,
1205 (GAsyncReadyCallback)__bt_unbond_cb,
1208 g_free(device_path);
1209 return BLUETOOTH_ERROR_NONE;
1212 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1213 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1214 unbonding_info->addr);
1216 g_array_append_vals(*out_param1, &dev_info,
1217 sizeof(bluetooth_device_info_t));
1218 g_free(device_path);
1219 g_free(unbonding_info->addr);
1220 g_free(unbonding_info);
1224 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1229 GVariant *out_param1;
1230 int result = BLUETOOTH_ERROR_NONE;
1231 bluetooth_device_info_t dev_info;
1232 bt_remote_dev_info_t *remote_dev_info;
1233 request_info_t *req_info;
1234 GVariant *uuid_list, *manufacture_data;
1236 GVariantBuilder *builder = NULL;
1239 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1241 g_variant_unref(reply);
1243 g_object_unref(proxy);
1245 if (searching_info == NULL) {
1247 BT_ERR("unbonding_info == NULL");
1251 req_info = _bt_get_request_info(searching_info->req_id);
1252 if (req_info == NULL) {
1253 BT_ERR("req_info == NULL");
1258 g_dbus_error_strip_remote_error(err);
1259 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1261 if (g_strrstr("Operation canceled", err->message))
1262 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1263 else if (g_strrstr("In Progress", err->message))
1264 result = BLUETOOTH_ERROR_IN_PROGRESS;
1265 else if (g_strrstr("Host is down", err->message))
1266 result = BLUETOOTH_ERROR_HOST_DOWN;
1268 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1270 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1271 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1272 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1273 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1274 remote_dev_info->uuid_count > 0) {
1275 result = BLUETOOTH_ERROR_NONE;
1278 _bt_free_device_info(remote_dev_info);
1283 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1284 if (!remote_dev_info)
1288 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1289 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1290 g_variant_builder_add(builder, "s",
1291 remote_dev_info->uuids[i]);
1293 uuid_list = g_variant_new("as", builder);
1294 g_variant_builder_unref(builder);
1295 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1296 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1299 param = g_variant_new("(isunsbub@asn@ay)",
1301 searching_info->addr,
1302 remote_dev_info->class,
1303 remote_dev_info->rssi,
1304 remote_dev_info->name,
1305 remote_dev_info->paired,
1306 remote_dev_info->connected,
1307 remote_dev_info->trust,
1309 remote_dev_info->manufacturer_data_len,
1312 /* Send the event to application */
1313 _bt_send_event(BT_ADAPTER_EVENT,
1314 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1317 _bt_free_device_info(remote_dev_info);
1320 if (req_info->context == NULL)
1323 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1324 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1325 searching_info->addr);
1327 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1328 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1330 g_dbus_method_invocation_return_value(req_info->context,
1331 g_variant_new("(iv)", result, out_param1));
1334 _bt_delete_request_list(req_info->req_id);
1339 if (searching_info) {
1340 g_free(searching_info->addr);
1341 g_free(searching_info);
1342 searching_info = NULL;
1346 int _bt_search_device(int request_id,
1347 bluetooth_device_address_t *device_address)
1349 char *device_path = NULL;
1350 GDBusProxy *device_proxy = NULL;
1351 GDBusConnection *conn;
1353 GDBusProxy *adapter_proxy;
1354 int result = BLUETOOTH_ERROR_INTERNAL;
1356 BT_CHECK_PARAMETER(device_address, return);
1359 BT_ERR("Bonding in progress");
1360 return BLUETOOTH_ERROR_DEVICE_BUSY;
1363 if (searching_info) {
1364 BT_ERR("Service searching in progress");
1365 return BLUETOOTH_ERROR_DEVICE_BUSY;
1368 adapter_proxy = _bt_get_adapter_proxy();
1369 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1371 /* allocate user data so that it can be retrieved in callback */
1372 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1373 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1374 searching_info->req_id = request_id;
1376 _bt_convert_addr_type_to_string(searching_info->addr,
1377 device_address->addr);
1379 conn = _bt_gdbus_get_system_gconn();
1380 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1383 device_path = _bt_get_device_object_path(searching_info->addr);
1385 if (device_path == NULL) {
1386 BT_ERR("No paired device");
1387 result = BLUETOOTH_ERROR_NOT_PAIRED;
1391 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1392 NULL, BT_BLUEZ_NAME,
1393 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1394 g_free(device_path);
1395 if (device_proxy == NULL) {
1396 result = BLUETOOTH_ERROR_INTERNAL;
1400 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1401 g_variant_new("(s)", ""),
1402 G_DBUS_CALL_FLAGS_NONE,
1403 BT_MAX_DBUS_TIMEOUT,
1405 (GAsyncReadyCallback)__bt_discover_cb,
1408 searching_info->device_proxy = device_proxy;
1410 return BLUETOOTH_ERROR_NONE;
1413 g_free(searching_info->addr);
1414 g_free(searching_info);
1415 searching_info = NULL;
1419 int _bt_cancel_search_device(void)
1421 GVariant *ret = NULL;
1424 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1426 if (searching_info->device_proxy) {
1427 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1429 G_DBUS_CALL_FLAGS_NONE,
1434 g_variant_unref(ret);
1436 __bt_cancel_search_service_done();
1438 return BLUETOOTH_ERROR_NONE;
1441 int _bt_set_alias(bluetooth_device_address_t *device_address,
1444 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1445 gchar *device_path = NULL;
1446 GDBusProxy *adapter_proxy;
1447 GDBusProxy *device_proxy;
1448 GVariant *ret = NULL;
1449 GError *error = NULL;
1450 GDBusConnection *conn;
1452 BT_CHECK_PARAMETER(device_address, return);
1453 BT_CHECK_PARAMETER(alias, return);
1455 adapter_proxy = _bt_get_adapter_proxy();
1456 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1458 conn = _bt_gdbus_get_system_gconn();
1459 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1461 _bt_convert_addr_type_to_string(address, device_address->addr);
1463 device_path = _bt_get_device_object_path(address);
1465 if (device_path == NULL) {
1466 BT_ERR("No paired device");
1467 return BLUETOOTH_ERROR_NOT_PAIRED;
1470 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1471 NULL, BT_BLUEZ_NAME,
1472 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1474 g_free(device_path);
1475 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1477 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1478 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1479 G_DBUS_CALL_FLAGS_NONE,
1484 g_variant_unref(ret);
1486 g_object_unref(device_proxy);
1489 BT_ERR("SetProperty error: [%s]", error->message);
1490 g_error_free(error);
1491 return BLUETOOTH_ERROR_INTERNAL;
1494 return BLUETOOTH_ERROR_NONE;
1497 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1500 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1501 gchar *device_path = NULL;
1502 GDBusProxy *device_proxy;
1503 gboolean previous_value;
1504 GError *error = NULL;
1505 GDBusConnection *conn;
1506 GVariant *result = NULL;
1507 GVariant *temp = NULL;
1508 int ret = BLUETOOTH_ERROR_NONE;
1510 BT_CHECK_PARAMETER(device_address, return);
1512 conn = _bt_gdbus_get_system_gconn();
1513 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1515 _bt_convert_addr_type_to_string(address, device_address->addr);
1517 device_path = _bt_get_device_object_path(address);
1519 if (device_path == NULL) {
1520 BT_ERR("No paired device");
1521 return BLUETOOTH_ERROR_NOT_PAIRED;
1524 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1525 NULL, BT_BLUEZ_NAME,
1526 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1528 g_free(device_path);
1529 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1531 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1532 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1533 G_DBUS_CALL_FLAGS_NONE,
1537 if (error != NULL) {
1538 BT_ERR("Getting property failed: [%s]\n", error->message);
1539 g_error_free(error);
1540 g_object_unref(device_proxy);
1541 return BLUETOOTH_ERROR_INTERNAL;
1544 g_variant_get(result, "(v)", &temp);
1545 previous_value = g_variant_get_boolean(temp);
1546 g_variant_unref(temp);
1547 g_variant_unref(result);
1548 /* If the input is same with previous value, return error. */
1549 if (previous_value == authorize) {
1550 BT_ERR("Same value: %d", previous_value);
1551 g_object_unref(device_proxy);
1552 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1556 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1557 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1558 G_DBUS_CALL_FLAGS_NONE,
1563 g_object_unref(device_proxy);
1565 BT_ERR("SetProperty error: [%s]", error->message);
1566 g_error_free(error);
1567 ret = BLUETOOTH_ERROR_INTERNAL;
1571 g_variant_unref(result);
1576 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1577 gboolean *is_connected)
1579 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1580 char *object_path = NULL;
1582 GDBusProxy *device_proxy;
1583 GError *error = NULL;
1585 GVariant *tmp_value;
1586 GDBusConnection *conn;
1587 GVariant *result = NULL;
1588 int ret = BLUETOOTH_ERROR_NONE;
1590 BT_CHECK_PARAMETER(device_address, return);
1592 conn = _bt_gdbus_get_system_gconn();
1593 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1595 _bt_convert_addr_type_to_string(address, device_address->addr);
1597 object_path = _bt_get_device_object_path(address);
1598 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1600 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1601 NULL, BT_BLUEZ_NAME, object_path,
1602 BT_PROPERTIES_INTERFACE, NULL, NULL);
1603 g_free(object_path);
1604 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1606 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1607 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1608 G_DBUS_CALL_FLAGS_NONE,
1612 if (result == NULL) {
1613 if (error != NULL) {
1614 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1615 g_error_free(error);
1617 g_object_unref(device_proxy);
1618 return BLUETOOTH_ERROR_INTERNAL;
1621 g_variant_get(result , "(@a{sv})", &value);
1622 g_variant_unref(result);
1624 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1625 if (tmp_value == NULL) {
1626 g_object_unref(device_proxy);
1627 g_variant_unref(value);
1628 return BLUETOOTH_ERROR_INTERNAL;
1631 *is_connected = g_variant_get_boolean(tmp_value);
1633 BT_DBG("gatt is connected : %d", *is_connected);
1635 g_variant_unref(tmp_value);
1636 g_variant_unref(value);
1637 g_object_unref(device_proxy);
1642 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1643 int connection_type, gboolean *is_connected)
1645 char *object_path = NULL;
1646 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1647 char secure_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 _bt_convert_addr_type_to_string(address, device_address->addr);
1661 _bt_convert_addr_string_to_secure_string(secure_address, address);
1663 *is_connected = FALSE;
1664 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1666 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1667 return _bt_rfcomm_is_device_connected(device_address,
1669 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1670 return _bt_is_gatt_connected(device_address, is_connected);
1671 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1672 return _bt_pbap_is_connected(device_address, is_connected);
1674 adapter_proxy = _bt_get_adapter_proxy();
1675 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1677 conn = _bt_gdbus_get_system_gconn();
1678 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1680 _bt_convert_addr_type_to_string(address, device_address->addr);
1682 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1683 object_path = _bt_get_adapter_path();
1684 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1685 NULL, BT_BLUEZ_NAME,
1686 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1687 g_free(object_path);
1688 if (device_proxy == NULL) {
1689 BT_DBG("Device don't have this service");
1690 return BLUETOOTH_ERROR_INTERNAL;
1693 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1694 g_variant_new("(s)", address),
1695 G_DBUS_CALL_FLAGS_NONE,
1700 if (result == NULL) {
1701 BT_ERR("[GetProperties] Error occured in Proxy call");
1703 BT_ERR("%s", error->message);
1704 g_error_free(error);
1706 *is_connected = FALSE;
1707 g_object_unref(device_proxy);
1708 return BLUETOOTH_ERROR_NONE;
1710 g_variant_get(result , "(@a{sv})", &value);
1711 g_variant_unref(result);
1714 tmp_value = g_variant_lookup_value(value,
1716 G_VARIANT_TYPE_BOOLEAN);
1718 *is_connected = g_variant_get_boolean(tmp_value);
1719 g_variant_unref(tmp_value);
1721 g_variant_unref(value);
1723 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1724 return _bt_is_network_connected(_bt_get_net_conn(),
1725 device_address->addr, is_connected);
1727 uuid = _bt_get_profile_uuid128(connection_type);
1729 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1730 return BLUETOOTH_ERROR_INTERNAL;
1732 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1734 object_path = _bt_get_device_object_path(address);
1735 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1736 BT_DBG("object_path: %s", object_path);
1737 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1738 NULL, BT_BLUEZ_NAME,
1739 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1740 g_free(object_path);
1741 if (device_proxy == NULL) {
1742 BT_DBG("Device don't have this service");
1744 return BLUETOOTH_ERROR_INTERNAL;
1747 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1748 g_variant_new("(s)", uuid),
1749 G_DBUS_CALL_FLAGS_NONE,
1754 if (result == NULL) {
1755 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1757 BT_ERR("%s", error->message);
1758 if (g_strrstr(error->message, "Not Connected"))
1759 BT_DBG("Not connected");
1760 g_error_free(error);
1762 *is_connected = FALSE;
1763 g_object_unref(device_proxy);
1765 return BLUETOOTH_ERROR_NONE;
1767 g_variant_get(result, "(b)", is_connected);
1769 g_variant_unref(result);
1772 g_object_unref(device_proxy);
1773 return BLUETOOTH_ERROR_NONE;
1776 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1777 bluetooth_connected_link_t *connected)
1779 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1780 char *object_path = NULL;
1782 GDBusProxy *device_proxy;
1783 GError *error = NULL;
1784 GDBusConnection *conn;
1785 GVariant *tmp_value = NULL;
1786 GVariant *value = NULL;
1787 GVariant *result = NULL;
1789 BT_CHECK_PARAMETER(device_address, return);
1791 conn = _bt_gdbus_get_system_gconn();
1792 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1794 _bt_convert_addr_type_to_string(address, device_address->addr);
1796 object_path = _bt_get_device_object_path(address);
1797 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1799 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1800 NULL, BT_BLUEZ_NAME,
1801 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1802 g_free(object_path);
1803 if (device_proxy == NULL) {
1804 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1805 return BLUETOOTH_ERROR_NONE;
1808 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1809 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1810 G_DBUS_CALL_FLAGS_NONE,
1815 if (error != NULL) {
1816 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1817 g_error_free(error);
1818 g_object_unref(device_proxy);
1819 return BLUETOOTH_ERROR_INTERNAL;
1822 g_variant_get(result , "(@a{sv})", &value);
1823 g_variant_unref(result);
1825 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1826 if (tmp_value != NULL) {
1827 *connected = g_variant_get_byte(tmp_value);
1828 g_variant_unref(tmp_value);
1829 g_object_unref(device_proxy);
1830 return BLUETOOTH_ERROR_NONE;
1832 BT_ERR("g_variant value is NULL");
1833 return BLUETOOTH_ERROR_INTERNAL;
1837 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1841 GVariant *out_param1;
1843 int result = BLUETOOTH_ERROR_NONE;
1844 bt_function_data_t *func_data = user_data;
1845 request_info_t *req_info = NULL;
1846 bluetooth_device_address_t device_addr = { {0} };
1848 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1849 g_object_unref(proxy);
1851 if (reply == NULL) {
1852 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1854 BT_ERR("Error: %s", err->message);
1855 g_clear_error(&err);
1857 result = BLUETOOTH_ERROR_INTERNAL;
1859 g_variant_unref(reply);
1862 if (func_data == NULL) {
1863 BT_ERR("func_data is NULL");
1867 req_info = _bt_get_request_info(func_data->req_id);
1868 if (req_info == NULL) {
1869 BT_ERR("req_info is NULL");
1873 if (req_info->context == NULL) {
1874 BT_ERR("req_info->context is NULL");
1878 _bt_convert_addr_string_to_type(device_addr.addr,
1879 (const char *)func_data->address);
1881 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1882 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1885 g_dbus_method_invocation_return_value(req_info->context,
1886 g_variant_new("(iv)", result, out_param1));
1890 _bt_delete_request_list(req_info->req_id);
1893 g_free(func_data->address);
1898 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1899 gboolean auto_connect)
1901 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1902 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1903 gchar *device_path = NULL;
1904 GDBusProxy *device_proxy = NULL;
1905 GDBusConnection *conn;
1906 int ret = BLUETOOTH_ERROR_NONE;
1907 bt_function_data_t *func_data;
1909 BT_CHECK_PARAMETER(bd_addr, return);
1911 conn = _bt_gdbus_get_system_gconn();
1912 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1914 _bt_convert_addr_type_to_string(device_address,
1915 (unsigned char *)bd_addr->addr);
1916 device_path = _bt_get_device_object_path(device_address);
1917 if (device_path == NULL) {
1918 BT_ERR_C("device_path NULL : [%s]", device_address);
1919 ret = BLUETOOTH_ERROR_INTERNAL;
1923 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1924 NULL, BT_BLUEZ_NAME,
1925 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1926 g_free(device_path);
1927 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1929 func_data = g_malloc0(sizeof(bt_function_data_t));
1930 if (func_data == NULL) {
1931 BT_ERR("Unable to allocate memory for address");
1932 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1936 func_data->address = g_strdup(device_address);
1937 if (func_data->address == NULL) {
1938 BT_ERR("Unable to allocate memory for address");
1939 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1943 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1944 BT_INFO("Connect LE [%s]", secure_address);
1946 func_data->req_id = req_id;
1948 g_dbus_proxy_call(device_proxy, "ConnectLE",
1949 g_variant_new("(b)", auto_connect),
1950 G_DBUS_CALL_FLAGS_NONE,
1951 BT_MAX_DBUS_TIMEOUT,
1953 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1959 g_object_unref(device_proxy);
1961 g_free(func_data->address);
1967 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1969 BT_INFO("Try to initiate pending LE connection");
1971 pending_le_conn_timer_id = 0;
1973 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1974 &pending_le_conn_info->bd_addr,
1975 pending_le_conn_info->auto_connect);
1977 g_free(pending_le_conn_info);
1978 pending_le_conn_info = NULL;
1983 void _bt_pending_connect_le_device(void)
1985 if (pending_le_conn_timer_id > 0) {
1986 g_source_remove(pending_le_conn_timer_id);
1987 __bt_connect_le_timer_cb(NULL);
1991 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1992 gboolean auto_connect)
1994 int ret = BLUETOOTH_ERROR_NONE;
1996 BT_CHECK_PARAMETER(bd_addr, return);
1998 ret = _bt_hold_current_advertising();
1999 if (ret == BLUETOOTH_ERROR_NONE) {
2000 BT_INFO("Current advertising is held");
2001 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
2002 pending_le_conn_info->req_id = req_id;
2003 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
2004 BLUETOOTH_ADDRESS_LENGTH);
2005 pending_le_conn_info->auto_connect = auto_connect;
2007 pending_le_conn_timer_id =
2008 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
2010 return BLUETOOTH_ERROR_NONE;
2013 BT_ERR("Unable to hold advertising");
2015 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
2018 int _bt_disconnect_le_device(int req_id,
2019 const bluetooth_device_address_t *bd_addr)
2021 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2022 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2024 GDBusProxy *device_proxy;
2025 GDBusConnection *conn;
2026 int ret = BLUETOOTH_ERROR_NONE;
2027 bt_function_data_t *func_data;
2029 BT_CHECK_PARAMETER(bd_addr, return);
2031 conn = _bt_gdbus_get_system_gconn();
2032 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2034 _bt_convert_addr_type_to_string(device_address,
2035 (unsigned char *)bd_addr->addr);
2036 device_path = _bt_get_device_object_path(device_address);
2037 if (device_path == NULL) {
2038 BT_DBG("device_path NULL");
2039 ret = BLUETOOTH_ERROR_INTERNAL;
2043 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2045 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2046 NULL, BT_BLUEZ_NAME,
2047 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2048 g_free(device_path);
2049 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2051 func_data = g_malloc0(sizeof(bt_function_data_t));
2052 if (func_data == NULL) {
2053 BT_ERR("Unable to allocate memory for address");
2054 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2058 func_data->address = g_strdup(device_address);
2059 if (func_data->address == NULL) {
2060 BT_ERR("Unable to allocate memory for address");
2061 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
2065 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
2066 BT_INFO("Disconnect LE [%s]", secure_address);
2068 func_data->req_id = req_id;
2070 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2072 G_DBUS_CALL_FLAGS_NONE,
2073 BT_MAX_DBUS_TIMEOUT,
2075 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2080 g_object_unref(device_proxy);
2082 g_free(func_data->address);
2088 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2090 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2091 gchar *device_path = NULL;
2092 GError *error = NULL;
2093 GDBusProxy *device_proxy = NULL;
2094 GDBusProxy *adapter_proxy;
2095 GDBusConnection *conn;
2096 int ret = BLUETOOTH_ERROR_NONE;
2098 BT_CHECK_PARAMETER(bd_addr, return);
2100 _bt_convert_addr_type_to_string(device_address,
2101 (unsigned char *)bd_addr->addr);
2103 conn = _bt_gdbus_get_system_gconn();
2104 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2106 adapter_proxy = _bt_get_adapter_proxy();
2107 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2109 device_path = _bt_get_device_object_path(device_address);
2110 if (device_path == NULL) {
2111 BT_DBG("device_path NULL");
2112 ret = BLUETOOTH_ERROR_INTERNAL;
2116 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2118 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2119 NULL, BT_BLUEZ_NAME,
2120 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2121 g_free(device_path);
2122 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2124 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2126 G_DBUS_CALL_FLAGS_NONE,
2131 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2132 g_error_free(error);
2133 g_object_unref(device_proxy);
2134 return BLUETOOTH_ERROR_INTERNAL;
2137 g_object_unref(device_proxy);
2142 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2144 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2145 gchar *device_path = NULL;
2146 GError *error = NULL;
2147 GDBusProxy *device_proxy = NULL;
2148 GDBusProxy *adapter_proxy;
2149 GDBusConnection *conn;
2150 int ret = BLUETOOTH_ERROR_NONE;
2152 BT_CHECK_PARAMETER(bd_addr, return);
2154 _bt_convert_addr_type_to_string(device_address,
2155 (unsigned char *)bd_addr->addr);
2157 conn = _bt_gdbus_get_system_gconn();
2158 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2160 adapter_proxy = _bt_get_adapter_proxy();
2161 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2163 device_path = _bt_get_device_object_path(device_address);
2164 if (device_path == NULL) {
2165 BT_DBG("device_path NULL");
2166 ret = BLUETOOTH_ERROR_INTERNAL;
2170 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2172 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2173 NULL, BT_BLUEZ_NAME,
2174 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2175 g_free(device_path);
2176 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2178 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2180 G_DBUS_CALL_FLAGS_NONE,
2185 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2186 g_error_free(error);
2187 g_object_unref(device_proxy);
2188 return BLUETOOTH_ERROR_INTERNAL;
2191 g_object_unref(device_proxy);
2196 int _bt_connect_profile(char *address, char *uuid,
2197 void *cb, gpointer func_data)
2201 GDBusConnection *conn;
2202 GDBusProxy *adapter_proxy;
2203 GVariant *result = NULL;
2204 GError *error = NULL;
2206 conn = _bt_gdbus_get_system_gconn();
2207 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2209 object_path = _bt_get_device_object_path(address);
2210 if (object_path == NULL) {
2211 BT_ERR("No searched device");
2213 adapter_proxy = _bt_get_adapter_proxy();
2214 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2216 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2217 g_variant_new("(s)", address),
2218 G_DBUS_CALL_FLAGS_NONE,
2223 if (error != NULL) {
2224 BT_ERR("CreateDevice Fail: %s", error->message);
2225 g_error_free(error);
2228 g_variant_unref(result);
2230 object_path = _bt_get_device_object_path(address);
2232 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2234 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2235 NULL, BT_BLUEZ_NAME,
2236 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2237 g_free(object_path);
2238 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2240 g_dbus_proxy_call(proxy, "ConnectProfile",
2241 g_variant_new("(s)", uuid),
2242 G_DBUS_CALL_FLAGS_NONE,
2243 BT_MAX_DBUS_TIMEOUT,
2245 (GAsyncReadyCallback)cb,
2248 return BLUETOOTH_ERROR_NONE;
2251 int _bt_disconnect_profile(char *address, char *uuid,
2252 void *cb, gpointer func_data)
2256 GDBusConnection *conn;
2258 conn = _bt_gdbus_get_system_gconn();
2259 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2261 object_path = _bt_get_device_object_path(address);
2262 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2264 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2265 NULL, BT_BLUEZ_NAME,
2266 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2267 g_free(object_path);
2268 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2270 g_dbus_proxy_call(proxy, "DisconnectProfile",
2271 g_variant_new("(s)", uuid),
2272 G_DBUS_CALL_FLAGS_NONE,
2273 BT_MAX_DBUS_TIMEOUT,
2275 (GAsyncReadyCallback)cb,
2278 return BLUETOOTH_ERROR_NONE;
2281 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2282 int low_threshold, int in_range_threshold, int high_threshold)
2284 int ret = BLUETOOTH_ERROR_NONE;
2286 GVariant *result = NULL;
2287 GError *error = NULL;
2288 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2290 BT_CHECK_PARAMETER(bd_addr, return);
2291 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2292 bd_addr->addr[0], bd_addr->addr[1],
2293 bd_addr->addr[2], bd_addr->addr[3],
2294 bd_addr->addr[4], bd_addr->addr[5],
2296 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2297 in_range_threshold, high_threshold);
2299 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2301 proxy = _bt_get_adapter_proxy();
2302 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2304 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2305 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2306 G_DBUS_CALL_FLAGS_NONE,
2310 if (error != NULL) {
2311 BT_ERR("Dbus Call Error:[%s]", error->message);
2312 g_error_free(error);
2313 ret = BLUETOOTH_ERROR_INTERNAL;
2317 g_variant_unref(result);
2322 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2325 int ret = BLUETOOTH_ERROR_NONE;
2327 GVariant *result = NULL;
2328 GError *error = NULL;
2329 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2331 BT_CHECK_PARAMETER(bd_addr, return);
2332 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2333 bd_addr->addr[0], bd_addr->addr[1],
2334 bd_addr->addr[2], bd_addr->addr[3],
2335 bd_addr->addr[4], bd_addr->addr[5],
2338 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2340 proxy = _bt_get_adapter_proxy();
2341 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2343 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2344 g_variant_new("(si)", address, link_type),
2345 G_DBUS_CALL_FLAGS_NONE,
2350 if (error != NULL) {
2351 BT_ERR("Dbus Call Error:[%s]", error->message);
2352 g_error_free(error);
2353 ret = BLUETOOTH_ERROR_INTERNAL;
2357 g_variant_unref(result);
2362 int _bt_le_conn_update(unsigned char *device_address,
2363 float interval_min, float interval_max,
2364 guint16 latency, guint16 time_out)
2366 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2367 gchar *device_path = NULL;
2368 GError *error = NULL;
2369 GDBusProxy *device_proxy = NULL;
2370 GDBusConnection *conn;
2372 guint32 min, max, to;
2373 guint32 min_supervision_to;
2374 int ret = BLUETOOTH_ERROR_NONE;
2376 BT_CHECK_PARAMETER(device_address, return);
2378 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2379 interval_min, interval_max, latency, time_out);
2381 if (interval_min > interval_max ||
2382 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2383 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2384 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2388 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2389 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2390 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2394 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2395 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2400 * The Supervision_Timeout in milliseconds shall be larger than
2401 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2402 * where Conn_Interval_Max is given in milliseconds.
2404 min_supervision_to = (1 + latency) * interval_max * 2;
2405 if (time_out <= min_supervision_to) {
2406 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2410 _bt_convert_addr_type_to_string(address, device_address);
2412 BT_DBG("Remote device address: %s", address);
2414 device_path = _bt_get_device_object_path(address);
2416 if (device_path == NULL) {
2417 BT_ERR("device_path NULL");
2418 ret = BLUETOOTH_ERROR_INTERNAL;
2422 conn = _bt_gdbus_get_system_gconn();
2424 BT_ERR("conn NULL");
2425 ret = BLUETOOTH_ERROR_INTERNAL;
2429 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2430 NULL, BT_BLUEZ_NAME,
2431 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2433 g_free(device_path);
2434 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2436 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2437 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2438 to = time_out / BT_LE_CONN_TO_SPLIT;
2440 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2441 g_variant_new("(uuuu)", min, max, latency, to),
2442 G_DBUS_CALL_FLAGS_NONE,
2447 g_object_unref(device_proxy);
2448 if (reply == NULL) {
2450 BT_ERR("Error %s[%s]", error->message, address);
2451 if (g_strrstr(error->message, "In Progress"))
2452 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2454 ret = BLUETOOTH_ERROR_INTERNAL;
2455 g_error_free(error);
2459 g_variant_unref(reply);
2465 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2466 bluetooth_device_pin_code_t *pin_code)
2468 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2470 bt_pin_code_info_t *pin_info = NULL;
2472 BT_CHECK_PARAMETER(device_address, return);
2473 BT_CHECK_PARAMETER(pin_code, return);
2474 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2475 BLUETOOTH_ERROR_NO_RESOURCES);
2477 _bt_convert_addr_type_to_string(address, device_address->addr);
2479 for (l = pin_info_list; l != NULL; l = l->next) {
2482 if (g_strcmp0(pin_info->address, address) == 0) {
2483 g_free(pin_info->pin_code);
2484 pin_info->pin_code = g_strdup(pin_code->pin_code);
2485 return BLUETOOTH_ERROR_NONE;
2489 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2490 pin_info->address = g_strdup(address);
2491 pin_info->pin_code = g_strdup(pin_code->pin_code);
2492 pin_info_list = g_slist_append(pin_info_list, pin_info);
2494 return BLUETOOTH_ERROR_NONE;
2497 gint __bt_compare_address(gpointer *a, gpointer *b)
2499 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2500 char *address = (char *)b;
2501 return g_strcmp0(pin_info->address, address);
2504 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2506 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2508 bt_pin_code_info_t *pin_info = NULL;
2510 BT_CHECK_PARAMETER(device_address, return);
2512 _bt_convert_addr_type_to_string(address, device_address->addr);
2514 l = g_slist_find_custom(pin_info_list, address,
2515 (GCompareFunc)__bt_compare_address);
2519 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2520 g_free(pin_info->address);
2521 g_free(pin_info->pin_code);
2525 return BLUETOOTH_ERROR_NONE;
2528 int _bt_get_device_pin_code(const char *address, char *pin_code)
2532 BT_CHECK_PARAMETER(address, return);
2533 BT_CHECK_PARAMETER(pin_code, return);
2535 for (l = pin_info_list; l != NULL; l = l->next) {
2536 bt_pin_code_info_t *pin_info = l->data;
2538 if (g_strcmp0(pin_info->address, address) == 0) {
2539 g_strlcpy(pin_code, pin_info->pin_code,
2540 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2542 return BLUETOOTH_ERROR_NONE;
2546 return BLUETOOTH_ERROR_NOT_FOUND;
2549 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2550 bluetooth_le_connection_param_t *param)
2553 return BLUETOOTH_ERROR_INVALID_PARAM;
2555 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2556 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2557 return BLUETOOTH_ERROR_INVALID_PARAM;
2559 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2562 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2563 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2564 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2565 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2566 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2569 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2570 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2571 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2572 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2573 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2576 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2577 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2578 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2579 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2580 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2584 BT_ERR("Unhandled mode : %d", mode);
2588 return BLUETOOTH_ERROR_NONE;
2591 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2592 guint trusted_profile_flag, guint *trusted)
2598 case TRUSTED_PROFILE_PBAP:
2599 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2600 trust_profile = trusted_profile_flag & (1 << 1);
2602 return BLUETOOTH_ERROR_NOT_SUPPORT;
2604 case TRUSTED_PROFILE_MAP:
2605 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2606 trust_profile = trusted_profile_flag & (1 << 3);
2608 return BLUETOOTH_ERROR_NOT_SUPPORT;
2610 case TRUSTED_PROFILE_SAP:
2611 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2612 trust_profile = trusted_profile_flag & (1 << 5);
2614 return BLUETOOTH_ERROR_NOT_SUPPORT;
2616 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2617 *trusted = trusted_profile_flag;
2618 return BLUETOOTH_ERROR_NONE;
2620 return BLUETOOTH_ERROR_NOT_SUPPORT;
2626 return BLUETOOTH_ERROR_NONE;
2629 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2630 guint restricted_profile_flag, guint *restricted)
2632 int restrict_profile;
2633 *restricted = FALSE;
2636 case RESTRICTED_PROFILE_HFP_HS:
2637 restrict_profile = restricted_profile_flag & (1 << 0);
2639 case RESTRICTED_PROFILE_A2DP:
2640 restrict_profile = restricted_profile_flag & (1 << 2);
2643 return BLUETOOTH_ERROR_NOT_SUPPORT;
2646 if (restrict_profile)
2649 return BLUETOOTH_ERROR_NONE;
2652 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2655 case TRUSTED_PROFILE_PBAP:
2656 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2657 case TRUSTED_PROFILE_MAP:
2658 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2659 case TRUSTED_PROFILE_SAP:
2660 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2661 case TRUSTED_PROFILE_ALL:
2668 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2671 case RESTRICTED_PROFILE_HFP_HS:
2672 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2673 case RESTRICTED_PROFILE_A2DP:
2674 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2680 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2682 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2683 return TRUSTED_PROFILE_PBAP;
2684 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2685 return TRUSTED_PROFILE_MAP;
2686 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2687 return TRUSTED_PROFILE_SAP;
2689 return 0; /* 0 - Unknown Profile */
2692 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2693 bluetooth_trusted_profile_t profile, gboolean trust)
2695 int ret = BLUETOOTH_ERROR_NONE;
2696 GDBusConnection *conn;
2698 GError *error = NULL;
2699 char *device_path = NULL;
2701 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2704 BT_CHECK_PARAMETER(bd_addr, return);
2705 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2706 bd_addr->addr[0], bd_addr->addr[1],
2707 bd_addr->addr[2], bd_addr->addr[3],
2708 bd_addr->addr[4], bd_addr->addr[5],
2711 conn = _bt_gdbus_get_system_gconn();
2712 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2714 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2716 device_path = _bt_get_device_object_path(address);
2717 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2719 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2720 NULL, BT_BLUEZ_NAME, device_path,
2721 BT_DEVICE_INTERFACE, NULL, NULL);
2723 g_free(device_path);
2724 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2726 uuid = _bt_get_trusted_profile_uuid(profile);
2728 g_object_unref(proxy);
2729 return BLUETOOTH_ERROR_NOT_SUPPORT;
2732 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2733 g_variant_new("(sb)", uuid, trust),
2734 G_DBUS_CALL_FLAGS_NONE, -1,
2736 g_object_unref(proxy);
2738 if (reply == NULL) {
2739 BT_ERR("Failed to Set Profile Trusted");
2740 ret = BLUETOOTH_ERROR_INTERNAL;
2742 BT_ERR("Error %s[%s]", error->message, address);
2743 g_error_free(error);
2747 g_variant_unref(reply);
2754 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2755 bluetooth_trusted_profile_t profile, guint *trust)
2757 int ret = BLUETOOTH_ERROR_NONE;
2758 GDBusConnection *conn;
2760 GError *error = NULL;
2761 char *device_path = NULL;
2762 guint trusted_profile_flag;
2763 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2766 BT_CHECK_PARAMETER(bd_addr, return);
2767 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2768 bd_addr->addr[0], bd_addr->addr[1],
2769 bd_addr->addr[2], bd_addr->addr[3],
2770 bd_addr->addr[4], bd_addr->addr[5],
2773 conn = _bt_gdbus_get_system_gconn();
2774 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2776 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2778 device_path = _bt_get_device_object_path(address);
2779 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2781 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2782 NULL, BT_BLUEZ_NAME, device_path,
2783 BT_PROPERTIES_INTERFACE, NULL, NULL);
2785 g_free(device_path);
2786 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2788 reply = g_dbus_proxy_call_sync(proxy, "Get",
2789 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2790 G_DBUS_CALL_FLAGS_NONE, -1,
2792 g_object_unref(proxy);
2794 if (reply == NULL) {
2795 BT_ERR("Failed to Get Profile Trusted");
2796 ret = BLUETOOTH_ERROR_INTERNAL;
2798 BT_ERR("Error %s[%s]", error->message, address);
2799 g_error_free(error);
2804 g_variant_get(reply, "(v)", &temp);
2805 trusted_profile_flag = g_variant_get_uint32(temp);
2806 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2808 ret = _bt_get_trusted_profile_from_flag(profile,
2809 trusted_profile_flag, trust);
2810 g_variant_unref(temp);
2811 g_variant_unref(reply);
2814 BT_DBG("TRUST %d", *trust);
2818 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2819 bluetooth_restricted_profile_t profile, gboolean restricted)
2821 int ret = BLUETOOTH_ERROR_NONE;
2822 GDBusConnection *conn;
2824 GError *error = NULL;
2825 char *device_path = NULL;
2827 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2830 BT_CHECK_PARAMETER(bd_addr, return);
2831 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2832 bd_addr->addr[0], bd_addr->addr[1],
2833 bd_addr->addr[2], bd_addr->addr[3],
2834 bd_addr->addr[4], bd_addr->addr[5],
2835 profile, restricted);
2837 conn = _bt_gdbus_get_system_gconn();
2838 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2840 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2842 device_path = _bt_get_device_object_path(address);
2843 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2845 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2846 NULL, BT_BLUEZ_NAME, device_path,
2847 BT_DEVICE_INTERFACE, NULL, NULL);
2849 g_free(device_path);
2850 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2852 uuid = _bt_get_restricted_profile_uuid(profile);
2854 g_object_unref(proxy);
2855 return BLUETOOTH_ERROR_NOT_SUPPORT;
2858 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2859 g_variant_new("(sb)", uuid, restricted),
2860 G_DBUS_CALL_FLAGS_NONE, -1,
2862 g_object_unref(proxy);
2864 if (reply == NULL) {
2865 BT_ERR("Failed to Set Profile Restricted");
2866 ret = BLUETOOTH_ERROR_INTERNAL;
2868 BT_ERR("Error %s[%s]", error->message, address);
2869 g_error_free(error);
2873 g_variant_unref(reply);
2880 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2881 bluetooth_restricted_profile_t profile, guint *restricted)
2883 int ret = BLUETOOTH_ERROR_NONE;
2884 GDBusConnection *conn;
2886 GError *error = NULL;
2887 char *device_path = NULL;
2888 guint restricted_profile_flag;
2889 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2892 BT_CHECK_PARAMETER(bd_addr, return);
2893 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2894 bd_addr->addr[0], bd_addr->addr[1],
2895 bd_addr->addr[2], bd_addr->addr[3],
2896 bd_addr->addr[4], bd_addr->addr[5],
2897 profile, *restricted);
2899 conn = _bt_gdbus_get_system_gconn();
2900 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2902 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2904 device_path = _bt_get_device_object_path(address);
2905 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2907 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2908 NULL, BT_BLUEZ_NAME, device_path,
2909 BT_PROPERTIES_INTERFACE, NULL, NULL);
2911 g_free(device_path);
2912 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2914 reply = g_dbus_proxy_call_sync(proxy, "Get",
2915 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2916 G_DBUS_CALL_FLAGS_NONE, -1,
2918 g_object_unref(proxy);
2920 if (reply == NULL) {
2921 BT_ERR("Failed to Get Profile Restricted");
2922 ret = BLUETOOTH_ERROR_INTERNAL;
2924 BT_ERR("Error %s[%s]", error->message, address);
2925 g_error_free(error);
2930 g_variant_get(reply, "(v)", &temp);
2931 restricted_profile_flag = g_variant_get_uint32(temp);
2932 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2934 ret = _bt_get_restricted_profile_from_flag(profile,
2935 restricted_profile_flag, restricted);
2936 g_variant_unref(temp);
2937 g_variant_unref(reply);
2940 BT_DBG("TRUST %d", *restricted);
2944 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2948 GVariant *out_param1;
2949 request_info_t *req_info;
2950 GVariant *val = NULL;
2951 GVariant *param = NULL;
2954 bluetooth_device_address_t device_addr = { {0} };
2955 int result = BLUETOOTH_ERROR_NONE;
2958 val = g_dbus_proxy_call_finish(proxy, res, &err);
2960 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2961 if (req_info == NULL) {
2962 BT_ERR("req_info == NULL");
2963 g_object_unref(proxy);
2964 att_mtu_req_info->device_proxy = NULL;
2969 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2971 if (g_strrstr(err->message, "NotSupported")) {
2972 BT_INFO("Connection Not Supported");
2973 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2974 } else if (g_strrstr(err->message, "NotConnected")) {
2975 BT_INFO("Not connected");
2976 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2977 } else if (g_strrstr(err->message, "InvalidArguments")) {
2978 BT_INFO("Not connected");
2979 result = BLUETOOTH_ERROR_INVALID_PARAM;
2981 BT_DBG("Default case");
2982 result = BLUETOOTH_ERROR_INTERNAL;
2986 g_object_unref(proxy);
2987 att_mtu_req_info->device_proxy = NULL;
2989 if (result != BLUETOOTH_ERROR_NONE)
2993 g_variant_get(val, "(qy)", &mtu, &status);
2994 g_variant_unref(val);
2997 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
2999 param = g_variant_new("(isqy)",
3001 att_mtu_req_info->addr,
3005 /* Send the event to application */
3006 _bt_send_event(BT_DEVICE_EVENT,
3007 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
3011 if (req_info->context == NULL)
3014 _bt_convert_addr_string_to_type(device_addr.addr,
3015 (const char *)att_mtu_req_info->addr);
3017 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
3018 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
3019 g_dbus_method_invocation_return_value(req_info->context,
3020 g_variant_new("(iv)", result, out_param1));
3022 _bt_delete_request_list(req_info->req_id);
3027 g_free(att_mtu_req_info->addr);
3028 g_free(att_mtu_req_info);
3029 att_mtu_req_info = NULL;
3034 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3037 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3038 gchar *device_path = NULL;
3039 GDBusProxy *adapter_proxy;
3040 GDBusProxy *device_proxy;
3041 GDBusConnection *conn;
3042 int ret = BLUETOOTH_ERROR_NONE;
3044 BT_CHECK_PARAMETER(device_address, return);
3046 if (att_mtu_req_info) {
3047 BT_ERR("ATT MTU request in progress");
3048 return BLUETOOTH_ERROR_DEVICE_BUSY;
3051 conn = _bt_gdbus_get_system_gconn();
3052 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3054 adapter_proxy = _bt_get_adapter_proxy();
3055 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3057 _bt_convert_addr_type_to_string(address, device_address->addr);
3059 BT_DBG("Remote device address: %s", address);
3061 device_path = _bt_get_device_object_path(address);
3063 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3065 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3066 NULL, BT_BLUEZ_NAME,
3067 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3068 g_free(device_path);
3069 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3071 if (device_path == NULL) {
3072 BT_DBG("device_path NULL");
3073 ret = BLUETOOTH_ERROR_INTERNAL;
3077 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3078 att_mtu_req_info->addr = (char *)g_strdup(address);
3079 att_mtu_req_info->req_id = request_id;
3080 att_mtu_req_info->device_proxy = device_proxy;
3082 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3083 g_variant_new("(q)", mtu),
3084 G_DBUS_CALL_FLAGS_NONE,
3085 BT_MAX_DBUS_TIMEOUT,
3087 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3093 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3096 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3097 char *object_path = NULL;
3099 GDBusProxy *device_proxy;
3100 GError *error = NULL;
3102 GVariant *tmp_value;
3103 GDBusConnection *conn;
3104 GVariant *result = NULL;
3105 int ret = BLUETOOTH_ERROR_NONE;
3107 BT_CHECK_PARAMETER(device_address, return);
3109 conn = _bt_gdbus_get_system_gconn();
3110 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3112 _bt_convert_addr_type_to_string(address, device_address->addr);
3114 object_path = _bt_get_device_object_path(address);
3115 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3117 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3118 NULL, BT_BLUEZ_NAME, object_path,
3119 BT_PROPERTIES_INTERFACE, NULL, NULL);
3120 g_free(object_path);
3121 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3123 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3124 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3125 G_DBUS_CALL_FLAGS_NONE,
3129 if (result == NULL) {
3130 if (error != NULL) {
3131 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3132 g_error_free(error);
3134 g_object_unref(device_proxy);
3135 return BLUETOOTH_ERROR_INTERNAL;
3138 g_variant_get(result , "(@a{sv})", &value);
3139 g_variant_unref(result);
3141 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3142 if (tmp_value == NULL) {
3143 g_object_unref(device_proxy);
3144 g_variant_unref(value);
3145 return BLUETOOTH_ERROR_INTERNAL;
3148 *mtu = g_variant_get_uint16(tmp_value);
3150 BT_DBG("ATT MTU : %d", *mtu);
3152 g_variant_unref(tmp_value);
3153 g_variant_unref(value);
3154 g_object_unref(device_proxy);
3159 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3160 bluetooth_device_address_t *id_address)
3162 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3163 gchar *device_path = NULL;
3164 const gchar *idaddress = NULL;
3165 GDBusProxy *device_proxy;
3166 GError *error = NULL;
3167 GVariant *result = NULL;
3168 GDBusConnection *conn;
3169 int ret = BLUETOOTH_ERROR_NONE;
3171 BT_CHECK_PARAMETER(device_address, return);
3173 conn = _bt_gdbus_get_system_gconn();
3174 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3176 _bt_convert_addr_type_to_string(address, device_address->addr);
3178 device_path = _bt_get_device_object_path(address);
3179 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3181 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3182 NULL, BT_BLUEZ_NAME,
3183 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3184 g_free(device_path);
3185 if (!device_proxy) {
3186 BT_ERR("Unable to get proxy");
3187 return BLUETOOTH_ERROR_INTERNAL;
3190 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3192 G_DBUS_CALL_FLAGS_NONE,
3197 if (result == NULL) {
3198 BT_ERR("Failed to get device ID address");
3199 if (error != NULL) {
3200 BT_ERR("Error occured in Proxy call [%s]", error->message);
3201 g_error_free(error);
3203 g_object_unref(device_proxy);
3204 return BLUETOOTH_ERROR_INTERNAL;
3207 g_variant_get(result , "(s)", &idaddress);
3208 g_variant_unref(result);
3210 if (idaddress == NULL) {
3211 BT_ERR("No paired device");
3212 g_object_unref(device_proxy);
3213 return BLUETOOTH_ERROR_NOT_PAIRED;
3216 BT_DBG("ID Address:%s", idaddress);
3219 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3221 ret = BLUETOOTH_ERROR_INTERNAL;
3223 g_object_unref(device_proxy);
3228 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3230 GapAgentPrivate *agent = _bt_get_adapter_agent();
3231 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3233 if (authentication_reply)
3234 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3236 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3238 BT_DBG("BT_PASSKEY_REPLY");
3239 return BLUETOOTH_ERROR_NONE;
3242 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3244 GapAgentPrivate *agent = _bt_get_adapter_agent();
3245 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3247 if (confirmation_reply)
3248 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3250 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3252 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3253 return BLUETOOTH_ERROR_NONE;