Skip to content

Commit 049c69d

Browse files
committed
Replace LibUsbException with UsbPlatformException
1 parent 3cab91c commit 049c69d

File tree

10 files changed

+176
-110
lines changed

10 files changed

+176
-110
lines changed

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

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import javax.usb.UsbDeviceDescriptor;
2424
import javax.usb.UsbDisconnectedException;
2525
import javax.usb.UsbException;
26+
import javax.usb.UsbPlatformException;
2627
import javax.usb.UsbPort;
2728
import javax.usb.UsbStringDescriptor;
2829
import javax.usb.event.UsbDeviceEvent;
@@ -99,12 +100,12 @@ abstract class AbstractDevice implements UsbDevice
99100
* The libusb device. This reference is only valid during the
100101
* constructor execution, so don't store it in a property or
101102
* something like that.
102-
* @throws LibUsbException
103+
* @throws UsbPlatformException
103104
* When device configuration could not be read.
104105
*/
105106
AbstractDevice(final DeviceManager manager, final DeviceId id,
106107
final DeviceId parentId, final int speed, final Device device)
107-
throws LibUsbException
108+
throws UsbPlatformException
108109
{
109110
if (manager == null)
110111
throw new IllegalArgumentException("manager must be set");
@@ -126,8 +127,9 @@ abstract class AbstractDevice implements UsbDevice
126127
configDescriptor);
127128
if (result < 0)
128129
{
129-
throw new LibUsbException("Unable to get configuation " + i
130-
+ " for device " + id, result);
130+
throw ExceptionUtils.createPlatformException(
131+
"Unable to get configuation " + i + " for device " + id,
132+
result);
131133
}
132134
try
133135
{
@@ -159,7 +161,7 @@ abstract class AbstractDevice implements UsbDevice
159161
}
160162
else if (result < 0)
161163
{
162-
throw new LibUsbException(
164+
throw ExceptionUtils.createPlatformException(
163165
"Unable to read active config descriptor from device " + id,
164166
result);
165167
}
@@ -221,8 +223,8 @@ public final DeviceHandle open() throws UsbException
221223
final int result = LibUsb.open(device, handle);
222224
if (result < 0)
223225
{
224-
throw new LibUsbException("Can't open device "
225-
+ this.id, result);
226+
throw ExceptionUtils.createPlatformException(
227+
"Can't open device " + this.id, result);
226228
}
227229
this.handle = handle;
228230
}
@@ -379,8 +381,8 @@ final void setActiveUsbConfigurationNumber(final byte number)
379381

380382
final int result = LibUsb.setConfiguration(open(), number & 0xff);
381383
if (result < 0)
382-
throw new LibUsbException("Unable to set configuration",
383-
result);
384+
throw ExceptionUtils.createPlatformException(
385+
"Unable to set configuration", result);
384386
this.activeConfigurationNumber = number;
385387
}
386388
}
@@ -414,16 +416,20 @@ final void claimInterface(final byte number, final boolean force)
414416
{
415417
result = LibUsb.detachKernelDriver(handle, number);
416418
if (result < 0)
417-
throw new LibUsbException(
419+
{
420+
throw ExceptionUtils.createPlatformException(
418421
"Unable to detach kernel driver", result);
422+
}
419423
this.detachedKernelDriver = true;
420424
}
421425
}
422426

423427
final int result = LibUsb.claimInterface(handle, number & 0xff);
424428
if (result < 0)
425-
throw new LibUsbException("Unable to claim interface",
426-
result);
429+
{
430+
throw ExceptionUtils.createPlatformException(
431+
"Unable to claim interface", result);
432+
}
427433
this.claimedInterfaceNumbers.add(number);
428434
}
429435

@@ -444,14 +450,20 @@ final void releaseInterface(final byte number) throws UsbException
444450

445451
final DeviceHandle handle = open();
446452
int result = LibUsb.releaseInterface(handle, number & 0xff);
447-
if (result < 0) throw new LibUsbException(
448-
"Unable to release interface", result);
453+
if (result < 0)
454+
{
455+
throw ExceptionUtils.createPlatformException(
456+
"Unable to release interface", result);
457+
}
449458

