|
| 1 | +/* |
| 2 | + * Copyright 2022 Google LLC |
| 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 | + * https://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 | +package com.google.devtools.mobileharness.platform.android.lightning.networkconnector; |
| 18 | + |
| 19 | +import com.google.common.annotations.VisibleForTesting; |
| 20 | +import com.google.common.base.Strings; |
| 21 | +import com.google.common.flogger.FluentLogger; |
| 22 | +import com.google.devtools.mobileharness.api.model.error.AndroidErrorId; |
| 23 | +import com.google.devtools.mobileharness.api.model.error.MobileHarnessException; |
| 24 | +import com.google.devtools.mobileharness.platform.android.connectivity.AndroidConnectivityUtil; |
| 25 | +import com.google.devtools.mobileharness.platform.android.connectivity.ConnectToWifiArgs; |
| 26 | +import com.google.devtools.mobileharness.platform.android.lightning.apkinstaller.ApkInstallArgs; |
| 27 | +import com.google.devtools.mobileharness.platform.android.lightning.apkinstaller.ApkInstaller; |
| 28 | +import com.google.devtools.mobileharness.platform.android.lightning.shared.SharedLogUtil; |
| 29 | +import com.google.devtools.mobileharness.platform.android.systemsetting.AndroidSystemSettingUtil; |
| 30 | +import com.google.devtools.mobileharness.shared.util.flags.Flags; |
| 31 | +import com.google.errorprone.annotations.CanIgnoreReturnValue; |
| 32 | +import com.google.wireless.qa.mobileharness.shared.android.WifiUtil; |
| 33 | +import com.google.wireless.qa.mobileharness.shared.api.device.Device; |
| 34 | +import com.google.wireless.qa.mobileharness.shared.log.LogCollector; |
| 35 | +import com.google.wireless.qa.mobileharness.shared.util.DeviceUtil; |
| 36 | +import java.time.Duration; |
| 37 | +import java.util.logging.Level; |
| 38 | +import javax.annotation.Nullable; |
| 39 | + |
| 40 | +/** |
| 41 | + * Network connector for managing Android device network. |
| 42 | + * |
| 43 | + * <p>Please keep all methods in this class sorted in alphabetical order by name. |
| 44 | + */ |
| 45 | +public class NetworkConnector { |
| 46 | + |
| 47 | + private static final FluentLogger logger = FluentLogger.forEnclosingClass(); |
| 48 | + |
| 49 | + private final WifiUtil wifiUtil; |
| 50 | + |
| 51 | + private final AndroidConnectivityUtil connectivityUtil; |
| 52 | + |
| 53 | + private final ApkInstaller apkInstaller; |
| 54 | + |
| 55 | + private final AndroidSystemSettingUtil systemSettingUtil; |
| 56 | + |
| 57 | + public NetworkConnector() { |
| 58 | + this( |
| 59 | + new WifiUtil(), |
| 60 | + new AndroidConnectivityUtil(), |
| 61 | + new ApkInstaller(), |
| 62 | + new AndroidSystemSettingUtil()); |
| 63 | + } |
| 64 | + |
| 65 | + @VisibleForTesting |
| 66 | + NetworkConnector( |
| 67 | + WifiUtil wifiUtil, |
| 68 | + AndroidConnectivityUtil connectivityUtil, |
| 69 | + ApkInstaller apkInstaller, |
| 70 | + AndroidSystemSettingUtil systemSettingUtil) { |
| 71 | + this.wifiUtil = wifiUtil; |
| 72 | + this.connectivityUtil = connectivityUtil; |
| 73 | + this.apkInstaller = apkInstaller; |
| 74 | + this.systemSettingUtil = systemSettingUtil; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Connects device to a WiFi network. |
| 79 | + * |
| 80 | + * @param device the device that connects to given wifi |
| 81 | + * @param connectArgs argument wrapper for connecting wifi |
| 82 | + * @param log log of the currently running test, usually from {@code TestInfo} |
| 83 | + * @return {@code true} if device connects to given wifi successfully, or {@code false} if failed |
| 84 | + * connecting device to given wifi, or it's not a MH managed device. |
| 85 | + * @throws MobileHarnessException if logFailuresOnly is {@code false} and it failed connecting to |
| 86 | + * wifi, or failed to get wifiutil apk path, or falied to install wifiutil on device. |
| 87 | + * @throws InterruptedException if the thread executing the commands is interrupted. |
| 88 | + */ |
| 89 | + @CanIgnoreReturnValue |
| 90 | + public boolean connectToWifi( |
| 91 | + Device device, WifiConnectArgs connectArgs, @Nullable LogCollector<?> log) |
| 92 | + throws MobileHarnessException, InterruptedException { |
| 93 | + String deviceId = device.getDeviceId(); |
| 94 | + if (Flags.disableWifiUtilFunc.getNonNull()) { |
| 95 | + SharedLogUtil.logMsg( |
| 96 | + logger, |
| 97 | + Level.INFO, |
| 98 | + log, |
| 99 | + /* cause= */ null, |
| 100 | + "Ignoring attempt to connect device %s to WiFi while WifiUtil functionality is" |
| 101 | + + " disabled.", |
| 102 | + deviceId); |
| 103 | + return false; |
| 104 | + } |
| 105 | + if (DeviceUtil.inSharedLab()) { |
| 106 | + SharedLogUtil.logMsg( |
| 107 | + logger, |
| 108 | + Level.SEVERE, |
| 109 | + log, |
| 110 | + /* cause= */ null, |
| 111 | + "Ignoring attempt to connect device %s to WiFi while not managing devices.", |
| 112 | + deviceId); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + String wifiSsid = connectArgs.wifiSsid(); |
| 117 | + String wifiPsk = connectArgs.wifiPsk().orElse(null); |
| 118 | + boolean scanSsid = connectArgs.scanSsid().orElse(false); |
| 119 | + boolean logFailuresOnly = connectArgs.logFailuresOnly().orElse(false); |
| 120 | + Duration waitTimeout = connectArgs.waitTimeout().orElse(Duration.ofMinutes(2)); |
| 121 | + int retryNum = connectArgs.retryNum().orElse(0); |
| 122 | + |
| 123 | + int sdkVersion = systemSettingUtil.getDeviceSdkVersion(deviceId); |
| 124 | + String currentSsid = null; |
| 125 | + try { |
| 126 | + currentSsid = connectivityUtil.getNetworkSsid(deviceId, sdkVersion); |
| 127 | + } catch (MobileHarnessException e) { |
| 128 | + SharedLogUtil.logMsg( |
| 129 | + logger, |
| 130 | + Level.WARNING, |
| 131 | + log, |
| 132 | + e, |
| 133 | + "Failed to get current SSID for device %s, try to connect it to WiFi(SSID=%s) anyway", |
| 134 | + deviceId, |
| 135 | + wifiSsid); |
| 136 | + } |
| 137 | + if (wifiSsid.equals(currentSsid)) { |
| 138 | + SharedLogUtil.logMsg( |
| 139 | + logger, |
| 140 | + log, |
| 141 | + "Device %s has already connected to WiFi(SSID=%s), skipped", |
| 142 | + deviceId, |
| 143 | + wifiSsid); |
| 144 | + return true; |
| 145 | + } |
| 146 | + SharedLogUtil.logMsg(logger, log, "Install WifiUtil for %s", deviceId); |
| 147 | + String wifiUtilApkPath; |
| 148 | + try { |
| 149 | + wifiUtilApkPath = wifiUtil.getWifiUtilApkPath(); |
| 150 | + apkInstaller.installApkIfNotExist( |
| 151 | + device, |
| 152 | + ApkInstallArgs.builder().setApkPath(wifiUtilApkPath).setGrantPermissions(true).build(), |
| 153 | + log); |
| 154 | + } catch (MobileHarnessException e) { |
| 155 | + String msg = |
| 156 | + String.format("Failed installing WifiUtil on device %s:%n%s", deviceId, e.getMessage()); |
| 157 | + if (logFailuresOnly) { |
| 158 | + SharedLogUtil.logMsg(logger, Level.WARNING, log, e, "%s", msg); |
| 159 | + return false; |
| 160 | + } else { |
| 161 | + throw new MobileHarnessException( |
| 162 | + AndroidErrorId.NETWORK_CONNECTOR_INSTALL_WIFIUTIL_ERROR, msg, e); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + SharedLogUtil.logMsg(logger, log, "Connecting to WiFi(SSID=%s) for %s", wifiSsid, deviceId); |
| 167 | + boolean connectedToWifi = false; |
| 168 | + Exception connectWifiException = null; |
| 169 | + String msg = String.format("Failed connecting to WiFi(SSID=%s) for %s", wifiSsid, deviceId); |
| 170 | + if (retryNum != 0) { |
| 171 | + msg = |
| 172 | + String.format( |
| 173 | + "Failed connecting to WiFi(SSID=%s) for %s after %d retries", |
| 174 | + wifiSsid, deviceId, retryNum); |
| 175 | + } |
| 176 | + int retryCnt = retryNum; |
| 177 | + ConnectToWifiArgs.Builder argsBuilder = |
| 178 | + ConnectToWifiArgs.builder() |
| 179 | + .setSerial(deviceId) |
| 180 | + .setSdkVersion(sdkVersion) |
| 181 | + .setWifiSsid(wifiSsid) |
| 182 | + .setScanSsid(scanSsid) |
| 183 | + .setWaitTimeout(waitTimeout); |
| 184 | + if (!Strings.isNullOrEmpty(wifiPsk)) { |
| 185 | + argsBuilder.setWifiPsk(wifiPsk); |
| 186 | + } |
| 187 | + |
| 188 | + do { |
| 189 | + if (retryCnt > 0 && retryNum == 0) { |
| 190 | + // Forces connecting to wifi at the last retry when the given retry number > 0. |
| 191 | + argsBuilder.setForceTryConnect(true); |
| 192 | + } |
| 193 | + try { |
| 194 | + connectedToWifi = connectivityUtil.connectToWifi(argsBuilder.build(), log); |
| 195 | + } catch (MobileHarnessException e) { |
| 196 | + connectWifiException = e; |
| 197 | + } |
| 198 | + if (connectedToWifi) { |
| 199 | + return true; |
| 200 | + } |
| 201 | + if (retryNum > 0) { |
| 202 | + try { |
| 203 | + connectivityUtil.reEnableWifi(device.getDeviceId(), log); |
| 204 | + } catch (MobileHarnessException e) { |
| 205 | + connectWifiException = e; |
| 206 | + } |
| 207 | + } |
| 208 | + } while (--retryNum >= 0); |
| 209 | + |
| 210 | + SharedLogUtil.logMsg(logger, Level.WARNING, log, connectWifiException, "%s", msg); |
| 211 | + |
| 212 | + if (!logFailuresOnly) { |
| 213 | + if (connectWifiException != null) { |
| 214 | + throw new MobileHarnessException( |
| 215 | + AndroidErrorId.NETWORK_CONNECTOR_CONNECT_TO_WIFI_ERROR, msg, connectWifiException); |
| 216 | + } else { |
| 217 | + throw new MobileHarnessException( |
| 218 | + AndroidErrorId.NETWORK_CONNECTOR_CONNECT_TO_WIFI_FAILURE, msg); |
| 219 | + } |
| 220 | + } |
| 221 | + return false; |
| 222 | + } |
| 223 | +} |
0 commit comments