4 * Copyright (c) 2015 -2016 Samsung Electronics Co., Ltd All Rights Reserved.
6 * Contact: Anupam Roy <anupam.r@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
24 #include <hardware/bluetooth.h>
25 #include <hardware/bt_gatt.h>
34 #include <vconf-keys.h>
35 #include <vconf-internal-radio-keys.h>
42 #include "bt-hal-log.h"
43 #include "bt-hal-msg.h"
44 #include "bt-hal-utils.h"
46 #include <bt-hal-adapter-dbus-handler.h>
47 #include <bt-hal-dbus-common-utils.h>
49 #include "bt-hal-gatt-server.h"
50 #include "bt-hal-gatt.h"
58 } bt_adapter_le_feature_info_t;
62 gboolean is_multi_adv; /* To be removed once we complete descope Legacy Adv */
64 gboolean is_advertising;
67 } bt_adapter_le_adv_slot_t;
69 static bt_adapter_le_feature_info_t le_feature_info = { 1, 0, 0 };
70 static bt_adapter_le_adv_slot_t *le_adv_slot = NULL;
77 } bt_hal_adv_event_data_t;
80 #define BT_HAL_ADV_CONNECTABLE 0x00 /* ADV_IND */
81 #define BT_HAL_ADV_CONNECTABLE_DIRECT_HIGH 0x01 /* ADV_DIRECT_IND, high duty cycle */
82 #define BT_HAL_ADV_SCANNABLE 0x02 /* ADV_SCAN_IND */
83 #define BT_HAL_ADV_NON_CONNECTABLE 0x03 /* ADV_NONCOND_IND */
84 #define BT_HAL_ADV_CONNECTABLE_DIRECT_LOW 0x04 /* ADV_DIRECT_IND, low duty cycle */
86 #define BT_HAL_ADV_INTERVAL_MIN 20 /* msec */
87 #define BT_HAL_ADV_INTERVAL_MAX 10240
88 #define BT_HAL_ADV_INTERVAL_SPLIT 0.625
89 #define BT_HAL_DEFAULT_ADV_MIN_INTERVAL 500
90 #define BT_HAL_DEFAULT_ADV_MAX_INTERVAL 500
91 #define BT_HAL_ADV_FILTER_POLICY_DEFAULT 0x00
92 #define BT_HAL_ADV_TYPE_DEFAULT 0x00
93 #define BT_HAL_ADV_FILTER_POLICY_ALLOW_SCAN_CONN_WL_ONLY 0x03
95 /* Multi Advertisement callback event */
96 #define BT_HAL_MULTI_ADV_ENB_EVT 1
97 #define BT_HAL_MULTI_ADV_DISABLE_EVT 2
98 #define BT_HAL_MULTI_ADV_PARAM_EVT 3
99 #define BT_HAL_MULTI_ADV_DATA_EVT 4
100 #define BT_HAL_MULTI_ADV_UPDATE_EVT 5
101 #define BT_HAL_LEGACY_ADV_STATUS 6
103 handle_stack_msg gatt_le_event_cb;
105 /* Advertising filter_policy */
106 static int adv_filter_policy;
108 /* Forward declarations */
109 static gboolean __bt_hal_is_factory_test_mode(void);
110 static void __bt_hal_free_le_adv_slot(void);
111 static gboolean __bt_hal_adv_event_cb(gpointer param);
115 /* Enable LE Adapter */
116 int _bt_hal_le_enable(void)
118 return _bt_hal_dbus_enable_le();
121 /* Disable LE Adapter */
122 int _bt_hal_le_disable(void)
124 return _bt_hal_dbus_disable_le();
127 void _bt_hal_set_filter_policy_param(int filter_policy)
129 adv_filter_policy = filter_policy;
132 int _bt_hal_set_le_static_random_address(uint8_t enable)
134 GError *error = NULL;
138 proxy = _bt_hal_get_adapter_proxy();
140 return BT_STATUS_FAIL;
142 ret = g_dbus_proxy_call_sync(proxy, "SetLeStaticRandomAddress",
143 g_variant_new("(b)", (enable ? TRUE : FALSE)),
144 G_DBUS_CALL_FLAGS_NONE,
150 ERR("Set static address Fail: %s", error->message);
151 g_clear_error(&error);
152 return BT_STATUS_FAIL;
155 INFO("Set le static address [%d]", enable);
157 g_variant_unref(ret);
159 return BT_STATUS_SUCCESS;
163 static void __bt_hal_free_le_adv_slot(void)
167 if (le_adv_slot == NULL)
170 for (i = 0; i < le_feature_info.adv_inst_max; i++)
171 memset(&le_adv_slot[i], 0x00, sizeof(bt_adapter_le_adv_slot_t));
177 void _bt_hal_unregister_adv_slot_owner(int slot_id)
179 if (le_adv_slot == NULL)
181 INFO("Unregister Adv Slot [%d]", slot_id);
182 memset(&le_adv_slot[slot_id], 0x00, sizeof(bt_adapter_le_adv_slot_t));
185 int _bt_hal_get_adv_slot_adv_handle(int slot_id)
187 if (le_adv_slot == NULL)
190 return le_adv_slot[slot_id].adv_handle;
193 gboolean _bt_hal_is_advertising_in_slot(int slot)
195 return le_adv_slot[slot].is_advertising;
198 void _bt_hal_set_advertising_status(int slot_id, gboolean mode)
200 bt_hal_adv_event_data_t *event;
201 int adv_slot_id = slot_id;
204 server_if = bt_hal_gatts_get_server_if(slot_id);
206 DBG("Advertising enabled [%s] server_slot [%d] server_if[%d]", mode ? "TRUE" : "FALSE", slot_id, server_if);
208 if (le_adv_slot == NULL)
211 le_adv_slot[adv_slot_id].is_advertising = mode;
213 event = g_malloc0(sizeof(bt_hal_adv_event_data_t));
214 event->event = mode ? BT_HAL_MULTI_ADV_ENB_EVT : BT_HAL_MULTI_ADV_DISABLE_EVT;
216 event->server_if = server_if;
217 event->status = BT_STATUS_SUCCESS;
218 /* To be removed later when we completely descope Legacy Adv concept */
220 DBG("adv_slot_id[%d] Is multi ? [%d]", adv_slot_id, le_adv_slot[adv_slot_id].is_multi_adv);
221 if (le_adv_slot[adv_slot_id].is_multi_adv == FALSE)
222 event->event = BT_HAL_LEGACY_ADV_STATUS;
224 if (mode == false ) {
225 DBG("release the adv_slot");
226 bt_hal_gatts_release_adv_slot(server_if);
229 __bt_hal_adv_event_cb((gpointer)event);
232 gboolean _bt_hal_is_advertising(void)
236 if (le_adv_slot == NULL)
239 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
240 if (le_adv_slot[i].is_advertising == TRUE)
247 int _bt_hal_le_init(void)
249 le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
250 return BT_STATUS_SUCCESS;
252 void _bt_hal_le_deinit(void)
254 __bt_hal_free_le_adv_slot();
257 gboolean _bt_hal_update_le_feature_support(const char *item, const char *value,
258 bt_local_le_features_t *le_features)
260 if (item == NULL || value == NULL)
265 if (g_strcmp0(item, "adv_inst_max") == 0) {
268 slot_num = atoi(value);
269 INFO("slot_num:[%d]", slot_num);
271 ERR("ERR:Advertising MAX instance [%d]", slot_num);
275 if (slot_num != le_feature_info.adv_inst_max) {
276 __bt_hal_free_le_adv_slot();
277 le_feature_info.adv_inst_max = slot_num;
278 INFO("Advertising instance max : %d", le_feature_info.adv_inst_max);
279 le_adv_slot = g_malloc0(sizeof(bt_adapter_le_adv_slot_t) * le_feature_info.adv_inst_max);
282 INFO("Advertising instance max: [%d]", le_feature_info.adv_inst_max);
283 /* Fill LE feature bytes */
284 le_features->max_adv_instance = atoi(value);
286 } else if (g_strcmp0(item, "rpa_offloading") == 0) {
287 le_feature_info.rpa_offloading = atoi(value);
288 INFO("RPA offloading : %d", le_feature_info.rpa_offloading);
290 /* Fill LE feature bytes */
291 le_features->rpa_offload_supported = atoi(value);
293 } else if (g_strcmp0(item, "max_filter") == 0) {
294 le_feature_info.max_filter = atoi(value);
295 INFO("BLE Scan max filter : %d", le_feature_info.max_filter);
297 /* Fill LE feature bytes */
298 le_features->max_adv_filter_supported = atoi(value);
300 } else if (g_strcmp0(item, "2m_phy") == 0) {
301 if (g_strcmp0(value, "true") == 0) {
302 le_feature_info.le_2m_phy = TRUE;
303 /* Fill LE feature bytes */
304 le_features->le_2m_phy_supported = 0x1;
306 le_feature_info.le_2m_phy = FALSE;
307 /* Fill LE feature bytes */
308 le_features->le_2m_phy_supported = 0x0;
310 INFO("2M PHY Supported [%s]", le_feature_info.le_2m_phy ? "TRUE" : "FALSE");
311 } else if (g_strcmp0(item, "coded_phy") == 0) {
312 if (g_strcmp0(value, "true") == 0) {
313 le_feature_info.le_coded_phy = TRUE;
314 /* Fill LE feature bytes */
315 le_features->le_coded_phy_supported = 0x1;
317 le_feature_info.le_coded_phy = FALSE;
318 /* Fill LE feature bytes */
319 le_features->le_coded_phy_supported = 0x0;
321 INFO("CODED PHY Supported [%s]", le_feature_info.le_coded_phy ? "TRUE" : "FALSE");
323 DBG("No registered item");
330 void _bt_hal_free_server_slot(int slot_id)
332 if (le_adv_slot == NULL)
334 memset(&le_adv_slot[slot_id], 0x00, sizeof(bt_adapter_le_adv_slot_t));
337 int _bt_hal_get_available_adv_slot_id(bt_uuid_t *uuid, gboolean use_reserved_slot)
341 if (le_adv_slot == NULL) {
342 ERR("le_adv_slot is NULL");
346 DBG("adv_inst_max : %d", le_feature_info.adv_inst_max);
348 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
349 if (le_adv_slot[i].initialized == 0)
351 if (memcmp(uuid->uu, le_adv_slot[i].app_uuid.uu, sizeof(bt_uuid_t)) == 0) {
352 DBG("UUID [%s] matched, return slot [%d]", btuuid2str(uuid->uu), i);
357 /* We should consider 'use_reverved_slot' in later */
358 if (le_feature_info.adv_inst_max <= 1)
360 else if (use_reserved_slot == TRUE)
365 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
366 if (le_adv_slot[i].initialized == 0) {
367 DBG("Slot to be allocated [%d] UUID to be registered [%s]",
368 i, btuuid2str(uuid->uu));
369 le_adv_slot[i].initialized = 1;
370 memcpy(&le_adv_slot[i].app_uuid.uu, &uuid->uu, sizeof(bt_uuid_t));
378 void _bt_hal_get_gatt_server_instance_initialized(int *instance)
381 if (le_adv_slot == NULL) {
382 ERR("le_adv_slot is NULL");
386 DBG("adv_inst_max : %d", le_feature_info.adv_inst_max);
388 for (i = 0; i < le_feature_info.adv_inst_max; i++) {
389 if (le_adv_slot[i].initialized == 0)
391 DBG("Initialized Slot found: UUID [%s] slot [%d]", le_adv_slot[i].app_uuid.uu, i);
397 gboolean _bt_is_advertising(void)
402 int _bt_set_advertising(const char *sender, int adv_handle,
403 gboolean enable, gboolean use_reserved_slot)
405 return BT_STATUS_UNSUPPORTED;
408 static int __bt_hal_uuid_type(uint8_t* p_uuid)
414 for (i = 0; i != 16; ++i) {
415 if (i == 12 || i == 13)
418 if (p_uuid[i] == BASE_UUID_CONVERTED[i])
427 return BT_HAL_UUID_32;
429 return BT_HAL_UUID_16;
430 return BT_HAL_UUID_128;
433 static void __bt_hal_parse_uuid(int len, char *src, uint8_t *dest, int *length, int is_solicit)
435 int prev_byte_len = 0;
436 /* dest[index] will contain the length followed by AD Type
437 Move length only when different byte_len is found Ex) 2->4, 2->16, 4->16 etc */
442 /* Create Local buffer & copy source 16 bytes in sequence */
445 memset(&uuid, 0, sizeof(bt_uuid_t));
446 memcpy(&uuid.uu, src, 16);
448 /* Compute current UUID's byte length */
449 byte_len = __bt_hal_uuid_type(uuid.uu);
453 if (prev_byte_len == byte_len) {
454 memcpy(&(dest[dest[index] + 1]), &src[12], byte_len);
458 if (dest[index] != 0)
459 index = dest[index] +1;
460 dest[index] = byte_len + 1;
462 dest[index+1] = 0x14; /* AD Type */
464 dest[index+1] = 0x02; /* AD Type */
465 memcpy(&(dest[index + 2]), &src[12], byte_len);
469 /* Update current type */
470 prev_byte_len = byte_len;
474 if (prev_byte_len == byte_len) {
475 memcpy(&(dest[dest[index] + 1]), &src[12], byte_len);;
479 if (dest[index] != 0)
480 index = dest[index] +1;
481 dest[index] = byte_len + 1;
483 dest[index+1] = 0x1F; /* AD Type */
485 dest[index+1] = 0x04; /* AD Type */
486 memcpy(&(dest[index + 2]), &src[12], byte_len);
489 /* Update current type */
490 prev_byte_len = byte_len;
494 if (dest[index] != 0)
495 index = dest[index] +1;
496 dest[index] = byte_len + 1;
498 dest[index+1] = 0x15; /* AD Type */
500 dest[index+1] = 0x06; /* AD Type */
501 memcpy(&(dest[index + 2]), &src[0], byte_len);
502 /* Update current type */
503 prev_byte_len = byte_len;
508 ERR("Abnormal Byte len [%d]", byte_len);
513 /* Process Next 16 bytes of MW UUID */
519 static gboolean __bt_hal_is_factory_test_mode(void)
523 if (vconf_get_bool(VCONFKEY_BT_DUT_MODE, &mode)) {
524 ERR("Get the DUT Mode fail");
529 INFO("DUT Test Mode !!");
536 static gboolean __bt_hal_adv_event_cb(gpointer param)
538 bt_hal_adv_event_data_t *event = (bt_hal_adv_event_data_t*)param;
543 if (!gatt_le_event_cb) {
544 ERR("GATT event callback not registered!!!");
548 switch (event->event) {
549 case BT_HAL_MULTI_ADV_ENB_EVT: {
550 INFO("BLE Advertising enabled slot [%d]", event->server_if);
551 struct hal_ev_multi_adv_enable ev;
552 memset(&ev, 0, sizeof(struct hal_ev_multi_adv_enable));
553 ev.status = event->status;
554 ev.server_instance = event->server_if;
555 gatt_le_event_cb(HAL_EV_MULTI_ADV_ENABLE, (void *)&ev, sizeof(ev));
558 case BT_HAL_MULTI_ADV_DISABLE_EVT: {
559 INFO("BLE Advertising disabled slot [%d]", event->server_if);
560 struct hal_ev_multi_adv_disable ev;
561 memset(&ev, 0, sizeof(struct hal_ev_multi_adv_disable));
562 ev.status = event->status;
563 ev.server_instance = event->server_if;
564 gatt_le_event_cb(HAL_EV_MULTI_ADV_DISABLE, (void *)&ev, sizeof(ev));
567 case BT_HAL_MULTI_ADV_PARAM_EVT: {
568 INFO("Unhandled event slot [%d]", event->server_if);
571 case BT_HAL_MULTI_ADV_UPDATE_EVT: {
572 INFO("BLE Advertising Param update slot [%d]", event->server_if);
573 struct hal_ev_multi_adv_update ev;
574 memset(&ev, 0, sizeof(struct hal_ev_multi_adv_update));
575 ev.status = event->status;
576 ev.server_instance = event->server_if;
577 gatt_le_event_cb(HAL_EV_MULTI_ADV_UPDATE, (void *)&ev, sizeof(ev));
580 case BT_HAL_MULTI_ADV_DATA_EVT: {
581 INFO("BLE Advertising data set slot [%d]", event->server_if);
582 struct hal_ev_multi_adv_data_set ev_data_set;
584 memset(&ev_data_set, 0, sizeof(struct hal_ev_multi_adv_data_set));
585 ev_data_set.status = event->status;
586 ev_data_set.server_instance = event->server_if;
587 gatt_le_event_cb(HAL_EV_MULTI_ADV_DATA_SET, (void *)&ev_data_set, sizeof(ev_data_set));
590 case BT_HAL_LEGACY_ADV_STATUS: {
591 INFO("BLE Legacy Advertising [%d]", event->server_if);
592 struct hal_ev_legacy_adv_status ev;
594 memset(&ev, 0, sizeof(struct hal_ev_legacy_adv_status));
595 ev.status = event->status;
596 ev.server_instance = event->server_if;
597 gatt_le_event_cb(HAL_EV_LEGACY_ADV_ENABLE, (void *)&ev, sizeof(ev));
601 ERR("Unknown event");
610 int _bt_hal_enable_advertising(int server_if, int adv_slot_id, bool enable, bool is_multi_adv)
612 GError *error = NULL;
616 proxy = _bt_hal_get_adapter_proxy();
618 return BT_STATUS_FAIL;
620 if (le_adv_slot[adv_slot_id].is_advertising == TRUE && enable == TRUE)
621 return BT_STATUS_BUSY;
623 if (le_adv_slot[adv_slot_id].initialized == TRUE &&
624 le_adv_slot[adv_slot_id].is_advertising == FALSE &&
626 return BT_STATUS_DONE;
628 if (le_adv_slot[adv_slot_id].hold_timer_id > 0) {
629 g_source_remove(le_adv_slot[adv_slot_id].hold_timer_id);
630 le_adv_slot[adv_slot_id].hold_timer_id = 0;
633 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertising",
634 g_variant_new("(bi)", enable, adv_slot_id),
635 G_DBUS_CALL_FLAGS_NONE,
641 ERR("SetAdvertising Fail: %s", error->message);
642 g_clear_error(&error);
643 return BT_STATUS_FAIL;
646 INFO("Enable advertising [%d] SLot Id [%d] gatt_server [%d] Is Multi? [%d]",
647 enable, adv_slot_id, server_if, is_multi_adv);
649 g_variant_unref(ret);
651 le_adv_slot[adv_slot_id].is_multi_adv = is_multi_adv;
652 le_adv_slot[adv_slot_id].is_advertising = enable;
653 return BT_STATUS_SUCCESS;
656 int _bt_hal_set_advertising_params(int server_if, int min_interval,
657 int max_interval, int adv_type,
658 int chnl_map, int tx_power, int timeout_s)
662 GError *error = NULL;
665 bt_hal_adv_event_data_t *event;
669 proxy = _bt_hal_get_adapter_proxy();
671 return BT_STATUS_FAIL;
673 if (min_interval > max_interval ||
674 min_interval < BT_HAL_ADV_INTERVAL_MIN ||
675 max_interval > BT_HAL_ADV_INTERVAL_MAX)
676 return BT_STATUS_PARM_INVALID;
679 if (adv_type == BT_HAL_ADV_CONNECTABLE_DIRECT_HIGH ||
680 adv_type == BT_HAL_ADV_CONNECTABLE_DIRECT_LOW ||
681 adv_type == BT_HAL_ADV_NON_CONNECTABLE)
682 return BT_STATUS_UNSUPPORTED;
684 if (adv_filter_policy < 0 || adv_filter_policy > 3) {
685 ERR("Invalid filter policy, Setting the filter policy to default");
686 adv_filter_policy = BT_HAL_ADV_FILTER_POLICY_DEFAULT;
689 min = min_interval / BT_HAL_ADV_INTERVAL_SPLIT;
690 max = max_interval / BT_HAL_ADV_INTERVAL_SPLIT;
692 slot_id = bt_hal_gatts_allocate_adv_slot_by_server_if(server_if);
694 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertisingParameters",
695 g_variant_new("(uuuuii)", min, max,
696 adv_filter_policy, adv_type,
697 tx_power, slot_id), G_DBUS_CALL_FLAGS_NONE,
700 ERR("SetAdvertisingParameters Fail: %s", error->message);
701 g_clear_error(&error);
702 return BT_STATUS_FAIL;
705 INFO("Set advertising data");
707 g_variant_unref(ret);
710 * As we need to provide async callback to user from HAL, simply schedule a
711 * callback method which will carry actual result
713 event = g_malloc0(sizeof(bt_hal_adv_event_data_t));
714 event->event = BT_HAL_MULTI_ADV_UPDATE_EVT;
715 event->server_if = server_if;
716 event->status = BT_STATUS_SUCCESS;
717 g_idle_add(__bt_hal_adv_event_cb, (gpointer)event);
719 return BT_STATUS_SUCCESS;
722 /* Takes care of both Scan Response and Advertising data */
723 int _bt_hal_set_advertising_data(btgatt_adv_param_setup_t adv_param_setup)
725 uint8_t adv_data[31];
726 char adv_data_str[(31 * 2) + 1];
729 GError *error = NULL;
730 GVariant *ret = NULL;
731 GVariant *temp = NULL;
732 GVariantBuilder *builder;
733 bt_hal_adv_event_data_t *event;
738 /* Parse data according to Bluez Interface */
739 if (__bt_hal_is_factory_test_mode()) {
740 ERR("Unable to set advertising data in factory binary !!");
741 return BT_STATUS_UNSUPPORTED;
744 /* TODO: Check adapter and LE adapter status */
745 proxy = _bt_hal_get_adapter_proxy();
747 return BT_STATUS_FAIL;
749 slot_id = bt_hal_gatts_allocate_adv_slot_by_server_if(adv_param_setup.server_if);
751 memset(&adv_data, 0, 31);
754 if (adv_param_setup.service_uuid_len > 0) {
755 __bt_hal_parse_uuid(adv_param_setup.service_uuid_len,
756 adv_param_setup.service_uuid, &adv_data[index], &length, FALSE);
758 DBG("After Service UUID:Index [%d]", index);
762 if (adv_param_setup.solicit_uuid_len > 0) {
763 __bt_hal_parse_uuid(adv_param_setup.solicit_uuid_len,
764 adv_param_setup.solicit_uuid, &adv_data[index], &length, TRUE);
766 DBG("After Solicit UUID: Index [%d]", index);
769 /* Service Data UUID*/
770 if (adv_param_setup.service_data_len > 0) {
771 adv_data[index] = 1 + adv_param_setup.service_data_len;
772 adv_data[index+1] = 0x16; /* Fixed */
773 memcpy(&adv_data[index+2], adv_param_setup.service_data, adv_param_setup.service_data_len);
774 index += (2 + adv_param_setup.service_data_len);
775 length += (2 + adv_param_setup.service_data_len);
776 DBG("After Service data: Index [%d]", index);
780 if (adv_param_setup.appearance > 0) {
781 adv_data[index] = 0x03;
782 adv_data[index+1] = 0x19;
783 adv_data[index+2] = (uint8_t) (adv_param_setup.appearance & 0xFF);
784 adv_data[index+3] = (uint8_t) ((adv_param_setup.appearance >> 8) & 0xFF);
787 DBG("After Apperance: Index [%d]", index);
791 if (adv_param_setup.include_txpower != 0) {
792 adv_data[index] = 0x01;
793 adv_data[index+1] = 0x0A;
796 DBG("After TX Power: Index [%d]", index);
800 if (adv_param_setup.include_name != 0) {
801 adv_data[index] = 0x01;
802 adv_data[index+1] = 0x09;
805 DBG("After Name: Index [%d]", index);
808 /* Manufacturer data */
809 if (adv_param_setup.manufacturer_data_len > 0) {
810 adv_data[index] = 1 + adv_param_setup.manufacturer_data_len;
811 adv_data[index+1] = 0xFF;
812 memcpy(&adv_data[index+2], adv_param_setup.manufacturer_data, adv_param_setup.manufacturer_data_len);
813 index += (2 + adv_param_setup.manufacturer_data_len);
814 length += (2 + adv_param_setup.manufacturer_data_len);
815 DBG("After Manuf Data: Index [%d]", index);
819 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
820 for (i = 0; i < length; i++)
821 g_variant_builder_add(builder, "y", adv_data[i]);
823 temp = g_variant_new("ay", builder);
824 g_variant_builder_unref(builder);
826 if (adv_param_setup.set_scan_rsp == 0) {
827 /* Set Advertising data to stack */
828 ret = g_dbus_proxy_call_sync(proxy, "SetAdvertisingData",
829 g_variant_new("(@ayi)", temp, slot_id),
830 G_DBUS_CALL_FLAGS_NONE,
833 /* Set Scan response data to stack */
834 ret = g_dbus_proxy_call_sync(proxy, "SetScanRespData",
835 g_variant_new("(@ayi)", temp, slot_id),
836 G_DBUS_CALL_FLAGS_NONE,
841 ERR("SetAdvertisingData Fail: %s", error->message);
842 g_clear_error(&error);
843 return BT_STATUS_FAIL;
846 for (i = 0; i < length; i++)
847 snprintf(&adv_data_str[i * 2], 3, "%02X", adv_data[i]);
848 INFO("Set adv data. Index [%d] length [%d] Data[%s]", index, length, adv_data_str);
851 g_variant_unref(ret);
854 * As we need to provide async callback to user from HAL, simply schedule a
855 * callback method which will carry actual result
857 event = g_malloc0(sizeof(bt_hal_adv_event_data_t));
858 event->event = BT_HAL_MULTI_ADV_DATA_EVT;
859 event->server_if = adv_param_setup.server_if;
860 event->status = BT_STATUS_SUCCESS;
861 memcpy(&event->data, adv_data, 31);
862 g_idle_add(__bt_hal_adv_event_cb, (gpointer)event);
864 return BT_STATUS_SUCCESS;
867 int _bt_hal_adapter_le_start_scan(void)
870 GError *error = NULL;
875 /* TODO: Check adapter and LE adapter status */
876 proxy = _bt_hal_get_adapter_proxy();
878 return BT_STATUS_FAIL;
880 ret = g_dbus_proxy_call_sync(proxy, "StartLEDiscovery",
881 NULL, G_DBUS_CALL_FLAGS_NONE,
885 ERR("StartLEDiscovery Fail: %s", error->message);
886 g_clear_error(&error);
889 return BT_STATUS_FAIL;
892 g_variant_unref(ret);
894 return BT_STATUS_SUCCESS;
897 int _bt_hal_adapter_le_stop_scan(void)
900 GError *error = NULL;
905 /* TODO: Check adapter and LE adapter status */
906 proxy = _bt_hal_get_adapter_proxy();
908 return BT_STATUS_FAIL;
910 ret = g_dbus_proxy_call_sync(proxy, "StopLEDiscovery",
911 NULL, G_DBUS_CALL_FLAGS_NONE,
915 g_dbus_error_strip_remote_error(error);
916 ERR("StopLEDiscovery Fail: %s", error->message);
918 /* Abnormal case for ARTIK530 */
919 if (g_strrstr(error->message, "No discovery started") ||
920 g_strrstr(error->message, "Operation already in progress")) {
921 g_clear_error(&error);
922 return BT_STATUS_SUCCESS;
925 g_clear_error(&error);
926 return BT_STATUS_FAIL;
930 g_variant_unref(ret);
932 bt_hal_release_pending_adv_ind_list();
934 return BT_STATUS_SUCCESS;
937 /*sets the privacy functionality of the adapter*/
938 int _bt_hal_adapter_le_set_privacy(uint8_t set_privacy)
941 GError *error = NULL;
942 GVariant *result = NULL;
943 proxy = _bt_hal_get_adapter_proxy();
945 return BT_STATUS_FAIL;
947 result = g_dbus_proxy_call_sync(proxy,
949 g_variant_new("(b)", set_privacy),
950 G_DBUS_CALL_FLAGS_NONE,
957 ERR("Failed to SetLePrivacy (Error: %s)", error->message);
958 g_clear_error(&error);
960 ERR("Failed to SetLePrivacy");
961 return BT_STATUS_FAIL;
964 g_variant_unref(result);
965 INFO("SetLePrivacy as %d", set_privacy);
966 return BT_STATUS_SUCCESS;
969 int _bt_hal_adapter_le_set_scan_parameters(
970 int scan_type, int scan_interval, int scan_window)
973 GError *error = NULL;
978 /* TODO: Check adapter and LE adapter status */
979 proxy = _bt_hal_get_adapter_proxy();
981 return BT_STATUS_FAIL;
983 ret = g_dbus_proxy_call_sync(proxy, "SetScanParameters",
984 g_variant_new("(uuu)", scan_type, scan_interval, scan_window),
985 G_DBUS_CALL_FLAGS_NONE, -1, NULL, &error);
988 ERR("SetScanParameters Fail: %s", error->message);
989 g_clear_error(&error);
992 return BT_STATUS_FAIL;
995 g_variant_unref(ret);
997 return BT_STATUS_SUCCESS;
1000 /* To send stack event to hal-av handler */
1001 void _bt_hal_register_gatt_le_dbus_handler_cb(handle_stack_msg cb)
1003 gatt_le_event_cb = cb;
1006 void _bt_hal_unregister_gatt_le_dbus_handler_cb(void)
1008 gatt_le_event_cb = NULL;
1011 int _bt_hal_adapter_le_set_manufacturer_data(bt_manufacturer_data_t *m_data)
1014 GError *error = NULL;
1018 GVariantBuilder *builder;
1020 proxy = _bt_hal_get_adapter_proxy();
1022 return BT_STATUS_FAIL;
1024 if (m_data->data_len + 2 >= BT_MANUFACTURER_DATA_LENGTH_MAX)
1025 return BT_STATUS_PARM_INVALID;
1027 builder = g_variant_builder_new(G_VARIANT_TYPE("ay"));
1029 for (i = 0; i < (m_data->data_len) + 2; i++)
1030 g_variant_builder_add(builder, "y", m_data->data[i]);
1032 val = g_variant_new("(ay)", builder);
1034 ret = g_dbus_proxy_call_sync(proxy,
1035 "SetManufacturerData",
1037 G_DBUS_CALL_FLAGS_NONE,
1041 g_variant_builder_unref(builder);
1043 if (error != NULL) {
1044 ERR("Failed to SetManufacturerData (Error: %s)", error->message);
1045 g_clear_error(&error);
1047 ERR("Failed to SetManufacturerData");
1049 return BT_STATUS_FAIL;
1052 INFO("Set manufacturer data");
1053 g_variant_unref(ret);
1055 return BT_STATUS_SUCCESS;
1060 /*add/remove remote device address from white list*/
1061 int _bt_hal_adapter_le_set_white_list(bt_bdaddr_t *device_address, bt_dev_addr_type_t address_type, bool is_add)
1064 char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
1065 GError *error = NULL;
1068 if (address_type != BLUETOOTH_HAL_DEVICE_PUBLIC_ADDRESS &&
1069 address_type != BLUETOOTH_HAL_DEVICE_RANDOM_ADDRESS)
1070 return BT_STATUS_PARM_INVALID;
1072 _bt_hal_convert_addr_type_to_string(address, device_address->address);
1074 proxy = _bt_hal_get_adapter_proxy();
1076 return BT_STATUS_FAIL;
1078 ret = g_dbus_proxy_call_sync(proxy, "AddDeviceWhiteList",
1079 g_variant_new("(su)", address, address_type),
1080 G_DBUS_CALL_FLAGS_NONE, -1,
1083 ret = g_dbus_proxy_call_sync(proxy, "RemoveDeviceWhiteList",
1084 g_variant_new("(su)", address, address_type),
1085 G_DBUS_CALL_FLAGS_NONE, -1,
1090 ERR("RemoveDeviceWhiteList Fail: %s", error->message);
1092 ERR("AddDeviceWhiteList Fail: %s", error->message);
1093 g_clear_error(&error);
1094 return BT_STATUS_FAIL;
1098 g_variant_unref(ret);
1100 INFO("Device Added to white list");
1102 INFO("Device Removed from white list");
1103 return BT_STATUS_SUCCESS;