Skip to content

Commit

Permalink
Kotlin-friendly API
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Nov 10, 2023
1 parent 8cb47de commit ae14ce8
Show file tree
Hide file tree
Showing 85 changed files with 1,588 additions and 1,080 deletions.
2 changes: 1 addition & 1 deletion examples/bulk_transfer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>net.codecrete.usb</groupId>
<artifactId>java-does-usb</artifactId>
<version>0.6.1</version>
<version>0.7.0-SNAPSHOT</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion examples/enumerate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<dependency>
<groupId>net.codecrete.usb</groupId>
<artifactId>java-does-usb</artifactId>
<version>0.6.1</version>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -59,36 +59,36 @@ private static void printInParens(Optional<String> 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) {
Expand Down
38 changes: 38 additions & 0 deletions examples/enumerate_kotlin/.gitignore
Original file line number Diff line number Diff line change
@@ -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
66 changes: 66 additions & 0 deletions examples/enumerate_kotlin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>net.codecrete.usb.examples</groupId>
<artifactId>kotlin</artifactId>
<version>0.6.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>kotlin</name>
<url>https://github.com/manuelbl/JavaDoesUSB/examples/kotlin</url>

<properties>
<kotlin.version>1.9.10</kotlin.version>
<kotlin.compiler.incremental>true</kotlin.compiler.incremental>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<build>
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>

<plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<extensions>true</extensions>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>net.codecrete.usb</groupId>
<artifactId>java-does-usb</artifactId>
<version>0.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>tinylog-impl</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.tinylog</groupId>
<artifactId>jsl-tinylog</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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
}
}

}
Loading

0 comments on commit ae14ce8

Please sign in to comment.