OSDN Git Service

Stat-ify functions in bta/sys/bta_sys_main am: 2b22c4ea7f am: 3265b3000d
[android-x86/system-bt.git] / bta / sys / bta_sys_main.cc
1 /******************************************************************************
2  *
3  *  Copyright 2003-2012 Broadcom Corporation
4  *
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:
8  *
9  *  http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  ******************************************************************************/
18
19 /******************************************************************************
20  *
21  *  This is the main implementation file for the BTA system manager.
22  *
23  ******************************************************************************/
24
25 #define LOG_TAG "bt_bta_sys_main"
26
27 #include <base/bind.h>
28 #include <base/logging.h>
29 #include <string.h>
30
31 #include "bt_common.h"
32 #include "bta_api.h"
33 #include "bta_sys.h"
34 #include "bta_sys_int.h"
35 #include "btm_api.h"
36 #include "btu.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"
41 #include "utl.h"
42
43 void BTIF_dm_on_hw_error();
44
45 /* system manager control block definition */
46 tBTA_SYS_CB bta_sys_cb;
47
48 /* trace level */
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;
52
53 /*******************************************************************************
54  *
55  * Function         bta_sys_init
56  *
57  * Description      BTA initialization; called from task initialization.
58  *
59  *
60  * Returns          void
61  *
62  ******************************************************************************/
63 void bta_sys_init(void) {
64   memset(&bta_sys_cb, 0, sizeof(tBTA_SYS_CB));
65 }
66
67 void bta_set_forward_hw_failures(bool value) {
68   bta_sys_cb.forward_hw_failures = value;
69 }
70
71 void BTA_sys_signal_hw_error() {
72   if (bta_sys_cb.forward_hw_failures) {
73     BTIF_dm_on_hw_error();
74   }
75 }
76
77 /*******************************************************************************
78  *
79  * Function         bta_sys_event
80  *
81  * Description      BTA event handler; called from task event handler.
82  *
83  *
84  * Returns          void
85  *
86  ******************************************************************************/
87 static void bta_sys_event(BT_HDR* p_msg) {
88   uint8_t id;
89   bool freebuf = true;
90
91   APPL_TRACE_EVENT("%s: Event 0x%x", __func__, p_msg->event);
92
93   /* get subsystem id from event */
94   id = (uint8_t)(p_msg->event >> 8);
95
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);
99   } else {
100     LOG_INFO("Ignoring receipt of unregistered event id:%s",
101              BtaIdSysText(id).c_str());
102   }
103
104   if (freebuf) {
105     osi_free(p_msg);
106   }
107 }
108
109 /*******************************************************************************
110  *
111  * Function         bta_sys_register
112  *
113  * Description      Called by other BTA subsystems to register their event
114  *                  handler.
115  *
116  *
117  * Returns          void
118  *
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;
123 }
124
125 /*******************************************************************************
126  *
127  * Function         bta_sys_deregister
128  *
129  * Description      Called by other BTA subsystems to de-register
130  *                  handler.
131  *
132  *
133  * Returns          void
134  *
135  ******************************************************************************/
136 void bta_sys_deregister(uint8_t id) { bta_sys_cb.is_reg[id] = false; }
137
138 /*******************************************************************************
139  *
140  * Function         bta_sys_is_register
141  *
142  * Description      Called by other BTA subsystems to get registeration
143  *                  status.
144  *
145  *
146  * Returns          void
147  *
148  ******************************************************************************/
149 bool bta_sys_is_register(uint8_t id) { return bta_sys_cb.is_reg[id]; }
150
151 /*******************************************************************************
152  *
153  * Function         bta_sys_sendmsg
154  *
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.
158  *
159  *                  TODO (apanicke): Add location object as parameter for easier
160  *                  future debugging when doing alarm refactor
161  *
162  *
163  * Returns          void
164  *
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))) !=
169       BT_STATUS_SUCCESS) {
170     LOG(ERROR) << __func__ << ": do_in_main_thread failed";
171   }
172 }
173
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";
179   }
180 }
181
182 /*******************************************************************************
183  *
184  * Function         bta_sys_start_timer
185  *
186  * Description      Start a protocol timer for the specified amount
187  *                  of time in milliseconds.
188  *
189  * Returns          void
190  *
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));
195
196   p_buf->event = event;
197   p_buf->layer_specific = layer_specific;
198
199   alarm_set_on_mloop(alarm, interval_ms, bta_sys_sendmsg, p_buf);
200 }
201
202 /*******************************************************************************
203  *
204  * Function         bta_sys_disable
205  *
206  * Description      For each registered subsystem execute its disable function.
207  *
208  * Returns          void
209  *
210  ******************************************************************************/
211 void bta_sys_disable() {
212   int bta_id = BTA_ID_DM_SEARCH;
213   int bta_id_max = BTA_ID_BLUETOOTH_MAX;
214
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)();
220       }
221     }
222   }
223 }