4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
28 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
29 #include <syspopup_caller.h>
32 #include "bluetooth-api.h"
33 #include "bt-internal-types.h"
35 #include "bt-service-common.h"
36 #include "bt-service-event.h"
37 #include "bt-service-device.h"
38 #include "bt-service-rfcomm-client.h"
39 #include "bt-service-util.h"
40 #include "bt-service-agent.h"
41 #include "bt-service-network.h"
42 #include "bt-service-adapter.h"
44 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
45 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
46 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
48 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
49 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
50 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
51 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
52 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
53 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
54 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
61 GDBusProxy *device_proxy;
62 GDBusProxy *adapter_proxy;
64 unsigned short conn_type;
67 gboolean is_device_creating;
68 bt_funcion_data_t *bonding_info;
69 bt_funcion_data_t *searching_info;
71 /* This HID Mouse does not support pairing precedure. need to skip it. */
72 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
74 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
77 static int __bt_retry_bond(void);
80 /*static void __bt_decline_pair_request()
84 request_info_t *req_info;
85 bluetooth_device_info_t dev_info;
86 bt_remote_dev_info_t *remote_dev_info;
87 GVariant *uuid_list, *manufacture_data;
92 req_info = _bt_get_request_info(bonding_info->req_id);
93 if (req_info == NULL) {
94 BT_ERR("req_info == NULL");
97 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
99 BT_DBG("bonding_info is NULL");
104 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
105 remote_dev_info->uuids, remote_dev_info->uuid_count,
108 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
109 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
112 param = g_variant_new("isunsbuba{s}na{y})",
113 bonding_info->result,
115 remote_dev_info->class,
116 remote_dev_info->rssi,
117 remote_dev_info->name,
118 remote_dev_info->paired,
119 remote_dev_info->connected,
120 remote_dev_info->trust,
122 remote_dev_info->manufacturer_data_len,
126 //Send the event to application
127 if (remote_dev_info != NULL) {
128 _bt_send_event(BT_ADAPTER_EVENT,
129 BLUETOOTH_EVENT_BONDING_FINISHED,
132 _bt_free_device_info(remote_dev_info);
135 if (req_info->context == NULL)
138 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
139 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
142 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
143 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
145 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
146 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
149 g_dbus_method_invocation_return_value(req_info->context,
150 g_variant_new("@ayi", out_param1, out_param2));
153 _bt_delete_request_list(req_info->req_id);
156 g_free(bonding_info->addr);
157 g_free(bonding_info);
163 #ifdef TIZEN_WEARABLE
164 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
168 retv_if(user_data == NULL, FALSE);
170 b = (bundle *)user_data;
172 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
173 ret = syspopup_launch("bt-syspopup", b);
176 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
178 BT_DBG("Hurray!!! Finally Popup launched");
181 return (ret < 0) ? TRUE : FALSE;
184 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
189 GDBusConnection *conn;
191 conn = _bt_get_system_gconn();
199 bundle_add(b, "event-type", "unable-to-pairing");
201 if (result == BLUETOOTH_ERROR_TIMEOUT )
202 bundle_add(b, "error", "timeout");
203 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
204 bundle_add(b, "error", "authfailed");
206 bundle_add(b, "error", "error");
208 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
209 ret = syspopup_launch("bt-syspopup", b);
212 BT_ERR("Popup launch failed...retry %d \n", ret);
213 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
224 gboolean _bt_is_device_creating(void)
226 return is_device_creating;
229 gboolean _bt_is_bonding_device_address(const char *address)
231 if (bonding_info == NULL || bonding_info->addr == NULL)
234 if (g_strcmp0(bonding_info->addr, address) == 0) {
235 BT_DBG("[%s] is bonding device", address);
239 BT_DBG("[%s] is NOT bonding device", address);
243 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
245 ret_if(bonding_info == NULL);
246 bonding_info->is_autopair = is_autopair;
249 void _bt_device_path_to_address(const char *device_path,
250 char *device_address)
252 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
256 ret_if(device_path == NULL);
257 ret_if(device_address == NULL);
259 dev_addr = strstr(device_path, "dev_");
260 ret_if(dev_addr == NULL);
263 g_strlcpy(address, dev_addr, sizeof(address));
265 while ((pos = strchr(address, '_')) != NULL) {
269 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
272 void __bt_cancel_search_service_done(void)
274 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
275 request_info_t *req_info;
276 bluetooth_device_info_t dev_info;
277 GVariant *out_param1;
279 ret_if(searching_info == NULL);
281 req_info = _bt_get_request_info(searching_info->req_id);
282 if (req_info == NULL) {
283 BT_ERR("req_info == NULL");
287 if (req_info->context == NULL)
290 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
291 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
292 searching_info->addr);
294 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
295 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
298 g_dbus_method_invocation_return_value(req_info->context,
299 g_variant_new("(iv)", result, out_param1));
301 _bt_delete_request_list(req_info->req_id);
305 g_free(searching_info->addr);
306 g_free(searching_info);
307 searching_info = NULL;
310 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
312 ret_if(value == NULL);
313 ret_if(info == NULL);
315 info->uuid_count = g_variant_get_size(value);
316 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
319 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
321 bt_remote_dev_info_t *dev_info;
322 char *object_path = NULL;
323 GDBusProxy *adapter_proxy;
324 GDBusProxy *device_proxy;
328 gchar *manufacturer_data = NULL;
329 GDBusConnection *conn;
330 GError *error = NULL;
331 GVariant *result = NULL;
333 retv_if(address == NULL, NULL);
335 adapter_proxy = _bt_get_adapter_proxy();
336 retv_if(adapter_proxy == NULL, NULL);
339 object_path = _bt_get_device_object_path(address);
341 retv_if(object_path == NULL, NULL);
343 conn = _bt_get_system_gconn();
345 BT_ERR("conn == NULL");
350 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
352 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
355 retv_if(device_proxy == NULL, NULL);
357 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
358 g_variant_new("(s)", BT_DEVICE_INTERFACE),
359 G_DBUS_CALL_FLAGS_NONE,
364 g_object_unref(device_proxy);
366 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
368 if (result != NULL) {
369 g_variant_get(result , "(@a{sv})", &value);
370 g_variant_unref(result);
372 tmp_value = g_variant_lookup_value (value, "Alias", G_VARIANT_TYPE_STRING);
374 g_variant_get(tmp_value, "s", &name);
375 g_variant_unref(tmp_value);
377 DBG_SECURE("Alias Name [%s]", name);
379 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
380 g_variant_get(tmp_value, "s", &name);
381 g_variant_unref(tmp_value);
384 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
385 dev_info->class = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
386 g_variant_unref(tmp_value);
388 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BOOLEAN);
389 dev_info->connected = tmp_value ? g_variant_get_boolean(tmp_value) : BLUETOOTH_CONNECTED_LINK_NONE;
390 g_variant_unref(tmp_value);
392 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
393 dev_info->trust = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
394 g_variant_unref(tmp_value);
396 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
397 dev_info->paired = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
398 g_variant_unref(tmp_value);
400 BT_DBG("Paired %d", dev_info->paired );
402 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT32);
403 dev_info->rssi = tmp_value ? g_variant_get_int32(tmp_value) : 0;
404 g_variant_unref(tmp_value);
406 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_UINT32);
407 dev_info->addr_type = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
408 g_variant_unref(tmp_value);
410 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
411 __bt_get_uuids(tmp_value, dev_info);
412 g_variant_unref(tmp_value);
414 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT32);
415 dev_info->manufacturer_data_len = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
416 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
417 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
418 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
420 g_variant_unref(tmp_value);
422 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_BYTESTRING);
423 manufacturer_data = value ? (gchar *)g_variant_get_bytestring(tmp_value) : NULL;
424 if (manufacturer_data) {
425 if (dev_info->manufacturer_data_len > 0) {
426 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
427 memcpy(dev_info->manufacturer_data, manufacturer_data, dev_info->manufacturer_data_len);
430 g_variant_unref(tmp_value);
432 dev_info->address = g_strdup(address);
433 dev_info->name = g_strdup(name);
435 g_variant_unref(value);
437 BT_ERR("result is NULL\n");
445 char *_bt_get_bonded_device_name(char *address)
447 bluetooth_device_address_t device_address = { {0} };
448 bluetooth_device_info_t dev_info;
450 retv_if(address == NULL, strdup(""));
452 _bt_convert_addr_string_to_type(device_address.addr, address);
454 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
456 _bt_get_bonded_device_info(&device_address, &dev_info);
458 return g_strdup(dev_info.device_name.name);
461 static gboolean __ignore_auto_pairing_request(const char *address)
466 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
477 /* Get the LAP(Lower Address part) */
478 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
480 snprintf(lap_address, sizeof(lap_address), ",%s", address);
482 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
485 BT_ERR("fopen failed \n");
489 fseek(fp, 0, SEEK_END);
494 BT_ERR("Get file size failed \n");
499 buffer = g_malloc0(sizeof(char) * size);
500 /* Fix : NULL_RETURNS */
501 if (buffer == NULL) {
502 BT_ERR("Memory allocation error\n");
506 result = fread((char *)buffer, 1, size, fp);
508 if (result != size) {
509 BT_ERR("Read Error\n");
514 BT_DBG("Buffer = %s\n", buffer);
516 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
522 /* Write the data and insert new device data */
523 for (i = 0; lines[i] != NULL; i++) {
524 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
525 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
527 lines[i] = temp_buffer;
530 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
532 /* Fix : NULL_RETURNS */
533 retv_if(buffer == NULL, FALSE);
535 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
538 BT_ERR("fopen failed \n");
543 BT_DBG("Buffer = %s\n", buffer);
544 fwrite(buffer, 1, strlen(buffer), fp);
554 static int __bt_retry_bond(void)
556 BT_CHECK_PARAMETER(bonding_info, return);
557 BT_CHECK_PARAMETER(bonding_info->addr, return);
559 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
560 g_variant_new("(y)", bonding_info->conn_type),
561 G_DBUS_CALL_FLAGS_NONE,
564 (GAsyncReadyCallback)__bt_bond_device_cb,
567 return BLUETOOTH_ERROR_NONE;
571 static int __bt_remove_and_bond(void)
573 GDBusProxy *adapter_proxy;
574 GVariant *result = NULL;
576 char *device_path = NULL;
578 BT_CHECK_PARAMETER(bonding_info, return);
579 BT_CHECK_PARAMETER(bonding_info->addr, return);
581 adapter_proxy = _bt_get_adapter_proxy();
582 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
584 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
585 g_variant_new("(s)", bonding_info->addr),
586 G_DBUS_CALL_FLAGS_NONE,
591 return BLUETOOTH_ERROR_INTERNAL;
593 g_variant_get(result , "(&o)", &device_path);
594 g_variant_unref(result);
596 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
598 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
599 g_variant_new("(o)", device_path),
600 G_DBUS_CALL_FLAGS_NONE,
606 BT_ERR("UnpairDevice Fail: %s", err->message);
608 return BLUETOOTH_ERROR_INTERNAL;
611 return __bt_retry_bond();
614 static int __bt_cancel_and_bond(void)
616 int ret = BLUETOOTH_ERROR_NONE;
618 ret = _bt_agent_reply_cancellation();
619 if (ret != BLUETOOTH_ERROR_NONE){
620 BT_ERR("Fail to call reply cancellation");
624 return __bt_retry_bond();
628 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
631 int result = BLUETOOTH_ERROR_NONE;
633 GVariant *out_param1;
634 request_info_t *req_info;
635 bluetooth_device_info_t dev_info;
636 bt_remote_dev_info_t *remote_dev_info = NULL;
637 GVariant *manufacture_data;
640 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
641 /* Terminate ALL system popup */
642 syspopup_destroy_all();
645 g_dbus_proxy_call_finish(proxy, res, &err);
647 is_device_creating = FALSE;
649 if (bonding_info == NULL) {
651 BT_ERR("bonding_info == NULL");
657 req_info = _bt_get_request_info(bonding_info->req_id);
658 if (req_info == NULL) {
659 BT_ERR("req_info == NULL");
664 BT_ERR("Error occured in CreateBonding [%s]", err->message);
666 if (!strcmp(err->message, "Already Exists")) {
667 BT_INFO("Existing Bond, remove and retry");
668 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
670 result = BLUETOOTH_ERROR_PARING_FAILED;
671 } else if (_bt_agent_is_canceled() ||
672 !strcmp(err->message, "Authentication Canceled")) {
673 BT_INFO("Cancelled by USER");
674 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
675 } else if (!strcmp(err->message, "Authentication Rejected")) {
677 result = BLUETOOTH_ERROR_ACCESS_DENIED;
678 } else if (!strcmp(err->message, "In Progress")) {
679 BT_INFO("Bond in progress, cancel and retry");
680 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
682 result = BLUETOOTH_ERROR_PARING_FAILED;
683 } else if (!strcmp(err->message, "Authentication Failed")) {
684 BT_INFO("Authentication Failed");
685 if (bonding_info->is_autopair == TRUE) {
686 _bt_set_autopair_status_in_bonding_info(FALSE);
687 __ignore_auto_pairing_request(bonding_info->addr);
689 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
690 } else if (!strcmp(err->message, "Page Timeout")) {
691 BT_INFO("Page Timeout");
692 /* This is the special case
693 As soon as call bluetooth_bond_device, try to cancel bonding.
694 In this case, before completing to call 'CreatePairedDevice' method
695 the procedure is stopped. So 'Cancle' error is not return.
697 result = BLUETOOTH_ERROR_HOST_DOWN;
698 } else if (!strcmp(err->message, BT_TIMEOUT_MESSAGE)) {
699 g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
700 g_variant_new("(s)", bonding_info->addr),
701 G_DBUS_CALL_FLAGS_NONE,
706 result = BLUETOOTH_ERROR_INTERNAL;
707 } else if (!strcmp(err->message, "Connection Timeout")) {
708 /* Pairing request timeout */
709 result = BLUETOOTH_ERROR_TIMEOUT;
710 } else if (!strcmp(err->message, "Authentication Timeout")) {
711 /* Pairing request timeout */
712 result = BLUETOOTH_ERROR_TIMEOUT;
714 BT_DBG("Default case");
715 result = BLUETOOTH_ERROR_PARING_FAILED;
719 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
720 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
721 result == BLUETOOTH_ERROR_TIMEOUT ||
722 result == BLUETOOTH_ERROR_HOST_DOWN) {
723 bonding_info->result = result;
724 #ifdef TIZEN_WEARABLE
725 __bt_launch_unable_to_pairing_syspopup(result);
729 g_object_unref(proxy);
730 bonding_info->device_proxy = NULL;
732 if (result != BLUETOOTH_ERROR_NONE)
735 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
736 if (NULL == remote_dev_info) {
737 BT_ERR("Failed to get remote device info");
741 GVariant *uuids = NULL;
742 GVariantBuilder *builder = NULL;
744 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
745 for (i=0; i < remote_dev_info->uuid_count; i++) {
746 g_variant_builder_add(builder, "s",
747 remote_dev_info->uuids[i]);
749 uuids = g_variant_new("as", builder);
750 g_variant_builder_unref(builder);
751 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
752 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
755 param = g_variant_new("(isunsbub@asn@ay)",
758 remote_dev_info->class,
759 remote_dev_info->rssi,
760 remote_dev_info->name,
761 remote_dev_info->paired,
762 remote_dev_info->connected,
763 remote_dev_info->trust,
765 remote_dev_info->manufacturer_data_len,
769 /* Send the event to application */
770 _bt_send_event(BT_ADAPTER_EVENT,
771 BLUETOOTH_EVENT_BONDING_FINISHED,
774 _bt_free_device_info(remote_dev_info);
777 if (req_info->context == NULL)
780 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
781 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
784 if (_bt_adapter_get_status() != BT_ACTIVATED)
785 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
787 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
788 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
790 g_dbus_method_invocation_return_value(req_info->context,
791 g_variant_new("(iv)", result, out_param1));
793 g_variant_unref(out_param1);
794 // g_variant_unref(out_param2);
796 _bt_delete_request_list(req_info->req_id);
801 _bt_agent_set_canceled(FALSE);
804 g_free(bonding_info->addr);
805 g_free(bonding_info);
809 int _bt_bond_device(int request_id,
810 bluetooth_device_address_t *device_address,
811 unsigned short conn_type, GArray **out_param1)
814 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
815 bluetooth_device_info_t dev_info;
817 GDBusConnection *conn;
818 char *device_path = NULL;
819 GDBusProxy *adapter_proxy;
820 GError *error = NULL;
822 BT_CHECK_PARAMETER(device_address, return);
825 BT_ERR("Bonding in progress");
827 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
828 memcpy(dev_info.device_address.addr, device_address->addr,
829 BLUETOOTH_ADDRESS_LENGTH);
831 g_array_append_vals(*out_param1, &dev_info,
832 sizeof(bluetooth_device_info_t));
834 return BLUETOOTH_ERROR_DEVICE_BUSY;
837 conn = _bt_get_system_gconn();
838 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
840 _bt_convert_addr_type_to_string(address, device_address->addr);
842 device_path = _bt_get_device_object_path(address);
844 if (device_path == NULL) {
845 BT_ERR("No searched device");
846 GVariant *ret = NULL;
847 adapter_proxy = _bt_get_adapter_proxy();
848 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
850 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
851 g_variant_new("(s)", address),
852 G_DBUS_CALL_FLAGS_NONE,
858 BT_ERR("CreateDevice Fail: %s", error->message);
859 g_clear_error(&error);
862 g_variant_unref(ret);
863 device_path = _bt_get_device_object_path(address);
864 if (device_path == NULL) {
865 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
866 memcpy(dev_info.device_address.addr, device_address->addr,
867 BLUETOOTH_ADDRESS_LENGTH);
869 g_array_append_vals(*out_param1, &dev_info,
870 sizeof(bluetooth_device_info_t));
872 return BLUETOOTH_ERROR_NOT_PAIRED;
874 BT_INFO("device_path is created[%s]", device_path);
878 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
880 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
883 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
885 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
886 bonding_info->addr = g_strdup(address);
887 bonding_info->req_id = request_id;
889 bonding_info->device_proxy = proxy;
890 bonding_info->conn_type = conn_type;
892 is_device_creating = TRUE;
894 g_dbus_proxy_call(proxy, "Pair",
895 g_variant_new("(y)", conn_type),
896 G_DBUS_CALL_FLAGS_NONE,
899 (GAsyncReadyCallback)__bt_bond_device_cb,
902 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
904 return BLUETOOTH_ERROR_NONE;
906 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
907 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
910 g_array_append_vals(*out_param1, &dev_info,
911 sizeof(bluetooth_device_info_t));
913 is_device_creating = FALSE;
915 g_free(bonding_info->addr);
916 g_free(bonding_info);
919 return BLUETOOTH_ERROR_INTERNAL;*/
922 int _bt_cancel_bonding(void)
924 int ret = BLUETOOTH_ERROR_NONE;
926 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
928 ret = _bt_agent_reply_cancellation();
929 if (ret != BLUETOOTH_ERROR_NONE){
930 BT_ERR("Fail to call reply cancellation");
934 _bt_agent_set_canceled(TRUE);
936 return BLUETOOTH_ERROR_NONE;
939 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
943 GVariant *out_param1;
944 int result = BLUETOOTH_ERROR_NONE;
945 bt_funcion_data_t *unbonding_info;
946 bluetooth_device_info_t dev_info;
947 request_info_t *req_info;
949 g_dbus_proxy_call_finish(proxy, res, &err);
951 unbonding_info = user_data;
953 if (unbonding_info == NULL) {
955 BT_ERR("unbonding_info == NULL");
959 req_info = _bt_get_request_info(unbonding_info->req_id);
960 if (req_info == NULL) {
961 BT_ERR("req_info == NULL");
966 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
967 result = BLUETOOTH_ERROR_INTERNAL;
970 if (req_info->context == NULL)
973 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
974 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
975 unbonding_info->addr);
977 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
978 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
980 g_dbus_method_invocation_return_value(req_info->context,
981 g_variant_new("(iv)", result, out_param1));
984 _bt_delete_request_list(req_info->req_id);
990 if (unbonding_info) {
991 g_free(unbonding_info->addr);
992 g_free(unbonding_info);
996 int _bt_unbond_device(int request_id,
997 bluetooth_device_address_t *device_address,
1000 char *device_path = NULL;
1001 bt_funcion_data_t *unbonding_info;
1002 GDBusProxy *adapter_proxy = NULL;
1003 GDBusProxy *device_proxy = NULL;
1004 GDBusConnection *conn;
1005 int result = BLUETOOTH_ERROR_INTERNAL;
1006 bluetooth_device_info_t dev_info;
1007 GError *error = NULL;
1008 GVariant *ret = NULL;
1010 BT_CHECK_PARAMETER(device_address, return);
1012 adapter_proxy = _bt_get_adapter_proxy();
1013 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1015 /* allocate user data so that it can be retrieved in callback */
1016 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1017 /* Fix : NULL_RETURNS */
1018 if (unbonding_info == NULL) {
1019 BT_ERR("Memory not allocated !");
1020 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1023 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1024 unbonding_info->req_id = request_id;
1026 _bt_convert_addr_type_to_string(unbonding_info->addr,
1027 device_address->addr);
1029 device_path = _bt_get_device_object_path(unbonding_info->addr);
1031 if (device_path == NULL) {
1032 BT_ERR("No paired device");
1033 result = BLUETOOTH_ERROR_NOT_PAIRED;
1037 conn = _bt_get_system_gconn();
1039 BT_ERR("conn is NULL");
1040 result = BLUETOOTH_ERROR_INTERNAL;
1044 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1045 NULL, BT_BLUEZ_NAME,
1046 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1048 if (device_proxy != NULL) {
1050 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1051 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1052 G_DBUS_CALL_FLAGS_NONE,
1057 BT_ERR("Getting property failed: [%s]\n", error->message);
1058 g_error_free(error);
1059 result = BLUETOOTH_ERROR_NOT_PAIRED;
1063 BT_ERR("No paired device");
1064 g_object_unref(device_proxy);
1065 result = BLUETOOTH_ERROR_NOT_PAIRED;
1068 g_variant_unref(ret);
1070 g_object_unref(device_proxy);
1073 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1074 g_variant_new("(o)", device_path),
1075 G_DBUS_CALL_FLAGS_NONE,
1076 BT_MAX_DBUS_TIMEOUT,
1078 (GAsyncReadyCallback)__bt_unbond_cb,
1081 g_free(device_path);
1082 return BLUETOOTH_ERROR_NONE;
1085 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1086 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1087 unbonding_info->addr);
1089 g_array_append_vals(*out_param1, &dev_info,
1090 sizeof(bluetooth_device_info_t));
1091 g_free(device_path);
1092 g_free(unbonding_info->addr);
1093 g_free(unbonding_info);
1097 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1101 GVariant *out_param1;
1102 int result = BLUETOOTH_ERROR_NONE;
1103 bluetooth_device_info_t dev_info;
1104 bt_remote_dev_info_t *remote_dev_info = NULL;
1105 request_info_t *req_info;
1106 GVariant *uuid_list, *manufacture_data;
1108 GVariantBuilder *builder = NULL;
1111 g_dbus_proxy_call_finish(proxy, res, &err);
1113 g_object_unref(proxy);
1115 if (searching_info == NULL) {
1117 BT_ERR("unbonding_info == NULL");
1121 req_info = _bt_get_request_info(searching_info->req_id);
1122 if (req_info == NULL) {
1123 BT_ERR("req_info == NULL");
1128 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1130 if (!strcmp("Operation canceled", err->message)) {
1131 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1132 } else if (!strcmp("In Progress", err->message)) {
1133 result = BLUETOOTH_ERROR_IN_PROGRESS;
1134 } else if (!strcmp("Host is down", err->message)) {
1135 result = BLUETOOTH_ERROR_HOST_DOWN;
1137 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1140 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1141 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1142 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1143 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1144 remote_dev_info->uuid_count > 0) {
1145 result = BLUETOOTH_ERROR_NONE;
1148 _bt_free_device_info(remote_dev_info);
1153 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1154 if (NULL == remote_dev_info) {
1155 BT_ERR("Failed to get remote device info");
1160 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1161 for (i=0; i < remote_dev_info->uuid_count; i++) {
1162 g_variant_builder_add(builder, "s",
1163 remote_dev_info->uuids[i]);
1165 uuid_list = g_variant_new("as", builder);
1166 g_variant_builder_unref(builder);
1167 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1168 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1171 param = g_variant_new("(isunsbub@asn@ay)",
1173 searching_info->addr,
1174 remote_dev_info->class,
1175 remote_dev_info->rssi,
1176 remote_dev_info->name,
1177 remote_dev_info->paired,
1178 remote_dev_info->connected,
1179 remote_dev_info->trust,
1181 remote_dev_info->manufacturer_data_len,
1185 /* Send the event to application */
1186 _bt_send_event(BT_ADAPTER_EVENT,
1187 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1190 _bt_free_device_info(remote_dev_info);
1193 if (req_info->context == NULL)
1196 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1197 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1198 searching_info->addr);
1200 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1201 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1203 g_dbus_method_invocation_return_value(req_info->context,
1204 g_variant_new("(iv)", result, out_param1));
1207 _bt_delete_request_list(req_info->req_id);
1212 if (searching_info) {
1213 g_free(searching_info->addr);
1214 g_free(searching_info);
1215 searching_info = NULL;
1219 int _bt_search_device(int request_id,
1220 bluetooth_device_address_t *device_address)
1222 char *device_path = NULL;
1223 GDBusProxy *device_proxy = NULL;
1224 GDBusConnection *conn;
1226 GDBusProxy *adapter_proxy;
1227 int result = BLUETOOTH_ERROR_INTERNAL;
1229 BT_CHECK_PARAMETER(device_address, return);
1232 BT_ERR("Bonding in progress");
1233 return BLUETOOTH_ERROR_DEVICE_BUSY;
1236 if (searching_info) {
1237 BT_ERR("Service searching in progress");
1238 return BLUETOOTH_ERROR_DEVICE_BUSY;
1241 adapter_proxy = _bt_get_adapter_proxy();
1242 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1244 /* allocate user data so that it can be retrieved in callback */
1245 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1246 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1247 searching_info->req_id = request_id;
1249 _bt_convert_addr_type_to_string(searching_info->addr,
1250 device_address->addr);
1252 conn = _bt_get_system_gconn();
1253 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1256 device_path = _bt_get_device_object_path(searching_info->addr);
1258 if (device_path == NULL) {
1259 BT_ERR("No paired device");
1260 result = BLUETOOTH_ERROR_NOT_PAIRED;
1264 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1265 NULL, BT_BLUEZ_NAME,
1266 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1267 g_free(device_path);
1268 if (device_proxy == NULL) {
1269 result = BLUETOOTH_ERROR_INTERNAL;
1273 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1274 g_variant_new("(s)", ""),
1275 G_DBUS_CALL_FLAGS_NONE,
1276 BT_MAX_DBUS_TIMEOUT,
1278 (GAsyncReadyCallback)__bt_discover_cb,
1281 searching_info->device_proxy = device_proxy;
1283 return BLUETOOTH_ERROR_NONE;
1286 g_object_unref(device_proxy);
1288 g_free(searching_info->addr);
1289 g_free(searching_info);
1290 searching_info = NULL;
1294 int _bt_cancel_search_device(void)
1298 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1300 if (searching_info->device_proxy) {
1301 g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1303 G_DBUS_CALL_FLAGS_NONE,
1308 __bt_cancel_search_service_done();
1310 return BLUETOOTH_ERROR_NONE;
1313 int _bt_set_alias(bluetooth_device_address_t *device_address,
1316 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1317 gchar *device_path = NULL;
1318 GDBusProxy *adapter_proxy;
1319 GDBusProxy *device_proxy;
1320 GError *error = NULL;
1321 GDBusConnection *conn;
1323 BT_CHECK_PARAMETER(device_address, return);
1324 BT_CHECK_PARAMETER(alias, return);
1326 adapter_proxy = _bt_get_adapter_proxy();
1327 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1329 conn = _bt_get_system_gconn();
1330 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1332 _bt_convert_addr_type_to_string(address, device_address->addr);
1334 device_path = _bt_get_device_object_path(address);
1336 if (device_path == NULL) {
1337 BT_ERR("No paired device");
1338 return BLUETOOTH_ERROR_NOT_PAIRED;
1341 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1342 NULL, BT_BLUEZ_NAME,
1343 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1345 g_free(device_path);
1346 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1348 g_dbus_proxy_call_sync(device_proxy, "Set",
1349 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1350 G_DBUS_CALL_FLAGS_NONE,
1355 g_object_unref(device_proxy);
1358 BT_ERR("SetProperty error: [%s]", error->message);
1359 g_error_free(error);
1360 return BLUETOOTH_ERROR_INTERNAL;
1363 return BLUETOOTH_ERROR_NONE;
1366 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1369 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1370 gchar *device_path = NULL;
1371 GDBusProxy *device_proxy;
1372 gboolean previous_value;
1373 GError *error = NULL;
1374 GDBusConnection *conn;
1375 GVariant *result = NULL;
1376 int ret = BLUETOOTH_ERROR_NONE;
1378 BT_CHECK_PARAMETER(device_address, return);
1380 conn = _bt_get_system_gconn();
1381 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1383 _bt_convert_addr_type_to_string(address, device_address->addr);
1385 device_path = _bt_get_device_object_path(address);
1387 if (device_path == NULL) {
1388 BT_ERR("No paired device");
1389 return BLUETOOTH_ERROR_NOT_PAIRED;
1392 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1393 NULL, BT_BLUEZ_NAME,
1394 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1396 g_free(device_path);
1397 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1399 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1400 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted" ),
1401 G_DBUS_CALL_FLAGS_NONE,
1405 if (error != NULL) {
1406 BT_ERR("Getting property failed: [%s]\n", error->message);
1407 g_error_free(error);
1408 g_object_unref(device_proxy);
1409 return BLUETOOTH_ERROR_INTERNAL;
1412 previous_value = g_variant_get_boolean(result);
1413 g_variant_unref(result);
1414 /* If the input is same with previous value, return error. */
1415 if (previous_value == authorize) {
1416 BT_ERR("Same value: %d", previous_value);
1417 g_object_unref(device_proxy);
1418 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1422 g_dbus_proxy_call_sync(device_proxy, "Set",
1423 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
1424 G_DBUS_CALL_FLAGS_NONE,
1429 g_object_unref(device_proxy);
1431 BT_ERR("SetProperty error: [%s]", error->message);
1432 g_error_free(error);
1433 ret = BLUETOOTH_ERROR_INTERNAL;
1439 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1440 gboolean *is_connected)
1442 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1443 char *object_path = NULL;
1445 GDBusProxy *device_proxy;
1446 GError *error = NULL;
1448 GVariant *tmp_value;
1449 GDBusConnection *conn;
1450 GVariant *result = NULL;
1451 int ret = BLUETOOTH_ERROR_NONE;
1453 BT_CHECK_PARAMETER(device_address, return);
1455 conn = _bt_get_system_gconn();
1456 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1458 _bt_convert_addr_type_to_string(address, device_address->addr);
1460 object_path = _bt_get_device_object_path(address);
1461 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1463 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1464 NULL, BT_BLUEZ_NAME,
1465 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1466 g_free(object_path);
1467 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1469 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1471 G_DBUS_CALL_FLAGS_NONE,
1476 if (error != NULL || result == NULL) {
1477 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1478 g_error_free(error);
1479 g_object_unref(device_proxy);
1480 return BLUETOOTH_ERROR_INTERNAL;
1483 g_variant_get(result , "(@a{sv})", &value);
1484 g_variant_unref(result);
1486 tmp_value = g_variant_lookup_value (value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1487 if (tmp_value != NULL) {
1488 g_object_unref(device_proxy);
1489 return BLUETOOTH_ERROR_INTERNAL;
1491 *is_connected = g_variant_get_boolean (tmp_value);
1493 BT_DBG("gatt is connected : %d", *is_connected);
1494 g_variant_unref(tmp_value);
1495 g_variant_unref(value);
1496 g_object_unref(device_proxy);
1501 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1502 int connection_type, gboolean *is_connected)
1504 char *object_path = NULL;
1505 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1506 GDBusProxy *device_proxy = NULL;
1507 GDBusProxy *adapter_proxy = NULL;
1508 GDBusConnection *conn;
1509 GError *error = NULL;
1510 GVariant *tmp_value = NULL;
1511 GVariant *value = NULL;
1512 GVariant *result = NULL;
1515 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1516 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1518 *is_connected = FALSE;
1519 BT_DBG("connection_type: %d", connection_type);
1520 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1521 return _bt_rfcomm_is_device_connected(device_address,
1523 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1524 return _bt_is_gatt_connected(device_address, is_connected);
1526 adapter_proxy = _bt_get_adapter_proxy();
1527 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1529 conn = _bt_get_system_gconn();
1530 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1532 _bt_convert_addr_type_to_string(address, device_address->addr);
1534 if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1535 object_path = _bt_get_adapter_path();
1536 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1537 NULL, BT_BLUEZ_NAME,
1538 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1539 g_free(object_path);
1540 if (device_proxy == NULL) {
1541 BT_DBG("Device don't have this service");
1542 return BLUETOOTH_ERROR_INTERNAL;
1545 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1546 g_variant_new("(s)", address),
1547 G_DBUS_CALL_FLAGS_NONE,
1552 if (result == NULL) {
1553 BT_ERR("Error occured in Proxy call");
1555 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1556 g_error_free(error);
1558 g_object_unref(device_proxy);
1559 return BLUETOOTH_ERROR_INTERNAL;
1561 g_variant_get(result , "(@a{sv})", &value);
1562 g_variant_unref(result);
1565 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1566 if (tmp_value != NULL) {
1567 g_object_unref(device_proxy);
1568 return BLUETOOTH_ERROR_INTERNAL;
1570 *is_connected = g_variant_get_boolean (tmp_value);
1571 g_variant_unref(tmp_value);
1572 g_variant_unref(value);
1573 } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1574 return _bt_is_network_connected(_bt_get_net_conn(),
1575 device_address->addr, is_connected);
1577 uuid = _bt_get_profile_uuid128(connection_type);
1579 BT_ERR("uuid is NULL");
1580 return BLUETOOTH_ERROR_INTERNAL;
1583 BT_DBG("uuid: %s", uuid);
1585 object_path = _bt_get_device_object_path(address);
1586 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1587 BT_DBG("object_path: %s", object_path);
1588 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1589 NULL, BT_BLUEZ_NAME,
1590 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1591 g_free(object_path);
1592 if (device_proxy == NULL) {
1593 BT_DBG("Device don't have this service");
1595 return BLUETOOTH_ERROR_INTERNAL;
1598 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1599 g_variant_new("(s)", uuid),
1600 G_DBUS_CALL_FLAGS_NONE,
1605 if (result == NULL) {
1606 BT_ERR("Error occured in Proxy call");
1608 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1609 g_error_free(error);
1611 g_object_unref(device_proxy);
1612 return BLUETOOTH_ERROR_INTERNAL;
1614 g_variant_get(result, "(b)", is_connected);
1616 g_variant_unref(result);
1619 g_object_unref(device_proxy);
1620 return BLUETOOTH_ERROR_NONE;
1623 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1624 bluetooth_connected_link_t *connected)
1626 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1627 char *object_path = NULL;
1629 GDBusProxy *device_proxy;
1630 GError *error = NULL;
1631 GDBusConnection *conn;
1632 GVariant *tmp_value = NULL;
1633 GVariant *value = NULL;
1634 GVariant *result = NULL;
1636 int ret = BLUETOOTH_ERROR_NONE;
1638 BT_CHECK_PARAMETER(device_address, return);
1640 conn = _bt_get_system_gconn();
1641 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1643 _bt_convert_addr_type_to_string(address, device_address->addr);
1645 object_path = _bt_get_device_object_path(address);
1646 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1648 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1649 NULL, BT_BLUEZ_NAME,
1650 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1651 g_free(object_path);
1652 if (device_proxy == NULL) {
1653 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1654 return BLUETOOTH_ERROR_NONE;
1657 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1658 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1659 G_DBUS_CALL_FLAGS_NONE,
1664 if (error != NULL) {
1665 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1666 g_error_free(error);
1667 g_object_unref(device_proxy);
1668 return BLUETOOTH_ERROR_INTERNAL;
1671 g_variant_get(result , "(@a{sv})", &value);
1672 g_variant_unref(result);
1674 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1675 if (tmp_value != NULL) {
1676 g_object_unref(device_proxy);
1677 g_variant_unref(value);
1678 return BLUETOOTH_ERROR_INTERNAL;
1680 *connected = g_variant_get_boolean (tmp_value);
1681 g_variant_unref(tmp_value);
1682 g_variant_unref(value);
1683 g_object_unref(device_proxy);
1688 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1690 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1691 gchar *device_path = NULL;
1692 GError *error = NULL;
1693 GDBusProxy *device_proxy = NULL;
1694 GDBusProxy *adapter_proxy;
1695 GDBusConnection *conn;
1696 int ret = BLUETOOTH_ERROR_NONE;
1698 BT_CHECK_PARAMETER(bd_addr, return);
1700 _bt_convert_addr_type_to_string(device_address,
1701 (unsigned char *)bd_addr->addr);
1703 conn = _bt_get_system_gconn();
1704 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1706 adapter_proxy = _bt_get_adapter_proxy();
1707 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1709 device_path = _bt_get_device_object_path(device_address);
1710 if (device_path == NULL) {
1711 BT_DBG("device_path NULL");
1712 ret = BLUETOOTH_ERROR_INTERNAL;
1716 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1718 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1719 NULL, BT_BLUEZ_NAME,
1720 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1721 g_free(device_path);
1722 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1724 g_dbus_proxy_call_sync(device_proxy, "ConnectLE",
1725 g_variant_new("(b)", auto_connect),
1726 G_DBUS_CALL_FLAGS_NONE,
1732 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1733 g_error_free(error);
1734 g_object_unref(device_proxy);
1735 return BLUETOOTH_ERROR_INTERNAL;
1738 g_object_unref(device_proxy);
1743 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1745 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1746 gchar *device_path = NULL;
1747 GError *error = NULL;
1748 GDBusProxy *device_proxy = NULL;
1749 GDBusProxy *adapter_proxy;
1750 GDBusConnection *conn;
1751 int ret = BLUETOOTH_ERROR_NONE;
1753 BT_CHECK_PARAMETER(bd_addr, return);
1755 _bt_convert_addr_type_to_string(device_address,
1756 (unsigned char *)bd_addr->addr);
1758 conn = _bt_get_system_gconn();
1759 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1761 adapter_proxy = _bt_get_adapter_proxy();
1762 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1764 device_path = _bt_get_device_object_path(device_address);
1765 if (device_path == NULL) {
1766 BT_DBG("device_path NULL");
1767 ret = BLUETOOTH_ERROR_INTERNAL;
1771 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1773 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1774 NULL, BT_BLUEZ_NAME,
1775 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1776 g_free(device_path);
1777 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1779 g_dbus_proxy_call_sync(device_proxy, "DisconnectLE",
1781 G_DBUS_CALL_FLAGS_NONE,
1786 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1787 g_error_free(error);
1788 g_object_unref(device_proxy);
1789 return BLUETOOTH_ERROR_INTERNAL;
1792 g_object_unref(device_proxy);
1797 int _bt_connect_profile(char *address, char *uuid,
1798 void *cb, gpointer func_data)
1802 GDBusConnection *conn;
1803 GDBusProxy *adapter_proxy;
1804 GError *error = NULL;
1806 conn = _bt_get_system_gconn();
1807 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1809 object_path = _bt_get_device_object_path(address);
1810 if (object_path == NULL) {
1811 BT_ERR("No searched device");
1813 adapter_proxy = _bt_get_adapter_proxy();
1814 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1816 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1817 g_variant_new("(s)", address),
1818 G_DBUS_CALL_FLAGS_NONE,
1823 if (error != NULL) {
1824 BT_ERR("CreateDevice Fail: %s", error->message);
1825 g_error_free(error);
1828 object_path = _bt_get_device_object_path(address);
1830 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1832 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1833 NULL, BT_BLUEZ_NAME,
1834 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1835 g_free(object_path);
1836 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1838 g_dbus_proxy_call(proxy, "ConnectProfile",
1839 g_variant_new("(s)", uuid),
1840 G_DBUS_CALL_FLAGS_NONE,
1841 BT_MAX_DBUS_TIMEOUT,
1843 (GAsyncReadyCallback)cb,
1846 return BLUETOOTH_ERROR_NONE;
1849 int _bt_disconnect_profile(char *address, char *uuid,
1850 void *cb, gpointer func_data)
1854 GDBusConnection *conn;
1856 conn = _bt_get_system_gconn();
1857 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1859 object_path = _bt_get_device_object_path(address);
1860 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1862 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1863 NULL, BT_BLUEZ_NAME,
1864 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1865 g_free(object_path);
1866 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1868 g_dbus_proxy_call(proxy, "DisconnectProfile",
1869 g_variant_new("(s)", uuid),
1870 G_DBUS_CALL_FLAGS_NONE,
1871 BT_MAX_DBUS_TIMEOUT,
1873 (GAsyncReadyCallback)cb,
1876 return BLUETOOTH_ERROR_NONE;
1879 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1880 int low_threshold, int in_range_threshold, int high_threshold)
1882 int ret = BLUETOOTH_ERROR_NONE;
1884 GError *error = NULL;
1885 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1887 BT_CHECK_PARAMETER(bd_addr, return);
1888 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1889 bd_addr->addr[0], bd_addr->addr[1],
1890 bd_addr->addr[2], bd_addr->addr[3],
1891 bd_addr->addr[4], bd_addr->addr[5],
1893 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1894 in_range_threshold, high_threshold);
1896 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1898 proxy = _bt_get_adapter_proxy();
1899 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1901 g_dbus_proxy_call_sync(proxy, "EnableRssi",
1902 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
1903 G_DBUS_CALL_FLAGS_NONE,
1907 if (error != NULL) {
1908 BT_ERR("Dbus Call Error:[%s]", error->message);
1909 g_error_free(error);
1910 ret = BLUETOOTH_ERROR_INTERNAL;
1915 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1918 int ret = BLUETOOTH_ERROR_NONE;
1920 GError *error = NULL;
1921 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1923 BT_CHECK_PARAMETER(bd_addr, return);
1924 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1925 bd_addr->addr[0], bd_addr->addr[1],
1926 bd_addr->addr[2], bd_addr->addr[3],
1927 bd_addr->addr[4], bd_addr->addr[5],
1930 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1932 proxy = _bt_get_adapter_proxy();
1933 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1935 g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
1936 g_variant_new("(si)", address, link_type),
1937 G_DBUS_CALL_FLAGS_NONE,
1942 if (error != NULL) {
1943 BT_ERR("Dbus Call Error:[%s]", error->message);
1944 g_error_free(error);
1945 ret = BLUETOOTH_ERROR_INTERNAL;
1950 int _bt_le_conn_update(unsigned char *device_address,
1951 guint16 interval_min, guint16 interval_max,
1952 guint16 latency, guint16 time_out)
1954 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1955 gchar *device_path = NULL;
1956 GError *error = NULL;
1957 GDBusProxy *device_proxy = NULL;
1958 GDBusConnection *conn;
1960 guint32 min, max, to;
1961 guint32 min_supervision_to;
1962 int ret = BLUETOOTH_ERROR_NONE;
1966 BT_CHECK_PARAMETER(device_address, return);
1968 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1969 interval_min, interval_max, latency, time_out);
1971 if (interval_min > interval_max ||
1972 interval_min < BT_LE_CONN_INTERVAL_MIN ||
1973 interval_max > BT_LE_CONN_INTERVAL_MAX) {
1974 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1978 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1979 time_out > BT_LE_CONN_SUPER_TO_MAX) {
1980 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1984 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
1985 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1990 * The Supervision_Timeout in milliseconds shall be larger than
1991 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
1992 * where Conn_Interval_Max is given in milliseconds.
1994 min_supervision_to = (1 + latency) * interval_max * 2;
1995 if (time_out <= min_supervision_to) {
1996 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2000 _bt_convert_addr_type_to_string(address, device_address);
2002 BT_DBG("Remote device address: %s", address);
2004 device_path = _bt_get_device_object_path(address);
2006 if (device_path == NULL) {
2007 BT_DBG("device_path NULL");
2008 ret = BLUETOOTH_ERROR_INTERNAL;
2012 conn = _bt_get_system_gconn();
2014 BT_DBG("conn NULL");
2015 ret = BLUETOOTH_ERROR_INTERNAL;
2019 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2020 NULL, BT_BLUEZ_NAME,
2021 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2023 g_free(device_path);
2024 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2026 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2027 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2028 to = time_out / BT_LE_CONN_TO_SPLIT;
2030 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2031 g_variant_new("(uuuu)", min, max, latency, to),
2032 G_DBUS_CALL_FLAGS_NONE,
2037 g_object_unref(device_proxy);
2038 BT_ERR("LeConnUpdate Call Error for %s", address);
2039 if (reply == NULL) {
2041 BT_ERR("Error %s[%s]", error->message, address);
2042 g_error_free(error);
2043 return BLUETOOTH_ERROR_INTERNAL;
2046 g_variant_unref(reply);