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;
87 gboolean is_device_creating;
88 bt_funcion_data_t *bonding_info;
89 bt_funcion_data_t *searching_info;
90 bt_funcion_data_t *att_mtu_req_info;
92 static GSList *pin_info_list = NULL;
94 /* This HID Mouse does not support pairing precedure. need to skip it. */
95 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
97 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
100 static int __bt_retry_bond(void);
103 /*static void __bt_decline_pair_request()
105 GVariant *out_param1;
106 GVariant *out_param2;
107 request_info_t *req_info;
108 bluetooth_device_info_t dev_info;
109 bt_remote_dev_info_t *remote_dev_info;
110 GVariant *uuid_list, *manufacture_data;
115 req_info = _bt_get_request_info(bonding_info->req_id);
116 if (req_info == NULL) {
117 BT_ERR("req_info == NULL");
120 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
122 BT_DBG("bonding_info is NULL");
127 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
128 remote_dev_info->uuids, remote_dev_info->uuid_count,
131 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
132 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
135 param = g_variant_new("isunsbuba{s}na{y})",
136 bonding_info->result,
138 remote_dev_info->class,
139 remote_dev_info->rssi,
140 remote_dev_info->name,
141 remote_dev_info->paired,
142 remote_dev_info->connected,
143 remote_dev_info->trust,
145 remote_dev_info->manufacturer_data_len,
149 //Send the event to application
150 if (remote_dev_info != NULL) {
151 _bt_send_event(BT_ADAPTER_EVENT,
152 BLUETOOTH_EVENT_BONDING_FINISHED,
155 _bt_free_device_info(remote_dev_info);
158 if (req_info->context == NULL)
161 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
162 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
165 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
166 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
168 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
169 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
172 g_dbus_method_invocation_return_value(req_info->context,
173 g_variant_new("@ayi", out_param1, out_param2));
176 _bt_delete_request_list(req_info->req_id);
179 g_free(bonding_info->addr);
180 g_free(bonding_info);
186 #ifdef TIZEN_PROFILE_WEARABLE
187 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
191 retv_if(user_data == NULL, FALSE);
193 b = (bundle *)user_data;
195 ret = syspopup_launch("bt-syspopup", b);
197 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
199 BT_DBG("Hurray!!! Finally Popup launched");
202 return (ret < 0) ? TRUE : FALSE;
205 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
210 GDBusConnection *conn;
212 conn = _bt_get_system_gconn();
220 bundle_add(b, "event-type", "unable-to-pairing");
222 if (result == BLUETOOTH_ERROR_TIMEOUT)
223 bundle_add(b, "error", "timeout");
224 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
225 bundle_add(b, "error", "authfailed");
227 bundle_add(b, "error", "error");
229 ret = syspopup_launch("bt-syspopup", b);
231 BT_ERR("Popup launch failed...retry %d \n", ret);
232 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
243 gboolean _bt_is_device_creating(void)
245 return is_device_creating;
248 gboolean _bt_is_bonding_device_address(const char *address)
250 if (bonding_info == NULL || bonding_info->addr == NULL)
253 if (g_strcmp0(bonding_info->addr, address) == 0) {
254 BT_DBG("[%s] is bonding device", address);
258 BT_DBG("[%s] is NOT bonding device", address);
262 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
264 ret_if(bonding_info == NULL);
265 bonding_info->is_autopair = is_autopair;
268 void __bt_cancel_search_service_done(void)
270 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
271 request_info_t *req_info;
272 bluetooth_device_info_t dev_info;
273 GVariant *out_param1;
275 ret_if(searching_info == NULL);
277 req_info = _bt_get_request_info(searching_info->req_id);
278 if (req_info == NULL) {
279 BT_ERR("req_info == NULL");
283 if (req_info->context == NULL)
286 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
287 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
288 searching_info->addr);
290 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
291 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
294 g_dbus_method_invocation_return_value(req_info->context,
295 g_variant_new("(iv)", result, out_param1));
297 _bt_delete_request_list(req_info->req_id);
301 g_free(searching_info->addr);
302 g_free(searching_info);
303 searching_info = NULL;
306 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
308 ret_if(value == NULL);
309 ret_if(info == NULL);
311 info->uuid_count = g_variant_get_size(value);
312 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
315 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
317 char *object_path = NULL;
318 bt_remote_dev_info_t *dev_info;
320 retv_if(address == NULL, NULL);
322 object_path = _bt_get_device_object_path(address);
323 retv_if(object_path == NULL, NULL);
325 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
331 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
332 const char *object_path)
334 bt_remote_dev_info_t *dev_info;
335 GDBusProxy *adapter_proxy;
336 GDBusProxy *device_proxy;
341 GDBusConnection *conn;
342 GError *error = NULL;
343 GVariant *result = NULL;
344 GVariantIter *value_iter;
348 adapter_proxy = _bt_get_adapter_proxy();
349 retv_if(adapter_proxy == NULL, NULL);
351 retv_if(object_path == NULL, NULL);
353 conn = _bt_get_system_gconn();
355 BT_ERR("conn == NULL");
359 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
360 NULL, BT_BLUEZ_NAME, object_path,
361 BT_PROPERTIES_INTERFACE, NULL, NULL);
363 retv_if(device_proxy == NULL, NULL);
365 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
366 g_variant_new("(s)", BT_DEVICE_INTERFACE),
367 G_DBUS_CALL_FLAGS_NONE,
372 g_object_unref(device_proxy);
374 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
376 if (result != NULL) {
377 g_variant_get(result , "(@a{sv})", &value);
378 g_variant_unref(result);
380 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
382 g_variant_get(tmp_value, "s", &name);
383 g_variant_unref(tmp_value);
385 DBG_SECURE("Alias Name [%s]", name);
387 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
388 g_variant_get(tmp_value, "s", &name);
389 g_variant_unref(tmp_value);
392 tmp_value = g_variant_lookup_value (value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
394 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
395 g_variant_unref(tmp_value);
397 dev_info->is_alias_set = FALSE;
399 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
401 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
403 dev_info->class = g_variant_get_uint32(tmp_value);
404 g_variant_unref(tmp_value);
408 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
410 dev_info->connected = g_variant_get_byte(tmp_value);
411 g_variant_unref(tmp_value);
413 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
414 BT_DBG("connected link : %d", dev_info->connected);
416 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
418 dev_info->trust = g_variant_get_boolean(tmp_value);
419 g_variant_unref(tmp_value);
421 dev_info->trust = FALSE;
423 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
425 dev_info->paired = g_variant_get_boolean(tmp_value);
426 g_variant_unref(tmp_value);
428 dev_info->paired = FALSE;
430 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
432 dev_info->rssi = g_variant_get_int16(tmp_value);
433 g_variant_unref(tmp_value);
437 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
439 dev_info->addr_type = g_variant_get_byte(tmp_value);
440 g_variant_unref(tmp_value);
442 dev_info->addr_type = 0;
444 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
446 __bt_get_uuids(tmp_value, dev_info);
447 g_variant_unref(tmp_value);
450 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
452 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
453 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
454 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
455 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
457 g_variant_unref(tmp_value);
459 dev_info->manufacturer_data_len = 0;
461 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
463 if ((dev_info->manufacturer_data_len == 0) ||
464 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
465 BT_ERR("manufacturer data length doesn't match");
466 dev_info->manufacturer_data_len = 0;
467 dev_info->manufacturer_data = NULL;
469 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
470 g_variant_get(tmp_value, "ay", &value_iter);
471 while (g_variant_iter_loop(value_iter, "y", &m_value))
472 dev_info->manufacturer_data[i++] = m_value;
474 g_variant_unref(tmp_value);
476 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
477 dev_info->manufacturer_data_len = 0;
478 dev_info->manufacturer_data = NULL;
481 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
482 g_variant_get(tmp_value, "s", &address);
483 g_variant_unref(tmp_value);
485 dev_info->address = g_strdup(address);
486 dev_info->name = g_strdup(name);
488 g_variant_unref(value);
490 BT_ERR("result is NULL\n");
498 char *_bt_get_bonded_device_name(char *address)
500 bluetooth_device_address_t device_address = { {0} };
501 bluetooth_device_info_t dev_info;
503 retv_if(address == NULL, strdup(""));
505 _bt_convert_addr_string_to_type(device_address.addr, address);
507 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
509 _bt_get_bonded_device_info(&device_address, &dev_info);
511 return g_strdup(dev_info.device_name.name);
514 static gboolean __ignore_auto_pairing_request(const char *address)
519 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
530 /* Get the LAP(Lower Address part) */
531 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
533 snprintf(lap_address, sizeof(lap_address), ",%s", address);
535 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
538 BT_ERR("fopen failed \n");
542 fseek(fp, 0, SEEK_END);
547 BT_ERR("Get file size failed \n");
552 buffer = g_malloc0(sizeof(char) * size);
553 /* Fix : NULL_RETURNS */
554 if (buffer == NULL) {
555 BT_ERR("Memory allocation error\n");
559 result = fread((char *)buffer, 1, size, fp);
561 if (result != size) {
562 BT_ERR("Read Error\n");
567 BT_DBG("Buffer = %s\n", buffer);
569 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
575 /* Write the data and insert new device data */
576 for (i = 0; lines[i] != NULL; i++) {
577 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
578 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
580 lines[i] = temp_buffer;
583 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
585 /* Fix : NULL_RETURNS */
586 retv_if(buffer == NULL, FALSE);
588 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
591 BT_ERR("fopen failed \n");
596 BT_DBG("Buffer = %s\n", buffer);
597 fwrite(buffer, 1, strlen(buffer), fp);
607 static int __bt_cancel_bonding(void)
609 BT_CHECK_PARAMETER(bonding_info, return);
610 BT_CHECK_PARAMETER(bonding_info->addr, return);
611 /* First Cancel the ongoing pairing in bluez otherwise if we send
612 * pair request bluez will send inprogress and we again retry bond so
613 * this cycle continues */
615 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
616 NULL, G_DBUS_CALL_FLAGS_NONE,
619 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
621 return BLUETOOTH_ERROR_PARING_FAILED;
623 return BLUETOOTH_ERROR_NONE;
626 static int __bt_retry_bond(void)
628 BT_CHECK_PARAMETER(bonding_info, return);
629 BT_CHECK_PARAMETER(bonding_info->addr, return);
631 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
632 g_variant_new("(y)", bonding_info->conn_type),
633 G_DBUS_CALL_FLAGS_NONE,
636 (GAsyncReadyCallback)__bt_bond_device_cb,
639 return BLUETOOTH_ERROR_NONE;
643 static int __bt_remove_and_bond(void)
645 GDBusProxy *adapter_proxy;
646 GVariant *result = NULL;
648 char *device_path = NULL;
650 BT_CHECK_PARAMETER(bonding_info, return);
651 BT_CHECK_PARAMETER(bonding_info->addr, return);
653 adapter_proxy = _bt_get_adapter_proxy();
654 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
656 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
657 g_variant_new("(s)", bonding_info->addr),
658 G_DBUS_CALL_FLAGS_NONE,
663 return BLUETOOTH_ERROR_INTERNAL;
665 g_variant_get(result , "(o)", &device_path);
666 g_variant_unref(result);
668 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
670 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
671 g_variant_new("(o)", device_path),
672 G_DBUS_CALL_FLAGS_NONE,
678 BT_ERR("UnpairDevice Fail: %s", err->message);
680 return BLUETOOTH_ERROR_INTERNAL;
683 return __bt_retry_bond();
686 static int __bt_cancel_and_bond(void)
688 int ret = BLUETOOTH_ERROR_NONE;
690 ret = _bt_agent_reply_cancellation();
691 if (ret != BLUETOOTH_ERROR_NONE) {
692 BT_ERR("Fail to call reply cancellation");
695 return __bt_retry_bond();
699 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
702 int result = BLUETOOTH_ERROR_NONE;
704 GVariant *out_param1;
705 request_info_t *req_info;
706 bluetooth_device_info_t dev_info;
707 bt_remote_dev_info_t *remote_dev_info;
708 GVariant *manufacture_data;
711 /* Terminate ALL system popup */
712 syspopup_destroy_all();
714 g_dbus_proxy_call_finish(proxy, res, &err);
716 is_device_creating = FALSE;
718 if (bonding_info == NULL) {
720 BT_ERR("bonding_info == NULL");
726 req_info = _bt_get_request_info(bonding_info->req_id);
727 if (req_info == NULL) {
728 BT_ERR("req_info == NULL");
733 g_dbus_error_strip_remote_error(err);
734 BT_ERR("Error occured in CreateBonding [%s]", err->message);
736 if (g_strrstr(err->message, "Already Exists")) {
737 BT_INFO("Existing Bond, remove and retry");
738 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
740 result = BLUETOOTH_ERROR_PARING_FAILED;
741 } else if (_bt_agent_is_canceled() ||
742 g_strrstr(err->message, "Authentication Canceled")) {
743 BT_INFO("Cancelled by USER");
744 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
745 } else if (g_strrstr(err->message, "Authentication Rejected")) {
747 result = BLUETOOTH_ERROR_ACCESS_DENIED;
748 } else if (g_strrstr(err->message, "In Progress")) {
749 BT_INFO("Bond in progress, cancel and retry");
750 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
752 result = BLUETOOTH_ERROR_PARING_FAILED;
753 } else if (g_strrstr(err->message, "Authentication Failed")) {
754 BT_INFO("Authentication Failed");
755 if (bonding_info->is_autopair == TRUE) {
756 _bt_set_autopair_status_in_bonding_info(FALSE);
757 __ignore_auto_pairing_request(bonding_info->addr);
759 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
760 } else if (g_strrstr(err->message, "Page Timeout")) {
761 BT_INFO("Page Timeout");
762 /* This is the special case
763 As soon as call bluetooth_bond_device, try to cancel bonding.
764 In this case, before completing to call 'CreatePairedDevice' method
765 the procedure is stopped. So 'Cancle' error is not return.
767 result = BLUETOOTH_ERROR_HOST_DOWN;
768 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
769 BT_INFO("Cancel already running bonding");
770 if(__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
771 BT_INFO("Error while Cancelling bonding");
772 /* we need to unref proxy so continue */
774 result = BLUETOOTH_ERROR_INTERNAL;
775 } else if (g_strrstr(err->message, "Connection Timeout")) {
776 /* Pairing request timeout */
777 result = BLUETOOTH_ERROR_TIMEOUT;
778 } else if (g_strrstr(err->message, "Authentication Timeout")) {
779 /* Pairing request timeout */
780 result = BLUETOOTH_ERROR_TIMEOUT;
782 BT_DBG("Default case");
783 result = BLUETOOTH_ERROR_PARING_FAILED;
787 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
788 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
789 result == BLUETOOTH_ERROR_TIMEOUT ||
790 result == BLUETOOTH_ERROR_HOST_DOWN) {
791 #ifdef TIZEN_PROFILE_WEARABLE
792 int is_sw_running = 0;
794 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
795 BT_ERR("vconf_get_int for setup wizard state failed");
798 __bt_launch_unable_to_pairing_syspopup(result);
800 BT_ERR("Unable to pair");
802 bonding_info->result = result;
805 g_object_unref(proxy);
806 bonding_info->device_proxy = NULL;
808 if (result != BLUETOOTH_ERROR_NONE) {
809 if (bonding_info->bonding_wo_discovery) {
810 GDBusProxy *adapter_proxy;
811 GVariant *ret = NULL;
812 GError *error = NULL;
815 BT_ERR("Bond was tried without discovery. Remove it");
817 adapter_proxy = _bt_get_adapter_proxy();
818 if (adapter_proxy == NULL) {
819 BT_ERR("Cannot get adapter_proxy");
823 device_path = _bt_get_device_object_path(bonding_info->addr);
824 if (device_path == NULL) {
825 BT_ERR("Cannot get device path");
829 ret = g_dbus_proxy_call_sync(adapter_proxy,
831 g_variant_new("(o)", device_path),
832 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
835 BT_ERR("RemoveDevice Fail: %s", error->message);
836 g_clear_error(&error);
840 g_variant_unref(ret);
848 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
852 GVariant *uuids = NULL;
853 GVariantBuilder *builder = NULL;
855 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
856 for (i = 0; i < remote_dev_info->uuid_count; i++) {
857 g_variant_builder_add(builder, "s",
858 remote_dev_info->uuids[i]);
860 uuids = g_variant_new("as", builder);
861 g_variant_builder_unref(builder);
862 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
863 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
866 param = g_variant_new("(isunsbub@asn@ay)",
869 remote_dev_info->class,
870 remote_dev_info->rssi,
871 remote_dev_info->name,
872 remote_dev_info->paired,
873 remote_dev_info->connected,
874 remote_dev_info->trust,
876 remote_dev_info->manufacturer_data_len,
880 /* Send the event to application */
881 _bt_send_event(BT_ADAPTER_EVENT,
882 BLUETOOTH_EVENT_BONDING_FINISHED,
885 _bt_free_device_info(remote_dev_info);
888 if (req_info->context == NULL)
891 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
892 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
895 if (_bt_adapter_get_status() != BT_ACTIVATED)
896 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
898 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
899 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
901 g_dbus_method_invocation_return_value(req_info->context,
902 g_variant_new("(iv)", result, out_param1));
904 g_variant_unref(out_param1);
906 _bt_delete_request_list(req_info->req_id);
911 _bt_agent_set_canceled(FALSE);
914 g_free(bonding_info->addr);
915 g_free(bonding_info);
919 int _bt_bond_device(int request_id,
920 bluetooth_device_address_t *device_address,
921 unsigned short conn_type, GArray **out_param1)
924 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
925 bluetooth_device_info_t dev_info;
927 GDBusConnection *conn;
928 char *device_path = NULL;
929 GDBusProxy *adapter_proxy;
930 GError *error = NULL;
931 gboolean bonding_wo_discovery = FALSE;
933 BT_CHECK_PARAMETER(device_address, return);
936 BT_ERR("Bonding in progress");
938 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
939 memcpy(dev_info.device_address.addr, device_address->addr,
940 BLUETOOTH_ADDRESS_LENGTH);
942 g_array_append_vals(*out_param1, &dev_info,
943 sizeof(bluetooth_device_info_t));
945 return BLUETOOTH_ERROR_DEVICE_BUSY;
948 conn = _bt_get_system_gconn();
949 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
951 _bt_convert_addr_type_to_string(address, device_address->addr);
953 device_path = _bt_get_device_object_path(address);
955 if (device_path == NULL) {
956 BT_ERR("No searched device");
957 GVariant *ret = NULL;
958 adapter_proxy = _bt_get_adapter_proxy();
959 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
961 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
962 g_variant_new("(s)", address),
963 G_DBUS_CALL_FLAGS_NONE,
969 BT_ERR("CreateDevice Fail: %s", error->message);
970 g_clear_error(&error);
973 g_variant_unref(ret);
974 device_path = _bt_get_device_object_path(address);
975 if (device_path == NULL) {
976 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
977 memcpy(dev_info.device_address.addr, device_address->addr,
978 BLUETOOTH_ADDRESS_LENGTH);
980 g_array_append_vals(*out_param1, &dev_info,
981 sizeof(bluetooth_device_info_t));
983 return BLUETOOTH_ERROR_NOT_PAIRED;
985 BT_INFO("device_path is created[%s]", device_path);
987 bonding_wo_discovery = TRUE;
990 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
992 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
995 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
997 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
998 bonding_info->addr = g_strdup(address);
999 bonding_info->req_id = request_id;
1001 bonding_info->device_proxy = proxy;
1002 bonding_info->conn_type = conn_type;
1003 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
1005 is_device_creating = TRUE;
1007 g_dbus_proxy_call(proxy, "Pair",
1008 g_variant_new("(y)", conn_type),
1009 G_DBUS_CALL_FLAGS_NONE,
1010 BT_MAX_DBUS_TIMEOUT,
1012 (GAsyncReadyCallback)__bt_bond_device_cb,
1015 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
1017 return BLUETOOTH_ERROR_NONE;
1019 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1020 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1021 bonding_info->addr);
1023 g_array_append_vals(*out_param1, &dev_info,
1024 sizeof(bluetooth_device_info_t));
1026 is_device_creating = FALSE;
1028 g_free(bonding_info->addr);
1029 g_free(bonding_info);
1030 bonding_info = NULL;
1032 return BLUETOOTH_ERROR_INTERNAL;*/
1035 int _bt_cancel_bonding(void)
1037 int ret = BLUETOOTH_ERROR_NONE;
1039 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1041 ret = _bt_agent_reply_cancellation();
1042 if (ret != BLUETOOTH_ERROR_NONE) {
1043 BT_ERR("Fail to call reply cancellation");
1047 _bt_agent_set_canceled(TRUE);
1049 return BLUETOOTH_ERROR_NONE;
1052 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1056 GVariant *out_param1;
1057 int result = BLUETOOTH_ERROR_NONE;
1058 bt_funcion_data_t *unbonding_info;
1059 bluetooth_device_info_t dev_info;
1060 request_info_t *req_info;
1062 g_dbus_proxy_call_finish(proxy, res, &err);
1064 unbonding_info = user_data;
1066 if (unbonding_info == NULL) {
1068 BT_ERR("unbonding_info == NULL");
1072 req_info = _bt_get_request_info(unbonding_info->req_id);
1073 if (req_info == NULL) {
1074 BT_ERR("req_info == NULL");
1079 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1080 result = BLUETOOTH_ERROR_INTERNAL;
1083 if (req_info->context == NULL)
1086 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1087 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1088 unbonding_info->addr);
1090 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1091 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1093 g_dbus_method_invocation_return_value(req_info->context,
1094 g_variant_new("(iv)", result, out_param1));
1097 _bt_delete_request_list(req_info->req_id);
1103 if (unbonding_info) {
1104 g_free(unbonding_info->addr);
1105 g_free(unbonding_info);
1109 int _bt_unbond_device(int request_id,
1110 bluetooth_device_address_t *device_address,
1111 GArray **out_param1)
1113 char *device_path = NULL;
1114 bt_funcion_data_t *unbonding_info;
1115 GDBusProxy *adapter_proxy = NULL;
1116 GDBusProxy *device_proxy = NULL;
1117 GDBusConnection *conn;
1118 int result = BLUETOOTH_ERROR_INTERNAL;
1119 bluetooth_device_info_t dev_info;
1120 GError *error = NULL;
1121 GVariant *ret = NULL;
1123 BT_CHECK_PARAMETER(device_address, return);
1125 adapter_proxy = _bt_get_adapter_proxy();
1126 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1128 /* allocate user data so that it can be retrieved in callback */
1129 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1130 /* Fix : NULL_RETURNS */
1131 if (unbonding_info == NULL) {
1132 BT_ERR("Memory not allocated !");
1133 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1136 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1137 unbonding_info->req_id = request_id;
1139 _bt_convert_addr_type_to_string(unbonding_info->addr,
1140 device_address->addr);
1142 device_path = _bt_get_device_object_path(unbonding_info->addr);
1144 if (device_path == NULL) {
1145 BT_ERR("No paired device");
1146 result = BLUETOOTH_ERROR_NOT_PAIRED;
1150 conn = _bt_get_system_gconn();
1152 BT_ERR("conn is NULL");
1153 result = BLUETOOTH_ERROR_INTERNAL;
1157 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1158 NULL, BT_BLUEZ_NAME,
1159 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1161 if (device_proxy != NULL) {
1163 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1164 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1165 G_DBUS_CALL_FLAGS_NONE,
1170 BT_ERR("Getting property failed: [%s]\n", error->message);
1171 g_error_free(error);
1172 result = BLUETOOTH_ERROR_NOT_PAIRED;
1176 BT_ERR("No paired device");
1177 g_object_unref(device_proxy);
1178 result = BLUETOOTH_ERROR_NOT_PAIRED;
1181 g_variant_unref(ret);
1183 g_object_unref(device_proxy);
1186 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1187 g_variant_new("(o)", device_path),
1188 G_DBUS_CALL_FLAGS_NONE,
1189 BT_MAX_DBUS_TIMEOUT,
1191 (GAsyncReadyCallback)__bt_unbond_cb,
1194 g_free(device_path);
1195 return BLUETOOTH_ERROR_NONE;
1198 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1199 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1200 unbonding_info->addr);
1202 g_array_append_vals(*out_param1, &dev_info,
1203 sizeof(bluetooth_device_info_t));
1204 g_free(device_path);
1205 g_free(unbonding_info->addr);
1206 g_free(unbonding_info);
1210 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1214 GVariant *out_param1;
1215 int result = BLUETOOTH_ERROR_NONE;
1216 bluetooth_device_info_t dev_info;
1217 bt_remote_dev_info_t *remote_dev_info;
1218 request_info_t *req_info;
1219 GVariant *uuid_list, *manufacture_data;
1221 GVariantBuilder *builder = NULL;
1224 g_dbus_proxy_call_finish(proxy, res, &err);
1226 g_object_unref(proxy);
1228 if (searching_info == NULL) {
1230 BT_ERR("unbonding_info == NULL");
1234 req_info = _bt_get_request_info(searching_info->req_id);
1235 if (req_info == NULL) {
1236 BT_ERR("req_info == NULL");
1241 g_dbus_error_strip_remote_error(err);
1242 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1244 if (g_strrstr("Operation canceled", err->message)) {
1245 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1246 } else if (g_strrstr("In Progress", err->message)) {
1247 result = BLUETOOTH_ERROR_IN_PROGRESS;
1248 } else if (g_strrstr("Host is down", err->message)) {
1249 result = BLUETOOTH_ERROR_HOST_DOWN;
1251 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1254 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1255 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1256 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1257 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1258 remote_dev_info->uuid_count > 0) {
1259 result = BLUETOOTH_ERROR_NONE;
1262 _bt_free_device_info(remote_dev_info);
1267 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1268 if(!remote_dev_info)
1272 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1273 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1274 g_variant_builder_add(builder, "s",
1275 remote_dev_info->uuids[i]);
1277 uuid_list = g_variant_new("as", builder);
1278 g_variant_builder_unref(builder);
1279 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1280 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1283 param = g_variant_new("(isunsbub@asn@ay)",
1285 searching_info->addr,
1286 remote_dev_info->class,
1287 remote_dev_info->rssi,
1288 remote_dev_info->name,
1289 remote_dev_info->paired,
1290 remote_dev_info->connected,
1291 remote_dev_info->trust,
1293 remote_dev_info->manufacturer_data_len,
1296 /* Send the event to application */
1297 _bt_send_event(BT_ADAPTER_EVENT,
1298 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1301 _bt_free_device_info(remote_dev_info);
1304 if (req_info->context == NULL)
1307 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1308 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1309 searching_info->addr);
1311 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1312 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1314 g_dbus_method_invocation_return_value(req_info->context,
1315 g_variant_new("(iv)", result, out_param1));
1318 _bt_delete_request_list(req_info->req_id);
1323 if (searching_info) {
1324 g_free(searching_info->addr);
1325 g_free(searching_info);
1326 searching_info = NULL;
1330 int _bt_search_device(int request_id,
1331 bluetooth_device_address_t *device_address)
1333 char *device_path = NULL;
1334 GDBusProxy *device_proxy = NULL;
1335 GDBusConnection *conn;
1337 GDBusProxy *adapter_proxy;
1338 int result = BLUETOOTH_ERROR_INTERNAL;
1340 BT_CHECK_PARAMETER(device_address, return);
1343 BT_ERR("Bonding in progress");
1344 return BLUETOOTH_ERROR_DEVICE_BUSY;
1347 if (searching_info) {
1348 BT_ERR("Service searching in progress");
1349 return BLUETOOTH_ERROR_DEVICE_BUSY;
1352 adapter_proxy = _bt_get_adapter_proxy();
1353 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1355 /* allocate user data so that it can be retrieved in callback */
1356 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1357 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1358 searching_info->req_id = request_id;
1360 _bt_convert_addr_type_to_string(searching_info->addr,
1361 device_address->addr);
1363 conn = _bt_get_system_gconn();
1364 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1367 device_path = _bt_get_device_object_path(searching_info->addr);
1369 if (device_path == NULL) {
1370 BT_ERR("No paired device");
1371 result = BLUETOOTH_ERROR_NOT_PAIRED;
1375 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1376 NULL, BT_BLUEZ_NAME,
1377 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1378 g_free(device_path);
1379 if (device_proxy == NULL) {
1380 result = BLUETOOTH_ERROR_INTERNAL;
1384 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1385 g_variant_new("(s)", ""),
1386 G_DBUS_CALL_FLAGS_NONE,
1387 BT_MAX_DBUS_TIMEOUT,
1389 (GAsyncReadyCallback)__bt_discover_cb,
1392 searching_info->device_proxy = device_proxy;
1394 return BLUETOOTH_ERROR_NONE;
1397 g_free(searching_info->addr);
1398 g_free(searching_info);
1399 searching_info = NULL;
1403 int _bt_cancel_search_device(void)
1405 GVariant *ret = NULL;
1408 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1410 if (searching_info->device_proxy) {
1411 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1413 G_DBUS_CALL_FLAGS_NONE,
1418 g_variant_unref(ret);
1420 __bt_cancel_search_service_done();
1422 return BLUETOOTH_ERROR_NONE;
1425 int _bt_set_alias(bluetooth_device_address_t *device_address,
1428 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1429 gchar *device_path = NULL;
1430 GDBusProxy *adapter_proxy;
1431 GDBusProxy *device_proxy;
1432 GVariant *ret = NULL;
1433 GError *error = NULL;
1434 GDBusConnection *conn;
1436 BT_CHECK_PARAMETER(device_address, return);
1437 BT_CHECK_PARAMETER(alias, return);
1439 adapter_proxy = _bt_get_adapter_proxy();
1440 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1442 conn = _bt_get_system_gconn();
1443 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1445 _bt_convert_addr_type_to_string(address, device_address->addr);
1447 device_path = _bt_get_device_object_path(address);
1449 if (device_path == NULL) {
1450 BT_ERR("No paired device");
1451 return BLUETOOTH_ERROR_NOT_PAIRED;
1454 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1455 NULL, BT_BLUEZ_NAME,
1456 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1458 g_free(device_path);
1459 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1461 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1462 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1463 G_DBUS_CALL_FLAGS_NONE,
1468 g_variant_unref(ret);
1470 g_object_unref(device_proxy);
1473 BT_ERR("SetProperty error: [%s]", error->message);
1474 g_error_free(error);
1475 return BLUETOOTH_ERROR_INTERNAL;
1478 return BLUETOOTH_ERROR_NONE;
1481 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1484 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1485 gchar *device_path = NULL;
1486 GDBusProxy *device_proxy;
1487 gboolean previous_value;
1488 GError *error = NULL;
1489 GDBusConnection *conn;
1490 GVariant *result = NULL;
1491 GVariant *temp = NULL;
1492 int ret = BLUETOOTH_ERROR_NONE;
1494 BT_CHECK_PARAMETER(device_address, return);
1496 conn = _bt_get_system_gconn();
1497 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1499 _bt_convert_addr_type_to_string(address, device_address->addr);
1501 device_path = _bt_get_device_object_path(address);
1503 if (device_path == NULL) {
1504 BT_ERR("No paired device");
1505 return BLUETOOTH_ERROR_NOT_PAIRED;
1508 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1509 NULL, BT_BLUEZ_NAME,
1510 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1512 g_free(device_path);
1513 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1515 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1516 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1517 G_DBUS_CALL_FLAGS_NONE,
1521 if (error != NULL) {
1522 BT_ERR("Getting property failed: [%s]\n", error->message);
1523 g_error_free(error);
1524 g_object_unref(device_proxy);
1525 return BLUETOOTH_ERROR_INTERNAL;
1528 g_variant_get(result, "(v)", &temp);
1529 previous_value = g_variant_get_boolean(temp);
1530 g_variant_unref(temp);
1531 g_variant_unref(result);
1532 /* If the input is same with previous value, return error. */
1533 if (previous_value == authorize) {
1534 BT_ERR("Same value: %d", previous_value);
1535 g_object_unref(device_proxy);
1536 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1540 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1541 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
1542 G_DBUS_CALL_FLAGS_NONE,
1547 g_object_unref(device_proxy);
1549 BT_ERR("SetProperty error: [%s]", error->message);
1550 g_error_free(error);
1551 ret = BLUETOOTH_ERROR_INTERNAL;
1555 g_variant_unref(result);
1560 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1561 gboolean *is_connected)
1563 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1564 char *object_path = NULL;
1566 GDBusProxy *device_proxy;
1567 GError *error = NULL;
1569 GVariant *tmp_value;
1570 GDBusConnection *conn;
1571 GVariant *result = NULL;
1572 int ret = BLUETOOTH_ERROR_NONE;
1574 BT_CHECK_PARAMETER(device_address, return);
1576 conn = _bt_get_system_gconn();
1577 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1579 _bt_convert_addr_type_to_string(address, device_address->addr);
1581 object_path = _bt_get_device_object_path(address);
1582 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1584 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1585 NULL, BT_BLUEZ_NAME, object_path,
1586 BT_PROPERTIES_INTERFACE, NULL, NULL);
1587 g_free(object_path);
1588 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1590 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1591 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1592 G_DBUS_CALL_FLAGS_NONE,
1596 if (result == NULL) {
1597 if (error != NULL) {
1598 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1599 g_error_free(error);
1601 g_object_unref(device_proxy);
1602 return BLUETOOTH_ERROR_INTERNAL;
1605 g_variant_get(result , "(@a{sv})", &value);
1606 g_variant_unref(result);
1608 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1609 if (tmp_value == NULL) {
1610 g_object_unref(device_proxy);
1611 g_variant_unref(value);
1612 return BLUETOOTH_ERROR_INTERNAL;
1615 *is_connected = g_variant_get_boolean(tmp_value);
1617 BT_DBG("gatt is connected : %d", *is_connected);
1619 g_variant_unref(tmp_value);
1620 g_variant_unref(value);
1621 g_object_unref(device_proxy);
1626 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1627 int connection_type, gboolean *is_connected)
1629 char *object_path = NULL;
1630 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1631 GDBusProxy *device_proxy = NULL;
1632 GDBusProxy *adapter_proxy = NULL;
1633 GDBusConnection *conn;
1634 GError *error = NULL;
1635 GVariant *tmp_value = NULL;
1636 GVariant *value = NULL;
1637 GVariant *result = NULL;
1640 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1641 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1643 *is_connected = FALSE;
1644 BT_DBG("connection_type: %d", connection_type);
1645 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1646 return _bt_rfcomm_is_device_connected(device_address,
1648 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1649 return _bt_is_gatt_connected(device_address, is_connected);
1651 adapter_proxy = _bt_get_adapter_proxy();
1652 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1654 conn = _bt_get_system_gconn();
1655 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1657 _bt_convert_addr_type_to_string(address, device_address->addr);
1659 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1660 object_path = _bt_get_adapter_path();
1661 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1662 NULL, BT_BLUEZ_NAME,
1663 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1664 g_free(object_path);
1665 if (device_proxy == NULL) {
1666 BT_DBG("Device don't have this service");
1667 return BLUETOOTH_ERROR_INTERNAL;
1670 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1671 g_variant_new("(s)", address),
1672 G_DBUS_CALL_FLAGS_NONE,
1677 if (result == NULL) {
1678 BT_ERR("[GetProperties] Error occured in Proxy call");
1680 BT_ERR("%s", error->message);
1681 g_error_free(error);
1683 g_object_unref(device_proxy);
1684 return BLUETOOTH_ERROR_INTERNAL;
1686 g_variant_get(result , "(@a{sv})", &value);
1687 g_variant_unref(result);
1690 tmp_value = g_variant_lookup_value(value,
1692 G_VARIANT_TYPE_BOOLEAN);
1694 *is_connected = g_variant_get_boolean(tmp_value);
1695 g_variant_unref(tmp_value);
1697 g_variant_unref(value);
1699 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1700 return _bt_is_network_connected(_bt_get_net_conn(),
1701 device_address->addr, is_connected);
1703 uuid = _bt_get_profile_uuid128(connection_type);
1705 BT_ERR("uuid is NULL");
1706 return BLUETOOTH_ERROR_INTERNAL;
1709 BT_DBG("uuid: %s", uuid);
1711 object_path = _bt_get_device_object_path(address);
1712 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1713 BT_DBG("object_path: %s", object_path);
1714 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1715 NULL, BT_BLUEZ_NAME,
1716 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1717 g_free(object_path);
1718 if (device_proxy == NULL) {
1719 BT_DBG("Device don't have this service");
1721 return BLUETOOTH_ERROR_INTERNAL;
1724 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1725 g_variant_new("(s)", uuid),
1726 G_DBUS_CALL_FLAGS_NONE,
1731 if (result == NULL) {
1732 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1734 BT_ERR("%s", error->message);
1735 if (g_strrstr(error->message, "Not Connected"))
1736 BT_DBG("Not connected");
1737 g_error_free(error);
1739 *is_connected = FALSE;
1740 g_object_unref(device_proxy);
1742 return BLUETOOTH_ERROR_NONE;
1744 g_variant_get(result, "(b)", is_connected);
1746 g_variant_unref(result);
1749 g_object_unref(device_proxy);
1750 return BLUETOOTH_ERROR_NONE;
1753 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1754 bluetooth_connected_link_t *connected)
1756 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1757 char *object_path = NULL;
1759 GDBusProxy *device_proxy;
1760 GError *error = NULL;
1761 GDBusConnection *conn;
1762 GVariant *tmp_value = NULL;
1763 GVariant *value = NULL;
1764 GVariant *result = NULL;
1766 BT_CHECK_PARAMETER(device_address, return);
1768 conn = _bt_get_system_gconn();
1769 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1771 _bt_convert_addr_type_to_string(address, device_address->addr);
1773 object_path = _bt_get_device_object_path(address);
1774 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1776 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1777 NULL, BT_BLUEZ_NAME,
1778 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1779 g_free(object_path);
1780 if (device_proxy == NULL) {
1781 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1782 return BLUETOOTH_ERROR_NONE;
1785 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1786 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1787 G_DBUS_CALL_FLAGS_NONE,
1792 if (error != NULL) {
1793 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1794 g_error_free(error);
1795 g_object_unref(device_proxy);
1796 return BLUETOOTH_ERROR_INTERNAL;
1799 g_variant_get(result , "(@a{sv})", &value);
1800 g_variant_unref(result);
1802 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1803 if (tmp_value != NULL) {
1804 *connected = g_variant_get_byte(tmp_value);
1805 g_variant_unref(tmp_value);
1806 g_object_unref(device_proxy);
1807 return BLUETOOTH_ERROR_NONE;
1809 BT_ERR("g_variant value is NULL");
1810 return BLUETOOTH_ERROR_INTERNAL;
1814 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1818 GVariant *out_param1;
1820 int result = BLUETOOTH_ERROR_NONE;
1821 bt_function_data_t *func_data = user_data;
1822 request_info_t *req_info = NULL;
1823 bluetooth_device_address_t device_addr = { {0} };
1825 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1826 g_object_unref(proxy);
1828 if (reply == NULL) {
1829 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1831 BT_ERR("Error: %s", err->message);
1832 g_clear_error(&err);
1834 result = BLUETOOTH_ERROR_INTERNAL;
1836 g_variant_unref(reply);
1839 if (func_data == NULL) {
1840 BT_ERR("func_data is NULL");
1844 req_info = _bt_get_request_info(func_data->req_id);
1845 if (req_info == NULL) {
1846 BT_ERR("req_info is NULL");
1850 if (req_info->context == NULL) {
1851 BT_ERR("req_info->context is NULL");
1855 _bt_convert_addr_string_to_type(device_addr.addr,
1856 (const char *)func_data->address);
1858 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1859 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1862 g_dbus_method_invocation_return_value(req_info->context,
1863 g_variant_new("(iv)", result, out_param1));
1867 _bt_delete_request_list(req_info->req_id);
1870 g_free(func_data->address);
1875 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1876 gboolean auto_connect)
1878 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1879 gchar *device_path = NULL;
1880 GDBusProxy *device_proxy = NULL;
1881 GDBusConnection *conn;
1882 int ret = BLUETOOTH_ERROR_NONE;
1883 bt_function_data_t *func_data;
1885 BT_CHECK_PARAMETER(bd_addr, return);
1887 conn = _bt_get_system_gconn();
1888 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1890 _bt_convert_addr_type_to_string(device_address,
1891 (unsigned char *)bd_addr->addr);
1892 device_path = _bt_get_device_object_path(device_address);
1893 if (device_path == NULL) {
1894 BT_ERR_C("device_path NULL : [%s]", device_address);
1895 ret = BLUETOOTH_ERROR_INTERNAL;
1899 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1900 NULL, BT_BLUEZ_NAME,
1901 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1902 g_free(device_path);
1903 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1905 func_data = g_malloc0(sizeof(bt_function_data_t));
1906 if (func_data == NULL) {
1907 BT_ERR("Unable to allocate memory for address");
1908 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1912 func_data->address = g_strdup(device_address);
1913 if (func_data->address == NULL) {
1914 BT_ERR("Unable to allocate memory for address");
1915 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1919 func_data->req_id = req_id;
1921 ret = _bt_hold_current_advertising();
1922 if (ret != BLUETOOTH_ERROR_NONE) {
1923 BT_ERR("Unable to hold advertising");
1926 g_dbus_proxy_call(device_proxy, "ConnectLE",
1927 g_variant_new("(b)", auto_connect),
1928 G_DBUS_CALL_FLAGS_NONE,
1929 BT_MAX_DBUS_TIMEOUT,
1931 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1937 g_object_unref(device_proxy);
1939 g_free(func_data->address);
1945 int _bt_disconnect_le_device(int req_id,
1946 const bluetooth_device_address_t *bd_addr)
1948 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1950 GDBusProxy *device_proxy;
1951 GDBusConnection *conn;
1952 int ret = BLUETOOTH_ERROR_NONE;
1953 bt_function_data_t *func_data;
1955 BT_CHECK_PARAMETER(bd_addr, return);
1957 conn = _bt_get_system_gconn();
1958 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1960 _bt_convert_addr_type_to_string(device_address,
1961 (unsigned char *)bd_addr->addr);
1962 device_path = _bt_get_device_object_path(device_address);
1963 if (device_path == NULL) {
1964 BT_DBG("device_path NULL");
1965 ret = BLUETOOTH_ERROR_INTERNAL;
1969 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1971 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1972 NULL, BT_BLUEZ_NAME,
1973 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1974 g_free(device_path);
1975 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1977 func_data = g_malloc0(sizeof(bt_function_data_t));
1978 if (func_data == NULL) {
1979 BT_ERR("Unable to allocate memory for address");
1980 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1984 func_data->address = g_strdup(device_address);
1985 if (func_data->address == NULL) {
1986 BT_ERR("Unable to allocate memory for address");
1987 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1991 func_data->req_id = req_id;
1993 g_dbus_proxy_call(device_proxy, "DisconnectLE",
1995 G_DBUS_CALL_FLAGS_NONE,
1996 BT_MAX_DBUS_TIMEOUT,
1998 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2003 g_object_unref(device_proxy);
2005 g_free(func_data->address);
2011 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2013 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2014 gchar *device_path = NULL;
2015 GError *error = NULL;
2016 GDBusProxy *device_proxy = NULL;
2017 GDBusProxy *adapter_proxy;
2018 GDBusConnection *conn;
2019 int ret = BLUETOOTH_ERROR_NONE;
2021 BT_CHECK_PARAMETER(bd_addr, return);
2023 _bt_convert_addr_type_to_string(device_address,
2024 (unsigned char *)bd_addr->addr);
2026 conn = _bt_get_system_gconn();
2027 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2029 adapter_proxy = _bt_get_adapter_proxy();
2030 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2032 device_path = _bt_get_device_object_path(device_address);
2033 if (device_path == NULL) {
2034 BT_DBG("device_path NULL");
2035 ret = BLUETOOTH_ERROR_INTERNAL;
2039 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2041 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2042 NULL, BT_BLUEZ_NAME,
2043 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2044 g_free(device_path);
2045 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2047 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2049 G_DBUS_CALL_FLAGS_NONE,
2054 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2055 g_error_free(error);
2056 g_object_unref(device_proxy);
2057 return BLUETOOTH_ERROR_INTERNAL;
2060 g_object_unref(device_proxy);
2065 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2067 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2068 gchar *device_path = NULL;
2069 GError *error = NULL;
2070 GDBusProxy *device_proxy = NULL;
2071 GDBusProxy *adapter_proxy;
2072 GDBusConnection *conn;
2073 int ret = BLUETOOTH_ERROR_NONE;
2075 BT_CHECK_PARAMETER(bd_addr, return);
2077 _bt_convert_addr_type_to_string(device_address,
2078 (unsigned char *)bd_addr->addr);
2080 conn = _bt_get_system_gconn();
2081 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2083 adapter_proxy = _bt_get_adapter_proxy();
2084 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2086 device_path = _bt_get_device_object_path(device_address);
2087 if (device_path == NULL) {
2088 BT_DBG("device_path NULL");
2089 ret = BLUETOOTH_ERROR_INTERNAL;
2093 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2095 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2096 NULL, BT_BLUEZ_NAME,
2097 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2098 g_free(device_path);
2099 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2101 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2103 G_DBUS_CALL_FLAGS_NONE,
2108 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2109 g_error_free(error);
2110 g_object_unref(device_proxy);
2111 return BLUETOOTH_ERROR_INTERNAL;
2114 g_object_unref(device_proxy);
2119 int _bt_connect_profile(char *address, char *uuid,
2120 void *cb, gpointer func_data)
2124 GDBusConnection *conn;
2125 GDBusProxy *adapter_proxy;
2126 GVariant *result = NULL;
2127 GError *error = NULL;
2129 conn = _bt_get_system_gconn();
2130 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2132 object_path = _bt_get_device_object_path(address);
2133 if (object_path == NULL) {
2134 BT_ERR("No searched device");
2136 adapter_proxy = _bt_get_adapter_proxy();
2137 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2139 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2140 g_variant_new("(s)", address),
2141 G_DBUS_CALL_FLAGS_NONE,
2146 if (error != NULL) {
2147 BT_ERR("CreateDevice Fail: %s", error->message);
2148 g_error_free(error);
2151 g_variant_unref(result);
2153 object_path = _bt_get_device_object_path(address);
2155 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2157 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2158 NULL, BT_BLUEZ_NAME,
2159 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2160 g_free(object_path);
2161 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2163 g_dbus_proxy_call(proxy, "ConnectProfile",
2164 g_variant_new("(s)", uuid),
2165 G_DBUS_CALL_FLAGS_NONE,
2166 BT_MAX_DBUS_TIMEOUT,
2168 (GAsyncReadyCallback)cb,
2171 return BLUETOOTH_ERROR_NONE;
2174 int _bt_disconnect_profile(char *address, char *uuid,
2175 void *cb, gpointer func_data)
2179 GDBusConnection *conn;
2181 conn = _bt_get_system_gconn();
2182 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2184 object_path = _bt_get_device_object_path(address);
2185 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2187 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2188 NULL, BT_BLUEZ_NAME,
2189 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2190 g_free(object_path);
2191 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2193 g_dbus_proxy_call(proxy, "DisconnectProfile",
2194 g_variant_new("(s)", uuid),
2195 G_DBUS_CALL_FLAGS_NONE,
2196 BT_MAX_DBUS_TIMEOUT,
2198 (GAsyncReadyCallback)cb,
2201 return BLUETOOTH_ERROR_NONE;
2204 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2205 int low_threshold, int in_range_threshold, int high_threshold)
2207 int ret = BLUETOOTH_ERROR_NONE;
2209 GVariant *result = NULL;
2210 GError *error = NULL;
2211 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2213 BT_CHECK_PARAMETER(bd_addr, return);
2214 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2215 bd_addr->addr[0], bd_addr->addr[1],
2216 bd_addr->addr[2], bd_addr->addr[3],
2217 bd_addr->addr[4], bd_addr->addr[5],
2219 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2220 in_range_threshold, high_threshold);
2222 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2224 proxy = _bt_get_adapter_proxy();
2225 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2227 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2228 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2229 G_DBUS_CALL_FLAGS_NONE,
2233 if (error != NULL) {
2234 BT_ERR("Dbus Call Error:[%s]", error->message);
2235 g_error_free(error);
2236 ret = BLUETOOTH_ERROR_INTERNAL;
2240 g_variant_unref(result);
2245 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2248 int ret = BLUETOOTH_ERROR_NONE;
2250 GVariant *result = NULL;
2251 GError *error = NULL;
2252 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2254 BT_CHECK_PARAMETER(bd_addr, return);
2255 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2256 bd_addr->addr[0], bd_addr->addr[1],
2257 bd_addr->addr[2], bd_addr->addr[3],
2258 bd_addr->addr[4], bd_addr->addr[5],
2261 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2263 proxy = _bt_get_adapter_proxy();
2264 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2266 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2267 g_variant_new("(si)", address, link_type),
2268 G_DBUS_CALL_FLAGS_NONE,
2273 if (error != NULL) {
2274 BT_ERR("Dbus Call Error:[%s]", error->message);
2275 g_error_free(error);
2276 ret = BLUETOOTH_ERROR_INTERNAL;
2280 g_variant_unref(result);
2285 int _bt_le_conn_update(unsigned char *device_address,
2286 float interval_min, float interval_max,
2287 guint16 latency, guint16 time_out)
2289 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2290 gchar *device_path = NULL;
2291 GError *error = NULL;
2292 GDBusProxy *device_proxy = NULL;
2293 GDBusConnection *conn;
2295 guint32 min, max, to;
2296 guint32 min_supervision_to;
2297 int ret = BLUETOOTH_ERROR_NONE;
2299 BT_CHECK_PARAMETER(device_address, return);
2301 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2302 interval_min, interval_max, latency, time_out);
2304 if (interval_min > interval_max ||
2305 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2306 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2307 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2311 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2312 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2313 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2317 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2318 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2323 * The Supervision_Timeout in milliseconds shall be larger than
2324 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2325 * where Conn_Interval_Max is given in milliseconds.
2327 min_supervision_to = (1 + latency) * interval_max * 2;
2328 if (time_out <= min_supervision_to) {
2329 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2333 _bt_convert_addr_type_to_string(address, device_address);
2335 BT_DBG("Remote device address: %s", address);
2337 device_path = _bt_get_device_object_path(address);
2339 if (device_path == NULL) {
2340 BT_ERR("device_path NULL");
2341 ret = BLUETOOTH_ERROR_INTERNAL;
2345 conn = _bt_get_system_gconn();
2347 BT_ERR("conn NULL");
2348 ret = BLUETOOTH_ERROR_INTERNAL;
2352 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2353 NULL, BT_BLUEZ_NAME,
2354 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2356 g_free(device_path);
2357 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2359 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2360 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2361 to = time_out / BT_LE_CONN_TO_SPLIT;
2363 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2364 g_variant_new("(uuuu)", min, max, latency, to),
2365 G_DBUS_CALL_FLAGS_NONE,
2370 g_object_unref(device_proxy);
2371 if (reply == NULL) {
2373 BT_ERR("Error %s[%s]", error->message, address);
2374 if (g_strrstr(error->message, "In Progress"))
2375 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2377 ret = BLUETOOTH_ERROR_INTERNAL;
2378 g_error_free(error);
2382 g_variant_unref(reply);
2388 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2389 bluetooth_device_pin_code_t *pin_code)
2391 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2393 bt_pin_code_info_t *pin_info = NULL;
2395 BT_CHECK_PARAMETER(device_address, return);
2396 BT_CHECK_PARAMETER(pin_code, return);
2397 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2398 BLUETOOTH_ERROR_NO_RESOURCES);
2400 _bt_convert_addr_type_to_string(address, device_address->addr);
2402 for (l = pin_info_list; l != NULL; l = l->next) {
2405 if (g_strcmp0(pin_info->address, address) == 0) {
2406 g_free(pin_info->pin_code);
2407 pin_info->pin_code = g_strdup(pin_code->pin_code);
2408 return BLUETOOTH_ERROR_NONE;
2412 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2413 pin_info->address = g_strdup(address);
2414 pin_info->pin_code = g_strdup(pin_code->pin_code);
2415 pin_info_list = g_slist_append(pin_info_list, pin_info);
2417 return BLUETOOTH_ERROR_NONE;
2420 gint __bt_compare_address(gpointer *a, gpointer *b)
2422 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2423 char *address = (char *)b;
2424 return g_strcmp0(pin_info->address, address);
2427 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2429 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2431 bt_pin_code_info_t *pin_info = NULL;
2433 BT_CHECK_PARAMETER(device_address, return);
2435 _bt_convert_addr_type_to_string(address, device_address->addr);
2437 l = g_slist_find_custom(pin_info_list, address,
2438 (GCompareFunc)__bt_compare_address);
2442 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2443 g_free(pin_info->address);
2444 g_free(pin_info->pin_code);
2448 return BLUETOOTH_ERROR_NONE;
2451 int _bt_get_device_pin_code(const char *address, char *pin_code)
2455 BT_CHECK_PARAMETER(address, return);
2456 BT_CHECK_PARAMETER(pin_code, return);
2458 for (l = pin_info_list; l != NULL; l = l->next) {
2459 bt_pin_code_info_t *pin_info = l->data;
2461 if (g_strcmp0(pin_info->address, address) == 0) {
2462 g_strlcpy(pin_code, pin_info->pin_code,
2463 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2465 return BLUETOOTH_ERROR_NONE;
2469 return BLUETOOTH_ERROR_NOT_FOUND;
2472 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2473 bluetooth_le_connection_param_t *param)
2476 return BLUETOOTH_ERROR_INVALID_PARAM;
2478 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2479 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2480 return BLUETOOTH_ERROR_INVALID_PARAM;
2482 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2485 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2486 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2487 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2488 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2489 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2492 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2493 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2494 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2495 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2496 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2499 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2500 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2501 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2502 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2503 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2507 BT_ERR("Unhandled mode : %d", mode);
2511 return BLUETOOTH_ERROR_NONE;
2514 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2515 guint trusted_profile_flag, guint *trusted)
2521 case TRUSTED_PROFILE_PBAP:
2522 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2523 trust_profile = trusted_profile_flag & (1 << 1);
2525 return BLUETOOTH_ERROR_NOT_SUPPORT;
2527 case TRUSTED_PROFILE_MAP:
2528 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2529 trust_profile = trusted_profile_flag & (1 << 3);
2531 return BLUETOOTH_ERROR_NOT_SUPPORT;
2533 case TRUSTED_PROFILE_SAP:
2534 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2535 trust_profile = trusted_profile_flag & (1 << 5);
2537 return BLUETOOTH_ERROR_NOT_SUPPORT;
2539 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2540 *trusted = trusted_profile_flag;
2541 return BLUETOOTH_ERROR_NONE;
2543 return BLUETOOTH_ERROR_NOT_SUPPORT;
2549 return BLUETOOTH_ERROR_NONE;
2552 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2553 guint restricted_profile_flag, guint *restricted)
2555 int restrict_profile;
2556 *restricted = FALSE;
2559 case RESTRICTED_PROFILE_HFP_HS:
2560 restrict_profile = restricted_profile_flag & (1 << 0);
2562 case RESTRICTED_PROFILE_A2DP:
2563 restrict_profile = restricted_profile_flag & (1 << 2);
2566 return BLUETOOTH_ERROR_NOT_SUPPORT;
2569 if (restrict_profile)
2572 return BLUETOOTH_ERROR_NONE;
2575 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2578 case TRUSTED_PROFILE_PBAP:
2579 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2580 case TRUSTED_PROFILE_MAP:
2581 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2582 case TRUSTED_PROFILE_SAP:
2583 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2584 case TRUSTED_PROFILE_ALL:
2591 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2594 case RESTRICTED_PROFILE_HFP_HS:
2595 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2596 case RESTRICTED_PROFILE_A2DP:
2597 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2603 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2605 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0) {
2606 return TRUSTED_PROFILE_PBAP;
2607 } else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0) {
2608 return TRUSTED_PROFILE_MAP;
2609 } else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0) {
2610 return TRUSTED_PROFILE_SAP;
2612 return 0; /* 0 - Unknown Profile */
2615 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2616 bluetooth_trusted_profile_t profile, gboolean trust)
2618 int ret = BLUETOOTH_ERROR_NONE;
2619 GDBusConnection *conn;
2621 GError *error = NULL;
2622 char *device_path = NULL;
2624 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2627 BT_CHECK_PARAMETER(bd_addr, return);
2628 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2629 bd_addr->addr[0], bd_addr->addr[1],
2630 bd_addr->addr[2], bd_addr->addr[3],
2631 bd_addr->addr[4], bd_addr->addr[5],
2634 conn = _bt_get_system_gconn();
2635 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2637 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2639 device_path = _bt_get_device_object_path(address);
2640 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2642 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2643 NULL, BT_BLUEZ_NAME, device_path,
2644 BT_DEVICE_INTERFACE, NULL, NULL);
2646 g_free(device_path);
2647 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2649 uuid = _bt_get_trusted_profile_uuid(profile);
2651 g_object_unref(proxy);
2652 return BLUETOOTH_ERROR_NOT_SUPPORT;
2655 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2656 g_variant_new("(sb)", uuid, trust),
2657 G_DBUS_CALL_FLAGS_NONE, -1,
2659 g_object_unref(proxy);
2661 if (reply == NULL) {
2662 BT_ERR("Failed to Set Profile Trusted");
2663 ret = BLUETOOTH_ERROR_INTERNAL;
2665 BT_ERR("Error %s[%s]", error->message, address);
2666 g_error_free(error);
2670 g_variant_unref(reply);
2677 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2678 bluetooth_trusted_profile_t profile, guint *trust)
2680 int ret = BLUETOOTH_ERROR_NONE;
2681 GDBusConnection *conn;
2683 GError *error = NULL;
2684 char *device_path = NULL;
2685 guint trusted_profile_flag;
2686 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2689 BT_CHECK_PARAMETER(bd_addr, return);
2690 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2691 bd_addr->addr[0], bd_addr->addr[1],
2692 bd_addr->addr[2], bd_addr->addr[3],
2693 bd_addr->addr[4], bd_addr->addr[5],
2696 conn = _bt_get_system_gconn();
2697 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2699 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2701 device_path = _bt_get_device_object_path(address);
2702 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2704 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2705 NULL, BT_BLUEZ_NAME, device_path,
2706 BT_PROPERTIES_INTERFACE, NULL, NULL);
2708 g_free(device_path);
2709 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2711 reply = g_dbus_proxy_call_sync(proxy, "Get",
2712 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2713 G_DBUS_CALL_FLAGS_NONE, -1,
2715 g_object_unref(proxy);
2717 if (reply == NULL) {
2718 BT_ERR("Failed to Get Profile Trusted");
2719 ret = BLUETOOTH_ERROR_INTERNAL;
2721 BT_ERR("Error %s[%s]", error->message, address);
2722 g_error_free(error);
2727 g_variant_get(reply, "(v)", &temp);
2728 trusted_profile_flag = g_variant_get_uint32(temp);
2729 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2731 ret = _bt_get_trusted_profile_from_flag(profile,
2732 trusted_profile_flag, trust);
2733 g_variant_unref(temp);
2734 g_variant_unref(reply);
2737 BT_DBG("TRUST %d", *trust);
2741 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2742 bluetooth_restricted_profile_t profile, gboolean restricted)
2744 int ret = BLUETOOTH_ERROR_NONE;
2745 GDBusConnection *conn;
2747 GError *error = NULL;
2748 char *device_path = NULL;
2750 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2753 BT_CHECK_PARAMETER(bd_addr, return);
2754 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2755 bd_addr->addr[0], bd_addr->addr[1],
2756 bd_addr->addr[2], bd_addr->addr[3],
2757 bd_addr->addr[4], bd_addr->addr[5],
2758 profile, restricted);
2760 conn = _bt_get_system_gconn();
2761 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2763 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2765 device_path = _bt_get_device_object_path(address);
2766 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2768 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2769 NULL, BT_BLUEZ_NAME, device_path,
2770 BT_DEVICE_INTERFACE, NULL, NULL);
2772 g_free(device_path);
2773 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2775 uuid = _bt_get_restricted_profile_uuid(profile);
2777 g_object_unref(proxy);
2778 return BLUETOOTH_ERROR_NOT_SUPPORT;
2781 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2782 g_variant_new("(sb)", uuid, restricted),
2783 G_DBUS_CALL_FLAGS_NONE, -1,
2785 g_object_unref(proxy);
2787 if (reply == NULL) {
2788 BT_ERR("Failed to Set Profile Restricted");
2789 ret = BLUETOOTH_ERROR_INTERNAL;
2791 BT_ERR("Error %s[%s]", error->message, address);
2792 g_error_free(error);
2796 g_variant_unref(reply);
2803 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2804 bluetooth_restricted_profile_t profile, guint *restricted)
2806 int ret = BLUETOOTH_ERROR_NONE;
2807 GDBusConnection *conn;
2809 GError *error = NULL;
2810 char *device_path = NULL;
2811 guint restricted_profile_flag;
2812 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2815 BT_CHECK_PARAMETER(bd_addr, return);
2816 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2817 bd_addr->addr[0], bd_addr->addr[1],
2818 bd_addr->addr[2], bd_addr->addr[3],
2819 bd_addr->addr[4], bd_addr->addr[5],
2820 profile, *restricted);
2822 conn = _bt_get_system_gconn();
2823 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2825 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2827 device_path = _bt_get_device_object_path(address);
2828 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2830 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2831 NULL, BT_BLUEZ_NAME, device_path,
2832 BT_PROPERTIES_INTERFACE, NULL, NULL);
2834 g_free(device_path);
2835 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2837 reply = g_dbus_proxy_call_sync(proxy, "Get",
2838 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2839 G_DBUS_CALL_FLAGS_NONE, -1,
2841 g_object_unref(proxy);
2843 if (reply == NULL) {
2844 BT_ERR("Failed to Get Profile Restricted");
2845 ret = BLUETOOTH_ERROR_INTERNAL;
2847 BT_ERR("Error %s[%s]", error->message, address);
2848 g_error_free(error);
2853 g_variant_get(reply, "(v)", &temp);
2854 restricted_profile_flag = g_variant_get_uint32(temp);
2855 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2857 ret = _bt_get_restricted_profile_from_flag(profile,
2858 restricted_profile_flag, restricted);
2859 g_variant_unref(temp);
2860 g_variant_unref(reply);
2863 BT_DBG("TRUST %d", *restricted);
2867 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2871 GVariant *out_param1;
2872 request_info_t *req_info;
2873 GVariant *val = NULL;
2874 GVariant *param = NULL;
2877 bluetooth_device_address_t device_addr = { {0} };
2878 int result = BLUETOOTH_ERROR_NONE;
2881 val = g_dbus_proxy_call_finish(proxy, res, &err);
2883 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2884 if (req_info == NULL) {
2885 BT_ERR("req_info == NULL");
2886 g_object_unref(proxy);
2887 att_mtu_req_info->device_proxy = NULL;
2892 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2894 if (g_strrstr(err->message, "NotSupported")) {
2895 BT_INFO("Connection Not Supported");
2896 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2897 } else if (g_strrstr(err->message, "NotConnected")) {
2898 BT_INFO("Not connected");
2899 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2900 } else if (g_strrstr(err->message, "InvalidArguments")) {
2901 BT_INFO("Not connected");
2902 result = BLUETOOTH_ERROR_INVALID_PARAM;
2904 BT_DBG("Default case");
2905 result = BLUETOOTH_ERROR_INTERNAL;
2909 g_object_unref(proxy);
2910 att_mtu_req_info->device_proxy = NULL;
2912 if (result != BLUETOOTH_ERROR_NONE)
2916 g_variant_get (val, "(qy)", &mtu, &status);
2917 g_variant_unref (val);
2920 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
2922 param = g_variant_new("(isqy)",
2924 att_mtu_req_info->addr,
2928 /* Send the event to application */
2929 _bt_send_event(BT_DEVICE_EVENT,
2930 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2934 if (req_info->context == NULL)
2937 _bt_convert_addr_string_to_type(device_addr.addr,
2938 (const char *)att_mtu_req_info->addr);
2940 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
2941 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
2942 g_dbus_method_invocation_return_value(req_info->context,
2943 g_variant_new("(iv)", result, out_param1));
2945 _bt_delete_request_list(req_info->req_id);
2950 g_free(att_mtu_req_info->addr);
2951 g_free(att_mtu_req_info);
2952 att_mtu_req_info = NULL;
2957 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
2960 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2961 gchar *device_path = NULL;
2962 GDBusProxy *adapter_proxy;
2963 GDBusProxy *device_proxy;
2964 GDBusConnection *conn;
2965 int ret = BLUETOOTH_ERROR_NONE;
2967 BT_CHECK_PARAMETER(device_address, return);
2969 if (att_mtu_req_info) {
2970 BT_ERR("ATT MTU request in progress");
2971 return BLUETOOTH_ERROR_DEVICE_BUSY;
2974 conn = _bt_get_system_gconn();
2975 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2977 adapter_proxy = _bt_get_adapter_proxy();
2978 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2980 _bt_convert_addr_type_to_string(address, device_address->addr);
2982 BT_DBG("Remote device address: %s", address);
2984 device_path = _bt_get_device_object_path(address);
2986 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2988 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2989 NULL, BT_BLUEZ_NAME,
2990 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2991 g_free(device_path);
2992 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2994 if (device_path == NULL) {
2995 BT_DBG("device_path NULL");
2996 ret = BLUETOOTH_ERROR_INTERNAL;
3000 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3001 att_mtu_req_info->addr = (char *)g_strdup(address);
3002 att_mtu_req_info->req_id = request_id;
3003 att_mtu_req_info->device_proxy = device_proxy;
3005 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3006 g_variant_new("(q)", mtu),
3007 G_DBUS_CALL_FLAGS_NONE,
3008 BT_MAX_DBUS_TIMEOUT,
3010 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3016 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3019 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3020 char *object_path = NULL;
3022 GDBusProxy *device_proxy;
3023 GError *error = NULL;
3025 GVariant *tmp_value;
3026 GDBusConnection *conn;
3027 GVariant *result = NULL;
3028 int ret = BLUETOOTH_ERROR_NONE;
3030 BT_CHECK_PARAMETER(device_address, return);
3032 conn = _bt_get_system_gconn();
3033 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3035 _bt_convert_addr_type_to_string(address, device_address->addr);
3037 object_path = _bt_get_device_object_path(address);
3038 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3040 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3041 NULL, BT_BLUEZ_NAME, object_path,
3042 BT_PROPERTIES_INTERFACE, NULL, NULL);
3043 g_free(object_path);
3044 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3046 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3047 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3048 G_DBUS_CALL_FLAGS_NONE,
3052 if (result == NULL) {
3053 if (error != NULL) {
3054 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3055 g_error_free(error);
3057 g_object_unref(device_proxy);
3058 return BLUETOOTH_ERROR_INTERNAL;
3061 g_variant_get(result , "(@a{sv})", &value);
3062 g_variant_unref(result);
3064 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3065 if (tmp_value == NULL) {
3066 g_object_unref(device_proxy);
3067 g_variant_unref(value);
3068 return BLUETOOTH_ERROR_INTERNAL;
3071 *mtu = g_variant_get_uint16(tmp_value);
3073 BT_DBG("ATT MTU : %d", *mtu);
3075 g_variant_unref(tmp_value);
3076 g_variant_unref(value);
3077 g_object_unref(device_proxy);
3082 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3083 bluetooth_device_address_t *id_address)
3085 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3086 gchar *device_path = NULL;
3087 const gchar *idaddress = NULL;
3088 GDBusProxy *device_proxy;
3089 GError *error = NULL;
3090 GVariant *result = NULL;
3091 GDBusConnection *conn;
3092 int ret = BLUETOOTH_ERROR_NONE;
3094 BT_CHECK_PARAMETER(device_address, return);
3096 conn = _bt_get_system_gconn();
3097 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3099 _bt_convert_addr_type_to_string(address, device_address->addr);
3101 device_path = _bt_get_device_object_path(address);
3102 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3104 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3105 NULL, BT_BLUEZ_NAME,
3106 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3107 g_free(device_path);
3108 if (!device_proxy) {
3109 BT_ERR("Unable to get proxy");
3110 return BLUETOOTH_ERROR_INTERNAL;
3113 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3115 G_DBUS_CALL_FLAGS_NONE,
3120 if (result == NULL) {
3121 BT_ERR("Failed to get device ID address");
3122 if (error != NULL) {
3123 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3124 g_error_free(error);
3126 g_object_unref(device_proxy);
3127 return BLUETOOTH_ERROR_INTERNAL;
3130 g_variant_get(result , "(s)", &idaddress);
3131 g_variant_unref(result);
3133 if (idaddress == NULL) {
3134 BT_ERR("No paired device");
3135 g_object_unref(device_proxy);
3136 return BLUETOOTH_ERROR_NOT_PAIRED;
3139 BT_DBG("ID Address:%s", idaddress);
3142 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3144 ret = BLUETOOTH_ERROR_INTERNAL;
3147 g_object_unref(device_proxy);
3152 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3154 GapAgentPrivate *agent = _bt_get_adapter_agent();
3155 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3157 if (authentication_reply)
3158 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3160 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3162 BT_DBG("BT_PASSKEY_REPLY");
3163 return BLUETOOTH_ERROR_NONE;
3166 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3168 GapAgentPrivate *agent = _bt_get_adapter_agent();
3169 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3171 if (confirmation_reply)
3172 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3174 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3176 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3177 return BLUETOOTH_ERROR_NONE;