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 static void cb_adapter_properties(bt_status_t status,
370 bt_property_t *properties)
374 BT_DBG("status: %d, count: %d", status, num_properties);
376 if (status != BT_STATUS_SUCCESS) {
377 if (num_properties == 1) {
378 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
379 status2string(status), num_properties, properties[num_properties-1].type);
381 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
386 for (i = 0; i < num_properties; i++) {
387 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
388 switch (properties[i].type) {
389 case BT_PROPERTY_VERSION: {
390 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
391 local_version[properties[i].len] = '\0';
393 BT_DBG("Version: %s", local_version);
394 /* Send event to application */
395 if (num_properties == 1) {
396 char *adapter_ver = g_strdup(local_version);
398 /* Application has requested this property SET/GET hence send EVENT */
399 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
403 case BT_PROPERTY_BDNAME: {
404 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
405 local_name[properties[i].len] = '\0';
407 BT_DBG("Name: %s", local_name);
408 /* Send event to application */
409 if (num_properties == 1) {
410 char * adap_name = g_strdup(local_name);
412 /* Application has requested this property SET/GET hence send EVENT */
413 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
417 case BT_PROPERTY_BDADDR: {
420 addr = properties[i].val;
421 memcpy(local_address.addr, addr->address, 6);
422 if (num_properties == 1) {
423 /* Application has requested this property SET/GET hence send EVENT */
424 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
425 g_memdup(&local_address, sizeof(local_address)),
426 sizeof(local_address));
430 case BT_PROPERTY_UUIDS: {
433 num_uuid = properties[i].len/sizeof(bt_uuid_t);
435 BT_DBG("num_uuid: %d", num_uuid);
437 /* Send event to application */
438 if (num_properties == 1) {
439 event_adapter_services_t *uuids_event;
441 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
442 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
443 uuids_event->num = num_uuid;
445 /* Application has requested this property SET/GET hence send EVENT */
446 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
447 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
451 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
452 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
454 BT_INFO("Scan mode (%d)", cur_mode);
456 scan_mode = cur_mode;
458 /* Send event to application */
459 if (num_properties == 1) {
460 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
462 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
463 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
464 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
465 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
467 /* Application has requested this property SET/GET hence send EVENT */
468 send_event(event, NULL, 0);
472 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
475 timeout = *((uint32_t*)properties[i].val);
477 BT_INFO("Discoverability timeout: %d", timeout);
478 discoverable_timeout = timeout;
480 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
481 g_memdup(properties[i].val, sizeof(uint32_t)),
485 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
488 bt_bdaddr_t *bonded_addr_list;
489 event_device_list_t *event_data;
491 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
492 BT_DBG("num_bonded %d", num_bonded);
494 if (num_properties > 1) /* No explicit req for this prop, ignore */
497 bonded_addr_list = properties[i].val;
498 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
499 event_data->num = num_bonded;
501 for (j = 0; j < num_bonded; j++)
502 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
504 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
505 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
509 BT_WARN("Unhandled property: %d", properties[i].type);
515 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
519 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
522 send_event(event, NULL, 0);
525 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
527 remote_device_t dev_info;
528 ble_adv_data_t adv_info;
534 if (num_properties == 0) {
535 BT_ERR("Unexpected, properties count is zero!!");
539 memset(&dev_info, 0x00, sizeof(remote_device_t));
540 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
542 print_bt_properties(num_properties, properties);
543 parse_device_properties(num_properties, properties, &dev_info, &adv_info);
545 BT_INFO("number of properties= [%d] ", num_properties, size);
547 if (dev_info.type != DEV_TYPE_BREDR) {
548 /* BLE Single or DUAL mode found, so it should have Adv data */
549 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
551 ble_dev_event->adv_len = adv_info.len;
553 if (adv_info.len > 0 && adv_info.adv_data) {
554 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
555 ble_dev_event->adv_len = adv_info.len;
557 ble_dev_event->adv_len = 0;
559 ble_dev_event->device_info = dev_info;
561 event_data = ble_dev_event;
562 size = sizeof(event_ble_dev_found_t);
563 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
565 /* BREDR device, so No Adv data */
566 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
568 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
569 event_data = dev_event;
570 size = sizeof(remote_device_t);
571 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
574 send_event(event, event_data, size);