1 /******************************************************************************
2  *
3  *  Copyright 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 <base/logging.h>
22 
23 #include "bt_target.h"
24 #include "btm_int.h"
25 #include "btu.h"
26 #include "common/message_loop_thread.h"
27 #include "device/include/controller.h"
28 #include "gatt_api.h"
29 #include "l2c_int.h"
30 #include "sdpint.h"
31 
32 using bluetooth::common::MessageLoopThread;
33 
34 MessageLoopThread bt_startup_thread("bt_startup_thread");
35 
36 void btu_task_start_up(void* context);
37 void btu_task_shut_down(void* context);
38 
39 /*****************************************************************************
40  *
41  * Function         btu_init_core
42  *
43  * Description      Initialize control block memory for each core component.
44  *
45  *
46  * Returns          void
47  *
48  *****************************************************************************/
btu_init_core()49 void btu_init_core() {
50   /* Initialize the mandatory core stack components */
51   btm_init();
52 
53   l2c_init();
54 
55   sdp_init();
56 
57   gatt_init();
58 
59   SMP_Init();
60 
61   btm_ble_init();
62 }
63 
64 /*****************************************************************************
65  *
66  * Function         btu_free_core
67  *
68  * Description      Releases control block memory for each core component.
69  *
70  *
71  * Returns          void
72  *
73  *****************************************************************************/
btu_free_core()74 void btu_free_core() {
75   /* Free the mandatory core stack components */
76   gatt_free();
77 
78   l2c_free();
79 
80   sdp_free();
81 
82   btm_free();
83 }
84 
85 /*****************************************************************************
86  *
87  * Function         BTU_StartUp
88  *
89  * Description      Initializes the BTU control block.
90  *
91  *                  NOTE: Must be called before creating any tasks
92  *                      (RPC, BTU, HCIT, APPL, etc.)
93  *
94  * Returns          void
95  *
96  *****************************************************************************/
BTU_StartUp()97 void BTU_StartUp() {
98   btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
99   bt_startup_thread.StartUp();
100   if (!bt_startup_thread.EnableRealTimeScheduling()) {
101     LOG(ERROR) << __func__ << ": Unable to set real time scheduling policy for "
102                << bt_startup_thread;
103     BTU_ShutDown();
104     return;
105   }
106   if (!bt_startup_thread.DoInThread(FROM_HERE,
107                                     base::Bind(btu_task_start_up, nullptr))) {
108     LOG(ERROR) << __func__ << ": Unable to continue start-up on "
109                << bt_startup_thread;
110     BTU_ShutDown();
111     return;
112   }
113 }
114 
BTU_ShutDown()115 void BTU_ShutDown() {
116   btu_task_shut_down(nullptr);
117   bt_startup_thread.ShutDown();
118 }
119