• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "os/log.h"
24 #include "test/headless/get_options.h"
25 #include "test/headless/headless.h"
26 #include "test/headless/interface.h"
27 #include "test/headless/log.h"
28 #include "test/headless/messenger.h"
29 #include "test/headless/property.h"
30 #include "test/headless/stopwatch.h"
31 
32 using namespace bluetooth::test;
33 using namespace bluetooth;
34 using namespace std::chrono_literals;
35 
36 namespace {
37 
start_scan(unsigned int num_loops)38 int start_scan([[maybe_unused]] unsigned int num_loops) {
39   log::info("Started Device Scan");
40 
41   log::assert_that(bluetoothInterface.start_discovery() == BT_STATUS_SUCCESS,
42                    "assert failed: bluetoothInterface.start_discovery() == "
43                    "BT_STATUS_SUCCESS");
44   LOG_CONSOLE("Started inquiry - device discovery");
45 
46   headless::messenger::Context context{
47       .stop_watch = Stopwatch("Inquiry_timeout"),
48       .timeout = 1s,
49       .check_point = {},
50       .callbacks = {Callback::RemoteDeviceProperties, Callback::DeviceFound},
51   };
52 
53   while (context.stop_watch.LapMs() < 10000) {
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::RemoteDeviceProperties: {
61             remote_device_properties_params_t* q =
62                 static_cast<remote_device_properties_params_t*>(p.get());
63             for (const auto& p2 : q->properties()) {
64               LOG_CONSOLE("  %s prop:%s", p->Name().c_str(),
65                           p2->ToString().c_str());
66             }
67           } break;
68           case Callback::DeviceFound: {
69             device_found_params_t* q =
70                 static_cast<device_found_params_t*>(p.get());
71             for (const auto& p2 : q->properties()) {
72               LOG_CONSOLE("  %s prop:%s", p->Name().c_str(),
73                           p2->ToString().c_str());
74             }
75           } break;
76           default:
77             LOG_CONSOLE("WARN Received callback for unasked:%s",
78                         p->Name().c_str());
79             break;
80         }
81       }
82     }
83   }
84 
85   LOG_CONSOLE("Stopped inquiry - device discovery");
86   return 0;
87 }
88 
89 }  // namespace
90 
Run()91 int bluetooth::test::headless::Scan::Run() {
92   if (options_.loop_ < 1) {
93     LOG_CONSOLE("This test requires at least a single loop");
94     options_.Usage();
95     return -1;
96   }
97   return RunOnHeadlessStack<int>([this]() {
98     return start_scan(options_.loop_);
99   });
100 }
101