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