450459
if (this.detachedKernelDriver)
451460
{
452461
result = LibUsb.attachKernelDriver(handle, number & 0xff);
453-
if (result < 0) throw new LibUsbException(
454-
"Uanble to re-attach kernel driver", result);
462+
if (result < 0)
463+
{
464+
throw ExceptionUtils.createPlatformException(
465+
"Uanble to re-attach kernel driver", result);
466+
}
455467
}
456468

457469
this.claimedInterfaceNumbers.remove(number);
@@ -499,8 +511,11 @@ public final UsbStringDescriptor getUsbStringDescriptor(final byte index)
499511
final int result =
500512
LibUsb.getStringDescriptor(handle, index, langId, data);
501513
if (result < 0)
502-
throw new LibUsbException("Unable to get string descriptor "
503-
+ index + " from device " + this, result);
514+
{
515+
throw ExceptionUtils.createPlatformException(
516+
"Unable to get string descriptor " + index + " from device "
517+
+ this, result);
518+
}
504519
return new SimpleUsbStringDescriptor(data);
505520
}
506521

@@ -525,8 +540,10 @@ private short[] getLanguages() throws UsbException
525540
final int result = LibUsb.getDescriptor(handle, LibUsb.DT_STRING,
526541
(byte) 0, buffer);
527542
if (result < 0)
528-
throw new LibUsbException(
543+
{
544+
throw ExceptionUtils.createPlatformException(
529545
"Unable to get string descriptor languages", result);
546+
}
530547
if (result < 2)
531548
throw new UsbException("Received illegal descriptor length: "
532549
+ result);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ protected final void processControlIrp(final UsbControlIrp irp)
226226
getConfig().getTimeout());
227227
if (result < 0)
228228
{
229-
throw new LibUsbException(
229+
throw ExceptionUtils.createPlatformException(
230230
"Unable to submit control message", result);
231231
}
232232
buffer.rewind();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ void setUsbInterface(final byte number, final Interface iface)
169169
iface.getUsbInterfaceDescriptor().bAlternateSetting());
170170
if (result < 0)
171171
{
172-
throw new LibUsbException(
172+
throw ExceptionUtils.createPlatformException(
173173
"Unable to set alternate interface", result);
174174
}
175175
this.activeSettings.put(number & 0xff, iface);

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import javax.usb.UsbException;
1515
import javax.usb.UsbHub;
16+
import javax.usb.UsbPlatformException;
1617

1718
import org.usb4java.Context;
1819
import org.usb4java.Device;
@@ -64,7 +65,10 @@ final class DeviceManager
6465
this.context = new Context();
6566
final int result = LibUsb.init(this.context);
6667
if (result != 0)
67-
throw new LibUsbException("Unable to initialize libusb", result);
68+
{
69+
throw ExceptionUtils.createPlatformException(
70+
"Unable to initialize libusb", result);
71+
}
6872
}
6973

