OSDN Git Service

Remove always false flag BTM_BYPASS_EVENT_FILTERING
[android-x86/system-bt.git] / main / bte_main.c
1 /******************************************************************************
2  *
3  *  Copyright (C) 2009-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  *  Filename:      bte_main.c
22  *
23  *  Description:   Contains BTE core stack initialization and shutdown code
24  *
25  ******************************************************************************/
26 #include <assert.h>
27 #include <cutils/properties.h>
28 #include <fcntl.h>
29 #include <hardware/bluetooth.h>
30 #include <pthread.h>
31 #include <signal.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include <utils/Log.h>
35
36 #include "alarm.h"
37 #include "bta_api.h"
38 #include "bt_hci_bdroid.h"
39 #include "bte.h"
40 #include "btif_common.h"
41 #include "btu.h"
42 #include "btsnoop.h"
43 #include "bt_utils.h"
44 #include "counter.h"
45 #include "fixed_queue.h"
46 #include "future.h"
47 #include "gki.h"
48 #include "hash_functions.h"
49 #include "hash_map.h"
50 #include "hci_layer.h"
51 #include "module.h"
52 #include "osi.h"
53 #include "stack_config.h"
54 #include "thread.h"
55
56 /*******************************************************************************
57 **  Constants & Macros
58 *******************************************************************************/
59
60 /* Run-time configuration file for BLE*/
61 #ifndef BTE_BLE_STACK_CONF_FILE
62 #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
63 #endif
64
65 /******************************************************************************
66 **  Variables
67 ******************************************************************************/
68
69 /*******************************************************************************
70 **  Static variables
71 *******************************************************************************/
72 static const hci_t *hci;
73
74 /*******************************************************************************
75 **  Static functions
76 *******************************************************************************/
77
78 /*******************************************************************************
79 **  Externs
80 *******************************************************************************/
81 extern void bte_load_ble_conf(const char *p_path);
82 fixed_queue_t *btu_hci_msg_queue;
83
84 /******************************************************************************
85 **
86 ** Function         bte_main_boot_entry
87 **
88 ** Description      BTE MAIN API - Entry point for BTE chip/stack initialization
89 **
90 ** Returns          None
91 **
92 ******************************************************************************/
93 void bte_main_boot_entry(void)
94 {
95     module_init(get_module(GKI_MODULE));
96     module_init(get_module(COUNTER_MODULE));
97
98     hci = hci_layer_get_interface();
99     if (!hci)
100       ALOGE("%s could not get hci layer interface.", __func__);
101
102     btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
103     if (btu_hci_msg_queue == NULL) {
104       ALOGE("%s unable to allocate hci message queue.", __func__);
105       return;
106     }
107
108     data_dispatcher_register_default(hci->upward_dispatcher, btu_hci_msg_queue);
109
110 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
111     bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
112 #endif
113     module_init(get_module(STACK_CONFIG_MODULE));
114 }
115
116 /******************************************************************************
117 **
118 ** Function         bte_main_shutdown
119 **
120 ** Description      BTE MAIN API - Shutdown code for BTE chip/stack
121 **
122 ** Returns          None
123 **
124 ******************************************************************************/
125 void bte_main_shutdown()
126 {
127     data_dispatcher_register_default(hci_layer_get_interface()->upward_dispatcher, NULL);
128     fixed_queue_free(btu_hci_msg_queue, NULL);
129
130     btu_hci_msg_queue = NULL;
131
132     module_clean_up(get_module(STACK_CONFIG_MODULE));
133
134     module_clean_up(get_module(COUNTER_MODULE));
135     module_clean_up(get_module(GKI_MODULE));
136 }
137
138 /******************************************************************************
139 **
140 ** Function         bte_main_enable
141 **
142 ** Description      BTE MAIN API - Creates all the BTE tasks. Should be called
143 **                  part of the Bluetooth stack enable sequence
144 **
145 ** Returns          None
146 **
147 ******************************************************************************/
148 void bte_main_enable()
149 {
150     APPL_TRACE_DEBUG("%s", __FUNCTION__);
151
152     module_start_up(get_module(BTSNOOP_MODULE));
153     module_start_up(get_module(HCI_MODULE));
154
155     BTU_StartUp();
156 }
157
158 /******************************************************************************
159 **
160 ** Function         bte_main_disable
161 **
162 ** Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
163 **                  part of the Bluetooth stack disable sequence
164 **
165 ** Returns          None
166 **
167 ******************************************************************************/
168 void bte_main_disable(void)
169 {
170     APPL_TRACE_DEBUG("%s", __FUNCTION__);
171
172     module_shut_down(get_module(HCI_MODULE));
173     module_shut_down(get_module(BTSNOOP_MODULE));
174
175     BTU_ShutDown();
176 }
177
178 /******************************************************************************
179 **
180 ** Function         bte_main_postload_cfg
181 **
182 ** Description      BTE MAIN API - Stack postload configuration
183 **
184 ** Returns          None
185 **
186 ******************************************************************************/
187 void bte_main_postload_cfg(void)
188 {
189     hci->do_postload();
190 }
191
192 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
193 /******************************************************************************
194 **
195 ** Function         bte_main_enable_lpm
196 **
197 ** Description      BTE MAIN API - Enable/Disable low power mode operation
198 **
199 ** Returns          None
200 **
201 ******************************************************************************/
202 void bte_main_enable_lpm(BOOLEAN enable)
203 {
204     hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
205 }
206
207 /******************************************************************************
208 **
209 ** Function         bte_main_lpm_allow_bt_device_sleep
210 **
211 ** Description      BTE MAIN API - Allow BT controller goest to sleep
212 **
213 ** Returns          None
214 **
215 ******************************************************************************/
216 void bte_main_lpm_allow_bt_device_sleep()
217 {
218     hci->send_low_power_command(LPM_WAKE_DEASSERT);
219 }
220
221 /******************************************************************************
222 **
223 ** Function         bte_main_lpm_wake_bt_device
224 **
225 ** Description      BTE MAIN API - Wake BT controller up if it is in sleep mode
226 **
227 ** Returns          None
228 **
229 ******************************************************************************/
230 void bte_main_lpm_wake_bt_device()
231 {
232     hci->send_low_power_command(LPM_WAKE_ASSERT);
233 }
234 #endif  // HCILP_INCLUDED
235
236
237 /* NOTICE:
238  *  Definitions for audio state structure, this type needs to match to
239  *  the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h
240  */
241 typedef struct {
242     UINT16  handle;
243     UINT16  peer_codec;
244     UINT16  state;
245 } bt_hc_audio_state_t;
246
247 struct bt_audio_state_tag {
248     BT_HDR hdr;
249     bt_hc_audio_state_t audio;
250 };
251
252 /******************************************************************************
253 **
254 ** Function         set_audio_state
255 **
256 ** Description      Sets audio state on controller state for SCO (PCM, WBS, FM)
257 **
258 ** Parameters       handle: codec related handle for SCO: sco cb idx, unused for
259 **                  codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec
260 **                  state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP
261 **                  param: future extensions, e.g. call-in structure/event.
262 **
263 ** Returns          None
264 **
265 ******************************************************************************/
266 int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param)
267 {
268     struct bt_audio_state_tag *p_msg;
269     int result = -1;
270
271     APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle,
272                     codec, state);
273     if (NULL != param)
274         APPL_TRACE_WARNING("set_audio_state() non-null param not supported");
275     p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg));
276     if (!p_msg)
277         return result;
278     p_msg->audio.handle = handle;
279     p_msg->audio.peer_codec = codec;
280     p_msg->audio.state = state;
281
282     p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE);
283     p_msg->hdr.len = sizeof(p_msg->audio);
284     p_msg->hdr.offset = 0;
285     /* layer_specific shall contain return path event! for BTA events!
286      * 0 means no return message is expected. */
287     p_msg->hdr.layer_specific = 0;
288     hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg);
289     return result;
290 }
291
292
293 /******************************************************************************
294 **
295 ** Function         bte_main_hci_send
296 **
297 ** Description      BTE MAIN API - This function is called by the upper stack to
298 **                  send an HCI message. The function displays a protocol trace
299 **                  message (if enabled), and then calls the 'transmit' function
300 **                  associated with the currently selected HCI transport
301 **
302 ** Returns          None
303 **
304 ******************************************************************************/
305 void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
306 {
307     UINT16 sub_event = event & BT_SUB_EVT_MASK;  /* local controller ID */
308
309     p_msg->event = event;
310
311     counter_add("main.tx.packets", 1);
312     counter_add("main.tx.bytes", p_msg->len);
313
314     if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
315        (sub_event == LOCAL_BLE_CONTROLLER_ID))
316     {
317         hci->transmit_downward(event, p_msg);
318     }
319     else
320     {
321         APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
322         GKI_freebuf(p_msg);
323     }
324 }