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 ******************************************************************************/
19 #define LOG_TAG "bt_task"
23 #include "bt_target.h"
28 #include "osi/include/alarm.h"
29 #include "device/include/controller.h"
30 #include "osi/include/fixed_queue.h"
31 #include "osi/include/hash_map.h"
34 #include "osi/include/hash_functions.h"
36 #include "osi/include/thread.h"
38 #include "osi/include/log.h"
40 #if (BLE_INCLUDED == TRUE)
43 #if SMP_INCLUDED == TRUE
48 extern fixed_queue_t *btif_msg_queue;
50 // Communication queue from bta thread to bt_workqueue.
51 fixed_queue_t *btu_bta_msg_queue;
53 // Communication queue from hci thread to bt_workqueue.
54 extern fixed_queue_t *btu_hci_msg_queue;
56 // General timer queue.
57 fixed_queue_t *btu_general_alarm_queue;
58 hash_map_t *btu_general_alarm_hash_map;
59 pthread_mutex_t btu_general_alarm_lock;
60 static const size_t BTU_GENERAL_ALARM_HASH_MAP_SIZE = 17;
62 // Oneshot timer queue.
63 fixed_queue_t *btu_oneshot_alarm_queue;
64 hash_map_t *btu_oneshot_alarm_hash_map;
65 pthread_mutex_t btu_oneshot_alarm_lock;
66 static const size_t BTU_ONESHOT_ALARM_HASH_MAP_SIZE = 17;
69 fixed_queue_t *btu_l2cap_alarm_queue;
70 hash_map_t *btu_l2cap_alarm_hash_map;
71 pthread_mutex_t btu_l2cap_alarm_lock;
72 static const size_t BTU_L2CAP_ALARM_HASH_MAP_SIZE = 17;
74 thread_t *bt_workqueue_thread;
75 static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
77 extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
78 /*****************************************************************************
79 ** V A R I A B L E S *
80 ******************************************************************************/
81 // TODO(cmanton) Move this out of this file
82 const BD_ADDR BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
84 void btu_task_start_up(void *context);
85 void btu_task_shut_down(void *context);
87 /*****************************************************************************
89 ** Function btu_init_core
91 ** Description Initialize control block memory for each core component.
96 ******************************************************************************/
97 void btu_init_core(void)
99 /* Initialize the mandatory core stack components */
106 #if BLE_INCLUDED == TRUE
108 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
115 /*****************************************************************************
117 ** Function btu_free_core
119 ** Description Releases control block memory for each core component.
124 ******************************************************************************/
125 void btu_free_core(void)
127 /* Free the mandatory core stack components */
130 #if BLE_INCLUDED == TRUE
135 /*****************************************************************************
137 ** Function BTU_StartUp
139 ** Description Initializes the BTU control block.
141 ** NOTE: Must be called before creating any tasks
142 ** (RPC, BTU, HCIT, APPL, etc.)
146 ******************************************************************************/
147 void BTU_StartUp(void)
149 memset (&btu_cb, 0, sizeof (tBTU_CB));
150 btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
152 btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
153 if (btu_bta_msg_queue == NULL)
156 btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
157 hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
158 if (btu_general_alarm_hash_map == NULL)
161 if (pthread_mutex_init(&btu_general_alarm_lock, NULL))
164 btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
165 if (btu_general_alarm_queue == NULL)
168 btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
169 hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
170 if (btu_oneshot_alarm_hash_map == NULL)
173 if (pthread_mutex_init(&btu_oneshot_alarm_lock, NULL))
176 btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
177 if (btu_oneshot_alarm_queue == NULL)
180 btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
181 hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
182 if (btu_l2cap_alarm_hash_map == NULL)
185 if (pthread_mutex_init(&btu_l2cap_alarm_lock, NULL))
188 btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
189 if (btu_l2cap_alarm_queue == NULL)
192 bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
193 if (bt_workqueue_thread == NULL)
196 // Continue startup on bt workqueue thread.
197 thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
201 LOG_ERROR("%s Unable to allocate resources for bt_workqueue", __func__);
205 void BTU_ShutDown(void) {
206 btu_task_shut_down(NULL);
208 fixed_queue_free(btu_bta_msg_queue, NULL);
210 hash_map_free(btu_general_alarm_hash_map);
211 pthread_mutex_destroy(&btu_general_alarm_lock);
212 fixed_queue_free(btu_general_alarm_queue, NULL);
214 hash_map_free(btu_oneshot_alarm_hash_map);
215 pthread_mutex_destroy(&btu_oneshot_alarm_lock);
216 fixed_queue_free(btu_oneshot_alarm_queue, NULL);
218 hash_map_free(btu_l2cap_alarm_hash_map);
219 pthread_mutex_destroy(&btu_l2cap_alarm_lock);
220 fixed_queue_free(btu_l2cap_alarm_queue, NULL);
222 thread_free(bt_workqueue_thread);
224 btu_bta_msg_queue = NULL;
226 btu_general_alarm_hash_map = NULL;
227 btu_general_alarm_queue = NULL;
229 btu_oneshot_alarm_hash_map = NULL;
230 btu_oneshot_alarm_queue = NULL;
232 btu_l2cap_alarm_hash_map = NULL;
233 btu_l2cap_alarm_queue = NULL;
235 bt_workqueue_thread = NULL;
238 /*****************************************************************************
240 ** Function BTU_BleAclPktSize
242 ** Description export the BLE ACL packet size.
246 ******************************************************************************/
247 UINT16 BTU_BleAclPktSize(void)
249 #if BLE_INCLUDED == TRUE
250 return controller_get_interface()->get_acl_packet_size_ble();
256 /*******************************************************************************
258 ** Function btu_uipc_rx_cback
265 *******************************************************************************/
266 void btu_uipc_rx_cback(BT_HDR *p_msg) {
267 assert(p_msg != NULL);
268 BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_DEBUG, "btu_uipc_rx_cback event 0x%x,"
269 " len %d, offset %d", p_msg->event, p_msg->len, p_msg->offset);
270 fixed_queue_enqueue(btu_hci_msg_queue, p_msg);