OSDN Git Service

am 9ee1ea15: (-s ours) am c6760d82: Provide --android-ipc-socket-suffix.
[android-x86/system-bt.git] / 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
19 #define LOG_TAG "bt_task"
20
21 #include <assert.h>
22 #include <pthread.h>
23 #include <string.h>
24
25 #include "bt_target.h"
26 #include "btm_int.h"
27 #include "btu.h"
28 #include "device/include/controller.h"
29 #include "dyn_mem.h"
30 #include "l2c_int.h"
31 #include "osi/include/alarm.h"
32 #include "osi/include/fixed_queue.h"
33 #include "osi/include/hash_functions.h"
34 #include "osi/include/hash_map.h"
35 #include "osi/include/log.h"
36 #include "osi/include/thread.h"
37 #include "sdpint.h"
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 = 17;
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 = 17;
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 = 17;
72
73 thread_t *bt_workqueue_thread;
74 static const char *BT_WORKQUEUE_NAME = "bt_workqueue";
75
76 extern void PLATFORM_DisableHciTransport(UINT8 bDisable);
77 /*****************************************************************************
78 **                          V A R I A B L E S                                *
79 ******************************************************************************/
80 // TODO(cmanton) Move this out of this file
81 const BD_ADDR   BT_BD_ANY = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
82
83 void btu_task_start_up(void *context);
84 void btu_task_shut_down(void *context);
85
86 /*****************************************************************************
87 **
88 ** Function         btu_init_core
89 **
90 ** Description      Initialize control block memory for each core component.
91 **
92 **
93 ** Returns          void
94 **
95 ******************************************************************************/
96 void btu_init_core(void)
97 {
98     /* Initialize the mandatory core stack components */
99     btm_init();
100
101     l2c_init();
102
103     sdp_init();
104
105 #if BLE_INCLUDED == TRUE
106     gatt_init();
107 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
108     SMP_Init();
109 #endif
110     btm_ble_init();
111 #endif
112 }
113
114 /*****************************************************************************
115 **
116 ** Function         btu_free_core
117 **
118 ** Description      Releases control block memory for each core component.
119 **
120 **
121 ** Returns          void
122 **
123 ******************************************************************************/
124 void btu_free_core(void)
125 {
126       /* Free the mandatory core stack components */
127       l2c_free();
128
129 #if BLE_INCLUDED == TRUE
130       gatt_free();
131 #endif
132 }
133
134 /*****************************************************************************
135 **
136 ** Function         BTU_StartUp
137 **
138 ** Description      Initializes the BTU control block.
139 **
140 **                  NOTE: Must be called before creating any tasks
141 **                      (RPC, BTU, HCIT, APPL, etc.)
142 **
143 ** Returns          void
144 **
145 ******************************************************************************/
146 void BTU_StartUp(void)
147 {
148     memset (&btu_cb, 0, sizeof (tBTU_CB));
149     btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
150
151     btu_bta_msg_queue = fixed_queue_new(SIZE_MAX);
152     if (btu_bta_msg_queue == NULL)
153         goto error_exit;
154
155     btu_general_alarm_hash_map = hash_map_new(BTU_GENERAL_ALARM_HASH_MAP_SIZE,
156             hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
157     if (btu_general_alarm_hash_map == NULL)
158         goto error_exit;
159
160     if (pthread_mutex_init(&btu_general_alarm_lock, NULL))
161         goto error_exit;
162
163     btu_general_alarm_queue = fixed_queue_new(SIZE_MAX);
164     if (btu_general_alarm_queue == NULL)
165         goto error_exit;
166
167     btu_oneshot_alarm_hash_map = hash_map_new(BTU_ONESHOT_ALARM_HASH_MAP_SIZE,
168             hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
169     if (btu_oneshot_alarm_hash_map == NULL)
170         goto error_exit;
171
172     if (pthread_mutex_init(&btu_oneshot_alarm_lock, NULL))
173         goto error_exit;
174
175     btu_oneshot_alarm_queue = fixed_queue_new(SIZE_MAX);
176     if (btu_oneshot_alarm_queue == NULL)
177         goto error_exit;
178
179     btu_l2cap_alarm_hash_map = hash_map_new(BTU_L2CAP_ALARM_HASH_MAP_SIZE,
180             hash_function_pointer, NULL, (data_free_fn)alarm_free, NULL);
181     if (btu_l2cap_alarm_hash_map == NULL)
182         goto error_exit;
183
184     if (pthread_mutex_init(&btu_l2cap_alarm_lock, NULL))
185         goto error_exit;
186
187     btu_l2cap_alarm_queue = fixed_queue_new(SIZE_MAX);
188     if (btu_l2cap_alarm_queue == NULL)
189          goto error_exit;
190
191     bt_workqueue_thread = thread_new(BT_WORKQUEUE_NAME);
192     if (bt_workqueue_thread == NULL)
193         goto error_exit;
194
195     // Continue startup on bt workqueue thread.
196     thread_post(bt_workqueue_thread, btu_task_start_up, NULL);
197     return;
198
199   error_exit:;
200     LOG_ERROR(LOG_TAG, "%s Unable to allocate resources for bt_workqueue", __func__);
201     BTU_ShutDown();
202 }
203
204 void BTU_ShutDown(void) {
205   btu_task_shut_down(NULL);
206
207   fixed_queue_free(btu_bta_msg_queue, NULL);
208
209   hash_map_free(btu_general_alarm_hash_map);
210   pthread_mutex_destroy(&btu_general_alarm_lock);
211   fixed_queue_free(btu_general_alarm_queue, NULL);
212
213   hash_map_free(btu_oneshot_alarm_hash_map);
214   pthread_mutex_destroy(&btu_oneshot_alarm_lock);
215   fixed_queue_free(btu_oneshot_alarm_queue, NULL);
216
217   hash_map_free(btu_l2cap_alarm_hash_map);
218   pthread_mutex_destroy(&btu_l2cap_alarm_lock);
219   fixed_queue_free(btu_l2cap_alarm_queue, NULL);
220
221   thread_free(bt_workqueue_thread);
222
223   btu_bta_msg_queue = NULL;
224
225   btu_general_alarm_hash_map = NULL;
226   btu_general_alarm_queue = NULL;
227
228   btu_oneshot_alarm_hash_map = NULL;
229   btu_oneshot_alarm_queue = NULL;
230
231   btu_l2cap_alarm_hash_map = NULL;
232   btu_l2cap_alarm_queue = NULL;
233
234   bt_workqueue_thread = NULL;
235 }
236
237 /*****************************************************************************
238 **
239 ** Function         BTU_BleAclPktSize
240 **
241 ** Description      export the BLE ACL packet size.
242 **
243 ** Returns          UINT16
244 **
245 ******************************************************************************/
246 UINT16 BTU_BleAclPktSize(void)
247 {
248 #if BLE_INCLUDED == TRUE
249     return controller_get_interface()->get_acl_packet_size_ble();
250 #else
251     return 0;
252 #endif
253 }
254
255 /*******************************************************************************
256 **
257 ** Function         btu_uipc_rx_cback
258 **
259 ** Description
260 **
261 **
262 ** Returns          void
263 **
264 *******************************************************************************/
265 void btu_uipc_rx_cback(BT_HDR *p_msg) {
266   assert(p_msg != NULL);
267   BT_TRACE(TRACE_LAYER_BTM, TRACE_TYPE_DEBUG, "btu_uipc_rx_cback event 0x%x,"
268       " len %d, offset %d", p_msg->event, p_msg->len, p_msg->offset);
269   fixed_queue_enqueue(btu_hci_msg_queue, p_msg);
270 }