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/hidl/manager/1.0/IServiceManager.h>
20 #include <hidl/HidlTransportSupport.h>
21 
22 #include <wifi_system/hostapd_manager.h>
23 #include <wifi_system/interface_tool.h>
24 #include <wifi_system/supplicant_manager.h>
25 
26 #include "hostapd_hidl_test_utils.h"
27 #include "wifi_hidl_test_utils.h"
28 
29 using ::android::sp;
30 using ::android::hardware::configureRpcThreadpool;
31 using ::android::hardware::hidl_string;
32 using ::android::hardware::hidl_vec;
33 using ::android::hardware::joinRpcThreadpool;
34 using ::android::hardware::Return;
35 using ::android::hardware::Void;
36 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatus;
37 using ::android::hardware::wifi::hostapd::V1_0::HostapdStatusCode;
38 using ::android::hardware::wifi::hostapd::V1_0::IHostapd;
39 using ::android::hardware::wifi::V1_0::ChipModeId;
40 using ::android::hardware::wifi::V1_0::IWifiChip;
41 using ::android::wifi_system::HostapdManager;
42 using ::android::wifi_system::SupplicantManager;
43 
44 // Helper function to initialize the driver and firmware to AP mode
45 // using the vendor HAL HIDL interface.
initializeDriverAndFirmware(const std::string & wifi_instance_name)46 void initializeDriverAndFirmware(const std::string& wifi_instance_name) {
47     if (getWifi(wifi_instance_name) != nullptr) {
48         sp<IWifiChip> wifi_chip = getWifiChip(wifi_instance_name);
49         ChipModeId mode_id;
50         EXPECT_TRUE(configureChipToSupportIfaceType(
51             wifi_chip, ::android::hardware::wifi::V1_0::IfaceType::AP, &mode_id));
52     } else {
53         LOG(WARNING) << __func__ << ": Vendor HAL not supported";
54     }
55 }
56 
57 // Helper function to deinitialize the driver and firmware
58 // using the vendor HAL HIDL interface.
deInitializeDriverAndFirmware(const std::string & wifi_instance_name)59 void deInitializeDriverAndFirmware(const std::string& wifi_instance_name) {
60     if (getWifi(wifi_instance_name) != nullptr) {
61         stopWifi(wifi_instance_name);
62     } else {
63         LOG(WARNING) << __func__ << ": Vendor HAL not supported";
64     }
65 }
66 
stopSupplicantIfNeeded(const std::string & instance_name)67 void stopSupplicantIfNeeded(const std::string& instance_name) {
68     SupplicantManager supplicant_manager;
69     if (supplicant_manager.IsSupplicantRunning()) {
70         LOG(INFO) << "Supplicant is running, stop supplicant first.";
71         ASSERT_TRUE(supplicant_manager.StopSupplicant());
72         deInitializeDriverAndFirmware(instance_name);
73         ASSERT_FALSE(supplicant_manager.IsSupplicantRunning());
74     }
75 }
76 
stopHostapd(const std::string & instance_name)77 void stopHostapd(const std::string& instance_name) {
78     HostapdManager hostapd_manager;
79 
80     ASSERT_TRUE(hostapd_manager.StopHostapd());
81     deInitializeDriverAndFirmware(instance_name);
82 }
83 
startHostapdAndWaitForHidlService(const std::string & wifi_instance_name,const std::string & hostapd_instance_name)84 void startHostapdAndWaitForHidlService(
85     const std::string& wifi_instance_name,
86     const std::string& hostapd_instance_name) {
87     initializeDriverAndFirmware(wifi_instance_name);
88 
89     HostapdManager hostapd_manager;
90     ASSERT_TRUE(hostapd_manager.StartHostapd());
91 
92     // Wait for hostapd service to come up.
93     IHostapd::getService(hostapd_instance_name);
94 }
95 
is_1_1(const sp<IHostapd> & hostapd)96 bool is_1_1(const sp<IHostapd>& hostapd) {
97     sp<::android::hardware::wifi::hostapd::V1_1::IHostapd> hostapd_1_1 =
98         ::android::hardware::wifi::hostapd::V1_1::IHostapd::castFrom(hostapd);
99     return hostapd_1_1.get() != nullptr;
100 }
101 
toggleWifiFramework(bool enable)102 void toggleWifiFramework(bool enable) {
103     std::string cmd = "/system/bin/cmd wifi set-wifi-enabled ";
104     cmd += enable ? "enabled" : "disabled";
105     testing::checkSubstringInCommandOutput(cmd.c_str(), "X");
106 }
107 
toggleWifiScanAlwaysAvailable(bool enable)108 void toggleWifiScanAlwaysAvailable(bool enable) {
109     std::string cmd = "/system/bin/cmd wifi set-scan-always-available ";
110     cmd += enable ? "enabled" : "disabled";
111     testing::checkSubstringInCommandOutput(cmd.c_str(), "X");
112 }
113 
isWifiFrameworkEnabled()114 bool isWifiFrameworkEnabled() {
115     return testing::checkSubstringInCommandOutput("/system/bin/cmd wifi status", "Wifi is enabled");
116 }
117 
isWifiScanAlwaysAvailable()118 bool isWifiScanAlwaysAvailable() {
119     return testing::checkSubstringInCommandOutput("/system/bin/cmd wifi status",
120                                                   "Wifi scanning is always available");
121 }
122