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);
65 static bt_callbacks_t callbacks = {
67 cb_adapter_state_change,
68 cb_adapter_properties,
70 cb_adapter_device_found,
71 cb_adapter_discovery_state_changed,
72 NULL, /* pin_request_callback */
73 NULL, /* ssp_request_callback */
74 cb_device_bond_state_changed,
75 cb_device_acl_state_changed,
76 NULL, /* callback_thread_event */
77 NULL, /* dut_mode_recv_callback */
78 NULL, /* le_test_mode_callback*/
79 NULL, /* energy_info_callback */
82 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
87 ret = blued_api->init(&callbacks);
89 if (ret != BT_STATUS_SUCCESS) {
90 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
92 return convert_to_oal_status(ret);
95 return OAL_STATUS_SUCCESS;
98 const bt_interface_t* adapter_get_stack_interface(void)
103 void adapter_mgr_cleanup(void)
105 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
109 oal_status_t adapter_enable(void)
111 int ret = BT_STATUS_SUCCESS;
114 CHECK_OAL_INITIALIZED();
115 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
116 g_timeout_add(200, retry_enable_adapter, NULL);
117 return OAL_STATUS_PENDING;
120 ret = blued_api->enable();
122 if (ret != BT_STATUS_SUCCESS) {
123 BT_ERR("Enable failed: [%s]", status2string(ret));
124 return convert_to_oal_status(ret);
127 return OAL_STATUS_SUCCESS;
130 oal_status_t adapter_disable(void)
136 CHECK_OAL_INITIALIZED();
138 ret = blued_api->disable();
140 if (ret != BT_STATUS_SUCCESS) {
141 BT_ERR("Disable failed: [%s]", status2string(ret));
142 return convert_to_oal_status(ret);
144 return OAL_STATUS_SUCCESS;
147 oal_status_t adapter_start_inquiry(void)
153 CHECK_OAL_INITIALIZED();
155 ret = blued_api->start_discovery();
156 if (ret != BT_STATUS_SUCCESS) {
157 BT_ERR("start_discovery failed: [%s]", status2string(ret));
158 return convert_to_oal_status(ret);
161 return OAL_STATUS_SUCCESS;
164 oal_status_t adapter_stop_inquiry(void)
170 CHECK_OAL_INITIALIZED();
172 ret = blued_api->cancel_discovery();
173 if (ret != BT_STATUS_SUCCESS) {
174 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
175 return convert_to_oal_status(ret);
178 return OAL_STATUS_SUCCESS;
181 /* Callbacks from Stack */
182 static void cb_adapter_state_change(bt_state_t status)
187 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
189 send_event(event, NULL, 0);
192 static gboolean retry_enable_adapter(gpointer data)
198 oal_status_t adapter_get_address(void)
203 CHECK_OAL_INITIALIZED();
205 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
206 if (ret != BT_STATUS_SUCCESS) {
207 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
208 return convert_to_oal_status(ret);
211 return OAL_STATUS_SUCCESS;
214 oal_status_t adapter_get_version(void)
219 CHECK_OAL_INITIALIZED();
221 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
222 if (ret != BT_STATUS_SUCCESS) {
223 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
224 return convert_to_oal_status(ret);
227 return OAL_STATUS_SUCCESS;
230 oal_status_t adapter_get_name(void)
234 CHECK_OAL_INITIALIZED();
238 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
239 if (ret != BT_STATUS_SUCCESS) {
240 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
241 return convert_to_oal_status(ret);
244 return OAL_STATUS_SUCCESS;
247 oal_status_t adapter_set_name(char * name)
252 CHECK_OAL_INITIALIZED();
254 OAL_CHECK_PARAMETER(name, return);
255 API_TRACE("Name: %s", name);
257 prop.type = BT_PROPERTY_BDNAME;
258 prop.len = strlen(name);
261 ret = blued_api->set_adapter_property(&prop);
262 if (ret != BT_STATUS_SUCCESS) {
263 BT_ERR("set_adapter_property: [%s]", status2string(ret));
264 ret = OAL_STATUS_INTERNAL_ERROR;
266 ret = OAL_STATUS_SUCCESS;
271 oal_status_t adapter_is_discoverable(int *p_discoverable)
273 OAL_CHECK_PARAMETER(p_discoverable, return);
275 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
277 API_TRACE("%d", *p_discoverable);
279 return OAL_STATUS_SUCCESS;
282 oal_status_t adapter_is_connectable(int *p_connectable)
284 OAL_CHECK_PARAMETER(p_connectable, return);
286 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
287 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
289 API_TRACE("%d", *p_connectable);
291 return OAL_STATUS_SUCCESS;
294 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
296 API_TRACE("%d", discoverable_timeout);
298 *p_timeout = discoverable_timeout;
300 return OAL_STATUS_SUCCESS;
303 oal_status_t adapter_get_service_uuids(void)
307 CHECK_OAL_INITIALIZED();
311 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
312 if (ret != BT_STATUS_SUCCESS) {
313 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
314 return convert_to_oal_status(ret);
317 return OAL_STATUS_SUCCESS;
320 oal_status_t adapter_get_bonded_devices(void)
324 CHECK_OAL_INITIALIZED();
328 ret = blued_api->get_adapter_property(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
329 if (ret != BT_STATUS_SUCCESS) {
330 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
331 return convert_to_oal_status(ret);
334 return OAL_STATUS_SUCCESS;
337 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
344 CHECK_OAL_INITIALIZED();
346 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
347 prop.len = sizeof(bt_scan_mode_t);
349 res = blued_api->set_adapter_property(&prop);
350 if (res != BT_STATUS_SUCCESS) {
351 BT_ERR("set scan mode failed [%s]", status2string(res));
352 return convert_to_oal_status(res);
356 return OAL_STATUS_SUCCESS;
359 oal_status_t adapter_set_connectable(int connectable)
363 API_TRACE("%d", connectable);
365 CHECK_OAL_INITIALIZED();
367 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
369 return set_scan_mode(mode);
372 oal_status_t adapter_set_discoverable(void)
374 CHECK_OAL_INITIALIZED();
377 return set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
380 oal_status_t adapter_set_discoverable_timeout(int timeout)
384 uint32_t prop_val = timeout;
386 CHECK_OAL_INITIALIZED();
387 API_TRACE("%d", timeout);
389 prop.type = BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT;
390 prop.len = sizeof(prop_val);
391 prop.val = &prop_val;
392 res = blued_api->set_adapter_property(&prop);
393 if (res != BT_STATUS_SUCCESS) {
394 BT_ERR("set_adapter_property failed [%s]", status2string(res));
395 return convert_to_oal_status(res);
397 return OAL_STATUS_SUCCESS;
400 static void cb_adapter_properties(bt_status_t status,
402 bt_property_t *properties)
406 BT_DBG("status: %d, count: %d", status, num_properties);
408 if (status != BT_STATUS_SUCCESS) {
409 if (num_properties == 1) {
410 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
411 status2string(status), num_properties, properties[num_properties-1].type);
413 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
418 for (i = 0; i < num_properties; i++) {
419 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
420 switch (properties[i].type) {
421 case BT_PROPERTY_VERSION: {
422 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
423 local_version[properties[i].len] = '\0';
425 BT_DBG("Version: %s", local_version);
426 /* Send event to application */
427 if (num_properties == 1) {
428 char *adapter_ver = g_strdup(local_version);
430 /* Application has requested this property SET/GET hence send EVENT */
431 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
435 case BT_PROPERTY_BDNAME: {
436 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
437 local_name[properties[i].len] = '\0';
439 BT_DBG("Name: %s", local_name);
440 /* Send event to application */
441 if (num_properties == 1) {
442 char * adap_name = g_strdup(local_name);
444 /* Application has requested this property SET/GET hence send EVENT */
445 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
449 case BT_PROPERTY_BDADDR: {
452 addr = properties[i].val;
453 memcpy(local_address.addr, addr->address, 6);
454 if (num_properties == 1) {
455 /* Application has requested this property SET/GET hence send EVENT */
456 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
457 g_memdup(&local_address, sizeof(local_address)),
458 sizeof(local_address));
462 case BT_PROPERTY_UUIDS: {
465 num_uuid = properties[i].len/sizeof(bt_uuid_t);
467 BT_DBG("num_uuid: %d", num_uuid);
469 /* Send event to application */
470 if (num_properties == 1) {
471 event_adapter_services_t *uuids_event;
473 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
474 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
475 uuids_event->num = num_uuid;
477 /* Application has requested this property SET/GET hence send EVENT */
478 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
479 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
483 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
484 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
486 BT_INFO("Scan mode (%d)", cur_mode);
488 scan_mode = cur_mode;
490 /* Send event to application */
491 if (num_properties == 1) {
492 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
494 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
495 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
496 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
497 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
499 /* Application has requested this property SET/GET hence send EVENT */
500 send_event(event, NULL, 0);
504 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
507 timeout = *((uint32_t*)properties[i].val);
509 BT_INFO("Discoverability timeout: %d", timeout);
510 discoverable_timeout = timeout;
512 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
513 g_memdup(properties[i].val, sizeof(uint32_t)),
517 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
520 bt_bdaddr_t *bonded_addr_list;
521 event_device_list_t *event_data;
523 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
524 BT_DBG("num_bonded %d", num_bonded);
526 if (num_properties > 1) /* No explicit req for this prop, ignore */
529 bonded_addr_list = properties[i].val;
530 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
531 event_data->num = num_bonded;
533 for (j = 0; j < num_bonded; j++)
534 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
536 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
537 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
541 BT_WARN("Unhandled property: %d", properties[i].type);
547 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
551 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
554 send_event(event, NULL, 0);
557 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
559 remote_device_t dev_info;
560 ble_adv_data_t adv_info;
566 if (num_properties == 0) {
567 BT_ERR("Unexpected, properties count is zero!!");
571 memset(&dev_info, 0x00, sizeof(remote_device_t));
572 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
574 print_bt_properties(num_properties, properties);
575 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
577 BT_INFO("number of properties= [%d] ", num_properties, size);
579 if (dev_info.type != DEV_TYPE_BREDR) {
580 /* BLE Single or DUAL mode found, so it should have Adv data */
581 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
583 ble_dev_event->adv_len = adv_info.len;
585 if (adv_info.len > 0 && adv_info.adv_data) {
586 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
587 ble_dev_event->adv_len = adv_info.len;
589 ble_dev_event->adv_len = 0;
591 ble_dev_event->device_info = dev_info;
593 event_data = ble_dev_event;
594 size = sizeof(event_ble_dev_found_t);
595 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
597 /* BREDR device, so No Adv data */
598 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
600 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
601 event_data = dev_event;
602 size = sizeof(remote_device_t);
603 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
606 send_event(event, event_data, size);