Skip to content

Commit a4da0d5

Browse files
committed
Use Windows API generator
1 parent 80f46ec commit a4da0d5

File tree

79 files changed

+805
-9263
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+805
-9263
lines changed
-61.1 KB
Binary file not shown.

java-does-usb/.mvn/wrapper/maven-wrapper.properties

Lines changed: 0 additions & 18 deletions
This file was deleted.

java-does-usb/jextract/README.md

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ The resulting code is then committed to the source code repository.
1515

1616
- According to the jextract mailing list, it would be required to create separate code for Intel x64 and ARM64 architecture. And jextract would need to be run on each architecture separately (no cross-compilation). Fortunately, this doesn't seem to be the case. Linux code generated on Intel x64 also runs on ARM64 without change. The same holds for macOS. However, jextract needs to be run on each operating system separately.
1717

18-
- The *Foreign Function And Memory* API has the abilitiy to save the thread-specific error values (`GetLastError()` on Windows, `errno` on Linux). This is required as the JVM calls operation system functions as well, which overwrite the result values. To save the values, an additional parameter must be added to function calls. Unfortunately, this is not supported by jextract. So a good number of function bindings have to be written manually.
18+
- The *Foreign Function And Memory* API has the abilitiy to save the thread-specific error values (`GetLastError()` on Windows, `errno` on Linux). This is required as the JVM calls operating system functions as well, which overwrite the result values. To save the values, an additional parameter must be added to function calls. Unfortunately, this is not supported by jextract. So a good number of function bindings have to be written manually.
1919

2020
- *jextract* is not really transparent about what it does. It often skips elements without providing any information. In particular, it will silently skip a requested element in these cases:
2121

@@ -53,22 +53,11 @@ Most of the required native functions on macOS are part of a framework. Framewor
5353

5454
## Windows
5555

