1 /*
2  * Copyright (C) 2016 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 <android-base/logging.h>
18 
19 #include <android/hardware/wifi/1.1/IWifi.h>
20 #include <android/hardware/wifi/1.1/IWifiChip.h>
21 #include <gtest/gtest.h>
22 #include <hidl/GtestPrinter.h>
23 #include <hidl/ServiceManagement.h>
24 
25 #include "wifi_hidl_call_util.h"
26 #include "wifi_hidl_test_utils.h"
27 
28 using ::android::sp;
29 using ::android::hardware::hidl_string;
30 using ::android::hardware::hidl_vec;
31 using ::android::hardware::wifi::V1_0::IfaceType;
32 using ::android::hardware::wifi::V1_0::ChipId;
33 using ::android::hardware::wifi::V1_0::ChipModeId;
34 using ::android::hardware::wifi::V1_0::WifiStatus;
35 using ::android::hardware::wifi::V1_0::WifiStatusCode;
36 using ::android::hardware::wifi::V1_1::IWifi;
37 using ::android::hardware::wifi::V1_1::IWifiChip;
38 using ::android::hardware::wifi::V1_0::IWifiStaIface;
39 
40 namespace {
41 constexpr IWifiChip::TxPowerScenario kFakePowerScenario =
42     IWifiChip::TxPowerScenario::VOICE_CALL;
43 }; //namespace
44 
45 /**
46  * Fixture to use for all Wifi chip HIDL interface tests.
47  */
48 class WifiChipHidlTest : public ::testing::TestWithParam<std::string> {
49    public:
SetUp()50     virtual void SetUp() override {
51         // Make sure to start with a clean state
52         stopWifi(GetInstanceName());
53 
54         wifi_chip_ = IWifiChip::castFrom(getWifiChip(GetInstanceName()));
55         ASSERT_NE(nullptr, wifi_chip_.get());
56     }
57 
TearDown()58     virtual void TearDown() override { stopWifi(GetInstanceName()); }
59 
60    protected:
configureChipForStaIfaceAndGetCapabilities()61     uint32_t configureChipForStaIfaceAndGetCapabilities() {
62         ChipModeId mode_id;
63         EXPECT_TRUE(configureChipToSupportIfaceType(
64             wifi_chip_, IfaceType::STA, &mode_id));
65 
66         return getChipCapabilitiesLatest(wifi_chip_);
67     }
68 
69     sp<IWifiChip> wifi_chip_;
70 
71    private:
GetInstanceName()72     std::string GetInstanceName() { return GetParam(); }
73 };
74 
75 /*
76  * SelectTxPowerScenario
77  */
TEST_P(WifiChipHidlTest,SelectTxPowerScenario)78 TEST_P(WifiChipHidlTest, SelectTxPowerScenario) {
79     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
80     const auto& status =
81         HIDL_INVOKE(wifi_chip_, selectTxPowerScenario, kFakePowerScenario);
82     if (caps & IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT) {
83         EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
84     } else {
85         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
86     }
87 }
88 
89 /*
90  * ResetTxPowerScenario
91  */
TEST_P(WifiChipHidlTest,ResetTxPowerScenario)92 TEST_P(WifiChipHidlTest, ResetTxPowerScenario) {
93     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
94     const auto& status =
95         HIDL_INVOKE(wifi_chip_, resetTxPowerScenario);
96     if (caps & IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT) {
97         EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
98     } else {
99         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
100     }
101 }
102 
103 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiChipHidlTest);
104 INSTANTIATE_TEST_SUITE_P(
105     PerInstance, WifiChipHidlTest,
106     testing::ValuesIn(
107         android::hardware::getAllHalInstanceNames(IWifi::descriptor)),
108     android::hardware::PrintInstanceNameToString);
109