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);
59 static bt_callbacks_t callbacks = {
61 cb_adapter_state_change,
62 cb_adapter_properties,
63 NULL, /* remote_device_properties_callback */
64 cb_adapter_device_found,
65 cb_adapter_discovery_state_changed,
66 NULL, /* pin_request_callback */
67 NULL, /* ssp_request_callback */
68 NULL, /* bond_state_changed_callback */
69 NULL, /* acl_state_changed_callback */
70 NULL, /* callback_thread_event */
71 NULL, /* dut_mode_recv_callback */
72 NULL, /* le_test_mode_callback*/
73 NULL, /* energy_info_callback */
76 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
81 ret = blued_api->init(&callbacks);
83 if (ret != BT_STATUS_SUCCESS) {
84 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
86 return convert_to_oal_status(ret);
89 return OAL_STATUS_SUCCESS;
92 const bt_interface_t* adapter_get_stack_interface(void)
97 void adapter_mgr_cleanup(void)
99 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
103 oal_status_t adapter_enable(void)
105 int ret = BT_STATUS_SUCCESS;
108 CHECK_OAL_INITIALIZED();
109 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
110 g_timeout_add(200, retry_enable_adapter, NULL);
111 return OAL_STATUS_PENDING;
114 ret = blued_api->enable();
116 if (ret != BT_STATUS_SUCCESS) {
117 BT_ERR("Enable failed: [%s]", status2string(ret));
118 return convert_to_oal_status(ret);
121 return OAL_STATUS_SUCCESS;
124 oal_status_t adapter_disable(void)
130 CHECK_OAL_INITIALIZED();
132 ret = blued_api->disable();
134 if (ret != BT_STATUS_SUCCESS) {
135 BT_ERR("Disable failed: [%s]", status2string(ret));
136 return convert_to_oal_status(ret);
138 return OAL_STATUS_SUCCESS;
141 oal_status_t adapter_start_inquiry(void)
147 CHECK_OAL_INITIALIZED();
149 ret = blued_api->start_discovery();
150 if (ret != BT_STATUS_SUCCESS) {
151 BT_ERR("start_discovery failed: [%s]", status2string(ret));
152 return convert_to_oal_status(ret);
155 return OAL_STATUS_SUCCESS;
158 oal_status_t adapter_stop_inquiry(void)
164 CHECK_OAL_INITIALIZED();
166 ret = blued_api->cancel_discovery();
167 if (ret != BT_STATUS_SUCCESS) {
168 BT_ERR("cancel_discovery failed: [%s]", status2string(ret));
169 return convert_to_oal_status(ret);
172 return OAL_STATUS_SUCCESS;
175 /* Callbacks from Stack */
176 static void cb_adapter_state_change(bt_state_t status)
181 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
183 send_event(event, NULL, 0);
186 static gboolean retry_enable_adapter(gpointer data)
192 oal_status_t adapter_get_address(void)
197 CHECK_OAL_INITIALIZED();
199 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
200 if (ret != BT_STATUS_SUCCESS) {
201 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
202 return convert_to_oal_status(ret);
205 return OAL_STATUS_SUCCESS;
208 oal_status_t adapter_get_version(void)
213 CHECK_OAL_INITIALIZED();
215 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
216 if (ret != BT_STATUS_SUCCESS) {
217 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
218 return convert_to_oal_status(ret);
221 return OAL_STATUS_SUCCESS;
224 oal_status_t adapter_get_name(void)
228 CHECK_OAL_INITIALIZED();
232 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
233 if (ret != BT_STATUS_SUCCESS) {
234 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
235 return convert_to_oal_status(ret);
238 return OAL_STATUS_SUCCESS;
241 oal_status_t adapter_set_name(char * name)
246 CHECK_OAL_INITIALIZED();
248 OAL_CHECK_PARAMETER(name, return);
249 API_TRACE("Name: %s", name);
251 prop.type = BT_PROPERTY_BDNAME;
252 prop.len = strlen(name);
255 ret = blued_api->set_adapter_property(&prop);
256 if (ret != BT_STATUS_SUCCESS) {
257 BT_ERR("set_adapter_property: [%s]", status2string(ret));
258 ret = OAL_STATUS_INTERNAL_ERROR;
260 ret = OAL_STATUS_SUCCESS;
265 oal_status_t adapter_is_discoverable(int *p_discoverable)
267 OAL_CHECK_PARAMETER(p_discoverable, return);
269 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
271 API_TRACE("%d", *p_discoverable);
273 return OAL_STATUS_SUCCESS;
276 oal_status_t adapter_is_connectable(int *p_connectable)
278 OAL_CHECK_PARAMETER(p_connectable, return);
280 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
281 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
283 API_TRACE("%d", *p_connectable);
285 return OAL_STATUS_SUCCESS;
288 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
290 API_TRACE("%d", discoverable_timeout);
292 *p_timeout = discoverable_timeout;
294 return OAL_STATUS_SUCCESS;
297 oal_status_t adapter_get_service_uuids(void)
301 CHECK_OAL_INITIALIZED();
305 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
306 if (ret != BT_STATUS_SUCCESS) {
307 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
308 return convert_to_oal_status(ret);
311 return OAL_STATUS_SUCCESS;
314 static oal_status_t set_scan_mode(bt_scan_mode_t mode)
321 CHECK_OAL_INITIALIZED();
323 prop.type = BT_PROPERTY_ADAPTER_SCAN_MODE;
324 prop.len = sizeof(bt_scan_mode_t);
326 res = blued_api->set_adapter_property(&prop);
327 if (res != BT_STATUS_SUCCESS) {
328 BT_ERR("set scan mode failed [%s]", status2string(res));
329 return convert_to_oal_status(res);
333 return OAL_STATUS_SUCCESS;
336 oal_status_t adapter_set_connectable(int connectable)
340 API_TRACE("%d", connectable);
342 CHECK_OAL_INITIALIZED();
344 mode = connectable ? BT_SCAN_MODE_CONNECTABLE : BT_SCAN_MODE_NONE;
346 return set_scan_mode(mode);
349 static void cb_adapter_properties(bt_status_t status,
351 bt_property_t *properties)
355 BT_DBG("status: %d, count: %d", status, num_properties);
357 if (status != BT_STATUS_SUCCESS) {
358 if (num_properties == 1) {
359 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
360 status2string(status), num_properties, properties[num_properties-1].type);
362 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
367 for (i = 0; i < num_properties; i++) {
368 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
369 switch (properties[i].type) {
370 case BT_PROPERTY_VERSION: {
371 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
372 local_version[properties[i].len] = '\0';
374 BT_DBG("Version: %s", local_version);
375 /* Send event to application */
376 if (num_properties == 1) {
377 char *adapter_ver = g_strdup(local_version);
379 /* Application has requested this property SET/GET hence send EVENT */
380 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
384 case BT_PROPERTY_BDNAME: {
385 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
386 local_name[properties[i].len] = '\0';
388 BT_DBG("Name: %s", local_name);
389 /* Send event to application */
390 if (num_properties == 1) {
391 char * adap_name = g_strdup(local_name);
393 /* Application has requested this property SET/GET hence send EVENT */
394 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
398 case BT_PROPERTY_BDADDR: {
401 addr = properties[i].val;
402 memcpy(local_address.addr, addr->address, 6);
403 if (num_properties == 1) {
404 /* Application has requested this property SET/GET hence send EVENT */
405 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
406 g_memdup(&local_address, sizeof(local_address)),
407 sizeof(local_address));
411 case BT_PROPERTY_UUIDS: {
414 num_uuid = properties[i].len/sizeof(bt_uuid_t);
416 BT_DBG("num_uuid: %d", num_uuid);
418 /* Send event to application */
419 if (num_properties == 1) {
420 event_adapter_services_t *uuids_event;
422 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
423 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
424 uuids_event->num = num_uuid;
426 /* Application has requested this property SET/GET hence send EVENT */
427 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
428 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
432 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
433 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
435 BT_INFO("Scan mode (%d)", cur_mode);
437 scan_mode = cur_mode;
439 /* Send event to application */
440 if (num_properties == 1) {
441 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
443 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
444 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
445 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
446 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
448 /* Application has requested this property SET/GET hence send EVENT */
449 send_event(event, NULL, 0);
453 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
456 timeout = *((uint32_t*)properties[i].val);
458 BT_INFO("Discoverability timeout: %d", timeout);
459 discoverable_timeout = timeout;
461 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
462 g_memdup(properties[i].val, sizeof(uint32_t)),
466 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
469 bt_bdaddr_t *bonded_addr_list;
470 event_device_list_t *event_data;
472 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
473 BT_DBG("num_bonded %d", num_bonded);
475 if (num_properties > 1) /* No explicit req for this prop, ignore */
478 bonded_addr_list = properties[i].val;
479 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
480 event_data->num = num_bonded;
482 for (j = 0; j < num_bonded; j++)
483 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
485 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
486 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
490 BT_WARN("Unhandled property: %d", properties[i].type);
496 static void cb_adapter_discovery_state_changed(bt_discovery_state_t state)
500 event = (BT_DISCOVERY_STARTED == state)?OAL_EVENT_ADAPTER_INQUIRY_STARTED:OAL_EVENT_ADAPTER_INQUIRY_FINISHED;
503 send_event(event, NULL, 0);
506 static void cb_adapter_device_found(int num_properties, bt_property_t *properties)
508 remote_device_t dev_info;
509 ble_adv_data_t adv_info;
512 gsize properties_size = 0;
515 if (num_properties == 0) {
516 BT_ERR("Unexpected, properties count is zero!!");
520 memset(&dev_info, 0x00, sizeof(remote_device_t));
521 memset(&adv_info, 0x00, sizeof(ble_adv_data_t));
523 print_bt_properties(num_properties, properties);
524 parse_device_properties(num_properties, properties, &dev_info, &adv_info, &properties_size);
526 BT_INFO("number of properties= [%d] total size [%u]", num_properties, properties_size);
528 if (dev_info.type != DEV_TYPE_BREDR) {
529 /* BLE Single or DUAL mode found, so it should have Adv data */
530 event_ble_dev_found_t * ble_dev_event = g_new0(event_ble_dev_found_t, 1);
532 ble_dev_event->adv_len = adv_info.len;
534 if (adv_info.len > 0 && adv_info.adv_data) {
535 memcpy(ble_dev_event->adv_data, adv_info.adv_data, adv_info.len);
536 ble_dev_event->adv_len = adv_info.len;
538 ble_dev_event->adv_len = 0;
540 ble_dev_event->device_info = dev_info;
542 event_data = ble_dev_event;
543 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BLE;
545 /* BREDR device, so No Adv data */
546 event_dev_found_t * dev_event = g_new0(event_dev_found_t, 1);
548 memcpy(dev_event, &dev_info, sizeof(remote_device_t));
549 event_data = dev_event;
550 event = OAL_EVENT_ADAPTER_INQUIRY_RESULT_BREDR_ONLY;
553 send_event(event, event_data, properties_size);