1 /*
2  * Copyright (C) 2022 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 <VtsCoreUtil.h>
20 #include <aidl/android/hardware/wifi/IWifi.h>
21 #include <android-base/logging.h>
22 #include <wifi_system/supplicant_manager.h>
23 
24 #include "wifi_aidl_test_utils.h"
25 
26 using android::wifi_system::SupplicantManager;
27 
28 // Helper methods to interact with wifi_aidl_test_utils
29 namespace SupplicantAidlTestUtils {
30 const std::string kWifiInstanceNameStr = std::string() + IWifi::descriptor + "/default";
31 const char* kWifiInstanceName = kWifiInstanceNameStr.c_str();
32 
33 // Initialize the driver and firmware to STA mode using the vendor HAL.
initializeDriverAndFirmware(const std::string & wifi_instance_name)34 void initializeDriverAndFirmware(const std::string& wifi_instance_name) {
35     // Skip if wifi instance is not set.
36     if (wifi_instance_name == "") {
37         return;
38     }
39     if (getWifi(wifi_instance_name.c_str()) != nullptr) {
40         std::shared_ptr<IWifiChip> wifi_chip = getWifiChip(wifi_instance_name.c_str());
41         int mode_id;
42         EXPECT_TRUE(configureChipToSupportConcurrencyType(wifi_chip, IfaceConcurrencyType::STA,
43                                                           &mode_id));
44     } else {
45         LOG(WARNING) << __func__ << ": Vendor HAL not supported";
46     }
47 }
48 
49 // Deinitialize the driver and firmware using the vendor HAL.
deInitializeDriverAndFirmware(const std::string & wifi_instance_name)50 void deInitializeDriverAndFirmware(const std::string& wifi_instance_name) {
51     // Skip if wifi instance is not set.
52     if (wifi_instance_name == "") {
53         return;
54     }
55     if (getWifi(wifi_instance_name.c_str()) != nullptr) {
56         stopWifiService(wifi_instance_name.c_str());
57     } else {
58         LOG(WARNING) << __func__ << ": Vendor HAL not supported";
59     }
60 }
61 
waitForSupplicantState(bool is_running)62 bool waitForSupplicantState(bool is_running) {
63     SupplicantManager supplicant_manager;
64     int count = 50; /* wait at most 5 seconds for completion */
65     while (count-- > 0) {
66         if (supplicant_manager.IsSupplicantRunning() == is_running) {
67             return true;
68         }
69         usleep(100000);
70     }
71     LOG(ERROR) << "Unable to " << (is_running ? "start" : "stop") << " supplicant";
72     return false;
73 }
74 
waitForSupplicantStart()75 bool waitForSupplicantStart() {
76     return waitForSupplicantState(true);
77 }
78 
waitForSupplicantStop()79 bool waitForSupplicantStop() {
80     return waitForSupplicantState(false);
81 }
82 
waitForWifiHalStop(const std::string & wifi_instance_name)83 bool waitForWifiHalStop(const std::string& wifi_instance_name) {
84     std::shared_ptr<IWifi> wifi = getWifi(wifi_instance_name.c_str());
85     int count = 50; /* wait at most 5 seconds for completion */
86     while (count-- > 0) {
87         if (wifi != nullptr) {
88             bool started = false;
89             auto status = wifi->isStarted(&started);
90             if (status.isOk() && !started) {
91                 return true;
92             }
93         }
94         usleep(100000);
95         wifi = getWifi(wifi_instance_name.c_str());
96     }
97     LOG(ERROR) << "Wifi HAL was not stopped";
98     return false;
99 }
100 
waitForFrameworkReady()101 bool waitForFrameworkReady() {
102     int waitCount = 15;
103     do {
104         // Check whether package service is ready or not.
105         if (!testing::checkSubstringInCommandOutput("/system/bin/service check package",
106                                                     ": not found")) {
107             return true;
108         }
109         LOG(INFO) << "Framework is not ready";
110         sleep(1);
111     } while (waitCount-- > 0);
112     return false;
113 }
114 
useAidlService()115 bool useAidlService() {
116     return isAidlServiceAvailable(kWifiInstanceName);
117 }
118 
startSupplicant()119 void startSupplicant() {
120     initializeDriverAndFirmware(kWifiInstanceName);
121     SupplicantManager supplicant_manager;
122     ASSERT_TRUE(supplicant_manager.StartSupplicant());
123     ASSERT_TRUE(supplicant_manager.IsSupplicantRunning());
124 }
125 
stopSupplicantService()126 void stopSupplicantService() {
127     SupplicantManager supplicant_manager;
128     ASSERT_TRUE(supplicant_manager.StopSupplicant());
129     deInitializeDriverAndFirmware(kWifiInstanceName);
130     ASSERT_FALSE(supplicant_manager.IsSupplicantRunning());
131 }
132 
stopWifiFramework(const std::string & wifi_instance_name)133 bool stopWifiFramework(const std::string& wifi_instance_name) {
134     std::system("svc wifi disable");
135     std::system("cmd wifi set-scan-always-available disabled");
136     return waitForSupplicantStop() && waitForWifiHalStop(wifi_instance_name);
137 }
138 
initializeService()139 void initializeService() {
140     ASSERT_TRUE(stopWifiFramework(kWifiInstanceName));
141     std::system("/system/bin/start");
142     ASSERT_TRUE(waitForFrameworkReady());
143     stopSupplicantService();
144     startSupplicant();
145 }
146 }  // namespace SupplicantAidlTestUtils
147