diff --git a/README.md b/README.md index 7300c16..f122645 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The library can be included into your project like this: com.atech-software libaums-usb4java - Tag + 0.4.0 ``` There are plenty examples inside the project how to use usb4java (copied from usb4java samples), and I will add some examples on how to use library itself, as soon as I am so far. @@ -53,6 +53,13 @@ with protocol Bulk-Only (80)): List configs = UsbMassStorageDevice.getListOfAttachedUsbMassStorageDevices(); ``` +There is also one additional method here where you can filter by either idVendorString(s) (must be uppercase) or idProductString(s). Inputs are Sets + +```java +List configs = UsbMassStorageDevice.getListOfAttachedUsbMassStorageDevices((Sets.newHashSet("27A6"), null)); +``` + + We can get all instances of UsbMassStorageDevice by calling: ```java @@ -112,6 +119,7 @@ blockDevice.read(deviceOffset, readBuffer); You can see some basic examples in ExampleCreateScsiBlockDevice. + ### Logging Library has Slf4j (Simple Log Framework for Java) integrated. I would recommend that you enable following packages for DEBUG while you are developing, but turn them to INFO for production systems and set your own logging to follow slf4j. diff --git a/libaums/pom.xml b/libaums/pom.xml index 10a7896..64d4379 100755 --- a/libaums/pom.xml +++ b/libaums/pom.xml @@ -54,19 +54,6 @@ 1.3.0 - - com.google.code.gson - gson - 2.9.1 - - - - - com.google.code.findbugs - jsr305 - 3.0.2 - - @@ -113,13 +100,6 @@ test - - - - @@ -128,7 +108,6 @@ 1.18.30 1.2.25 - UTF-8 UTF-8 diff --git a/libaums/src/main/java/com/atech/library/usb/libaums/usb/device/ATUsbDevice.java b/libaums/src/main/java/com/atech/library/usb/libaums/usb/device/ATUsbDevice.java index b9eba23..6eded85 100644 --- a/libaums/src/main/java/com/atech/library/usb/libaums/usb/device/ATUsbDevice.java +++ b/libaums/src/main/java/com/atech/library/usb/libaums/usb/device/ATUsbDevice.java @@ -66,21 +66,17 @@ public boolean isUsbHub() { } @Override - public String getManufacturerString() throws UsbException, UnsupportedEncodingException, UsbDisconnectedException { + public String getManufacturerString() { return this.descriptor.manufacturer(); } -// private ATUsbDeviceDescriptor getMyDescriptor() { -// return (ATUsbDeviceDescriptor)this.getUsbDeviceDescriptor(); -// } - @Override - public String getSerialNumberString() throws UsbException, UnsupportedEncodingException, UsbDisconnectedException { + public String getSerialNumberString() { return this.descriptor.serialNumber; } @Override - public String getProductString() throws UsbException, UnsupportedEncodingException, UsbDisconnectedException { + public String getProductString() { return this.descriptor.product; } @@ -199,5 +195,20 @@ public String toString() { return toLsUsbString(); } + public String getReadableId() { + return String.format("%04x:%04x", this.descriptor.idVendor, this.descriptor.idProduct); + } + + public String getManufacturerAndProductName() { + return this.descriptor.manufacturer + " " + this.descriptor.product; + } + + public String getVendorId() { + return String.format("%04x", this.descriptor.idVendor); + } + + public String getProductId() { + return String.format("%04x", this.descriptor.idProduct); + } } diff --git a/libaums/src/main/java/com/atech/library/usb/libaums/usb4java/Usb4JavaManager.java b/libaums/src/main/java/com/atech/library/usb/libaums/usb4java/Usb4JavaManager.java index bac25ca..15b2bc3 100644 --- a/libaums/src/main/java/com/atech/library/usb/libaums/usb4java/Usb4JavaManager.java +++ b/libaums/src/main/java/com/atech/library/usb/libaums/usb4java/Usb4JavaManager.java @@ -139,7 +139,7 @@ public static ATUsbDevice getDevice(Device device, boolean details, DeviceHandle // Dump port number if available int portNumber = LibUsb.getPortNumber(device); if (portNumber != 0) { - log.info("Connected to port: " + portNumber); + log.debug("Connected to port: " + portNumber); usbDevice.portNumber(portNumber); } @@ -164,7 +164,7 @@ public static ATUsbDevice getDevice(Device device, boolean details, DeviceHandle } usbDevice.descriptor(deviceDescriptor); - log.info(String.format("Bus %03d, Device %03d: Vendor %04x, Product %04x%n", + log.info(String.format("Bus %03d, Device %03d: Vendor %04x, Product %04x", usbDevice.busNumber(), usbDevice.address(), descriptor.idVendor(), descriptor.idProduct())); diff --git a/libaums/src/main/java/com/github/mjdev/libaums/UsbMassStorageDevice.java b/libaums/src/main/java/com/github/mjdev/libaums/UsbMassStorageDevice.java index 5fde469..506d811 100644 --- a/libaums/src/main/java/com/github/mjdev/libaums/UsbMassStorageDevice.java +++ b/libaums/src/main/java/com/github/mjdev/libaums/UsbMassStorageDevice.java @@ -21,16 +21,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; +import java.util.Set; -//import android.annotation.TargetApi; -//import android.content.Context; -//import android.hardware.usb.UsbConstants; -//import android.hardware.usb.UsbDevice; -//import android.hardware.usb.UsbDeviceConnection; -//import android.hardware.usb.UsbEndpoint; -//import android.hardware.usb.UsbInterface; -//import android.hardware.usb.UsbManager; -//import android.os.Build; import com.atech.library.usb.libaums.UsbMassStorageLibrary; import com.atech.library.usb.libaums.data.LibAumsException; import com.atech.library.usb.libaums.data.UsbConstants; @@ -216,14 +208,33 @@ public static List getListOfAttachedUsbMassStorageDe return getListOfAttachedUsbMassStorageDevices(null, null); } - public static List getListOfAttachedUsbMassStorageDevices(Integer filterVendor, Integer filterProduct) throws LibAumsException { - // TODO add filtering + /** + * getListOfAttachedUsbMassStorageDevices() filtered with vendor or product (if both are null no filtering will be done) + * @param filterVendor - Set of Vendors as String (in uppercase letter, so something like 3344 or 1D6B) + * @param filterProduct - Set of Products as String (uppercase) + * @return + * @throws LibAumsException + */ + public static List getListOfAttachedUsbMassStorageDevices(Set filterVendor, + Set filterProduct) throws LibAumsException { List outList = new ArrayList<>(); List deviceList = Usb4JavaManager.getDeviceList(); for (ATUsbDevice device : deviceList) { - log.debug("Found usb device: " + device); + log.debug("Found usb device: {} - {}", device.getReadableId() , device.getManufacturerAndProductName()); + + if (filterVendor!=null || filterProduct!=null) { + if (filterVendor!=null && !filterVendor.contains(device.getVendorId())) { + log.debug(" Device filtered out."); + continue; + } + + if (filterProduct!=null && !filterProduct.contains(device.getProductId())) { + log.debug(" Device filtered out."); + continue; + } + } //int interfaceCount = device.getInterfaceCount(); //for (int i = 0; i < interfaceCount; i++) { @@ -237,7 +248,7 @@ public static List getListOfAttachedUsbMassStorageDe if (usbInterface.bInterfaceClass() != USB_CLASS_MASS_STORAGE || usbInterface.bInterfaceSubClass() != MASS_STORAGE_SUBCLASS_SCSI || usbInterface.bInterfaceProtocol() != MASS_STORAGE_PROTOCOL_BBB_BULK_ONLY) { - log.debug(" Device interface not suitable ! Found class={},subclass={},protocol={}), required=8/6/80 (Mass Storage/SCSI/Bulk-Only)", + log.debug(" Device interface not suitable ! Found class={},subclass={},protocol={}), required=8/6/80 (Mass Storage/SCSI/Bulk-Only)", usbInterface.bInterfaceClass(), usbInterface.bInterfaceSubClass(), usbInterface.bInterfaceProtocol()); @@ -248,14 +259,14 @@ public static List getListOfAttachedUsbMassStorageDe // One IN and one OUT endpoint int endpointCount = usbInterface.bNumEndpoints(); if (endpointCount != 2) { - log.debug(" interface endpoint count != 2"); + log.debug(" Interface endpoint count != 2"); } ATUsbEndpointDescriptor outEndpoint = null; ATUsbEndpointDescriptor inEndpoint = null; for (int j = 0; j < endpointCount; j++) { ATUsbEndpointDescriptor endpoint = usbInterface.getEndpoint(j); - log.debug(" found usb endpoint: " + endpoint); + log.debug(" Found usb endpoint: " + endpoint); if (endpoint.getTransferType() == UsbConstants.USB_ENDPOINT_XFER_BULK) { if (endpoint.getDirection() == UsbConstants.USB_DIR_OUT) { outEndpoint = endpoint; @@ -266,7 +277,7 @@ public static List getListOfAttachedUsbMassStorageDe } if (outEndpoint == null || inEndpoint == null) { - log.debug(" Not all needed endpoints found!"); + log.debug(" Not all needed endpoints found!"); continue; } @@ -278,7 +289,7 @@ public static List getListOfAttachedUsbMassStorageDe .outEndpointAddress(outEndpoint.bEndpointAddress()) .build(); - log.info(" Device is relevant: {}", config.getReadableDeviceId()); + log.info("Found relevant usb device: {} - {}", device.getReadableId() , device.getManufacturerAndProductName()); outList.add(config); } diff --git a/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFile.java b/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFile.java index 5f0805d..3af142a 100644 --- a/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFile.java +++ b/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFile.java @@ -19,8 +19,6 @@ import com.atech.library.usb.libaums.data.LibAumsException; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import java.io.Closeable; import java.io.IOException; import java.nio.ByteBuffer; @@ -47,8 +45,7 @@ public interface UsbFile extends Closeable { * @param path The path to the resource to search. * @return UsbFile directory or file if found, null otherwise. */ - @Nullable - UsbFile search(@Nonnull String path) throws LibAumsException; + UsbFile search(String path) throws LibAumsException; /** * diff --git a/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFileOutputStream.java b/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFileOutputStream.java index 100aa28..b7c2f37 100644 --- a/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFileOutputStream.java +++ b/libaums/src/main/java/com/github/mjdev/libaums/fs/UsbFileOutputStream.java @@ -20,7 +20,6 @@ import com.atech.library.usb.libaums.data.LibAumsException; -import javax.annotation.Nonnull; import java.io.IOException; import java.io.OutputStream; import java.nio.ByteBuffer; @@ -33,7 +32,7 @@ public class UsbFileOutputStream extends OutputStream { private UsbFile file; private int currentByteOffset = 0; - public UsbFileOutputStream(@Nonnull UsbFile file) { + public UsbFileOutputStream(UsbFile file) { if(file.isDirectory()) { throw new RuntimeException("UsbFileOutputStream cannot be created on directory!");