1 /*
2 * Copyright (C) 2020 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 #include <VtsHalHidlTargetCallbackBase.h>
18 #include <android-base/logging.h>
19
20 #undef NAN // NAN is defined in bionic/libc/include/math.h:38
21
22 #include <VtsCoreUtil.h>
23 #include <android/hardware/wifi/1.3/IWifiStaIface.h>
24 #include <android/hardware/wifi/1.4/IWifi.h>
25 #include <android/hardware/wifi/1.4/IWifiChip.h>
26 #include <android/hardware/wifi/1.4/IWifiRttController.h>
27 #include <android/hardware/wifi/1.4/IWifiRttControllerEventCallback.h>
28 #include <gtest/gtest.h>
29 #include <hidl/GtestPrinter.h>
30 #include <hidl/ServiceManagement.h>
31
32 #include "wifi_hidl_call_util.h"
33 #include "wifi_hidl_test_utils.h"
34
35 using ::android::sp;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::hardware::wifi::V1_0::CommandId;
40 using ::android::hardware::wifi::V1_0::RttBw;
41 using ::android::hardware::wifi::V1_0::RttPeerType;
42 using ::android::hardware::wifi::V1_0::RttType;
43 using ::android::hardware::wifi::V1_0::WifiChannelInfo;
44 using ::android::hardware::wifi::V1_0::WifiChannelWidthInMhz;
45 using ::android::hardware::wifi::V1_0::WifiStatus;
46 using ::android::hardware::wifi::V1_0::WifiStatusCode;
47 using ::android::hardware::wifi::V1_3::IWifiStaIface;
48 using ::android::hardware::wifi::V1_4::IWifiChip;
49 using ::android::hardware::wifi::V1_4::IWifiRttController;
50 using ::android::hardware::wifi::V1_4::IWifiRttControllerEventCallback;
51 using ::android::hardware::wifi::V1_4::RttCapabilities;
52 using ::android::hardware::wifi::V1_4::RttConfig;
53 using ::android::hardware::wifi::V1_4::RttPreamble;
54 using ::android::hardware::wifi::V1_4::RttResponder;
55 using ::android::hardware::wifi::V1_4::RttResult;
56
57 /**
58 * Fixture to use for all RTT controller HIDL interface tests.
59 */
60 class WifiRttControllerHidlTest : public ::testing::TestWithParam<std::string> {
61 public:
SetUp()62 virtual void SetUp() override {
63 if (!::testing::deviceSupportsFeature("android.hardware.wifi.rtt"))
64 GTEST_SKIP() << "Skipping this test since RTT is not supported.";
65 // Make sure to start with a clean state
66 stopWifi(GetInstanceName());
67
68 wifi_rtt_controller_ = getWifiRttController();
69 if (wifi_rtt_controller_.get() == nullptr) {
70 GTEST_SKIP() << "Skipping this test since API is deprecated.";
71 }
72
73 // Check RTT support before we run the test.
74 std::pair<WifiStatus, RttCapabilities> status_and_caps;
75 status_and_caps =
76 HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_4);
77 if (status_and_caps.first.code == WifiStatusCode::ERROR_NOT_SUPPORTED) {
78 GTEST_SKIP() << "Skipping this test since RTT is not supported.";
79 }
80 }
81
TearDown()82 virtual void TearDown() override { stopWifi(GetInstanceName()); }
83
84 // A simple test implementation of WifiRttControllerEventCallback.
85 class WifiRttControllerEventCallback
86 : public ::testing::VtsHalHidlTargetCallbackBase<
87 WifiRttControllerHidlTest>,
88 public IWifiRttControllerEventCallback {
89 public:
WifiRttControllerEventCallback()90 WifiRttControllerEventCallback(){};
91
92 virtual ~WifiRttControllerEventCallback() = default;
93
onResults(CommandId cmdId __unused,const hidl_vec<::android::hardware::wifi::V1_0::RttResult> & results __unused)94 Return<void> onResults(
95 CommandId cmdId __unused,
96 const hidl_vec<::android::hardware::wifi::V1_0::RttResult>& results
97 __unused) {
98 return Void();
99 };
100
onResults_1_4(CommandId cmdId __unused,const hidl_vec<RttResult> & results __unused)101 Return<void> onResults_1_4(CommandId cmdId __unused,
102 const hidl_vec<RttResult>& results
103 __unused) {
104 return Void();
105 };
106 };
107
108 protected:
109 sp<IWifiRttController> wifi_rtt_controller_;
110
111 private:
GetInstanceName()112 std::string GetInstanceName() { return GetParam(); }
113
getWifiRttController()114 sp<IWifiRttController> getWifiRttController() {
115 const std::string& instance_name = GetInstanceName();
116
117 sp<IWifiChip> wifi_chip =
118 IWifiChip::castFrom(getWifiChip(instance_name));
119 EXPECT_NE(nullptr, wifi_chip.get());
120
121 sp<IWifiStaIface> wifi_sta_iface =
122 IWifiStaIface::castFrom(getWifiStaIface(instance_name));
123 EXPECT_NE(nullptr, wifi_sta_iface.get());
124
125 const auto& status_and_controller =
126 HIDL_INVOKE(wifi_chip, createRttController_1_4, wifi_sta_iface);
127
128 if (status_and_controller.first.code == WifiStatusCode::ERROR_NOT_SUPPORTED) {
129 return nullptr;
130 }
131
132 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_controller.first.code);
133 EXPECT_NE(nullptr, status_and_controller.second.get());
134
135 return status_and_controller.second.get();
136 }
137 };
138
139 /*
140 * registerEventCallback_1_4
141 * This test case tests the registerEventCallback_1_4() API which registers
142 * a call back function with the hal implementation
143 *
144 * Note: it is not feasible to test the invocation of the call back function
145 * since event is triggered internally in the HAL implementation, and can not be
146 * triggered from the test case
147 */
TEST_P(WifiRttControllerHidlTest,RegisterEventCallback_1_4)148 TEST_P(WifiRttControllerHidlTest, RegisterEventCallback_1_4) {
149 sp<WifiRttControllerEventCallback> wifiRttControllerEventCallback =
150 new WifiRttControllerEventCallback();
151 const auto& status =
152 HIDL_INVOKE(wifi_rtt_controller_, registerEventCallback_1_4,
153 wifiRttControllerEventCallback);
154 EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
155 }
156
157 /*
158 * Request2SidedRangeMeasurement
159 * This test case tests the two sided ranging - 802.11mc FTM protocol.
160 */
TEST_P(WifiRttControllerHidlTest,Request2SidedRangeMeasurement)161 TEST_P(WifiRttControllerHidlTest, Request2SidedRangeMeasurement) {
162 std::pair<WifiStatus, RttCapabilities> status_and_caps;
163
164 // Get the Capabilities
165 status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_4);
166 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
167 if (!status_and_caps.second.rttFtmSupported) {
168 GTEST_SKIP()
169 << "Skipping two sided RTT since driver/fw doesn't support";
170 }
171 std::vector<RttConfig> configs;
172 RttConfig config;
173 int cmdId = 55;
174 // Set the config with test data
175 for (int i = 0; i < 6; i++) {
176 config.addr[i] = i;
177 }
178 config.type = RttType::TWO_SIDED;
179 config.peer = RttPeerType::AP;
180 config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
181 config.channel.centerFreq = 5180;
182 config.channel.centerFreq0 = 5210;
183 config.channel.centerFreq1 = 0;
184 config.bw = RttBw::BW_20MHZ;
185 config.preamble = RttPreamble::HT;
186 config.mustRequestLci = false;
187 config.mustRequestLcr = false;
188 config.burstPeriod = 0;
189 config.numBurst = 0;
190 config.numFramesPerBurst = 8;
191 config.numRetriesPerRttFrame = 0;
192 config.numRetriesPerFtmr = 0;
193 config.burstDuration = 9;
194 // Insert config in the vector
195 configs.push_back(config);
196
197 // Invoke the call
198 const auto& status =
199 HIDL_INVOKE(wifi_rtt_controller_, rangeRequest_1_4, cmdId, configs);
200 EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
201 // sleep for 2 seconds to wait for driver/firmware to complete RTT
202 sleep(2);
203 }
204 /*
205 * rangeRequest_1_4
206 */
TEST_P(WifiRttControllerHidlTest,RangeRequest_1_4)207 TEST_P(WifiRttControllerHidlTest, RangeRequest_1_4) {
208 std::pair<WifiStatus, RttCapabilities> status_and_caps;
209
210 // Get the Capabilities
211 status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_4);
212 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
213 if (!status_and_caps.second.rttOneSidedSupported) {
214 GTEST_SKIP()
215 << "Skipping one sided RTT since driver/fw doesn't support";
216 }
217 // Get the highest support preamble
218 int preamble = 1;
219 status_and_caps.second.preambleSupport >>= 1;
220 while (status_and_caps.second.preambleSupport != 0) {
221 status_and_caps.second.preambleSupport >>= 1;
222 preamble <<= 1;
223 }
224 std::vector<RttConfig> configs;
225 RttConfig config;
226 int cmdId = 55;
227 // Set the config with test data
228 for (int i = 0; i < 6; i++) {
229 config.addr[i] = i;
230 }
231 config.type = RttType::ONE_SIDED;
232 config.peer = RttPeerType::AP;
233 config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
234 config.channel.centerFreq = 5765;
235 config.channel.centerFreq0 = 5775;
236 config.channel.centerFreq1 = 0;
237 config.bw = RttBw::BW_80MHZ;
238 config.preamble = (RttPreamble)preamble;
239 config.mustRequestLci = false;
240 config.mustRequestLcr = false;
241 config.burstPeriod = 0;
242 config.numBurst = 0;
243 config.numFramesPerBurst = 8;
244 config.numRetriesPerRttFrame = 3;
245 config.numRetriesPerFtmr = 3;
246 config.burstDuration = 9;
247 // Insert config in the vector
248 configs.push_back(config);
249
250 // Invoke the call
251 const auto& status =
252 HIDL_INVOKE(wifi_rtt_controller_, rangeRequest_1_4, cmdId, configs);
253 EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
254 // sleep for 2 seconds to wait for driver/firmware to complete RTT
255 sleep(2);
256 }
257
258 /*
259 * getCapabilities_1_4
260 */
TEST_P(WifiRttControllerHidlTest,GetCapabilities_1_4)261 TEST_P(WifiRttControllerHidlTest, GetCapabilities_1_4) {
262 std::pair<WifiStatus, RttCapabilities> status_and_caps;
263
264 // Invoke the call
265 status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_4);
266 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
267 }
268
269 /*
270 * getResponderInfo_1_4
271 */
TEST_P(WifiRttControllerHidlTest,GetResponderInfo_1_4)272 TEST_P(WifiRttControllerHidlTest, GetResponderInfo_1_4) {
273 std::pair<WifiStatus, RttResponder> status_and_info;
274
275 // Invoke the call
276 status_and_info = HIDL_INVOKE(wifi_rtt_controller_, getResponderInfo_1_4);
277 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_info.first.code);
278 }
279
280 /*
281 * enableResponder_1_4
282 */
TEST_P(WifiRttControllerHidlTest,EnableResponder_1_4)283 TEST_P(WifiRttControllerHidlTest, EnableResponder_1_4) {
284 std::pair<WifiStatus, RttResponder> status_and_info;
285 int cmdId = 55;
286 WifiChannelInfo channelInfo;
287 channelInfo.width = WifiChannelWidthInMhz::WIDTH_80;
288 channelInfo.centerFreq = 5660;
289 channelInfo.centerFreq0 = 5660;
290 channelInfo.centerFreq1 = 0;
291
292 // Get the responder first
293 status_and_info = HIDL_INVOKE(wifi_rtt_controller_, getResponderInfo_1_4);
294 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_info.first.code);
295
296 // Invoke the call
297 const auto& status =
298 HIDL_INVOKE(wifi_rtt_controller_, enableResponder_1_4, cmdId,
299 channelInfo, 10, status_and_info.second);
300 EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
301 }
302
303 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiRttControllerHidlTest);
304 INSTANTIATE_TEST_SUITE_P(
305 PerInstance, WifiRttControllerHidlTest,
306 testing::ValuesIn(android::hardware::getAllHalInstanceNames(
307 ::android::hardware::wifi::V1_4::IWifi::descriptor)),
308 android::hardware::PrintInstanceNameToString);
309