From 86087547956c7771975c8bf9556d7a2650310d7e Mon Sep 17 00:00:00 2001 From: "andy.rozman" Date: Fri, 12 Apr 2024 22:44:48 +0100 Subject: [PATCH] - removed some classes --- .../usb/libaums/UsbDeviceCommunication.java | 124 ------------------ .../library/usb/libaums/UsbManagement.java | 80 ----------- .../usb/libaums/usb4java/Usb4JavaManager.java | 54 ++++---- 3 files changed, 24 insertions(+), 234 deletions(-) delete mode 100644 libaums/src/main/java/com/atech/library/usb/libaums/UsbDeviceCommunication.java delete mode 100644 libaums/src/main/java/com/atech/library/usb/libaums/UsbManagement.java diff --git a/libaums/src/main/java/com/atech/library/usb/libaums/UsbDeviceCommunication.java b/libaums/src/main/java/com/atech/library/usb/libaums/UsbDeviceCommunication.java deleted file mode 100644 index 58a6fd5..0000000 --- a/libaums/src/main/java/com/atech/library/usb/libaums/UsbDeviceCommunication.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.atech.library.usb.libaums; - -import com.atech.library.usb.libaums.data.UsbMassStorageDeviceConfig; -import com.github.mjdev.libaums.UsbCommunication; -import org.usb4java.BufferUtils; -import org.usb4java.DeviceHandle; -import org.usb4java.LibUsb; -import org.usb4java.LibUsbException; - -import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.IntBuffer; - -/** - * Created by andy on 10.04.2024. - */ -public class UsbDeviceCommunication implements UsbCommunication { - - private final UsbMassStorageDeviceConfig deviceSettings; - DeviceHandle handle; - private static int TIMEOUT = 5000; - - public UsbDeviceCommunication(UsbMassStorageDeviceConfig deviceSettings) { - this.deviceSettings = deviceSettings; - initDeviceWithLibUsb(); - } - - private void initDeviceWithLibUsb() { - // Initialize the libusb context - int result = LibUsb.init(null); - if (result != LibUsb.SUCCESS) - { - throw new LibUsbException("Unable to initialize libusb", result); - } - - // Open device - DeviceHandle handle = LibUsb.openDeviceWithVidPid(null, deviceSettings.getVendorId(), - deviceSettings.getProductId()); - if (handle == null) - { - System.err.println("Test device not found."); - System.exit(1); - } - - // Claim interface - result = LibUsb.claimInterface(handle, deviceSettings.getInterfaceNumber()); - if (result != LibUsb.SUCCESS) - { - throw new LibUsbException("Unable to claim interface", result); - } - } - - - @Override - public int bulkOutTransfer(byte[] data, int length) { - ByteBuffer buffer = BufferUtils.allocateByteBuffer(length); - buffer.put(data, 0, length); - //put(byte[] src, int offset, int length) - IntBuffer transferred = BufferUtils.allocateIntBuffer(); - int result = LibUsb.bulkTransfer(handle, deviceSettings.getOutEndpointAddress(), buffer, - transferred, TIMEOUT); - if (result != LibUsb.SUCCESS) - { - throw new LibUsbException("Unable to send data", result); - } - System.out.println(transferred.get() + " bytes sent to device"); - - - return result; - } - - @Override - public int bulkOutTransfer(byte[] buffer, int offset, int length) { - int newSize = buffer.length-offset; - byte[] outBuffer = new byte[newSize]; - - System.arraycopy(buffer, offset, outBuffer, 0, newSize); - - return bulkOutTransfer(outBuffer, length); - } - - @Override - 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.getInEndpointAddress(), buffer, - transferred, TIMEOUT); - if (result != LibUsb.SUCCESS) - { - throw new LibUsbException("Unable to read data", result); - } - System.out.println(transferred.get() + " bytes read from device"); - data = buffer.array(); - - return result; - } - - @Override - public int bulkInTransfer(byte[] buffer, int offset, int length) { - int newSize = buffer.length-offset; - byte[] outBuffer = new byte[newSize]; - - System.arraycopy(buffer, offset, outBuffer, 0, newSize); - - return bulkInTransfer(outBuffer, length); - } - - public void closeDevice() { - // Release the ADB interface - int result = LibUsb.releaseInterface(handle, deviceSettings.getInterfaceNumber()); - if (result != LibUsb.SUCCESS) - { - throw new LibUsbException("Unable to release interface", result); - } - - // Close the device - LibUsb.close(handle); - -// // Deinitialize the libusb context -// LibUsb.exit(null); - - } -} diff --git a/libaums/src/main/java/com/atech/library/usb/libaums/UsbManagement.java b/libaums/src/main/java/com/atech/library/usb/libaums/UsbManagement.java deleted file mode 100644 index ad1f7ab..0000000 --- a/libaums/src/main/java/com/atech/library/usb/libaums/UsbManagement.java +++ /dev/null @@ -1,80 +0,0 @@ -package com.atech.library.usb.libaums; - -import com.atech.library.usb.libaums.data.UsbMassStorageDeviceConfig; -import com.atech.library.usb.libaums.usb.device.ATUsbDevice; -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.*; - -import java.util.*; - -/** - * Created by andy on 10.04.2024. - */ -@Slf4j -public class UsbManagement { - - - - - - public void getUsbDeviceDetails(UsbMassStorageDeviceConfig usbDeviceSettings) { - - // Initialize the libusb context - int result = LibUsb.init(null); - if (result != LibUsb.SUCCESS) - { - throw new LibUsbException("Unable to initialize libusb", result); - } - - // Open test device (Samsung Galaxy Nexus) - DeviceHandle handle = LibUsb.openDeviceWithVidPid(null, usbDeviceSettings.getVendorId(), - usbDeviceSettings.getProductId()); - if (handle == null) - { - System.err.println("Test device not found."); - System.exit(1); - } - - Device device = LibUsb.getDevice(handle); - - // TODO discover device - - - // Close the device - LibUsb.close(handle); - - // Deinitialize the libusb context - LibUsb.exit(null); - - } - - public void openDevice(UsbMassStorageDeviceConfig usbDeviceSettings) { - - } - - - @SneakyThrows - public static void main(String[] args) { - - //Usb4JavaManager usbManagement = new Usb4JavaManager(); - - List fullDeviceList = Usb4JavaManager.getDeviceList(); - - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - - //System.out.println(gson.toJson(fullDeviceList)); - - System.out.println("test output: =================================================="); - - ATUsbDevice atUsbDevice = fullDeviceList.get(0); - - System.out.println(atUsbDevice.toLsUsbString()); - - } - - -} 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 ab6cdb2..bac25ca 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 @@ -19,6 +19,7 @@ import com.atech.library.usb.libaums.UsbMassStorageLibrary; import com.atech.library.usb.libaums.data.LibAumsException; +import com.atech.library.usb.libaums.data.UsbMassStorageDeviceConfig; import com.atech.library.usb.libaums.usb.device.ATUsbConfigurationDescriptor; import com.atech.library.usb.libaums.usb.device.ATUsbDevice; import com.atech.library.usb.libaums.usb.device.ATUsbDeviceDescriptor; @@ -94,6 +95,7 @@ public static List getFullDeviceList(int...excludeClass) throws Lib return outList; } + /** * Get Device * @param device Device instance @@ -169,18 +171,17 @@ public static ATUsbDevice getDevice(Device device, boolean details, DeviceHandle return usbDevice; } - public static List getConfigurationDescriptors(final Device device, + private static List getConfigurationDescriptors(final Device device, final int numConfigurations, - DeviceHandle handle) - { + DeviceHandle handle) { List list = new ArrayList<>(); - for (byte i = 0; i < numConfigurations; i += 1) - { + + for (byte i = 0; i < numConfigurations; i += 1) { final ConfigDescriptor descriptor = new ConfigDescriptor(); final int result = LibUsb.getConfigDescriptor(device, i, descriptor); + if (result < 0) { - throw new LibUsbException("Unable to read config descriptor", - result); + throw new LibUsbException("Unable to read config descriptor", result); } try @@ -201,33 +202,26 @@ public static List getConfigurationDescriptors(fin return list; } + public void getDeviceDetails(UsbMassStorageDeviceConfig usbDeviceSettings) throws LibAumsException { + + UsbMassStorageLibrary.initLibrary(); + + // open device + DeviceHandle handle = LibUsb.openDeviceWithVidPid(null, usbDeviceSettings.getVendorId(), + usbDeviceSettings.getProductId()); + if (handle == null) { + log.error("Device {} not found.", usbDeviceSettings.getReadableDeviceId()); + throw LibAumsException.createWithLibUsbException("Device " + usbDeviceSettings.getReadableDeviceId() + " not found.", -1); + } + Device device = LibUsb.getDevice(handle); + getDevice(device, true, handle, null); -// static UsbServices services; -// -// public static List getUsbDevices() throws UsbException { -// if (services==null) { -// services = UsbHostManager.getUsbServices(); -// } -// -// return discoverDevices(services.getRootUsbHub()); -// } -// -// public static List discoverDevices(UsbHub usbHub) { -// List deviceList = new ArrayList<>(); -// for (Object attachedUsbDevice : usbHub.getAttachedUsbDevices()) { -// UsbDevice device = (UsbDevice) attachedUsbDevice; -// -// if (device.isUsbHub()) { -// deviceList.addAll(discoverDevices((UsbHub)device)); -// } else { -// deviceList.add(device); -// } -// } -// return deviceList; -// } + // Close the device + LibUsb.close(handle); + } }