OSDN Git Service

DO NOT MERGE Add ability to add interop entries dynamically (2/2)
[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
27 #define LOG_TAG "bt_main"
28
29 #include <assert.h>
30 #include <cutils/properties.h>
31 #include <fcntl.h>
32 #include <hardware/bluetooth.h>
33 #include <pthread.h>
34 #include <signal.h>
35 #include <stdlib.h>
36 #include <time.h>
37
38 #include "osi/include/alarm.h"
39 #include "bta_api.h"
40 #include "bt_hci_bdroid.h"
41 #include "bte.h"
42 #include "btif_common.h"
43 #include "btu.h"
44 #include "btsnoop.h"
45 #include "bt_utils.h"
46 #include "btcore/include/counter.h"
47 #include "btcore/include/module.h"
48 #include "device/include/interop.h"
49 #include "hci_layer.h"
50 #include "osi/include/alarm.h"
51 #include "osi/include/fixed_queue.h"
52 #include "osi/include/future.h"
53 #include "gki.h"
54 #include "osi/include/hash_functions.h"
55 #include "osi/include/hash_map.h"
56 #include "hci_layer.h"
57 #include "osi/include/osi.h"
58 #include "osi/include/log.h"
59 #include "stack_config.h"
60 #include "osi/include/thread.h"
61
62 /*******************************************************************************
63 **  Constants & Macros
64 *******************************************************************************/
65
66 /* Run-time configuration file for BLE*/
67 #ifndef BTE_BLE_STACK_CONF_FILE
68 #define BTE_BLE_STACK_CONF_FILE "/etc/bluetooth/ble_stack.conf"
69 #endif
70
71 /******************************************************************************
72 **  Variables
73 ******************************************************************************/
74
75 /*******************************************************************************
76 **  Static variables
77 *******************************************************************************/
78 static const hci_t *hci;
79
80 /*******************************************************************************
81 **  Static functions
82 *******************************************************************************/
83
84 /*******************************************************************************
85 **  Externs
86 *******************************************************************************/
87 extern void bte_load_ble_conf(const char *p_path);
88 fixed_queue_t *btu_hci_msg_queue;
89
90 /******************************************************************************
91 **
92 ** Function         bte_main_boot_entry
93 **
94 ** Description      BTE MAIN API - Entry point for BTE chip/stack initialization
95 **
96 ** Returns          None
97 **
98 ******************************************************************************/
99 void bte_main_boot_entry(void)
100 {
101     module_init(get_module(GKI_MODULE));
102     module_init(get_module(COUNTER_MODULE));
103     module_init(get_module(INTEROP_MODULE));
104
105     hci = hci_layer_get_interface();
106     if (!hci)
107       LOG_ERROR("%s could not get hci layer interface.", __func__);
108
109     btu_hci_msg_queue = fixed_queue_new(SIZE_MAX);
110     if (btu_hci_msg_queue == NULL) {
111       LOG_ERROR("%s unable to allocate hci message queue.", __func__);
112       return;
113     }
114
115     data_dispatcher_register_default(hci->event_dispatcher, btu_hci_msg_queue);
116     hci->set_data_queue(btu_hci_msg_queue);
117
118 #if (defined(BLE_INCLUDED) && (BLE_INCLUDED == TRUE))
119     bte_load_ble_conf(BTE_BLE_STACK_CONF_FILE);
120 #endif
121     module_init(get_module(STACK_CONFIG_MODULE));
122 }
123
124 /******************************************************************************
125 **
126 ** Function         bte_main_shutdown
127 **
128 ** Description      BTE MAIN API - Shutdown code for BTE chip/stack
129 **
130 ** Returns          None
131 **
132 ******************************************************************************/
133 void bte_main_shutdown()
134 {
135     data_dispatcher_register_default(hci_layer_get_interface()->event_dispatcher, NULL);
136     hci->set_data_queue(NULL);
137     fixed_queue_free(btu_hci_msg_queue, NULL);
138
139     btu_hci_msg_queue = NULL;
140
141     module_clean_up(get_module(STACK_CONFIG_MODULE));
142
143     module_clean_up(get_module(INTEROP_MODULE));
144     module_clean_up(get_module(COUNTER_MODULE));
145     module_clean_up(get_module(GKI_MODULE));
146 }
147
148 /******************************************************************************
149 **
150 ** Function         bte_main_enable
151 **
152 ** Description      BTE MAIN API - Creates all the BTE tasks. Should be called
153 **                  part of the Bluetooth stack enable sequence
154 **
155 ** Returns          None
156 **
157 ******************************************************************************/
158 void bte_main_enable()
159 {
160     APPL_TRACE_DEBUG("%s", __FUNCTION__);
161
162     module_start_up(get_module(BTSNOOP_MODULE));
163     module_start_up(get_module(HCI_MODULE));
164
165     BTU_StartUp();
166 }
167
168 /******************************************************************************
169 **
170 ** Function         bte_main_disable
171 **
172 ** Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
173 **                  part of the Bluetooth stack disable sequence
174 **
175 ** Returns          None
176 **
177 ******************************************************************************/
178 void bte_main_disable(void)
179 {
180     APPL_TRACE_DEBUG("%s", __FUNCTION__);
181
182     module_shut_down(get_module(HCI_MODULE));
183     module_shut_down(get_module(BTSNOOP_MODULE));
184
185     BTU_ShutDown();
186 }
187
188 /******************************************************************************
189 **
190 ** Function         bte_main_postload_cfg
191 **
192 ** Description      BTE MAIN API - Stack postload configuration
193 **
194 ** Returns          None
195 **
196 ******************************************************************************/
197 void bte_main_postload_cfg(void)
198 {
199     hci->do_postload();
200 }
201
202 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
203 /******************************************************************************
204 **
205 ** Function         bte_main_enable_lpm
206 **
207 ** Description      BTE MAIN API - Enable/Disable low power mode operation
208 **
209 ** Returns          None
210 **
211 ******************************************************************************/
212 void bte_main_enable_lpm(BOOLEAN enable)
213 {
214     hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
215 }
216
217 /******************************************************************************
218 **
219 ** Function         bte_main_lpm_allow_bt_device_sleep
220 **
221 ** Description      BTE MAIN API - Allow BT controller goest to sleep
222 **
223 ** Returns          None
224 **
225 ******************************************************************************/
226 void bte_main_lpm_allow_bt_device_sleep()
227 {
228     hci->send_low_power_command(LPM_WAKE_DEASSERT);
229 }
230
231 /******************************************************************************
232 **
233 ** Function         bte_main_lpm_wake_bt_device
234 **
235 ** Description      BTE MAIN API - Wake BT controller up if it is in sleep mode
236 **
237 ** Returns          None
238 **
239 ******************************************************************************/
240 void bte_main_lpm_wake_bt_device()
241 {
242     hci->send_low_power_command(LPM_WAKE_ASSERT);
243 }
244 #endif  // HCILP_INCLUDED
245
246 /******************************************************************************
247 **
248 ** Function         bte_main_hci_send
249 **
250 ** Description      BTE MAIN API - This function is called by the upper stack to
251 **                  send an HCI message. The function displays a protocol trace
252 **                  message (if enabled), and then calls the 'transmit' function
253 **                  associated with the currently selected HCI transport
254 **
255 ** Returns          None
256 **
257 ******************************************************************************/
258 void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
259 {
260     UINT16 sub_event = event & BT_SUB_EVT_MASK;  /* local controller ID */
261
262     p_msg->event = event;
263
264     counter_add("main.tx.packets", 1);
265     counter_add("main.tx.bytes", p_msg->len);
266
267     if((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
268        (sub_event == LOCAL_BLE_CONTROLLER_ID))
269     {
270         hci->transmit_downward(event, p_msg);
271     }
272     else
273     {
274         APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
275         GKI_freebuf(p_msg);
276     }
277 }