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);
61 static bt_callbacks_t callbacks = {
63 cb_adapter_state_change,
64 cb_adapter_properties,
66 cb_adapter_device_found,
67 cb_adapter_discovery_state_changed,
68 NULL, /* pin_request_callback */
69 NULL, /* ssp_request_callback */
70 NULL, /* bond_state_changed_callback */
71 NULL, /* acl_state_changed_callback */
72 NULL, /* callback_thread_event */
73 NULL, /* dut_mode_recv_callback */
74 NULL, /* le_test_mode_callback*/
75 NULL, /* energy_info_callback */
78 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
83 ret = blued_api->init(&callbacks);
85 if (ret != BT_STATUS_SUCCESS) {
86 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
88 return convert_to_oal_status(ret);
91 return OAL_STATUS_SUCCESS;
94 const bt_interface_t* adapter_get_stack_interface(void)
99 void adapter_mgr_cleanup(void)
101 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
105 oal_status_t adapter_enable(void)
107 int ret = BT_STATUS_SUCCESS;
110 CHECK_OAL_INITIALIZED();
111 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
112 g_timeout_add(200, retry_enable_adapter, NULL);
113 return OAL_STATUS_PENDING;
116 ret = blued_api->enable();
118 if (ret != BT_STATUS_SUCCESS) {
119 BT_ERR("Enable failed: [%s]", status2string(ret));
120 return convert_to_oal_status(ret);
123 return OAL_STATUS_SUCCESS;
126 oal_status_t adapter_disable(void)
132 CHECK_OAL_INITIALIZED();
134 ret = blued_api->disable();
136 if (ret != BT_STATUS_SUCCESS) {
137 BT_ERR("Disable failed: [%s]", status2string(ret));
138 return convert_to_oal_status(ret);
140 return OAL_STATUS_SUCCESS;
143 oal_status_t adapter_start_inquiry(void)
149 CHECK_OAL_INITIALIZED();
151 ret = blued_api->start_discovery();
152 if (ret != BT_STATUS_SUCCESS) {
153 BT_ERR("start_discovery failed: [%s]", status2string(ret));
154 return convert_to_oal_status(ret);
157 return OAL_STATUS_SUCCESS;
160 oal_status_t adapter_stop_inquiry(void)
166 CHECK_OAL_INITIALIZED();
168 ret = blued_api->cancel_discovery();
169 if (ret != BT_STATUS_SUCCESS) {
170 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
171 return convert_to_oal_status(ret);
174 return OAL_STATUS_SUCCESS;
177 /* Callbacks from Stack */
178 static void cb_adapter_state_change(bt_state_t status)
183 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
185 send_event(event, NULL, 0);
188 static gboolean retry_enable_adapter(gpointer data)
194 oal_status_t adapter_get_address(void)
199 CHECK_OAL_INITIALIZED();
201 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
202 if (ret != BT_STATUS_SUCCESS) {
203 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
204 return convert_to_oal_status(ret);
207 return OAL_STATUS_SUCCESS;
210 oal_status_t adapter_get_version(void)
215 CHECK_OAL_INITIALIZED();
217 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
218 if (ret != BT_STATUS_SUCCESS) {
219 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
220 return convert_to_oal_status(ret);
223 return OAL_STATUS_SUCCESS;
226 oal_status_t adapter_get_name(void)
230 CHECK_OAL_INITIALIZED();
234 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
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_set_name(char * name)
248 CHECK_OAL_INITIALIZED();
250 OAL_CHECK_PARAMETER(name, return);
251 API_TRACE("Name: %s", name);
253 prop.type = BT_PROPERTY_BDNAME;
254 prop.len = strlen(name);
257 ret = blued_api->set_adapter_property(&prop);
258 if (ret != BT_STATUS_SUCCESS) {
259 BT_ERR("set_adapter_property: [%s]", status2string(ret));
260 ret = OAL_STATUS_INTERNAL_ERROR;
262 ret = OAL_STATUS_SUCCESS;
267 oal_status_t adapter_is_discoverable(int *p_discoverable)
269 OAL_CHECK_PARAMETER(p_discoverable, return);
271 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
273 API_TRACE("%d", *p_discoverable);
275 return OAL_STATUS_SUCCESS;
278 oal_status_t adapter_is_connectable(int *p_connectable)
280 OAL_CHECK_PARAMETER(p_connectable, return);
282 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
283 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
285 API_TRACE("%d", *p_connectable);
287 return OAL_STATUS_SUCCESS;
290 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
292 API_TRACE("%d", discoverable_timeout);
294 *p_timeout = discoverable_timeout;
296 return OAL_STATUS_SUCCESS;
299 oal_status_t adapter_get_service_uuids(void)
303 CHECK_OAL_INITIALIZED();
307 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
308 if (ret != BT_STATUS_SUCCESS) {
309 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
310 return convert_to_oal_status(ret);
313 return OAL_STATUS_SUCCESS;
316 oal_status_t adapter_get_bonded_devices(void)
320 CHECK_OAL_INITIALIZED();
324 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
325 if (ret != BT_STATUS_SUCCESS) {
326 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
327 return convert_to_oal_status(ret);
330 return OAL_STATUS_SUCCESS;
333 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
340 CHECK_OAL_INITIALIZED();
342 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
343 prop.len = sizeof(bt_scan_mode_t);
345 res = blued_api->set_adapter_property(&prop);
346 if (res != BT_STATUS_SUCCESS) {
347 BT_ERR("set scan mode failed [%s]", status2string(res));
348 return convert_to_oal_status(res);
352 return OAL_STATUS_SUCCESS;
355 oal_status_t adapter_set_connectable(int connectable)
359 API_TRACE("%d", connectable);
361 CHECK_OAL_INITIALIZED();
363 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
365 return set_scan_mode(mode);
368 oal_status_t adapter_set_discoverable(void)
370 CHECK_OAL_INITIALIZED();
373 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
376 oal_status_t adapter_set_discoverable_timeout(int timeout)
380 uint32_t prop_val = timeout;
382 CHECK_OAL_INITIALIZED();
383 API_TRACE("%d", timeout);
385 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
386 prop.len = sizeof(prop_val);
387 prop.val = &prop_val;
388 res = blued_api->set_adapter_property(&prop);
389 if (res != BT_STATUS_SUCCESS) {
390 BT_ERR("set_adapter_property failed [%s]", status2string(res));
391 return convert_to_oal_status(res);
393 return OAL_STATUS_SUCCESS;
396 static void cb_adapter_properties(bt_status_t status,
398 bt_property_t *properties)
402 BT_DBG("status: %d, count: %d", status, num_properties);
404 if (status != BT_STATUS_SUCCESS) {
405 if (num_properties == 1) {
406 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
407 status2string(status), num_properties, properties[num_properties-1].type);
409 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
414 for (i = 0; i < num_properties; i++) {
415 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
416 switch (properties[i].type) {
417 case BT_PROPERTY_VERSION: {
418 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
419 local_version[properties[i].len] = '\0';
421 BT_DBG("Version: %s", local_version);
422 /* Send event to application */
423 if (num_properties == 1) {
424 char *adapter_ver = g_strdup(local_version);
426 /* Application has requested this property SET/GET hence send EVENT */
427 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
431 case BT_PROPERTY_BDNAME: {
432 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
433 local_name[properties[i].len] = '\0';
435 BT_DBG("Name: %s", local_name);
436 /* Send event to application */
437 if (num_properties == 1) {
438 char * adap_name = g_strdup(local_name);
440 /* Application has requested this property SET/GET hence send EVENT */
441 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
445 case BT_PROPERTY_BDADDR: {
448 addr = properties[i].val;
449 memcpy(local_address.addr, addr->address, 6);
450 if (num_properties == 1) {
451 /* Application has requested this property SET/GET hence send EVENT */
452 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
453 g_memdup(&local_address, sizeof(local_address)),
454 sizeof(local_address));
458 case BT_PROPERTY_UUIDS: {
461 num_uuid = properties[i].len/sizeof(bt_uuid_t);
463 BT_DBG("num_uuid: %d", num_uuid);
465 /* Send event to application */
466 if (num_properties == 1) {
467 event_adapter_services_t *uuids_event;
469 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
470 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
471 uuids_event->num = num_uuid;
473 /* Application has requested this property SET/GET hence send EVENT */
474 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
475 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
479 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
480 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
482 BT_INFO("Scan mode (%d)", cur_mode);
484 scan_mode = cur_mode;
486 /* Send event to application */
487 if (num_properties == 1) {
488 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
490 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
491 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
492 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
493 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
495 /* Application has requested this property SET/GET hence send EVENT */
496 send_event(event, NULL, 0);
500 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
503 timeout = *((uint32_t*)properties[i].val);
505 BT_INFO("Discoverability timeout: %d", timeout);
506 discoverable_timeout = timeout;
508 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
509 g_memdup(properties[i].val, sizeof(uint32_t)),
513 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
516 bt_bdaddr_t *bonded_addr_list;
517 event_device_list_t *event_data;
519 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
520 BT_DBG("num_bonded %d", num_bonded);
522 if (num_properties > 1) /* No explicit req for this prop, ignore */
525 bonded_addr_list = properties[i].val;
526 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
527 event_data->num = num_bonded;
529 for (j = 0; j < num_bonded; j++)
530 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
532 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
533 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
537 BT_WARN("Unhandled property: %d", properties[i].type);
543 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
547 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
550 send_event(event, NULL, 0);
553 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
555 remote_device_t dev_info;
556 ble_adv_data_t adv_info;
562 if (num_properties == 0) {
563 BT_ERR("Unexpected, properties count is zero!!");
567 memset(&dev_info, 0x00, sizeof(remote_device_t));
568 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
570 print_bt_properties(num_properties, properties);
571 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
573 BT_INFO("number of properties= [%d] ", num_properties, size);
575 if (dev_info.type != DEV_TYPE_BREDR) {
576 /* BLE Single or DUAL mode found, so it should have Adv data */
577 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
579 ble_dev_event->adv_len = adv_info.len;
581 if (adv_info.len > 0 && adv_info.adv_data) {
582 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
583 ble_dev_event->adv_len = adv_info.len;
585 ble_dev_event->adv_len = 0;
587 ble_dev_event->device_info = dev_info;
589 event_data = ble_dev_event;
590 size = sizeof(event_ble_dev_found_t);
591 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
593 /* BREDR device, so No Adv data */
594 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
596 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
597 event_data = dev_event;
598 size = sizeof(remote_device_t);
599 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
602 send_event(event, event_data, size);