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     sp<::android::hardware::wifi::V1_3::IWifiChip> chip_converted =
115         ::android::hardware::wifi::V1_3::IWifiChip::castFrom(wifi_chip_);
116 
117     std::pair<WifiStatus, uint32_t> status_and_caps;
118 
119     if (chip_converted != nullptr) {
120         // Call the newer HAL version
121         status_and_caps = HIDL_INVOKE(chip_converted, getCapabilities_1_3);
122     } else {
123         status_and_caps = HIDL_INVOKE(wifi_chip_, getCapabilities);
124     }
125 
126     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_caps.first.code);
127     return status_and_caps.second;
128   }
129 
130   sp<IWifiChip> wifi_chip_;
131 
132  private:
GetInstanceName()133   std::string GetInstanceName() { return GetParam(); }
134 };
135 
136 /*
137  * SelectTxPowerScenario_1_2_body
138  * This test case tests the selectTxPowerScenario_1_2() API with SAR scenarios
139  * newly defined in 1.2
140  */
TEST_P(WifiChipHidlTest,SelectTxPowerScenario_1_2_body)141 TEST_P(WifiChipHidlTest, SelectTxPowerScenario_1_2_body) {
142   uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
143   const auto& status =
144       HIDL_INVOKE(wifi_chip_, selectTxPowerScenario_1_2, kPowerScenarioBody);
145   if (caps & (IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT |
146               IWifiChip::ChipCapabilityMask::USE_BODY_HEAD_SAR)) {
147     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
148   } else {
149     EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
150   }
151 }
152 
153 /*
154  * SelectTxPowerScenario_1_2_voiceCall
155  * This test case tests the selectTxPowerScenario_1_2() API with previously
156  * defined SAR scenarios
157  */
TEST_P(WifiChipHidlTest,SelectTxPowerScenario_1_2_voiceCall)158 TEST_P(WifiChipHidlTest, SelectTxPowerScenario_1_2_voiceCall) {
159   uint32_t caps = configureChipForStaIfaceAndGetCapabilities();
160   const auto& status =
161       HIDL_INVOKE(wifi_chip_, selectTxPowerScenario_1_2, kPowerScenarioVoiceCall);
162   if (caps & (IWifiChip::ChipCapabilityMask::SET_TX_POWER_LIMIT)) {
163     EXPECT_EQ(WifiStatusCode::SUCCESS, status.code);
164   } else {
165     EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
166   }
167 }
168 
169 /*
170  * registerEventCallback_1_2
171  * This test case tests the registerEventCallback_1_2() API which registers
172  * a call back function with the hal implementation
173  *
174  * Note: it is not feasible to test the invocation of the call back function
175  * since event is triggered internally in the HAL implementation, and can not be
176  * triggered from the test case
177  */
TEST_P(WifiChipHidlTest,registerEventCallback_1_2)178 TEST_P(WifiChipHidlTest, registerEventCallback_1_2) {
179     sp<WifiChipEventCallback> wifiChipEventCallback = new WifiChipEventCallback();
180     const auto& status =
181         HIDL_INVOKE(wifi_chip_, registerEventCallback_1_2, wifiChipEventCallback);
182 
183     if (status.code != WifiStatusCode::SUCCESS) {
184         EXPECT_EQ(WifiStatusCode::ERROR_NOT_SUPPORTED, status.code);
185         return;
186     }
187 }
188 
189 INSTANTIATE_TEST_SUITE_P(
190     PerInstance, WifiChipHidlTest,
191     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
192         ::android::hardware::wifi::V1_2::IWifi::descriptor)),
193     android::hardware::PrintInstanceNameToString);
194