7074
/**
@@ -82,11 +86,11 @@ public void dispose()
8286
* @param device
8387
* The libusb device. Must not be null.
8488
* @return The device id.
85-
* @throws LibUsbException
89+
* @throws UsbPlatformException
8690
* When device descriptor could not be read from the specified
8791
* device.
8892
*/
89-
private DeviceId createId(final Device device) throws LibUsbException
93+
private DeviceId createId(final Device device) throws UsbPlatformException
9094
{
9195
if (device == null)
9296
throw new IllegalArgumentException("device must be set");
@@ -97,7 +101,7 @@ private DeviceId createId(final Device device) throws LibUsbException
97101
final int result = LibUsb.getDeviceDescriptor(device, deviceDescriptor);
98102
if (result < 0)
99103
{
100-
throw new LibUsbException(
104+
throw ExceptionUtils.createPlatformException(
101105
"Unable to get device descriptor for device " + addressNumber
102106
+ " at bus " + busNumber, result);
103107
}
@@ -171,7 +175,7 @@ public void scan(final UsbHub hub)
171175
{
172176
updateDeviceList();
173177
}
174-
catch (LibUsbException e)
178+
catch (UsbException e)
175179
{
176180
throw new ScanException("Unable to scan for USB devices: " + e, e);
177181
}
@@ -194,20 +198,22 @@ public void scan(final UsbHub hub)
194198
* Updates the device list by adding newly connected devices to it and by
195199
* removing no longer connected devices.
196200
*
197-
* @throws LibUsbException
201+
* @throws UsbPlatformException
198202
* When libusb reported an error which we can't ignore during
199203
* scan.
200204
*/
201-
private void updateDeviceList() throws LibUsbException
205+
private void updateDeviceList() throws UsbPlatformException
202206
{
203207
final List<DeviceId> current = new ArrayList<DeviceId>();
204208

205209
// Get device list from libusb and abort if it failed
206210
final DeviceList devices = new DeviceList();
207211
final int result = LibUsb.getDeviceList(this.context, devices);
208212
if (result < 0)
209-
throw new LibUsbException("Unable to get USB device list",
210-
result);
213+
{
214+
throw ExceptionUtils.createPlatformException(
215+
"Unable to get USB device list", result);
216+
}
211217

212218
try
213219
{
@@ -245,7 +251,7 @@ private void updateDeviceList() throws LibUsbException
245251
// Remember current device as "current"
246252
current.add(id);
247253
}
248-
catch (LibUsbException e)
254+
catch (UsbPlatformException e)
249255
{
250256
// Devices which can't be enumerated are ignored
251257
continue;
@@ -278,18 +284,20 @@ public synchronized void scan()
278284
* @return device The libusb device. Never null.
279285
* @throws DeviceNotFoundException
280286
* When the device was not found.
281-
* @throws LibUsbException
287+
* @throws UsbPlatformException
282288
* When libusb reported an error while enumerating USB devices.
283289
*/
284-
public Device getLibUsbDevice(final DeviceId id) throws LibUsbException
290+
public Device getLibUsbDevice(final DeviceId id) throws UsbPlatformException
285291
{
286292
if (id == null) throw new IllegalArgumentException("id must be set");
287293

288294
final DeviceList devices = new DeviceList();
289295
final int result = LibUsb.getDeviceList(this.context, devices);
290296
if (result < 0)
291-
throw new LibUsbException("Unable to get USB device list",
292-
result);
297+
{
298+
throw ExceptionUtils.createPlatformException(
299+
"Unable to get USB device list", result);
300+
}
293301
try
294302
{
295303
for (Device device: devices)
@@ -302,7 +310,7 @@ public Device getLibUsbDevice(final DeviceId id) throws LibUsbException
302310
return device;
303311
}
304312
}
305-
catch (LibUsbException e)
313+
catch (UsbPlatformException e)
306314
{
307315
// Devices for which no ID can be created are ignored
308316
continue;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2014 Klaus Reimer <k@ailis.de>
3+
* See LICENSE.md for licensing information.
4+
*/
5+
6+
package org.usb4java.javax;
7+
8+
import javax.usb.UsbPlatformException;
9+
10+
import org.usb4java.LibUsb;
11+
12+
/**
13+
* Utility methods for exception handling.
14+
*
15+
* @author Klaus Reimer (k@ailis.de)
16+
*/
17+
final class ExceptionUtils
18+
{
19+
/**
20+
* Private constructor to prevent instantiation.
21+
*/
22+
private ExceptionUtils()
23+
{
24+
// Empty
25+
}
26+
27+
/**
28+
* Creates a USB platform exception.
29+
*
30+
* @param message
31+
* The error message.
32+
* @param errorCode
33+
* The error code.
34+
* @return The USB platform exception.
35+
*/
36+
static UsbPlatformException createPlatformException(final String message,
37+
final int errorCode)
38+
{
39+
return new UsbPlatformException(String.format("USB error %d: %s: %s",
40+
-errorCode, message, LibUsb.strError(errorCode)), errorCode);
41+
}
42+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.List;
99

1010
import javax.usb.UsbHub;
11+
import javax.usb.UsbPlatformException;
1112

1213
import org.usb4java.Device;
1314

@@ -37,12 +38,12 @@ final class Hub extends AbstractDevice implements UsbHub,
3738
* The libusb device. This reference is only valid during the
3839
* constructor execution, so don't store it in a property or
3940
* something like that.
40-
* @throws LibUsbException
41+
* @throws UsbPlatformException
4142
* When device configuration could not be read.
4243
*/
4344
Hub(final DeviceManager manager, final DeviceId id,
4445
final DeviceId parentId, final int speed, final Device device)
45-
throws LibUsbException
46+
throws UsbPlatformException
4647
{
4748
super(manager, id, parentId, speed, device);
4849
}

0 commit comments

Comments
 (0)