2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
23 #include <syspopup_caller.h>
25 #include <bundle_internal.h>
27 #include "bluetooth-api.h"
28 #include "bt-internal-types.h"
30 #include "bt-service-common.h"
31 #include "bt-service-event.h"
32 #include "bt-service-device.h"
33 #include "bt-service-adapter-le.h"
34 #include "bt-service-rfcomm-client.h"
35 #include "bt-service-util.h"
36 #include "bt-service-agent.h"
37 #include "bt-service-network.h"
38 #include "bt-service-adapter.h"
39 #include "bt-service-gap-agent.h"
41 #define BT_SYSPOPUP_IPC_RESPONSE_OBJECT "/org/projectx/bt_syspopup_res"
42 #define BT_SYSPOPUP_INTERFACE "User.Bluetooth.syspopup"
43 #define BT_SYSPOPUP_METHOD_RESPONSE "Response"
45 #define BT_LE_CONN_INTERVAL_MIN 7.5 /* msec */
46 #define BT_LE_CONN_INTERVAL_MAX 4000 /* msec */
47 #define BT_LE_CONN_SUPER_TO_MIN 100 /* msec */
48 #define BT_LE_CONN_SUPER_TO_MAX 32000 /* msec */
49 #define BT_LE_CONN_SLAVE_LATENCY_MAX 499
50 #define BT_LE_CONN_INTERVAL_SPLIT 1.25 /* msec */
51 #define BT_LE_CONN_TO_SPLIT 10 /* msec */
52 #define BT_DEVICE_PIN_CODE_SLOT_MAX 10
54 #define BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT 20000 /* msec */
56 #define BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL 30 /* msec */
57 #define BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL 50 /* msec */
58 #define BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY 0 /* event */
60 #define BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL 7.5 /* msec */
61 #define BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL 10 /* msec */
62 #define BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY 0 /* event */
64 #define BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL 80 /* msec */
65 #define BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL 100 /* msec */
66 #define BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY 2 /* event */
73 GDBusProxy *device_proxy;
74 GDBusProxy *adapter_proxy;
76 unsigned short conn_type;
84 gboolean is_device_creating;
85 bt_funcion_data_t *bonding_info;
86 bt_funcion_data_t *searching_info;
87 bt_funcion_data_t *le_connection_info;
89 static GSList *pin_info_list = NULL;
91 /* This HID Mouse does not support pairing precedure. need to skip it. */
92 #define SMB_MOUSE_LAP_ADDR "00:12:A1"
94 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
97 static int __bt_retry_bond(void);
100 /*static void __bt_decline_pair_request()
102 GVariant *out_param1;
103 GVariant *out_param2;
104 request_info_t *req_info;
105 bluetooth_device_info_t dev_info;
106 bt_remote_dev_info_t *remote_dev_info;
107 GVariant *uuid_list, *manufacture_data;
112 req_info = _bt_get_request_info(bonding_info->req_id);
113 if (req_info == NULL) {
114 BT_ERR("req_info == NULL");
117 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
119 BT_DBG("bonding_info is NULL");
124 uuid_list = g_variant_new_from_data((const GVariantType *)"as",
125 remote_dev_info->uuids, remote_dev_info->uuid_count,
128 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
129 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
132 param = g_variant_new("isunsbuba{s}na{y})",
133 bonding_info->result,
135 remote_dev_info->class,
136 remote_dev_info->rssi,
137 remote_dev_info->name,
138 remote_dev_info->paired,
139 remote_dev_info->connected,
140 remote_dev_info->trust,
142 remote_dev_info->manufacturer_data_len,
146 //Send the event to application
147 if (remote_dev_info != NULL) {
148 _bt_send_event(BT_ADAPTER_EVENT,
149 BLUETOOTH_EVENT_BONDING_FINISHED,
152 _bt_free_device_info(remote_dev_info);
155 if (req_info->context == NULL)
158 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
159 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
162 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
163 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
165 out_param2 = g_variant_new_from_data((const GVariantType *)"i",
166 &bonding_info->result, sizeof(int), TRUE, NULL, NULL);
169 g_dbus_method_invocation_return_value(req_info->context,
170 g_variant_new("@ayi", out_param1, out_param2));
173 _bt_delete_request_list(req_info->req_id);
176 g_free(bonding_info->addr);
177 g_free(bonding_info);
183 #ifdef TIZEN_WEARABLE
184 static gboolean __bt_syspopup_timer_cb(gpointer user_data)
188 retv_if(user_data == NULL, FALSE);
190 b = (bundle *)user_data;
192 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
193 ret = syspopup_launch("bt-syspopup", b);
196 BT_ERR("Sorry!! Cannot launch popup return = %d, Retrying...", ret);
198 BT_DBG("Hurray!!! Finally Popup launched");
201 return (ret < 0) ? TRUE : FALSE;
204 static gboolean __bt_launch_unable_to_pairing_syspopup(int result)
209 GDBusConnection *conn;
211 conn = _bt_get_system_gconn();
219 bundle_add(b, "event-type", "unable-to-pairing");
221 if (result == BLUETOOTH_ERROR_TIMEOUT)
222 bundle_add(b, "error", "timeout");
223 else if (result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED)
224 bundle_add(b, "error", "authfailed");
226 bundle_add(b, "error", "error");
228 #if !defined(LIBNOTIFY_SUPPORT) && !defined(LIBNOTIFICATION_SUPPORT)
229 ret = syspopup_launch("bt-syspopup", b);
232 BT_ERR("Popup launch failed...retry %d \n", ret);
233 g_timeout_add(200, (GSourceFunc) __bt_syspopup_timer_cb,
244 gboolean _bt_is_device_creating(void)
246 return is_device_creating;
249 gboolean _bt_is_bonding_device_address(const char *address)
251 if (bonding_info == NULL || bonding_info->addr == NULL)
254 if (g_strcmp0(bonding_info->addr, address) == 0) {
255 BT_DBG("[%s] is bonding device", address);
259 BT_DBG("[%s] is NOT bonding device", address);
263 void _bt_set_autopair_status_in_bonding_info(gboolean is_autopair)
265 ret_if(bonding_info == NULL);
266 bonding_info->is_autopair = is_autopair;
269 void _bt_device_path_to_address(const char *device_path,
270 char *device_address)
272 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
276 ret_if(device_path == NULL);
277 ret_if(device_address == NULL);
279 dev_addr = strstr(device_path, "dev_");
280 ret_if(dev_addr == NULL);
283 g_strlcpy(address, dev_addr, sizeof(address));
285 while ((pos = strchr(address, '_')) != NULL) {
289 g_strlcpy(device_address, address, BT_ADDRESS_STRING_SIZE);
292 void __bt_cancel_search_service_done(void)
294 int result = BLUETOOTH_ERROR_CANCEL_BY_USER;
295 request_info_t *req_info;
296 bluetooth_device_info_t dev_info;
297 GVariant *out_param1;
299 ret_if(searching_info == NULL);
301 req_info = _bt_get_request_info(searching_info->req_id);
302 if (req_info == NULL) {
303 BT_ERR("req_info == NULL");
307 if (req_info->context == NULL)
310 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
311 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
312 searching_info->addr);
314 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
315 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
318 g_dbus_method_invocation_return_value(req_info->context,
319 g_variant_new("(iv)", result, out_param1));
321 _bt_delete_request_list(req_info->req_id);
325 g_free(searching_info->addr);
326 g_free(searching_info);
327 searching_info = NULL;
330 static void __bt_get_uuids(GVariant *value, bt_remote_dev_info_t *info)
332 ret_if(value == NULL);
333 ret_if(info == NULL);
335 info->uuid_count = g_variant_get_size(value);
336 info->uuids = g_variant_dup_strv(value, &info->uuid_count);
339 bt_remote_dev_info_t *_bt_get_remote_device_info(char *address)
341 bt_remote_dev_info_t *dev_info;
342 char *object_path = NULL;
343 GDBusProxy *adapter_proxy;
344 GDBusProxy *device_proxy;
348 gchar *manufacturer_data = NULL;
349 GDBusConnection *conn;
350 GError *error = NULL;
351 GVariant *result = NULL;
353 retv_if(address == NULL, NULL);
355 adapter_proxy = _bt_get_adapter_proxy();
356 retv_if(adapter_proxy == NULL, NULL);
359 object_path = _bt_get_device_object_path(address);
361 retv_if(object_path == NULL, NULL);
363 conn = _bt_get_system_gconn();
365 BT_ERR("conn == NULL");
370 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
372 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
375 retv_if(device_proxy == NULL, NULL);
377 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
378 g_variant_new("(s)", BT_DEVICE_INTERFACE),
379 G_DBUS_CALL_FLAGS_NONE,
384 g_object_unref(device_proxy);
386 dev_info = g_malloc0(sizeof(bt_remote_dev_info_t));
388 if (result != NULL) {
389 g_variant_get(result , "(@a{sv})", &value);
390 g_variant_unref(result);
392 tmp_value = g_variant_lookup_value(value, "Alias", G_VARIANT_TYPE_STRING);
394 g_variant_get(tmp_value, "s", &name);
395 g_variant_unref(tmp_value);
397 DBG_SECURE("Alias Name [%s]", name);
399 tmp_value = g_variant_lookup_value(value, "Name", G_VARIANT_TYPE_STRING);
400 g_variant_get(tmp_value, "s", &name);
401 g_variant_unref(tmp_value);
404 tmp_value = g_variant_lookup_value(value, "Class", G_VARIANT_TYPE_UINT32);
405 dev_info->class = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
406 g_variant_unref(tmp_value);
408 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BOOLEAN);
409 dev_info->connected = tmp_value ? g_variant_get_boolean(tmp_value) : BLUETOOTH_CONNECTED_LINK_NONE;
410 g_variant_unref(tmp_value);
412 tmp_value = g_variant_lookup_value(value, "Trusted", G_VARIANT_TYPE_BOOLEAN);
413 dev_info->trust = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
414 g_variant_unref(tmp_value);
416 tmp_value = g_variant_lookup_value(value, "Paired", G_VARIANT_TYPE_BOOLEAN);
417 dev_info->paired = tmp_value ? g_variant_get_boolean(tmp_value) : FALSE;
418 g_variant_unref(tmp_value);
420 BT_DBG("Paired %d", dev_info->paired);
422 tmp_value = g_variant_lookup_value(value, "RSSI", G_VARIANT_TYPE_INT32);
423 dev_info->rssi = tmp_value ? g_variant_get_int32(tmp_value) : 0;
424 g_variant_unref(tmp_value);
426 tmp_value = g_variant_lookup_value(value, "LastAddrType", G_VARIANT_TYPE_UINT32);
427 dev_info->addr_type = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
428 g_variant_unref(tmp_value);
430 tmp_value = g_variant_lookup_value(value, "UUIDs", G_VARIANT_TYPE_STRING_ARRAY);
431 __bt_get_uuids(tmp_value, dev_info);
432 g_variant_unref(tmp_value);
434 tmp_value = g_variant_lookup_value(value, "ManufacturerDataLen", G_VARIANT_TYPE_UINT32);
435 dev_info->manufacturer_data_len = tmp_value ? g_variant_get_uint32(tmp_value) : 0;
436 if (dev_info->manufacturer_data_len > BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX) {
437 BT_ERR("manufacturer_data_len is too long(len = %d)", dev_info->manufacturer_data_len);
438 dev_info->manufacturer_data_len = BLUETOOTH_MANUFACTURER_DATA_LENGTH_MAX;
440 g_variant_unref(tmp_value);
442 tmp_value = g_variant_lookup_value(value, "ManufacturerData", G_VARIANT_TYPE_BYTESTRING);
443 manufacturer_data = value ? (gchar *)g_variant_get_bytestring(tmp_value) : NULL;
444 if (manufacturer_data) {
445 if (dev_info->manufacturer_data_len > 0) {
446 dev_info->manufacturer_data = g_malloc0(dev_info->manufacturer_data_len);
447 memcpy(dev_info->manufacturer_data, manufacturer_data, dev_info->manufacturer_data_len);
450 g_variant_unref(tmp_value);
452 dev_info->address = g_strdup(address);
453 dev_info->name = g_strdup(name);
455 g_variant_unref(value);
457 BT_ERR("result is NULL\n");
465 char *_bt_get_bonded_device_name(char *address)
467 bluetooth_device_address_t device_address = { {0} };
468 bluetooth_device_info_t dev_info;
470 retv_if(address == NULL, strdup(""));
472 _bt_convert_addr_string_to_type(device_address.addr, address);
474 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
476 _bt_get_bonded_device_info(&device_address, &dev_info);
478 return g_strdup(dev_info.device_name.name);
481 static gboolean __ignore_auto_pairing_request(const char *address)
486 char lap_address[BT_LOWER_ADDRESS_LENGTH + 1] = {0,};
497 /* Get the LAP(Lower Address part) */
498 /* User BT_LOWER_ADDRESS_LENGTH+1 for lap_address to accomodate
500 snprintf(lap_address, sizeof(lap_address), ",%s", address);
502 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "r");
505 BT_ERR("fopen failed \n");
509 fseek(fp, 0, SEEK_END);
514 BT_ERR("Get file size failed \n");
519 buffer = g_malloc0(sizeof(char) * size);
520 /* Fix : NULL_RETURNS */
521 if (buffer == NULL) {
522 BT_ERR("Memory allocation error\n");
526 result = fread((char *)buffer, 1, size, fp);
528 if (result != size) {
529 BT_ERR("Read Error\n");
534 BT_DBG("Buffer = %s\n", buffer);
536 lines = g_strsplit_set(buffer, BT_AGENT_NEW_LINE, 0);
542 /* Write the data and insert new device data */
543 for (i = 0; lines[i] != NULL; i++) {
544 if (g_str_has_prefix(lines[i], "AddressBlacklist")) {
545 temp_buffer = g_strconcat(lines[i], lap_address, NULL);
547 lines[i] = temp_buffer;
550 buffer = g_strjoinv(BT_AGENT_NEW_LINE, lines);
552 /* Fix : NULL_RETURNS */
553 retv_if(buffer == NULL, FALSE);
555 fp = fopen(BT_AGENT_AUTO_PAIR_BLACKLIST_FILE, "w");
558 BT_ERR("fopen failed \n");
563 BT_DBG("Buffer = %s\n", buffer);
564 fwrite(buffer, 1, strlen(buffer), fp);
574 static int __bt_retry_bond(void)
576 BT_CHECK_PARAMETER(bonding_info, return);
577 BT_CHECK_PARAMETER(bonding_info->addr, return);
579 g_dbus_proxy_call(bonding_info->device_proxy, "Pair",
580 g_variant_new("(y)", bonding_info->conn_type),
581 G_DBUS_CALL_FLAGS_NONE,
584 (GAsyncReadyCallback)__bt_bond_device_cb,
587 return BLUETOOTH_ERROR_NONE;
591 static int __bt_remove_and_bond(char *device_path)
593 GDBusProxy *adapter_proxy;
596 BT_CHECK_PARAMETER(bonding_info, return);
597 BT_CHECK_PARAMETER(bonding_info->addr, return);
599 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
601 adapter_proxy = _bt_get_adapter_proxy();
602 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
604 g_dbus_proxy_call_sync(adapter_proxy, "UnpairDevice",
605 g_variant_new("(o)", device_path),
606 G_DBUS_CALL_FLAGS_NONE,
612 BT_ERR("UnpairDevice Fail: %s", err->message);
614 return BLUETOOTH_ERROR_INTERNAL;
617 return __bt_retry_bond();
620 static int __bt_cancel_and_bond(void)
622 int ret = BLUETOOTH_ERROR_NONE;
624 ret = _bt_agent_reply_cancellation();
625 if (ret != BLUETOOTH_ERROR_NONE) {
626 BT_ERR("Fail to call reply cancellation");
630 return __bt_retry_bond();
634 static void __bt_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
637 int result = BLUETOOTH_ERROR_NONE;
639 GVariant *out_param1;
640 request_info_t *req_info;
641 bluetooth_device_info_t dev_info;
642 bt_remote_dev_info_t *remote_dev_info;
643 GVariant *manufacture_data;
645 const char *device_path;
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");
670 device_path = g_dbus_proxy_get_object_path(proxy);
672 BT_DBG("device path: %s", device_path);
675 g_dbus_error_strip_remote_error(err);
676 BT_ERR("Error occured in CreateBonding [%s]", err->message);
678 if (g_strrstr(err->message, "Already Exists")) {
679 BT_INFO("Existing Bond, remove and retry");
680 ret_if(__bt_remove_and_bond((char *)device_path) == BLUETOOTH_ERROR_NONE);
682 BT_INFO("Fail to retry bonding");
684 result = BLUETOOTH_ERROR_PARING_FAILED;
685 } else if (_bt_agent_is_canceled() ||
686 g_strrstr(err->message, "Authentication Canceled")) {
687 BT_INFO("Cancelled by USER");
688 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
689 } else if (g_strrstr(err->message, "Authentication Rejected")) {
691 result = BLUETOOTH_ERROR_ACCESS_DENIED;
692 } else if (g_strrstr(err->message, "In Progress")) {
693 BT_INFO("Bond in progress, cancel and retry");
694 ret_if(__bt_cancel_and_bond() == BLUETOOTH_ERROR_NONE);
696 result = BLUETOOTH_ERROR_PARING_FAILED;
697 } else if (g_strrstr(err->message, "Authentication Failed")) {
698 BT_INFO("Authentication Failed");
699 if (bonding_info->is_autopair == TRUE) {
700 _bt_set_autopair_status_in_bonding_info(FALSE);
701 __ignore_auto_pairing_request(bonding_info->addr);
703 result = BLUETOOTH_ERROR_AUTHENTICATION_FAILED;
704 } else if (g_strrstr(err->message, "Page Timeout")) {
705 BT_INFO("Page Timeout");
706 /* This is the special case
707 As soon as call bluetooth_bond_device, try to cancel bonding.
708 In this case, before completing to call 'CreatePairedDevice' method
709 the procedure is stopped. So 'Cancle' error is not return.
711 result = BLUETOOTH_ERROR_HOST_DOWN;
712 } else if (g_strrstr(err->message, BT_TIMEOUT_MESSAGE)) {
713 g_dbus_proxy_call_sync(proxy, "CancelDeviceCreation",
714 g_variant_new("(s)", bonding_info->addr),
715 G_DBUS_CALL_FLAGS_NONE,
720 result = BLUETOOTH_ERROR_INTERNAL;
721 } else if (g_strrstr(err->message, "Connection Timeout")) {
722 /* Pairing request timeout */
723 result = BLUETOOTH_ERROR_TIMEOUT;
724 } else if (g_strrstr(err->message, "Authentication Timeout")) {
725 /* Pairing request timeout */
726 result = BLUETOOTH_ERROR_TIMEOUT;
728 BT_DBG("Default case");
729 result = BLUETOOTH_ERROR_PARING_FAILED;
733 if (result == BLUETOOTH_ERROR_PARING_FAILED ||
734 result == BLUETOOTH_ERROR_AUTHENTICATION_FAILED ||
735 result == BLUETOOTH_ERROR_TIMEOUT ||
736 result == BLUETOOTH_ERROR_HOST_DOWN) {
737 bonding_info->result = result;
738 #ifdef TIZEN_WEARABLE
739 __bt_launch_unable_to_pairing_syspopup(result);
743 g_object_unref(proxy);
744 bonding_info->device_proxy = NULL;
746 if (result != BLUETOOTH_ERROR_NONE)
749 remote_dev_info = _bt_get_remote_device_info(bonding_info->addr);
750 if (remote_dev_info == NULL)
753 GVariant *uuids = NULL;
754 GVariantBuilder *builder = NULL;
756 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
757 for (i = 0; i < remote_dev_info->uuid_count; i++) {
758 g_variant_builder_add(builder, "s",
759 remote_dev_info->uuids[i]);
761 uuids = g_variant_new("as", builder);
762 g_variant_builder_unref(builder);
763 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
764 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
767 param = g_variant_new("(isunsbub@asn@ay)",
770 remote_dev_info->class,
771 remote_dev_info->rssi,
772 remote_dev_info->name,
773 remote_dev_info->paired,
774 remote_dev_info->connected,
775 remote_dev_info->trust,
777 remote_dev_info->manufacturer_data_len,
781 /* Send the event to application */
782 _bt_send_event(BT_ADAPTER_EVENT,
783 BLUETOOTH_EVENT_BONDING_FINISHED,
786 _bt_free_device_info(remote_dev_info);
789 if (req_info->context == NULL)
792 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
793 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
796 if (_bt_adapter_get_status() != BT_ACTIVATED)
797 result = BLUETOOTH_ERROR_NOT_IN_OPERATION;
799 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
800 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
802 g_dbus_method_invocation_return_value(req_info->context,
803 g_variant_new("(iv)", result, out_param1));
805 g_variant_unref(out_param1);
806 // g_variant_unref(out_param2);
808 _bt_delete_request_list(req_info->req_id);
813 _bt_agent_set_canceled(FALSE);
816 g_free(bonding_info->addr);
817 g_free(bonding_info);
821 int _bt_bond_device(int request_id,
822 bluetooth_device_address_t *device_address,
823 unsigned short conn_type, GArray **out_param1)
826 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
827 bluetooth_device_info_t dev_info;
829 GDBusConnection *conn;
830 char *device_path = NULL;
831 GDBusProxy *adapter_proxy;
832 GError *error = NULL;
834 BT_CHECK_PARAMETER(device_address, return);
837 BT_ERR("Bonding in progress");
839 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
840 memcpy(dev_info.device_address.addr, device_address->addr,
841 BLUETOOTH_ADDRESS_LENGTH);
843 g_array_append_vals(*out_param1, &dev_info,
844 sizeof(bluetooth_device_info_t));
846 return BLUETOOTH_ERROR_DEVICE_BUSY;
849 conn = _bt_get_system_gconn();
850 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
852 _bt_convert_addr_type_to_string(address, device_address->addr);
854 device_path = _bt_get_device_object_path(address);
856 if (device_path == NULL) {
857 BT_ERR("No searched device");
858 GVariant *ret = NULL;
859 adapter_proxy = _bt_get_adapter_proxy();
860 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
862 ret = g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
863 g_variant_new("(s)", address),
864 G_DBUS_CALL_FLAGS_NONE,
870 BT_ERR("CreateDevice Fail: %s", error->message);
871 g_clear_error(&error);
874 g_variant_unref(ret);
875 device_path = _bt_get_device_object_path(address);
876 if (device_path == NULL) {
877 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
878 memcpy(dev_info.device_address.addr, device_address->addr,
879 BLUETOOTH_ADDRESS_LENGTH);
881 g_array_append_vals(*out_param1, &dev_info,
882 sizeof(bluetooth_device_info_t));
884 return BLUETOOTH_ERROR_NOT_PAIRED;
886 BT_INFO("device_path is created[%s]", device_path);
890 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
892 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
895 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
897 bonding_info = g_malloc0(sizeof(bt_funcion_data_t));
898 bonding_info->addr = g_strdup(address);
899 bonding_info->req_id = request_id;
901 bonding_info->device_proxy = proxy;
902 bonding_info->conn_type = conn_type;
904 is_device_creating = TRUE;
906 g_dbus_proxy_call(proxy, "Pair",
907 g_variant_new("(y)", conn_type),
908 G_DBUS_CALL_FLAGS_NONE,
911 (GAsyncReadyCallback)__bt_bond_device_cb,
914 /* TODO: We need to check if we can pair the specific device using 'pair' API of bluez 5.x */
916 return BLUETOOTH_ERROR_NONE;
918 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
919 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
922 g_array_append_vals(*out_param1, &dev_info,
923 sizeof(bluetooth_device_info_t));
925 is_device_creating = FALSE;
927 g_free(bonding_info->addr);
928 g_free(bonding_info);
931 return BLUETOOTH_ERROR_INTERNAL;*/
934 int _bt_cancel_bonding(void)
936 int ret = BLUETOOTH_ERROR_NONE;
938 retv_if(bonding_info == NULL, BLUETOOTH_ERROR_NOT_IN_OPERATION);
940 ret = _bt_agent_reply_cancellation();
941 if (ret != BLUETOOTH_ERROR_NONE) {
942 BT_ERR("Fail to call reply cancellation");
946 _bt_agent_set_canceled(TRUE);
948 return BLUETOOTH_ERROR_NONE;
951 static void __bt_unbond_cb(GDBusProxy *proxy, GAsyncResult *res,
955 GVariant *out_param1;
956 int result = BLUETOOTH_ERROR_NONE;
957 bt_funcion_data_t *unbonding_info;
958 bluetooth_device_info_t dev_info;
959 request_info_t *req_info;
961 g_dbus_proxy_call_finish(proxy, res, &err);
963 unbonding_info = user_data;
965 if (unbonding_info == NULL) {
967 BT_ERR("unbonding_info == NULL");
971 req_info = _bt_get_request_info(unbonding_info->req_id);
972 if (req_info == NULL) {
973 BT_ERR("req_info == NULL");
978 BT_ERR("Error occured in RemoveBonding [%s]\n", err->message);
979 result = BLUETOOTH_ERROR_INTERNAL;
982 if (req_info->context == NULL)
985 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
986 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
987 unbonding_info->addr);
989 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
990 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
992 g_dbus_method_invocation_return_value(req_info->context,
993 g_variant_new("(iv)", result, out_param1));
996 _bt_delete_request_list(req_info->req_id);
1002 if (unbonding_info) {
1003 g_free(unbonding_info->addr);
1004 g_free(unbonding_info);
1008 int _bt_unbond_device(int request_id,
1009 bluetooth_device_address_t *device_address,
1010 GArray **out_param1)
1012 char *device_path = NULL;
1013 bt_funcion_data_t *unbonding_info;
1014 GDBusProxy *adapter_proxy = NULL;
1015 GDBusProxy *device_proxy = NULL;
1016 GDBusConnection *conn;
1017 int result = BLUETOOTH_ERROR_INTERNAL;
1018 bluetooth_device_info_t dev_info;
1019 GError *error = NULL;
1020 GVariant *ret = NULL;
1022 BT_CHECK_PARAMETER(device_address, return);
1024 adapter_proxy = _bt_get_adapter_proxy();
1025 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1027 /* allocate user data so that it can be retrieved in callback */
1028 unbonding_info = g_malloc0(sizeof(bt_funcion_data_t));
1029 /* Fix : NULL_RETURNS */
1030 if (unbonding_info == NULL) {
1031 BT_ERR("Memory not allocated !");
1032 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
1035 unbonding_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1036 unbonding_info->req_id = request_id;
1038 _bt_convert_addr_type_to_string(unbonding_info->addr,
1039 device_address->addr);
1041 device_path = _bt_get_device_object_path(unbonding_info->addr);
1043 if (device_path == NULL) {
1044 BT_ERR("No paired device");
1045 result = BLUETOOTH_ERROR_NOT_PAIRED;
1049 conn = _bt_get_system_gconn();
1051 BT_ERR("conn is NULL");
1052 result = BLUETOOTH_ERROR_INTERNAL;
1056 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1057 NULL, BT_BLUEZ_NAME,
1058 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1060 if (device_proxy != NULL) {
1062 ret = g_dbus_proxy_call_sync(device_proxy, "Get",
1063 g_variant_new("(ss)", BT_DEVICE_INTERFACE, "Paired"),
1064 G_DBUS_CALL_FLAGS_NONE,
1069 BT_ERR("Getting property failed: [%s]\n", error->message);
1070 g_error_free(error);
1071 result = BLUETOOTH_ERROR_NOT_PAIRED;
1075 BT_ERR("No paired device");
1076 g_object_unref(device_proxy);
1077 result = BLUETOOTH_ERROR_NOT_PAIRED;
1080 g_variant_unref(ret);
1082 g_object_unref(device_proxy);
1085 g_dbus_proxy_call(adapter_proxy, "UnpairDevice",
1086 g_variant_new("(o)", device_path),
1087 G_DBUS_CALL_FLAGS_NONE,
1088 BT_MAX_DBUS_TIMEOUT,
1090 (GAsyncReadyCallback)__bt_unbond_cb,
1093 g_free(device_path);
1094 return BLUETOOTH_ERROR_NONE;
1097 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1098 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1099 unbonding_info->addr);
1101 g_array_append_vals(*out_param1, &dev_info,
1102 sizeof(bluetooth_device_info_t));
1103 g_free(device_path);
1104 g_free(unbonding_info->addr);
1105 g_free(unbonding_info);
1109 static void __bt_discover_cb(GDBusProxy *proxy, GAsyncResult *res,
1113 GVariant *out_param1;
1114 int result = BLUETOOTH_ERROR_NONE;
1115 bluetooth_device_info_t dev_info;
1116 bt_remote_dev_info_t *remote_dev_info;
1117 request_info_t *req_info;
1118 GVariant *uuid_list, *manufacture_data;
1120 GVariantBuilder *builder = NULL;
1123 g_dbus_proxy_call_finish(proxy, res, &err);
1125 g_object_unref(proxy);
1127 if (searching_info == NULL) {
1129 BT_ERR("unbonding_info == NULL");
1133 req_info = _bt_get_request_info(searching_info->req_id);
1134 if (req_info == NULL) {
1135 BT_ERR("req_info == NULL");
1140 g_dbus_error_strip_remote_error(err);
1141 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
1143 if (g_strrstr("Operation canceled", err->message)) {
1144 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1145 } else if (g_strrstr("In Progress", err->message)) {
1146 result = BLUETOOTH_ERROR_IN_PROGRESS;
1147 } else if (g_strrstr("Host is down", err->message)) {
1148 result = BLUETOOTH_ERROR_HOST_DOWN;
1150 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1153 if (result == BLUETOOTH_ERROR_HOST_DOWN ||
1154 result == BLUETOOTH_ERROR_CONNECTION_ERROR) {
1155 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1156 if (remote_dev_info && remote_dev_info->uuids != NULL &&
1157 remote_dev_info->uuid_count > 0) {
1158 result = BLUETOOTH_ERROR_NONE;
1161 _bt_free_device_info(remote_dev_info);
1166 remote_dev_info = _bt_get_remote_device_info(searching_info->addr);
1167 if (remote_dev_info == NULL)
1171 builder = g_variant_builder_new(G_VARIANT_TYPE("as"));
1172 for (i = 0; i < remote_dev_info->uuid_count; i++) {
1173 g_variant_builder_add(builder, "s",
1174 remote_dev_info->uuids[i]);
1176 uuid_list = g_variant_new("as", builder);
1177 g_variant_builder_unref(builder);
1178 manufacture_data = g_variant_new_from_data((const GVariantType *)"ay",
1179 remote_dev_info->manufacturer_data, remote_dev_info->manufacturer_data_len,
1182 param = g_variant_new("(isunsbub@asn@ay)",
1184 searching_info->addr,
1185 remote_dev_info->class,
1186 remote_dev_info->rssi,
1187 remote_dev_info->name,
1188 remote_dev_info->paired,
1189 remote_dev_info->connected,
1190 remote_dev_info->trust,
1192 remote_dev_info->manufacturer_data_len,
1195 /* Send the event to application */
1196 _bt_send_event(BT_ADAPTER_EVENT,
1197 BLUETOOTH_EVENT_SERVICE_SEARCHED,
1200 _bt_free_device_info(remote_dev_info);
1203 if (req_info->context == NULL)
1206 memset(&dev_info, 0x00, sizeof(bluetooth_device_info_t));
1207 _bt_convert_addr_string_to_type(dev_info.device_address.addr,
1208 searching_info->addr);
1210 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1211 &dev_info, sizeof(bluetooth_device_info_t), TRUE, NULL, NULL);
1213 g_dbus_method_invocation_return_value(req_info->context,
1214 g_variant_new("(iv)", result, out_param1));
1217 _bt_delete_request_list(req_info->req_id);
1222 if (searching_info) {
1223 g_free(searching_info->addr);
1224 g_free(searching_info);
1225 searching_info = NULL;
1229 int _bt_search_device(int request_id,
1230 bluetooth_device_address_t *device_address)
1232 char *device_path = NULL;
1233 GDBusProxy *device_proxy = NULL;
1234 GDBusConnection *conn;
1236 GDBusProxy *adapter_proxy;
1237 int result = BLUETOOTH_ERROR_INTERNAL;
1239 BT_CHECK_PARAMETER(device_address, return);
1242 BT_ERR("Bonding in progress");
1243 return BLUETOOTH_ERROR_DEVICE_BUSY;
1246 if (searching_info) {
1247 BT_ERR("Service searching in progress");
1248 return BLUETOOTH_ERROR_DEVICE_BUSY;
1251 adapter_proxy = _bt_get_adapter_proxy();
1252 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1254 /* allocate user data so that it can be retrieved in callback */
1255 searching_info = g_malloc0(sizeof(bt_funcion_data_t));
1256 searching_info->addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
1257 searching_info->req_id = request_id;
1259 _bt_convert_addr_type_to_string(searching_info->addr,
1260 device_address->addr);
1262 conn = _bt_get_system_gconn();
1263 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1266 device_path = _bt_get_device_object_path(searching_info->addr);
1268 if (device_path == NULL) {
1269 BT_ERR("No paired device");
1270 result = BLUETOOTH_ERROR_NOT_PAIRED;
1274 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1275 NULL, BT_BLUEZ_NAME,
1276 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1277 g_free(device_path);
1278 if (device_proxy == NULL) {
1279 result = BLUETOOTH_ERROR_INTERNAL;
1283 g_dbus_proxy_call(device_proxy, "DiscoverServices",
1284 g_variant_new("(s)", ""),
1285 G_DBUS_CALL_FLAGS_NONE,
1286 BT_MAX_DBUS_TIMEOUT,
1288 (GAsyncReadyCallback)__bt_discover_cb,
1291 searching_info->device_proxy = device_proxy;
1293 return BLUETOOTH_ERROR_NONE;
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",
1477 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1478 G_DBUS_CALL_FLAGS_NONE,
1482 if (error != NULL || result == NULL) {
1483 if (error != NULL) {
1484 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1485 g_error_free(error);
1487 g_object_unref(device_proxy);
1488 return BLUETOOTH_ERROR_INTERNAL;
1491 g_variant_get(result , "(@a{sv})", &value);
1492 g_variant_unref(result);
1494 tmp_value = g_variant_lookup_value(value, "GattConnected", G_VARIANT_TYPE_BOOLEAN);
1496 if (tmp_value == NULL) {
1497 g_object_unref(device_proxy);
1498 return BLUETOOTH_ERROR_INTERNAL;
1501 *is_connected = g_variant_get_boolean(tmp_value);
1503 BT_DBG("gatt is connected : %d", *is_connected);
1504 g_variant_unref(tmp_value);
1505 g_variant_unref(value);
1506 g_object_unref(device_proxy);
1511 int _bt_is_device_connected(bluetooth_device_address_t *device_address,
1512 int connection_type, gboolean *is_connected)
1514 char *object_path = NULL;
1515 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1516 GDBusProxy *device_proxy = NULL;
1517 GDBusProxy *adapter_proxy = NULL;
1518 GDBusConnection *conn;
1519 GError *error = NULL;
1520 GVariant *tmp_value = NULL;
1521 GVariant *value = NULL;
1522 GVariant *result = NULL;
1525 retv_if(device_address == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1526 retv_if(is_connected == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
1528 *is_connected = FALSE;
1529 BT_DBG("connection_type: %d", connection_type);
1530 if (connection_type == BLUETOOTH_RFCOMM_SERVICE)
1531 return _bt_rfcomm_is_device_connected(device_address,
1533 else if (connection_type == BLUETOOTH_GATT_SERVICE)
1534 return _bt_is_gatt_connected(device_address, is_connected);
1536 adapter_proxy = _bt_get_adapter_proxy();
1537 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1539 conn = _bt_get_system_gconn();
1540 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1542 _bt_convert_addr_type_to_string(address, device_address->addr);
1544 if (connection_type == BLUETOOTH_NAP_SERVER_SERVICE) {
1545 object_path = _bt_get_adapter_path();
1546 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1547 NULL, BT_BLUEZ_NAME,
1548 object_path, BT_NETWORK_SERVER_INTERFACE, NULL, NULL);
1549 g_free(object_path);
1550 if (device_proxy == NULL) {
1551 BT_DBG("Device don't have this service");
1552 return BLUETOOTH_ERROR_NONE;
1555 result = g_dbus_proxy_call_sync(device_proxy, "GetProperties",
1556 g_variant_new("(s)", address),
1557 G_DBUS_CALL_FLAGS_NONE,
1562 if (result == NULL) {
1563 BT_ERR("Error occured in Proxy call");
1565 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1566 g_error_free(error);
1568 g_object_unref(device_proxy);
1569 return BLUETOOTH_ERROR_NONE;
1571 g_variant_get(result , "(@a{sv})", &value);
1572 g_variant_unref(result);
1575 tmp_value = g_variant_lookup_value(value,
1577 G_VARIANT_TYPE_BOOLEAN);
1579 *is_connected = g_variant_get_boolean(tmp_value);
1580 g_variant_unref(tmp_value);
1582 g_variant_unref(value);
1584 } else if (connection_type == BLUETOOTH_NAP_SERVICE) {
1585 return _bt_is_network_connected(_bt_get_net_conn(),
1586 device_address->addr, is_connected);
1588 uuid = _bt_get_profile_uuid128(connection_type);
1590 BT_ERR("uuid is NULL");
1591 return BLUETOOTH_ERROR_NONE;
1594 BT_DBG("uuid: %s", uuid);
1596 object_path = _bt_get_device_object_path(address);
1597 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1598 BT_DBG("object_path: %s", object_path);
1599 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1600 NULL, BT_BLUEZ_NAME,
1601 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
1602 g_free(object_path);
1603 if (device_proxy == NULL) {
1604 BT_DBG("Device don't have this service");
1606 return BLUETOOTH_ERROR_NONE;
1609 result = g_dbus_proxy_call_sync(device_proxy, "IsConnectedProfile",
1610 g_variant_new("(s)", uuid),
1611 G_DBUS_CALL_FLAGS_NONE,
1616 if (result == NULL) {
1617 BT_ERR("Error occured in Proxy call");
1619 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1620 g_error_free(error);
1623 g_variant_get(result, "(b)", is_connected);
1625 g_variant_unref(result);
1629 g_object_unref(device_proxy);
1630 return BLUETOOTH_ERROR_NONE;
1633 int _bt_get_connected_link(bluetooth_device_address_t *device_address,
1634 bluetooth_connected_link_t *connected)
1636 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1637 char *object_path = NULL;
1639 GDBusProxy *device_proxy;
1640 GError *error = NULL;
1641 GDBusConnection *conn;
1642 GVariant *tmp_value = NULL;
1643 GVariant *value = NULL;
1644 GVariant *result = NULL;
1646 int ret = BLUETOOTH_ERROR_NONE;
1648 BT_CHECK_PARAMETER(device_address, return);
1650 conn = _bt_get_system_gconn();
1651 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1653 _bt_convert_addr_type_to_string(address, device_address->addr);
1655 object_path = _bt_get_device_object_path(address);
1656 retv_if(object_path == NULL, BLUETOOTH_ERROR_NOT_PAIRED);
1658 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1659 NULL, BT_BLUEZ_NAME,
1660 object_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
1661 g_free(object_path);
1662 if (device_proxy == NULL) {
1663 *connected = BLUETOOTH_CONNECTED_LINK_NONE;
1664 return BLUETOOTH_ERROR_NONE;
1667 result = g_dbus_proxy_call_sync(device_proxy, "GetAll",
1668 g_variant_new("(s)", BT_DEVICE_INTERFACE),
1669 G_DBUS_CALL_FLAGS_NONE,
1674 if (error != NULL) {
1675 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
1676 g_error_free(error);
1677 g_object_unref(device_proxy);
1678 return BLUETOOTH_ERROR_INTERNAL;
1681 g_variant_get(result , "(@a{sv})", &value);
1682 g_variant_unref(result);
1684 tmp_value = g_variant_lookup_value(value, "Connected", G_VARIANT_TYPE_BOOLEAN);
1685 if (tmp_value != NULL) {
1686 g_object_unref(device_proxy);
1687 g_variant_unref(value);
1688 return BLUETOOTH_ERROR_INTERNAL;
1690 *connected = g_variant_get_boolean(tmp_value);
1691 g_variant_unref(tmp_value);
1692 g_variant_unref(value);
1693 g_object_unref(device_proxy);
1698 static void __bt_connect_le_device_cb(GDBusProxy *proxy, GAsyncResult *res,
1702 GVariant *out_param1;
1703 request_info_t *req_info;
1704 bluetooth_device_address_t device_addr = { {0} };
1705 int result = BLUETOOTH_ERROR_NONE;
1708 g_dbus_proxy_call_finish(proxy, res, &err);
1710 /* When cb datas of lescan are received, back to parameta values for Web TCT */
1711 if (_bt_is_set_scan_parameter() == FALSE) {
1712 bluetooth_le_scan_params_t scan_params;
1713 BT_ERR("set parameter of lescan when receive cb data");
1714 scan_params.type = BT_LE_ACTIVE_SCAN;
1715 scan_params.interval = 5000;
1716 scan_params.window = 500;
1717 _bt_set_scan_parameters(&scan_params);
1720 req_info = _bt_get_request_info(le_connection_info->req_id);
1721 if (req_info == NULL) {
1722 BT_ERR("req_info == NULL");
1723 g_object_unref(proxy);
1724 le_connection_info->device_proxy = NULL;
1729 BT_ERR("Error occured in ConnectLE [%s]", err->message);
1731 if (g_strrstr(err->message, "NotSupported")) {
1732 BT_INFO("Connection Not Supported");
1733 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1734 } else if (g_strrstr(err->message, "AlreadyConnected")) {
1735 BT_INFO("Connection already exists");
1736 result = BLUETOOTH_ERROR_ALREADY_CONNECT;
1737 } else if (g_strrstr(err->message, "Connection Timeout")) {
1738 BT_INFO("Connection Timeout");
1739 result = BLUETOOTH_ERROR_TIMEOUT;
1741 BT_DBG("Default case");
1742 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1746 g_object_unref(proxy);
1747 le_connection_info->device_proxy = NULL;
1749 _bt_convert_addr_string_to_type(device_addr.addr,
1750 le_connection_info->addr);
1752 if (req_info->context == NULL)
1755 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1756 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
1757 g_dbus_method_invocation_return_value(req_info->context,
1758 g_variant_new("(iv)", result, out_param1));
1760 g_variant_unref(out_param1);
1762 _bt_delete_request_list(req_info->req_id);
1767 g_free(le_connection_info->addr);
1768 g_free(le_connection_info);
1769 le_connection_info = NULL;
1774 int _bt_connect_le_device(int request_id,
1775 const bluetooth_device_address_t *bd_addr,
1776 gboolean auto_connect)
1778 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1779 gchar *device_path = NULL;
1780 GDBusProxy *device_proxy = NULL;
1781 GDBusProxy *adapter_proxy;
1782 GDBusConnection *conn;
1783 int ret = BLUETOOTH_ERROR_NONE;
1785 BT_CHECK_PARAMETER(bd_addr, return);
1787 _bt_convert_addr_type_to_string(device_address,
1788 (unsigned char *)bd_addr->addr);
1790 if (le_connection_info) {
1791 BT_ERR("LE Connection in progress");
1792 return BLUETOOTH_ERROR_DEVICE_BUSY;
1795 /* When auto_connect is true, Web TCT fail. It need to be checked */
1796 auto_connect = FALSE;
1798 conn = _bt_get_system_gconn();
1799 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1801 adapter_proxy = _bt_get_adapter_proxy();
1802 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1804 device_path = _bt_get_device_object_path(device_address);
1805 if (device_path == NULL) {
1806 BT_DBG("device_path NULL");
1807 ret = BLUETOOTH_ERROR_INTERNAL;
1811 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1813 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1814 NULL, BT_BLUEZ_NAME,
1815 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1816 g_free(device_path);
1817 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1820 le_connection_info = g_malloc0(sizeof(bt_funcion_data_t));
1821 le_connection_info->addr = g_strdup(device_address);
1822 le_connection_info->req_id = request_id;
1823 le_connection_info->device_proxy = device_proxy;
1825 /* The connection between Ble device and TM1 often fail. So It need to be set for Web TCT*/
1826 if (_bt_is_set_scan_parameter() == FALSE) {
1827 BT_ERR("set parameter of le scan when connect device");
1828 bluetooth_le_scan_params_t scan_params;
1829 scan_params.type = BT_LE_ACTIVE_SCAN;
1830 scan_params.interval = 60;
1831 scan_params.window = 60;
1832 _bt_set_scan_parameters(&scan_params);
1835 auto_connect = FALSE;
1837 g_dbus_proxy_call(device_proxy, "ConnectLE",
1838 g_variant_new("(b)", auto_connect),
1839 G_DBUS_CALL_FLAGS_NONE,
1840 BT_MAX_DBUS_TIMEOUT,
1842 (GAsyncReadyCallback)__bt_connect_le_device_cb,
1849 static void __bt_disconnect_le_device_cb(GDBusProxy *proxy, GAsyncResult *res,
1853 GVariant *out_param1;
1854 request_info_t *req_info;
1855 bt_funcion_data_t *le_disconnection_info = user_data;
1856 bluetooth_device_address_t device_addr = { {0} };
1857 int result = BLUETOOTH_ERROR_NONE;
1860 g_dbus_proxy_call_finish(proxy, res, &err);
1862 req_info = _bt_get_request_info(le_disconnection_info->req_id);
1863 if (req_info == NULL) {
1864 BT_ERR("req_info == NULL");
1865 g_object_unref(proxy);
1866 le_disconnection_info->device_proxy = NULL;
1871 BT_ERR("Error occured in DisconnectLE [%s]", err->message);
1873 if (g_strrstr(err->message, "NotSupported")) {
1874 BT_INFO("Connection Not Supported");
1875 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1876 } else if (g_strrstr(err->message, "NotConnected")) {
1877 BT_INFO("Connection does not exists");
1878 result = BLUETOOTH_ERROR_NOT_CONNECTED;
1879 } else if (g_strrstr(err->message, "Cancelled")) {
1880 BT_INFO("User Cancelled");
1881 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
1883 BT_DBG("Default case");
1884 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
1888 g_object_unref(proxy);
1889 le_disconnection_info->device_proxy = NULL;
1891 _bt_convert_addr_string_to_type(device_addr.addr,
1892 le_disconnection_info->addr);
1894 if (req_info->context == NULL)
1897 out_param1 = g_variant_new_from_data((const GVariantType *)"ay",
1898 &device_addr, sizeof(bluetooth_device_address_t), TRUE, NULL, NULL);
1900 g_dbus_method_invocation_return_value(req_info->context,
1901 g_variant_new("(iv)", result, out_param1));
1903 g_variant_unref(out_param1);
1905 _bt_delete_request_list(req_info->req_id);
1910 g_free(le_disconnection_info->addr);
1911 g_free(le_disconnection_info);
1912 le_disconnection_info = NULL;
1917 int _bt_disconnect_le_device(int request_id,
1918 const bluetooth_device_address_t *bd_addr)
1920 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1921 gchar *device_path = NULL;
1922 GDBusProxy *device_proxy = NULL;
1923 GDBusProxy *adapter_proxy;
1924 GDBusConnection *conn;
1925 int ret = BLUETOOTH_ERROR_NONE;
1926 bt_funcion_data_t *le_disconnection_info = NULL;
1928 BT_CHECK_PARAMETER(bd_addr, return);
1930 _bt_convert_addr_type_to_string(device_address,
1931 (unsigned char *)bd_addr->addr);
1933 conn = _bt_get_system_gconn();
1934 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1936 adapter_proxy = _bt_get_adapter_proxy();
1937 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1939 device_path = _bt_get_device_object_path(device_address);
1940 if (device_path == NULL) {
1941 BT_DBG("device_path NULL");
1942 ret = BLUETOOTH_ERROR_INTERNAL;
1946 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
1948 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
1949 NULL, BT_BLUEZ_NAME,
1950 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
1951 g_free(device_path);
1952 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1954 le_disconnection_info = g_malloc0(sizeof(bt_funcion_data_t));
1955 le_disconnection_info->addr = g_strdup(device_address);
1956 le_disconnection_info->req_id = request_id;
1957 le_disconnection_info->device_proxy = device_proxy;
1959 g_dbus_proxy_call(device_proxy, "DisconnectLE",
1961 G_DBUS_CALL_FLAGS_NONE,
1962 BT_MAX_DBUS_TIMEOUT,
1964 (GAsyncReadyCallback)__bt_disconnect_le_device_cb,
1965 le_disconnection_info);
1970 int _bt_connect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
1972 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
1973 gchar *device_path = NULL;
1974 GError *error = NULL;
1975 GDBusProxy *device_proxy = NULL;
1976 GDBusProxy *adapter_proxy;
1977 GDBusConnection *conn;
1978 int ret = BLUETOOTH_ERROR_NONE;
1980 BT_CHECK_PARAMETER(bd_addr, return);
1982 _bt_convert_addr_type_to_string(device_address,
1983 (unsigned char *)bd_addr->addr);
1985 conn = _bt_get_system_gconn();
1986 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
1988 adapter_proxy = _bt_get_adapter_proxy();
1989 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1991 device_path = _bt_get_device_object_path(device_address);
1992 if (device_path == NULL) {
1993 BT_DBG("device_path NULL");
1994 ret = BLUETOOTH_ERROR_INTERNAL;
1998 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2000 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2001 NULL, BT_BLUEZ_NAME,
2002 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2003 g_free(device_path);
2004 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2006 g_dbus_proxy_call_sync(device_proxy, "ConnectIpsp",
2008 G_DBUS_CALL_FLAGS_NONE,
2013 BT_ERR("ConnectIpsp Call Error %s[%s]", error->message, device_address);
2014 g_error_free(error);
2015 g_object_unref(device_proxy);
2016 return BLUETOOTH_ERROR_INTERNAL;
2019 g_object_unref(device_proxy);
2024 int _bt_disconnect_le_ipsp_device(const bluetooth_device_address_t *bd_addr)
2026 char device_address[BT_ADDRESS_STRING_SIZE] = { 0 };
2027 gchar *device_path = NULL;
2028 GError *error = NULL;
2029 GDBusProxy *device_proxy = NULL;
2030 GDBusProxy *adapter_proxy;
2031 GDBusConnection *conn;
2032 int ret = BLUETOOTH_ERROR_NONE;
2034 BT_CHECK_PARAMETER(bd_addr, return);
2036 _bt_convert_addr_type_to_string(device_address,
2037 (unsigned char *)bd_addr->addr);
2039 conn = _bt_get_system_gconn();
2040 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2042 adapter_proxy = _bt_get_adapter_proxy();
2043 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2045 device_path = _bt_get_device_object_path(device_address);
2046 if (device_path == NULL) {
2047 BT_DBG("device_path NULL");
2048 ret = BLUETOOTH_ERROR_INTERNAL;
2052 retv_if(device_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2054 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2055 NULL, BT_BLUEZ_NAME,
2056 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2057 g_free(device_path);
2058 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2060 g_dbus_proxy_call_sync(device_proxy, "DisconnectIpsp",
2062 G_DBUS_CALL_FLAGS_NONE,
2067 BT_ERR("DisconnectIpsp Call Error %s[%s]", error->message, device_address);
2068 g_error_free(error);
2069 g_object_unref(device_proxy);
2070 return BLUETOOTH_ERROR_INTERNAL;
2073 g_object_unref(device_proxy);
2078 int _bt_connect_profile(char *address, char *uuid,
2079 void *cb, gpointer func_data)
2083 GDBusConnection *conn;
2084 GDBusProxy *adapter_proxy;
2085 GError *error = NULL;
2087 conn = _bt_get_system_gconn();
2088 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2090 object_path = _bt_get_device_object_path(address);
2091 if (object_path == NULL) {
2092 BT_ERR("No searched device");
2094 adapter_proxy = _bt_get_adapter_proxy();
2095 retv_if(adapter_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2097 g_dbus_proxy_call_sync(adapter_proxy, "CreateDevice",
2098 g_variant_new("(s)", address),
2099 G_DBUS_CALL_FLAGS_NONE,
2104 if (error != NULL) {
2105 BT_ERR("CreateDevice Fail: %s", error->message);
2106 g_error_free(error);
2109 object_path = _bt_get_device_object_path(address);
2111 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2113 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2114 NULL, BT_BLUEZ_NAME,
2115 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2116 g_free(object_path);
2117 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2119 g_dbus_proxy_call(proxy, "ConnectProfile",
2120 g_variant_new("(s)", uuid),
2121 G_DBUS_CALL_FLAGS_NONE,
2122 BT_MAX_DBUS_TIMEOUT,
2124 (GAsyncReadyCallback)cb,
2127 return BLUETOOTH_ERROR_NONE;
2130 int _bt_disconnect_profile(char *address, char *uuid,
2131 void *cb, gpointer func_data)
2135 GDBusConnection *conn;
2137 conn = _bt_get_system_gconn();
2138 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
2140 object_path = _bt_get_device_object_path(address);
2141 retv_if(object_path == NULL, BLUETOOTH_ERROR_INTERNAL);
2143 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2144 NULL, BT_BLUEZ_NAME,
2145 object_path, BT_DEVICE_INTERFACE, NULL, NULL);
2146 g_free(object_path);
2147 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2149 g_dbus_proxy_call(proxy, "DisconnectProfile",
2150 g_variant_new("(s)", uuid),
2151 G_DBUS_CALL_FLAGS_NONE,
2152 BT_MAX_DBUS_TIMEOUT,
2154 (GAsyncReadyCallback)cb,
2157 return BLUETOOTH_ERROR_NONE;
2160 int _bt_enable_rssi(bluetooth_device_address_t *bd_addr, int link_type,
2161 int low_threshold, int in_range_threshold, int high_threshold)
2163 int ret = BLUETOOTH_ERROR_NONE;
2165 GError *error = NULL;
2166 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2168 BT_CHECK_PARAMETER(bd_addr, return);
2169 BT_DBG("BD Address [%2.2X %2.2X %2.2X %2.2X %2.2X %2.2X] Link Type[%d]",
2170 bd_addr->addr[0], bd_addr->addr[1],
2171 bd_addr->addr[2], bd_addr->addr[3],
2172 bd_addr->addr[4], bd_addr->addr[5],
2174 BT_DBG("Enable RSSI: [Threshold %d %d %d]", low_threshold,
2175 in_range_threshold, high_threshold);
2177 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2179 proxy = _bt_get_adapter_proxy();
2180 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2182 g_dbus_proxy_call_sync(proxy, "EnableRssi",
2183 g_variant_new("(siiii)", address, link_type, low_threshold, in_range_threshold, high_threshold),
2184 G_DBUS_CALL_FLAGS_NONE,
2188 if (error != NULL) {
2189 BT_ERR("Dbus Call Error:[%s]", error->message);
2190 g_error_free(error);
2191 ret = BLUETOOTH_ERROR_INTERNAL;
2196 int _bt_get_rssi_strength(bluetooth_device_address_t *bd_addr,
2199 int ret = BLUETOOTH_ERROR_NONE;
2201 GError *error = NULL;
2202 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2204 BT_CHECK_PARAMETER(bd_addr, return);
2205 BT_DBG("BD Address [%2.2X:%2.2X:%2.2X:%2.2X:%2.2X:%2.2X] Link Type[%d]",
2206 bd_addr->addr[0], bd_addr->addr[1],
2207 bd_addr->addr[2], bd_addr->addr[3],
2208 bd_addr->addr[4], bd_addr->addr[5],
2211 _bt_convert_addr_type_to_string(address, bd_addr->addr);
2213 proxy = _bt_get_adapter_proxy();
2214 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2216 g_dbus_proxy_call_sync(proxy, "GetRssiStrength",
2217 g_variant_new("(si)", address, link_type),
2218 G_DBUS_CALL_FLAGS_NONE,
2223 if (error != NULL) {
2224 BT_ERR("Dbus Call Error:[%s]", error->message);
2225 g_error_free(error);
2226 ret = BLUETOOTH_ERROR_INTERNAL;
2231 int _bt_le_conn_update(unsigned char *device_address,
2232 guint16 interval_min, guint16 interval_max,
2233 guint16 latency, guint16 time_out)
2235 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2236 gchar *device_path = NULL;
2237 GError *error = NULL;
2238 GDBusProxy *device_proxy = NULL;
2239 GDBusConnection *conn;
2241 guint32 min, max, to;
2242 guint32 min_supervision_to;
2243 int ret = BLUETOOTH_ERROR_NONE;
2247 BT_CHECK_PARAMETER(device_address, return);
2249 BT_DBG("Min interval: %u, Max interval: %u, Latency: %u, Supervision timeout: %u",
2250 interval_min, interval_max, latency, time_out);
2252 if (interval_min > interval_max ||
2253 interval_min < BT_LE_CONN_INTERVAL_MIN ||
2254 interval_max > BT_LE_CONN_INTERVAL_MAX) {
2255 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2259 if (time_out < BT_LE_CONN_SUPER_TO_MIN ||
2260 time_out > BT_LE_CONN_SUPER_TO_MAX) {
2261 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2265 if (latency > BT_LE_CONN_SLAVE_LATENCY_MAX) {
2266 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2271 * The Supervision_Timeout in milliseconds shall be larger than
2272 * (1 + Conn_Latency) * Conn_Interval_Max * 2,
2273 * where Conn_Interval_Max is given in milliseconds.
2275 min_supervision_to = (1 + latency) * interval_max * 2;
2276 if (time_out <= min_supervision_to) {
2277 ret = BLUETOOTH_ERROR_INVALID_PARAM;
2281 _bt_convert_addr_type_to_string(address, device_address);
2283 BT_DBG("Remote device address: %s", address);
2285 device_path = _bt_get_device_object_path(address);
2287 if (device_path == NULL) {
2288 BT_DBG("device_path NULL");
2289 ret = BLUETOOTH_ERROR_INTERNAL;
2293 conn = _bt_get_system_gconn();
2295 BT_DBG("conn NULL");
2296 ret = BLUETOOTH_ERROR_INTERNAL;
2300 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2301 NULL, BT_BLUEZ_NAME,
2302 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2304 g_free(device_path);
2305 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2307 min = interval_min / BT_LE_CONN_INTERVAL_SPLIT;
2308 max = interval_max / BT_LE_CONN_INTERVAL_SPLIT;
2309 to = time_out / BT_LE_CONN_TO_SPLIT;
2311 reply = g_dbus_proxy_call_sync(device_proxy, "LeConnUpdate",
2312 g_variant_new("(uuuu)", min, max, latency, to),
2313 G_DBUS_CALL_FLAGS_NONE,
2318 g_object_unref(device_proxy);
2319 BT_ERR("LeConnUpdate Call Error for %s", address);
2320 if (reply == NULL) {
2322 BT_ERR("Error %s[%s]", error->message, address);
2323 g_error_free(error);
2324 return BLUETOOTH_ERROR_INTERNAL;
2327 g_variant_unref(reply);
2334 int _bt_set_pin_code(bluetooth_device_address_t *device_address,
2335 bluetooth_device_pin_code_t *pin_code)
2337 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2339 bt_pin_code_info_t *pin_info = NULL;
2341 BT_CHECK_PARAMETER(device_address, return);
2342 BT_CHECK_PARAMETER(pin_code, return);
2343 retv_if(g_slist_length(pin_info_list) >= BT_DEVICE_PIN_CODE_SLOT_MAX,
2344 BLUETOOTH_ERROR_NO_RESOURCES);
2346 _bt_convert_addr_type_to_string(address, device_address->addr);
2348 for (l = pin_info_list; l != NULL; l = l->next) {
2351 if (g_strcmp0(pin_info->address, address) == 0) {
2352 g_free(pin_info->pin_code);
2353 pin_info->pin_code = g_strdup(pin_code->pin_code);
2354 return BLUETOOTH_ERROR_NONE;
2358 pin_info = g_malloc0(sizeof(bt_pin_code_info_t));
2359 if (pin_info != NULL) {
2360 pin_info->address = g_strdup(address);
2361 pin_info->pin_code = g_strdup(pin_code->pin_code);
2362 pin_info_list = g_slist_append(pin_info_list, pin_info);
2363 return BLUETOOTH_ERROR_NONE;
2366 return BLUETOOTH_ERROR_INTERNAL;
2369 gint __bt_compare_address(gpointer *a, gpointer *b)
2371 bt_pin_code_info_t *pin_info = (bt_pin_code_info_t *)a;
2372 char *address = (char *)b;
2373 return g_strcmp0(pin_info->address, address);
2376 int _bt_unset_pin_code(bluetooth_device_address_t *device_address)
2378 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2380 bt_pin_code_info_t *pin_info = NULL;
2382 BT_CHECK_PARAMETER(device_address, return);
2384 _bt_convert_addr_type_to_string(address, device_address->addr);
2386 l = g_slist_find_custom(pin_info_list, address,
2387 (GCompareFunc)__bt_compare_address);
2391 pin_info_list = g_slist_remove(pin_info_list, pin_info);
2392 g_free(pin_info->address);
2393 g_free(pin_info->pin_code);
2397 return BLUETOOTH_ERROR_NONE;
2400 int _bt_get_device_pin_code(const char *address, char *pin_code)
2404 BT_CHECK_PARAMETER(address, return);
2405 BT_CHECK_PARAMETER(pin_code, return);
2407 for (l = pin_info_list; l != NULL; l = l->next) {
2408 bt_pin_code_info_t *pin_info = l->data;
2410 if (g_strcmp0(pin_info->address, address) == 0) {
2411 g_strlcpy(pin_code, pin_info->pin_code,
2412 BLUETOOTH_PIN_CODE_MAX_LENGTH + 1);
2414 return BLUETOOTH_ERROR_NONE;
2418 return BLUETOOTH_ERROR_NOT_FOUND;
2421 int _bt_get_le_connection_parameter(bluetooth_le_connection_mode_t mode,
2422 bluetooth_le_connection_param_t *param)
2425 return BLUETOOTH_ERROR_INVALID_PARAM;
2427 if (mode < BLUETOOTH_LE_CONNECTION_MODE_BALANCED ||
2428 mode > BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER)
2429 return BLUETOOTH_ERROR_INVALID_PARAM;
2431 memset(param, 0x00, sizeof(bluetooth_le_connection_param_t));
2434 case BLUETOOTH_LE_CONNECTION_MODE_BALANCED:
2435 param->interval_min = BT_LE_CONN_PARAM_BALANCED_MIN_INTERVAL;
2436 param->interval_max = BT_LE_CONN_PARAM_BALANCED_MAX_INTERVAL;
2437 param->latency = BT_LE_CONN_PARAM_BALANCED_SLAVE_LATENCY;
2438 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2441 case BLUETOOTH_LE_CONNECTION_MODE_LOW_LATENCY:
2442 param->interval_min = BT_LE_CONN_PARAM_LOW_LATENCY_MIN_INTERVAL;
2443 param->interval_max = BT_LE_CONN_PARAM_LOW_LATENCY_MAX_INTERVAL;
2444 param->latency = BT_LE_CONN_PARAM_LOW_LATENCY_SLAVE_LATENCY;
2445 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2448 case BLUETOOTH_LE_CONNECTION_MODE_LOW_POWER:
2449 param->interval_min = BT_LE_CONN_PARAM_LOW_POWER_MIN_INTERVAL;
2450 param->interval_max = BT_LE_CONN_PARAM_LOW_POWER_MAX_INTERVAL;
2451 param->latency = BT_LE_CONN_PARAM_LOW_POWER_SLAVE_LATENCY;
2452 param->timeout = BT_LE_CONN_PARAM_DEFAULT_SUPERVISION_TIMEOUT;
2456 BT_ERR("Unhandled mode : %d", mode);
2460 return BLUETOOTH_ERROR_NONE;
2463 int _bt_passkey_reply(const char *passkey, gboolean authentication_reply)
2465 GapAgentPrivate *agent = _bt_get_adapter_agent();
2466 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
2468 if (authentication_reply)
2469 gap_agent_reply_pin_code(agent, GAP_AGENT_ACCEPT, passkey, NULL);
2471 gap_agent_reply_pin_code(agent, GAP_AGENT_REJECT, passkey, NULL);
2473 BT_DBG("BT_PASSKEY_REPLY");
2474 return BLUETOOTH_ERROR_NONE;
2477 int _bt_passkey_confirmation_reply(gboolean confirmation_reply)
2479 GapAgentPrivate *agent = _bt_get_adapter_agent();
2480 retv_if(!agent, BLUETOOTH_ERROR_INTERNAL);
2482 if (confirmation_reply)
2483 gap_agent_reply_confirmation(agent, GAP_AGENT_ACCEPT, NULL);
2485 gap_agent_reply_confirmation(agent, GAP_AGENT_REJECT, NULL);
2487 BT_DBG("BT_PASSKEY_CONFIRMATION_REPLY");
2488 return BLUETOOTH_ERROR_NONE;