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 #pragma once 18 19 #include <bluetooth/log.h> 20 #include <unistd.h> 21 22 #include <unordered_map> 23 24 #include "include/hardware/bluetooth.h" 25 #include "test/headless/bt_stack_info.h" 26 #include "test/headless/get_options.h" 27 #include "test/headless/log.h" 28 29 extern bt_interface_t bluetoothInterface; 30 31 namespace bluetooth { 32 namespace test { 33 namespace headless { 34 35 template <typename T> 36 using ExecutionUnit = std::function<T()>; 37 38 constexpr char kHeadlessInitialSentinel[] = 39 " INITIAL HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS"; 40 constexpr char kHeadlessStartSentinel[] = 41 " START HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS"; 42 constexpr char kHeadlessStopSentinel[] = 43 " STOP HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS"; 44 constexpr char kHeadlessFinalSentinel[] = 45 " FINAL HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS HEADLESS"; 46 47 class HeadlessStack { 48 protected: HeadlessStack(const char ** stack_init_flags)49 HeadlessStack(const char** stack_init_flags) 50 : stack_init_flags_(stack_init_flags) {} 51 virtual ~HeadlessStack() = default; 52 53 void SetUp(); 54 void TearDown(); 55 StackInitFlags()56 const char** StackInitFlags() const { return stack_init_flags_; } 57 58 private: 59 const char** stack_init_flags_; 60 std::unique_ptr<BtStackInfo> bt_stack_info_; 61 }; 62 63 class HeadlessRun : public HeadlessStack { 64 protected: 65 const bluetooth::test::headless::GetOpt& options_; 66 unsigned long loop_{0}; 67 HeadlessRun(const bluetooth::test::headless::GetOpt & options)68 HeadlessRun(const bluetooth::test::headless::GetOpt& options) 69 : HeadlessStack(options.StackInitFlags()), options_(options) {} 70 71 template <typename T> RunOnHeadlessStack(ExecutionUnit<T> func)72 T RunOnHeadlessStack(ExecutionUnit<T> func) { 73 log::info("{}", kHeadlessInitialSentinel); 74 SetUp(); 75 log::info("{}", kHeadlessStartSentinel); 76 77 T rc; 78 for (loop_ = 0; loop_ < options_.loop_; loop_++) { 79 LOG_CONSOLE("Loop started: %lu", loop_); 80 rc = func(); 81 if (options_.msec_ != 0) { 82 usleep(options_.msec_ * 1000); 83 } 84 if (rc) { 85 break; 86 } 87 LOG_CONSOLE("Loop completed: %lu", loop_); 88 } 89 if (rc) { 90 log::error("FAIL:{} loop/loops:{}/{}", rc, loop_, options_.loop_); 91 } else { 92 log::info("PASS:{} loop/loops:{}/{}", rc, loop_, options_.loop_); 93 } 94 95 log::info("{}", kHeadlessStopSentinel); 96 TearDown(); 97 log::info("{}", kHeadlessFinalSentinel); 98 return rc; 99 } 100 virtual ~HeadlessRun() = default; 101 }; 102 103 template <typename T> 104 class HeadlessTest : public HeadlessRun { 105 public: Run()106 virtual T Run() { 107 if (options_.non_options_.size() == 0) { 108 fprintf(stdout, "Must supply at least one subtest name\n"); 109 return -1; 110 } 111 112 std::string subtest = options_.GetNextSubTest(); 113 if (test_nodes_.find(subtest) == test_nodes_.end()) { 114 fprintf(stdout, "Unknown subtest module:%s\n", subtest.c_str()); 115 return -1; 116 } 117 return test_nodes_.at(subtest)->Run(); 118 } 119 120 virtual ~HeadlessTest() = default; 121 122 protected: HeadlessTest(const bluetooth::test::headless::GetOpt & options)123 HeadlessTest(const bluetooth::test::headless::GetOpt& options) 124 : HeadlessRun(options) {} 125 126 std::unordered_map<std::string, std::unique_ptr<HeadlessTest<T>>> test_nodes_; 127 }; 128 129 } // namespace headless 130 } // namespace test 131 } // namespace bluetooth 132