• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 #pragma once
18 
19 #include <aidl/android/hardware/wifi/hostapd/BnHostapd.h>
20 #include <android-base/logging.h>
21 #include <wifi_system/hostapd_manager.h>
22 #include <wifi_system/supplicant_manager.h>
23 
24 #include "hostapd_aidl_test_utils.h"
25 #include "hostapd_legacy_test_utils.h"
26 
27 using aidl::android::hardware::wifi::hostapd::IHostapd;
28 using android::wifi_system::HostapdManager;
29 using android::wifi_system::SupplicantManager;
30 
31 namespace {
32 
startAndConfigureVendorHal()33 void startAndConfigureVendorHal() {
34     if (HostapdAidlTestUtils::useAidlService()) {
35         HostapdAidlTestUtils::startAndConfigureVendorHal();
36     } else {
37         HostapdLegacyTestUtils::startAndConfigureVendorHal();
38     }
39 }
40 
stopVendorHal()41 void stopVendorHal() {
42     if (HostapdAidlTestUtils::useAidlService()) {
43         HostapdAidlTestUtils::stopVendorHal();
44     } else {
45         HostapdLegacyTestUtils::stopVendorHal();
46     }
47 }
48 
stopHostapd()49 void stopHostapd() {
50     HostapdManager hostapd_manager;
51     ASSERT_TRUE(hostapd_manager.StopHostapd());
52 }
53 
waitForSupplicantState(bool enable)54 void waitForSupplicantState(bool enable) {
55     SupplicantManager supplicant_manager;
56     int count = 50;  // wait at most 5 seconds
57     while (count-- > 0) {
58         if (supplicant_manager.IsSupplicantRunning() == enable) {
59             return;
60         }
61         usleep(100000);  // 100 ms
62     }
63     LOG(ERROR) << "Unable to " << (enable ? "start" : "stop") << " supplicant";
64 }
65 
toggleWifiFrameworkAndScan(bool enable)66 void toggleWifiFrameworkAndScan(bool enable) {
67     if (enable) {
68         std::system("svc wifi enable");
69         std::system("cmd wifi set-scan-always-available enabled");
70         waitForSupplicantState(true);
71     } else {
72         std::system("svc wifi disable");
73         std::system("cmd wifi set-scan-always-available disabled");
74         waitForSupplicantState(false);
75     }
76 }
77 
78 }  // namespace
79 
getHostapd(const std::string & hostapd_instance_name)80 std::shared_ptr<IHostapd> getHostapd(const std::string& hostapd_instance_name) {
81     return IHostapd::fromBinder(
82             ndk::SpAIBinder(AServiceManager_waitForService(hostapd_instance_name.c_str())));
83 }
84 
85 /**
86  * Disable the Wifi framework, hostapd, and vendor HAL.
87  *
88  * Note: The framework should be disabled to avoid having
89  *       any other clients to the HALs during testing.
90  */
disableHalsAndFramework()91 void disableHalsAndFramework() {
92     toggleWifiFrameworkAndScan(false);
93     stopHostapd();
94     stopVendorHal();
95 
96     // Wait for the services to stop.
97     sleep(3);
98 }
99 
initializeHostapdAndVendorHal(const std::string & hostapd_instance_name)100 void initializeHostapdAndVendorHal(const std::string& hostapd_instance_name) {
101     startAndConfigureVendorHal();
102     HostapdManager hostapd_manager;
103     ASSERT_TRUE(hostapd_manager.StartHostapd());
104     getHostapd(hostapd_instance_name);
105 }
106 
stopHostapdAndVendorHal()107 void stopHostapdAndVendorHal() {
108     stopHostapd();
109     stopVendorHal();
110 }
111 
startWifiFramework()112 void startWifiFramework() {
113     toggleWifiFrameworkAndScan(true);
114 }
115 
setupApIfaceAndGetName(bool isBridged)116 std::string setupApIfaceAndGetName(bool isBridged) {
117     if (HostapdAidlTestUtils::useAidlService()) {
118         return HostapdAidlTestUtils::setupApIfaceAndGetName(isBridged);
119     } else {
120         return HostapdLegacyTestUtils::setupApIfaceAndGetName(isBridged);
121     }
122 }
123