1 /*
2  * Copyright (C) 2022 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.6/IWifi.h>
24 #include <android/hardware/wifi/1.6/IWifiChip.h>
25 #include <android/hardware/wifi/1.6/IWifiRttController.h>
26 #include <android/hardware/wifi/1.6/IWifiRttControllerEventCallback.h>
27 #include <android/hardware/wifi/1.6/IWifiStaIface.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::RttPeerType;
41 using ::android::hardware::wifi::V1_0::RttType;
42 using ::android::hardware::wifi::V1_0::WifiStatus;
43 using ::android::hardware::wifi::V1_0::WifiStatusCode;
44 using ::android::hardware::wifi::V1_6::IWifiChip;
45 using ::android::hardware::wifi::V1_6::IWifiRttController;
46 using ::android::hardware::wifi::V1_6::IWifiRttControllerEventCallback;
47 using ::android::hardware::wifi::V1_6::IWifiStaIface;
48 using ::android::hardware::wifi::V1_6::RttBw;
49 using ::android::hardware::wifi::V1_6::RttCapabilities;
50 using ::android::hardware::wifi::V1_6::RttConfig;
51 using ::android::hardware::wifi::V1_6::RttPreamble;
52 using ::android::hardware::wifi::V1_6::RttResponder;
53 using ::android::hardware::wifi::V1_6::RttResult;
54 using ::android::hardware::wifi::V1_6::WifiChannelInfo;
55 using ::android::hardware::wifi::V1_6::WifiChannelWidthInMhz;
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 = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_6);
74         if (status_and_caps.first.code == WifiStatusCode::ERROR_NOT_SUPPORTED) {
75             GTEST_SKIP() << "Skipping this test since RTT is not supported.";
76         }
77     }
78 
TearDown()79     virtual void TearDown() override { stopWifi(GetInstanceName()); }
80 
getRttCapabilities()81     RttCapabilities getRttCapabilities() {
82         std::pair<WifiStatus, RttCapabilities> status_and_caps;
83         status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_6);
84         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
85         return status_and_caps.second;
86     }
87 
88     // A simple test implementation of WifiRttControllerEventCallback.
89     class WifiRttControllerEventCallback
90         : public ::testing::VtsHalHidlTargetCallbackBase<WifiRttControllerHidlTest>,
91           public IWifiRttControllerEventCallback {
92       public:
WifiRttControllerEventCallback()93         WifiRttControllerEventCallback(){};
94 
95         virtual ~WifiRttControllerEventCallback() = default;
96 
onResults(CommandId cmdId __unused,const hidl_vec<::android::hardware::wifi::V1_0::RttResult> & results __unused)97         Return<void> onResults(CommandId cmdId __unused,
98                                const hidl_vec<::android::hardware::wifi::V1_0::RttResult>& results
99                                        __unused) {
100             return Void();
101         };
102 
onResults_1_4(CommandId cmdId __unused,const hidl_vec<::android::hardware::wifi::V1_4::RttResult> & results __unused)103         Return<void> onResults_1_4(
104                 CommandId cmdId __unused,
105                 const hidl_vec<::android::hardware::wifi::V1_4::RttResult>& results __unused) {
106             return Void();
107         };
108 
onResults_1_6(CommandId cmdId __unused,const hidl_vec<RttResult> & results __unused)109         Return<void> onResults_1_6(CommandId cmdId __unused,
110                                    const hidl_vec<RttResult>& results __unused) {
111             return Void();
112         };
113     };
114 
115   protected:
116     sp<IWifiRttController> wifi_rtt_controller_;
117 
118   private:
GetInstanceName()119     std::string GetInstanceName() { return GetParam(); }
120 
getWifiRttController()121     sp<IWifiRttController> getWifiRttController() {
122         const std::string& instance_name = GetInstanceName();
123 
124         sp<IWifiChip> wifi_chip = IWifiChip::castFrom(getWifiChip(instance_name));
125         EXPECT_NE(nullptr, wifi_chip.get());
126 
127         sp<IWifiStaIface> wifi_sta_iface = IWifiStaIface::castFrom(getWifiStaIface(instance_name));
128         EXPECT_NE(nullptr, wifi_sta_iface.get());
129 
130         const auto& status_and_controller =
131                 HIDL_INVOKE(wifi_chip, createRttController_1_6, wifi_sta_iface);
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_6
141  * This test case tests the registerEventCallback_1_6() 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_6)148 TEST_P(WifiRttControllerHidlTest, RegisterEventCallback_1_6) {
149     sp<WifiRttControllerEventCallback> wifiRttControllerEventCallback =
150             new WifiRttControllerEventCallback();
151     const auto& status = HIDL_INVOKE(wifi_rtt_controller_, registerEventCallback_1_6,
152                                      wifiRttControllerEventCallback);
153     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
154 }
155 
156 /*
157  * Request2SidedRangeMeasurement
158  * This test case tests the two sided ranging - 802.11mc FTM protocol.
159  */
TEST_P(WifiRttControllerHidlTest,Request2SidedRangeMeasurement)160 TEST_P(WifiRttControllerHidlTest, Request2SidedRangeMeasurement) {
161     // Get the Capabilities
162     RttCapabilities capabilities = getRttCapabilities();
163     if (!capabilities.rttFtmSupported) {
164         GTEST_SKIP() << "Skipping two sided RTT since driver/fw doesn't support";
165     }
166     std::vector<RttConfig> configs;
167     RttConfig config;
168     int cmdId = 55;
169     // Set the config with test data
170     for (int i = 0; i < 6; i++) {
171         config.addr[i] = i;
172     }
173     config.type = RttType::TWO_SIDED;
174     config.peer = RttPeerType::AP;
175     config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
176     config.channel.centerFreq = 5180;
177     config.channel.centerFreq0 = 5210;
178     config.channel.centerFreq1 = 0;
179     config.bw = RttBw::BW_20MHZ;
180     config.preamble = RttPreamble::HT;
181     config.mustRequestLci = false;
182     config.mustRequestLcr = false;
183     config.burstPeriod = 0;
184     config.numBurst = 0;
185     config.numFramesPerBurst = 8;
186     config.numRetriesPerRttFrame = 0;
187     config.numRetriesPerFtmr = 0;
188     config.burstDuration = 9;
189     // Insert config in the vector
190     configs.push_back(config);
191 
192     // Invoke the call
193     const auto& status = HIDL_INVOKE(wifi_rtt_controller_, rangeRequest_1_6, cmdId, configs);
194     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
195     // sleep for 2 seconds to wait for driver/firmware to complete RTT
196     sleep(2);
197 }
198 
199 /*
200  * rangeRequest_1_6
201  */
TEST_P(WifiRttControllerHidlTest,RangeRequest_1_6)202 TEST_P(WifiRttControllerHidlTest, RangeRequest_1_6) {
203     // Get the Capabilities
204     RttCapabilities capabilities = getRttCapabilities();
205     if (!capabilities.rttOneSidedSupported) {
206         GTEST_SKIP() << "Skipping one sided RTT since driver/fw doesn't support";
207     }
208     // Get the highest support preamble
209     int preamble = 1;
210     capabilities.preambleSupport >>= 1;
211     while (capabilities.preambleSupport != 0) {
212         capabilities.preambleSupport >>= 1;
213         preamble <<= 1;
214     }
215     std::vector<RttConfig> configs;
216     RttConfig config;
217     int cmdId = 55;
218     // Set the config with test data
219     for (int i = 0; i < 6; i++) {
220         config.addr[i] = i;
221     }
222     config.type = RttType::ONE_SIDED;
223     config.peer = RttPeerType::AP;
224     config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
225     config.channel.centerFreq = 5765;
226     config.channel.centerFreq0 = 5775;
227     config.channel.centerFreq1 = 0;
228     config.bw = RttBw::BW_80MHZ;
229     config.preamble = (RttPreamble)preamble;
230     config.mustRequestLci = false;
231     config.mustRequestLcr = false;
232     config.burstPeriod = 0;
233     config.numBurst = 0;
234     config.numFramesPerBurst = 8;
235     config.numRetriesPerRttFrame = 3;
236     config.numRetriesPerFtmr = 3;
237     config.burstDuration = 9;
238     // Insert config in the vector
239     configs.push_back(config);
240 
241     // Invoke the call
242     const auto& status = HIDL_INVOKE(wifi_rtt_controller_, rangeRequest_1_6, cmdId, configs);
243     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
244     // sleep for 2 seconds to wait for driver/firmware to complete RTT
245     sleep(2);
246 }
247 
248 /*
249  * getCapabilities_1_6
250  */
TEST_P(WifiRttControllerHidlTest,GetCapabilities_1_6)251 TEST_P(WifiRttControllerHidlTest, GetCapabilities_1_6) {
252     std::pair<WifiStatus, RttCapabilities> status_and_caps;
253 
254     // Invoke the call
255     status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_6);
256     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
257 }
258 
259 /*
260  * getResponderInfo_1_6
261  */
TEST_P(WifiRttControllerHidlTest,GetResponderInfo_1_6)262 TEST_P(WifiRttControllerHidlTest, GetResponderInfo_1_6) {
263     // Get the capabilities
264     RttCapabilities capabilities = getRttCapabilities();
265     if (!capabilities.responderSupported) {
266         GTEST_SKIP() << "Skipping because responder is not supported";
267     }
268 
269     // Invoke the call
270     std::pair<WifiStatus, RttResponder> status_and_info;
271     status_and_info = HIDL_INVOKE(wifi_rtt_controller_, getResponderInfo_1_6);
272     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_info.first.code);
273 }
274 
275 /*
276  * enableResponder_1_6
277  */
TEST_P(WifiRttControllerHidlTest,EnableResponder_1_6)278 TEST_P(WifiRttControllerHidlTest, EnableResponder_1_6) {
279     // Get the capabilities
280     RttCapabilities capabilities = getRttCapabilities();
281     if (!capabilities.responderSupported) {
282         GTEST_SKIP() << "Skipping because responder is not supported";
283     }
284 
285     std::pair<WifiStatus, RttResponder> status_and_info;
286     int cmdId = 55;
287     WifiChannelInfo channelInfo;
288     channelInfo.width = WifiChannelWidthInMhz::WIDTH_80;
289     channelInfo.centerFreq = 5660;
290     channelInfo.centerFreq0 = 5660;
291     channelInfo.centerFreq1 = 0;
292 
293     // Get the responder first
294     status_and_info = HIDL_INVOKE(wifi_rtt_controller_, getResponderInfo_1_6);
295     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_info.first.code);
296 
297     // Invoke the call
298     const auto& status = HIDL_INVOKE(wifi_rtt_controller_, enableResponder_1_6, cmdId, channelInfo,
299                                      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(PerInstance, WifiRttControllerHidlTest,
305                          testing::ValuesIn(android::hardware::getAllHalInstanceNames(
306                                  ::android::hardware::wifi::V1_6::IWifi::descriptor)),
307                          android::hardware::PrintInstanceNameToString);
308