diff --git a/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java b/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java index 1a830a738..1b0358f90 100644 --- a/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java +++ b/src/java/com/google/devtools/mobileharness/platform/android/connectivity/AndroidConnectivityUtil.java @@ -675,7 +675,7 @@ public String getWifiRssi(String serial) throws MobileHarnessException, Interrup * @throws InterruptedException if current thread is interrupted during this method */ public double pingSuccessRate(String serial, String host, int count) throws InterruptedException { - String shell = String.format(ADB_SHELL_TEMPLATE_PING, SHORT_PING_TIMEOUT.getSeconds(), host); + String shell = String.format(ADB_SHELL_TEMPLATE_PING, SHORT_PING_TIMEOUT.toSeconds(), host); int successCount = 0; for (int i = 0; i < count; i++) { String output = null; @@ -734,6 +734,76 @@ public boolean pingSuccessfully(String serial) throws InterruptedException { return false; } + /** + * Checks if the device is reachable. + * + *

This function first tries to connect to the device via adb. If it fails, it then tries to + * ping the device. If the ping command returns "0% packet loss", then the device is reachable. + * + * @param deviceId The device ID of the device to check. + * @return True if the device is reachable, false otherwise. + */ + public boolean canPingDevice(String deviceId) + throws MobileHarnessException, InterruptedException { + try { + adbUtil.connect(deviceId); + } catch (MobileHarnessException e) { + // Log the original connection error for debugging + logger.atWarning().withCause(e).log( + "Failed to connect to device %s. Trying to ping instead.", deviceId); + + try { + String output = adb.runShell(deviceId, "ping -c 1"); // Ping the device + // If the ping command returns "0% packet loss", then the device is reachable. + if (!output.contains("0% packet loss")) { + logger.atWarning().log("Packet loss detected when pinging device %s.", deviceId); + return false; + } + return true; + } catch (MobileHarnessException pingException) { + logger.atWarning().withCause(e).log("Failed to ping device %s", deviceId); + return false; + } + } + return true; + } + + /** + * Gets the ping response time value for the given device. + * + * @param deviceId The device ID of the device to check. + * @param metric The metric to get the value for. + * @return The ping response time value for the given device. + */ + public String getPingResponseValue(String deviceId, String metric) throws InterruptedException { + try { + String pingOutput = adb.runShell(deviceId, "ping -c 5"); + + // Extract the last line of the ping output. + List lines = Splitter.on("\n").splitToList(pingOutput.trim()); + // The last line is the ping statistics. + String lastLine = Iterables.getLast(lines); + + // Extract the RTT values. It will be in the format: + // rtt min/avg/max/mdev = 1.234/1.234/1.234/0.000 ms + List values = + Splitter.on("/").splitToList(Splitter.on("=").splitToList(lastLine).getLast()); + + switch (metric) { + case "avg": + return values.get(1); + case "max": + return values.get(2); + default: + throw new IllegalArgumentException("Invalid metric: " + metric); + } + } catch (MobileHarnessException | IndexOutOfBoundsException e) { + logger.atWarning().withCause(e).log( + "Failed to get ping response value for device %s", deviceId); + return "failed"; + } + } + /** * Disable and re-enables Wifi of a device. This only works if the device has root access and has * already become root. diff --git a/src/java/com/google/wireless/qa/mobileharness/shared/constant/BUILD b/src/java/com/google/wireless/qa/mobileharness/shared/constant/BUILD index 124f9ccb3..e6bbe0b59 100644 --- a/src/java/com/google/wireless/qa/mobileharness/shared/constant/BUILD +++ b/src/java/com/google/wireless/qa/mobileharness/shared/constant/BUILD @@ -46,7 +46,7 @@ java_library( name = "dimension", srcs = ["Dimension.java"], deps = [ - "@maven//:com_google_guava_guava", + "@maven//:com_google_guava_guava:base-gwt", ], ) diff --git a/src/java/com/google/wireless/qa/mobileharness/shared/constant/Dimension.java b/src/java/com/google/wireless/qa/mobileharness/shared/constant/Dimension.java index 042e815c9..147884a43 100644 --- a/src/java/com/google/wireless/qa/mobileharness/shared/constant/Dimension.java +++ b/src/java/com/google/wireless/qa/mobileharness/shared/constant/Dimension.java @@ -29,6 +29,8 @@ public enum Name { API_LEVEL, /** The cpu architecture of the device, such as armv7, arm64, x86_64. */ ARCHITECTURE, + /** The average ping time of the device. */ + AVG_PING_TIME, /** The battery level of the device. The value is 0-100. */ BATTERY_LEVEL, /** The battery status of the device. The common value is OK. */ @@ -106,6 +108,8 @@ public enum Name { LABEL, /** Network connection stability. */ MAC_ADDRESS, + /** The max ping time of the device. */ + MAX_PING_TIME, /** Network provider of the SIM card. */ MCC_MNC, /** The model name of the device. */ @@ -131,6 +135,8 @@ public enum Name { PING_GOOGLE_STABILITY, /** Device pool name. Values include: shared/shared_without_recovery/group_shared/dedicated. */ POOL, + /** The reachability of the device. */ + REACHABILITY, /** The release version of the device. */ RELEASE_VERSION, /**