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