Skip to content

Commit 70722c4

Browse files
committed
Print device/configuration descriptor
1 parent 6732cdb commit 70722c4

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

examples/enumerate/pom.xml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,25 @@
2323
<artifactId>java-does-usb</artifactId>
2424
<version>0.6.0-SNAPSHOT</version>
2525
</dependency>
26+
<dependency>
27+
<groupId>org.tinylog</groupId>
28+
<artifactId>tinylog-api</artifactId>
29+
<version>2.6.2</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.tinylog</groupId>
33+
<artifactId>tinylog-impl</artifactId>
34+
<version>2.6.2</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.tinylog</groupId>
38+
<artifactId>jsl-tinylog</artifactId>
39+
<version>2.6.2</version>
40+
</dependency>
2641
</dependencies>
2742

2843
<build>
29-
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
44+
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (maybe moved to parent pom) -->
3045
<plugins>
3146
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
3247
<plugin>

examples/enumerate/src/main/java/net/codecrete/usb/examples/Enumerate.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
/**
1515
* Sample application enumerating connected USB devices.
1616
*/
17+
@SuppressWarnings("java:S106")
1718
public class Enumerate {
1819

1920
public static void main(String[] args) {
@@ -42,10 +43,14 @@ private static void printDevice(USBDevice device) {
4243
for (var intf: device.interfaces())
4344
printInterface(intf);
4445

46+
printRawDescriptor("Device descriptor", device.deviceDescriptor());
47+
printRawDescriptor("Configuration descriptor", device.configurationDescriptor());
48+
4549
System.out.println();
4650
System.out.println();
4751
}
4852

53+
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
4954
private static void printInParens(Optional<String> text) {
5055
if (text.isPresent()) {
5156
System.out.printf(" (%s)%n", text.get());
@@ -85,4 +90,17 @@ private static void printEndpoint(USBEndpoint endpoint) {
8590
System.out.printf(" Transfer type: %s%n", endpoint.transferType().name());
8691
System.out.printf(" Packet size: %d bytes%n", endpoint.packetSize());
8792
}
93+
94+
private static void printRawDescriptor(String title, byte[] descriptor) {
95+
System.out.println();
96+
System.out.println(title);
97+
98+
int len = descriptor.length;
99+
for (int i = 0; i < len; i += 16) {
100+
System.out.printf("%04x ", i);
101+
for (int j = i; j < Math.min(i + 16, len); j += 1)
102+
System.out.printf(" %02x", descriptor[j] & 255);
103+
System.out.println();
104+
}
105+
}
88106
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
writer = console
2+
writer.format = {date: HH:mm:ss.SSS} {level|size=5} [{class|size=40}] {message}
3+
writer.level = info

0 commit comments

Comments
 (0)