Skip to content

Commit

Permalink
Internal change
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 717982831
  • Loading branch information
DeviceInfra authored and copybara-github committed Jan 27, 2025
1 parent b6e9356 commit d874384
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -734,6 +734,76 @@ public boolean pingSuccessfully(String serial) throws InterruptedException {
return false;
}

/**
* Checks if the device is reachable.
*
* <p>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<String> 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<String> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ java_library(
name = "dimension",
srcs = ["Dimension.java"],
deps = [
"@maven//:com_google_guava_guava",
"@maven//:com_google_guava_guava:base-gwt",
],
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -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. */
Expand All @@ -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,
/**
Expand Down

0 comments on commit d874384

Please sign in to comment.