2 * Open Adaptation Layer (OAL)
4 * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
27 #include <bluetooth.h>
29 #include "oal-event.h"
30 #include "oal-internal.h"
31 #include "oal-manager.h"
32 #include "oal-hardware.h"
33 #include "oal-common.h"
34 #include "oal-utils.h"
37 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
39 static const bt_interface_t * blued_api;
41 static bt_address_t local_address;
42 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
43 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
44 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
45 static int discoverable_timeout = 0;
47 /* Forward declarations */
48 oal_status_t convert_to_oal_status(bt_status_t status);
49 static gboolean retry_enable_adapter(gpointer data);
51 static gboolean retry_enable_le(gpointer data);
53 oal_status_t oal_mgr_init_internal(void);
56 /* Callback registered with Stack */
57 static void cb_adapter_state_change(bt_state_t status);
58 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
59 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
60 static void cb_adapter_properties(bt_status_t status,
61 int num_properties, bt_property_t *properties);
62 static void cb_adapter_profile_connected_devices(uint8_t count, uint8_t bdaddr_list[][6]);
63 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
64 int num_properties, bt_property_t *properties);
65 extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
66 bt_bond_state_t state);
67 extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
68 bt_acl_state_t state);
69 extern void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class);
70 extern void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
71 bt_ssp_variant_t pairing_variant, uint32_t pass_key);
72 extern void cb_device_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_service_id_t service_d);
73 extern void cb_device_trust_state_changed(bt_bdaddr_t *remote_bd_addr, bt_device_trust_state_t trusted);
75 extern void cb_socket_conn_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_uuid_t *uuid, uint8_t *name, uint8_t *path, uint32_t fd);
76 static void cb_ble_state_change(bt_state_t status);
77 extern void cb_device_le_conn_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
78 bt_le_conn_state_t state);
79 extern void cb_device_trusted_profiles_changed(bt_bdaddr_t *bd_addr, uint32_t trust_val);
80 extern void cb_rssi_monitor_state_changed(bt_bdaddr_t *bd_addr, int32_t link_type, uint8_t state);
81 extern void cb_rssi_alert(bt_bdaddr_t *bd_addr, int32_t link_type, int32_t alert_type, int32_t rssi);
82 extern void cb_raw_rssi_received(bt_bdaddr_t *bd_addr, int32_t link_type, int32_t rssi);
83 extern void cb_dbfw_plus_info_received(unsigned char *data, uint32_t length, uint8_t event_code);
84 extern void cb_controller_error_received(uint8_t error_code);
87 static bt_callbacks_t callbacks = {
88 .size = sizeof(callbacks),
89 .adapter_state_changed_cb = cb_adapter_state_change,
90 .adapter_properties_cb = cb_adapter_properties,
91 .adapter_profile_connected_devices_cb = cb_adapter_profile_connected_devices,
92 .remote_device_properties_cb = cb_device_properties,
93 .device_found_cb = cb_adapter_device_found,
94 .discovery_state_changed_cb = cb_adapter_discovery_state_changed,
95 .pin_request_cb = cb_device_pin_request,
96 .ssp_request_cb = cb_device_ssp_request,
97 .bond_state_changed_cb = cb_device_bond_state_changed,
98 .acl_state_changed_cb = cb_device_acl_state_changed,
99 .thread_evt_cb = NULL,
100 .dut_mode_recv_cb = NULL,
101 .le_test_mode_cb = NULL,
102 .energy_info_cb = NULL,
103 .authorize_request_cb = cb_device_authorize_request,
104 .device_trust_state_changed_cb = cb_device_trust_state_changed,
106 .socket_authorize_request_cb = cb_socket_conn_authorize_request,
107 .le_state_changed_cb = cb_ble_state_change,
108 .le_conn_state_changed_cb = cb_device_le_conn_state_changed,
109 .device_trusted_profiles_changed_cb = cb_device_trusted_profiles_changed,
110 .rssi_monitor_state_changed_cb = cb_rssi_monitor_state_changed,
111 .rssi_alert_cb = cb_rssi_alert,
112 .raw_rssi_received_cb = cb_raw_rssi_received,
113 .dbfw_plus_info_received_cb = cb_dbfw_plus_info_received,
114 .controller_error_received_cb = cb_controller_error_received,
118 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
121 blued_api = stack_if;
123 ret = blued_api->init(&callbacks);
125 if (ret != BT_STATUS_SUCCESS) {
126 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
127 blued_api->cleanup();
128 return convert_to_oal_status(ret);
131 return OAL_STATUS_SUCCESS;
134 const bt_interface_t* adapter_get_stack_interface(void)
139 void adapter_mgr_cleanup(void)
141 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
146 int oal_set_adapter_request_state(int enable)
148 CHECK_OAL_INITIALIZED();
150 return blued_api->set_hal_adapter_request_state(enable);
153 int oal_set_le_request_state(int enable)
155 CHECK_OAL_INITIALIZED();
157 return blued_api->set_hal_le_request_state(enable);
161 oal_status_t adapter_enable(void)
163 int ret = BT_STATUS_SUCCESS;
167 if (blued_api == NULL) {
168 BT_INFO("Stack is initializing, so pending enable");
169 g_timeout_add(200, retry_enable_adapter, NULL);
170 return OAL_STATUS_PENDING;
173 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
174 g_timeout_add(200, retry_enable_adapter, NULL);
175 return OAL_STATUS_PENDING;
178 ret = blued_api->enable();
180 if (ret != BT_STATUS_SUCCESS) {
181 BT_ERR("Enable failed: [%s]", status2string(ret));
182 return convert_to_oal_status(ret);
185 return OAL_STATUS_SUCCESS;
188 oal_status_t adapter_disable(void)
194 CHECK_OAL_INITIALIZED();
196 ret = blued_api->disable();
198 if (ret != BT_STATUS_SUCCESS) {
199 BT_ERR("Disable failed: [%s]", status2string(ret));
200 return convert_to_oal_status(ret);
202 return OAL_STATUS_SUCCESS;
205 oal_status_t le_enable(void)
207 int ret = BT_STATUS_SUCCESS;
210 CHECK_OAL_INITIALIZED();
213 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
214 g_timeout_add(200, retry_enable_le, NULL);
215 return OAL_STATUS_PENDING;
218 ret = blued_api->le_enable();
220 if (ret != BT_STATUS_SUCCESS) {
221 BT_ERR("Enable failed: [%s]", status2string(ret));
222 return convert_to_oal_status(ret);
225 BT_INFO("Not Supported");
226 ret = OAL_STATUS_NOT_SUPPORT;
232 oal_status_t le_disable(void)
238 CHECK_OAL_INITIALIZED();
241 ret = blued_api->le_disable();
243 if (ret != BT_STATUS_SUCCESS) {
244 BT_ERR("Disable failed: [%s]", status2string(ret));
245 return convert_to_oal_status(ret);
248 BT_INFO("Not Supported");
249 ret = OAL_STATUS_NOT_SUPPORT;
254 oal_status_t le_init(void)
256 int ret = BT_STATUS_SUCCESS;
258 CHECK_OAL_INITIALIZED();
260 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
261 g_timeout_add(200, retry_enable_le, NULL);
262 return OAL_STATUS_PENDING;
264 ret = blued_api->le_init();
265 if (ret != BT_STATUS_SUCCESS) {
266 BT_ERR("Enable failed: [%s]", status2string(ret));
267 return convert_to_oal_status(ret);
270 BT_INFO("Not Supported");
271 ret = OAL_STATUS_NOT_SUPPORT;
275 oal_status_t le_deinit(void)
277 int ret = BT_STATUS_SUCCESS;
279 CHECK_OAL_INITIALIZED();
281 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
282 g_timeout_add(200, retry_enable_le, NULL);
283 return OAL_STATUS_PENDING;
285 blued_api->le_deinit();
287 BT_INFO("Not Supported");
288 ret = OAL_STATUS_NOT_SUPPORT;
292 oal_status_t is_advertising(void)
294 int ret = BT_STATUS_SUCCESS;
296 CHECK_OAL_INITIALIZED();
298 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
299 g_timeout_add(200, retry_enable_le, NULL);
300 return OAL_STATUS_PENDING;
302 int r = blued_api->is_advertising();
304 ret = BT_STATUS_SUCCESS;
306 ret = BT_STATUS_FAIL;
308 BT_INFO("Not Supported");
309 ret = OAL_STATUS_NOT_SUPPORT;
313 oal_status_t adapter_start_custom_inquiry(discovery_type_t disc_type)
319 CHECK_OAL_INITIALIZED();
320 BT_INFO("Custom Discovery Type [0x%x]", disc_type);
323 ret = blued_api->start_custom_discovery(disc_type);
324 if (ret != BT_STATUS_SUCCESS) {
325 BT_ERR("start_custom_discovery failed: [%s]", status2string(ret));
326 return convert_to_oal_status(ret);
329 BT_INFO("Not Supported");
330 ret = OAL_STATUS_NOT_SUPPORT;
335 oal_status_t adapter_get_powered_status(gboolean *status)
338 unsigned char powered = 0;
342 CHECK_OAL_INITIALIZED();
344 OAL_CHECK_PARAMETER(status, return);
345 BT_INFO("Get Adapter Powered status");
348 ret = blued_api->get_adapter_powered_status(&powered);
349 if (ret != BT_STATUS_SUCCESS) {
350 BT_ERR("adapter_get_powered_status failed: [%s]", status2string(ret));
352 return convert_to_oal_status(ret);
359 BT_INFO("Not Supported");
360 ret = OAL_STATUS_NOT_SUPPORT;
365 oal_status_t adapter_get_energy_info(uint32_t *tx_time, uint32_t *rx_time,
366 uint32_t *idle_time, uint32_t *energy_used)
370 CHECK_OAL_INITIALIZED();
372 OAL_CHECK_PARAMETER(tx_time, return);
373 OAL_CHECK_PARAMETER(rx_time, return);
374 OAL_CHECK_PARAMETER(idle_time, return);
375 OAL_CHECK_PARAMETER(energy_used, return);
377 BT_DBG("Get Adapter Energy Info");
380 ret = blued_api->get_adapter_energy_info(tx_time, rx_time, idle_time, energy_used);
381 if (ret != BT_STATUS_SUCCESS) {
382 BT_ERR("get_adapter_energy_info failed: [%s]", status2string(ret));
383 return convert_to_oal_status(ret);
386 BT_INFO("Not Supported");
387 ret = OAL_STATUS_NOT_SUPPORT;
392 oal_status_t adapter_reset(void)
398 CHECK_OAL_INITIALIZED();
399 BT_INFO("Adapter Reset");
402 ret = blued_api->reset();
403 if (ret != BT_STATUS_SUCCESS) {
404 BT_ERR("Adapter Reset failed: [%s]", status2string(ret));
405 return convert_to_oal_status(ret);
408 BT_INFO("Not Supported");
409 ret = OAL_STATUS_NOT_SUPPORT;
414 oal_status_t adapter_recover(void)
420 CHECK_OAL_INITIALIZED();
421 BT_INFO("Adapter Recover");
423 result = blued_api->recover();
424 if (result != BT_STATUS_SUCCESS) {
425 BT_ERR("Adapter Recover Failed: [%s]", status2string(result));
426 return convert_to_oal_status(result);
432 oal_status_t adapter_start_inquiry(unsigned short duration)
438 CHECK_OAL_INITIALIZED();
440 ret = blued_api->start_discovery();
441 if (ret != BT_STATUS_SUCCESS) {
442 BT_ERR("start_discovery failed: [%s]", status2string(ret));
443 return convert_to_oal_status(ret);
446 return OAL_STATUS_SUCCESS;
449 oal_status_t adapter_get_local_irk(uint8_t *local_irk)
451 int ret = OAL_STATUS_SUCCESS;
452 CHECK_OAL_INITIALIZED();
454 ret = blued_api->get_local_irk(local_irk);
455 if (ret != BT_STATUS_SUCCESS) {
456 BT_ERR("get property local irk failed, status: %s", status2string(ret));
457 return convert_to_oal_status(ret);
460 return OAL_STATUS_SUCCESS;
463 oal_status_t adapter_stop_inquiry(void)
469 CHECK_OAL_INITIALIZED();
471 ret = blued_api->cancel_discovery();
472 if (ret != BT_STATUS_SUCCESS) {
473 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
474 return convert_to_oal_status(ret);
477 return OAL_STATUS_SUCCESS;
480 /* Callbacks from Stack */
481 static void cb_adapter_state_change(bt_state_t status)
486 event = (BT_STATE_ON == status) ? OAL_EVENT_ADAPTER_ENABLED : OAL_EVENT_ADAPTER_DISABLED;
488 send_event(event, NULL, 0);
492 /* Callbacks from Stack */
493 static void cb_ble_state_change(bt_state_t status)
498 event = (BT_STATE_ON == status) ? OAL_EVENT_BLE_ENABLED : OAL_EVENT_BLE_DISABLED;
500 send_event(event, NULL, 0);
504 static gboolean retry_enable_adapter(gpointer data)
511 static gboolean retry_enable_le(gpointer data)
517 oal_status_t adapter_get_properties(void)
522 CHECK_OAL_INITIALIZED();
524 ret = blued_api->get_adapter_properties();
525 if (ret != BT_STATUS_SUCCESS) {
526 BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
527 return convert_to_oal_status(ret);
530 return OAL_STATUS_SUCCESS;
533 oal_status_t adapter_get_address(void)
538 CHECK_OAL_INITIALIZED();
540 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
541 if (ret != BT_STATUS_SUCCESS) {
542 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
543 return convert_to_oal_status(ret);
546 return OAL_STATUS_SUCCESS;
549 oal_status_t adapter_get_version(void)
554 CHECK_OAL_INITIALIZED();
556 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
557 if (ret != BT_STATUS_SUCCESS) {
558 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
559 return convert_to_oal_status(ret);
562 return OAL_STATUS_SUCCESS;
565 oal_status_t adapter_get_name(void)
569 CHECK_OAL_INITIALIZED();
573 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
574 if (ret != BT_STATUS_SUCCESS) {
575 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
576 return convert_to_oal_status(ret);
579 return OAL_STATUS_SUCCESS;
582 oal_status_t adapter_get_connectable(void)
586 CHECK_OAL_INITIALIZED();
590 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_SCAN_MODE);
591 if (ret != BT_STATUS_SUCCESS) {
592 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
593 return convert_to_oal_status(ret);
596 return OAL_STATUS_SUCCESS;
599 oal_status_t adapter_set_name(char * name)
604 CHECK_OAL_INITIALIZED();
606 OAL_CHECK_PARAMETER(name, return);
607 API_TRACE("Name: %s", name);
609 prop.type = BT_PROPERTY_BDNAME;
610 prop.len = strlen(name);
613 ret = blued_api->set_adapter_property(&prop);
614 if (ret != BT_STATUS_SUCCESS) {
615 BT_ERR("set_adapter_property: [%s]", status2string(ret));
616 ret = OAL_STATUS_INTERNAL_ERROR;
618 ret = OAL_STATUS_SUCCESS;
623 oal_status_t adapter_is_discoverable(int *p_discoverable)
625 OAL_CHECK_PARAMETER(p_discoverable, return);
627 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
629 API_TRACE("%d", *p_discoverable);
631 return OAL_STATUS_SUCCESS;
634 oal_status_t adapter_is_connectable(int *p_connectable)
636 OAL_CHECK_PARAMETER(p_connectable, return);
638 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
639 || (scan_mode == BT_SCAN_MODE_CONNECTABLE);
641 API_TRACE("%d", *p_connectable);
643 return OAL_STATUS_SUCCESS;
646 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
648 API_TRACE("%d", discoverable_timeout);
650 *p_timeout = discoverable_timeout;
652 return OAL_STATUS_SUCCESS;
655 oal_status_t adapter_get_service_uuids(void)
659 CHECK_OAL_INITIALIZED();
662 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
663 if (ret != BT_STATUS_SUCCESS) {
664 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
665 return convert_to_oal_status(ret);
667 return OAL_STATUS_SUCCESS;
670 oal_status_t adapter_get_bonded_devices(void)
674 CHECK_OAL_INITIALIZED();
678 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
679 if (ret != BT_STATUS_SUCCESS) {
680 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
681 return convert_to_oal_status(ret);
684 return OAL_STATUS_SUCCESS;
687 oal_status_t adapter_get_profile_connected_devices(const char *profile_uuid)
691 CHECK_OAL_INITIALIZED();
695 ret = blued_api->get_profile_connected_devices(profile_uuid);
696 if (ret != BT_STATUS_SUCCESS) {
697 BT_ERR("get_profile_connected_devices failed: [%s]", status2string(ret));
698 return convert_to_oal_status(ret);
701 return OAL_STATUS_SUCCESS;
704 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
711 CHECK_OAL_INITIALIZED();
713 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
714 prop.len = sizeof(bt_scan_mode_t);
716 res = blued_api->set_adapter_property(&prop);
717 if (res != BT_STATUS_SUCCESS) {
718 BT_ERR("set scan mode failed [%s]", status2string(res));
719 return convert_to_oal_status(res);
723 return OAL_STATUS_SUCCESS;
726 oal_status_t adapter_set_connectable(int connectable)
730 API_TRACE("%d", connectable);
732 CHECK_OAL_INITIALIZED();
734 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
736 return set_scan_mode(mode);
739 oal_status_t adapter_set_discoverable(void)
741 CHECK_OAL_INITIALIZED();
744 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
747 oal_status_t adapter_set_discoverable_timeout(int timeout)
751 uint32_t prop_val = timeout;
753 CHECK_OAL_INITIALIZED();
754 API_TRACE("%d", timeout);
756 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
757 prop.len = sizeof(prop_val);
758 prop.val = &prop_val;
759 res = blued_api->set_adapter_property(&prop);
760 if (res != BT_STATUS_SUCCESS) {
761 BT_ERR("set_adapter_property failed [%s]", status2string(res));
762 return convert_to_oal_status(res);
764 return OAL_STATUS_SUCCESS;
767 oal_status_t adapter_ble_set_filter_policy(int filter_policy)
769 int ret = OAL_STATUS_SUCCESS;
771 CHECK_OAL_INITIALIZED();
774 BT_DBG("Filter policy applied is [%d]", filter_policy);
776 ret = gatts_set_filter_policy(filter_policy);
778 if (ret != OAL_STATUS_SUCCESS) {
779 BT_ERR("gatts_set_filter_policy: [%d]", ret);
783 return OAL_STATUS_SUCCESS;
786 oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
787 int adv_type, int chnl_map, int tx_power, int timeout_s,
788 bool is_legacy, int primary_phy, int secondary_phy)
791 CHECK_OAL_INITIALIZED();
794 res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
795 adv_type, chnl_map, tx_power, timeout_s,
796 is_legacy, primary_phy, secondary_phy);
797 if (res != OAL_STATUS_SUCCESS) {
798 BT_ERR("gatts_multi_adv_update: [%d]", res);
801 return OAL_STATUS_SUCCESS;
804 oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
805 oal_ble_multi_adv_param_setup_t * adv_param_setup)
808 CHECK_OAL_INITIALIZED();
809 OAL_CHECK_PARAMETER(adv_param_setup, return);
813 res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
814 if (res != OAL_STATUS_SUCCESS) {
815 BT_ERR("failed: [%d]", res);
818 return OAL_STATUS_SUCCESS;
821 oal_status_t adapter_ble_multi_adv_enable(int instance_id)
824 CHECK_OAL_INITIALIZED();
827 res = gatts_multi_adv_enable(instance_id);
828 if (res != OAL_STATUS_SUCCESS) {
829 BT_ERR("failed: [%d]", res);
833 return OAL_STATUS_SUCCESS;
836 oal_status_t adapter_ble_multi_adv_disable(int instance_id)
839 CHECK_OAL_INITIALIZED();
842 res = gatts_multi_adv_disable(instance_id);
843 if (res != OAL_STATUS_SUCCESS) {
844 BT_ERR("failed: [%d]", res);
848 return OAL_STATUS_SUCCESS;
851 oal_status_t adapter_set_le_static_random_address(int enable)
855 CHECK_OAL_INITIALIZED();
856 API_TRACE("%d", enable);
859 ret = blued_api->set_le_static_random_address((enable ? 1 : 0));
860 if (ret != BT_STATUS_SUCCESS)
861 BT_ERR("Static address set failed: [%s]", status2string(ret));
862 ret = convert_to_oal_status(ret);
864 BT_INFO("Not Supported");
865 ret = OAL_STATUS_NOT_SUPPORT;
871 oal_status_t adapter_set_manufacturer_data(oal_manufacturer_data_t *m_data)
875 CHECK_OAL_INITIALIZED();
878 OAL_CHECK_PARAMETER(m_data, return);
879 ret = blued_api->adapter_le_set_manufacturer_data((bt_manufacturer_data_t*)m_data);
880 if (ret != BT_STATUS_SUCCESS)
881 BT_ERR("Setting manufacturer data Failed: [%s]",status2string(ret));
883 ret = convert_to_oal_status(ret);
887 oal_status_t adapter_set_allow_list(bt_address_t *device_address, int address_type, bool is_add)
892 CHECK_OAL_INITIALIZED();
894 BT_INFO("BT remote device Address: %s", bdt_bd2str(device_address, &bdstr) + 12);
896 ret = blued_api->adapter_le_set_allow_list((bt_bdaddr_t*)device_address, address_type, is_add);
897 if (ret != BT_STATUS_SUCCESS) {
899 BT_ERR("Add to allow List Failed: [%s]",status2string(ret));
901 BT_ERR("Remove from allow List Failed: [%s]",status2string(ret));
903 ret = convert_to_oal_status(ret);
907 oal_status_t adapter_ble_set_privacy(int set_privacy)
911 CHECK_OAL_INITIALIZED();
914 res = blued_api->adapter_le_set_privacy(set_privacy);
915 if (res != BT_STATUS_SUCCESS)
916 BT_ERR("Setting LE Privacy Failed: [%s]", status2string(res));
917 res = convert_to_oal_status(res);
922 static void cb_adapter_properties(bt_status_t status,
924 bt_property_t *properties)
928 BT_DBG("status: %d, count: %d", status, num_properties);
930 print_bt_properties(num_properties, properties);
932 if (status != BT_STATUS_SUCCESS) {
933 if (num_properties == 1) {
934 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
935 status2string(status), num_properties, properties[num_properties-1].type);
937 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
942 for (i = 0; i < num_properties; i++) {
943 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
944 switch (properties[i].type) {
945 case BT_PROPERTY_VERSION: {
946 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
947 local_version[properties[i].len] = '\0';
949 BT_DBG("Version: %s", local_version);
950 /* Send event to application */
951 if (num_properties == 1) {
952 char *adapter_ver = g_strdup(local_version);
954 /* Application has requested this property SET/GET hence send EVENT */
955 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
959 case BT_PROPERTY_BDNAME: {
960 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
961 local_name[properties[i].len] = '\0';
963 BT_DBG("Name: %s", local_name);
964 /* Send event to application */
965 if (num_properties == 1) {
966 char * adap_name = g_strdup(local_name);
968 /* Application has requested this property SET/GET hence send EVENT */
969 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name)+1);
973 case BT_PROPERTY_BDADDR: {
976 addr = properties[i].val;
977 memcpy(local_address.addr, addr->address, 6);
978 if (num_properties == 1) {
979 /* Application has requested this property SET/GET hence send EVENT */
980 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
981 g_memdup2(&local_address, sizeof(local_address)),
982 sizeof(local_address));
986 case BT_PROPERTY_UUIDS: {
989 num_uuid = properties[i].len/sizeof(bt_uuid_t);
991 BT_DBG("num_uuid: %d", num_uuid);
993 /* Send event to application */
994 if (num_properties == 1) {
995 event_adapter_services_t *uuids_event;
997 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
998 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
999 uuids_event->num = num_uuid;
1001 /* Application has requested this property SET/GET hence send EVENT */
1002 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
1003 uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
1007 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
1008 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
1010 BT_INFO("Scan mode (%d)", cur_mode);
1012 scan_mode = cur_mode;
1014 /* Send event to application */
1015 if (num_properties == 1) {
1016 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
1018 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
1019 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
1020 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
1021 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
1023 /* Application has requested this property SET/GET hence send EVENT */
1024 send_event(event, NULL, 0);
1028 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
1031 timeout = *((uint32_t*)properties[i].val);
1033 BT_INFO("Discoverability timeout: %d", timeout);
1034 discoverable_timeout = timeout;
1036 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
1037 g_memdup2(properties[i].val, sizeof(uint32_t)),
1041 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
1044 bt_bdaddr_t *bonded_addr_list;
1045 event_device_list_t *event_data;
1047 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
1048 BT_DBG("num_bonded %d", num_bonded);
1050 if (num_properties > 1) /* No explicit req for this prop, ignore */
1053 bonded_addr_list = properties[i].val;
1054 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
1055 event_data->num = num_bonded;
1057 for (j = 0; j < num_bonded; j++)
1058 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
1060 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
1061 event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
1064 case BT_PROPERTY_A2DP_ROLE: {
1065 unsigned int a2dp_role;
1067 a2dp_role = *((uint32_t*)properties[i].val);
1069 BT_INFO("A2DP role: %u", a2dp_role);
1071 send_event(OAL_EVENT_ADAPTER_PROPERTY_A2DP_ROLE,
1072 g_memdup2(properties[i].val, sizeof(uint32_t)),
1076 case BT_PROPERTY_LOCAL_LE_FEATURES: {
1077 event_adapter_le_features_t *le_features;
1079 le_features = g_malloc(sizeof(event_adapter_le_features_t));
1081 le_features->max_adv_instance = ((bt_local_le_features_t *)(properties[i].val))->max_adv_instance;
1082 le_features->rpa_offloading = ((bt_local_le_features_t *)(properties[i].val))->rpa_offload_supported;
1083 le_features->max_adv_filter = ((bt_local_le_features_t *)(properties[i].val))->max_adv_filter_supported;
1084 le_features->le_2m_phy_support = ((bt_local_le_features_t *)(properties[i].val))->le_2m_phy_supported;
1085 le_features->le_coded_phy_support = ((bt_local_le_features_t *)(properties[i].val))->le_2m_phy_supported;
1086 le_features->le_extended_advertising_support =
1087 ((bt_local_le_features_t *)(properties[i].val))->le_extended_advertising_supported;
1088 le_features->le_extended_scan_support =
1089 ((bt_local_le_features_t *)(properties[i].val))->extended_scan_support;
1090 le_features->le_maximum_advertising_data_length =
1091 ((bt_local_le_features_t *)(properties[i].val))->le_maximum_advertising_data_length;
1093 BT_INFO("LE 2M PHY Support (%d)", le_features->le_2m_phy_support);
1094 BT_INFO("LE CODED PHY Support (%d)", le_features->le_coded_phy_support);
1096 send_event(OAL_EVENT_BLE_LOCAL_FEATURES,
1098 sizeof(event_adapter_le_features_t));
1101 case BT_PROPERTY_ADAPTER_LE_DISCOVERY_STARTED: {
1102 BT_INFO("LE Discovery started");
1103 send_event(OAL_EVENT_BLE_DISCOVERY_STARTED, NULL, 0);
1106 case BT_PROPERTY_ADAPTER_LE_DISCOVERY_STOPPED: {
1107 BT_INFO("LE Discovery stopped");
1108 send_event(OAL_EVENT_BLE_DISCOVERY_STOPPED, NULL, 0);
1112 BT_WARN("Unhandled property: %d", properties[i].type);
1118 static void cb_adapter_profile_connected_devices(uint8_t count, uint8_t addr_list[][6])
1120 event_adapter_profile_connected_devices *event_data;
1123 event_data = g_malloc0(sizeof(event_adapter_profile_connected_devices));
1124 event_data->count = count;
1125 for (i = 0; i < count; i++) {
1126 memcpy(event_data->addr_list[i].addr, addr_list[i], BT_ADDRESS_BYTES_NUM);
1128 send_event(OAL_EVENT_ADAPTER_PROFILE_CONNECTED_DEVICES, event_data, sizeof(event_adapter_profile_connected_devices));
1131 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
1135 event = (BT_DISCOVERY_STARTED == state) ? OAL_EVENT_ADAPTER_INQUIRY_STARTED : OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
1137 BT_DBG("%d", state);
1138 send_event(event, NULL, 0);
1141 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
1143 remote_device_t dev_info;
1144 ble_adv_data_t adv_info;
1146 gpointer event_data;
1150 if (num_properties == 0) {
1151 BT_ERR("Unexpected, properties count is zero!!");
1155 memset(&dev_info, 0x00, sizeof(remote_device_t));
1156 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
1158 print_bt_properties(num_properties, properties);
1159 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
1161 BT_INFO("number of properties= [%d] ", num_properties);
1163 if (dev_info.type != DEV_TYPE_BREDR) {
1164 /* BLE Single or DUAL mode found, so it should have Adv data */
1165 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
1167 ble_dev_event->adv_len = adv_info.len;
1169 if (adv_info.len > 0 && adv_info.adv_data) {
1170 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
1171 ble_dev_event->adv_len = adv_info.len;
1173 ble_dev_event->adv_len = 0;
1175 ble_dev_event->device_info = dev_info;
1177 event_data = ble_dev_event;
1178 size = sizeof(event_ble_dev_found_t);
1179 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
1181 /* BREDR device, so No Adv data */
1182 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
1184 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
1185 event_data = dev_event;
1186 size = sizeof(remote_device_t);
1187 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
1190 send_event(event, event_data, size);
1195 void cb_controller_error_received(uint8_t error_code)
1197 uint8_t *event_data;
1199 event_data = g_new0(uint8_t, 1);
1200 *event_data = error_code;
1202 send_event(OAL_EVENT_CONTROLLER_ERROR_RECEIVED,
1203 (gpointer)event_data, sizeof(uint8_t));