1 /*
2  * Copyright (C) 2018 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.2/IWifi.h>
20 #include <android/hardware/wifi/1.2/IWifiChip.h>
21 #include <android/hardware/wifi/1.2/IWifiChipEventCallback.h>
22 #include <android/hardware/wifi/1.3/IWifiChip.h>
23 #include <gtest/gtest.h>
24 #include <hidl/GtestPrinter.h>
25 #include <hidl/ServiceManagement.h>
26 #include <VtsHalHidlTargetCallbackBase.h>
27 
28 #include "wifi_hidl_call_util.h"
29 #include "wifi_hidl_test_utils.h"
30 
31 using ::android::hardware::hidl_string;
32 using ::android::hardware::hidl_vec;
33 using ::android::hardware::Return;
34 using ::android::hardware::wifi::V1_0::ChipModeId;
35 using ::android::hardware::wifi::V1_0::IfaceType;
36 using ::android::hardware::wifi::V1_0::WifiStatus;
37 using ::android::hardware::wifi::V1_0::WifiDebugRingBufferStatus;
38 using ::android::hardware::wifi::V1_0::WifiStatusCode;
39 using ::android::hardware::wifi::V1_2::IWifiChip;
40 using ::android::hardware::wifi::V1_2::IWifiChipEventCallback;
41 using ::android::hardware::Void;
42 using ::android::sp;
43 
44 namespace {
45 constexpr IWifiChip::TxPowerScenario kPowerScenarioBody =
46     IWifiChip::TxPowerScenario::ON_BODY_CELL_OFF;
47 
48 constexpr IWifiChip::TxPowerScenario kPowerScenarioVoiceCall =
49     IWifiChip::TxPowerScenario::VOICE_CALL;
50 };  // namespace
51 
52 /**
53  * Fixture to use for all Wifi chip HIDL interface tests.
54  */
55 class WifiChipHidlTest : public ::testing::TestWithParam<std::string> {
56  public:
SetUp()57   virtual void SetUp() override {
58       // Make sure test starts with a clean state
59       stopWifi(GetInstanceName());
60 
61       wifi_chip_ = IWifiChip::castFrom(getWifiChip(GetInstanceName()));
62       ASSERT_NE(nullptr, wifi_chip_.get());
63   }
64 
TearDown()65   virtual void TearDown() override { stopWifi(GetInstanceName()); }
66 
67   // A simple test implementation of WifiChipEventCallback.
68   class WifiChipEventCallback
69       : public ::testing::VtsHalHidlTargetCallbackBase<WifiChipHidlTest>,
70         public IWifiChipEventCallback {
71 
72    public:
WifiChipEventCallback()73     WifiChipEventCallback() {};
74 
75     virtual ~WifiChipEventCallback() = default;
76 
onChipReconfigured(uint32_t modeId __unused)77     Return<void> onChipReconfigured(uint32_t modeId __unused) {
78       return Void();
79     };
80 
onChipReconfigureFailure(const WifiStatus & status __unused)81     Return<void> onChipReconfigureFailure(const WifiStatus& status __unused) {
82       return Void();
83     };
84 
onIfaceAdded(IfaceType type __unused,const hidl_string & name __unused)85     Return<void> onIfaceAdded(IfaceType type __unused, const hidl_string& name __unused) {
86       return Void();
87     };
88 
onIfaceRemoved(IfaceType type __unused,const hidl_string & name __unused)89     Return<void> onIfaceRemoved(IfaceType type __unused, const hidl_string& name __unused) {
90       return Void();
91     };
92 
onDebugRingBufferDataAvailable(const WifiDebugRingBufferStatus & status __unused,const hidl_vec<uint8_t> & data __unused)93     Return<void> onDebugRingBufferDataAvailable(const WifiDebugRingBufferStatus& status __unused,
94         const hidl_vec<uint8_t>& data __unused) {
95       return Void();
96     };
97 
onDebugErrorAlert(int32_t errorCode __unused,const hidl_vec<uint8_t> & debugData __unused)98     Return<void> onDebugErrorAlert(int32_t errorCode __unused,
99         const hidl_vec<uint8_t>& debugData __unused) {
100       return Void();
101     };
102 
onRadioModeChange(const hidl_vec<RadioModeInfo> & radioModeInfos __unused)103     Return<void> onRadioModeChange(const hidl_vec<RadioModeInfo>& radioModeInfos __unused) {
104       return Void();
105     };
106   };
107 
108  protected:
configureChipForStaIfaceAndGetCapabilities()109   uint32_t configureChipForStaIfaceAndGetCapabilities() {
110     ChipModeId mode_id;
111     EXPECT_TRUE(
112         configureChipToSupportIfaceType(wifi_chip_, IfaceType::STA, &mode_id));
113 
114     return getChipCapabilitiesLatest(wifi_chip_);
115   }
116 
117   sp<IWifiChip> wifi_chip_;
118 
119  private:
GetInstanceName()120   std::string GetInstanceName() { return GetParam(); }
121 };
122 
123 /*
124  * SelectTxPowerScenario_1_2_body
125  * This test case tests the selectTxPowerScenario_1_2() API with SAR scenarios
126  * newly defined in 1.2
127  */
TEST_P(WifiChipHidlTest,SelectTxPowerScenario_1_2_body)128 TEST_P(WifiChipHidlTest, SelectTxPowerScenario_1_2_body) {
129   uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
130   const auto& status =
131       HIDL_INVOKE(wifi_chip_, selectTxPowerScenario_1_2, kPowerScenarioBody);
132   if (caps & (IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT |
133               IWifiChip::ChipCapabilityMask::USE_BODY_HEAD_SAR)) {
134     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
135   } else {
136     EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
137   }
138 }
139 
140 /*
141  * SelectTxPowerScenario_1_2_voiceCall
142  * This test case tests the selectTxPowerScenario_1_2() API with previously
143  * defined SAR scenarios
144  */
TEST_P(WifiChipHidlTest,SelectTxPowerScenario_1_2_voiceCall)145 TEST_P(WifiChipHidlTest, SelectTxPowerScenario_1_2_voiceCall) {
146   uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
147   const auto& status =
148       HIDL_INVOKE(wifi_chip_, selectTxPowerScenario_1_2, kPowerScenarioVoiceCall);
149   if (caps & (IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT)) {
150     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
151   } else {
152     EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
153   }
154 }
155 
156 /*
157  * registerEventCallback_1_2
158  * This test case tests the registerEventCallback_1_2() API which registers
159  * a call back function with the hal implementation
160  *
161  * Note: it is not feasible to test the invocation of the call back function
162  * since event is triggered internally in the HAL implementation, and can not be
163  * triggered from the test case
164  */
TEST_P(WifiChipHidlTest,registerEventCallback_1_2)165 TEST_P(WifiChipHidlTest, registerEventCallback_1_2) {
166     sp<WifiChipEventCallback> wifiChipEventCallback = new WifiChipEventCallback();
167     const auto& status =
168         HIDL_INVOKE(wifi_chip_, registerEventCallback_1_2, wifiChipEventCallback);
169 
170     if (status.code != WifiStatusCode::SUCCESS) {
171         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
172         return;
173     }
174 }
175 
176 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiChipHidlTest);
177 INSTANTIATE_TEST_SUITE_P(
178     PerInstance, WifiChipHidlTest,
179     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
180         ::android::hardware::wifi::V1_2::IWifi::descriptor)),
181     android::hardware::PrintInstanceNameToString);
182