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"
36 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
38 static const bt_interface_t * blued_api;
40 static bt_address_t local_address;
41 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
42 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
43 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
44 static int discoverable_timeout = 0;
46 /* Forward declarations */
47 oal_status_t convert_to_oal_status(bt_status_t status);
48 static gboolean retry_enable_adapter(gpointer data);
49 oal_status_t oal_mgr_init_internal(void);
52 /* Callback registered with Stack */
53 static void cb_adapter_state_change(bt_state_t status);
54 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state);
55 static void cb_adapter_device_found(int num_properties, bt_property_t *properties);
56 static void cb_adapter_properties (bt_status_t status,
57 int num_properties, bt_property_t *properties);
58 extern void cb_device_properties(bt_status_t status, bt_bdaddr_t *bd_addr,
59 int num_properties, bt_property_t *properties);
60 extern void cb_device_bond_state_changed(bt_status_t status, bt_bdaddr_t *bd_addr,
61 bt_bond_state_t state);
62 extern void cb_device_acl_state_changed(bt_status_t status, bt_bdaddr_t *remote_bd_addr,
63 bt_acl_state_t state);
64 extern void cb_device_pin_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class);
65 extern void cb_device_ssp_request(bt_bdaddr_t *bd_addr, bt_bdname_t *bdname, uint32_t device_class,
66 bt_ssp_variant_t pairing_variant, uint32_t pass_key);
67 extern void cb_device_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_service_id_t service_d);
69 static bt_callbacks_t callbacks = {
71 cb_adapter_state_change,
72 cb_adapter_properties,
74 cb_adapter_device_found,
75 cb_adapter_discovery_state_changed,
76 cb_device_pin_request,
77 cb_device_ssp_request,
78 cb_device_bond_state_changed,
79 cb_device_acl_state_changed,
80 NULL, /* callback_thread_event */
81 NULL, /* dut_mode_recv_callback */
82 NULL, /* le_test_mode_callback*/
83 NULL, /* energy_info_callback */
84 cb_device_authorize_request,
87 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
92 ret = blued_api->init(&callbacks);
94 if (ret != BT_STATUS_SUCCESS) {
95 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
97 return convert_to_oal_status(ret);
100 return OAL_STATUS_SUCCESS;
103 const bt_interface_t* adapter_get_stack_interface(void)
108 void adapter_mgr_cleanup(void)
110 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
114 oal_status_t adapter_enable(void)
116 int ret = BT_STATUS_SUCCESS;
119 CHECK_OAL_INITIALIZED();
120 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
121 g_timeout_add(200, retry_enable_adapter, NULL);
122 return OAL_STATUS_PENDING;
125 ret = blued_api->enable();
127 if (ret != BT_STATUS_SUCCESS) {
128 BT_ERR("Enable failed: [%s]", status2string(ret));
129 return convert_to_oal_status(ret);
132 return OAL_STATUS_SUCCESS;
135 oal_status_t adapter_disable(void)
141 CHECK_OAL_INITIALIZED();
143 ret = blued_api->disable();
145 if (ret != BT_STATUS_SUCCESS) {
146 BT_ERR("Disable failed: [%s]", status2string(ret));
147 return convert_to_oal_status(ret);
149 return OAL_STATUS_SUCCESS;
152 oal_status_t adapter_start_inquiry(void)
158 CHECK_OAL_INITIALIZED();
160 ret = blued_api->start_discovery();
161 if (ret != BT_STATUS_SUCCESS) {
162 BT_ERR("start_discovery failed: [%s]", status2string(ret));
163 return convert_to_oal_status(ret);
166 return OAL_STATUS_SUCCESS;
169 oal_status_t adapter_stop_inquiry(void)
175 CHECK_OAL_INITIALIZED();
177 ret = blued_api->cancel_discovery();
178 if (ret != BT_STATUS_SUCCESS) {
179 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
180 return convert_to_oal_status(ret);
183 return OAL_STATUS_SUCCESS;
186 /* Callbacks from Stack */
187 static void cb_adapter_state_change(bt_state_t status)
192 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
194 send_event(event, NULL, 0);
197 static gboolean retry_enable_adapter(gpointer data)
203 oal_status_t adapter_get_address(void)
208 CHECK_OAL_INITIALIZED();
210 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
211 if (ret != BT_STATUS_SUCCESS) {
212 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
213 return convert_to_oal_status(ret);
216 return OAL_STATUS_SUCCESS;
219 oal_status_t adapter_get_version(void)
224 CHECK_OAL_INITIALIZED();
226 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
227 if (ret != BT_STATUS_SUCCESS) {
228 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
229 return convert_to_oal_status(ret);
232 return OAL_STATUS_SUCCESS;
235 oal_status_t adapter_get_name(void)
239 CHECK_OAL_INITIALIZED();
243 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
244 if (ret != BT_STATUS_SUCCESS) {
245 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
246 return convert_to_oal_status(ret);
249 return OAL_STATUS_SUCCESS;
252 oal_status_t adapter_set_name(char * name)
257 CHECK_OAL_INITIALIZED();
259 OAL_CHECK_PARAMETER(name, return);
260 API_TRACE("Name: %s", name);
262 prop.type = BT_PROPERTY_BDNAME;
263 prop.len = strlen(name);
266 ret = blued_api->set_adapter_property(&prop);
267 if (ret != BT_STATUS_SUCCESS) {
268 BT_ERR("set_adapter_property: [%s]", status2string(ret));
269 ret = OAL_STATUS_INTERNAL_ERROR;
271 ret = OAL_STATUS_SUCCESS;
276 oal_status_t adapter_is_discoverable(int *p_discoverable)
278 OAL_CHECK_PARAMETER(p_discoverable, return);
280 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
282 API_TRACE("%d", *p_discoverable);
284 return OAL_STATUS_SUCCESS;
287 oal_status_t adapter_is_connectable(int *p_connectable)
289 OAL_CHECK_PARAMETER(p_connectable, return);
291 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
292 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
294 API_TRACE("%d", *p_connectable);
296 return OAL_STATUS_SUCCESS;
299 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
301 API_TRACE("%d", discoverable_timeout);
303 *p_timeout = discoverable_timeout;
305 return OAL_STATUS_SUCCESS;
308 oal_status_t adapter_get_service_uuids(void)
312 CHECK_OAL_INITIALIZED();
316 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
317 if (ret != BT_STATUS_SUCCESS) {
318 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
319 return convert_to_oal_status(ret);
322 return OAL_STATUS_SUCCESS;
325 oal_status_t adapter_get_bonded_devices(void)
329 CHECK_OAL_INITIALIZED();
333 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
334 if (ret != BT_STATUS_SUCCESS) {
335 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
336 return convert_to_oal_status(ret);
339 return OAL_STATUS_SUCCESS;
342 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
349 CHECK_OAL_INITIALIZED();
351 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
352 prop.len = sizeof(bt_scan_mode_t);
354 res = blued_api->set_adapter_property(&prop);
355 if (res != BT_STATUS_SUCCESS) {
356 BT_ERR("set scan mode failed [%s]", status2string(res));
357 return convert_to_oal_status(res);
361 return OAL_STATUS_SUCCESS;
364 oal_status_t adapter_set_connectable(int connectable)
368 API_TRACE("%d", connectable);
370 CHECK_OAL_INITIALIZED();
372 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
374 return set_scan_mode(mode);
377 oal_status_t adapter_set_discoverable(void)
379 CHECK_OAL_INITIALIZED();
382 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
385 oal_status_t adapter_set_discoverable_timeout(int timeout)
389 uint32_t prop_val = timeout;
391 CHECK_OAL_INITIALIZED();
392 API_TRACE("%d", timeout);
394 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
395 prop.len = sizeof(prop_val);
396 prop.val = &prop_val;
397 res = blued_api->set_adapter_property(&prop);
398 if (res != BT_STATUS_SUCCESS) {
399 BT_ERR("set_adapter_property failed [%s]", status2string(res));
400 return convert_to_oal_status(res);
402 return OAL_STATUS_SUCCESS;
405 static void cb_adapter_properties(bt_status_t status,
407 bt_property_t *properties)
411 BT_DBG("status: %d, count: %d", status, num_properties);
413 if (status != BT_STATUS_SUCCESS) {
414 if (num_properties == 1) {
415 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
416 status2string(status), num_properties, properties[num_properties-1].type);
418 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
423 for (i = 0; i < num_properties; i++) {
424 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
425 switch (properties[i].type) {
426 case BT_PROPERTY_VERSION: {
427 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
428 local_version[properties[i].len] = '\0';
430 BT_DBG("Version: %s", local_version);
431 /* Send event to application */
432 if (num_properties == 1) {
433 char *adapter_ver = g_strdup(local_version);
435 /* Application has requested this property SET/GET hence send EVENT */
436 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
440 case BT_PROPERTY_BDNAME: {
441 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
442 local_name[properties[i].len] = '\0';
444 BT_DBG("Name: %s", local_name);
445 /* Send event to application */
446 if (num_properties == 1) {
447 char * adap_name = g_strdup(local_name);
449 /* Application has requested this property SET/GET hence send EVENT */
450 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
454 case BT_PROPERTY_BDADDR: {
457 addr = properties[i].val;
458 memcpy(local_address.addr, addr->address, 6);
459 if (num_properties == 1) {
460 /* Application has requested this property SET/GET hence send EVENT */
461 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
462 g_memdup(&local_address, sizeof(local_address)),
463 sizeof(local_address));
467 case BT_PROPERTY_UUIDS: {
470 num_uuid = properties[i].len/sizeof(bt_uuid_t);
472 BT_DBG("num_uuid: %d", num_uuid);
474 /* Send event to application */
475 if (num_properties == 1) {
476 event_adapter_services_t *uuids_event;
478 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
479 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
480 uuids_event->num = num_uuid;
482 /* Application has requested this property SET/GET hence send EVENT */
483 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
484 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
488 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
489 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
491 BT_INFO("Scan mode (%d)", cur_mode);
493 scan_mode = cur_mode;
495 /* Send event to application */
496 if (num_properties == 1) {
497 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
499 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
500 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
501 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
502 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
504 /* Application has requested this property SET/GET hence send EVENT */
505 send_event(event, NULL, 0);
509 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
512 timeout = *((uint32_t*)properties[i].val);
514 BT_INFO("Discoverability timeout: %d", timeout);
515 discoverable_timeout = timeout;
517 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
518 g_memdup(properties[i].val, sizeof(uint32_t)),
522 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
525 bt_bdaddr_t *bonded_addr_list;
526 event_device_list_t *event_data;
528 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
529 BT_DBG("num_bonded %d", num_bonded);
531 if (num_properties > 1) /* No explicit req for this prop, ignore */
534 bonded_addr_list = properties[i].val;
535 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
536 event_data->num = num_bonded;
538 for (j = 0; j < num_bonded; j++)
539 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
541 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
542 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
546 BT_WARN("Unhandled property: %d", properties[i].type);
552 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
556 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
559 send_event(event, NULL, 0);
562 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
564 remote_device_t dev_info;
565 ble_adv_data_t adv_info;
571 if (num_properties == 0) {
572 BT_ERR("Unexpected, properties count is zero!!");
576 memset(&dev_info, 0x00, sizeof(remote_device_t));
577 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
579 print_bt_properties(num_properties, properties);
580 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
582 BT_INFO("number of properties= [%d] ", num_properties, size);
584 if (dev_info.type != DEV_TYPE_BREDR) {
585 /* BLE Single or DUAL mode found, so it should have Adv data */
586 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
588 ble_dev_event->adv_len = adv_info.len;
590 if (adv_info.len > 0 && adv_info.adv_data) {
591 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
592 ble_dev_event->adv_len = adv_info.len;
594 ble_dev_event->adv_len = 0;
596 ble_dev_event->device_info = dev_info;
598 event_data = ble_dev_event;
599 size = sizeof(event_ble_dev_found_t);
600 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
602 /* BREDR device, so No Adv data */
603 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
605 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
606 event_data = dev_event;
607 size = sizeof(remote_device_t);
608 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
611 send_event(event, event_data, size);