1 /*
2 * Copyright 2022 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/scan/scan.h"
20
21 #include <bluetooth/log.h>
22
23 #include "test/headless/get_options.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/property.h"
29 #include "test/headless/stopwatch.h"
30
31 using namespace bluetooth::test;
32 using namespace bluetooth;
33 using namespace std::chrono_literals;
34
35 namespace {
36
start_scan(unsigned int num_loops)37 int start_scan([[maybe_unused]] unsigned int num_loops) {
38 log::info("Started Device Scan");
39
40 log::assert_that(bluetoothInterface.start_discovery() == BT_STATUS_SUCCESS,
41 "assert failed: bluetoothInterface.start_discovery() == "
42 "BT_STATUS_SUCCESS");
43 LOG_CONSOLE("Started inquiry - device discovery");
44
45 headless::messenger::Context context{
46 .stop_watch = Stopwatch("Inquiry_timeout"),
47 .timeout = 1s,
48 .check_point = {},
49 .callbacks = {Callback::RemoteDeviceProperties, Callback::DeviceFound},
50 };
51
52 while (context.stop_watch.LapMs() < 10000) {
53 // If we have received callback results within this timeframe...
54 if (headless::messenger::await_callback(context)) {
55 while (!context.callback_ready_q.empty()) {
56 std::shared_ptr<callback_params_t> p = context.callback_ready_q.front();
57 context.callback_ready_q.pop_front();
58 switch (p->CallbackType()) {
59 case Callback::RemoteDeviceProperties: {
60 remote_device_properties_params_t* q =
61 static_cast<remote_device_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 } break;
66 case Callback::DeviceFound: {
67 device_found_params_t* q = static_cast<device_found_params_t*>(p.get());
68 for (const auto& p2 : q->properties()) {
69 LOG_CONSOLE(" %s prop:%s", p->Name().c_str(), p2->ToString().c_str());
70 }
71 } break;
72 default:
73 LOG_CONSOLE("WARN Received callback for unasked:%s", p->Name().c_str());
74 break;
75 }
76 }
77 }
78 }
79
80 LOG_CONSOLE("Stopped inquiry - device discovery");
81 return 0;
82 }
83
84 } // namespace
85
Run()86 int bluetooth::test::headless::Scan::Run() {
87 if (options_.loop_ < 1) {
88 LOG_CONSOLE("This test requires at least a single loop");
89 options_.Usage();
90 return -1;
91 }
92 return RunOnHeadlessStack<int>([this]() { return start_scan(options_.loop_); });
93 }
94