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 
28 #include "common/bt_defs.h"
29 #include "common/bt_common_types.h"
30 #include "common/bte.h"
31 #include "stack/btu.h"
32 #include "common/bt_trace.h"
33 #include "osi/osi.h"
34 #include "osi/alarm.h"
35 #include "osi/hash_map.h"
36 #include "osi/hash_functions.h"
37 #include "device/controller.h"
38 #include "hci/hci_layer.h"
39 #include "bta/bta_api.h"
40 
41 /*******************************************************************************
42 **  Constants & Macros
43 *******************************************************************************/
44 
45 /******************************************************************************
46 **  Variables
47 ******************************************************************************/
48 
49 /*******************************************************************************
50 **  Static variables
51 *******************************************************************************/
52 static const hci_t *hci;
53 
54 /*******************************************************************************
55 **  Static functions
56 *******************************************************************************/
57 static void bte_main_disable(void);
58 static void bte_main_enable(void);
59 
60 /*******************************************************************************
61 **  Externs
62 *******************************************************************************/
63 
64 bluedroid_init_done_cb_t bluedroid_init_done_cb;
65 
66 extern void osi_mem_dbg_init(void);
67 /******************************************************************************
68 **
69 ** Function         bte_main_boot_entry
70 **
71 ** Description      BTE MAIN API - Entry point for BTE chip/stack initialization
72 **
73 ** Returns          None
74 **
75 ******************************************************************************/
bte_main_boot_entry(bluedroid_init_done_cb_t cb)76 int bte_main_boot_entry(bluedroid_init_done_cb_t cb)
77 {
78     hci = hci_layer_get_interface();
79     if (!hci) {
80         APPL_TRACE_ERROR("%s could not get hci layer interface.\n", __func__);
81         return -2;
82     }
83 
84     bluedroid_init_done_cb = cb;
85 
86     osi_init();
87 
88     //Enbale HCI
89     bte_main_enable();
90 
91     return 0;
92 }
93 
94 /******************************************************************************
95 **
96 ** Function         bte_main_shutdown
97 **
98 ** Description      BTE MAIN API - Shutdown code for BTE chip/stack
99 **
100 ** Returns          None
101 **
102 ******************************************************************************/
bte_main_shutdown(void)103 void bte_main_shutdown(void)
104 {
105 #if (BLE_INCLUDED == TRUE)
106     BTA_VendorCleanup();
107 #endif
108     bte_main_disable();
109 
110     osi_deinit();
111 }
112 
113 /******************************************************************************
114 **
115 ** Function         bte_main_enable
116 **
117 ** Description      BTE MAIN API - Creates all the BTE tasks. Should be called
118 **                  part of the Bluetooth stack enable sequence
119 **
120 ** Returns          None
121 **
122 ******************************************************************************/
bte_main_enable(void)123 static void bte_main_enable(void)
124 {
125     APPL_TRACE_DEBUG("Enable HCI\n");
126     if (hci_start_up()) {
127         APPL_TRACE_ERROR("Start HCI Host Layer Failure\n");
128         return;
129     }
130 
131     //Now Test Case Not Supported BTU
132     BTU_StartUp();
133 }
134 
135 /******************************************************************************
136 **
137 ** Function         bte_main_disable
138 **
139 ** Description      BTE MAIN API - Destroys all the BTE tasks. Should be called
140 **                  part of the Bluetooth stack disable sequence
141 **
142 ** Returns          None
143 **
144 ******************************************************************************/
bte_main_disable(void)145 static void bte_main_disable(void)
146 {
147     /*
148         APPL_TRACE_DEBUG("%s", __FUNCTION__);
149 
150         module_shut_down(get_module(HCI_MODULE));
151         module_shut_down(get_module(BTSNOOP_MODULE));
152     */
153 
154     hci_shut_down();
155 
156     BTU_ShutDown();
157 }
158 
159 /******************************************************************************
160 **
161 ** Function         bte_main_postload_cfg
162 **
163 ** Description      BTE MAIN API - Stack postload configuration
164 **
165 ** Returns          None
166 **
167 ******************************************************************************/
bte_main_postload_cfg(void)168 void bte_main_postload_cfg(void)
169 {
170     hci->do_postload();
171 }
172 
173 #if (defined(HCILP_INCLUDED) && HCILP_INCLUDED == TRUE)
174 /******************************************************************************
175 **
176 ** Function         bte_main_enable_lpm
177 **
178 ** Description      BTE MAIN API - Enable/Disable low power mode operation
179 **
180 ** Returns          None
181 **
182 ******************************************************************************/
bte_main_enable_lpm(BOOLEAN enable)183 void bte_main_enable_lpm(BOOLEAN enable)
184 {
185     /*Enable Low power ?*/
186     //hci->send_low_power_command(enable ? LPM_ENABLE : LPM_DISABLE);
187 }
188 
189 /******************************************************************************
190 **
191 ** Function         bte_main_lpm_allow_bt_device_sleep
192 **
193 ** Description      BTE MAIN API - Allow BT controller goest to sleep
194 **
195 ** Returns          None
196 **
197 ******************************************************************************/
bte_main_lpm_allow_bt_device_sleep(void)198 void bte_main_lpm_allow_bt_device_sleep(void)
199 {
200     /**/
201     //hci->send_low_power_command(LPM_WAKE_DEASSERT);
202 }
203 
204 /******************************************************************************
205 **
206 ** Function         bte_main_lpm_wake_bt_device
207 **
208 ** Description      BTE MAIN API - Wake BT controller up if it is in sleep mode
209 **
210 ** Returns          None
211 **
212 ******************************************************************************/
bte_main_lpm_wake_bt_device(void)213 void bte_main_lpm_wake_bt_device(void)
214 {
215     //hci->send_low_power_command(LPM_WAKE_ASSERT);
216 }
217 #endif  // HCILP_INCLUDED
218 
219 /******************************************************************************
220 **
221 ** Function         bte_main_hci_send
222 **
223 ** Description      BTE MAIN API - This function is called by the upper stack to
224 **                  send an HCI message. The function displays a protocol trace
225 **                  message (if enabled), and then calls the 'transmit' function
226 **                  associated with the currently selected HCI transport
227 **
228 ** Returns          None
229 **
230 ******************************************************************************/
bte_main_hci_send(BT_HDR * p_msg,UINT16 event)231 void bte_main_hci_send (BT_HDR *p_msg, UINT16 event)
232 {
233     UINT16 sub_event = event & BT_SUB_EVT_MASK;  /* local controller ID */
234 
235     p_msg->event = event;
236 
237     //counter_add("main.tx.packets", 1);
238     //counter_add("main.tx.bytes", p_msg->len);
239 
240     if ((sub_event == LOCAL_BR_EDR_CONTROLLER_ID) || \
241             (sub_event == LOCAL_BLE_CONTROLLER_ID)) {
242         hci->transmit_downward(event, p_msg);
243     } else {
244         //APPL_TRACE_ERROR("Invalid Controller ID. Discarding message.");
245         osi_free(p_msg);
246     }
247 }
248