Skip to content

Commit

Permalink
Linux: check that device has not been disconnected
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelbl committed Apr 20, 2024
1 parent e019f50 commit c8c49a6
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ protected void checkIsOpen() {
throw new UsbException("device needs to be opened first for this operation");
}

protected void checkIsClosed(String message) {
if (!connected)
throw new UsbException("device has been disconnected");
if (isOpened())
throw new UsbException(message);
}

protected synchronized void disconnect() {
connected = false;
close();
}

@Override
public int getProductId() {
return pid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ protected void closeAndRemoveDevice(Object deviceId) {
return;

try {
device.close();
((UsbDeviceImpl)device).disconnect();
} catch (Exception e) {
LOG.log(INFO, "failed to close USB device - ignoring exception", e);
LOG.log(INFO, "failed to close disconnected USB device - ignoring exception", e);
}

removeDevice(deviceId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,14 @@ private void loadDescription(String path) {
}

@Override
public void detachStandardDrivers() {
if (isOpened())
throwException("detachStandardDrivers() must not be called while the device is open");
public synchronized void detachStandardDrivers() {
checkIsClosed("detachStandardDrivers() must not be called while the device is open");
detachDrivers = true;
}

@Override
public void attachStandardDrivers() {
if (isOpened())
throwException("attachStandardDrivers() must not be called while the device is open");
public synchronized void attachStandardDrivers() {
checkIsClosed("attachStandardDrivers() must not be called while the device is open");
detachDrivers = false;
}

Expand All @@ -91,8 +89,7 @@ public boolean isOpened() {

@Override
public synchronized void open() {
if (isOpened())
throwException("device is already open");
checkIsClosed("device is already open");

try (var arena = Arena.ofConfined()) {
var pathUtf8 = arena.allocateFrom(uniqueDeviceId.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,6 @@ public boolean isOpened() {
return claimedInterfaces != null;
}

private void checkIsClosed(String message) {
if (!connected)
throwException("device has been disconnected");
if (isOpened())
throwException(message);
}

@SuppressWarnings("java:S2276")
@Override
public synchronized void open() {
Expand Down Expand Up @@ -164,9 +157,9 @@ public synchronized void close() {
asyncTask.removeEventSource(source);
}

synchronized void closeFully() {
connected = false;
close();
@Override
protected synchronized void disconnect() {
super.disconnect();
IoKitUsb.Release(device);
device = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,19 +267,7 @@ private void onDevicesConnected(MemorySegment ignoredRefCon, int iterator) {
private void onDevicesDisconnected(MemorySegment ignoredRefCon, int iterator) {

// process device iterator for disconnected devices
iterateDevices(iterator, (entryId, _, _) -> {
var device = findDevice(entryId);
if (device == null)
return;

try {
((MacosUsbDevice) device).closeFully();
} catch (Exception e) {
LOG.log(INFO, "failed to close USB device - ignoring exception", e);
}

removeDevice(entryId);
});
iterateDevices(iterator, (entryId, _, _) -> closeAndRemoveDevice(entryId));
}

@FunctionalInterface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private static void onUnpluggedDevice(UsbDevice device) {
device.open();
System.err.println("Device should not be openable after disconnect");
} catch (UsbException e) {
// expected
if (!e.getMessage().contains("disconnected"))
System.err.println(STR."Unexpected error: \{e.getMessage()}");
}
}).start();
}
Expand Down

0 comments on commit c8c49a6

Please sign in to comment.