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/Akm.h>
23 #include <aidl/android/hardware/wifi/BnWifi.h>
24 #include <aidl/android/hardware/wifi/BnWifiRttControllerEventCallback.h>
25 #include <aidl/android/hardware/wifi/RttSecureConfig.h>
26 #include <android-base/logging.h>
27 #include <android/binder_manager.h>
28 #include <android/binder_status.h>
29 #include <binder/IServiceManager.h>
30 #include <binder/ProcessState.h>
31
32 #include "wifi_aidl_test_utils.h"
33
34 using aidl::android::hardware::wifi::Akm;
35 using aidl::android::hardware::wifi::BnWifiRttControllerEventCallback;
36 using aidl::android::hardware::wifi::IWifiRttController;
37 using aidl::android::hardware::wifi::RttBw;
38 using aidl::android::hardware::wifi::RttCapabilities;
39 using aidl::android::hardware::wifi::RttConfig;
40 using aidl::android::hardware::wifi::RttLciInformation;
41 using aidl::android::hardware::wifi::RttLcrInformation;
42 using aidl::android::hardware::wifi::RttPeerType;
43 using aidl::android::hardware::wifi::RttPreamble;
44 using aidl::android::hardware::wifi::RttResponder;
45 using aidl::android::hardware::wifi::RttResult;
46 using aidl::android::hardware::wifi::RttSecureConfig;
47 using aidl::android::hardware::wifi::RttType;
48 using aidl::android::hardware::wifi::WifiChannelInfo;
49 using aidl::android::hardware::wifi::WifiChannelWidthInMhz;
50 using aidl::android::hardware::wifi::WifiStatusCode;
51
52 namespace {
53 const auto& kTestVendorDataOptional = generateOuiKeyedDataListOptional(5);
54 }
55
56 class WifiRttControllerAidlTest : public testing::TestWithParam<std::string> {
57 public:
SetUp()58 void SetUp() override {
59 if (!::testing::deviceSupportsFeature("android.hardware.wifi.rtt"))
60 GTEST_SKIP() << "Skipping this test since RTT is not supported.";
61 stopWifiService(getInstanceName());
62 wifi_rtt_controller_ = getWifiRttController();
63 ASSERT_NE(nullptr, wifi_rtt_controller_.get());
64 ASSERT_TRUE(wifi_rtt_controller_->getInterfaceVersion(&interface_version_).isOk());
65
66 // Check RTT support before we run the test.
67 RttCapabilities caps = {};
68 auto status = wifi_rtt_controller_->getCapabilities(&caps);
69 if (checkStatusCode(&status, WifiStatusCode::ERROR_NOT_SUPPORTED)) {
70 GTEST_SKIP() << "Skipping this test since RTT is not supported.";
71 }
72 }
73
TearDown()74 void TearDown() override { stopWifiService(getInstanceName()); }
75
76 protected:
getWifiRttController()77 std::shared_ptr<IWifiRttController> getWifiRttController() {
78 std::shared_ptr<IWifiChip> wifi_chip = getWifiChip(getInstanceName());
79 EXPECT_NE(nullptr, wifi_chip.get());
80
81 std::shared_ptr<IWifiStaIface> wifi_sta_iface = getWifiStaIface(getInstanceName());
82 EXPECT_NE(nullptr, wifi_sta_iface.get());
83
84 std::shared_ptr<IWifiRttController> rtt_controller;
85 EXPECT_TRUE(wifi_chip->createRttController(wifi_sta_iface, &rtt_controller).isOk());
86 EXPECT_NE(nullptr, rtt_controller.get());
87 return rtt_controller;
88 }
89
getCapabilities()90 RttCapabilities getCapabilities() {
91 RttCapabilities caps = {};
92 EXPECT_TRUE(wifi_rtt_controller_->getCapabilities(&caps).isOk());
93 return caps;
94 }
95
getMostSignificantSetBitMask(int n)96 int getMostSignificantSetBitMask(int n) {
97 if (n == 0) return 0;
98 int pos = std::numeric_limits<int>::digits - 1;
99 while ((n & (1 << pos)) == 0) {
100 pos--;
101 }
102 return 1 << pos;
103 }
104
105 std::shared_ptr<IWifiRttController> wifi_rtt_controller_;
106 int interface_version_;
107
108 private:
getInstanceName()109 const char* getInstanceName() { return GetParam().c_str(); }
110 };
111
112 class WifiRttControllerEventCallback : public BnWifiRttControllerEventCallback {
113 public:
114 WifiRttControllerEventCallback() = default;
115
onResults(int,const std::vector<RttResult> &)116 ::ndk::ScopedAStatus onResults(int /* cmdId */,
117 const std::vector<RttResult>& /* results */) override {
118 return ndk::ScopedAStatus::ok();
119 }
120 };
121
122 /*
123 * RegisterEventCallback
124 *
125 * Note: it is not feasible to test the invocation of the callback function,
126 * since events are triggered internally in the HAL implementation and cannot be
127 * triggered from the test case.
128 */
TEST_P(WifiRttControllerAidlTest,RegisterEventCallback)129 TEST_P(WifiRttControllerAidlTest, RegisterEventCallback) {
130 std::shared_ptr<WifiRttControllerEventCallback> callback =
131 ndk::SharedRefBase::make<WifiRttControllerEventCallback>();
132 ASSERT_NE(nullptr, callback.get());
133 EXPECT_TRUE(wifi_rtt_controller_->registerEventCallback(callback).isOk());
134 }
135
136 /*
137 * GetCapabilities
138 */
TEST_P(WifiRttControllerAidlTest,GetCapabilities)139 TEST_P(WifiRttControllerAidlTest, GetCapabilities) {
140 RttCapabilities caps = {};
141 EXPECT_TRUE(wifi_rtt_controller_->getCapabilities(&caps).isOk());
142 }
143
144 /*
145 * GetResponderInfo
146 */
TEST_P(WifiRttControllerAidlTest,GetResponderInfo)147 TEST_P(WifiRttControllerAidlTest, GetResponderInfo) {
148 RttCapabilities caps = getCapabilities();
149 if (!caps.responderSupported) {
150 GTEST_SKIP() << "Skipping because responder is not supported";
151 }
152
153 RttResponder responder = {};
154 EXPECT_TRUE(wifi_rtt_controller_->getResponderInfo(&responder).isOk());
155 }
156
157 /*
158 * EnableResponder
159 */
TEST_P(WifiRttControllerAidlTest,EnableResponder)160 TEST_P(WifiRttControllerAidlTest, EnableResponder) {
161 RttCapabilities caps = getCapabilities();
162 if (!caps.responderSupported) {
163 GTEST_SKIP() << "Skipping because responder is not supported";
164 }
165
166 int cmdId = 55;
167 WifiChannelInfo channelInfo;
168 channelInfo.width = WifiChannelWidthInMhz::WIDTH_80;
169 channelInfo.centerFreq = 5660;
170 channelInfo.centerFreq0 = 5660;
171 channelInfo.centerFreq1 = 0;
172
173 RttResponder responder = {};
174 EXPECT_TRUE(wifi_rtt_controller_->getResponderInfo(&responder).isOk());
175 EXPECT_TRUE(wifi_rtt_controller_->enableResponder(cmdId, channelInfo, 10, responder).isOk());
176 EXPECT_TRUE(wifi_rtt_controller_->disableResponder(cmdId).isOk());
177 }
178
179 /*
180 * Request80211azNtbSecureRangeMeasurement
181 * Tests the two sided 11az non-trigger based secure ranging - 802.11az NTB FTM protocol.
182 */
TEST_P(WifiRttControllerAidlTest,Request80211azNtbSecureRangeMeasurement)183 TEST_P(WifiRttControllerAidlTest, Request80211azNtbSecureRangeMeasurement) {
184 if (interface_version_ < 3) {
185 GTEST_SKIP() << "Request80211azNtbRangeMeasurement is available as of RttController V3";
186 }
187
188 RttCapabilities caps = getCapabilities();
189 if (!caps.ntbInitiatorSupported) {
190 GTEST_SKIP() << "Skipping 11az NTB RTT since driver/fw does not support";
191 }
192 if (!caps.secureHeLtfSupported && !caps.rangingFrameProtectionSupported) {
193 GTEST_SKIP() << "Skipping 11az NTB secure RTT since driver/fw does not support";
194 }
195 if (!(caps.akmsSupported & Akm::PASN)) {
196 GTEST_SKIP() << "Skipping 11az NTB secure RTT since driver/fw does not support PASN";
197 }
198 if (!caps.cipherSuitesSupported) {
199 GTEST_SKIP()
200 << "Skipping 11az NTB secure RTT since driver/fw does not support Cipher Suites";
201 }
202
203 RttConfig config;
204 config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
205 config.type = RttType::TWO_SIDED_11AZ_NTB_SECURE;
206 config.peer = RttPeerType::AP;
207 config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
208 config.channel.centerFreq = 5180;
209 config.channel.centerFreq0 = 5210;
210 config.channel.centerFreq1 = 0;
211 config.bw = RttBw::BW_20MHZ;
212 config.preamble = RttPreamble::HT;
213 config.mustRequestLci = false;
214 config.mustRequestLcr = false;
215 config.numFramesPerBurst = 8;
216 config.numRetriesPerRttFrame = 0;
217 config.numRetriesPerFtmr = 0;
218 // 11az non-trigger based minimum measurement time in units of 100 microseconds.
219 config.ntbMinMeasurementTime = 2500;
220 // 11az non-trigger based maximum measurement time in units of 10 milliseconds.
221 config.ntbMaxMeasurementTime = 1500;
222 RttSecureConfig secureConfig;
223 // PASN is a must to test secure config; which does not need any password.
224 secureConfig.pasnConfig.baseAkm = Akm::PASN;
225 // Get the best Cipher suite supported by the chip.
226 secureConfig.pasnConfig.cipherSuite = getMostSignificantSetBitMask(caps.cipherSuitesSupported);
227 secureConfig.enableSecureHeLtf = caps.secureHeLtfSupported;
228 secureConfig.enableRangingFrameProtection = caps.rangingFrameProtectionSupported;
229 config.secureConfig = secureConfig;
230
231 int cmdId = 55;
232 std::vector<RttConfig> configs = {config};
233 EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
234
235 // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
236 sleep(2);
237 }
238
239 /*
240 * Request80211azNtbRangeMeasurement
241 * Tests the two sided 11az non-trigger based ranging - 802.11az NTB FTM protocol.
242 */
TEST_P(WifiRttControllerAidlTest,Request80211azNtbRangeMeasurement)243 TEST_P(WifiRttControllerAidlTest, Request80211azNtbRangeMeasurement) {
244 if (interface_version_ < 2) {
245 GTEST_SKIP() << "Request80211azNtbRangeMeasurement is available as of RttController V2";
246 }
247
248 RttCapabilities caps = getCapabilities();
249 if (!caps.ntbInitiatorSupported) {
250 GTEST_SKIP() << "Skipping 11az NTB RTT since driver/fw does not support";
251 }
252
253 RttConfig config;
254 config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
255 config.type = RttType::TWO_SIDED_11AZ_NTB;
256 config.peer = RttPeerType::AP;
257 config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
258 config.channel.centerFreq = 5180;
259 config.channel.centerFreq0 = 5210;
260 config.channel.centerFreq1 = 0;
261 config.bw = RttBw::BW_20MHZ;
262 config.preamble = RttPreamble::HT;
263 config.mustRequestLci = false;
264 config.mustRequestLcr = false;
265 config.numFramesPerBurst = 8;
266 config.numRetriesPerRttFrame = 0;
267 config.numRetriesPerFtmr = 0;
268 // 11az non-trigger based minimum measurement time in units of 100 microseconds.
269 config.ntbMinMeasurementTime = 2500;
270 // 11az non-trigger based maximum measurement time in units of 10 milliseconds.
271 config.ntbMaxMeasurementTime = 1500;
272
273 int cmdId = 55;
274 std::vector<RttConfig> configs = {config};
275 EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
276
277 // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
278 sleep(2);
279 }
280
281 /*
282 * Request2SidedRangeMeasurement
283 * Tests the two sided ranging - 802.11mc FTM protocol.
284 */
TEST_P(WifiRttControllerAidlTest,Request2SidedRangeMeasurement)285 TEST_P(WifiRttControllerAidlTest, Request2SidedRangeMeasurement) {
286 RttCapabilities caps = getCapabilities();
287 if (!caps.rttFtmSupported) {
288 GTEST_SKIP() << "Skipping two sided RTT since driver/fw does not support";
289 }
290
291 RttConfig config;
292 config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
293 config.type = RttType::TWO_SIDED;
294 config.peer = RttPeerType::AP;
295 config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
296 config.channel.centerFreq = 5180;
297 config.channel.centerFreq0 = 5210;
298 config.channel.centerFreq1 = 0;
299 config.bw = RttBw::BW_20MHZ;
300 config.preamble = RttPreamble::HT;
301 config.mustRequestLci = false;
302 config.mustRequestLcr = false;
303 config.burstPeriod = 0;
304 config.numBurst = 0;
305 config.numFramesPerBurst = 8;
306 config.numRetriesPerRttFrame = 0;
307 config.numRetriesPerFtmr = 0;
308 config.burstDuration = 9;
309
310 int cmdId = 55;
311 std::vector<RttConfig> configs = {config};
312 EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
313
314 // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
315 sleep(2);
316 }
317
318 /*
319 * RangeRequest
320 */
TEST_P(WifiRttControllerAidlTest,RangeRequest)321 TEST_P(WifiRttControllerAidlTest, RangeRequest) {
322 RttCapabilities caps = getCapabilities();
323 if (!caps.rttOneSidedSupported) {
324 GTEST_SKIP() << "Skipping one sided RTT since driver/fw does not support";
325 }
326
327 // Get the highest supported preamble.
328 int preamble = 1;
329 int caps_preamble_support = static_cast<int>(caps.preambleSupport);
330 caps_preamble_support >>= 1;
331 while (caps_preamble_support != 0) {
332 caps_preamble_support >>= 1;
333 preamble <<= 1;
334 }
335
336 RttConfig config;
337 config.addr = {{0x00, 0x01, 0x02, 0x03, 0x04, 0x05}};
338 config.type = RttType::ONE_SIDED;
339 config.peer = RttPeerType::AP;
340 config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
341 config.channel.centerFreq = 5765;
342 config.channel.centerFreq0 = 5775;
343 config.channel.centerFreq1 = 0;
344 config.bw = RttBw::BW_80MHZ;
345 config.preamble = static_cast<RttPreamble>(preamble);
346 config.mustRequestLci = false;
347 config.mustRequestLcr = false;
348 config.burstPeriod = 0;
349 config.numBurst = 0;
350 config.numFramesPerBurst = 8;
351 config.numRetriesPerRttFrame = 3;
352 config.numRetriesPerFtmr = 3;
353 config.burstDuration = 9;
354 if (interface_version_ >= 2) {
355 LOG(INFO) << "Including vendor data in Rtt Config";
356 config.vendorData = kTestVendorDataOptional;
357 }
358
359 int cmdId = 55;
360 std::vector<RttConfig> configs = {config};
361 EXPECT_TRUE(wifi_rtt_controller_->rangeRequest(cmdId, configs).isOk());
362
363 // Sleep for 2 seconds to wait for driver/firmware to complete RTT.
364 sleep(2);
365 }
366
367 /*
368 * GetBoundIface
369 */
TEST_P(WifiRttControllerAidlTest,GetBoundIface)370 TEST_P(WifiRttControllerAidlTest, GetBoundIface) {
371 std::shared_ptr<IWifiStaIface> boundIface;
372 EXPECT_TRUE(wifi_rtt_controller_->getBoundIface(&boundIface).isOk());
373 EXPECT_NE(boundIface, nullptr);
374 }
375
376 /*
377 * Set LCI and LCR
378 */
TEST_P(WifiRttControllerAidlTest,SetLciAndLcr)379 TEST_P(WifiRttControllerAidlTest, SetLciAndLcr) {
380 RttCapabilities caps = getCapabilities();
381 if (!caps.responderSupported) {
382 GTEST_SKIP() << "Skipping because responder is not supported";
383 }
384
385 int cmdId = 55;
386 RttLciInformation lci = {};
387 RttLcrInformation lcr = {};
388 EXPECT_TRUE(wifi_rtt_controller_->setLci(cmdId, lci).isOk());
389 EXPECT_TRUE(wifi_rtt_controller_->setLcr(cmdId, lcr).isOk());
390 }
391
392 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiRttControllerAidlTest);
393 INSTANTIATE_TEST_SUITE_P(WifiTest, WifiRttControllerAidlTest,
394 testing::ValuesIn(android::getAidlHalInstanceNames(IWifi::descriptor)),
395 android::PrintInstanceNameToString);
396
main(int argc,char ** argv)397 int main(int argc, char** argv) {
398 ::testing::InitGoogleTest(&argc, argv);
399 android::ProcessState::self()->setThreadPoolMaxThreadCount(1);
400 android::ProcessState::self()->startThreadPool();
401 return RUN_ALL_TESTS();
402 }
403