Skip to content

Commit 5ae0a7a

Browse files
committed
Implement USBDK support
1 parent 2df41b3 commit 5ae0a7a

File tree

3 files changed

+59
-31
lines changed

3 files changed

+59
-31
lines changed

src/main/java/org/usb4java/javax/Config.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ final class Config
1616
{
1717
/** Base key name for properties. */
1818
private static final String KEY_BASE = "org.usb4java.javax.";
19-
19+
2020
/** The default USB communication timeout in milliseconds. */
2121
private static final int DEFAULT_TIMEOUT = 5000;
22-
22+
2323
/** The default scan interval in milliseconds. */
2424
private static final int DEFAULT_SCAN_INTERVAL = 500;
2525

@@ -29,12 +29,18 @@ final class Config
2929
/** Key name for USB communication timeout. */
3030
private static final String SCAN_INTERVAL_KEY = KEY_BASE + "scanInterval";
3131

32+
/** Key name for the USBDK usage flag. */
33+
private static final String USE_USBDK_KEY = KEY_BASE + "useUSBDK";
34+
3235
/** The timeout for USB communication in milliseconds. */
3336
private int timeout = DEFAULT_TIMEOUT;
34-
37+
3538
/** The scan interval in milliseconds. */
3639
private int scanInterval = DEFAULT_SCAN_INTERVAL;
3740

41+
/** If USBDK is to be used on Windows. */
42+
private boolean useUSBDK = false;
43+
3844
/**
3945
* Constructs new configuration from the specified properties.
4046
*
@@ -55,6 +61,12 @@ final class Config
5561
this.scanInterval = Integer.valueOf(properties.getProperty(
5662
SCAN_INTERVAL_KEY));
5763
}
64+
65+
// Read the USBDK usage flag
66+
if (properties.containsKey(USE_USBDK_KEY))
67+
{
68+
this.useUSBDK = Boolean.valueOf(properties.getProperty(USE_USBDK_KEY));
69+
}
5870
}
5971

6072
/**
@@ -76,4 +88,14 @@ public int getScanInterval()
7688
{
7789
return this.scanInterval;
7890
}
91+
92+
/**
93+
* Check if USBDK is to be used on Windows.
94+
*
95+
* @return True if USBDK is to be used, false if not.
96+
*/
97+
public boolean isUseUSBDK()
98+
{
99+
return this.useUSBDK;
100+
}
79101
}

src/main/java/org/usb4java/javax/DeviceManager.java

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525
/**
2626
* Manages the USB devices.
27-
*
27+
*
2828
* @author Klaus Reimer (k@ailis.de)
2929
*/
3030
final class DeviceManager
@@ -47,20 +47,19 @@ final class DeviceManager
4747

4848
/**
4949
* Constructs a new device manager.
50-
*
50+
*
5151
* @param rootHub
5252
* The root hub. Must not be null.
53-
* @param scanInterval
54-
* The scan interval in milliseconds.
53+
* @param config
54+
* The configuration.
5555
* @throws UsbException
5656
* When USB initialization fails.
5757
*/
58-
DeviceManager(final RootHub rootHub, final int scanInterval)
59-
throws UsbException
58+
DeviceManager(final RootHub rootHub, final Config config) throws UsbException
6059
{
6160
if (rootHub == null)
6261
throw new IllegalArgumentException("rootHub must be set");
63-
this.scanInterval = scanInterval;
62+
this.scanInterval = config.getScanInterval();
6463
this.rootHub = rootHub;
6564
this.context = new Context();
6665
final int result = LibUsb.init(this.context);
@@ -69,6 +68,14 @@ final class DeviceManager
6968
throw ExceptionUtils.createPlatformException(
7069
"Unable to initialize libusb", result);
7170
}
71+
if (config.isUseUSBDK())
72+
{
73+
final int usbdkResult = LibUsb.setOption(this.context, LibUsb.OPTION_USE_USBDK);
74+
if (usbdkResult != LibUsb.SUCCESS && usbdkResult != LibUsb.ERROR_NOT_SUPPORTED)
75+
{
76+
throw ExceptionUtils.createPlatformException("Unable to set USE_USBDK option", usbdkResult);
77+
}
78+
}
7279
}
7380

7481
/**
@@ -79,10 +86,10 @@ public void dispose()
7986
{
8087
LibUsb.exit(this.context);
8188
}
82-
89+
8390
/**
8491
* Creates a device ID from the specified device.
85-
*
92+
*
8693
* @param device
8794
* The libusb device. Must not be null.
8895
* @return The device id.
@@ -111,7 +118,7 @@ private DeviceId createId(final Device device) throws UsbPlatformException
111118

112119
/**
113120
* Scans the specified ports for removed devices.
114-
*
121+
*
115122
* @param ports
116123
* The ports to scan for removals.
117124
*/
@@ -130,7 +137,7 @@ private void scanRemovedDevices(final UsbPorts<Port, AbstractDevice> ports)
130137