56-
Most Windows SDK header files are not independent. They require `Windows.h` to be included first. So instead of specifying the target header files directly, a helper header file (`windows_headers.h` in this directory) is specified.
57-
58-
Compared to Linux and macOS, the code generation on Windows is very slow (about 1 min vs 3 seconds). And jextract crashes sometimes.
59-
60-
The known limitations are:
61-
62-
- Variable size `struct`: Several Windows struct are of variable size. The last member is an array. The `struct` definition specifies array length 1. But you are expected to allocate more space depending on the actual array size you need. *jextract* generates code for array length 1 and checks the length when the members are accessed. So the generated code is difficult to use. Variable size `struct`s are a pain - in any language.
63-
64-
- GUID constants like `GUID_DEVINTERFACE_USB_DEVICE` do not work. While code is generated, the code fails at run-time as it is unable to locate the symbol. This is due to the fact that `GUID_DEVINTERFACE_USB_DEVICE` actually resolve to a variable definition and not to a variable declaration. The GUID constant is not contained in any library; instead the header files use linkage options to generate the constant in the callers code, which does not work with FFM. Such constants should be skipped by *jextract*.
65-
66-
- *jextract* is a batch script and turns off *echo mode*. If a single batch scripts has multiple calls of *jextract*, two things need to be considered:
67-
68-
- If the regular command interpreter `cmd.exe` is used, *jextract* must be called using `call`, i.e. `call jextract header.h`.
69-
- If *PowerShell* is used instead, `call` is not needed but *PowerShell* must be configured to allow the execution of scripts.
70-
- *jextract* turns off *echo mode*. So the first call will behave differently than the following calls.
56+
The Windows code is not generated with _jextract_ but with [Windows API Generator](https://github.com/manuelbl/WindowsApiGenerator)
57+
instead. It is run as a Maven plugin. The generated code is not committed to GitHub.
7158

59+
Windows API Generator supports call state capturing (`GetLastError()`), structs with a
60+
variable size, GUID and device property key (`DEVPKEY`) constants etc.
7261

7362

7463
## Code Size

java-does-usb/jextract/windows/gen_win.cmd

Lines changed: 0 additions & 143 deletions
This file was deleted.

java-does-usb/jextract/windows/windows_headers.h

Lines changed: 0 additions & 6 deletions
This file was deleted.

java-does-usb/pom.xml

Lines changed: 94 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<version>1.2.0</version>
1010

1111
<properties>
12-
<maven.compiler.source>22</maven.compiler.source>
13-
<maven.compiler.target>22</maven.compiler.target>
12+
<maven.compiler.source>23</maven.compiler.source>
13+
<maven.compiler.target>23</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<sonar.exclusions>src/main/java/net/codecrete/usb/**/gen/**/*</sonar.exclusions>
1616
</properties>
@@ -52,14 +52,97 @@
5252

5353
<build>
5454
<plugins>
55+
<plugin>
56+
<groupId>net.codecrete.windows-api</groupId>
57+
<artifactId>windowsapi-maven-plugin</artifactId>
58+
<executions>
59+
<execution>
60+
<goals>
61+
<goal>windows-api</goal>
62+
</goals>
63+
<configuration>
64+
<functions>
65+
<function>CLSIDFromString</function>
66+
<function>CreateFileW</function>
67+
<function>CreateIoCompletionPort</function>
68+
<function>CreateWindowExW</function>
69+
<function>DefWindowProcW</function>
70+
<function>DeviceIoControl</function>
71+
<function>FormatMessageW</function>
72+
<function>GetMessageW</function>
73+
<function>GetModuleHandleW</function>
74+
<function>GetQueuedCompletionStatus</function>
75+
<function>LocalFree</function>
76+
<function>RegCloseKey</function>
77+
<function>RegQueryValueExW</function>
78+
<function>RegisterClassExW</function>
79+
<function>RegisterDeviceNotificationW</function>
80+
<function>SetupDiCreateDeviceInfoList</function>
81+
<function>SetupDiDeleteDeviceInterfaceData</function>
82+
<function>SetupDiDestroyDeviceInfoList</function>
83+
<function>SetupDiEnumDeviceInfo</function>
84+
<function>SetupDiEnumDeviceInterfaces</function>
85+
<function>SetupDiGetClassDevsW</function>
86+
<function>SetupDiGetDeviceInterfaceDetailW</function>
87+
<function>SetupDiGetDevicePropertyW</function>
88+
<function>SetupDiOpenDevRegKey</function>
89+
<function>SetupDiOpenDeviceInfoW</function>
90+
<function>SetupDiOpenDeviceInterfaceW</function>
91+
<function>WinUsb_AbortPipe</function>
92+
<function>WinUsb_Free</function>
93+
<function>WinUsb_GetAssociatedInterface</function>
94+
<function>WinUsb_Initialize</function>
95+
<function>WinUsb_ReadPipe</function>
96+
<function>WinUsb_ResetPipe</function>
97+
<function>WinUsb_SetCurrentAlternateSetting</function>
98+
<function>WinUsb_SetPipePolicy</function>
99+
<function>WinUsb_WritePipe</function>
100+
</functions>
101+
<structs>
102+
<struct>DEV_BROADCAST_DEVICEINTERFACE_W</struct>
103+
<struct>DEV_BROADCAST_HDR</struct>
104+
<struct>USB_DESCRIPTOR_REQUEST</struct>
105+
<struct>USB_NODE_CONNECTION_INFORMATION_EX</struct>
106+
</structs>
107+
<enumerations>
108+
<enumeration>DEV_BROADCAST_HDR_DEVICE_TYPE</enumeration>
109+
<enumeration>FORMAT_MESSAGE_OPTIONS</enumeration>
110+
<enumeration>GENERIC_ACCESS_RIGHTS</enumeration>
111+
<enumeration>REG_SAM_FLAGS</enumeration>
112+
<enumeration>SETUP_DI_PROPERTY_CHANGE_SCOPE</enumeration>
113+
</enumerations>
114+
<constants>
115+
<constant>DBT_DEVICEARRIVAL</constant>
116+
<constant>DBT_DEVICEREMOVECOMPLETE</constant>
117+
<constant>DEVPKEY_Device_Address</constant>
118+
<constant>DEVPKEY_Device_Children</constant>
119+
<constant>DEVPKEY_Device_HardwareIds</constant>
120+
<constant>DEVPKEY_Device_InstanceId</constant>
121+
<constant>DEVPKEY_Device_Parent</constant>
122+
<constant>DEVPKEY_Device_Service</constant>
123+
<constant>DIREG_DEV</constant>
124+
<constant>GUID_DEVINTERFACE_USB_DEVICE</constant>
125+
<constant>GUID_DEVINTERFACE_USB_HUB</constant>
126+
<constant>HWND_MESSAGE</constant>
127+
<constant>INFINITE</constant>
128+
<constant>IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION</constant>
129+
<constant>IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX</constant>
130+
<constant>STATUS_UNSUCCESSFUL</constant>
131+
<constant>USB_REQUEST_GET_DESCRIPTOR</constant>
132+
<constant>WM_DEVICECHANGE</constant>
133+
</constants>
134+
</configuration>
135+
</execution>
136+
</executions>
137+
</plugin>
55138
<plugin>
56139
<groupId>org.apache.maven.plugins</groupId>
57140
<artifactId>maven-compiler-plugin</artifactId>
58141
<version>3.12.1</version>
59142
<configuration>
60-
<release>22</release>
61-
<source>22</source>
62-
<target>22</target>
143+
<release>23</release>
144+
<source>23</source>
145+
<target>23</target>
63146
</configuration>
64147
</plugin>
65148
<plugin>
@@ -129,6 +212,12 @@
129212
<autoReleaseAfterClose>true</autoReleaseAfterClose>
130213
</configuration>
131214
</plugin>
215+
216+
<plugin>
217+
<groupId>net.codecrete.windows-api</groupId>
218+
<artifactId>windowsapi-maven-plugin</artifactId>
219+
<version>0.8.0</version>
220+
</plugin>
132221
</plugins>
133222
</pluginManagement>
134223
</build>

java-does-usb/src/main/java/net/codecrete/usb/Usb.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
*/
2727
public class Usb {
2828

29+
@SuppressWarnings("java:S1192")
2930
private static UsbDeviceRegistry createInstance() {
3031
var osName = System.getProperty("os.name");
3132
var osArch = System.getProperty("os.arch");

java-does-usb/src/main/java/net/codecrete/usb/UsbAlternateInterface.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public interface UsbAlternateInterface {
7171
*
7272
* @return a list of endpoints.
7373
*/
74-
@NotNull @Unmodifiable List<UsbEndpoint> getEndpoints();
74+
@NotNull
75+
@Unmodifiable
76+
List<UsbEndpoint> getEndpoints();
7577

7678
/**
7779
* Gets the endpoint with the specified number and direction.

java-does-usb/src/main/java/net/codecrete/usb/UsbDevice.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,9 @@ public interface UsbDevice {
189189
*
190190
* @return a list of USB interfaces
191191
*/
192-
@NotNull @Unmodifiable List<UsbInterface> getInterfaces();
192+
@NotNull
193+
@Unmodifiable
194+
List<UsbInterface> getInterfaces();
193195

194196
/**
195197
* Gets the interface with the specified number.
@@ -260,7 +262,7 @@ public interface UsbDevice {
260262
/**
261263
* Executes a control transfer request and optionally sends data.
262264
* <p>
263-
* This method blocks until the device has acknowledge the request or an error has occurred.
265+
* This method blocks until the device has acknowledged the request or an error has occurred.
264266
* </p>
265267
* <p>
266268
* The control transfer request is sent to endpoint 0. The transfer is expected to either have
@@ -390,7 +392,7 @@ public interface UsbDevice {
390392
* and the last packet size was equal to maximum packet size of the endpoint.
391393
* </p>
392394
* <p>
393-
* If {@link #transferOut(int, byte[])} and a output stream or multiple output streams
395+
* If {@link #transferOut(int, byte[])} and an output stream or multiple output streams
394396
* are used concurrently for the same endpoint, the behavior is unpredictable.
395397
* </p>
396398
*

java-does-usb/src/main/java/net/codecrete/usb/UsbInterface.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,7 @@ public interface UsbInterface {
6464
*
6565
* @return a list of the alternate settings
6666
*/
67-
@NotNull @Unmodifiable List<UsbAlternateInterface> getAlternates();
67+
@NotNull
68+
@Unmodifiable
69+
List<UsbAlternateInterface> getAlternates();
6870
}

0 commit comments

Comments
 (0)