1 /******************************************************************************
2 *
3 * Copyright (C) 2000-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 #include <string.h>
19
20 #include "common/bt_defs.h"
21 #include "common/bt_target.h"
22 #include "common/bt_trace.h"
23 #include "device/controller.h"
24 #include "osi/alarm.h"
25 #include "osi/hash_map.h"
26 #include "osi/hash_functions.h"
27 #include "osi/thread.h"
28 #include "osi/mutex.h"
29
30 #include "l2c_int.h"
31 #include "stack/dyn_mem.h"
32 #include "stack/btu.h"
33 #include "btm_int.h"
34
35 #if SDP_INCLUDED == TRUE
36 #include "sdpint.h"
37 #endif
38
39 #if (BLE_INCLUDED == TRUE)
40 #include "stack/gatt_api.h"
41 #include "gatt_int.h"
42 #if SMP_INCLUDED == TRUE
43 #include "smp_int.h"
44 #endif
45 #endif
46
47 #define BTU_TASK_PINNED_TO_CORE (TASK_PINNED_TO_CORE)
48 #define BTU_TASK_STACK_SIZE (BT_BTU_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE)
49 #define BTU_TASK_PRIO (BT_TASK_MAX_PRIORITIES - 5)
50 #define BTU_TASK_NAME "BTU_TASK"
51
52 hash_map_t *btu_general_alarm_hash_map;
53 osi_mutex_t btu_general_alarm_lock;
54 static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 34;
55
56 hash_map_t *btu_oneshot_alarm_hash_map;
57 osi_mutex_t btu_oneshot_alarm_lock;
58 static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 34;
59
60 hash_map_t *btu_l2cap_alarm_hash_map;
61 osi_mutex_t btu_l2cap_alarm_lock;
62 static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 34;
63
64 osi_thread_t *btu_thread = NULL;
65
66 extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
67
68 extern void btu_task_thread_handler(void *arg);
69 void btu_task_start_up(void * param);
70 void btu_task_shut_down(void);
71
72 /*****************************************************************************
73 ** V A R I A B L E S *
74 ******************************************************************************/
75 // TODO(cmanton) Move this out of this file
76 const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
77 /*****************************************************************************
78 **
79 ** Function btu_init_core
80 **
81 ** Description Initialize control block memory for each core component.
82 **
83 **
84 ** Returns void
85 **
86 ******************************************************************************/
btu_init_core(void)87 void btu_init_core(void)
88 {
89 /* Initialize the mandatory core stack components */
90 btm_init();
91
92 l2c_init();
93
94 #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
95 sdp_init();
96 #endif
97
98 #if BLE_INCLUDED == TRUE
99 #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
100 gatt_init();
101 #endif
102 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
103 SMP_Init();
104 #endif
105 btm_ble_init();
106 #endif
107 }
108
109 /*****************************************************************************
110 **
111 ** Function btu_free_core
112 **
113 ** Description Releases control block memory for each core component.
114 **
115 **
116 ** Returns void
117 **
118 ******************************************************************************/
btu_free_core(void)119 void btu_free_core(void)
120 {
121 // Free the mandatory core stack components
122 l2c_free();
123
124 #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
125 sdp_deinit();
126 #endif
127
128 #if BLE_INCLUDED == TRUE
129 #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
130 gatt_free();
131 #endif
132 #if SMP_INCLUDED == TRUE
133 SMP_Free();
134 #endif
135 btm_ble_free();
136 #endif
137 btm_free();
138 }
139
140 /*****************************************************************************
141 **
142 ** Function BTU_StartUp
143 **
144 ** Description Initializes the BTU control block.
145 **
146 ** NOTE: Must be called before creating any tasks
147 ** (RPC, BTU, HCIT, APPL, etc.)
148 **
149 ** Returns void
150 **
151 ******************************************************************************/
BTU_StartUp(void)152 void BTU_StartUp(void)
153 {
154 #if BTU_DYNAMIC_MEMORY
155 btu_cb_ptr = (tBTU_CB *)osi_malloc(sizeof(tBTU_CB));
156 #endif /* #if BTU_DYNAMIC_MEMORY */
157 memset (&btu_cb, 0, sizeof (tBTU_CB));
158 btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
159
160 btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
161 hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
162 if (btu_general_alarm_hash_map == NULL) {
163 goto error_exit;
164 }
165
166 osi_mutex_new(&btu_general_alarm_lock);
167
168 btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
169 hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
170 if (btu_oneshot_alarm_hash_map == NULL) {
171 goto error_exit;
172 }
173
174 osi_mutex_new(&btu_oneshot_alarm_lock);
175
176 btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
177 hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
178 if (btu_l2cap_alarm_hash_map == NULL) {
179 goto error_exit;
180 }
181
182 osi_mutex_new(&btu_l2cap_alarm_lock);
183
184 btu_thread = osi_thread_create(BTU_TASK_NAME, BTU_TASK_STACK_SIZE, BTU_TASK_PRIO, BTU_TASK_PINNED_TO_CORE, 1);
185 if (btu_thread == NULL) {
186 goto error_exit;
187 }
188
189 if (btu_task_post(SIG_BTU_START_UP, NULL, OSI_THREAD_MAX_TIMEOUT) == false) {
190 goto error_exit;
191 }
192
193 return;
194
195 error_exit:;
196 LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
197 BTU_ShutDown();
198 }
199
200 /*****************************************************************************
201 **
202 ** Function BTU_ShutDown
203 **
204 ** Description Deinitializes the BTU control block.
205 **
206 ** Returns void
207 **
208 ******************************************************************************/
BTU_ShutDown(void)209 void BTU_ShutDown(void)
210 {
211 #if BTU_DYNAMIC_MEMORY
212 FREE_AND_RESET(btu_cb_ptr);
213 #endif
214 btu_task_shut_down();
215
216 hash_map_free(btu_general_alarm_hash_map);
217 osi_mutex_free(&btu_general_alarm_lock);
218
219 hash_map_free(btu_oneshot_alarm_hash_map);
220 osi_mutex_free(&btu_oneshot_alarm_lock);
221
222 hash_map_free(btu_l2cap_alarm_hash_map);
223 osi_mutex_free(&btu_l2cap_alarm_lock);
224
225 if (btu_thread) {
226 osi_thread_free(btu_thread);
227 btu_thread = NULL;
228 }
229
230 btu_general_alarm_hash_map = NULL;
231 btu_oneshot_alarm_hash_map = NULL;
232 btu_l2cap_alarm_hash_map = NULL;
233 }
234
235 /*****************************************************************************
236 **
237 ** Function BTU_BleAclPktSize
238 **
239 ** Description export the BLE ACL packet size.
240 **
241 ** Returns UINT16
242 **
243 ******************************************************************************/
BTU_BleAclPktSize(void)244 UINT16 BTU_BleAclPktSize(void)
245 {
246 #if BLE_INCLUDED == TRUE
247 return controller_get_interface()->get_acl_packet_size_ble();
248 #else
249 return 0;
250 #endif
251 }
252
253 #if SCAN_QUEUE_CONGEST_CHECK
BTU_check_queue_is_congest(void)254 bool BTU_check_queue_is_congest(void)
255 {
256 if (osi_thread_queue_wait_size(btu_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
257 return true;
258 }
259
260 return false;
261 }
262 #endif
263
get_btu_work_queue_size(void)264 int get_btu_work_queue_size(void)
265 {
266 return osi_thread_queue_wait_size(btu_thread, 0);
267 }
268