1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Staache 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 <numeric>
18 #include <vector>
19
20 #include <android-base/logging.h>
21
22 #include <android/hardware/wifi/1.5/IWifi.h>
23 #include <android/hardware/wifi/1.5/IWifiChip.h>
24 #include <android/hardware/wifi/1.5/IWifiStaIface.h>
25 #include <gtest/gtest.h>
26 #include <hidl/GtestPrinter.h>
27 #include <hidl/ServiceManagement.h>
28
29 #include "wifi_hidl_call_util.h"
30 #include "wifi_hidl_test_utils.h"
31
32 using ::android::sp;
33 using ::android::hardware::hidl_array;
34 using ::android::hardware::wifi::V1_0::WifiStatus;
35 using ::android::hardware::wifi::V1_0::WifiStatusCode;
36 using ::android::hardware::wifi::V1_5::IWifiChip;
37 using ::android::hardware::wifi::V1_5::IWifiStaIface;
38
39 /**
40 * Fixture to use for all STA Iface HIDL interface tests.
41 */
42 class WifiStaIfaceHidlTest : public ::testing::TestWithParam<std::string> {
43 public:
SetUp()44 virtual void SetUp() override {
45 // Make sure to start with a clean state
46 stopWifi(GetInstanceName());
47
48 wifi_sta_iface_ =
49 IWifiStaIface::castFrom(getWifiStaIface(GetInstanceName()));
50 ASSERT_NE(nullptr, wifi_sta_iface_.get());
51 }
52
TearDown()53 virtual void TearDown() override { stopWifi(GetInstanceName()); }
54
55 protected:
isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask)56 bool isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask) {
57 const auto& status_and_caps =
58 HIDL_INVOKE(wifi_sta_iface_, getCapabilities);
59 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
60 return (status_and_caps.second & cap_mask) != 0;
61 }
62
createStaIface(sp<IWifiStaIface> * sta_iface)63 WifiStatusCode createStaIface(sp<IWifiStaIface>* sta_iface) {
64 sp<IWifiChip> wifi_chip =
65 IWifiChip::castFrom(getWifiChip(GetInstanceName()));
66 EXPECT_NE(nullptr, wifi_chip.get());
67 const auto& status_and_iface = HIDL_INVOKE(wifi_chip, createStaIface);
68 *sta_iface = IWifiStaIface::castFrom(status_and_iface.second);
69 return status_and_iface.first.code;
70 }
71
72 sp<IWifiStaIface> wifi_sta_iface_;
73
74 private:
GetInstanceName()75 std::string GetInstanceName() { return GetParam(); }
76 };
77
78 /*
79 * GetLinkLayerStats_1_5
80 * Ensures that calls to get link layer stats V1_5 will retrieve a non-empty
81 * StaLinkLayerStats after link layer stats collection is enabled.
82 */
TEST_P(WifiStaIfaceHidlTest,GetLinkLayerStats_1_5)83 TEST_P(WifiStaIfaceHidlTest, GetLinkLayerStats_1_5) {
84 if (!isCapabilitySupported(
85 IWifiStaIface::StaIfaceCapabilityMask::LINK_LAYER_STATS)) {
86 // No-op if link layer stats is not supported.
87 return;
88 }
89
90 // Enable link layer stats collection.
91 EXPECT_EQ(WifiStatusCode::SUCCESS,
92 HIDL_INVOKE(wifi_sta_iface_, enableLinkLayerStatsCollection, true)
93 .code);
94 // Retrieve link layer stats.
95 const auto& status_and_stats =
96 HIDL_INVOKE(wifi_sta_iface_, getLinkLayerStats_1_5);
97
98 if (status_and_stats.first.code == WifiStatusCode::ERROR_NOT_SUPPORTED) {
99 GTEST_SKIP() << "Skipping this test since API is deprecated.";
100 }
101
102 EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_stats.first.code);
103 EXPECT_GT(status_and_stats.second.timeStampInMs, 0u);
104 // Try to create 2nd iface. If yes, it should fill in the duty cycle field.
105 sp<IWifiStaIface> iface;
106 auto status = createStaIface(&iface);
107 if (status == WifiStatusCode::SUCCESS) {
108 EXPECT_GT(status_and_stats.second.iface.timeSliceDutyCycleInPercent,
109 0u);
110 }
111 // Disable link layer stats collection.
112 EXPECT_EQ(
113 WifiStatusCode::SUCCESS,
114 HIDL_INVOKE(wifi_sta_iface_, disableLinkLayerStatsCollection).code);
115 }
116 /**
117 * SetScanMode
118 */
TEST_P(WifiStaIfaceHidlTest,SetScanMode)119 TEST_P(WifiStaIfaceHidlTest, SetScanMode) {
120 auto statusCode =
121 HIDL_INVOKE(wifi_sta_iface_, setScanMode, true).code;
122 EXPECT_TRUE(statusCode == WifiStatusCode::SUCCESS ||
123 statusCode == WifiStatusCode::ERROR_NOT_SUPPORTED);
124
125 statusCode = HIDL_INVOKE(wifi_sta_iface_, setScanMode, false).code;
126 EXPECT_TRUE(statusCode == WifiStatusCode::SUCCESS ||
127 statusCode == WifiStatusCode::ERROR_NOT_SUPPORTED);
128 }
129
130 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiStaIfaceHidlTest);
131 INSTANTIATE_TEST_SUITE_P(
132 PerInstance, WifiStaIfaceHidlTest,
133 testing::ValuesIn(android::hardware::getAllHalInstanceNames(
134 ::android::hardware::wifi::V1_5::IWifi::descriptor)),
135 android::hardware::PrintInstanceNameToString);
136