1 /*
2 * Copyright 2020 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 #define LOG_TAG "bt_headless"
18
19 #include <bluetooth/log.h>
20 #include <fcntl.h>
21 #include <sys/socket.h>
22 #include <sys/wait.h>
23 #include <unistd.h>
24
25 #include <iostream>
26 #include <unordered_map>
27
28 #include "test/headless/adapter/adapter.h"
29 #include "test/headless/connect/connect.h"
30 #include "test/headless/discovery/discovery.h"
31 #include "test/headless/dumpsys/dumpsys.h"
32 #include "test/headless/get_options.h"
33 #include "test/headless/headless.h"
34 #include "test/headless/log.h"
35 #include "test/headless/mode/mode.h"
36 #include "test/headless/nop/nop.h"
37 #include "test/headless/pairing/pairing.h"
38 #include "test/headless/read/read.h"
39 #include "test/headless/scan/scan.h"
40 #include "test/headless/sdp/sdp.h"
41 #include "test/headless/util.h"
42
43 using namespace bluetooth::test::headless;
44 using namespace bluetooth;
45
46 int console_fd = -1;
47
48 namespace {
49
50 constexpr char kRedirectedStderrFilename[] = "/dev/null";
51 FILE* redirected_stderr_{nullptr};
52
53 // Ok...so if `stderr` stream is closed, the Android logging system will find
54 // another stream to write `LOG(<LogLevel>)` messages...any...stream.
55 // Unfortunately the next stream/fd is the bluetooth snooplog output
56 // file, so then a btsnoop_hci.log will interleave both raw hci packet
57 // data and Android LOG files preventing proper capture of hci traffic.
58 // To mitigate this, the `stderr` stream is redirected to another user
59 // provided stream, may be `/dev/null`, may be any file if so desired.
60 // This keeps everybody happy.
start_trick_the_android_logging_subsystem()61 void start_trick_the_android_logging_subsystem() {
62 redirected_stderr_ = freopen(kRedirectedStderrFilename, "w", stderr);
63 log::assert_that(redirected_stderr_ != nullptr, "Unable to open redirected stderr file");
64 }
65
stop_trick_the_android_logging_subsystem()66 void stop_trick_the_android_logging_subsystem() {
67 log::assert_that(redirected_stderr_ != nullptr, "assert failed: redirected_stderr_ != nullptr");
68 fclose(redirected_stderr_);
69 redirected_stderr_ = nullptr;
70 }
71
clear_logcat()72 void clear_logcat() {
73 int pid;
74 if ((pid = fork())) {
75 // parent process
76 int status;
77 waitpid(pid, &status, 0); // wait for the child to exit
78 log::assert_that(WIFEXITED(status), "Unable to clear logcat");
79 } else {
80 // child process
81 const char exec[] = "/system/bin/logcat";
82 const char arg0[] = "-c";
83
84 execl(exec, exec, arg0, NULL);
85
86 log::fatal("Should not return from exec process");
87 }
88 }
89
90 class Main : public HeadlessTest<int> {
91 public:
Main(const bluetooth::test::headless::GetOpt & options)92 Main(const bluetooth::test::headless::GetOpt& options) : HeadlessTest<int>(options) {
93 test_nodes_.emplace("adapter", std::make_unique<bluetooth::test::headless::Adapter>(options));
94 test_nodes_.emplace("dumpsys", std::make_unique<bluetooth::test::headless::Dumpsys>(options));
95 test_nodes_.emplace("connect", std::make_unique<bluetooth::test::headless::Connect>(options));
96 test_nodes_.emplace("mode", std::make_unique<bluetooth::test::headless::Mode>(options));
97 test_nodes_.emplace("nop", std::make_unique<bluetooth::test::headless::Nop>(options));
98 test_nodes_.emplace("pairing", std::make_unique<bluetooth::test::headless::Pairing>(options));
99 test_nodes_.emplace("read", std::make_unique<bluetooth::test::headless::Read>(options));
100 test_nodes_.emplace("scan", std::make_unique<bluetooth::test::headless::Scan>(options));
101 test_nodes_.emplace("sdp", std::make_unique<bluetooth::test::headless::Sdp>(options));
102 test_nodes_.emplace("discovery",
103 std::make_unique<bluetooth::test::headless::Discovery>(options));
104 }
105
Run()106 int Run() override {
107 console_fd = fcntl(STDERR_FILENO, F_DUPFD_CLOEXEC, STDERR_FILENO);
108 log::assert_that(console_fd != -1, "assert failed: console_fd != -1");
109 if (options_.close_stderr_) {
110 fclose(stderr);
111 }
112
113 if (options_.clear_logcat_) {
114 clear_logcat();
115 }
116
117 start_trick_the_android_logging_subsystem();
118 if (is_android_running()) {
119 LOG_CONSOLE("Android must be shutdown for binary to run");
120 LOG_CONSOLE(" /system/bin/stop");
121 return -1;
122 }
123 LOG_CONSOLE("bt_headless version:\'%s\'", build_id().c_str());
124 int rc = HeadlessTest<int>::Run();
125 stop_trick_the_android_logging_subsystem();
126 return rc;
127 }
128 };
129
130 } // namespace
131
main(int argc,char ** argv)132 int main(int argc, char** argv) {
133 fflush(nullptr);
134 setvbuf(stdout, nullptr, _IOLBF, 0);
135
136 bluetooth::test::headless::GetOpt options(argc, argv);
137 if (!options.IsValid()) {
138 return -1;
139 }
140
141 Main main(options);
142 return main.Run();
143 }
144