1 /******************************************************************************
2  *
3  *  Copyright (C) 2002-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 file contains the definition of the btm control block when
22  *  BTM_DYNAMIC_MEMORY is used.
23  *
24  ******************************************************************************/
25 
26 #include "stack/bt_types.h"
27 #include "common/bt_target.h"
28 #include <string.h>
29 #include "btm_int.h"
30 #include "osi/allocator.h"
31 
32 /* Global BTM control block structure
33 */
34 #if BTM_DYNAMIC_MEMORY == FALSE
35 tBTM_CB  btm_cb;
36 #else
37 tBTM_CB  *btm_cb_ptr;
38 #endif
39 
40 #if (BLE_50_FEATURE_SUPPORT == TRUE)
41 extern void btm_ble_extendadvcb_init(void);
42 extern void btm_ble_advrecod_init(void);
43 #endif
44 
45 
46 /*******************************************************************************
47 **
48 ** Function         btm_init
49 **
50 ** Description      This function is called at BTM startup to allocate the
51 **                  control block (if using dynamic memory), and initializes the
52 **                  tracing level.  It then initializes the various components of
53 **                  btm.
54 **
55 ** Returns          void
56 **
57 *******************************************************************************/
btm_init(void)58 void btm_init (void)
59 {
60 #if BTM_DYNAMIC_MEMORY
61     btm_cb_ptr = (tBTM_CB *)osi_malloc(sizeof(tBTM_CB));
62 #endif /* #if BTM_DYNAMIC_MEMORY */
63     /* All fields are cleared; nonzero fields are reinitialized in appropriate function */
64     memset(&btm_cb, 0, sizeof(tBTM_CB));
65     btm_cb.page_queue = fixed_queue_new(QUEUE_SIZE_MAX);
66     btm_cb.sec_pending_q = fixed_queue_new(QUEUE_SIZE_MAX);
67 
68 #if defined(BTM_INITIAL_TRACE_LEVEL)
69     btm_cb.trace_level = BTM_INITIAL_TRACE_LEVEL;
70 #else
71     btm_cb.trace_level = BT_TRACE_LEVEL_NONE;
72 #endif
73     /* Initialize BTM component structures */
74     btm_inq_db_init();                  /* Inquiry Database and Structures */
75     btm_acl_init();                     /* ACL Database and Structures */
76 #if (SMP_INCLUDED == TRUE)
77     btm_sec_init(BTM_SEC_MODE_SP);      /* Security Manager Database and Structures */
78 #endif  ///SMP_INCLUDED == TRUE
79 #if BTM_SCO_INCLUDED == TRUE
80     btm_sco_init();                     /* SCO Database and Structures (If included) */
81 #endif
82 
83     btm_dev_init();                     /* Device Manager Structures & HCI_Reset */
84 #if BLE_INCLUDED == TRUE
85     btm_ble_lock_init();
86     btm_ble_sem_init();
87 #endif
88     btm_sec_dev_init();
89 #if (BLE_50_FEATURE_SUPPORT == TRUE)
90     btm_ble_extendadvcb_init();
91     btm_ble_advrecod_init();
92 #endif
93 
94 }
95 
96 
97 /*******************************************************************************
98 **
99 ** Function         btm_free
100 **
101 ** Description      This function is called at btu core free the fixed queue
102 **
103 ** Returns          void
104 **
105 *******************************************************************************/
btm_free(void)106 void btm_free(void)
107 {
108     fixed_queue_free(btm_cb.page_queue, osi_free_func);
109     fixed_queue_free(btm_cb.sec_pending_q, osi_free_func);
110     btm_acl_free();
111     btm_sec_dev_free();
112 #if BTM_DYNAMIC_MEMORY
113     FREE_AND_RESET(btm_cb_ptr);
114 #endif
115 #if BLE_INCLUDED == TRUE
116     btm_ble_lock_free();
117     btm_ble_sem_free();
118 #endif
119 }
120