OSDN Git Service

Modulizification of GKI
[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 "bd.h"
38 #include "bta_api.h"
39 #include "bt_hci_bdroid.h"
40 #include "bte.h"
41 #include "btif_common.h"
42 #include "btu.h"
43 #include "btsnoop.h"
44 #include "bt_utils.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
97     hci = hci_layer_get_interface();
98     if (!hci)
99       ALOGE("%s could not get hci layer interface.", __func__);
100
101     btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
102     if (btu_hci_msg_queue == NULL) {
103       ALOGE("%s unable to allocate hci message queue.", __func__);
104       return;
105     }
106
107     data_dispatcher_register_default(hci->upward_dispatcher, btu_hci_msg_queue);
108
109 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
110     bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
111 #endif
112     module_init(get_module(STACK_CONFIG_MODULE));
113
114 #if (BTTRC_INCLUDED == TRUE)
115     /* Initialize trace feature */
116     BTTRC_TraceInit(MAX_TRACE_RAM_SIZE, &BTE_TraceLogBuf[0], BTTRC_METHOD_RAM);
117 #endif
118 }
119
120 /******************************************************************************
121 **
122 ** Function         bte_main_shutdown
123 **
124 ** Description      BTE MAIN API - Shutdown code for BTE chip/stack
125 **
126 ** Returns          None
127 **
128 ******************************************************************************/
129 void bte_main_shutdown()
130 {
131     data_dispatcher_register_default(hci_layer_get_interface()->upward_dispatcher, NULL);
132     fixed_queue_free(btu_hci_msg_queue, NULL);
133
134     btu_hci_msg_queue = NULL;
135
136     module_clean_up(get_module(STACK_CONFIG_MODULE));
137
138     module_clean_up(get_module(GKI_MODULE));
139 }
140
141 /******************************************************************************
142 **
143 ** Function         bte_main_enable
144 **
145 ** Description      BTE MAIN API - Creates all the BTE tasks. Should be called
146 **                  part of the Bluetooth stack enable sequence
147 **
148 ** Returns          None
149 **
150 ******************************************************************************/
151 void bte_main_enable()
152 {
153     APPL_TRACE_DEBUG("%s", __FUNCTION__);
154
155     module_start_up(get_module(BTSNOOP_MODULE));
156     module_start_up(get_module(HCI_MODULE));
157
158     BTU_StartUp();
159 }
160
161 /******************************************************************************
162 **
163 ** Function         bte_main_disable
164 **
165 ** Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
166 **                  part of the Bluetooth stack disable sequence
167 **
168 ** Returns          None
169 **
170 ******************************************************************************/
171 void bte_main_disable(void)
172 {
173     APPL_TRACE_DEBUG("%s", __FUNCTION__);
174
175     module_shut_down(get_module(HCI_MODULE));
176     module_shut_down(get_module(BTSNOOP_MODULE));
177
178     BTU_ShutDown();
179 }
180
181 /******************************************************************************
182 **
183 ** Function         bte_main_postload_cfg
184 **
185 ** Description      BTE MAIN API - Stack postload configuration
186 **
187 ** Returns          None
188 **
189 ******************************************************************************/
190 void bte_main_postload_cfg(void)
191 {
192     hci->do_postload();
193 }
194
195 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
196 /******************************************************************************
197 **
198 ** Function         bte_main_enable_lpm
199 **
200 ** Description      BTE MAIN API - Enable/Disable low power mode operation
201 **
202 ** Returns          None
203 **
204 ******************************************************************************/
205 void bte_main_enable_lpm(BOOLEAN enable)
206 {
207     hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
208 }
209
210 /******************************************************************************
211 **
212 ** Function         bte_main_lpm_allow_bt_device_sleep
213 **
214 ** Description      BTE MAIN API - Allow BT controller goest to sleep
215 **
216 ** Returns          None
217 **
218 ******************************************************************************/
219 void bte_main_lpm_allow_bt_device_sleep()
220 {
221     hci->send_low_power_command(LPM_WAKE_DEASSERT);
222 }
223
224 /******************************************************************************
225 **
226 ** Function         bte_main_lpm_wake_bt_device
227 **
228 ** Description      BTE MAIN API - Wake BT controller up if it is in sleep mode
229 **
230 ** Returns          None
231 **
232 ******************************************************************************/
233 void bte_main_lpm_wake_bt_device()
234 {
235     hci->send_low_power_command(LPM_WAKE_ASSERT);
236 }
237 #endif  // HCILP_INCLUDED
238
239
240 /* NOTICE:
241  *  Definitions for audio state structure, this type needs to match to
242  *  the bt_vendor_op_audio_state_t type defined in bt_vendor_lib.h
243  */
244 typedef struct {
245     UINT16  handle;
246     UINT16  peer_codec;
247     UINT16  state;
248 } bt_hc_audio_state_t;
249
250 struct bt_audio_state_tag {
251     BT_HDR hdr;
252     bt_hc_audio_state_t audio;
253 };
254
255 /******************************************************************************
256 **
257 ** Function         set_audio_state
258 **
259 ** Description      Sets audio state on controller state for SCO (PCM, WBS, FM)
260 **
261 ** Parameters       handle: codec related handle for SCO: sco cb idx, unused for
262 **                  codec: BTA_AG_CODEC_MSBC, BTA_AG_CODEC_CSVD or FM codec
263 **                  state: codec state, eg. BTA_AG_CO_AUD_STATE_SETUP
264 **                  param: future extensions, e.g. call-in structure/event.
265 **
266 ** Returns          None
267 **
268 ******************************************************************************/
269 int set_audio_state(UINT16 handle, UINT16 codec, UINT8 state, void *param)
270 {
271     struct bt_audio_state_tag *p_msg;
272     int result = -1;
273
274     APPL_TRACE_API("set_audio_state(handle: %d, codec: 0x%x, state: %d)", handle,
275                     codec, state);
276     if (NULL != param)
277         APPL_TRACE_WARNING("set_audio_state() non-null param not supported");
278     p_msg = (struct bt_audio_state_tag *)GKI_getbuf(sizeof(*p_msg));
279     if (!p_msg)
280         return result;
281     p_msg->audio.handle = handle;
282     p_msg->audio.peer_codec = codec;
283     p_msg->audio.state = state;
284
285     p_msg->hdr.event = MSG_CTRL_TO_HC_CMD | (MSG_SUB_EVT_MASK & BT_HC_AUDIO_STATE);
286     p_msg->hdr.len = sizeof(p_msg->audio);
287     p_msg->hdr.offset = 0;
288     /* layer_specific shall contain return path event! for BTA events!
289      * 0 means no return message is expected. */
290     p_msg->hdr.layer_specific = 0;
291     hci->transmit_downward(MSG_STACK_TO_HC_HCI_CMD, p_msg);
292     return result;
293 }
294
295
296 /******************************************************************************
297 **
298 ** Function         bte_main_hci_send
299 **
300 ** Description      BTE MAIN API - This function is called by the upper stack to
301 **                  send an HCI message. The function displays a protocol trace
302 **                  message (if enabled), and then calls the 'transmit' function
303 **                  associated with the currently selected HCI transport
304 **
305 ** Returns          None
306 **
307 ******************************************************************************/
308 void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
309 {
310     UINT16 sub_event = event & BT_SUB_EVT_MASK;  /* local controller ID */
311
312     p_msg->event = event;
313
314
315     if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
316        (sub_event == LOCAL_BLE_CONTROLLER_ID))
317     {
318         hci->transmit_downward(event, p_msg);
319     }
320     else
321     {
322         APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
323         GKI_freebuf(p_msg);
324     }
325 }