From ae14ce894d872c7355f9350dd3db77aff4458e8c Mon Sep 17 00:00:00 2001
From: Manuel Bl
Date: Fri, 10 Nov 2023 15:49:59 +0100
Subject: [PATCH] Kotlin-friendly API
---
examples/bulk_transfer/pom.xml | 2 +-
.../codecrete/usb/examples/BulkTransfer.java | 2 +-
examples/enumerate/pom.xml | 2 +-
.../net/codecrete/usb/examples/Enumerate.java | 74 +++---
examples/enumerate_kotlin/.gitignore | 38 +++
examples/enumerate_kotlin/pom.xml | 66 +++++
.../net/codecrete/usb/examples/Enumerate.kt | 95 +++++++
.../codecrete/usb/examples/UsbClassInfo.kt | 234 ++++++++++++++++++
examples/epaper_display/pom.xml | 2 +-
.../codecrete/usb/examples/IT8951Driver.java | 10 +-
examples/monitor/pom.xml | 2 +-
.../net/codecrete/usb/examples/Monitor.java | 12 +-
examples/stm_dfu/pom.xml | 2 +-
.../java/net/codecrete/usb/dfu/DFUDevice.java | 49 ++--
.../java/net/codecrete/usb/dfu/STM32.java | 3 +
.../java/net/codecrete/usb/dfu/Segment.java | 19 +-
java-does-usb/pom.xml | 8 +-
java-does-usb/src/main/java/module-info.java | 1 +
.../net/codecrete/usb/{USB.java => Usb.java} | 49 ++--
...erface.java => UsbAlternateInterface.java} | 23 +-
...lTransfer.java => UsbControlTransfer.java} | 6 +-
.../usb/{USBDevice.java => UsbDevice.java} | 82 +++---
...Predicate.java => UsbDevicePredicate.java} | 10 +-
.../{USBDirection.java => UsbDirection.java} | 2 +-
.../{USBEndpoint.java => UsbEndpoint.java} | 12 +-
.../{USBException.java => UsbException.java} | 13 +-
.../{USBInterface.java => UsbInterface.java} | 19 +-
.../{USBRecipient.java => UsbRecipient.java} | 2 +-
...SBRequestType.java => UsbRequestType.java} | 2 +-
...lException.java => UsbStallException.java} | 8 +-
...xception.java => UsbTimeoutException.java} | 6 +-
...TransferType.java => UsbTransferType.java} | 2 +-
.../main/java/net/codecrete/usb/Version.java | 8 +-
.../codecrete/usb/common/Configuration.java | 12 +-
.../usb/common/ConfigurationParser.java | 50 ++--
.../usb/common/EndpointInputStream.java | 17 +-
.../usb/common/EndpointOutputStream.java | 11 +-
.../usb/common/USBAlternateInterfaceImpl.java | 68 -----
.../usb/common/USBInterfaceImpl.java | 66 -----
.../usb/common/UsbAlternateInterfaceImpl.java | 74 ++++++
...{USBDeviceImpl.java => UsbDeviceImpl.java} | 129 +++++-----
...ceRegistry.java => UsbDeviceRegistry.java} | 45 ++--
...EndpointImpl.java => UsbEndpointImpl.java} | 24 +-
.../usb/common/UsbInterfaceImpl.java | 75 ++++++
.../codecrete/usb/linux/LinuxAsyncTask.java | 18 +-
.../usb/linux/LinuxEndpointInputStream.java | 6 +-
.../usb/linux/LinuxEndpointOutputStream.java | 6 +-
...inuxUSBDevice.java => LinuxUsbDevice.java} | 112 ++++-----
...istry.java => LinuxUsbDeviceRegistry.java} | 20 +-
...BException.java => LinuxUsbException.java} | 14 +-
.../linux/{USBDevFS.java => UsbDevFS.java} | 4 +-
.../net/codecrete/usb/macos/IoKitHelper.java | 4 +-
.../macos/{IoKitUSB.java => IoKitUsb.java} | 4 +-
.../codecrete/usb/macos/MacosAsyncTask.java | 4 +-
.../usb/macos/MacosEndpointInputStream.java | 4 +-
.../usb/macos/MacosEndpointOutputStream.java | 4 +-
...acosUSBDevice.java => MacosUsbDevice.java} | 195 +++++++--------
...istry.java => MacosUsbDeviceRegistry.java} | 28 +--
...BException.java => MacosUsbException.java} | 18 +-
.../codecrete/usb/windows/DeviceInfoSet.java | 4 +-
.../{USBConstants.java => UsbConstants.java} | 4 +-
.../usb/windows/WindowsAsyncTask.java | 2 +-
.../windows/WindowsEndpointInputStream.java | 8 +-
.../windows/WindowsEndpointOutputStream.java | 8 +-
...wsUSBDevice.java => WindowsUsbDevice.java} | 92 ++++---
...try.java => WindowsUsbDeviceRegistry.java} | 42 ++--
...xception.java => WindowsUsbException.java} | 14 +-
.../codecrete/usb/AlternateInterfaceTest.java | 14 +-
.../net/codecrete/usb/BulkTransferTest.java | 4 +-
.../usb/ConfigurationParserTest.java | 178 ++++++-------
.../codecrete/usb/ControlTransferTest.java | 18 +-
.../net/codecrete/usb/DescriptionTest.java | 163 ++++++------
.../net/codecrete/usb/DescriptorTest.java | 4 +-
.../codecrete/usb/DeviceEnumerationTest.java | 20 +-
.../codecrete/usb/DeviceLifecycleTest.java | 36 +--
.../codecrete/usb/InvalidOperationTest.java | 20 +-
.../java/net/codecrete/usb/SpeedTest.java | 2 +-
.../java/net/codecrete/usb/StallTest.java | 22 +-
.../net/codecrete/usb/TestDeviceBase.java | 22 +-
.../java/net/codecrete/usb/TimeoutTest.java | 10 +-
.../usb/special/EnumerateDevices.java | 4 +-
.../codecrete/usb/special/LogicAnalyzer.java | 32 +--
.../codecrete/usb/special/MonitorDevices.java | 14 +-
.../codecrete/usb/special/USBSerialTest.java | 26 +-
.../net/codecrete/usb/special/Unplug.java | 28 +--
85 files changed, 1588 insertions(+), 1080 deletions(-)
create mode 100644 examples/enumerate_kotlin/.gitignore
create mode 100644 examples/enumerate_kotlin/pom.xml
create mode 100644 examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/Enumerate.kt
create mode 100644 examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/UsbClassInfo.kt
rename java-does-usb/src/main/java/net/codecrete/usb/{USB.java => Usb.java} (64%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBAlternateInterface.java => UsbAlternateInterface.java} (74%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBControlTransfer.java => UsbControlTransfer.java} (90%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBDevice.java => UsbDevice.java} (86%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBDevicePredicate.java => UsbDevicePredicate.java} (78%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBDirection.java => UsbDirection.java} (92%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBEndpoint.java => UsbEndpoint.java} (87%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBException.java => UsbException.java} (74%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBInterface.java => UsbInterface.java} (66%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBRecipient.java => UsbRecipient.java} (93%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBRequestType.java => UsbRequestType.java} (93%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBStallException.java => UsbStallException.java} (73%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBTimeoutException.java => UsbTimeoutException.java} (69%)
rename java-does-usb/src/main/java/net/codecrete/usb/{USBTransferType.java => UsbTransferType.java} (93%)
delete mode 100644 java-does-usb/src/main/java/net/codecrete/usb/common/USBAlternateInterfaceImpl.java
delete mode 100644 java-does-usb/src/main/java/net/codecrete/usb/common/USBInterfaceImpl.java
create mode 100644 java-does-usb/src/main/java/net/codecrete/usb/common/UsbAlternateInterfaceImpl.java
rename java-does-usb/src/main/java/net/codecrete/usb/common/{USBDeviceImpl.java => UsbDeviceImpl.java} (74%)
rename java-does-usb/src/main/java/net/codecrete/usb/common/{USBDeviceRegistry.java => UsbDeviceRegistry.java} (83%)
rename java-does-usb/src/main/java/net/codecrete/usb/common/{USBEndpointImpl.java => UsbEndpointImpl.java} (51%)
create mode 100644 java-does-usb/src/main/java/net/codecrete/usb/common/UsbInterfaceImpl.java
rename java-does-usb/src/main/java/net/codecrete/usb/linux/{LinuxUSBDevice.java => LinuxUsbDevice.java} (73%)
rename java-does-usb/src/main/java/net/codecrete/usb/linux/{LinuxUSBDeviceRegistry.java => LinuxUsbDeviceRegistry.java} (93%)
rename java-does-usb/src/main/java/net/codecrete/usb/linux/{LinuxUSBException.java => LinuxUsbException.java} (85%)
rename java-does-usb/src/main/java/net/codecrete/usb/linux/{USBDevFS.java => UsbDevFS.java} (95%)
rename java-does-usb/src/main/java/net/codecrete/usb/macos/{IoKitUSB.java => IoKitUsb.java} (99%)
rename java-does-usb/src/main/java/net/codecrete/usb/macos/{MacosUSBDevice.java => MacosUsbDevice.java} (78%)
rename java-does-usb/src/main/java/net/codecrete/usb/macos/{MacosUSBDeviceRegistry.java => MacosUsbDeviceRegistry.java} (93%)
rename java-does-usb/src/main/java/net/codecrete/usb/macos/{MacosUSBException.java => MacosUsbException.java} (79%)
rename java-does-usb/src/main/java/net/codecrete/usb/windows/{USBConstants.java => UsbConstants.java} (95%)
rename java-does-usb/src/main/java/net/codecrete/usb/windows/{WindowsUSBDevice.java => WindowsUsbDevice.java} (86%)
rename java-does-usb/src/main/java/net/codecrete/usb/windows/{WindowsUSBDeviceRegistry.java => WindowsUsbDeviceRegistry.java} (92%)
rename java-does-usb/src/main/java/net/codecrete/usb/windows/{WindowsUSBException.java => WindowsUsbException.java} (91%)
diff --git a/examples/bulk_transfer/pom.xml b/examples/bulk_transfer/pom.xml
index 7d7974b..8e34bba 100644
--- a/examples/bulk_transfer/pom.xml
+++ b/examples/bulk_transfer/pom.xml
@@ -21,7 +21,7 @@
net.codecrete.usb
java-does-usb
- 0.6.1
+ 0.7.0-SNAPSHOT
diff --git a/examples/bulk_transfer/src/main/java/net/codecrete/usb/examples/BulkTransfer.java b/examples/bulk_transfer/src/main/java/net/codecrete/usb/examples/BulkTransfer.java
index bfef613..4e738ef 100644
--- a/examples/bulk_transfer/src/main/java/net/codecrete/usb/examples/BulkTransfer.java
+++ b/examples/bulk_transfer/src/main/java/net/codecrete/usb/examples/BulkTransfer.java
@@ -27,7 +27,7 @@ public class BulkTransfer {
private static final int ENDPOINT_IN = 2;
public static void main(String[] args) {
- var optionalDevice = USB.getDevice(VID, PID);
+ var optionalDevice = Usb.findDevice(VID, PID);
if (optionalDevice.isEmpty()) {
System.out.printf("No USB device with VID=0x%04x and PID=0x%04x found.%n", VID, PID);
return;
diff --git a/examples/enumerate/pom.xml b/examples/enumerate/pom.xml
index f2ac247..7e3b68e 100644
--- a/examples/enumerate/pom.xml
+++ b/examples/enumerate/pom.xml
@@ -21,7 +21,7 @@
net.codecrete.usb
java-does-usb
- 0.6.1
+ 0.7.0-SNAPSHOT
org.tinylog
diff --git a/examples/enumerate/src/main/java/net/codecrete/usb/examples/Enumerate.java b/examples/enumerate/src/main/java/net/codecrete/usb/examples/Enumerate.java
index b575db8..4e387b8 100644
--- a/examples/enumerate/src/main/java/net/codecrete/usb/examples/Enumerate.java
+++ b/examples/enumerate/src/main/java/net/codecrete/usb/examples/Enumerate.java
@@ -19,32 +19,32 @@ public class Enumerate {
public static void main(String[] args) {
// display the already present USB devices
- for (var device : USB.getAllDevices())
+ for (var device : Usb.getDevices())
printDevice(device);
}
- private static void printDevice(USBDevice device) {
+ private static void printDevice(UsbDevice device) {
System.out.println("Device:");
- System.out.printf(" VID: 0x%04x%n", device.vendorId());
- System.out.printf(" PID: 0x%04x%n", device.productId());
- if (device.manufacturer() != null)
- System.out.printf(" Manufacturer: %s%n", device.manufacturer());
- if (device.product() != null)
- System.out.printf(" Product name: %s%n", device.product());
- if (device.serialNumber() != null)
- System.out.printf(" Serial number: %s%n", device.serialNumber());
- System.out.printf(" Device class: 0x%02x", device.classCode());
- printInParens(USBClassInfo.lookupClass(device.classCode()));
- System.out.printf(" Device subclass: 0x%02x", device.subclassCode());
- printInParens(USBClassInfo.lookupSubclass(device.classCode(), device.subclassCode()));
- System.out.printf(" Device protocol: 0x%02x", device.protocolCode());
- printInParens(USBClassInfo.lookupProtocol(device.classCode(), device.subclassCode(), device.protocolCode()));
-
- for (var intf: device.interfaces())
+ System.out.printf(" VID: 0x%04x%n", device.getVendorId());
+ System.out.printf(" PID: 0x%04x%n", device.getProductId());
+ if (device.getManufacturer() != null)
+ System.out.printf(" Manufacturer: %s%n", device.getManufacturer());
+ if (device.getProduct() != null)
+ System.out.printf(" Product name: %s%n", device.getProduct());
+ if (device.getSerialNumber() != null)
+ System.out.printf(" Serial number: %s%n", device.getSerialNumber());
+ System.out.printf(" Device class: 0x%02x", device.getClassCode());
+ printInParens(USBClassInfo.lookupClass(device.getClassCode()));
+ System.out.printf(" Device subclass: 0x%02x", device.getSubclassCode());
+ printInParens(USBClassInfo.lookupSubclass(device.getClassCode(), device.getSubclassCode()));
+ System.out.printf(" Device protocol: 0x%02x", device.getProtocolCode());
+ printInParens(USBClassInfo.lookupProtocol(device.getClassCode(), device.getSubclassCode(), device.getProtocolCode()));
+
+ for (var intf: device.getInterfaces())
printInterface(intf);
- printRawDescriptor("Device descriptor", device.deviceDescriptor());
- printRawDescriptor("Configuration descriptor", device.configurationDescriptor());
+ printRawDescriptor("Device descriptor", device.getDeviceDescriptor());
+ printRawDescriptor("Configuration descriptor", device.getConfigurationDescriptor());
System.out.println();
System.out.println();
@@ -59,36 +59,36 @@ private static void printInParens(Optional text) {
}
}
- private static void printInterface(USBInterface intf) {
- for (var alt : intf.alternates())
- printAlternate(alt, intf.number(), alt == intf.alternate());
+ private static void printInterface(UsbInterface intf) {
+ for (var alt : intf.getAlternates())
+ printAlternate(alt, intf.getNumber(), alt == intf.getCurrentAlternate());
}
- private static void printAlternate(USBAlternateInterface alt, int intferaceNumber, boolean isDefault) {
+ private static void printAlternate(UsbAlternateInterface alt, int intferaceNumber, boolean isDefault) {
System.out.println();
if (isDefault) {
System.out.printf(" Interface %d%n", intferaceNumber);
} else {
- System.out.printf(" Interface %d (alternate %d)%n", intferaceNumber, alt.number());
+ System.out.printf(" Interface %d (alternate %d)%n", intferaceNumber, alt.getNumber());
}
- System.out.printf(" Interface class: 0x%02x", alt.classCode());
- printInParens(USBClassInfo.lookupClass(alt.classCode()));
- System.out.printf(" Interface subclass: 0x%02x", alt.subclassCode());
- printInParens(USBClassInfo.lookupProtocol(alt.classCode(), alt.subclassCode(), alt.protocolCode()));
- System.out.printf(" Interface protocol: 0x%02x", alt.protocolCode());
- printInParens(USBClassInfo.lookupProtocol(alt.classCode(), alt.subclassCode(), alt.protocolCode()));
+ System.out.printf(" Interface class: 0x%02x", alt.getClassCode());
+ printInParens(USBClassInfo.lookupClass(alt.getClassCode()));
+ System.out.printf(" Interface subclass: 0x%02x", alt.getSubclassCode());
+ printInParens(USBClassInfo.lookupProtocol(alt.getClassCode(), alt.getSubclassCode(), alt.getProtocolCode()));
+ System.out.printf(" Interface protocol: 0x%02x", alt.getProtocolCode());
+ printInParens(USBClassInfo.lookupProtocol(alt.getClassCode(), alt.getSubclassCode(), alt.getProtocolCode()));
- for (var endpoint : alt.endpoints())
+ for (var endpoint : alt.getEndpoints())
printEndpoint(endpoint);
}
- private static void printEndpoint(USBEndpoint endpoint) {
+ private static void printEndpoint(UsbEndpoint endpoint) {
System.out.println();
- System.out.printf(" Endpoint %d%n", endpoint.number());
- System.out.printf(" Direction: %s%n", endpoint.direction().name());
- System.out.printf(" Transfer type: %s%n", endpoint.transferType().name());
- System.out.printf(" Packet size: %d bytes%n", endpoint.packetSize());
+ System.out.printf(" Endpoint %d%n", endpoint.getNumber());
+ System.out.printf(" Direction: %s%n", endpoint.getDirection().name());
+ System.out.printf(" Transfer type: %s%n", endpoint.getTransferType().name());
+ System.out.printf(" Packet size: %d bytes%n", endpoint.getPacketSize());
}
private static void printRawDescriptor(String title, byte[] descriptor) {
diff --git a/examples/enumerate_kotlin/.gitignore b/examples/enumerate_kotlin/.gitignore
new file mode 100644
index 0000000..5ff6309
--- /dev/null
+++ b/examples/enumerate_kotlin/.gitignore
@@ -0,0 +1,38 @@
+target/
+!.mvn/wrapper/maven-wrapper.jar
+!**/src/main/**/target/
+!**/src/test/**/target/
+
+### IntelliJ IDEA ###
+.idea/modules.xml
+.idea/jarRepositories.xml
+.idea/compiler.xml
+.idea/libraries/
+*.iws
+*.iml
+*.ipr
+
+### Eclipse ###
+.apt_generated
+.classpath
+.factorypath
+.project
+.settings
+.springBeans
+.sts4-cache
+
+### NetBeans ###
+/nbproject/private/
+/nbbuild/
+/dist/
+/nbdist/
+/.nb-gradle/
+build/
+!**/src/main/**/build/
+!**/src/test/**/build/
+
+### VS Code ###
+.vscode/
+
+### Mac OS ###
+.DS_Store
\ No newline at end of file
diff --git a/examples/enumerate_kotlin/pom.xml b/examples/enumerate_kotlin/pom.xml
new file mode 100644
index 0000000..5110c7d
--- /dev/null
+++ b/examples/enumerate_kotlin/pom.xml
@@ -0,0 +1,66 @@
+
+ 4.0.0
+
+ net.codecrete.usb.examples
+ kotlin
+ 0.6.1-SNAPSHOT
+ jar
+
+ kotlin
+ https://github.com/manuelbl/JavaDoesUSB/examples/kotlin
+
+
+ 1.9.10
+ true
+ UTF-8
+
+
+
+ ${project.basedir}/src/main/kotlin
+ ${project.basedir}/src/test/kotlin
+
+
+
+ org.jetbrains.kotlin
+ kotlin-maven-plugin
+ ${kotlin.version}
+ true
+
+
+
+
+
+
+ org.jetbrains.kotlin
+ kotlin-stdlib
+ ${kotlin.version}
+
+
+ net.codecrete.usb
+ java-does-usb
+ 0.7.0-SNAPSHOT
+
+
+ org.tinylog
+ tinylog-api
+ 2.6.2
+
+
+ org.tinylog
+ tinylog-impl
+ 2.6.2
+
+
+ org.tinylog
+ jsl-tinylog
+ 2.6.2
+
+
+ junit
+ junit
+ 4.13.2
+ test
+
+
+
diff --git a/examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/Enumerate.kt b/examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/Enumerate.kt
new file mode 100644
index 0000000..cf74172
--- /dev/null
+++ b/examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/Enumerate.kt
@@ -0,0 +1,95 @@
+package net.codecrete.usb.examples
+
+import net.codecrete.usb.*
+import kotlin.math.min
+
+
+fun main() {
+ Enumerate().enumerate()
+}
+
+class Enumerate {
+
+ private var classInfo = UsbClassInfo()
+
+ fun enumerate() {
+ for (device in Usb.getDevices()) {
+ printDevice(device)
+ }
+ }
+
+ private fun printDevice(device: UsbDevice) {
+ println("Device:")
+ System.out.printf(" VID: 0x%04x%n", device.vendorId)
+ System.out.printf(" PID: 0x%04x%n", device.productId)
+ if (device.manufacturer != null) System.out.printf(" Manufacturer: %s%n", device.manufacturer)
+ if (device.product != null) System.out.printf(" Product name: %s%n", device.product)
+ if (device.serialNumber != null) System.out.printf(" Serial number: %s%n", device.serialNumber)
+ System.out.printf(" Device class: 0x%02x", device.classCode)
+ printInParens(classInfo.lookupClass(device.classCode))
+ System.out.printf(" Device subclass: 0x%02x", device.subclassCode)
+ printInParens(classInfo.lookupSubclass(device.classCode, device.subclassCode))
+ System.out.printf(" Device protocol: 0x%02x", device.protocolCode)
+ printInParens(classInfo.lookupProtocol(device.classCode, device.subclassCode, device.protocolCode))
+ for (intf in device.interfaces) printInterface(intf)
+ printRawDescriptor("Device descriptor", device.deviceDescriptor)
+ printRawDescriptor("Configuration descriptor", device.configurationDescriptor)
+ println()
+ println()
+ }
+
+ private fun printInParens(text: String?) {
+ if (text != null) {
+ System.out.printf(" (%s)%n", text)
+ } else {
+ println()
+ }
+ }
+
+ private fun printInterface(intf: UsbInterface) {
+ for (alt in intf.alternates) printAlternate(alt, intf.number, alt === intf.currentAlternate)
+ }
+
+ private fun printAlternate(alt: UsbAlternateInterface, intferfaceNumber: Int, isDefault: Boolean) {
+ println()
+ if (isDefault) {
+ System.out.printf(" Interface %d%n", intferfaceNumber)
+ } else {
+ System.out.printf(" Interface %d (alternate %d)%n", intferfaceNumber, alt.number)
+ }
+ System.out.printf(" Interface class: 0x%02x", alt.classCode)
+ printInParens(classInfo.lookupClass(alt.classCode))
+ System.out.printf(" Interface subclass: 0x%02x", alt.subclassCode)
+ printInParens(classInfo.lookupProtocol(alt.classCode, alt.subclassCode, alt.protocolCode))
+ System.out.printf(" Interface protocol: 0x%02x", alt.protocolCode)
+ printInParens(classInfo.lookupProtocol(alt.classCode, alt.subclassCode, alt.protocolCode))
+ for (endpoint in alt.endpoints)
+ printEndpoint(endpoint)
+ }
+
+ private fun printEndpoint(endpoint: UsbEndpoint) {
+ println()
+ System.out.printf(" Endpoint %d%n", endpoint.number)
+ System.out.printf(" Direction: %s%n", endpoint.direction.name)
+ System.out.printf(" Transfer type: %s%n", endpoint.transferType.name)
+ System.out.printf(" Packet size: %d bytes%n", endpoint.packetSize)
+ }
+
+ private fun printRawDescriptor(title: String, descriptor: ByteArray) {
+ println()
+ println(title)
+ val len = descriptor.size
+ var i = 0
+ while (i < len) {
+ System.out.printf("%04x ", i)
+ var j = i
+ while (j < min(i + 16, len)) {
+ System.out.printf(" %02x", descriptor[j].toInt() and 255)
+ j += 1
+ }
+ println()
+ i += 16
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/UsbClassInfo.kt b/examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/UsbClassInfo.kt
new file mode 100644
index 0000000..56ee48d
--- /dev/null
+++ b/examples/enumerate_kotlin/src/main/kotlin/net/codecrete/usb/examples/UsbClassInfo.kt
@@ -0,0 +1,234 @@
+package net.codecrete.usb.examples
+
+import java.io.BufferedReader
+import java.io.IOException
+import java.io.StringReader
+import java.util.*
+
+
+class UsbClassInfo {
+ private val classCodes: MutableList = ArrayList()
+ private val subclassCodes: MutableList = ArrayList()
+ private val protocolCodes: MutableList = ArrayList()
+
+ // List of known device classes, subclasses and
+ // from http://www.linux-usb.org/usb.ids
+ private val rawClassData = """
+C 00 (Defined at Interface level)
+C 01 Audio
+ 01 Control Device
+ 02 Streaming
+ 03 MIDI Streaming
+C 02 Communications
+ 01 Direct Line
+ 02 Abstract (modem)
+ 00 None
+ 01 AT-commands (v.25ter)
+ 02 AT-commands (PCCA101)
+ 03 AT-commands (PCCA101 + wakeup)
+ 04 AT-commands (GSM)
+ 05 AT-commands (3G)
+ 06 AT-commands (CDMA)
+ fe Defined by command set descriptor
+ ff Vendor Specific (MSFT RNDIS?)
+ 03 Telephone
+ 04 Multi-Channel
+ 05 CAPI Control
+ 06 Ethernet Networking
+ 07 ATM Networking
+ 08 Wireless Handset Control
+ 09 Device Management
+ 0a Mobile Direct Line
+ 0b OBEX
+ 0c Ethernet Emulation
+ 07 Ethernet Emulation (EEM)
+C 03 Human Interface Device
+ 00 No Subclass
+ 00 None
+ 01 Keyboard
+ 02 Mouse
+ 01 Boot Interface Subclass
+ 00 None
+ 01 Keyboard
+ 02 Mouse
+C 05 Physical Interface Device
+C 06 Imaging
+ 01 Still Image Capture
+ 01 Picture Transfer Protocol (PIMA 15470)
+C 07 Printer
+ 01 Printer
+ 00 Reserved/Undefined
+ 01 Unidirectional
+ 02 Bidirectional
+ 03 IEEE 1284.4 compatible bidirectional
+ ff Vendor Specific
+C 08 Mass Storage
+ 01 RBC (typically Flash)
+ 00 Control/Bulk/Interrupt
+ 01 Control/Bulk
+ 50 Bulk-Only
+ 02 SFF-8020i, MMC-2 (ATAPI)
+ 03 QIC-157
+ 04 Floppy (UFI)
+ 00 Control/Bulk/Interrupt
+ 01 Control/Bulk
+ 50 Bulk-Only
+ 05 SFF-8070i
+ 06 SCSI
+ 00 Control/Bulk/Interrupt
+ 01 Control/Bulk
+ 50 Bulk-Only
+C 09 Hub
+ 00 Unused
+ 00 Full speed (or root) hub
+ 01 Single TT
+ 02 TT per port
+C 0a CDC Data
+ 00 Unused
+ 30 I.430 ISDN BRI
+ 31 HDLC
+ 32 Transparent
+ 50 Q.921M
+ 51 Q.921
+ 52 Q.921TM
+ 90 V.42bis
+ 91 Q.932 EuroISDN
+ 92 V.120 V.24 rate ISDN
+ 93 CAPI 2.0
+ fd Host Based Driver
+ fe CDC PUF
+ ff Vendor specific
+C 0b Chip/SmartCard
+C 0d Content Security
+C 0e Video
+ 00 Undefined
+ 01 Video Control
+ 02 Video Streaming
+ 03 Video Interface Collection
+C 58 Xbox
+ 42 Controller
+C dc Diagnostic
+ 01 Reprogrammable Diagnostics
+ 01 USB2 Compliance
+C e0 Wireless
+ 01 Radio Frequency
+ 01 Bluetooth
+ 02 Ultra WideBand Radio Control
+ 03 RNDIS
+ 02 Wireless USB Wire Adapter
+ 01 Host Wire Adapter Control/Data Streaming
+ 02 Device Wire Adapter Control/Data Streaming
+ 03 Device Wire Adapter Isochronous Streaming
+C ef Miscellaneous Device
+ 01 ?
+ 01 Microsoft ActiveSync
+ 02 Palm Sync
+ 02 ?
+ 01 Interface Association
+ 02 Wire Adapter Multifunction Peripheral
+ 03 ?
+ 01 Cable Based Association
+ 05 USB3 Vision
+C fe Application Specific Interface
+ 01 Device Firmware Update
+ 02 IRDA Bridge
+ 03 Test and Measurement
+ 01 TMC
+ 02 USB488
+C ff Vendor Specific Class
+ ff Vendor Specific Subclass
+ ff Vendor Specific Protocol
+ """.trimIndent()
+
+
+ /**
+ * Provides the name of the specified USB class.
+ *
+ * @param classCode the USB class code
+ * @return an optional name
+ */
+ fun lookupClass(classCode: Int): String? {
+ loadData()
+ return classCodes
+ .filter { cc -> cc.classCode == classCode }
+ .map { cc -> cc.name }
+ .firstOrNull()
+ }
+
+ /**
+ * Provides the name of the specified USB subclass.
+ *
+ * @param classCode the USB class code
+ * @param subclassCode the USB subclass code
+ * @return an optional name
+ */
+ fun lookupSubclass(classCode: Int, subclassCode: Int): String? {
+ loadData()
+ return subclassCodes
+ .filter { scc -> scc.classCode == classCode && scc.subclassCode == subclassCode }
+ .map { scc -> scc.name }
+ .firstOrNull()
+ }
+
+ /**
+ * Provides the name of the specified USB protocol.
+ *
+ * @param classCode the USB class code
+ * @param subclassCode the USB subclass code
+ * @param protocolCode the USB protocol code
+ * @return an optional name
+ */
+ fun lookupProtocol(classCode: Int, subclassCode: Int, protocolCode: Int): String? {
+ loadData()
+ return protocolCodes
+ .filter { prot -> prot.classCode == classCode && prot.subclassCode == subclassCode && prot.protocolCode == protocolCode }
+ .map { prot -> prot.name }
+ .firstOrNull()
+ }
+
+ private fun loadData() {
+ if (classCodes.isNotEmpty())
+ return
+
+ try {
+ StringReader(rawClassData).use { stringReader ->
+ BufferedReader(stringReader).use { reader ->
+ var classCode = 0
+ var subclassCode = 0
+ var line = reader.readLine()
+ while (line != null) {
+ // protocol line
+ when {
+ line.startsWith("\t\t") -> {
+ val protocol = line.substring(2, 4).toInt(16)
+ protocolCodes.add(ProtocolCode(classCode, subclassCode, protocol, line.substring(6)))
+ // subclass line
+ }
+ line.startsWith("\t") -> {
+ subclassCode = line.substring(1, 3).toInt(16)
+ subclassCodes.add(SubclassCode(classCode, subclassCode, line.substring(5)))
+ // class line
+ }
+ line.startsWith("C ") -> {
+ classCode = line.substring(2, 4).toInt(16)
+ classCodes.add(ClassCode(classCode, line.substring(6)))
+ }
+ else -> {
+ error("Invalid raw data")
+ }
+ }
+ line = reader.readLine()
+ }
+ }
+ }
+ } catch (e: IOException) {
+ throw RuntimeException(e)
+ }
+ }
+
+ internal data class ClassCode(val classCode: Int, val name: String)
+
+ internal data class SubclassCode(val classCode: Int, val subclassCode: Int, val name: String)
+
+ internal data class ProtocolCode(val classCode: Int, val subclassCode: Int, val protocolCode: Int, val name: String)
+}
\ No newline at end of file
diff --git a/examples/epaper_display/pom.xml b/examples/epaper_display/pom.xml
index 79874da..9ea7e0e 100644
--- a/examples/epaper_display/pom.xml
+++ b/examples/epaper_display/pom.xml
@@ -21,7 +21,7 @@
net.codecrete.usb
java-does-usb
- 0.6.1
+ 0.7.0-SNAPSHOT
diff --git a/examples/epaper_display/src/main/java/net/codecrete/usb/examples/IT8951Driver.java b/examples/epaper_display/src/main/java/net/codecrete/usb/examples/IT8951Driver.java
index 7170fc7..47ae2b2 100644
--- a/examples/epaper_display/src/main/java/net/codecrete/usb/examples/IT8951Driver.java
+++ b/examples/epaper_display/src/main/java/net/codecrete/usb/examples/IT8951Driver.java
@@ -7,8 +7,8 @@
package net.codecrete.usb.examples;
-import net.codecrete.usb.USB;
-import net.codecrete.usb.USBDevice;
+import net.codecrete.usb.Usb;
+import net.codecrete.usb.UsbDevice;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
@@ -35,7 +35,7 @@ public class IT8951Driver {
(byte)0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, (byte)0x94, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
- private USBDevice device;
+ private UsbDevice device;
private int sequenceNo = 1;
private DisplayInfo displayInfo;
@@ -46,7 +46,7 @@ public class IT8951Driver {
* @throws IllegalStateException if no IT8951 device is found
*/
public void open() {
- var optionalDevice = USB.getDevice(0x048d, 0x8951);
+ var optionalDevice = Usb.findDevice(0x048d, 0x8951);
if (optionalDevice.isEmpty())
throw new IllegalStateException("No IT8951 device found");
@@ -276,7 +276,7 @@ private Status readStatus() {
if (result.length == 13)
return Status.from(result);
- throw new RuntimeException(String.format("Unexpected length of status block (%d)", result.length));
+ throw new IllegalStateException(String.format("Unexpected length of status block (%d)", result.length));
}
/**
diff --git a/examples/monitor/pom.xml b/examples/monitor/pom.xml
index b36fbf1..4d2ae39 100644
--- a/examples/monitor/pom.xml
+++ b/examples/monitor/pom.xml
@@ -21,7 +21,7 @@
net.codecrete.usb
java-does-usb
- 0.6.1
+ 0.7.0-SNAPSHOT
org.tinylog
diff --git a/examples/monitor/src/main/java/net/codecrete/usb/examples/Monitor.java b/examples/monitor/src/main/java/net/codecrete/usb/examples/Monitor.java
index a897ba9..482d1b9 100644
--- a/examples/monitor/src/main/java/net/codecrete/usb/examples/Monitor.java
+++ b/examples/monitor/src/main/java/net/codecrete/usb/examples/Monitor.java
@@ -7,8 +7,8 @@
package net.codecrete.usb.examples;
-import net.codecrete.usb.USB;
-import net.codecrete.usb.USBDevice;
+import net.codecrete.usb.Usb;
+import net.codecrete.usb.UsbDevice;
import java.io.IOException;
@@ -19,11 +19,11 @@ public class Monitor {
public static void main(String[] args) throws IOException {
// register callbacks for events
- USB.setOnDeviceConnected((device) -> printDetails(device, "Connected"));
- USB.setOnDeviceDisconnected((device) -> printDetails(device, "Disconnected"));
+ Usb.setOnDeviceConnected(device -> printDetails(device, "Connected"));
+ Usb.setOnDeviceDisconnected(device -> printDetails(device, "Disconnected"));
// display the already present USB devices
- for (var device : USB.getAllDevices())
+ for (var device : Usb.getDevices())
printDetails(device, "Present");
// wait for ENTER to quit program
@@ -31,7 +31,7 @@ public static void main(String[] args) throws IOException {
System.in.read();
}
- private static void printDetails(USBDevice device, String event) {
+ private static void printDetails(UsbDevice device, String event) {
System.out.printf("%-14s", event + ":");
System.out.println(device.toString());
}
diff --git a/examples/stm_dfu/pom.xml b/examples/stm_dfu/pom.xml
index fe623b6..13ee51f 100644
--- a/examples/stm_dfu/pom.xml
+++ b/examples/stm_dfu/pom.xml
@@ -19,7 +19,7 @@
net.codecrete.usb
java-does-usb
- 0.6.1
+ 0.7.0-SNAPSHOT
diff --git a/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/DFUDevice.java b/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/DFUDevice.java
index f5b12f8..c8b0d79 100644
--- a/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/DFUDevice.java
+++ b/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/DFUDevice.java
@@ -11,7 +11,9 @@
import java.util.Arrays;
import java.util.List;
-import java.util.stream.Collectors;
+
+import static net.codecrete.usb.UsbRecipient.INTERFACE;
+import static net.codecrete.usb.UsbRequestType.CLASS;
/**
* DFU device.
@@ -21,7 +23,7 @@
*/
public class DFUDevice {
- private final USBDevice usbDevice_;
+ private final UsbDevice usbDevice_;
private final int interfaceNumber_;
private final int transferSize_;
private final Version dfuVersion_;
@@ -33,10 +35,10 @@ public class DFUDevice {
* @return List of DFU devices
*/
public static List getAll() {
- return USB.getAllDevices().stream()
- .filter(DFUDevice::hasDFUDescriptor)
+ return Usb.findDevices(DFUDevice::hasDFUDescriptor)
+ .stream()
.map(DFUDevice::new)
- .collect(Collectors.toList());
+ .toList();
}
/**
@@ -44,8 +46,8 @@ public static List getAll() {
* @param device USB device
* @return {@code true} if it has a DFU descriptor, {@code false} otherwise
*/
- public static boolean hasDFUDescriptor(USBDevice device) {
- return getDFUDescriptorOffset(device.configurationDescriptor()) > 0
+ public static boolean hasDFUDescriptor(UsbDevice device) {
+ return getDFUDescriptorOffset(device.getConfigurationDescriptor()) > 0
&& getDFUInterfaceNumber(device) >= 0;
}
@@ -69,11 +71,11 @@ public static int getDFUDescriptorOffset(byte[] descriptor) {
* @param device the USB device
* @return the interface number, of -1 if not found
*/
- public static int getDFUInterfaceNumber(USBDevice device) {
- for (var intf : device.interfaces()) {
- var alt = intf.alternate();
- if (alt.classCode() == 0xFE && alt.subclassCode() == 0x01 && alt.protocolCode() == 0x02)
- return intf.number();
+ public static int getDFUInterfaceNumber(UsbDevice device) {
+ for (var intf : device.getInterfaces()) {
+ var alt = intf.getCurrentAlternate();
+ if (alt.getClassCode() == 0xFE && alt.getSubclassCode() == 0x01 && alt.getProtocolCode() == 0x02)
+ return intf.getNumber();
}
return -1;
@@ -86,11 +88,11 @@ public static int getDFUInterfaceNumber(USBDevice device) {
*
* @param usbDevice the USB device
*/
- public DFUDevice(USBDevice usbDevice) {
+ public DFUDevice(UsbDevice usbDevice) {
usbDevice_ = usbDevice;
interfaceNumber_ = getDFUInterfaceNumber(usbDevice);
- var configDesc = usbDevice.configurationDescriptor();
+ var configDesc = usbDevice.getConfigurationDescriptor();
int offset = getDFUDescriptorOffset(configDesc);
assert offset > 0;
@@ -111,7 +113,7 @@ public Version dfuVersion() {
* @return the serial number
*/
public String serialNumber() {
- return usbDevice_.serialNumber();
+ return usbDevice_.getSerialNumber();
}
/**
@@ -135,7 +137,7 @@ public void close() {
* Clears an error status.
*/
public void clearStatus() {
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.CLEAR_STATUS.value(), 0, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.CLEAR_STATUS.value(), 0, interfaceNumber_);
usbDevice_.controlTransferOut(setup, null);
}
@@ -143,7 +145,7 @@ public void clearStatus() {
* Aborts the download mode.
*/
public void abort() {
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.ABORT.value(), 0, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.ABORT.value(), 0, interfaceNumber_);
usbDevice_.controlTransferOut(setup, null);
}
@@ -152,7 +154,7 @@ public void abort() {
* @return the status
*/
public DFUStatus getStatus() {
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.GET_STATUS.value(), 0, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.GET_STATUS.value(), 0, interfaceNumber_);
return DFUStatus.fromBytes(usbDevice_.controlTransferIn(setup, 6));
}
@@ -176,7 +178,7 @@ public byte[] read(int address, int length) {
int blockNum = 2;
while (offset < length) {
int chunkSize = Math.min(transferSize_, length - offset);
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.UPLOAD.value(), blockNum, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.UPLOAD.value(), blockNum, interfaceNumber_);
var chunk = usbDevice_.controlTransferIn(setup, chunkSize);
System.arraycopy(chunk, 0, result, offset, chunkSize);
offset += chunkSize;
@@ -184,7 +186,7 @@ public byte[] read(int address, int length) {
}
// request zero lenght chunk to exit out of upload mode
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.UPLOAD.value(), blockNum, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.UPLOAD.value(), blockNum, interfaceNumber_);
usbDevice_.controlTransferIn(setup, 0);
return result;
@@ -223,7 +225,7 @@ public void download(byte[] firmware) {
System.arraycopy(firmware, offset, chunk, 0, chunkSize);
System.out.printf("Writing data at 0x%x (size 0x%x)%n", startAddress + offset, chunkSize);
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.DOWNLOAD.value(), transaction, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.DOWNLOAD.value(), transaction, interfaceNumber_);
usbDevice_.controlTransferOut(setup, chunk);
finishDownloadCommand("writing data");
@@ -272,7 +274,7 @@ public void setAddress(int address) {
}
private void execDownloadCommandWithAddress(byte command, String action, int address) {
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.DOWNLOAD.value(), 0, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.DOWNLOAD.value(), 0, interfaceNumber_);
var data = new byte[] {
command,
(byte) address,
@@ -325,7 +327,7 @@ private void exitDownloadMode() {
public void startApplication() {
expectState(DeviceState.DFU_IDLE, DeviceState.DFU_DNLOAD_IDLE);
- var setup = new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, DFURequest.DOWNLOAD.value(), 2, interfaceNumber_);
+ var setup = new UsbControlTransfer(CLASS, INTERFACE, DFURequest.DOWNLOAD.value(), 2, interfaceNumber_);
usbDevice_.controlTransferOut(setup, null);
var status = getStatus();
@@ -359,6 +361,7 @@ private static void sleep(int millis) {
try {
Thread.sleep(millis);
} catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
throw new DFUException("Sleep failed", e);
}
}
diff --git a/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/STM32.java b/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/STM32.java
index a157ca5..a9f40b5 100644
--- a/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/STM32.java
+++ b/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/STM32.java
@@ -11,5 +11,8 @@
* STM32 specific constants
*/
public class STM32 {
+
+ private STM32() { }
+
public static final int FLASH_BASE_ADDRESS = 0x08000000;
}
diff --git a/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/Segment.java b/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/Segment.java
index eef8d1f..740a845 100644
--- a/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/Segment.java
+++ b/examples/stm_dfu/src/main/java/net/codecrete/usb/dfu/Segment.java
@@ -7,15 +7,16 @@
package net.codecrete.usb.dfu;
-import net.codecrete.usb.USBControlTransfer;
-import net.codecrete.usb.USBDevice;
-import net.codecrete.usb.USBRecipient;
-import net.codecrete.usb.USBRequestType;
+import net.codecrete.usb.UsbControlTransfer;
+import net.codecrete.usb.UsbDevice;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
+import static net.codecrete.usb.UsbRecipient.DEVICE;
+import static net.codecrete.usb.UsbRequestType.STANDARD;
+
/**
* Represents a memory segment of the USB device, be it flash memory, RAM or any other type.
*/
@@ -27,12 +28,12 @@ public class Segment {
* @param interfaceNumber the number of the DFU USB interface
* @return list of segments
*/
- public static List getSegments(USBDevice device, int interfaceNumber) {
+ public static List getSegments(UsbDevice device, int interfaceNumber) {
var result = new ArrayList();
// STM uses multiple alternate interface settings to represent segments.
// The alternate interface settings name describes the sectors within the segment.
- var configDesc = device.configurationDescriptor();
+ var configDesc = device.getConfigurationDescriptor();
int offset = 0;
while (offset < configDesc.length) {
if (configDesc[offset + 1] == 4 && (configDesc[offset + 2] & 0xff) == interfaceNumber) {
@@ -53,8 +54,8 @@ public static List getSegments(USBDevice device, int interfaceNumber) {
* @param index the index of the string descriptor
* @return the string
*/
- private static String getStringDescriptor(USBDevice device, int index) {
- var setup = new USBControlTransfer(USBRequestType.STANDARD, USBRecipient.DEVICE, 6, (3 << 8) | index, 0);
+ private static String getStringDescriptor(UsbDevice device, int index) {
+ var setup = new UsbControlTransfer(STANDARD, DEVICE, 6, (3 << 8) | index, 0);
byte[] stringDesc = device.controlTransferIn(setup, 255);
int descLen = stringDesc[0] & 0xff;
return new String(stringDesc, 2, descLen - 2, StandardCharsets.UTF_16LE);
@@ -97,7 +98,7 @@ public static Page findPage(List segments, int address) {
private Segment(int altSetting, String segmentDesc) {
// The format is described in "UM0424 STM32 USB-FS-Device development kit", ch. 10.3.2
altSetting_ = altSetting;
- sectors_ = new ArrayList();
+ sectors_ = new ArrayList<>();
int offset = segmentDesc.indexOf('/', 1);
name_ = segmentDesc.substring(1, offset).trim();
diff --git a/java-does-usb/pom.xml b/java-does-usb/pom.xml
index ee8e73a..99f2dea 100644
--- a/java-does-usb/pom.xml
+++ b/java-does-usb/pom.xml
@@ -6,7 +6,7 @@
net.codecrete.usb
java-does-usb
- 0.6.2-SNAPSHOT
+ 0.7.0-SNAPSHOT
21
@@ -137,6 +137,12 @@
+
+ org.jetbrains
+ annotations
+ 24.0.1
+ compile
+
org.junit.jupiter
junit-jupiter
diff --git a/java-does-usb/src/main/java/module-info.java b/java-does-usb/src/main/java/module-info.java
index cf4daec..496c87e 100644
--- a/java-does-usb/src/main/java/module-info.java
+++ b/java-does-usb/src/main/java/module-info.java
@@ -9,5 +9,6 @@
* Java Does USB – work with USB devices
*/
module net.codecrete.usb {
+ requires org.jetbrains.annotations;
exports net.codecrete.usb;
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USB.java b/java-does-usb/src/main/java/net/codecrete/usb/Usb.java
similarity index 64%
rename from java-does-usb/src/main/java/net/codecrete/usb/USB.java
rename to java-does-usb/src/main/java/net/codecrete/usb/Usb.java
index 5956473..3c28730 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USB.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/Usb.java
@@ -7,11 +7,16 @@
package net.codecrete.usb;
-import net.codecrete.usb.common.USBDeviceRegistry;
-import net.codecrete.usb.linux.LinuxUSBDeviceRegistry;
-import net.codecrete.usb.macos.MacosUSBDeviceRegistry;
-import net.codecrete.usb.windows.WindowsUSBDeviceRegistry;
+import net.codecrete.usb.common.UsbDeviceRegistry;
+import net.codecrete.usb.linux.LinuxUsbDeviceRegistry;
+import net.codecrete.usb.macos.MacosUsbDeviceRegistry;
+import net.codecrete.usb.windows.WindowsUsbDeviceRegistry;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.annotations.Unmodifiable;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
@@ -19,19 +24,19 @@
/**
* Provides access to USB devices.
*/
-public class USB {
+public class Usb {
- private static USBDeviceRegistry createInstance() {
+ private static UsbDeviceRegistry createInstance() {
var osName = System.getProperty("os.name");
var osArch = System.getProperty("os.arch");
- USBDeviceRegistry impl;
+ UsbDeviceRegistry impl;
if (osName.equals("Mac OS X") && (osArch.equals("x86_64") || osArch.equals("aarch64"))) {
- impl = new MacosUSBDeviceRegistry();
+ impl = new MacosUsbDeviceRegistry();
} else if (osName.startsWith("Windows") && osArch.equals("amd64")) {
- impl = new WindowsUSBDeviceRegistry();
+ impl = new WindowsUsbDeviceRegistry();
} else if (osName.equals("Linux") && (osArch.equals("amd64") || osArch.equals("aarch64"))) {
- impl = new LinuxUSBDeviceRegistry();
+ impl = new LinuxUsbDeviceRegistry();
} else {
throw new UnsupportedOperationException(String.format(
"Java Does USB has no implementation for architecture %s/%s",
@@ -40,9 +45,9 @@ private static USBDeviceRegistry createInstance() {
return impl;
}
- private static USBDeviceRegistry singletonInstance = null;
+ private static UsbDeviceRegistry singletonInstance = null;
- private static synchronized USBDeviceRegistry instance() {
+ private static synchronized UsbDeviceRegistry instance() {
if (singletonInstance == null) {
singletonInstance = createInstance();
singletonInstance.start();
@@ -51,7 +56,7 @@ private static synchronized USBDeviceRegistry instance() {
}
// Private, so no instance can be created
- private USB() {
+ private Usb() {
}
/**
@@ -64,8 +69,8 @@ private USB() {
*
* @return list of USB devices
*/
- public static List getAllDevices() {
- return instance().getAllDevices();
+ public static @NotNull @Unmodifiable Collection getDevices() {
+ return Collections.unmodifiableCollection(instance().getAllDevices());
}
/**
@@ -74,7 +79,7 @@ public static List getAllDevices() {
* @param predicate device predicate
* @return list of USB devices
*/
- public static List getDevices(USBDevicePredicate predicate) {
+ public static @NotNull @Unmodifiable List findDevices(@NotNull UsbDevicePredicate predicate) {
return instance().getAllDevices().stream().filter(predicate::matches).toList();
}
@@ -84,7 +89,7 @@ public static List getDevices(USBDevicePredicate predicate) {
* @param predicate device predicate
* @return optional USB device
*/
- public static Optional getDevice(USBDevicePredicate predicate) {
+ public static Optional findDevice(@NotNull UsbDevicePredicate predicate) {
return instance().getAllDevices().stream().filter(predicate::matches).findFirst();
}
@@ -95,8 +100,8 @@ public static Optional getDevice(USBDevicePredicate predicate) {
* @param productId product ID
* @return optional USB device
*/
- public static Optional getDevice(int vendorId, int productId) {
- return getDevice(device -> device.vendorId() == vendorId && device.productId() == productId);
+ public static Optional findDevice(int vendorId, int productId) {
+ return findDevice(device -> device.getVendorId() == vendorId && device.getProductId() == productId);
}
/**
@@ -104,21 +109,21 @@ public static Optional getDevice(int vendorId, int productId) {
*
* @param handler handler function, or {@code null} to remove a previous handler
*/
- public static void setOnDeviceConnected(Consumer handler) {
+ public static void setOnDeviceConnected(@Nullable Consumer handler) {
instance().setOnDeviceConnected(handler);
}
/**
* Sets the handler to be called when a USB device is disconnected.
*
- * When the handler is called, the {@link USBDevice} instance has already been closed.
+ * When the handler is called, the {@link UsbDevice} instance has already been closed.
* Descriptive information (such as vendor and product ID, serial number, interfaces, endpoints)
* can still be accessed.
*
*
* @param handler handler function, or {@code null} to remove a previous handler
*/
- public static void setOnDeviceDisconnected(Consumer handler) {
+ public static void setOnDeviceDisconnected(@Nullable Consumer handler) {
instance().setOnDeviceDisconnected(handler);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBAlternateInterface.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbAlternateInterface.java
similarity index 74%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBAlternateInterface.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbAlternateInterface.java
index b0445f9..68d3bcf 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBAlternateInterface.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbAlternateInterface.java
@@ -7,6 +7,9 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Unmodifiable;
+
import java.util.List;
/**
@@ -15,7 +18,7 @@
* Instances of this class describe an alternate setting of a USB interface.
*
*/
-public interface USBAlternateInterface {
+public interface UsbAlternateInterface {
/**
* Gets the alternate setting number.
@@ -24,7 +27,7 @@ public interface USBAlternateInterface {
*
* @return the alternate setting number
*/
- int number();
+ int getNumber();
/**
* Gets the interface class.
@@ -34,7 +37,7 @@ public interface USBAlternateInterface {
*
* @return the interface class
*/
- int classCode();
+ int getClassCode();
/**
* Gets the interface subclass.
@@ -44,7 +47,7 @@ public interface USBAlternateInterface {
*
* @return the interface subclass
*/
- int subclassCode();
+ int getSubclassCode();
/**
* Gets the interface protocol.
@@ -54,7 +57,7 @@ public interface USBAlternateInterface {
*
* @return the interface protocol
*/
- int protocolCode();
+ int getProtocolCode();
/**
* Gets the endpoints of this alternate interface settings.
@@ -62,18 +65,22 @@ public interface USBAlternateInterface {
* The endpoint list does not include endpoint 0, which
* is always available and reserved for control transfers.
*
+ *
+ * The returned list is sorted by endpoint number.
+ *
*
* @return a list of endpoints.
*/
- List endpoints();
+ @NotNull @Unmodifiable List getEndpoints();
/**
* Gets the endpoint with the specified number and direction.
*
* @param endpointNumber endpoint number (in the range between 1 and 127, without the direction bit)
* @param direction endpoint direction
- * @return the endpoint, or {@code null} if no endpoint with the given number and direction exists
+ * @return the endpoint
+ * @exception UsbException if the endpoint does not exist
*/
- USBEndpoint getEndpoint(int endpointNumber, USBDirection direction);
+ @NotNull UsbEndpoint getEndpoint(int endpointNumber, UsbDirection direction);
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBControlTransfer.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbControlTransfer.java
similarity index 90%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBControlTransfer.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbControlTransfer.java
index ef93711..6008911 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBControlTransfer.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbControlTransfer.java
@@ -24,9 +24,9 @@
* @param value value (value between 0 and 65535, called {@code wValue} in USB specification)
* @param index index (value between 0 and 65535, called {@code wIndex} in USB specification).
*/
-public record USBControlTransfer(
- USBRequestType requestType,
- USBRecipient recipient,
+public record UsbControlTransfer(
+ UsbRequestType requestType,
+ UsbRecipient recipient,
int request,
int value,
int index
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBDevice.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbDevice.java
similarity index 86%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBDevice.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbDevice.java
index 6783747..acf0374 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBDevice.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbDevice.java
@@ -7,6 +7,9 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Unmodifiable;
+
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
@@ -23,35 +26,35 @@
* closed state.
*
*/
-public interface USBDevice {
+public interface UsbDevice {
/**
* USB product ID.
*
* @return product ID
*/
- int productId();
+ int getProductId();
/**
* USB vendor ID.
*
* @return vendor ID
*/
- int vendorId();
+ int getVendorId();
/**
* Product name.
*
* @return product name or {@code null} if not provided by the device
*/
- String product();
+ String getProduct();
/**
* Manufacturer name
*
* @return manufacturer name or {@code null} if not provided by the device
*/
- String manufacturer();
+ String getManufacturer();
/**
* Serial number
@@ -62,42 +65,42 @@ public interface USBDevice {
*
* @return serial number or {@code null} if not provided by the device
*/
- String serialNumber();
+ String getSerialNumber();
/**
* USB device class code ({@code bDeviceClass} from device descriptor).
*
* @return class code
*/
- int classCode();
+ int getClassCode();
/**
* USB device subclass code ({@code bDeviceSubClass} from device descriptor).
*
* @return subclass code
*/
- int subclassCode();
+ int getSubclassCode();
/**
* USB device protocol ({@code bDeviceProtocol} from device descriptor).
*
* @return protocol code
*/
- int protocolCode();
+ int getProtocolCode();
/**
* USB protocol version supported by this device.
*
* @return version
*/
- Version usbVersion();
+ @NotNull Version getUsbVersion();
/**
* Device version (as declared by the manufacturer).
*
* @return version
*/
- Version deviceVersion();
+ @NotNull Version getDeviceVersion();
/**
* Detaches the standard operating-system drivers of this device.
@@ -159,7 +162,7 @@ public interface USBDevice {
*
* @return {@code true} if the device is open, {@code false} if it is closed.
*/
- boolean isOpen();
+ boolean isOpened();
/**
* Closes the device.
@@ -168,27 +171,32 @@ public interface USBDevice {
/**
* Gets the interfaces of this device.
+ *
+ * The returned list is sorted by interface number.
+ *
*
* @return a list of USB interfaces
*/
- List interfaces();
+ @NotNull @Unmodifiable List getInterfaces();
/**
* Gets the interface with the specified number.
*
* @param interfaceNumber the interface number
- * @return the interface, or {@code null} if no interface with the given number exists
+ * @return the interface
+ * @exception UsbException if the interface does not exist
*/
- USBInterface getInterface(int interfaceNumber);
+ @NotNull UsbInterface getInterface(int interfaceNumber);
/**
* Gets the endpoint with the specified number.
*
* @param direction the endpoint direction
* @param endpointNumber the endpoint number (between 1 and 127)
- * @return the endpoint, or {@code null} if no endpoint with the given direction and number exists
+ * @return the endpoint
+ * @exception UsbException if the endpoint does not exist
*/
- USBEndpoint getEndpoint(USBDirection direction, int endpointNumber);
+ @NotNull UsbEndpoint getEndpoint(UsbDirection direction, int endpointNumber);
/**
* Claims the specified interface for exclusive use.
@@ -231,11 +239,11 @@ public interface USBDevice {
* or the interface of the addressed endpoint must have been claimed.
*
*
- * @param setup control transfer setup parameters
+ * @param transfer control transfer setup parameters
* @param length maximum length of expected data
* @return received data.
*/
- byte[] controlTransferIn(USBControlTransfer setup, int length);
+ byte @NotNull [] controlTransferIn(@NotNull UsbControlTransfer transfer, int length);
/**
* Executes a control transfer request and optionally sends data.
@@ -253,10 +261,10 @@ public interface USBDevice {
* or the interface of the addressed endpoint must have been claimed.
*
*
- * @param setup control transfer setup parameters
+ * @param transfer control transfer setup parameters
* @param data data to send, or {@code null} if the transfer has no data stage.
*/
- void controlTransferOut(USBControlTransfer setup, byte[] data);
+ void controlTransferOut(@NotNull UsbControlTransfer transfer, byte[] data);
/**
* Sends data to this device.
@@ -275,13 +283,13 @@ public interface USBDevice {
* @param endpointNumber endpoint number (in the range between 1 and 127)
* @param data data to send
*/
- void transferOut(int endpointNumber, byte[] data);
+ void transferOut(int endpointNumber, byte @NotNull [] data);
/**
* Sends data to this device.
*
* This method blocks until the data has been sent, the timeout period has expired
- * or an error has occurred. If the timeout expires, a {@link USBTimeoutException} is thrown.
+ * or an error has occurred. If the timeout expires, a {@link UsbTimeoutException} is thrown.
*
*
* This method can send data to bulk and interrupt endpoints.
@@ -296,13 +304,13 @@ public interface USBDevice {
* @param data data to send
* @param timeout the timeout period, in milliseconds (0 for no timeout)
*/
- void transferOut(int endpointNumber, byte[] data, int timeout);
+ void transferOut(int endpointNumber, byte @NotNull [] data, int timeout);
/**
* Sends data to this device.
*
* This method blocks until the data has been sent, the timeout period has expired
- * or an error has occurred. If the timeout expires, a {@link USBTimeoutException} is thrown.
+ * or an error has occurred. If the timeout expires, a {@link UsbTimeoutException} is thrown.
*
*
* This method can send data to bulk and interrupt endpoints.
@@ -319,7 +327,7 @@ public interface USBDevice {
* @param length number of bytes to send
* @param timeout the timeout period, in milliseconds (0 for no timeout)
*/
- void transferOut(int endpointNumber, byte[] data, int offset, int length, int timeout);
+ void transferOut(int endpointNumber, byte @NotNull [] data, int offset, int length, int timeout);
/**
* Receives data from this device.
@@ -337,13 +345,13 @@ public interface USBDevice {
* @param endpointNumber endpoint number (in the range between 1 and 127, i.e. without the direction bit)
* @return received data
*/
- byte[] transferIn(int endpointNumber);
+ byte @NotNull [] transferIn(int endpointNumber);
/**
* Receives data from this device.
*
* This method blocks until at least a packet has been received, the timeout period has expired
- * or an error has occurred. If the timeout expired, a {@link USBTimeoutException} is thrown.
+ * or an error has occurred. If the timeout expired, a {@link UsbTimeoutException} is thrown.
*
*
* The returned data is the payload of a packet. It can have a length of 0 if the USB device
@@ -357,7 +365,7 @@ public interface USBDevice {
* @param timeout the timeout period, in milliseconds (0 for no timeout)
* @return received data
*/
- byte[] transferIn(int endpointNumber, int timeout);
+ byte @NotNull [] transferIn(int endpointNumber, int timeout);
/**
* Opens a new output stream to send data to a bulk endpoint.
@@ -378,7 +386,7 @@ public interface USBDevice {
* @param bufferSize approximate buffer size (in bytes)
* @return the new output stream
*/
- OutputStream openOutputStream(int endpointNumber, int bufferSize);
+ @NotNull OutputStream openOutputStream(int endpointNumber, int bufferSize);
/**
* Opens a new output stream to send data to a bulk endpoint.
@@ -389,7 +397,7 @@ public interface USBDevice {
* @param endpointNumber bulk endpoint number (in the range between 1 and 127)
* @return the new output stream
*/
- default OutputStream openOutputStream(int endpointNumber) {
+ default @NotNull OutputStream openOutputStream(int endpointNumber) {
return openOutputStream(endpointNumber, 1);
}
@@ -409,7 +417,7 @@ default OutputStream openOutputStream(int endpointNumber) {
* @param bufferSize approximate buffer size (in bytes)
* @return the new input stream
*/
- InputStream openInputStream(int endpointNumber, int bufferSize);
+ @NotNull InputStream openInputStream(int endpointNumber, int bufferSize);
/**
* Opens a new input stream to receive data from a bulk endpoint.
@@ -421,7 +429,7 @@ default OutputStream openOutputStream(int endpointNumber) {
* @param endpointNumber bulk endpoint number (in the range between 1 and 127, i.e. without the direction bit)
* @return the new input stream
*/
- default InputStream openInputStream(int endpointNumber) {
+ default @NotNull InputStream openInputStream(int endpointNumber) {
return openInputStream(endpointNumber, 1);
}
@@ -434,7 +442,7 @@ default InputStream openInputStream(int endpointNumber) {
* @param direction endpoint direction
* @param endpointNumber endpoint number (in the range between 1 and 127)
*/
- void abortTransfers(USBDirection direction, int endpointNumber);
+ void abortTransfers(UsbDirection direction, int endpointNumber);
/**
* Clears an endpoint's halt condition.
@@ -450,19 +458,19 @@ default InputStream openInputStream(int endpointNumber) {
* @param direction endpoint direction
* @param endpointNumber endpoint number (in the range between 1 and 127)
*/
- void clearHalt(USBDirection direction, int endpointNumber);
+ void clearHalt(UsbDirection direction, int endpointNumber);
/**
* Gets the device descriptor.
*
* @return the device descriptor (as a byte array)
*/
- byte[] deviceDescriptor();
+ byte @NotNull [] getDeviceDescriptor();
/**
* Gets the configuration descriptor.
*
* @return the configuration descriptor (as a byte array)
*/
- byte[] configurationDescriptor();
+ byte @NotNull [] getConfigurationDescriptor();
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBDevicePredicate.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbDevicePredicate.java
similarity index 78%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBDevicePredicate.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbDevicePredicate.java
index 6b1b773..609db9b 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBDevicePredicate.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbDevicePredicate.java
@@ -7,23 +7,25 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+
import java.util.List;
/**
* Represents a predicate (boolean-valued function) of one argument, evaluated for a given USB device.
*
- * This is a functional interface whose functional method is {@link #matches(USBDevice)}.
+ * This is a functional interface whose functional method is {@link #matches(UsbDevice)}.
*
*/
@FunctionalInterface
-public interface USBDevicePredicate {
+public interface UsbDevicePredicate {
/**
* Evaluates this predicate on the given USB device.
*
* @param device the USB device
* @return {@code true} of the devices matches the predicate, otherwise {@code false}
*/
- boolean matches(USBDevice device);
+ boolean matches(@NotNull UsbDevice device);
/**
* Test if the USB devices matches any of the filter conditions.
@@ -32,7 +34,7 @@ public interface USBDevicePredicate {
* @param predicates a list of filter predicates
* @return {@code true} if it matches, {@code false} otherwise
*/
- static boolean matchesAny(USBDevice device, List predicates) {
+ static boolean matchesAny(@NotNull UsbDevice device, @NotNull List predicates) {
return predicates.stream().anyMatch(predicate -> predicate.matches(device));
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBDirection.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbDirection.java
similarity index 92%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBDirection.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbDirection.java
index 8e2e798..8e561dc 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBDirection.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbDirection.java
@@ -10,7 +10,7 @@
/**
* USB endpoint data direction enumeration.
*/
-public enum USBDirection {
+public enum UsbDirection {
/**
* Direction OUT (host to device)
*/
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBEndpoint.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbEndpoint.java
similarity index 87%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBEndpoint.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbEndpoint.java
index 434e0d3..1cebc06 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBEndpoint.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbEndpoint.java
@@ -13,7 +13,7 @@
* Instances of this class describe a USB endpoint.
*
*/
-public interface USBEndpoint {
+public interface UsbEndpoint {
/**
* Gets the USB endpoint number.
@@ -24,12 +24,12 @@ public interface USBEndpoint {
*
*
* Use this number when calling any of the transfer methods of a
- * {@link USBDevice} instance.
+ * {@link UsbDevice} instance.
*
*
* @return the endpoint number
*/
- int number();
+ int getNumber();
/**
* Gets the direction of the endpoint.
@@ -40,14 +40,14 @@ public interface USBEndpoint {
*
* @return the direction
*/
- USBDirection direction();
+ UsbDirection getDirection();
/**
* Gets the USB endpoint transfer type.
*
* @return the transfer type
*/
- USBTransferType transferType();
+ UsbTransferType getTransferType();
/**
* Gets the packet size.
@@ -58,5 +58,5 @@ public interface USBEndpoint {
*
* @return the packet size, in bytes.
*/
- int packetSize();
+ int getPacketSize();
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBException.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbException.java
similarity index 74%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBException.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbException.java
index dacc902..e0c62ac 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBException.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbException.java
@@ -7,10 +7,13 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
/**
* USB exception, thrown if an operation with USB devices fails.
*/
-public class USBException extends RuntimeException {
+public class UsbException extends RuntimeException {
/**
* Error code.
@@ -22,7 +25,7 @@ public class USBException extends RuntimeException {
*
* @param message the message
*/
- public USBException(String message) {
+ public UsbException(@NotNull String message) {
super(message);
code = -1;
}
@@ -33,7 +36,7 @@ public USBException(String message) {
* @param message the message
* @param errorCode the error code
*/
- public USBException(String message, int errorCode) {
+ public UsbException(@NotNull String message, int errorCode) {
super(message + " (error code: " + errorCode + ")");
code = errorCode;
}
@@ -44,7 +47,7 @@ public USBException(String message, int errorCode) {
* @param message the message
* @param cause the causal exception
*/
- public USBException(String message, Throwable cause) {
+ public UsbException(@NotNull String message, @Nullable Throwable cause) {
super(message, cause);
code = -1;
}
@@ -54,7 +57,7 @@ public USBException(String message, Throwable cause) {
*
* @return the error code
*/
- public int errorCode() {
+ public int getErrorCode() {
return code;
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBInterface.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbInterface.java
similarity index 66%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBInterface.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbInterface.java
index 8f63c64..372ebfd 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBInterface.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbInterface.java
@@ -7,6 +7,9 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Unmodifiable;
+
import java.util.List;
/**
@@ -15,7 +18,7 @@
* Instances of this class describe an interface of a USB device.
*
*/
-public interface USBInterface {
+public interface UsbInterface {
/**
* Gets the interface number.
@@ -25,10 +28,10 @@ public interface USBInterface {
*
* @return the interface number
*/
- int number();
+ int getNumber();
/**
- * Indicates if this interface is currently claimed for exclusive access.
+ * Indicates if this interface has been claimed by this program for exclusive access.
*
* @return {@code true} if it is claimed, {@code false} otherwise.
*/
@@ -42,20 +45,24 @@ public interface USBInterface {
*
* @return the alternate interface setting.
*/
- USBAlternateInterface alternate();
+ @NotNull UsbAlternateInterface getCurrentAlternate();
/**
* Gets the alternate interface settings with the specified number.
*
* @param alternateNumber alternate setting number
* @return alternate interface setting
+ * @throws UsbException if the alternate setting does not exist
*/
- USBAlternateInterface getAlternate(int alternateNumber);
+ @NotNull UsbAlternateInterface getAlternate(int alternateNumber);
/**
* Gets all alternate settings of this interface.
+ *
+ * The returned list is sorted by alternate setting number.
+ *
*
* @return a list of the alternate settings
*/
- List alternates();
+ @NotNull @Unmodifiable List getAlternates();
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBRecipient.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbRecipient.java
similarity index 93%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBRecipient.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbRecipient.java
index 250238a..756027a 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBRecipient.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbRecipient.java
@@ -10,7 +10,7 @@
/**
* USB control transfer recipient enumeration.
*/
-public enum USBRecipient {
+public enum UsbRecipient {
/**
* USB device
*/
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBRequestType.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbRequestType.java
similarity index 93%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBRequestType.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbRequestType.java
index 05ede7e..08dec0d 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBRequestType.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbRequestType.java
@@ -10,7 +10,7 @@
/**
* USB control transfer request type enumeration.
*/
-public enum USBRequestType {
+public enum UsbRequestType {
/**
* Standard request type
*/
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBStallException.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbStallException.java
similarity index 73%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBStallException.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbStallException.java
index 2b986a2..b6b7ed4 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBStallException.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbStallException.java
@@ -7,24 +7,26 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+
/**
* Exception thrown if a communication on a USB endpoint failed.
*
* If a USB endpoint stalls, it is halted and the halt condition must be cleared
- * using {@link USBDevice#clearHalt(USBDirection, int)} before communication can resume.
+ * using {@link UsbDevice#clearHalt(UsbDirection, int)} before communication can resume.
*
*
* If the control endpoint 0 stalls, it throws this exception but is not halted.
*
*/
-public class USBStallException extends USBException {
+public class UsbStallException extends UsbException {
/**
* Creates a new instance with a message.
*
* @param message the message
*/
- public USBStallException(String message) {
+ public UsbStallException(@NotNull String message) {
super(message);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBTimeoutException.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbTimeoutException.java
similarity index 69%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBTimeoutException.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbTimeoutException.java
index 3b18558..28ae2f7 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBTimeoutException.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbTimeoutException.java
@@ -7,17 +7,19 @@
package net.codecrete.usb;
+import org.jetbrains.annotations.NotNull;
+
/**
* Exception thrown if a USB operation times out.
*/
-public class USBTimeoutException extends USBException {
+public class UsbTimeoutException extends UsbException {
/**
* Creates a new instance with a message.
*
* @param message the message
*/
- public USBTimeoutException(String message) {
+ public UsbTimeoutException(@NotNull String message) {
super(message);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/USBTransferType.java b/java-does-usb/src/main/java/net/codecrete/usb/UsbTransferType.java
similarity index 93%
rename from java-does-usb/src/main/java/net/codecrete/usb/USBTransferType.java
rename to java-does-usb/src/main/java/net/codecrete/usb/UsbTransferType.java
index bfeaa0c..f08a2e4 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/USBTransferType.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/UsbTransferType.java
@@ -10,7 +10,7 @@
/**
* USB endpoint transfer type enumeration.
*/
-public enum USBTransferType {
+public enum UsbTransferType {
/**
* Control transfer
*/
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/Version.java b/java-does-usb/src/main/java/net/codecrete/usb/Version.java
index 106d7b7..f9722ef 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/Version.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/Version.java
@@ -34,7 +34,7 @@ public Version(int bcdVersion) {
*
* @return major version
*/
- public int major() {
+ public int getMajor() {
return bcdVersion >> 8;
}
@@ -43,7 +43,7 @@ public int major() {
*
* @return minor version
*/
- public int minor() {
+ public int getMinor() {
return (bcdVersion >> 4) & 0x0f;
}
@@ -52,13 +52,13 @@ public int minor() {
*
* @return subminor version
*/
- public int subminor() {
+ public int getSubminor() {
return bcdVersion & 0x0f;
}
@Override
public String toString() {
- return String.format("%d.%d.%d", major(), minor(), subminor());
+ return String.format("%d.%d.%d", getMajor(), getMinor(), getSubminor());
}
@Override
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/Configuration.java b/java-does-usb/src/main/java/net/codecrete/usb/common/Configuration.java
index e1ad699..87c54a7 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/Configuration.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/Configuration.java
@@ -7,7 +7,7 @@
package net.codecrete.usb.common;
-import net.codecrete.usb.USBInterface;
+import net.codecrete.usb.UsbInterface;
import java.util.ArrayList;
import java.util.List;
@@ -17,7 +17,7 @@
*/
public class Configuration {
private final List functionList;
- private final List interfaceList;
+ private final List interfaceList;
private final int configurationValue;
private final int configurationAttributes;
private final int configurationMaxPower;
@@ -42,7 +42,7 @@ public int maxPower() {
return configurationMaxPower;
}
- public List interfaces() {
+ public List interfaces() {
return interfaceList;
}
@@ -50,12 +50,12 @@ public List functions() {
return functionList;
}
- public void addInterface(USBInterface intf) {
+ public void addInterface(UsbInterface intf) {
interfaceList.add(intf);
}
- public USBInterfaceImpl findInterfaceByNumber(int number) {
- return (USBInterfaceImpl) interfaceList.stream().filter(intf -> intf.number() == number).findFirst().orElse(null);
+ public UsbInterfaceImpl findInterfaceByNumber(int number) {
+ return (UsbInterfaceImpl) interfaceList.stream().filter(intf -> intf.getNumber() == number).findFirst().orElse(null);
}
public void addFunction(CompositeFunction function) {
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/ConfigurationParser.java b/java-does-usb/src/main/java/net/codecrete/usb/common/ConfigurationParser.java
index 7df741f..31b8298 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/ConfigurationParser.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/ConfigurationParser.java
@@ -7,10 +7,10 @@
package net.codecrete.usb.common;
-import net.codecrete.usb.USBAlternateInterface;
-import net.codecrete.usb.USBDirection;
-import net.codecrete.usb.USBException;
-import net.codecrete.usb.USBTransferType;
+import net.codecrete.usb.UsbAlternateInterface;
+import net.codecrete.usb.UsbDirection;
+import net.codecrete.usb.UsbException;
+import net.codecrete.usb.UsbTransferType;
import net.codecrete.usb.usbstandard.ConfigurationDescriptor;
import net.codecrete.usb.usbstandard.EndpointDescriptor;
import net.codecrete.usb.usbstandard.InterfaceAssociationDescriptor;
@@ -58,7 +58,7 @@ public ConfigurationParser(MemorySegment descriptor) {
public Configuration parse() {
parseHeader();
- USBAlternateInterfaceImpl lastAlternate = null;
+ UsbAlternateInterfaceImpl lastAlternate = null;
var offset = peekDescLength(0);
while (offset < descriptor.byteSize()) {
@@ -69,18 +69,18 @@ public Configuration parse() {
if (descType == INTERFACE_DESCRIPTOR_TYPE) {
var intf = parseInterface(offset);
- var parent = configuration.findInterfaceByNumber(intf.number());
+ var parent = configuration.findInterfaceByNumber(intf.getNumber());
if (parent != null) {
- parent.addAlternate(intf.alternate());
+ parent.addAlternate(intf.getCurrentAlternate());
} else {
configuration.addInterface(intf);
}
- lastAlternate = (USBAlternateInterfaceImpl) intf.alternate();
+ lastAlternate = (UsbAlternateInterfaceImpl) intf.getCurrentAlternate();
- var function = configuration.findFunction(intf.number());
+ var function = configuration.findFunction(intf.getNumber());
if (function == null) {
- function = new CompositeFunction(intf.number(), 1, lastAlternate.classCode(),
- lastAlternate.subclassCode(), lastAlternate.protocolCode());
+ function = new CompositeFunction(intf.getNumber(), 1, lastAlternate.getClassCode(),
+ lastAlternate.getSubclassCode(), lastAlternate.getProtocolCode());
configuration.addFunction(function);
}
@@ -102,22 +102,22 @@ public Configuration parse() {
private void parseHeader() {
var desc = new ConfigurationDescriptor(descriptor);
if (CONFIGURATION_DESCRIPTOR_TYPE != desc.descriptorType())
- throw new USBException("invalid USB configuration descriptor");
+ throw new UsbException("invalid USB configuration descriptor");
var totalLength = desc.totalLength();
if (descriptor.byteSize() != totalLength)
- throw new USBException("invalid USB configuration descriptor (invalid length)");
+ throw new UsbException("invalid USB configuration descriptor (invalid length)");
configuration = new Configuration(desc.configurationValue(), desc.attributes(), desc.maxPower());
}
- private USBInterfaceImpl parseInterface(int offset) {
+ private UsbInterfaceImpl parseInterface(int offset) {
var desc = new InterfaceDescriptor(descriptor, offset);
- var alternate = new USBAlternateInterfaceImpl(desc.alternateSetting(), desc.interfaceClass(),
+ var alternate = new UsbAlternateInterfaceImpl(desc.alternateSetting(), desc.interfaceClass(),
desc.interfaceSubClass(), desc.interfaceProtocol(), new ArrayList<>());
- var alternates = new ArrayList();
+ var alternates = new ArrayList();
alternates.add(alternate);
- return new USBInterfaceImpl(desc.interfaceNumber(), alternates);
+ return new UsbInterfaceImpl(desc.interfaceNumber(), alternates);
}
private void parseIAD(int offset) {
@@ -127,26 +127,26 @@ private void parseIAD(int offset) {
configuration.addFunction(function);
}
- private USBEndpointImpl parseEndpoint(int offset) {
+ private UsbEndpointImpl parseEndpoint(int offset) {
var desc = new EndpointDescriptor(descriptor, offset);
var address = desc.endpointAddress();
- return new USBEndpointImpl(getEndpointNumber(address), getEndpointDirection(address),
+ return new UsbEndpointImpl(getEndpointNumber(address), getEndpointDirection(address),
getEndpointType(desc.attributes()), desc.maxPacketSize());
}
- private static USBDirection getEndpointDirection(int address) {
- return (address & 0x80) != 0 ? USBDirection.IN : USBDirection.OUT;
+ private static UsbDirection getEndpointDirection(int address) {
+ return (address & 0x80) != 0 ? UsbDirection.IN : UsbDirection.OUT;
}
private static int getEndpointNumber(int address) {
return address & 0x7f;
}
- private static USBTransferType getEndpointType(int attributes) {
+ private static UsbTransferType getEndpointType(int attributes) {
return switch (attributes & 0x3) {
- case 1 -> USBTransferType.ISOCHRONOUS;
- case 2 -> USBTransferType.BULK;
- case 3 -> USBTransferType.INTERRUPT;
+ case 1 -> UsbTransferType.ISOCHRONOUS;
+ case 2 -> UsbTransferType.BULK;
+ case 3 -> UsbTransferType.INTERRUPT;
default -> null;
};
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java
index 7e00f1f..5db3fb4 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointInputStream.java
@@ -7,8 +7,9 @@
package net.codecrete.usb.common;
-import net.codecrete.usb.USBDirection;
-import net.codecrete.usb.USBException;
+import net.codecrete.usb.UsbDirection;
+import net.codecrete.usb.UsbException;
+import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
@@ -35,7 +36,7 @@
*/
public abstract class EndpointInputStream extends InputStream {
- protected USBDeviceImpl device;
+ protected UsbDeviceImpl device;
protected final int endpointNumber;
// Arena to allocate buffers and completion handlers
protected final Arena arena;
@@ -58,12 +59,12 @@ public abstract class EndpointInputStream extends InputStream {
* @param endpointNumber endpoint number
* @param bufferSize approximate buffer size (in bytes)
*/
- protected EndpointInputStream(USBDeviceImpl device, int endpointNumber, int bufferSize) {
+ protected EndpointInputStream(UsbDeviceImpl device, int endpointNumber, int bufferSize) {
this.device = device;
this.endpointNumber = endpointNumber;
arena = Arena.ofShared();
- var packetSize = device.getEndpoint(USBDirection.IN, endpointNumber).packetSize();
+ var packetSize = device.getEndpoint(UsbDirection.IN, endpointNumber).getPacketSize();
// use between 4 and 32 packets per transfer (256B to 2KB for FS, 2KB to 16KB for HS)
var numPacketsPerTransfer = (int) Math.round(Math.sqrt((double) bufferSize / packetSize));
@@ -109,9 +110,9 @@ public void close() throws IOException {
// abort all transfers on endpoint
try {
- device.abortTransfers(USBDirection.IN, endpointNumber);
+ device.abortTransfers(UsbDirection.IN, endpointNumber);
- } catch (USBException e) {
+ } catch (UsbException e) {
// If aborting the transfer is not possible, the device has
// likely been closed or unplugged. So all outstanding
// transfers will terminate anyway.
@@ -135,7 +136,7 @@ public int read() throws IOException {
}
@Override
- public int read(byte[] b, int off, int len) throws IOException {
+ public int read(byte @NotNull [] b, int off, int len) throws IOException {
if (isClosed())
return -1;
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java
index 0a8c7f8..3fb6dc8 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/EndpointOutputStream.java
@@ -7,7 +7,8 @@
package net.codecrete.usb.common;
-import net.codecrete.usb.USBDirection;
+import net.codecrete.usb.UsbDirection;
+import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.OutputStream;
@@ -37,7 +38,7 @@
*/
public abstract class EndpointOutputStream extends OutputStream {
- protected USBDeviceImpl device;
+ protected UsbDeviceImpl device;
protected final int endpointNumber;
protected final Arena arena;
// Endpoint packet size
@@ -60,12 +61,12 @@ public abstract class EndpointOutputStream extends OutputStream {
* @param endpointNumber endpoint number
* @param bufferSize approximate buffer size (in bytes)
*/
- protected EndpointOutputStream(USBDeviceImpl device, int endpointNumber, int bufferSize) {
+ protected EndpointOutputStream(UsbDeviceImpl device, int endpointNumber, int bufferSize) {
this.device = device;
this.endpointNumber = endpointNumber;
arena = Arena.ofShared();
- packetSize = device.getEndpoint(USBDirection.OUT, endpointNumber).packetSize();
+ packetSize = device.getEndpoint(UsbDirection.OUT, endpointNumber).getPacketSize();
// use between 4 and 32 packets per transfer (256B to 2KB for FS, 2KB to 16KB for HS)
var numPacketsPerTransfer = (int) Math.round(Math.sqrt((double) bufferSize / packetSize));
@@ -124,7 +125,7 @@ public void write(int b) throws IOException {
}
@Override
- public void write(byte[] b, int off, int len) throws IOException {
+ public void write(byte @NotNull [] b, int off, int len) throws IOException {
checkIsOpen();
while (len > 0) {
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/USBAlternateInterfaceImpl.java b/java-does-usb/src/main/java/net/codecrete/usb/common/USBAlternateInterfaceImpl.java
deleted file mode 100644
index 401ba45..0000000
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/USBAlternateInterfaceImpl.java
+++ /dev/null
@@ -1,68 +0,0 @@
-//
-// Java Does USB
-// Copyright (c) 2022 Manuel Bleichenbacher
-// Licensed under MIT License
-// https://opensource.org/licenses/MIT
-//
-
-package net.codecrete.usb.common;
-
-import net.codecrete.usb.USBAlternateInterface;
-import net.codecrete.usb.USBDirection;
-import net.codecrete.usb.USBEndpoint;
-
-import java.util.List;
-
-import static java.util.Collections.unmodifiableList;
-
-public class USBAlternateInterfaceImpl implements USBAlternateInterface {
-
- private final int alternateInterfaceNumber;
- private final int alternateInterfaceClass;
- private final int alternateInterfaceSubclass;
- private final int alternateInterfaceProtocol;
- private final List endpointList;
-
- public USBAlternateInterfaceImpl(int number, int classCode, int subclassCode, int protocolCode,
- List endpoints) {
- alternateInterfaceNumber = number;
- alternateInterfaceClass = classCode;
- alternateInterfaceSubclass = subclassCode;
- alternateInterfaceProtocol = protocolCode;
- endpointList = endpoints;
- }
-
- @Override
- public int number() {
- return alternateInterfaceNumber;
- }
-
- @Override
- public int classCode() {
- return alternateInterfaceClass;
- }
-
- @Override
- public int subclassCode() {
- return alternateInterfaceSubclass;
- }
-
- @Override
- public int protocolCode() {
- return alternateInterfaceProtocol;
- }
-
- @Override
- public List endpoints() {
- return unmodifiableList(endpointList);
- }
-
- void addEndpoint(USBEndpoint endpoint) {
- endpointList.add(endpoint);
- }
-
- @Override
- public USBEndpoint getEndpoint(int endpointNumber, USBDirection direction) {
- return endpointList.stream().filter(ep -> ep.number() == endpointNumber && ep.direction() == direction).findFirst().orElse(null);
- }
-}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/USBInterfaceImpl.java b/java-does-usb/src/main/java/net/codecrete/usb/common/USBInterfaceImpl.java
deleted file mode 100644
index 0723466..0000000
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/USBInterfaceImpl.java
+++ /dev/null
@@ -1,66 +0,0 @@
-//
-// Java Does USB
-// Copyright (c) 2022 Manuel Bleichenbacher
-// Licensed under MIT License
-// https://opensource.org/licenses/MIT
-//
-
-package net.codecrete.usb.common;
-
-import net.codecrete.usb.USBAlternateInterface;
-import net.codecrete.usb.USBInterface;
-
-import java.util.Collections;
-import java.util.List;
-
-public class USBInterfaceImpl implements USBInterface {
-
- private final int interfaceNumber;
- private USBAlternateInterface currentAlternate;
- private final List alternateInterfaces;
-
- private boolean claimed;
-
- public USBInterfaceImpl(int number, List alternates) {
- interfaceNumber = number;
- alternateInterfaces = alternates;
- currentAlternate = alternates.get(0);
- }
-
- @Override
- public int number() {
- return interfaceNumber;
- }
-
- @Override
- public boolean isClaimed() {
- return claimed;
- }
-
- public void setClaimed(boolean claimed) {
- this.claimed = claimed;
- }
-
- @Override
- public USBAlternateInterface alternate() {
- return currentAlternate;
- }
-
- @Override
- public USBAlternateInterface getAlternate(int alternateNumber) {
- return alternateInterfaces.stream().filter(alt -> alt.number() == alternateNumber).findFirst().orElse(null);
- }
-
- @Override
- public List alternates() {
- return Collections.unmodifiableList(alternateInterfaces);
- }
-
- void addAlternate(USBAlternateInterface alt) {
- alternateInterfaces.add(alt);
- }
-
- public void setAlternate(USBAlternateInterface alternate) {
- currentAlternate = alternate;
- }
-}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/UsbAlternateInterfaceImpl.java b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbAlternateInterfaceImpl.java
new file mode 100644
index 0000000..9a7d70b
--- /dev/null
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbAlternateInterfaceImpl.java
@@ -0,0 +1,74 @@
+//
+// Java Does USB
+// Copyright (c) 2022 Manuel Bleichenbacher
+// Licensed under MIT License
+// https://opensource.org/licenses/MIT
+//
+
+package net.codecrete.usb.common;
+
+import net.codecrete.usb.UsbAlternateInterface;
+import net.codecrete.usb.UsbDirection;
+import net.codecrete.usb.UsbEndpoint;
+import net.codecrete.usb.UsbException;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Comparator;
+import java.util.List;
+
+import static java.util.Collections.unmodifiableList;
+
+public class UsbAlternateInterfaceImpl implements UsbAlternateInterface {
+
+ private final int alternateInterfaceNumber;
+ private final int alternateInterfaceClass;
+ private final int alternateInterfaceSubclass;
+ private final int alternateInterfaceProtocol;
+ private final List endpointList;
+
+ public UsbAlternateInterfaceImpl(int number, int classCode, int subclassCode, int protocolCode,
+ List endpoints) {
+ alternateInterfaceNumber = number;
+ alternateInterfaceClass = classCode;
+ alternateInterfaceSubclass = subclassCode;
+ alternateInterfaceProtocol = protocolCode;
+ endpointList = endpoints;
+ endpointList.sort(Comparator.comparingInt(UsbEndpoint::getNumber));
+ }
+
+ @Override
+ public int getNumber() {
+ return alternateInterfaceNumber;
+ }
+
+ @Override
+ public int getClassCode() {
+ return alternateInterfaceClass;
+ }
+
+ @Override
+ public int getSubclassCode() {
+ return alternateInterfaceSubclass;
+ }
+
+ @Override
+ public int getProtocolCode() {
+ return alternateInterfaceProtocol;
+ }
+
+ @Override
+ public @NotNull List getEndpoints() {
+ return unmodifiableList(endpointList);
+ }
+
+ void addEndpoint(UsbEndpoint endpoint) {
+ endpointList.add(endpoint);
+ }
+
+ @Override
+ public @NotNull UsbEndpoint getEndpoint(int endpointNumber, UsbDirection direction) {
+ return endpointList.stream()
+ .filter(ep -> ep.getNumber() == endpointNumber && ep.getDirection() == direction).findFirst()
+ .orElseThrow(() -> new UsbException(String.format("Endpoint %d (%s) does not exist", endpointNumber, direction)));
+ }
+}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceImpl.java b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbDeviceImpl.java
similarity index 74%
rename from java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceImpl.java
rename to java-does-usb/src/main/java/net/codecrete/usb/common/UsbDeviceImpl.java
index 6db36ed..e17d566 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceImpl.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbDeviceImpl.java
@@ -9,16 +9,18 @@
import net.codecrete.usb.*;
import net.codecrete.usb.usbstandard.DeviceDescriptor;
+import org.jetbrains.annotations.NotNull;
import java.lang.foreign.MemorySegment;
import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import java.util.function.IntFunction;
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
-public abstract class USBDeviceImpl implements USBDevice {
+public abstract class UsbDeviceImpl implements UsbDevice {
/**
* Operating system-specific device ID used for {@link #equals(Object)} and {@link #hashCode()}.
@@ -29,7 +31,7 @@ public abstract class USBDeviceImpl implements USBDevice {
*/
protected final Object uniqueDeviceId;
- protected List interfaceList;
+ protected List interfaceList;
protected byte[] rawDeviceDescriptor;
@@ -44,7 +46,7 @@ public abstract class USBDeviceImpl implements USBDevice {
protected int deviceClass;
protected int deviceSubclass;
protected int deviceProtocol;
- protected Version versionUSB;
+ protected Version versionUsb;
protected Version versionDevice;
/**
@@ -54,7 +56,7 @@ public abstract class USBDeviceImpl implements USBDevice {
* @param vendorId USB vendor ID
* @param productId USB product ID
*/
- protected USBDeviceImpl(Object id, int vendorId, int productId) {
+ protected UsbDeviceImpl(Object id, int vendorId, int productId) {
assert id != null;
@@ -65,82 +67,82 @@ protected USBDeviceImpl(Object id, int vendorId, int productId) {
@Override
public void detachStandardDrivers() {
- if (isOpen())
- throw new USBException("detachStandardDrivers() must not be called while the device is open");
+ if (isOpened())
+ throw new UsbException("detachStandardDrivers() must not be called while the device is open");
// default implementation: do nothing
}
@Override
public void attachStandardDrivers() {
- if (isOpen())
- throw new USBException("attachStandardDrivers() must not be called while the device is open");
+ if (isOpened())
+ throw new UsbException("attachStandardDrivers() must not be called while the device is open");
// default implementation: do nothing
}
protected void checkIsOpen() {
- if (!isOpen())
- throw new USBException("device needs to be opened first for this operation");
+ if (!isOpened())
+ throw new UsbException("device needs to be opened first for this operation");
}
@Override
- public int productId() {
+ public int getProductId() {
return pid;
}
@Override
- public int vendorId() {
+ public int getVendorId() {
return vid;
}
@Override
- public String product() {
+ public String getProduct() {
return productString;
}
@Override
- public String manufacturer() {
+ public String getManufacturer() {
return manufacturerString;
}
@Override
- public String serialNumber() {
+ public String getSerialNumber() {
return serialString;
}
@Override
- public int classCode() {
+ public int getClassCode() {
return deviceClass;
}
@Override
- public int subclassCode() {
+ public int getSubclassCode() {
return deviceSubclass;
}
@Override
- public int protocolCode() {
+ public int getProtocolCode() {
return deviceProtocol;
}
@Override
- public Version usbVersion() {
- return versionUSB;
+ public @NotNull Version getUsbVersion() {
+ return versionUsb;
}
@Override
- public Version deviceVersion() {
+ public @NotNull Version getDeviceVersion() {
return versionDevice;
}
@Override
- public byte[] configurationDescriptor() {
+ public byte @NotNull [] getConfigurationDescriptor() {
return rawConfigurationDescriptor;
}
@Override
- public byte[] deviceDescriptor() {
+ public byte @NotNull [] getDeviceDescriptor() {
return rawDeviceDescriptor;
}
@@ -159,7 +161,7 @@ public void setFromDeviceDescriptor(MemorySegment descriptor) {
deviceClass = deviceDescriptor.deviceClass() & 255;
deviceSubclass = deviceDescriptor.deviceSubClass() & 255;
deviceProtocol = deviceDescriptor.deviceProtocol() & 255;
- versionUSB = new Version(deviceDescriptor.usbVersion());
+ versionUsb = new Version(deviceDescriptor.usbVersion());
versionDevice = new Version(deviceDescriptor.deviceVersion());
}
@@ -173,6 +175,7 @@ protected Configuration setConfigurationDescriptor(MemorySegment descriptor) {
rawConfigurationDescriptor = descriptor.toArray(JAVA_BYTE);
var configuration = ConfigurationParser.parseConfigurationDescriptor(descriptor);
interfaceList = configuration.interfaces();
+ interfaceList.sort(Comparator.comparingInt(UsbInterface::getNumber));
return configuration;
}
@@ -213,51 +216,51 @@ public void setClassCodes(int classCode, int subclassCode, int protocolCode) {
}
public void setVersions(int usbVersion, int deviceVersion) {
- versionUSB = new Version(usbVersion);
+ versionUsb = new Version(usbVersion);
versionDevice = new Version(deviceVersion);
}
@Override
- public List interfaces() {
+ public @NotNull List getInterfaces() {
return Collections.unmodifiableList(interfaceList);
}
public void setClaimed(int interfaceNumber, boolean claimed) {
for (var intf : interfaceList) {
- if (intf.number() == interfaceNumber) {
- ((USBInterfaceImpl) intf).setClaimed(claimed);
+ if (intf.getNumber() == interfaceNumber) {
+ ((UsbInterfaceImpl) intf).setClaimed(claimed);
return;
}
}
- throw new USBException("internal error (interface not found)");
+ throw new UsbException("internal error (interface not found)");
}
@Override
- public USBInterfaceImpl getInterface(int interfaceNumber) {
- return (USBInterfaceImpl) interfaceList.stream().filter(intf -> intf.number() == interfaceNumber).findFirst().orElse(null);
+ public @NotNull UsbInterfaceImpl getInterface(int interfaceNumber) {
+ return (UsbInterfaceImpl) interfaceList.stream()
+ .filter(intf -> intf.getNumber() == interfaceNumber).findFirst()
+ .orElseThrow(() -> new UsbException(String.format("USB device has no interface %d", interfaceNumber)));
}
- public USBInterfaceImpl getInterfaceWithCheck(int interfaceNumber, boolean isClaimed) {
+ public UsbInterfaceImpl getInterfaceWithCheck(int interfaceNumber, boolean isClaimed) {
var intf = getInterface(interfaceNumber);
- if (intf == null)
- throw new USBException(String.format("invalid interface number: %d", interfaceNumber));
if (isClaimed && !intf.isClaimed()) {
- throw new USBException(String.format("interface %d must be claimed first", interfaceNumber));
+ throw new UsbException(String.format("interface %d must be claimed first", interfaceNumber));
} else if (!isClaimed && intf.isClaimed()) {
- throw new USBException(String.format("interface %d has already been claimed", interfaceNumber));
+ throw new UsbException(String.format("interface %d has already been claimed", interfaceNumber));
}
return intf;
}
@Override
- public USBEndpoint getEndpoint(USBDirection direction, int endpointNumber) {
+ public @NotNull UsbEndpoint getEndpoint(UsbDirection direction, int endpointNumber) {
for (var intf : interfaceList) {
- for (var endpoint : intf.alternate().endpoints()) {
- if (endpoint.direction() == direction && endpoint.number() == endpointNumber)
+ for (var endpoint : intf.getCurrentAlternate().getEndpoints()) {
+ if (endpoint.getDirection() == direction && endpoint.getNumber() == endpointNumber)
return endpoint;
}
}
- return null;
+ throw new UsbException(String.format("endpoint %d (%s) does not exist", endpointNumber, direction.name()));
}
/**
@@ -270,20 +273,20 @@ public USBEndpoint getEndpoint(USBDirection direction, int endpointNumber) {
* @return endpoint
*/
@SuppressWarnings("java:S3776")
- protected EndpointInfo getEndpoint(USBDirection direction, int endpointNumber, USBTransferType transferType1,
- USBTransferType transferType2) {
+ protected EndpointInfo getEndpoint(UsbDirection direction, int endpointNumber, UsbTransferType transferType1,
+ UsbTransferType transferType2) {
checkIsOpen();
if (endpointNumber >= 1 && endpointNumber <= 127) {
for (var intf : interfaceList) {
if (intf.isClaimed()) {
- for (var ep : intf.alternate().endpoints()) {
- if (ep.number() == endpointNumber && ep.direction() == direction
- && (ep.transferType() == transferType1 || ep.transferType() == transferType2))
- return new EndpointInfo(intf.number(), ep.number(),
- (byte) (endpointNumber | (direction == USBDirection.IN ? 0x80 : 0)),
- ep.packetSize(), ep.transferType());
+ for (var ep : intf.getCurrentAlternate().getEndpoints()) {
+ if (ep.getNumber() == endpointNumber && ep.getDirection() == direction
+ && (ep.getTransferType() == transferType1 || ep.getTransferType() == transferType2))
+ return new EndpointInfo(intf.getNumber(), ep.getNumber(),
+ (byte) (endpointNumber | (direction == UsbDirection.IN ? 0x80 : 0)),
+ ep.getPacketSize(), ep.getTransferType());
}
}
}
@@ -293,27 +296,27 @@ protected EndpointInfo getEndpoint(USBDirection direction, int endpointNumber, U
return null; // will never be reached
}
- protected void throwInvalidEndpointException(USBDirection direction, int endpointNumber,
- USBTransferType transferType1, USBTransferType transferType2) {
+ protected void throwInvalidEndpointException(UsbDirection direction, int endpointNumber,
+ UsbTransferType transferType1, UsbTransferType transferType2) {
String transferTypeDesc;
if (transferType2 == null)
transferTypeDesc = transferType1.name();
else
transferTypeDesc = String.format("%s or %s", transferType1.name(), transferType2.name());
- throw new USBException(String.format(
+ throw new UsbException(String.format(
"endpoint number %d does not exist, is not part of a claimed interface or is not valid for %s transfer in %s direction",
endpointNumber, transferTypeDesc, direction.name()));
}
- protected int getInterfaceNumber(USBDirection direction, int endpointNumber) {
+ protected int getInterfaceNumber(UsbDirection direction, int endpointNumber) {
if (endpointNumber < 1 || endpointNumber > 127)
return -1;
for (var intf : interfaceList) {
if (intf.isClaimed()) {
- for (var ep : intf.alternate().endpoints()) {
- if (ep.number() == endpointNumber && ep.direction() == direction)
- return intf.number();
+ for (var ep : intf.getCurrentAlternate().getEndpoints()) {
+ if (ep.getNumber() == endpointNumber && ep.getDirection() == direction)
+ return intf.getNumber();
}
}
}
@@ -322,21 +325,21 @@ protected int getInterfaceNumber(USBDirection direction, int endpointNumber) {
}
@Override
- public void transferOut(int endpointNumber, byte[] data) {
+ public void transferOut(int endpointNumber, byte @NotNull [] data) {
transferOut(endpointNumber, data, 0, data.length, 0);
}
@Override
- public void transferOut(int endpointNumber, byte[] data, int timeout) {
+ public void transferOut(int endpointNumber, byte @NotNull [] data, int timeout) {
transferOut(endpointNumber, data, 0, data.length, timeout);
}
@Override
- public byte[] transferIn(int endpointNumber) {
+ public byte @NotNull [] transferIn(int endpointNumber) {
return transferIn(endpointNumber, 0);
}
- protected void waitForTransfer(Transfer transfer, int timeout, USBDirection direction, int endpointNumber) {
+ protected void waitForTransfer(Transfer transfer, int timeout, UsbDirection direction, int endpointNumber) {
if (timeout <= 0) {
waitNoTimeout(transfer);
@@ -347,7 +350,7 @@ protected void waitForTransfer(Transfer transfer, int timeout, USBDirection dire
if (hasTimedOut && transfer.resultCode() == 0) {
abortTransfers(direction, endpointNumber);
waitNoTimeout(transfer);
- throw new USBTimeoutException(getOperationDescription(direction, endpointNumber)
+ throw new UsbTimeoutException(getOperationDescription(direction, endpointNumber)
+ "aborted due to timeout");
}
}
@@ -389,7 +392,7 @@ private static boolean waitWithTimeout(Transfer transfer, int timeout) {
return remainingTimeout <= 0;
}
- protected static String getOperationDescription(USBDirection direction, int endpointNumber) {
+ protected static String getOperationDescription(UsbDirection direction, int endpointNumber) {
if (endpointNumber == 0) {
return "control transfer";
} else {
@@ -436,7 +439,7 @@ public boolean equals(Object o) {
return true;
if (o == null || getClass() != o.getClass())
return false;
- var that = (USBDeviceImpl) o;
+ var that = (UsbDeviceImpl) o;
return uniqueDeviceId.equals(that.uniqueDeviceId);
}
@@ -451,6 +454,6 @@ public String toString() {
}
public record EndpointInfo(int interfaceNumber, int endpointNumber, byte endpointAddress, int packetSize,
- USBTransferType transferType) {
+ UsbTransferType transferType) {
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceRegistry.java b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbDeviceRegistry.java
similarity index 83%
rename from java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceRegistry.java
rename to java-does-usb/src/main/java/net/codecrete/usb/common/UsbDeviceRegistry.java
index b3224df..72268f1 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceRegistry.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbDeviceRegistry.java
@@ -7,11 +7,10 @@
package net.codecrete.usb.common;
-import net.codecrete.usb.USBDevice;
-import net.codecrete.usb.USBException;
+import net.codecrete.usb.UsbDevice;
+import net.codecrete.usb.UsbException;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
@@ -33,14 +32,14 @@
* and builds the initial device list.
*
*/
-public abstract class USBDeviceRegistry {
+public abstract class UsbDeviceRegistry {
- private static final System.Logger LOG = System.getLogger(USBDeviceRegistry.class.getName());
+ private static final System.Logger LOG = System.getLogger(UsbDeviceRegistry.class.getName());
- private List devices;
+ private List devices;
private Throwable failureCause;
- protected Consumer onDeviceConnectedHandler;
- protected Consumer onDeviceDisconnectedHandler;
+ protected Consumer onDeviceConnectedHandler;
+ protected Consumer onDeviceDisconnectedHandler;
private final Lock lock = new ReentrantLock();
private final Condition enumerationComplete = lock.newCondition();
@@ -74,19 +73,19 @@ public void start() {
*
* @return list of devices
*/
- public synchronized List getAllDevices() {
- return Collections.unmodifiableList(devices);
+ public synchronized List getAllDevices() {
+ return devices;
}
- public void setOnDeviceConnected(Consumer handler) {
+ public void setOnDeviceConnected(Consumer handler) {
onDeviceConnectedHandler = handler;
}
- public void setOnDeviceDisconnected(Consumer handler) {
+ public void setOnDeviceDisconnected(Consumer handler) {
onDeviceDisconnectedHandler = handler;
}
- protected void emitOnDeviceConnected(USBDevice device) {
+ protected void emitOnDeviceConnected(UsbDevice device) {
if (onDeviceConnectedHandler == null)
return;
@@ -98,7 +97,7 @@ protected void emitOnDeviceConnected(USBDevice device) {
}
}
- protected void emitOnDeviceDisconnected(USBDevice device) {
+ protected void emitOnDeviceDisconnected(UsbDevice device) {
if (onDeviceDisconnectedHandler == null)
return;
@@ -136,7 +135,7 @@ protected void startDeviceMonitor(Runnable monitorTask) {
}
if (failureCause != null)
- throw new USBException("initial device enumeration has failed", failureCause);
+ throw new UsbException("initial device enumeration has failed", failureCause);
}
/**
@@ -169,7 +168,7 @@ protected void enumerationFailed(Throwable e) {
*
* @param deviceList the device list
*/
- protected void setInitialDeviceList(List deviceList) {
+ protected void setInitialDeviceList(List deviceList) {
synchronized (this) {
devices = deviceList;
}
@@ -181,14 +180,14 @@ protected void setInitialDeviceList(List deviceList) {
*
* @param device device to add
*/
- protected void addDevice(USBDevice device) {
+ protected void addDevice(UsbDevice device) {
synchronized (this) {
// check for duplicates
- if (findDeviceIndex(devices, ((USBDeviceImpl) device).getUniqueId()) >= 0)
+ if (findDeviceIndex(devices, ((UsbDeviceImpl) device).getUniqueId()) >= 0)
return;
// copy list
- var newDeviceList = new ArrayList(devices.size() + 1);
+ var newDeviceList = new ArrayList(devices.size() + 1);
newDeviceList.addAll(devices);
newDeviceList.add(device);
devices = newDeviceList;
@@ -219,7 +218,7 @@ protected void closeAndRemoveDevice(Object deviceId) {
* @param deviceId the unique ID of the device to remove
*/
protected void removeDevice(Object deviceId) {
- USBDevice device;
+ UsbDevice device;
synchronized (this) {
// locate device to be removed
int index = findDeviceIndex(devices, deviceId);
@@ -244,9 +243,9 @@ protected void removeDevice(Object deviceId) {
* @param deviceId the unique device ID
* @return return the index, or -1 if the device is not found
*/
- protected int findDeviceIndex(List deviceList, Object deviceId) {
+ protected int findDeviceIndex(List deviceList, Object deviceId) {
for (int i = 0; i < deviceList.size(); i++) {
- var dev = (USBDeviceImpl) deviceList.get(i);
+ var dev = (UsbDeviceImpl) deviceList.get(i);
if (deviceId.equals(dev.getUniqueId()))
return i;
}
@@ -259,7 +258,7 @@ protected int findDeviceIndex(List deviceList, Object deviceId) {
* @param deviceId the unique device ID
* @return return device, or {@code null} if not found.
*/
- protected USBDevice findDevice(Object deviceId) {
+ protected UsbDevice findDevice(Object deviceId) {
int index = findDeviceIndex(devices, deviceId);
if (index < 0)
return null;
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/USBEndpointImpl.java b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbEndpointImpl.java
similarity index 51%
rename from java-does-usb/src/main/java/net/codecrete/usb/common/USBEndpointImpl.java
rename to java-does-usb/src/main/java/net/codecrete/usb/common/UsbEndpointImpl.java
index f896b0f..f40525e 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/common/USBEndpointImpl.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbEndpointImpl.java
@@ -7,21 +7,21 @@
package net.codecrete.usb.common;
-import net.codecrete.usb.USBDirection;
-import net.codecrete.usb.USBEndpoint;
-import net.codecrete.usb.USBTransferType;
+import net.codecrete.usb.UsbDirection;
+import net.codecrete.usb.UsbEndpoint;
+import net.codecrete.usb.UsbTransferType;
/**
- * Implementation of {@code USBEndpoint} interface.
+ * Implementation of {@code UsbEndpoint} interface.
*/
-public class USBEndpointImpl implements USBEndpoint {
+public class UsbEndpointImpl implements UsbEndpoint {
private final int endpointNumber;
- private final USBDirection transferDirection;
- private final USBTransferType type;
+ private final UsbDirection transferDirection;
+ private final UsbTransferType type;
private final int maxPacketSize;
- public USBEndpointImpl(int number, USBDirection direction, USBTransferType type, int packetSize) {
+ public UsbEndpointImpl(int number, UsbDirection direction, UsbTransferType type, int packetSize) {
endpointNumber = number;
transferDirection = direction;
this.type = type;
@@ -29,22 +29,22 @@ public USBEndpointImpl(int number, USBDirection direction, USBTransferType type,
}
@Override
- public int number() {
+ public int getNumber() {
return endpointNumber;
}
@Override
- public USBDirection direction() {
+ public UsbDirection getDirection() {
return transferDirection;
}
@Override
- public USBTransferType transferType() {
+ public UsbTransferType getTransferType() {
return type;
}
@Override
- public int packetSize() {
+ public int getPacketSize() {
return maxPacketSize;
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/common/UsbInterfaceImpl.java b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbInterfaceImpl.java
new file mode 100644
index 0000000..8f6ea08
--- /dev/null
+++ b/java-does-usb/src/main/java/net/codecrete/usb/common/UsbInterfaceImpl.java
@@ -0,0 +1,75 @@
+//
+// Java Does USB
+// Copyright (c) 2022 Manuel Bleichenbacher
+// Licensed under MIT License
+// https://opensource.org/licenses/MIT
+//
+
+package net.codecrete.usb.common;
+
+import net.codecrete.usb.UsbAlternateInterface;
+import net.codecrete.usb.UsbException;
+import net.codecrete.usb.UsbInterface;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.List;
+
+public class UsbInterfaceImpl implements UsbInterface {
+
+ private final int interfaceNumber;
+ private UsbAlternateInterface currentAlternate;
+ private final List alternateInterfaces;
+
+ private boolean claimed;
+
+ public UsbInterfaceImpl(int number, List alternates) {
+ interfaceNumber = number;
+ alternateInterfaces = alternates;
+ currentAlternate = alternates.get(0);
+ alternateInterfaces.sort(Comparator.comparingInt(UsbAlternateInterface::getNumber));
+ }
+
+ @Override
+ public int getNumber() {
+ return interfaceNumber;
+ }
+
+ @Override
+ public boolean isClaimed() {
+ return claimed;
+ }
+
+ public void setClaimed(boolean claimed) {
+ this.claimed = claimed;
+ }
+
+ @Override
+ public @NotNull UsbAlternateInterface getCurrentAlternate() {
+ return currentAlternate;
+ }
+
+ @Override
+ public @NotNull UsbAlternateInterface getAlternate(int alternateNumber) {
+ return alternateInterfaces.stream()
+ .filter(alt -> alt.getNumber() == alternateNumber).findFirst()
+ .orElseThrow(() -> new UsbException(String.format(
+ "Interface %d does not have an alternate interface setting %d",
+ interfaceNumber, alternateNumber)
+ ));
+ }
+
+ @Override
+ public @NotNull List getAlternates() {
+ return Collections.unmodifiableList(alternateInterfaces);
+ }
+
+ void addAlternate(UsbAlternateInterface alt) {
+ alternateInterfaces.add(alt);
+ }
+
+ public void setAlternate(UsbAlternateInterface alternate) {
+ currentAlternate = alternate;
+ }
+}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxAsyncTask.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxAsyncTask.java
index 19d0149..524fe81 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxAsyncTask.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxAsyncTask.java
@@ -7,7 +7,7 @@
package net.codecrete.usb.linux;
-import net.codecrete.usb.USBTransferType;
+import net.codecrete.usb.UsbTransferType;
import net.codecrete.usb.linux.gen.errno.errno;
import net.codecrete.usb.linux.gen.poll.poll;
import net.codecrete.usb.linux.gen.poll.pollfd;
@@ -24,9 +24,9 @@
import static java.lang.foreign.ValueLayout.JAVA_LONG;
import static net.codecrete.usb.common.ForeignMemory.dereference;
import static net.codecrete.usb.linux.Linux.allocateErrorState;
-import static net.codecrete.usb.linux.LinuxUSBException.throwException;
-import static net.codecrete.usb.linux.LinuxUSBException.throwLastError;
-import static net.codecrete.usb.linux.USBDevFS.*;
+import static net.codecrete.usb.linux.LinuxUsbException.throwException;
+import static net.codecrete.usb.linux.LinuxUsbException.throwLastError;
+import static net.codecrete.usb.linux.UsbDevFS.*;
import static net.codecrete.usb.linux.gen.usbdevice_fs.usbdevice_fs.*;
/**
@@ -194,7 +194,7 @@ private void notifyAsyncIOTask() {
*
* @param device USB device
*/
- synchronized void addForAsyncIOCompletion(LinuxUSBDevice device) {
+ synchronized void addForAsyncIOCompletion(LinuxUsbDevice device) {
var n = asyncFds != null ? asyncFds.length : 0;
var fds = new int[n + 1];
if (n > 0)
@@ -211,7 +211,7 @@ synchronized void addForAsyncIOCompletion(LinuxUSBDevice device) {
*
* @param device USB device
*/
- synchronized void removeFromAsyncIOCompletion(LinuxUSBDevice device) {
+ synchronized void removeFromAsyncIOCompletion(LinuxUsbDevice device) {
removeFdFromAsyncIOCompletion(device.fileDescriptor());
notifyAsyncIOTask();
}
@@ -237,7 +237,7 @@ private synchronized void removeFdFromAsyncIOCompletion(int fd) {
asyncFds = fds;
}
- synchronized void submitTransfer(LinuxUSBDevice device, int endpointAddress, USBTransferType transferType, LinuxTransfer transfer) {
+ synchronized void submitTransfer(LinuxUsbDevice device, int endpointAddress, UsbTransferType transferType, LinuxTransfer transfer) {
addURB(transfer);
var urb = transfer.urb;
@@ -258,7 +258,7 @@ synchronized void submitTransfer(LinuxUSBDevice device, int endpointAddress, USB
}
}
- private static int urbTransferType(USBTransferType transferType) {
+ private static int urbTransferType(UsbTransferType transferType) {
return switch (transferType) {
case BULK -> USBDEVFS_URB_TYPE_BULK();
case INTERRUPT -> USBDEVFS_URB_TYPE_INTERRUPT();
@@ -295,7 +295,7 @@ private synchronized LinuxTransfer getTransferResult(MemorySegment urb) {
}
@SuppressWarnings("java:S1066")
- synchronized void abortTransfers(LinuxUSBDevice device, byte endpointAddress) {
+ synchronized void abortTransfers(LinuxUsbDevice device, byte endpointAddress) {
var fd = device.fileDescriptor();
try (var arena = Arena.ofConfined()) {
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointInputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointInputStream.java
index 9d1f7ce..2f48a0e 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointInputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointInputStream.java
@@ -7,18 +7,18 @@
package net.codecrete.usb.linux;
-import net.codecrete.usb.USBDirection;
+import net.codecrete.usb.UsbDirection;
import net.codecrete.usb.common.EndpointInputStream;
import net.codecrete.usb.common.Transfer;
public class LinuxEndpointInputStream extends EndpointInputStream {
- LinuxEndpointInputStream(LinuxUSBDevice device, int endpointNumber, int bufferSize) {
+ LinuxEndpointInputStream(LinuxUsbDevice device, int endpointNumber, int bufferSize) {
super(device, endpointNumber, bufferSize);
}
@Override
protected void submitTransferIn(Transfer transfer) {
- ((LinuxUSBDevice) device).submitTransfer(USBDirection.IN, endpointNumber, (LinuxTransfer) transfer);
+ ((LinuxUsbDevice) device).submitTransfer(UsbDirection.IN, endpointNumber, (LinuxTransfer) transfer);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointOutputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointOutputStream.java
index ab5b997..024b8da 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointOutputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxEndpointOutputStream.java
@@ -7,18 +7,18 @@
package net.codecrete.usb.linux;
-import net.codecrete.usb.USBDirection;
+import net.codecrete.usb.UsbDirection;
import net.codecrete.usb.common.EndpointOutputStream;
import net.codecrete.usb.common.Transfer;
public class LinuxEndpointOutputStream extends EndpointOutputStream {
- LinuxEndpointOutputStream(LinuxUSBDevice device, int endpointNumber, int bufferSize) {
+ LinuxEndpointOutputStream(LinuxUsbDevice device, int endpointNumber, int bufferSize) {
super(device, endpointNumber, bufferSize);
}
@Override
protected void submitTransferOut(Transfer transfer) {
- ((LinuxUSBDevice) device).submitTransfer(USBDirection.OUT, endpointNumber, (LinuxTransfer) transfer);
+ ((LinuxUsbDevice) device).submitTransfer(UsbDirection.OUT, endpointNumber, (LinuxTransfer) transfer);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBDevice.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbDevice.java
similarity index 73%
rename from java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBDevice.java
rename to java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbDevice.java
index 897d3c1..d82aed8 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBDevice.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbDevice.java
@@ -7,13 +7,13 @@
package net.codecrete.usb.linux;
-import net.codecrete.usb.USBControlTransfer;
-import net.codecrete.usb.USBDirection;
-import net.codecrete.usb.USBException;
-import net.codecrete.usb.USBTransferType;
+import net.codecrete.usb.UsbControlTransfer;
+import net.codecrete.usb.UsbDirection;
+import net.codecrete.usb.UsbException;
+import net.codecrete.usb.UsbTransferType;
import net.codecrete.usb.common.Transfer;
-import net.codecrete.usb.common.USBDeviceImpl;
-import net.codecrete.usb.common.USBInterfaceImpl;
+import net.codecrete.usb.common.UsbDeviceImpl;
+import net.codecrete.usb.common.UsbInterfaceImpl;
import net.codecrete.usb.linux.gen.fcntl.fcntl;
import net.codecrete.usb.linux.gen.unistd.unistd;
import net.codecrete.usb.linux.gen.usbdevice_fs.usbdevfs_disconnect_claim;
@@ -22,6 +22,7 @@
import net.codecrete.usb.linux.gen.usbdevice_fs.usbdevice_fs;
import net.codecrete.usb.usbstandard.DeviceDescriptor;
import net.codecrete.usb.usbstandard.SetupPacket;
+import org.jetbrains.annotations.NotNull;
import java.io.IOException;
import java.io.InputStream;
@@ -34,11 +35,11 @@
import static java.lang.foreign.ValueLayout.JAVA_BYTE;
import static java.lang.foreign.ValueLayout.JAVA_INT;
import static net.codecrete.usb.linux.Linux.allocateErrorState;
-import static net.codecrete.usb.linux.LinuxUSBException.throwException;
-import static net.codecrete.usb.linux.LinuxUSBException.throwLastError;
+import static net.codecrete.usb.linux.LinuxUsbException.throwException;
+import static net.codecrete.usb.linux.LinuxUsbException.throwLastError;
@SuppressWarnings("java:S2160")
-public class LinuxUSBDevice extends USBDeviceImpl {
+public class LinuxUsbDevice extends UsbDeviceImpl {
@SuppressWarnings("resource")
private static final MemorySegment DRIVER_NAME_USBFS = Arena.global().allocateUtf8String("usbfs");
@@ -49,7 +50,7 @@ public class LinuxUSBDevice extends USBDeviceImpl {
private boolean detachDrivers = false;
- LinuxUSBDevice(Object id, int vendorId, int productId) {
+ LinuxUsbDevice(Object id, int vendorId, int productId) {
super(id, vendorId, productId);
asyncTask = LinuxAsyncTask.INSTANCE;
loadDescription((String) id);
@@ -60,7 +61,7 @@ private void loadDescription(String path) {
try {
descriptors = Files.readAllBytes(Path.of(path));
} catch (IOException e) {
- throw new USBException("reading configuration descriptor failed", e);
+ throw new UsbException("reading configuration descriptor failed", e);
}
// `descriptors` contains the device descriptor followed by the configuration descriptor
@@ -72,26 +73,26 @@ private void loadDescription(String path) {
@Override
public void detachStandardDrivers() {
- if (isOpen())
+ if (isOpened())
throwException("detachStandardDrivers() must not be called while the device is open");
detachDrivers = true;
}
@Override
public void attachStandardDrivers() {
- if (isOpen())
+ if (isOpened())
throwException("attachStandardDrivers() must not be called while the device is open");
detachDrivers = false;
}
@Override
- public boolean isOpen() {
+ public boolean isOpened() {
return fd != -1;
}
@Override
public synchronized void open() {
- if (isOpen())
+ if (isOpened())
throwException("device is already open");
try (var arena = Arena.ofConfined()) {
@@ -106,13 +107,13 @@ public synchronized void open() {
@Override
public synchronized void close() {
- if (!isOpen())
+ if (!isOpened())
return;
asyncTask.removeFromAsyncIOCompletion(this);
for (var intf : interfaceList)
- ((USBInterfaceImpl) intf).setClaimed(false);
+ ((UsbInterfaceImpl) intf).setClaimed(false);
unistd.close(fd);
fd = -1;
@@ -137,12 +138,12 @@ public synchronized void claimInterface(int interfaceNumber) {
usbdevfs_disconnect_claim.interface_$set(disconnectClaim, interfaceNumber);
usbdevfs_disconnect_claim.flags$set(disconnectClaim, usbdevice_fs.USBDEVFS_DISCONNECT_CLAIM_EXCEPT_DRIVER());
usbdevfs_disconnect_claim.driver$slice(disconnectClaim).copyFrom(DRIVER_NAME_USBFS);
- ret = IO.ioctl(fd, USBDevFS.DISCONNECT_CLAIM, disconnectClaim, errorState);
+ ret = IO.ioctl(fd, UsbDevFS.DISCONNECT_CLAIM, disconnectClaim, errorState);
} else {
// claim interface (without detaching kernel driver)
var intfNumSegment = arena.allocate(JAVA_INT, interfaceNumber);
- ret = IO.ioctl(fd, USBDevFS.CLAIMINTERFACE, intfNumSegment, errorState);
+ ret = IO.ioctl(fd, UsbDevFS.CLAIMINTERFACE, intfNumSegment, errorState);
}
if (ret != 0)
@@ -160,16 +161,13 @@ public synchronized void selectAlternateSetting(int interfaceNumber, int alterna
// check alternate setting
var altSetting = intf.getAlternate(alternateNumber);
- if (altSetting == null)
- throwException("interface %d does not have an alternate interface setting %d", interfaceNumber,
- alternateNumber);
try (var arena = Arena.ofConfined()) {
var setIntfSegment = usbdevfs_setinterface.allocate(arena);
usbdevfs_setinterface.interface_$set(setIntfSegment, interfaceNumber);
usbdevfs_setinterface.altsetting$set(setIntfSegment, alternateNumber);
var errorState = allocateErrorState(arena);
- var ret = IO.ioctl(fd, USBDevFS.SETINTERFACE, setIntfSegment, errorState);
+ var ret = IO.ioctl(fd, UsbDevFS.SETINTERFACE, setIntfSegment, errorState);
if (ret != 0)
throwLastError(errorState, "setting alternate interface failed");
}
@@ -185,7 +183,7 @@ public synchronized void releaseInterface(int interfaceNumber) {
try (var arena = Arena.ofConfined()) {
var intfNumSegment = arena.allocate(JAVA_INT, interfaceNumber);
var errorState = allocateErrorState(arena);
- var ret = IO.ioctl(fd, USBDevFS.RELEASEINTERFACE, intfNumSegment, errorState);
+ var ret = IO.ioctl(fd, UsbDevFS.RELEASEINTERFACE, intfNumSegment, errorState);
if (ret != 0)
throwLastError(errorState, "releasing USB interface failed");
@@ -195,36 +193,36 @@ public synchronized void releaseInterface(int interfaceNumber) {
// reattach kernel driver
var request = usbdevfs_ioctl.allocate(arena);
usbdevfs_ioctl.ifno$set(request, interfaceNumber);
- usbdevfs_ioctl.ioctl_code$set(request, USBDevFS.CONNECT);
+ usbdevfs_ioctl.ioctl_code$set(request, UsbDevFS.CONNECT);
usbdevfs_ioctl.data$set(request, MemorySegment.NULL);
- IO.ioctl(fd, USBDevFS.IOCTL, request, errorState);
+ IO.ioctl(fd, UsbDevFS.IOCTL, request, errorState);
}
}
}
@Override
- public void controlTransferOut(USBControlTransfer setup, byte[] data) {
+ public void controlTransferOut(@NotNull UsbControlTransfer setup, byte[] data) {
try (var arena = Arena.ofConfined()) {
var dataLength = data != null ? data.length : 0;
- var transfer = createSyncCtrlTransfer(arena, USBDirection.OUT, setup, dataLength);
+ var transfer = createSyncCtrlTransfer(arena, UsbDirection.OUT, setup, dataLength);
if (dataLength != 0)
transfer.data().asSlice(8).copyFrom(MemorySegment.ofArray(data));
synchronized (transfer) {
- submitTransfer(USBDirection.OUT, 0, transfer);
- waitForTransfer(transfer, 0, USBDirection.OUT, 0);
+ submitTransfer(UsbDirection.OUT, 0, transfer);
+ waitForTransfer(transfer, 0, UsbDirection.OUT, 0);
}
}
}
@Override
- public byte[] controlTransferIn(USBControlTransfer setup, int length) {
+ public byte @NotNull [] controlTransferIn(@NotNull UsbControlTransfer setup, int length) {
try (var arena = Arena.ofConfined()) {
- var transfer = createSyncCtrlTransfer(arena, USBDirection.IN, setup, length);
+ var transfer = createSyncCtrlTransfer(arena, UsbDirection.IN, setup, length);
synchronized (transfer) {
- submitTransfer(USBDirection.IN, 0, transfer);
- waitForTransfer(transfer, 0, USBDirection.IN, 0);
+ submitTransfer(UsbDirection.IN, 0, transfer);
+ waitForTransfer(transfer, 0, UsbDirection.IN, 0);
}
return transfer.data().asSlice(8, transfer.resultSize()).toArray(JAVA_BYTE);
@@ -240,10 +238,10 @@ public byte[] controlTransferIn(USBControlTransfer setup, int length) {
* @param dataLength data length (in addition to setup data)
* @return transfer object
*/
- private LinuxTransfer createSyncCtrlTransfer(Arena arena, USBDirection direction, USBControlTransfer setup,
+ private LinuxTransfer createSyncCtrlTransfer(Arena arena, UsbDirection direction, UsbControlTransfer setup,
int dataLength) {
var bmRequest =
- (direction == USBDirection.IN ? 0x80 : 0) | (setup.requestType().ordinal() << 5) | setup.recipient().ordinal();
+ (direction == UsbDirection.IN ? 0x80 : 0) | (setup.requestType().ordinal() << 5) | setup.recipient().ordinal();
var buffer = arena.allocate(8L + dataLength, 8);
var setupPacket = new SetupPacket(buffer);
setupPacket.setRequestType(bmRequest);
@@ -256,36 +254,36 @@ private LinuxTransfer createSyncCtrlTransfer(Arena arena, USBDirection direction
transfer.setData(buffer);
transfer.setDataSize((int) buffer.byteSize());
transfer.setResultSize(-1);
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
return transfer;
}
@Override
- public void transferOut(int endpointNumber, byte[] data, int offset, int length, int timeout) {
+ public void transferOut(int endpointNumber, byte @NotNull [] data, int offset, int length, int timeout) {
try (var arena = Arena.ofConfined()) {
var buffer = arena.allocate(length);
buffer.copyFrom(MemorySegment.ofArray(data).asSlice(offset, length));
var transfer = createSyncTransfer(buffer);
synchronized (transfer) {
- submitTransfer(USBDirection.OUT, endpointNumber, transfer);
- waitForTransfer(transfer, timeout, USBDirection.OUT, endpointNumber);
+ submitTransfer(UsbDirection.OUT, endpointNumber, transfer);
+ waitForTransfer(transfer, timeout, UsbDirection.OUT, endpointNumber);
}
}
}
@Override
- public byte[] transferIn(int endpointNumber, int timeout) {
- var endpoint = getEndpoint(USBDirection.IN, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ public byte @NotNull [] transferIn(int endpointNumber, int timeout) {
+ var endpoint = getEndpoint(UsbDirection.IN, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
try (var arena = Arena.ofConfined()) {
var buffer = arena.allocate(endpoint.packetSize());
var transfer = createSyncTransfer(buffer);
synchronized (transfer) {
- submitTransfer(USBDirection.IN, endpointNumber, transfer);
- waitForTransfer(transfer, timeout, USBDirection.IN, endpointNumber);
+ submitTransfer(UsbDirection.IN, endpointNumber, transfer);
+ waitForTransfer(transfer, timeout, UsbDirection.IN, endpointNumber);
}
return buffer.asSlice(0, transfer.resultSize()).toArray(JAVA_BYTE);
@@ -297,16 +295,16 @@ private LinuxTransfer createSyncTransfer(MemorySegment data) {
transfer.setData(data);
transfer.setDataSize((int) data.byteSize());
transfer.setResultSize(-1);
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
return transfer;
}
- synchronized void submitTransfer(USBDirection direction, int endpointNumber, LinuxTransfer transfer) {
+ synchronized void submitTransfer(UsbDirection direction, int endpointNumber, LinuxTransfer transfer) {
if (endpointNumber != 0) {
- var endpoint = getEndpoint(direction, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ var endpoint = getEndpoint(direction, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
asyncTask.submitTransfer(this, endpoint.endpointAddress(), endpoint.transferType(), transfer);
} else {
- asyncTask.submitTransfer(this, 0, USBTransferType.CONTROL, transfer);
+ asyncTask.submitTransfer(this, 0, UsbTransferType.CONTROL, transfer);
}
}
@@ -321,37 +319,37 @@ protected void throwOSException(int errorCode, String message, Object... args) {
}
@Override
- public void clearHalt(USBDirection direction, int endpointNumber) {
- var endpoint = getEndpoint(direction, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ public void clearHalt(UsbDirection direction, int endpointNumber) {
+ var endpoint = getEndpoint(direction, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
try (var arena = Arena.ofConfined()) {
var endpointAddrSegment = arena.allocate(JAVA_INT, endpoint.endpointAddress() & 0xff);
var errorState = allocateErrorState(arena);
- var res = IO.ioctl(fd, USBDevFS.CLEAR_HALT, endpointAddrSegment, errorState);
+ var res = IO.ioctl(fd, UsbDevFS.CLEAR_HALT, endpointAddrSegment, errorState);
if (res < 0)
throwLastError(errorState, "clearing halt failed");
}
}
@Override
- public synchronized void abortTransfers(USBDirection direction, int endpointNumber) {
- var endpoint = getEndpoint(direction, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ public synchronized void abortTransfers(UsbDirection direction, int endpointNumber) {
+ var endpoint = getEndpoint(direction, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
asyncTask.abortTransfers(this, endpoint.endpointAddress());
}
@Override
- public synchronized InputStream openInputStream(int endpointNumber, int bufferSize) {
+ public synchronized @NotNull InputStream openInputStream(int endpointNumber, int bufferSize) {
// check that endpoint number is valid
- getEndpoint(USBDirection.IN, endpointNumber, USBTransferType.BULK, null);
+ getEndpoint(UsbDirection.IN, endpointNumber, UsbTransferType.BULK, null);
return new LinuxEndpointInputStream(this, endpointNumber, bufferSize);
}
@Override
- public synchronized OutputStream openOutputStream(int endpointNumber, int bufferSize) {
+ public synchronized @NotNull OutputStream openOutputStream(int endpointNumber, int bufferSize) {
// check that endpoint number is valid
- getEndpoint(USBDirection.OUT, endpointNumber, USBTransferType.BULK, null);
+ getEndpoint(UsbDirection.OUT, endpointNumber, UsbTransferType.BULK, null);
return new LinuxEndpointOutputStream(this, endpointNumber, bufferSize);
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBDeviceRegistry.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbDeviceRegistry.java
similarity index 93%
rename from java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBDeviceRegistry.java
rename to java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbDeviceRegistry.java
index ec12e75..01051c9 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBDeviceRegistry.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbDeviceRegistry.java
@@ -7,9 +7,9 @@
package net.codecrete.usb.linux;
-import net.codecrete.usb.USBDevice;
+import net.codecrete.usb.UsbDevice;
import net.codecrete.usb.common.ScopeCleanup;
-import net.codecrete.usb.common.USBDeviceRegistry;
+import net.codecrete.usb.common.UsbDeviceRegistry;
import net.codecrete.usb.linux.gen.poll.poll;
import net.codecrete.usb.linux.gen.poll.pollfd;
import net.codecrete.usb.linux.gen.udev.udev;
@@ -20,14 +20,14 @@
import java.util.List;
import static java.lang.System.Logger.Level.INFO;
-import static net.codecrete.usb.linux.LinuxUSBException.throwException;
+import static net.codecrete.usb.linux.LinuxUsbException.throwException;
/**
* Linux implementation of USB device registry.
*/
-public class LinuxUSBDeviceRegistry extends USBDeviceRegistry {
+public class LinuxUsbDeviceRegistry extends UsbDeviceRegistry {
- private static final System.Logger LOG = System.getLogger(LinuxUSBDeviceRegistry.class.getName());
+ private static final System.Logger LOG = System.getLogger(LinuxUsbDeviceRegistry.class.getName());
private static final MemorySegment SUBSYSTEM_USB;
private static final MemorySegment MONITOR_NAME;
@@ -118,8 +118,8 @@ protected void monitorDevices() {
}
@SuppressWarnings("java:S135")
- private List enumeratePresentDevices(MemorySegment udevInstance) {
- List result = new ArrayList<>();
+ private List enumeratePresentDevices(MemorySegment udevInstance) {
+ List result = new ArrayList<>();
try (var outerCleanup = new ScopeCleanup()) {
// create device enumerator
@@ -181,7 +181,7 @@ private void onDeviceDisconnected(MemorySegment udevDevice) {
}
/**
- * Retrieves the device details and returns a {@code USBDevice} instance.
+ * Retrieves the device details and returns a {@code UsbDevice} instance.
*
* If the device is missing one of vendor ID, product ID or device path,
* {@code null} is returned.
@@ -191,7 +191,7 @@ private void onDeviceDisconnected(MemorySegment udevDevice) {
* @return the device instance
*/
@SuppressWarnings("java:S106")
- private USBDevice getDeviceDetails(MemorySegment udevDevice) {
+ private UsbDevice getDeviceDetails(MemorySegment udevDevice) {
int vendorId = 0;
int productId = 0;
@@ -215,7 +215,7 @@ private USBDevice getDeviceDetails(MemorySegment udevDevice) {
productId = Integer.parseInt(idProduct, 16);
// create device instance
- var device = new LinuxUSBDevice(devPath, vendorId, productId);
+ var device = new LinuxUsbDevice(devPath, vendorId, productId);
device.setProductStrings(getDeviceAttribute(udevDevice, ATTR_MANUFACTURER), getDeviceAttribute(udevDevice
, ATTR_PRODUCT), getDeviceAttribute(udevDevice, ATTR_SERIAL));
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBException.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbException.java
similarity index 85%
rename from java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBException.java
rename to java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbException.java
index 43924a9..4f0d95c 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUSBException.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/LinuxUsbException.java
@@ -6,8 +6,8 @@
//
package net.codecrete.usb.linux;
-import net.codecrete.usb.USBException;
-import net.codecrete.usb.USBStallException;
+import net.codecrete.usb.UsbException;
+import net.codecrete.usb.UsbStallException;
import net.codecrete.usb.linux.gen.errno.errno;
import java.lang.foreign.MemorySegment;
@@ -15,7 +15,7 @@
/**
* Exception thrown if a Linux specific error occurs.
*/
-public class LinuxUSBException extends USBException {
+public class LinuxUsbException extends UsbException {
/**
* Creates a new instance.
@@ -26,7 +26,7 @@ public class LinuxUSBException extends USBException {
* @param message exception message
* @param errorCode Linux error code (returned by {@code errno})
*/
- public LinuxUSBException(String message, int errorCode) {
+ public LinuxUsbException(String message, int errorCode) {
super(String.format("%s: %s", message, Linux.getErrorMessage(errorCode)), errorCode);
}
@@ -43,9 +43,9 @@ public LinuxUSBException(String message, int errorCode) {
static void throwException(int errorCode, String message, Object... args) {
var formattedMessage = String.format(message, args);
if (errorCode == errno.EPIPE()) {
- throw new USBStallException(formattedMessage);
+ throw new UsbStallException(formattedMessage);
} else {
- throw new LinuxUSBException(formattedMessage, errorCode);
+ throw new LinuxUsbException(formattedMessage, errorCode);
}
}
@@ -56,7 +56,7 @@ static void throwException(int errorCode, String message, Object... args) {
* @param args arguments for exception message
*/
static void throwException(String message, Object... args) {
- throw new USBException(String.format(message, args));
+ throw new UsbException(String.format(message, args));
}
/**
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/linux/USBDevFS.java b/java-does-usb/src/main/java/net/codecrete/usb/linux/UsbDevFS.java
similarity index 95%
rename from java-does-usb/src/main/java/net/codecrete/usb/linux/USBDevFS.java
rename to java-does-usb/src/main/java/net/codecrete/usb/linux/UsbDevFS.java
index f3be0b3..72d9e5c 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/linux/USBDevFS.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/linux/UsbDevFS.java
@@ -14,9 +14,9 @@
* Thus, they cannot be generated using jextract.
*
*/
-class USBDevFS {
+class UsbDevFS {
- private USBDevFS() {
+ private UsbDevFS() {
}
static final long CLAIMINTERFACE = 0x8004550FL;
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitHelper.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitHelper.java
index b508a01..22786f0 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitHelper.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitHelper.java
@@ -124,8 +124,8 @@ static MemorySegment getInterface(int service, MemorySegment pluginType, MemoryS
var refiid = CoreFoundation.CFUUIDGetUUIDBytes(arena, interfaceId);
// MemorySegment for holding xxxInterface**
var intfHolder = arena.allocate(ADDRESS, NULL);
- ret = IoKitUSB.QueryInterface(plug, refiid, intfHolder);
- IoKitUSB.Release(plug);
+ ret = IoKitUsb.QueryInterface(plug, refiid, intfHolder);
+ IoKitUsb.Release(plug);
if (ret != 0)
return null;
return dereference(intfHolder, COM_OBJECT);
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitUSB.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitUsb.java
similarity index 99%
rename from java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitUSB.java
rename to java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitUsb.java
index 2ba04f1..6dd1e76 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitUSB.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/IoKitUsb.java
@@ -19,9 +19,9 @@
* Helper functions to call the virtual methods of IOKit USB interfaces.
*/
@SuppressWarnings({"java:S100", "java:S107", "UnusedReturnValue", "SameParameterValue"})
-class IoKitUSB {
+class IoKitUsb {
- private IoKitUSB() {
+ private IoKitUsb() {
}
// HRESULT (STDMETHODCALLTYPE *QueryInterface)(void *thisPointer, REFIID iid, LPVOID *ppv)
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosAsyncTask.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosAsyncTask.java
index 0ca192b..c9992de 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosAsyncTask.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosAsyncTask.java
@@ -7,7 +7,7 @@
package net.codecrete.usb.macos;
-import net.codecrete.usb.USBException;
+import net.codecrete.usb.UsbException;
import net.codecrete.usb.macos.gen.corefoundation.CoreFoundation;
import net.codecrete.usb.macos.gen.iokit.IOKit;
@@ -116,7 +116,7 @@ private void startAsyncIOThread(MemorySegment firstSource) {
Arena.global());
} catch (IllegalAccessException | NoSuchMethodException e) {
- throw new USBException("internal error (creating method handle)", e);
+ throw new UsbException("internal error (creating method handle)", e);
}
var thread = new Thread(() -> asyncIOCompletionTask(firstSource), "USB async IO");
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointInputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointInputStream.java
index e947cae..06dff33 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointInputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointInputStream.java
@@ -12,12 +12,12 @@
public class MacosEndpointInputStream extends EndpointInputStream {
- MacosEndpointInputStream(MacosUSBDevice device, int endpointNumber, int bufferSize) {
+ MacosEndpointInputStream(MacosUsbDevice device, int endpointNumber, int bufferSize) {
super(device, endpointNumber, bufferSize);
}
@Override
protected void submitTransferIn(Transfer transfer) {
- ((MacosUSBDevice) device).submitTransferIn(endpointNumber, (MacosTransfer) transfer, 0);
+ ((MacosUsbDevice) device).submitTransferIn(endpointNumber, (MacosTransfer) transfer, 0);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointOutputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointOutputStream.java
index d8f16f2..898ab5e 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointOutputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosEndpointOutputStream.java
@@ -12,12 +12,12 @@
public class MacosEndpointOutputStream extends EndpointOutputStream {
- MacosEndpointOutputStream(MacosUSBDevice device, int endpointNumber, int bufferSize) {
+ MacosEndpointOutputStream(MacosUsbDevice device, int endpointNumber, int bufferSize) {
super(device, endpointNumber, bufferSize);
}
@Override
protected void submitTransferOut(Transfer request) {
- ((MacosUSBDevice) device).submitTransferOut(endpointNumber, (MacosTransfer) request, 0);
+ ((MacosUsbDevice) device).submitTransferOut(endpointNumber, (MacosTransfer) request, 0);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDevice.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDevice.java
similarity index 78%
rename from java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDevice.java
rename to java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDevice.java
index 18673d2..512238c 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDevice.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDevice.java
@@ -10,12 +10,13 @@
import net.codecrete.usb.*;
import net.codecrete.usb.common.ScopeCleanup;
import net.codecrete.usb.common.Transfer;
-import net.codecrete.usb.common.USBDeviceImpl;
+import net.codecrete.usb.common.UsbDeviceImpl;
import net.codecrete.usb.macos.gen.iokit.IOKit;
import net.codecrete.usb.macos.gen.iokit.IOUSBDevRequest;
import net.codecrete.usb.macos.gen.iokit.IOUSBFindInterfaceRequest;
import net.codecrete.usb.usbstandard.ConfigurationDescriptor;
import net.codecrete.usb.usbstandard.Constants;
+import org.jetbrains.annotations.NotNull;
import java.io.InputStream;
import java.io.OutputStream;
@@ -28,10 +29,10 @@
import static java.lang.foreign.ValueLayout.*;
import static net.codecrete.usb.common.ForeignMemory.dereference;
-import static net.codecrete.usb.macos.MacosUSBException.throwException;
+import static net.codecrete.usb.macos.MacosUsbException.throwException;
/**
- * MacOS implementation of {@link net.codecrete.usb.USBDevice}.
+ * MacOS implementation of {@link UsbDevice}.
*
* All read and write operations on endpoints are submitted through synchronized methods in order to control
* concurrency. If it wasn't controlled, the danger is that device and interface pointers are used, which have
@@ -43,7 +44,7 @@
*
*/
@SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter", "java:S2160"})
-public class MacosUSBDevice extends USBDeviceImpl {
+public class MacosUsbDevice extends UsbDeviceImpl {
private final MacosAsyncTask asyncTask;
// Native USB device interface (IOUSBDeviceInterface**)
@@ -57,7 +58,7 @@ public class MacosUSBDevice extends USBDeviceImpl {
private final long discoveryTime;
- MacosUSBDevice(MemorySegment device, Object id, int vendorId, int productId) {
+ MacosUsbDevice(MemorySegment device, Object id, int vendorId, int productId) {
super(id, vendorId, productId);
discoveryTime = System.currentTimeMillis();
asyncTask = MacosAsyncTask.INSTANCE;
@@ -65,36 +66,36 @@ public class MacosUSBDevice extends USBDeviceImpl {
loadDescription(device);
this.device = device;
- IoKitUSB.AddRef(device);
+ IoKitUsb.AddRef(device);
}
@Override
public void detachStandardDrivers() {
- if (isOpen())
+ if (isOpened())
throwException("detachStandardDrivers() must not be called while the device is open");
- var ret = IoKitUSB.USBDeviceReEnumerate(device, IOKit.kUSBReEnumerateCaptureDeviceMask());
+ var ret = IoKitUsb.USBDeviceReEnumerate(device, IOKit.kUSBReEnumerateCaptureDeviceMask());
if (ret != 0)
throwException(ret, "detaching standard drivers failed");
}
@Override
public void attachStandardDrivers() {
- if (isOpen())
+ if (isOpened())
throwException("attachStandardDrivers() must not be called while the device is open");
- var ret = IoKitUSB.USBDeviceReEnumerate(device, IOKit.kUSBReEnumerateReleaseDeviceMask());
+ var ret = IoKitUsb.USBDeviceReEnumerate(device, IOKit.kUSBReEnumerateReleaseDeviceMask());
if (ret != 0)
throwException(ret, "attaching standard drivers failed");
}
@Override
- public boolean isOpen() {
+ public boolean isOpened() {
return claimedInterfaces != null;
}
@SuppressWarnings("java:S2276")
@Override
public synchronized void open() {
- if (isOpen())
+ if (isOpened())
throwException("device is already open");
// open device (several retries if device has just been connected/discovered)
@@ -103,7 +104,7 @@ public synchronized void open() {
var ret = 0;
while (numTries > 0) {
numTries -= 1;
- ret = IoKitUSB.USBDeviceOpenSeize(device);
+ ret = IoKitUsb.USBDeviceOpenSeize(device);
if (ret != IOKit.kIOReturnExclusiveAccess())
break;
@@ -121,7 +122,7 @@ public synchronized void open() {
addDeviceEventSource();
// set configuration
- ret = IoKitUSB.SetConfiguration(device, (byte) configurationValue);
+ ret = IoKitUsb.SetConfiguration(device, (byte) configurationValue);
if (ret != 0)
throwException(ret, "setting configuration failed");
@@ -130,28 +131,28 @@ public synchronized void open() {
@Override
public synchronized void close() {
- if (!isOpen())
+ if (!isOpened())
return;
for (var interfaceInfo : claimedInterfaces) {
- IoKitUSB.USBInterfaceClose(interfaceInfo.iokitInterface);
- IoKitUSB.Release(interfaceInfo.iokitInterface);
+ IoKitUsb.USBInterfaceClose(interfaceInfo.iokitInterface);
+ IoKitUsb.Release(interfaceInfo.iokitInterface);
setClaimed(interfaceInfo.interfaceNumber, false);
}
claimedInterfaces = null;
endpoints = null;
- var source = IoKitUSB.GetDeviceAsyncEventSource(device);
+ var source = IoKitUsb.GetDeviceAsyncEventSource(device);
if (source.address() != 0)
asyncTask.removeEventSource(source);
- IoKitUSB.USBDeviceClose(device);
+ IoKitUsb.USBDeviceClose(device);
}
synchronized void closeFully() {
close();
- IoKitUSB.Release(device);
+ IoKitUsb.Release(device);
device = null;
}
@@ -160,14 +161,14 @@ private void loadDescription(MemorySegment device) {
// retrieve device descriptor using synchronous control transfer
var data = arena.allocate(255);
- var deviceRequest = createDeviceRequest(arena, USBDirection.IN, new USBControlTransfer(
- USBRequestType.STANDARD,
- USBRecipient.DEVICE,
+ var deviceRequest = createDeviceRequest(arena, UsbDirection.IN, new UsbControlTransfer(
+ UsbRequestType.STANDARD,
+ UsbRecipient.DEVICE,
6, // get descriptor
Constants.DEVICE_DESCRIPTOR_TYPE << 8,
0
), data);
- var ret = IoKitUSB.DeviceRequest(device, deviceRequest);
+ var ret = IoKitUsb.DeviceRequest(device, deviceRequest);
if (ret != 0)
throwException(ret, "querying device descriptor failed");
@@ -178,7 +179,7 @@ private void loadDescription(MemorySegment device) {
// retrieve information of first configuration
var descPtrHolder = arena.allocate(ADDRESS);
- ret = IoKitUSB.GetConfigurationDescriptorPtr(device, (byte) 0, descPtrHolder);
+ ret = IoKitUsb.GetConfigurationDescriptorPtr(device, (byte) 0, descPtrHolder);
if (ret != 0)
throwException(ret, "querying first configuration failed");
@@ -192,7 +193,7 @@ private void loadDescription(MemorySegment device) {
}
@SuppressWarnings("java:S135")
- private InterfaceInfo findInterface(int interfaceNumber) {
+ private InterfaceInfo findInterfaceInfo(int interfaceNumber) {
try (var arena = Arena.ofConfined(); var outerCleanup = new ScopeCleanup()) {
var request = IOUSBFindInterfaceRequest.allocate(arena);
@@ -202,7 +203,7 @@ private InterfaceInfo findInterface(int interfaceNumber) {
IOUSBFindInterfaceRequest.bAlternateSetting$set(request, (short) IOKit.kIOUSBFindInterfaceDontCare());
var iterHolder = arena.allocate(JAVA_INT);
- var ret = IoKitUSB.CreateInterfaceIterator(device, request, iterHolder);
+ var ret = IoKitUsb.CreateInterfaceIterator(device, request, iterHolder);
if (ret != 0)
throwException("internal error (CreateInterfaceIterator)");
@@ -223,13 +224,13 @@ private InterfaceInfo findInterface(int interfaceNumber) {
if (intf == null)
continue;
- cleanup.add(() -> IoKitUSB.Release(intf));
+ cleanup.add(() -> IoKitUsb.Release(intf));
- IoKitUSB.GetInterfaceNumber(intf, intfNumberHolder);
+ IoKitUsb.GetInterfaceNumber(intf, intfNumberHolder);
if (intfNumberHolder.get(JAVA_INT, 0) != interfaceNumber)
continue;
- IoKitUSB.AddRef(intf);
+ IoKitUsb.AddRef(intf);
return new InterfaceInfo(intf, interfaceNumber);
}
}
@@ -245,14 +246,14 @@ public synchronized void claimInterface(int interfaceNumber) {
try (var cleanup = new ScopeCleanup()) {
- var interfaceInfo = findInterface(interfaceNumber);
- cleanup.add(() -> IoKitUSB.Release(interfaceInfo.iokitInterface()));
+ var interfaceInfo = findInterfaceInfo(interfaceNumber);
+ cleanup.add(() -> IoKitUsb.Release(interfaceInfo.iokitInterface()));
- var ret = IoKitUSB.USBInterfaceOpen(interfaceInfo.iokitInterface());
+ var ret = IoKitUsb.USBInterfaceOpen(interfaceInfo.iokitInterface());
if (ret != 0)
throwException(ret, "claiming interface failed");
- IoKitUSB.AddRef(interfaceInfo.iokitInterface());
+ IoKitUsb.AddRef(interfaceInfo.iokitInterface());
claimedInterfaces.add(interfaceInfo);
setClaimed(interfaceNumber, true);
addInterfaceEventSource(interfaceInfo);
@@ -268,14 +269,10 @@ public synchronized void selectAlternateSetting(int interfaceNumber, int alterna
// check alternate setting
var altSetting = intf.getAlternate(alternateNumber);
- if (altSetting == null)
- throwException("interface %d does not have an alternate interface setting %d", interfaceNumber,
- alternateNumber);
-
var intfInfo =
claimedInterfaces.stream().filter(interf -> interf.interfaceNumber() == interfaceNumber).findFirst().get();
- var ret = IoKitUSB.SetAlternateInterface(intfInfo.iokitInterface(), (byte) alternateNumber);
+ var ret = IoKitUsb.SetAlternateInterface(intfInfo.iokitInterface(), (byte) alternateNumber);
if (ret != 0)
throwException(ret, "setting alternate interface failed");
@@ -292,16 +289,16 @@ public synchronized void releaseInterface(int interfaceNumber) {
var interfaceInfo =
claimedInterfaces.stream().filter(info -> info.interfaceNumber == interfaceNumber).findFirst().get();
- var source = IoKitUSB.GetInterfaceAsyncEventSource(interfaceInfo.iokitInterface());
+ var source = IoKitUsb.GetInterfaceAsyncEventSource(interfaceInfo.iokitInterface());
if (source.address() != 0)
asyncTask.removeEventSource(source);
- var ret = IoKitUSB.USBInterfaceClose(interfaceInfo.iokitInterface());
+ var ret = IoKitUsb.USBInterfaceClose(interfaceInfo.iokitInterface());
if (ret != 0)
throwException(ret, "releasing interface failed");
claimedInterfaces.remove(interfaceInfo);
- IoKitUSB.Release(interfaceInfo.iokitInterface());
+ IoKitUsb.Release(interfaceInfo.iokitInterface());
setClaimed(interfaceNumber, false);
updateEndpointList();
@@ -329,14 +326,14 @@ private void updateEndpointList() {
var intf = interfaceInfo.iokitInterface();
var numEndpointsHolder = arena.allocate(JAVA_BYTE);
- var ret = IoKitUSB.GetNumEndpoints(intf, numEndpointsHolder);
+ var ret = IoKitUsb.GetNumEndpoints(intf, numEndpointsHolder);
if (ret != 0)
throwException(ret, "internal error (GetNumEndpoints)");
var numEndpoints = numEndpointsHolder.get(JAVA_BYTE, 0) & 255;
for (var pipeIndex = 1; pipeIndex <= numEndpoints; pipeIndex++) {
- ret = IoKitUSB.GetPipeProperties(intf, (byte) pipeIndex, directionHolder, numberHolder,
+ ret = IoKitUsb.GetPipeProperties(intf, (byte) pipeIndex, directionHolder, numberHolder,
transferTypeHolder, maxPacketSizeHolder, intervalHolder);
if (ret != 0)
throwException(ret, "internal error (GetPipeProperties)");
@@ -355,10 +352,10 @@ private void updateEndpointList() {
}
@SuppressWarnings("SameParameterValue")
- private synchronized EndpointInfo getEndpointInfo(int endpointNumber, USBDirection direction,
- USBTransferType transferType1, USBTransferType transferType2) {
+ private synchronized EndpointInfo getEndpointInfo(int endpointNumber, UsbDirection direction,
+ UsbTransferType transferType1, UsbTransferType transferType2) {
if (endpoints != null) {
- var endpointAddress = (byte) (endpointNumber | (direction == USBDirection.IN ? 0x80 : 0));
+ var endpointAddress = (byte) (endpointNumber | (direction == UsbDirection.IN ? 0x80 : 0));
var endpointInfo = endpoints.get(endpointAddress);
if (endpointInfo != null && (endpointInfo.transferType == transferType1 || endpointInfo.transferType == transferType2))
return endpointInfo;
@@ -376,11 +373,11 @@ private synchronized EndpointInfo getEndpointInfo(int endpointNumber, USBDirecti
throw new AssertionError("not reached");
}
- private static MemorySegment createDeviceRequest(Arena arena, USBDirection direction, USBControlTransfer setup,
+ private static MemorySegment createDeviceRequest(Arena arena, UsbDirection direction, UsbControlTransfer setup,
MemorySegment data) {
var deviceRequest = IOUSBDevRequest.allocate(arena);
var bmRequestType =
- (direction == USBDirection.IN ? 0x80 : 0x00) | (setup.requestType().ordinal() << 5) | setup.recipient().ordinal();
+ (direction == UsbDirection.IN ? 0x80 : 0x00) | (setup.requestType().ordinal() << 5) | setup.recipient().ordinal();
IOUSBDevRequest.bmRequestType$set(deviceRequest, (byte) bmRequestType);
IOUSBDevRequest.bRequest$set(deviceRequest, (byte) setup.request());
IOUSBDevRequest.wValue$set(deviceRequest, (short) setup.value());
@@ -391,17 +388,17 @@ private static MemorySegment createDeviceRequest(Arena arena, USBDirection direc
}
@Override
- public byte[] controlTransferIn(USBControlTransfer setup, int length) {
+ public byte @NotNull [] controlTransferIn(@NotNull UsbControlTransfer setup, int length) {
try (var arena = Arena.ofConfined()) {
var data = arena.allocate(length);
- var deviceRequest = createDeviceRequest(arena, USBDirection.IN, setup, data);
+ var deviceRequest = createDeviceRequest(arena, UsbDirection.IN, setup, data);
var transfer = new MacosTransfer();
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
synchronized (transfer) {
submitControlTransfer(deviceRequest, transfer);
- waitForTransfer(transfer, 0, USBDirection.IN, 0);
+ waitForTransfer(transfer, 0, UsbDirection.IN, 0);
}
return data.asSlice(0, transfer.resultSize()).toArray(JAVA_BYTE);
@@ -409,29 +406,29 @@ public byte[] controlTransferIn(USBControlTransfer setup, int length) {
}
@Override
- public void controlTransferOut(USBControlTransfer setup, byte[] data) {
+ public void controlTransferOut(@NotNull UsbControlTransfer setup, byte[] data) {
try (var arena = Arena.ofConfined()) {
var dataLength = data != null ? data.length : 0;
var dataSegment = arena.allocate(dataLength);
if (dataLength > 0)
dataSegment.copyFrom(MemorySegment.ofArray(data));
- var deviceRequest = createDeviceRequest(arena, USBDirection.OUT, setup, dataSegment);
+ var deviceRequest = createDeviceRequest(arena, UsbDirection.OUT, setup, dataSegment);
var transfer = new MacosTransfer();
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
synchronized (transfer) {
submitControlTransfer(deviceRequest, transfer);
- waitForTransfer(transfer, 0, USBDirection.OUT, 0);
+ waitForTransfer(transfer, 0, UsbDirection.OUT, 0);
}
}
}
@Override
- public void transferOut(int endpointNumber, byte[] data, int offset, int length, int timeout) {
+ public void transferOut(int endpointNumber, byte @NotNull [] data, int offset, int length, int timeout) {
- var epInfo = getEndpointInfo(endpointNumber, USBDirection.OUT, USBTransferType.BULK,
- USBTransferType.INTERRUPT);
+ var epInfo = getEndpointInfo(endpointNumber, UsbDirection.OUT, UsbTransferType.BULK,
+ UsbTransferType.INTERRUPT);
try (var arena = Arena.ofConfined()) {
var nativeData = arena.allocateArray(JAVA_BYTE, length);
@@ -440,28 +437,28 @@ public void transferOut(int endpointNumber, byte[] data, int offset, int length,
var transfer = new MacosTransfer();
transfer.setData(nativeData);
transfer.setDataSize(length);
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
synchronized (transfer) {
- if (timeout <= 0 || epInfo.transferType() == USBTransferType.BULK) {
+ if (timeout <= 0 || epInfo.transferType() == UsbTransferType.BULK) {
// no timeout or timeout handled by operating system
submitTransferOut(endpointNumber, transfer, timeout);
- waitForTransfer(transfer, 0, USBDirection.OUT, endpointNumber);
+ waitForTransfer(transfer, 0, UsbDirection.OUT, endpointNumber);
} else {
// interrupt transfer with timeout
submitTransferOut(endpointNumber, transfer, 0);
- waitForTransfer(transfer, timeout, USBDirection.OUT, endpointNumber);
+ waitForTransfer(transfer, timeout, UsbDirection.OUT, endpointNumber);
}
}
}
}
@Override
- public byte[] transferIn(int endpointNumber, int timeout) {
+ public byte @NotNull [] transferIn(int endpointNumber, int timeout) {
- var epInfo = getEndpointInfo(endpointNumber, USBDirection.IN, USBTransferType.BULK,
- USBTransferType.INTERRUPT);
+ var epInfo = getEndpointInfo(endpointNumber, UsbDirection.IN, UsbTransferType.BULK,
+ UsbTransferType.INTERRUPT);
try (var arena = Arena.ofConfined()) {
var nativeData = arena.allocateArray(JAVA_BYTE, epInfo.packetSize());
@@ -469,18 +466,18 @@ public byte[] transferIn(int endpointNumber, int timeout) {
var transfer = new MacosTransfer();
transfer.setData(nativeData);
transfer.setDataSize(epInfo.packetSize());
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
synchronized (transfer) {
- if (timeout <= 0 || epInfo.transferType() == USBTransferType.BULK) {
+ if (timeout <= 0 || epInfo.transferType() == UsbTransferType.BULK) {
// no timeout, or timeout handled by operating system
submitTransferIn(endpointNumber, transfer, timeout);
- waitForTransfer(transfer, 0, USBDirection.IN, endpointNumber);
+ waitForTransfer(transfer, 0, UsbDirection.IN, endpointNumber);
} else {
// interrupt transfer with timeout
submitTransferIn(endpointNumber, transfer, 0);
- waitForTransfer(transfer, timeout, USBDirection.IN, endpointNumber);
+ waitForTransfer(transfer, timeout, UsbDirection.IN, endpointNumber);
}
}
@@ -500,17 +497,17 @@ public byte[] transferIn(int endpointNumber, int timeout) {
*/
synchronized void submitTransferIn(int endpointNumber, MacosTransfer transfer, int timeout) {
- var epInfo = getEndpointInfo(endpointNumber, USBDirection.IN, USBTransferType.BULK,
- USBTransferType.INTERRUPT);
+ var epInfo = getEndpointInfo(endpointNumber, UsbDirection.IN, UsbTransferType.BULK,
+ UsbTransferType.INTERRUPT);
asyncTask.prepareForSubmission(transfer);
// submit transfer
int ret;
if (timeout <= 0)
- ret = IoKitUSB.ReadPipeAsync(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
+ ret = IoKitUsb.ReadPipeAsync(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
transfer.dataSize(), asyncTask.nativeCompletionCallback(), MemorySegment.ofAddress(transfer.id()));
else
- ret = IoKitUSB.ReadPipeAsyncTO(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
+ ret = IoKitUsb.ReadPipeAsyncTO(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
transfer.dataSize(), timeout, timeout, asyncTask.nativeCompletionCallback(),
MemorySegment.ofAddress(transfer.id()));
@@ -530,17 +527,17 @@ synchronized void submitTransferIn(int endpointNumber, MacosTransfer transfer, i
*/
synchronized void submitTransferOut(int endpointNumber, MacosTransfer transfer, int timeout) {
- var epInfo = getEndpointInfo(endpointNumber, USBDirection.OUT, USBTransferType.BULK,
- USBTransferType.INTERRUPT);
+ var epInfo = getEndpointInfo(endpointNumber, UsbDirection.OUT, UsbTransferType.BULK,
+ UsbTransferType.INTERRUPT);
asyncTask.prepareForSubmission(transfer);
// submit transfer
int ret;
if (timeout <= 0)
- ret = IoKitUSB.WritePipeAsync(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
+ ret = IoKitUsb.WritePipeAsync(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
transfer.dataSize(), asyncTask.nativeCompletionCallback(), MemorySegment.ofAddress(transfer.id()));
else
- ret = IoKitUSB.WritePipeAsyncTO(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
+ ret = IoKitUsb.WritePipeAsyncTO(epInfo.iokitInterface(), epInfo.pipeIndex(), transfer.data(),
transfer.dataSize(), timeout, timeout, asyncTask.nativeCompletionCallback(),
MemorySegment.ofAddress(transfer.id()));
@@ -560,7 +557,7 @@ synchronized void submitControlTransfer(MemorySegment deviceRequest, MacosTransf
asyncTask.prepareForSubmission(transfer);
// submit transfer
- var ret = IoKitUSB.DeviceRequestAsync(device, deviceRequest, asyncTask.nativeCompletionCallback(),
+ var ret = IoKitUsb.DeviceRequestAsync(device, deviceRequest, asyncTask.nativeCompletionCallback(),
MemorySegment.ofAddress(transfer.id()));
if (ret != 0)
@@ -573,37 +570,37 @@ protected Transfer createTransfer() {
}
@Override
- public void abortTransfers(USBDirection direction, int endpointNumber) {
- var epInfo = getEndpointInfo(endpointNumber, direction, USBTransferType.BULK,
- USBTransferType.INTERRUPT);
+ public void abortTransfers(UsbDirection direction, int endpointNumber) {
+ var epInfo = getEndpointInfo(endpointNumber, direction, UsbTransferType.BULK,
+ UsbTransferType.INTERRUPT);
- var ret = IoKitUSB.AbortPipe(epInfo.iokitInterface(), epInfo.pipeIndex());
+ var ret = IoKitUsb.AbortPipe(epInfo.iokitInterface(), epInfo.pipeIndex());
if (ret != 0)
throwException(ret, "aborting transfers failed");
}
@Override
- public void clearHalt(USBDirection direction, int endpointNumber) {
- var epInfo = getEndpointInfo(endpointNumber, direction, USBTransferType.BULK,
- USBTransferType.INTERRUPT);
+ public void clearHalt(UsbDirection direction, int endpointNumber) {
+ var epInfo = getEndpointInfo(endpointNumber, direction, UsbTransferType.BULK,
+ UsbTransferType.INTERRUPT);
- var ret = IoKitUSB.ClearPipeStallBothEnds(epInfo.iokitInterface(), epInfo.pipeIndex());
+ var ret = IoKitUsb.ClearPipeStallBothEnds(epInfo.iokitInterface(), epInfo.pipeIndex());
if (ret != 0)
throwException(ret, "clearing halt condition failed");
}
@Override
- public synchronized InputStream openInputStream(int endpointNumber, int bufferSize) {
+ public synchronized @NotNull InputStream openInputStream(int endpointNumber, int bufferSize) {
// check that endpoint number is valid
- getEndpointInfo(endpointNumber, USBDirection.IN, USBTransferType.BULK, null);
+ getEndpointInfo(endpointNumber, UsbDirection.IN, UsbTransferType.BULK, null);
return new MacosEndpointInputStream(this, endpointNumber, bufferSize);
}
@Override
- public synchronized OutputStream openOutputStream(int endpointNumber, int bufferSize) {
+ public synchronized @NotNull OutputStream openOutputStream(int endpointNumber, int bufferSize) {
// check that endpoint number is valid
- getEndpointInfo(endpointNumber, USBDirection.OUT, USBTransferType.BULK, null);
+ getEndpointInfo(endpointNumber, UsbDirection.OUT, UsbTransferType.BULK, null);
return new MacosEndpointOutputStream(this, endpointNumber, bufferSize);
}
@@ -613,11 +610,11 @@ protected void throwOSException(int errorCode, String message, Object... args) {
throwException(errorCode, message, args);
}
- private static USBTransferType getTransferType(byte macosTransferType) {
+ private static UsbTransferType getTransferType(byte macosTransferType) {
return switch (macosTransferType) {
- case 1 -> USBTransferType.ISOCHRONOUS;
- case 2 -> USBTransferType.BULK;
- case 3 -> USBTransferType.INTERRUPT;
+ case 1 -> UsbTransferType.ISOCHRONOUS;
+ case 2 -> UsbTransferType.BULK;
+ case 3 -> UsbTransferType.INTERRUPT;
default -> null;
};
}
@@ -625,7 +622,7 @@ private static USBTransferType getTransferType(byte macosTransferType) {
private synchronized void addDeviceEventSource() {
try (var innerArena = Arena.ofConfined()) {
var sourceHolder = innerArena.allocate(ADDRESS);
- var ret = IoKitUSB.CreateDeviceAsyncEventSource(device, sourceHolder);
+ var ret = IoKitUsb.CreateDeviceAsyncEventSource(device, sourceHolder);
if (ret != 0)
throwException(ret, "internal error (CreateDeviceAsyncEventSource)");
var source = dereference(sourceHolder);
@@ -636,7 +633,7 @@ private synchronized void addDeviceEventSource() {
private synchronized void addInterfaceEventSource(InterfaceInfo interfaceInfo) {
try (var innerArena = Arena.ofConfined()) {
var sourceHolder = innerArena.allocate(ADDRESS);
- var ret = IoKitUSB.CreateInterfaceAsyncEventSource(interfaceInfo.iokitInterface(), sourceHolder);
+ var ret = IoKitUsb.CreateInterfaceAsyncEventSource(interfaceInfo.iokitInterface(), sourceHolder);
if (ret != 0)
throwException(ret, "internal error (CreateInterfaceAsyncEventSource)");
var source = dereference(sourceHolder);
@@ -647,6 +644,6 @@ private synchronized void addInterfaceEventSource(InterfaceInfo interfaceInfo) {
record InterfaceInfo(MemorySegment iokitInterface, int interfaceNumber) {
}
- record EndpointInfo(MemorySegment iokitInterface, byte pipeIndex, USBTransferType transferType, int packetSize) {
+ record EndpointInfo(MemorySegment iokitInterface, byte pipeIndex, UsbTransferType transferType, int packetSize) {
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDeviceRegistry.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDeviceRegistry.java
similarity index 93%
rename from java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDeviceRegistry.java
rename to java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDeviceRegistry.java
index d2bb8c0..dbb2ffc 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDeviceRegistry.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbDeviceRegistry.java
@@ -7,9 +7,9 @@
package net.codecrete.usb.macos;
-import net.codecrete.usb.USBDevice;
+import net.codecrete.usb.UsbDevice;
import net.codecrete.usb.common.ScopeCleanup;
-import net.codecrete.usb.common.USBDeviceRegistry;
+import net.codecrete.usb.common.UsbDeviceRegistry;
import net.codecrete.usb.macos.gen.corefoundation.CoreFoundation;
import net.codecrete.usb.macos.gen.iokit.IOKit;
@@ -24,15 +24,15 @@
import static java.lang.foreign.MemorySegment.NULL;
import static java.lang.foreign.ValueLayout.*;
import static net.codecrete.usb.macos.CoreFoundationHelper.createCFStringRef;
-import static net.codecrete.usb.macos.MacosUSBException.throwException;
+import static net.codecrete.usb.macos.MacosUsbException.throwException;
/**
* MacOS implementation of USB device registry.
*/
@SuppressWarnings("java:S116")
-public class MacosUSBDeviceRegistry extends USBDeviceRegistry {
+public class MacosUsbDeviceRegistry extends UsbDeviceRegistry {
- private static final System.Logger LOG = System.getLogger(MacosUSBDeviceRegistry.class.getName());
+ private static final System.Logger LOG = System.getLogger(MacosUsbDeviceRegistry.class.getName());
private static final MemorySegment KEY_ID_VENDOR;
private static final MemorySegment KEY_ID_PRODUCT;
@@ -80,18 +80,18 @@ protected void monitorDevices() {
CoreFoundation.CFRunLoopAddSource(runLoop, runLoopSource, IOKit.kCFRunLoopDefaultMode$get());
// setup notification for connected devices
- var onDeviceConnectedMH = MethodHandles.lookup().findVirtual(MacosUSBDeviceRegistry.class,
+ var onDeviceConnectedMH = MethodHandles.lookup().findVirtual(MacosUsbDeviceRegistry.class,
"onDevicesConnected", MethodType.methodType(void.class, MemorySegment.class, int.class));
var deviceConnectedIter = setupNotification(arena, notifyPort, IOKit.kIOFirstMatchNotification(),
onDeviceConnectedMH);
// iterate current devices in order to arm the notifications (and build initial device list)
- var deviceList = new ArrayList();
+ var deviceList = new ArrayList();
iterateDevices(deviceConnectedIter, device -> deviceList.add(device)); // NOSONAR
setInitialDeviceList(deviceList);
// setup notification for disconnected devices
- var onDeviceDisconnectedMH = MethodHandles.lookup().findVirtual(MacosUSBDeviceRegistry.class,
+ var onDeviceDisconnectedMH = MethodHandles.lookup().findVirtual(MacosUsbDeviceRegistry.class,
"onDevicesDisconnected", MethodType.methodType(void.class, MemorySegment.class, int.class));
var deviceDisconnectedIter = setupNotification(arena, notifyPort, IOKit.kIOTerminatedNotification(),
onDeviceDisconnectedMH);
@@ -130,7 +130,7 @@ private void iterateDevices(int iterator, IOKitDeviceConsumer consumer) {
var device = IoKitHelper.getInterface(service, IoKitHelper.kIOUSBDeviceUserClientTypeID,
IoKitHelper.kIOUSBDeviceInterfaceID187);
if (device != null)
- cleanup.add(() -> IoKitUSB.Release(device));
+ cleanup.add(() -> IoKitUsb.Release(device));
// get entry ID (as unique ID)
var ret = IOKit.IORegistryEntryGetRegistryEntryID(service, entryIdHolder);
@@ -148,7 +148,7 @@ private void iterateDevices(int iterator, IOKitDeviceConsumer consumer) {
/**
* Calls the consumer for all devices produced by the iterator.
*
- * This method tries to create a {@link USBDevice} instance.
+ * This method tries to create a {@link UsbDevice} instance.
* If it fails, an information is printed, but the consumer is not called.
*
*
@@ -156,7 +156,7 @@ private void iterateDevices(int iterator, IOKitDeviceConsumer consumer) {
* @param consumer the consumer
*/
@SuppressWarnings("java:S106")
- private void iterateDevices(int iterator, Consumer consumer) {
+ private void iterateDevices(int iterator, Consumer consumer) {
iterateDevices(iterator, (entryId, service, deviceIntf) -> {
var deviceInfo = new VidPid();
@@ -172,7 +172,7 @@ private void iterateDevices(int iterator, Consumer consumer) {
});
}
- private USBDevice createDevice(Long entryID, int service, MemorySegment deviceIntf, VidPid info) {
+ private UsbDevice createDevice(Long entryID, int service, MemorySegment deviceIntf, VidPid info) {
if (deviceIntf == null)
return null;
@@ -187,7 +187,7 @@ private USBDevice createDevice(Long entryID, int service, MemorySegment deviceIn
info.vid = vendorId;
info.pid = productId;
- var device = new MacosUSBDevice(deviceIntf, entryID, vendorId, productId);
+ var device = new MacosUsbDevice(deviceIntf, entryID, vendorId, productId);
var manufacturer = IoKitHelper.getPropertyString(service, KEY_VENDOR, arena);
var product = IoKitHelper.getPropertyString(service, KEY_PRODUCT, arena);
@@ -267,7 +267,7 @@ private void onDevicesDisconnected(MemorySegment ignoredRefCon, int iterator) {
return;
try {
- ((MacosUSBDevice) device).closeFully();
+ ((MacosUsbDevice) device).closeFully();
} catch (Exception e) {
LOG.log(INFO, "failed to close USB device - ignoring exception", e);
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBException.java b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbException.java
similarity index 79%
rename from java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBException.java
rename to java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbException.java
index 9065394..3f0c607 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBException.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUsbException.java
@@ -6,16 +6,16 @@
//
package net.codecrete.usb.macos;
-import net.codecrete.usb.USBException;
-import net.codecrete.usb.USBStallException;
-import net.codecrete.usb.USBTimeoutException;
+import net.codecrete.usb.UsbException;
+import net.codecrete.usb.UsbStallException;
+import net.codecrete.usb.UsbTimeoutException;
import net.codecrete.usb.macos.gen.iokit.IOKit;
import net.codecrete.usb.macos.gen.mach.mach;
/**
* Exception thrown if a macOS specific error occurs.
*/
-public class MacosUSBException extends USBException {
+public class MacosUsbException extends UsbException {
/**
* Creates a new instance.
@@ -26,7 +26,7 @@ public class MacosUSBException extends USBException {
* @param message exception message
* @param errorCode macOS error code (usually returned by macOS functions)
*/
- public MacosUSBException(String message, int errorCode) {
+ public MacosUsbException(String message, int errorCode) {
super(String.format("%s: %s", message, machErrorMessage(errorCode)), errorCode);
}
@@ -48,11 +48,11 @@ private static String machErrorMessage(int errorCode) {
static void throwException(int errorCode, String message, Object... args) {
var formattedMessage = String.format(message, args);
if (errorCode == IOKit.kIOUSBPipeStalled()) {
- throw new USBStallException(formattedMessage);
+ throw new UsbStallException(formattedMessage);
} else if (errorCode == IOKit.kIOUSBTransactionTimeout()) {
- throw new USBTimeoutException(formattedMessage);
+ throw new UsbTimeoutException(formattedMessage);
} else {
- throw new MacosUSBException(formattedMessage, errorCode);
+ throw new MacosUsbException(formattedMessage, errorCode);
}
}
@@ -63,7 +63,7 @@ static void throwException(int errorCode, String message, Object... args) {
* @param args arguments for exception message
*/
static void throwException(String message, Object... args) {
- throw new USBException(String.format(message, args));
+ throw new UsbException(String.format(message, args));
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/DeviceInfoSet.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/DeviceInfoSet.java
index 623168f..095c85b 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/DeviceInfoSet.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/DeviceInfoSet.java
@@ -20,8 +20,8 @@
import static java.lang.foreign.ValueLayout.JAVA_INT;
import static net.codecrete.usb.windows.DevicePropertyKey.Service;
import static net.codecrete.usb.windows.Win.allocateErrorState;
-import static net.codecrete.usb.windows.WindowsUSBException.throwException;
-import static net.codecrete.usb.windows.WindowsUSBException.throwLastError;
+import static net.codecrete.usb.windows.WindowsUsbException.throwException;
+import static net.codecrete.usb.windows.WindowsUsbException.throwLastError;
/**
* Device information set (of Windows Setup API).
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/USBConstants.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/UsbConstants.java
similarity index 95%
rename from java-does-usb/src/main/java/net/codecrete/usb/windows/USBConstants.java
rename to java-does-usb/src/main/java/net/codecrete/usb/windows/UsbConstants.java
index c4e1857..da14dff 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/USBConstants.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/UsbConstants.java
@@ -13,9 +13,9 @@
* USB constants (general ones and Windows specific ones)
*/
@SuppressWarnings({"java:S125", "java:S1192", "java:S115", "java:S100"})
-class USBConstants {
+class UsbConstants {
- private USBConstants() {
+ private UsbConstants() {
}
static final byte USB_REQUEST_GET_DESCRIPTOR = 0x06;
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsAsyncTask.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsAsyncTask.java
index 3a50965..afc66e3 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsAsyncTask.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsAsyncTask.java
@@ -21,7 +21,7 @@
import static java.lang.foreign.MemorySegment.NULL;
import static java.lang.foreign.ValueLayout.*;
import static net.codecrete.usb.windows.Win.allocateErrorState;
-import static net.codecrete.usb.windows.WindowsUSBException.throwLastError;
+import static net.codecrete.usb.windows.WindowsUsbException.throwLastError;
/**
* Background task for handling asynchronous transfers.
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointInputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointInputStream.java
index d331954..0c96fc9 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointInputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointInputStream.java
@@ -7,23 +7,23 @@
package net.codecrete.usb.windows;
-import net.codecrete.usb.USBDirection;
+import net.codecrete.usb.UsbDirection;
import net.codecrete.usb.common.EndpointInputStream;
import net.codecrete.usb.common.Transfer;
public class WindowsEndpointInputStream extends EndpointInputStream {
- WindowsEndpointInputStream(WindowsUSBDevice device, int endpointNumber, int bufferSize) {
+ WindowsEndpointInputStream(WindowsUsbDevice device, int endpointNumber, int bufferSize) {
super(device, endpointNumber, bufferSize);
}
@Override
protected void submitTransferIn(Transfer transfer) {
- ((WindowsUSBDevice) device).submitTransferIn(endpointNumber, (WindowsTransfer) transfer);
+ ((WindowsUsbDevice) device).submitTransferIn(endpointNumber, (WindowsTransfer) transfer);
}
@Override
protected void configureEndpoint() {
- ((WindowsUSBDevice) device).configureForAsyncIo(USBDirection.IN, endpointNumber);
+ ((WindowsUsbDevice) device).configureForAsyncIo(UsbDirection.IN, endpointNumber);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointOutputStream.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointOutputStream.java
index 46eb0f3..8125dd5 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointOutputStream.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsEndpointOutputStream.java
@@ -7,23 +7,23 @@
package net.codecrete.usb.windows;
-import net.codecrete.usb.USBDirection;
+import net.codecrete.usb.UsbDirection;
import net.codecrete.usb.common.EndpointOutputStream;
import net.codecrete.usb.common.Transfer;
public class WindowsEndpointOutputStream extends EndpointOutputStream {
- WindowsEndpointOutputStream(WindowsUSBDevice device, int endpointNumber, int bufferSize) {
+ WindowsEndpointOutputStream(WindowsUsbDevice device, int endpointNumber, int bufferSize) {
super(device, endpointNumber, bufferSize);
}
@Override
protected void submitTransferOut(Transfer request) {
- ((WindowsUSBDevice) device).submitTransferOut(endpointNumber, (WindowsTransfer) request);
+ ((WindowsUsbDevice) device).submitTransferOut(endpointNumber, (WindowsTransfer) request);
}
@Override
protected void configureEndpoint() {
- ((WindowsUSBDevice) device).configureForAsyncIo(USBDirection.OUT, endpointNumber);
+ ((WindowsUsbDevice) device).configureForAsyncIo(UsbDirection.OUT, endpointNumber);
}
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDevice.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbDevice.java
similarity index 86%
rename from java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDevice.java
rename to java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbDevice.java
index f2d2d73..28dce23 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDevice.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbDevice.java
@@ -9,12 +9,13 @@
import net.codecrete.usb.*;
import net.codecrete.usb.common.Transfer;
-import net.codecrete.usb.common.USBDeviceImpl;
+import net.codecrete.usb.common.UsbDeviceImpl;
import net.codecrete.usb.usbstandard.SetupPacket;
import net.codecrete.usb.windows.gen.kernel32.Kernel32;
import net.codecrete.usb.windows.gen.winusb.WinUSB;
import net.codecrete.usb.windows.winsdk.Kernel32B;
import net.codecrete.usb.windows.winsdk.WinUSB2;
+import org.jetbrains.annotations.NotNull;
import java.io.InputStream;
import java.io.OutputStream;
@@ -33,16 +34,16 @@
import static net.codecrete.usb.windows.DevicePropertyKey.Children;
import static net.codecrete.usb.windows.DevicePropertyKey.HardwareIds;
import static net.codecrete.usb.windows.Win.allocateErrorState;
-import static net.codecrete.usb.windows.WindowsUSBException.throwException;
-import static net.codecrete.usb.windows.WindowsUSBException.throwLastError;
+import static net.codecrete.usb.windows.WindowsUsbException.throwException;
+import static net.codecrete.usb.windows.WindowsUsbException.throwLastError;
/**
* Windows implementation for USB device.
*/
@SuppressWarnings("java:S2160")
-public class WindowsUSBDevice extends USBDeviceImpl {
+public class WindowsUsbDevice extends UsbDeviceImpl {
- private static final System.Logger LOG = System.getLogger(WindowsUSBDevice.class.getName());
+ private static final System.Logger LOG = System.getLogger(WindowsUsbDevice.class.getName());
private final WindowsAsyncTask asyncTask;
/**
@@ -61,7 +62,7 @@ public class WindowsUSBDevice extends USBDeviceImpl {
*/
private boolean showAsOpen;
- WindowsUSBDevice(String devicePath, int vendorId, int productId, MemorySegment configDesc, boolean isComposite) {
+ WindowsUsbDevice(String devicePath, int vendorId, int productId, MemorySegment configDesc, boolean isComposite) {
super(devicePath, vendorId, productId);
asyncTask = WindowsAsyncTask.INSTANCE;
this.isComposite = isComposite;
@@ -76,7 +77,7 @@ private void readDescription(MemorySegment configDesc) {
// build list of interface handles
interfaceHandles = configuration.interfaces().stream()
.map(intf -> {
- var interfaceNumber = intf.number();
+ var interfaceNumber = intf.getNumber();
var function = configuration.findFunction(interfaceNumber);
return new InterfaceHandle(interfaceNumber, function.firstInterfaceNumber());
}).
@@ -84,13 +85,13 @@ private void readDescription(MemorySegment configDesc) {
}
@Override
- public boolean isOpen() {
+ public boolean isOpened() {
return showAsOpen;
}
@Override
public synchronized void open() {
- if (isOpen())
+ if (isOpened())
throwException("device is already open");
showAsOpen = true;
@@ -98,12 +99,12 @@ public synchronized void open() {
@Override
public synchronized void close() {
- if (!isOpen())
+ if (!isOpened())
return;
for (var intf : interfaceList) {
if (intf.isClaimed())
- releaseInterface(intf.number());
+ releaseInterface(intf.getNumber());
}
showAsOpen = false;
@@ -121,7 +122,7 @@ public void claimInterface(int interfaceNumber) {
numRetries -= 1;
if (numRetries == 0)
- throw new USBException("claiming interface failed (function has no device path / interface GUID, might be missing WinUSB driver)");
+ throw new UsbException("claiming interface failed (function has no device path / interface GUID, might be missing WinUSB driver)");
// sleep and retry
try {
@@ -206,9 +207,6 @@ public synchronized void selectAlternateSetting(int interfaceNumber, int alterna
// check alternate setting
var altSetting = intf.getAlternate(alternateNumber);
- if (altSetting == null)
- throwException("interface %d does not have an alternate interface setting %d", interfaceNumber,
- alternateNumber);
try (var arena = Arena.ofConfined()) {
var errorState = allocateErrorState(arena);
@@ -251,7 +249,7 @@ public synchronized void releaseInterface(int interfaceNumber) {
}
@Override
- public void controlTransferOut(USBControlTransfer setup, byte[] data) {
+ public void controlTransferOut(@NotNull UsbControlTransfer setup, byte[] data) {
try (var arena = Arena.ofConfined()) {
// copy data to native memory
@@ -267,22 +265,22 @@ public void controlTransferOut(USBControlTransfer setup, byte[] data) {
}
synchronized (transfer) {
- submitControlTransfer(USBDirection.OUT, setup, transfer);
- waitForTransfer(transfer, 0, USBDirection.OUT, 0);
+ submitControlTransfer(UsbDirection.OUT, setup, transfer);
+ waitForTransfer(transfer, 0, UsbDirection.OUT, 0);
}
}
}
@Override
- public byte[] controlTransferIn(USBControlTransfer setup, int length) {
+ public byte @NotNull [] controlTransferIn(@NotNull UsbControlTransfer setup, int length) {
try (var arena = Arena.ofConfined()) {
var transfer = createSyncControlTransfer();
transfer.setData(arena.allocate(length));
transfer.setDataSize(length);
synchronized (transfer) {
- submitControlTransfer(USBDirection.IN, setup, transfer);
- waitForTransfer(transfer, 0, USBDirection.IN, 0);
+ submitControlTransfer(UsbDirection.IN, setup, transfer);
+ waitForTransfer(transfer, 0, UsbDirection.IN, 0);
}
return transfer.data().asSlice(0, transfer.resultSize()).toArray(JAVA_BYTE);
@@ -290,7 +288,7 @@ public byte[] controlTransferIn(USBControlTransfer setup, int length) {
}
@Override
- public void transferOut(int endpointNumber, byte[] data, int offset, int length, int timeout) {
+ public void transferOut(int endpointNumber, byte @NotNull [] data, int offset, int length, int timeout) {
try (var arena = Arena.ofConfined()) {
var buffer = arena.allocate(data.length);
buffer.copyFrom(MemorySegment.ofArray(data).asSlice(offset, length));
@@ -298,14 +296,14 @@ public void transferOut(int endpointNumber, byte[] data, int offset, int length,
synchronized (transfer) {
submitTransferOut(endpointNumber, transfer);
- waitForTransfer(transfer, timeout, USBDirection.OUT, endpointNumber);
+ waitForTransfer(transfer, timeout, UsbDirection.OUT, endpointNumber);
}
}
}
@Override
- public byte[] transferIn(int endpointNumber, int timeout) {
- var endpoint = getEndpoint(USBDirection.IN, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ public byte @NotNull [] transferIn(int endpointNumber, int timeout) {
+ var endpoint = getEndpoint(UsbDirection.IN, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
try (var arena = Arena.ofConfined()) {
var buffer = arena.allocate(endpoint.packetSize());
@@ -313,7 +311,7 @@ public byte[] transferIn(int endpointNumber, int timeout) {
synchronized (transfer) {
submitTransferIn(endpointNumber, transfer);
- waitForTransfer(transfer, timeout, USBDirection.IN, endpointNumber);
+ waitForTransfer(transfer, timeout, UsbDirection.IN, endpointNumber);
}
return buffer.asSlice(0, transfer.resultSize()).toArray(JAVA_BYTE);
@@ -322,7 +320,7 @@ public byte[] transferIn(int endpointNumber, int timeout) {
private WindowsTransfer createSyncControlTransfer() {
var transfer = new WindowsTransfer();
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
return transfer;
}
@@ -330,7 +328,7 @@ private WindowsTransfer createSyncTransfer(MemorySegment data) {
var transfer = new WindowsTransfer();
transfer.setData(data);
transfer.setDataSize((int) data.byteSize());
- transfer.setCompletion(USBDeviceImpl::onSyncTransferCompleted);
+ transfer.setCompletion(UsbDeviceImpl::onSyncTransferCompleted);
return transfer;
}
@@ -344,14 +342,14 @@ protected void throwOSException(int errorCode, String message, Object... args) {
throwException(errorCode, message, args);
}
- synchronized void submitControlTransfer(USBDirection direction, USBControlTransfer setup, WindowsTransfer transfer) {
+ synchronized void submitControlTransfer(UsbDirection direction, UsbControlTransfer setup, WindowsTransfer transfer) {
checkIsOpen();
var intfHandle = findControlTransferInterface(setup);
try (var arena = Arena.ofConfined()) {
var setupPacket = new SetupPacket(arena);
var bmRequest =
- (direction == USBDirection.IN ? 0x80 : 0) | (setup.requestType().ordinal() << 5) | setup.recipient().ordinal();
+ (direction == UsbDirection.IN ? 0x80 : 0) | (setup.requestType().ordinal() << 5) | setup.recipient().ordinal();
setupPacket.setRequestType(bmRequest);
setupPacket.setRequest(setup.request());
setupPacket.setValue(setup.value());
@@ -372,7 +370,7 @@ synchronized void submitControlTransfer(USBDirection direction, USBControlTransf
}
synchronized void submitTransferOut(int endpointNumber, WindowsTransfer transfer) {
- var endpoint = getEndpoint(USBDirection.OUT, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ var endpoint = getEndpoint(UsbDirection.OUT, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
var intfHandle = getInterfaceHandle(endpoint.interfaceNumber());
try (var arena = Arena.ofConfined()) {
@@ -390,7 +388,7 @@ synchronized void submitTransferOut(int endpointNumber, WindowsTransfer transfer
}
synchronized void submitTransferIn(int endpointNumber, WindowsTransfer transfer) {
- var endpoint = getEndpoint(USBDirection.IN, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ var endpoint = getEndpoint(UsbDirection.IN, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
var intfHandle = getInterfaceHandle(endpoint.interfaceNumber());
try (var arena = Arena.ofConfined()) {
@@ -407,8 +405,8 @@ synchronized void submitTransferIn(int endpointNumber, WindowsTransfer transfer)
}
}
- synchronized void configureForAsyncIo(USBDirection direction, int endpointNumber) {
- var endpoint = getEndpoint(direction, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ synchronized void configureForAsyncIo(UsbDirection direction, int endpointNumber) {
+ var endpoint = getEndpoint(direction, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
var intfHandle = getInterfaceHandle(endpoint.interfaceNumber());
try (var arena = Arena.ofConfined()) {
@@ -427,8 +425,8 @@ synchronized void configureForAsyncIo(USBDirection direction, int endpointNumber
}
@Override
- public synchronized void clearHalt(USBDirection direction, int endpointNumber) {
- var endpoint = getEndpoint(direction, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ public synchronized void clearHalt(UsbDirection direction, int endpointNumber) {
+ var endpoint = getEndpoint(direction, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
var intfHandle = getInterfaceHandle(endpoint.interfaceNumber());
try (var arena = Arena.ofConfined()) {
@@ -439,8 +437,8 @@ public synchronized void clearHalt(USBDirection direction, int endpointNumber) {
}
@Override
- public synchronized void abortTransfers(USBDirection direction, int endpointNumber) {
- var endpoint = getEndpoint(direction, endpointNumber, USBTransferType.BULK, USBTransferType.INTERRUPT);
+ public synchronized void abortTransfers(UsbDirection direction, int endpointNumber) {
+ var endpoint = getEndpoint(direction, endpointNumber, UsbTransferType.BULK, UsbTransferType.INTERRUPT);
var intfHandle = getInterfaceHandle(endpoint.interfaceNumber());
try (var arena = Arena.ofConfined()) {
@@ -451,17 +449,17 @@ public synchronized void abortTransfers(USBDirection direction, int endpointNumb
}
@Override
- public synchronized InputStream openInputStream(int endpointNumber, int bufferSize) {
+ public synchronized @NotNull InputStream openInputStream(int endpointNumber, int bufferSize) {
// check that endpoint number is valid
- getEndpoint(USBDirection.IN, endpointNumber, USBTransferType.BULK, null);
+ getEndpoint(UsbDirection.IN, endpointNumber, UsbTransferType.BULK, null);
return new WindowsEndpointInputStream(this, endpointNumber, bufferSize);
}
@Override
- public synchronized OutputStream openOutputStream(int endpointNumber, int bufferSize) {
+ public synchronized @NotNull OutputStream openOutputStream(int endpointNumber, int bufferSize) {
// check that endpoint number is valid
- getEndpoint(USBDirection.OUT, endpointNumber, USBTransferType.BULK, null);
+ getEndpoint(UsbDirection.OUT, endpointNumber, UsbTransferType.BULK, null);
return new WindowsEndpointOutputStream(this, endpointNumber, bufferSize);
}
@@ -476,19 +474,19 @@ private InterfaceHandle getInterfaceHandle(int interfaceNumber) {
throw new AssertionError("not reached");
}
- private InterfaceHandle findControlTransferInterface(USBControlTransfer setup) {
+ private InterfaceHandle findControlTransferInterface(UsbControlTransfer setup) {
var interfaceNumber = -1;
int endpointNumber;
- if (setup.recipient() == USBRecipient.INTERFACE) {
+ if (setup.recipient() == UsbRecipient.INTERFACE) {
interfaceNumber = setup.index() & 0xff;
- } else if (setup.recipient() == USBRecipient.ENDPOINT) {
+ } else if (setup.recipient() == UsbRecipient.ENDPOINT) {
endpointNumber = setup.index() & 0x7f;
- var direction = (setup.index() & 0x80) != 0 ? USBDirection.IN : USBDirection.OUT;
+ var direction = (setup.index() & 0x80) != 0 ? UsbDirection.IN : UsbDirection.OUT;
if (endpointNumber != 0) {
interfaceNumber = getInterfaceNumber(direction, endpointNumber);
if (interfaceNumber == -1)
@@ -568,7 +566,7 @@ private String getChildDevicePath(String instanceId, int interfaceNumber) {
var devicePath = deviceInfoSet.getDevicePathByGUID(instanceId);
if (devicePath == null) {
LOG.log(INFO, "Child device {0} has no device path / interface GUID", instanceId);
- throw new USBException("claiming interface failed (function has no device path / interface GUID, might be missing WinUSB driver)");
+ throw new UsbException("claiming interface failed (function has no device path / interface GUID, might be missing WinUSB driver)");
}
if (devicePaths == null)
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDeviceRegistry.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbDeviceRegistry.java
similarity index 92%
rename from java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDeviceRegistry.java
rename to java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbDeviceRegistry.java
index bc8ab22..03fe5dc 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDeviceRegistry.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbDeviceRegistry.java
@@ -7,11 +7,11 @@
package net.codecrete.usb.windows;
-import net.codecrete.usb.USBDevice;
-import net.codecrete.usb.USBException;
+import net.codecrete.usb.UsbDevice;
+import net.codecrete.usb.UsbException;
import net.codecrete.usb.common.ScopeCleanup;
-import net.codecrete.usb.common.USBDeviceImpl;
-import net.codecrete.usb.common.USBDeviceRegistry;
+import net.codecrete.usb.common.UsbDeviceImpl;
+import net.codecrete.usb.common.UsbDeviceRegistry;
import net.codecrete.usb.usbstandard.ConfigurationDescriptor;
import net.codecrete.usb.usbstandard.DeviceDescriptor;
import net.codecrete.usb.usbstandard.SetupPacket;
@@ -37,11 +37,11 @@
import static java.lang.foreign.ValueLayout.*;
import static net.codecrete.usb.usbstandard.Constants.*;
import static net.codecrete.usb.windows.DevicePropertyKey.*;
-import static net.codecrete.usb.windows.USBConstants.GUID_DEVINTERFACE_USB_DEVICE;
-import static net.codecrete.usb.windows.USBConstants.GUID_DEVINTERFACE_USB_HUB;
+import static net.codecrete.usb.windows.UsbConstants.GUID_DEVINTERFACE_USB_DEVICE;
+import static net.codecrete.usb.windows.UsbConstants.GUID_DEVINTERFACE_USB_HUB;
import static net.codecrete.usb.windows.Win.allocateErrorState;
-import static net.codecrete.usb.windows.WindowsUSBException.throwException;
-import static net.codecrete.usb.windows.WindowsUSBException.throwLastError;
+import static net.codecrete.usb.windows.WindowsUsbException.throwException;
+import static net.codecrete.usb.windows.WindowsUsbException.throwLastError;
/**
* Windows implementation of USB device registry.
@@ -51,9 +51,9 @@
* the device's port number is needed.
*
*/
-public class WindowsUSBDeviceRegistry extends USBDeviceRegistry {
+public class WindowsUsbDeviceRegistry extends UsbDeviceRegistry {
- private static final System.Logger LOG = System.getLogger(WindowsUSBDeviceRegistry.class.getName());
+ private static final System.Logger LOG = System.getLogger(WindowsUsbDeviceRegistry.class.getName());
private static final long REQUEST_DATA_OFFSET
= _USB_DESCRIPTOR_REQUEST.$LAYOUT().byteOffset(PathElement.groupElement("Data"));
@@ -71,7 +71,7 @@ protected void monitorDevices() {
final var instance = Kernel32.GetModuleHandleW(NULL);
// create upcall for handling window messages
- var handleWindowMessageMH = MethodHandles.lookup().findVirtual(WindowsUSBDeviceRegistry.class,
+ var handleWindowMessageMH = MethodHandles.lookup().findVirtual(WindowsUsbDeviceRegistry.class,
"handleWindowMessage", MethodType.methodType(long.class, MemorySegment.class, int.class,
long.class, long.class)).bindTo(this);
var handleWindowMessageStub = Linker.nativeLinker().upcallStub(handleWindowMessageMH,
@@ -128,7 +128,7 @@ protected void monitorDevices() {
@SuppressWarnings("java:S106")
private void enumeratePresentDevices() {
- List deviceList = new ArrayList<>();
+ List deviceList = new ArrayList<>();
try (var cleanup = new ScopeCleanup();
var deviceInfoSet = DeviceInfoSet.ofPresentDevices(GUID_DEVINTERFACE_USB_DEVICE, null)) {
@@ -154,7 +154,7 @@ private void enumeratePresentDevices() {
}
}
- private USBDevice createDeviceFromDeviceInfo(DeviceInfoSet deviceInfoSet, String devicePath,
+ private UsbDevice createDeviceFromDeviceInfo(DeviceInfoSet deviceInfoSet, String devicePath,
HashMap hubHandles) {
try (var arena = Arena.ofConfined()) {
@@ -179,14 +179,14 @@ private USBDevice createDeviceFromDeviceInfo(DeviceInfoSet deviceInfoSet, String
}
/**
- * Retrieve device descriptor and create {@code USBDevice} instance
+ * Retrieve device descriptor and create {@code UsbDevice} instance
*
* @param devicePath the device path
* @param hubHandle the hub handle (parent)
* @param usbPortNum the USB port number
- * @return the {@code USBDevice} instance
+ * @return the {@code UsbDevice} instance
*/
- private USBDevice createDevice(String devicePath, boolean isComposite, MemorySegment hubHandle, int usbPortNum) {
+ private UsbDevice createDevice(String devicePath, boolean isComposite, MemorySegment hubHandle, int usbPortNum) {
try (var arena = Arena.ofConfined()) {
@@ -209,7 +209,7 @@ private USBDevice createDevice(String devicePath, boolean isComposite, MemorySeg
var configDesc = getDescriptor(hubHandle, usbPortNum, CONFIGURATION_DESCRIPTOR_TYPE, 0, (short) 0, arena);
// create new device
- var device = new WindowsUSBDevice(devicePath, vendorId, productId, configDesc, isComposite);
+ var device = new WindowsUsbDevice(devicePath, vendorId, productId, configDesc, isComposite);
device.setFromDeviceDescriptor(descriptorSegment);
device.setProductString(descriptorSegment, index -> getStringDescriptor(hubHandle, usbPortNum, index));
@@ -232,7 +232,7 @@ private MemorySegment getDescriptor(MemorySegment hubHandle, int usbPortNumber,
_USB_DESCRIPTOR_REQUEST.ConnectionIndex$set(descriptorRequest, usbPortNumber);
var setupPacket = new SetupPacket(_USB_DESCRIPTOR_REQUEST.SetupPacket$slice(descriptorRequest));
setupPacket.setRequestType(0x80); // device-to-host / type standard / recipient device
- setupPacket.setRequest(USBConstants.USB_REQUEST_GET_DESCRIPTOR);
+ setupPacket.setRequest(UsbConstants.USB_REQUEST_GET_DESCRIPTOR);
setupPacket.setValue((descriptorType << 8) | index);
setupPacket.setIndex(languageID);
setupPacket.setLength(size - (int) REQUEST_DATA_OFFSET);
@@ -277,7 +277,7 @@ private String getStringDescriptor(MemorySegment hubHandle, int usbPortNumber, i
index, DEFAULT_LANGUAGE, arena));
return stringDesc.string();
- } catch (USBException e) {
+ } catch (UsbException e) {
return null;
}
}
@@ -344,10 +344,10 @@ private void onDeviceDisconnected(String devicePath) {
* @return index, or -1 if not found
*/
@Override
- protected int findDeviceIndex(List deviceList, Object deviceId) {
+ protected int findDeviceIndex(List deviceList, Object deviceId) {
var id = deviceId.toString();
for (var i = 0; i < deviceList.size(); i++) {
- var dev = (USBDeviceImpl) deviceList.get(i);
+ var dev = (UsbDeviceImpl) deviceList.get(i);
if (id.equalsIgnoreCase(dev.getUniqueId().toString()))
return i;
}
diff --git a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBException.java b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbException.java
similarity index 91%
rename from java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBException.java
rename to java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbException.java
index 7b93a57..734a03f 100644
--- a/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBException.java
+++ b/java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUsbException.java
@@ -6,8 +6,8 @@
//
package net.codecrete.usb.windows;
-import net.codecrete.usb.USBException;
-import net.codecrete.usb.USBStallException;
+import net.codecrete.usb.UsbException;
+import net.codecrete.usb.UsbStallException;
import net.codecrete.usb.windows.gen.kernel32.Kernel32;
import net.codecrete.usb.windows.gen.ntdll.NtDll;
@@ -21,7 +21,7 @@
/**
* Exception thrown if a Windows specific error occurs.
*/
-public class WindowsUSBException extends USBException {
+public class WindowsUsbException extends UsbException {
/**
* Creates a new instance.
@@ -32,7 +32,7 @@ public class WindowsUSBException extends USBException {
* @param message exception message
* @param errorCode Windows error code (usually returned from {@code GetLastError()})
*/
- public WindowsUSBException(String message, int errorCode) {
+ public WindowsUsbException(String message, int errorCode) {
super(String.format("%s: %s", message, getErrorMessage(errorCode)), errorCode);
}
@@ -49,9 +49,9 @@ public WindowsUSBException(String message, int errorCode) {
static void throwException(int errorCode, String message, Object... args) {
var formattedMessage = String.format(message, args);
if (errorCode == Kernel32.ERROR_GEN_FAILURE() || errorCode == NtDll.STATUS_UNSUCCESSFUL()) {
- throw new USBStallException(formattedMessage);
+ throw new UsbStallException(formattedMessage);
} else {
- throw new WindowsUSBException(formattedMessage, errorCode);
+ throw new WindowsUsbException(formattedMessage, errorCode);
}
}
@@ -62,7 +62,7 @@ static void throwException(int errorCode, String message, Object... args) {
* @param args arguments for exception message
*/
static void throwException(String message, Object... args) {
- throw new USBException(String.format(message, args));
+ throw new UsbException(String.format(message, args));
}
/**
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/AlternateInterfaceTest.java b/java-does-usb/src/test/java/net/codecrete/usb/AlternateInterfaceTest.java
index 857b9b6..2cf058b 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/AlternateInterfaceTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/AlternateInterfaceTest.java
@@ -28,19 +28,19 @@ void selectAlternateIntf_succeeds() {
testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 1);
- var altIntf = testDevice.getInterface(LOOPBACK_INTF_LOOPBACK).alternate();
+ var altIntf = testDevice.getInterface(LOOPBACK_INTF_LOOPBACK).getCurrentAlternate();
assertNotNull(altIntf);
- assertEquals(2, altIntf.endpoints().size());
- assertEquals(0xff, altIntf.classCode());
+ assertEquals(2, altIntf.getEndpoints().size());
+ assertEquals(0xff, altIntf.getClassCode());
testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 0);
}
@Test
void selectInvalidAlternateIntf_fails() {
- assertThrows(USBException.class, () -> testDevice.selectAlternateSetting(1, 0));
+ assertThrows(UsbException.class, () -> testDevice.selectAlternateSetting(1, 0));
- assertThrows(USBException.class, () -> testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 2));
+ assertThrows(UsbException.class, () -> testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 2));
}
@Test
@@ -57,8 +57,8 @@ void transferOnValidEndpoint_succeeds() {
void transferOnInvalidEndpoint_fails() {
testDevice.selectAlternateSetting(LOOPBACK_INTF_LOOPBACK, 1);
- assertThrows(USBException.class, () -> testDevice.transferOut(ECHO_EP_OUT, new byte[] { 1, 2, 3 }));
+ assertThrows(UsbException.class, () -> testDevice.transferOut(ECHO_EP_OUT, new byte[] { 1, 2, 3 }));
- assertThrows(USBException.class, () -> testDevice.transferIn(ECHO_EP_IN));
+ assertThrows(UsbException.class, () -> testDevice.transferIn(ECHO_EP_IN));
}
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/BulkTransferTest.java b/java-does-usb/src/test/java/net/codecrete/usb/BulkTransferTest.java
index 2418fd9..da1f5c7 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/BulkTransferTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/BulkTransferTest.java
@@ -39,8 +39,8 @@ void mediumTransfer_succeeds() {
@Test
void transferWithZLP_succeeds() {
- var inEndpoint = testDevice.getEndpoint(USBDirection.IN, LOOPBACK_EP_IN);
- var sampleData = generateRandomBytes(inEndpoint.packetSize(), 97333894);
+ var inEndpoint = testDevice.getEndpoint(UsbDirection.IN, LOOPBACK_EP_IN);
+ var sampleData = generateRandomBytes(inEndpoint.getPacketSize(), 97333894);
testDevice.transferOut(LOOPBACK_EP_OUT, sampleData);
testDevice.transferOut(LOOPBACK_EP_OUT, new byte[0]);
var data = testDevice.transferIn(LOOPBACK_EP_IN);
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/ConfigurationParserTest.java b/java-does-usb/src/test/java/net/codecrete/usb/ConfigurationParserTest.java
index 6a3d11d..ee55be3 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/ConfigurationParserTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/ConfigurationParserTest.java
@@ -20,16 +20,16 @@ void simpleDescriptor_canBeParsed() {
softly.assertThat(configuration.interfaces())
.hasSize(1)
.singleElement().satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(0);
- softly.assertThat(intf.alternates())
+ softly.assertThat(intf.getNumber()).isEqualTo(0);
+ softly.assertThat(intf.getAlternates())
.hasSize(1)
.singleElement().satisfies(altIntf -> {
- softly.assertThat(altIntf).isSameAs(intf.alternate());
- softly.assertThat(altIntf.number()).isEqualTo(0);
- softly.assertThat(altIntf.endpoints()).isEmpty();
- softly.assertThat(altIntf.classCode()).isEqualTo(0x0ff);
- softly.assertThat(altIntf.subclassCode()).isEqualTo(0x0dd);
- softly.assertThat(altIntf.protocolCode()).isEqualTo(0x0cc);
+ softly.assertThat(altIntf).isSameAs(intf.getCurrentAlternate());
+ softly.assertThat(altIntf.getNumber()).isEqualTo(0);
+ softly.assertThat(altIntf.getEndpoints()).isEmpty();
+ softly.assertThat(altIntf.getClassCode()).isEqualTo(0x0ff);
+ softly.assertThat(altIntf.getSubclassCode()).isEqualTo(0x0dd);
+ softly.assertThat(altIntf.getProtocolCode()).isEqualTo(0x0cc);
});
softly.assertThat(intf.isClaimed()).isFalse();
});
@@ -67,65 +67,65 @@ void largeCompositeDescriptor_canBeParsed() {
// interface 0
softly.assertThat(configuration.interfaces().get(0)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(0);
- softly.assertThat(intf.alternates()).hasSize(1);
- softly.assertThat(intf.alternate().endpoints()).hasSize(1);
- softly.assertThat(intf.alternate().endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(5);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.INTERRUPT);
+ softly.assertThat(intf.getNumber()).isEqualTo(0);
+ softly.assertThat(intf.getAlternates()).hasSize(1);
+ softly.assertThat(intf.getCurrentAlternate().getEndpoints()).hasSize(1);
+ softly.assertThat(intf.getCurrentAlternate().getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(5);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.INTERRUPT);
});
});
// interface 1
softly.assertThat(configuration.interfaces().get(1)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(1);
- softly.assertThat(intf.alternates()).hasSize(2);
- softly.assertThat(intf.alternates().get(0)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(0);
- softly.assertThat(alternate.endpoints()).isEmpty();
+ softly.assertThat(intf.getNumber()).isEqualTo(1);
+ softly.assertThat(intf.getAlternates()).hasSize(2);
+ softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(0);
+ softly.assertThat(alternate.getEndpoints()).isEmpty();
});
- softly.assertThat(intf.alternates().get(1)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(1);
- softly.assertThat(alternate.endpoints()).hasSize(1);
- softly.assertThat(alternate.endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(1);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.ISOCHRONOUS);
+ softly.assertThat(intf.getAlternates().get(1)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(1);
+ softly.assertThat(alternate.getEndpoints()).hasSize(1);
+ softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(1);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.ISOCHRONOUS);
});
});
});
// interface 2
softly.assertThat(configuration.interfaces().get(2)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(2);
- softly.assertThat(intf.alternates()).hasSize(2);
- softly.assertThat(intf.alternates().get(0)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(0);
- softly.assertThat(alternate.endpoints()).isEmpty();
+ softly.assertThat(intf.getNumber()).isEqualTo(2);
+ softly.assertThat(intf.getAlternates()).hasSize(2);
+ softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(0);
+ softly.assertThat(alternate.getEndpoints()).isEmpty();
});
- softly.assertThat(intf.alternates().get(1)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(1);
- softly.assertThat(alternate.endpoints()).hasSize(1);
- softly.assertThat(alternate.endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(2);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.ISOCHRONOUS);
+ softly.assertThat(intf.getAlternates().get(1)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(1);
+ softly.assertThat(alternate.getEndpoints()).hasSize(1);
+ softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(2);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.ISOCHRONOUS);
});
});
});
// interface 3
softly.assertThat(configuration.interfaces().get(3)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(3);
- softly.assertThat(intf.alternates()).hasSize(1);
- softly.assertThat(intf.alternates().get(0)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(0);
- softly.assertThat(alternate.endpoints()).hasSize(1);
- softly.assertThat(alternate.endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(4);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.INTERRUPT);
+ softly.assertThat(intf.getNumber()).isEqualTo(3);
+ softly.assertThat(intf.getAlternates()).hasSize(1);
+ softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(0);
+ softly.assertThat(alternate.getEndpoints()).hasSize(1);
+ softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(4);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.INTERRUPT);
});
});
});
@@ -161,62 +161,62 @@ void compositeTestDeviceDescriptor_canBeParsed() {
// interface 0
softly.assertThat(configuration.interfaces().get(0)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(0);
- softly.assertThat(intf.alternates()).hasSize(1);
- softly.assertThat(intf.alternate().endpoints()).hasSize(1);
- softly.assertThat(intf.alternate().endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(3);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.INTERRUPT);
+ softly.assertThat(intf.getNumber()).isEqualTo(0);
+ softly.assertThat(intf.getAlternates()).hasSize(1);
+ softly.assertThat(intf.getCurrentAlternate().getEndpoints()).hasSize(1);
+ softly.assertThat(intf.getCurrentAlternate().getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(3);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.INTERRUPT);
});
});
// interface 1
softly.assertThat(configuration.interfaces().get(1)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(1);
- softly.assertThat(intf.alternates()).hasSize(1);
- softly.assertThat(intf.alternates().get(0)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(0);
- softly.assertThat(alternate.endpoints()).hasSize(2);
- softly.assertThat(alternate.endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(2);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.OUT);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.BULK);
+ softly.assertThat(intf.getNumber()).isEqualTo(1);
+ softly.assertThat(intf.getAlternates()).hasSize(1);
+ softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(0);
+ softly.assertThat(alternate.getEndpoints()).hasSize(2);
+ softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(2);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.OUT);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.BULK);
});
- softly.assertThat(alternate.endpoints().get(1)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(1);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.BULK);
+ softly.assertThat(alternate.getEndpoints().get(1)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(1);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.BULK);
});
});
});
// interface 2
softly.assertThat(configuration.interfaces().get(2)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(2);
- softly.assertThat(intf.alternates()).hasSize(1);
- softly.assertThat(intf.alternates().get(0)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(0);
- softly.assertThat(alternate.endpoints()).isEmpty();
+ softly.assertThat(intf.getNumber()).isEqualTo(2);
+ softly.assertThat(intf.getAlternates()).hasSize(1);
+ softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(0);
+ softly.assertThat(alternate.getEndpoints()).isEmpty();
});
});
// interface 3
softly.assertThat(configuration.interfaces().get(3)).satisfies(intf -> {
- softly.assertThat(intf.number()).isEqualTo(3);
- softly.assertThat(intf.alternates()).hasSize(1);
- softly.assertThat(intf.alternates().get(0)).satisfies(alternate -> {
- softly.assertThat(alternate.number()).isEqualTo(0);
- softly.assertThat(alternate.endpoints()).hasSize(2);
- softly.assertThat(alternate.endpoints().get(0)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(1);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.OUT);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.BULK);
+ softly.assertThat(intf.getNumber()).isEqualTo(3);
+ softly.assertThat(intf.getAlternates()).hasSize(1);
+ softly.assertThat(intf.getAlternates().get(0)).satisfies(alternate -> {
+ softly.assertThat(alternate.getNumber()).isEqualTo(0);
+ softly.assertThat(alternate.getEndpoints()).hasSize(2);
+ softly.assertThat(alternate.getEndpoints().get(0)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(1);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.OUT);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.BULK);
});
- softly.assertThat(alternate.endpoints().get(1)).satisfies(endpoint -> {
- softly.assertThat(endpoint.number()).isEqualTo(2);
- softly.assertThat(endpoint.direction()).isEqualTo(USBDirection.IN);
- softly.assertThat(endpoint.transferType()).isEqualTo(USBTransferType.BULK);
+ softly.assertThat(alternate.getEndpoints().get(1)).satisfies(endpoint -> {
+ softly.assertThat(endpoint.getNumber()).isEqualTo(2);
+ softly.assertThat(endpoint.getDirection()).isEqualTo(UsbDirection.IN);
+ softly.assertThat(endpoint.getTransferType()).isEqualTo(UsbTransferType.BULK);
});
});
});
@@ -231,7 +231,7 @@ void tooShortDescriptor_throwsException() {
var segment = MemorySegment.ofArray(desc);
assertThatThrownBy(() -> ConfigurationParser.parseConfigurationDescriptor(segment))
- .isInstanceOf(USBException.class)
+ .isInstanceOf(UsbException.class)
.hasMessage("invalid USB configuration descriptor (invalid length)");
}
@@ -242,7 +242,7 @@ void tooLongDescriptor_throwsException() {
var segment = MemorySegment.ofArray(desc);
assertThatThrownBy(() -> ConfigurationParser.parseConfigurationDescriptor(segment))
- .isInstanceOf(USBException.class)
+ .isInstanceOf(UsbException.class)
.hasMessage("invalid USB configuration descriptor (invalid length)");
}
@@ -252,7 +252,7 @@ void invalidDescriptor_throwsException() {
var segment = MemorySegment.ofArray(desc);
assertThatThrownBy(() -> ConfigurationParser.parseConfigurationDescriptor(segment))
- .isInstanceOf(USBException.class)
+ .isInstanceOf(UsbException.class)
.hasMessage("invalid USB configuration descriptor");
}
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/ControlTransferTest.java b/java-does-usb/src/test/java/net/codecrete/usb/ControlTransferTest.java
index 98131c4..de26d03 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/ControlTransferTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/ControlTransferTest.java
@@ -11,8 +11,7 @@
import org.junit.jupiter.api.Test;
-import static org.junit.jupiter.api.Assertions.assertArrayEquals;
-import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests control transfers
@@ -21,13 +20,14 @@ class ControlTransferTest extends TestDeviceBase {
@Test
void storeValue_succeeds() {
- testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x01, (short) 10730, (short) interfaceNumber), null);
+ var setup = new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x01, (short) 10730, (short) interfaceNumber);
+ assertDoesNotThrow(() -> testDevice.controlTransferOut(setup, null));
}
@Test
void retrieveValue_isSameAsStored() {
- testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x01, (short) 0x9a41, (short) interfaceNumber), null);
- var valueBytes = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
+ testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x01, (short) 0x9a41, (short) interfaceNumber), null);
+ var valueBytes = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
var expectedBytes = new byte[]{(byte) 0x41, (byte) 0x9a, (byte) 0x00, (byte) 0x00};
assertArrayEquals(expectedBytes, valueBytes);
}
@@ -35,19 +35,19 @@ void retrieveValue_isSameAsStored() {
@Test
void storeValueInDataStage_canBeRetrieved() {
var sentValue = new byte[]{(byte) 0x83, (byte) 0x03, (byte) 0xda, (byte) 0x3e};
- testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x02, (short) 0, (short) interfaceNumber), sentValue);
- var retrievedValue = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
+ testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x02, (short) 0, (short) interfaceNumber), sentValue);
+ var retrievedValue = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x03, (short) 0, (short) interfaceNumber), 4);
assertArrayEquals(sentValue, retrievedValue);
}
@Test
void interfaceNumber_canBeRetrieved() {
- var response = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) interfaceNumber), 1);
+ var response = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) interfaceNumber), 1);
assertEquals(interfaceNumber, response[0] & 0xff);
if (isCompositeDevce()) {
testDevice.claimInterface(2);
- response = testDevice.controlTransferIn(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) 2), 1);
+ response = testDevice.controlTransferIn(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x05, (short) 0, (short) 2), 1);
assertEquals(2, response[0] & 0xff);
testDevice.releaseInterface(2);
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/DescriptionTest.java b/java-does-usb/src/test/java/net/codecrete/usb/DescriptionTest.java
index fc443c5..79b2ca8 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/DescriptionTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/DescriptionTest.java
@@ -20,121 +20,134 @@ class DescriptionTest extends TestDeviceBase {
@Test
void deviceInfo_isCorrect() {
- assertEquals("JavaDoesUSB", testDevice.manufacturer());
- assertEquals(isLoopbackDevice() ? "Loopback" : "Composite", testDevice.product());
- assertEquals(12, testDevice.serialNumber().length());
+ assertEquals("JavaDoesUSB", testDevice.getManufacturer());
+ assertEquals(isLoopbackDevice() ? "Loopback" : "Composite", testDevice.getProduct());
+ assertEquals(12, testDevice.getSerialNumber().length());
if (isLoopbackDevice()) {
// simple device
- assertEquals(0xff, testDevice.classCode());
- assertEquals(0x00, testDevice.subclassCode());
- assertEquals(0x00, testDevice.protocolCode());
+ assertEquals(0xff, testDevice.getClassCode());
+ assertEquals(0x00, testDevice.getSubclassCode());
+ assertEquals(0x00, testDevice.getProtocolCode());
} else {
// composite device
- assertEquals(0xef, testDevice.classCode());
- assertEquals(0x02, testDevice.subclassCode());
- assertEquals(0x01, testDevice.protocolCode());
+ assertEquals(0xef, testDevice.getClassCode());
+ assertEquals(0x02, testDevice.getSubclassCode());
+ assertEquals(0x01, testDevice.getProtocolCode());
}
var isComposite = isCompositeDevce();
- assertEquals(2, testDevice.usbVersion().major());
- assertEquals(isComposite ? 1 : 0, testDevice.usbVersion().minor());
- assertEquals(0, testDevice.usbVersion().subminor());
+ assertEquals(2, testDevice.getUsbVersion().getMajor());
+ assertEquals(isComposite ? 1 : 0, testDevice.getUsbVersion().getMinor());
+ assertEquals(0, testDevice.getUsbVersion().getSubminor());
- assertEquals(0, testDevice.deviceVersion().major());
- assertEquals(isComposite ? 3 : 7, testDevice.deviceVersion().minor());
- assertEquals(isComposite ? 6 : 4, testDevice.deviceVersion().subminor());
+ assertEquals(0, testDevice.getDeviceVersion().getMajor());
+ assertEquals(isComposite ? 3 : 7, testDevice.getDeviceVersion().getMinor());
+ assertEquals(isComposite ? 6 : 4, testDevice.getDeviceVersion().getSubminor());
}
@Test
void interfaceDescriptor_isCorrect() {
- assertNotNull(testDevice.interfaces());
- assertEquals(interfaceNumber + 1, testDevice.interfaces().size());
+ assertNotNull(testDevice.getInterfaces());
+ assertEquals(interfaceNumber + 1, testDevice.getInterfaces().size());
- var intf = testDevice.interfaces().get(interfaceNumber);
- assertEquals(interfaceNumber, intf.number());
- assertNotNull(intf.alternate());
+ var intf = testDevice.getInterfaces().get(interfaceNumber);
+ assertEquals(interfaceNumber, intf.getNumber());
+ assertNotNull(intf.getCurrentAlternate());
assertTrue(intf.isClaimed());
}
+ @Test
+ void invalidInterfaceNumber_shouldThrow() {
+ assertThrows(UsbException.class, () -> testDevice.getInterface(4));
+ }
+
@Test
void alternateInterfaceDescriptor_isCorrect() {
- var intf = testDevice.interfaces().get(interfaceNumber);
- var altIntf = intf.alternate();
- assertNotNull(intf.alternates());
- assertEquals(isLoopbackDevice() ? 2 : 1, intf.alternates().size());
- assertSame(intf.alternates().get(0), altIntf);
- assertEquals(0, altIntf.number());
+ var intf = testDevice.getInterfaces().get(interfaceNumber);
+ var altIntf = intf.getCurrentAlternate();
+ assertNotNull(intf.getAlternates());
+ assertEquals(isLoopbackDevice() ? 2 : 1, intf.getAlternates().size());
+ assertSame(intf.getAlternates().get(0), altIntf);
+ assertEquals(0, altIntf.getNumber());
- assertEquals(0xff, altIntf.classCode());
- assertEquals(0x00, altIntf.subclassCode());
- assertEquals(0x00, altIntf.protocolCode());
+ assertEquals(0xff, altIntf.getClassCode());
+ assertEquals(0x00, altIntf.getSubclassCode());
+ assertEquals(0x00, altIntf.getProtocolCode());
if (isLoopbackDevice()) {
- altIntf = intf.alternates().get(1);
- assertEquals(1, altIntf.number());
+ altIntf = intf.getAlternates().get(1);
+ assertEquals(1, altIntf.getNumber());
- assertEquals(0xff, altIntf.classCode());
- assertEquals(0x00, altIntf.subclassCode());
- assertEquals(0x00, altIntf.protocolCode());
+ assertEquals(0xff, altIntf.getClassCode());
+ assertEquals(0x00, altIntf.getSubclassCode());
+ assertEquals(0x00, altIntf.getProtocolCode());
}
}
@Test
void endpointDescriptors_areCorrect() {
- var altIntf = testDevice.interfaces().get(interfaceNumber).alternate();
- assertNotNull(altIntf.endpoints());
- assertEquals(isLoopbackDevice() ? 4 : 2, altIntf.endpoints().size());
-
- var endpoint = altIntf.endpoints().get(0);
- assertEquals(1, endpoint.number());
- assertEquals(USBDirection.OUT, endpoint.direction());
- assertEquals(USBTransferType.BULK, endpoint.transferType());
- assertTrue(endpoint.packetSize() == 64 || endpoint.packetSize() == 512);
-
- endpoint = altIntf.endpoints().get(1);
- assertEquals(2, endpoint.number());
- assertEquals(USBDirection.IN, endpoint.direction());
- assertEquals(USBTransferType.BULK, endpoint.transferType());
- assertTrue(endpoint.packetSize() == 64 || endpoint.packetSize() == 512);
+ var altIntf = testDevice.getInterfaces().get(interfaceNumber).getCurrentAlternate();
+ assertNotNull(altIntf.getEndpoints());
+ assertEquals(isLoopbackDevice() ? 4 : 2, altIntf.getEndpoints().size());
+
+ var endpoint = altIntf.getEndpoints().get(0);
+ assertEquals(1, endpoint.getNumber());
+ assertEquals(UsbDirection.OUT, endpoint.getDirection());
+ assertEquals(UsbTransferType.BULK, endpoint.getTransferType());
+ assertTrue(endpoint.getPacketSize() == 64 || endpoint.getPacketSize() == 512);
+
+ endpoint = altIntf.getEndpoints().get(1);
+ assertEquals(2, endpoint.getNumber());
+ assertEquals(UsbDirection.IN, endpoint.getDirection());
+ assertEquals(UsbTransferType.BULK, endpoint.getTransferType());
+ assertTrue(endpoint.getPacketSize() == 64 || endpoint.getPacketSize() == 512);
if (isLoopbackDevice()) {
- endpoint = altIntf.endpoints().get(2);
- assertEquals(3, endpoint.number());
- assertEquals(USBDirection.OUT, endpoint.direction());
- assertEquals(USBTransferType.INTERRUPT, endpoint.transferType());
- assertEquals(16, endpoint.packetSize());
-
- endpoint = altIntf.endpoints().get(3);
- assertEquals(3, endpoint.number());
- assertEquals(USBDirection.IN, endpoint.direction());
- assertEquals(USBTransferType.INTERRUPT, endpoint.transferType());
- assertEquals(16, endpoint.packetSize());
+ endpoint = altIntf.getEndpoints().get(2);
+ assertEquals(3, endpoint.getNumber());
+ assertEquals(UsbDirection.OUT, endpoint.getDirection());
+ assertEquals(UsbTransferType.INTERRUPT, endpoint.getTransferType());
+ assertEquals(16, endpoint.getPacketSize());
+
+ endpoint = altIntf.getEndpoints().get(3);
+ assertEquals(3, endpoint.getNumber());
+ assertEquals(UsbDirection.IN, endpoint.getDirection());
+ assertEquals(UsbTransferType.INTERRUPT, endpoint.getTransferType());
+ assertEquals(16, endpoint.getPacketSize());
// test alternate interface 1
- altIntf = testDevice.interfaces().get(interfaceNumber).alternates().get(1);
- assertEquals(2, altIntf.endpoints().size());
-
- endpoint = altIntf.endpoints().get(0);
- assertEquals(1, endpoint.number());
- assertEquals(USBDirection.OUT, endpoint.direction());
- assertEquals(USBTransferType.BULK, endpoint.transferType());
- assertTrue(endpoint.packetSize() == 64 || endpoint.packetSize() == 512);
-
- endpoint = altIntf.endpoints().get(1);
- assertEquals(2, endpoint.number());
- assertEquals(USBDirection.IN, endpoint.direction());
- assertEquals(USBTransferType.BULK, endpoint.transferType());
- assertTrue(endpoint.packetSize() == 64 || endpoint.packetSize() == 512);
+ altIntf = testDevice.getInterfaces().get(interfaceNumber).getAlternates().get(1);
+ assertEquals(2, altIntf.getEndpoints().size());
+
+ endpoint = altIntf.getEndpoints().get(0);
+ assertEquals(1, endpoint.getNumber());
+ assertEquals(UsbDirection.OUT, endpoint.getDirection());
+ assertEquals(UsbTransferType.BULK, endpoint.getTransferType());
+ assertTrue(endpoint.getPacketSize() == 64 || endpoint.getPacketSize() == 512);
+
+ endpoint = altIntf.getEndpoints().get(1);
+ assertEquals(2, endpoint.getNumber());
+ assertEquals(UsbDirection.IN, endpoint.getDirection());
+ assertEquals(UsbTransferType.BULK, endpoint.getTransferType());
+ assertTrue(endpoint.getPacketSize() == 64 || endpoint.getPacketSize() == 512);
}
}
+ @Test
+ void invalidEndpoint_shouldThrow() {
+ assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.IN, 1));
+ assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.OUT, 4));
+ assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.IN, 0));
+ assertThrows(UsbException.class, () -> testDevice.getEndpoint(UsbDirection.OUT, 0));
+ }
+
@Test
void configurationDescription_isAvailable() {
var expectedLength = isLoopbackDevice() ? 69 : 115;
- var configDesc = testDevice.configurationDescriptor();
+ var configDesc = testDevice.getConfigurationDescriptor();
assertNotNull(configDesc);
assertEquals(expectedLength, configDesc.length);
assertEquals(2, configDesc[1]);
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/DescriptorTest.java b/java-does-usb/src/test/java/net/codecrete/usb/DescriptorTest.java
index 3b4f4bb..4ad8b7d 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/DescriptorTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/DescriptorTest.java
@@ -8,14 +8,14 @@ class DescriptorTest extends TestDeviceBase {
@Test
void deviceDescriptor_isAvailable() {
- var desc = testDevice.deviceDescriptor();
+ var desc = testDevice.getDeviceDescriptor();
assertThat(desc).hasSize(18);
assertThat(desc[1]).isEqualTo((byte) 0x01);
}
@Test
void configurationDescriptor_isAvailable() {
- var desc = testDevice.configurationDescriptor();
+ var desc = testDevice.getConfigurationDescriptor();
assertThat(desc).hasSizeGreaterThan(60);
assertThat(desc[1]).isEqualTo((byte) 0x02);
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/DeviceEnumerationTest.java b/java-does-usb/src/test/java/net/codecrete/usb/DeviceEnumerationTest.java
index 02a3f84..8438444 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/DeviceEnumerationTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/DeviceEnumerationTest.java
@@ -17,33 +17,33 @@ class DeviceEnumerationTest extends TestDeviceBase {
@Test
void getAllDevices_includesLoopback() {
- var deviceList = USB.getAllDevices();
+ var deviceList = Usb.getDevices();
assertThat(deviceList)
.isNotEmpty()
- .anyMatch(device -> device.vendorId() == vid && device.productId() == pid);
+ .anyMatch(device -> device.getVendorId() == vid && device.getProductId() == pid);
}
@Test
void getDevices_includesLoopback() {
- var deviceList = USB.getDevices(device -> device.vendorId() == vid && device.productId() == pid);
+ var deviceList = Usb.findDevices(device -> device.getVendorId() == vid && device.getProductId() == pid);
assertThat(deviceList)
.isNotEmpty()
- .anyMatch(device -> device.vendorId() == vid && device.productId() == pid);
+ .anyMatch(device -> device.getVendorId() == vid && device.getProductId() == pid);
}
@Test
void getDevicePredicate_returnsLoopback() {
- var device = USB.getDevice(dev -> dev.vendorId() == vid && dev.productId() == pid);
+ var device = Usb.findDevice(dev -> dev.getVendorId() == vid && dev.getProductId() == pid);
assertThat(device).isPresent();
- assertThat(device.get().productId()).isEqualTo(pid);
- assertThat(device.get().vendorId()).isEqualTo(vid);
+ assertThat(device.get().getProductId()).isEqualTo(pid);
+ assertThat(device.get().getVendorId()).isEqualTo(vid);
}
@Test
void getDeviceVidPid_returnsLoopback() {
- var device = USB.getDevice(vid, pid);
+ var device = Usb.findDevice(vid, pid);
assertThat(device).isPresent();
- assertThat(device.get().productId()).isEqualTo(pid);
- assertThat(device.get().vendorId()).isEqualTo(vid);
+ assertThat(device.get().getProductId()).isEqualTo(pid);
+ assertThat(device.get().getVendorId()).isEqualTo(vid);
}
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/DeviceLifecycleTest.java b/java-does-usb/src/test/java/net/codecrete/usb/DeviceLifecycleTest.java
index af45e92..2e40e5d 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/DeviceLifecycleTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/DeviceLifecycleTest.java
@@ -16,48 +16,48 @@
class DeviceLifecycleTest {
- private USBDevice device;
+ private UsbDevice device;
@Test
void lifecycle_showsValidState() {
device = TestDeviceBase.getDevice();
var interfaceNumber = TestDeviceBase.getInterfaceNumber(device);
- var intf = device.interfaces().get(interfaceNumber);
- assertEquals(interfaceNumber, intf.number());
+ var intf = device.getInterfaces().get(interfaceNumber);
+ assertEquals(interfaceNumber, intf.getNumber());
- assertFalse(device.isOpen());
+ assertFalse(device.isOpened());
assertFalse(intf.isClaimed());
- assertThrows(USBException.class, () -> device.claimInterface(interfaceNumber));
- assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.claimInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.releaseInterface(interfaceNumber));
device.open();
- assertTrue(device.isOpen());
+ assertTrue(device.isOpened());
assertFalse(intf.isClaimed());
- assertThrows(USBException.class, () -> device.open());
- assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.open());
+ assertThrows(UsbException.class, () -> device.releaseInterface(interfaceNumber));
device.claimInterface(interfaceNumber);
- assertTrue(device.isOpen());
+ assertTrue(device.isOpened());
assertTrue(intf.isClaimed());
- assertThrows(USBException.class, () -> device.open());
- assertThrows(USBException.class, () -> device.claimInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.open());
+ assertThrows(UsbException.class, () -> device.claimInterface(interfaceNumber));
device.releaseInterface(interfaceNumber);
- assertTrue(device.isOpen());
+ assertTrue(device.isOpened());
assertFalse(intf.isClaimed());
- assertThrows(USBException.class, () -> device.open());
- assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.open());
+ assertThrows(UsbException.class, () -> device.releaseInterface(interfaceNumber));
device.close();
- assertFalse(device.isOpen());
+ assertFalse(device.isOpened());
assertFalse(intf.isClaimed());
- assertThrows(USBException.class, () -> device.claimInterface(interfaceNumber));
- assertThrows(USBException.class, () -> device.releaseInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.claimInterface(interfaceNumber));
+ assertThrows(UsbException.class, () -> device.releaseInterface(interfaceNumber));
}
@AfterEach
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/InvalidOperationTest.java b/java-does-usb/src/test/java/net/codecrete/usb/InvalidOperationTest.java
index 4ee735c..7682009 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/InvalidOperationTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/InvalidOperationTest.java
@@ -17,16 +17,16 @@ class InvalidOperationTest extends TestDeviceBase {
@Test
void claimInvalidInterface_throws() {
// throws error because it's already claimed
- Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(interfaceNumber));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.claimInterface(interfaceNumber));
// throws error because it's an invalid interface number
- Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(3));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.claimInterface(3));
// throws error because it's an invalid interface number
- Assertions.assertThrows(USBException.class, () -> testDevice.claimInterface(888));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.claimInterface(888));
}
@Test
void releaseInvalidInterface_throws() {
- Assertions.assertThrows(USBException.class, () -> testDevice.releaseInterface(1));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.releaseInterface(1));
}
@@ -34,16 +34,16 @@ void releaseInvalidInterface_throws() {
void invalidEndpoint_throws() {
var data = new byte[] { 34, 23, 99, 0, 17 };
- Assertions.assertThrows(USBException.class, () -> testDevice.transferOut(2, data));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.transferOut(2, data));
- Assertions.assertThrows(USBException.class, () -> testDevice.transferOut(0, data));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.transferOut(0, data));
- Assertions.assertThrows(USBException.class, () -> testDevice.transferOut(4, data));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.transferOut(4, data));
- Assertions.assertThrows(USBException.class, () -> testDevice.transferIn(1));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.transferIn(1));
- Assertions.assertThrows(USBException.class, () -> testDevice.transferIn(0));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.transferIn(0));
- Assertions.assertThrows(USBException.class, () -> testDevice.transferIn(5));
+ Assertions.assertThrows(UsbException.class, () -> testDevice.transferIn(5));
}
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/SpeedTest.java b/java-does-usb/src/test/java/net/codecrete/usb/SpeedTest.java
index 732bd3b..aa9b1c5 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/SpeedTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/SpeedTest.java
@@ -20,7 +20,7 @@ class SpeedTest extends TestDeviceBase {
@Test
void loopback_isFast() throws Throwable {
- final var isHighSpeed = testDevice.getEndpoint(USBDirection.IN, LOOPBACK_EP_IN).packetSize() == 512;
+ final var isHighSpeed = testDevice.getEndpoint(UsbDirection.IN, LOOPBACK_EP_IN).getPacketSize() == 512;
final var numBytes = isHighSpeed ? 5000000 : 500000;
var sampleData = generateRandomBytes(numBytes, 7219937602343L);
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/StallTest.java b/java-does-usb/src/test/java/net/codecrete/usb/StallTest.java
index 6f97fe7..7fce14a 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/StallTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/StallTest.java
@@ -18,12 +18,12 @@ class StallTest extends TestDeviceBase {
@Test
void stalledBulkTransferOut_recovers() {
- haltEndpoint(USBDirection.OUT, LOOPBACK_EP_OUT);
+ haltEndpoint(UsbDirection.OUT, LOOPBACK_EP_OUT);
var data = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- assertThrows(USBStallException.class, () -> testDevice.transferOut(LOOPBACK_EP_OUT, data));
+ assertThrows(UsbStallException.class, () -> testDevice.transferOut(LOOPBACK_EP_OUT, data));
- testDevice.clearHalt(USBDirection.OUT, LOOPBACK_EP_OUT);
+ testDevice.clearHalt(UsbDirection.OUT, LOOPBACK_EP_OUT);
testDevice.transferOut(LOOPBACK_EP_OUT, data);
var receivedData = testDevice.transferIn(LOOPBACK_EP_IN);
@@ -32,11 +32,11 @@ void stalledBulkTransferOut_recovers() {
@Test
void stalledBulkTransferIn_recovers() {
- haltEndpoint(USBDirection.IN, LOOPBACK_EP_IN);
+ haltEndpoint(UsbDirection.IN, LOOPBACK_EP_IN);
- assertThrows(USBStallException.class, () -> testDevice.transferIn(LOOPBACK_EP_IN));
+ assertThrows(UsbStallException.class, () -> testDevice.transferIn(LOOPBACK_EP_IN));
- testDevice.clearHalt(USBDirection.IN, LOOPBACK_EP_IN);
+ testDevice.clearHalt(UsbDirection.IN, LOOPBACK_EP_IN);
var data = new byte[] { 9, 8, 7, 6, 5, 4, 3, 2 };
testDevice.transferOut(LOOPBACK_EP_OUT, data);
@@ -46,15 +46,15 @@ void stalledBulkTransferIn_recovers() {
@Test
void invalidControlTransfer_throws() {
- var request = new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE, (byte) 0x08,
+ var request = new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE, (byte) 0x08,
(short) 0, (short) interfaceNumber);
- assertThrows(USBStallException.class, () -> testDevice.controlTransferIn(request, 2));
+ assertThrows(UsbStallException.class, () -> testDevice.controlTransferIn(request, 2));
}
- void haltEndpoint(USBDirection direction, int endpointNumber) {
+ void haltEndpoint(UsbDirection direction, int endpointNumber) {
final var SET_FEATURE = 0x03;
final var ENDPOINT_HALT = 0x00;
- var endpointAddress = (direction == USBDirection.IN ? 0x80 : 0x00) | endpointNumber;
- testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.STANDARD, USBRecipient.ENDPOINT, SET_FEATURE, ENDPOINT_HALT, endpointAddress), null);
+ var endpointAddress = (direction == UsbDirection.IN ? 0x80 : 0x00) | endpointNumber;
+ testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.STANDARD, UsbRecipient.ENDPOINT, SET_FEATURE, ENDPOINT_HALT, endpointAddress), null);
}
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java b/java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java
index 34c5f33..c35f5cd 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/TestDeviceBase.java
@@ -54,26 +54,26 @@ public class TestDeviceBase {
protected static final int ECHO_EP_OUT = 3;
protected static final int ECHO_EP_IN = 3;
protected static final int ECHO_MAX_PACKET_SIZE = 16;
- protected static USBDevice testDevice;
+ protected static UsbDevice testDevice;
- static USBDevice getDevice() {
- var optionalDevice = USB.getDevice(VID_COMPOSITE, PID_COMPOSITE);
+ static UsbDevice getDevice() {
+ var optionalDevice = Usb.findDevice(VID_COMPOSITE, PID_COMPOSITE);
if (optionalDevice.isEmpty())
- optionalDevice = USB.getDevice(VID_LOOPBACK, PID_LOOPBACK);
+ optionalDevice = Usb.findDevice(VID_LOOPBACK, PID_LOOPBACK);
if (optionalDevice.isEmpty())
throw new IllegalStateException("No test device connected");
return optionalDevice.get();
}
- static int getInterfaceNumber(USBDevice device) {
- return device.productId() == PID_COMPOSITE ? LOOPBACK_INTF_COMPOSITE : LOOPBACK_INTF_LOOPBACK;
+ static int getInterfaceNumber(UsbDevice device) {
+ return device.getProductId() == PID_COMPOSITE ? LOOPBACK_INTF_COMPOSITE : LOOPBACK_INTF_LOOPBACK;
}
@BeforeAll
static void openDevice() {
testDevice = getDevice();
- vid = testDevice.vendorId();
- pid = testDevice.productId();
+ vid = testDevice.getVendorId();
+ pid = testDevice.getProductId();
interfaceNumber = getInterfaceNumber(testDevice);
testDevice.open();
@@ -109,7 +109,7 @@ private static void resetDevice() {
while (true) {
try {
testDevice.transferIn(LOOPBACK_EP_IN, 1);
- } catch (USBTimeoutException e) {
+ } catch (UsbTimeoutException e) {
break;
}
}
@@ -119,7 +119,7 @@ private static void resetDevice() {
while (true) {
try {
testDevice.transferIn(ECHO_EP_IN, 1);
- } catch (USBTimeoutException e) {
+ } catch (UsbTimeoutException e) {
break;
}
}
@@ -130,7 +130,7 @@ private static void resetDevice() {
}
static void resetBuffers() {
- testDevice.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE,
+ testDevice.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE,
(byte) 0x04, (short) 0, (short) interfaceNumber), null);
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/TimeoutTest.java b/java-does-usb/src/test/java/net/codecrete/usb/TimeoutTest.java
index a692aa1..619aded 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/TimeoutTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/TimeoutTest.java
@@ -23,7 +23,7 @@ class TimeoutTest extends TestDeviceBase {
@Test
@Timeout(value = 1, unit = TimeUnit.SECONDS)
void bulkTransferIn_timesOut() {
- assertThrows(USBTimeoutException.class, () -> testDevice.transferIn(LOOPBACK_EP_IN, 200));
+ assertThrows(UsbTimeoutException.class, () -> testDevice.transferIn(LOOPBACK_EP_IN, 200));
}
@Test
@@ -43,12 +43,12 @@ void bulkTransferOut_timesOut() {
// The test device has an internal buffer of about 2KB for full-speed
// and 16KB for high-speed. The first transfer should not time out.
final var bufferSize = 32 * testDevice
- .getEndpoint(USBDirection.OUT, LOOPBACK_EP_OUT).packetSize();
+ .getEndpoint(UsbDirection.OUT, LOOPBACK_EP_OUT).getPacketSize();
var data = generateRandomBytes(100, 9383073929L);
testDevice.transferOut(LOOPBACK_EP_OUT, data, 200);
- assertThrows(USBTimeoutException.class, () -> {
+ assertThrows(UsbTimeoutException.class, () -> {
for (var i = 0; i < bufferSize / data.length; i++) {
testDevice.transferOut(LOOPBACK_EP_OUT, data, 200);
}
@@ -58,7 +58,7 @@ void bulkTransferOut_timesOut() {
while (true) {
try {
testDevice.transferIn(LOOPBACK_EP_IN, 200);
- } catch (USBTimeoutException e) {
+ } catch (UsbTimeoutException e) {
break;
}
}
@@ -70,7 +70,7 @@ void interruptTransferIn_timesOut() {
Assumptions.assumeTrue(isLoopbackDevice(),
"Interrupt transfer only supported by loopback test device");
- assertThrows(USBTimeoutException.class, () -> testDevice.transferIn(ECHO_EP_IN, 200));
+ assertThrows(UsbTimeoutException.class, () -> testDevice.transferIn(ECHO_EP_IN, 200));
}
@Test
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/special/EnumerateDevices.java b/java-does-usb/src/test/java/net/codecrete/usb/special/EnumerateDevices.java
index 2951df4..a88ff90 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/special/EnumerateDevices.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/special/EnumerateDevices.java
@@ -7,7 +7,7 @@
package net.codecrete.usb.special;
-import net.codecrete.usb.USB;
+import net.codecrete.usb.Usb;
/**
* Sample program displaying the connected USB devices
@@ -15,7 +15,7 @@
public class EnumerateDevices {
public static void main(String[] args) {
- for (var device : USB.getAllDevices()) {
+ for (var device : Usb.getDevices()) {
System.out.println(device);
}
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/special/LogicAnalyzer.java b/java-does-usb/src/test/java/net/codecrete/usb/special/LogicAnalyzer.java
index 7014dd7..c50b04e 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/special/LogicAnalyzer.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/special/LogicAnalyzer.java
@@ -80,10 +80,10 @@ public static void main(String[] args) {
}
}
- private USBDevice device;
+ private UsbDevice device;
LogicAnalyzer() {
- var optionalDevice = USB.getDevice(VID, PID);
+ var optionalDevice = Usb.findDevice(VID, PID);
if (optionalDevice.isEmpty())
throw new IllegalStateException("no logic analyzer connected");
@@ -175,7 +175,7 @@ void startAcquisition() {
// send the start command
final int commandCodeStart = 0xb1;
- device.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.DEVICE, commandCodeStart, 0, 0), cmd);
+ device.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.DEVICE, commandCodeStart, 0, 0), cmd);
}
void saveSamples() {
@@ -196,7 +196,7 @@ void saveSamples() {
activityValue = size;
}
- } catch (USBException e) {
+ } catch (UsbException e) {
if (!stopped && !bufferOverrunDetected)
throw e;
@@ -230,7 +230,7 @@ void saveSamples() {
void stopAcquisition() {
// stop the acquisition by halting the bulk endpoint and clearing the halt
stopped = true;
- device.abortTransfers(USBDirection.IN, EP);
+ device.abortTransfers(UsbDirection.IN, EP);
}
void detectBufferOverrun() {
@@ -255,7 +255,7 @@ void detectBufferOverrun() {
}
void checkFirmware() {
- if (device.manufacturer() != null) {
+ if (device.getManufacturer() != null) {
System.out.println("Device ready");
return;
}
@@ -274,19 +274,19 @@ void checkFirmware() {
device.claimInterface(0);
byte[] cmd = new byte[] { 1 };
- device.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.DEVICE, 0xa0, 0xe600, 0x0000), cmd);
+ device.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.DEVICE, 0xa0, 0xe600, 0x0000), cmd);
final int len = firmware.length;
int offset = 0;
while (offset < len) {
int n = Math.min(len - offset, 0x1000);
byte[] chunk = Arrays.copyOfRange(firmware, offset, offset + n);
- device.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.DEVICE, 0xa0, offset, 0x0000), chunk);
+ device.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.DEVICE, 0xa0, offset, 0x0000), chunk);
offset += n;
}
cmd = new byte[] { 0 };
- device.controlTransferOut(new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.DEVICE, 0xa0, 0xe600, 0x0000), cmd);
+ device.controlTransferOut(new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.DEVICE, 0xa0, 0xe600, 0x0000), cmd);
device.close();
DeviceMonitor.instance().awaitDevice(false);
@@ -296,11 +296,11 @@ void checkFirmware() {
DeviceMonitor.instance().awaitDevice(true);
sleep(200);
- var optionalDevice = USB.getDevice(VID, PID);
+ var optionalDevice = Usb.findDevice(VID, PID);
if (optionalDevice.isEmpty())
throw new IllegalStateException("no logic analyzer connected");
device = optionalDevice.get();
- if (device.manufacturer() == null)
+ if (device.getManufacturer() == null)
throw new IllegalStateException("firmware upload failed");
System.out.println("Device is ready");
@@ -338,13 +338,13 @@ static synchronized DeviceMonitor instance() {
private DeviceMonitor() { }
private void start() {
- USB.setOnDeviceConnected((device) -> onDeviceConnected(device, true));
- USB.setOnDeviceDisconnected((device) -> onDeviceConnected(device, false));
- isDeviceConnected = USB.getDevice(VID, PID).isPresent();
+ Usb.setOnDeviceConnected((device) -> onDeviceConnected(device, true));
+ Usb.setOnDeviceDisconnected((device) -> onDeviceConnected(device, false));
+ isDeviceConnected = Usb.findDevice(VID, PID).isPresent();
}
- private void onDeviceConnected(USBDevice device, boolean connected) {
- if (device.vendorId() == VID && device.productId() == device.productId()) {
+ private void onDeviceConnected(UsbDevice device, boolean connected) {
+ if (device.getVendorId() == VID && device.getProductId() == device.getProductId()) {
try {
deviceLock.lock();
isDeviceConnected = connected;
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/special/MonitorDevices.java b/java-does-usb/src/test/java/net/codecrete/usb/special/MonitorDevices.java
index 8fe853b..c39388f 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/special/MonitorDevices.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/special/MonitorDevices.java
@@ -20,13 +20,13 @@
public class MonitorDevices {
public static void main(String[] args) throws IOException {
- USB.setOnDeviceConnected((device) -> {
+ Usb.setOnDeviceConnected((device) -> {
System.out.println("Connected: " + device.toString());
talkToTestDevice(device);
});
- USB.setOnDeviceDisconnected((device) -> System.out.println("Disconnected: " + device.toString()));
+ Usb.setOnDeviceDisconnected((device) -> System.out.println("Disconnected: " + device.toString()));
- for (var device : USB.getAllDevices()) {
+ for (var device : Usb.getDevices()) {
System.out.println("Present: " + device.toString());
talkToTestDevice(device);
}
@@ -36,15 +36,15 @@ public static void main(String[] args) throws IOException {
System.in.read();
}
- private static void talkToTestDevice(USBDevice device) {
- if (device.vendorId() != 0xcafe)
+ private static void talkToTestDevice(UsbDevice device) {
+ if (device.getVendorId() != 0xcafe)
return; // no test device
- int interfaceNumber = device.productId() == 0xcea0 ? 2 : 0;
+ int interfaceNumber = device.getProductId() == 0xcea0 ? 2 : 0;
device.open();
device.claimInterface(interfaceNumber);
var response = device.controlTransferIn(
- new USBControlTransfer(USBRequestType.VENDOR, USBRecipient.INTERFACE,
+ new UsbControlTransfer(UsbRequestType.VENDOR, UsbRecipient.INTERFACE,
(byte) 0x05, (short) 0, (short) interfaceNumber),
1
);
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/special/USBSerialTest.java b/java-does-usb/src/test/java/net/codecrete/usb/special/USBSerialTest.java
index 053aa4f..04964ba 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/special/USBSerialTest.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/special/USBSerialTest.java
@@ -29,7 +29,7 @@
public class USBSerialTest {
public static void main(String[] args) {
- for (var device : USB.getAllDevices()) {
+ for (var device : Usb.getDevices()) {
int commInterfaceNum = getCDCCommInterfaceNum(device);
if (commInterfaceNum >= 0) {
System.out.printf("USB CDC device: %s%n", device);
@@ -38,7 +38,7 @@ public static void main(String[] args) {
}
}
- static void interact(USBDevice device, int commInterfaceNum) {
+ static void interact(UsbDevice device, int commInterfaceNum) {
// communication and data interface must have consecutive numbers
int dataInterfaceNum = commInterfaceNum + 1;
@@ -50,7 +50,7 @@ static void interact(USBDevice device, int commInterfaceNum) {
// set line coding (9600bps, 8 bit)
byte[] coding = { (byte)0x80, 0x25, 0, 0, 0, 0, 8 };
device.controlTransferOut(
- new USBControlTransfer(USBRequestType.CLASS, USBRecipient.INTERFACE, 0x20, 0, commInterfaceNum),
+ new UsbControlTransfer(UsbRequestType.CLASS, UsbRecipient.INTERFACE, 0x20, 0, commInterfaceNum),
coding);
// send some data
@@ -64,29 +64,29 @@ static void interact(USBDevice device, int commInterfaceNum) {
device.close();
}
- static int getCDCCommInterfaceNum(USBDevice device) {
+ static int getCDCCommInterfaceNum(UsbDevice device) {
// CDC ACM implementations consist of two consecutive interfaces
// with certain class, subclass and protocol codes
- int numInterfaces = device.interfaces().size();
+ int numInterfaces = device.getInterfaces().size();
if (numInterfaces < 2)
return -1;
for (int i = 0; i < numInterfaces - 1; i += 1) {
- var commIntf = device.getInterface(i).alternate();
- var dataIntf = device.getInterface(i + 1).alternate();
+ var commIntf = device.getInterface(i).getCurrentAlternate();
+ var dataIntf = device.getInterface(i + 1).getCurrentAlternate();
- if (commIntf.classCode() == 2 && commIntf.subclassCode() == 2 && commIntf.protocolCode() == 1 && dataIntf.classCode() == 10)
+ if (commIntf.getClassCode() == 2 && commIntf.getSubclassCode() == 2 && commIntf.getProtocolCode() == 1 && dataIntf.getClassCode() == 10)
return i;
}
return -1;
}
- static int getDataOutEndpointNum(USBDevice device, int dataInterfaceNum) {
- var altInterface = device.getInterface(dataInterfaceNum).alternate();
- for (var endpoint : altInterface.endpoints()) {
- if (endpoint.direction() == USBDirection.OUT)
- return endpoint.number();
+ static int getDataOutEndpointNum(UsbDevice device, int dataInterfaceNum) {
+ var altInterface = device.getInterface(dataInterfaceNum).getCurrentAlternate();
+ for (var endpoint : altInterface.getEndpoints()) {
+ if (endpoint.getDirection() == UsbDirection.OUT)
+ return endpoint.getNumber();
}
return -1;
}
diff --git a/java-does-usb/src/test/java/net/codecrete/usb/special/Unplug.java b/java-does-usb/src/test/java/net/codecrete/usb/special/Unplug.java
index 198ef74..6d0e5f6 100644
--- a/java-does-usb/src/test/java/net/codecrete/usb/special/Unplug.java
+++ b/java-does-usb/src/test/java/net/codecrete/usb/special/Unplug.java
@@ -7,9 +7,9 @@
package net.codecrete.usb.special;
-import net.codecrete.usb.USB;
-import net.codecrete.usb.USBDevice;
-import net.codecrete.usb.USBException;
+import net.codecrete.usb.Usb;
+import net.codecrete.usb.UsbDevice;
+import net.codecrete.usb.UsbException;
import java.io.IOException;
import java.util.HashMap;
@@ -42,21 +42,21 @@ public class Unplug {
private static final int ECHO_EP_OUT = 3;
private static final int ECHO_EP_IN = 3;
- private static final HashMap activeDevices = new HashMap<>();
+ private static final HashMap activeDevices = new HashMap<>();
public static void main(String[] args) throws IOException {
System.out.println("Plug and unplug test device multiple times.");
System.out.println("Hit ENTER to exit.");
- USB.setOnDeviceConnected(Unplug::onPluggedDevice);
- USB.setOnDeviceDisconnected(Unplug::onUnpluggedDevice);
- USB.getAllDevices().forEach(Unplug::onPluggedDevice);
+ Usb.setOnDeviceConnected(Unplug::onPluggedDevice);
+ Usb.setOnDeviceDisconnected(Unplug::onUnpluggedDevice);
+ Usb.getDevices().forEach(Unplug::onPluggedDevice);
//noinspection ResultOfMethodCallIgnored
System.in.read();
}
- private static void onPluggedDevice(USBDevice device) {
+ private static void onPluggedDevice(UsbDevice device) {
if (!isTestDevice(device))
return;
@@ -65,7 +65,7 @@ private static void onPluggedDevice(USBDevice device) {
worker.start();
}
- private static void onUnpluggedDevice(USBDevice device) {
+ private static void onUnpluggedDevice(UsbDevice device) {
if (!isTestDevice(device))
return;
@@ -75,13 +75,13 @@ private static void onUnpluggedDevice(USBDevice device) {
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
- private static boolean isTestDevice(USBDevice device) {
- return device.vendorId() == VID_LOOPBACK && device.productId() == PID_LOOPBACK;
+ private static boolean isTestDevice(UsbDevice device) {
+ return device.getVendorId() == VID_LOOPBACK && device.getProductId() == PID_LOOPBACK;
}
static class DeviceWorker {
- private final USBDevice device;
+ private final UsbDevice device;
private final int seed;
@@ -89,7 +89,7 @@ static class DeviceWorker {
private final HashMap workTracking = new HashMap<>();
- DeviceWorker(USBDevice device) {
+ DeviceWorker(UsbDevice device) {
this.device = device;
this.seed = (int) System.currentTimeMillis();
}
@@ -174,7 +174,7 @@ private void logWork(long amount) {
private void runAction(Runnable action) {
try {
action.run();
- } catch (USBException e) {
+ } catch (UsbException e) {
logFinish();
}
}