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 "btm_iso_api.h"
31 #include "common/message_loop_thread.h"
32 #include "osi/include/log.h"
33 #include "osi/include/osi.h"
34 #include "stack/include/acl_hci_link_interface.h"
35 #include "stack/include/btu.h"
36
37 #include <base/bind.h>
38 #include <base/logging.h>
39 #include <base/run_loop.h>
40 #include <base/threading/thread.h>
41
42 using bluetooth::common::MessageLoopThread;
43 using bluetooth::hci::IsoManager;
44
45 void btm_route_sco_data(BT_HDR* p_msg);
46
47 /* Define BTU storage area */
48 uint8_t btu_trace_level = HCI_INITIAL_TRACE_LEVEL;
49
50 static MessageLoopThread main_thread("bt_main_thread", true);
51
btu_hci_msg_process(BT_HDR * p_msg)52 void btu_hci_msg_process(BT_HDR* p_msg) {
53 /* Determine the input message type. */
54 switch (p_msg->event & BT_EVT_MASK) {
55 case BT_EVT_TO_BTU_HCI_ACL:
56 /* All Acl Data goes to ACL */
57 acl_rcv_acl_data(p_msg);
58 break;
59
60 case BT_EVT_TO_BTU_L2C_SEG_XMIT:
61 /* L2CAP segment transmit complete */
62 acl_link_segments_xmitted(p_msg);
63 break;
64
65 case BT_EVT_TO_BTU_HCI_SCO:
66 btm_route_sco_data(p_msg);
67 break;
68
69 case BT_EVT_TO_BTU_HCI_EVT:
70 btu_hcif_process_event((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
71 osi_free(p_msg);
72 break;
73
74 case BT_EVT_TO_BTU_HCI_CMD:
75 btu_hcif_send_cmd((uint8_t)(p_msg->event & BT_SUB_EVT_MASK), p_msg);
76 break;
77
78 case BT_EVT_TO_BTU_HCI_ISO:
79 IsoManager::GetInstance()->HandleIsoData(p_msg);
80 osi_free(p_msg);
81 break;
82
83 default:
84 osi_free(p_msg);
85 break;
86 }
87 }
88
get_main_thread()89 bluetooth::common::MessageLoopThread* get_main_thread() { return &main_thread; }
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 (!main_thread.DoInThreadDelayed(from_here, std::move(task), delay)) {
104 LOG(ERROR) << __func__ << ": failed from " << from_here.ToString();
105 return BT_STATUS_FAIL;
106 }
107 return BT_STATUS_SUCCESS;
108 }
109
do_post_on_bt_main(BtMainClosure closure)110 static void do_post_on_bt_main(BtMainClosure closure) { closure(); }
111
post_on_bt_main(BtMainClosure closure)112 void post_on_bt_main(BtMainClosure closure) {
113 ASSERT(do_in_main_thread(
114 FROM_HERE, base::Bind(do_post_on_bt_main, std::move(closure))) ==
115 BT_STATUS_SUCCESS);
116 }
117
main_thread_start_up()118 void main_thread_start_up() {
119 main_thread.StartUp();
120 if (!main_thread.IsRunning()) {
121 LOG(FATAL) << __func__ << ": unable to start btu message loop thread.";
122 }
123 if (!main_thread.EnableRealTimeScheduling()) {
124 LOG(FATAL) << __func__ << ": unable to enable real time scheduling";
125 }
126 }
127
main_thread_shut_down()128 void main_thread_shut_down() { main_thread.ShutDown(); }
129