]> granicus.if.org Git - esp-idf/blob - components/bt/bluedroid/stack/btu/btu_init.c
component/bt: reduce the task stack size
[esp-idf] / components / bt / bluedroid / stack / btu / btu_init.c
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 "bt_defs.h"
21 #include "bt_target.h"
22 #include "bt_trace.h"
23 #include "controller.h"
24 #include "alarm.h"
25 #include "fixed_queue.h"
26 #include "hash_map.h"
27 #include "hash_functions.h"
28 #include "thread.h"
29
30 #include "l2c_int.h"
31 #include "dyn_mem.h"
32 #include "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 "gatt_api.h"
41 #include "gatt_int.h"
42 #if SMP_INCLUDED == TRUE
43 #include "smp_int.h"
44 #endif
45 #endif
46
47 // extern fixed_queue_t *btif_msg_queue;
48
49 // Communication queue from bta thread to bt_workqueue.
50 fixed_queue_t *btu_bta_msg_queue;
51
52 // Communication queue from hci thread to bt_workqueue.
53 extern fixed_queue_t *btu_hci_msg_queue;
54
55 // General timer queue.
56 fixed_queue_t *btu_general_alarm_queue;
57 hash_map_t *btu_general_alarm_hash_map;
58 pthread_mutex_t btu_general_alarm_lock;
59 static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 34;
60
61 // Oneshot timer queue.
62 fixed_queue_t *btu_oneshot_alarm_queue;
63 hash_map_t *btu_oneshot_alarm_hash_map;
64 pthread_mutex_t btu_oneshot_alarm_lock;
65 static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 34;
66
67 // l2cap timer queue.
68 fixed_queue_t *btu_l2cap_alarm_queue;
69 hash_map_t *btu_l2cap_alarm_hash_map;
70 pthread_mutex_t btu_l2cap_alarm_lock;
71 static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 34;
72
73 //thread_t *bt_workqueue_thread;
74 //static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
75 xTaskHandle  xBtuTaskHandle = NULL;
76 xQueueHandle xBtuQueue = 0;
77
78 extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
79
80 extern void btu_task_thread_handler(void *arg);
81 void btu_task_start_up(void);
82 void btu_task_shut_down(void);
83 /*****************************************************************************
84 **                          V A R I A B L E S                                *
85 ******************************************************************************/
86 // TODO(cmanton) Move this out of this file
87 const BD_ADDR   BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
88 /*****************************************************************************
89 **
90 ** Function         btu_init_core
91 **
92 ** Description      Initialize control block memory for each core component.
93 **
94 **
95 ** Returns          void
96 **
97 ******************************************************************************/
98 void btu_init_core(void)
99 {
100     /* Initialize the mandatory core stack components */
101     btm_init();
102
103     l2c_init();
104
105 #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
106     sdp_init();
107 #endif
108
109 #if BLE_INCLUDED == TRUE
110 #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
111     gatt_init();
112 #endif
113 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
114     SMP_Init();
115 #endif
116     btm_ble_init();
117 #endif
118 }
119
120 /*****************************************************************************
121 **
122 ** Function         btu_free_core
123 **
124 ** Description      Releases control block memory for each core component.
125 **
126 **
127 ** Returns          void
128 **
129 ******************************************************************************/
130 void btu_free_core(void)
131 {
132       // Free the mandatory core stack components
133       l2c_free();
134
135 #if BLE_INCLUDED == TRUE
136 #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
137       gatt_free();
138 #endif
139 #endif
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 ******************************************************************************/
154 void BTU_StartUp(void)
155 {
156     memset (&btu_cb, 0, sizeof (tBTU_CB));
157     btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
158
159     btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
160     if (btu_bta_msg_queue == NULL)
161         goto error_exit;
162
163     btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
164             hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
165     if (btu_general_alarm_hash_map == NULL)
166         goto error_exit;
167
168     pthread_mutex_init(&btu_general_alarm_lock, NULL);
169
170     btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
171     if (btu_general_alarm_queue == NULL)
172         goto error_exit;
173
174     btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
175             hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
176     if (btu_oneshot_alarm_hash_map == NULL)
177         goto error_exit;
178
179     pthread_mutex_init(&btu_oneshot_alarm_lock, NULL);
180
181     btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
182     if (btu_oneshot_alarm_queue == NULL)
183         goto error_exit;
184
185     btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
186             hash_function_pointer, NULL, (data_free_fn)osi_alarm_free, NULL);
187     if (btu_l2cap_alarm_hash_map == NULL)
188         goto error_exit;
189
190     pthread_mutex_init(&btu_l2cap_alarm_lock, NULL);
191
192     btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
193     if (btu_l2cap_alarm_queue == NULL)
194          goto error_exit;
195
196     xBtuQueue = xQueueCreate(60, sizeof(BtTaskEvt_t));
197     xTaskCreate(btu_task_thread_handler, "BtuT", 4096, NULL, configMAX_PRIORITIES - 1, &xBtuTaskHandle);
198     btu_task_post(SIG_BTU_START_UP);
199 /*
200     // Continue startup on bt workqueue thread.
201     thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
202 */
203     return;
204
205   error_exit:;
206     LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
207     BTU_ShutDown();
208 }
209
210 void BTU_ShutDown(void) {
211   btu_task_shut_down();
212 /*
213   fixed_queue_free(btu_bta_msg_queue, NULL);
214 */
215   hash_map_free(btu_general_alarm_hash_map);
216   pthread_mutex_destroy(&btu_general_alarm_lock);
217   fixed_queue_free(btu_general_alarm_queue, NULL);
218
219   hash_map_free(btu_oneshot_alarm_hash_map);
220   pthread_mutex_destroy(&btu_oneshot_alarm_lock);
221   fixed_queue_free(btu_oneshot_alarm_queue, NULL);
222
223   hash_map_free(btu_l2cap_alarm_hash_map);
224   pthread_mutex_destroy(&btu_l2cap_alarm_lock);
225   fixed_queue_free(btu_l2cap_alarm_queue, NULL);
226
227   //thread_free(bt_workqueue_thread);
228   vTaskDelete(xBtuTaskHandle);
229   vQueueDelete(xBtuQueue);
230
231   btu_bta_msg_queue = NULL;
232
233   btu_general_alarm_hash_map = NULL;
234   btu_general_alarm_queue = NULL;
235
236   btu_oneshot_alarm_hash_map = NULL;
237   btu_oneshot_alarm_queue = NULL;
238
239   btu_l2cap_alarm_hash_map = NULL;
240   btu_l2cap_alarm_queue = NULL;
241
242 //  bt_workqueue_thread = NULL;
243   xBtuTaskHandle = NULL;
244   xBtuQueue = 0;
245 }
246
247 /*****************************************************************************
248 **
249 ** Function         BTU_BleAclPktSize
250 **
251 ** Description      export the BLE ACL packet size.
252 **
253 ** Returns          UINT16
254 **
255 ******************************************************************************/
256 UINT16 BTU_BleAclPktSize(void)
257 {
258 #if BLE_INCLUDED == TRUE
259     return controller_get_interface()->get_acl_packet_size_ble();
260 #else
261     return 0;
262 #endif
263 }
264 /*******************************************************************************
265 **
266 ** Function         btu_uipc_rx_cback
267 **
268 ** Description
269 **
270 **
271 ** Returns          void
272 **
273 *******************************************************************************/
274 /*
275 void btu_uipc_rx_cback(BT_HDR *p_msg) {
276   assert(p_msg != NULL);
277   BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_DEBUG, "btu_uipc_rx_cback event 0x%x,"
278       " len %d, offset %d", p_msg->event, p_msg->len, p_msg->offset);
279   fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
280 }
281 */