1 /******************************************************************************
3 * Copyright (C) 2000-2012 Broadcom Corporation
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 ******************************************************************************/
21 #include "bt_target.h"
23 #include "controller.h"
25 #include "fixed_queue.h"
27 #include "hash_functions.h"
35 #if SDP_INCLUDED == TRUE
39 #if (BLE_INCLUDED == TRUE)
42 #if SMP_INCLUDED == TRUE
47 // extern fixed_queue_t *btif_msg_queue;
49 // Communication queue from bta thread to bt_workqueue.
50 fixed_queue_t *btu_bta_msg_queue;
52 // Communication queue from hci thread to bt_workqueue.
53 extern fixed_queue_t *btu_hci_msg_queue;
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;
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;
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;
73 //thread_t *bt_workqueue_thread;
74 //static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
75 xTaskHandle xBtuTaskHandle = NULL;
76 xQueueHandle xBtuQueue = 0;
78 extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
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 /*****************************************************************************
90 ** Function btu_init_core
92 ** Description Initialize control block memory for each core component.
97 ******************************************************************************/
98 void btu_init_core(void)
100 /* Initialize the mandatory core stack components */
105 #if (defined(SDP_INCLUDED) && SDP_INCLUDED == TRUE)
109 #if BLE_INCLUDED == TRUE
110 #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
113 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
120 /*****************************************************************************
122 ** Function btu_free_core
124 ** Description Releases control block memory for each core component.
129 ******************************************************************************/
130 void btu_free_core(void)
132 // Free the mandatory core stack components
135 #if BLE_INCLUDED == TRUE
136 #if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
142 /*****************************************************************************
144 ** Function BTU_StartUp
146 ** Description Initializes the BTU control block.
148 ** NOTE: Must be called before creating any tasks
149 ** (RPC, BTU, HCIT, APPL, etc.)
153 ******************************************************************************/
154 void BTU_StartUp(void)
156 memset (&btu_cb, 0, sizeof (tBTU_CB));
157 btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
159 btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
160 if (btu_bta_msg_queue == NULL)
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)
168 pthread_mutex_init(&btu_general_alarm_lock, NULL);
170 btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
171 if (btu_general_alarm_queue == NULL)
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)
179 pthread_mutex_init(&btu_oneshot_alarm_lock, NULL);
181 btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
182 if (btu_oneshot_alarm_queue == NULL)
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)
190 pthread_mutex_init(&btu_l2cap_alarm_lock, NULL);
192 btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
193 if (btu_l2cap_alarm_queue == NULL)
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);
200 // Continue startup on bt workqueue thread.
201 thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
206 LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
210 void BTU_ShutDown(void) {
211 btu_task_shut_down();
213 fixed_queue_free(btu_bta_msg_queue, NULL);
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);
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);
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);
227 //thread_free(bt_workqueue_thread);
228 vTaskDelete(xBtuTaskHandle);
229 vQueueDelete(xBtuQueue);
231 btu_bta_msg_queue = NULL;
233 btu_general_alarm_hash_map = NULL;
234 btu_general_alarm_queue = NULL;
236 btu_oneshot_alarm_hash_map = NULL;
237 btu_oneshot_alarm_queue = NULL;
239 btu_l2cap_alarm_hash_map = NULL;
240 btu_l2cap_alarm_queue = NULL;
242 // bt_workqueue_thread = NULL;
243 xBtuTaskHandle = NULL;
247 /*****************************************************************************
249 ** Function BTU_BleAclPktSize
251 ** Description export the BLE ACL packet size.
255 ******************************************************************************/
256 UINT16 BTU_BleAclPktSize(void)
258 #if BLE_INCLUDED == TRUE
259 return controller_get_interface()->get_acl_packet_size_ble();
264 /*******************************************************************************
266 ** Function btu_uipc_rx_cback
273 *******************************************************************************/
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);