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.
23 #include <bundle_internal.h>
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-adapter-le.h"
31 #include "bt-service-event.h"
32 #include "bt-service-device.h"
33 #include "bt-service-rfcomm-client.h"
34 #include "bt-service-util.h"
35 #include "bt-service-agent.h"
36 #include "bt-service-network.h"
37 #include "bt-service-adapter.h"
38 #include "bt-service-gap-agent.h"
39 #include "bt-service-pbap.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 gboolean _bt_is_device_creating(void)
196 return is_device_creating;
199 gboolean _bt_is_bonding_device_address(const char *address)
201 if (bonding_info == NULL || bonding_info->addr == NULL)
204 if (g_strcmp0(bonding_info->addr, address) == 0) {
205 BT_DBG("[%s] is bonding device", address);
209 BT_DBG("[%s] is NOT bonding device", address);
213 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
215 ret_if(bonding_info == NULL);
216 bonding_info->is_autopair = is_autopair;
219 void __bt_cancel_search_service_done(void)
221 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
222 request_info_t *req_info;
223 bluetooth_device_info_t dev_info;
224 GVariant *out_param1;
226 ret_if(searching_info == NULL);
228 req_info = _bt_get_request_info(searching_info->req_id);
229 if (req_info == NULL) {
230 BT_ERR("req_info == NULL");
234 if (req_info->context == NULL)
237 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
238 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
239 searching_info->addr);
241 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
242 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
245 g_dbus_method_invocation_return_value(req_info->context,
246 g_variant_new("(iv)", result, out_param1));
248 _bt_delete_request_list(req_info->req_id);
252 g_free(searching_info->addr);
253 g_free(searching_info);
254 searching_info = NULL;
257 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
259 ret_if(value == NULL);
260 ret_if(info == NULL);
262 gsize uuid_count = 0;
264 info->uuids = g_variant_dup_strv(value, &uuid_count);
265 info->uuid_count = (unsigned int)uuid_count;
267 BT_DBG("uuid count : %d", uuid_count);
270 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
272 char *object_path = NULL;
273 bt_remote_dev_info_t *dev_info;
275 retv_if(address == NULL, NULL);
277 object_path = _bt_get_device_object_path(address);
278 retv_if(object_path == NULL, NULL);
280 dev_info = _bt_get_remote_device_info_by_object_path(object_path);
286 bt_remote_dev_info_t *_bt_get_remote_device_info_by_object_path(
287 const char *object_path)
289 bt_remote_dev_info_t *dev_info;
290 GDBusProxy *adapter_proxy;
291 GDBusProxy *device_proxy;
296 GDBusConnection *conn;
297 GError *error = NULL;
298 GVariant *result = NULL;
299 GVariantIter *value_iter;
303 adapter_proxy = _bt_get_adapter_proxy();
304 retv_if(adapter_proxy == NULL, NULL);
306 retv_if(object_path == NULL, NULL);
308 conn = _bt_gdbus_get_system_gconn();
310 BT_ERR("conn == NULL");
314 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
315 NULL, BT_BLUEZ_NAME, object_path,
316 BT_PROPERTIES_INTERFACE, NULL, NULL);
318 retv_if(device_proxy == NULL, NULL);
320 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
321 g_variant_new("(s)", BT_DEVICE_INTERFACE),
322 G_DBUS_CALL_FLAGS_NONE,
327 g_object_unref(device_proxy);
329 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
331 if (result != NULL) {
332 g_variant_get(result , "(@a{sv})", &value);
333 g_variant_unref(result);
335 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
337 g_variant_get(tmp_value, "s", &name);
338 g_variant_unref(tmp_value);
340 DBG_SECURE("Alias Name [%s]", name);
342 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
343 g_variant_get(tmp_value, "s", &name);
344 g_variant_unref(tmp_value);
347 tmp_value = g_variant_lookup_value(value, "IsAliasSet", G_VARIANT_TYPE_BOOLEAN);
349 dev_info->is_alias_set = g_variant_get_boolean(tmp_value);
350 g_variant_unref(tmp_value);
352 dev_info->is_alias_set = FALSE;
354 BT_DBG("IsAliasSet: [%s]", dev_info->is_alias_set ? "TRUE" : "FALSE");
356 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
358 dev_info->class = g_variant_get_uint32(tmp_value);
359 g_variant_unref(tmp_value);
363 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
365 dev_info->connected = g_variant_get_byte(tmp_value);
366 g_variant_unref(tmp_value);
368 dev_info->connected = BLUETOOTH_CONNECTED_LINK_NONE;
369 BT_DBG("connected link : %d", dev_info->connected);
371 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
373 dev_info->trust = g_variant_get_boolean(tmp_value);
374 g_variant_unref(tmp_value);
376 dev_info->trust = FALSE;
378 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
380 dev_info->paired = g_variant_get_boolean(tmp_value);
381 g_variant_unref(tmp_value);
383 dev_info->paired = FALSE;
385 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT16);
387 dev_info->rssi = g_variant_get_int16(tmp_value);
388 g_variant_unref(tmp_value);
392 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_BYTE);
394 dev_info->addr_type = g_variant_get_byte(tmp_value);
395 g_variant_unref(tmp_value);
397 dev_info->addr_type = 0;
399 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
401 __bt_get_uuids(tmp_value, dev_info);
402 g_variant_unref(tmp_value);
405 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT16);
407 dev_info->manufacturer_data_len = g_variant_get_uint16(tmp_value);
408 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
409 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
410 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
412 g_variant_unref(tmp_value);
414 dev_info->manufacturer_data_len = 0;
416 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_ARRAY);
418 if ((dev_info->manufacturer_data_len == 0) ||
419 dev_info->manufacturer_data_len != g_variant_get_size(tmp_value)) {
420 BT_ERR("manufacturer data length doesn't match");
421 dev_info->manufacturer_data_len = 0;
422 dev_info->manufacturer_data = NULL;
424 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
425 g_variant_get(tmp_value, "ay", &value_iter);
426 while (g_variant_iter_loop(value_iter, "y", &m_value))
427 dev_info->manufacturer_data[i++] = m_value;
429 g_variant_unref(tmp_value);
431 BT_INFO("manufacture data is not a G_VARIANT_TYPE_ARRAY ");
432 dev_info->manufacturer_data_len = 0;
433 dev_info->manufacturer_data = NULL;
436 tmp_value = g_variant_lookup_value(value, "Address", G_VARIANT_TYPE_STRING);
437 g_variant_get(tmp_value, "s", &address);
438 g_variant_unref(tmp_value);
440 dev_info->address = g_strdup(address);
441 dev_info->name = g_strdup(name);
443 g_variant_unref(value);
445 BT_ERR("result is NULL\n");
453 char *_bt_get_bonded_device_name(char *address)
455 bluetooth_device_address_t device_address = { {0} };
456 bluetooth_device_info_t dev_info;
458 retv_if(address == NULL, strdup(""));
460 _bt_convert_addr_string_to_type(device_address.addr, address);
462 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
464 _bt_get_bonded_device_info(&device_address, &dev_info);
466 return g_strdup(dev_info.device_name.name);
469 static gboolean __ignore_auto_pairing_request(const char *address)
474 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
485 /* Get the LAP(Lower Address part) */
486 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
488 snprintf(lap_address, sizeof(lap_address), ",%s", address);
490 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
493 BT_ERR("fopen failed \n");
497 fseek(fp, 0, SEEK_END);
502 BT_ERR("Get file size failed \n");
507 buffer = g_malloc0(sizeof(char) * size);
508 result = fread((char *)buffer, 1, size, fp);
510 if (result != size) {
511 BT_ERR("Read Error\n");
516 BT_DBG("Buffer = %s\n", buffer);
518 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
524 /* Write the data and insert new device data */
525 for (i = 0; lines[i] != NULL; i++) {
526 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
527 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
529 lines[i] = temp_buffer;
532 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
534 /* Fix : NULL_RETURNS */
535 retv_if(buffer == NULL, FALSE);
537 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
540 BT_ERR("fopen failed \n");
545 BT_DBG("Buffer = %s\n", buffer);
546 fwrite(buffer, 1, strlen(buffer), fp);
556 static int __bt_cancel_bonding(void)
558 BT_CHECK_PARAMETER(bonding_info, return);
559 BT_CHECK_PARAMETER(bonding_info->addr, return);
560 /* First Cancel the ongoing pairing in bluez otherwise if we send
561 * pair request bluez will send inprogress and we again retry bond so
562 * this cycle continues */
564 g_dbus_proxy_call_sync(bonding_info->device_proxy, "CancelPairing",
565 NULL, G_DBUS_CALL_FLAGS_NONE,
568 BT_ERR("Cancelling bonding request error msg (%s)", err->message);
570 return BLUETOOTH_ERROR_PARING_FAILED;
572 return BLUETOOTH_ERROR_NONE;
575 static int __bt_retry_bond(void)
577 BT_CHECK_PARAMETER(bonding_info, return);
578 BT_CHECK_PARAMETER(bonding_info->addr, return);
580 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
581 g_variant_new("(y)", bonding_info->conn_type),
582 G_DBUS_CALL_FLAGS_NONE,
585 (GAsyncReadyCallback)__bt_bond_device_cb,
588 return BLUETOOTH_ERROR_NONE;
592 static int __bt_remove_and_bond(void)
594 GDBusProxy *adapter_proxy;
595 GVariant *result = NULL;
597 char *device_path = NULL;
599 BT_CHECK_PARAMETER(bonding_info, return);
600 BT_CHECK_PARAMETER(bonding_info->addr, return);
602 adapter_proxy = _bt_get_adapter_proxy();
603 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
605 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
606 g_variant_new("(s)", bonding_info->addr),
607 G_DBUS_CALL_FLAGS_NONE,
612 return BLUETOOTH_ERROR_INTERNAL;
614 g_variant_get(result , "(o)", &device_path);
615 g_variant_unref(result);
617 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
619 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
620 g_variant_new("(o)", device_path),
621 G_DBUS_CALL_FLAGS_NONE,
627 BT_ERR("UnpairDevice Fail: %s", err->message);
629 return BLUETOOTH_ERROR_INTERNAL;
632 return __bt_retry_bond();
635 static int __bt_cancel_and_bond(void)
637 int ret = BLUETOOTH_ERROR_NONE;
639 ret = _bt_agent_reply_cancellation();
640 if (ret != BLUETOOTH_ERROR_NONE) {
641 BT_ERR("Fail to call reply cancellation");
644 return __bt_retry_bond();
648 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
651 int result = BLUETOOTH_ERROR_NONE;
654 GVariant *out_param1;
655 request_info_t *req_info;
656 bluetooth_device_info_t dev_info;
657 bt_remote_dev_info_t *remote_dev_info;
658 GVariant *manufacture_data;
661 /* Terminate ALL system popup */
662 if (headed_plugin_info->plugin_headed_enabled)
663 headed_plugin_info->headed_plugin->bt_destroy_popup_all();
665 reply = g_dbus_proxy_call_finish(proxy, res, &err);
667 g_variant_unref(reply);
669 is_device_creating = FALSE;
671 if (bonding_info == NULL) {
673 BT_ERR("bonding_info == NULL");
679 req_info = _bt_get_request_info(bonding_info->req_id);
680 if (req_info == NULL) {
681 BT_ERR("req_info == NULL");
686 g_dbus_error_strip_remote_error(err);
687 BT_ERR("Error occured in CreateBonding [%s]", err->message);
689 if (g_strrstr(err->message, "Already Exists")) {
690 BT_INFO("Existing Bond, remove and retry");
691 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
693 result = BLUETOOTH_ERROR_PARING_FAILED;
694 } else if (_bt_agent_is_canceled() ||
695 g_strrstr(err->message, "Authentication Canceled")) {
696 BT_INFO("Cancelled by USER");
697 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
698 } else if (g_strrstr(err->message, "Authentication Rejected")) {
700 result = BLUETOOTH_ERROR_ACCESS_DENIED;
701 } else if (g_strrstr(err->message, "In Progress")) {
702 BT_INFO("Bond in progress, cancel and retry");
703 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
705 result = BLUETOOTH_ERROR_PARING_FAILED;
706 } else if (g_strrstr(err->message, "Authentication Failed")) {
707 BT_INFO("Authentication Failed");
708 if (bonding_info->is_autopair == TRUE) {
709 _bt_set_autopair_status_in_bonding_info(FALSE);
710 __ignore_auto_pairing_request(bonding_info->addr);
712 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
713 } else if (g_strrstr(err->message, "Page Timeout")) {
714 BT_INFO("Page Timeout");
715 /* This is the special case
716 As soon as call bluetooth_bond_device, try to cancel bonding.
717 In this case, before completing to call 'CreatePairedDevice' method
718 the procedure is stopped. So 'Cancle' error is not return.
720 result = BLUETOOTH_ERROR_HOST_DOWN;
721 } else if (g_strrstr(err->message, BT_DBUS_TIMEOUT_MESSAGE)) {
722 BT_INFO("Cancel already running bonding");
723 if (__bt_cancel_bonding() != BLUETOOTH_ERROR_NONE) {
724 BT_INFO("Error while Cancelling bonding");
725 /* we need to unref proxy so continue */
727 result = BLUETOOTH_ERROR_INTERNAL;
728 } else if (g_strrstr(err->message, "Connection Timeout")) {
729 /* Pairing request timeout */
730 result = BLUETOOTH_ERROR_TIMEOUT;
731 } else if (g_strrstr(err->message, "Authentication Timeout")) {
732 /* Pairing request timeout */
733 result = BLUETOOTH_ERROR_TIMEOUT;
735 BT_DBG("Default case");
736 result = BLUETOOTH_ERROR_PARING_FAILED;
740 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
741 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
742 result == BLUETOOTH_ERROR_TIMEOUT ||
743 result == BLUETOOTH_ERROR_HOST_DOWN) {
744 if (TIZEN_PROFILE_WEARABLE) {
745 int is_sw_running = 0;
747 if (vconf_get_int(VCONFKEY_SETUP_WIZARD_STATE, &is_sw_running))
748 BT_ERR("vconf_get_int for setup wizard state failed");
750 if (!is_sw_running) {
751 if (headed_plugin_info->plugin_headed_enabled)
752 headed_plugin_info->headed_plugin->bt_launch_unable_to_pairing_syspopup(result);
754 BT_ERR("Unable to pair");
757 bonding_info->result = result;
760 g_object_unref(proxy);
761 bonding_info->device_proxy = NULL;
763 if (result != BLUETOOTH_ERROR_NONE) {
764 if (bonding_info->bonding_wo_discovery) {
765 GDBusProxy *adapter_proxy;
766 GVariant *ret = NULL;
767 GError *error = NULL;
770 BT_ERR("Bond was tried without discovery. Remove it");
772 adapter_proxy = _bt_get_adapter_proxy();
773 if (adapter_proxy == NULL) {
774 BT_ERR("Cannot get adapter_proxy");
778 device_path = _bt_get_device_object_path(bonding_info->addr);
779 if (device_path == NULL) {
780 BT_ERR("Cannot get device path");
784 ret = g_dbus_proxy_call_sync(adapter_proxy,
786 g_variant_new("(o)", device_path),
787 G_DBUS_CALL_FLAGS_NONE, -1, NULL,
790 BT_ERR("RemoveDevice Fail: %s", error->message);
791 g_clear_error(&error);
795 g_variant_unref(ret);
803 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
804 if (!remote_dev_info)
807 GVariant *uuids = NULL;
808 GVariantBuilder *builder = NULL;
810 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
811 for (i = 0; i < remote_dev_info->uuid_count; i++) {
812 g_variant_builder_add(builder, "s",
813 remote_dev_info->uuids[i]);
815 uuids = g_variant_new("as", builder);
816 g_variant_builder_unref(builder);
817 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
818 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
821 param = g_variant_new("(isunsbub@asn@ay)",
824 remote_dev_info->class,
825 remote_dev_info->rssi,
826 remote_dev_info->name,
827 remote_dev_info->paired,
828 remote_dev_info->connected,
829 remote_dev_info->trust,
831 remote_dev_info->manufacturer_data_len,
835 /* Send the event to application */
836 _bt_send_event(BT_ADAPTER_EVENT,
837 BLUETOOTH_EVENT_BONDING_FINISHED,
840 _bt_free_device_info(remote_dev_info);
843 if (req_info->context == NULL)
846 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
847 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
850 if (_bt_adapter_get_status() != BT_ACTIVATED)
851 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
853 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
854 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
856 g_dbus_method_invocation_return_value(req_info->context,
857 g_variant_new("(iv)", result, out_param1));
859 g_variant_unref(out_param1);
861 _bt_delete_request_list(req_info->req_id);
866 _bt_agent_set_canceled(FALSE);
869 g_free(bonding_info->addr);
870 g_free(bonding_info);
874 int _bt_bond_device(int request_id,
875 bluetooth_device_address_t *device_address,
876 unsigned short conn_type, GArray **out_param1)
879 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
880 bluetooth_device_info_t dev_info;
882 GDBusConnection *conn;
883 char *device_path = NULL;
884 GDBusProxy *adapter_proxy;
885 GError *error = NULL;
886 gboolean bonding_wo_discovery = FALSE;
888 BT_CHECK_PARAMETER(device_address, return);
891 BT_ERR("Bonding in progress");
893 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
894 memcpy(dev_info.device_address.addr, device_address->addr,
895 BLUETOOTH_ADDRESS_LENGTH);
897 g_array_append_vals(*out_param1, &dev_info,
898 sizeof(bluetooth_device_info_t));
900 return BLUETOOTH_ERROR_DEVICE_BUSY;
903 conn = _bt_gdbus_get_system_gconn();
904 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
906 _bt_convert_addr_type_to_string(address, device_address->addr);
908 device_path = _bt_get_device_object_path(address);
910 if (device_path == NULL) {
911 BT_ERR("No searched device");
912 GVariant *ret = NULL;
913 adapter_proxy = _bt_get_adapter_proxy();
914 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
916 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
917 g_variant_new("(s)", address),
918 G_DBUS_CALL_FLAGS_NONE,
924 BT_ERR("CreateDevice Fail: %s", error->message);
925 g_clear_error(&error);
928 g_variant_unref(ret);
929 device_path = _bt_get_device_object_path(address);
930 if (device_path == NULL) {
931 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
932 memcpy(dev_info.device_address.addr, device_address->addr,
933 BLUETOOTH_ADDRESS_LENGTH);
935 g_array_append_vals(*out_param1, &dev_info,
936 sizeof(bluetooth_device_info_t));
938 return BLUETOOTH_ERROR_NOT_PAIRED;
940 BT_INFO("device_path is created[%s]", device_path);
942 bonding_wo_discovery = TRUE;
945 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
947 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
950 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
952 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
953 bonding_info->addr = g_strdup(address);
954 bonding_info->req_id = request_id;
956 bonding_info->device_proxy = proxy;
957 bonding_info->conn_type = conn_type;
958 bonding_info->bonding_wo_discovery = bonding_wo_discovery;
960 is_device_creating = TRUE;
962 g_dbus_proxy_call(proxy, "Pair",
963 g_variant_new("(y)", conn_type),
964 G_DBUS_CALL_FLAGS_NONE,
967 (GAsyncReadyCallback)__bt_bond_device_cb,
970 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
972 return BLUETOOTH_ERROR_NONE;
974 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
975 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
978 g_array_append_vals(*out_param1, &dev_info,
979 sizeof(bluetooth_device_info_t));
981 is_device_creating = FALSE;
983 g_free(bonding_info->addr);
984 g_free(bonding_info);
987 return BLUETOOTH_ERROR_INTERNAL;*/
990 int _bt_cancel_bonding(void)
992 int ret = BLUETOOTH_ERROR_NONE;
994 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
996 ret = _bt_agent_reply_cancellation();
997 if (ret != BLUETOOTH_ERROR_NONE) {
998 BT_ERR("Fail to call reply cancellation");
1002 _bt_agent_set_canceled(TRUE);
1004 return BLUETOOTH_ERROR_NONE;
1007 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
1012 GVariant *out_param1;
1013 int result = BLUETOOTH_ERROR_NONE;
1014 bt_funcion_data_t *unbonding_info;
1015 bluetooth_device_info_t dev_info;
1016 request_info_t *req_info;
1018 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1020 g_variant_unref(reply);
1022 unbonding_info = user_data;
1024 if (unbonding_info == NULL) {
1026 BT_ERR("unbonding_info == NULL");
1030 req_info = _bt_get_request_info(unbonding_info->req_id);
1031 if (req_info == NULL) {
1032 BT_ERR("req_info == NULL");
1037 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
1038 result = BLUETOOTH_ERROR_INTERNAL;
1041 if (req_info->context == NULL)
1044 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1045 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1046 unbonding_info->addr);
1048 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1049 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1051 g_dbus_method_invocation_return_value(req_info->context,
1052 g_variant_new("(iv)", result, out_param1));
1055 _bt_delete_request_list(req_info->req_id);
1061 if (unbonding_info) {
1062 g_free(unbonding_info->addr);
1063 g_free(unbonding_info);
1067 int _bt_unbond_device(int request_id,
1068 bluetooth_device_address_t *device_address,
1069 GArray **out_param1)
1071 char *device_path = NULL;
1072 bt_funcion_data_t *unbonding_info;
1073 GDBusProxy *adapter_proxy = NULL;
1074 GDBusProxy *device_proxy = NULL;
1075 GDBusConnection *conn;
1076 int result = BLUETOOTH_ERROR_INTERNAL;
1077 bluetooth_device_info_t dev_info;
1078 GError *error = NULL;
1079 GVariant *ret = NULL;
1081 BT_CHECK_PARAMETER(device_address, return);
1083 adapter_proxy = _bt_get_adapter_proxy();
1084 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1086 /* allocate user data so that it can be retrieved in callback */
1087 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1088 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1089 unbonding_info->req_id = request_id;
1091 _bt_convert_addr_type_to_string(unbonding_info->addr,
1092 device_address->addr);
1094 device_path = _bt_get_device_object_path(unbonding_info->addr);
1096 if (device_path == NULL) {
1097 BT_ERR("No paired device");
1098 result = BLUETOOTH_ERROR_NOT_PAIRED;
1102 conn = _bt_gdbus_get_system_gconn();
1104 BT_ERR("conn is NULL");
1105 result = BLUETOOTH_ERROR_INTERNAL;
1109 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1110 NULL, BT_BLUEZ_NAME,
1111 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1113 if (device_proxy != NULL) {
1115 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1116 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1117 G_DBUS_CALL_FLAGS_NONE,
1122 BT_ERR("Getting property failed: [%s]\n", error->message);
1123 g_error_free(error);
1124 result = BLUETOOTH_ERROR_NOT_PAIRED;
1128 BT_ERR("No paired device");
1129 g_object_unref(device_proxy);
1130 result = BLUETOOTH_ERROR_NOT_PAIRED;
1133 g_variant_unref(ret);
1135 g_object_unref(device_proxy);
1138 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1139 g_variant_new("(o)", device_path),
1140 G_DBUS_CALL_FLAGS_NONE,
1141 BT_MAX_DBUS_TIMEOUT,
1143 (GAsyncReadyCallback)__bt_unbond_cb,
1146 g_free(device_path);
1147 return BLUETOOTH_ERROR_NONE;
1150 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1151 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1152 unbonding_info->addr);
1154 g_array_append_vals(*out_param1, &dev_info,
1155 sizeof(bluetooth_device_info_t));
1156 g_free(device_path);
1157 g_free(unbonding_info->addr);
1158 g_free(unbonding_info);
1162 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1167 GVariant *out_param1;
1168 int result = BLUETOOTH_ERROR_NONE;
1169 bluetooth_device_info_t dev_info;
1170 bt_remote_dev_info_t *remote_dev_info;
1171 request_info_t *req_info;
1172 GVariant *uuid_list, *manufacture_data;
1174 GVariantBuilder *builder = NULL;
1177 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1179 g_variant_unref(reply);
1181 g_object_unref(proxy);
1183 if (searching_info == NULL) {
1185 BT_ERR("unbonding_info == NULL");
1189 req_info = _bt_get_request_info(searching_info->req_id);
1190 if (req_info == NULL) {
1191 BT_ERR("req_info == NULL");
1196 g_dbus_error_strip_remote_error(err);
1197 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1199 if (g_strrstr("Operation canceled", err->message))
1200 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1201 else if (g_strrstr("In Progress", err->message))
1202 result = BLUETOOTH_ERROR_IN_PROGRESS;
1203 else if (g_strrstr("Host is down", err->message))
1204 result = BLUETOOTH_ERROR_HOST_DOWN;
1206 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1208 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1209 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1210 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1211 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1212 remote_dev_info->uuid_count > 0) {
1213 result = BLUETOOTH_ERROR_NONE;
1216 _bt_free_device_info(remote_dev_info);
1221 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1222 if (!remote_dev_info)
1226 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1227 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1228 g_variant_builder_add(builder, "s",
1229 remote_dev_info->uuids[i]);
1231 uuid_list = g_variant_new("as", builder);
1232 g_variant_builder_unref(builder);
1233 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1234 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1237 param = g_variant_new("(isunsbub@asn@ay)",
1239 searching_info->addr,
1240 remote_dev_info->class,
1241 remote_dev_info->rssi,
1242 remote_dev_info->name,
1243 remote_dev_info->paired,
1244 remote_dev_info->connected,
1245 remote_dev_info->trust,
1247 remote_dev_info->manufacturer_data_len,
1250 /* Send the event to application */
1251 _bt_send_event(BT_ADAPTER_EVENT,
1252 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1255 _bt_free_device_info(remote_dev_info);
1258 if (req_info->context == NULL)
1261 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1262 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1263 searching_info->addr);
1265 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1266 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1268 g_dbus_method_invocation_return_value(req_info->context,
1269 g_variant_new("(iv)", result, out_param1));
1272 _bt_delete_request_list(req_info->req_id);
1277 if (searching_info) {
1278 g_free(searching_info->addr);
1279 g_free(searching_info);
1280 searching_info = NULL;
1284 int _bt_search_device(int request_id,
1285 bluetooth_device_address_t *device_address)
1287 char *device_path = NULL;
1288 GDBusProxy *device_proxy = NULL;
1289 GDBusConnection *conn;
1291 GDBusProxy *adapter_proxy;
1292 int result = BLUETOOTH_ERROR_INTERNAL;
1294 BT_CHECK_PARAMETER(device_address, return);
1297 BT_ERR("Bonding in progress");
1298 return BLUETOOTH_ERROR_DEVICE_BUSY;
1301 if (searching_info) {
1302 BT_ERR("Service searching in progress");
1303 return BLUETOOTH_ERROR_DEVICE_BUSY;
1306 adapter_proxy = _bt_get_adapter_proxy();
1307 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1309 /* allocate user data so that it can be retrieved in callback */
1310 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1311 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1312 searching_info->req_id = request_id;
1314 _bt_convert_addr_type_to_string(searching_info->addr,
1315 device_address->addr);
1317 conn = _bt_gdbus_get_system_gconn();
1318 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1321 device_path = _bt_get_device_object_path(searching_info->addr);
1323 if (device_path == NULL) {
1324 BT_ERR("No paired device");
1325 result = BLUETOOTH_ERROR_NOT_PAIRED;
1329 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1330 NULL, BT_BLUEZ_NAME,
1331 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1332 g_free(device_path);
1333 if (device_proxy == NULL) {
1334 result = BLUETOOTH_ERROR_INTERNAL;
1338 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1339 g_variant_new("(s)", ""),
1340 G_DBUS_CALL_FLAGS_NONE,
1341 BT_MAX_DBUS_TIMEOUT,
1343 (GAsyncReadyCallback)__bt_discover_cb,
1346 searching_info->device_proxy = device_proxy;
1348 return BLUETOOTH_ERROR_NONE;
1351 g_free(searching_info->addr);
1352 g_free(searching_info);
1353 searching_info = NULL;
1357 int _bt_cancel_search_device(void)
1359 GVariant *ret = NULL;
1362 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1364 if (searching_info->device_proxy) {
1365 ret = g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1367 G_DBUS_CALL_FLAGS_NONE,
1372 g_variant_unref(ret);
1374 __bt_cancel_search_service_done();
1376 return BLUETOOTH_ERROR_NONE;
1379 int _bt_set_alias(bluetooth_device_address_t *device_address,
1382 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1383 gchar *device_path = NULL;
1384 GDBusProxy *adapter_proxy;
1385 GDBusProxy *device_proxy;
1386 GVariant *ret = NULL;
1387 GError *error = NULL;
1388 GDBusConnection *conn;
1390 BT_CHECK_PARAMETER(device_address, return);
1391 BT_CHECK_PARAMETER(alias, return);
1393 adapter_proxy = _bt_get_adapter_proxy();
1394 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1396 conn = _bt_gdbus_get_system_gconn();
1397 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1399 _bt_convert_addr_type_to_string(address, device_address->addr);
1401 device_path = _bt_get_device_object_path(address);
1403 if (device_path == NULL) {
1404 BT_ERR("No paired device");
1405 return BLUETOOTH_ERROR_NOT_PAIRED;
1408 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1409 NULL, BT_BLUEZ_NAME,
1410 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1412 g_free(device_path);
1413 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1415 ret = g_dbus_proxy_call_sync(device_proxy, "Set",
1416 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1417 G_DBUS_CALL_FLAGS_NONE,
1422 g_variant_unref(ret);
1424 g_object_unref(device_proxy);
1427 BT_ERR("SetProperty error: [%s]", error->message);
1428 g_error_free(error);
1429 return BLUETOOTH_ERROR_INTERNAL;
1432 return BLUETOOTH_ERROR_NONE;
1435 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1438 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1439 gchar *device_path = NULL;
1440 GDBusProxy *device_proxy;
1441 gboolean previous_value;
1442 GError *error = NULL;
1443 GDBusConnection *conn;
1444 GVariant *result = NULL;
1445 GVariant *temp = NULL;
1446 int ret = BLUETOOTH_ERROR_NONE;
1448 BT_CHECK_PARAMETER(device_address, return);
1450 conn = _bt_gdbus_get_system_gconn();
1451 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1453 _bt_convert_addr_type_to_string(address, device_address->addr);
1455 device_path = _bt_get_device_object_path(address);
1457 if (device_path == NULL) {
1458 BT_ERR("No paired device");
1459 return BLUETOOTH_ERROR_NOT_PAIRED;
1462 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1463 NULL, BT_BLUEZ_NAME,
1464 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1466 g_free(device_path);
1467 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1469 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1470 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted"),
1471 G_DBUS_CALL_FLAGS_NONE,
1475 if (error != NULL) {
1476 BT_ERR("Getting property failed: [%s]\n", error->message);
1477 g_error_free(error);
1478 g_object_unref(device_proxy);
1479 return BLUETOOTH_ERROR_INTERNAL;
1482 g_variant_get(result, "(v)", &temp);
1483 previous_value = g_variant_get_boolean(temp);
1484 g_variant_unref(temp);
1485 g_variant_unref(result);
1486 /* If the input is same with previous value, return error. */
1487 if (previous_value == authorize) {
1488 BT_ERR("Same value: %d", previous_value);
1489 g_object_unref(device_proxy);
1490 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1494 result = g_dbus_proxy_call_sync(device_proxy, "Set",
1495 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b", authorize)),
1496 G_DBUS_CALL_FLAGS_NONE,
1501 g_object_unref(device_proxy);
1503 BT_ERR("SetProperty error: [%s]", error->message);
1504 g_error_free(error);
1505 ret = BLUETOOTH_ERROR_INTERNAL;
1509 g_variant_unref(result);
1514 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1515 gboolean *is_connected)
1517 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1518 char *object_path = NULL;
1520 GDBusProxy *device_proxy;
1521 GError *error = NULL;
1523 GVariant *tmp_value;
1524 GDBusConnection *conn;
1525 GVariant *result = NULL;
1526 int ret = BLUETOOTH_ERROR_NONE;
1528 BT_CHECK_PARAMETER(device_address, return);
1530 conn = _bt_gdbus_get_system_gconn();
1531 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1533 _bt_convert_addr_type_to_string(address, device_address->addr);
1535 object_path = _bt_get_device_object_path(address);
1536 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1538 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1539 NULL, BT_BLUEZ_NAME, object_path,
1540 BT_PROPERTIES_INTERFACE, NULL, NULL);
1541 g_free(object_path);
1542 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1544 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1545 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1546 G_DBUS_CALL_FLAGS_NONE,
1550 if (result == NULL) {
1551 if (error != NULL) {
1552 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1553 g_error_free(error);
1555 g_object_unref(device_proxy);
1556 return BLUETOOTH_ERROR_INTERNAL;
1559 g_variant_get(result , "(@a{sv})", &value);
1560 g_variant_unref(result);
1562 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1563 if (tmp_value == NULL) {
1564 g_object_unref(device_proxy);
1565 g_variant_unref(value);
1566 return BLUETOOTH_ERROR_INTERNAL;
1569 *is_connected = g_variant_get_boolean(tmp_value);
1571 BT_DBG("gatt is connected : %d", *is_connected);
1573 g_variant_unref(tmp_value);
1574 g_variant_unref(value);
1575 g_object_unref(device_proxy);
1580 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1581 int connection_type, gboolean *is_connected)
1583 char *object_path = NULL;
1584 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1585 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1586 GDBusProxy *device_proxy = NULL;
1587 GDBusProxy *adapter_proxy = NULL;
1588 GDBusConnection *conn;
1589 GError *error = NULL;
1590 GVariant *tmp_value = NULL;
1591 GVariant *value = NULL;
1592 GVariant *result = NULL;
1595 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1596 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1598 _bt_convert_addr_type_to_string(address, device_address->addr);
1599 _bt_convert_addr_string_to_secure_string(secure_address, address);
1601 *is_connected = FALSE;
1602 BT_DBG("%s connection_type: 0x%02x", secure_address, connection_type);
1604 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1605 return _bt_rfcomm_is_device_connected(device_address,
1607 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1608 return _bt_is_gatt_connected(device_address, is_connected);
1609 else if (connection_type == BLUETOOTH_PBAP_SERVICE)
1610 return _bt_pbap_is_connected(device_address, is_connected);
1612 adapter_proxy = _bt_get_adapter_proxy();
1613 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1615 conn = _bt_gdbus_get_system_gconn();
1616 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1618 _bt_convert_addr_type_to_string(address, device_address->addr);
1620 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1621 object_path = _bt_get_adapter_path();
1622 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1623 NULL, BT_BLUEZ_NAME,
1624 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1625 g_free(object_path);
1626 if (device_proxy == NULL) {
1627 BT_DBG("Device don't have this service");
1628 return BLUETOOTH_ERROR_INTERNAL;
1631 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1632 g_variant_new("(s)", address),
1633 G_DBUS_CALL_FLAGS_NONE,
1638 if (result == NULL) {
1639 BT_ERR("[GetProperties] Error occured in Proxy call");
1641 BT_ERR("%s", error->message);
1642 g_error_free(error);
1644 *is_connected = FALSE;
1645 g_object_unref(device_proxy);
1646 return BLUETOOTH_ERROR_NONE;
1648 g_variant_get(result , "(@a{sv})", &value);
1649 g_variant_unref(result);
1652 tmp_value = g_variant_lookup_value(value,
1654 G_VARIANT_TYPE_BOOLEAN);
1656 *is_connected = g_variant_get_boolean(tmp_value);
1657 g_variant_unref(tmp_value);
1659 g_variant_unref(value);
1661 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1662 return _bt_is_network_connected(_bt_get_net_conn(),
1663 device_address->addr, is_connected);
1665 uuid = _bt_get_profile_uuid128(connection_type);
1667 BT_ERR("connection_type: %d, uuid is NULL", connection_type);
1668 return BLUETOOTH_ERROR_INTERNAL;
1670 BT_DBG("uuid %s [%s]", uuid, _bt_convert_uuid_to_string(uuid));
1672 object_path = _bt_get_device_object_path(address);
1674 BT_ERR("object_path is NULL");
1676 return BLUETOOTH_ERROR_NOT_PAIRED;
1679 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1680 NULL, BT_BLUEZ_NAME,
1681 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1682 g_free(object_path);
1683 if (device_proxy == NULL) {
1684 BT_DBG("Device don't have this service");
1686 return BLUETOOTH_ERROR_INTERNAL;
1689 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1690 g_variant_new("(s)", uuid),
1691 G_DBUS_CALL_FLAGS_NONE,
1696 if (result == NULL) {
1697 BT_ERR("[IsConnectedProfile] Error occured in Proxy call");
1699 BT_ERR("%s", error->message);
1700 if (g_strrstr(error->message, "Not Connected"))
1701 BT_DBG("Not connected");
1702 g_error_free(error);
1704 *is_connected = FALSE;
1705 g_object_unref(device_proxy);
1707 return BLUETOOTH_ERROR_NONE;
1709 g_variant_get(result, "(b)", is_connected);
1711 g_variant_unref(result);
1714 g_object_unref(device_proxy);
1715 return BLUETOOTH_ERROR_NONE;
1718 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1719 bluetooth_connected_link_t *connected)
1721 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1722 char *object_path = NULL;
1724 GDBusProxy *device_proxy;
1725 GError *error = NULL;
1726 GDBusConnection *conn;
1727 GVariant *tmp_value = NULL;
1728 GVariant *value = NULL;
1729 GVariant *result = NULL;
1731 BT_CHECK_PARAMETER(device_address, return);
1733 conn = _bt_gdbus_get_system_gconn();
1734 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1736 _bt_convert_addr_type_to_string(address, device_address->addr);
1738 object_path = _bt_get_device_object_path(address);
1739 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1741 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1742 NULL, BT_BLUEZ_NAME,
1743 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1744 g_free(object_path);
1745 if (device_proxy == NULL) {
1746 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1747 return BLUETOOTH_ERROR_NONE;
1750 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1751 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1752 G_DBUS_CALL_FLAGS_NONE,
1757 if (error != NULL) {
1758 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1759 g_error_free(error);
1760 g_object_unref(device_proxy);
1761 return BLUETOOTH_ERROR_INTERNAL;
1764 g_variant_get(result , "(@a{sv})", &value);
1765 g_variant_unref(result);
1767 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BYTE);
1768 if (tmp_value != NULL) {
1769 *connected = g_variant_get_byte(tmp_value);
1770 g_variant_unref(tmp_value);
1771 g_object_unref(device_proxy);
1772 return BLUETOOTH_ERROR_NONE;
1774 BT_ERR("g_variant value is NULL");
1775 return BLUETOOTH_ERROR_INTERNAL;
1779 static void __le_connection_req_cb(GDBusProxy *proxy, GAsyncResult *res,
1783 GVariant *out_param1;
1785 int result = BLUETOOTH_ERROR_NONE;
1786 bt_function_data_t *func_data = user_data;
1787 request_info_t *req_info = NULL;
1788 bluetooth_device_address_t device_addr = { {0} };
1790 reply = g_dbus_proxy_call_finish(proxy, res, &err);
1791 g_object_unref(proxy);
1793 if (reply == NULL) {
1794 BT_ERR("ConnectLE / DisconnectLE DBus call error");
1796 BT_ERR("Error: %s", err->message);
1797 g_clear_error(&err);
1799 result = BLUETOOTH_ERROR_INTERNAL;
1801 g_variant_unref(reply);
1804 if (func_data == NULL) {
1805 BT_ERR("func_data is NULL");
1809 req_info = _bt_get_request_info(func_data->req_id);
1810 if (req_info == NULL) {
1811 BT_ERR("req_info is NULL");
1815 if (req_info->context == NULL) {
1816 BT_ERR("req_info->context is NULL");
1820 _bt_convert_addr_string_to_type(device_addr.addr,
1821 (const char *)func_data->address);
1823 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1824 &device_addr, sizeof(bluetooth_device_address_t), TRUE,
1827 g_dbus_method_invocation_return_value(req_info->context,
1828 g_variant_new("(iv)", result, out_param1));
1832 _bt_delete_request_list(req_info->req_id);
1835 g_free(func_data->address);
1840 static int __bt_connect_le_device_internal(int req_id, const bluetooth_device_address_t *bd_addr,
1841 gboolean auto_connect)
1843 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1844 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1845 gchar *device_path = NULL;
1846 GDBusProxy *device_proxy = NULL;
1847 GDBusConnection *conn;
1848 int ret = BLUETOOTH_ERROR_NONE;
1849 bt_function_data_t *func_data;
1851 BT_CHECK_PARAMETER(bd_addr, return);
1853 conn = _bt_gdbus_get_system_gconn();
1854 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1856 _bt_convert_addr_type_to_string(device_address,
1857 (unsigned char *)bd_addr->addr);
1858 device_path = _bt_get_device_object_path(device_address);
1859 if (device_path == NULL) {
1860 BT_ERR_C("device_path NULL : [%s]", device_address);
1861 ret = BLUETOOTH_ERROR_INTERNAL;
1865 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1866 NULL, BT_BLUEZ_NAME,
1867 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1868 g_free(device_path);
1869 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1871 func_data = g_malloc0(sizeof(bt_function_data_t));
1872 func_data->address = g_strdup(device_address);
1873 if (func_data->address == NULL) {
1874 BT_ERR("Unable to allocate memory for address");
1875 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1879 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1880 BT_INFO("Connect LE [%s]", secure_address);
1882 func_data->req_id = req_id;
1884 g_dbus_proxy_call(device_proxy, "ConnectLE",
1885 g_variant_new("(b)", auto_connect),
1886 G_DBUS_CALL_FLAGS_NONE,
1887 BT_MAX_DBUS_TIMEOUT,
1889 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
1895 g_object_unref(device_proxy);
1897 g_free(func_data->address);
1903 static gboolean __bt_connect_le_timer_cb(gpointer user_data)
1905 BT_INFO("Try to initiate pending LE connection");
1907 pending_le_conn_timer_id = 0;
1909 __bt_connect_le_device_internal(pending_le_conn_info->req_id,
1910 &pending_le_conn_info->bd_addr,
1911 pending_le_conn_info->auto_connect);
1913 g_free(pending_le_conn_info);
1914 pending_le_conn_info = NULL;
1919 void _bt_pending_connect_le_device(void)
1921 if (pending_le_conn_timer_id > 0) {
1922 g_source_remove(pending_le_conn_timer_id);
1923 __bt_connect_le_timer_cb(NULL);
1927 int _bt_connect_le_device(int req_id, const bluetooth_device_address_t *bd_addr,
1928 gboolean auto_connect)
1930 int ret = BLUETOOTH_ERROR_NONE;
1932 BT_CHECK_PARAMETER(bd_addr, return);
1934 ret = _bt_hold_current_advertising();
1935 if (ret == BLUETOOTH_ERROR_NONE) {
1936 BT_INFO("Current advertising is held");
1937 pending_le_conn_info = g_malloc0(sizeof(bt_pending_le_conn_info_s));
1938 pending_le_conn_info->req_id = req_id;
1939 memcpy(pending_le_conn_info->bd_addr.addr, bd_addr->addr,
1940 BLUETOOTH_ADDRESS_LENGTH);
1941 pending_le_conn_info->auto_connect = auto_connect;
1943 pending_le_conn_timer_id =
1944 g_timeout_add(1000, __bt_connect_le_timer_cb, NULL);
1946 return BLUETOOTH_ERROR_NONE;
1949 BT_ERR("Unable to hold advertising");
1951 return __bt_connect_le_device_internal(req_id, bd_addr, auto_connect);
1954 int _bt_disconnect_le_device(int req_id,
1955 const bluetooth_device_address_t *bd_addr)
1957 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1958 char secure_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1960 GDBusProxy *device_proxy;
1961 GDBusConnection *conn;
1962 int ret = BLUETOOTH_ERROR_NONE;
1963 bt_function_data_t *func_data;
1965 BT_CHECK_PARAMETER(bd_addr, return);
1967 conn = _bt_gdbus_get_system_gconn();
1968 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1970 _bt_convert_addr_type_to_string(device_address,
1971 (unsigned char *)bd_addr->addr);
1972 device_path = _bt_get_device_object_path(device_address);
1973 if (device_path == NULL) {
1974 BT_DBG("device_path NULL");
1975 ret = BLUETOOTH_ERROR_INTERNAL;
1979 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1981 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1982 NULL, BT_BLUEZ_NAME,
1983 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1984 g_free(device_path);
1985 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1987 func_data = g_malloc0(sizeof(bt_function_data_t));
1988 func_data->address = g_strdup(device_address);
1989 if (func_data->address == NULL) {
1990 BT_ERR("Unable to allocate memory for address");
1991 ret = BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1995 _bt_convert_addr_string_to_secure_string(secure_address, device_address);
1996 BT_INFO("Disconnect LE [%s]", secure_address);
1998 func_data->req_id = req_id;
2000 g_dbus_proxy_call(device_proxy, "DisconnectLE",
2002 G_DBUS_CALL_FLAGS_NONE,
2003 BT_MAX_DBUS_TIMEOUT,
2005 (GAsyncReadyCallback)__le_connection_req_cb, func_data);
2010 g_object_unref(device_proxy);
2012 g_free(func_data->address);
2018 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2020 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2021 gchar *device_path = NULL;
2022 GError *error = NULL;
2023 GDBusProxy *device_proxy = NULL;
2024 GDBusProxy *adapter_proxy;
2025 GDBusConnection *conn;
2026 int ret = BLUETOOTH_ERROR_NONE;
2028 BT_CHECK_PARAMETER(bd_addr, return);
2030 _bt_convert_addr_type_to_string(device_address,
2031 (unsigned char *)bd_addr->addr);
2033 conn = _bt_gdbus_get_system_gconn();
2034 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2036 adapter_proxy = _bt_get_adapter_proxy();
2037 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2039 device_path = _bt_get_device_object_path(device_address);
2040 if (device_path == NULL) {
2041 BT_DBG("device_path NULL");
2042 ret = BLUETOOTH_ERROR_INTERNAL;
2046 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2048 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2049 NULL, BT_BLUEZ_NAME,
2050 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2051 g_free(device_path);
2052 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2054 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2056 G_DBUS_CALL_FLAGS_NONE,
2061 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2062 g_error_free(error);
2063 g_object_unref(device_proxy);
2064 return BLUETOOTH_ERROR_INTERNAL;
2066 g_object_unref(device_proxy);
2068 /* IPSP daemon launch */
2069 GDBusProxy *ipsp_proxy;
2071 ipsp_proxy = _bt_get_ipsp_proxy();
2072 retv_if(ipsp_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2074 g_dbus_proxy_call(ipsp_proxy, "EnableIpsp",
2075 NULL, G_DBUS_CALL_FLAGS_NONE,
2076 -1, NULL, NULL, NULL);
2081 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2083 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2084 gchar *device_path = NULL;
2085 GError *error = NULL;
2086 GDBusProxy *device_proxy = NULL;
2087 GDBusProxy *adapter_proxy;
2088 GDBusConnection *conn;
2089 int ret = BLUETOOTH_ERROR_NONE;
2091 BT_CHECK_PARAMETER(bd_addr, return);
2093 _bt_convert_addr_type_to_string(device_address,
2094 (unsigned char *)bd_addr->addr);
2096 conn = _bt_gdbus_get_system_gconn();
2097 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2099 adapter_proxy = _bt_get_adapter_proxy();
2100 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2102 device_path = _bt_get_device_object_path(device_address);
2103 if (device_path == NULL) {
2104 BT_DBG("device_path NULL");
2105 ret = BLUETOOTH_ERROR_INTERNAL;
2109 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2111 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2112 NULL, BT_BLUEZ_NAME,
2113 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2114 g_free(device_path);
2115 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2117 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2119 G_DBUS_CALL_FLAGS_NONE,
2124 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2125 g_error_free(error);
2126 g_object_unref(device_proxy);
2127 return BLUETOOTH_ERROR_INTERNAL;
2130 g_object_unref(device_proxy);
2135 int _bt_connect_profile(char *address, char *uuid,
2136 void *cb, gpointer func_data)
2140 GDBusConnection *conn;
2141 GDBusProxy *adapter_proxy;
2142 GVariant *result = NULL;
2143 GError *error = NULL;
2145 conn = _bt_gdbus_get_system_gconn();
2146 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2148 object_path = _bt_get_device_object_path(address);
2149 if (object_path == NULL) {
2150 BT_ERR("No searched device");
2152 adapter_proxy = _bt_get_adapter_proxy();
2153 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2155 result = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2156 g_variant_new("(s)", address),
2157 G_DBUS_CALL_FLAGS_NONE,
2162 if (error != NULL) {
2163 BT_ERR("CreateDevice Fail: %s", error->message);
2164 g_error_free(error);
2167 g_variant_unref(result);
2169 object_path = _bt_get_device_object_path(address);
2171 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2173 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2174 NULL, BT_BLUEZ_NAME,
2175 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2176 g_free(object_path);
2177 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2179 g_dbus_proxy_call(proxy, "ConnectProfile",
2180 g_variant_new("(s)", uuid),
2181 G_DBUS_CALL_FLAGS_NONE,
2182 BT_MAX_DBUS_TIMEOUT,
2184 (GAsyncReadyCallback)cb,
2187 return BLUETOOTH_ERROR_NONE;
2190 int _bt_disconnect_all(char *address)
2192 int ret = BLUETOOTH_ERROR_NONE;
2195 GDBusConnection *conn;
2196 GVariant *result = NULL;
2200 conn = _bt_gdbus_get_system_gconn();
2201 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2203 object_path = _bt_get_device_object_path(address);
2204 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2206 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2207 NULL, BT_BLUEZ_NAME,
2208 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2209 g_free(object_path);
2210 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2212 result = g_dbus_proxy_call_sync(proxy, "Disconnect",
2214 G_DBUS_CALL_FLAGS_NONE,
2219 BT_ERR("Dbus Call Error:[%s]", err->message);
2221 ret = BLUETOOTH_ERROR_INTERNAL;
2224 g_object_unref(proxy);
2226 g_variant_unref(result);
2231 int _bt_disconnect_profile(char *address, char *uuid,
2232 void *cb, gpointer func_data)
2236 GDBusConnection *conn;
2238 conn = _bt_gdbus_get_system_gconn();
2239 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2241 object_path = _bt_get_device_object_path(address);
2242 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2244 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2245 NULL, BT_BLUEZ_NAME,
2246 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2247 g_free(object_path);
2248 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2250 g_dbus_proxy_call(proxy, "DisconnectProfile",
2251 g_variant_new("(s)", uuid),
2252 G_DBUS_CALL_FLAGS_NONE,
2253 BT_MAX_DBUS_TIMEOUT,
2255 (GAsyncReadyCallback)cb,
2258 return BLUETOOTH_ERROR_NONE;
2261 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2262 int low_threshold, int in_range_threshold, int high_threshold)
2264 int ret = BLUETOOTH_ERROR_NONE;
2266 GVariant *result = NULL;
2267 GError *error = NULL;
2268 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2270 BT_CHECK_PARAMETER(bd_addr, return);
2271 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2272 bd_addr->addr[0], bd_addr->addr[1],
2273 bd_addr->addr[2], bd_addr->addr[3],
2274 bd_addr->addr[4], bd_addr->addr[5],
2276 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2277 in_range_threshold, high_threshold);
2279 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2281 proxy = _bt_get_adapter_proxy();
2282 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2284 result = g_dbus_proxy_call_sync(proxy, "EnableRssi",
2285 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2286 G_DBUS_CALL_FLAGS_NONE,
2290 if (error != NULL) {
2291 BT_ERR("Dbus Call Error:[%s]", error->message);
2292 g_error_free(error);
2293 ret = BLUETOOTH_ERROR_INTERNAL;
2297 g_variant_unref(result);
2302 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2305 int ret = BLUETOOTH_ERROR_NONE;
2307 GVariant *result = NULL;
2308 GError *error = NULL;
2309 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2311 BT_CHECK_PARAMETER(bd_addr, return);
2312 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2313 bd_addr->addr[0], bd_addr->addr[1],
2314 bd_addr->addr[2], bd_addr->addr[3],
2315 bd_addr->addr[4], bd_addr->addr[5],
2318 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2320 proxy = _bt_get_adapter_proxy();
2321 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2323 result = g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2324 g_variant_new("(si)", address, link_type),
2325 G_DBUS_CALL_FLAGS_NONE,
2330 if (error != NULL) {
2331 BT_ERR("Dbus Call Error:[%s]", error->message);
2332 g_error_free(error);
2333 ret = BLUETOOTH_ERROR_INTERNAL;
2337 g_variant_unref(result);
2342 int _bt_le_conn_update(unsigned char *device_address,
2343 float interval_min, float interval_max,
2344 guint16 latency, guint16 time_out)
2346 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2347 gchar *device_path = NULL;
2348 GError *error = NULL;
2349 GDBusProxy *device_proxy = NULL;
2350 GDBusConnection *conn;
2352 guint32 min, max, to;
2353 guint32 min_supervision_to;
2354 int ret = BLUETOOTH_ERROR_NONE;
2356 BT_CHECK_PARAMETER(device_address, return);
2358 BT_INFO("Min interval: %f, Max interval: %f, Latency: %u, Supervision timeout: %u",
2359 interval_min, interval_max, latency, time_out);
2361 if (interval_min > interval_max ||
2362 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2363 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2364 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2368 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2369 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2370 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2374 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2375 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2380 * The Supervision_Timeout in milliseconds shall be larger than
2381 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2382 * where Conn_Interval_Max is given in milliseconds.
2384 min_supervision_to = (1 + latency) * interval_max * 2;
2385 if (time_out <= min_supervision_to) {
2386 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2390 _bt_convert_addr_type_to_string(address, device_address);
2392 BT_DBG("Remote device address: %s", address);
2394 device_path = _bt_get_device_object_path(address);
2396 if (device_path == NULL) {
2397 BT_ERR("device_path NULL");
2398 ret = BLUETOOTH_ERROR_INTERNAL;
2402 conn = _bt_gdbus_get_system_gconn();
2404 BT_ERR("conn NULL");
2405 ret = BLUETOOTH_ERROR_INTERNAL;
2409 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2410 NULL, BT_BLUEZ_NAME,
2411 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2413 g_free(device_path);
2414 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2416 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2417 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2418 to = time_out / BT_LE_CONN_TO_SPLIT;
2420 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2421 g_variant_new("(uuuu)", min, max, latency, to),
2422 G_DBUS_CALL_FLAGS_NONE,
2427 g_object_unref(device_proxy);
2428 if (reply == NULL) {
2430 BT_ERR("Error %s[%s]", error->message, address);
2431 if (g_strrstr(error->message, "In Progress"))
2432 ret = BLUETOOTH_ERROR_IN_PROGRESS;
2434 ret = BLUETOOTH_ERROR_INTERNAL;
2435 g_error_free(error);
2439 g_variant_unref(reply);
2445 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2446 bluetooth_device_pin_code_t *pin_code)
2448 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2450 bt_pin_code_info_t *pin_info = NULL;
2452 BT_CHECK_PARAMETER(device_address, return);
2453 BT_CHECK_PARAMETER(pin_code, return);
2454 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2455 BLUETOOTH_ERROR_NO_RESOURCES);
2457 _bt_convert_addr_type_to_string(address, device_address->addr);
2459 for (l = pin_info_list; l != NULL; l = l->next) {
2462 if (g_strcmp0(pin_info->address, address) == 0) {
2463 g_free(pin_info->pin_code);
2464 pin_info->pin_code = g_strdup(pin_code->pin_code);
2465 return BLUETOOTH_ERROR_NONE;
2469 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2470 pin_info->address = g_strdup(address);
2471 pin_info->pin_code = g_strdup(pin_code->pin_code);
2472 pin_info_list = g_slist_append(pin_info_list, pin_info);
2474 return BLUETOOTH_ERROR_NONE;
2477 gint __bt_compare_address(gpointer *a, gpointer *b)
2479 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2480 char *address = (char *)b;
2481 return g_strcmp0(pin_info->address, address);
2484 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2486 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2488 bt_pin_code_info_t *pin_info = NULL;
2490 BT_CHECK_PARAMETER(device_address, return);
2492 _bt_convert_addr_type_to_string(address, device_address->addr);
2494 l = g_slist_find_custom(pin_info_list, address,
2495 (GCompareFunc)__bt_compare_address);
2499 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2500 g_free(pin_info->address);
2501 g_free(pin_info->pin_code);
2505 return BLUETOOTH_ERROR_NONE;
2508 int _bt_get_device_pin_code(const char *address, char *pin_code)
2512 BT_CHECK_PARAMETER(address, return);
2513 BT_CHECK_PARAMETER(pin_code, return);
2515 for (l = pin_info_list; l != NULL; l = l->next) {
2516 bt_pin_code_info_t *pin_info = l->data;
2518 if (g_strcmp0(pin_info->address, address) == 0) {
2519 g_strlcpy(pin_code, pin_info->pin_code,
2520 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2522 return BLUETOOTH_ERROR_NONE;
2526 return BLUETOOTH_ERROR_NOT_FOUND;
2529 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2530 bluetooth_le_connection_param_t *param)
2533 return BLUETOOTH_ERROR_INVALID_PARAM;
2535 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2536 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2537 return BLUETOOTH_ERROR_INVALID_PARAM;
2539 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2542 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2543 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2544 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2545 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2546 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2549 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2550 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2551 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2552 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2553 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2556 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2557 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2558 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2559 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2560 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2564 BT_ERR("Unhandled mode : %d", mode);
2568 return BLUETOOTH_ERROR_NONE;
2571 int _bt_get_trusted_profile_from_flag(bluetooth_trusted_profile_t profile,
2572 guint trusted_profile_flag, guint *trusted)
2578 case TRUSTED_PROFILE_PBAP:
2579 if (trusted_profile_flag & (PROFILE_SUPPORTED << 0))
2580 trust_profile = trusted_profile_flag & (1 << 1);
2582 return BLUETOOTH_ERROR_NOT_SUPPORT;
2584 case TRUSTED_PROFILE_MAP:
2585 if (trusted_profile_flag & (PROFILE_SUPPORTED << 2))
2586 trust_profile = trusted_profile_flag & (1 << 3);
2588 return BLUETOOTH_ERROR_NOT_SUPPORT;
2590 case TRUSTED_PROFILE_SAP:
2591 if (trusted_profile_flag & (PROFILE_SUPPORTED << 4))
2592 trust_profile = trusted_profile_flag & (1 << 5);
2594 return BLUETOOTH_ERROR_NOT_SUPPORT;
2596 case TRUSTED_PROFILE_ALL: /* Return Flag for All profiles*/
2597 *trusted = trusted_profile_flag;
2598 return BLUETOOTH_ERROR_NONE;
2600 return BLUETOOTH_ERROR_NOT_SUPPORT;
2606 return BLUETOOTH_ERROR_NONE;
2609 int _bt_get_restricted_profile_from_flag(bluetooth_restricted_profile_t profile,
2610 guint restricted_profile_flag, guint *restricted)
2612 int restrict_profile;
2613 *restricted = FALSE;
2616 case RESTRICTED_PROFILE_HFP_HS:
2617 restrict_profile = restricted_profile_flag & (1 << 0);
2619 case RESTRICTED_PROFILE_A2DP:
2620 restrict_profile = restricted_profile_flag & (1 << 2);
2623 return BLUETOOTH_ERROR_NOT_SUPPORT;
2626 if (restrict_profile)
2629 return BLUETOOTH_ERROR_NONE;
2632 char *_bt_get_trusted_profile_uuid(bluetooth_trusted_profile_t profile)
2635 case TRUSTED_PROFILE_PBAP:
2636 return g_strdup("00001130-0000-1000-8000-00805f9b34fb");
2637 case TRUSTED_PROFILE_MAP:
2638 return g_strdup("00001134-0000-1000-8000-00805f9b34fb");
2639 case TRUSTED_PROFILE_SAP:
2640 return g_strdup("0000112D-0000-1000-8000-00805f9b34fb");
2641 case TRUSTED_PROFILE_ALL:
2648 char *_bt_get_restricted_profile_uuid(bluetooth_restricted_profile_t profile)
2651 case RESTRICTED_PROFILE_HFP_HS:
2652 return g_strdup("0000111e-0000-1000-8000-00805f9b34fb");
2653 case RESTRICTED_PROFILE_A2DP:
2654 return g_strdup("0000110b-0000-1000-8000-00805f9b34fb");
2660 bluetooth_trusted_profile_t _bt_get_trusted_profile_enum(const char *uuid)
2662 if (g_strcmp0("0000112f-0000-1000-8000-00805f9b34fb", uuid) == 0)
2663 return TRUSTED_PROFILE_PBAP;
2664 else if (g_strcmp0("00001132-0000-1000-8000-00805f9b34fb", uuid) == 0)
2665 return TRUSTED_PROFILE_MAP;
2666 else if (g_strcmp0("0000112D-0000-1000-8000-00805f9b34fb", uuid) == 0)
2667 return TRUSTED_PROFILE_SAP;
2669 return 0; /* 0 - Unknown Profile */
2672 int _bt_set_trust_profile(bluetooth_device_address_t *bd_addr,
2673 bluetooth_trusted_profile_t profile, gboolean trust)
2675 int ret = BLUETOOTH_ERROR_NONE;
2676 GDBusConnection *conn;
2678 GError *error = NULL;
2679 char *device_path = NULL;
2681 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2684 BT_CHECK_PARAMETER(bd_addr, return);
2685 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2686 bd_addr->addr[0], bd_addr->addr[1],
2687 bd_addr->addr[2], bd_addr->addr[3],
2688 bd_addr->addr[4], bd_addr->addr[5],
2691 conn = _bt_gdbus_get_system_gconn();
2692 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2694 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2696 device_path = _bt_get_device_object_path(address);
2697 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2699 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2700 NULL, BT_BLUEZ_NAME, device_path,
2701 BT_DEVICE_INTERFACE, NULL, NULL);
2703 g_free(device_path);
2704 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2706 uuid = _bt_get_trusted_profile_uuid(profile);
2708 g_object_unref(proxy);
2709 return BLUETOOTH_ERROR_NOT_SUPPORT;
2712 reply = g_dbus_proxy_call_sync(proxy, "SetTrustedProfile",
2713 g_variant_new("(sb)", uuid, trust),
2714 G_DBUS_CALL_FLAGS_NONE, -1,
2716 g_object_unref(proxy);
2718 if (reply == NULL) {
2719 BT_ERR("Failed to Set Profile Trusted");
2720 ret = BLUETOOTH_ERROR_INTERNAL;
2722 BT_ERR("Error %s[%s]", error->message, address);
2723 g_error_free(error);
2727 g_variant_unref(reply);
2734 int _bt_get_trust_profile(bluetooth_device_address_t *bd_addr,
2735 bluetooth_trusted_profile_t profile, guint *trust)
2737 int ret = BLUETOOTH_ERROR_NONE;
2738 GDBusConnection *conn;
2740 GError *error = NULL;
2741 char *device_path = NULL;
2742 guint trusted_profile_flag;
2743 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2746 BT_CHECK_PARAMETER(bd_addr, return);
2747 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] trust[%d]",
2748 bd_addr->addr[0], bd_addr->addr[1],
2749 bd_addr->addr[2], bd_addr->addr[3],
2750 bd_addr->addr[4], bd_addr->addr[5],
2753 conn = _bt_gdbus_get_system_gconn();
2754 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2756 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2758 device_path = _bt_get_device_object_path(address);
2759 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2761 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2762 NULL, BT_BLUEZ_NAME, device_path,
2763 BT_PROPERTIES_INTERFACE, NULL, NULL);
2765 g_free(device_path);
2766 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2768 reply = g_dbus_proxy_call_sync(proxy, "Get",
2769 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "TrustedProfiles"),
2770 G_DBUS_CALL_FLAGS_NONE, -1,
2772 g_object_unref(proxy);
2774 if (reply == NULL) {
2775 BT_ERR("Failed to Get Profile Trusted");
2776 ret = BLUETOOTH_ERROR_INTERNAL;
2778 BT_ERR("Error %s[%s]", error->message, address);
2779 g_error_free(error);
2784 g_variant_get(reply, "(v)", &temp);
2785 trusted_profile_flag = g_variant_get_uint32(temp);
2786 BT_DBG("TRUST_FLAG %d", trusted_profile_flag);
2788 ret = _bt_get_trusted_profile_from_flag(profile,
2789 trusted_profile_flag, trust);
2790 g_variant_unref(temp);
2791 g_variant_unref(reply);
2794 BT_DBG("TRUST %d", *trust);
2798 int _bt_set_restrict_profile(bluetooth_device_address_t *bd_addr,
2799 bluetooth_restricted_profile_t profile, gboolean restricted)
2801 int ret = BLUETOOTH_ERROR_NONE;
2802 GDBusConnection *conn;
2804 GError *error = NULL;
2805 char *device_path = NULL;
2807 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2810 BT_CHECK_PARAMETER(bd_addr, return);
2811 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2812 bd_addr->addr[0], bd_addr->addr[1],
2813 bd_addr->addr[2], bd_addr->addr[3],
2814 bd_addr->addr[4], bd_addr->addr[5],
2815 profile, restricted);
2817 conn = _bt_gdbus_get_system_gconn();
2818 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2820 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2822 device_path = _bt_get_device_object_path(address);
2823 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2825 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2826 NULL, BT_BLUEZ_NAME, device_path,
2827 BT_DEVICE_INTERFACE, NULL, NULL);
2829 g_free(device_path);
2830 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2832 uuid = _bt_get_restricted_profile_uuid(profile);
2834 g_object_unref(proxy);
2835 return BLUETOOTH_ERROR_NOT_SUPPORT;
2838 reply = g_dbus_proxy_call_sync(proxy, "SetRestrictedProfile",
2839 g_variant_new("(sb)", uuid, restricted),
2840 G_DBUS_CALL_FLAGS_NONE, -1,
2842 g_object_unref(proxy);
2844 if (reply == NULL) {
2845 BT_ERR("Failed to Set Profile Restricted");
2846 ret = BLUETOOTH_ERROR_INTERNAL;
2848 BT_ERR("Error %s[%s]", error->message, address);
2849 g_error_free(error);
2853 g_variant_unref(reply);
2860 int _bt_get_restrict_profile(bluetooth_device_address_t *bd_addr,
2861 bluetooth_restricted_profile_t profile, guint *restricted)
2863 int ret = BLUETOOTH_ERROR_NONE;
2864 GDBusConnection *conn;
2866 GError *error = NULL;
2867 char *device_path = NULL;
2868 guint restricted_profile_flag;
2869 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2872 BT_CHECK_PARAMETER(bd_addr, return);
2873 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] profile[%d] restricted[%d]",
2874 bd_addr->addr[0], bd_addr->addr[1],
2875 bd_addr->addr[2], bd_addr->addr[3],
2876 bd_addr->addr[4], bd_addr->addr[5],
2877 profile, *restricted);
2879 conn = _bt_gdbus_get_system_gconn();
2880 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2882 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2884 device_path = _bt_get_device_object_path(address);
2885 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2887 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2888 NULL, BT_BLUEZ_NAME, device_path,
2889 BT_PROPERTIES_INTERFACE, NULL, NULL);
2891 g_free(device_path);
2892 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2894 reply = g_dbus_proxy_call_sync(proxy, "Get",
2895 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "RestrictedProfiles"),
2896 G_DBUS_CALL_FLAGS_NONE, -1,
2898 g_object_unref(proxy);
2900 if (reply == NULL) {
2901 BT_ERR("Failed to Get Profile Restricted");
2902 ret = BLUETOOTH_ERROR_INTERNAL;
2904 BT_ERR("Error %s[%s]", error->message, address);
2905 g_error_free(error);
2910 g_variant_get(reply, "(v)", &temp);
2911 restricted_profile_flag = g_variant_get_uint32(temp);
2912 BT_DBG("Restricted_FLAG %d", restricted_profile_flag);
2914 ret = _bt_get_restricted_profile_from_flag(profile,
2915 restricted_profile_flag, restricted);
2916 g_variant_unref(temp);
2917 g_variant_unref(reply);
2920 BT_DBG("TRUST %d", *restricted);
2924 static void __bt_request_att_mtu_device_cb(GDBusProxy *proxy, GAsyncResult *res,
2928 GVariant *out_param1;
2929 request_info_t *req_info;
2930 GVariant *val = NULL;
2931 GVariant *param = NULL;
2934 bluetooth_device_address_t device_addr = { {0} };
2935 int result = BLUETOOTH_ERROR_NONE;
2938 val = g_dbus_proxy_call_finish(proxy, res, &err);
2940 req_info = _bt_get_request_info(att_mtu_req_info->req_id);
2941 if (req_info == NULL) {
2942 BT_ERR("req_info == NULL");
2943 g_object_unref(proxy);
2944 att_mtu_req_info->device_proxy = NULL;
2949 BT_ERR("Error occured in RequestAttMtu [%s]", err->message);
2951 if (g_strrstr(err->message, "NotSupported")) {
2952 BT_INFO("Connection Not Supported");
2953 result = BLUETOOTH_ERROR_NOT_SUPPORT;
2954 } else if (g_strrstr(err->message, "NotConnected")) {
2955 BT_INFO("Not connected");
2956 result = BLUETOOTH_ERROR_NOT_CONNECTED;
2957 } else if (g_strrstr(err->message, "InvalidArguments")) {
2958 BT_INFO("Not connected");
2959 result = BLUETOOTH_ERROR_INVALID_PARAM;
2961 BT_DBG("Default case");
2962 result = BLUETOOTH_ERROR_INTERNAL;
2966 g_object_unref(proxy);
2967 att_mtu_req_info->device_proxy = NULL;
2969 if (result != BLUETOOTH_ERROR_NONE)
2973 g_variant_get(val, "(qy)", &mtu, &status);
2974 g_variant_unref(val);
2977 BT_DBG("MTU %d, Status %d, %s", mtu, status, att_mtu_req_info->addr);
2979 param = g_variant_new("(isqy)",
2981 att_mtu_req_info->addr,
2985 /* Send the event to application */
2986 _bt_send_event(BT_DEVICE_EVENT,
2987 BLUETOOTH_EVENT_GATT_ATT_MTU_CHANGED,
2991 if (req_info->context == NULL)
2994 _bt_convert_addr_string_to_type(device_addr.addr,
2995 (const char *)att_mtu_req_info->addr);
2997 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
2998 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
2999 g_dbus_method_invocation_return_value(req_info->context,
3000 g_variant_new("(iv)", result, out_param1));
3002 _bt_delete_request_list(req_info->req_id);
3007 g_free(att_mtu_req_info->addr);
3008 g_free(att_mtu_req_info);
3009 att_mtu_req_info = NULL;
3014 int _bt_request_att_mtu(int request_id, bluetooth_device_address_t *device_address,
3017 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3018 gchar *device_path = NULL;
3019 GDBusProxy *adapter_proxy;
3020 GDBusProxy *device_proxy;
3021 GDBusConnection *conn;
3022 int ret = BLUETOOTH_ERROR_NONE;
3024 BT_CHECK_PARAMETER(device_address, return);
3026 if (att_mtu_req_info) {
3027 BT_ERR("ATT MTU request in progress");
3028 return BLUETOOTH_ERROR_DEVICE_BUSY;
3031 conn = _bt_gdbus_get_system_gconn();
3032 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3034 adapter_proxy = _bt_get_adapter_proxy();
3035 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3037 _bt_convert_addr_type_to_string(address, device_address->addr);
3039 BT_DBG("Remote device address: %s", address);
3041 device_path = _bt_get_device_object_path(address);
3043 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
3045 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3046 NULL, BT_BLUEZ_NAME,
3047 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3048 g_free(device_path);
3049 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3051 att_mtu_req_info = g_malloc0(sizeof(bt_funcion_data_t));
3052 att_mtu_req_info->addr = (char *)g_strdup(address);
3053 att_mtu_req_info->req_id = request_id;
3054 att_mtu_req_info->device_proxy = device_proxy;
3056 g_dbus_proxy_call(device_proxy, "RequestAttMtu",
3057 g_variant_new("(q)", mtu),
3058 G_DBUS_CALL_FLAGS_NONE,
3059 BT_MAX_DBUS_TIMEOUT,
3061 (GAsyncReadyCallback)__bt_request_att_mtu_device_cb,
3067 int _bt_get_att_mtu(bluetooth_device_address_t *device_address,
3070 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3071 char *object_path = NULL;
3073 GDBusProxy *device_proxy;
3074 GError *error = NULL;
3076 GVariant *tmp_value;
3077 GDBusConnection *conn;
3078 GVariant *result = NULL;
3079 int ret = BLUETOOTH_ERROR_NONE;
3081 BT_CHECK_PARAMETER(device_address, return);
3083 conn = _bt_gdbus_get_system_gconn();
3084 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3086 _bt_convert_addr_type_to_string(address, device_address->addr);
3088 object_path = _bt_get_device_object_path(address);
3089 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3091 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3092 NULL, BT_BLUEZ_NAME, object_path,
3093 BT_PROPERTIES_INTERFACE, NULL, NULL);
3094 g_free(object_path);
3095 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
3097 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
3098 g_variant_new("(s)", BT_DEVICE_INTERFACE),
3099 G_DBUS_CALL_FLAGS_NONE,
3103 if (result == NULL) {
3104 if (error != NULL) {
3105 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3106 g_error_free(error);
3108 g_object_unref(device_proxy);
3109 return BLUETOOTH_ERROR_INTERNAL;
3112 g_variant_get(result , "(@a{sv})", &value);
3113 g_variant_unref(result);
3115 tmp_value = g_variant_lookup_value(value, "AttMtu", G_VARIANT_TYPE_UINT16);
3116 if (tmp_value == NULL) {
3117 g_object_unref(device_proxy);
3118 g_variant_unref(value);
3119 return BLUETOOTH_ERROR_INTERNAL;
3122 *mtu = g_variant_get_uint16(tmp_value);
3124 BT_DBG("ATT MTU : %d", *mtu);
3126 g_variant_unref(tmp_value);
3127 g_variant_unref(value);
3128 g_object_unref(device_proxy);
3133 int _bt_get_device_ida(bluetooth_device_address_t *device_address,
3134 bluetooth_device_address_t *id_address)
3136 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
3137 gchar *device_path = NULL;
3138 const gchar *idaddress = NULL;
3139 GDBusProxy *device_proxy;
3140 GError *error = NULL;
3141 GVariant *result = NULL;
3142 GDBusConnection *conn;
3143 int ret = BLUETOOTH_ERROR_NONE;
3145 BT_CHECK_PARAMETER(device_address, return);
3147 conn = _bt_gdbus_get_system_gconn();
3148 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
3150 _bt_convert_addr_type_to_string(address, device_address->addr);
3152 device_path = _bt_get_device_object_path(address);
3153 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
3155 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
3156 NULL, BT_BLUEZ_NAME,
3157 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
3158 g_free(device_path);
3159 if (!device_proxy) {
3160 BT_ERR("Unable to get proxy");
3161 return BLUETOOTH_ERROR_INTERNAL;
3164 result = g_dbus_proxy_call_sync(device_proxy, "GetIDAddress",
3166 G_DBUS_CALL_FLAGS_NONE,
3171 if (result == NULL) {
3172 BT_ERR("Failed to get device ID address");
3173 if (error != NULL) {
3174 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
3175 g_error_free(error);
3177 g_object_unref(device_proxy);
3178 return BLUETOOTH_ERROR_INTERNAL;
3181 g_variant_get(result , "(s)", &idaddress);
3182 g_variant_unref(result);
3184 if (idaddress == NULL) {
3185 BT_ERR("No paired device");
3186 g_object_unref(device_proxy);
3187 return BLUETOOTH_ERROR_NOT_PAIRED;
3190 BT_DBG("ID Address:%s", idaddress);
3193 _bt_convert_addr_string_to_type(id_address->addr, idaddress);
3195 ret = BLUETOOTH_ERROR_INTERNAL;
3197 g_object_unref(device_proxy);
3202 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
3204 GapAgentPrivate *agent = _bt_get_adapter_agent();
3205 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3207 if (authentication_reply)
3208 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
3210 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
3212 BT_DBG("BT_PASSKEY_REPLY");
3213 return BLUETOOTH_ERROR_NONE;
3216 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
3218 GapAgentPrivate *agent = _bt_get_adapter_agent();
3219 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
3221 if (confirmation_reply)
3222 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
3224 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
3226 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
3227 return BLUETOOTH_ERROR_NONE;