1 /*
2  * Copyright (C) 2020 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 <VtsCoreUtil.h>
18 #include <VtsHalHidlTargetCallbackBase.h>
19 #include <android-base/logging.h>
20 
21 #undef NAN  // NAN is defined in bionic/libc/include/math.h:38
22 
23 #include <android/hardware/wifi/1.4/IWifiChipEventCallback.h>
24 #include <android/hardware/wifi/1.5/IWifi.h>
25 #include <android/hardware/wifi/1.5/IWifiApIface.h>
26 #include <android/hardware/wifi/1.5/IWifiChip.h>
27 #include <gtest/gtest.h>
28 #include <hidl/GtestPrinter.h>
29 #include <hidl/ServiceManagement.h>
30 
31 #include "wifi_hidl_call_util.h"
32 #include "wifi_hidl_test_utils.h"
33 
34 using ::android::sp;
35 using ::android::hardware::hidl_string;
36 using ::android::hardware::hidl_vec;
37 using ::android::hardware::Return;
38 using ::android::hardware::Void;
39 using ::android::hardware::wifi::V1_0::ChipModeId;
40 using ::android::hardware::wifi::V1_0::IfaceType;
41 using ::android::hardware::wifi::V1_0::IWifiIface;
42 using ::android::hardware::wifi::V1_0::WifiDebugRingBufferStatus;
43 using ::android::hardware::wifi::V1_0::WifiStatus;
44 using ::android::hardware::wifi::V1_0::WifiStatusCode;
45 using ::android::hardware::wifi::V1_4::IWifiChipEventCallback;
46 using ::android::hardware::wifi::V1_5::IWifiApIface;
47 using ::android::hardware::wifi::V1_5::IWifiChip;
48 
49 /**
50  * Fixture for IWifiChip tests that are conditioned on SoftAP support.
51  */
52 class WifiChipHidlTest : public ::testing::TestWithParam<std::string> {
53    public:
SetUp()54     virtual void SetUp() override {
55         isBridgedSupport_ = testing::checkSubstringInCommandOutput(
56             "/system/bin/cmd wifi get-softap-supported-features",
57             "wifi_softap_bridged_ap_supported");
58         // Make sure to start 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    protected:
68     bool isBridgedSupport_ = false;
69     // Helper function to configure the Chip in one of the supported modes.
70     // Most of the non-mode-configuration-related methods require chip
71     // to be first configured.
configureChipForIfaceType(IfaceType type,bool expectSuccess)72     ChipModeId configureChipForIfaceType(IfaceType type, bool expectSuccess) {
73         ChipModeId mode_id;
74         EXPECT_EQ(expectSuccess,
75                   configureChipToSupportIfaceType(wifi_chip_, type, &mode_id));
76         return mode_id;
77     }
78 
createBridgedApIface(sp<IWifiApIface> * ap_iface)79     void createBridgedApIface(sp<IWifiApIface>* ap_iface) {
80         configureChipForIfaceType(IfaceType::AP, true);
81         const auto& status_and_iface =
82             HIDL_INVOKE(wifi_chip_, createBridgedApIface);
83         *ap_iface = status_and_iface.second;
84         EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_iface.first.code);
85     }
86 
87     sp<IWifiChip> wifi_chip_;
88 
89    private:
GetInstanceName()90     std::string GetInstanceName() { return GetParam(); }
91 };
92 
93 /**
94  * createBridgedApIface & removeIfaceInstanceFromBridgedApIface
95  */
TEST_P(WifiChipHidlTest,createBridgedApIfaceAndremoveIfaceInstanceFromBridgedApIfaceTest)96 TEST_P(WifiChipHidlTest,
97        createBridgedApIfaceAndremoveIfaceInstanceFromBridgedApIfaceTest) {
98     if (!isBridgedSupport_) GTEST_SKIP() << "Missing Bridged AP support";
99     sp<IWifiApIface> wifi_ap_iface;
100     createBridgedApIface(&wifi_ap_iface);
101     ASSERT_NE(nullptr, wifi_ap_iface.get());
102     const auto& status_and_name = HIDL_INVOKE(wifi_ap_iface, getName);
103     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_name.first.code);
104     std::string br_name = status_and_name.second;
105     const auto& status_and_instances =
106         HIDL_INVOKE(wifi_ap_iface, getBridgedInstances);
107     EXPECT_EQ(WifiStatusCode::SUCCESS, status_and_instances.first.code);
108     const auto& instances = status_and_instances.second;
109     EXPECT_EQ(2, instances.size());
110     const auto& status_code =
111         HIDL_INVOKE(wifi_chip_, removeIfaceInstanceFromBridgedApIface, br_name,
112                     instances[0]);
113     EXPECT_EQ(WifiStatusCode::SUCCESS, status_code.code);
114     const auto& status_and_instances_after_remove =
115         HIDL_INVOKE(wifi_ap_iface, getBridgedInstances);
116     EXPECT_EQ(WifiStatusCode::SUCCESS,
117               status_and_instances_after_remove.first.code);
118     const auto& instances_after_remove =
119         status_and_instances_after_remove.second;
120     EXPECT_EQ(1, instances_after_remove.size());
121 }
122 
123 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(WifiChipHidlTest);
124 INSTANTIATE_TEST_SUITE_P(
125     PerInstance, WifiChipHidlTest,
126     testing::ValuesIn(android::hardware::getAllHalInstanceNames(
127         ::android::hardware::wifi::V1_5::IWifi::descriptor)),
128     android::hardware::PrintInstanceNameToString);
129