Merge "Restrict enumeration of USB Audio Interfaces"

This commit is contained in:
Paul Mclean
2019-09-05 21:22:20 +00:00
committed by Android (Google) Code Review
4 changed files with 45 additions and 15 deletions

View File

@@ -41,6 +41,8 @@ public final class UsbConfigDescriptor extends UsbDescriptor {
// D4..0 Reserved, set to 0.
private int mMaxPower; // 8:1 Maximum Power Consumption in 2mA units
private boolean mBlockAudio; // leave it off for now. We be replace with a "Developer Option"
private ArrayList<UsbInterfaceDescriptor> mInterfaceDescriptors =
new ArrayList<UsbInterfaceDescriptor>();
@@ -77,21 +79,35 @@ public final class UsbConfigDescriptor extends UsbDescriptor {
mInterfaceDescriptors.add(interfaceDesc);
}
private boolean isAudioInterface(UsbInterfaceDescriptor descriptor) {
return descriptor.getUsbClass() == UsbDescriptor.CLASSID_AUDIO
&& descriptor.getUsbSubclass() == UsbDescriptor.AUDIO_AUDIOSTREAMING;
}
UsbConfiguration toAndroid(UsbDescriptorParser parser) {
if (UsbDescriptorParser.DEBUG) {
Log.d(TAG, " toAndroid()");
}
// NOTE - This code running in the server process.
//TODO (pmclean@) - remove this
// int pid = android.os.Process.myPid();
// int uid = android.os.Process.myUid();
// Log.d(TAG, " ---- pid:" + pid + " uid:" + uid);
String name = parser.getDescriptorString(mConfigIndex);
UsbConfiguration config = new
UsbConfiguration(mConfigValue, name, mAttribs, mMaxPower);
UsbInterface[] interfaces = new UsbInterface[mInterfaceDescriptors.size()];
if (UsbDescriptorParser.DEBUG) {
Log.d(TAG, " " + mInterfaceDescriptors.size() + " interfaces.");
ArrayList<UsbInterface> filteredInterfaces = new ArrayList<UsbInterface>();
for (UsbInterfaceDescriptor descriptor : mInterfaceDescriptors) {
if (!mBlockAudio || !isAudioInterface(descriptor)) {
filteredInterfaces.add(descriptor.toAndroid(parser));
}
}
for (int index = 0; index < mInterfaceDescriptors.size(); index++) {
interfaces[index] = mInterfaceDescriptors.get(index).toAndroid(parser);
}
config.setInterfaces(interfaces);
UsbInterface[] interfaceArray = new UsbInterface[0];
interfaceArray = filteredInterfaces.toArray(interfaceArray);
config.setInterfaces(interfaceArray);
return config;
}

View File

@@ -26,7 +26,7 @@ import java.util.ArrayList;
*/
public final class UsbDescriptorParser {
private static final String TAG = "UsbDescriptorParser";
public static final boolean DEBUG = false;
public static final boolean DEBUG = true;
private final String mDeviceAddr;

View File

@@ -31,7 +31,7 @@ public class UsbEndpointDescriptor extends UsbDescriptor {
public static final int MASK_ENDPOINT_ADDRESS = 0b000000000001111;
public static final int MASK_ENDPOINT_DIRECTION = (byte) 0b0000000010000000;
public static final int DIRECTION_OUTPUT = 0x0000;
public static final int DIRECTION_INPUT = (byte) 0x0080;
public static final int DIRECTION_INPUT = 0x0080;
public static final int MASK_ATTRIBS_TRANSTYPE = 0b00000011;
public static final int TRANSTYPE_CONTROL = 0x00;
@@ -85,7 +85,7 @@ public class UsbEndpointDescriptor extends UsbDescriptor {
}
public int getEndpointAddress() {
return mEndpointAddress;
return mEndpointAddress & MASK_ENDPOINT_ADDRESS;
}
public int getAttributes() {
@@ -108,6 +108,10 @@ public class UsbEndpointDescriptor extends UsbDescriptor {
return mSyncAddress;
}
public int getDirection() {
return mEndpointAddress & UsbEndpointDescriptor.MASK_ENDPOINT_DIRECTION;
}
/* package */ UsbEndpoint toAndroid(UsbDescriptorParser parser) {
if (UsbDescriptorParser.DEBUG) {
Log.d(TAG, "toAndroid() type:"
@@ -137,11 +141,9 @@ public class UsbEndpointDescriptor extends UsbDescriptor {
canvas.openList();
int address = getEndpointAddress();
canvas.writeListItem("Address: "
+ ReportCanvas.getHexString(address & UsbEndpointDescriptor.MASK_ENDPOINT_ADDRESS)
+ ((address & UsbEndpointDescriptor.MASK_ENDPOINT_DIRECTION)
== UsbEndpointDescriptor.DIRECTION_OUTPUT ? " [out]" : " [in]"));
+ ReportCanvas.getHexString(getEndpointAddress())
+ (getDirection() == UsbEndpointDescriptor.DIRECTION_OUTPUT ? " [out]" : " [in]"));
int attributes = getAttributes();
canvas.openListItem();

View File

@@ -31,7 +31,6 @@ import java.util.ArrayList;
*/
public class UsbInterfaceDescriptor extends UsbDescriptor {
private static final String TAG = "UsbInterfaceDescriptor";
protected int mInterfaceNumber; // 2:1 Number of Interface
protected byte mAlternateSetting; // 3:1 Value used to select alternative setting
protected byte mNumEndpoints; // 4:1 Number of Endpoints used for this interface
@@ -73,6 +72,19 @@ public class UsbInterfaceDescriptor extends UsbDescriptor {
return mNumEndpoints;
}
/**
* @param index Index of desired UsbEndpointDescriptor.
* @return the UsbEndpointDescriptor descriptor at the specified index, or
* null if an invalid index.
*/
public UsbEndpointDescriptor getEndpointDescriptor(int index) {
if (index < 0 || index >= mEndpointDescriptors.size()) {
return null;
}
return mEndpointDescriptors.get(index);
}
public int getUsbClass() {
return mUsbClass;
}