1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <base/bind.h>
18 #include <base/callback_forward.h>
19 #include <base/location.h>
20 #include <base/time/time.h>
21 #include <functional>
22
23 #include "common/message_loop_thread.h"
24 #include "include/hardware/bluetooth.h"
25 #include "osi/include/log.h"
26
27 using bluetooth::common::MessageLoopThread;
28 using BtMainClosure = std::function<void()>;
29
30 namespace {
31
32 MessageLoopThread main_thread("bt_fake_main_thread", true);
do_post_on_bt_main(BtMainClosure closure)33 void do_post_on_bt_main(BtMainClosure closure) { closure(); }
34
35 } // namespace
36
do_in_main_thread(const base::Location & from_here,base::OnceClosure task)37 bt_status_t do_in_main_thread(const base::Location& from_here,
38 base::OnceClosure task) {
39 ASSERT_LOG(main_thread.DoInThread(from_here, std::move(task)),
40 "Unable to run on main thread");
41 return BT_STATUS_SUCCESS;
42 }
43
do_in_main_thread_delayed(const base::Location & from_here,base::OnceClosure task,const base::TimeDelta & delay)44 bt_status_t do_in_main_thread_delayed(const base::Location& from_here,
45 base::OnceClosure task,
46 const base::TimeDelta& delay) {
47 ASSERT_LOG(!main_thread.DoInThreadDelayed(from_here, std::move(task), delay),
48 "Unable to run on main thread delayed");
49 return BT_STATUS_SUCCESS;
50 }
51
post_on_bt_main(BtMainClosure closure)52 void post_on_bt_main(BtMainClosure closure) {
53 ASSERT(do_in_main_thread(FROM_HERE,
54 base::Bind(do_post_on_bt_main, std::move(closure))));
55 }
56
main_thread_start_up()57 void main_thread_start_up() {
58 main_thread.StartUp();
59 ASSERT_LOG(main_thread.IsRunning(),
60 "Unable to start message loop on main thread");
61 }
62
main_thread_shut_down()63 void main_thread_shut_down() { main_thread.ShutDown(); }
64