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;
167 oal_status_t le_enable(void)
169 int ret = BT_STATUS_SUCCESS;
172 CHECK_OAL_INITIALIZED();
173 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
174 g_timeout_add(200, retry_enable_le, NULL);
175 return OAL_STATUS_PENDING;
178 ret = blued_api->le_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 le_disable(void)
194 CHECK_OAL_INITIALIZED();
196 ret = blued_api->le_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 adapter_start_inquiry(unsigned short duration)
211 CHECK_OAL_INITIALIZED();
213 ret = blued_api->start_discovery();
214 if (ret != BT_STATUS_SUCCESS) {
215 BT_ERR("start_discovery failed: [%s]", status2string(ret));
216 return convert_to_oal_status(ret);
219 return OAL_STATUS_SUCCESS;
222 oal_status_t adapter_stop_inquiry(void)
228 CHECK_OAL_INITIALIZED();
230 ret = blued_api->cancel_discovery();
231 if (ret != BT_STATUS_SUCCESS) {
232 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
233 return convert_to_oal_status(ret);
236 return OAL_STATUS_SUCCESS;
239 /* Callbacks from Stack */
240 static void cb_adapter_state_change(bt_state_t status)
245 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
247 send_event(event, NULL, 0);
251 /* Callbacks from Stack */
252 static void cb_ble_state_change(bt_state_t status)
257 event = (BT_STATE_ON == status)?OAL_EVENT_BLE_ENABLED:OAL_EVENT_BLE_DISABLED;
259 send_event(event, NULL, 0);
263 static gboolean retry_enable_adapter(gpointer data)
270 static gboolean retry_enable_le(gpointer data)
276 oal_status_t adapter_get_properties(void)
281 CHECK_OAL_INITIALIZED();
283 ret = blued_api->get_adapter_properties();
284 if (ret != BT_STATUS_SUCCESS) {
285 BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
286 return convert_to_oal_status(ret);
289 return OAL_STATUS_SUCCESS;
292 oal_status_t adapter_get_address(void)
297 CHECK_OAL_INITIALIZED();
299 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
300 if (ret != BT_STATUS_SUCCESS) {
301 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
302 return convert_to_oal_status(ret);
305 return OAL_STATUS_SUCCESS;
308 oal_status_t adapter_get_version(void)
313 CHECK_OAL_INITIALIZED();
315 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
316 if (ret != BT_STATUS_SUCCESS) {
317 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
318 return convert_to_oal_status(ret);
321 return OAL_STATUS_SUCCESS;
324 oal_status_t adapter_get_name(void)
328 CHECK_OAL_INITIALIZED();
332 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
333 if (ret != BT_STATUS_SUCCESS) {
334 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
335 return convert_to_oal_status(ret);
338 return OAL_STATUS_SUCCESS;
341 oal_status_t adapter_set_name(char * name)
346 CHECK_OAL_INITIALIZED();
348 OAL_CHECK_PARAMETER(name, return);
349 API_TRACE("Name: %s", name);
351 prop.type = BT_PROPERTY_BDNAME;
352 prop.len = strlen(name);
355 ret = blued_api->set_adapter_property(&prop);
356 if (ret != BT_STATUS_SUCCESS) {
357 BT_ERR("set_adapter_property: [%s]", status2string(ret));
358 ret = OAL_STATUS_INTERNAL_ERROR;
360 ret = OAL_STATUS_SUCCESS;
365 oal_status_t adapter_is_discoverable(int *p_discoverable)
367 OAL_CHECK_PARAMETER(p_discoverable, return);
369 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
371 API_TRACE("%d", *p_discoverable);
373 return OAL_STATUS_SUCCESS;
376 oal_status_t adapter_is_connectable(int *p_connectable)
378 OAL_CHECK_PARAMETER(p_connectable, return);
380 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
381 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
383 API_TRACE("%d", *p_connectable);
385 return OAL_STATUS_SUCCESS;
388 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
390 API_TRACE("%d", discoverable_timeout);
392 *p_timeout = discoverable_timeout;
394 return OAL_STATUS_SUCCESS;
397 oal_status_t adapter_get_service_uuids(void)
401 CHECK_OAL_INITIALIZED();
405 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
406 if (ret != BT_STATUS_SUCCESS) {
407 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
408 return convert_to_oal_status(ret);
411 return OAL_STATUS_SUCCESS;
414 oal_status_t adapter_get_bonded_devices(void)
418 CHECK_OAL_INITIALIZED();
422 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
423 if (ret != BT_STATUS_SUCCESS) {
424 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
425 return convert_to_oal_status(ret);
428 return OAL_STATUS_SUCCESS;
431 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
438 CHECK_OAL_INITIALIZED();
440 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
441 prop.len = sizeof(bt_scan_mode_t);
443 res = blued_api->set_adapter_property(&prop);
444 if (res != BT_STATUS_SUCCESS) {
445 BT_ERR("set scan mode failed [%s]", status2string(res));
446 return convert_to_oal_status(res);
450 return OAL_STATUS_SUCCESS;
453 oal_status_t adapter_set_connectable(int connectable)
457 API_TRACE("%d", connectable);
459 CHECK_OAL_INITIALIZED();
461 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
463 return set_scan_mode(mode);
466 oal_status_t adapter_set_discoverable(void)
468 CHECK_OAL_INITIALIZED();
471 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
474 oal_status_t adapter_set_discoverable_timeout(int timeout)
478 uint32_t prop_val = timeout;
480 CHECK_OAL_INITIALIZED();
481 API_TRACE("%d", timeout);
483 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
484 prop.len = sizeof(prop_val);
485 prop.val = &prop_val;
486 res = blued_api->set_adapter_property(&prop);
487 if (res != BT_STATUS_SUCCESS) {
488 BT_ERR("set_adapter_property failed [%s]", status2string(res));
489 return convert_to_oal_status(res);
491 return OAL_STATUS_SUCCESS;
494 oal_status_t adapter_ble_multi_adv_update(int Ins_id, int min_intv, int max_intv,
495 int adv_type, int chnl_map, int tx_power, int timeout_s)
498 CHECK_OAL_INITIALIZED();
501 res = gatts_multi_adv_update(Ins_id, min_intv, max_intv,
502 adv_type, chnl_map, tx_power, timeout_s);
503 if (res != OAL_STATUS_SUCCESS) {
504 BT_ERR("gatts_multi_adv_update: [%d]", res);
507 return OAL_STATUS_SUCCESS;
510 oal_status_t adapter_ble_multi_adv_set_inst_data(int instance_id,
511 oal_ble_multi_adv_param_setup_t * adv_param_setup)
514 CHECK_OAL_INITIALIZED();
515 OAL_CHECK_PARAMETER(adv_param_setup, return);
519 res = gatts_multi_adv_set_inst_data(instance_id, adv_param_setup);
520 if (res != OAL_STATUS_SUCCESS) {
521 BT_ERR("failed: [%d]", res);
524 return OAL_STATUS_SUCCESS;
527 oal_status_t adapter_ble_multi_adv_enable(int instance_id)
530 CHECK_OAL_INITIALIZED();
533 res = gatts_multi_adv_enable(instance_id);
534 if (res != OAL_STATUS_SUCCESS) {
535 BT_ERR("failed: [%d]", res);
539 return OAL_STATUS_SUCCESS;
542 oal_status_t adapter_ble_multi_adv_disable(int instance_id)
545 CHECK_OAL_INITIALIZED();
548 res = gatts_multi_adv_disable(instance_id);
549 if (res != OAL_STATUS_SUCCESS) {
550 BT_ERR("failed: [%d]", res);
554 return OAL_STATUS_SUCCESS;
557 static void cb_adapter_properties(bt_status_t status,
559 bt_property_t *properties)
563 BT_DBG("status: %d, count: %d", status, num_properties);
565 if (status != BT_STATUS_SUCCESS) {
566 if (num_properties == 1) {
567 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
568 status2string(status), num_properties, properties[num_properties-1].type);
570 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
575 for (i = 0; i < num_properties; i++) {
576 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
577 switch (properties[i].type) {
578 case BT_PROPERTY_VERSION: {
579 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
580 local_version[properties[i].len] = '\0';
582 BT_DBG("Version: %s", local_version);
583 /* Send event to application */
584 if (num_properties == 1) {
585 char *adapter_ver = g_strdup(local_version);
587 /* Application has requested this property SET/GET hence send EVENT */
588 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
592 case BT_PROPERTY_BDNAME: {
593 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
594 local_name[properties[i].len] = '\0';
596 BT_DBG("Name: %s", local_name);
597 /* Send event to application */
598 if (num_properties == 1) {
599 char * adap_name = g_strdup(local_name);
601 /* Application has requested this property SET/GET hence send EVENT */
602 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
606 case BT_PROPERTY_BDADDR: {
609 addr = properties[i].val;
610 memcpy(local_address.addr, addr->address, 6);
611 if (num_properties == 1) {
612 /* Application has requested this property SET/GET hence send EVENT */
613 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
614 g_memdup(&local_address, sizeof(local_address)),
615 sizeof(local_address));
619 case BT_PROPERTY_UUIDS: {
622 num_uuid = properties[i].len/sizeof(bt_uuid_t);
624 BT_DBG("num_uuid: %d", num_uuid);
626 /* Send event to application */
627 if (num_properties == 1) {
628 event_adapter_services_t *uuids_event;
630 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
631 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
632 uuids_event->num = num_uuid;
634 /* Application has requested this property SET/GET hence send EVENT */
635 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
636 uuids_event, (sizeof(event_adapter_services_t) + num_uuid * sizeof(bt_uuid_t)));
640 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
641 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
643 BT_INFO("Scan mode (%d)", cur_mode);
645 scan_mode = cur_mode;
647 /* Send event to application */
648 if (num_properties == 1) {
649 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
651 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
652 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
653 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
654 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
656 /* Application has requested this property SET/GET hence send EVENT */
657 send_event(event, NULL, 0);
661 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
664 timeout = *((uint32_t*)properties[i].val);
666 BT_INFO("Discoverability timeout: %d", timeout);
667 discoverable_timeout = timeout;
669 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
670 g_memdup(properties[i].val, sizeof(uint32_t)),
674 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
677 bt_bdaddr_t *bonded_addr_list;
678 event_device_list_t *event_data;
680 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
681 BT_DBG("num_bonded %d", num_bonded);
683 if (num_properties > 1) /* No explicit req for this prop, ignore */
686 bonded_addr_list = properties[i].val;
687 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
688 event_data->num = num_bonded;
690 for (j = 0; j < num_bonded; j++)
691 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
693 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
694 event_data, (sizeof(event_device_list_t) + num_bonded * sizeof(bt_bdaddr_t)));
698 BT_WARN("Unhandled property: %d", properties[i].type);
704 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
708 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
711 send_event(event, NULL, 0);
714 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
716 remote_device_t dev_info;
717 ble_adv_data_t adv_info;
723 if (num_properties == 0) {
724 BT_ERR("Unexpected, properties count is zero!!");
728 memset(&dev_info, 0x00, sizeof(remote_device_t));
729 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
731 print_bt_properties(num_properties, properties);
732 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
734 BT_INFO("number of properties= [%d] ", num_properties, size);
736 if (dev_info.type != DEV_TYPE_BREDR) {
737 /* BLE Single or DUAL mode found, so it should have Adv data */
738 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
740 ble_dev_event->adv_len = adv_info.len;
742 if (adv_info.len > 0 && adv_info.adv_data) {
743 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
744 ble_dev_event->adv_len = adv_info.len;
746 ble_dev_event->adv_len = 0;
748 ble_dev_event->device_info = dev_info;
750 event_data = ble_dev_event;
751 size = sizeof(event_ble_dev_found_t);
752 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
754 /* BREDR device, so No Adv data */
755 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
757 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
758 event_data = dev_event;
759 size = sizeof(remote_device_t);
760 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
763 send_event(event, event_data, size);