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