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 /******************************************************************************
20  *
21  *  This module contains the routines that initialize the stack components.
22  *  It must be called before the BTU task is started.
23  *
24  ******************************************************************************/
25 
26 #include "common/bt_target.h"
27 #include <string.h>
28 
29 
30 /* Stack Configuation Related Init Definaton
31  * TODO: Now Just Unmask these defination until stack layer is OK
32  */
33 
34 #ifndef BTA_INCLUDED
35 #define BTA_INCLUDED FALSE
36 #endif
37 
38 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
39 // Include initialization functions definitions
40 #include "stack/port_api.h"
41 #endif
42 
43 #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
44 #include "bnep_api.h"
45 #endif
46 
47 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
48 #include "stack/gap_api.h"
49 #endif
50 
51 #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
52 #include "pan_api.h"
53 #endif
54 
55 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
56 #include "stack/hidh_api.h"
57 #endif
58 
59 #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE)
60 #include "stack/hidd_api.h"
61 #endif
62 
63 #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
64 #include "stack/avrc_api.h"
65 #endif
66 
67 #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
68 #include "stack/a2d_api.h"
69 #endif
70 
71 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
72 #include "avdt_int.h"
73 #endif
74 
75 #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
76 #include "avct_int.h"
77 #endif
78 
79 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
80 #include "stack/hidh_api.h"
81 #endif
82 
83 #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
84 #include "mca_api.h"
85 #endif
86 
87 #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
88 #include "stack/gatt_api.h"
89 #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
90 #include "stack/smp_api.h"
91 #endif
92 #endif
93 
94 //BTA Modules
95 #if BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
96 #include "bta/bta_api.h"
97 #include "bta/bta_sys.h"
98 #include "osi/allocator.h"
99 
100 #if BTA_HF_INCLUDED == TRUE
101 #include "bta_hf_client_int.h"
102 #endif
103 
104 #if BTA_AG_INCLUDED == TRUE
105 #include "bta_ag_int.h"
106 #endif
107 
108 #if BTA_SDP_INCLUDED == TRUE
109 #include "bta_sdp_int.h"
110 #endif
111 
112 #if BTA_HS_INCLUDED == TRUE
113 #include "bta_hs_int.h"
114 #endif
115 
116 #include "bta_dm_int.h"
117 
118 #if BTA_AR_INCLUDED==TRUE
119 #include "bta_ar_int.h"
120 #endif
121 #if BTA_AV_INCLUDED==TRUE
122 #include "bta_av_int.h"
123 #endif
124 
125 #if BTA_HH_INCLUDED==TRUE
126 #include "bta_hh_int.h"
127 #endif
128 
129 #if BTA_HD_INCLUDED==TRUE
130 #include "bta_hd_int.h"
131 #endif
132 
133 #if BTA_JV_INCLUDED==TRUE
134 #include "bta_jv_int.h"
135 #endif
136 
137 #if BTA_HL_INCLUDED == TRUE
138 #include "bta_hl_int.h"
139 #endif
140 
141 #if BTA_GATT_INCLUDED == TRUE
142 #include "bta_gattc_int.h"
143 #include "bta_gatts_int.h"
144 #endif
145 
146 #if BTA_PAN_INCLUDED==TRUE
147 #include "bta_pan_int.h"
148 #endif
149 
150 #include "bta_sys_int.h"
151 
152 // control block for patch ram downloading
153 //#include "bta_prm_int.h"
154 
155 #endif // BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
156 
157 
158 /*****************************************************************************
159 **                          F U N C T I O N S                                *
160 ******************************************************************************/
161 
162 /*****************************************************************************
163 **
164 ** Function         BTE_DeinitStack
165 **
166 ** Description      Deinitialize control block memory for each component.
167 **
168 **                  Note: This API must be called
169 **                      after freeing the BTU Task.
170 **
171 ** Returns          void
172 **
173 ******************************************************************************/
BTE_DeinitStack(void)174 void BTE_DeinitStack(void)
175 {
176     //BTA Modules
177 #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
178 #if GATTS_INCLUDED == TRUE
179     if (bta_gatts_cb_ptr){
180         osi_free(bta_gatts_cb_ptr);
181         bta_gatts_cb_ptr = NULL;
182     }
183 #endif
184 #if GATTC_INCLUDED==TRUE
185     if (bta_gattc_cb_ptr){
186         osi_free(bta_gattc_cb_ptr);
187         bta_gattc_cb_ptr = NULL;
188     }
189 #endif
190 #if BTA_HD_INCLUDED==TRUE
191     if (bta_hd_cb_ptr){
192         osi_free(bta_hd_cb_ptr);
193         bta_hd_cb_ptr = NULL;
194     }
195 #endif
196 #if BTA_HH_INCLUDED==TRUE
197     if (bta_hh_cb_ptr){
198         osi_free(bta_hh_cb_ptr);
199         bta_hh_cb_ptr = NULL;
200     }
201 #endif
202 #if BTA_AV_INCLUDED==TRUE
203     if (bta_av_cb_ptr){
204         osi_free(bta_av_cb_ptr);
205         bta_av_cb_ptr = NULL;
206     }
207     if (bta_av_sbc_ups_cb_ptr){
208         osi_free(bta_av_sbc_ups_cb_ptr);
209         bta_av_sbc_ups_cb_ptr = NULL;
210     }
211 #endif
212 #if BTA_AR_INCLUDED==TRUE
213     if (bta_ar_cb_ptr){
214         osi_free(bta_ar_cb_ptr);
215         bta_ar_cb_ptr = NULL;
216     }
217 #endif
218 #if SDP_INCLUDED == TRUE
219     if (g_disc_raw_data_buf){
220         osi_free(g_disc_raw_data_buf);
221         g_disc_raw_data_buf = NULL;
222     }
223 #endif
224 #if BTA_SDP_INCLUDED == TRUE
225     if (bta_sdp_cb_ptr){
226         osi_free(bta_sdp_cb_ptr);
227         bta_sdp_cb_ptr = NULL;
228     }
229 #endif
230 #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
231     if (bta_jv_cb_ptr){
232         osi_free(bta_jv_cb_ptr);
233         bta_jv_cb_ptr = NULL;
234     }
235 #endif //JV
236 #if (defined BTA_HF_INCLUDED && BTA_HF_INCLUDED == TRUE)
237     if (bta_hf_client_cb_ptr){
238         osi_free(bta_hf_client_cb_ptr);
239         bta_hf_client_cb_ptr = NULL;
240     }
241 #endif
242 #if (defined BTA_AG_INCLUDED && BTA_AG_INCLUDED == TRUE)
243     if (bta_ag_cb_ptr){
244         osi_free(bta_ag_cb_ptr);
245         bta_ag_cb_ptr = NULL;
246     }
247 #endif
248     if (bta_dm_conn_srvcs_ptr){
249         osi_free(bta_dm_conn_srvcs_ptr);
250         bta_dm_conn_srvcs_ptr = NULL;
251     }
252     if (bta_dm_di_cb_ptr){
253         osi_free(bta_dm_di_cb_ptr);
254         bta_dm_di_cb_ptr = NULL;
255     }
256     if (bta_dm_search_cb_ptr){
257         osi_free(bta_dm_search_cb_ptr);
258         bta_dm_search_cb_ptr = NULL;
259     }
260     if (bta_dm_cb_ptr){
261         osi_free(bta_dm_cb_ptr);
262         bta_dm_cb_ptr = NULL;
263     }
264     if (bta_sys_cb_ptr){
265         osi_free(bta_sys_cb_ptr);
266         bta_sys_cb_ptr = NULL;
267     }
268 #endif // BTA_INCLUDED == TRUE
269 
270 #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE)
271     HID_DevDeinit();
272 #endif
273 
274 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
275     HID_HostDeinit();
276 #endif
277 
278 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
279     GAP_Deinit();
280 #endif
281 
282 #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
283     if (avct_cb_ptr){
284         osi_free(avct_cb_ptr);
285         avct_cb_ptr = NULL;
286     }
287 #endif
288 
289 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
290     if (avdt_cb_ptr){
291         osi_free(avdt_cb_ptr);
292         avdt_cb_ptr = NULL;
293     }
294 #endif
295 
296 #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
297     AVRC_Deinit();
298 #endif
299 
300 #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
301     A2D_Deinit();
302 #endif
303 
304 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
305     RFCOMM_Deinit();
306 #endif
307 }
308 
309 /*****************************************************************************
310 **
311 ** Function         BTE_InitStack
312 **
313 ** Description      Initialize control block memory for each component.
314 **
315 **                  Note: The core stack components must be called
316 **                      before creating the BTU Task.  The rest of the
317 **                      components can be initialized at a later time if desired
318 **                      as long as the component's init function is called
319 **                      before accessing any of its functions.
320 **
321 ** Returns          status
322 **
323 ******************************************************************************/
BTE_InitStack(void)324 bt_status_t BTE_InitStack(void)
325 {
326 #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
327     //Initialize the optional stack components
328     if (RFCOMM_Init() != BT_STATUS_SUCCESS) {
329         goto error_exit;
330     }
331 #endif
332 
333     //BNEP and its profiles
334 #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
335     BNEP_Init();
336 
337 #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
338     PAN_Init();
339 #endif  // PAN
340 #endif  // BNEP Included
341 
342     //AVDT and its profiles
343 #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
344     if (A2D_Init() != BT_STATUS_SUCCESS) {
345         goto error_exit;
346     }
347 #endif  // AADP
348 
349 #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
350     if (AVRC_Init() != BT_STATUS_SUCCESS) {
351         goto error_exit;
352     }
353 #endif
354 
355 #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
356     if ((avdt_cb_ptr = (tAVDT_CB *)osi_malloc(sizeof(tAVDT_CB))) == NULL) {
357         goto error_exit;
358     }
359     memset((void *)avdt_cb_ptr, 0, sizeof(tAVDT_CB));
360 #endif
361 
362 #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
363     if ((avct_cb_ptr = (tAVCT_CB *)osi_malloc(sizeof(tAVCT_CB))) == NULL) {
364         goto error_exit;
365     }
366     memset((void *)avct_cb_ptr, 0, sizeof(tAVCT_CB));
367 #endif
368 
369 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
370     if (GAP_Init() != BT_STATUS_SUCCESS) {
371         goto error_exit;
372     }
373 #endif
374 
375 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
376     if (HID_HostInit() != HID_SUCCESS) {
377         goto error_exit;
378     }
379 #endif
380 
381 #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE)
382     if (HID_DevInit() != HID_SUCCESS) {
383         goto error_exit;
384     }
385 #endif
386 
387 #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
388     MCA_Init();
389 #endif
390 
391     //BTA Modules
392 #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
393     if ((bta_sys_cb_ptr = (tBTA_SYS_CB *)osi_malloc(sizeof(tBTA_SYS_CB))) == NULL) {
394         goto error_exit;
395     }
396     if ((bta_dm_cb_ptr = (tBTA_DM_CB *)osi_malloc(sizeof(tBTA_DM_CB))) == NULL) {
397         goto error_exit;
398     }
399     if ((bta_dm_search_cb_ptr = (tBTA_DM_SEARCH_CB *)osi_malloc(sizeof(tBTA_DM_SEARCH_CB))) == NULL) {
400         goto error_exit;
401     }
402     if ((bta_dm_di_cb_ptr = (tBTA_DM_DI_CB *)osi_malloc(sizeof(tBTA_DM_DI_CB))) == NULL) {
403         goto error_exit;
404     }
405     if ((bta_dm_conn_srvcs_ptr = (tBTA_DM_CONNECTED_SRVCS *)osi_malloc(sizeof(tBTA_DM_CONNECTED_SRVCS))) == NULL) {
406         goto error_exit;
407     }
408     memset((void *)bta_sys_cb_ptr, 0, sizeof(tBTA_SYS_CB));
409     memset((void *)bta_dm_cb_ptr, 0, sizeof(tBTA_DM_CB));
410     memset((void *)bta_dm_search_cb_ptr, 0, sizeof(tBTA_DM_SEARCH_CB));
411     memset((void *)bta_dm_di_cb_ptr, 0, sizeof(tBTA_DM_DI_CB));
412     memset((void *)bta_dm_conn_srvcs_ptr, 0, sizeof(tBTA_DM_CONNECTED_SRVCS));
413     //memset((void *)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
414 
415 #if (defined BTA_HF_INCLUDED && BTA_HF_INCLUDED == TRUE)
416     if ((bta_hf_client_cb_ptr = (tBTA_HF_CLIENT_CB *)osi_malloc(sizeof(tBTA_HF_CLIENT_CB))) == NULL) {
417         goto error_exit;
418     }
419     memset((void *)bta_hf_client_cb_ptr, 0, sizeof(tBTA_HF_CLIENT_CB));
420 #endif
421 #if (defined BTA_AG_INCLUDED && BTA_AG_INCLUDED == TRUE)
422     if ((bta_ag_cb_ptr = (tBTA_AG_CB *)osi_malloc(sizeof(tBTA_AG_CB))) == NULL) {
423         goto error_exit;
424     }
425     memset((void *)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
426 #endif
427 #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
428     if ((bta_jv_cb_ptr = (tBTA_JV_CB *)osi_malloc(sizeof(tBTA_JV_CB))) == NULL) {
429         goto error_exit;
430     }
431     memset((void *)bta_jv_cb_ptr, 0, sizeof(tBTA_JV_CB));
432 #endif //JV
433 #if BTA_HS_INCLUDED == TRUE
434     memset((void *)bta_hs_cb_ptr, 0, sizeof(tBTA_HS_CB));
435 #endif
436 #if BTA_SDP_INCLUDED == TRUE
437     if ((bta_sdp_cb_ptr = (tBTA_SDP_CB *)osi_malloc(sizeof(tBTA_SDP_CB))) == NULL) {
438         goto error_exit;
439     }
440     memset((void *)bta_sdp_cb_ptr, 0, sizeof(tBTA_SDP_CB));
441 #endif
442 #if SDP_INCLUDED == TRUE
443     if ((g_disc_raw_data_buf = (UINT8 *)osi_malloc(MAX_DISC_RAW_DATA_BUF)) == NULL) {
444         goto error_exit;
445     }
446     memset((void *)g_disc_raw_data_buf, 0, MAX_DISC_RAW_DATA_BUF);
447 #endif
448 #if BTA_AR_INCLUDED==TRUE
449     if ((bta_ar_cb_ptr = (tBTA_AR_CB *)osi_malloc(sizeof(tBTA_AR_CB))) == NULL) {
450         goto error_exit;
451     }
452     memset((void *)bta_ar_cb_ptr, 0, sizeof(tBTA_AR_CB));
453 #endif
454 #if BTA_AV_INCLUDED==TRUE
455     if ((bta_av_cb_ptr = (tBTA_AV_CB *)osi_malloc(sizeof(tBTA_AV_CB))) == NULL) {
456         goto error_exit;
457     }
458     memset((void *)bta_av_cb_ptr, 0, sizeof(tBTA_AV_CB));
459 
460     if ((bta_av_sbc_ups_cb_ptr = (tBTA_AV_SBC_UPS_CB *)osi_malloc(sizeof(tBTA_AV_SBC_UPS_CB))) == NULL) {
461         goto error_exit;
462     }
463     memset((void *)bta_av_sbc_ups_cb_ptr, 0, sizeof(tBTA_AV_SBC_UPS_CB));
464 #endif
465 #if BTA_HH_INCLUDED==TRUE
466     if ((bta_hh_cb_ptr = (tBTA_HH_CB *)osi_malloc(sizeof(tBTA_HH_CB))) == NULL) {
467         goto error_exit;
468     }
469     memset((void *)bta_hh_cb_ptr, 0, sizeof(tBTA_HH_CB));
470 #endif
471 #if BTA_HD_INCLUDED==TRUE
472     if ((bta_hd_cb_ptr = (tBTA_HD_CB *)osi_malloc(sizeof(tBTA_HD_CB))) == NULL) {
473         goto error_exit;
474     }
475     memset((void *)bta_hd_cb_ptr, 0, sizeof(tBTA_HD_CB));
476 #endif
477 #if BTA_HL_INCLUDED==TRUE
478     memset((void *)bta_hl_cb_ptr, 0, sizeof(tBTA_HL_CB));
479 #endif
480 #if GATTC_INCLUDED==TRUE
481     if ((bta_gattc_cb_ptr = (tBTA_GATTC_CB *)osi_malloc(sizeof(tBTA_GATTC_CB))) == NULL) {
482         goto error_exit;
483     }
484     memset((void *)bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
485 #endif
486 #if GATTS_INCLUDED == TRUE
487     if ((bta_gatts_cb_ptr = (tBTA_GATTS_CB *)osi_malloc(sizeof(tBTA_GATTS_CB))) == NULL) {
488         goto error_exit;
489     }
490     memset((void *)bta_gatts_cb_ptr, 0, sizeof(tBTA_GATTS_CB));
491 #endif
492 #if BTA_PAN_INCLUDED==TRUE
493     memset((void *)bta_pan_cb_ptr, 0, sizeof(tBTA_PAN_CB));
494 #endif
495 
496 #endif // BTA_INCLUDED == TRUE
497     return BT_STATUS_SUCCESS;
498 
499 error_exit:;
500     LOG_ERROR("%s failed due to no memory", __func__);
501     BTE_DeinitStack();
502     return BT_STATUS_NOMEM;
503 }
504