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     btm_cb.addr_res_en = TRUE;
88 #endif
89     btm_sec_dev_init();
90 #if (BLE_50_FEATURE_SUPPORT == TRUE)
91     btm_ble_extendadvcb_init();
92     btm_ble_advrecod_init();
93 #endif
94 
95 }
96 
97 
98 /*******************************************************************************
99 **
100 ** Function         btm_free
101 **
102 ** Description      This function is called at btu core free the fixed queue
103 **
104 ** Returns          void
105 **
106 *******************************************************************************/
btm_free(void)107 void btm_free(void)
108 {
109     fixed_queue_free(btm_cb.page_queue, osi_free_func);
110     fixed_queue_free(btm_cb.sec_pending_q, osi_free_func);
111     btm_acl_free();
112     btm_sec_dev_free();
113 #if BTM_SCO_INCLUDED == TRUE
114     btm_sco_free();
115 #endif
116 #if BTM_DYNAMIC_MEMORY
117     FREE_AND_RESET(btm_cb_ptr);
118 #endif
119 #if BLE_INCLUDED == TRUE
120     btm_ble_lock_free();
121     btm_ble_sem_free();
122 #endif
123 }
124 
btm_acl_active_count(void)125 uint8_t btm_acl_active_count(void)
126 {
127     list_node_t *p_node = NULL;
128     tACL_CONN *p_acl_conn = NULL;
129     uint8_t count = 0;
130 
131     for (p_node = list_begin(btm_cb.p_acl_db_list); p_node; p_node = list_next(p_node)) {
132         p_acl_conn = list_node(p_node);
133         if (p_acl_conn && p_acl_conn->in_use) {
134             count++;
135         }
136     }
137 
138     return count;
139 }
140 #if (BLE_INCLUDED == TRUE)
141 // Address resolution status
btm_get_ble_addr_resolve_disable_status(void)142 uint8_t btm_get_ble_addr_resolve_disable_status(void)
143 {
144     // Returns false if address resolution is enabled, true if disabled
145     return (btm_cb.addr_res_en) ? 0 : 1;
146 }
147 
btm_ble_addr_resolve_enable(bool enable)148 void btm_ble_addr_resolve_enable(bool enable)
149 {
150     btm_cb.addr_res_en = enable;
151 }
152 #endif /*BLE_INCLUDED*/
153