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, [[maybe_unused]] const RawAddress& bd_addr,
40             [[maybe_unused]] std::list<std::string> options) {
41   LOG_CONSOLE("Starting mode change test");
42   // Requires a BR_EDR connection to work
43 
44   headless::messenger::Context context{
45           .stop_watch = Stopwatch("Connect_timeout"),
46           .timeout = 3s,
47           .check_point = {},
48           .callbacks = {Callback::AclStateChanged},
49   };
50 
51   PowerMode power_mode;
52 
53   bluetooth::shim::ACL_CreateClassicConnection(bd_addr);
54 
55   std::shared_ptr<acl_state_changed_params_t> acl{nullptr};
56 
57   while (context.stop_watch.LapMs() < 10000) {
58     // If we have received callback results within this timeframe...
59     if (headless::messenger::await_callback(context)) {
60       while (!context.callback_ready_q.empty()) {
61         std::shared_ptr<callback_params_t> p = context.callback_ready_q.front();
62         context.callback_ready_q.pop_front();
63         switch (p->CallbackType()) {
64           case Callback::AclStateChanged: {
65             acl = Cast<acl_state_changed_params_t>(p);
66             LOG_CONSOLE("Acl state changed:%s", acl->ToString().c_str());
67           } break;
68           default:
69             LOG_CONSOLE("WARN Received callback for unasked:%s", p->Name().c_str());
70             break;
71         }
72       }
73     }
74     if (acl != nullptr) {
75       break;
76     }
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 == tBTM_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", cmd_status.ToString().c_str());
95       if (cmd_status.status == BTM_PM_STS_PENDING) {
96         LOG_CONSOLE("Sniff mode command accepted; awaiting mode change event");
97         power_mode_callback_t mode_event = result.mode_event_future.get();
98         LOG_CONSOLE("Sniff mode command complete:%s", mode_event.ToString().c_str());
99       } else {
100         client.remove_mode_event_promise();
101         LOG_CONSOLE("Command failed; no mode change event forthcoming");
102       }
103     } else {
104       LOG_CONSOLE("Smiff mode command failed:%s", btm_status_text(result.btm_status).c_str());
105     }
106   }
107 
108   {
109     pwr_command_t pwr_command;
110     pwr_result_t result = client.set_active(std::move(pwr_command));
111     LOG_CONSOLE("Active mode command sent");
112     if (result.btm_status == tBTM_STATUS::BTM_CMD_STARTED) {
113       power_mode_callback_t cmd_status = result.cmd_status_future.get();
114       LOG_CONSOLE("Active mode command complete:%s", cmd_status.ToString().c_str());
115       if (cmd_status.status == BTM_PM_STS_PENDING) {
116         LOG_CONSOLE("Active mode command accepted; awaiting mode change event");
117         power_mode_callback_t mode_event = result.mode_event_future.get();
118         LOG_CONSOLE("Active mode command complete:%s", mode_event.ToString().c_str());
119       } else {
120         client.remove_mode_event_promise();
121         LOG_CONSOLE("Command failed; no mode change event forthcoming");
122       }
123     } else {
124       LOG_CONSOLE("Active mode command failed:%s", btm_status_text(result.btm_status).c_str());
125     }
126   }
127 
128   LOG_CONSOLE("Disconnecting");
129   acl_disconnect_from_handle(acl->acl_handle, HCI_SUCCESS, "BT headless disconnect");
130   LOG_CONSOLE("Waiting to disconnect");
131 
132   sleep(3);
133 
134   return 0;
135 }
136 
137 }  // namespace
138    //
Run()139 int bluetooth::test::headless::Mode::Run() {
140   return RunOnHeadlessStack<int>([this]() {
141     return do_mode(options_.loop_, options_.device_.front(), options_.non_options_);
142   });
143 }
144