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 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
63 int num_properties, bt_property_t *properties);
64 extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
65 bt_bond_state_t state);
66 extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
67 bt_acl_state_t state);
68 extern void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class);
69 extern void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
70 bt_ssp_variant_t pairing_variant, uint32_t pass_key);
71 extern void cb_device_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_service_id_t service_d);
72 extern void cb_device_trust_state_changed(bt_bdaddr_t *remote_bd_addr, bt_device_trust_state_t trusted);
74 extern void cb_socket_conn_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_uuid_t *uuid);
75 static void cb_ble_state_change(bt_state_t status);
78 static bt_callbacks_t callbacks = {
80 cb_adapter_state_change,
81 cb_adapter_properties,
83 cb_adapter_device_found,
84 cb_adapter_discovery_state_changed,
85 cb_device_pin_request,
86 cb_device_ssp_request,
87 cb_device_bond_state_changed,
88 cb_device_acl_state_changed,
89 NULL, /* callback_thread_event */
90 NULL, /* dut_mode_recv_callback */
91 NULL, /* le_test_mode_callback*/
92 NULL, /* energy_info_callback */
93 cb_device_authorize_request,
94 cb_device_trust_state_changed,
96 cb_socket_conn_authorize_request,
101 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
104 blued_api = stack_if;
106 ret = blued_api->init(&callbacks);
108 if (ret != BT_STATUS_SUCCESS) {
109 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
110 blued_api->cleanup();
111 return convert_to_oal_status(ret);
114 return OAL_STATUS_SUCCESS;
117 const bt_interface_t* adapter_get_stack_interface(void)
122 void adapter_mgr_cleanup(void)
124 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
128 oal_status_t adapter_enable(void)
130 int ret = BT_STATUS_SUCCESS;
133 CHECK_OAL_INITIALIZED();
134 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
135 g_timeout_add(200, retry_enable_adapter, NULL);
136 return OAL_STATUS_PENDING;
139 ret = blued_api->enable();
141 if (ret != BT_STATUS_SUCCESS) {
142 BT_ERR("Enable failed: [%s]", status2string(ret));
143 return convert_to_oal_status(ret);
146 return OAL_STATUS_SUCCESS;
149 oal_status_t adapter_disable(void)
155 CHECK_OAL_INITIALIZED();
157 ret = blued_api->disable();
159 if (ret != BT_STATUS_SUCCESS) {
160 BT_ERR("Disable failed: [%s]", status2string(ret));
161 return convert_to_oal_status(ret);
163 return OAL_STATUS_SUCCESS;
166 oal_status_t le_enable(void)
168 int ret = BT_STATUS_SUCCESS;
171 CHECK_OAL_INITIALIZED();
174 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
175 g_timeout_add(200, retry_enable_le, NULL);
176 return OAL_STATUS_PENDING;
179 ret = blued_api->le_enable();
181 if (ret != BT_STATUS_SUCCESS) {
182 BT_ERR("Enable failed: [%s]", status2string(ret));
183 return convert_to_oal_status(ret);
186 BT_INFO("Not Supported");
187 ret = OAL_STATUS_NOT_SUPPORT;
193 oal_status_t le_disable(void)
199 CHECK_OAL_INITIALIZED();
202 ret = blued_api->le_disable();
204 if (ret != BT_STATUS_SUCCESS) {
205 BT_ERR("Disable failed: [%s]", status2string(ret));
206 return convert_to_oal_status(ret);
209 BT_INFO("Not Supported");
210 ret = OAL_STATUS_NOT_SUPPORT;
215 oal_status_t adapter_start_custom_inquiry(discovery_type_t disc_type)
221 CHECK_OAL_INITIALIZED();
222 BT_INFO("Custom Discovery Type [0x%x]", disc_type);
225 ret = blued_api->start_custom_discovery(disc_type);
226 if (ret != BT_STATUS_SUCCESS) {
227 BT_ERR("start_custom_discovery failed: [%s]", status2string(ret));
228 return convert_to_oal_status(ret);
231 BT_INFO("Not Supported");
232 ret = OAL_STATUS_NOT_SUPPORT;
237 oal_status_t adapter_start_inquiry(unsigned short duration)
243 CHECK_OAL_INITIALIZED();
245 ret = blued_api->start_discovery();
246 if (ret != BT_STATUS_SUCCESS) {
247 BT_ERR("start_discovery failed: [%s]", status2string(ret));
248 return convert_to_oal_status(ret);
251 return OAL_STATUS_SUCCESS;
254 oal_status_t adapter_stop_inquiry(void)
260 CHECK_OAL_INITIALIZED();
262 ret = blued_api->cancel_discovery();
263 if (ret != BT_STATUS_SUCCESS) {
264 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
265 return convert_to_oal_status(ret);
268 return OAL_STATUS_SUCCESS;
271 /* Callbacks from Stack */
272 static void cb_adapter_state_change(bt_state_t status)
277 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
279 send_event(event, NULL, 0);
283 /* Callbacks from Stack */
284 static void cb_ble_state_change(bt_state_t status)
289 event = (BT_STATE_ON == status)?OAL_EVENT_BLE_ENABLED:OAL_EVENT_BLE_DISABLED;
291 send_event(event, NULL, 0);
295 static gboolean retry_enable_adapter(gpointer data)
302 static gboolean retry_enable_le(gpointer data)
308 oal_status_t adapter_get_properties(void)
313 CHECK_OAL_INITIALIZED();
315 ret = blued_api->get_adapter_properties();
316 if (ret != BT_STATUS_SUCCESS) {
317 BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
318 return convert_to_oal_status(ret);
321 return OAL_STATUS_SUCCESS;
324 oal_status_t adapter_get_address(void)
329 CHECK_OAL_INITIALIZED();
331 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
332 if (ret != BT_STATUS_SUCCESS) {
333 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
334 return convert_to_oal_status(ret);
337 return OAL_STATUS_SUCCESS;
340 oal_status_t adapter_get_version(void)
345 CHECK_OAL_INITIALIZED();
347 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
348 if (ret != BT_STATUS_SUCCESS) {
349 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
350 return convert_to_oal_status(ret);
353 return OAL_STATUS_SUCCESS;
356 oal_status_t adapter_get_name(void)
360 CHECK_OAL_INITIALIZED();
364 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
365 if (ret != BT_STATUS_SUCCESS) {
366 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
367 return convert_to_oal_status(ret);
370 return OAL_STATUS_SUCCESS;
373 oal_status_t adapter_set_name(char * name)
378 CHECK_OAL_INITIALIZED();
380 OAL_CHECK_PARAMETER(name, return);
381 API_TRACE("Name: %s", name);
383 prop.type = BT_PROPERTY_BDNAME;
384 prop.len = strlen(name);
387 ret = blued_api->set_adapter_property(&prop);
388 if (ret != BT_STATUS_SUCCESS) {
389 BT_ERR("set_adapter_property: [%s]", status2string(ret));
390 ret = OAL_STATUS_INTERNAL_ERROR;
392 ret = OAL_STATUS_SUCCESS;
397 oal_status_t adapter_is_discoverable(int *p_discoverable)
399 OAL_CHECK_PARAMETER(p_discoverable, return);
401 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
403 API_TRACE("%d", *p_discoverable);
405 return OAL_STATUS_SUCCESS;
408 oal_status_t adapter_is_connectable(int *p_connectable)
410 OAL_CHECK_PARAMETER(p_connectable, return);
412 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
413 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
415 API_TRACE("%d", *p_connectable);
417 return OAL_STATUS_SUCCESS;
420 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
422 API_TRACE("%d", discoverable_timeout);
424 *p_timeout = discoverable_timeout;
426 return OAL_STATUS_SUCCESS;
429 oal_status_t adapter_get_service_uuids(void)
433 CHECK_OAL_INITIALIZED();
437 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
438 if (ret != BT_STATUS_SUCCESS) {
439 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
440 return convert_to_oal_status(ret);
443 return OAL_STATUS_SUCCESS;
446 oal_status_t adapter_get_bonded_devices(void)
450 CHECK_OAL_INITIALIZED();
454 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
455 if (ret != BT_STATUS_SUCCESS) {
456 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
457 return convert_to_oal_status(ret);
460 return OAL_STATUS_SUCCESS;
463 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
470 CHECK_OAL_INITIALIZED();
472 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
473 prop.len = sizeof(bt_scan_mode_t);
475 res = blued_api->set_adapter_property(&prop);
476 if (res != BT_STATUS_SUCCESS) {
477 BT_ERR("set scan mode failed [%s]", status2string(res));
478 return convert_to_oal_status(res);
482 return OAL_STATUS_SUCCESS;
485 oal_status_t adapter_set_connectable(int connectable)
489 API_TRACE("%d", connectable);
491 CHECK_OAL_INITIALIZED();
493 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
495 return set_scan_mode(mode);
498 oal_status_t adapter_set_discoverable(void)
500 CHECK_OAL_INITIALIZED();
503 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
506 oal_status_t adapter_set_discoverable_timeout(int timeout)
510 uint32_t prop_val = timeout;
512 CHECK_OAL_INITIALIZED();
513 API_TRACE("%d", timeout);
515 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
516 prop.len = sizeof(prop_val);
517 prop.val = &prop_val;
518 res = blued_api->set_adapter_property(&prop);
519 if (res != BT_STATUS_SUCCESS) {
520 BT_ERR("set_adapter_property failed [%s]", status2string(res));
521 return convert_to_oal_status(res);
523 return OAL_STATUS_SUCCESS;
526 oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
527 int adv_type, int chnl_map, int tx_power, int timeout_s)
530 CHECK_OAL_INITIALIZED();
533 res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
534 adv_type, chnl_map, tx_power, timeout_s);
535 if (res != OAL_STATUS_SUCCESS) {
536 BT_ERR("gatts_multi_adv_update: [%d]", res);
539 return OAL_STATUS_SUCCESS;
542 oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
543 oal_ble_multi_adv_param_setup_t * adv_param_setup)
546 CHECK_OAL_INITIALIZED();
547 OAL_CHECK_PARAMETER(adv_param_setup, return);
551 res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
552 if (res != OAL_STATUS_SUCCESS) {
553 BT_ERR("failed: [%d]", res);
556 return OAL_STATUS_SUCCESS;
559 oal_status_t adapter_ble_multi_adv_enable(int instance_id)
562 CHECK_OAL_INITIALIZED();
565 res = gatts_multi_adv_enable(instance_id);
566 if (res != OAL_STATUS_SUCCESS) {
567 BT_ERR("failed: [%d]", res);
571 return OAL_STATUS_SUCCESS;
574 oal_status_t adapter_ble_multi_adv_disable(int instance_id)
577 CHECK_OAL_INITIALIZED();
580 res = gatts_multi_adv_disable(instance_id);
581 if (res != OAL_STATUS_SUCCESS) {
582 BT_ERR("failed: [%d]", res);
586 return OAL_STATUS_SUCCESS;
589 static void cb_adapter_properties(bt_status_t status,
591 bt_property_t *properties)
595 BT_DBG("status: %d, count: %d", status, num_properties);
597 if (status != BT_STATUS_SUCCESS) {
598 if (num_properties == 1) {
599 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
600 status2string(status), num_properties, properties[num_properties-1].type);
602 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
607 for (i = 0; i < num_properties; i++) {
608 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
609 switch (properties[i].type) {
610 case BT_PROPERTY_VERSION: {
611 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
612 local_version[properties[i].len] = '\0';
614 BT_DBG("Version: %s", local_version);
615 /* Send event to application */
616 if (num_properties == 1) {
617 char *adapter_ver = g_strdup(local_version);
619 /* Application has requested this property SET/GET hence send EVENT */
620 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
624 case BT_PROPERTY_BDNAME: {
625 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
626 local_name[properties[i].len] = '\0';
628 BT_DBG("Name: %s", local_name);
629 /* Send event to application */
630 if (num_properties == 1) {
631 char * adap_name = g_strdup(local_name);
633 /* Application has requested this property SET/GET hence send EVENT */
634 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
638 case BT_PROPERTY_BDADDR: {
641 addr = properties[i].val;
642 memcpy(local_address.addr, addr->address, 6);
643 if (num_properties == 1) {
644 /* Application has requested this property SET/GET hence send EVENT */
645 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
646 g_memdup(&local_address, sizeof(local_address)),
647 sizeof(local_address));
651 case BT_PROPERTY_UUIDS: {
654 num_uuid = properties[i].len/sizeof(bt_uuid_t);
656 BT_DBG("num_uuid: %d", num_uuid);
658 /* Send event to application */
659 if (num_properties == 1) {
660 event_adapter_services_t *uuids_event;
662 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
663 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
664 uuids_event->num = num_uuid;
666 /* Application has requested this property SET/GET hence send EVENT */
667 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
668 uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
672 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
673 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
675 BT_INFO("Scan mode (%d)", cur_mode);
677 scan_mode = cur_mode;
679 /* Send event to application */
680 if (num_properties == 1) {
681 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
683 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
684 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
685 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
686 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
688 /* Application has requested this property SET/GET hence send EVENT */
689 send_event(event, NULL, 0);
693 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
696 timeout = *((uint32_t*)properties[i].val);
698 BT_INFO("Discoverability timeout: %d", timeout);
699 discoverable_timeout = timeout;
701 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
702 g_memdup(properties[i].val, sizeof(uint32_t)),
706 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
709 bt_bdaddr_t *bonded_addr_list;
710 event_device_list_t *event_data;
712 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
713 BT_DBG("num_bonded %d", num_bonded);
715 if (num_properties > 1) /* No explicit req for this prop, ignore */
718 bonded_addr_list = properties[i].val;
719 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
720 event_data->num = num_bonded;
722 for (j = 0; j < num_bonded; j++)
723 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
725 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
726 event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
730 BT_WARN("Unhandled property: %d", properties[i].type);
736 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
740 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
743 send_event(event, NULL, 0);
746 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
748 remote_device_t dev_info;
749 ble_adv_data_t adv_info;
755 if (num_properties == 0) {
756 BT_ERR("Unexpected, properties count is zero!!");
760 memset(&dev_info, 0x00, sizeof(remote_device_t));
761 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
763 print_bt_properties(num_properties, properties);
764 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
766 BT_INFO("number of properties= [%d] ", num_properties, size);
768 if (dev_info.type != DEV_TYPE_BREDR) {
769 /* BLE Single or DUAL mode found, so it should have Adv data */
770 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
772 ble_dev_event->adv_len = adv_info.len;
774 if (adv_info.len > 0 && adv_info.adv_data) {
775 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
776 ble_dev_event->adv_len = adv_info.len;
778 ble_dev_event->adv_len = 0;
780 ble_dev_event->device_info = dev_info;
782 event_data = ble_dev_event;
783 size = sizeof(event_ble_dev_found_t);
784 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
786 /* BREDR device, so No Adv data */
787 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
789 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
790 event_data = dev_event;
791 size = sizeof(remote_device_t);
792 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
795 send_event(event, event_data, size);