1 /******************************************************************************
3 * Copyright 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>
31 #include "bt_common.h"
34 #include "bta_sys_int.h"
37 #include "osi/include/alarm.h"
38 #include "osi/include/fixed_queue.h"
39 #include "osi/include/log.h"
40 #include "osi/include/osi.h"
43 void BTIF_dm_on_hw_error();
45 /* system manager control block definition */
46 tBTA_SYS_CB bta_sys_cb;
49 /* TODO Hard-coded trace levels - Needs to be configurable */
50 uint8_t appl_trace_level = APPL_INITIAL_TRACE_LEVEL;
51 uint8_t btif_trace_level = BT_TRACE_LEVEL_WARNING;
53 /*******************************************************************************
55 * Function bta_sys_init
57 * Description BTA initialization; called from task initialization.
62 ******************************************************************************/
63 void bta_sys_init(void) {
64 memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
67 void bta_set_forward_hw_failures(bool value) {
68 bta_sys_cb.forward_hw_failures = value;
71 void BTA_sys_signal_hw_error() {
72 if (bta_sys_cb.forward_hw_failures) {
73 BTIF_dm_on_hw_error();
77 /*******************************************************************************
79 * Function bta_sys_event
81 * Description BTA event handler; called from task event handler.
86 ******************************************************************************/
87 static void bta_sys_event(BT_HDR* p_msg) {
91 APPL_TRACE_EVENT("%s: Event 0x%x", __func__, p_msg->event);
93 /* get subsystem id from event */
94 id = (uint8_t)(p_msg->event >> 8);
96 /* verify id and call subsystem event handler */
97 if ((id < BTA_ID_MAX) && (bta_sys_cb.reg[id] != NULL)) {
98 freebuf = (*bta_sys_cb.reg[id]->evt_hdlr)(p_msg);
100 LOG_INFO("Ignoring receipt of unregistered event id:%s",
101 BtaIdSysText(id).c_str());
109 /*******************************************************************************
111 * Function bta_sys_register
113 * Description Called by other BTA subsystems to register their event
119 ******************************************************************************/
120 void bta_sys_register(uint8_t id, const tBTA_SYS_REG* p_reg) {
121 bta_sys_cb.reg[id] = (tBTA_SYS_REG*)p_reg;
122 bta_sys_cb.is_reg[id] = true;
125 /*******************************************************************************
127 * Function bta_sys_deregister
129 * Description Called by other BTA subsystems to de-register
135 ******************************************************************************/
136 void bta_sys_deregister(uint8_t id) { bta_sys_cb.is_reg[id] = false; }
138 /*******************************************************************************
140 * Function bta_sys_is_register
142 * Description Called by other BTA subsystems to get registeration
148 ******************************************************************************/
149 bool bta_sys_is_register(uint8_t id) { return bta_sys_cb.is_reg[id]; }
151 /*******************************************************************************
153 * Function bta_sys_sendmsg
155 * Description Send a GKI message to BTA. This function is designed to
156 * optimize sending of messages to BTA. It is called by BTA
157 * API functions and call-in functions.
159 * TODO (apanicke): Add location object as parameter for easier
160 * future debugging when doing alarm refactor
165 ******************************************************************************/
166 void bta_sys_sendmsg(void* p_msg) {
167 if (do_in_main_thread(
168 FROM_HERE, base::Bind(&bta_sys_event, static_cast<BT_HDR*>(p_msg))) !=
170 LOG(ERROR) << __func__ << ": do_in_main_thread failed";
174 void bta_sys_sendmsg_delayed(void* p_msg, const base::TimeDelta& delay) {
175 if (do_in_main_thread_delayed(
176 FROM_HERE, base::Bind(&bta_sys_event, static_cast<BT_HDR*>(p_msg)),
177 delay) != BT_STATUS_SUCCESS) {
178 LOG(ERROR) << __func__ << ": do_in_main_thread_delayed failed";
182 /*******************************************************************************
184 * Function bta_sys_start_timer
186 * Description Start a protocol timer for the specified amount
187 * of time in milliseconds.
191 ******************************************************************************/
192 void bta_sys_start_timer(alarm_t* alarm, uint64_t interval_ms, uint16_t event,
193 uint16_t layer_specific) {
194 BT_HDR* p_buf = (BT_HDR*)osi_malloc(sizeof(BT_HDR));
196 p_buf->event = event;
197 p_buf->layer_specific = layer_specific;
199 alarm_set_on_mloop(alarm, interval_ms, bta_sys_sendmsg, p_buf);
202 /*******************************************************************************
204 * Function bta_sys_disable
206 * Description For each registered subsystem execute its disable function.
210 ******************************************************************************/
211 void bta_sys_disable() {
212 int bta_id = BTA_ID_DM_SEARCH;
213 int bta_id_max = BTA_ID_BLUETOOTH_MAX;
215 for (; bta_id <= bta_id_max; bta_id++) {
216 if (bta_sys_cb.reg[bta_id] != NULL) {
217 if (bta_sys_cb.is_reg[bta_id] &&
218 bta_sys_cb.reg[bta_id]->disable != NULL) {
219 (*bta_sys_cb.reg[bta_id]->disable)();