1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Staache 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 #include <vector>
18 
19 #include <VtsCoreUtil.h>
20 #include <aidl/Gtest.h>
21 #include <aidl/Vintf.h>
22 #include <aidl/android/hardware/wifi/BnWifi.h>
23 #include <aidl/android/hardware/wifi/BnWifiRttControllerEventCallback.h>
24 #include <android-base/logging.h>
25 #include <android/binder_manager.h>
26 #include <android/binder_status.h>
27 #include <binder/IServiceManager.h>
28 #include <binder/ProcessState.h>
29 
30 #include "wifi_aidl_test_utils.h"
31 
32 using aidl::android::hardware::wifi::BnWifiRttControllerEventCallback;
33 using aidl::android::hardware::wifi::IWifiRttController;
34 using aidl::android::hardware::wifi::RttBw;
35 using aidl::android::hardware::wifi::RttCapabilities;
36 using aidl::android::hardware::wifi::RttConfig;
37 using aidl::android::hardware::wifi::RttPeerType;
38 using aidl::android::hardware::wifi::RttPreamble;
39 using aidl::android::hardware::wifi::RttResponder;
40 using aidl::android::hardware::wifi::RttResult;
41 using aidl::android::hardware::wifi::RttType;
42 using aidl::android::hardware::wifi::WifiChannelInfo;
43 using aidl::android::hardware::wifi::WifiChannelWidthInMhz;
44 using aidl::android::hardware::wifi::WifiStatusCode;
45 
46 namespace {
47 const auto& kTestVendorDataOptional = generateOuiKeyedDataListOptional(5);
48 }
49 
50 class WifiRttControllerAidlTest : public testing::TestWithParam<std::string> {
51   public:
SetUp()52     void SetUp() override {
53         if (!::testing::deviceSupportsFeature("android.hardware.wifi.rtt"))
54             GTEST_SKIP() << "Skipping this test since RTT is not supported.";
55         stopWifiService(getInstanceName());
56         wifi_rtt_controller_ = getWifiRttController();
57         ASSERT_NE(nullptr, wifi_rtt_controller_.get());
58         ASSERT_TRUE(wifi_rtt_controller_->getInterfaceVersion(&interface_version_).isOk());
59 
60         // Check RTT support before we run the test.
61         RttCapabilities caps = {};
62         auto status = wifi_rtt_controller_->getCapabilities(&caps);
63         if (checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
64             GTEST_SKIP() << "Skipping this test since RTT is not supported.";
65         }
66     }
67 
TearDown()68     void TearDown() override { stopWifiService(getInstanceName()); }
69 
70   protected:
getWifiRttController()71     std::shared_ptr<IWifiRttController> getWifiRttController() {
72         std::shared_ptr<IWifiChip> wifi_chip = getWifiChip(getInstanceName());
73         EXPECT_NE(nullptr, wifi_chip.get());
74 
75         std::shared_ptr<IWifiStaIface> wifi_sta_iface = getWifiStaIface(getInstanceName());
76         EXPECT_NE(nullptr, wifi_sta_iface.get());
77 
78         std::shared_ptr<IWifiRttController> rtt_controller;
79         EXPECT_TRUE(wifi_chip->createRttController(wifi_sta_iface, &rtt_controller).isOk());
80         EXPECT_NE(nullptr, rtt_controller.get());
81         return rtt_controller;
82     }
83 
getCapabilities()84     RttCapabilities getCapabilities() {
85         RttCapabilities caps = {};
86         EXPECT_TRUE(wifi_rtt_controller_->getCapabilities(&caps).isOk());
87         return caps;
88     }
89 
90     std::shared_ptr<IWifiRttController> wifi_rtt_controller_;
91     int interface_version_;
92 
93   private:
getInstanceName()94     const char* getInstanceName() { return GetParam().c_str(); }
95 };
96 
97 class WifiRttControllerEventCallback : public BnWifiRttControllerEventCallback {
98   public:
99     WifiRttControllerEventCallback() = default;
100 
onResults(int,const std::vector<RttResult> &)101     ::ndk::ScopedAStatus onResults(int /* cmdId */,
102                                    const std::vector<RttResult>& /* results */) override {
103         return ndk::ScopedAStatus::ok();
104     }
105 };
106 
107 /*
108  * RegisterEventCallback
109  *
110  * Note: it is not feasible to test the invocation of the callback function,
111  * since events are triggered internally in the HAL implementation and cannot be
112  * triggered from the test case.
113  */
TEST_P(WifiRttControllerAidlTest,RegisterEventCallback)114 TEST_P(WifiRttControllerAidlTest, RegisterEventCallback) {
115     std::shared_ptr<WifiRttControllerEventCallback> callback =
116             ndk::SharedRefBase::make<WifiRttControllerEventCallback>();
117     ASSERT_NE(nullptr, callback.get());
118     EXPECT_TRUE(wifi_rtt_controller_->registerEventCallback(callback).isOk());
119 }
120 
121 /*
122  * GetCapabilities
123  */
TEST_P(WifiRttControllerAidlTest,GetCapabilities)124 TEST_P(WifiRttControllerAidlTest, GetCapabilities) {
125     RttCapabilities caps = {};
126     EXPECT_TRUE(wifi_rtt_controller_->getCapabilities(&caps).isOk());
127 }
128 
129 /*
130  * GetResponderInfo
131  */
TEST_P(WifiRttControllerAidlTest,GetResponderInfo)132 TEST_P(WifiRttControllerAidlTest, GetResponderInfo) {
133     RttCapabilities caps = getCapabilities();
134     if (!caps.responderSupported) {
135         GTEST_SKIP() << "Skipping because responder is not supported";
136     }
137 
138     RttResponder responder = {};
139     EXPECT_TRUE(wifi_rtt_controller_->getResponderInfo(&responder).isOk());
140 }
141 
142 /*
143  * EnableResponder
144  */
TEST_P(WifiRttControllerAidlTest,EnableResponder)145 TEST_P(WifiRttControllerAidlTest, EnableResponder) {
146     RttCapabilities caps = getCapabilities();
147     if (!caps.responderSupported) {
148         GTEST_SKIP() << "Skipping because responder is not supported";
149     }
150 
151     int cmdId = 55;
152     WifiChannelInfo channelInfo;
153     channelInfo.width = WifiChannelWidthInMhz::WIDTH_80;
154     channelInfo.centerFreq = 5660;
155     channelInfo.centerFreq0 = 5660;
156     channelInfo.centerFreq1 = 0;
157 
158     RttResponder responder = {};
159     EXPECT_TRUE(wifi_rtt_controller_->getResponderInfo(&responder).isOk());
160     EXPECT_TRUE(wifi_rtt_controller_->enableResponder(cmdId, channelInfo, 10, responder).isOk());
161 }
162 
163 /*
164  * Request80211azNtbRangeMeasurement
165  * Tests the two sided 11az non-trigger based ranging - 802.11az NTB FTM protocol.
166  */
TEST_P(WifiRttControllerAidlTest,Request80211azNtbRangeMeasurement)167 TEST_P(WifiRttControllerAidlTest, Request80211azNtbRangeMeasurement) {
168     if (interface_version_ < 2) {
169         GTEST_SKIP() << "Request80211azNtbRangeMeasurement is available as of RttController V2";
170     }
171 
172     RttCapabilities caps = getCapabilities();
173     if (!caps.ntbInitiatorSupported) {
174         GTEST_SKIP() << "Skipping 11az NTB RTT since driver/fw does not support";
175     }
176 
177     RttConfig config;
178     config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
179     config.type = RttType::TWO_SIDED_11AZ_NTB;
180     config.peer = RttPeerType::AP;
181     config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
182     config.channel.centerFreq = 5180;
183     config.channel.centerFreq0 = 5210;
184     config.channel.centerFreq1 = 0;
185     config.bw = RttBw::BW_20MHZ;
186     config.preamble = RttPreamble::HT;
187     config.mustRequestLci = false;
188     config.mustRequestLcr = false;
189     config.numFramesPerBurst = 8;
190     config.numRetriesPerRttFrame = 0;
191     config.numRetriesPerFtmr = 0;
192     // 11az non-trigger based minimum measurement time in units of 100 microseconds.
193     config.ntbMinMeasurementTime = 2500;
194     // 11az non-trigger based maximum measurement time in units of 10 milliseconds.
195     config.ntbMaxMeasurementTime = 1500;
196 
197     int cmdId = 55;
198     std::vector<RttConfig> configs = {config};
199     EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
200 
201     // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
202     sleep(2);
203 }
204 
205 /*
206  * Request2SidedRangeMeasurement
207  * Tests the two sided ranging - 802.11mc FTM protocol.
208  */
TEST_P(WifiRttControllerAidlTest,Request2SidedRangeMeasurement)209 TEST_P(WifiRttControllerAidlTest, Request2SidedRangeMeasurement) {
210     RttCapabilities caps = getCapabilities();
211     if (!caps.rttFtmSupported) {
212         GTEST_SKIP() << "Skipping two sided RTT since driver/fw does not support";
213     }
214 
215     RttConfig config;
216     config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
217     config.type = RttType::TWO_SIDED;
218     config.peer = RttPeerType::AP;
219     config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
220     config.channel.centerFreq = 5180;
221     config.channel.centerFreq0 = 5210;
222     config.channel.centerFreq1 = 0;
223     config.bw = RttBw::BW_20MHZ;
224     config.preamble = RttPreamble::HT;
225     config.mustRequestLci = false;
226     config.mustRequestLcr = false;
227     config.burstPeriod = 0;
228     config.numBurst = 0;
229     config.numFramesPerBurst = 8;
230     config.numRetriesPerRttFrame = 0;
231     config.numRetriesPerFtmr = 0;
232     config.burstDuration = 9;
233 
234     int cmdId = 55;
235     std::vector<RttConfig> configs = {config};
236     EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
237 
238     // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
239     sleep(2);
240 }
241 
242 /*
243  * RangeRequest
244  */
TEST_P(WifiRttControllerAidlTest,RangeRequest)245 TEST_P(WifiRttControllerAidlTest, RangeRequest) {
246     RttCapabilities caps = getCapabilities();
247     if (!caps.rttOneSidedSupported) {
248         GTEST_SKIP() << "Skipping one sided RTT since driver/fw does not support";
249     }
250 
251     // Get the highest supported preamble.
252     int preamble = 1;
253     int caps_preamble_support = static_cast<int>(caps.preambleSupport);
254     caps_preamble_support >>= 1;
255     while (caps_preamble_support != 0) {
256         caps_preamble_support >>= 1;
257         preamble <<= 1;
258     }
259 
260     RttConfig config;
261     config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
262     config.type = RttType::ONE_SIDED;
263     config.peer = RttPeerType::AP;
264     config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
265     config.channel.centerFreq = 5765;
266     config.channel.centerFreq0 = 5775;
267     config.channel.centerFreq1 = 0;
268     config.bw = RttBw::BW_80MHZ;
269     config.preamble = static_cast<RttPreamble>(preamble);
270     config.mustRequestLci = false;
271     config.mustRequestLcr = false;
272     config.burstPeriod = 0;
273     config.numBurst = 0;
274     config.numFramesPerBurst = 8;
275     config.numRetriesPerRttFrame = 3;
276     config.numRetriesPerFtmr = 3;
277     config.burstDuration = 9;
278     if (interface_version_ >= 2) {
279         LOG(INFO) << "Including vendor data in Rtt Config";
280         config.vendorData = kTestVendorDataOptional;
281     }
282 
283     int cmdId = 55;
284     std::vector<RttConfig> configs = {config};
285     EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
286 
287     // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
288     sleep(2);
289 }
290 
291 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiRttControllerAidlTest);
292 INSTANTIATE_TEST_SUITE_P(WifiTest, WifiRttControllerAidlTest,
293                          testing::ValuesIn(android::getAidlHalInstanceNames(IWifi::descriptor)),
294                          android::PrintInstanceNameToString);
295 
main(int argc,char ** argv)296 int main(int argc, char** argv) {
297     ::testing::InitGoogleTest(&argc, argv);
298     android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
299     android::ProcessState::self()->startThreadPool();
300     return RUN_ALL_TESTS();
301 }
302