• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2023 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_scan"
18 
19 #include "test/headless/adapter/adapter.h"
20 
21 #include <bluetooth/log.h>
22 
23 #include "gd/os/log.h"
24 #include "test/headless/headless.h"
25 #include "test/headless/interface.h"
26 #include "test/headless/log.h"
27 #include "test/headless/messenger.h"
28 #include "test/headless/stopwatch.h"
29 
30 using namespace bluetooth::test;
31 using namespace bluetooth;
32 using namespace std::chrono_literals;
33 
34 namespace {
35 
36 unsigned kTimeoutMs = 5000;
37 
get_adapter_info(unsigned int num_loops)38 int get_adapter_info([[maybe_unused]] unsigned int num_loops) {
39   log::info("Started Device Adapter Properties");
40 
41   log::assert_that(
42       bluetoothInterface.get_adapter_properties() == BT_STATUS_SUCCESS,
43       "assert failed: bluetoothInterface.get_adapter_properties() == "
44       "BT_STATUS_SUCCESS");
45   LOG_CONSOLE("Started get adapter properties");
46 
47   headless::messenger::Context context{
48       .stop_watch = Stopwatch(__func__),
49       .timeout = 1s,  // Poll time
50       .check_point = {},
51       .callbacks = {Callback::AdapterProperties},
52   };
53 
54   bool adapter_properties_found = false;
55   while (context.stop_watch.LapMs() < kTimeoutMs) {
56     // If we have received callback results within this timeframe...
57     if (headless::messenger::await_callback(context)) {
58       while (!context.callback_ready_q.empty()) {
59         std::shared_ptr<callback_params_t> p = context.callback_ready_q.front();
60         context.callback_ready_q.pop_front();
61         switch (p->CallbackType()) {
62           case Callback::AdapterProperties: {
63             adapter_properties_params_t* q =
64                 static_cast<adapter_properties_params_t*>(p.get());
65             for (const auto& p2 : q->properties()) {
66               LOG_CONSOLE("  %s prop:%s", p->Name().c_str(),
67                           p2->ToString().c_str());
68             }
69             adapter_properties_found = true;
70           } break;
71           default:
72             LOG_CONSOLE("WARN Received callback for unasked:%s",
73                         p->Name().c_str());
74             break;
75         }
76       }
77     }
78     if (adapter_properties_found) break;
79   }
80 
81   LOG_CONSOLE("Retrieved adapter properties");
82   return 0;
83 }
84 
85 }  // namespace
86 
Run()87 int bluetooth::test::headless::Adapter::Run() {
88   if (options_.loop_ < 1) {
89     LOG_CONSOLE("This test requires at least a single loop");
90     options_.Usage();
91     return -1;
92   }
93   return RunOnHeadlessStack<int>(
94       [this]() { return get_adapter_info(options_.loop_); });
95 }
96