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 <android/hardware/wifi/1.3/IWifiChip.h>
22 
23 #include <VtsHalHidlTargetTestBase.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::VtsHalHidlTargetTestBase {
49    public:
SetUp()50     virtual void SetUp() override {
51         wifi_chip_ = IWifiChip::castFrom(getWifiChip());
52         ASSERT_NE(nullptr, wifi_chip_.get());
53     }
54 
TearDown()55     virtual void TearDown() override { stopWifi(); }
56 
57    protected:
configureChipForStaIfaceAndGetCapabilities()58     uint32_t configureChipForStaIfaceAndGetCapabilities() {
59         ChipModeId mode_id;
60         EXPECT_TRUE(configureChipToSupportIfaceType(
61             wifi_chip_, IfaceType::STA, &mode_id));
62 
63         sp<::android::hardware::wifi::V1_3::IWifiChip> chip_converted =
64             ::android::hardware::wifi::V1_3::IWifiChip::castFrom(wifi_chip_);
65 
66         std::pair<WifiStatus, uint32_t> status_and_caps;
67 
68         if (chip_converted != nullptr) {
69             // Call the newer HAL version
70             status_and_caps = HIDL_INVOKE(chip_converted, getCapabilities_1_3);
71         } else {
72             status_and_caps = HIDL_INVOKE(wifi_chip_, getCapabilities);
73         }
74 
75         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
76         return status_and_caps.second;
77     }
78 
79     sp<IWifiChip> wifi_chip_;
80 };
81 
82 /*
83  * SelectTxPowerScenario
84  */
TEST_F(WifiChipHidlTest,SelectTxPowerScenario)85 TEST_F(WifiChipHidlTest, SelectTxPowerScenario) {
86     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
87     const auto& status =
88         HIDL_INVOKE(wifi_chip_, selectTxPowerScenario, kFakePowerScenario);
89     if (caps & IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT) {
90         EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
91     } else {
92         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
93     }
94 }
95 
96 /*
97  * ResetTxPowerScenario
98  */
TEST_F(WifiChipHidlTest,ResetTxPowerScenario)99 TEST_F(WifiChipHidlTest, ResetTxPowerScenario) {
100     uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
101     const auto& status =
102         HIDL_INVOKE(wifi_chip_, resetTxPowerScenario);
103     if (caps & IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT) {
104         EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
105     } else {
106         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
107     }
108 }
109