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;
577 char *device_path = NULL;
579 BT_CHECK_PARAMETER(bonding_info, return);
580 BT_CHECK_PARAMETER(bonding_info->addr, return);
582 adapter_proxy = _bt_get_adapter_proxy();
583 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
585 result = g_dbus_proxy_call_sync(adapter_proxy, "FindDevice",
586 g_variant_new("(s)", bonding_info->addr),
587 G_DBUS_CALL_FLAGS_NONE,
592 return BLUETOOTH_ERROR_INTERNAL;
594 g_variant_get(result , "(&o)", &data);
595 device_path = g_strdup(data);
596 g_variant_unref(result);
598 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
600 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
601 g_variant_new("(o)", device_path),
602 G_DBUS_CALL_FLAGS_NONE,
609 if (result == NULL) {
611 BT_ERR("UnpairDevice Fail: %s", err->message);
613 return BLUETOOTH_ERROR_INTERNAL;
616 g_variant_unref(result);
618 return __bt_retry_bond();
621 static int __bt_cancel_and_bond(void)
623 int ret = BLUETOOTH_ERROR_NONE;
625 ret = _bt_agent_reply_cancellation();
626 if (ret != BLUETOOTH_ERROR_NONE){
627 BT_ERR("Fail to call reply cancellation");
631 return __bt_retry_bond();
635 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
638 int result = BLUETOOTH_ERROR_NONE;
640 GVariant *out_param1;
641 request_info_t *req_info;
642 bluetooth_device_info_t dev_info;
643 bt_remote_dev_info_t *remote_dev_info = NULL;
644 GVariant *manufacture_data;
647 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
648 /* Terminate ALL system popup */
649 syspopup_destroy_all();
652 g_dbus_proxy_call_finish(proxy, res, &err);
654 is_device_creating = FALSE;
656 if (bonding_info == NULL) {
658 BT_ERR("bonding_info == NULL");
664 req_info = _bt_get_request_info(bonding_info->req_id);
665 if (req_info == NULL) {
666 BT_ERR("req_info == NULL");
671 BT_ERR("Error occured in CreateBonding [%s]", err->message);
673 if (g_strrstr(err->message, "Already Exists")) {
674 BT_INFO("Existing Bond, remove and retry");
675 ret_if(__bt_remove_and_bond() == BLUETOOTH_ERROR_NONE);
677 result = BLUETOOTH_ERROR_PARING_FAILED;
678 } else if (_bt_agent_is_canceled() ||
679 g_strrstr(err->message, "Authentication Canceled")) {
680 BT_INFO("Cancelled by USER");
681 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
682 } else if (g_strrstr(err->message, "Authentication Rejected")) {
684 result = BLUETOOTH_ERROR_ACCESS_DENIED;
685 } else if (g_strrstr(err->message, "In Progress")) {
686 BT_INFO("Bond in progress, cancel and retry");
687 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
689 result = BLUETOOTH_ERROR_PARING_FAILED;
690 } else if (g_strrstr(err->message, "Authentication Failed")) {
691 BT_INFO("Authentication Failed");
692 if (bonding_info->is_autopair == TRUE) {
693 _bt_set_autopair_status_in_bonding_info(FALSE);
694 __ignore_auto_pairing_request(bonding_info->addr);
696 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
697 } else if (g_strrstr(err->message, "Page Timeout")) {
698 BT_INFO("Page Timeout");
699 /* This is the special case
700 As soon as call bluetooth_bond_device, try to cancel bonding.
701 In this case, before completing to call 'CreatePairedDevice' method
702 the procedure is stopped. So 'Cancle' error is not return.
704 result = BLUETOOTH_ERROR_HOST_DOWN;
705 } else if (g_strrstr(err->message, BT_TIMEOUT_MESSAGE)) {
706 g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
707 g_variant_new("(s)", bonding_info->addr),
708 G_DBUS_CALL_FLAGS_NONE,
713 result = BLUETOOTH_ERROR_INTERNAL;
714 } else if (g_strrstr(err->message, "Connection Timeout")) {
715 /* Pairing request timeout */
716 result = BLUETOOTH_ERROR_TIMEOUT;
717 } else if (g_strrstr(err->message, "Authentication Timeout")) {
718 /* Pairing request timeout */
719 result = BLUETOOTH_ERROR_TIMEOUT;
721 BT_DBG("Default case");
722 result = BLUETOOTH_ERROR_PARING_FAILED;
726 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
727 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
728 result == BLUETOOTH_ERROR_TIMEOUT ||
729 result == BLUETOOTH_ERROR_HOST_DOWN) {
730 bonding_info->result = result;
731 #ifdef TIZEN_WEARABLE
732 __bt_launch_unable_to_pairing_syspopup(result);
736 g_object_unref(proxy);
737 bonding_info->device_proxy = NULL;
739 if (result != BLUETOOTH_ERROR_NONE)
742 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
743 if (NULL == remote_dev_info) {
744 BT_ERR("Failed to get remote device info");
748 GVariant *uuids = NULL;
749 GVariantBuilder *builder = NULL;
751 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
752 for (i=0; i < remote_dev_info->uuid_count; i++) {
753 g_variant_builder_add(builder, "s",
754 remote_dev_info->uuids[i]);
756 uuids = g_variant_new("as", builder);
757 g_variant_builder_unref(builder);
758 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
759 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
762 param = g_variant_new("(isunsbub@asn@ay)",
765 remote_dev_info->class,
766 remote_dev_info->rssi,
767 remote_dev_info->name,
768 remote_dev_info->paired,
769 remote_dev_info->connected,
770 remote_dev_info->trust,
772 remote_dev_info->manufacturer_data_len,
776 /* Send the event to application */
777 _bt_send_event(BT_ADAPTER_EVENT,
778 BLUETOOTH_EVENT_BONDING_FINISHED,
781 _bt_free_device_info(remote_dev_info);
784 if (req_info->context == NULL)
787 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
788 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
791 if (_bt_adapter_get_status() != BT_ACTIVATED)
792 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
794 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
795 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
797 g_dbus_method_invocation_return_value(req_info->context,
798 g_variant_new("(iv)", result, out_param1));
800 g_variant_unref(out_param1);
801 // g_variant_unref(out_param2);
803 _bt_delete_request_list(req_info->req_id);
808 _bt_agent_set_canceled(FALSE);
811 g_free(bonding_info->addr);
812 g_free(bonding_info);
816 int _bt_bond_device(int request_id,
817 bluetooth_device_address_t *device_address,
818 unsigned short conn_type, GArray **out_param1)
821 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
822 bluetooth_device_info_t dev_info;
824 GDBusConnection *conn;
825 char *device_path = NULL;
826 GDBusProxy *adapter_proxy;
827 GError *error = NULL;
829 BT_CHECK_PARAMETER(device_address, return);
832 BT_ERR("Bonding in progress");
834 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
835 memcpy(dev_info.device_address.addr, device_address->addr,
836 BLUETOOTH_ADDRESS_LENGTH);
838 g_array_append_vals(*out_param1, &dev_info,
839 sizeof(bluetooth_device_info_t));
841 return BLUETOOTH_ERROR_DEVICE_BUSY;
844 conn = _bt_get_system_gconn();
845 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
847 _bt_convert_addr_type_to_string(address, device_address->addr);
849 device_path = _bt_get_device_object_path(address);
851 if (device_path == NULL) {
852 BT_ERR("No searched device");
853 GVariant *ret = NULL;
854 adapter_proxy = _bt_get_adapter_proxy();
855 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
857 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
858 g_variant_new("(s)", address),
859 G_DBUS_CALL_FLAGS_NONE,
865 BT_ERR("CreateDevice Fail: %s", error->message);
866 g_clear_error(&error);
869 g_variant_unref(ret);
870 device_path = _bt_get_device_object_path(address);
871 if (device_path == NULL) {
872 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
873 memcpy(dev_info.device_address.addr, device_address->addr,
874 BLUETOOTH_ADDRESS_LENGTH);
876 g_array_append_vals(*out_param1, &dev_info,
877 sizeof(bluetooth_device_info_t));
879 return BLUETOOTH_ERROR_NOT_PAIRED;
881 BT_INFO("device_path is created[%s]", device_path);
885 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
887 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
890 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
892 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
893 bonding_info->addr = g_strdup(address);
894 bonding_info->req_id = request_id;
896 bonding_info->device_proxy = proxy;
897 bonding_info->conn_type = conn_type;
899 is_device_creating = TRUE;
901 g_dbus_proxy_call(proxy, "Pair",
902 g_variant_new("(y)", conn_type),
903 G_DBUS_CALL_FLAGS_NONE,
906 (GAsyncReadyCallback)__bt_bond_device_cb,
909 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
911 return BLUETOOTH_ERROR_NONE;
913 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
914 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
917 g_array_append_vals(*out_param1, &dev_info,
918 sizeof(bluetooth_device_info_t));
920 is_device_creating = FALSE;
922 g_free(bonding_info->addr);
923 g_free(bonding_info);
926 return BLUETOOTH_ERROR_INTERNAL;*/
929 int _bt_cancel_bonding(void)
931 int ret = BLUETOOTH_ERROR_NONE;
933 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
935 ret = _bt_agent_reply_cancellation();
936 if (ret != BLUETOOTH_ERROR_NONE){
937 BT_ERR("Fail to call reply cancellation");
941 _bt_agent_set_canceled(TRUE);
943 return BLUETOOTH_ERROR_NONE;
946 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
950 GVariant *out_param1;
951 int result = BLUETOOTH_ERROR_NONE;
952 bt_funcion_data_t *unbonding_info;
953 bluetooth_device_info_t dev_info;
954 request_info_t *req_info;
956 g_dbus_proxy_call_finish(proxy, res, &err);
958 unbonding_info = user_data;
960 if (unbonding_info == NULL) {
962 BT_ERR("unbonding_info == NULL");
966 req_info = _bt_get_request_info(unbonding_info->req_id);
967 if (req_info == NULL) {
968 BT_ERR("req_info == NULL");
973 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
974 result = BLUETOOTH_ERROR_INTERNAL;
977 if (req_info->context == NULL)
980 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
981 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
982 unbonding_info->addr);
984 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
985 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
987 g_dbus_method_invocation_return_value(req_info->context,
988 g_variant_new("(iv)", result, out_param1));
991 _bt_delete_request_list(req_info->req_id);
997 if (unbonding_info) {
998 g_free(unbonding_info->addr);
999 g_free(unbonding_info);
1003 int _bt_unbond_device(int request_id,
1004 bluetooth_device_address_t *device_address,
1005 GArray **out_param1)
1007 char *device_path = NULL;
1008 bt_funcion_data_t *unbonding_info;
1009 GDBusProxy *adapter_proxy = NULL;
1010 GDBusProxy *device_proxy = NULL;
1011 GDBusConnection *conn;
1012 int result = BLUETOOTH_ERROR_INTERNAL;
1013 bluetooth_device_info_t dev_info;
1014 GError *error = NULL;
1015 GVariant *ret = NULL;
1017 BT_CHECK_PARAMETER(device_address, return);
1019 adapter_proxy = _bt_get_adapter_proxy();
1020 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1022 /* allocate user data so that it can be retrieved in callback */
1023 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1024 /* Fix : NULL_RETURNS */
1025 if (unbonding_info == NULL) {
1026 BT_ERR("Memory not allocated !");
1027 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1030 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1031 unbonding_info->req_id = request_id;
1033 _bt_convert_addr_type_to_string(unbonding_info->addr,
1034 device_address->addr);
1036 device_path = _bt_get_device_object_path(unbonding_info->addr);
1038 if (device_path == NULL) {
1039 BT_ERR("No paired device");
1040 result = BLUETOOTH_ERROR_NOT_PAIRED;
1044 conn = _bt_get_system_gconn();
1046 BT_ERR("conn is NULL");
1047 result = BLUETOOTH_ERROR_INTERNAL;
1051 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1052 NULL, BT_BLUEZ_NAME,
1053 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1055 if (device_proxy != NULL) {
1057 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1058 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1059 G_DBUS_CALL_FLAGS_NONE,
1064 BT_ERR("Getting property failed: [%s]\n", error->message);
1065 g_error_free(error);
1066 result = BLUETOOTH_ERROR_NOT_PAIRED;
1070 BT_ERR("No paired device");
1071 g_object_unref(device_proxy);
1072 result = BLUETOOTH_ERROR_NOT_PAIRED;
1075 g_variant_unref(ret);
1077 g_object_unref(device_proxy);
1080 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1081 g_variant_new("(o)", device_path),
1082 G_DBUS_CALL_FLAGS_NONE,
1083 BT_MAX_DBUS_TIMEOUT,
1085 (GAsyncReadyCallback)__bt_unbond_cb,
1088 g_free(device_path);
1089 return BLUETOOTH_ERROR_NONE;
1092 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1093 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1094 unbonding_info->addr);
1096 g_array_append_vals(*out_param1, &dev_info,
1097 sizeof(bluetooth_device_info_t));
1098 g_free(device_path);
1099 g_free(unbonding_info->addr);
1100 g_free(unbonding_info);
1104 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1108 GVariant *out_param1;
1109 int result = BLUETOOTH_ERROR_NONE;
1110 bluetooth_device_info_t dev_info;
1111 bt_remote_dev_info_t *remote_dev_info = NULL;
1112 request_info_t *req_info;
1113 GVariant *uuid_list, *manufacture_data;
1115 GVariantBuilder *builder = NULL;
1118 g_dbus_proxy_call_finish(proxy, res, &err);
1120 g_object_unref(proxy);
1122 if (searching_info == NULL) {
1124 BT_ERR("unbonding_info == NULL");
1128 req_info = _bt_get_request_info(searching_info->req_id);
1129 if (req_info == NULL) {
1130 BT_ERR("req_info == NULL");
1135 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1137 if (!strcmp("Operation canceled", err->message)) {
1138 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1139 } else if (!strcmp("In Progress", err->message)) {
1140 result = BLUETOOTH_ERROR_IN_PROGRESS;
1141 } else if (!strcmp("Host is down", err->message)) {
1142 result = BLUETOOTH_ERROR_HOST_DOWN;
1144 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1147 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1148 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1149 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1150 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1151 remote_dev_info->uuid_count > 0) {
1152 result = BLUETOOTH_ERROR_NONE;
1155 _bt_free_device_info(remote_dev_info);
1160 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1161 if (NULL == remote_dev_info) {
1162 BT_ERR("Failed to get remote device info");
1167 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1168 for (i=0; i < remote_dev_info->uuid_count; i++) {
1169 g_variant_builder_add(builder, "s",
1170 remote_dev_info->uuids[i]);
1172 uuid_list = g_variant_new("as", builder);
1173 g_variant_builder_unref(builder);
1174 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1175 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1178 param = g_variant_new("(isunsbub@asn@ay)",
1180 searching_info->addr,
1181 remote_dev_info->class,
1182 remote_dev_info->rssi,
1183 remote_dev_info->name,
1184 remote_dev_info->paired,
1185 remote_dev_info->connected,
1186 remote_dev_info->trust,
1188 remote_dev_info->manufacturer_data_len,
1192 /* Send the event to application */
1193 _bt_send_event(BT_ADAPTER_EVENT,
1194 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1197 _bt_free_device_info(remote_dev_info);
1200 if (req_info->context == NULL)
1203 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1204 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1205 searching_info->addr);
1207 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1208 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1210 g_dbus_method_invocation_return_value(req_info->context,
1211 g_variant_new("(iv)", result, out_param1));
1214 _bt_delete_request_list(req_info->req_id);
1219 if (searching_info) {
1220 g_free(searching_info->addr);
1221 g_free(searching_info);
1222 searching_info = NULL;
1226 int _bt_search_device(int request_id,
1227 bluetooth_device_address_t *device_address)
1229 char *device_path = NULL;
1230 GDBusProxy *device_proxy = NULL;
1231 GDBusConnection *conn;
1233 GDBusProxy *adapter_proxy;
1234 int result = BLUETOOTH_ERROR_INTERNAL;
1236 BT_CHECK_PARAMETER(device_address, return);
1239 BT_ERR("Bonding in progress");
1240 return BLUETOOTH_ERROR_DEVICE_BUSY;
1243 if (searching_info) {
1244 BT_ERR("Service searching in progress");
1245 return BLUETOOTH_ERROR_DEVICE_BUSY;
1248 adapter_proxy = _bt_get_adapter_proxy();
1249 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1251 /* allocate user data so that it can be retrieved in callback */
1252 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1253 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1254 searching_info->req_id = request_id;
1256 _bt_convert_addr_type_to_string(searching_info->addr,
1257 device_address->addr);
1259 conn = _bt_get_system_gconn();
1260 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1263 device_path = _bt_get_device_object_path(searching_info->addr);
1265 if (device_path == NULL) {
1266 BT_ERR("No paired device");
1267 result = BLUETOOTH_ERROR_NOT_PAIRED;
1271 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1272 NULL, BT_BLUEZ_NAME,
1273 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1274 g_free(device_path);
1275 if (device_proxy == NULL) {
1276 result = BLUETOOTH_ERROR_INTERNAL;
1280 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1281 g_variant_new("(s)", ""),
1282 G_DBUS_CALL_FLAGS_NONE,
1283 BT_MAX_DBUS_TIMEOUT,
1285 (GAsyncReadyCallback)__bt_discover_cb,
1288 searching_info->device_proxy = device_proxy;
1290 return BLUETOOTH_ERROR_NONE;
1293 g_object_unref(device_proxy);
1295 g_free(searching_info->addr);
1296 g_free(searching_info);
1297 searching_info = NULL;
1301 int _bt_cancel_search_device(void)
1305 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1307 if (searching_info->device_proxy) {
1308 g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1310 G_DBUS_CALL_FLAGS_NONE,
1315 __bt_cancel_search_service_done();
1317 return BLUETOOTH_ERROR_NONE;
1320 int _bt_set_alias(bluetooth_device_address_t *device_address,
1323 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1324 gchar *device_path = NULL;
1325 GDBusProxy *adapter_proxy;
1326 GDBusProxy *device_proxy;
1327 GError *error = NULL;
1328 GDBusConnection *conn;
1330 BT_CHECK_PARAMETER(device_address, return);
1331 BT_CHECK_PARAMETER(alias, return);
1333 adapter_proxy = _bt_get_adapter_proxy();
1334 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1336 conn = _bt_get_system_gconn();
1337 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1339 _bt_convert_addr_type_to_string(address, device_address->addr);
1341 device_path = _bt_get_device_object_path(address);
1343 if (device_path == NULL) {
1344 BT_ERR("No paired device");
1345 return BLUETOOTH_ERROR_NOT_PAIRED;
1348 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1349 NULL, BT_BLUEZ_NAME,
1350 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1352 g_free(device_path);
1353 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1355 g_dbus_proxy_call_sync(device_proxy, "Set",
1356 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1357 G_DBUS_CALL_FLAGS_NONE,
1362 g_object_unref(device_proxy);
1365 BT_ERR("SetProperty error: [%s]", error->message);
1366 g_error_free(error);
1367 return BLUETOOTH_ERROR_INTERNAL;
1370 return BLUETOOTH_ERROR_NONE;
1373 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1376 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1377 gchar *device_path = NULL;
1378 GDBusProxy *device_proxy;
1379 gboolean previous_value;
1380 GError *error = NULL;
1381 GDBusConnection *conn;
1382 GVariant *result = NULL;
1383 int ret = BLUETOOTH_ERROR_NONE;
1385 BT_CHECK_PARAMETER(device_address, return);
1387 conn = _bt_get_system_gconn();
1388 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1390 _bt_convert_addr_type_to_string(address, device_address->addr);
1392 device_path = _bt_get_device_object_path(address);
1394 if (device_path == NULL) {
1395 BT_ERR("No paired device");
1396 return BLUETOOTH_ERROR_NOT_PAIRED;
1399 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1400 NULL, BT_BLUEZ_NAME,
1401 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1403 g_free(device_path);
1404 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1406 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1407 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted" ),
1408 G_DBUS_CALL_FLAGS_NONE,
1412 if (error != NULL) {
1413 BT_ERR("Getting property failed: [%s]\n", error->message);
1414 g_error_free(error);
1415 g_object_unref(device_proxy);
1416 return BLUETOOTH_ERROR_INTERNAL;
1419 previous_value = g_variant_get_boolean(result);
1420 g_variant_unref(result);
1421 /* If the input is same with previous value, return error. */
1422 if (previous_value == authorize) {
1423 BT_ERR("Same value: %d", previous_value);
1424 g_object_unref(device_proxy);
1425 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1429 g_dbus_proxy_call_sync(device_proxy, "Set",
1430 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
1431 G_DBUS_CALL_FLAGS_NONE,
1436 g_object_unref(device_proxy);
1438 BT_ERR("SetProperty error: [%s]", error->message);
1439 g_error_free(error);
1440 ret = BLUETOOTH_ERROR_INTERNAL;
1446 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1447 gboolean *is_connected)
1449 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1450 char *object_path = NULL;
1452 GDBusProxy *device_proxy;
1453 GError *error = NULL;
1455 GVariant *tmp_value;
1456 GDBusConnection *conn;
1457 GVariant *result = NULL;
1458 int ret = BLUETOOTH_ERROR_NONE;
1460 BT_CHECK_PARAMETER(device_address, return);
1462 conn = _bt_get_system_gconn();
1463 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1465 _bt_convert_addr_type_to_string(address, device_address->addr);
1467 object_path = _bt_get_device_object_path(address);
1468 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1470 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1471 NULL, BT_BLUEZ_NAME,
1472 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1473 g_free(object_path);
1474 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1476 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1478 G_DBUS_CALL_FLAGS_NONE,
1483 if (error != NULL || result == NULL) {
1484 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1485 g_error_free(error);
1486 g_object_unref(device_proxy);
1487 return BLUETOOTH_ERROR_INTERNAL;
1490 g_variant_get(result , "(@a{sv})", &value);
1491 g_variant_unref(result);
1493 tmp_value = g_variant_lookup_value (value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1494 if (tmp_value != NULL) {
1495 g_object_unref(device_proxy);
1496 return BLUETOOTH_ERROR_INTERNAL;
1498 *is_connected = g_variant_get_boolean (tmp_value);
1500 BT_DBG("gatt is connected : %d", *is_connected);
1501 g_variant_unref(tmp_value);
1502 g_variant_unref(value);
1503 g_object_unref(device_proxy);
1508 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1509 int connection_type, gboolean *is_connected)
1511 char *object_path = NULL;
1512 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1513 GDBusProxy *device_proxy = NULL;
1514 GDBusProxy *adapter_proxy = NULL;
1515 GDBusConnection *conn;
1516 GError *error = NULL;
1517 GVariant *tmp_value = NULL;
1518 GVariant *value = NULL;
1519 GVariant *result = NULL;
1522 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1523 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1525 *is_connected = FALSE;
1526 BT_DBG("connection_type: %d", connection_type);
1527 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1528 return _bt_rfcomm_is_device_connected(device_address,
1530 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1531 return _bt_is_gatt_connected(device_address, is_connected);
1533 adapter_proxy = _bt_get_adapter_proxy();
1534 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1536 conn = _bt_get_system_gconn();
1537 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1539 _bt_convert_addr_type_to_string(address, device_address->addr);
1541 if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1542 object_path = _bt_get_adapter_path();
1543 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1544 NULL, BT_BLUEZ_NAME,
1545 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1546 g_free(object_path);
1547 if (device_proxy == NULL) {
1548 BT_DBG("Device don't have this service");
1549 return BLUETOOTH_ERROR_INTERNAL;
1552 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1553 g_variant_new("(s)", address),
1554 G_DBUS_CALL_FLAGS_NONE,
1559 if (result == NULL) {
1560 BT_ERR("Error occured in Proxy call");
1562 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1563 g_error_free(error);
1565 g_object_unref(device_proxy);
1566 return BLUETOOTH_ERROR_INTERNAL;
1568 g_variant_get(result , "(@a{sv})", &value);
1569 g_variant_unref(result);
1572 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1573 if (tmp_value != NULL) {
1574 g_object_unref(device_proxy);
1575 return BLUETOOTH_ERROR_INTERNAL;
1577 *is_connected = g_variant_get_boolean (tmp_value);
1578 g_variant_unref(tmp_value);
1579 g_variant_unref(value);
1580 } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1581 return _bt_is_network_connected(_bt_get_net_conn(),
1582 device_address->addr, is_connected);
1584 uuid = _bt_get_profile_uuid128(connection_type);
1586 BT_ERR("uuid is NULL");
1587 return BLUETOOTH_ERROR_INTERNAL;
1590 BT_DBG("uuid: %s", uuid);
1592 object_path = _bt_get_device_object_path(address);
1593 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1594 BT_DBG("object_path: %s", object_path);
1595 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1596 NULL, BT_BLUEZ_NAME,
1597 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1598 g_free(object_path);
1599 if (device_proxy == NULL) {
1600 BT_DBG("Device don't have this service");
1602 return BLUETOOTH_ERROR_INTERNAL;
1605 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1606 g_variant_new("(s)", uuid),
1607 G_DBUS_CALL_FLAGS_NONE,
1612 if (result == NULL) {
1613 BT_ERR("Error occured in Proxy call");
1615 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1616 g_error_free(error);
1618 g_object_unref(device_proxy);
1619 return BLUETOOTH_ERROR_INTERNAL;
1621 g_variant_get(result, "(b)", is_connected);
1623 g_variant_unref(result);
1626 g_object_unref(device_proxy);
1627 return BLUETOOTH_ERROR_NONE;
1630 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1631 bluetooth_connected_link_t *connected)
1633 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1634 char *object_path = NULL;
1636 GDBusProxy *device_proxy;
1637 GError *error = NULL;
1638 GDBusConnection *conn;
1639 GVariant *tmp_value = NULL;
1640 GVariant *value = NULL;
1641 GVariant *result = NULL;
1643 int ret = BLUETOOTH_ERROR_NONE;
1645 BT_CHECK_PARAMETER(device_address, return);
1647 conn = _bt_get_system_gconn();
1648 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1650 _bt_convert_addr_type_to_string(address, device_address->addr);
1652 object_path = _bt_get_device_object_path(address);
1653 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1655 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1656 NULL, BT_BLUEZ_NAME,
1657 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1658 g_free(object_path);
1659 if (device_proxy == NULL) {
1660 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1661 return BLUETOOTH_ERROR_NONE;
1664 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1665 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1666 G_DBUS_CALL_FLAGS_NONE,
1671 if (error != NULL) {
1672 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1673 g_error_free(error);
1674 g_object_unref(device_proxy);
1675 return BLUETOOTH_ERROR_INTERNAL;
1678 g_variant_get(result , "(@a{sv})", &value);
1679 g_variant_unref(result);
1681 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1682 if (tmp_value != NULL) {
1683 g_object_unref(device_proxy);
1684 g_variant_unref(value);
1685 return BLUETOOTH_ERROR_INTERNAL;
1687 *connected = g_variant_get_boolean (tmp_value);
1688 g_variant_unref(tmp_value);
1689 g_variant_unref(value);
1690 g_object_unref(device_proxy);
1695 int _bt_connect_le_device(const bluetooth_device_address_t *bd_addr, gboolean auto_connect)
1697 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1698 gchar *device_path = NULL;
1699 GError *error = NULL;
1700 GDBusProxy *device_proxy = NULL;
1701 GDBusProxy *adapter_proxy;
1702 GDBusConnection *conn;
1703 int ret = BLUETOOTH_ERROR_NONE;
1705 BT_CHECK_PARAMETER(bd_addr, return);
1707 _bt_convert_addr_type_to_string(device_address,
1708 (unsigned char *)bd_addr->addr);
1710 conn = _bt_get_system_gconn();
1711 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1713 adapter_proxy = _bt_get_adapter_proxy();
1714 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1716 device_path = _bt_get_device_object_path(device_address);
1717 if (device_path == NULL) {
1718 BT_DBG("device_path NULL");
1719 ret = BLUETOOTH_ERROR_INTERNAL;
1723 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1725 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1726 NULL, BT_BLUEZ_NAME,
1727 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1728 g_free(device_path);
1729 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1731 g_dbus_proxy_call_sync(device_proxy, "ConnectLE",
1732 g_variant_new("(b)", auto_connect),
1733 G_DBUS_CALL_FLAGS_NONE,
1739 BT_ERR("ConnectLE Call Error %s[%s]", error->message, device_address);
1740 g_error_free(error);
1741 g_object_unref(device_proxy);
1742 return BLUETOOTH_ERROR_INTERNAL;
1745 g_object_unref(device_proxy);
1750 int _bt_disconnect_le_device(const bluetooth_device_address_t *bd_addr)
1752 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1753 gchar *device_path = NULL;
1754 GError *error = NULL;
1755 GDBusProxy *device_proxy = NULL;
1756 GDBusProxy *adapter_proxy;
1757 GDBusConnection *conn;
1758 int ret = BLUETOOTH_ERROR_NONE;
1760 BT_CHECK_PARAMETER(bd_addr, return);
1762 _bt_convert_addr_type_to_string(device_address,
1763 (unsigned char *)bd_addr->addr);
1765 conn = _bt_get_system_gconn();
1766 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1768 adapter_proxy = _bt_get_adapter_proxy();
1769 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1771 device_path = _bt_get_device_object_path(device_address);
1772 if (device_path == NULL) {
1773 BT_DBG("device_path NULL");
1774 ret = BLUETOOTH_ERROR_INTERNAL;
1778 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1780 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1781 NULL, BT_BLUEZ_NAME,
1782 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1783 g_free(device_path);
1784 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1786 g_dbus_proxy_call_sync(device_proxy, "DisconnectLE",
1788 G_DBUS_CALL_FLAGS_NONE,
1793 BT_ERR("DisconnectLE Call Error %s[%s]", error->message, device_address);
1794 g_error_free(error);
1795 g_object_unref(device_proxy);
1796 return BLUETOOTH_ERROR_INTERNAL;
1799 g_object_unref(device_proxy);
1804 int _bt_connect_profile(char *address, char *uuid,
1805 void *cb, gpointer func_data)
1809 GDBusConnection *conn;
1810 GDBusProxy *adapter_proxy;
1811 GError *error = NULL;
1813 conn = _bt_get_system_gconn();
1814 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1816 object_path = _bt_get_device_object_path(address);
1817 if (object_path == NULL) {
1818 BT_ERR("No searched device");
1820 adapter_proxy = _bt_get_adapter_proxy();
1821 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1823 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
1824 g_variant_new("(s)", address),
1825 G_DBUS_CALL_FLAGS_NONE,
1830 if (error != NULL) {
1831 BT_ERR("CreateDevice Fail: %s", error->message);
1832 g_error_free(error);
1835 object_path = _bt_get_device_object_path(address);
1837 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1839 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1840 NULL, BT_BLUEZ_NAME,
1841 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1842 g_free(object_path);
1843 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1845 g_dbus_proxy_call(proxy, "ConnectProfile",
1846 g_variant_new("(s)", uuid),
1847 G_DBUS_CALL_FLAGS_NONE,
1848 BT_MAX_DBUS_TIMEOUT,
1850 (GAsyncReadyCallback)cb,
1853 return BLUETOOTH_ERROR_NONE;
1856 int _bt_disconnect_profile(char *address, char *uuid,
1857 void *cb, gpointer func_data)
1861 GDBusConnection *conn;
1863 conn = _bt_get_system_gconn();
1864 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1866 object_path = _bt_get_device_object_path(address);
1867 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1869 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1870 NULL, BT_BLUEZ_NAME,
1871 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1872 g_free(object_path);
1873 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1875 g_dbus_proxy_call(proxy, "DisconnectProfile",
1876 g_variant_new("(s)", uuid),
1877 G_DBUS_CALL_FLAGS_NONE,
1878 BT_MAX_DBUS_TIMEOUT,
1880 (GAsyncReadyCallback)cb,
1883 return BLUETOOTH_ERROR_NONE;
1886 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
1887 int low_threshold, int in_range_threshold, int high_threshold)
1889 int ret = BLUETOOTH_ERROR_NONE;
1891 GError *error = NULL;
1892 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1894 BT_CHECK_PARAMETER(bd_addr, return);
1895 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
1896 bd_addr->addr[0], bd_addr->addr[1],
1897 bd_addr->addr[2], bd_addr->addr[3],
1898 bd_addr->addr[4], bd_addr->addr[5],
1900 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
1901 in_range_threshold, high_threshold);
1903 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1905 proxy = _bt_get_adapter_proxy();
1906 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1908 g_dbus_proxy_call_sync(proxy, "EnableRssi",
1909 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
1910 G_DBUS_CALL_FLAGS_NONE,
1914 if (error != NULL) {
1915 BT_ERR("Dbus Call Error:[%s]", error->message);
1916 g_error_free(error);
1917 ret = BLUETOOTH_ERROR_INTERNAL;
1922 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
1925 int ret = BLUETOOTH_ERROR_NONE;
1927 GError *error = NULL;
1928 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1930 BT_CHECK_PARAMETER(bd_addr, return);
1931 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
1932 bd_addr->addr[0], bd_addr->addr[1],
1933 bd_addr->addr[2], bd_addr->addr[3],
1934 bd_addr->addr[4], bd_addr->addr[5],
1937 _bt_convert_addr_type_to_string(address, bd_addr->addr);
1939 proxy = _bt_get_adapter_proxy();
1940 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1942 g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
1943 g_variant_new("(si)", address, link_type),
1944 G_DBUS_CALL_FLAGS_NONE,
1949 if (error != NULL) {
1950 BT_ERR("Dbus Call Error:[%s]", error->message);
1951 g_error_free(error);
1952 ret = BLUETOOTH_ERROR_INTERNAL;
1957 int _bt_le_conn_update(unsigned char *device_address,
1958 guint16 interval_min, guint16 interval_max,
1959 guint16 latency, guint16 time_out)
1961 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1962 gchar *device_path = NULL;
1963 GError *error = NULL;
1964 GDBusProxy *device_proxy = NULL;
1965 GDBusConnection *conn;
1967 guint32 min, max, to;
1968 guint32 min_supervision_to;
1969 int ret = BLUETOOTH_ERROR_NONE;
1973 BT_CHECK_PARAMETER(device_address, return);
1975 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
1976 interval_min, interval_max, latency, time_out);
1978 if (interval_min > interval_max ||
1979 interval_min < BT_LE_CONN_INTERVAL_MIN ||
1980 interval_max > BT_LE_CONN_INTERVAL_MAX) {
1981 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1985 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
1986 time_out > BT_LE_CONN_SUPER_TO_MAX) {
1987 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1991 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
1992 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1997 * The Supervision_Timeout in milliseconds shall be larger than
1998 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
1999 * where Conn_Interval_Max is given in milliseconds.
2001 min_supervision_to = (1 + latency) * interval_max * 2;
2002 if (time_out <= min_supervision_to) {
2003 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2007 _bt_convert_addr_type_to_string(address, device_address);
2009 BT_DBG("Remote device address: %s", address);
2011 device_path = _bt_get_device_object_path(address);
2013 if (device_path == NULL) {
2014 BT_DBG("device_path NULL");
2015 ret = BLUETOOTH_ERROR_INTERNAL;
2019 conn = _bt_get_system_gconn();
2021 BT_DBG("conn NULL");
2022 ret = BLUETOOTH_ERROR_INTERNAL;
2026 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2027 NULL, BT_BLUEZ_NAME,
2028 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2030 g_free(device_path);
2031 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2033 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2034 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2035 to = time_out / BT_LE_CONN_TO_SPLIT;
2037 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2038 g_variant_new("(uuuu)", min, max, latency, to),
2039 G_DBUS_CALL_FLAGS_NONE,
2044 g_object_unref(device_proxy);
2045 BT_ERR("LeConnUpdate Call Error for %s", address);
2046 if (reply == NULL) {
2048 BT_ERR("Error %s[%s]", error->message, address);
2049 g_error_free(error);
2050 return BLUETOOTH_ERROR_INTERNAL;
2053 g_variant_unref(reply);