Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,12 @@ public int openCamera(final @NonNull String deviceName, final int desiredFrameAr
}

// NOTE: The device is already connected, this should just retrieve the device control block
final var deviceCtrlBlock = usbMonitor.openDevice(device);
final USBMonitor.UsbControlBlock deviceCtrlBlock;
try {
deviceCtrlBlock = usbMonitor.openDevice(device);
} catch (final IOException e) {
throw new IllegalStateException("Failed to open USB device: " + deviceName, e);
}

final var camera = new UVCCamera();
final var cameraId = deviceCtrlBlock.getConnection().getFileDescriptor();
Expand Down
31 changes: 25 additions & 6 deletions lib/src/main/java/com/serenegiant/usb/USBMonitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

package com.serenegiant.usb;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
Expand Down Expand Up @@ -467,7 +468,7 @@ public synchronized boolean requestPermission(final UsbDevice device) {
* @return
* @throws SecurityException パーミッションがなければSecurityExceptionを投げる
*/
public UsbControlBlock openDevice(final UsbDevice device) throws SecurityException {
public UsbControlBlock openDevice(final UsbDevice device) throws SecurityException, IOException {
if (hasPermission(device)) {
UsbControlBlock result = mCtrlBlocks.get(device);
if (result == null) {
Expand Down Expand Up @@ -577,7 +578,13 @@ public void run() {
final boolean createNew;
ctrlBlock = mCtrlBlocks.get(device);
if (ctrlBlock == null) {
ctrlBlock = new UsbControlBlock(USBMonitor.this, device);
try {
ctrlBlock = new UsbControlBlock(USBMonitor.this, device);
} catch (final IOException e) {
Log.w(TAG, "processConnect:failed to open device", e);
processCancel(device);
return;
}
mCtrlBlocks.put(device, ctrlBlock);
createNew = true;
} else {
Expand Down Expand Up @@ -909,15 +916,23 @@ public static UsbDeviceInfo updateDeviceInfo(final UsbManager manager, final Usb

if (device != null) {
if (BuildCheck.isLollipop()) {
info.manufacturer = device.getManufacturerName();
info.product = device.getProductName();
info.serial = device.getSerialNumber();
try {
info.manufacturer = device.getManufacturerName();
info.product = device.getProductName();
info.serial = device.getSerialNumber();
} catch (final SecurityException e) {
Log.w(TAG, "updateDeviceInfo:failed to get device info", e);
}
}
if (BuildCheck.isMarshmallow()) {
info.usb_version = device.getVersion();
}
if ((manager != null) && manager.hasPermission(device)) {
final UsbDeviceConnection connection = manager.openDevice(device);
if (connection == null) {
Log.w(TAG, "updateDeviceInfo:openDevice failed, device may have been disconnected");
return info;
}
final byte[] desc = connection.getRawDescriptors();

if (TextUtils.isEmpty(info.usb_version)) {
Expand Down Expand Up @@ -987,11 +1002,15 @@ public static final class UsbControlBlock implements Cloneable {
* @param monitor
* @param device
*/
private UsbControlBlock(final USBMonitor monitor, final UsbDevice device) {
private UsbControlBlock(final USBMonitor monitor, final UsbDevice device) throws IOException {
if (DEBUG) Log.i(TAG, "UsbControlBlock:constructor");
mWeakMonitor = new WeakReference<USBMonitor>(monitor);
mWeakDevice = new WeakReference<UsbDevice>(device);
mConnection = monitor.mUsbManager.openDevice(device);
if (mConnection == null) {
throw new IOException("failed to open device " + device.getDeviceName()
+ ", device may have been disconnected or restricted");
}
mInfo = updateDeviceInfo(monitor.mUsbManager, device, null);
final String name = device.getDeviceName();
final String[] v = !TextUtils.isEmpty(name) ? name.split("/") : null;
Expand Down