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 <iostream>
20 #include <unordered_map>
21
22 #include <sys/wait.h>
23 #include <unistd.h>
24
25 #include "base/logging.h" // LOG() stdout and android log
26 #include "osi/include/log.h" // android log only
27 #include "test/headless/connect/connect.h"
28 #include "test/headless/dumpsys/dumpsys.h"
29 #include "test/headless/get_options.h"
30 #include "test/headless/headless.h"
31 #include "test/headless/nop/nop.h"
32 #include "test/headless/pairing/pairing.h"
33 #include "test/headless/read/read.h"
34 #include "test/headless/sdp/sdp.h"
35
36 using namespace bluetooth::test::headless;
37
38 namespace {
39
clear_logcat()40 void clear_logcat() {
41 int pid;
42 if ((pid = fork())) {
43 // parent process
44 int status;
45 waitpid(pid, &status, 0); // wait for the child to exit
46 ASSERT_LOG(WIFEXITED(status), "Unable to clear logcat");
47 } else {
48 // child process
49 const char exec[] = "/system/bin/logcat";
50 const char arg0[] = "-c";
51
52 execl(exec, exec, arg0, NULL);
53
54 ASSERT_LOG(false, "Should not return from exec process");
55 }
56 }
57
58 class Main : public HeadlessTest<int> {
59 public:
Main(const bluetooth::test::headless::GetOpt & options)60 Main(const bluetooth::test::headless::GetOpt& options)
61 : HeadlessTest<int>(options) {
62 test_nodes_.emplace(
63 "dumpsys",
64 std::make_unique<bluetooth::test::headless::Dumpsys>(options));
65 test_nodes_.emplace(
66 "connect",
67 std::make_unique<bluetooth::test::headless::Connect>(options));
68 test_nodes_.emplace(
69 "nop", std::make_unique<bluetooth::test::headless::Nop>(options));
70 test_nodes_.emplace(
71 "pairing",
72 std::make_unique<bluetooth::test::headless::Pairing>(options));
73 test_nodes_.emplace(
74 "read", std::make_unique<bluetooth::test::headless::Read>(options));
75 test_nodes_.emplace(
76 "sdp", std::make_unique<bluetooth::test::headless::Sdp>(options));
77 }
78
Run()79 int Run() override {
80 if (options_.close_stderr_) {
81 fclose(stderr);
82 }
83
84 if (options_.clear_logcat_) {
85 clear_logcat();
86 }
87
88 return HeadlessTest<int>::Run();
89 }
90 };
91
92 } // namespace
93
main(int argc,char ** argv)94 int main(int argc, char** argv) {
95 fflush(nullptr);
96 setvbuf(stdout, nullptr, _IOLBF, 0);
97
98 bluetooth::test::headless::GetOpt options(argc, argv);
99 if (!options.IsValid()) {
100 return -1;
101 }
102
103 Main main(options);
104 return main.Run();
105 }
106