Skip to content

Commit b852efe

Browse files
committed
Windows: more refactoring
1 parent 7279d98 commit b852efe

File tree

5 files changed

+141
-117
lines changed

5 files changed

+141
-117
lines changed

java-does-usb/src/main/java/net/codecrete/usb/linux/Linux.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,11 @@
99

1010
import net.codecrete.usb.linux.gen.string.string;
1111

12-
import java.lang.foreign.*;
12+
import java.lang.foreign.Arena;
13+
import java.lang.foreign.Linker;
1314
import java.lang.foreign.MemoryLayout.PathElement;
15+
import java.lang.foreign.MemorySegment;
16+
import java.lang.foreign.StructLayout;
1417
import java.lang.invoke.VarHandle;
1518

1619
/**

java-does-usb/src/main/java/net/codecrete/usb/windows/DeviceInfoSet.java

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import static java.lang.foreign.MemorySegment.NULL;
1919
import static java.lang.foreign.ValueLayout.JAVA_CHAR;
2020
import static java.lang.foreign.ValueLayout.JAVA_INT;
21-
import static net.codecrete.usb.windows.DeviceProperty.DEVPKEY_Device_Service;
21+
import static net.codecrete.usb.windows.DevicePropertyKey.Service;
2222
import static net.codecrete.usb.windows.Win.allocateErrorState;
2323
import static net.codecrete.usb.windows.WindowsUSBException.throwException;
2424
import static net.codecrete.usb.windows.WindowsUSBException.throwLastError;
@@ -45,21 +45,13 @@ interface InfoSetCreator {
4545
private MemorySegment devIntfData;
4646
private int iterationIndex = -1;
4747

48-
/**
49-
* Creates a new empty device info set.
50-
*
51-
* @return device info set
52-
*/
53-
static DeviceInfoSet ofEmpty() {
54-
return new DeviceInfoSet((arena, errorState) -> SetupAPI2.SetupDiCreateDeviceInfoList(NULL, NULL, errorState));
55-
}
56-
5748
/**
5849
* Creates a new device info set containing the present devices of the specified device class and
5950
* optionally device instance ID.
6051
*
6152
* <p>
62-
* After creation, there is no current element. {@link #next()} must be called first.
53+
* After creation, there is no current element. {@link #next()} should be called to iterate the first
54+
* and all subsequent elements.
6355
* </p>
6456
*
6557
* @param interfaceGuid device interface class GUID
@@ -74,6 +66,45 @@ static DeviceInfoSet ofPresentDevices(MemorySegment interfaceGuid, String instan
7466
});
7567
}
7668

69+
/**
70+
* Creates a new device info set containing a single device with the specified instance ID.
71+
*
72+
* <p>
73+
* The device becomes the current element. The set cannot be iterated.
74+
* </p>
75+
*
76+
* @param instanceId instance ID
77+
*/
78+
static DeviceInfoSet ofInstance(String instanceId) {
79+
var devInfoSet = ofEmpty();
80+
devInfoSet.addInstanceId(instanceId);
81+
return devInfoSet;
82+
}
83+
84+
/**
85+
* Creates a new device info set containing a single device with the specified path.
86+
*
87+
* <p>
88+
* The device becomes the current element. The set cannot be iterated.
89+
* </p>
90+
*
91+
* @param devicePath device path
92+
*/
93+
static DeviceInfoSet ofPath(String devicePath) {
94+
var devInfoSet = ofEmpty();
95+
devInfoSet.addDevicePath(devicePath);
96+
return devInfoSet;
97+
}
98+
99+
/**
100+
* Creates a new empty device info set.
101+
*
102+
* @return device info set
103+
*/
104+
private static DeviceInfoSet ofEmpty() {
105+
return new DeviceInfoSet((arena, errorState) -> SetupAPI2.SetupDiCreateDeviceInfoList(NULL, NULL, errorState));
106+
}
107+
77108
private DeviceInfoSet(InfoSetCreator creator) {
78109
arena = Arena.ofConfined();
79110
try {
@@ -101,48 +132,13 @@ public void close() {
101132
arena.close();
102133
}
103134

104-
/**
105-
* Iterates to the next element in this set.
106-
*
107-
* @return {@code true} if there is a current element, {@code false} if the iteration moved beyond the last element
108-
*/
109-
boolean next() {
110-
iterationIndex += 1;
111-
if (SetupAPI2.SetupDiEnumDeviceInfo(devInfoSet, iterationIndex, devInfoData, errorState) == 0) {
112-
var err = Win.getLastError(errorState);
113-
if (err == Kernel32.ERROR_NO_MORE_ITEMS())
114-
return false;
115-
throwLastError(errorState, "internal error (SetupDiEnumDeviceInfo)");
116-
}
117-
118-
return true;
119-
}
120-
121-
/**
122-
* Adds the device with the specified instance ID to this device info set.
123-
*
124-
* <p>
125-
* The added device becomes the current element.
126-
* </p>
127-
*
128-
* @param instanceId instance ID
129-
*/
130-
void addInstance(String instanceId) {
135+
private void addInstanceId(String instanceId) {
131136
var instanceIdSegment = Win.createSegmentFromString(instanceId, arena);
132137
if (SetupAPI2.SetupDiOpenDeviceInfoW(devInfoSet, instanceIdSegment, NULL, 0, devInfoData, errorState) == 0)
133138
throwLastError(errorState, "internal error (SetupDiOpenDeviceInfoW)");
134139
}
135140

136-
/**
137-
* Adds the device with the specified path to this device info set.
138-
*
139-
* <p>
140-
* The added device becomes the current element.
141-
* </p>
142-
*
143-
* @param devicePath device path
144-
*/
145-
void addDevice(String devicePath) {
141+
private void addDevicePath(String devicePath) {
146142
if (devIntfData != null)
147143
throw new AssertionError("calling addDevice() multiple times is not implemented");
148144

@@ -163,14 +159,30 @@ void addDevice(String devicePath) {
163159
}
164160
}
165161

162+
/**
163+
* Iterates to the next element in this set.
164+
*
165+
* @return {@code true} if there is a current element, {@code false} if the iteration moved beyond the last element
166+
*/
167+
boolean next() {
168+
iterationIndex += 1;
169+
if (SetupAPI2.SetupDiEnumDeviceInfo(devInfoSet, iterationIndex, devInfoData, errorState) == 0) {
170+
var err = Win.getLastError(errorState);
171+
if (err == Kernel32.ERROR_NO_MORE_ITEMS())
172+
return false;
173+
throwLastError(errorState, "internal error (SetupDiEnumDeviceInfo)");
174+
}
175+
176+
return true;
177+
}
166178

167179
/**
168180
* Checks if the current element is a composite USB device
169181
*
170182
* @return {@code true} if it is a composite device
171183
*/
172184
boolean isCompositeDevice() {
173-
var deviceService = getStringProperty(DEVPKEY_Device_Service);
185+
var deviceService = getStringProperty(Service);
174186

175187
// usbccgp is the USB Generic Parent Driver used for composite devices
176188
return "usbccgp".equalsIgnoreCase(deviceService);

java-does-usb/src/main/java/net/codecrete/usb/windows/DeviceProperty.java renamed to java-does-usb/src/main/java/net/codecrete/usb/windows/DevicePropertyKey.java

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,58 @@
77

88
package net.codecrete.usb.windows;
99

10-
import net.codecrete.usb.windows.gen.setupapi.*;
10+
import net.codecrete.usb.windows.gen.setupapi._DEVPROPKEY;
1111

1212
import java.lang.foreign.Arena;
1313
import java.lang.foreign.MemorySegment;
1414

1515
/**
16-
* Device property GUIDs
16+
* Device property keys (GUIDs)
1717
*/
18-
class DeviceProperty {
18+
class DevicePropertyKey {
1919

20-
private DeviceProperty() {
20+
private DevicePropertyKey() {
2121
}
2222

23-
static final MemorySegment DEVPKEY_Device_Address = createDEVPROPKEY(0xa45c254e, (short) 0xdf1c,
23+
/**
24+
* DEVPKEY_Device_Address
25+
*/
26+
static final MemorySegment Address = createDEVPROPKEY(0xa45c254e, (short) 0xdf1c,
2427
(short) 0x4efd, (byte) 0x80, (byte) 0x20, (byte) 0x67, (byte) 0xd1, (byte) 0x46, (byte) 0xa8, (byte) 0x50
2528
, (byte) 0xe0, 30);
2629

27-
static final MemorySegment DEVPKEY_Device_InstanceId = createDEVPROPKEY(0x78c34fc8, (short) 0x104a,
30+
/**
31+
* DEVPKEY_Device_InstanceId
32+
*/
33+
static final MemorySegment InstanceId = createDEVPROPKEY(0x78c34fc8, (short) 0x104a,
2834
(short) 0x4aca, (byte) 0x9e, (byte) 0xa4, (byte) 0x52, (byte) 0x4d, (byte) 0x52, (byte) 0x99, (byte) 0x6e
2935
, (byte) 0x57, 256);
3036

31-
static final MemorySegment DEVPKEY_Device_Parent = createDEVPROPKEY(0x4340a6c5, (short) 0x93fa,
37+
/**
38+
* DEVPKEY_Device_Parent
39+
*/
40+
static final MemorySegment Parent = createDEVPROPKEY(0x4340a6c5, (short) 0x93fa,
3241
(short) 0x4706, (byte) 0x97, (byte) 0x2c, (byte) 0x7b, (byte) 0x64, (byte) 0x80, (byte) 0x08, (byte) 0xa5
3342
, (byte) 0xa7, 8);
3443

35-
static final MemorySegment DEVPKEY_Device_Service = createDEVPROPKEY(0xa45c254e, (short) 0xdf1c,
44+
/**
45+
* DEVPKEY_Device_Service
46+
*/
47+
static final MemorySegment Service = createDEVPROPKEY(0xa45c254e, (short) 0xdf1c,
3648
(short) 0x4efd, (byte) 0x80, (byte) 0x20, (byte) 0x67, (byte) 0xd1, (byte) 0x46, (byte) 0xa8, (byte) 0x50
3749
, (byte) 0xe0, 6);
3850

39-
static final MemorySegment DEVPKEY_Device_Children = createDEVPROPKEY(0x4340a6c5, (short) 0x93fa,
51+
/**
52+
* DEVPKEY_Device_Children
53+
*/
54+
static final MemorySegment Children = createDEVPROPKEY(0x4340a6c5, (short) 0x93fa,
4055
(short) 0x4706, (byte) 0x97, (byte) 0x2c, (byte) 0x7b, (byte) 0x64, (byte) 0x80, (byte) 0x08, (byte) 0xa5
4156
, (byte) 0xa7, 9);
4257

43-
static final MemorySegment DEVPKEY_Device_HardwareIds = createDEVPROPKEY(0xa45c254e, (short) 0xdf1c,
58+
/**
59+
* DEVPKEY_Device_HardwareIds
60+
*/
61+
static final MemorySegment HardwareIds = createDEVPROPKEY(0xa45c254e, (short) 0xdf1c,
4462
(short) 0x4efd, (byte) 0x80, (byte) 0x20, (byte) 0x67, (byte) 0xd1, (byte) 0x46, (byte) 0xa8, (byte) 0x50
4563
, (byte) 0xe0, 3);
4664

java-does-usb/src/main/java/net/codecrete/usb/windows/WindowsUSBDevice.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
package net.codecrete.usb.windows;
99

10-
import net.codecrete.usb.*;
10+
import net.codecrete.usb.USBControlTransfer;
11+
import net.codecrete.usb.USBDirection;
12+
import net.codecrete.usb.USBRecipient;
13+
import net.codecrete.usb.USBTransferType;
1114
import net.codecrete.usb.common.Transfer;
1215
import net.codecrete.usb.common.USBDeviceImpl;
1316
import net.codecrete.usb.usbstandard.SetupPacket;

0 commit comments

Comments
 (0)