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>
31 #include <bundle_internal.h>
33 #include "bluetooth-api.h"
34 #include "bt-internal-types.h"
36 #include "bt-service-common.h"
37 #include "bt-service-event.h"
38 #include "bt-service-device.h"
39 #include "bt-service-adapter-le.h"
40 #include "bt-service-rfcomm-client.h"
41 #include "bt-service-util.h"
42 #include "bt-service-agent.h"
43 #include "bt-service-network.h"
44 #include "bt-service-adapter.h"
45 #include "bt-service-gap-agent.h"
47 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
48 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
49 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
51 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
52 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
53 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
54 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
55 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
56 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
57 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
58 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
60 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 20000 /* msec */
62 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
63 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
64 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
66 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 7.5 /* msec */
67 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 10 /* msec */
68 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
70 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
71 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
72 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
79 GDBusProxy *device_proxy;
80 GDBusProxy *adapter_proxy;
82 unsigned short conn_type;
90 gboolean is_device_creating;
91 bt_funcion_data_t *bonding_info;
92 bt_funcion_data_t *searching_info;
93 bt_funcion_data_t *le_connection_info;
95 static GSList *pin_info_list = NULL;
97 /* This HID Mouse does not support pairing precedure. need to skip it. */
98 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
100 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
103 static int __bt_retry_bond(void);
106 /*static void __bt_decline_pair_request()
108 GVariant *out_param1;
109 GVariant *out_param2;
110 request_info_t *req_info;
111 bluetooth_device_info_t dev_info;
112 bt_remote_dev_info_t *remote_dev_info;
113 GVariant *uuid_list, *manufacture_data;
118 req_info = _bt_get_request_info(bonding_info->req_id);
119 if (req_info == NULL) {
120 BT_ERR("req_info == NULL");
123 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
125 BT_DBG("bonding_info is NULL");
130 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
131 remote_dev_info->uuids, remote_dev_info->uuid_count,
134 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
135 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
138 param = g_variant_new("isunsbuba{s}na{y})",
139 bonding_info->result,
141 remote_dev_info->class,
142 remote_dev_info->rssi,
143 remote_dev_info->name,
144 remote_dev_info->paired,
145 remote_dev_info->connected,
146 remote_dev_info->trust,
148 remote_dev_info->manufacturer_data_len,
152 //Send the event to application
153 if (remote_dev_info != NULL) {
154 _bt_send_event(BT_ADAPTER_EVENT,
155 BLUETOOTH_EVENT_BONDING_FINISHED,
158 _bt_free_device_info(remote_dev_info);
161 if (req_info->context == NULL)
164 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
165 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
168 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
169 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
171 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
172 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
175 g_dbus_method_invocation_return_value(req_info->context,
176 g_variant_new("@ayi", out_param1, out_param2));
179 _bt_delete_request_list(req_info->req_id);
182 g_free(bonding_info->addr);
183 g_free(bonding_info);
189 #ifdef TIZEN_WEARABLE
190 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
194 retv_if(user_data == NULL, FALSE);
196 b = (bundle *)user_data;
198 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
199 ret = syspopup_launch("bt-syspopup", b);
202 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
204 BT_DBG("Hurray!!! Finally Popup launched");
207 return (ret < 0) ? TRUE : FALSE;
210 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
215 GDBusConnection *conn;
217 conn = _bt_get_system_gconn();
225 bundle_add(b, "event-type", "unable-to-pairing");
227 if (result == BLUETOOTH_ERROR_TIMEOUT )
228 bundle_add(b, "error", "timeout");
229 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
230 bundle_add(b, "error", "authfailed");
232 bundle_add(b, "error", "error");
234 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
235 ret = syspopup_launch("bt-syspopup", b);
238 BT_ERR("Popup launch failed...retry %d \n", ret);
239 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
250 gboolean _bt_is_device_creating(void)
252 return is_device_creating;
255 gboolean _bt_is_bonding_device_address(const char *address)
257 if (bonding_info == NULL || bonding_info->addr == NULL)
260 if (g_strcmp0(bonding_info->addr, address) == 0) {
261 BT_DBG("[%s] is bonding device", address);
265 BT_DBG("[%s] is NOT bonding device", address);
269 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
271 ret_if(bonding_info == NULL);
272 bonding_info->is_autopair = is_autopair;
275 void _bt_device_path_to_address(const char *device_path,
276 char *device_address)
278 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
282 ret_if(device_path == NULL);
283 ret_if(device_address == NULL);
285 dev_addr = strstr(device_path, "dev_");
286 ret_if(dev_addr == NULL);
289 g_strlcpy(address, dev_addr, sizeof(address));
291 while ((pos = strchr(address, '_')) != NULL) {
295 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
298 void __bt_cancel_search_service_done(void)
300 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
301 request_info_t *req_info;
302 bluetooth_device_info_t dev_info;
303 GVariant *out_param1;
305 ret_if(searching_info == NULL);
307 req_info = _bt_get_request_info(searching_info->req_id);
308 if (req_info == NULL) {
309 BT_ERR("req_info == NULL");
313 if (req_info->context == NULL)
316 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
317 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
318 searching_info->addr);
320 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
321 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
324 g_dbus_method_invocation_return_value(req_info->context,
325 g_variant_new("(iv)", result, out_param1));
327 _bt_delete_request_list(req_info->req_id);
331 g_free(searching_info->addr);
332 g_free(searching_info);
333 searching_info = NULL;
336 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
338 ret_if(value == NULL);
339 ret_if(info == NULL);
341 info->uuid_count = g_variant_get_size(value);
342 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
345 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
347 bt_remote_dev_info_t *dev_info;
348 char *object_path = NULL;
349 GDBusProxy *adapter_proxy;
350 GDBusProxy *device_proxy;
354 gchar *manufacturer_data = NULL;
355 GDBusConnection *conn;
356 GError *error = NULL;
357 GVariant *result = NULL;
359 retv_if(address == NULL, NULL);
361 adapter_proxy = _bt_get_adapter_proxy();
362 retv_if(adapter_proxy == NULL, NULL);
365 object_path = _bt_get_device_object_path(address);
367 retv_if(object_path == NULL, NULL);
369 conn = _bt_get_system_gconn();
371 BT_ERR("conn == NULL");
376 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
378 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
381 retv_if(device_proxy == NULL, NULL);
383 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
384 g_variant_new("(s)", BT_DEVICE_INTERFACE),
385 G_DBUS_CALL_FLAGS_NONE,
390 g_object_unref(device_proxy);
392 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
394 if (result != NULL) {
395 g_variant_get(result , "(@a{sv})", &value);
396 g_variant_unref(result);
398 tmp_value = g_variant_lookup_value (value, "Alias", G_VARIANT_TYPE_STRING);
400 g_variant_get(tmp_value, "s", &name);
401 g_variant_unref(tmp_value);
403 DBG_SECURE("Alias Name [%s]", name);
405 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
406 g_variant_get(tmp_value, "s", &name);
407 g_variant_unref(tmp_value);
410 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
411 dev_info->class = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
412 g_variant_unref(tmp_value);
414 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BOOLEAN);
415 dev_info->connected = tmp_value ? g_variant_get_boolean(tmp_value) : BLUETOOTH_CONNECTED_LINK_NONE;
416 g_variant_unref(tmp_value);
418 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
419 dev_info->trust = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
420 g_variant_unref(tmp_value);
422 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
423 dev_info->paired = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
424 g_variant_unref(tmp_value);
426 BT_DBG("Paired %d", dev_info->paired );
428 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT32);
429 dev_info->rssi = tmp_value ? g_variant_get_int32(tmp_value) : 0;
430 g_variant_unref(tmp_value);
432 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_UINT32);
433 dev_info->addr_type = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
434 g_variant_unref(tmp_value);
436 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
437 __bt_get_uuids(tmp_value, dev_info);
438 g_variant_unref(tmp_value);
440 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT32);
441 dev_info->manufacturer_data_len = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
442 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
443 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
444 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
446 g_variant_unref(tmp_value);
448 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_BYTESTRING);
449 manufacturer_data = value ? (gchar *)g_variant_get_bytestring(tmp_value) : NULL;
450 if (manufacturer_data) {
451 if (dev_info->manufacturer_data_len > 0) {
452 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
453 memcpy(dev_info->manufacturer_data, manufacturer_data, dev_info->manufacturer_data_len);
456 g_variant_unref(tmp_value);
458 dev_info->address = g_strdup(address);
459 dev_info->name = g_strdup(name);
461 g_variant_unref(value);
463 BT_ERR("result is NULL\n");
471 char *_bt_get_bonded_device_name(char *address)
473 bluetooth_device_address_t device_address = { {0} };
474 bluetooth_device_info_t dev_info;
476 retv_if(address == NULL, strdup(""));
478 _bt_convert_addr_string_to_type(device_address.addr, address);
480 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
482 _bt_get_bonded_device_info(&device_address, &dev_info);
484 return g_strdup(dev_info.device_name.name);
487 static gboolean __ignore_auto_pairing_request(const char *address)
492 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
503 /* Get the LAP(Lower Address part) */
504 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
506 snprintf(lap_address, sizeof(lap_address), ",%s", address);
508 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
511 BT_ERR("fopen failed \n");
515 fseek(fp, 0, SEEK_END);
520 BT_ERR("Get file size failed \n");
525 buffer = g_malloc0(sizeof(char) * size);
526 /* Fix : NULL_RETURNS */
527 if (buffer == NULL) {
528 BT_ERR("Memory allocation error\n");
532 result = fread((char *)buffer, 1, size, fp);
534 if (result != size) {
535 BT_ERR("Read Error\n");
540 BT_DBG("Buffer = %s\n", buffer);
542 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
548 /* Write the data and insert new device data */
549 for (i = 0; lines[i] != NULL; i++) {
550 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
551 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
553 lines[i] = temp_buffer;
556 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
558 /* Fix : NULL_RETURNS */
559 retv_if(buffer == NULL, FALSE);
561 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
564 BT_ERR("fopen failed \n");
569 BT_DBG("Buffer = %s\n", buffer);
570 fwrite(buffer, 1, strlen(buffer), fp);
580 static int __bt_retry_bond(void)
582 BT_CHECK_PARAMETER(bonding_info, return);
583 BT_CHECK_PARAMETER(bonding_info->addr, return);
585 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
586 g_variant_new("(y)", bonding_info->conn_type),
587 G_DBUS_CALL_FLAGS_NONE,
590 (GAsyncReadyCallback)__bt_bond_device_cb,
593 return BLUETOOTH_ERROR_NONE;
597 static int __bt_remove_and_bond(char *device_path)
599 GDBusProxy *adapter_proxy;
600 GVariant *result = NULL;
603 BT_CHECK_PARAMETER(bonding_info, return);
604 BT_CHECK_PARAMETER(bonding_info->addr, return);
606 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
608 adapter_proxy = _bt_get_adapter_proxy();
609 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
611 result = g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
612 g_variant_new("(o)", device_path),
613 G_DBUS_CALL_FLAGS_NONE,
619 BT_ERR("UnpairDevice Fail: %s", err->message);
621 return BLUETOOTH_ERROR_INTERNAL;
624 return __bt_retry_bond();
627 static int __bt_cancel_and_bond(void)
629 int ret = BLUETOOTH_ERROR_NONE;
631 ret = _bt_agent_reply_cancellation();
632 if (ret != BLUETOOTH_ERROR_NONE){
633 BT_ERR("Fail to call reply cancellation");
637 return __bt_retry_bond();
641 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
644 int result = BLUETOOTH_ERROR_NONE;
646 GVariant *out_param1;
647 request_info_t *req_info;
648 bluetooth_device_info_t dev_info;
649 bt_remote_dev_info_t *remote_dev_info;
650 GVariant *manufacture_data;
652 const char *device_path;
654 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
655 /* Terminate ALL system popup */
656 syspopup_destroy_all();
659 g_dbus_proxy_call_finish(proxy, res, &err);
661 is_device_creating = FALSE;
663 if (bonding_info == NULL) {
665 BT_ERR("bonding_info == NULL");
671 req_info = _bt_get_request_info(bonding_info->req_id);
672 if (req_info == NULL) {
673 BT_ERR("req_info == NULL");
677 device_path = g_dbus_proxy_get_object_path(proxy);
679 BT_DBG("device path: %s", device_path);
682 g_dbus_error_strip_remote_error(err);
683 BT_ERR("Error occured in CreateBonding [%s]", err->message);
685 if (g_strrstr(err->message, "Already Exists")) {
686 BT_INFO("Existing Bond, remove and retry");
687 ret_if(__bt_remove_and_bond(device_path) == BLUETOOTH_ERROR_NONE);
689 BT_INFO("Fail to retry bonding");
691 result = BLUETOOTH_ERROR_PARING_FAILED;
692 } else if (_bt_agent_is_canceled() ||
693 g_strrstr(err->message, "Authentication Canceled")) {
694 BT_INFO("Cancelled by USER");
695 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
696 } else if (g_strrstr(err->message, "Authentication Rejected")) {
698 result = BLUETOOTH_ERROR_ACCESS_DENIED;
699 } else if (g_strrstr(err->message, "In Progress")) {
700 BT_INFO("Bond in progress, cancel and retry");
701 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
703 result = BLUETOOTH_ERROR_PARING_FAILED;
704 } else if (g_strrstr(err->message, "Authentication Failed")) {
705 BT_INFO("Authentication Failed");
706 if (bonding_info->is_autopair == TRUE) {
707 _bt_set_autopair_status_in_bonding_info(FALSE);
708 __ignore_auto_pairing_request(bonding_info->addr);
710 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
711 } else if (g_strrstr(err->message, "Page Timeout")) {
712 BT_INFO("Page Timeout");
713 /* This is the special case
714 As soon as call bluetooth_bond_device, try to cancel bonding.
715 In this case, before completing to call 'CreatePairedDevice' method
716 the procedure is stopped. So 'Cancle' error is not return.
718 result = BLUETOOTH_ERROR_HOST_DOWN;
719 } else if (g_strrstr(err->message, BT_TIMEOUT_MESSAGE)) {
720 g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
721 g_variant_new("(s)", bonding_info->addr),
722 G_DBUS_CALL_FLAGS_NONE,
727 result = BLUETOOTH_ERROR_INTERNAL;
728 } else if (g_strrstr(err->message, "Connection Timeout")) {
729 /* Pairing request timeout */
730 result = BLUETOOTH_ERROR_TIMEOUT;
731 } else if (g_strrstr(err->message, "Authentication Timeout")) {
732 /* Pairing request timeout */
733 result = BLUETOOTH_ERROR_TIMEOUT;
735 BT_DBG("Default case");
736 result = BLUETOOTH_ERROR_PARING_FAILED;
740 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
741 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
742 result == BLUETOOTH_ERROR_TIMEOUT ||
743 result == BLUETOOTH_ERROR_HOST_DOWN) {
744 bonding_info->result = result;
745 #ifdef TIZEN_WEARABLE
746 __bt_launch_unable_to_pairing_syspopup(result);
750 g_object_unref(proxy);
751 bonding_info->device_proxy = NULL;
753 if (result != BLUETOOTH_ERROR_NONE)
756 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
757 if (remote_dev_info == NULL)
760 GVariant *uuids = NULL;
761 GVariantBuilder *builder = NULL;
763 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
764 for (i=0; i < remote_dev_info->uuid_count; i++) {
765 g_variant_builder_add(builder, "s",
766 remote_dev_info->uuids[i]);
768 uuids = g_variant_new("as", builder);
769 g_variant_builder_unref(builder);
770 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
771 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
774 param = g_variant_new("(isunsbub@asn@ay)",
777 remote_dev_info->class,
778 remote_dev_info->rssi,
779 remote_dev_info->name,
780 remote_dev_info->paired,
781 remote_dev_info->connected,
782 remote_dev_info->trust,
784 remote_dev_info->manufacturer_data_len,
788 /* Send the event to application */
789 _bt_send_event(BT_ADAPTER_EVENT,
790 BLUETOOTH_EVENT_BONDING_FINISHED,
793 _bt_free_device_info(remote_dev_info);
796 if (req_info->context == NULL)
799 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
800 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
803 if (_bt_adapter_get_status() != BT_ACTIVATED)
804 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
806 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
807 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
809 g_dbus_method_invocation_return_value(req_info->context,
810 g_variant_new("(iv)", result, out_param1));
812 g_variant_unref(out_param1);
813 // g_variant_unref(out_param2);
815 _bt_delete_request_list(req_info->req_id);
820 _bt_agent_set_canceled(FALSE);
823 g_free(bonding_info->addr);
824 g_free(bonding_info);
828 int _bt_bond_device(int request_id,
829 bluetooth_device_address_t *device_address,
830 unsigned short conn_type, GArray **out_param1)
833 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
834 bluetooth_device_info_t dev_info;
836 GDBusConnection *conn;
837 char *device_path = NULL;
838 GDBusProxy *adapter_proxy;
839 GError *error = NULL;
841 BT_CHECK_PARAMETER(device_address, return);
844 BT_ERR("Bonding in progress");
846 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
847 memcpy(dev_info.device_address.addr, device_address->addr,
848 BLUETOOTH_ADDRESS_LENGTH);
850 g_array_append_vals(*out_param1, &dev_info,
851 sizeof(bluetooth_device_info_t));
853 return BLUETOOTH_ERROR_DEVICE_BUSY;
856 conn = _bt_get_system_gconn();
857 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
859 _bt_convert_addr_type_to_string(address, device_address->addr);
861 device_path = _bt_get_device_object_path(address);
863 if (device_path == NULL) {
864 BT_ERR("No searched device");
865 GVariant *ret = NULL;
866 adapter_proxy = _bt_get_adapter_proxy();
867 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
869 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
870 g_variant_new("(s)", address),
871 G_DBUS_CALL_FLAGS_NONE,
877 BT_ERR("CreateDevice Fail: %s", error->message);
878 g_clear_error(&error);
881 g_variant_unref(ret);
882 device_path = _bt_get_device_object_path(address);
883 if (device_path == NULL) {
884 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
885 memcpy(dev_info.device_address.addr, device_address->addr,
886 BLUETOOTH_ADDRESS_LENGTH);
888 g_array_append_vals(*out_param1, &dev_info,
889 sizeof(bluetooth_device_info_t));
891 return BLUETOOTH_ERROR_NOT_PAIRED;
893 BT_INFO("device_path is created[%s]", device_path);
897 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
899 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
902 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
904 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
905 bonding_info->addr = g_strdup(address);
906 bonding_info->req_id = request_id;
908 bonding_info->device_proxy = proxy;
909 bonding_info->conn_type = conn_type;
911 is_device_creating = TRUE;
913 g_dbus_proxy_call(proxy, "Pair",
914 g_variant_new("(y)", conn_type),
915 G_DBUS_CALL_FLAGS_NONE,
918 (GAsyncReadyCallback)__bt_bond_device_cb,
921 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
923 return BLUETOOTH_ERROR_NONE;
925 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
926 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
929 g_array_append_vals(*out_param1, &dev_info,
930 sizeof(bluetooth_device_info_t));
932 is_device_creating = FALSE;
934 g_free(bonding_info->addr);
935 g_free(bonding_info);
938 return BLUETOOTH_ERROR_INTERNAL;*/
941 int _bt_cancel_bonding(void)
943 int ret = BLUETOOTH_ERROR_NONE;
945 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
947 ret = _bt_agent_reply_cancellation();
948 if (ret != BLUETOOTH_ERROR_NONE){
949 BT_ERR("Fail to call reply cancellation");
953 _bt_agent_set_canceled(TRUE);
955 return BLUETOOTH_ERROR_NONE;
958 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
962 GVariant *out_param1;
963 int result = BLUETOOTH_ERROR_NONE;
964 bt_funcion_data_t *unbonding_info;
965 bluetooth_device_info_t dev_info;
966 request_info_t *req_info;
968 g_dbus_proxy_call_finish(proxy, res, &err);
970 unbonding_info = user_data;
972 if (unbonding_info == NULL) {
974 BT_ERR("unbonding_info == NULL");
978 req_info = _bt_get_request_info(unbonding_info->req_id);
979 if (req_info == NULL) {
980 BT_ERR("req_info == NULL");
985 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
986 result = BLUETOOTH_ERROR_INTERNAL;
989 if (req_info->context == NULL)
992 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
993 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
994 unbonding_info->addr);
996 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
997 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
999 g_dbus_method_invocation_return_value(req_info->context,
1000 g_variant_new("(iv)", result, out_param1));
1003 _bt_delete_request_list(req_info->req_id);
1009 if (unbonding_info) {
1010 g_free(unbonding_info->addr);
1011 g_free(unbonding_info);
1015 int _bt_unbond_device(int request_id,
1016 bluetooth_device_address_t *device_address,
1017 GArray **out_param1)
1019 char *device_path = NULL;
1020 bt_funcion_data_t *unbonding_info;
1021 GDBusProxy *adapter_proxy = NULL;
1022 GDBusProxy *device_proxy = NULL;
1023 GDBusConnection *conn;
1024 int result = BLUETOOTH_ERROR_INTERNAL;
1025 bluetooth_device_info_t dev_info;
1026 GError *error = NULL;
1027 GVariant *ret = NULL;
1029 BT_CHECK_PARAMETER(device_address, return);
1031 adapter_proxy = _bt_get_adapter_proxy();
1032 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1034 /* allocate user data so that it can be retrieved in callback */
1035 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1036 /* Fix : NULL_RETURNS */
1037 if (unbonding_info == NULL) {
1038 BT_ERR("Memory not allocated !");
1039 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1042 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1043 unbonding_info->req_id = request_id;
1045 _bt_convert_addr_type_to_string(unbonding_info->addr,
1046 device_address->addr);
1048 device_path = _bt_get_device_object_path(unbonding_info->addr);
1050 if (device_path == NULL) {
1051 BT_ERR("No paired device");
1052 result = BLUETOOTH_ERROR_NOT_PAIRED;
1056 conn = _bt_get_system_gconn();
1058 BT_ERR("conn is NULL");
1059 result = BLUETOOTH_ERROR_INTERNAL;
1063 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1064 NULL, BT_BLUEZ_NAME,
1065 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1067 if (device_proxy != NULL) {
1069 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1070 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1071 G_DBUS_CALL_FLAGS_NONE,
1076 BT_ERR("Getting property failed: [%s]\n", error->message);
1077 g_error_free(error);
1078 result = BLUETOOTH_ERROR_NOT_PAIRED;
1082 BT_ERR("No paired device");
1083 g_object_unref(device_proxy);
1084 result = BLUETOOTH_ERROR_NOT_PAIRED;
1087 g_variant_unref(ret);
1089 g_object_unref(device_proxy);
1092 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1093 g_variant_new("(o)", device_path),
1094 G_DBUS_CALL_FLAGS_NONE,
1095 BT_MAX_DBUS_TIMEOUT,
1097 (GAsyncReadyCallback)__bt_unbond_cb,
1100 g_free(device_path);
1101 return BLUETOOTH_ERROR_NONE;
1104 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1105 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1106 unbonding_info->addr);
1108 g_array_append_vals(*out_param1, &dev_info,
1109 sizeof(bluetooth_device_info_t));
1110 g_free(device_path);
1111 g_free(unbonding_info->addr);
1112 g_free(unbonding_info);
1116 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1120 GVariant *out_param1;
1121 int result = BLUETOOTH_ERROR_NONE;
1122 bluetooth_device_info_t dev_info;
1123 bt_remote_dev_info_t *remote_dev_info;
1124 request_info_t *req_info;
1125 GVariant *uuid_list, *manufacture_data;
1127 GVariantBuilder *builder = NULL;
1130 g_dbus_proxy_call_finish(proxy, res, &err);
1132 g_object_unref(proxy);
1134 if (searching_info == NULL) {
1136 BT_ERR("unbonding_info == NULL");
1140 req_info = _bt_get_request_info(searching_info->req_id);
1141 if (req_info == NULL) {
1142 BT_ERR("req_info == NULL");
1147 g_dbus_error_strip_remote_error(err);
1148 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1150 if (g_strrstr("Operation canceled", err->message)) {
1151 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1152 } else if (g_strrstr("In Progress", err->message)) {
1153 result = BLUETOOTH_ERROR_IN_PROGRESS;
1154 } else if (g_strrstr("Host is down", err->message)) {
1155 result = BLUETOOTH_ERROR_HOST_DOWN;
1157 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1160 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1161 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1162 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1163 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1164 remote_dev_info->uuid_count > 0) {
1165 result = BLUETOOTH_ERROR_NONE;
1168 _bt_free_device_info(remote_dev_info);
1173 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1174 if (remote_dev_info == NULL)
1178 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1179 for (i=0; i < remote_dev_info->uuid_count; i++) {
1180 g_variant_builder_add(builder, "s",
1181 remote_dev_info->uuids[i]);
1183 uuid_list = g_variant_new("as", builder);
1184 g_variant_builder_unref(builder);
1185 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1186 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1189 param = g_variant_new("(isunsbub@asn@ay)",
1191 searching_info->addr,
1192 remote_dev_info->class,
1193 remote_dev_info->rssi,
1194 remote_dev_info->name,
1195 remote_dev_info->paired,
1196 remote_dev_info->connected,
1197 remote_dev_info->trust,
1199 remote_dev_info->manufacturer_data_len,
1202 /* Send the event to application */
1203 _bt_send_event(BT_ADAPTER_EVENT,
1204 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1207 _bt_free_device_info(remote_dev_info);
1210 if (req_info->context == NULL)
1213 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1214 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1215 searching_info->addr);
1217 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1218 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1220 g_dbus_method_invocation_return_value(req_info->context,
1221 g_variant_new("(iv)", result, out_param1));
1224 _bt_delete_request_list(req_info->req_id);
1229 if (searching_info) {
1230 g_free(searching_info->addr);
1231 g_free(searching_info);
1232 searching_info = NULL;
1236 int _bt_search_device(int request_id,
1237 bluetooth_device_address_t *device_address)
1239 char *device_path = NULL;
1240 GDBusProxy *device_proxy = NULL;
1241 GDBusConnection *conn;
1243 GDBusProxy *adapter_proxy;
1244 int result = BLUETOOTH_ERROR_INTERNAL;
1246 BT_CHECK_PARAMETER(device_address, return);
1249 BT_ERR("Bonding in progress");
1250 return BLUETOOTH_ERROR_DEVICE_BUSY;
1253 if (searching_info) {
1254 BT_ERR("Service searching in progress");
1255 return BLUETOOTH_ERROR_DEVICE_BUSY;
1258 adapter_proxy = _bt_get_adapter_proxy();
1259 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1261 /* allocate user data so that it can be retrieved in callback */
1262 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1263 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1264 searching_info->req_id = request_id;
1266 _bt_convert_addr_type_to_string(searching_info->addr,
1267 device_address->addr);
1269 conn = _bt_get_system_gconn();
1270 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1273 device_path = _bt_get_device_object_path(searching_info->addr);
1275 if (device_path == NULL) {
1276 BT_ERR("No paired device");
1277 result = BLUETOOTH_ERROR_NOT_PAIRED;
1281 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1282 NULL, BT_BLUEZ_NAME,
1283 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1284 g_free(device_path);
1285 if (device_proxy == NULL) {
1286 result = BLUETOOTH_ERROR_INTERNAL;
1290 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1291 g_variant_new("(s)", ""),
1292 G_DBUS_CALL_FLAGS_NONE,
1293 BT_MAX_DBUS_TIMEOUT,
1295 (GAsyncReadyCallback)__bt_discover_cb,
1298 searching_info->device_proxy = device_proxy;
1300 return BLUETOOTH_ERROR_NONE;
1302 g_free(searching_info->addr);
1303 g_free(searching_info);
1304 searching_info = NULL;
1308 int _bt_cancel_search_device(void)
1312 retv_if(searching_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
1314 if (searching_info->device_proxy) {
1315 g_dbus_proxy_call_sync(searching_info->device_proxy, "CancelDiscovery",
1317 G_DBUS_CALL_FLAGS_NONE,
1322 __bt_cancel_search_service_done();
1324 return BLUETOOTH_ERROR_NONE;
1327 int _bt_set_alias(bluetooth_device_address_t *device_address,
1330 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1331 gchar *device_path = NULL;
1332 GDBusProxy *adapter_proxy;
1333 GDBusProxy *device_proxy;
1334 GError *error = NULL;
1335 GDBusConnection *conn;
1337 BT_CHECK_PARAMETER(device_address, return);
1338 BT_CHECK_PARAMETER(alias, return);
1340 adapter_proxy = _bt_get_adapter_proxy();
1341 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1343 conn = _bt_get_system_gconn();
1344 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1346 _bt_convert_addr_type_to_string(address, device_address->addr);
1348 device_path = _bt_get_device_object_path(address);
1350 if (device_path == NULL) {
1351 BT_ERR("No paired device");
1352 return BLUETOOTH_ERROR_NOT_PAIRED;
1355 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1356 NULL, BT_BLUEZ_NAME,
1357 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1359 g_free(device_path);
1360 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1362 g_dbus_proxy_call_sync(device_proxy, "Set",
1363 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Alias", g_variant_new("s", alias)),
1364 G_DBUS_CALL_FLAGS_NONE,
1369 g_object_unref(device_proxy);
1372 BT_ERR("SetProperty error: [%s]", error->message);
1373 g_error_free(error);
1374 return BLUETOOTH_ERROR_INTERNAL;
1377 return BLUETOOTH_ERROR_NONE;
1380 int _bt_set_authorization(bluetooth_device_address_t *device_address,
1383 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1384 gchar *device_path = NULL;
1385 GDBusProxy *device_proxy;
1386 gboolean previous_value;
1387 GError *error = NULL;
1388 GDBusConnection *conn;
1389 GVariant *result = NULL;
1390 int ret = BLUETOOTH_ERROR_NONE;
1392 BT_CHECK_PARAMETER(device_address, return);
1394 conn = _bt_get_system_gconn();
1395 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1397 _bt_convert_addr_type_to_string(address, device_address->addr);
1399 device_path = _bt_get_device_object_path(address);
1401 if (device_path == NULL) {
1402 BT_ERR("No paired device");
1403 return BLUETOOTH_ERROR_NOT_PAIRED;
1406 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1407 NULL, BT_BLUEZ_NAME,
1408 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1410 g_free(device_path);
1411 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1413 result = g_dbus_proxy_call_sync(device_proxy, "Get",
1414 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Trusted" ),
1415 G_DBUS_CALL_FLAGS_NONE,
1419 if (error != NULL) {
1420 BT_ERR("Getting property failed: [%s]\n", error->message);
1421 g_error_free(error);
1422 g_object_unref(device_proxy);
1423 return BLUETOOTH_ERROR_INTERNAL;
1426 previous_value = g_variant_get_boolean(result);
1427 g_variant_unref(result);
1428 /* If the input is same with previous value, return error. */
1429 if (previous_value == authorize) {
1430 BT_ERR("Same value: %d", previous_value);
1431 g_object_unref(device_proxy);
1432 ret = BLUETOOTH_ERROR_INVALID_PARAM;
1436 g_dbus_proxy_call_sync(device_proxy, "Set",
1437 g_variant_new("(ssv)", BT_DEVICE_INTERFACE, "Trusted", g_variant_new("b",authorize)),
1438 G_DBUS_CALL_FLAGS_NONE,
1443 g_object_unref(device_proxy);
1445 BT_ERR("SetProperty error: [%s]", error->message);
1446 g_error_free(error);
1447 ret = BLUETOOTH_ERROR_INTERNAL;
1453 int _bt_is_gatt_connected(bluetooth_device_address_t *device_address,
1454 gboolean *is_connected)
1456 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1457 char *object_path = NULL;
1459 GDBusProxy *device_proxy;
1460 GError *error = NULL;
1462 GVariant *tmp_value;
1463 GDBusConnection *conn;
1464 GVariant *result = NULL;
1465 int ret = BLUETOOTH_ERROR_NONE;
1467 BT_CHECK_PARAMETER(device_address, return);
1469 conn = _bt_get_system_gconn();
1470 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1472 _bt_convert_addr_type_to_string(address, device_address->addr);
1474 object_path = _bt_get_device_object_path(address);
1475 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1477 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1478 NULL, BT_BLUEZ_NAME,
1479 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1480 g_free(object_path);
1481 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1483 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1484 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1485 G_DBUS_CALL_FLAGS_NONE,
1489 if (error != NULL || result == NULL) {
1490 if (error != NULL) {
1491 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1492 g_error_free(error);
1494 g_object_unref(device_proxy);
1495 return BLUETOOTH_ERROR_INTERNAL;
1498 g_variant_get(result , "(@a{sv})", &value);
1499 g_variant_unref(result);
1501 tmp_value = g_variant_lookup_value (value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1503 if (tmp_value == NULL) {
1504 g_object_unref(device_proxy);
1505 return BLUETOOTH_ERROR_INTERNAL;
1508 *is_connected = g_variant_get_boolean (tmp_value);
1510 BT_DBG("gatt is connected : %d", *is_connected);
1511 g_variant_unref(tmp_value);
1512 g_variant_unref(value);
1513 g_object_unref(device_proxy);
1518 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1519 int connection_type, gboolean *is_connected)
1521 char *object_path = NULL;
1522 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1523 GDBusProxy *device_proxy = NULL;
1524 GDBusProxy *adapter_proxy = NULL;
1525 GDBusConnection *conn;
1526 GError *error = NULL;
1527 GVariant *tmp_value = NULL;
1528 GVariant *value = NULL;
1529 GVariant *result = NULL;
1532 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1533 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1535 *is_connected = FALSE;
1536 BT_DBG("connection_type: %d", connection_type);
1537 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1538 return _bt_rfcomm_is_device_connected(device_address,
1540 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1541 return _bt_is_gatt_connected(device_address, is_connected);
1543 adapter_proxy = _bt_get_adapter_proxy();
1544 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1546 conn = _bt_get_system_gconn();
1547 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1549 _bt_convert_addr_type_to_string(address, device_address->addr);
1551 if(connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1552 object_path = _bt_get_adapter_path();
1553 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1554 NULL, BT_BLUEZ_NAME,
1555 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1556 g_free(object_path);
1557 if (device_proxy == NULL) {
1558 BT_DBG("Device don't have this service");
1559 return BLUETOOTH_ERROR_NONE;
1562 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1563 g_variant_new("(s)", address),
1564 G_DBUS_CALL_FLAGS_NONE,
1569 if (result == NULL) {
1570 BT_ERR("Error occured in Proxy call");
1572 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1573 g_error_free(error);
1575 g_object_unref(device_proxy);
1576 return BLUETOOTH_ERROR_NONE;
1578 g_variant_get(result , "(@a{sv})", &value);
1579 g_variant_unref(result);
1582 tmp_value = g_variant_lookup_value(value,
1584 G_VARIANT_TYPE_BOOLEAN);
1586 *is_connected = g_variant_get_boolean(tmp_value);
1587 g_variant_unref(tmp_value);
1589 g_variant_unref(value);
1591 } else if(connection_type == BLUETOOTH_NAP_SERVICE) {
1592 return _bt_is_network_connected(_bt_get_net_conn(),
1593 device_address->addr, is_connected);
1595 uuid = _bt_get_profile_uuid128(connection_type);
1597 BT_ERR("uuid is NULL");
1598 return BLUETOOTH_ERROR_NONE;
1601 BT_DBG("uuid: %s", uuid);
1603 object_path = _bt_get_device_object_path(address);
1604 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1605 BT_DBG("object_path: %s", object_path);
1606 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1607 NULL, BT_BLUEZ_NAME,
1608 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1609 g_free(object_path);
1610 if (device_proxy == NULL) {
1611 BT_DBG("Device don't have this service");
1613 return BLUETOOTH_ERROR_NONE;
1616 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1617 g_variant_new("(s)", uuid),
1618 G_DBUS_CALL_FLAGS_NONE,
1623 if (result == NULL) {
1624 BT_ERR("Error occured in Proxy call");
1626 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1627 g_error_free(error);
1630 g_variant_get(result, "(b)", is_connected);
1632 g_variant_unref(result);
1636 g_object_unref(device_proxy);
1637 return BLUETOOTH_ERROR_NONE;
1640 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1641 bluetooth_connected_link_t *connected)
1643 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1644 char *object_path = NULL;
1646 GDBusProxy *device_proxy;
1647 GError *error = NULL;
1648 GDBusConnection *conn;
1649 GVariant *tmp_value = NULL;
1650 GVariant *value = NULL;
1651 GVariant *result = NULL;
1653 int ret = BLUETOOTH_ERROR_NONE;
1655 BT_CHECK_PARAMETER(device_address, return);
1657 conn = _bt_get_system_gconn();
1658 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1660 _bt_convert_addr_type_to_string(address, device_address->addr);
1662 object_path = _bt_get_device_object_path(address);
1663 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1665 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1666 NULL, BT_BLUEZ_NAME,
1667 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1668 g_free(object_path);
1669 if (device_proxy == NULL) {
1670 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1671 return BLUETOOTH_ERROR_NONE;
1674 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1675 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1676 G_DBUS_CALL_FLAGS_NONE,
1681 if (error != NULL) {
1682 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1683 g_error_free(error);
1684 g_object_unref(device_proxy);
1685 return BLUETOOTH_ERROR_INTERNAL;
1688 g_variant_get(result , "(@a{sv})", &value);
1689 g_variant_unref(result);
1691 tmp_value = g_variant_lookup_value (value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1692 if (tmp_value != NULL) {
1693 g_object_unref(device_proxy);
1694 g_variant_unref(value);
1695 return BLUETOOTH_ERROR_INTERNAL;
1697 *connected = g_variant_get_boolean (tmp_value);
1698 g_variant_unref(tmp_value);
1699 g_variant_unref(value);
1700 g_object_unref(device_proxy);
1705 static void __bt_connect_le_device_cb(GDBusProxy *proxy, GAsyncResult *res,
1709 GVariant *out_param1;
1710 request_info_t *req_info;
1711 bluetooth_device_address_t device_addr = { {0} };
1712 int result = BLUETOOTH_ERROR_NONE;
1715 g_dbus_proxy_call_finish(proxy, res, &err);
1717 /* When cb datas of lescan are received, back to parameta values for Web TCT */
1718 if ( _bt_is_set_scan_parameter() == FALSE) {
1719 bluetooth_le_scan_params_t scan_params;
1720 BT_ERR("set parameter of lescan when receive cb data");
1721 scan_params.type = BT_LE_ACTIVE_SCAN;
1722 scan_params.interval = 5000;
1723 scan_params.window = 500;
1724 _bt_set_scan_parameters(&scan_params);
1727 req_info = _bt_get_request_info(le_connection_info->req_id);
1728 if (req_info == NULL) {
1729 BT_ERR("req_info == NULL");
1730 g_object_unref(proxy);
1731 le_connection_info->device_proxy = NULL;
1736 BT_ERR("Error occured in ConnectLE [%s]", err->message);
1738 if (g_strrstr(err->message, "NotSupported")) {
1739 BT_INFO("Connection Not Supported");
1740 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1741 } else if (g_strrstr(err->message, "AlreadyConnected")) {
1742 BT_INFO("Connection already exists");
1743 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1744 } else if (g_strrstr(err->message, "Connection Timeout")) {
1745 BT_INFO("Connection Timeout");
1746 result = BLUETOOTH_ERROR_TIMEOUT;
1748 BT_DBG("Default case");
1749 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1753 g_object_unref(proxy);
1754 le_connection_info->device_proxy = NULL;
1756 _bt_convert_addr_string_to_type(device_addr.addr,
1757 le_connection_info->addr);
1759 if (req_info->context == NULL)
1762 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1763 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
1764 g_dbus_method_invocation_return_value(req_info->context,
1765 g_variant_new("(iv)", result, out_param1));
1767 g_variant_unref(out_param1);
1769 _bt_delete_request_list(req_info->req_id);
1774 g_free(le_connection_info->addr);
1775 g_free(le_connection_info);
1776 le_connection_info = NULL;
1781 int _bt_connect_le_device(int request_id,
1782 const bluetooth_device_address_t *bd_addr,
1783 gboolean auto_connect)
1785 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1786 gchar *device_path = NULL;
1787 GDBusProxy *device_proxy = NULL;
1788 GDBusProxy *adapter_proxy;
1789 GDBusConnection *conn;
1790 int ret = BLUETOOTH_ERROR_NONE;
1792 BT_CHECK_PARAMETER(bd_addr, return);
1794 _bt_convert_addr_type_to_string(device_address,
1795 (unsigned char *)bd_addr->addr);
1797 if (le_connection_info) {
1798 BT_ERR("LE Connection in progress");
1799 return BLUETOOTH_ERROR_DEVICE_BUSY;
1802 /* When auto_connect is true, Web TCT fail. It need to be checked */
1803 auto_connect = FALSE;
1805 conn = _bt_get_system_gconn();
1806 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1808 adapter_proxy = _bt_get_adapter_proxy();
1809 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1811 device_path = _bt_get_device_object_path(device_address);
1812 if (device_path == NULL) {
1813 BT_DBG("device_path NULL");
1814 ret = BLUETOOTH_ERROR_INTERNAL;
1818 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1820 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1821 NULL, BT_BLUEZ_NAME,
1822 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1823 g_free(device_path);
1824 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1827 le_connection_info = g_malloc0(sizeof(bt_funcion_data_t));
1828 le_connection_info->addr = g_strdup(device_address);
1829 le_connection_info->req_id = request_id;
1830 le_connection_info->device_proxy = device_proxy;
1832 /* The connection between Ble device and TM1 often fail. So It need to be set for Web TCT*/
1833 if ( _bt_is_set_scan_parameter() == FALSE) {
1834 BT_ERR("set parameter of le scan when connect device");
1835 bluetooth_le_scan_params_t scan_params;
1836 scan_params.type = BT_LE_ACTIVE_SCAN;
1837 scan_params.interval = 60;
1838 scan_params.window = 60;
1839 _bt_set_scan_parameters(&scan_params);
1842 g_dbus_proxy_call(device_proxy, "ConnectLE",
1843 g_variant_new("(b)", auto_connect),
1844 G_DBUS_CALL_FLAGS_NONE,
1845 BT_MAX_DBUS_TIMEOUT,
1847 (GAsyncReadyCallback)__bt_connect_le_device_cb,
1854 static void __bt_disconnect_le_device_cb(GDBusProxy *proxy, GAsyncResult *res,
1858 GVariant *out_param1;
1859 request_info_t *req_info;
1860 bt_funcion_data_t *le_disconnection_info = user_data;
1861 bluetooth_device_address_t device_addr = { {0} };
1862 int result = BLUETOOTH_ERROR_NONE;
1865 g_dbus_proxy_call_finish(proxy, res, &err);
1867 req_info = _bt_get_request_info(le_disconnection_info->req_id);
1868 if (req_info == NULL) {
1869 BT_ERR("req_info == NULL");
1870 g_object_unref(proxy);
1871 le_disconnection_info->device_proxy = NULL;
1876 BT_ERR("Error occured in DisconnectLE [%s]", err->message);
1878 if (g_strrstr(err->message, "NotSupported")) {
1879 BT_INFO("Connection Not Supported");
1880 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1881 } else if (g_strrstr(err->message, "NotConnected")) {
1882 BT_INFO("Connection does not exists");
1883 result = BLUETOOTH_ERROR_NOT_CONNECTED;
1884 } else if (g_strrstr(err->message, "Cancelled")) {
1885 BT_INFO("User Cancelled");
1886 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1888 BT_DBG("Default case");
1889 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1893 g_object_unref(proxy);
1894 le_disconnection_info->device_proxy = NULL;
1896 _bt_convert_addr_string_to_type(device_addr.addr,
1897 le_disconnection_info->addr);
1899 if (req_info->context == NULL)
1902 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1903 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
1905 g_dbus_method_invocation_return_value(req_info->context,
1906 g_variant_new("(iv)", result, out_param1));
1908 g_variant_unref(out_param1);
1910 _bt_delete_request_list(req_info->req_id);
1915 g_free(le_disconnection_info->addr);
1916 g_free(le_disconnection_info);
1917 le_disconnection_info = NULL;
1922 int _bt_disconnect_le_device(int request_id,
1923 const bluetooth_device_address_t *bd_addr)
1925 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1926 gchar *device_path = NULL;
1927 GDBusProxy *device_proxy = NULL;
1928 GDBusProxy *adapter_proxy;
1929 GDBusConnection *conn;
1930 int ret = BLUETOOTH_ERROR_NONE;
1931 bt_funcion_data_t *le_disconnection_info = NULL;
1933 BT_CHECK_PARAMETER(bd_addr, return);
1935 _bt_convert_addr_type_to_string(device_address,
1936 (unsigned char *)bd_addr->addr);
1938 conn = _bt_get_system_gconn();
1939 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1941 adapter_proxy = _bt_get_adapter_proxy();
1942 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1944 device_path = _bt_get_device_object_path(device_address);
1945 if (device_path == NULL) {
1946 BT_DBG("device_path NULL");
1947 ret = BLUETOOTH_ERROR_INTERNAL;
1951 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1953 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1954 NULL, BT_BLUEZ_NAME,
1955 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1956 g_free(device_path);
1957 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1959 le_disconnection_info = g_malloc0(sizeof(bt_funcion_data_t));
1960 le_disconnection_info->addr = g_strdup(device_address);
1961 le_disconnection_info->req_id = request_id;
1962 le_disconnection_info->device_proxy = device_proxy;
1964 g_dbus_proxy_call(device_proxy, "DisconnectLE",
1966 G_DBUS_CALL_FLAGS_NONE,
1967 BT_MAX_DBUS_TIMEOUT,
1969 (GAsyncReadyCallback)__bt_disconnect_le_device_cb,
1970 le_disconnection_info);
1975 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
1977 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1978 gchar *device_path = NULL;
1979 GError *error = NULL;
1980 GDBusProxy *device_proxy = NULL;
1981 GDBusProxy *adapter_proxy;
1982 GDBusConnection *conn;
1983 int ret = BLUETOOTH_ERROR_NONE;
1985 BT_CHECK_PARAMETER(bd_addr, return);
1987 _bt_convert_addr_type_to_string(device_address,
1988 (unsigned char *)bd_addr->addr);
1990 conn = _bt_get_system_gconn();
1991 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1993 adapter_proxy = _bt_get_adapter_proxy();
1994 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1996 device_path = _bt_get_device_object_path(device_address);
1997 if (device_path == NULL) {
1998 BT_DBG("device_path NULL");
1999 ret = BLUETOOTH_ERROR_INTERNAL;
2003 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2005 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2006 NULL, BT_BLUEZ_NAME,
2007 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2008 g_free(device_path);
2009 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2011 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2013 G_DBUS_CALL_FLAGS_NONE,
2018 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2019 g_error_free(error);
2020 g_object_unref(device_proxy);
2021 return BLUETOOTH_ERROR_INTERNAL;
2024 g_object_unref(device_proxy);
2029 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2031 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2032 gchar *device_path = NULL;
2033 GError *error = NULL;
2034 GDBusProxy *device_proxy = NULL;
2035 GDBusProxy *adapter_proxy;
2036 GDBusConnection *conn;
2037 int ret = BLUETOOTH_ERROR_NONE;
2039 BT_CHECK_PARAMETER(bd_addr, return);
2041 _bt_convert_addr_type_to_string(device_address,
2042 (unsigned char *)bd_addr->addr);
2044 conn = _bt_get_system_gconn();
2045 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2047 adapter_proxy = _bt_get_adapter_proxy();
2048 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2050 device_path = _bt_get_device_object_path(device_address);
2051 if (device_path == NULL) {
2052 BT_DBG("device_path NULL");
2053 ret = BLUETOOTH_ERROR_INTERNAL;
2057 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2059 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2060 NULL, BT_BLUEZ_NAME,
2061 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2062 g_free(device_path);
2063 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2065 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2067 G_DBUS_CALL_FLAGS_NONE,
2072 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2073 g_error_free(error);
2074 g_object_unref(device_proxy);
2075 return BLUETOOTH_ERROR_INTERNAL;
2078 g_object_unref(device_proxy);
2083 int _bt_connect_profile(char *address, char *uuid,
2084 void *cb, gpointer func_data)
2088 GDBusConnection *conn;
2089 GDBusProxy *adapter_proxy;
2090 GError *error = NULL;
2092 conn = _bt_get_system_gconn();
2093 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2095 object_path = _bt_get_device_object_path(address);
2096 if (object_path == NULL) {
2097 BT_ERR("No searched device");
2099 adapter_proxy = _bt_get_adapter_proxy();
2100 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2102 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2103 g_variant_new("(s)", address),
2104 G_DBUS_CALL_FLAGS_NONE,
2109 if (error != NULL) {
2110 BT_ERR("CreateDevice Fail: %s", error->message);
2111 g_error_free(error);
2114 object_path = _bt_get_device_object_path(address);
2116 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2118 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2119 NULL, BT_BLUEZ_NAME,
2120 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2121 g_free(object_path);
2122 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2124 g_dbus_proxy_call(proxy, "ConnectProfile",
2125 g_variant_new("(s)", uuid),
2126 G_DBUS_CALL_FLAGS_NONE,
2127 BT_MAX_DBUS_TIMEOUT,
2129 (GAsyncReadyCallback)cb,
2132 return BLUETOOTH_ERROR_NONE;
2135 int _bt_disconnect_profile(char *address, char *uuid,
2136 void *cb, gpointer func_data)
2140 GDBusConnection *conn;
2142 conn = _bt_get_system_gconn();
2143 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2145 object_path = _bt_get_device_object_path(address);
2146 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2148 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2149 NULL, BT_BLUEZ_NAME,
2150 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2151 g_free(object_path);
2152 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2154 g_dbus_proxy_call(proxy, "DisconnectProfile",
2155 g_variant_new("(s)", uuid),
2156 G_DBUS_CALL_FLAGS_NONE,
2157 BT_MAX_DBUS_TIMEOUT,
2159 (GAsyncReadyCallback)cb,
2162 return BLUETOOTH_ERROR_NONE;
2165 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2166 int low_threshold, int in_range_threshold, int high_threshold)
2168 int ret = BLUETOOTH_ERROR_NONE;
2170 GError *error = NULL;
2171 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2173 BT_CHECK_PARAMETER(bd_addr, return);
2174 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2175 bd_addr->addr[0], bd_addr->addr[1],
2176 bd_addr->addr[2], bd_addr->addr[3],
2177 bd_addr->addr[4], bd_addr->addr[5],
2179 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2180 in_range_threshold, high_threshold);
2182 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2184 proxy = _bt_get_adapter_proxy();
2185 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2187 g_dbus_proxy_call_sync(proxy, "EnableRssi",
2188 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2189 G_DBUS_CALL_FLAGS_NONE,
2193 if (error != NULL) {
2194 BT_ERR("Dbus Call Error:[%s]", error->message);
2195 g_error_free(error);
2196 ret = BLUETOOTH_ERROR_INTERNAL;
2201 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2204 int ret = BLUETOOTH_ERROR_NONE;
2206 GError *error = NULL;
2207 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2209 BT_CHECK_PARAMETER(bd_addr, return);
2210 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2211 bd_addr->addr[0], bd_addr->addr[1],
2212 bd_addr->addr[2], bd_addr->addr[3],
2213 bd_addr->addr[4], bd_addr->addr[5],
2216 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2218 proxy = _bt_get_adapter_proxy();
2219 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2221 g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2222 g_variant_new("(si)", address, link_type),
2223 G_DBUS_CALL_FLAGS_NONE,
2228 if (error != NULL) {
2229 BT_ERR("Dbus Call Error:[%s]", error->message);
2230 g_error_free(error);
2231 ret = BLUETOOTH_ERROR_INTERNAL;
2236 int _bt_le_conn_update(unsigned char *device_address,
2237 guint16 interval_min, guint16 interval_max,
2238 guint16 latency, guint16 time_out)
2240 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2241 gchar *device_path = NULL;
2242 GError *error = NULL;
2243 GDBusProxy *device_proxy = NULL;
2244 GDBusConnection *conn;
2246 guint32 min, max, to;
2247 guint32 min_supervision_to;
2248 int ret = BLUETOOTH_ERROR_NONE;
2252 BT_CHECK_PARAMETER(device_address, return);
2254 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
2255 interval_min, interval_max, latency, time_out);
2257 if (interval_min > interval_max ||
2258 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2259 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2260 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2264 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2265 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2266 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2270 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2271 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2276 * The Supervision_Timeout in milliseconds shall be larger than
2277 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2278 * where Conn_Interval_Max is given in milliseconds.
2280 min_supervision_to = (1 + latency) * interval_max * 2;
2281 if (time_out <= min_supervision_to) {
2282 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2286 _bt_convert_addr_type_to_string(address, device_address);
2288 BT_DBG("Remote device address: %s", address);
2290 device_path = _bt_get_device_object_path(address);
2292 if (device_path == NULL) {
2293 BT_DBG("device_path NULL");
2294 ret = BLUETOOTH_ERROR_INTERNAL;
2298 conn = _bt_get_system_gconn();
2300 BT_DBG("conn NULL");
2301 ret = BLUETOOTH_ERROR_INTERNAL;
2305 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2306 NULL, BT_BLUEZ_NAME,
2307 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2309 g_free(device_path);
2310 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2312 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2313 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2314 to = time_out / BT_LE_CONN_TO_SPLIT;
2316 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2317 g_variant_new("(uuuu)", min, max, latency, to),
2318 G_DBUS_CALL_FLAGS_NONE,
2323 g_object_unref(device_proxy);
2324 BT_ERR("LeConnUpdate Call Error for %s", address);
2325 if (reply == NULL) {
2327 BT_ERR("Error %s[%s]", error->message, address);
2328 g_error_free(error);
2329 return BLUETOOTH_ERROR_INTERNAL;
2332 g_variant_unref(reply);
2339 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2340 bluetooth_device_pin_code_t *pin_code)
2342 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2344 bt_pin_code_info_t *pin_info = NULL;
2346 BT_CHECK_PARAMETER(device_address, return);
2347 BT_CHECK_PARAMETER(pin_code, return);
2348 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2349 BLUETOOTH_ERROR_NO_RESOURCES);
2351 _bt_convert_addr_type_to_string(address, device_address->addr);
2353 for (l = pin_info_list; l != NULL; l = l->next) {
2356 if (g_strcmp0(pin_info->address, address) == 0) {
2357 g_free(pin_info->pin_code);
2358 pin_info->pin_code = g_strdup(pin_code->pin_code);
2359 return BLUETOOTH_ERROR_NONE;
2363 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2364 if (pin_info != NULL) {
2365 pin_info->address = g_strdup(address);
2366 pin_info->pin_code = g_strdup(pin_code->pin_code);
2367 pin_info_list = g_slist_append(pin_info_list, pin_info);
2368 return BLUETOOTH_ERROR_NONE;
2371 return BLUETOOTH_ERROR_INTERNAL;
2374 gint __bt_compare_address(gpointer *a, gpointer *b)
2376 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2377 char *address = (char *)b;
2378 return g_strcmp0(pin_info->address, address);
2381 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2383 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2385 bt_pin_code_info_t *pin_info = NULL;
2387 BT_CHECK_PARAMETER(device_address, return);
2389 _bt_convert_addr_type_to_string(address, device_address->addr);
2391 l = g_slist_find_custom(pin_info_list, address,
2392 (GCompareFunc)__bt_compare_address);
2396 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2397 g_free(pin_info->address);
2398 g_free(pin_info->pin_code);
2402 return BLUETOOTH_ERROR_NONE;
2405 int _bt_get_device_pin_code(const char *address, char *pin_code)
2409 BT_CHECK_PARAMETER(address, return);
2410 BT_CHECK_PARAMETER(pin_code, return);
2412 for (l = pin_info_list; l != NULL; l = l->next) {
2413 bt_pin_code_info_t *pin_info = l->data;
2415 if (g_strcmp0(pin_info->address, address) == 0) {
2416 g_strlcpy(pin_code, pin_info->pin_code,
2417 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2419 return BLUETOOTH_ERROR_NONE;
2423 return BLUETOOTH_ERROR_NOT_FOUND;
2426 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2427 bluetooth_le_connection_param_t *param)
2430 return BLUETOOTH_ERROR_INVALID_PARAM;
2432 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2433 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2434 return BLUETOOTH_ERROR_INVALID_PARAM;
2436 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2439 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2440 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2441 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2442 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2443 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2446 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2447 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2448 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2449 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2450 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2453 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2454 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2455 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2456 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2457 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2461 BT_ERR("Unhandled mode : %d", mode);
2465 return BLUETOOTH_ERROR_NONE;
2468 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
2470 GapAgentPrivate *agent = _bt_get_adapter_agent();
2471 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
2473 if (authentication_reply)
2474 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
2476 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
2478 BT_DBG("BT_PASSKEY_REPLY");
2479 return BLUETOOTH_ERROR_NONE;
2482 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
2484 GapAgentPrivate *agent = _bt_get_adapter_agent();
2485 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
2487 if (confirmation_reply)
2488 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
2490 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
2492 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
2493 return BLUETOOTH_ERROR_NONE;