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 <android/hardware/wifi/1.4/IWifiChipEventCallback.h>
23 #include <android/hardware/wifi/1.5/IWifi.h>
24 #include <android/hardware/wifi/1.5/IWifiChip.h>
25 #include <android/hardware/wifi/1.5/IWifiStaIface.h>
26 #include <gtest/gtest.h>
27 #include <hidl/GtestPrinter.h>
28 #include <hidl/ServiceManagement.h>
29 
30 #include "wifi_hidl_call_util.h"
31 #include "wifi_hidl_test_utils.h"
32 
33 using ::android::sp;
34 using ::android::hardware::hidl_string;
35 using ::android::hardware::hidl_vec;
36 using ::android::hardware::Return;
37 using ::android::hardware::Void;
38 using ::android::hardware::wifi::V1_0::ChipModeId;
39 using ::android::hardware::wifi::V1_0::IfaceType;
40 using ::android::hardware::wifi::V1_0::IWifiIface;
41 using ::android::hardware::wifi::V1_0::IWifiStaIface;
42 using ::android::hardware::wifi::V1_0::WifiDebugRingBufferStatus;
43 using ::android::hardware::wifi::V1_0::WifiStatus;
44 using ::android::hardware::wifi::V1_0::WifiStatusCode;
45 using ::android::hardware::wifi::V1_4::IWifiChipEventCallback;
46 using ::android::hardware::wifi::V1_5::IWifiChip;
47 using ::android::hardware::wifi::V1_5::WifiBand;
48 using ::android::hardware::wifi::V1_5::WifiIfaceMode;
49 
50 /**
51  * Fixture to use for all Wifi chip HIDL interface tests.
52  */
53 class WifiChipHidlTest : public ::testing::TestWithParam<std::string> {
54    public:
SetUp()55     virtual void SetUp() override {
56         // Make sure to start with a clean state
57         stopWifi(GetInstanceName());
58 
59         wifi_chip_ = IWifiChip::castFrom(getWifiChip(GetInstanceName()));
60         ASSERT_NE(nullptr, wifi_chip_.get());
61     }
62 
TearDown()63     virtual void TearDown() override { stopWifi(GetInstanceName()); }
64 
65    protected:
66     // Helper function to configure the Chip in one of the supported modes.
67     // Most of the non-mode-configuration-related methods require chip
68     // to be first configured.
configureChipForIfaceType(IfaceType type,bool expectSuccess)69     ChipModeId configureChipForIfaceType(IfaceType type, bool expectSuccess) {
70         ChipModeId mode_id;
71         EXPECT_EQ(expectSuccess,
72                   configureChipToSupportIfaceType(wifi_chip_, type, &mode_id));
73         return mode_id;
74     }
75 
createStaIface(sp<IWifiStaIface> * sta_iface)76     WifiStatusCode createStaIface(sp<IWifiStaIface>* sta_iface) {
77         const auto& status_and_iface = HIDL_INVOKE(wifi_chip_, createStaIface);
78         *sta_iface = status_and_iface.second;
79         return status_and_iface.first.code;
80     }
81 
getIfaceName(const sp<IWifiIface> & iface)82     std::string getIfaceName(const sp<IWifiIface>& iface) {
83         const auto& status_and_name = HIDL_INVOKE(iface, getName);
84         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_name.first.code);
85         return status_and_name.second;
86     }
87 
create2StaIfacesIfPossible()88     std::vector<sp<IWifiStaIface>> create2StaIfacesIfPossible() {
89         configureChipForIfaceType(IfaceType::STA, true);
90         sp<IWifiStaIface> iface1, iface2;
91         EXPECT_EQ(WifiStatusCode::SUCCESS, createStaIface(&iface1));
92         EXPECT_NE(nullptr, iface1.get());
93 
94         // Try to create 2nd iface
95         auto status = createStaIface(&iface2);
96         if (status != WifiStatusCode::SUCCESS) {
97             return {iface1};
98         }
99         EXPECT_NE(nullptr, iface2.get());
100         return {iface1, iface2};
101     }
102 
103     sp<IWifiChip> wifi_chip_;
104 
105    private:
GetInstanceName()106     std::string GetInstanceName() { return GetParam(); }
107 };
108 
109 /*
110  * setMultiStaPrimaryConnection
111  *
112  * Only run if device supports 2 STA ifaces.
113  */
TEST_P(WifiChipHidlTest,setMultiStaPrimaryConnection)114 TEST_P(WifiChipHidlTest, setMultiStaPrimaryConnection) {
115     auto ifaces = create2StaIfacesIfPossible();
116     if (ifaces.size() < 2) {
117         GTEST_SKIP() << "Device does not support more than 1 STA concurrently";
118     }
119 
120     const auto& status = HIDL_INVOKE(wifi_chip_, setMultiStaPrimaryConnection,
121                                      getIfaceName(ifaces.front()));
122     if (status.code != WifiStatusCode::SUCCESS) {
123         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
124     }
125 }
126 
127 /*
128  * setMultiStaUseCase
129  *
130  * Only run if device supports 2 STA ifaces.
131  */
TEST_P(WifiChipHidlTest,setMultiStaUseCase)132 TEST_P(WifiChipHidlTest, setMultiStaUseCase) {
133     auto ifaces = create2StaIfacesIfPossible();
134     if (ifaces.size() < 2) {
135         GTEST_SKIP() << "Device does not support more than 1 STA concurrently";
136     }
137 
138     const auto& status = HIDL_INVOKE(
139         wifi_chip_, setMultiStaUseCase,
140         IWifiChip::MultiStaUseCase::DUAL_STA_TRANSIENT_PREFER_PRIMARY);
141     if (status.code != WifiStatusCode::SUCCESS) {
142         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
143     }
144 }
145 
146 /*
147  * setCoexUnsafeChannels
148  */
TEST_P(WifiChipHidlTest,setCoexUnsafeChannels)149 TEST_P(WifiChipHidlTest, setCoexUnsafeChannels) {
150     configureChipForIfaceType(IfaceType::STA, true);
151     // Test with empty vector of CoexUnsafeChannels
152     std::vector<IWifiChip::CoexUnsafeChannel> vec;
153     const auto& statusEmpty =
154         HIDL_INVOKE(wifi_chip_, setCoexUnsafeChannels, vec, 0);
155     if (statusEmpty.code != WifiStatusCode::SUCCESS) {
156         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, statusEmpty.code);
157     }
158 
159     // Test with non-empty vector of CoexUnsafeChannels
160     IWifiChip::CoexUnsafeChannel unsafeChannel24Ghz;
161     unsafeChannel24Ghz.band = WifiBand::BAND_24GHZ;
162     unsafeChannel24Ghz.channel = 6;
163     vec.push_back(unsafeChannel24Ghz);
164     IWifiChip::CoexUnsafeChannel unsafeChannel5Ghz;
165     unsafeChannel5Ghz.band = WifiBand::BAND_5GHZ;
166     unsafeChannel5Ghz.channel = 36;
167     vec.push_back(unsafeChannel5Ghz);
168     uint32_t restrictions = IWifiChip::CoexRestriction::WIFI_AWARE |
169                             IWifiChip::CoexRestriction::SOFTAP |
170                             IWifiChip::CoexRestriction::WIFI_DIRECT;
171     const auto& statusNonEmpty =
172         HIDL_INVOKE(wifi_chip_, setCoexUnsafeChannels, vec, restrictions);
173     if (statusNonEmpty.code != WifiStatusCode::SUCCESS) {
174         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, statusNonEmpty.code);
175     }
176 }
177 
178 /*
179  * SetCountryCode:
180  * Ensures that a call to set the country code will return with a success
181  * status code.
182  */
TEST_P(WifiChipHidlTest,setCountryCode)183 TEST_P(WifiChipHidlTest, setCountryCode) {
184     const android::hardware::hidl_array<int8_t, 2> kCountryCode{
185         std::array<int8_t, 2>{{0x55, 0x53}}};
186 
187     configureChipForIfaceType(IfaceType::STA, true);
188     EXPECT_EQ(WifiStatusCode::SUCCESS,
189               HIDL_INVOKE(wifi_chip_, setCountryCode, kCountryCode).code);
190 }
191 
192 /* getUsableChannels:
193  * Ensure that a call to getUsableChannels will return with a success
194  * status for valid inputs.
195  */
TEST_P(WifiChipHidlTest,getUsableChannels)196 TEST_P(WifiChipHidlTest, getUsableChannels) {
197     uint32_t ifaceModeMask =
198         WifiIfaceMode::IFACE_MODE_P2P_CLIENT | WifiIfaceMode::IFACE_MODE_P2P_GO;
199     uint32_t filterMask = IWifiChip::UsableChannelFilter::CELLULAR_COEXISTENCE |
200                           IWifiChip::UsableChannelFilter::CONCURRENCY;
201     configureChipForIfaceType(IfaceType::STA, true);
202     WifiBand band = WifiBand::BAND_24GHZ_5GHZ_6GHZ;
203     const auto& statusNonEmpty = HIDL_INVOKE(wifi_chip_, getUsableChannels,
204                                              band, ifaceModeMask, filterMask);
205     if (statusNonEmpty.first.code != WifiStatusCode::SUCCESS) {
206         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED,
207                   statusNonEmpty.first.code);
208     }
209 }
210 
211 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiChipHidlTest);
212 INSTANTIATE_TEST_SUITE_P(
213     PerInstance, WifiChipHidlTest,
214     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
215         ::android::hardware::wifi::V1_5::IWifi::descriptor)),
216     android::hardware::PrintInstanceNameToString);
217