Skip to content

Commit 7c77528

Browse files
committed
detach/attach drivers: check device is closed
1 parent 1b8b41d commit 7c77528

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

java-does-usb/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
<artifactId>maven-surefire-plugin</artifactId>
7070
<version>3.1.2</version>
7171
<configuration>
72-
<argLine>--enable-preview --enable-native-access=ALL-UNNAMED</argLine>
72+
<argLine>--enable-preview --enable-native-access=net.codecrete.usb</argLine>
7373
</configuration>
7474
</plugin>
7575
<plugin>

java-does-usb/src/main/java/net/codecrete/usb/common/USBDeviceImpl.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,18 @@ protected USBDeviceImpl(Object id, int vendorId, int productId) {
6363

6464
@Override
6565
public void detachStandardDrivers() {
66-
// Default implementation: do nothing
66+
if (isOpen())
67+
throw new USBException("detachStandardDrivers() must not be called while the device is open");
68+
69+
// default implementation: do nothing
6770
}
6871

6972
@Override
7073
public void attachStandardDrivers() {
71-
// Default implementation: do nothing
74+
if (isOpen())
75+
throw new USBException("attachStandardDrivers() must not be called while the device is open");
76+
77+
// default implementation: do nothing
7278
}
7379

7480
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,15 @@ private void loadDescription(String path) {
6868

6969
@Override
7070
public void detachStandardDrivers() {
71+
if (isOpen())
72+
throwException("detachStandardDrivers() must not be called while the device is open");
7173
detachDrivers = true;
7274
}
7375

7476
@Override
7577
public void attachStandardDrivers() {
78+
if (isOpen())
79+
throwException("attachStandardDrivers() must not be called while the device is open");
7680
detachDrivers = false;
7781
}
7882

java-does-usb/src/main/java/net/codecrete/usb/macos/MacosUSBDevice.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,17 @@ public class MacosUSBDevice extends USBDeviceImpl {
7171

7272
@Override
7373
public void detachStandardDrivers() {
74+
if (isOpen())
75+
throwException("detachStandardDrivers() must not be called while the device is open");
7476
int ret = IoKitUSB.USBDeviceReEnumerate(device, IOKit.kUSBReEnumerateCaptureDeviceMask());
7577
if (ret != 0)
7678
throwException(ret, "failed to detach kernel drivers");
7779
}
7880

7981
@Override
8082
public void attachStandardDrivers() {
83+
if (isOpen())
84+
throwException("attachStandardDrivers() must not be called while the device is open");
8185
int ret = IoKitUSB.USBDeviceReEnumerate(device, IOKit.kUSBReEnumerateReleaseDeviceMask());
8286
if (ret != 0)
8387
throwException(ret, "failed to attach kernel drivers");

0 commit comments

Comments
 (0)