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_mode"
18 
19 #include "test/headless/mode/mode.h"
20 
21 #include <deque>
22 #include <future>
23 #include <memory>
24 
25 #include "btm_status.h"
26 #include "hci_error_code.h"
27 #include "main/shim/acl_api.h"
28 #include "stack/include/acl_api.h"
29 #include "test/headless/get_options.h"
30 #include "test/headless/headless.h"
31 #include "test/headless/messenger.h"
32 #include "test/headless/utils/power_mode_client.h"
33 #include "types/raw_address.h"
34 
35 using namespace bluetooth::test;
36 using namespace std::chrono_literals;
37 
38 namespace {
do_mode(unsigned int num_loops,const RawAddress & bd_addr,std::list<std::string> options)39 int do_mode([[maybe_unused]] unsigned int num_loops,
40             [[maybe_unused]] const RawAddress& bd_addr,
41             [[maybe_unused]] std::list<std::string> options) {
42   LOG_CONSOLE("Starting mode change test");
43   // Requires a BR_EDR connection to work
44 
45   headless::messenger::Context context{
46       .stop_watch = Stopwatch("Connect_timeout"),
47       .timeout = 3s,
48       .check_point = {},
49       .callbacks = {Callback::AclStateChanged},
50   };
51 
52   PowerMode power_mode;
53 
54   bluetooth::shim::ACL_CreateClassicConnection(bd_addr);
55 
56   std::shared_ptr<acl_state_changed_params_t> acl{nullptr};
57 
58   while (context.stop_watch.LapMs() < 10000) {
59     // If we have received callback results within this timeframe...
60     if (headless::messenger::await_callback(context)) {
61       while (!context.callback_ready_q.empty()) {
62         std::shared_ptr<callback_params_t> p = context.callback_ready_q.front();
63         context.callback_ready_q.pop_front();
64         switch (p->CallbackType()) {
65           case Callback::AclStateChanged: {
66             acl = Cast<acl_state_changed_params_t>(p);
67             LOG_CONSOLE("Acl state changed:%s", acl->ToString().c_str());
68           } break;
69           default:
70             LOG_CONSOLE("WARN Received callback for unasked:%s",
71                         p->Name().c_str());
72             break;
73         }
74       }
75     }
76     if (acl != nullptr) break;
77   }
78 
79   if (acl->state == BT_ACL_STATE_DISCONNECTED) {
80     LOG_CONSOLE("Connection failed");
81     return 1;
82   }
83 
84   LOG_CONSOLE("Connection completed");
85   PowerMode::Client client = power_mode.GetClient(bd_addr);
86 
87   {
88     pwr_command_t pwr_command;
89     pwr_result_t result = client.set_typical_sniff(std::move(pwr_command));
90     LOG_CONSOLE("Sniff mode command sent");
91     if (result.btm_status == BTM_CMD_STARTED) {
92       // This awaits the command status callback
93       power_mode_callback_t cmd_status = result.cmd_status_future.get();
94       LOG_CONSOLE("Sniff mode command complete:%s",
95                   cmd_status.ToString().c_str());
96       if (cmd_status.status == BTM_PM_STS_PENDING) {
97         LOG_CONSOLE("Sniff mode command accepted; awaiting mode change event");
98         power_mode_callback_t mode_event = result.mode_event_future.get();
99         LOG_CONSOLE("Sniff mode command complete:%s",
100                     mode_event.ToString().c_str());
101       } else {
102         client.remove_mode_event_promise();
103         LOG_CONSOLE("Command failed; no mode change event forthcoming");
104       }
105     } else {
106       LOG_CONSOLE("Smiff mode command failed:%s",
107                   btm_status_text(result.btm_status).c_str());
108     }
109   }
110 
111   {
112     pwr_command_t pwr_command;
113     pwr_result_t result = client.set_active(std::move(pwr_command));
114     LOG_CONSOLE("Active mode command sent");
115     if (result.btm_status == BTM_CMD_STARTED) {
116       power_mode_callback_t cmd_status = result.cmd_status_future.get();
117       LOG_CONSOLE("Active mode command complete:%s",
118                   cmd_status.ToString().c_str());
119       if (cmd_status.status == BTM_PM_STS_PENDING) {
120         LOG_CONSOLE("Active mode command accepted; awaiting mode change event");
121         power_mode_callback_t mode_event = result.mode_event_future.get();
122         LOG_CONSOLE("Active mode command complete:%s",
123                     mode_event.ToString().c_str());
124       } else {
125         client.remove_mode_event_promise();
126         LOG_CONSOLE("Command failed; no mode change event forthcoming");
127       }
128     } else {
129       LOG_CONSOLE("Active mode command failed:%s",
130                   btm_status_text(result.btm_status).c_str());
131     }
132   }
133 
134   LOG_CONSOLE("Disconnecting");
135   acl_disconnect_from_handle(acl->acl_handle, HCI_SUCCESS,
136                              "BT headless disconnect");
137   LOG_CONSOLE("Waiting to disconnect");
138 
139   sleep(3);
140 
141   return 0;
142 }
143 
144 }  // namespace
145    //
Run()146 int bluetooth::test::headless::Mode::Run() {
147   return RunOnHeadlessStack<int>([this]() {
148     return do_mode(options_.loop_, options_.device_.front(),
149                    options_.non_options_);
150   });
151 }
152