1 /******************************************************************************
2  *
3  *  Copyright 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.
22  *
23  ******************************************************************************/
24 
25 #include <string.h>
26 #include "bt_target.h"
27 #include "bt_types.h"
28 #include "btm_int.h"
29 #include "stack_config.h"
30 
31 /* Global BTM control block structure
32 */
33 tBTM_CB btm_cb;
34 
35 /*******************************************************************************
36  *
37  * Function         btm_init
38  *
39  * Description      This function is called at BTM startup to allocate the
40  *                  control block (if using dynamic memory), and initializes the
41  *                  tracing level.  It then initializes the various components
42  *                  of btm.
43  *
44  * Returns          void
45  *
46  ******************************************************************************/
btm_init(void)47 void btm_init(void) {
48   /* All fields are cleared; nonzero fields are reinitialized in appropriate
49    * function */
50   memset(&btm_cb, 0, sizeof(tBTM_CB));
51   btm_cb.page_queue = fixed_queue_new(SIZE_MAX);
52   btm_cb.sec_pending_q = fixed_queue_new(SIZE_MAX);
53   btm_cb.sec_collision_timer = alarm_new("btm.sec_collision_timer");
54   btm_cb.pairing_timer = alarm_new("btm.pairing_timer");
55 
56 #if defined(BTM_INITIAL_TRACE_LEVEL)
57   btm_cb.trace_level = BTM_INITIAL_TRACE_LEVEL;
58 #else
59   btm_cb.trace_level = BT_TRACE_LEVEL_NONE; /* No traces */
60 #endif
61   /* Initialize BTM component structures */
62   btm_inq_db_init(); /* Inquiry Database and Structures */
63   btm_acl_init();    /* ACL Database and Structures */
64   /* Security Manager Database and Structures */
65   if (stack_config_get_interface()->get_pts_secure_only_mode())
66     btm_sec_init(BTM_SEC_MODE_SC);
67   else
68     btm_sec_init(BTM_SEC_MODE_SP);
69   btm_sco_init(); /* SCO Database and Structures (If included) */
70 
71   btm_cb.sec_dev_rec = list_new(osi_free);
72 
73   btm_dev_init(); /* Device Manager Structures & HCI_Reset */
74 }
75 
76 /** This function is called to free dynamic memory and system resource allocated by btm_init */
btm_free(void)77 void btm_free(void) {
78   fixed_queue_free(btm_cb.page_queue, NULL);
79   btm_cb.page_queue = NULL;
80 
81   fixed_queue_free(btm_cb.sec_pending_q, NULL);
82   btm_cb.sec_pending_q = NULL;
83 
84   list_node_t* end = list_end(btm_cb.sec_dev_rec);
85   list_node_t* node = list_begin(btm_cb.sec_dev_rec);
86   while (node != end) {
87     tBTM_SEC_DEV_REC* p_dev_rec = static_cast<tBTM_SEC_DEV_REC*>(list_node(node));
88 
89     // we do list_remove in, must grab next before removing
90     node = list_next(node);
91     wipe_secrets_and_remove(p_dev_rec);
92   }
93 
94   list_free(btm_cb.sec_dev_rec);
95   btm_cb.sec_dev_rec = NULL;
96 
97   alarm_free(btm_cb.sec_collision_timer);
98   btm_cb.sec_collision_timer = NULL;
99 
100   alarm_free(btm_cb.pairing_timer);
101   btm_cb.pairing_timer = NULL;
102 }
103