1 /******************************************************************************
3 * Copyright (C) 2003-2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /******************************************************************************
21 * This is the main implementation file for the BTA system manager.
23 ******************************************************************************/
25 #define LOG_TAG "bt_bta_sys_main"
27 #include <base/bind.h>
28 #include <base/logging.h>
29 #include <base/threading/thread.h>
33 #include "bt_common.h"
36 #include "bta_sys_int.h"
39 #include "osi/include/alarm.h"
40 #include "osi/include/fixed_queue.h"
41 #include "osi/include/log.h"
42 #include "osi/include/osi.h"
43 #include "osi/include/thread.h"
46 #if (defined BTA_AR_INCLUDED) && (BTA_AR_INCLUDED == true)
47 #include "bta_ar_api.h"
50 /* system manager control block definition */
51 tBTA_SYS_CB bta_sys_cb;
53 extern thread_t* bt_workqueue_thread;
56 /* TODO Hard-coded trace levels - Needs to be configurable */
57 uint8_t appl_trace_level = BT_TRACE_LEVEL_WARNING; // APPL_INITIAL_TRACE_LEVEL;
58 uint8_t btif_trace_level = BT_TRACE_LEVEL_WARNING;
60 static const tBTA_SYS_REG bta_sys_hw_reg = {bta_sys_sm_execute, NULL};
62 /* type for action functions */
63 typedef void (*tBTA_SYS_ACTION)(tBTA_SYS_HW_MSG* p_data);
65 /* action function list */
66 const tBTA_SYS_ACTION bta_sys_action[] = {
67 /* device manager local device API events - cf bta_sys.h for events */
68 bta_sys_hw_api_enable, /* 0 BTA_SYS_HW_API_ENABLE_EVT */
69 bta_sys_hw_evt_enabled, /* 1 BTA_SYS_HW_EVT_ENABLED_EVT */
70 bta_sys_hw_evt_stack_enabled, /* 2 BTA_SYS_HW_EVT_STACK_ENABLED_EVT */
71 bta_sys_hw_api_disable, /* 3 BTA_SYS_HW_API_DISABLE_EVT */
72 bta_sys_hw_evt_disabled, /* 4 BTA_SYS_HW_EVT_DISABLED_EVT */
73 bta_sys_hw_error /* 5 BTA_SYS_HW_ERROR_EVT */
76 /* state machine action enumeration list */
78 /* device manager local device API events */
79 BTA_SYS_HW_API_ENABLE,
80 BTA_SYS_HW_EVT_ENABLED,
81 BTA_SYS_HW_EVT_STACK_ENABLED,
82 BTA_SYS_HW_API_DISABLE,
83 BTA_SYS_HW_EVT_DISABLED,
87 #define BTA_SYS_NUM_ACTIONS (BTA_SYS_MAX_EVT & 0x00ff)
88 #define BTA_SYS_IGNORE BTA_SYS_NUM_ACTIONS
90 /* state table information */
91 #define BTA_SYS_ACTIONS 2 /* number of actions */
92 #define BTA_SYS_NEXT_STATE 2 /* position of next state */
93 #define BTA_SYS_NUM_COLS 3 /* number of columns in state tables */
95 /* state table for OFF state */
96 const uint8_t bta_sys_hw_off[][BTA_SYS_NUM_COLS] = {
97 /* Event Action 1 Action 2
99 /* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE,
100 BTA_SYS_HW_STARTING},
101 /* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_STARTING},
102 /* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
103 /* API_DISABLE */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE,
105 /* EVT_DISABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF},
106 /* EVT_ERROR */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_OFF}};
108 const uint8_t bta_sys_hw_starting[][BTA_SYS_NUM_COLS] = {
109 /* Event Action 1 Action 2
111 /* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
112 BTA_SYS_HW_STARTING}, /* wait for completion event */
113 /* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE,
114 BTA_SYS_HW_STARTING},
115 /* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_IGNORE,
117 /* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
118 BTA_SYS_HW_STOPPING}, /* successive disable/enable:
119 change state wait for
120 completion to disable */
121 /* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_HW_API_ENABLE,
122 BTA_SYS_HW_STARTING}, /* successive enable/disable:
123 notify, then restart HW */
124 /* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}};
126 const uint8_t bta_sys_hw_on[][BTA_SYS_NUM_COLS] = {
127 /* Event Action 1 Action 2
129 /* API_ENABLE */ {BTA_SYS_HW_API_ENABLE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
130 /* EVT_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
131 /* STACK_ENABLED */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
133 {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE,
134 BTA_SYS_HW_ON}, /* don't change the state here, as some
135 other modules might be active */
136 /* EVT_DISABLED */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON},
137 /* EVT_ERROR */ {BTA_SYS_HW_ERROR, BTA_SYS_IGNORE, BTA_SYS_HW_ON}};
139 const uint8_t bta_sys_hw_stopping[][BTA_SYS_NUM_COLS] = {
140 /* Event Action 1 Action 2
142 /* API_ENABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
143 BTA_SYS_HW_STARTING}, /* change state, and wait for
144 completion event to enable */
145 /* EVT_ENABLED */ {BTA_SYS_HW_EVT_ENABLED, BTA_SYS_IGNORE,
146 BTA_SYS_HW_STOPPING}, /* successive enable/disable:
147 finish the enable before
149 /* STACK_ENABLED */ {BTA_SYS_HW_EVT_STACK_ENABLED, BTA_SYS_HW_API_DISABLE,
150 BTA_SYS_HW_STOPPING}, /* successive enable/disable:
152 /* API_DISABLE */ {BTA_SYS_IGNORE, BTA_SYS_IGNORE,
153 BTA_SYS_HW_STOPPING}, /* wait for completion event */
154 /* EVT_DISABLED */ {BTA_SYS_HW_EVT_DISABLED, BTA_SYS_IGNORE,
156 /* EVT_ERROR */ {BTA_SYS_HW_API_DISABLE, BTA_SYS_IGNORE,
157 BTA_SYS_HW_STOPPING}};
159 typedef const uint8_t (*tBTA_SYS_ST_TBL)[BTA_SYS_NUM_COLS];
162 const tBTA_SYS_ST_TBL bta_sys_st_tbl[] = {bta_sys_hw_off, bta_sys_hw_starting,
163 bta_sys_hw_on, bta_sys_hw_stopping};
165 /*******************************************************************************
167 * Function bta_sys_init
169 * Description BTA initialization; called from task initialization.
174 ******************************************************************************/
175 void bta_sys_init(void) {
176 memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
178 appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
180 /* register BTA SYS message handler */
181 bta_sys_register(BTA_ID_SYS, &bta_sys_hw_reg);
183 /* register for BTM notifications */
184 BTM_RegisterForDeviceStatusNotif((tBTM_DEV_STATUS_CB*)&bta_sys_hw_btm_cback);
186 #if (defined BTA_AR_INCLUDED) && (BTA_AR_INCLUDED == true)
191 void bta_sys_free(void) {
194 /*******************************************************************************
196 * Function bta_dm_sm_execute
198 * Description State machine event handling function for DM
203 ******************************************************************************/
204 bool bta_sys_sm_execute(BT_HDR* p_msg) {
206 tBTA_SYS_ST_TBL state_table;
210 APPL_TRACE_EVENT("bta_sys_sm_execute state:%d, event:0x%x", bta_sys_cb.state,
213 /* look up the state table for the current state */
214 state_table = bta_sys_st_tbl[bta_sys_cb.state];
216 bta_sys_cb.state = state_table[p_msg->event & 0x00ff][BTA_SYS_NEXT_STATE];
218 /* execute action functions */
219 for (i = 0; i < BTA_SYS_ACTIONS; i++) {
220 action = state_table[p_msg->event & 0x00ff][i];
221 if (action != BTA_SYS_IGNORE) {
222 (*bta_sys_action[action])((tBTA_SYS_HW_MSG*)p_msg);
230 void bta_sys_hw_register(tBTA_SYS_HW_MODULE module, tBTA_SYS_HW_CBACK* cback) {
231 bta_sys_cb.sys_hw_cback[module] = cback;
234 void bta_sys_hw_unregister(tBTA_SYS_HW_MODULE module) {
235 bta_sys_cb.sys_hw_cback[module] = NULL;
238 /*******************************************************************************
240 * Function bta_sys_hw_btm_cback
242 * Description This function is registered by BTA SYS to BTM in order to get
243 * status notifications
248 ******************************************************************************/
249 void bta_sys_hw_btm_cback(tBTM_DEV_STATUS status) {
250 tBTA_SYS_HW_MSG* sys_event =
251 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
253 APPL_TRACE_DEBUG("%s was called with parameter: %i", __func__, status);
255 /* send a message to BTA SYS */
256 if (status == BTM_DEV_STATUS_UP) {
257 sys_event->hdr.event = BTA_SYS_EVT_STACK_ENABLED_EVT;
258 } else if (status == BTM_DEV_STATUS_DOWN) {
259 sys_event->hdr.event = BTA_SYS_ERROR_EVT;
261 /* BTM_DEV_STATUS_CMD_TOUT is ignored for now. */
262 osi_free_and_reset((void**)&sys_event);
265 if (sys_event) bta_sys_sendmsg(sys_event);
268 /*******************************************************************************
270 * Function bta_sys_hw_error
272 * Description In case the HW device stops answering... Try to turn it off,
274 * previously active SW modules.
276 * Returns success or failure
278 ******************************************************************************/
279 void bta_sys_hw_error(UNUSED_ATTR tBTA_SYS_HW_MSG* p_sys_hw_msg) {
280 uint8_t module_index;
282 APPL_TRACE_DEBUG("%s", __func__);
284 for (module_index = 0; module_index < BTA_SYS_MAX_HW_MODULES;
286 if (bta_sys_cb.sys_hw_module_active & ((uint32_t)1 << module_index)) {
287 switch (module_index) {
288 case BTA_SYS_HW_BLUETOOTH:
289 /* Send BTA_SYS_HW_ERROR_EVT to DM */
290 if (bta_sys_cb.sys_hw_cback[module_index] != NULL)
291 bta_sys_cb.sys_hw_cback[module_index](BTA_SYS_HW_ERROR_EVT);
294 /* not yet supported */
301 /*******************************************************************************
303 * Function bta_sys_hw_enable
305 * Description this function is called after API enable and HW has been
309 * Returns success or failure
311 ******************************************************************************/
313 void bta_sys_hw_api_enable(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
314 if ((!bta_sys_cb.sys_hw_module_active) &&
315 (bta_sys_cb.state != BTA_SYS_HW_ON)) {
316 /* register which HW module was turned on */
317 bta_sys_cb.sys_hw_module_active |= ((uint32_t)1 << p_sys_hw_msg->hw_module);
319 tBTA_SYS_HW_MSG* p_msg =
320 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
321 p_msg->hdr.event = BTA_SYS_EVT_ENABLED_EVT;
322 p_msg->hw_module = p_sys_hw_msg->hw_module;
324 bta_sys_sendmsg(p_msg);
326 /* register which HW module was turned on */
327 bta_sys_cb.sys_hw_module_active |= ((uint32_t)1 << p_sys_hw_msg->hw_module);
329 /* HW already in use, so directly notify the caller */
330 if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module] != NULL)
331 bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module](BTA_SYS_HW_ON_EVT);
334 APPL_TRACE_EVENT("bta_sys_hw_api_enable for %d, active modules 0x%04X",
335 p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
338 /*******************************************************************************
340 * Function bta_sys_hw_disable
342 * Description if no other module is using the HW, this function will call
343 * (if defined) a user-macro to turn off the HW
346 * Returns success or failure
348 ******************************************************************************/
349 void bta_sys_hw_api_disable(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
350 APPL_TRACE_DEBUG("bta_sys_hw_api_disable for %d, active modules: 0x%04X",
351 p_sys_hw_msg->hw_module, bta_sys_cb.sys_hw_module_active);
353 /* make sure the related SW blocks were stopped */
354 bta_sys_disable(p_sys_hw_msg->hw_module);
356 /* register which module we turn off */
357 bta_sys_cb.sys_hw_module_active &= ~((uint32_t)1 << p_sys_hw_msg->hw_module);
359 /* if there are still some SW modules using the HW, just provide an answer to
361 if (bta_sys_cb.sys_hw_module_active != 0) {
362 /* if there are still some SW modules using the HW, directly notify the
364 if (bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module] != NULL)
365 bta_sys_cb.sys_hw_cback[p_sys_hw_msg->hw_module](BTA_SYS_HW_OFF_EVT);
367 /* manually update the state of our system */
368 bta_sys_cb.state = BTA_SYS_HW_STOPPING;
370 tBTA_SYS_HW_MSG* p_msg =
371 (tBTA_SYS_HW_MSG*)osi_malloc(sizeof(tBTA_SYS_HW_MSG));
372 p_msg->hdr.event = BTA_SYS_EVT_DISABLED_EVT;
373 p_msg->hw_module = p_sys_hw_msg->hw_module;
375 bta_sys_sendmsg(p_msg);
379 /*******************************************************************************
381 * Function bta_sys_hw_event_enabled
386 * Returns success or failure
388 ******************************************************************************/
389 void bta_sys_hw_evt_enabled(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
390 APPL_TRACE_EVENT("bta_sys_hw_evt_enabled for %i", p_sys_hw_msg->hw_module);
391 BTM_DeviceReset(NULL);
394 /*******************************************************************************
396 * Function bta_sys_hw_event_disabled
401 * Returns success or failure
403 ******************************************************************************/
404 void bta_sys_hw_evt_disabled(tBTA_SYS_HW_MSG* p_sys_hw_msg) {
405 uint8_t hw_module_index;
407 APPL_TRACE_DEBUG("bta_sys_hw_evt_disabled - module 0x%X",
408 p_sys_hw_msg->hw_module);
410 for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES;
412 if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
413 bta_sys_cb.sys_hw_cback[hw_module_index](BTA_SYS_HW_OFF_EVT);
417 /*******************************************************************************
419 * Function bta_sys_hw_event_stack_enabled
421 * Description we receive this event once the SW side is ready (stack, FW
422 * download,... ), i.e. we can really start using the device. So
425 * Returns success or failure
427 ******************************************************************************/
428 void bta_sys_hw_evt_stack_enabled(UNUSED_ATTR tBTA_SYS_HW_MSG* p_sys_hw_msg) {
429 uint8_t hw_module_index;
431 APPL_TRACE_DEBUG(" bta_sys_hw_evt_stack_enabled!notify the callers");
433 for (hw_module_index = 0; hw_module_index < BTA_SYS_MAX_HW_MODULES;
435 if (bta_sys_cb.sys_hw_cback[hw_module_index] != NULL)
436 bta_sys_cb.sys_hw_cback[hw_module_index](BTA_SYS_HW_ON_EVT);
440 /*******************************************************************************
442 * Function bta_sys_event
444 * Description BTA event handler; called from task event handler.
449 ******************************************************************************/
450 void bta_sys_event(BT_HDR* p_msg) {
454 APPL_TRACE_EVENT("%s: Event 0x%x", __func__, p_msg->event);
456 /* get subsystem id from event */
457 id = (uint8_t)(p_msg->event >> 8);
459 /* verify id and call subsystem event handler */
460 if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL)) {
461 freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
463 APPL_TRACE_WARNING("%s: Received unregistered event id %d", __func__, id);
471 /*******************************************************************************
473 * Function bta_sys_register
475 * Description Called by other BTA subsystems to register their event
481 ******************************************************************************/
482 void bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg) {
483 bta_sys_cb.reg[id] = (tBTA_SYS_REG*)p_reg;
484 bta_sys_cb.is_reg[id] = true;
487 /*******************************************************************************
489 * Function bta_sys_deregister
491 * Description Called by other BTA subsystems to de-register
497 ******************************************************************************/
498 void bta_sys_deregister(uint8_t id) { bta_sys_cb.is_reg[id] = false; }
500 /*******************************************************************************
502 * Function bta_sys_is_register
504 * Description Called by other BTA subsystems to get registeration
510 ******************************************************************************/
511 bool bta_sys_is_register(uint8_t id) { return bta_sys_cb.is_reg[id]; }
513 /*******************************************************************************
515 * Function bta_sys_sendmsg
517 * Description Send a GKI message to BTA. This function is designed to
518 * optimize sending of messages to BTA. It is called by BTA
519 * API functions and call-in functions.
521 * TODO (apanicke): Add location object as parameter for easier
522 * future debugging when doing alarm refactor
527 ******************************************************************************/
528 void bta_sys_sendmsg(void* p_msg) {
529 base::MessageLoop* bta_message_loop = get_message_loop();
531 if (!bta_message_loop || !bta_message_loop->task_runner().get()) {
532 APPL_TRACE_ERROR("%s: MessageLooper not initialized", __func__);
536 bta_message_loop->task_runner()->PostTask(
537 FROM_HERE, base::Bind(&bta_sys_event, static_cast<BT_HDR*>(p_msg)));
540 /*******************************************************************************
542 * Function do_in_bta_thread
544 * Description Post a closure to be ran in the bta thread
548 ******************************************************************************/
549 void do_in_bta_thread(const tracked_objects::Location& from_here,
550 const base::Closure& task) {
551 base::MessageLoop* bta_message_loop = get_message_loop();
553 if (!bta_message_loop || !bta_message_loop->task_runner().get()) {
554 APPL_TRACE_ERROR("%s: MessageLooper not initialized", __func__);
558 bta_message_loop->task_runner()->PostTask(from_here, task);
561 /*******************************************************************************
563 * Function bta_sys_start_timer
565 * Description Start a protocol timer for the specified amount
566 * of time in milliseconds.
570 ******************************************************************************/
571 void bta_sys_start_timer(alarm_t* alarm, period_ms_t interval, uint16_t event,
572 uint16_t layer_specific) {
573 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
575 p_buf->event = event;
576 p_buf->layer_specific = layer_specific;
578 alarm_set_on_mloop(alarm, interval, bta_sys_sendmsg, p_buf);
581 /*******************************************************************************
583 * Function bta_sys_disable
585 * Description For each registered subsystem execute its disable function.
589 ******************************************************************************/
590 void bta_sys_disable(tBTA_SYS_HW_MODULE module) {
594 APPL_TRACE_DEBUG("bta_sys_disable: module %i", module);
597 case BTA_SYS_HW_BLUETOOTH:
599 bta_id_max = BTA_ID_BLUETOOTH_MAX;
602 APPL_TRACE_WARNING("bta_sys_disable: unkown module");
606 for (; bta_id <= bta_id_max; bta_id++) {
607 if (bta_sys_cb.reg[bta_id] != NULL) {
608 if (bta_sys_cb.is_reg[bta_id] == true &&
609 bta_sys_cb.reg[bta_id]->disable != NULL) {
610 (*bta_sys_cb.reg[bta_id]->disable)();
616 /*******************************************************************************
618 * Function bta_sys_set_trace_level
620 * Description Set trace level for BTA
624 ******************************************************************************/
625 void bta_sys_set_trace_level(uint8_t level) { appl_trace_level = level; }
627 /*******************************************************************************
629 * Function bta_sys_get_sys_features
631 * Description Returns sys_features to other BTA modules.
633 * Returns sys_features
635 ******************************************************************************/
636 uint16_t bta_sys_get_sys_features(void) { return bta_sys_cb.sys_features; }