Skip to content

Commit

Permalink
- prepared classes so that we get device list with all data needed to…
Browse files Browse the repository at this point in the history
… create UsbMassStorageDevice instances

- removed remainder of android files
- added UsbConstants from Android
- added some other helper classes
  • Loading branch information
andy.rozman committed Apr 11, 2024
1 parent 3142199 commit ef8b833
Show file tree
Hide file tree
Showing 19 changed files with 738 additions and 238 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ compile 'com.github.mjdev:libaums:0.3'
```
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.

### How to use (2)

I added special class where you can configure what part of library you need, so you can load Block Device and/or you can load Partitions. By default we load booth, but this can be changed like this:

```java
// turn off partition loading
UsbMassStorageLibrarySettings.LOAD_PARTITIONS = false;
// turn off block device loading
UsbMassStorageLibrarySettings.LOAD_PARTITIONS = false;
```




### Status

Just started on this project in April 2024, and it will take me some time to get it to working state.
Expand Down
11 changes: 11 additions & 0 deletions docs/Changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

CHANGELOG
=========

0.4.0 [10.4.2024]
- initial import from https://github.com/norani/libaums-develop
- added sl4j logging, removing all android logging
- added usb4java and examples from it



40 changes: 35 additions & 5 deletions libaums/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@
<artifactId>libaums-usb4java</artifactId>
<description>USB mass storage library for access via SCSI.</description>

<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>mvnrepository</id>
<name>mvnrepository</name>
<url>https://www.mvnrepository.com</url>
</repository>
</repositories>


<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>8</source>
<target>8</target>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
Expand All @@ -36,7 +50,7 @@
<!-- USB4Java -->
<dependency>
<groupId>org.usb4java</groupId>
<artifactId>usb4java-javax</artifactId>
<artifactId>usb4java-javax</artifactId> <!-- usb4java-javax -->
<version>1.3.0</version>
</dependency>

Expand All @@ -55,20 +69,35 @@


<!-- Logging -->

<!-- This is drop-in replacement for outdated log4j -->
<dependency>
<groupId>ch.qos.reload4j</groupId>
<artifactId>reload4j</artifactId>
<version>${log4j-reload.version}</version>
</dependency>

<!-- slf4j API -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>

<!-- slf4j over log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId> <!-- slf4j-log4j12 -->
<version>${slf4j.version}</version>
<exclusions>
<exclusion>
<groupId>ch.qos.reload4j</groupId>
<artifactId>reload4j</artifactId>
</exclusion>
</exclusions>
</dependency>




<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
Expand Down Expand Up @@ -97,6 +126,7 @@
<properties>
<slf4j.version>1.7.36</slf4j.version>
<lombok.version>1.18.30</lombok.version>
<log4j-reload.version>1.2.25</log4j-reload.version>


<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
17 changes: 0 additions & 17 deletions libaums/proguard-rules.pro

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.atech.library.usb.libaums;

public class UsbConstants {

public static final int USB_CLASS_APP_SPEC = 254;
public static final int USB_CLASS_AUDIO = 1;
public static final int USB_CLASS_CDC_DATA = 10;
public static final int USB_CLASS_COMM = 2;
public static final int USB_CLASS_CONTENT_SEC = 13;
public static final int USB_CLASS_CSCID = 11;
public static final int USB_CLASS_HID = 3;
public static final int USB_CLASS_HUB = 9;
public static final int USB_CLASS_MASS_STORAGE = 8;
public static final int USB_CLASS_MISC = 239;
public static final int USB_CLASS_PER_INTERFACE = 0;
public static final int USB_CLASS_PHYSICA = 5;
public static final int USB_CLASS_PRINTER = 7;
public static final int USB_CLASS_STILL_IMAGE = 6;
public static final int USB_CLASS_VENDOR_SPEC = 255;
public static final int USB_CLASS_VIDEO = 14;
public static final int USB_CLASS_WIRELESS_CONTROLLER = 224;
public static final int USB_DIR_IN = 128;
public static final int USB_DIR_OUT = 0;
public static final int USB_ENDPOINT_DIR_MASK = 128;
public static final int USB_ENDPOINT_NUMBER_MASK = 15;
public static final int USB_ENDPOINT_XFERTYPE_MASK = 3;
public static final int USB_ENDPOINT_XFER_BULK = 2;
public static final int USB_ENDPOINT_XFER_CONTROL = 0;
public static final int USB_ENDPOINT_XFER_INT = 3;
public static final int USB_ENDPOINT_XFER_ISOC = 1;
public static final int USB_INTERFACE_SUBCLASS_BOOT = 1;
public static final int USB_SUBCLASS_VENDOR_SPEC = 255;
public static final int USB_TYPE_CLASS = 32;
public static final int USB_TYPE_MASK = 96;
public static final int USB_TYPE_RESERVED = 96;
public static final int USB_TYPE_STANDARD = 0;
public static final int USB_TYPE_VENDOR = 64;


}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.atech.library.usb.libaums;

import com.atech.library.usb.libaums.device.UsbConfigurationDescriptor;
import com.atech.library.usb.libaums.device.UsbDevice;
import com.atech.library.usb.libaums.device.UsbDeviceDescriptor;
import com.atech.library.usb.libaums.device.ATUsbConfigurationDescriptor;
import com.atech.library.usb.libaums.device.ATUsbDevice;
import com.atech.library.usb.libaums.device.ATUsbDeviceDescriptor;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.usb4java.*;
Expand All @@ -14,17 +14,17 @@
*/
public class UsbManagement {

public List<UsbDevice> getFullDeviceList() {
public static List<ATUsbDevice> getFullDeviceList() {
return getFullDeviceList(null);
}


public List<UsbDevice> getDeviceList() {
public static List<ATUsbDevice> getDeviceList() {
return getFullDeviceList(9); // 9 = Hubs
}


public List<UsbDevice> getFullDeviceList(int...excludeClass) {
public static List<ATUsbDevice> getFullDeviceList(int...excludeClass) {
// Create the libusb context
Context context = new Context();

Expand All @@ -35,7 +35,7 @@ public List<UsbDevice> getFullDeviceList(int...excludeClass) {
excludesForClass.add(aClass);
}

List<UsbDevice> outList = new ArrayList<>();
List<ATUsbDevice> outList = new ArrayList<>();

// Initialize the libusb context
int result = LibUsb.init(context);
Expand All @@ -57,7 +57,7 @@ public List<UsbDevice> getFullDeviceList(int...excludeClass) {
// Iterate over all devices and list them
for (Device device: list)
{
UsbDevice usbDevice = getDevice(device, false, null, excludesForClass);
ATUsbDevice usbDevice = getDevice(device, true, null, excludesForClass);

//
// UsbDevice usbDevice = new UsbDevice();
Expand Down Expand Up @@ -116,11 +116,11 @@ public List<UsbDevice> getFullDeviceList(int...excludeClass) {

}

public UsbDevice getDevice(Device device, boolean details, DeviceHandle handle, Set<Integer> excludesForClass) {
public static ATUsbDevice getDevice(Device device, boolean details, DeviceHandle handle, Set<Integer> excludesForClass) {

UsbDevice usbDevice = new UsbDevice();
usbDevice.setAddress(LibUsb.getDeviceAddress(device));
usbDevice.setBusNumber(LibUsb.getBusNumber(device));
ATUsbDevice usbDevice = new ATUsbDevice();
usbDevice.address(LibUsb.getDeviceAddress(device));
usbDevice.busNumber(LibUsb.getBusNumber(device));
DeviceDescriptor descriptor = new DeviceDescriptor();
int result = LibUsb.getDeviceDescriptor(device, descriptor);
if (result < 0)
Expand All @@ -133,7 +133,7 @@ public UsbDevice getDevice(Device device, boolean details, DeviceHandle handle,
return null;
}

UsbDeviceDescriptor deviceDescriptor = new UsbDeviceDescriptor();
ATUsbDeviceDescriptor deviceDescriptor = new ATUsbDeviceDescriptor();
deviceDescriptor.loadData(descriptor);

if (handle == null) {
Expand All @@ -156,7 +156,7 @@ public UsbDevice getDevice(Device device, boolean details, DeviceHandle handle,
int portNumber = LibUsb.getPortNumber(device);
if (portNumber != 0) {
System.out.println("Connected to port: " + portNumber);
usbDevice.setPortNumber(portNumber);
usbDevice.portNumber(portNumber);
}

// Dump parent device if available
Expand All @@ -171,30 +171,29 @@ public UsbDevice getDevice(Device device, boolean details, DeviceHandle handle,

// Dump the device speed
int speed = LibUsb.getDeviceSpeed(device);
usbDevice.setISpeed(speed);
usbDevice.setSpeed(DescriptorUtils.getSpeedName(speed));
System.out.println("Speed: "
+ usbDevice.getSpeed());
usbDevice.speed(speed);
usbDevice.speedString(DescriptorUtils.getSpeedName(speed));
System.out.println("Speed: " + usbDevice.getSpeed());

deviceDescriptor.setConfigurationDescriptors(
getConfigurationDescriptors(device, usbDevice.getDescriptor().getBNumConfigurations(), handle));
deviceDescriptor.configurationDescriptors(
getConfigurationDescriptors(device, deviceDescriptor.bNumConfigurations(), handle));

}

usbDevice.setDescriptor(deviceDescriptor);
usbDevice.descriptor(deviceDescriptor);
System.out.format(
"Bus %03d, Device %03d: Vendor %04x, Product %04x%n",
usbDevice.getBusNumber(), usbDevice.getAddress(), descriptor.idVendor(),
usbDevice.busNumber(), usbDevice.address(), descriptor.idVendor(),
descriptor.idProduct());

return usbDevice;
}

public static List<UsbConfigurationDescriptor> getConfigurationDescriptors(final Device device,
final int numConfigurations,
DeviceHandle handle)
public static List<ATUsbConfigurationDescriptor> getConfigurationDescriptors(final Device device,
final int numConfigurations,
DeviceHandle handle)
{
List<UsbConfigurationDescriptor> list = new ArrayList<>();
List<ATUsbConfigurationDescriptor> list = new ArrayList<>();
for (byte i = 0; i < numConfigurations; i += 1)
{
final ConfigDescriptor descriptor = new ConfigDescriptor();
Expand All @@ -208,7 +207,7 @@ public static List<UsbConfigurationDescriptor> getConfigurationDescriptors(final
{
System.out.println(descriptor.dump().replaceAll("(?m)^",
" "));
UsbConfigurationDescriptor usbConfigDescriptor = new UsbConfigurationDescriptor();
ATUsbConfigurationDescriptor usbConfigDescriptor = new ATUsbConfigurationDescriptor();
usbConfigDescriptor.loadData(descriptor);

list.add(usbConfigDescriptor);
Expand Down Expand Up @@ -265,16 +264,17 @@ public static void main(String[] args) {

UsbManagement usbManagement = new UsbManagement();

List<UsbDevice> fullDeviceList = usbManagement.getDeviceList();
List<ATUsbDevice> fullDeviceList = usbManagement.getDeviceList();

Gson gson = new GsonBuilder().setPrettyPrinting().create();

System.out.println(gson.toJson(fullDeviceList));
//System.out.println(gson.toJson(fullDeviceList));

System.out.println("test output: ==================================================");

for (UsbDevice usbDevice : fullDeviceList) {
ATUsbDevice atUsbDevice = fullDeviceList.get(0);

}
System.out.println(atUsbDevice.toLsUsbString());

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.atech.library.usb.libaums;

import lombok.Data;

@Data
public class UsbMassStorageDeviceConfig {





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.atech.library.usb.libaums;

public class UsbMassStorageLibrarySettings {

public static boolean LOAD_BLOCK_DEVICE = true; // loading block device by default

public static boolean LOAD_PARTITIONS = true; // loading partitions by default

}
Loading

0 comments on commit ef8b833

Please sign in to comment.