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.
19 #include <dbus/dbus.h>
25 #include <syspopup_caller.h>
28 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-event.h"
31 #include "bt-service-adapter.h"
32 #include "bt-service-adapter-le.h"
33 #include "bt-service-util.h"
36 #define BT_ADV_INTERVAL_MIN 20 /* msec */
37 #define BT_ADV_INTERVAL_MAX 10240
38 #define BT_ADV_INTERVAL_SPLIT 0.625
39 #define BT_DEFAULT_ADV_MIN_INTERVAL 500
40 #define BT_DEFAULT_ADV_MAX_INTERVAL 500
41 #define BT_ADV_FILTER_POLICY_DEFAULT 0x00
42 #define BT_ADV_TYPE_DEFAULT 0x00
43 #define BT_ADV_FILTER_POLICY_ALLOW_SCAN_CONN_WL_ONLY 0x03
44 #define BT_ADV_MULTI_MAX 16
50 } bt_adapter_le_feature_info_t;
55 gboolean is_advertising;
57 } bt_adapter_le_adv_slot_t;
63 } bt_adapter_le_scanner_t;
65 static bluetooth_advertising_params_t adv_params = {
66 BT_DEFAULT_ADV_MIN_INTERVAL,
67 BT_DEFAULT_ADV_MAX_INTERVAL,
68 BT_ADV_FILTER_POLICY_DEFAULT,
70 static bluetooth_advertising_data_t adv_data = { {0} };
71 static int adv_data_len;
72 static bluetooth_scan_resp_data_t resp_data = { {0} };
73 static int resp_data_len;
75 static bt_adapter_le_feature_info_t le_feature_info = { 1, 0, 0 };
76 static bt_adapter_le_adv_slot_t *le_adv_slot = NULL;
78 GSList *scanner_list = NULL;
79 static gboolean is_le_set_scan_parameter = FALSE;
80 static gboolean is_le_scanning = FALSE;
81 static gboolean scan_filter_enabled = FALSE;
82 static bt_le_scan_type_t le_scan_type = BT_LE_PASSIVE_SCAN;
84 static GSList *gatt_client_senders = NULL;
87 gboolean _bt_is_set_scan_parameter(void)
89 return is_le_set_scan_parameter;
92 void _bt_init_gatt_client_senders(void)
94 _bt_clear_request_list();
97 int _bt_insert_gatt_client_sender(char *sender)
101 retv_if(sender == NULL, BLUETOOTH_ERROR_INVALID_PARAM);
103 info = g_strdup(sender);
104 retv_if(info == NULL, BLUETOOTH_ERROR_MEMORY_ALLOCATION);
106 gatt_client_senders = g_slist_append(gatt_client_senders, info);
108 BT_DBG("insert sender: %s", sender);
110 return BLUETOOTH_ERROR_NONE;
113 int _bt_delete_gatt_client_sender(char *sender)
118 BT_DBG("remove sender: %s", sender);
120 for (l = gatt_client_senders; l != NULL; l = g_slist_next(l)) {
125 if (g_strcmp0(info, sender) == 0) {
126 BT_DBG("remove info");
127 gatt_client_senders = g_slist_remove(gatt_client_senders, info);
129 return BLUETOOTH_ERROR_NONE;
133 return BLUETOOTH_ERROR_NOT_FOUND;
136 void _bt_clear_gatt_client_senders(void)
138 if (gatt_client_senders) {
139 g_slist_foreach(gatt_client_senders, (GFunc)g_free, NULL);
140 g_slist_free(gatt_client_senders);
141 gatt_client_senders = NULL;
145 static void __bt_send_foreach_event(gpointer data, gpointer user_data)
148 GVariant *param = user_data;
150 _bt_send_event_to_dest(sender, BT_DEVICE_EVENT, BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED,
154 void _bt_send_char_value_changed_event(void *param)
157 g_slist_foreach(gatt_client_senders, __bt_send_foreach_event,
160 _bt_send_event(BT_DEVICE_EVENT, BLUETOOTH_EVENT_GATT_CHAR_VAL_CHANGED, param);
164 void __bt_free_le_adv_slot(void)
168 if (le_adv_slot == NULL)
171 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
172 if (le_adv_slot[i].sender)
173 g_free(le_adv_slot[i].sender);
179 int _bt_service_adapter_le_init(void)
181 le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
183 return BLUETOOTH_ERROR_NONE;
186 void _bt_service_adapter_le_deinit(void)
188 __bt_free_le_adv_slot();
191 gboolean _bt_update_le_feature_support(const char *item, const char *value)
193 if (item == NULL || value == NULL)
196 if (g_strcmp0(item, "adv_inst_max") == 0) {
199 slot_num = atoi(value);
200 retv_if(slot_num < 0, FALSE);
201 retv_if(slot_num > BT_ADV_MULTI_MAX, FALSE);
203 if (slot_num != le_feature_info.adv_inst_max) {
204 __bt_free_le_adv_slot();
205 le_feature_info.adv_inst_max = slot_num;
206 BT_INFO("Advertising instance max : %d", le_feature_info.adv_inst_max);
207 le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
209 } else if (g_strcmp0(item, "rpa_offloading") == 0) {
210 le_feature_info.rpa_offloading = atoi(value);
211 BT_INFO("RPA offloading : %d", le_feature_info.rpa_offloading);
212 } else if (g_strcmp0(item, "max_filter") == 0) {
213 le_feature_info.max_filter = atoi(value);
214 BT_INFO("BLE Scan max filter : %d", le_feature_info.max_filter);
216 BT_DBG("No registered item");
223 static gboolean __bt_is_factory_test_mode(void)
227 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
228 BT_ERR("Get the DUT Mode fail");
233 BT_INFO("DUT Test Mode !!");
240 int __bt_get_available_adv_slot_id(const char *sender, int adv_handle, gboolean use_reserved_slot)
244 if (le_adv_slot == NULL) {
245 BT_ERR("le_adv_slot is NULL");
249 BT_DBG("adv_inst_max : %d", le_feature_info.adv_inst_max);
251 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
252 if (le_adv_slot[i].sender == NULL)
254 if ((g_strcmp0(le_adv_slot[i].sender, sender) == 0) && (le_adv_slot[i].adv_handle == adv_handle))
258 if (le_feature_info.adv_inst_max <= 2)
260 else if (le_feature_info.adv_inst_max > 2 && use_reserved_slot == TRUE)
265 for (; i < le_feature_info.adv_inst_max; i++) {
266 if (le_adv_slot[i].sender == NULL)
273 static void __bt_register_adv_slot_owner(const char *sender, int adv_handle, int slot_id)
275 if (le_adv_slot[slot_id].sender == NULL) {
276 le_adv_slot[slot_id].sender = strdup(sender);
277 le_adv_slot[slot_id].adv_handle = adv_handle;
281 void _bt_unregister_adv_slot_owner(int slot_id)
283 g_free(le_adv_slot[slot_id].sender);
284 le_adv_slot[slot_id].sender = NULL;
285 le_adv_slot[slot_id].adv_handle = 0;
288 const char* _bt_get_adv_slot_owner(int slot_id)
290 if (le_adv_slot == NULL)
293 return le_adv_slot[slot_id].sender;
296 int _bt_get_adv_slot_adv_handle(int slot_id)
298 if (le_adv_slot == NULL)
301 return le_adv_slot[slot_id].adv_handle;
304 void _bt_set_advertising_status(int slot_id, gboolean mode)
306 le_adv_slot[slot_id].is_advertising = mode;
309 gboolean _bt_is_advertising(void)
311 gboolean status = FALSE;
314 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
315 if (le_adv_slot[i].is_advertising == TRUE)
322 void _bt_stop_advertising_by_terminated_process(const char* terminated_name)
326 if (le_adv_slot == NULL)
329 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
330 if (le_adv_slot[i].sender != NULL) {
331 if (strcasecmp(terminated_name, le_adv_slot[i].sender) == 0) {
332 BT_ERR("Stop advertising by terminated process(%s).", terminated_name);
333 _bt_set_advertising(terminated_name, le_adv_slot[i].adv_handle, FALSE, FALSE);
339 gboolean _bt_get_advertising_params(bluetooth_advertising_params_t *params)
344 memcpy(params, &adv_params, sizeof(bluetooth_advertising_params_t));
349 int _bt_set_advertising(const char *sender, int adv_handle, gboolean enable, gboolean use_reserved_slot)
352 GError *error = NULL;
356 if (__bt_is_factory_test_mode()) {
357 BT_ERR("Unable to start advertising in factory binary !!");
358 return BLUETOOTH_ERROR_NOT_SUPPORT;
361 if (_bt_adapter_get_status() != BT_ACTIVATED &&
362 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
363 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
366 slot_id = __bt_get_available_adv_slot_id(sender, adv_handle, use_reserved_slot);
368 BT_ERR("There is NO available slot!!");
369 return BLUETOOTH_ERROR_NO_RESOURCES;
372 if (le_adv_slot[slot_id].is_advertising == TRUE && enable == TRUE)
373 return BLUETOOTH_ERROR_IN_PROGRESS;
375 if (le_adv_slot[slot_id].sender != NULL && le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
376 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
378 if (le_adv_slot[slot_id].hold_timer_id > 0) {
379 g_source_remove(le_adv_slot[slot_id].hold_timer_id);
380 le_adv_slot[slot_id].hold_timer_id = 0;
383 proxy = _bt_get_adapter_proxy();
384 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
386 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertising",
387 g_variant_new("(bi)", enable, slot_id),
388 G_DBUS_CALL_FLAGS_NONE,
394 BT_ERR("SetAdvertising Fail: %s", error->message);
395 g_clear_error(&error);
396 return BLUETOOTH_ERROR_INTERNAL;
400 __bt_register_adv_slot_owner(sender, adv_handle, slot_id);
402 le_adv_slot[slot_id].is_advertising = enable;
403 BT_INFO_C("### Set advertising [%d]", enable);
406 g_variant_unref(ret);
408 return BLUETOOTH_ERROR_NONE;
411 int _bt_set_custom_advertising(const char *sender, int adv_handle,
412 gboolean enable, bluetooth_advertising_params_t *params, gboolean use_reserved_slot)
416 GError *error = NULL;
421 BT_CHECK_PARAMETER(params, return);
423 if (__bt_is_factory_test_mode()) {
424 BT_ERR("Unable to start advertising in factory binary !!");
425 return BLUETOOTH_ERROR_NOT_SUPPORT;
428 if (_bt_adapter_get_status() != BT_ACTIVATED &&
429 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
430 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
433 slot_id = __bt_get_available_adv_slot_id(sender, adv_handle, use_reserved_slot);
435 BT_ERR("There is NO available slot!!");
436 return BLUETOOTH_ERROR_NO_RESOURCES;
439 if (le_adv_slot[slot_id].is_advertising == TRUE && enable == TRUE)
440 return BLUETOOTH_ERROR_IN_PROGRESS;
442 if (le_adv_slot[slot_id].sender != NULL && le_adv_slot[slot_id].is_advertising == FALSE && enable == FALSE)
443 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
445 if (le_adv_slot[slot_id].hold_timer_id > 0) {
446 g_source_remove(le_adv_slot[slot_id].hold_timer_id);
447 le_adv_slot[slot_id].hold_timer_id = 0;
450 proxy = _bt_get_adapter_proxy();
451 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
453 if (params->interval_min > params->interval_max ||
454 params->interval_min < BT_ADV_INTERVAL_MIN ||
455 params->interval_max > BT_ADV_INTERVAL_MAX)
456 return BLUETOOTH_ERROR_INVALID_PARAM;
458 if (params->filter_policy > BLUETOOTH_ALLOW_SCAN_CONN_WHITE_LIST)
459 return BLUETOOTH_ERROR_INVALID_PARAM;
461 if (params->type == BLUETOOTH_ADV_CONNECTABLE_DIRECT_HIGH ||
462 params->type == BLUETOOTH_ADV_CONNECTABLE_DIRECT_LOW ||
463 params->type == BLUETOOTH_ADV_NON_CONNECTABLE)
464 return BLUETOOTH_ERROR_NOT_SUPPORT;
466 min = params->interval_min / BT_ADV_INTERVAL_SPLIT;
467 max = params->interval_max / BT_ADV_INTERVAL_SPLIT;
469 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertisingParameters",
470 g_variant_new("(uuuui)", min, max,
471 params->filter_policy, params->type,
472 slot_id), G_DBUS_CALL_FLAGS_NONE,
476 BT_ERR("SetAdvertisingParameters Fail: %s", error->message);
477 g_clear_error(&error);
478 return BLUETOOTH_ERROR_INTERNAL;
481 adv_params.interval_min = params->interval_min;
482 adv_params.interval_max = params->interval_max;
483 adv_params.filter_policy = params->filter_policy;
484 adv_params.type = params->type;
487 g_variant_unref(ret);
489 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertising",
490 g_variant_new("(bi)", enable, slot_id),
491 G_DBUS_CALL_FLAGS_NONE,
497 BT_ERR("SetAdvertising Fail: %s", error->message);
498 g_clear_error(&error);
499 return BLUETOOTH_ERROR_INTERNAL;
503 __bt_register_adv_slot_owner(sender, adv_handle, slot_id);
505 _bt_unregister_adv_slot_owner(slot_id);
507 le_adv_slot[slot_id].is_advertising = enable;
508 BT_INFO_C("### Set advertising [%d]", enable);
510 g_variant_unref(ret);
512 return BLUETOOTH_ERROR_NONE;
515 static gboolean __bt_hold_current_advertising_timeout_cb(gpointer user_data)
518 GError *error = NULL;
521 BT_INFO("Restart advertising stopped by bt-service");
523 le_adv_slot[0].hold_timer_id = 0;
525 proxy = _bt_get_adapter_proxy();
526 retv_if(proxy == NULL, FALSE);
528 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertising",
529 g_variant_new("(bi)", TRUE, 0),
530 G_DBUS_CALL_FLAGS_NONE,
536 BT_ERR("SetAdvertising Fail: %s", error->message);
537 g_clear_error(&error);
542 g_variant_unref(ret);
547 int _bt_hold_current_advertising(void)
550 GError *error = NULL;
553 if (le_adv_slot[0].sender && le_adv_slot[0].is_advertising == TRUE) {
554 BT_INFO("Stop current advertising by bt-service");
556 proxy = _bt_get_adapter_proxy();
557 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
559 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertising",
560 g_variant_new("(bi)", FALSE, 0),
561 G_DBUS_CALL_FLAGS_NONE,
567 BT_ERR("SetAdvertising Fail: %s", error->message);
568 g_clear_error(&error);
569 return BLUETOOTH_ERROR_INTERNAL;
573 g_variant_unref(ret);
575 le_adv_slot[0].hold_timer_id = g_timeout_add(2000,
576 __bt_hold_current_advertising_timeout_cb, NULL);
578 BT_ERR("It's NOT advertising");
579 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
582 return BLUETOOTH_ERROR_NONE;
585 static int __bt_get_ad_data_by_type(char *in_data, int in_len,
586 char in_type, char **data, int *data_len)
588 if (in_data == NULL || data == NULL || data_len == NULL)
589 return BLUETOOTH_ERROR_INTERNAL;
592 return BLUETOOTH_ERROR_INTERNAL;
598 for (i = 0; i < in_len; i++) {
600 if (len <= 0 || i + 1 >= in_len) {
601 BT_ERR("Invalid advertising data");
602 return BLUETOOTH_ERROR_INTERNAL;
605 type = in_data[i + 1];
606 if (type == in_type) {
616 if (i + len > in_len) {
617 BT_ERR("Invalid advertising data");
618 return BLUETOOTH_ERROR_INTERNAL;
619 } else if (len == 0) {
620 BT_DBG("AD Type 0x%02x data is not set", in_type);
623 return BLUETOOTH_ERROR_NONE;
626 *data = g_memdup(&in_data[i], len);
628 return BLUETOOTH_ERROR_OUT_OF_MEMORY;
631 return BLUETOOTH_ERROR_NONE;
634 int _bt_get_advertising_data(bluetooth_advertising_data_t *adv, int *length)
636 BT_CHECK_PARAMETER(adv, return);
637 BT_CHECK_PARAMETER(length, return);
639 memcpy(adv, &adv_data, sizeof(adv_data));
640 *length = adv_data_len;
642 return BLUETOOTH_ERROR_NONE;
645 int _bt_set_advertising_data(const char *sender, int adv_handle,
646 bluetooth_advertising_data_t *adv, int length, gboolean use_reserved_slot)
649 GError *error = NULL;
650 GVariant *ret, *ad_data, *param = NULL;
651 GVariant *temp = NULL;
652 GVariantBuilder *builder;
654 char *old_mdata = NULL;
655 char *new_mdata = NULL;
660 if (__bt_is_factory_test_mode()) {
661 BT_ERR("Unable to set advertising data in factory binary !!");
662 return BLUETOOTH_ERROR_NOT_SUPPORT;
665 BT_CHECK_PARAMETER(adv, return);
667 if (_bt_adapter_get_status() != BT_ACTIVATED &&
668 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
669 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
672 proxy = _bt_get_adapter_proxy();
673 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
675 slot_id = __bt_get_available_adv_slot_id(sender, adv_handle, use_reserved_slot);
677 BT_ERR("There is NO available slot!!");
678 return BLUETOOTH_ERROR_NO_RESOURCES;
681 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
682 for (i = 0; i < length; i++)
683 g_variant_builder_add(builder, "y", adv->data[i]);
685 temp = g_variant_new("ay", builder);
686 g_variant_builder_unref(builder);
687 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertisingData",
688 g_variant_new("(@ayi)", temp, slot_id),
689 G_DBUS_CALL_FLAGS_NONE,
693 BT_ERR("SetAdvertisingData Fail: %s", error->message);
694 g_clear_error(&error);
695 return BLUETOOTH_ERROR_INTERNAL;
698 __bt_register_adv_slot_owner(sender, adv_handle, slot_id);
700 __bt_get_ad_data_by_type((char *)adv_data.data, adv_data_len, 0xff,
701 &old_mdata, &old_len);
702 __bt_get_ad_data_by_type((char *)adv->data, length, 0xff,
703 &new_mdata, &new_len);
704 if (old_len != new_len ||
705 (old_mdata && new_mdata &&
706 memcmp(old_mdata, new_mdata, new_len))) {
707 ad_data = g_variant_new_from_data((const GVariantType *)"ay",
708 new_mdata, new_len, TRUE, NULL, NULL);
709 param = g_variant_new("(@ay)", ad_data);
710 _bt_send_event(BT_ADAPTER_EVENT,
711 BLUETOOTH_EVENT_ADVERTISING_MANUFACTURER_DATA_CHANGED,
717 memset(&adv_data, 0x00, sizeof(bluetooth_advertising_data_t));
718 memcpy(&adv_data, adv, length);
719 adv_data_len = length;
721 BT_INFO("Set advertising data");
723 g_variant_unref(ret);
725 return BLUETOOTH_ERROR_NONE;
728 int _bt_get_scan_response_data(bluetooth_scan_resp_data_t *response, int *length)
730 BT_CHECK_PARAMETER(response, return);
731 BT_CHECK_PARAMETER(length, return);
733 memcpy(response, &resp_data, sizeof(resp_data));
734 *length = resp_data_len;
736 return BLUETOOTH_ERROR_NONE;
739 int _bt_set_scan_response_data(const char *sender, int adv_handle,
740 bluetooth_scan_resp_data_t *response, int length, gboolean use_reserved_slot)
743 GError *error = NULL;
744 GVariant *ret, *scan_data, *param = NULL;
745 GVariant *temp = NULL;
746 GVariantBuilder *builder;
748 char *old_mdata = NULL;
749 char *new_mdata = NULL;
754 if (__bt_is_factory_test_mode()) {
755 BT_ERR("Unable to set scan response list in factory binary !!");
756 return BLUETOOTH_ERROR_NOT_SUPPORT;
759 BT_CHECK_PARAMETER(response, return);
761 if (_bt_adapter_get_status() != BT_ACTIVATED &&
762 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
763 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
766 proxy = _bt_get_adapter_proxy();
767 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
769 slot_id = __bt_get_available_adv_slot_id(sender, adv_handle, use_reserved_slot);
771 BT_ERR("There is NO available slot!!");
772 return BLUETOOTH_ERROR_NO_RESOURCES;
774 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
775 for (i = 0; i < length; i++)
776 g_variant_builder_add(builder, "y", response->data[i]);
778 temp = g_variant_new("ay", builder);
779 g_variant_builder_unref(builder);
780 ret = g_dbus_proxy_call_sync(proxy, "SetScanRespData",
781 g_variant_new("(@ayi)", temp, slot_id),
782 G_DBUS_CALL_FLAGS_NONE,
786 BT_ERR("SetScanRespData Fail: %s", error->message);
787 g_clear_error(&error);
788 return BLUETOOTH_ERROR_INTERNAL;
791 __bt_register_adv_slot_owner(sender, adv_handle, slot_id);
793 /* Compare with previous scan resp data */
794 __bt_get_ad_data_by_type((char *)resp_data.data, resp_data_len, 0xff,
795 &old_mdata, &old_len);
796 __bt_get_ad_data_by_type((char *)response->data, length, 0xff,
797 &new_mdata, &new_len);
798 if (old_len != new_len ||
799 (old_mdata && new_mdata &&
800 memcmp(old_mdata, new_mdata, new_len))) {
801 scan_data = g_variant_new_from_data((const GVariantType *)"ay",
802 new_mdata, new_len, TRUE, NULL, NULL);
803 param = g_variant_new("(@ay)", scan_data);
804 _bt_send_event(BT_ADAPTER_EVENT,
805 BLUETOOTH_EVENT_SCAN_RESPONSE_MANUFACTURER_DATA_CHANGED,
811 memset(&resp_data, 0x00, sizeof(bluetooth_scan_resp_data_t));
812 memcpy(&resp_data, response, length);
813 resp_data_len = length;
816 g_variant_unref(ret);
817 BT_INFO("Set scan response data");
818 return BLUETOOTH_ERROR_NONE;
821 int _bt_set_scan_parameters(bluetooth_le_scan_params_t *params)
824 GError *error = NULL;
829 BT_CHECK_PARAMETER(params, return);
831 if (_bt_adapter_get_status() != BT_ACTIVATED &&
832 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
833 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
836 proxy = _bt_get_adapter_proxy();
837 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
839 if (params->interval < BT_LE_SCAN_INTERVAL_MIN || params->interval > BT_LE_SCAN_INTERVAL_MAX)
840 return BLUETOOTH_ERROR_INVALID_PARAM;
842 if (params->window < BT_LE_SCAN_WINDOW_MIN || params->window > BT_LE_SCAN_WINDOW_MAX)
843 return BLUETOOTH_ERROR_INVALID_PARAM;
845 if (params->window > params->interval)
846 return BLUETOOTH_ERROR_INVALID_PARAM;
848 itv = params->interval / BT_ADV_INTERVAL_SPLIT;
849 win = params->window / BT_ADV_INTERVAL_SPLIT;
851 ret = g_dbus_proxy_call_sync(proxy, "SetScanParameters",
852 g_variant_new("(uuu)", params->type, itv, win),
853 G_DBUS_CALL_FLAGS_NONE, -1,
857 BT_ERR("SetScanParameters Fail: %s", error->message);
858 g_clear_error(&error);
859 return BLUETOOTH_ERROR_INTERNAL;
862 _bt_set_le_scan_type(params->type);
864 is_le_set_scan_parameter = TRUE;
867 g_variant_unref(ret);
868 BT_INFO("Set scan parameters");
869 return BLUETOOTH_ERROR_NONE;
872 bt_adapter_le_scanner_t* __bt_find_scanner_from_list(const char *sender)
875 bt_adapter_le_scanner_t *scanner;
877 for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
879 if (g_strcmp0(scanner->sender, sender) == 0)
886 int __bt_get_available_scan_filter_slot_id(void)
889 bt_adapter_le_scanner_t *scanner;
891 bluetooth_le_scan_filter_t *filter_data;
892 gboolean *slot_check_list = NULL;
895 if (le_feature_info.max_filter == 0) {
896 BT_ERR("Scan filter is NOT Supported");
899 slot_check_list = g_malloc0(sizeof(gboolean) * le_feature_info.max_filter);
900 if (slot_check_list == NULL) {
901 BT_ERR("Fail to allocate memory");
905 for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
907 for (fl = scanner->filter_list; fl != NULL; fl = g_slist_next(fl)) {
908 filter_data = fl->data;
909 if (filter_data->slot_id < le_feature_info.max_filter)
910 slot_check_list[filter_data->slot_id] = TRUE;
914 for (i = 0; i < le_feature_info.max_filter; i++) {
915 if (slot_check_list[i] == FALSE) {
916 g_free(slot_check_list);
921 BT_ERR("There is NO available slot for scan filter.");
922 g_free(slot_check_list);
926 gboolean _bt_is_scan_filter_supported(void)
928 if (le_feature_info.max_filter > 0)
934 int _bt_register_scan_filter(const char *sender, bluetooth_le_scan_filter_t *filter, int *slot_id)
937 GError *error = NULL;
938 GVariant *ret, *param;
939 GVariant *arr_uuid_param = NULL, *arr_uuid_mask_param = NULL;
940 GVariant *arr_data_param = NULL, *arr_data_mask_param = NULL;
941 GArray *arr_uuid = NULL;
942 GArray *arr_uuid_mask = NULL;
943 GArray *arr_data = NULL;
944 GArray *arr_data_mask = NULL;
945 bt_adapter_le_scanner_t *scanner = NULL;
946 bluetooth_le_scan_filter_t *filter_data = NULL;
947 int feature_selection = 0;
949 *slot_id = __bt_get_available_scan_filter_slot_id();
951 return BLUETOOTH_ERROR_NO_RESOURCES;
953 proxy = _bt_get_adapter_proxy();
954 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
956 if (filter->added_features & BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS) {
957 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
958 feature_selection |= BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS;
960 _bt_convert_addr_type_to_string(address, filter->device_address.addr);
962 arr_uuid_param = g_variant_new_from_data((const GVariantType *)"ay",
963 NULL, 0, TRUE, NULL, NULL);
964 arr_uuid_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
965 NULL, 0, TRUE, NULL, NULL);
966 arr_data_param = g_variant_new_from_data((const GVariantType *)"ay",
967 NULL, 0, TRUE, NULL, NULL);
968 arr_data_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
969 NULL, 0, TRUE, NULL, NULL);
971 param = g_variant_new("(iiiiii@ay@aysu@ay@ay)",
973 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
974 BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS, // filter_type
975 *slot_id, // filter_index
977 0, // company_id_mask
978 arr_uuid_param, // p_uuid
979 arr_uuid_mask_param, // p_uuid_mask
982 arr_data_param, // p_data
983 arr_data_mask_param); // p_mask
985 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_add_remove",
986 param, G_DBUS_CALL_FLAGS_NONE,
990 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
991 g_clear_error(&error);
994 g_variant_unref(ret);
997 if (filter->added_features & BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_NAME) {
998 feature_selection |= BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_NAME;
1000 arr_uuid_param = g_variant_new_from_data((const GVariantType *)"ay",
1001 NULL, 0, TRUE, NULL, NULL);
1002 arr_uuid_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1003 NULL, 0, TRUE, NULL, NULL);
1004 arr_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1005 NULL, 0, TRUE, NULL, NULL);
1006 arr_data_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1007 NULL, 0, TRUE, NULL, NULL);
1009 param = g_variant_new("(iiiiii@ay@aysu@ay@ay)",
1011 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
1012 BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_NAME, // filter_type
1013 *slot_id, // filter_index
1015 0, // company_id_mask
1016 arr_uuid_param, // p_uuid
1017 arr_uuid_mask_param, // p_uuid_mask
1018 filter->device_name, // string
1020 arr_data_param, // p_data
1021 arr_data_mask_param);
1023 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_add_remove",
1024 param, G_DBUS_CALL_FLAGS_NONE,
1028 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
1029 g_clear_error(&error);
1032 g_variant_unref(ret);
1035 if (filter->added_features & BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_UUID) {
1036 feature_selection |= BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_UUID;
1038 arr_uuid = g_array_new(TRUE, TRUE, sizeof(guint8));
1039 arr_uuid_mask = g_array_new(TRUE, TRUE, sizeof(guint8));
1041 g_array_append_vals(arr_uuid, filter->service_uuid.data.data, filter->service_uuid.data_len * sizeof(guint8));
1042 g_array_append_vals(arr_uuid_mask, filter->service_uuid_mask.data.data, filter->service_uuid_mask.data_len * sizeof(guint8));
1044 arr_uuid_param = g_variant_new_from_data((const GVariantType *)"ay",
1045 arr_uuid->data, arr_uuid->len, TRUE, NULL, NULL);
1046 arr_uuid_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1047 arr_uuid_mask->data, arr_uuid_mask->len, TRUE, NULL, NULL);
1048 arr_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1049 NULL, 0, TRUE, NULL, NULL);
1050 arr_data_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1051 NULL, 0, TRUE, NULL, NULL);
1053 param = g_variant_new("(iiiiii@ay@aysu@ay@ay)",
1055 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
1056 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_UUID, // filter_type
1057 *slot_id, // filter_index
1059 0, // company_id_mask
1060 arr_uuid_param, // p_uuid
1061 arr_uuid_mask_param, // p_uuid_mask
1064 arr_data_param, // p_data
1065 arr_data_mask_param);
1067 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_add_remove",
1068 param, G_DBUS_CALL_FLAGS_NONE,
1072 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
1073 g_clear_error(&error);
1076 g_variant_unref(ret);
1078 g_array_free(arr_uuid, TRUE);
1079 g_array_free(arr_uuid_mask, TRUE);
1080 g_array_free(arr_data, TRUE);
1081 g_array_free(arr_data_mask, TRUE);
1084 if (filter->added_features & BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID) {
1085 feature_selection |= BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID;
1087 arr_uuid = g_array_new(TRUE, TRUE, sizeof(guint8));
1088 arr_uuid_mask = g_array_new(TRUE, TRUE, sizeof(guint8));
1090 g_array_append_vals(arr_uuid, filter->service_solicitation_uuid.data.data, filter->service_solicitation_uuid.data_len * sizeof(guint8));
1091 g_array_append_vals(arr_uuid_mask, filter->service_solicitation_uuid_mask.data.data, filter->service_solicitation_uuid_mask.data_len * sizeof(guint8));
1093 arr_uuid_param = g_variant_new_from_data((const GVariantType *)"ay",
1094 arr_uuid->data, arr_uuid->len, TRUE, NULL, NULL);
1095 arr_uuid_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1096 arr_uuid_mask->data, arr_uuid_mask->len, TRUE, NULL, NULL);
1097 arr_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1098 NULL, 0, TRUE, NULL, NULL);
1099 arr_data_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1100 NULL, 0, TRUE, NULL, NULL);
1102 param = g_variant_new("(iiiiii@ay@aysu@ay@ay)",
1104 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
1105 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID, // filter_type
1106 *slot_id, // filter_index
1108 0, // company_id_mask
1109 arr_uuid_param, // p_uuid
1110 arr_uuid_mask_param, // p_uuid_mask
1113 arr_data_param, // p_data
1114 arr_data_mask_param);
1116 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_add_remove", param,
1117 G_DBUS_CALL_FLAGS_NONE,
1121 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
1122 g_clear_error(&error);
1125 g_variant_unref(ret);
1127 g_array_free(arr_uuid, TRUE);
1128 g_array_free(arr_uuid_mask, TRUE);
1131 if (filter->added_features & BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_DATA) {
1132 feature_selection |= BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_DATA;
1134 arr_data = g_array_new(TRUE, TRUE, sizeof(guint8));
1135 arr_data_mask = g_array_new(TRUE, TRUE, sizeof(guint8));
1137 g_array_append_vals(arr_data, filter->service_data.data.data, filter->service_data.data_len * sizeof(guint8));
1138 g_array_append_vals(arr_data_mask, filter->service_data_mask.data.data, filter->service_data_mask.data_len * sizeof(guint8));
1140 arr_uuid_param = g_variant_new_from_data((const GVariantType *)"ay",
1141 NULL, 0, TRUE, NULL, NULL);
1142 arr_uuid_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1143 NULL, 0, TRUE, NULL, NULL);
1144 arr_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1145 arr_data->data, arr_data->len, TRUE, NULL, NULL);
1146 arr_data_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1147 arr_data_mask->data, arr_data_mask->len, TRUE, NULL, NULL);
1149 param = g_variant_new("(iiiiii@ay@aysu@ay@ay)",
1151 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
1152 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_DATA, // filter_type
1153 *slot_id, // filter_index
1155 0, // company_id_mask
1156 arr_uuid_param, // p_uuid
1157 arr_uuid_mask_param, // p_uuid_mask
1160 arr_data_param, // p_data
1161 arr_data_mask_param);
1163 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_add_remove", param,
1164 G_DBUS_CALL_FLAGS_NONE,
1168 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
1169 g_clear_error(&error);
1172 g_variant_unref(ret);
1174 g_array_free(arr_data, TRUE);
1175 g_array_free(arr_data_mask, TRUE);
1178 if (filter->added_features & BLUETOOTH_LE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA) {
1179 feature_selection |= BLUETOOTH_LE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA;
1181 arr_data = g_array_new(TRUE, TRUE, sizeof(guint8));
1182 arr_data_mask = g_array_new(TRUE, TRUE, sizeof(guint8));
1184 g_array_append_vals(arr_data, filter->manufacturer_data.data.data, filter->manufacturer_data.data_len * sizeof(guint8));
1185 g_array_append_vals(arr_data_mask, filter->manufacturer_data_mask.data.data, filter->manufacturer_data_mask.data_len * sizeof(guint8));
1187 arr_uuid_param = g_variant_new_from_data((const GVariantType *)"ay",
1188 NULL, 0, TRUE, NULL, NULL);
1189 arr_uuid_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1190 NULL, 0, TRUE, NULL, NULL);
1191 arr_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1192 arr_data->data, arr_data->len, TRUE, NULL, NULL);
1193 arr_data_mask_param = g_variant_new_from_data((const GVariantType *)"ay",
1194 arr_data_mask->data, arr_data_mask->len, TRUE, NULL, NULL);
1196 param = g_variant_new("(iiiiii@ay@aysu@ay@ay)",
1198 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
1199 BLUETOOTH_LE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA, // filter_type
1200 *slot_id, // filter_index
1201 filter->manufacturer_id, // company_id
1202 0xFFFF, // company_id_mask
1203 arr_uuid_param, // p_uuid
1204 arr_uuid_mask_param, // p_uuid_mask
1207 arr_data_param, // p_data
1208 arr_data_mask_param);
1210 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_add_remove", param,
1211 G_DBUS_CALL_FLAGS_NONE,
1215 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
1216 g_clear_error(&error);
1219 g_variant_unref(ret);
1221 g_array_free(arr_data, TRUE);
1222 g_array_free(arr_data_mask, TRUE);
1225 BT_DBG("Filter selection %.2x", feature_selection);
1227 param = g_variant_new("(iiiiiiiiiiii)",
1229 0, // action (Add - 0x00, Delete - 0x01, Clear - 0x02)
1230 *slot_id, // filter_index
1231 feature_selection, // feat_seln
1232 0, // list_logic_type (OR - 0x00, AND - 0x01)
1233 1, // filt_logic_type (OR - 0x00, AND - 0x01)
1234 -127, // rssi_high_thres
1235 -127, // rssi_low_thres
1236 0, // dely_mode (Immediate - 0x00, on found - 0x01, batched - 0x02)
1239 0); // found_timeout_cnt
1240 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_param_setup",
1241 param, G_DBUS_CALL_FLAGS_NONE,
1245 BT_ERR("scan_filter_add_remove Fail: %s", error->message);
1246 g_clear_error(&error);
1249 scanner = __bt_find_scanner_from_list(sender);
1250 if (scanner == NULL) {
1251 scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
1253 scanner->sender = strdup(sender);
1254 scanner_list = g_slist_append(scanner_list, scanner);
1258 filter_data = g_malloc0(sizeof(bluetooth_le_scan_filter_t));
1260 memcpy(filter_data, filter, sizeof(bluetooth_le_scan_filter_t));
1261 filter_data->slot_id = *slot_id;
1264 scanner->filter_list = g_slist_append(scanner->filter_list, filter_data);
1268 g_variant_unref(ret);
1269 return BLUETOOTH_ERROR_NONE;
1272 int _bt_unregister_scan_filter(const char *sender, int slot_id)
1275 GError *error = NULL;
1277 bt_adapter_le_scanner_t *scanner = NULL;
1278 bluetooth_le_scan_filter_t *filter_data = NULL;
1280 gboolean is_slot_id_found = FALSE;
1282 scanner = __bt_find_scanner_from_list(sender);
1283 if (scanner == NULL) {
1284 BT_ERR("There is NO available scanner.");
1285 return BLUETOOTH_ERROR_NOT_FOUND;
1288 for (l = scanner->filter_list; l != NULL; l = g_slist_next(l)) {
1289 filter_data = l->data;
1290 if (filter_data->slot_id == slot_id) {
1291 is_slot_id_found = TRUE;
1295 if (is_slot_id_found == FALSE) {
1296 BT_ERR("There is NO registered slot.");
1297 return BLUETOOTH_ERROR_NOT_FOUND;
1300 proxy = _bt_get_adapter_proxy();
1301 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1303 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_clear",
1304 g_variant_new("(ii)", 0, slot_id),
1305 G_DBUS_CALL_FLAGS_NONE,
1309 BT_ERR("scan_filter_clear Fail: %s", error->message);
1310 g_clear_error(&error);
1313 scanner->filter_list = g_slist_remove(scanner->filter_list, filter_data);
1314 g_free(filter_data);
1317 g_variant_unref(ret);
1318 return BLUETOOTH_ERROR_NONE;
1321 int _bt_unregister_all_scan_filters(const char *sender)
1324 GError *error = NULL;
1326 bt_adapter_le_scanner_t *scanner = NULL;
1327 bluetooth_le_scan_filter_t *filter_data = NULL;
1330 scanner = __bt_find_scanner_from_list(sender);
1331 if (scanner == NULL) {
1332 BT_ERR("There is NO available scanner.");
1333 return BLUETOOTH_ERROR_NOT_FOUND;
1336 proxy = _bt_get_adapter_proxy();
1337 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1339 for (l = scanner->filter_list; l != NULL; l = g_slist_next(l)) {
1340 filter_data = l->data;
1342 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_clear",
1343 g_variant_new("(ii)", 0, filter_data->slot_id),
1344 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1347 BT_ERR("scan_filter_clear Fail: %s", error->message);
1348 g_clear_error(&error);
1351 g_variant_unref(ret);
1354 g_slist_free_full(scanner->filter_list, g_free);
1355 scanner->filter_list = NULL;
1357 return BLUETOOTH_ERROR_NONE;
1360 int _bt_start_le_scan(const char *sender)
1363 GError *error = NULL;
1365 bt_adapter_le_scanner_t *scanner = __bt_find_scanner_from_list(sender);
1367 if (scanner == NULL) {
1368 scanner = g_malloc0(sizeof(bt_adapter_le_scanner_t));
1369 retv_if(scanner == NULL, BLUETOOTH_ERROR_INTERNAL);
1371 scanner->sender = strdup(sender);
1372 scanner_list = g_slist_append(scanner_list, scanner);
1375 if (scanner->is_scanning == TRUE) {
1376 BT_ERR("BT is already in LE scanning");
1377 return BLUETOOTH_ERROR_IN_PROGRESS;
1379 scanner->is_scanning = TRUE;
1381 proxy = _bt_get_adapter_proxy();
1382 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1384 if (_bt_is_le_scanning()) {
1385 if (scan_filter_enabled == TRUE) {
1386 if (scanner->filter_list == NULL) {
1387 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_enable",
1388 g_variant_new("(ib)", 0, FALSE),
1389 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1392 BT_ERR("scan_filter_clear Fail: %s", error->message);
1393 g_clear_error(&error);
1397 g_variant_unref(ret);
1398 BT_INFO("Disable LE Scan Filter");
1399 scan_filter_enabled = FALSE;
1401 BT_INFO("LE Filter Scan is continue");
1404 BT_INFO("LE Full Scan is already on progress");
1406 return BLUETOOTH_ERROR_NONE;
1408 if (is_le_set_scan_parameter == FALSE) {
1409 /* Set default scan parameter same with BT_ADAPTER_LE_SCAN_MODE_LOW_ENERGY */
1410 bluetooth_le_scan_params_t scan_params;
1411 scan_params.type = BT_LE_ACTIVE_SCAN;
1412 scan_params.interval = 5000;
1413 scan_params.window = 500;
1414 _bt_set_scan_parameters(&scan_params);
1417 if (scanner->filter_list == NULL) {
1418 BT_INFO("Start LE Full Scan");
1419 scan_filter_enabled = FALSE;
1421 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_enable",
1422 g_variant_new("(ib)", 0, TRUE),
1423 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
1426 BT_ERR("scan_filter_clear Fail: %s", error->message);
1427 g_clear_error(&error);
1431 g_variant_unref(ret);
1432 BT_INFO("Enable LE Scan Filter");
1433 scan_filter_enabled = TRUE;
1437 ret = g_dbus_proxy_call_sync(proxy, "StartLEDiscovery",
1438 NULL, G_DBUS_CALL_FLAGS_NONE,
1442 BT_ERR("StartLEDiscovery Fail: %s", error->message);
1443 g_clear_error(&error);
1444 return BLUETOOTH_ERROR_INTERNAL;
1448 g_variant_unref(ret);
1449 return BLUETOOTH_ERROR_NONE;
1452 int _bt_stop_le_scan(const char *sender)
1455 GError *error = NULL;
1457 bt_adapter_le_scanner_t *scanner = __bt_find_scanner_from_list(sender);
1459 gboolean next_scanning = FALSE;
1460 gboolean need_scan_filter = TRUE;
1462 if (scanner == NULL || scanner->is_scanning == FALSE)
1463 return BLUETOOTH_ERROR_NOT_IN_OPERATION;
1465 scanner->is_scanning = FALSE;
1467 for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
1469 if (scanner->is_scanning == TRUE) {
1470 next_scanning = TRUE;
1471 if (scanner->filter_list == NULL)
1472 need_scan_filter = FALSE;
1476 proxy = _bt_get_adapter_proxy();
1477 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
1479 if (next_scanning == TRUE) {
1480 if (scan_filter_enabled == FALSE && need_scan_filter == TRUE) {
1481 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_enable",
1482 g_variant_new("(ib)", 0, TRUE),
1483 G_DBUS_CALL_FLAGS_NONE,
1487 BT_ERR("scan_filter_clear Fail: %s", error->message);
1488 g_clear_error(&error);
1492 g_variant_unref(ret);
1493 BT_INFO("Enable LE Scan Filter");
1494 scan_filter_enabled = TRUE;
1496 return BLUETOOTH_ERROR_NONE;
1498 if (scan_filter_enabled == TRUE) {
1499 ret = g_dbus_proxy_call_sync(proxy, "scan_filter_enable",
1500 g_variant_new("(ib)", 0, FALSE),
1501 G_DBUS_CALL_FLAGS_NONE,
1505 BT_ERR("scan_filter_clear Fail: %s", error->message);
1506 g_clear_error(&error);
1510 g_variant_unref(ret);
1511 BT_INFO("Disable LE Scan Filter");
1513 BT_INFO("Just stop LE scan");
1517 ret = g_dbus_proxy_call_sync(proxy, "StopLEDiscovery",
1518 NULL, G_DBUS_CALL_FLAGS_NONE,
1521 BT_ERR("LE Scan stop failed");
1522 return BLUETOOTH_ERROR_INTERNAL;
1525 scan_filter_enabled = FALSE;
1526 is_le_set_scan_parameter = FALSE;
1528 g_variant_unref(ret);
1529 return BLUETOOTH_ERROR_NONE;
1532 void _bt_disable_all_scanner_status(void)
1535 bt_adapter_le_scanner_t *scanner;
1537 for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
1539 scanner->is_scanning = FALSE;
1543 void _bt_set_le_scan_status(gboolean mode)
1545 is_le_scanning = mode;
1548 gboolean _bt_is_le_scanning(void)
1550 return is_le_scanning;
1553 void _bt_set_le_scan_type(bt_le_scan_type_t type)
1555 le_scan_type = type;
1558 bt_le_scan_type_t _bt_get_le_scan_type(void)
1560 return le_scan_type;
1563 static gboolean __bt_check_scan_result_uuid(const char *adv_data,
1564 int adv_data_len, const char *svc_uuid, int uuid_len,
1565 const char *uuid_mask, char ad_type)
1571 __bt_get_ad_data_by_type((char*)adv_data, adv_data_len,
1572 ad_type, &data, &data_len);
1574 for (i = 0; i < data_len; i += uuid_len) {
1575 if (uuid_len > (data_len - i))
1578 if (_bt_byte_arr_cmp_with_mask(data + i,
1579 svc_uuid, uuid_mask, uuid_len) == 0) {
1590 static gboolean __bt_check_scan_result_with_filter(const char *device_address,
1591 const char *adv_data, int adv_data_len,
1592 const char *scan_data, int scan_data_len,
1593 const bt_adapter_le_scanner_t *scanner)
1596 bluetooth_le_scan_filter_t *filter_data = NULL;
1599 gboolean is_matched = FALSE;
1601 if (scanner->filter_list == NULL) {
1602 BT_INFO("This scanner is on Full Scan.");
1606 for (l = scanner->filter_list; l != NULL; l = g_slist_next(l)) {
1607 filter_data = l->data;
1609 if (filter_data->added_features &
1610 BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_ADDRESS) {
1611 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1613 _bt_convert_addr_type_to_string(address,
1614 filter_data->device_address.addr);
1615 if (strncmp(address, device_address,
1616 BT_ADDRESS_STRING_SIZE) != 0)
1620 if (filter_data->added_features &
1621 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_UUID) {
1624 if (__bt_check_scan_result_uuid(adv_data,
1626 (char*)filter_data->service_uuid.data.data,
1627 filter_data->service_uuid.data_len,
1628 (char*)filter_data->service_uuid_mask.data.data,
1629 BT_LE_AD_TYPE_INCOMP_LIST_16_BIT_SERVICE_CLASS_UUIDS)
1632 if (__bt_check_scan_result_uuid(adv_data,
1634 (char*)filter_data->service_uuid.data.data,
1635 filter_data->service_uuid.data_len,
1636 (char*)filter_data->service_uuid_mask.data.data,
1637 BT_LE_AD_TYPE_COMP_LIST_16_BIT_SERVICE_CLASS_UUIDS)
1640 if (__bt_check_scan_result_uuid(adv_data,
1642 (char*)filter_data->service_uuid.data.data,
1643 filter_data->service_uuid.data_len,
1644 (char*)filter_data->service_uuid_mask.data.data,
1645 BT_LE_AD_TYPE_INCOMP_LIST_128_BIT_SERVICE_CLASS_UUIDS)
1648 if (__bt_check_scan_result_uuid(adv_data,
1650 (char*)filter_data->service_uuid.data.data,
1651 filter_data->service_uuid.data_len,
1652 (char*)filter_data->service_uuid_mask.data.data,
1653 BT_LE_AD_TYPE_COMP_LIST_128_BIT_SERVICE_CLASS_UUIDS)
1656 if (__bt_check_scan_result_uuid(scan_data,
1658 (char*)filter_data->service_uuid.data.data,
1659 filter_data->service_uuid.data_len,
1660 (char*)filter_data->service_uuid_mask.data.data,
1661 BT_LE_AD_TYPE_INCOMP_LIST_16_BIT_SERVICE_CLASS_UUIDS)
1664 if (__bt_check_scan_result_uuid(scan_data,
1666 (char*)filter_data->service_uuid.data.data,
1667 filter_data->service_uuid.data_len,
1668 (char*)filter_data->service_uuid_mask.data.data,
1669 BT_LE_AD_TYPE_COMP_LIST_16_BIT_SERVICE_CLASS_UUIDS)
1672 if (__bt_check_scan_result_uuid(scan_data,
1674 (char*)filter_data->service_uuid.data.data,
1675 filter_data->service_uuid.data_len,
1676 (char*)filter_data->service_uuid_mask.data.data,
1677 BT_LE_AD_TYPE_INCOMP_LIST_128_BIT_SERVICE_CLASS_UUIDS)
1680 if (__bt_check_scan_result_uuid(scan_data,
1682 (char*)filter_data->service_uuid.data.data,
1683 filter_data->service_uuid.data_len,
1684 (char*)filter_data->service_uuid_mask.data.data,
1685 BT_LE_AD_TYPE_COMP_LIST_128_BIT_SERVICE_CLASS_UUIDS)
1689 if (is_matched == FALSE)
1692 if (filter_data->added_features &
1693 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_SOLICITATION_UUID) {
1696 if (__bt_check_scan_result_uuid(adv_data,
1698 (char*)filter_data->service_solicitation_uuid.data.data,
1699 filter_data->service_solicitation_uuid.data_len,
1700 (char*)filter_data->service_solicitation_uuid_mask.data.data,
1701 BT_LE_AD_TYPE_LIST_16_BIT_SERVICE_SOLICITATION_UUIDS)
1704 if (__bt_check_scan_result_uuid(adv_data,
1706 (char*)filter_data->service_solicitation_uuid.data.data,
1707 filter_data->service_solicitation_uuid.data_len,
1708 (char*)filter_data->service_solicitation_uuid_mask.data.data,
1709 BT_LE_AD_TYPE_LIST_128_BIT_SERVICE_SOLICITATION_UUIDS)
1712 if (__bt_check_scan_result_uuid(scan_data,
1714 (char*)filter_data->service_solicitation_uuid.data.data,
1715 filter_data->service_solicitation_uuid.data_len,
1716 (char*)filter_data->service_solicitation_uuid_mask.data.data,
1717 BT_LE_AD_TYPE_LIST_16_BIT_SERVICE_SOLICITATION_UUIDS)
1720 if (__bt_check_scan_result_uuid(scan_data,
1722 (char*)filter_data->service_solicitation_uuid.data.data,
1723 filter_data->service_solicitation_uuid.data_len,
1724 (char*)filter_data->service_solicitation_uuid_mask.data.data,
1725 BT_LE_AD_TYPE_LIST_128_BIT_SERVICE_SOLICITATION_UUIDS)
1729 if (is_matched == FALSE)
1732 if (filter_data->added_features &
1733 BLUETOOTH_LE_SCAN_FILTER_FEATURE_DEVICE_NAME) {
1734 char name[BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX] = {0, };
1739 __bt_get_ad_data_by_type((char*)adv_data, adv_data_len,
1740 BT_LE_AD_TYPE_COMPLETE_LOCAL_NAME,
1743 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1744 data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
1745 memcpy(name, data, data_len);
1746 name[data_len] = '\0';
1749 if (strncmp(filter_data->device_name,
1750 name, data_len) == 0)
1753 __bt_get_ad_data_by_type((char*)scan_data,
1755 BT_LE_AD_TYPE_COMPLETE_LOCAL_NAME,
1758 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1759 data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
1760 memcpy(name, data, data_len);
1761 name[data_len] = '\0';
1764 if (strncmp(filter_data->device_name,
1765 name, data_len) == 0)
1769 if (is_matched == FALSE)
1772 if (filter_data->added_features &
1773 BLUETOOTH_LE_SCAN_FILTER_FEATURE_MANUFACTURER_DATA) {
1778 __bt_get_ad_data_by_type((char*)adv_data,
1780 BT_LE_AD_TYPE_MANUFACTURER_SPECIFIC_DATA,
1784 int manufacturer_id;
1785 manufacturer_id = (data[1] << 8) + data[0];
1787 if (filter_data->manufacturer_id == manufacturer_id) {
1788 if (filter_data->manufacturer_data.data_len == 0) {
1791 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1792 data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
1793 if (_bt_byte_arr_cmp_with_mask(data + 2,
1794 (char*)filter_data->manufacturer_data.data.data,
1795 (char*)filter_data->manufacturer_data_mask.data.data,
1796 data_len - 2) == 0) {
1804 __bt_get_ad_data_by_type((char*)scan_data,
1806 BT_LE_AD_TYPE_MANUFACTURER_SPECIFIC_DATA,
1810 int manufacturer_id;
1811 manufacturer_id = (data[1] << 8) + data[0];
1813 if (filter_data->manufacturer_id == manufacturer_id) {
1814 if (filter_data->manufacturer_data.data_len == 0) {
1817 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1818 data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
1819 if (_bt_byte_arr_cmp_with_mask(data + 2,
1820 (char*)filter_data->manufacturer_data.data.data,
1821 (char*)filter_data->manufacturer_data_mask.data.data,
1822 data_len - 2) == 0) {
1831 if (is_matched == FALSE)
1834 if (filter_data->added_features &
1835 BLUETOOTH_LE_SCAN_FILTER_FEATURE_SERVICE_DATA) {
1840 __bt_get_ad_data_by_type((char*)adv_data,
1842 BT_LE_AD_TYPE_SERVICE_DATA,
1845 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1846 data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
1847 if (_bt_byte_arr_cmp_with_mask(data,
1848 (char*)filter_data->service_data.data.data,
1849 (char*)filter_data->service_data_mask.data.data,
1856 __bt_get_ad_data_by_type((char*)scan_data,
1858 BT_LE_AD_TYPE_SERVICE_DATA,
1861 if (data_len >= BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX)
1862 data_len = BLUETOOTH_ADVERTISING_DATA_LENGTH_MAX - 1;
1863 if (_bt_byte_arr_cmp_with_mask(data,
1864 (char*)filter_data->service_data.data.data,
1865 (char*)filter_data->service_data_mask.data.data,
1873 if (is_matched == FALSE)
1877 BT_INFO("The scan result is conformable.");
1881 BT_INFO("The scan result is NOT conformable.");
1885 void _bt_send_scan_result_event(const bt_remote_le_dev_info_t *le_dev_info,
1886 const bt_le_adv_info_t *adv_info)
1888 int result = BLUETOOTH_ERROR_NONE;
1890 GVariant *scan_data_param, *adv_data_param;
1892 bt_adapter_le_scanner_t *scanner = NULL;
1893 const char *adv_data = NULL;
1894 int adv_data_len = 0;
1895 const char *scan_data = NULL;
1896 int scan_data_len = 0;
1898 ret_if(le_dev_info == NULL);
1899 if (_bt_get_le_scan_type() == BT_LE_ACTIVE_SCAN)
1900 ret_if(adv_info == NULL);
1902 if (_bt_get_le_scan_type() == BT_LE_PASSIVE_SCAN) {
1903 adv_data = le_dev_info->adv_data;
1904 adv_data_len = le_dev_info->adv_data_len;
1905 scan_data = le_dev_info->adv_data;
1908 adv_data = adv_info->data;
1909 adv_data_len = adv_info->data_len;
1910 scan_data = le_dev_info->adv_data;
1911 scan_data_len = le_dev_info->adv_data_len;
1914 for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
1916 if (scanner->is_scanning == FALSE)
1919 if (__bt_check_scan_result_with_filter(le_dev_info->address,
1920 adv_data, adv_data_len, scan_data, scan_data_len,
1924 adv_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1925 adv_data, adv_data_len, TRUE, NULL, NULL);
1926 scan_data_param = g_variant_new_from_data((const GVariantType *)"ay",
1927 scan_data, scan_data_len, TRUE, NULL, NULL);
1929 param = g_variant_new("(isnnn@ayn@ay)",
1931 le_dev_info->address,
1932 le_dev_info->addr_type,
1940 _bt_send_event_to_dest(scanner->sender, BT_LE_ADAPTER_EVENT,
1941 BLUETOOTH_EVENT_REMOTE_LE_DEVICE_FOUND, param);
1943 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_REMOTE_LE_DEVICE_FOUND, param);
1948 void _bt_send_ibeacon_scan_result_event(const bt_remote_ibeacon_dev_info_t *ibeacon_dev_info)
1950 int result = BLUETOOTH_ERROR_NONE;
1953 bt_adapter_le_scanner_t *scanner = NULL;
1955 ret_if(ibeacon_dev_info == NULL);
1956 BT_DBG("_bt_send_ibeacon_scan_result_event");
1958 for (l = scanner_list; l != NULL; l = g_slist_next(l)) {
1960 if (scanner->is_scanning == FALSE)
1963 param = g_variant_new("(isnnnsnnn)",
1965 ibeacon_dev_info->address,
1966 ibeacon_dev_info->addr_type,
1967 ibeacon_dev_info->company_id,
1968 ibeacon_dev_info->ibeacon_type,
1969 ibeacon_dev_info->uuid,
1970 ibeacon_dev_info->major_id,
1971 ibeacon_dev_info->minor_id,
1972 ibeacon_dev_info->measured_power);
1974 _bt_send_event(BT_LE_ADAPTER_EVENT, BLUETOOTH_EVENT_REMOTE_IBEACON_DEVICE_FOUND, param);
1978 int _bt_add_white_list(bluetooth_device_address_t *device_address, bluetooth_device_address_type_t address_type)
1981 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
1982 GError *error = NULL;
1985 if (__bt_is_factory_test_mode()) {
1986 BT_ERR("Unable to add white list in factory binary !!");
1987 return BLUETOOTH_ERROR_NOT_SUPPORT;
1990 BT_CHECK_PARAMETER(device_address, return);
1992 if (address_type != BLUETOOTH_DEVICE_PUBLIC_ADDRESS &&
1993 address_type != BLUETOOTH_DEVICE_RANDOM_ADDRESS)
1994 return BLUETOOTH_ERROR_INVALID_PARAM;
1996 _bt_convert_addr_type_to_string(address, device_address->addr);
1998 proxy = _bt_get_adapter_proxy();
1999 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2001 ret = g_dbus_proxy_call_sync(proxy, "AddDeviceWhiteList",
2002 g_variant_new("(su)", address, address_type),
2003 G_DBUS_CALL_FLAGS_NONE, -1,
2007 BT_ERR("AddDeviceWhiteList Fail: %s", error->message);
2008 g_clear_error(&error);
2009 return BLUETOOTH_ERROR_INTERNAL;
2013 g_variant_unref(ret);
2014 BT_INFO("Add white list");
2016 return BLUETOOTH_ERROR_NONE;
2019 int _bt_remove_white_list(bluetooth_device_address_t *device_address, bluetooth_device_address_type_t address_type)
2022 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2023 GError *error = NULL;
2026 if (__bt_is_factory_test_mode()) {
2027 BT_ERR("Unable to remove white list in factory binary !!");
2028 return BLUETOOTH_ERROR_NOT_SUPPORT;
2031 BT_CHECK_PARAMETER(device_address, return);
2033 if (address_type != BLUETOOTH_DEVICE_PUBLIC_ADDRESS &&
2034 address_type != BLUETOOTH_DEVICE_RANDOM_ADDRESS)
2035 return BLUETOOTH_ERROR_INVALID_PARAM;
2037 _bt_convert_addr_type_to_string(address, device_address->addr);
2039 proxy = _bt_get_adapter_proxy();
2040 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2042 ret = g_dbus_proxy_call_sync(proxy, "RemoveDeviceWhiteList",
2043 g_variant_new("(su)", address, address_type),
2044 G_DBUS_CALL_FLAGS_NONE, -1,
2048 BT_ERR("RemoveDeviceWhiteList Fail: %s", error->message);
2049 g_clear_error(&error);
2050 return BLUETOOTH_ERROR_INTERNAL;
2054 g_variant_unref(ret);
2055 BT_INFO("Remove white list");
2057 return BLUETOOTH_ERROR_NONE;
2060 int _bt_clear_white_list(void)
2063 GError *error = NULL;
2066 if (__bt_is_factory_test_mode()) {
2067 BT_ERR("Unable to clear white list in factory binary !!");
2068 return BLUETOOTH_ERROR_NOT_SUPPORT;
2071 proxy = _bt_get_adapter_proxy();
2072 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2074 ret = g_dbus_proxy_call_sync(proxy, "ClearDeviceWhiteList",
2075 NULL, G_DBUS_CALL_FLAGS_NONE,
2079 BT_ERR("ClearDeviceWhiteList Fail: %s", error->message);
2080 g_clear_error(&error);
2081 return BLUETOOTH_ERROR_INTERNAL;
2084 g_variant_unref(ret);
2086 BT_INFO("Clear white list");
2088 return BLUETOOTH_ERROR_NONE;
2091 int _bt_initialize_ipsp(void)
2095 GError *error = NULL;
2098 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2099 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2100 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2103 proxy = _bt_get_adapter_proxy();
2104 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2106 ret = g_dbus_proxy_call_sync(proxy, "InitializeIpsp",
2107 NULL, G_DBUS_CALL_FLAGS_NONE,
2110 BT_ERR("Initialize IPSP Failed :[%s]", error->message);
2111 g_clear_error(&error);
2112 return BLUETOOTH_ERROR_INTERNAL;
2115 g_variant_unref(ret);
2117 BT_INFO("IPSP initialization called successfully");
2119 return BLUETOOTH_ERROR_NONE;
2122 int _bt_deinitialize_ipsp(void)
2126 GError *error = NULL;
2129 if (_bt_adapter_get_status() != BT_ACTIVATED &&
2130 _bt_adapter_get_le_status() != BT_LE_ACTIVATED) {
2131 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
2134 proxy = _bt_get_adapter_proxy();
2135 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2137 ret = g_dbus_proxy_call_sync(proxy, "DeinitializeIpsp",
2138 NULL, G_DBUS_CALL_FLAGS_NONE,
2141 BT_ERR("De-Initialize IPSP Failed :[%s]", error->message);
2142 g_clear_error(&error);
2143 return BLUETOOTH_ERROR_INTERNAL;
2146 g_variant_unref(ret);
2148 BT_INFO("IPSP De-initialization called successfully");
2150 return BLUETOOTH_ERROR_NONE;
2153 int _bt_le_read_maximum_data_length(
2154 bluetooth_le_read_maximum_data_length_t *max_le_datalength)
2156 GError *error = NULL;
2158 GVariant *reply = NULL;
2159 guint16 max_tx_octets, max_tx_time;
2160 guint16 max_rx_octets, max_rx_time;
2162 proxy = _bt_get_adapter_proxy();
2163 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2165 reply = g_dbus_proxy_call_sync(proxy, "LEReadMaximumDataLength",
2166 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
2168 g_object_unref(proxy);
2170 if (reply == NULL) {
2171 BT_ERR("LEReadMaximumDataLength dBUS-RPC failed");
2172 if (error != NULL) {
2173 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
2174 error->code, error->message);
2175 g_clear_error(&error);
2177 return BLUETOOTH_ERROR_INTERNAL;
2180 g_variant_get(reply, "(qqqq)", &max_tx_octets, &max_tx_time,
2181 &max_rx_octets, &max_rx_time);
2183 max_le_datalength->max_tx_octets = max_tx_octets;
2184 max_le_datalength->max_tx_time = max_tx_time;
2185 max_le_datalength->max_rx_octets = max_rx_octets;
2186 max_le_datalength->max_rx_time = max_rx_time;
2188 g_variant_unref(reply);
2190 return BLUETOOTH_ERROR_NONE;
2192 int _bt_le_write_host_suggested_default_data_length(
2193 const unsigned int def_tx_Octets, const unsigned int def_tx_Time)
2195 GError *error = NULL;
2197 GVariant *reply = NULL;
2199 proxy = _bt_get_adapter_proxy();
2200 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2202 reply = g_dbus_proxy_call_sync(proxy,
2203 "LEWriteHostSuggestedDataLength",
2204 g_variant_new("(qq)", def_tx_Octets, def_tx_Time),
2205 G_DBUS_CALL_FLAGS_NONE,
2210 g_object_unref(proxy);
2212 if (reply == NULL) {
2213 BT_ERR("_bt_le_write_host_suggested_default_data_length dBUS-RPC failed");
2214 if (error != NULL) {
2215 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
2216 error->code, error->message);
2217 g_clear_error(&error);
2219 return BLUETOOTH_ERROR_INTERNAL;
2222 g_variant_unref(reply);
2224 return BLUETOOTH_ERROR_NONE;
2227 int _bt_le_read_host_suggested_default_data_length(
2228 bluetooth_le_read_host_suggested_data_length_t *def_data_length)
2230 GError *error = NULL;
2232 GVariant *reply = NULL;
2233 guint16 def_tx_octets, def_tx_time;
2235 proxy = _bt_get_adapter_proxy();
2236 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2238 reply = g_dbus_proxy_call_sync(proxy, "LEReadHostSuggestedDataLength",
2239 NULL, G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
2241 if (reply == NULL) {
2242 BT_ERR("LEReadHostSuggestedDataLength dBUS-RPC failed");
2243 if (error != NULL) {
2244 BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
2245 error->code, error->message);
2246 g_clear_error(&error);
2248 return BLUETOOTH_ERROR_INTERNAL;
2251 g_variant_get(reply, "(qq)", &def_tx_octets, &def_tx_time);
2253 def_data_length->def_tx_octets = def_tx_octets;
2254 def_data_length->def_tx_time = def_tx_time;
2256 g_variant_unref(reply);
2258 return BLUETOOTH_ERROR_NONE;
2261 int _bt_le_set_data_length(bluetooth_device_address_t *device_address,
2262 const unsigned int max_tx_Octets, const unsigned int max_tx_Time)
2264 GError *error = NULL;
2265 guint16 txOctets = max_tx_Octets;
2266 guint16 txTime = max_tx_Time;
2267 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
2268 gchar *device_path = NULL;
2269 GDBusConnection *conn;
2270 GDBusProxy *device_proxy;
2272 _bt_convert_addr_type_to_string(address, device_address->addr);
2274 device_path = _bt_get_device_object_path(address);
2276 if (device_path == NULL) {
2277 BT_DBG("Device path is null");
2278 return BLUETOOTH_ERROR_INTERNAL;
2281 conn = _bt_gdbus_get_system_gconn();
2283 BT_ERR("conn == NULL");
2284 g_free(device_path);
2285 return BLUETOOTH_ERROR_INTERNAL;
2288 device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
2289 NULL, BT_BLUEZ_NAME,
2290 device_path, BT_DEVICE_INTERFACE, NULL, NULL);
2292 g_free(device_path);
2293 retv_if(device_proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
2295 g_dbus_proxy_call_sync(device_proxy,
2297 g_variant_new("(qq)", txOctets, txTime),
2298 G_DBUS_CALL_FLAGS_NONE,
2303 g_object_unref(device_proxy);
2306 BT_ERR("LESetDataLength error: [%s]", error->message);
2307 g_error_free(error);
2308 return BLUETOOTH_ERROR_INTERNAL;
2311 return BLUETOOTH_ERROR_NONE;