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  * rangeRequest_1_4
152  */
TEST_P(WifiRttControllerHidlTest,RangeRequest_1_4)153 TEST_P(WifiRttControllerHidlTest, RangeRequest_1_4) {
154     std::pair<WifiStatus, RttCapabilities> status_and_caps;
155 
156     // Get the Capabilities
157     status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_4);
158     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
159     // Get the highest support preamble
160     int preamble = 1;
161     status_and_caps.second.preambleSupport >>= 1;
162     while (status_and_caps.second.preambleSupport != 0) {
163         status_and_caps.second.preambleSupport >>= 1;
164         preamble <<= 1;
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::ONE_SIDED;
174     config.peer = RttPeerType::AP;
175     config.channel.width = WifiChannelWidthInMhz::WIDTH_80;
176     config.channel.centerFreq = 5765;
177     config.channel.centerFreq0 = 5775;
178     config.channel.centerFreq1 = 0;
179     config.bw = RttBw::BW_80MHZ;
180     config.preamble = (RttPreamble)preamble;
181     config.mustRequestLci = false;
182     config.mustRequestLcr = false;
183     config.burstPeriod = 0;
184     config.numBurst = 0;
185     config.numFramesPerBurst = 8;
186     config.numRetriesPerRttFrame = 3;
187     config.numRetriesPerFtmr = 3;
188     config.burstDuration = 9;
189     // Insert config in the vector
190     configs.push_back(config);
191 
192     // Invoke the call
193     const auto& status =
194         HIDL_INVOKE(wifi_rtt_controller_, rangeRequest_1_4, cmdId, configs);
195     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
196 }
197 
198 /*
199  * getCapabilities_1_4
200  */
TEST_P(WifiRttControllerHidlTest,GetCapabilities_1_4)201 TEST_P(WifiRttControllerHidlTest, GetCapabilities_1_4) {
202     std::pair<WifiStatus, RttCapabilities> status_and_caps;
203 
204     // Invoke the call
205     status_and_caps = HIDL_INVOKE(wifi_rtt_controller_, getCapabilities_1_4);
206     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
207 }
208 
209 /*
210  * getResponderInfo_1_4
211  */
TEST_P(WifiRttControllerHidlTest,GetResponderInfo_1_4)212 TEST_P(WifiRttControllerHidlTest, GetResponderInfo_1_4) {
213     std::pair<WifiStatus, RttResponder> status_and_info;
214 
215     // Invoke the call
216     status_and_info = HIDL_INVOKE(wifi_rtt_controller_, getResponderInfo_1_4);
217     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_info.first.code);
218 }
219 
220 /*
221  * enableResponder_1_4
222  */
TEST_P(WifiRttControllerHidlTest,EnableResponder_1_4)223 TEST_P(WifiRttControllerHidlTest, EnableResponder_1_4) {
224     std::pair<WifiStatus, RttResponder> status_and_info;
225     int cmdId = 55;
226     WifiChannelInfo channelInfo;
227     channelInfo.width = WifiChannelWidthInMhz::WIDTH_80;
228     channelInfo.centerFreq = 5660;
229     channelInfo.centerFreq0 = 5660;
230     channelInfo.centerFreq1 = 0;
231 
232     // Get the responder first
233     status_and_info = HIDL_INVOKE(wifi_rtt_controller_, getResponderInfo_1_4);
234     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_info.first.code);
235 
236     // Invoke the call
237     const auto& status =
238         HIDL_INVOKE(wifi_rtt_controller_, enableResponder_1_4, cmdId,
239                     channelInfo, 10, status_and_info.second);
240     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
241 }
242 
243 INSTANTIATE_TEST_SUITE_P(
244     PerInstance, WifiRttControllerHidlTest,
245     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
246         ::android::hardware::wifi::V1_4::IWifi::descriptor)),
247     android::hardware::PrintInstanceNameToString);
248