• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /******************************************************************************
2  *
3  *  Copyright 1999-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_btu_task"
20 
21 #include <pthread.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 
26 #include "bta/sys/bta_sys.h"
27 #include "btcore/include/module.h"
28 #include "bte.h"
29 #include "btif/include/btif_common.h"
30 #include "common/message_loop_thread.h"
31 #include "osi/include/osi.h"
32 #include "stack/btm/btm_int.h"
33 #include "stack/include/btu.h"
34 #include "stack/l2cap/l2c_int.h"
35 
36 #include <base/bind.h>
37 #include <base/logging.h>
38 #include <base/run_loop.h>
39 #include <base/threading/thread.h>
40 
41 using bluetooth::common::MessageLoopThread;
42 
43 /* Define BTU storage area */
44 uint8_t btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
45 
46 static MessageLoopThread main_thread("bt_main_thread");
47 
btu_hci_msg_process(BT_HDR * p_msg)48 void btu_hci_msg_process(BT_HDR* p_msg) {
49   /* Determine the input message type. */
50   switch (p_msg->event & BT_EVT_MASK) {
51     case BT_EVT_TO_BTU_HCI_ACL:
52       /* All Acl Data goes to L2CAP */
53       l2c_rcv_acl_data(p_msg);
54       break;
55 
56     case BT_EVT_TO_BTU_L2C_SEG_XMIT:
57       /* L2CAP segment transmit complete */
58       l2c_link_segments_xmitted(p_msg);
59       break;
60 
61     case BT_EVT_TO_BTU_HCI_SCO:
62       btm_route_sco_data(p_msg);
63       break;
64 
65     case BT_EVT_TO_BTU_HCI_EVT:
66       btu_hcif_process_event((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
67       osi_free(p_msg);
68       break;
69 
70     case BT_EVT_TO_BTU_HCI_CMD:
71       btu_hcif_send_cmd((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
72       break;
73 
74     default:
75       osi_free(p_msg);
76       break;
77   }
78 }
79 
get_main_thread()80 bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; }
81 
get_main_message_loop()82 base::MessageLoop* get_main_message_loop() {
83   return main_thread.message_loop();
84 }
85 
do_in_main_thread(const base::Location & from_here,base::OnceClosure task)86 bt_status_t do_in_main_thread(const base::Location& from_here,
87                               base::OnceClosure task) {
88   if (!main_thread.DoInThread(from_here, std::move(task))) {
89     LOG(ERROR) << __func__ << ": failed from " << from_here.ToString();
90     return BT_STATUS_FAIL;
91   }
92   return BT_STATUS_SUCCESS;
93 }
94 
btu_task_start_up(UNUSED_ATTR void * context)95 void btu_task_start_up(UNUSED_ATTR void* context) {
96   LOG(INFO) << "Bluetooth chip preload is complete";
97 
98   /* Initialize the mandatory core stack control blocks
99      (BTU, BTM, L2CAP, and SDP)
100    */
101   btu_init_core();
102 
103   /* Initialize any optional stack components */
104   BTE_InitStack();
105 
106   bta_sys_init();
107 
108   /* Initialise platform trace levels at this point as BTE_InitStack() and
109    * bta_sys_init()
110    * reset the control blocks and preset the trace level with
111    * XXX_INITIAL_TRACE_LEVEL
112    */
113   module_init(get_module(BTE_LOGMSG_MODULE));
114 
115   main_thread.StartUp();
116   if (!main_thread.IsRunning()) {
117     LOG(FATAL) << __func__ << ": unable to start btu message loop thread.";
118   }
119   if (!main_thread.EnableRealTimeScheduling()) {
120     LOG(FATAL) << __func__ << ": unable to enable real time scheduling";
121   }
122   if (do_in_jni_thread(FROM_HERE, base::Bind(btif_init_ok, 0, nullptr)) !=
123       BT_STATUS_SUCCESS) {
124     LOG(FATAL) << __func__ << ": unable to continue starting Bluetooth";
125   }
126 }
127 
btu_task_shut_down(UNUSED_ATTR void * context)128 void btu_task_shut_down(UNUSED_ATTR void* context) {
129   // Shutdown message loop on task completed
130   main_thread.ShutDown();
131 
132   module_clean_up(get_module(BTE_LOGMSG_MODULE));
133 
134   bta_sys_free();
135   btu_free_core();
136 }
137