• 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     case BT_EVT_TO_BTU_HCI_ISO:
75       // TODO: implement handler
76       osi_free(p_msg);
77       break;
78 
79     default:
80       osi_free(p_msg);
81       break;
82   }
83 }
84 
get_main_thread()85 bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; }
86 
get_main_message_loop()87 base::MessageLoop* get_main_message_loop() {
88   return main_thread.message_loop();
89 }
90 
do_in_main_thread(const base::Location & from_here,base::OnceClosure task)91 bt_status_t do_in_main_thread(const base::Location& from_here,
92                               base::OnceClosure task) {
93   if (!main_thread.DoInThread(from_here, std::move(task))) {
94     LOG(ERROR) << __func__ << ": failed from " << from_here.ToString();
95     return BT_STATUS_FAIL;
96   }
97   return BT_STATUS_SUCCESS;
98 }
99 
do_in_main_thread_delayed(const base::Location & from_here,base::OnceClosure task,const base::TimeDelta & delay)100 bt_status_t do_in_main_thread_delayed(const base::Location& from_here,
101                                       base::OnceClosure task,
102                                       const base::TimeDelta& delay) {
103   if (!get_main_message_loop()->task_runner()->PostDelayedTask(
104           from_here, std::move(task), delay)) {
105     LOG(ERROR) << __func__ << ": failed from " << from_here.ToString();
106     return BT_STATUS_FAIL;
107   }
108   return BT_STATUS_SUCCESS;
109 }
110 
btu_task_start_up(UNUSED_ATTR void * context)111 void btu_task_start_up(UNUSED_ATTR void* context) {
112   LOG(INFO) << "Bluetooth chip preload is complete";
113 
114   /* Initialize the mandatory core stack control blocks
115      (BTU, BTM, L2CAP, and SDP)
116    */
117   btu_init_core();
118 
119   /* Initialize any optional stack components */
120   BTE_InitStack();
121 
122   bta_sys_init();
123 
124   /* Initialise platform trace levels at this point as BTE_InitStack() and
125    * bta_sys_init()
126    * reset the control blocks and preset the trace level with
127    * XXX_INITIAL_TRACE_LEVEL
128    */
129   module_init(get_module(BTE_LOGMSG_MODULE));
130 
131   main_thread.StartUp();
132   if (!main_thread.IsRunning()) {
133     LOG(FATAL) << __func__ << ": unable to start btu message loop thread.";
134   }
135   if (!main_thread.EnableRealTimeScheduling()) {
136     LOG(FATAL) << __func__ << ": unable to enable real time scheduling";
137   }
138   if (do_in_jni_thread(FROM_HERE, base::Bind(btif_init_ok, 0, nullptr)) !=
139       BT_STATUS_SUCCESS) {
140     LOG(FATAL) << __func__ << ": unable to continue starting Bluetooth";
141   }
142 }
143 
btu_task_shut_down(UNUSED_ATTR void * context)144 void btu_task_shut_down(UNUSED_ATTR void* context) {
145   // Shutdown message loop on task completed
146   main_thread.ShutDown();
147 
148   module_clean_up(get_module(BTE_LOGMSG_MODULE));
149 
150   bta_sys_free();
151   btu_free_core();
152 }
153