1 /*
2  * Copyright (C) 2022 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.6/IWifi.h>
23 #include <android/hardware/wifi/1.6/IWifiChip.h>
24 #include <android/hardware/wifi/1.6/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_6::IWifiChip;
37 using ::android::hardware::wifi::V1_6::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_ = IWifiStaIface::castFrom(getWifiStaIface(GetInstanceName()));
49         ASSERT_NE(nullptr, wifi_sta_iface_.get());
50     }
51 
TearDown()52     virtual void TearDown() override { stopWifi(GetInstanceName()); }
53 
54   protected:
isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask)55     bool isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask cap_mask) {
56         const auto& status_and_caps = HIDL_INVOKE(wifi_sta_iface_, getCapabilities);
57         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
58         return (status_and_caps.second & cap_mask) != 0;
59     }
60 
createStaIface(sp<IWifiStaIface> * sta_iface)61     WifiStatusCode createStaIface(sp<IWifiStaIface>* sta_iface) {
62         sp<IWifiChip> wifi_chip = IWifiChip::castFrom(getWifiChip(GetInstanceName()));
63         EXPECT_NE(nullptr, wifi_chip.get());
64         const auto& status_and_iface = HIDL_INVOKE(wifi_chip, createStaIface);
65         *sta_iface = IWifiStaIface::castFrom(status_and_iface.second);
66         return status_and_iface.first.code;
67     }
68 
69     sp<IWifiStaIface> wifi_sta_iface_;
70 
71   private:
GetInstanceName()72     std::string GetInstanceName() { return GetParam(); }
73 };
74 
75 /*
76  * GetLinkLayerStats_1_6
77  * Ensures that calls to get link layer stats V1_6 will retrieve a non-empty
78  * StaLinkLayerStats after link layer stats collection is enabled.
79  */
TEST_P(WifiStaIfaceHidlTest,GetLinkLayerStats_1_6)80 TEST_P(WifiStaIfaceHidlTest, GetLinkLayerStats_1_6) {
81     if (!isCapabilitySupported(IWifiStaIface::StaIfaceCapabilityMask::LINK_LAYER_STATS)) {
82         GTEST_SKIP() << "Skipping this test since link layer stats are not supported.";
83     }
84 
85     // Enable link layer stats collection.
86     EXPECT_EQ(WifiStatusCode::SUCCESS,
87               HIDL_INVOKE(wifi_sta_iface_, enableLinkLayerStatsCollection, true).code);
88     // Retrieve link layer stats.
89     const auto& status_and_stats = HIDL_INVOKE(wifi_sta_iface_, getLinkLayerStats_1_6);
90     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_stats.first.code);
91     EXPECT_GT(status_and_stats.second.timeStampInMs, 0u);
92     // Try to create 2nd iface. If yes, it should fill in the duty cycle field.
93     sp<IWifiStaIface> iface;
94     auto status = createStaIface(&iface);
95     if (status == WifiStatusCode::SUCCESS) {
96         EXPECT_GT(status_and_stats.second.iface.timeSliceDutyCycleInPercent, 0u);
97     }
98     // Disable link layer stats collection.
99     EXPECT_EQ(WifiStatusCode::SUCCESS,
100               HIDL_INVOKE(wifi_sta_iface_, disableLinkLayerStatsCollection).code);
101 }
102 
103 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiStaIfaceHidlTest);
104 INSTANTIATE_TEST_SUITE_P(PerInstance, WifiStaIfaceHidlTest,
105                          testing::ValuesIn(android::hardware::getAllHalInstanceNames(
106                                  ::android::hardware::wifi::V1_6::IWifi::descriptor)),
107                          android::hardware::PrintInstanceNameToString);
108