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"
34 #define CHECK_MAX(max, x) (((max) > (x)) ? (x) : (max))
36 static const bt_interface_t * blued_api;
38 static bt_address_t local_address;
39 static char local_name[BT_DEVICE_NAME_LENGTH_MAX + 1] = {'O', 'A', 'L', 0};
40 static char local_version[BT_VERSION_STR_LEN_MAX + 1];
41 static bt_scan_mode_t scan_mode = BT_SCAN_MODE_NONE;
42 static int discoverable_timeout = 0;
44 /* Forward declarations */
45 const char * status2string(bt_status_t status);
46 oal_status_t convert_to_oal_status(bt_status_t status);
47 void parse_device_properties(int num_properties, bt_property_t *properties,
48 remote_device_t *dev_info, ble_adv_data_t * adv_info);
49 static gboolean retry_enable_adapter(gpointer data);
50 oal_status_t oal_mgr_init_internal(void);
53 /* Callback registered with Stack */
54 static void cb_adapter_state_change(bt_state_t status);
55 static void cb_adapter_properties (bt_status_t status,
56 int num_properties, bt_property_t *properties);
58 static bt_callbacks_t callbacks = {
60 cb_adapter_state_change,
61 cb_adapter_properties,
62 NULL, /* remote_device_properties_callback */
63 NULL, /* device_found_callback */
64 NULL, /* discovery_state_changed_callback */
65 NULL, /* pin_request_callback */
66 NULL, /* ssp_request_callback */
67 NULL, /* bond_state_changed_callback */
68 NULL, /* acl_state_changed_callback */
69 NULL, /* callback_thread_event */
70 NULL, /* dut_mode_recv_callback */
71 NULL, /* le_test_mode_callback*/
72 NULL, /* energy_info_callback */
75 oal_status_t adapter_mgr_init(const bt_interface_t * stack_if)
80 ret = blued_api->init(&callbacks);
82 if (ret != BT_STATUS_SUCCESS) {
83 BT_ERR("Adapter callback registration failed: [%s]", status2string(ret));
85 return convert_to_oal_status(ret);
88 return OAL_STATUS_SUCCESS;
91 const bt_interface_t* adapter_get_stack_interface(void)
96 void adapter_mgr_cleanup(void)
98 /* Nothing to clean yet , do not set blued_api NULL as it will be used to clean Bluedroid states */
102 oal_status_t adapter_enable(void)
104 int ret = BT_STATUS_SUCCESS;
107 CHECK_OAL_INITIALIZED();
108 if (OAL_STATUS_SUCCESS != hw_is_module_ready()) {
109 g_timeout_add(200, retry_enable_adapter, NULL);
110 return OAL_STATUS_PENDING;
113 ret = blued_api->enable();
115 if (ret != BT_STATUS_SUCCESS) {
116 BT_ERR("Enable failed: [%s]", status2string(ret));
117 return convert_to_oal_status(ret);
120 return OAL_STATUS_SUCCESS;
123 oal_status_t adapter_disable(void)
129 CHECK_OAL_INITIALIZED();
131 ret = blued_api->disable();
133 if (ret != BT_STATUS_SUCCESS) {
134 BT_ERR("Disable failed: [%s]", status2string(ret));
135 return convert_to_oal_status(ret);
137 return OAL_STATUS_SUCCESS;
140 /* Callbacks from Stack */
141 static void cb_adapter_state_change(bt_state_t status)
146 event = (BT_STATE_ON == status)?OAL_EVENT_ADAPTER_ENABLED:OAL_EVENT_ADAPTER_DISABLED;
148 send_event(event, NULL, 0);
151 static gboolean retry_enable_adapter(gpointer data)
157 oal_status_t adapter_get_address(void)
161 CHECK_OAL_INITIALIZED();
165 ret = blued_api->get_adapter_property(BT_PROPERTY_BDADDR);
166 if (ret != BT_STATUS_SUCCESS) {
167 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
168 return convert_to_oal_status(ret);
171 return OAL_STATUS_SUCCESS;
174 oal_status_t adapter_get_version(void)
178 CHECK_OAL_INITIALIZED();
182 ret = blued_api->get_adapter_property(BT_PROPERTY_VERSION);
183 if (ret != BT_STATUS_SUCCESS) {
184 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
185 return convert_to_oal_status(ret);
188 return OAL_STATUS_SUCCESS;
191 oal_status_t adapter_get_name(void)
195 CHECK_OAL_INITIALIZED();
199 ret = blued_api->get_adapter_property(BT_PROPERTY_BDNAME);
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_is_discoverable(int *p_discoverable)
210 OAL_CHECK_PARAMETER(p_discoverable, return);
212 *p_discoverable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
214 API_TRACE("%d", *p_discoverable);
216 return OAL_STATUS_SUCCESS;
219 oal_status_t adapter_is_connectable(int *p_connectable)
221 OAL_CHECK_PARAMETER(p_connectable, return);
223 *p_connectable = (scan_mode == BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE)
224 ||(scan_mode == BT_SCAN_MODE_CONNECTABLE);
226 API_TRACE("%d", *p_connectable);
228 return OAL_STATUS_SUCCESS;
231 oal_status_t adapter_get_discoverable_timeout(int *p_timeout)
233 API_TRACE("%d", discoverable_timeout);
235 *p_timeout = discoverable_timeout;
237 return OAL_STATUS_SUCCESS;
240 oal_status_t adapter_get_service_uuids(void)
244 CHECK_OAL_INITIALIZED();
248 ret = blued_api->get_adapter_property(BT_PROPERTY_UUIDS);
249 if (ret != BT_STATUS_SUCCESS) {
250 BT_ERR("get_adapter_property failed: [%s]", status2string(ret));
251 return convert_to_oal_status(ret);
254 return OAL_STATUS_SUCCESS;
257 static void cb_adapter_properties (bt_status_t status,
259 bt_property_t *properties)
263 BT_DBG("status: %d, count: %d", status, num_properties);
265 if (status != BT_STATUS_SUCCESS) {
266 if (num_properties == 1) {
267 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
268 status2string(status), num_properties, properties[num_properties-1].type);
270 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
275 for (i = 0; i < num_properties; i++) {
276 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
277 switch (properties[i].type) {
278 case BT_PROPERTY_VERSION: {
279 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
280 local_version[properties[i].len] = '\0';
282 BT_DBG("Version: %s", local_version);
283 /* Send event to application */
284 if (num_properties == 1) {
285 char *adapter_ver = g_strdup(local_version);
287 /* Application has requested this property SET/GET hence send EVENT */
288 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
292 case BT_PROPERTY_BDNAME: {
293 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
294 local_name[properties[i].len] = '\0';
296 BT_DBG("Name: %s", local_name);
297 /* Send event to application */
298 if (num_properties == 1) {
299 char * adap_name = g_strdup(local_name);
301 /* Application has requested this property SET/GET hence send EVENT */
302 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
306 case BT_PROPERTY_BDADDR: {
309 addr = properties[i].val;
310 memcpy(local_address.addr, addr->address, 6);
311 if (num_properties == 1) {
312 /* Application has requested this property SET/GET hence send EVENT */
313 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
314 g_memdup(&local_address, sizeof(local_address)),
315 sizeof(local_address));
319 case BT_PROPERTY_UUIDS: {
322 num_uuid = properties[i].len/sizeof(bt_uuid_t);
324 BT_DBG("num_uuid: %d", num_uuid);
326 /* Send event to application */
327 if (num_properties == 1) {
328 event_adapter_services_t *uuids_event;
330 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
331 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
332 uuids_event->num = num_uuid;
334 /* Application has requested this property SET/GET hence send EVENT */
335 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
336 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
340 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
341 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
343 BT_INFO("Scan mode (%d)", cur_mode);
345 scan_mode = cur_mode;
347 /* Send event to application */
348 if (num_properties == 1) {
349 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
351 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
352 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
353 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
354 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
356 /* Application has requested this property SET/GET hence send EVENT */
357 send_event(event, NULL, 0);
361 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
364 timeout = *((uint32_t*)properties[i].val);
366 BT_INFO("Discoverability timeout: %d", timeout);
367 discoverable_timeout = timeout;
369 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
370 g_memdup(properties[i].val, sizeof(uint32_t)),
374 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
377 bt_bdaddr_t *bonded_addr_list;
378 event_device_list_t *event_data;
380 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
381 BT_DBG("num_bonded %d", num_bonded);
383 if (num_properties > 1) /* No explicit req for this prop, ignore */
386 bonded_addr_list = properties[i].val;
387 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
388 event_data->num = num_bonded;
390 for (j = 0; j < num_bonded; j++)
391 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
393 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
394 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
398 BT_WARN("Unhandled property: %d", properties[i].type);