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