From 66c77dfdf9c42308f84f7bd2a4e57925ca97ef6b Mon Sep 17 00:00:00 2001 From: Ricky Niu Date: Thu, 10 Feb 2022 17:12:36 +0800 Subject: [PATCH] Add resetUsbPort in svc usb command Implement resetUsbPort to support manual testing on real device to verify feature functionality from App layer and VTS test. Bug: 216387845 Test: Manual test with "svc usb reset UsbPort" correctly Signed-off-by: Ricky Niu Change-Id: Ieb29de3dd398859607490548a1f3a16f8359ae8f (cherry picked from commit 519062eabd54d4f961bb3c1ac65e937a678f905b) Merged-In: Ieb29de3dd398859607490548a1f3a16f8359ae8f --- .../com/android/commands/svc/UsbCommand.java | 63 ++++++++++++++++++- 1 file changed, 60 insertions(+), 3 deletions(-) diff --git a/cmds/svc/src/com/android/commands/svc/UsbCommand.java b/cmds/svc/src/com/android/commands/svc/UsbCommand.java index 115c1f23c521..66e982a9ec1c 100644 --- a/cmds/svc/src/com/android/commands/svc/UsbCommand.java +++ b/cmds/svc/src/com/android/commands/svc/UsbCommand.java @@ -16,12 +16,18 @@ package com.android.commands.svc; +import android.app.ActivityThread; import android.content.Context; import android.hardware.usb.IUsbManager; import android.hardware.usb.UsbManager; +import android.hardware.usb.UsbPort; +import android.hardware.usb.UsbPortStatus; +import android.os.Looper; import android.os.RemoteException; import android.os.ServiceManager; +import java.util.List; + public class UsbCommand extends Svc.Command { public UsbCommand() { super("usb"); @@ -39,8 +45,9 @@ public class UsbCommand extends Svc.Command { + "usage: svc usb setFunctions [function]\n" + " Set the current usb function. If function is blank, sets to charging.\n" + " svc usb setScreenUnlockedFunctions [function]\n" - + " Sets the functions which, if the device was charging, become current on" - + "screen unlock. If function is blank, turn off this feature.\n" + + " Sets the functions which, if the device was charging,\n" + + " become current on screen unlock.\n" + + " If function is blank, turn off this feature.\n" + " svc usb getFunctions\n" + " Gets the list of currently enabled functions\n" + " possible values of [function] are any of 'mtp', 'ptp', 'rndis',\n" @@ -59,14 +66,21 @@ public class UsbCommand extends Svc.Command { + " svc usb getUsbHalVersion\n" + " Gets current USB Hal Version\n" + " possible values of Hal version are any of 'unknown', 'V1_0', 'V1_1',\n" - + " 'V1_2', 'V1_3'\n"; + + " 'V1_2', 'V1_3'\n" + + " svc usb resetUsbPort [port number]\n" + + " Reset the specified connected usb port\n" + + " default: the first connected usb port\n"; } @Override public void run(String[] args) { if (args.length >= 2) { + Looper.prepareMainLooper(); + Context context = ActivityThread.systemMain().getSystemContext(); + UsbManager usbManager = context.getSystemService(UsbManager.class); IUsbManager usbMgr = IUsbManager.Stub.asInterface(ServiceManager.getService( Context.USB_SERVICE)); + if ("setFunctions".equals(args[1])) { try { usbMgr.setCurrentFunctions(UsbManager.usbFunctionsFromString( @@ -134,6 +148,49 @@ public class UsbCommand extends Svc.Command { System.err.println("Error communicating with UsbManager: " + e); } return; + } else if ("resetUsbPort".equals(args[1])) { + try { + int portNum = args.length >= 3 ? Integer.parseInt(args[2]) : -1; + UsbPort port = null; + UsbPortStatus portStatus = null; + List ports = usbManager.getPorts(); + final int numPorts = ports.size(); + + if (numPorts > 0) { + if (portNum != -1 && portNum < numPorts) { + portStatus = ports.get(portNum).getStatus(); + if (portStatus.isConnected()) { + port = ports.get(portNum); + System.err.println( + "Get the USB port: port" + portNum); + } + } else { + for (portNum = 0; portNum < numPorts; portNum++) { + UsbPortStatus status = ports.get(portNum).getStatus(); + if (status.isConnected()) { + port = ports.get(portNum); + portStatus = status; + System.err.println( + "Use the default USB port: port" + portNum); + break; + } + } + } + if (port != null && portStatus.isConnected()) { + System.err.println( + "Reset the USB port: port" + portNum); + port.resetUsbPort(); + } else { + System.err.println( + "There is no available reset USB port"); + } + } else { + System.err.println("No USB ports"); + } + } catch (Exception e) { + System.err.println("Error communicating with UsbManager: " + e); + } + return; } } System.err.println(longHelp());