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;
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);
737 GVariant *uuids = NULL;
738 GVariantBuilder *builder = NULL;
740 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
741 for (i=0; i < remote_dev_info->uuid_count; i++) {
742 g_variant_builder_add(builder, "s",
743 remote_dev_info->uuids[i]);
745 uuids = g_variant_new("as", builder);
746 g_variant_builder_unref(builder);
747 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
748 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
751 param = g_variant_new("(isunsbub@asn@ay)",
754 remote_dev_info->class,
755 remote_dev_info->rssi,
756 remote_dev_info->name,
757 remote_dev_info->paired,
758 remote_dev_info->connected,
759 remote_dev_info->trust,
761 remote_dev_info->manufacturer_data_len,
765 /* Send the event to application */
766 if (remote_dev_info != NULL) {
768 _bt_send_event(BT_ADAPTER_EVENT,
769 BLUETOOTH_EVENT_BONDING_FINISHED,
772 _bt_free_device_info(remote_dev_info);
776 if (req_info->context == NULL)
779 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
780 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
783 if (_bt_adapter_get_status() != BT_ACTIVATED)
784 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
786 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
787 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
789 g_dbus_method_invocation_return_value(req_info->context,
790 g_variant_new("(iv)", result, out_param1));
792 g_variant_unref(out_param1);
793 // g_variant_unref(out_param2);
795 _bt_delete_request_list(req_info->req_id);
800 _bt_agent_set_canceled(FALSE);
803 g_free(bonding_info->addr);
804 g_free(bonding_info);
808 int _bt_bond_device(int request_id,
809 bluetooth_device_address_t *device_address,
810 unsigned short conn_type, GArray **out_param1)
813 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
814 bluetooth_device_info_t dev_info;
816 GDBusConnection *conn;
817 char *device_path = NULL;
818 GDBusProxy *adapter_proxy;
819 GError *error = NULL;
821 BT_CHECK_PARAMETER(device_address, return);
824 BT_ERR("Bonding in progress");
826 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
827 memcpy(dev_info.device_address.addr, device_address->addr,
828 BLUETOOTH_ADDRESS_LENGTH);
830 g_array_append_vals(*out_param1, &dev_info,
831 sizeof(bluetooth_device_info_t));
833 return BLUETOOTH_ERROR_DEVICE_BUSY;
836 conn = _bt_get_system_gconn();
837 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
839 _bt_convert_addr_type_to_string(address, device_address->addr);
841 device_path = _bt_get_device_object_path(address);
843 if (device_path == NULL) {
844 BT_ERR("No searched device");
845 GVariant *ret = NULL;
846 adapter_proxy = _bt_get_adapter_proxy();
847 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
849 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
850 g_variant_new("(s)", address),
851 G_DBUS_CALL_FLAGS_NONE,
857 BT_ERR("CreateDevice Fail: %s", error->message);
858 g_clear_error(&error);
861 g_variant_unref(ret);
862 device_path = _bt_get_device_object_path(address);
863 if (device_path == NULL) {
864 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
865 memcpy(dev_info.device_address.addr, device_address->addr,
866 BLUETOOTH_ADDRESS_LENGTH);
868 g_array_append_vals(*out_param1, &dev_info,
869 sizeof(bluetooth_device_info_t));
871 return BLUETOOTH_ERROR_NOT_PAIRED;
873 BT_INFO("device_path is created[%s]", device_path);
877 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
879 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
882 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
884 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
885 bonding_info->addr = g_strdup(address);
886 bonding_info->req_id = request_id;
888 bonding_info->device_proxy = proxy;
889 bonding_info->conn_type = conn_type;
891 is_device_creating = TRUE;
893 g_dbus_proxy_call(proxy, "Pair",
894 g_variant_new("(y)", conn_type),
895 G_DBUS_CALL_FLAGS_NONE,
898 (GAsyncReadyCallback)__bt_bond_device_cb,
901 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
903 return BLUETOOTH_ERROR_NONE;
905 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
906 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
909 g_array_append_vals(*out_param1, &dev_info,
910 sizeof(bluetooth_device_info_t));
912 is_device_creating = FALSE;
914 g_free(bonding_info->addr);
915 g_free(bonding_info);
918 return BLUETOOTH_ERROR_INTERNAL;*/
921 int _bt_cancel_bonding(void)
923 int ret = BLUETOOTH_ERROR_NONE;
925 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
927 ret = _bt_agent_reply_cancellation();
928 if (ret != BLUETOOTH_ERROR_NONE){
929 BT_ERR("Fail to call reply cancellation");
933 _bt_agent_set_canceled(TRUE);
935 return BLUETOOTH_ERROR_NONE;
938 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
942 GVariant *out_param1;
943 int result = BLUETOOTH_ERROR_NONE;
944 bt_funcion_data_t *unbonding_info;
945 bluetooth_device_info_t dev_info;
946 request_info_t *req_info;
948 g_dbus_proxy_call_finish(proxy, res, &err);
950 unbonding_info = user_data;
952 if (unbonding_info == NULL) {
954 BT_ERR("unbonding_info == NULL");
958 req_info = _bt_get_request_info(unbonding_info->req_id);
959 if (req_info == NULL) {
960 BT_ERR("req_info == NULL");
965 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
966 result = BLUETOOTH_ERROR_INTERNAL;
969 if (req_info->context == NULL)
972 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
973 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
974 unbonding_info->addr);
976 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
977 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
979 g_dbus_method_invocation_return_value(req_info->context,
980 g_variant_new("(iv)", result, out_param1));
983 _bt_delete_request_list(req_info->req_id);
989 if (unbonding_info) {
990 g_free(unbonding_info->addr);
991 g_free(unbonding_info);
995 int _bt_unbond_device(int request_id,
996 bluetooth_device_address_t *device_address,
999 char *device_path = NULL;
1000 bt_funcion_data_t *unbonding_info;
1001 GDBusProxy *adapter_proxy = NULL;
1002 GDBusProxy *device_proxy = NULL;
1003 GDBusConnection *conn;
1004 int result = BLUETOOTH_ERROR_INTERNAL;
1005 bluetooth_device_info_t dev_info;
1006 GError *error = NULL;
1007 GVariant *ret = NULL;
1009 BT_CHECK_PARAMETER(device_address, return);
1011 adapter_proxy = _bt_get_adapter_proxy();
1012 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1014 /* allocate user data so that it can be retrieved in callback */
1015 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1016 /* Fix : NULL_RETURNS */
1017 if (unbonding_info == NULL) {
1018 BT_ERR("Memory not allocated !");
1019 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1022 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1023 unbonding_info->req_id = request_id;
1025 _bt_convert_addr_type_to_string(unbonding_info->addr,
1026 device_address->addr);
1028 device_path = _bt_get_device_object_path(unbonding_info->addr);
1030 if (device_path == NULL) {
1031 BT_ERR("No paired device");
1032 result = BLUETOOTH_ERROR_NOT_PAIRED;
1036 conn = _bt_get_system_gconn();
1038 BT_ERR("conn is NULL");
1039 result = BLUETOOTH_ERROR_INTERNAL;
1043 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1044 NULL, BT_BLUEZ_NAME,
1045 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1047 if (device_proxy != NULL) {
1049 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1050 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1051 G_DBUS_CALL_FLAGS_NONE,
1056 BT_ERR("Getting property failed: [%s]\n", error->message);
1057 g_error_free(error);
1058 result = BLUETOOTH_ERROR_NOT_PAIRED;
1062 BT_ERR("No paired device");
1063 g_object_unref(device_proxy);
1064 result = BLUETOOTH_ERROR_NOT_PAIRED;
1067 g_variant_unref(ret);
1069 g_object_unref(device_proxy);
1072 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1073 g_variant_new("(o)", device_path),
1074 G_DBUS_CALL_FLAGS_NONE,
1075 BT_MAX_DBUS_TIMEOUT,
1077 (GAsyncReadyCallback)__bt_unbond_cb,
1080 g_free(device_path);
1081 return BLUETOOTH_ERROR_NONE;
1084 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1085 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1086 unbonding_info->addr);
1088 g_array_append_vals(*out_param1, &dev_info,
1089 sizeof(bluetooth_device_info_t));
1090 g_free(device_path);
1091 g_free(unbonding_info->addr);
1092 g_free(unbonding_info);
1096 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1100 GVariant *out_param1;
1101 int result = BLUETOOTH_ERROR_NONE;
1102 bluetooth_device_info_t dev_info;
1103 bt_remote_dev_info_t *remote_dev_info;
1104 request_info_t *req_info;
1105 GVariant *uuid_list, *manufacture_data;
1107 GVariantBuilder *builder = NULL;
1110 g_dbus_proxy_call_finish(proxy, res, &err);
1112 g_object_unref(proxy);
1114 if (searching_info == NULL) {
1116 BT_ERR("unbonding_info == NULL");
1120 req_info = _bt_get_request_info(searching_info->req_id);
1121 if (req_info == NULL) {
1122 BT_ERR("req_info == NULL");
1127 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1129 if (!strcmp("Operation canceled", err->message)) {
1130 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1131 } else if (!strcmp("In Progress", err->message)) {
1132 result = BLUETOOTH_ERROR_IN_PROGRESS;
1133 } else if (!strcmp("Host is down", err->message)) {
1134 result = BLUETOOTH_ERROR_HOST_DOWN;
1136 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1139 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1140 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1141 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1142 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1143 remote_dev_info->uuid_count > 0) {
1144 result = BLUETOOTH_ERROR_NONE;
1147 _bt_free_device_info(remote_dev_info);
1152 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1155 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1156 for (i=0; i < remote_dev_info->uuid_count; i++) {
1157 g_variant_builder_add(builder, "s",
1158 remote_dev_info->uuids[i]);
1160 uuid_list = g_variant_new("as", builder);
1161 g_variant_builder_unref(builder);
1162 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1163 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1166 param = g_variant_new("(isunsbub@asn@ay)",
1168 searching_info->addr,
1169 remote_dev_info->class,
1170 remote_dev_info->rssi,
1171 remote_dev_info->name,
1172 remote_dev_info->paired,
1173 remote_dev_info->connected,
1174 remote_dev_info->trust,
1176 remote_dev_info->manufacturer_data_len,
1180 /* Send the event to application */
1181 if (remote_dev_info != NULL) {
1183 _bt_send_event(BT_ADAPTER_EVENT,
1184 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1187 _bt_free_device_info(remote_dev_info);
1191 if (req_info->context == NULL)
1194 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1195 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1196 searching_info->addr);
1198 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1199 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1201 g_dbus_method_invocation_return_value(req_info->context,
1202 g_variant_new("(iv)", result, out_param1));
1205 _bt_delete_request_list(req_info->req_id);
1210 if (searching_info) {
1211 g_free(searching_info->addr);
1212 g_free(searching_info);
1213 searching_info = NULL;
1217 int _bt_search_device(int request_id,
1218 bluetooth_device_address_t *device_address)
1220 char *device_path = NULL;
1221 GDBusProxy *device_proxy = NULL;
1222 GDBusConnection *conn;
1224 GDBusProxy *adapter_proxy;
1225 int result = BLUETOOTH_ERROR_INTERNAL;
1227 BT_CHECK_PARAMETER(device_address, return);
1230 BT_ERR("Bonding in progress");
1231 return BLUETOOTH_ERROR_DEVICE_BUSY;
1234 if (searching_info) {
1235 BT_ERR("Service searching in progress");
1236 return BLUETOOTH_ERROR_DEVICE_BUSY;
1239 adapter_proxy = _bt_get_adapter_proxy();
1240 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1242 /* allocate user data so that it can be retrieved in callback */
1243 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1244 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1245 searching_info->req_id = request_id;
1247 _bt_convert_addr_type_to_string(searching_info->addr,
1248 device_address->addr);
1250 conn = _bt_get_system_gconn();
1251 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1254 device_path = _bt_get_device_object_path(searching_info->addr);
1256 if (device_path == NULL) {
1257 BT_ERR("No paired device");
1258 result = BLUETOOTH_ERROR_NOT_PAIRED;
1262 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1263 NULL, BT_BLUEZ_NAME,
1264 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1265 g_free(device_path);
1266 if (device_proxy == NULL) {
1267 result = BLUETOOTH_ERROR_INTERNAL;
1271 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1272 g_variant_new("(s)", ""),
1273 G_DBUS_CALL_FLAGS_NONE,
1274 BT_MAX_DBUS_TIMEOUT,
1276 (GAsyncReadyCallback)__bt_discover_cb,
1279 searching_info->device_proxy = device_proxy;
1281 return BLUETOOTH_ERROR_NONE;
1284 g_object_unref(device_proxy);
1286 g_free(searching_info->addr);
1287 g_free(searching_info);
1288 searching_info = NULL;
1292 int _bt_cancel_search_device(void)
1296 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1298 if (searching_info->device_proxy) {
1299 g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1301 G_DBUS_CALL_FLAGS_NONE,
1306 __bt_cancel_search_service_done();
1308 return BLUETOOTH_ERROR_NONE;
1311 int _bt_set_alias(bluetooth_device_address_t *device_address,
1314 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1315 gchar *device_path = NULL;
1316 GDBusProxy *adapter_proxy;
1317 GDBusProxy *device_proxy;
1318 GError *error = NULL;
1319 GDBusConnection *conn;
1321 BT_CHECK_PARAMETER(device_address, return);
1322 BT_CHECK_PARAMETER(alias, return);
1324 adapter_proxy = _bt_get_adapter_proxy();
1325 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1327 conn = _bt_get_system_gconn();
1328 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1330 _bt_convert_addr_type_to_string(address, device_address->addr);
1332 device_path = _bt_get_device_object_path(address);
1334 if (device_path == NULL) {
1335 BT_ERR("No paired device");
1336 return BLUETOOTH_ERROR_NOT_PAIRED;
1339 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1340 NULL, BT_BLUEZ_NAME,
1341 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1343 g_free(device_path);
1344 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1346 g_dbus_proxy_call_sync(device_proxy, "Set",
1347 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1348 G_DBUS_CALL_FLAGS_NONE,
1353 g_object_unref(device_proxy);
1356 BT_ERR("SetProperty error: [%s]", error->message);
1357 g_error_free(error);
1358 return BLUETOOTH_ERROR_INTERNAL;
1361 return BLUETOOTH_ERROR_NONE;
1364 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1367 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1368 gchar *device_path = NULL;
1369 GDBusProxy *device_proxy;
1370 gboolean previous_value;
1371 GError *error = NULL;
1372 GDBusConnection *conn;
1373 GVariant *result = NULL;
1374 int ret = BLUETOOTH_ERROR_NONE;
1376 BT_CHECK_PARAMETER(device_address, return);
1378 conn = _bt_get_system_gconn();
1379 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1381 _bt_convert_addr_type_to_string(address, device_address->addr);
1383 device_path = _bt_get_device_object_path(address);
1385 if (device_path == NULL) {
1386 BT_ERR("No paired device");
1387 return BLUETOOTH_ERROR_NOT_PAIRED;
1390 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1391 NULL, BT_BLUEZ_NAME,
1392 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1394 g_free(device_path);
1395 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1397 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1398 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted" ),
1399 G_DBUS_CALL_FLAGS_NONE,
1403 if (error != NULL) {
1404 BT_ERR("Getting property failed: [%s]\n", error->message);
1405 g_error_free(error);
1406 g_object_unref(device_proxy);
1407 return BLUETOOTH_ERROR_INTERNAL;
1410 previous_value = g_variant_get_boolean(result);
1411 g_variant_unref(result);
1412 /* If the input is same with previous value, return error. */
1413 if (previous_value == authorize) {
1414 BT_ERR("Same value: %d", previous_value);
1415 g_object_unref(device_proxy);
1416 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1420 g_dbus_proxy_call_sync(device_proxy, "Set",
1421 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
1422 G_DBUS_CALL_FLAGS_NONE,
1427 g_object_unref(device_proxy);
1429 BT_ERR("SetProperty error: [%s]", error->message);
1430 g_error_free(error);
1431 ret = BLUETOOTH_ERROR_INTERNAL;
1437 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1438 gboolean *is_connected)
1440 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1441 char *object_path = NULL;
1443 GDBusProxy *device_proxy;
1444 GError *error = NULL;
1446 GVariant *tmp_value;
1447 GDBusConnection *conn;
1448 GVariant *result = NULL;
1449 int ret = BLUETOOTH_ERROR_NONE;
1451 BT_CHECK_PARAMETER(device_address, return);
1453 conn = _bt_get_system_gconn();
1454 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1456 _bt_convert_addr_type_to_string(address, device_address->addr);
1458 object_path = _bt_get_device_object_path(address);
1459 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1461 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1462 NULL, BT_BLUEZ_NAME,
1463 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1464 g_free(object_path);
1465 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1467 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1469 G_DBUS_CALL_FLAGS_NONE,
1474 if (error != NULL || result == NULL) {
1475 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1476 g_error_free(error);
1477 g_object_unref(device_proxy);
1478 return BLUETOOTH_ERROR_INTERNAL;
1481 g_variant_get(result , "(@a{sv})", &value);
1482 g_variant_unref(result);
1484 tmp_value = g_variant_lookup_value (value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1485 if (tmp_value != NULL) {
1486 g_object_unref(device_proxy);
1487 return BLUETOOTH_ERROR_INTERNAL;
1489 *is_connected = g_variant_get_boolean (tmp_value);
1491 BT_DBG("gatt is connected : %d", *is_connected);
1492 g_variant_unref(tmp_value);
1493 g_variant_unref(value);
1494 g_object_unref(device_proxy);
1499 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1500 int connection_type, gboolean *is_connected)
1502 char *object_path = NULL;
1503 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1504 GDBusProxy *device_proxy = NULL;
1505 GDBusProxy *adapter_proxy = NULL;
1506 GDBusConnection *conn;
1507 GError *error = NULL;
1508 GVariant *tmp_value = NULL;
1509 GVariant *value = NULL;
1510 GVariant *result = NULL;
1513 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1514 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1516 *is_connected = FALSE;
1517 BT_DBG("connection_type: %d", connection_type);
1518 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1519 return _bt_rfcomm_is_device_connected(device_address,
1521 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1522 return _bt_is_gatt_connected(device_address, is_connected);
1524 adapter_proxy = _bt_get_adapter_proxy();
1525 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1527 conn = _bt_get_system_gconn();
1528 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1530 _bt_convert_addr_type_to_string(address, device_address->addr);
1532 if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1533 object_path = _bt_get_adapter_path();
1534 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1535 NULL, BT_BLUEZ_NAME,
1536 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1537 g_free(object_path);
1538 if (device_proxy == NULL) {
1539 BT_DBG("Device don't have this service");
1540 return BLUETOOTH_ERROR_INTERNAL;
1543 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1544 g_variant_new("(s)", address),
1545 G_DBUS_CALL_FLAGS_NONE,
1550 if (result == NULL) {
1551 BT_ERR("Error occured in Proxy call");
1553 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1554 g_error_free(error);
1556 g_object_unref(device_proxy);
1557 return BLUETOOTH_ERROR_INTERNAL;
1559 g_variant_get(result , "(@a{sv})", &value);
1560 g_variant_unref(result);
1563 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1564 if (tmp_value != NULL) {
1565 g_object_unref(device_proxy);
1566 return BLUETOOTH_ERROR_INTERNAL;
1568 *is_connected = g_variant_get_boolean (tmp_value);
1569 g_variant_unref(tmp_value);
1570 g_variant_unref(value);
1571 } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1572 return _bt_is_network_connected(_bt_get_net_conn(),
1573 device_address->addr, is_connected);
1575 uuid = _bt_get_profile_uuid128(connection_type);
1577 BT_ERR("uuid is NULL");
1578 return BLUETOOTH_ERROR_INTERNAL;
1581 BT_DBG("uuid: %s", uuid);
1583 object_path = _bt_get_device_object_path(address);
1584 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1585 BT_DBG("object_path: %s", object_path);
1586 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1587 NULL, BT_BLUEZ_NAME,
1588 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1589 g_free(object_path);
1590 if (device_proxy == NULL) {
1591 BT_DBG("Device don't have this service");
1593 return BLUETOOTH_ERROR_INTERNAL;
1596 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1597 g_variant_new("(s)", uuid),
1598 G_DBUS_CALL_FLAGS_NONE,
1603 if (result == NULL) {
1604 BT_ERR("Error occured in Proxy call");
1606 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1607 g_error_free(error);
1609 g_object_unref(device_proxy);
1610 return BLUETOOTH_ERROR_INTERNAL;
1612 g_variant_get(result, "(b)", is_connected);
1614 g_variant_unref(result);
1617 g_object_unref(device_proxy);
1618 return BLUETOOTH_ERROR_NONE;
1621 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1622 bluetooth_connected_link_t *connected)
1624 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1625 char *object_path = NULL;
1627 GDBusProxy *device_proxy;
1628 GError *error = NULL;
1629 GDBusConnection *conn;
1630 GVariant *tmp_value = NULL;
1631 GVariant *value = NULL;
1632 GVariant *result = NULL;
1634 int ret = BLUETOOTH_ERROR_NONE;
1636 BT_CHECK_PARAMETER(device_address, return);
1638 conn = _bt_get_system_gconn();
1639 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1641 _bt_convert_addr_type_to_string(address, device_address->addr);
1643 object_path = _bt_get_device_object_path(address);
1644 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1646 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1647 NULL, BT_BLUEZ_NAME,
1648 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1649 g_free(object_path);
1650 if (device_proxy == NULL) {
1651 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1652 return BLUETOOTH_ERROR_NONE;
1655 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1656 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1657 G_DBUS_CALL_FLAGS_NONE,
1662 if (error != NULL) {
1663 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1664 g_error_free(error);
1665 g_object_unref(device_proxy);
1666 return BLUETOOTH_ERROR_INTERNAL;
1669 g_variant_get(result , "(@a{sv})", &value);
1670 g_variant_unref(result);
1672 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1673 if (tmp_value != NULL) {
1674 g_object_unref(device_proxy);
1675 g_variant_unref(value);
1676 return BLUETOOTH_ERROR_INTERNAL;
1678 *connected = g_variant_get_boolean (tmp_value);
1679 g_variant_unref(tmp_value);
1680 g_variant_unref(value);
1681 g_object_unref(device_proxy);
1686 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1688 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1689 gchar *device_path = NULL;
1690 GError *error = NULL;
1691 GDBusProxy *device_proxy = NULL;
1692 GDBusProxy *adapter_proxy;
1693 GDBusConnection *conn;
1694 int ret = BLUETOOTH_ERROR_NONE;
1696 BT_CHECK_PARAMETER(bd_addr, return);
1698 _bt_convert_addr_type_to_string(device_address,
1699 (unsigned char *)bd_addr->addr);
1701 conn = _bt_get_system_gconn();
1702 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1704 adapter_proxy = _bt_get_adapter_proxy();
1705 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1707 device_path = _bt_get_device_object_path(device_address);
1708 if (device_path == NULL) {
1709 BT_DBG("device_path NULL");
1710 ret = BLUETOOTH_ERROR_INTERNAL;
1714 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1716 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1717 NULL, BT_BLUEZ_NAME,
1718 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1719 g_free(device_path);
1720 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1722 g_dbus_proxy_call_sync(device_proxy, "ConnectLE",
1723 g_variant_new("(b)", auto_connect),
1724 G_DBUS_CALL_FLAGS_NONE,
1730 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1731 g_error_free(error);
1732 g_object_unref(device_proxy);
1733 return BLUETOOTH_ERROR_INTERNAL;
1736 g_object_unref(device_proxy);
1741 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1743 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1744 gchar *device_path = NULL;
1745 GError *error = NULL;
1746 GDBusProxy *device_proxy = NULL;
1747 GDBusProxy *adapter_proxy;
1748 GDBusConnection *conn;
1749 int ret = BLUETOOTH_ERROR_NONE;
1751 BT_CHECK_PARAMETER(bd_addr, return);
1753 _bt_convert_addr_type_to_string(device_address,
1754 (unsigned char *)bd_addr->addr);
1756 conn = _bt_get_system_gconn();
1757 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1759 adapter_proxy = _bt_get_adapter_proxy();
1760 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1762 device_path = _bt_get_device_object_path(device_address);
1763 if (device_path == NULL) {
1764 BT_DBG("device_path NULL");
1765 ret = BLUETOOTH_ERROR_INTERNAL;
1769 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1771 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1772 NULL, BT_BLUEZ_NAME,
1773 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1774 g_free(device_path);
1775 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1777 g_dbus_proxy_call_sync(device_proxy, "DisconnectLE",
1779 G_DBUS_CALL_FLAGS_NONE,
1784 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1785 g_error_free(error);
1786 g_object_unref(device_proxy);
1787 return BLUETOOTH_ERROR_INTERNAL;
1790 g_object_unref(device_proxy);
1795 int _bt_connect_profile(char *address, char *uuid,
1796 void *cb, gpointer func_data)
1800 GDBusConnection *conn;
1801 GDBusProxy *adapter_proxy;
1802 GError *error = NULL;
1804 conn = _bt_get_system_gconn();
1805 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1807 object_path = _bt_get_device_object_path(address);
1808 if (object_path == NULL) {
1809 BT_ERR("No searched device");
1811 adapter_proxy = _bt_get_adapter_proxy();
1812 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1814 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1815 g_variant_new("(s)", address),
1816 G_DBUS_CALL_FLAGS_NONE,
1821 if (error != NULL) {
1822 BT_ERR("CreateDevice Fail: %s", error->message);
1823 g_error_free(error);
1826 object_path = _bt_get_device_object_path(address);
1828 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1830 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1831 NULL, BT_BLUEZ_NAME,
1832 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1833 g_free(object_path);
1834 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1836 g_dbus_proxy_call(proxy, "ConnectProfile",
1837 g_variant_new("(s)", uuid),
1838 G_DBUS_CALL_FLAGS_NONE,
1839 BT_MAX_DBUS_TIMEOUT,
1841 (GAsyncReadyCallback)cb,
1844 return BLUETOOTH_ERROR_NONE;
1847 int _bt_disconnect_profile(char *address, char *uuid,
1848 void *cb, gpointer func_data)
1852 GDBusConnection *conn;
1854 conn = _bt_get_system_gconn();
1855 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1857 object_path = _bt_get_device_object_path(address);
1858 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1860 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1861 NULL, BT_BLUEZ_NAME,
1862 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1863 g_free(object_path);
1864 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1866 g_dbus_proxy_call(proxy, "DisconnectProfile",
1867 g_variant_new("(s)", uuid),
1868 G_DBUS_CALL_FLAGS_NONE,
1869 BT_MAX_DBUS_TIMEOUT,
1871 (GAsyncReadyCallback)cb,
1874 return BLUETOOTH_ERROR_NONE;
1877 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1878 int low_threshold, int in_range_threshold, int high_threshold)
1880 int ret = BLUETOOTH_ERROR_NONE;
1882 GError *error = NULL;
1883 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1885 BT_CHECK_PARAMETER(bd_addr, return);
1886 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1887 bd_addr->addr[0], bd_addr->addr[1],
1888 bd_addr->addr[2], bd_addr->addr[3],
1889 bd_addr->addr[4], bd_addr->addr[5],
1891 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1892 in_range_threshold, high_threshold);
1894 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1896 proxy = _bt_get_adapter_proxy();
1897 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1899 g_dbus_proxy_call_sync(proxy, "EnableRssi",
1900 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
1901 G_DBUS_CALL_FLAGS_NONE,
1905 if (error != NULL) {
1906 BT_ERR("Dbus Call Error:[%s]", error->message);
1907 g_error_free(error);
1908 ret = BLUETOOTH_ERROR_INTERNAL;
1913 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1916 int ret = BLUETOOTH_ERROR_NONE;
1918 GError *error = NULL;
1919 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1921 BT_CHECK_PARAMETER(bd_addr, return);
1922 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1923 bd_addr->addr[0], bd_addr->addr[1],
1924 bd_addr->addr[2], bd_addr->addr[3],
1925 bd_addr->addr[4], bd_addr->addr[5],
1928 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1930 proxy = _bt_get_adapter_proxy();
1931 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1933 g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
1934 g_variant_new("(si)", address, link_type),
1935 G_DBUS_CALL_FLAGS_NONE,
1940 if (error != NULL) {
1941 BT_ERR("Dbus Call Error:[%s]", error->message);
1942 g_error_free(error);
1943 ret = BLUETOOTH_ERROR_INTERNAL;
1948 int _bt_le_conn_update(unsigned char *device_address,
1949 guint16 interval_min, guint16 interval_max,
1950 guint16 latency, guint16 time_out)
1952 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1953 gchar *device_path = NULL;
1954 GError *error = NULL;
1955 GDBusProxy *device_proxy = NULL;
1956 GDBusConnection *conn;
1958 guint32 min, max, to;
1959 guint32 min_supervision_to;
1960 int ret = BLUETOOTH_ERROR_NONE;
1964 BT_CHECK_PARAMETER(device_address, return);
1966 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1967 interval_min, interval_max, latency, time_out);
1969 if (interval_min > interval_max ||
1970 interval_min < BT_LE_CONN_INTERVAL_MIN ||
1971 interval_max > BT_LE_CONN_INTERVAL_MAX) {
1972 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1976 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1977 time_out > BT_LE_CONN_SUPER_TO_MAX) {
1978 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1982 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
1983 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1988 * The Supervision_Timeout in milliseconds shall be larger than
1989 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
1990 * where Conn_Interval_Max is given in milliseconds.
1992 min_supervision_to = (1 + latency) * interval_max * 2;
1993 if (time_out <= min_supervision_to) {
1994 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1998 _bt_convert_addr_type_to_string(address, device_address);
2000 BT_DBG("Remote device address: %s", address);
2002 device_path = _bt_get_device_object_path(address);
2004 if (device_path == NULL) {
2005 BT_DBG("device_path NULL");
2006 ret = BLUETOOTH_ERROR_INTERNAL;
2010 conn = _bt_get_system_gconn();
2012 BT_DBG("conn NULL");
2013 ret = BLUETOOTH_ERROR_INTERNAL;
2017 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2018 NULL, BT_BLUEZ_NAME,
2019 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2021 g_free(device_path);
2022 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2024 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2025 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2026 to = time_out / BT_LE_CONN_TO_SPLIT;
2028 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2029 g_variant_new("(uuuu)", min, max, latency, to),
2030 G_DBUS_CALL_FLAGS_NONE,
2035 g_object_unref(device_proxy);
2036 BT_ERR("LeConnUpdate Call Error for %s", address);
2037 if (reply == NULL) {
2039 BT_ERR("Error %s[%s]", error->message, address);
2040 g_error_free(error);
2041 return BLUETOOTH_ERROR_INTERNAL;
2044 g_variant_unref(reply);