Skip to content

Commit

Permalink
Finalizing library after tests
Browse files Browse the repository at this point in the history
- ATUsbDevice: added some helper methods
- Usb4JavaManager: Fixed logging for some parts
- UsbMassStorageDevice: added method with filtering
- extended README.md
  • Loading branch information
andy.rozman committed Apr 15, 2024
1 parent a5155b0 commit c4e8c00
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 54 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The library can be included into your project like this:
<dependency>
<groupId>com.atech-software</groupId>
<artifactId>libaums-usb4java</artifactId>
<version>Tag</version>
<version>0.4.0</version>
</dependency>
```
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.
Expand All @@ -53,6 +53,13 @@ with protocol Bulk-Only (80)):
List<UsbMassStorageDeviceConfig> 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<UsbMassStorageDeviceConfig> configs = UsbMassStorageDevice.getListOfAttachedUsbMassStorageDevices((Sets.newHashSet("27A6"), null));
```


We can get all instances of UsbMassStorageDevice by calling:

```java
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 0 additions & 21 deletions libaums/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,6 @@
<version>1.3.0</version>
</dependency>

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.9.1</version>
</dependency>

<!-- NonNull -->
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>jsr305</artifactId>
<version>3.0.2</version>
</dependency>


<!-- Logging -->

Expand Down Expand Up @@ -113,13 +100,6 @@
<scope>test</scope>
</dependency>


<!--
Any additional libraries - don't forget to copy them in correct project afterwards
-->


</dependencies>


Expand All @@ -128,7 +108,6 @@
<lombok.version>1.18.30</lombok.version>
<log4j-reload.version>1.2.25</log4j-reload.version>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
</properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -216,14 +208,33 @@ public static List<UsbMassStorageDeviceConfig> getListOfAttachedUsbMassStorageDe
return getListOfAttachedUsbMassStorageDevices(null, null);
}

public static List<UsbMassStorageDeviceConfig> 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<UsbMassStorageDeviceConfig> getListOfAttachedUsbMassStorageDevices(Set<String> filterVendor,
Set<String> filterProduct) throws LibAumsException {
List<UsbMassStorageDeviceConfig> outList = new ArrayList<>();

List<ATUsbDevice> 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++) {
Expand All @@ -237,7 +248,7 @@ public static List<UsbMassStorageDeviceConfig> 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());
Expand All @@ -248,14 +259,14 @@ public static List<UsbMassStorageDeviceConfig> 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;
Expand All @@ -266,7 +277,7 @@ public static List<UsbMassStorageDeviceConfig> getListOfAttachedUsbMassStorageDe
}

if (outEndpoint == null || inEndpoint == null) {
log.debug(" Not all needed endpoints found!");
log.debug(" Not all needed endpoints found!");
continue;
}

Expand All @@ -278,7 +289,7 @@ public static List<UsbMassStorageDeviceConfig> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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!");
Expand Down

0 comments on commit c4e8c00

Please sign in to comment.