@Test public void getAlternateSetting() throws Exception { UsbDeviceConnection usbDeviceConnection = mockConnectionWithRawDescriptors(new byte[] { 18, 1, 0, 2, 0, 0, 0, 64, -38, 11, 56, 40, 0, 1, 1, 2, 3, 1, 9, 2, 34, 0, 2, 1, 4, -128, -6, 9, 4, 0, 0, 1, -1, -1, -1, 5, 9, 4, 0, 1, 1, -1, -1, -1, 5, 7, 5, -127, 2, 0, 2, 0, 9, 4, 1, 0, 0, -1, -1, -1, 5 }); UsbInterface i0 = mockInterface(0); assertThat(AlternateUsbInterface.forUsbInterface(usbDeviceConnection, i0), equalTo(asList(new AlternateUsbInterface(i0, 0), new AlternateUsbInterface(i0, 1)))); UsbInterface i1 = mockInterface(1); assertThat(AlternateUsbInterface.forUsbInterface(usbDeviceConnection, i1), equalTo(singletonList(new AlternateUsbInterface(i1, 0)))); }
public static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface) { byte[] rawDescriptors = deviceConnection.getRawDescriptors(); List<AlternateUsbInterface> alternateSettings = new ArrayList<>(2); int offset = 0; while(offset < rawDescriptors.length) { int bLength = rawDescriptors[offset] & 0xFF; int bDescriptorType = rawDescriptors[offset + 1] & 0xFF; if (bDescriptorType == 0x04) { int bInterfaceNumber = rawDescriptors[offset + 2] & 0xFF; int bAlternateSetting = rawDescriptors[offset + 3] & 0xFF; if (bInterfaceNumber == usbInterface.getId()) { alternateSettings.add(new AlternateUsbInterface(usbInterface, bAlternateSetting)); } } offset += bLength; } if (alternateSettings.size() < 1) throw new IllegalStateException(); return alternateSettings; }
AlternateUsbInterface { public static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface) { byte[] rawDescriptors = deviceConnection.getRawDescriptors(); List<AlternateUsbInterface> alternateSettings = new ArrayList<>(2); int offset = 0; while(offset < rawDescriptors.length) { int bLength = rawDescriptors[offset] & 0xFF; int bDescriptorType = rawDescriptors[offset + 1] & 0xFF; if (bDescriptorType == 0x04) { int bInterfaceNumber = rawDescriptors[offset + 2] & 0xFF; int bAlternateSetting = rawDescriptors[offset + 3] & 0xFF; if (bInterfaceNumber == usbInterface.getId()) { alternateSettings.add(new AlternateUsbInterface(usbInterface, bAlternateSetting)); } } offset += bLength; } if (alternateSettings.size() < 1) throw new IllegalStateException(); return alternateSettings; } }
AlternateUsbInterface { public static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface) { byte[] rawDescriptors = deviceConnection.getRawDescriptors(); List<AlternateUsbInterface> alternateSettings = new ArrayList<>(2); int offset = 0; while(offset < rawDescriptors.length) { int bLength = rawDescriptors[offset] & 0xFF; int bDescriptorType = rawDescriptors[offset + 1] & 0xFF; if (bDescriptorType == 0x04) { int bInterfaceNumber = rawDescriptors[offset + 2] & 0xFF; int bAlternateSetting = rawDescriptors[offset + 3] & 0xFF; if (bInterfaceNumber == usbInterface.getId()) { alternateSettings.add(new AlternateUsbInterface(usbInterface, bAlternateSetting)); } } offset += bLength; } if (alternateSettings.size() < 1) throw new IllegalStateException(); return alternateSettings; } @VisibleForTesting  AlternateUsbInterface(UsbInterface usbInterface, int alternateSettings); }
AlternateUsbInterface { public static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface) { byte[] rawDescriptors = deviceConnection.getRawDescriptors(); List<AlternateUsbInterface> alternateSettings = new ArrayList<>(2); int offset = 0; while(offset < rawDescriptors.length) { int bLength = rawDescriptors[offset] & 0xFF; int bDescriptorType = rawDescriptors[offset + 1] & 0xFF; if (bDescriptorType == 0x04) { int bInterfaceNumber = rawDescriptors[offset + 2] & 0xFF; int bAlternateSetting = rawDescriptors[offset + 3] & 0xFF; if (bInterfaceNumber == usbInterface.getId()) { alternateSettings.add(new AlternateUsbInterface(usbInterface, bAlternateSetting)); } } offset += bLength; } if (alternateSettings.size() < 1) throw new IllegalStateException(); return alternateSettings; } @VisibleForTesting  AlternateUsbInterface(UsbInterface usbInterface, int alternateSettings); static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface); UsbInterface getUsbInterface(); @SuppressWarnings("SimplifiableIfStatement") @Override boolean equals(Object o); @Override int hashCode(); }
AlternateUsbInterface { public static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface) { byte[] rawDescriptors = deviceConnection.getRawDescriptors(); List<AlternateUsbInterface> alternateSettings = new ArrayList<>(2); int offset = 0; while(offset < rawDescriptors.length) { int bLength = rawDescriptors[offset] & 0xFF; int bDescriptorType = rawDescriptors[offset + 1] & 0xFF; if (bDescriptorType == 0x04) { int bInterfaceNumber = rawDescriptors[offset + 2] & 0xFF; int bAlternateSetting = rawDescriptors[offset + 3] & 0xFF; if (bInterfaceNumber == usbInterface.getId()) { alternateSettings.add(new AlternateUsbInterface(usbInterface, bAlternateSetting)); } } offset += bLength; } if (alternateSettings.size() < 1) throw new IllegalStateException(); return alternateSettings; } @VisibleForTesting  AlternateUsbInterface(UsbInterface usbInterface, int alternateSettings); static List<AlternateUsbInterface> forUsbInterface(UsbDeviceConnection deviceConnection, UsbInterface usbInterface); UsbInterface getUsbInterface(); @SuppressWarnings("SimplifiableIfStatement") @Override boolean equals(Object o); @Override int hashCode();  }