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);
68 extern void cb_device_trust_state_changed(bt_bdaddr_t *remote_bd_addr, bt_device_trust_state_t trusted);
70 extern void cb_socket_conn_authorize_request(bt_bdaddr_t *remote_bd_addr, bt_uuid_t *uuid);
73 static bt_callbacks_t callbacks = {
75 cb_adapter_state_change,
76 cb_adapter_properties,
78 cb_adapter_device_found,
79 cb_adapter_discovery_state_changed,
80 cb_device_pin_request,
81 cb_device_ssp_request,
82 cb_device_bond_state_changed,
83 cb_device_acl_state_changed,
84 NULL, /* callback_thread_event */
85 NULL, /* dut_mode_recv_callback */
86 NULL, /* le_test_mode_callback*/
87 NULL, /* energy_info_callback */
88 cb_device_authorize_request,
89 cb_device_trust_state_changed,
91 cb_socket_conn_authorize_request,
95 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
100 ret = blued_api->init(&callbacks);
102 if (ret != BT_STATUS_SUCCESS) {
103 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
104 blued_api->cleanup();
105 return convert_to_oal_status(ret);
108 return OAL_STATUS_SUCCESS;
111 const bt_interface_t* adapter_get_stack_interface(void)
116 void adapter_mgr_cleanup(void)
118 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
122 oal_status_t adapter_enable(void)
124 int ret = BT_STATUS_SUCCESS;
127 CHECK_OAL_INITIALIZED();
128 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
129 g_timeout_add(200, retry_enable_adapter, NULL);
130 return OAL_STATUS_PENDING;
133 ret = blued_api->enable();
135 if (ret != BT_STATUS_SUCCESS) {
136 BT_ERR("Enable failed: [%s]", status2string(ret));
137 return convert_to_oal_status(ret);
140 return OAL_STATUS_SUCCESS;
143 oal_status_t adapter_disable(void)
149 CHECK_OAL_INITIALIZED();
151 ret = blued_api->disable();
153 if (ret != BT_STATUS_SUCCESS) {
154 BT_ERR("Disable failed: [%s]", status2string(ret));
155 return convert_to_oal_status(ret);
157 return OAL_STATUS_SUCCESS;
160 oal_status_t adapter_start_inquiry(void)
166 CHECK_OAL_INITIALIZED();
168 ret = blued_api->start_discovery();
169 if (ret != BT_STATUS_SUCCESS) {
170 BT_ERR("start_discovery failed: [%s]", status2string(ret));
171 return convert_to_oal_status(ret);
174 return OAL_STATUS_SUCCESS;
177 oal_status_t adapter_stop_inquiry(void)
183 CHECK_OAL_INITIALIZED();
185 ret = blued_api->cancel_discovery();
186 if (ret != BT_STATUS_SUCCESS) {
187 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
188 return convert_to_oal_status(ret);
191 return OAL_STATUS_SUCCESS;
194 /* Callbacks from Stack */
195 static void cb_adapter_state_change(bt_state_t status)
200 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
202 send_event(event, NULL, 0);
205 static gboolean retry_enable_adapter(gpointer data)
211 oal_status_t adapter_get_properties(void)
216 CHECK_OAL_INITIALIZED();
218 ret = blued_api->get_adapter_properties();
219 if (ret != BT_STATUS_SUCCESS) {
220 BT_ERR("get_adapter_properties failed: [%s]", status2string(ret));
221 return convert_to_oal_status(ret);
224 return OAL_STATUS_SUCCESS;
227 oal_status_t adapter_get_address(void)
232 CHECK_OAL_INITIALIZED();
234 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
235 if (ret != BT_STATUS_SUCCESS) {
236 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
237 return convert_to_oal_status(ret);
240 return OAL_STATUS_SUCCESS;
243 oal_status_t adapter_get_version(void)
248 CHECK_OAL_INITIALIZED();
250 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
251 if (ret != BT_STATUS_SUCCESS) {
252 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
253 return convert_to_oal_status(ret);
256 return OAL_STATUS_SUCCESS;
259 oal_status_t adapter_get_name(void)
263 CHECK_OAL_INITIALIZED();
267 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
268 if (ret != BT_STATUS_SUCCESS) {
269 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
270 return convert_to_oal_status(ret);
273 return OAL_STATUS_SUCCESS;
276 oal_status_t adapter_set_name(char * name)
281 CHECK_OAL_INITIALIZED();
283 OAL_CHECK_PARAMETER(name, return);
284 API_TRACE("Name: %s", name);
286 prop.type = BT_PROPERTY_BDNAME;
287 prop.len = strlen(name);
290 ret = blued_api->set_adapter_property(&prop);
291 if (ret != BT_STATUS_SUCCESS) {
292 BT_ERR("set_adapter_property: [%s]", status2string(ret));
293 ret = OAL_STATUS_INTERNAL_ERROR;
295 ret = OAL_STATUS_SUCCESS;
300 oal_status_t adapter_is_discoverable(int *p_discoverable)
302 OAL_CHECK_PARAMETER(p_discoverable, return);
304 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
306 API_TRACE("%d", *p_discoverable);
308 return OAL_STATUS_SUCCESS;
311 oal_status_t adapter_is_connectable(int *p_connectable)
313 OAL_CHECK_PARAMETER(p_connectable, return);
315 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
316 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
318 API_TRACE("%d", *p_connectable);
320 return OAL_STATUS_SUCCESS;
323 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
325 API_TRACE("%d", discoverable_timeout);
327 *p_timeout = discoverable_timeout;
329 return OAL_STATUS_SUCCESS;
332 oal_status_t adapter_get_service_uuids(void)
336 CHECK_OAL_INITIALIZED();
340 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
341 if (ret != BT_STATUS_SUCCESS) {
342 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
343 return convert_to_oal_status(ret);
346 return OAL_STATUS_SUCCESS;
349 oal_status_t adapter_get_bonded_devices(void)
353 CHECK_OAL_INITIALIZED();
357 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
358 if (ret != BT_STATUS_SUCCESS) {
359 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
360 return convert_to_oal_status(ret);
363 return OAL_STATUS_SUCCESS;
366 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
373 CHECK_OAL_INITIALIZED();
375 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
376 prop.len = sizeof(bt_scan_mode_t);
378 res = blued_api->set_adapter_property(&prop);
379 if (res != BT_STATUS_SUCCESS) {
380 BT_ERR("set scan mode failed [%s]", status2string(res));
381 return convert_to_oal_status(res);
385 return OAL_STATUS_SUCCESS;
388 oal_status_t adapter_set_connectable(int connectable)
392 API_TRACE("%d", connectable);
394 CHECK_OAL_INITIALIZED();
396 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
398 return set_scan_mode(mode);
401 oal_status_t adapter_set_discoverable(void)
403 CHECK_OAL_INITIALIZED();
406 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
409 oal_status_t adapter_set_discoverable_timeout(int timeout)
413 uint32_t prop_val = timeout;
415 CHECK_OAL_INITIALIZED();
416 API_TRACE("%d", timeout);
418 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
419 prop.len = sizeof(prop_val);
420 prop.val = &prop_val;
421 res = blued_api->set_adapter_property(&prop);
422 if (res != BT_STATUS_SUCCESS) {
423 BT_ERR("set_adapter_property failed [%s]", status2string(res));
424 return convert_to_oal_status(res);
426 return OAL_STATUS_SUCCESS;
429 static void cb_adapter_properties(bt_status_t status,
431 bt_property_t *properties)
435 BT_DBG("status: %d, count: %d", status, num_properties);
437 if (status != BT_STATUS_SUCCESS) {
438 if (num_properties == 1) {
439 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
440 status2string(status), num_properties, properties[num_properties-1].type);
442 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
447 for (i = 0; i < num_properties; i++) {
448 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
449 switch (properties[i].type) {
450 case BT_PROPERTY_VERSION: {
451 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
452 local_version[properties[i].len] = '\0';
454 BT_DBG("Version: %s", local_version);
455 /* Send event to application */
456 if (num_properties == 1) {
457 char *adapter_ver = g_strdup(local_version);
459 /* Application has requested this property SET/GET hence send EVENT */
460 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
464 case BT_PROPERTY_BDNAME: {
465 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
466 local_name[properties[i].len] = '\0';
468 BT_DBG("Name: %s", local_name);
469 /* Send event to application */
470 if (num_properties == 1) {
471 char * adap_name = g_strdup(local_name);
473 /* Application has requested this property SET/GET hence send EVENT */
474 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
478 case BT_PROPERTY_BDADDR: {
481 addr = properties[i].val;
482 memcpy(local_address.addr, addr->address, 6);
483 if (num_properties == 1) {
484 /* Application has requested this property SET/GET hence send EVENT */
485 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
486 g_memdup(&local_address, sizeof(local_address)),
487 sizeof(local_address));
491 case BT_PROPERTY_UUIDS: {
494 num_uuid = properties[i].len/sizeof(bt_uuid_t);
496 BT_DBG("num_uuid: %d", num_uuid);
498 /* Send event to application */
499 if (num_properties == 1) {
500 event_adapter_services_t *uuids_event;
502 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
503 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
504 uuids_event->num = num_uuid;
506 /* Application has requested this property SET/GET hence send EVENT */
507 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
508 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
512 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
513 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
515 BT_INFO("Scan mode (%d)", cur_mode);
517 scan_mode = cur_mode;
519 /* Send event to application */
520 if (num_properties == 1) {
521 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
523 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
524 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
525 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
526 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
528 /* Application has requested this property SET/GET hence send EVENT */
529 send_event(event, NULL, 0);
533 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
536 timeout = *((uint32_t*)properties[i].val);
538 BT_INFO("Discoverability timeout: %d", timeout);
539 discoverable_timeout = timeout;
541 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
542 g_memdup(properties[i].val, sizeof(uint32_t)),
546 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
549 bt_bdaddr_t *bonded_addr_list;
550 event_device_list_t *event_data;
552 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
553 BT_DBG("num_bonded %d", num_bonded);
555 if (num_properties > 1) /* No explicit req for this prop, ignore */
558 bonded_addr_list = properties[i].val;
559 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
560 event_data->num = num_bonded;
562 for (j = 0; j < num_bonded; j++)
563 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
565 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
566 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
570 BT_WARN("Unhandled property: %d", properties[i].type);
576 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
580 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
583 send_event(event, NULL, 0);
586 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
588 remote_device_t dev_info;
589 ble_adv_data_t adv_info;
595 if (num_properties == 0) {
596 BT_ERR("Unexpected, properties count is zero!!");
600 memset(&dev_info, 0x00, sizeof(remote_device_t));
601 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
603 print_bt_properties(num_properties, properties);
604 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
606 BT_INFO("number of properties= [%d] ", num_properties, size);
608 if (dev_info.type != DEV_TYPE_BREDR) {
609 /* BLE Single or DUAL mode found, so it should have Adv data */
610 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
612 ble_dev_event->adv_len = adv_info.len;
614 if (adv_info.len > 0 && adv_info.adv_data) {
615 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
616 ble_dev_event->adv_len = adv_info.len;
618 ble_dev_event->adv_len = 0;
620 ble_dev_event->device_info = dev_info;
622 event_data = ble_dev_event;
623 size = sizeof(event_ble_dev_found_t);
624 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
626 /* BREDR device, so No Adv data */
627 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
629 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
630 event_data = dev_event;
631 size = sizeof(remote_device_t);
632 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
635 send_event(event, event_data, size);