- release resources and clear device handle
- tests for invalid parameters git-svn-id: https://svn.code.sf.net/p/libusbjava/code/trunk@227 94ad28fe-ef68-46b1-9651-e7ae4fcf1c4c
This commit is contained in:
@@ -241,6 +241,7 @@ public class Device {
|
|||||||
if (usbDevHandle <= 0) {
|
if (usbDevHandle <= 0) {
|
||||||
throw new USBException("invalid device handle");
|
throw new USBException("invalid device handle");
|
||||||
}
|
}
|
||||||
|
release_interface(usbDevHandle, dev_interface);
|
||||||
if (LibusbJava.usb_reset(usbDevHandle) < 0) {
|
if (LibusbJava.usb_reset(usbDevHandle) < 0) {
|
||||||
usbDevHandle = 0;
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_reset: "
|
throw new USBException("LibusbWin.usb_reset: "
|
||||||
@@ -511,15 +512,23 @@ public class Device {
|
|||||||
private void claim_interface(int usb_dev_handle, int configuration,
|
private void claim_interface(int usb_dev_handle, int configuration,
|
||||||
int interface_, int altinterface) throws USBException {
|
int interface_, int altinterface) throws USBException {
|
||||||
if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) {
|
if (LibusbJava.usb_set_configuration(usb_dev_handle, configuration) < 0) {
|
||||||
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_set_configuration: "
|
throw new USBException("LibusbWin.usb_set_configuration: "
|
||||||
+ LibusbJava.usb_strerror());
|
+ LibusbJava.usb_strerror());
|
||||||
}
|
}
|
||||||
if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) {
|
if (LibusbJava.usb_claim_interface(usb_dev_handle, interface_) < 0) {
|
||||||
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_claim_interface: "
|
throw new USBException("LibusbWin.usb_claim_interface: "
|
||||||
+ LibusbJava.usb_strerror());
|
+ LibusbJava.usb_strerror());
|
||||||
}
|
}
|
||||||
if (altinterface >= 0) {
|
if (altinterface >= 0) {
|
||||||
if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
|
if (LibusbJava.usb_set_altinterface(usb_dev_handle, altinterface) < 0) {
|
||||||
|
try {
|
||||||
|
release_interface(usb_dev_handle, interface_);
|
||||||
|
} catch (USBException e) {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
usbDevHandle = 0;
|
||||||
throw new USBException("LibusbWin.usb_set_altinterface: "
|
throw new USBException("LibusbWin.usb_set_altinterface: "
|
||||||
+ LibusbJava.usb_strerror());
|
+ LibusbJava.usb_strerror());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Java libusb wrapper
|
* Java libusb wrapper
|
||||||
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
|
* Copyright (c) 2005-2006 Andreas Schl<68>pfer <libusb@drip.ch>
|
||||||
*
|
*
|
||||||
* This library is covered by the LGPL, read LGPL.txt for details.
|
* This library is covered by the LGPL, read LGPL.txt for details.
|
||||||
*/
|
*/
|
||||||
@@ -8,6 +8,7 @@ package ch.ntb.usb.test;
|
|||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.junit.Assert.fail;
|
||||||
|
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@@ -88,8 +89,14 @@ public class DeviceTest {
|
|||||||
|
|
||||||
@Test(expected = USBException.class)
|
@Test(expected = USBException.class)
|
||||||
public void testClose() throws Exception {
|
public void testClose() throws Exception {
|
||||||
doOpen();
|
try {
|
||||||
doClose();
|
// this calls must not throw an exception
|
||||||
|
doOpen();
|
||||||
|
doClose();
|
||||||
|
} catch (USBException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
// this call must throw an exception, because the device is closed
|
// this call must throw an exception, because the device is closed
|
||||||
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
|
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
|
||||||
devinfo.getTimeout(), false);
|
devinfo.getTimeout(), false);
|
||||||
@@ -97,9 +104,15 @@ public class DeviceTest {
|
|||||||
|
|
||||||
@Test(expected = USBException.class)
|
@Test(expected = USBException.class)
|
||||||
public void testReset1() throws Exception {
|
public void testReset1() throws Exception {
|
||||||
doOpen();
|
try {
|
||||||
dev.reset();
|
// this calls must not throw an exception
|
||||||
timeout();
|
doOpen();
|
||||||
|
dev.reset();
|
||||||
|
timeout();
|
||||||
|
} catch (USBException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
// this call must throw an exception, because the device is closed
|
// this call must throw an exception, because the device is closed
|
||||||
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
|
dev.writeBulk(devinfo.getOutEPBulk(), testData, testData.length,
|
||||||
devinfo.getTimeout(), false);
|
devinfo.getTimeout(), false);
|
||||||
@@ -107,9 +120,15 @@ public class DeviceTest {
|
|||||||
|
|
||||||
@Test(expected = USBException.class)
|
@Test(expected = USBException.class)
|
||||||
public void testReset2() throws Exception {
|
public void testReset2() throws Exception {
|
||||||
doOpen();
|
try {
|
||||||
dev.reset();
|
// this calls must not throw an exception
|
||||||
timeout();
|
doOpen();
|
||||||
|
dev.reset();
|
||||||
|
timeout();
|
||||||
|
} catch (USBException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
fail(e.getMessage());
|
||||||
|
}
|
||||||
// this call must throw an exception, because the device can't be closed
|
// this call must throw an exception, because the device can't be closed
|
||||||
doClose();
|
doClose();
|
||||||
}
|
}
|
||||||
@@ -182,6 +201,48 @@ public class DeviceTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void invalidConfig() throws Exception {
|
||||||
|
try {
|
||||||
|
dev.open(devinfo.getConfiguration() + 5, devinfo.getInterface(),
|
||||||
|
devinfo.getAltinterface());
|
||||||
|
fail("USBException expected");
|
||||||
|
} catch (USBException e) {
|
||||||
|
System.err.println("INFO: " + getClass()
|
||||||
|
+ ": error expected: could not set config "
|
||||||
|
+ (devinfo.getConfiguration() + 5));
|
||||||
|
}
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void invalidInterface() throws Exception {
|
||||||
|
try {
|
||||||
|
dev.open(devinfo.getConfiguration(), devinfo.getInterface() + 5,
|
||||||
|
devinfo.getAltinterface());
|
||||||
|
fail("USBException expected");
|
||||||
|
} catch (USBException e) {
|
||||||
|
System.err.println("INFO: " + getClass()
|
||||||
|
+ ": error expected: could not claim interface "
|
||||||
|
+ (devinfo.getInterface() + 5));
|
||||||
|
}
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void invalidAltinterface() throws Exception {
|
||||||
|
try {
|
||||||
|
dev.open(devinfo.getConfiguration(), devinfo.getInterface(),
|
||||||
|
devinfo.getAltinterface() + 5);
|
||||||
|
fail("USBException expected");
|
||||||
|
} catch (USBException e) {
|
||||||
|
System.err.println("INFO: " + getClass()
|
||||||
|
+ ": error expected: could not set alt interface "
|
||||||
|
+ (devinfo.getAltinterface() + 5));
|
||||||
|
}
|
||||||
|
doOpenWriteReadClose();
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testGetIdProduct() {
|
public void testGetIdProduct() {
|
||||||
Assert.assertEquals(dev.getIdProduct(), devinfo.getIdProduct());
|
Assert.assertEquals(dev.getIdProduct(), devinfo.getIdProduct());
|
||||||
@@ -247,8 +308,8 @@ public class DeviceTest {
|
|||||||
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
|
} else if (devinfo.getMode().equals(TransferMode.Interrupt)) {
|
||||||
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
|
dev.writeInterrupt(devinfo.getOutEPInt(), testData,
|
||||||
testData.length, devinfo.getTimeout(), false);
|
testData.length, devinfo.getTimeout(), false);
|
||||||
dev.readInterrupt(devinfo.getInEPInt(), readData,
|
dev.readInterrupt(devinfo.getInEPInt(), readData, readData.length,
|
||||||
readData.length, devinfo.getTimeout(), false);
|
devinfo.getTimeout(), false);
|
||||||
}
|
}
|
||||||
compare(testData, readData);
|
compare(testData, readData);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user