131138
/**
132139
* Scans the specified ports for new devices.
133-
*
140+
*
134141
* @param ports
135142
* The ports to scan for new devices.
136143
* @param hubId
@@ -165,7 +172,7 @@ private void scanNewDevices(final UsbPorts<Port, AbstractDevice> ports,
165172

166173
/**
167174
* Scans the specified hub for changes.
168-
*
175+
*
169176
* @param hub
170177
* The hub to scan.
171178
*/
@@ -197,7 +204,7 @@ public void scan(final UsbHub hub)
197204
/**
198205
* Updates the device list by adding newly connected devices to it and by
199206
* removing no longer connected devices.
200-
*
207+
*
201208
* @throws UsbPlatformException
202209
* When libusb reported an error which we can't ignore during
203210
* scan.
@@ -211,7 +218,7 @@ private void updateDeviceList() throws UsbPlatformException
211218
final int result = LibUsb.getDeviceList(this.context, devices);
212219
if (result < 0)
213220
{
214-
throw ExceptionUtils.createPlatformException(
221+
throw ExceptionUtils.createPlatformException(
215222
"Unable to get USB device list", result);
216223
}
217224

@@ -228,7 +235,7 @@ private void updateDeviceList() throws UsbPlatformException
228235
if (device == null)
229236
{
230237
final Device parent = LibUsb.getParent(libUsbDevice);
231-
final DeviceId parentId = parent == null ? null :
238+
final DeviceId parentId = parent == null ? null :
232239
createId(parent);
233240
final int speed = LibUsb.getDeviceSpeed(libUsbDevice);
234241
final boolean isHub = id.getDeviceDescriptor()
@@ -278,7 +285,7 @@ public synchronized void scan()
278285
/**
279286
* Returns the libusb device for the specified id. The device must be freed
280287
* after use.
281-
*
288+
*
282289
* @param id
283290
* The id of the device to return. Must not be null.
284291
* @return device The libusb device. Never null.
@@ -327,7 +334,7 @@ public Device getLibUsbDevice(final DeviceId id) throws UsbPlatformException
327334

328335
/**
329336
* Releases the specified device.
330-
*
337+
*
331338
* @param device
332339
* The device to release. Must not be null.
333340
*/
@@ -346,7 +353,7 @@ public void start()
346353
// Do not start the scan thread when interval is set to 0
347354
final int scanInterval = this.scanInterval;
348355
if (scanInterval == 0) return;
349-
356+
350357
final Thread thread = new Thread(new Runnable()
351358
{
352359
@Override

src/main/java/org/usb4java/javax/Services.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
/**
2020
* usb4java implementation of JSR-80 UsbServices.
21-
*
21+
*
2222
* @author Klaus Reimer (k@ailis.de)
2323
*/
2424
public final class Services implements UsbServices
@@ -42,12 +42,12 @@ public final class Services implements UsbServices
4242
/** The USB device scanner. */
4343
private final DeviceManager deviceManager;
4444

45-
/** If devices should be scanned by hierarchy. */
45+
/** The configuration. */
4646
private final Config config;
4747

4848
/**
4949
* Constructor.
50-
*
50+
*
5151
* @throws UsbException
5252
* When properties could not be loaded.
5353
* @throws LoaderException
@@ -58,8 +58,7 @@ public Services() throws UsbException
5858
this.config = new Config(UsbHostManager.getProperties());
5959
Loader.load();
6060
this.rootHub = new RootHub();
61-
this.deviceManager = new DeviceManager(this.rootHub,
62-
this.config.getScanInterval());
61+
this.deviceManager = new DeviceManager(this.rootHub, this.config);
6362
this.deviceManager.start();
6463
}
6564

@@ -102,7 +101,7 @@ public String getImpDescription()
102101

103102
/**
104103
* Informs listeners about a new attached device.
105-
*
104+
*
106105
* @param device
107106
* The new attached device.
108107
*/
@@ -113,7 +112,7 @@ void usbDeviceAttached(final UsbDevice device)
113112

114113
/**
115114
* Informs listeners about a detached device.
116-
*
115+
*
117116
* @param device
118117
* The detached device.
119118
*/
@@ -124,7 +123,7 @@ void usbDeviceDetached(final UsbDevice device)
124123

125124
/**
126125
* Returns the configuration.
127-
*
126+
*
128127
* @return The configuration.
129128
*/
130129
Config getConfig()
@@ -134,7 +133,7 @@ Config getConfig()
134133

135134
/**
136135
* Returns the usb4java services.
137-
*
136+
*
138137
* @return The usb4java services.
139138
*/
140139
static Services getInstance()
@@ -154,7 +153,7 @@ static Services getInstance()
154153
+ e, e);
155154
}
156155
}
157-
156+
158157
/**
159158
* Manually scans for USB device connection changes.
160159
*/

0 commit comments

Comments
 (0)