Skip to content

Commit

Permalink
Implementation with integration of usb4java is done, untested for now
Browse files Browse the repository at this point in the history
- Usb4JavaUsbDeviceCommunication all methods are now implemented, but untested
- UsbMassStorageLibrary added something to disposeLibrary
- updated README with some more sample code
  • Loading branch information
andy.rozman committed Apr 12, 2024
1 parent 2eb9286 commit a0518b7
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 90 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,27 @@ UsbMassStorageDevice device = UsbMassStorageDevice.getMassStorageDevice(config);
Once we have instance we need to initialize device, which will open device and claim its interface:

```java
// see previous step
// see previous step we have instance of UsbMassStorageDevice called device
device.init();
```

After device is initialized we can work with it get partitions, get blockDevice:



When we are done with specific device we need to close it:

```java
device.close();
```

When we are done with library we need to call this, to dispose of LibUsb instance (instance is created automatically, but needs to be disposed manually):

```java
UsbMassStorageLibrary.
```


### Status

Just started on this project in April 2024, and it will take me some time to get it to working state.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public int bulkOutTransfer(byte[] data, int length) {
buffer.put(data, 0, length);
//put(byte[] src, int offset, int length)
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(handle, deviceSettings.getOutEndpoint(), buffer,
int result = LibUsb.bulkTransfer(handle, deviceSettings.getOutEndpointAddress(), buffer,
transferred, TIMEOUT);
if (result != LibUsb.SUCCESS)
{
Expand All @@ -84,7 +84,7 @@ public int bulkInTransfer(byte[] data, int length) {
ByteBuffer buffer = BufferUtils.allocateByteBuffer(length).order(
ByteOrder.LITTLE_ENDIAN);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(handle, deviceSettings.getInEndpoint(), buffer,
int result = LibUsb.bulkTransfer(handle, deviceSettings.getInEndpointAddress(), buffer,
transferred, TIMEOUT);
if (result != LibUsb.SUCCESS)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.atech.library.usb.libaums.usb4java.Usb4JavaManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.usb4java.*;

Expand Down Expand Up @@ -56,6 +57,7 @@ public void openDevice(UsbMassStorageDeviceConfig usbDeviceSettings) {
}


@SneakyThrows
public static void main(String[] args) {

//Usb4JavaManager usbManagement = new Usb4JavaManager();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ public static void disposeLibrary() {
if (libraryInitialized) {
// Deinitialize the libusb context
LibUsb.exit(context);
libraryInitialized = false;
context = null;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
@Slf4j
public class Usb4JavaUsbDeviceCommunication implements UsbCommunication {

private static final int TRANSFER_TIMEOUT = 21000;
DeviceHandle deviceHandle;
UsbMassStorageDeviceConfig deviceConfig;

Expand All @@ -25,14 +26,14 @@ public Usb4JavaUsbDeviceCommunication(UsbMassStorageDeviceConfig usbMassStorageD
}

public void openDevice() throws LibAumsException {
log.info("openDevice {}:{}")
log.info("openDevice {}", deviceConfig.getReadableDeviceId());
Context context = UsbMassStorageLibrary.initLibrary();

// Open device
DeviceHandle handle = LibUsb.openDeviceWithVidPid(context, deviceConfig.getVendorId(),
deviceConfig.getProductId());
if (handle == null) {
System.err.println("Test device not found.");
log.error("Device {} not found, or could not be opened.", deviceConfig.getReadableDeviceId());
System.exit(1);
}

Expand All @@ -44,23 +45,59 @@ public void openDevice() throws LibAumsException {
{
throw new LibUsbException("Unable to claim interface", result);
}

log.info("Device {} opened and interafce {} claimed.", deviceConfig.getReadableDeviceId(), deviceConfig.getInterfaceNumber());
}


@Override
public int bulkOutTransfer(byte[] buffer, int length) {
public int bulkOutTransfer(byte[] data, int length) throws LibAumsException {
// TODO implement Usb4JavaUsbDeviceCommunication

// return deviceConnection.bulkTransfer(outEndpoint, buffer, length, TRANSFER_TIMEOUT);
log.info("BulkOutTransfer (data={},length={})", data, length);

ByteBuffer buffer = BufferUtils.allocateByteBuffer(data.length);
buffer.put(data);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(deviceHandle, deviceConfig.getOutEndpointAddress(), buffer,
transferred, TRANSFER_TIMEOUT);
if (result != LibUsb.SUCCESS) {
throw new LibUsbException("Unable to send data", result);
}
log.info(transferred.get() + " bytes sent to device");

// return deviceConnection.bulkTransfer(outEndpoint, buffer, length, TRANSFER_TIMEOUT);

return 0;
return LibUsb.SUCCESS;
}

@Override
public int bulkOutTransfer(byte[] buffer, int offset, int length) {
public int bulkOutTransfer(byte[] data, int offset, int length) throws LibAumsException{
// TODO implement Usb4JavaUsbDeviceCommunication

if (offset==0) {
return bulkOutTransfer(data, length);
}

log.info("BulkOutTransfer (data={},length={},offset={})", data, length, offset);

int remaining = length-offset;
byte[] newData = new byte[length-offset];
System.arraycopy(data, offset, newData, 0, remaining);

ByteBuffer buffer = BufferUtils.allocateByteBuffer(newData.length);
buffer.put(newData);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(deviceHandle, deviceConfig.getOutEndpointAddress(), buffer,
transferred, TRANSFER_TIMEOUT);
if (result != LibUsb.SUCCESS)
{
throw LibAumsException.createWithLibUsbException("Unable to send data", result);
}
log.info(transferred.get() + " bytes sent to device");

return LibUsb.SUCCESS;

// if (offset == 0)
// return deviceConnection.bulkTransfer(outEndpoint, buffer, length, TRANSFER_TIMEOUT);
//
Expand All @@ -69,23 +106,56 @@ public int bulkOutTransfer(byte[] buffer, int offset, int length) {
// return deviceConnection.bulkTransfer(outEndpoint, tmpBuffer, length,
// TRANSFER_TIMEOUT);

return 0;

}

@Override
public int bulkInTransfer(byte[] buffer, int length) {
public int bulkInTransfer(byte[] data, int length) throws LibAumsException {

LibUsb.bulkTransfer(deviceHandle, )
log.info("BulkInTransfer (data={},length={})", data, length);

// TODO implement Usb4JavaUsbDeviceCommunication
// return deviceConnection.bulkTransfer(inEndpoint, buffer, length, TRANSFER_TIMEOUT);
// TODO bulkInTransfer, Libusb does things different than android

ByteBuffer buffer = BufferUtils.allocateByteBuffer(length).order(
ByteOrder.LITTLE_ENDIAN);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(deviceHandle, deviceConfig.getInEndpointAddress(), buffer,
transferred, TRANSFER_TIMEOUT);
if (result != LibUsb.SUCCESS) {
throw LibAumsException.createWithLibUsbException("Unable to read data", result);
}
log.info(transferred.get() + " bytes read from device");
System.arraycopy(buffer.array(), 0, data, 0, length);

return 0;
// return LibUsb.bulkTransfer(
// deviceHandle, deviceConfig.getInEndpointAddress(), buffer, length, TRANSFER_TIMEOUT);


return LibUsb.SUCCESS;
}

@Override
public int bulkInTransfer(byte[] buffer, int offset, int length) {
// TODO implement Usb4JavaUsbDeviceCommunication
public int bulkInTransfer(byte[] data, int offset, int length) throws LibAumsException{
// TODO implement Usb4JavaUsbDeviceCommunication not Sure if this will work ok

if (offset==0) {
return bulkInTransfer(data, length);
}

log.info("BulkInTransfer (data={},length={},offset={})", data, length, offset);

ByteBuffer buffer = BufferUtils.allocateByteBuffer(length).order(
ByteOrder.LITTLE_ENDIAN);
IntBuffer transferred = BufferUtils.allocateIntBuffer();
int result = LibUsb.bulkTransfer(deviceHandle, deviceConfig.getInEndpointAddress(), buffer,
transferred, TRANSFER_TIMEOUT);
if (result != LibUsb.SUCCESS) {
throw LibAumsException.createWithLibUsbException("Unable to read data", result);
}
log.info(transferred.get() + " bytes read from device");
System.arraycopy(buffer.array(), 0, data, offset, length);

return LibUsb.SUCCESS;

// if (offset == 0)
// return deviceConnection.bulkTransfer(inEndpoint, buffer, length, TRANSFER_TIMEOUT);
Expand All @@ -95,10 +165,6 @@ public int bulkInTransfer(byte[] buffer, int offset, int length) {
// TRANSFER_TIMEOUT);
// System.arraycopy(tmpBuffer, 0, buffer, offset, length);
// return result;

// read()

return 0;
}


Expand All @@ -110,7 +176,7 @@ public int bulkInTransfer(byte[] buffer, int offset, int length) {
* @param data
* The data to send to the device.
*/
public static void write(DeviceHandle handle, byte[] data)
public static void write(DeviceHandle handle, byte[] data) throws LibAumsException
{
// ByteBuffer buffer = BufferUtils.allocateByteBuffer(data.length);
// buffer.put(data);
Expand All @@ -133,7 +199,7 @@ public static void write(DeviceHandle handle, byte[] data)
* The number of bytes to read from the device.
* @return The read data.
*/
public static ByteBuffer read(DeviceHandle handle, int size)
public static ByteBuffer read(DeviceHandle handle, int size) throws LibAumsException
{
// ByteBuffer buffer = BufferUtils.allocateByteBuffer(size).order(
// ByteOrder.LITTLE_ENDIAN);
Expand All @@ -153,7 +219,7 @@ public static ByteBuffer read(DeviceHandle handle, int size)


public void closeDevice() throws LibAumsException {
// Release the ADB interface
// Release interface
int result = LibUsb.releaseInterface(deviceHandle, deviceConfig.getInterfaceNumber());
if (result != LibUsb.SUCCESS) {
throw LibAumsException.createWithLibUsbException("Unable to release interface", result);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

package com.github.mjdev.libaums;

import com.atech.library.usb.libaums.data.LibAumsException;

/**
* This Interface describes a low level device to perform USB transfers. At the
* moment only bulk IN and OUT transfer are supported. Every class that follows
Expand All @@ -41,7 +43,7 @@ public interface UsbCommunication {
* @return Bytes transmitted if successful, or -1.
* @see #bulkInTransfer(byte[], int, int)
*/
public int bulkOutTransfer(byte[] buffer, int length);
public int bulkOutTransfer(byte[] buffer, int length) throws LibAumsException;

/**
* Performs a bulk out transfer beginning at the given offset in the
Expand All @@ -57,7 +59,7 @@ public interface UsbCommunication {
* @return Bytes transmitted if successful, or -1.
* @see #bulkInTransfer(byte[], int)
*/
public int bulkOutTransfer(byte[] buffer, int offset, int length);
public int bulkOutTransfer(byte[] buffer, int offset, int length) throws LibAumsException;

/**
* Performs a bulk in transfer beginning at offset zero in the
Expand All @@ -73,7 +75,7 @@ public interface UsbCommunication {
* @return Bytes read if successful, or -1.
* @see #bulkInTransfer(byte[], int, int)
*/
public int bulkInTransfer(byte[] buffer, int length);
public int bulkInTransfer(byte[] buffer, int length) throws LibAumsException;

/**
* Performs a bulk in transfer beginning at the given offset in the
Expand All @@ -89,5 +91,5 @@ public interface UsbCommunication {
* @return Bytes read if successful, or -1.
* @see #bulkInTransfer(byte[], int)
*/
public int bulkInTransfer(byte[] buffer, int offset, int length);
public int bulkInTransfer(byte[] buffer, int offset, int length) throws LibAumsException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -267,17 +267,14 @@ public static List<UsbMassStorageDeviceConfig> getListOfAttachedUsbMassStorageDe
continue;
}

// TODO UsbMassStorageDeviceConfig filling - missing endpoints
outList.add(UsbMassStorageDeviceConfig.builder()
.vendorId(device.getUsbDeviceDescriptor().idVendor())
.productId(device.getUsbDeviceDescriptor().idProduct())
.interfaceNumber(usbInterface.bInterfaceNumber())
.inEndpointAddress(inEndpoint.bEndpointAddress())
.outEndpointAddress(outEndpoint.bEndpointAddress())
.build());

}

}

return outList;
Expand Down
Loading

0 comments on commit a0518b7

Please sign in to comment.