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 static void cb_adapter_properties (bt_status_t status,
242 bt_property_t *properties)
246 BT_DBG("status: %d, count: %d", status, num_properties);
248 if (status != BT_STATUS_SUCCESS) {
249 if (num_properties == 1) {
250 BT_ERR("Adapter Prop failed: status: [%s], count: %d, prop: %d",
251 status2string(status), num_properties, properties[num_properties-1].type);
253 BT_ERR("Adapter Prop failed: status: [%s], count: %d", status2string(status), num_properties);
258 for (i = 0; i < num_properties; i++) {
259 BT_DBG("prop type %d, len %d", properties[i].type, properties[i].len);
260 switch (properties[i].type) {
261 case BT_PROPERTY_VERSION: {
262 g_strlcpy(local_version, properties[i].val, BT_VERSION_STR_LEN_MAX);
263 local_version[properties[i].len] = '\0';
265 BT_DBG("Version: %s", local_version);
266 /* Send event to application */
267 if (num_properties == 1) {
268 char *adapter_ver = g_strdup(local_version);
270 /* Application has requested this property SET/GET hence send EVENT */
271 send_event(OAL_EVENT_ADAPTER_PROPERTY_VERSION, adapter_ver, strlen(adapter_ver));
275 case BT_PROPERTY_BDNAME: {
276 g_strlcpy(local_name, properties[i].val, BT_DEVICE_NAME_LENGTH_MAX);
277 local_name[properties[i].len] = '\0';
279 BT_DBG("Name: %s", local_name);
280 /* Send event to application */
281 if (num_properties == 1) {
282 char * adap_name = g_strdup(local_name);
284 /* Application has requested this property SET/GET hence send EVENT */
285 send_event(OAL_EVENT_ADAPTER_PROPERTY_NAME, adap_name, strlen(adap_name));
289 case BT_PROPERTY_BDADDR: {
292 addr = properties[i].val;
293 memcpy(local_address.addr, addr->address, 6);
294 if (num_properties == 1) {
295 /* Application has requested this property SET/GET hence send EVENT */
296 send_event(OAL_EVENT_ADAPTER_PROPERTY_ADDRESS,
297 g_memdup(&local_address, sizeof(local_address)),
298 sizeof(local_address));
302 case BT_PROPERTY_UUIDS: {
305 num_uuid = properties[i].len/sizeof(bt_uuid_t);
307 BT_DBG("num_uuid: %d", num_uuid);
309 /* Send event to application */
310 if (num_properties == 1) {
311 event_adapter_services_t *uuids_event;
313 uuids_event = g_malloc(sizeof(event_adapter_services_t) + properties[i].len);
314 memcpy(uuids_event->service_list, properties[i].val, properties[i].len);
315 uuids_event->num = num_uuid;
317 /* Application has requested this property SET/GET hence send EVENT */
318 send_event(OAL_EVENT_ADAPTER_PROPERTY_SERVICES,
319 uuids_event, (num_uuid * sizeof(bt_uuid_t)));
323 case BT_PROPERTY_ADAPTER_SCAN_MODE: {
324 bt_scan_mode_t cur_mode = *((bt_scan_mode_t *)properties[i].val);
326 BT_INFO("Scan mode (%d)", cur_mode);
328 scan_mode = cur_mode;
330 /* Send event to application */
331 if (num_properties == 1) {
332 oal_event_t event = OAL_EVENT_ADAPTER_MODE_NON_CONNECTABLE;
334 if (BT_SCAN_MODE_CONNECTABLE == cur_mode)
335 event = OAL_EVENT_ADAPTER_MODE_CONNECTABLE;
336 else if (BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE == cur_mode)
337 event = OAL_EVENT_ADAPTER_MODE_DISCOVERABLE;
339 /* Application has requested this property SET/GET hence send EVENT */
340 send_event(event, NULL, 0);
344 case BT_PROPERTY_ADAPTER_DISCOVERY_TIMEOUT: {
347 timeout = *((uint32_t*)properties[i].val);
349 BT_INFO("Discoverability timeout: %d", timeout);
350 discoverable_timeout = timeout;
352 send_event(OAL_EVENT_ADAPTER_MODE_DISCOVERABLE_TIMEOUT,
353 g_memdup(properties[i].val, sizeof(uint32_t)),
357 case BT_PROPERTY_ADAPTER_BONDED_DEVICES: {
360 bt_bdaddr_t *bonded_addr_list;
361 event_device_list_t *event_data;
363 num_bonded = properties[i].len/sizeof(bt_bdaddr_t);
364 BT_DBG("num_bonded %d", num_bonded);
366 if (num_properties > 1) /* No explicit req for this prop, ignore */
369 bonded_addr_list = properties[i].val;
370 event_data = g_malloc(sizeof(event_device_list_t) + num_bonded*sizeof(bt_address_t));
371 event_data->num = num_bonded;
373 for (j = 0; j < num_bonded; j++)
374 memcpy(event_data->devices[j].addr, bonded_addr_list[j].address, 6);
376 send_event(OAL_EVENT_ADAPTER_BONDED_DEVICE_LIST,
377 event_data, (num_bonded * sizeof(bt_bdaddr_t)));
381 BT_WARN("Unhandled property: %d", properties[i].type);