• 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 <base/functional/bind.h>
22 #include <base/logging.h>
23 #include <base/run_loop.h>
24 #include <base/threading/thread.h>
25 #include <pthread.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #include "bta/sys/bta_sys.h"
31 #include "btcore/include/module.h"
32 #include "btif/include/btif_common.h"
33 #include "btm_iso_api.h"
34 #include "common/message_loop_thread.h"
35 #include "osi/include/allocator.h"
36 #include "osi/include/log.h"
37 #include "osi/include/osi.h"
38 #include "stack/include/acl_hci_link_interface.h"
39 #include "stack/include/bt_hdr.h"
40 #include "stack/include/btu.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 #if defined(__ANDROID__)
125     LOG(FATAL) << __func__ << ": unable to enable real time scheduling";
126 #else
127     LOG(ERROR) << __func__ << ": unable to enable real time scheduling";
128 #endif
129   }
130 }
131 
main_thread_shut_down()132 void main_thread_shut_down() { main_thread.ShutDown(); }
133 
is_on_main_thread()134 bool is_on_main_thread() {
135   // Pthreads doesn't have the concept of a thread ID, so we have to reach down
136   // into the kernel.
137 #if defined(OS_MACOSX)
138   return main_thread.GetThreadId() == pthread_mach_thread_np(pthread_self());
139 #elif defined(OS_LINUX)
140 #include <sys/syscall.h> /* For SYS_xxx definitions */
141 #include <unistd.h>
142   return main_thread.GetThreadId() == syscall(__NR_gettid);
143 #elif defined(__ANDROID__)
144 #include <sys/types.h>
145 #include <unistd.h>
146   return main_thread.GetThreadId() == gettid();
147 #else
148   LOG(ERROR) << __func__ << "Unable to determine if on main thread";
149   return true;
150 #endif
151 }
152