Merge remote-tracking branch 'origin/asyncio'

Conflicts:
	pom.xml
	src/main/c/build/common.sh
	src/main/c/build/linux-x86_64.sh
	src/main/c/configure.ac
	src/main/c/src/ConfigDescriptor.c
	src/main/c/src/DeviceDescriptor.c
	src/main/c/src/EndpointDescriptor.c
	src/main/c/src/EndpointDescriptor.h
	src/main/c/src/Interface.c
	src/main/c/src/InterfaceDescriptor.c
	src/main/c/src/LibUsb.c
	src/main/c/src/Makefile.am
	src/main/c/src/Version.c
	src/main/c/src/usb4java.h
	src/main/java/de/ailis/usb4java/libusb/ConfigDescriptor.java
	src/main/java/de/ailis/usb4java/libusb/Context.java
	src/main/java/de/ailis/usb4java/libusb/Device.java
	src/main/java/de/ailis/usb4java/libusb/DeviceDescriptor.java
	src/main/java/de/ailis/usb4java/libusb/DeviceHandle.java
	src/main/java/de/ailis/usb4java/libusb/DeviceList.java
	src/main/java/de/ailis/usb4java/libusb/EndpointDescriptor.java
	src/main/java/de/ailis/usb4java/libusb/Interface.java
	src/main/java/de/ailis/usb4java/libusb/InterfaceDescriptor.java
	src/main/java/de/ailis/usb4java/libusb/LibUsb.java
	src/main/java/de/ailis/usb4java/libusb/Transfer.java
	src/main/java/de/ailis/usb4java/libusb/Version.java
	src/main/resources/de/ailis/usb4java/libusb/linux-x86/libusb4java.so
	src/main/resources/de/ailis/usb4java/libusb/linux-x86_64/libusb4java.so
	src/test/java/de/ailis/usb4java/libusb/LibUSBDeviceTest.java
	src/test/java/de/ailis/usb4java/libusb/LibUSBTest.java
This commit is contained in:
Luca Longinotti 2013-07-14 23:38:29 +02:00
commit 9a608c7edc
66 changed files with 2594 additions and 1130 deletions

View File

@ -24,3 +24,5 @@ install-sh
ltmain.sh
m4
missing
ar-lib
compile

View File

@ -2,27 +2,28 @@ cd "$(dirname $0)"
SRCDIR="$(pwd)/.."
TMPDIR="$SRCDIR/tmp"
DOWNLOADS="$SRCDIR/downloads"
LIBUSB="libusbx"
LIBUSB_VERSION="1.0.16"
LIBUSB_RC="-rc8"
LIBUSBX_VERSION="1.0.16"
LIBUSBX_RC=""
LIBUSB="stable"
LIBUSB_STABLE_VERSION="1.0.16"
LIBUSB_STABLE_RC=""
LIBUSB_BETA_VERSION="1.0.16"
LIBUSB_BETA_RC="-rc3"
build()
{
if [ "$LIBUSB" = "libusbx" ]
if [ "$LIBUSB" = "stable" ]
then
LIBUSB_NAME="libusbx-$LIBUSBX_VERSION$LIBUSBX_RC"
LIBUSB_NAME="libusbx-$LIBUSB_STABLE_VERSION$LIBUSB_STABLE_RC"
LIBUSB_ARCHIVE="$LIBUSB_NAME.tar.bz2"
LIBUSB_URL="http://downloads.sf.net/project/libusbx/releases/$LIBUSBX_VERSION/source/$LIBUSB_ARCHIVE"
LIBUSB_URL="http://downloads.sf.net/project/libusbx/releases/$LIBUSB_STABLE_VERSION/source/$LIBUSB_ARCHIVE"
else
LIBUSB_NAME="libusb-$LIBUSB_VERSION$LIBUSB_RC"
LIBUSB_NAME="libusbx-$LIBUSB_BETA_VERSION$LIBUSB_BETA_RC"
LIBUSB_ARCHIVE="$LIBUSB_NAME.tar.bz2"
LIBUSB_URL="http://downloads.sf.net/project/libusb/libusb-1.0/libusb-$LIBUSB_VERSION$LIBUSB_RC/$LIBUSB_ARCHIVE"
LIBUSB_URL="http://downloads.sf.net/project/libusbx/releases/$LIBUSB_BETA_VERSION/source/$LIBUSB_ARCHIVE"
fi
DISTDIR="$SRCDIR/../resources/de/ailis/usb4java/libusb/$OS-$ARCH"
# Clean up
rm -rf "$TMPDIR"
rm -rf "$DISTDIR"
@ -62,7 +63,7 @@ build()
CFLAGS="$CFLAGS $USB4JAVA_CFLAGS" \
./configure --prefix=/ --host="$HOST" $USB4JAVA_CONFIG
make clean install-strip DESTDIR="$TMPDIR"
# Copy dist files to java resources directory
mkdir -p "$DISTDIR"
cp -faL 2>/dev/null \

View File

@ -9,9 +9,8 @@ set -e
OS="linux"
ARCH="x86_64"
HOST="$ARCH-$OS-gnu"
CFLAGS="-m64 -Wl,--wrap=memcpy"
CFLAGS="-m64"
LIBUSB_CONFIG="--disable-shared --disable-udev"
USB4JAVA_LIBS="-lrt"
USB4JAVA_CFLAGS="-DWRAP_MEMCPY"
build

View File

@ -12,6 +12,5 @@ ARCH="x86"
CFLAGS="-arch i686"
LIBUSB_CONFIG="--disable-shared"
USB4JAVA_LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
LIBUSB="libusb"
build

View File

@ -12,6 +12,5 @@ ARCH="x86_64"
CFLAGS="-arch x86_64"
LIBUSB_CONFIG="--disable-shared"
USB4JAVA_LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
LIBUSB="libusb"
build

View File

@ -1,13 +1,16 @@
AC_PREREQ([2.61])
AC_INIT([libusb4java], [1.0.0], [k@ailis.de])
AM_INIT_AUTOMAKE([foreign -Wall -Wno-extra-portability -Werror])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES(yes)])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_SRCDIR([src/usb4java.h])
AC_LANG_C
AC_PROG_CC
AC_PROG_CC_STDC
AM_PROG_LIBTOOL
m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
AC_CHECK_JAVA
AC_CHECK_SIZEOF([void *])
PKG_CHECK_MODULES([LIBUSB], [libusb-1.0])

View File

@ -7,7 +7,7 @@
#include "Interface.h"
void setConfigDescriptor(JNIEnv* env,
struct libusb_config_descriptor* descriptor, jobject object)
const struct libusb_config_descriptor* descriptor, jobject object)
{
SET_POINTER(env, descriptor, object, "configDescriptorPointer");
}
@ -35,7 +35,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bLength)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -49,7 +49,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bDescriptorType)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -63,7 +63,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(ConfigDescriptor, wTotalLength)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->wTotalLength;
return (jshort) descriptor->wTotalLength;
}
/**
@ -77,7 +77,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bNumInterfaces)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bNumInterfaces;
return (jbyte) descriptor->bNumInterfaces;
}
/**
@ -91,7 +91,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bConfigurationValue)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bConfigurationValue;
return (jbyte) descriptor->bConfigurationValue;
}
/**
@ -105,7 +105,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, iConfiguration)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iConfiguration;
return (jbyte) descriptor->iConfiguration;
}
/**
@ -119,7 +119,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bmAttributes)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bmAttributes;
return (jbyte) descriptor->bmAttributes;
}
/**
@ -133,7 +133,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(ConfigDescriptor, bMaxPower)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->MaxPower;
return (jbyte) descriptor->MaxPower;
}
/**
@ -162,8 +162,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(ConfigDescriptor, extra)
struct libusb_config_descriptor *descriptor =
unwrapConfigDescriptor(env, this);
if (!descriptor) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) descriptor->extra,
descriptor->extra_length);
return NewDirectReadOnlyByteBuffer(env, descriptor->extra, descriptor->extra_length);
}
/**

View File

@ -8,7 +8,7 @@
#include "usb4java.h"
void setConfigDescriptor(JNIEnv*, struct libusb_config_descriptor*, jobject);
void setConfigDescriptor(JNIEnv*, const struct libusb_config_descriptor*, jobject);
struct libusb_config_descriptor* unwrapConfigDescriptor(JNIEnv*, jobject);
void resetConfigDescriptor(JNIEnv*, jobject);

View File

@ -5,7 +5,7 @@
#include "Context.h"
void setContext(JNIEnv* env, libusb_context* context, jobject object)
void setContext(JNIEnv* env, const libusb_context* context, jobject object)
{
SET_POINTER(env, context, object, "contextPointer");
}

View File

@ -8,7 +8,7 @@
#include "usb4java.h"
void setContext(JNIEnv*, libusb_context*, jobject);
void setContext(JNIEnv*, const libusb_context*, jobject);
libusb_context* unwrapContext(JNIEnv*, jobject);
void resetContext(JNIEnv*, jobject);

View File

@ -5,7 +5,7 @@
#include "Device.h"
jobject wrapDevice(JNIEnv* env, libusb_device* device)
jobject wrapDevice(JNIEnv* env, const libusb_device* device)
{
WRAP_POINTER(env, device, "Device", "devicePointer");
}
@ -14,3 +14,8 @@ libusb_device* unwrapDevice(JNIEnv* env, jobject device)
{
UNWRAP_POINTER(env, device, libusb_device*, "devicePointer");
}
void resetDevice(JNIEnv* env, jobject object)
{
RESET_POINTER(env, object, "devicePointer");
}

View File

@ -8,7 +8,8 @@
#include "usb4java.h"
jobject wrapDevice(JNIEnv*, libusb_device*);
jobject wrapDevice(JNIEnv*, const libusb_device*);
libusb_device* unwrapDevice(JNIEnv*, jobject);
void resetDevice(JNIEnv*, jobject);
#endif

View File

@ -6,17 +6,21 @@
#include "DeviceDescriptor.h"
void setDeviceDescriptor(JNIEnv* env,
struct libusb_device_descriptor* descriptor, jobject object)
const struct libusb_device_descriptor* descriptor, jobject object)
{
SET_DATA(env, descriptor, sizeof(struct libusb_device_descriptor),
object, "deviceDescriptorData");
SET_POINTER(env, descriptor, object, "deviceDescriptorPointer");
}
struct libusb_device_descriptor* unwrapDeviceDescriptor(JNIEnv* env,
jobject descriptor)
struct libusb_device_descriptor* unwrapDeviceDescriptor(JNIEnv *env,
jobject obj)
{
UNWRAP_DATA(env, descriptor, struct libusb_device_descriptor*,
"deviceDescriptorData");
UNWRAP_POINTER(env, obj, struct libusb_device_descriptor*,
"deviceDescriptorPointer");
}
void resetDeviceDescriptor(JNIEnv* env, jobject obj)
{
RESET_POINTER(env, obj, "deviceDescriptorPointer");
}
/**
@ -30,7 +34,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bLength)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -44,7 +48,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDescriptorType)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -58,7 +62,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, bcdUSB)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bcdUSB;
return (jshort) descriptor->bcdUSB;
}
/**
@ -72,7 +76,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDeviceClass)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDeviceClass;
return (jbyte) descriptor->bDeviceClass;
}
/**
@ -86,7 +90,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDeviceSubClass)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDeviceSubClass;
return (jbyte) descriptor->bDeviceSubClass;
}
/**
@ -100,7 +104,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bDeviceProtocol)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDeviceProtocol;
return (jbyte) descriptor->bDeviceProtocol;
}
/**
@ -114,7 +118,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bMaxPacketSize0)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bMaxPacketSize0;
return (jbyte) descriptor->bMaxPacketSize0;
}
/**
@ -128,7 +132,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, idVendor)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->idVendor;
return (jshort) descriptor->idVendor;
}
/**
@ -142,7 +146,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, idProduct)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->idProduct;
return (jshort) descriptor->idProduct;
}
/**
@ -156,7 +160,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(DeviceDescriptor, bcdDevice)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bcdDevice;
return (jshort) descriptor->bcdDevice;
}
@ -171,7 +175,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iManufacturer)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iManufacturer;
return (jbyte) descriptor->iManufacturer;
}
/**
@ -185,7 +189,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iProduct)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iProduct;
return (jbyte) descriptor->iProduct;
}
/**
@ -197,7 +201,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, iSerialNumber)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iSerialNumber;
return (jbyte) descriptor->iSerialNumber;
}
/**
@ -211,5 +215,5 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(DeviceDescriptor, bNumConfigurations)
struct libusb_device_descriptor* descriptor =
unwrapDeviceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bNumConfigurations;
return (jbyte) descriptor->bNumConfigurations;
}

View File

@ -8,7 +8,8 @@
#include "usb4java.h"
void setDeviceDescriptor(JNIEnv*, struct libusb_device_descriptor*, jobject);
void setDeviceDescriptor(JNIEnv*, const struct libusb_device_descriptor*, jobject);
struct libusb_device_descriptor* unwrapDeviceDescriptor(JNIEnv*, jobject);
void resetDeviceDescriptor(JNIEnv*, jobject);
#endif

View File

@ -5,19 +5,21 @@
#include "DeviceHandle.h"
void setDeviceHandle(JNIEnv* env, libusb_device_handle* deviceHandle, jobject object)
void setDeviceHandle(JNIEnv* env, const libusb_device_handle* deviceHandle,
jobject object)
{
SET_POINTER(env, deviceHandle, object, "deviceHandlePointer");
}
jobject wrapDeviceHandle(JNIEnv* env, libusb_device_handle* deviceHandle)
jobject wrapDeviceHandle(JNIEnv* env, const libusb_device_handle* deviceHandle)
{
WRAP_POINTER(env, deviceHandle, "DeviceHandle", "deviceHandlePointer");
}
libusb_device_handle* unwrapDeviceHandle(JNIEnv* env, jobject deviceHandle)
{
UNWRAP_POINTER(env, deviceHandle, libusb_device_handle*, "deviceHandlePointer");
UNWRAP_POINTER(env, deviceHandle, libusb_device_handle*,
"deviceHandlePointer");
}
void resetDeviceHandle(JNIEnv* env, jobject object)

View File

@ -8,8 +8,8 @@
#include "usb4java.h"
void setDeviceHandle(JNIEnv*, libusb_device_handle*, jobject);
jobject wrapDeviceHandle(JNIEnv*, libusb_device_handle*);
void setDeviceHandle(JNIEnv*, const libusb_device_handle*, jobject);
jobject wrapDeviceHandle(JNIEnv*, const libusb_device_handle*);
libusb_device_handle* unwrapDeviceHandle(JNIEnv*, jobject);
void resetDeviceHandle(JNIEnv*, jobject);

View File

@ -6,12 +6,12 @@
#include "DeviceList.h"
#include "Device.h"
void setDeviceList(JNIEnv* env, libusb_device** list, int size, jobject object)
void setDeviceList(JNIEnv* env, libusb_device* const * list, jint size, jobject object)
{
SET_POINTER(env, list, object, "deviceListPointer");
jclass cls = (*env)->GetObjectClass(env, object);
jfieldID field = (*env)->GetFieldID(env, cls, "size", "I");
// We already have the class from the previous call.
field = (*env)->GetFieldID(env, cls, "size", "I");
(*env)->SetIntField(env, object, field, size);
}
@ -23,6 +23,11 @@ libusb_device** unwrapDeviceList(JNIEnv* env, jobject list)
void resetDeviceList(JNIEnv* env, jobject obj)
{
RESET_POINTER(env, obj, "deviceListPointer");
// We already have the class from the previous call.
// Reset size field to zero too.
field = (*env)->GetFieldID(env, cls, "size", "I");
(*env)->SetIntField(env, obj, field, 0);
}
/**
@ -33,9 +38,13 @@ JNIEXPORT jobject JNICALL METHOD_NAME(DeviceList, get)
JNIEnv *env, jobject this, jint index
)
{
libusb_device **list = unwrapDeviceList(env, this);
if (!list) return NULL;
jclass cls = (*env)->GetObjectClass(env, this);
jfieldID field = (*env)->GetFieldID(env, cls, "size", "I");
int size = (*env)->GetIntField(env, this, field);
if (index < 0 || index >= size) return NULL;
return wrapDevice(env, unwrapDeviceList(env, this)[index]);
return wrapDevice(env, list[index]);
}

View File

@ -8,7 +8,7 @@
#include "usb4java.h"
void setDeviceList(JNIEnv*, libusb_device**, int, jobject);
void setDeviceList(JNIEnv*, libusb_device* const *, jint, jobject);
libusb_device** unwrapDeviceList(JNIEnv*, jobject);
void resetDeviceList(JNIEnv*, jobject);

View File

@ -14,14 +14,14 @@ jobject wrapEndpointDescriptor(JNIEnv *env,
jobjectArray wrapEndpointDescriptors(JNIEnv *env, int count,
const struct libusb_endpoint_descriptor *descriptors)
{
int i;
jobjectArray array = (jobjectArray) (*env)->NewObjectArray(env,
count, (*env)->FindClass(env, PACKAGE_DIR"/EndpointDescriptor"),
NULL);
for (i = 0; i < count; i++)
for (int i = 0; i < count; i++)
(*env)->SetObjectArrayElement(env, array, i,
wrapEndpointDescriptor(env, &descriptors[i]));
return array;
}
@ -42,7 +42,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bLength)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -56,7 +56,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bDescriptorType)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -70,7 +70,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bEndpointAddress)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bEndpointAddress;
return (jbyte) descriptor->bEndpointAddress;
}
/**
@ -84,7 +84,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bmAttributes)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bmAttributes;
return (jbyte) descriptor->bmAttributes;
}
/**
@ -98,7 +98,7 @@ JNIEXPORT jshort JNICALL METHOD_NAME(EndpointDescriptor, wMaxPacketSize)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->wMaxPacketSize;
return (jshort) descriptor->wMaxPacketSize;
}
/**
@ -112,7 +112,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bInterval)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bInterval;
return (jbyte) descriptor->bInterval;
}
/**
@ -126,13 +126,13 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bRefresh)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bRefresh;
return (jbyte) descriptor->bRefresh;
}
/**
* byte bSynchAddress()
*/
JNIEXPORT jint JNICALL METHOD_NAME(EndpointDescriptor, bSynchAddress)
JNIEXPORT jbyte JNICALL METHOD_NAME(EndpointDescriptor, bSynchAddress)
(
JNIEnv *env, jobject this
)
@ -140,7 +140,7 @@ JNIEXPORT jint JNICALL METHOD_NAME(EndpointDescriptor, bSynchAddress)
struct libusb_endpoint_descriptor* descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bSynchAddress;
return (jbyte) descriptor->bSynchAddress;
}
/**
@ -154,8 +154,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(EndpointDescriptor, extra)
struct libusb_endpoint_descriptor *descriptor =
unwrapEndpointDescriptor(env, this);
if (!descriptor) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) descriptor->extra,
descriptor->extra_length);
return NewDirectReadOnlyByteBuffer(env, descriptor->extra, descriptor->extra_length);
}
/**

View File

@ -8,6 +8,8 @@
#include "usb4java.h"
jobject wrapEndpointDescriptor(JNIEnv*,
const struct libusb_endpoint_descriptor*);
jobjectArray wrapEndpointDescriptors(JNIEnv*, int,
const struct libusb_endpoint_descriptor*);
struct libusb_endpoint_descriptor* unwrapEndpointDescriptor(JNIEnv*,

View File

@ -14,14 +14,13 @@ jobject wrapInterface(JNIEnv *env, const struct libusb_interface *iface)
jobjectArray wrapInterfaces(JNIEnv *env, int count,
const struct libusb_interface *interfaces)
{
int i;
jobjectArray array = (jobjectArray) (*env)->NewObjectArray(env,
count, (*env)->FindClass(env, PACKAGE_DIR"/Interface"),
NULL);
for (i = 0; i < count; i++)
for (int i = 0; i < count; i++)
(*env)->SetObjectArrayElement(env, array, i,
wrapInterface(env, &interfaces[i]));
return array;
}
@ -30,14 +29,14 @@ struct libusb_interface *unwrapInterface(JNIEnv *env, jobject obj)
UNWRAP_POINTER(env, obj, struct libusb_interface*, "interfacePointer");
}
JNIEXPORT jshort JNICALL METHOD_NAME(Interface, numAltsetting)
JNIEXPORT jint JNICALL METHOD_NAME(Interface, numAltsetting)
(
JNIEnv *env, jobject this
)
{
struct libusb_interface* interface = unwrapInterface(env, this);
if (!interface) return 0;
return (jshort) interface->num_altsetting;
return interface->num_altsetting;
}
JNIEXPORT jobjectArray JNICALL METHOD_NAME(Interface, altsetting)

View File

@ -8,7 +8,8 @@
#include "usb4java.h"
extern jobject wrapInterface(JNIEnv *, const struct libusb_interface *);
extern jobjectArray wrapInterfaces(JNIEnv *, int, const struct libusb_interface *);
jobject wrapInterface(JNIEnv *, const struct libusb_interface *);
jobjectArray wrapInterfaces(JNIEnv *, int, const struct libusb_interface *);
struct libusb_interface *unwrapInterface(JNIEnv *, jobject);
#endif

View File

@ -17,14 +17,13 @@ jobject wrapInterfaceDescriptor(JNIEnv *env,
jobjectArray wrapInterfaceDescriptors(JNIEnv *env, int count,
const struct libusb_interface_descriptor *descriptors)
{
int i;
jobjectArray array = (jobjectArray) (*env)->NewObjectArray(env,
count, (*env)->FindClass(env, PACKAGE_DIR"/InterfaceDescriptor"),
NULL);
for (i = 0; i < count; i++)
for (int i = 0; i < count; i++)
(*env)->SetObjectArrayElement(env, array, i,
wrapInterfaceDescriptor(env, &descriptors[i]));
return array;
}
@ -46,7 +45,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bLength)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bLength;
return (jbyte) descriptor->bLength;
}
/**
@ -60,7 +59,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bDescriptorType)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bDescriptorType;
return (jbyte) descriptor->bDescriptorType;
}
/**
@ -74,7 +73,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bInterfaceNumber)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bInterfaceNumber;
return (jbyte) descriptor->bInterfaceNumber;
}
/**
@ -88,7 +87,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bAlternateSetting)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bAlternateSetting;
return (jbyte) descriptor->bAlternateSetting;
}
/**
@ -102,7 +101,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bNumEndpoints)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bNumEndpoints;
return (jbyte) descriptor->bNumEndpoints;
}
/**
@ -116,7 +115,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bInterfaceClass)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bInterfaceClass;
return (jbyte) descriptor->bInterfaceClass;
}
/**
@ -130,7 +129,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bInterfaceSubClass)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bInterfaceSubClass;
return (jbyte) descriptor->bInterfaceSubClass;
}
/**
@ -144,7 +143,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, bInterfaceProtocol)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->bInterfaceProtocol;
return (jbyte) descriptor->bInterfaceProtocol;
}
/**
@ -158,7 +157,7 @@ JNIEXPORT jbyte JNICALL METHOD_NAME(InterfaceDescriptor, iInterface)
struct libusb_interface_descriptor* descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return 0;
return descriptor->iInterface;
return (jbyte) descriptor->iInterface;
}
/**
@ -187,7 +186,7 @@ JNIEXPORT jobject JNICALL METHOD_NAME(InterfaceDescriptor, extra)
struct libusb_interface_descriptor *descriptor =
unwrapInterfaceDescriptor(env, this);
if (!descriptor) return NULL;
return (*env)->NewDirectByteBuffer(env, (void *) descriptor->extra,
return NewDirectReadOnlyByteBuffer(env, descriptor->extra,
descriptor->extra_length);
}

View File

@ -8,7 +8,10 @@
#include "usb4java.h"
jobject wrapInterfaceDescriptor(JNIEnv*,
const struct libusb_interface_descriptor*);
jobjectArray wrapInterfaceDescriptors(JNIEnv*, int,
const struct libusb_interface_descriptor*);
struct libusb_interface_descriptor *unwrapInterfaceDescriptor(JNIEnv*, jobject);
#endif

View File

@ -0,0 +1,88 @@
#include "IsoPacketDescriptor.h"
jobject wrapIsoPacketDescriptor(JNIEnv *env,
const struct libusb_iso_packet_descriptor *descriptor)
{
WRAP_POINTER(env, descriptor, "IsoPacketDescriptor",
"isoPacketDescriptorPointer");
}
jobjectArray wrapIsoPacketDescriptors(JNIEnv *env, int count,
const struct libusb_iso_packet_descriptor *descriptors)
{
jobjectArray array = (jobjectArray) (*env)->NewObjectArray(env, count,
(*env)->FindClass(env, PACKAGE_DIR"/IsoPacketDescriptor"), NULL);
for (int i = 0; i < count; i++)
(*env)->SetObjectArrayElement(env, array, i,
wrapIsoPacketDescriptor(env, &descriptors[i]));
return array;
}
struct libusb_iso_packet_descriptor *unwrapIsoPacketDescriptor(JNIEnv *env,
jobject obj)
{
UNWRAP_POINTER(env, obj, struct libusb_iso_packet_descriptor*,
"isoPacketDescriptorPointer");
}
/**
* int length()
*/
JNIEXPORT jint JNICALL METHOD_NAME(IsoPacketDescriptor, length)
(
JNIEnv *env, jobject this
)
{
struct libusb_iso_packet_descriptor *isopacket =
unwrapIsoPacketDescriptor(env, this);
if (!isopacket) return 0;
return (jint) isopacket->length;
}
/**
* void setLength(int)
*/
JNIEXPORT void JNICALL METHOD_NAME(IsoPacketDescriptor, setLength)
(
JNIEnv *env, jobject this, jint length
)
{
struct libusb_iso_packet_descriptor *isopacket =
unwrapIsoPacketDescriptor(env, this);
if (!isopacket) return;
isopacket->length = (unsigned int) length;
}
/**
* int actualLength()
*/
JNIEXPORT jint JNICALL METHOD_NAME(IsoPacketDescriptor, actualLength)
(
JNIEnv *env, jobject this
)
{
struct libusb_iso_packet_descriptor *isopacket =
unwrapIsoPacketDescriptor(env, this);
if (!isopacket) return 0;
return (jint) isopacket->actual_length;
}
/**
* int status()
*/
JNIEXPORT jint JNICALL METHOD_NAME(IsoPacketDescriptor, status)
(
JNIEnv *env, jobject this
)
{
struct libusb_iso_packet_descriptor *isopacket =
unwrapIsoPacketDescriptor(env, this);
if (!isopacket) return 0;
return isopacket->status;
}

View File

@ -0,0 +1,12 @@
#ifndef USB4JAVA_ISO_PACKET_DESCRIPTOR_H
#define USB4JAVA_ISO_PACKET_DESCRIPTOR_H
#include "usb4java.h"
jobject wrapIsoPacketDescriptor(JNIEnv*,
const struct libusb_iso_packet_descriptor*);
jobjectArray wrapIsoPacketDescriptors(JNIEnv*, int,
const struct libusb_iso_packet_descriptor*);
struct libusb_iso_packet_descriptor *unwrapIsoPacketDescriptor(JNIEnv*, jobject);
#endif

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
lib_LTLIBRARIES = libusb4java.la
libusb4java_la_CFLAGS = -Wall -Werror $(LIBUSB_CFLAGS)
libusb4java_la_CFLAGS = -pedantic -Wall -Wextra -Wformat=2 -Winit-self -Wunused -Wno-unused-parameter -Wuninitialized -Wundef -Wshadow -Wbad-function-cast -Wcast-qual -Wcast-align -Wwrite-strings -Wconversion -Wstrict-prototypes -Wredundant-decls -Wnested-externs $(LIBUSB_CFLAGS)
libusb4java_la_LIBADD = $(LIBUSB_LIBS)
libusb4java_la_LDFLAGS = -version-info 1:0:0 -no-undefined
EXTRA_DIST = *.h
@ -17,6 +17,7 @@ libusb4java_la_SOURCES = \
Interface.c \
InterfaceDescriptor.c \
EndpointDescriptor.c \
IsoPacketDescriptor.c \
Transfer.c \
SsEndpointCompanionDescriptor.c \
BosDescriptor.c \

View File

@ -3,22 +3,33 @@
* See COPYING file for copying conditions
*/
#include <stdlib.h>
#include "Transfer.h"
#include "DeviceHandle.h"
#include "IsoPacketDescriptor.h"
jobject wrapTransfer(JNIEnv* env, struct libusb_transfer* transfer)
static void LIBUSB_CALL cleanupCallback(struct libusb_transfer *transfer);
static void LIBUSB_CALL transferCallback(struct libusb_transfer *transfer);
jobject wrapTransfer(JNIEnv* env, const struct libusb_transfer* transfer)
{
WRAP_POINTER(env, transfer, "Transfer", "pointer");
WRAP_POINTER(env, transfer, "Transfer", "transferPointer");
}
struct libusb_transfer* unwrapTransfer(JNIEnv *env, jobject obj)
struct libusb_transfer* unwrapTransfer(JNIEnv* env, jobject obj)
{
UNWRAP_POINTER(env, obj, struct libusb_transfer*, "pointer");
UNWRAP_POINTER(env, obj, struct libusb_transfer*, "transferPointer");
}
void resetTransfer(JNIEnv* env, jobject obj)
{
RESET_POINTER(env, obj, "pointer");
RESET_POINTER(env, obj, "transferPointer");
// We already have the class from the previous call.
// Reset transferBuffer field to NULL too.
field = (*env)->GetFieldID(env, cls, "transferBuffer",
"Ljava/nio/ByteBuffer;");
(*env)->SetObjectField(env, obj, field, NULL);
}
/**
@ -29,84 +40,110 @@ JNIEXPORT void JNICALL METHOD_NAME(Transfer, setDevHandle)
JNIEnv *env, jobject this, jobject handle
)
{
unwrapTransfer(env, this)->dev_handle = unwrapDeviceHandle(env, handle);
libusb_device_handle *dev_handle = unwrapDeviceHandle(env, handle);
if (!dev_handle && handle) return;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->dev_handle = dev_handle;
}
/**
* DeviceHandle getDevHandle()
* DeviceHandle devHandle()
*/
JNIEXPORT jobject JNICALL METHOD_NAME(Transfer, getDevHandle)
JNIEXPORT jobject JNICALL METHOD_NAME(Transfer, devHandle)
(
JNIEnv *env, jobject this
)
{
return wrapDeviceHandle(env, unwrapTransfer(env, this)->dev_handle);
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return NULL;
return wrapDeviceHandle(env, transfer->dev_handle);
}
/**
* void setFlags(int)
* void setFlags(byte)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setFlags)
(
JNIEnv *env, jobject this, jint flags
JNIEnv *env, jobject this, jbyte flags
)
{
unwrapTransfer(env, this)->flags = flags;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->flags = (uint8_t) flags;
}
/**
* int getFlags()
* byte flags()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, getFlags)
JNIEXPORT jbyte JNICALL METHOD_NAME(Transfer, flags)
(
JNIEnv *env, jobject this
)
{
return unwrapTransfer(env, this)->flags;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return (jbyte) transfer->flags;
}
/**
* void setEndpoint(int)
* void setEndpoint(byte)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setEndpoint)
(
JNIEnv *env, jobject this, jint endpoint
JNIEnv *env, jobject this, jbyte endpoint
)
{
unwrapTransfer(env, this)->endpoint = endpoint;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->endpoint = (unsigned char) endpoint;
}
/**
* int getEndpoint()
* byte endpoint()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, getEndpoint)
JNIEXPORT jbyte JNICALL METHOD_NAME(Transfer, endpoint)
(
JNIEnv *env, jobject this
)
{
return unwrapTransfer(env, this)->endpoint;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return (jbyte) transfer->endpoint;
}
/**
* void setType(int)
* void setType(byte)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setType)
(
JNIEnv *env, jobject this, jint type
JNIEnv *env, jobject this, jbyte type
)
{
unwrapTransfer(env, this)->type = type;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->type = (unsigned char) type;
}
/**
* int getType()
* byte type()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, getType)
JNIEXPORT jbyte JNICALL METHOD_NAME(Transfer, type)
(
JNIEnv *env, jobject this
)
{
return unwrapTransfer(env, this)->type;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return (jbyte) transfer->type;
}
/**
@ -117,27 +154,350 @@ JNIEXPORT void JNICALL METHOD_NAME(Transfer, setTimeout)
JNIEnv *env, jobject this, jlong timeout
)
{
unwrapTransfer(env, this)->timeout = timeout;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->timeout = (unsigned int) timeout;
}
/**
* long getTimeout()
* long timeout()
*/
JNIEXPORT jlong JNICALL METHOD_NAME(Transfer, getTimeout)
JNIEXPORT jlong JNICALL METHOD_NAME(Transfer, timeout)
(
JNIEnv *env, jobject this
)
{
return unwrapTransfer(env, this)->timeout;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return transfer->timeout;
}
/**
* int getStatus()
* int status()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, getStatus)
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, status)
(
JNIEnv *env, jobject this
)
{
return unwrapTransfer(env, this)->status;
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return transfer->status;
}
/**
* void setLengthNative(int)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setLengthNative)
(
JNIEnv *env, jobject this, jint length
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->length = length;
}
/**
* int length()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, length)
(
JNIEnv *env, jobject this
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return transfer->length;
}
/**
* int actualLength()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, actualLength)
(
JNIEnv *env, jobject this
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return transfer->actual_length;
}
void cleanupGlobalReferences(JNIEnv *env, jobject obj)
{
struct libusb_transfer *transfer = unwrapTransfer(env, obj);
if (!transfer) return;
struct transfer_data *transferData =
((struct transfer_data *) transfer->user_data);
// Cleanup all global references, if any currently exist.
if (transferData->callbackObject != NULL)
{
(*env)->DeleteGlobalRef(env, transferData->callbackObject);
}
if (transferData->callbackUserDataObject != NULL)
{
(*env)->DeleteGlobalRef(env, transferData->callbackUserDataObject);
}
if (transferData->transferObject != NULL)
{
(*env)->DeleteGlobalRef(env, transferData->transferObject);
}
}
void cleanupCallbackEnable(JNIEnv *env, jobject obj)
{
struct libusb_transfer *transfer = unwrapTransfer(env, obj);
if (!transfer) return;
struct transfer_data *transferData =
((struct transfer_data *) transfer->user_data);
transferData->transferObject = (*env)->NewGlobalRef(env, obj);
transfer->callback = &cleanupCallback;
transferData->callbackObject = NULL;
transferData->callbackObjectMethod = 0;
}
static void LIBUSB_CALL cleanupCallback(struct libusb_transfer *transfer)
{
THREAD_BEGIN(env)
struct transfer_data *transferData =
((struct transfer_data *) transfer->user_data);
// The saved reference to the Java Transfer object.
jobject jTransfer = transferData->transferObject;
// Cleanup Java Transfer object too, if requested.
if (transfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
{
cleanupGlobalReferences(env, jTransfer);
resetTransfer(env, jTransfer);
free(transferData);
}
THREAD_END
}
static void LIBUSB_CALL transferCallback(struct libusb_transfer *transfer)
{
THREAD_BEGIN(env)
struct transfer_data *transferData =
((struct transfer_data *) transfer->user_data);
// The saved references to the Java TransferCallback object.
jobject jCallback = transferData->callbackObject;
jmethodID jCallbackMethod = transferData->callbackObjectMethod;
// The saved reference to the Java Transfer object.
jobject jTransfer = transferData->transferObject;
// Read flags before calling the Java method, as it could
// free the Transfer itself.
if (transfer->flags & LIBUSB_TRANSFER_FREE_TRANSFER)
{
// Call back into Java.
(*env)->CallVoidMethod(env, jCallback, jCallbackMethod, jTransfer);
// Cleanup Java Transfer object too, if requested.
cleanupGlobalReferences(env, jTransfer);
resetTransfer(env, jTransfer);
free(transferData);
}
else
{
// Call back into Java.
(*env)->CallVoidMethod(env, jCallback, jCallbackMethod, jTransfer);
}
THREAD_END
}
/**
* void setCallback(TransferCallback)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setCallback)
(
JNIEnv *env, jobject this, jobject callback
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
struct transfer_data *transferData =
((struct transfer_data *) transfer->user_data);
if (transferData->transferObject != NULL)
{
(*env)->DeleteGlobalRef(env, transferData->transferObject);
}
if (transferData->callbackObject != NULL)
{
(*env)->DeleteGlobalRef(env, transferData->callbackObject);
}
if (callback != NULL)
{
transferData->transferObject = (*env)->NewGlobalRef(env, this);
transfer->callback = &transferCallback;
jclass cls = (*env)->GetObjectClass(env, callback);
jmethodID method = (*env)->GetMethodID(env, cls, "processTransfer",
"(L"PACKAGE_DIR"/Transfer;)V");
transferData->callbackObject = (*env)->NewGlobalRef(env, callback);
transferData->callbackObjectMethod = method;
}
else
{
cleanupCallbackEnable(env, this);
}
}
/**
* TransferCallback callback()
*/
JNIEXPORT jobject JNICALL METHOD_NAME(Transfer, callback)
(
JNIEnv *env, jobject this
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return NULL;
return ((struct transfer_data *) transfer->user_data)->callbackObject;
}
/**
* void setUserData(Object)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setUserData)
(
JNIEnv *env, jobject this, jobject userData
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
struct transfer_data *transferData =
((struct transfer_data *) transfer->user_data);
if (transferData->callbackUserDataObject != NULL)
{
(*env)->DeleteGlobalRef(env, transferData->callbackUserDataObject);
}
if (userData != NULL)
{
transferData->callbackUserDataObject = (*env)->NewGlobalRef(env,
userData);
}
else
{
transferData->callbackUserDataObject = NULL;
}
}
/**
* Object userData()
*/
JNIEXPORT jobject JNICALL METHOD_NAME(Transfer, userData)
(
JNIEnv *env, jobject this
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return NULL;
return ((struct transfer_data *) transfer->user_data)->callbackUserDataObject;
}
/**
* void setBufferNative(ByteBuffer)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setBufferNative)
(
JNIEnv *env, jobject this, jobject buffer
)
{
unsigned char *buffer_ptr = NULL;
if (buffer)
{
DIRECT_BUFFER(env, buffer, buffer_tmp, return);
buffer_ptr = buffer_tmp;
}
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
transfer->buffer = buffer_ptr;
}
/**
* void setNumIsoPackets(int)
*/
JNIEXPORT void JNICALL METHOD_NAME(Transfer, setNumIsoPackets)
(
JNIEnv *env, jobject this, jint numIsoPackets
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return;
// Check that calls to setNumIsoPackets() never set a number exceeding
// the maximum, which was originally set at allocTransfer() time.
if (((struct transfer_data *) transfer->user_data)->maxNumIsoPackets
< (size_t) numIsoPackets)
{
illegalArgument(env,
"numIsoPackets exceeds maximum allowed number set with allocTransfer()");
return;
}
transfer->num_iso_packets = numIsoPackets;
}
/**
* int numIsoPackets()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Transfer, numIsoPackets)
(
JNIEnv *env, jobject this
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return 0;
return transfer->num_iso_packets;
}
/**
* IsoPacketDescriptor[] isoPacketDesc()
*/
JNIEXPORT jobjectArray JNICALL METHOD_NAME(Transfer, isoPacketDesc)
(
JNIEnv *env, jobject this
)
{
struct libusb_transfer *transfer = unwrapTransfer(env, this);
if (!transfer) return NULL;
return wrapIsoPacketDescriptors(env, transfer->num_iso_packets,
transfer->iso_packet_desc);
}

View File

@ -8,7 +8,19 @@
#include "usb4java.h"
jobject wrapTransfer(JNIEnv*, struct libusb_transfer*);
struct transfer_data
{
jobject transferObject;
jobject callbackObject;
jmethodID callbackObjectMethod;
jobject callbackUserDataObject;
size_t maxNumIsoPackets;
};
void cleanupGlobalReferences(JNIEnv*, jobject);
void cleanupCallbackEnable(JNIEnv*, jobject);
jobject wrapTransfer(JNIEnv*, const struct libusb_transfer*);
struct libusb_transfer* unwrapTransfer(JNIEnv*, jobject);
void resetTransfer(JNIEnv*, jobject);

View File

@ -7,12 +7,12 @@
jobject wrapVersion(JNIEnv* env, const struct libusb_version* pointer)
{
WRAP_POINTER(env, pointer, "Version", "pointer");
WRAP_POINTER(env, pointer, "Version", "versionPointer");
}
const struct libusb_version* unwrapVersion(JNIEnv* env, jobject object)
{
UNWRAP_POINTER(env, object, struct libusb_version*, "pointer");
UNWRAP_POINTER(env, object, const struct libusb_version*, "versionPointer");
}
/**
@ -55,7 +55,20 @@ JNIEXPORT jint JNICALL METHOD_NAME(Version, micro)
}
/**
* string rc()
* int nano()
*/
JNIEXPORT jint JNICALL METHOD_NAME(Version, nano)
(
JNIEnv *env, jobject this
)
{
const struct libusb_version *version = unwrapVersion(env, this);
if (!version) return 0;
return version->nano;
}
/**
* String rc()
*/
JNIEXPORT jstring JNICALL METHOD_NAME(Version, rc)
(

View File

@ -5,15 +5,37 @@
#include "usb4java.h"
jint illegalArgument(JNIEnv *env, char *message)
JavaVM *jvm = NULL;
jint illegalArgument(JNIEnv *env, const char *message)
{
jclass cls = (*env)->FindClass(env, "java/lang/IllegalArgumentException");
return (*env)->ThrowNew(env, cls, message);
}
jint illegalState(JNIEnv *env, char *message)
jint illegalState(JNIEnv *env, const char *message)
{
jclass cls = (*env)->FindClass(env, "java/lang/IllegalStateException");
return (*env)->ThrowNew(env, cls, message);
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wcast-qual"
jobject NewDirectReadOnlyByteBuffer(JNIEnv *env, const void *mem,
int mem_length)
{
jobject buffer = (*env)->NewDirectByteBuffer(env, (void *) mem, mem_length);
// Get a read-only buffer from this buffer.
jclass cls = (*env)->GetObjectClass(env, buffer);
jmethodID method = (*env)->GetMethodID(env, cls, "asReadOnlyBuffer",
"()Ljava/nio/ByteBuffer;");
return (*env)->CallObjectMethod(env, buffer, method);
}
#pragma GCC diagnostic pop
jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved)
{
jvm = vm;
return JNI_VERSION_1_4;
}

View File

@ -20,21 +20,16 @@
#endif
#define SET_POINTER(ENV, PTR, OBJECT, FIELD) \
{ \
jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \
(*ENV)->SetLongField(ENV, OBJECT, field, (jptr) PTR); \
}
(*ENV)->SetLongField(ENV, OBJECT, field, (jptr) PTR);
#define RESET_POINTER(ENV, OBJECT, FIELD) \
{ \
jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \
(*ENV)->SetLongField(ENV, OBJECT, field, 0); \
}
(*ENV)->SetLongField(ENV, OBJECT, field, 0);
#define WRAP_POINTER(ENV, PTR, CLASS_NAME, FIELD) \
{ \
if (!PTR) return NULL; \
jclass cls = (*ENV)->FindClass(ENV, PACKAGE_DIR"/"CLASS_NAME); \
if (cls == NULL) return NULL; \
@ -43,53 +38,24 @@
jobject object = (*ENV)->NewObject(ENV, cls, constructor); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \
(*ENV)->SetLongField(ENV, object, field, (jptr) PTR); \
return object; \
}
return object;
#define UNWRAP_POINTER(ENV, OBJECT, TYPE, FIELD) \
{ \
if (!OBJECT) return NULL; \
jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \
jptr ptr = (jptr) (*ENV)->GetLongField(ENV, OBJECT, field); \
if (!ptr) illegalState(ENV, FIELD" is not initialized"); \
return (TYPE) (jptr) (*ENV)->GetLongField(ENV, OBJECT, field); \
}
return (TYPE) ptr;
#define SET_DATA(ENV, PTR, SIZE, OBJECT, FIELD) \
{ \
jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, \
"Ljava/nio/ByteBuffer;"); \
jobject buffer = (*ENV)->NewDirectByteBuffer(env, PTR, SIZE); \
(*ENV)->SetObjectField(ENV, OBJECT, field, buffer); \
}
#define UNWRAP_DATA(ENV, OBJECT, TYPE, FIELD) \
{ \
jclass cls = (*ENV)->GetObjectClass(ENV, OBJECT); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, \
"Ljava/nio/ByteBuffer;"); \
jobject buffer = (*ENV)->GetObjectField(ENV, OBJECT, field); \
if (!buffer) \
{ \
illegalState(ENV, FIELD" is not initialized"); \
return NULL; \
} \
return (TYPE) (*ENV)->GetDirectBufferAddress(ENV, buffer); \
}
#define DIRECT_BUFFER(ENV, VAR, ACTION) \
{ \
jclass cls = (*ENV)->GetObjectClass(ENV, VAR); \
jmethodID method = (*ENV)->GetMethodID(ENV, cls, "isDirect", \
"()Z"); \
if (!(*ENV)->CallBooleanMethod(ENV, VAR, method)) \
{ \
illegalArgument(ENV, #VAR" must be a direct buffer"); \
ACTION; \
} \
}
// GetDirectBufferAddress returns NULL if called on a non-direct buffer.
#define DIRECT_BUFFER(ENV, VAR, BUFFER, ACTION) \
unsigned char *BUFFER = (*ENV)->GetDirectBufferAddress(ENV, VAR); \
if (!BUFFER) \
{ \
illegalArgument(ENV, #VAR" must be a direct buffer"); \
ACTION; \
}
#define NOT_NULL(ENV, VAR, ACTION) \
if (!VAR) \
@ -98,6 +64,16 @@
ACTION; \
}
#define NOT_SET(ENV, VAR, FIELD, ACTION) \
jclass cls = (*ENV)->GetObjectClass(ENV, VAR); \
jfieldID field = (*ENV)->GetFieldID(ENV, cls, FIELD, "J"); \
jptr ptr = (jptr) (*ENV)->GetLongField(ENV, VAR, field); \
if (ptr) \
{ \
illegalState(ENV, FIELD" is already initialized"); \
ACTION; \
}
#define THREAD_BEGIN(ENV) \
JNIEnv *ENV; \
jint getEnvResult = (*jvm)->GetEnv(jvm, (void **) &ENV, JNI_VERSION_1_4); \
@ -105,9 +81,14 @@
(*jvm)->AttachCurrentThread(jvm, (void**) &ENV, NULL);
#define THREAD_END \
if (getEnvResult == JNI_EDETACHED) (*jvm)->DetachCurrentThread(jvm);
if (getEnvResult == JNI_EDETACHED) \
(*jvm)->DetachCurrentThread(jvm);
jint illegalArgument(JNIEnv *env, char *message);
jint illegalState(JNIEnv *env, char *message);
extern JavaVM *jvm;
jint illegalArgument(JNIEnv *env, const char *message);
jint illegalState(JNIEnv *env, const char *message);
jobject NewDirectReadOnlyByteBuffer(JNIEnv *env, const void *mem,
int mem_length);
#endif

View File

@ -1,15 +0,0 @@
/*
* Copyright (C) 2013 Klaus Reimer (k@ailis.de)
* See COPYING file for copying conditions
*/
#include <string.h>
// Enforce usage of older memcpy to be compatible with older libc versions
#if WRAP_MEMCPY
asm (".symver memcpy, memcpy@GLIBC_2.2.5");
void *__wrap_memcpy(void *dest, const void *src, size_t n)
{
return memcpy(dest, src, n);
}
#endif

View File

@ -121,7 +121,7 @@ abstract class AbstractDevice implements UsbDevice
for (int i = 0; i < numConfigurations; i += 1)
{
final ConfigDescriptor configDescriptor = new ConfigDescriptor();
final int result = LibUsb.getConfigDescriptor(device, i,
final int result = LibUsb.getConfigDescriptor(device, (byte) i,
configDescriptor);
if (result < 0)
{
@ -521,8 +521,8 @@ abstract class AbstractDevice implements UsbDevice
{
final DeviceHandle handle = open();
final ByteBuffer buffer = ByteBuffer.allocateDirect(256);
final int result = LibUsb.getDescriptor(handle, LibUsb.DT_STRING, 0,
buffer);
final int result = LibUsb.getDescriptor(handle, LibUsb.DT_STRING,
(byte) 0, buffer);
if (result < 0)
throw new LibUsbException(
"Unable to get string descriptor languages", result);

View File

@ -113,53 +113,19 @@ public final class ConfigDescriptor implements UsbConfigurationDescriptor
* @return The descriptor dump.
*/
public String dump()
{
return dump(null);
}
/**
* Returns a dump of this descriptor.
*
* @param handle
* The USB device handle for resolving string descriptors. If
* null then no strings are resolved.
* @return The descriptor dump.
*/
public String dump(final DeviceHandle handle)
{
final StringBuilder builder = new StringBuilder();
builder
.append(String.format("%s"
+ " extralen %17d%n"
+ " extra:%n"
+ "%s%n",
DescriptorUtils.dump(this),
extraLength(),
DescriptorUtils.dump(extra()).replaceAll("(?m)^", " ")));
for (final Interface descriptor: iface())
{
builder.append(descriptor.dump(handle)
.replaceAll("(?m)^", " "));
}
return builder.toString();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final ConfigDescriptor other = (ConfigDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bConfigurationValue(), other.bConfigurationValue())
.append(bmAttributes(), other.bmAttributes())
.append(bNumInterfaces(), other.bNumInterfaces())
.append(iConfiguration(), other.iConfiguration())
.append(bMaxPower(), other.bMaxPower())
.append(wTotalLength(), other.wTotalLength()).isEquals();
builder.append(String.format("%s%n" + " extralen %17d%n"
+ " extra:%n" + "%s", DescriptorUtils.dump(this), extraLength(),
DescriptorUtils.dump(extra()).replaceAll("(?m)^", " ")));
for (final Interface iface : iface())
{
builder.append("%n" + iface.dump());
}
return builder.toString();
}
@Override
@ -174,11 +140,45 @@ public final class ConfigDescriptor implements UsbConfigurationDescriptor
.append(iConfiguration())
.append(bmAttributes())
.append(bMaxPower())
.append(iface())
.append(extra())
.append(extraLength())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ConfigDescriptor other = (ConfigDescriptor) obj;
return new EqualsBuilder()
.append(bLength(), other.bLength())
.append(bDescriptorType(), other.bDescriptorType())
.append(wTotalLength(), other.wTotalLength())
.append(bNumInterfaces(), other.bNumInterfaces())
.append(bConfigurationValue(), other.bConfigurationValue())
.append(iConfiguration(), other.iConfiguration())
.append(bmAttributes(), other.bmAttributes())
.append(bMaxPower(), other.bMaxPower())
.append(iface(), other.iface())
.append(extra(), other.extra())
.append(extraLength(), other.extraLength())
.isEquals();
}
@Override
public String toString()
{

View File

@ -18,8 +18,6 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Structure representing a libusb session. The concept of individual libusb
* sessions allows for your program to use two libraries (or dynamically load
@ -41,14 +39,14 @@ public final class Context
{
/** The native pointer to the context structure. */
private long contextPointer;
/**
* Constructs a new libusb context. Must be passed to
* {@link LibUsb#init(Context)} before passing it to any other method.
*/
public Context()
{
// Empty
// Empty
}
/**
@ -60,19 +58,43 @@ public final class Context
{
return this.contextPointer;
}
@Override
public int hashCode()
{
return new HashCodeBuilder().append(this.contextPointer).toHashCode();
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (contextPointer ^ (contextPointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Context other = (Context) obj;
return this.contextPointer == other.contextPointer;
}
if (contextPointer != other.contextPointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb context 0x%x", contextPointer);
}
}

View File

@ -0,0 +1,131 @@
package de.ailis.usb4java.libusb;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import de.ailis.usb4java.utils.BufferUtils;
public final class ControlSetup
{
private final ByteBuffer controlSetup;
ControlSetup(final ByteBuffer buffer)
{
if (buffer == null)
{
throw new IllegalArgumentException("buffer cannot be null");
}
controlSetup = BufferUtils.slice(buffer, 0, LibUsb.CONTROL_SETUP_SIZE);
// Control Setup (as all of USB) is Little Endian.
controlSetup.order(ByteOrder.LITTLE_ENDIAN);
}
/**
* USB Control Setup is always 8 bytes long.
* Structured as follows:
* byte 0: bmRequestType
* byte 1: bRequest
* bytes 2-3: wValue (Little Endian)
* bytes 4-5: wIndex (Little Endian)
* bytes 6-7: wLength (Little Endian)
*/
public byte bmRequestType()
{
return controlSetup.get(0);
}
public void setBmRequestType(final byte bmRequestType)
{
controlSetup.put(0, bmRequestType);
}
public byte bRequest()
{
return controlSetup.get(1);
}
public void setBRequest(final byte bRequest)
{
controlSetup.put(1, bRequest);
}
public short wValue()
{
return controlSetup.getShort(2);
}
public void setWValue(final short wValue)
{
controlSetup.putShort(2, wValue);
}
public short wIndex()
{
return controlSetup.getShort(4);
}
public void setWIndex(final short wIndex)
{
controlSetup.putShort(4, wIndex);
}
public short wLength()
{
return controlSetup.getShort(6);
}
public void setWLength(final short wLength)
{
controlSetup.putShort(6, wLength);
}
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result)
+ ((controlSetup == null) ? 0 : controlSetup.hashCode());
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final ControlSetup other = (ControlSetup) obj;
if (controlSetup == null)
{
if (other.controlSetup != null)
{
return false;
}
}
else if (!controlSetup.equals(other.controlSetup))
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb control setup with buffer %s",
controlSetup.toString());
}
}

View File

@ -18,8 +18,6 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Structure representing a USB device detected on the system.
*
@ -54,7 +52,7 @@ public final class Device
{
// Empty
}
/**
* Returns the native pointer to the device structure.
*
@ -64,25 +62,43 @@ public final class Device
{
return this.devicePointer;
}
@Override
public int hashCode()
{
return new HashCodeBuilder().append(this.devicePointer).toHashCode();
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (devicePointer ^ (devicePointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Device other = (Device) obj;
return this.devicePointer == other.devicePointer;
if (devicePointer != other.devicePointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb device 0x%x", this.devicePointer);
return String.format("libusb device 0x%x", devicePointer);
}
}

View File

@ -18,8 +18,6 @@
package de.ailis.usb4java.libusb;
import java.nio.ByteBuffer;
import javax.usb.UsbDeviceDescriptor;
import org.apache.commons.lang3.builder.EqualsBuilder;
@ -37,8 +35,8 @@ import de.ailis.usb4java.utils.DescriptorUtils;
*/
public final class DeviceDescriptor implements UsbDeviceDescriptor
{
/** The native data of the descriptor structure. */
private ByteBuffer deviceDescriptorData;
/** The native pointer to the descriptor structure. */
private long deviceDescriptorPointer;
/**
* Constructs a new device descriptor which can be passed to the
@ -50,13 +48,13 @@ public final class DeviceDescriptor implements UsbDeviceDescriptor
}
/**
* Returns the native data of the descriptor structure.
* Returns the native pointer.
*
* @return The native data.
* @return The native pointer.
*/
public ByteBuffer getData()
public long getPointer()
{
return this.deviceDescriptorData;
return this.deviceDescriptorPointer;
}
@Override
@ -121,41 +119,15 @@ public final class DeviceDescriptor implements UsbDeviceDescriptor
*/
public String dump(final DeviceHandle handle)
{
final String sManufacturer = LibUsb.getStringDescriptor(handle,
final String sManufacturer = LibUsb.getStringDescriptor(handle,
iManufacturer());
final String sProduct = LibUsb.getStringDescriptor(handle, iProduct());
final String sSerialNumber = LibUsb.getStringDescriptor(handle,
final String sSerialNumber = LibUsb.getStringDescriptor(handle,
iSerialNumber());
return DescriptorUtils.dump(this, sManufacturer, sProduct,
return DescriptorUtils.dump(this, sManufacturer, sProduct,
sSerialNumber);
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final DeviceDescriptor other = (DeviceDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(idProduct(), other.idProduct())
.append(idVendor(), other.idVendor())
.append(bcdDevice(), other.bcdDevice())
.append(bcdUSB(), other.bcdUSB())
.append(bDescriptorType(), other.bDescriptorType())
.append(bDeviceClass(), other.bDeviceClass())
.append(bDeviceProtocol(), other.bDeviceProtocol())
.append(bDeviceSubClass(), other.bDeviceSubClass())
.append(bLength(), other.bLength())
.append(bMaxPacketSize0(), other.bMaxPacketSize0())
.append(bNumConfigurations(), other.bNumConfigurations())
.append(iManufacturer(), other.iManufacturer())
.append(iProduct(), other.iProduct())
.append(iSerialNumber(), other.iSerialNumber()).isEquals();
}
@Override
public int hashCode()
{
@ -173,7 +145,44 @@ public final class DeviceDescriptor implements UsbDeviceDescriptor
.append(iManufacturer())
.append(iProduct())
.append(iSerialNumber())
.append(bNumConfigurations()).toHashCode();
.append(bNumConfigurations())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final DeviceDescriptor other = (DeviceDescriptor) obj;
return new EqualsBuilder()
.append(bLength(), other.bLength())
.append(bDescriptorType(), other.bDescriptorType())
.append(bcdUSB(), other.bcdUSB())
.append(bDeviceClass(), other.bDeviceClass())
.append(bDeviceSubClass(), other.bDeviceSubClass())
.append(bDeviceProtocol(), other.bDeviceProtocol())
.append(bMaxPacketSize0(), other.bMaxPacketSize0())
.append(idVendor(), other.idVendor())
.append(idProduct(), other.idProduct())
.append(bcdDevice(), other.bcdDevice())
.append(iManufacturer(), other.iManufacturer())
.append(iProduct(), other.iProduct())
.append(iSerialNumber(), other.iSerialNumber())
.append(bNumConfigurations(), other.bNumConfigurations())
.isEquals();
}
@Override

View File

@ -18,8 +18,6 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* Structure representing a handle on a USB device.
*
@ -59,22 +57,39 @@ public final class DeviceHandle
@Override
public int hashCode()
{
return new HashCodeBuilder().append(this.deviceHandlePointer)
.toHashCode();
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (deviceHandlePointer ^ (deviceHandlePointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final DeviceHandle other = (DeviceHandle) obj;
return this.deviceHandlePointer == other.deviceHandlePointer;
if (deviceHandlePointer != other.deviceHandlePointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb handle 0x%x", this.deviceHandlePointer);
return String.format("libusb device handle 0x%x", deviceHandlePointer);
}
}

View File

@ -20,8 +20,6 @@ package de.ailis.usb4java.libusb;
import java.util.Iterator;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
* List of devices as returned by
* {@link LibUsb#getDeviceList(Context, DeviceList)}.
@ -43,7 +41,7 @@ public final class DeviceList implements Iterable<Device>
public DeviceList()
{
// Empty
}
}
/**
* Returns the native pointer.
@ -54,7 +52,7 @@ public final class DeviceList implements Iterable<Device>
{
return this.deviceListPointer;
}
/**
* Returns the number of devices in the list.
*
@ -83,16 +81,45 @@ public final class DeviceList implements Iterable<Device>
@Override
public int hashCode()
{
return new HashCodeBuilder().append(this.deviceListPointer)
.toHashCode();
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (deviceListPointer ^ (deviceListPointer >>> 32));
result = (prime * result) + size;
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final DeviceList other = (DeviceList) obj;
return this.deviceListPointer == other.deviceListPointer;
if (deviceListPointer != other.deviceListPointer)
{
return false;
}
if (size != other.size)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb device list 0x%x with size %d",
deviceListPointer, size);
}
}

View File

@ -34,13 +34,13 @@ final class DeviceListIterator implements Iterator<Device>
@Override
public boolean hasNext()
{
return this.nextIndex < this.devices.getSize();
return nextIndex < devices.getSize();
}
@Override
public Device next()
{
return this.devices.get(this.nextIndex++);
return devices.get(nextIndex++);
}
@Override

View File

@ -41,14 +41,14 @@ public final class EndpointDescriptor implements UsbEndpointDescriptor
private long endpointDescriptorPointer;
/**
* Package-private constructor to prevent manual instantiation. Endpoint
* Package-private constructor to prevent manual instantiation. Endpoint
* descriptors are always created by JNI.
*/
EndpointDescriptor()
{
// Empty
}
/**
* Returns the native pointer.
*
@ -116,35 +116,15 @@ public final class EndpointDescriptor implements UsbEndpointDescriptor
*/
public String dump()
{
return String.format("%s"
return String.format("%s%n"
+ " extralen %17d%n"
+ " extra:%n"
+ "%s%n",
+ "%s",
DescriptorUtils.dump(this),
extraLength(),
DescriptorUtils.dump(extra()).replaceAll("(?m)^", " "));
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final EndpointDescriptor other = (EndpointDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bEndpointAddress(), other.bEndpointAddress())
.append(bmAttributes(), other.bmAttributes())
.append(bInterval(), other.bInterval())
.append(bSynchAddress(), other.bSynchAddress())
.append(wMaxPacketSize(), other.wMaxPacketSize())
.append(extraLength(), other.extraLength())
.append(extra(), other.extra())
.isEquals();
}
@Override
public int hashCode()
{
@ -162,6 +142,38 @@ public final class EndpointDescriptor implements UsbEndpointDescriptor
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final EndpointDescriptor other = (EndpointDescriptor) obj;
return new EqualsBuilder()
.append(bLength(), other.bLength())
.append(bDescriptorType(), other.bDescriptorType())
.append(bEndpointAddress(), other.bEndpointAddress())
.append(bmAttributes(), other.bmAttributes())
.append(wMaxPacketSize(), other.wMaxPacketSize())
.append(bInterval(), other.bInterval())
.append(bRefresh(), other.bRefresh())
.append(bSynchAddress(), other.bSynchAddress())
.append(extra(), other.extra())
.append(extraLength(), other.extraLength())
.isEquals();
}
@Override
public String toString()
{

View File

@ -18,6 +18,7 @@
package de.ailis.usb4java.libusb;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
/**
@ -31,14 +32,14 @@ public final class Interface
private long interfacePointer;
/**
* Package-private constructor to prevent manual instantiation. Interfaces
* Package-private constructor to prevent manual instantiation. Interfaces
* are always created by JNI.
*/
Interface()
{
// Empty
}
/**
* Returns the native pointer.
*
@ -48,7 +49,7 @@ public final class Interface
{
return this.interfacePointer;
}
/**
* Returns the array with interface descriptors. The length of this array is
* determined by the {@link #numAltsetting()} field.
@ -70,43 +71,53 @@ public final class Interface
* @return The interface dump.
*/
public String dump()
{
return dump(null);
}
/**
* Returns a dump of this descriptor.
*
* @param handle
* The USB device handle for resolving string descriptors. If
* null then no strings are resolved.
* @return The descriptor dump.
*/
public String dump(final DeviceHandle handle)
{
final StringBuilder builder = new StringBuilder();
for (final InterfaceDescriptor descriptor : altsetting())
builder.append(String.format("Interface:%n" + " numAltsetting %10d",
numAltsetting()));
for (final InterfaceDescriptor intDesc : altsetting())
{
builder.append(descriptor.dump(handle));
builder.append("%n" + intDesc.dump());
}
return builder.toString();
}
@Override
public int hashCode()
{
return new HashCodeBuilder().append(this.interfacePointer).toHashCode();
return new HashCodeBuilder()
.append(altsetting())
.append(numAltsetting())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Interface other = (Interface) obj;
return this.interfacePointer == other.interfacePointer;
return new EqualsBuilder()
.append(altsetting(), other.altsetting())
.append(numAltsetting(), other.numAltsetting())
.isEquals();
}
@Override
public String toString()
{

View File

@ -41,14 +41,14 @@ public final class InterfaceDescriptor implements UsbInterfaceDescriptor
private long interfaceDescriptorPointer;
/**
* Package-private constructor to prevent manual instantiation. Interface
* Package-private constructor to prevent manual instantiation. Interface
* descriptors are always created by JNI.
*/
InterfaceDescriptor()
{
// Empty
}
/**
* Returns the native pointer.
*
@ -116,59 +116,19 @@ public final class InterfaceDescriptor implements UsbInterfaceDescriptor
* @return The descriptor dump.
*/
public String dump()
{
return dump(null);
}
/**
* Returns a dump of this descriptor.
*
* @param handle
* The USB device handle for resolving string descriptors. If
* null then no strings are resolved.
* @return The descriptor dump.
*/
public String dump(final DeviceHandle handle)
{
final StringBuilder builder = new StringBuilder();
final int iInterface = iInterface();
String sInterface = LibUsb.getStringDescriptor(handle, iInterface);
if (sInterface == null) sInterface = "";
builder.append(String.format("%s"
+ " extralen %17d%n"
+ " extra:%n"
+ "%s%n",
DescriptorUtils.dump(this),
extraLength(),
DescriptorUtils.dump(extra()).replaceAll("(?m)^", " ")));
if (extraLength() != 0) return builder.toString();
for (final EndpointDescriptor edesc: endpoint())
{
builder.append(edesc.dump().replaceAll("(?m)^", " "));
}
return builder.toString();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
final InterfaceDescriptor other = (InterfaceDescriptor) obj;
return new EqualsBuilder()
.append(bDescriptorType(), other.bDescriptorType())
.append(bLength(), other.bLength())
.append(bAlternateSetting(), other.bAlternateSetting())
.append(bInterfaceClass(), other.bInterfaceClass())
.append(bInterfaceNumber(), other.bInterfaceNumber())
.append(bInterfaceProtocol(), other.bInterfaceProtocol())
.append(bInterfaceSubClass(), other.bInterfaceSubClass())
.append(bNumEndpoints(), other.bNumEndpoints())
.append(iInterface(), other.iInterface())
.append(extraLength(), other.extraLength())
.append(extra(), other.extra())
.isEquals();
builder.append(String.format("%s%n" + " extralen %17d%n"
+ " extra:%n" + "%s", DescriptorUtils.dump(this), extraLength(),
DescriptorUtils.dump(extra()).replaceAll("(?m)^", " ")));
for (final EndpointDescriptor epDesc : endpoint())
{
builder.append("%n" + epDesc.dump());
}
return builder.toString();
}
@Override
@ -184,11 +144,46 @@ public final class InterfaceDescriptor implements UsbInterfaceDescriptor
.append(bInterfaceSubClass())
.append(bInterfaceProtocol())
.append(iInterface())
.append(endpoint())
.append(extra())
.append(extraLength())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final InterfaceDescriptor other = (InterfaceDescriptor) obj;
return new EqualsBuilder()
.append(bLength(), other.bLength())
.append(bDescriptorType(), other.bDescriptorType())
.append(bInterfaceNumber(), other.bInterfaceNumber())
.append(bAlternateSetting(), other.bAlternateSetting())
.append(bNumEndpoints(), other.bNumEndpoints())
.append(bInterfaceClass(), other.bInterfaceClass())
.append(bInterfaceSubClass(), other.bInterfaceSubClass())
.append(bInterfaceProtocol(), other.bInterfaceProtocol())
.append(iInterface(), other.iInterface())
.append(endpoint(), other.endpoint())
.append(extra(), other.extra())
.append(extraLength(), other.extraLength())
.isEquals();
}
@Override
public String toString()
{

View File

@ -0,0 +1,77 @@
package de.ailis.usb4java.libusb;
public final class IsoPacketDescriptor
{
/** The native pointer to the descriptor structure. */
private long isoPacketDescriptorPointer;
/**
* Package-private constructor to prevent manual instantiation.
* IsoPacketDescriptors are always created by JNI.
*/
IsoPacketDescriptor()
{
// Empty
}
/**
* Returns the native pointer.
*
* @return The native pointer.
*/
public long getPointer()
{
return isoPacketDescriptorPointer;
}
public native int length();
// Theoretically the right representation for a C unsigned int would be a
// Java long, but the maximum length for ISO Packets is 1024 bytes, so an
// int more than suffices to hold any possible valid values here.
public native void setLength(final int length);
public native int actualLength();
public native int status();
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (isoPacketDescriptorPointer ^ (isoPacketDescriptorPointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final IsoPacketDescriptor other = (IsoPacketDescriptor) obj;
if (isoPacketDescriptorPointer != other.isoPacketDescriptorPointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb iso packet descriptor 0x%x",
isoPacketDescriptorPointer);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -22,7 +22,7 @@ import javax.usb.UsbException;
/**
* libusb-specific USB exception.
*
*
* @author Klaus Reimer (k@ailis.de)
*/
public final class LibUsbException extends UsbException
@ -35,7 +35,7 @@ public final class LibUsbException extends UsbException
/**
* Constructor.
*
*
* @param message
* The error message.
* @param errorCode
@ -50,11 +50,11 @@ public final class LibUsbException extends UsbException
/**
* Returns the error code.
*
*
* @return The error code
*/
public int getErrorCode()
{
return this.errorCode;
return errorCode;
}
}

View File

@ -32,12 +32,18 @@ import java.nio.ByteBuffer;
public final class Transfer
{
/** The native pointer to the transfer structure. */
private long pointer;
private long transferPointer;
// Keeping a reference to the buffer has multiple benefits: faster get(), GC
// prevention (while Transfer is alive) and you can check the buffer's
// original capacity (needed to check setLength() properly).
private ByteBuffer transferBuffer;
/**
* Constructs a new transfer structure.
* Package-private constructor to prevent manual instantiation.
* Transfers are always created by JNI with allocTransfer().
*/
public Transfer()
Transfer()
{
// Empty
}
@ -49,7 +55,7 @@ public final class Transfer
*/
public long getPointer()
{
return this.pointer;
return transferPointer;
}
/**
@ -57,7 +63,7 @@ public final class Transfer
*
* @return The handle of the device.
*/
public native DeviceHandle getDevHandle();
public native DeviceHandle devHandle();
/**
* Sets the handle of the device that this transfer will be submitted to.
@ -72,7 +78,7 @@ public final class Transfer
*
* @return The transfer flags.
*/
public native int getFlags();
public native byte flags();
/**
* Sets the bitwise OR combination of libusb transfer flags.
@ -80,14 +86,14 @@ public final class Transfer
* @param flags
* The transfer flags to set.
*/
public native void setFlags(final int flags);
public native void setFlags(final byte flags);
/**
* Returns the address of the endpoint where this transfer will be sent.
*
* @return The endpoint address.
*/
public native int getEndpoint();
public native byte endpoint();
/**
* Sets the address of the endpoint where this transfer will be sent.
@ -95,14 +101,14 @@ public final class Transfer
* @param endpoint
* The endpoint address to set
*/
public native void setEndpoint(final int endpoint);
public native void setEndpoint(final byte endpoint);
/**
* Returns the type of the endpoint.
*
* @return The endpoint type.
*/
public native int getType();
public native byte type();
/**
* Sets the type of the endpoint.
@ -110,7 +116,7 @@ public final class Transfer
* @param type
* The endpoint type to set.
*/
public native void setType(final int type);
public native void setType(final byte type);
/**
* Returns the timeout for this transfer in milliseconds. A value of 0
@ -118,7 +124,7 @@ public final class Transfer
*
* @return The timeout.
*/
public native long getTimeout();
public native long timeout();
/**
* Sets the timeout for this transfer in milliseconds. A value of 0
@ -127,7 +133,7 @@ public final class Transfer
* @param timeout
* The timeout to set.
*/
public native void setTimeout(final int timeout);
public native void setTimeout(final long timeout);
/**
* Returns the status of the transfer. Read-only, and only for use within
@ -140,22 +146,47 @@ public final class Transfer
*
* @return The transfer status.
*/
public native int getStatus();
public native int status();
/**
* Returns the length of the data buffer.
*
* @return The data buffer length.
*/
public native int getLength();
public native int length();
/**
* Sets the length of the data buffer.
*
* This is checked against the maximum capacity of the supplied ByteBuffer.
*
* @param length
* The data buffer length to set.
*/
public native void setLength(final int length);
public void setLength(final int length)
{
// Verify that the new length doesn't exceed the current buffer's
// maximum capacity.
if (length != 0)
{
if (transferBuffer == null)
{
throw new IllegalArgumentException(
"buffer is null, only a length of 0 is allowed");
}
if (transferBuffer.capacity() < length)
{
throw new IllegalArgumentException(
"buffer too small for requested length");
}
}
// Native call.
setLengthNative(length);
}
native void setLengthNative(final int length);
/**
* Returns the actual length of data that was transferred. Read-only, and
@ -164,14 +195,50 @@ public final class Transfer
*
* @return The actual length of the transferred data.
*/
public native int getActualLength();
public native int actualLength();
/**
* Returns the current callback object.
*
* @return The current callback object.
*/
public native TransferCallback callback();
/**
* Sets the callback object.
*
* This will be invoked when the transfer completes, fails, or is cancelled.
*
* @param callback
* The callback object to use.
*/
public native void setCallback(final TransferCallback callback);
/**
* Returns the current user data object.
*
* @return The current user data object.
*/
public native Object userData();
/**
* Sets the user data object, representing user context data to pass to
* the callback function and that can be accessed from there.
*
* @param userData
* The user data object to set.
*/
public native void setUserData(final Object userData);
/**
* Returns the data buffer.
*
* @return The data buffer.
*/
public native ByteBuffer getBuffer();
public ByteBuffer buffer()
{
return transferBuffer;
}
/**
* Sets the data buffer.
@ -179,7 +246,27 @@ public final class Transfer
* @param buffer
* The data buffer to set.
*/
public native void setBuffer(final ByteBuffer buffer);
public void setBuffer(final ByteBuffer buffer)
{
// Native call.
setBufferNative(buffer);
if (buffer != null)
{
// Set new length based on buffer's capacity.
setLengthNative(buffer.capacity());
}
else
{
setLengthNative(0);
}
// Once we know the native calls have gone through, update the
// reference.
transferBuffer = buffer;
}
native void setBufferNative(final ByteBuffer buffer);
/**
* Returns the number of isochronous packets. Only used for I/O with
@ -187,7 +274,7 @@ public final class Transfer
*
* @return The number of isochronous packets.
*/
public native int getNumIsoPackets();
public native int numIsoPackets();
/**
* Sets the number of isochronous packets.
@ -196,4 +283,50 @@ public final class Transfer
* The number of isochronous packets to set.
*/
public native void setNumIsoPackets(final int numIsoPackets);
/**
* Array of isochronous packet descriptors, for isochronous transfers only.
*
* @return The array of isochronous packet descriptors.
*/
public native IsoPacketDescriptor[] isoPacketDesc();
@Override
public int hashCode()
{
final int prime = 31;
int result = 1;
result = (prime * result)
+ (int) (transferPointer ^ (transferPointer >>> 32));
return result;
}
@Override
public boolean equals(final Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Transfer other = (Transfer) obj;
if (transferPointer != other.transferPointer)
{
return false;
}
return true;
}
@Override
public String toString()
{
return String.format("libusb transfer 0x%x", transferPointer);
}
}

View File

@ -0,0 +1,6 @@
package de.ailis.usb4java.libusb;
public interface TransferCallback
{
void processTransfer(Transfer transfer);
}

View File

@ -30,8 +30,8 @@ import org.apache.commons.lang3.builder.HashCodeBuilder;
public final class Version implements Comparable<Version>
{
/** The native pointer to the version structure. */
private long pointer;
private long versionPointer;
/**
* Package-private constructor to prevent manual instantiation. An instance
* is only returned by the JNI method {@link LibUsb#getVersion()}.
@ -48,7 +48,7 @@ public final class Version implements Comparable<Version>
*/
public long getPointer()
{
return this.pointer;
return versionPointer;
}
/**
@ -72,6 +72,13 @@ public final class Version implements Comparable<Version>
*/
public native int micro();
/**
* Returns the library nano version.
*
* @return The library nano version.
*/
public native int nano();
/**
* Returns the release candidate suffix string, e.g. "-rc4".
*
@ -79,29 +86,41 @@ public final class Version implements Comparable<Version>
*/
public native String rc();
@Override
public String toString()
{
return major() + "." + minor() + "." + micro() + rc();
}
@Override
public int hashCode()
{
return new HashCodeBuilder().append(this.pointer).toHashCode();
return new HashCodeBuilder()
.append(major())
.append(minor())
.append(micro())
.append(nano())
.append(rc())
.toHashCode();
}
@Override
public boolean equals(final Object obj)
{
if (obj == null) return false;
if (obj == this) return true;
if (obj.getClass() != getClass()) return false;
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final Version other = (Version) obj;
return new EqualsBuilder()
.append(major(), other.major())
.append(minor(), other.minor())
.append(micro(), other.micro())
.append(nano(), other.nano())
.append(rc(), other.rc())
.isEquals();
}
@ -109,11 +128,27 @@ public final class Version implements Comparable<Version>
@Override
public int compareTo(final Version other)
{
if (this == other)
{
return 0;
}
if (other == null)
{
return 1;
}
return new CompareToBuilder()
.append(major(), other.major())
.append(minor(), other.minor())
.append(micro(), other.micro())
.append(nano(), other.nano())
.append(rc(), other.rc())
.toComparison();
}
@Override
public String toString()
{
return major() + "." + minor() + "." + micro() + "." + nano() + rc();
}
}

View File

@ -0,0 +1,43 @@
package de.ailis.usb4java.utils;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
public final class BufferUtils
{
private static final int intSize = Integer.SIZE / Byte.SIZE;
private static final int longSize = Long.SIZE / Byte.SIZE;
public static ByteBuffer allocateByteBuffer(final int bytes)
{
return ByteBuffer.allocateDirect(bytes);
}
public static IntBuffer allocateIntBuffer()
{
return ByteBuffer.allocateDirect(intSize).asIntBuffer();
}
public static LongBuffer allocateLongBuffer()
{
return ByteBuffer.allocateDirect(longSize).asLongBuffer();
}
public static ByteBuffer slice(final ByteBuffer buffer, final int offset,
final int length)
{
final int oldPosition = buffer.position();
final int oldLimit = buffer.limit();
buffer.position(offset);
buffer.limit(offset + length);
final ByteBuffer slice = buffer.slice();
buffer.position(oldPosition);
buffer.limit(oldLimit);
return slice;
}
}

View File

@ -24,15 +24,16 @@ import de.ailis.usb4java.libusb.LibUsb;
public final class DescriptorUtils
{
/** Mapping from USB class id to USB class name. */
private static final Map<Integer, String> CLASS_NAMES =
new HashMap<Integer, String>();
private static final Map<Byte, String> CLASS_NAMES =
new HashMap<Byte, String>();
static
{
CLASS_NAMES.put(LibUsb.CLASS_PER_INTERFACE, "Per interface");
CLASS_NAMES.put(LibUsb.CLASS_PER_INTERFACE, "Per Interface");
CLASS_NAMES.put(LibUsb.CLASS_AUDIO, "Audio");
CLASS_NAMES.put(LibUsb.CLASS_COMM, "Communications");
CLASS_NAMES.put(LibUsb.CLASS_HID, "HID");
CLASS_NAMES.put(LibUsb.CLASS_PHYSICAL, "Physical");
CLASS_NAMES.put(LibUsb.CLASS_IMAGE, "Imaging");
CLASS_NAMES.put(LibUsb.CLASS_PRINTER, "Printer");
CLASS_NAMES.put(LibUsb.CLASS_MASS_STORAGE, "Mass Storage");
@ -41,13 +42,12 @@ public final class DescriptorUtils
CLASS_NAMES.put(LibUsb.CLASS_SMART_CARD, "Smart Card");
CLASS_NAMES.put(LibUsb.CLASS_CONTENT_SECURITY, "Content Security");
CLASS_NAMES.put(LibUsb.CLASS_VIDEO, "Video");
CLASS_NAMES.put(LibUsb.CLASS_VENDOR_SPEC, "Vendor-specific");
CLASS_NAMES.put(LibUsb.CLASS_APPLICATION, "Application");
CLASS_NAMES.put(LibUsb.CLASS_PERSONAL_HEALTHCARE,
"Personal Healthcare");
CLASS_NAMES.put(LibUsb.CLASS_DIAGNOSTIC_DEVICE, "Diagnostic Device");
CLASS_NAMES.put(LibUsb.CLASS_WIRELESS, "Wireless");
CLASS_NAMES.put(LibUsb.CLASS_APPLICATION, "Application");
CLASS_NAMES.put(LibUsb.CLASS_VENDOR_SPEC, "Vendor-specific");
}
/**
@ -66,10 +66,15 @@ public final class DescriptorUtils
* The numeric USB class.
* @return The USB class name.
*/
public static String getUSBClassName(final int usbClass)
public static String getUSBClassName(final byte usbClass)
{
final String name = CLASS_NAMES.get(usbClass);
if (name == null) return "Unknown";
if (name == null)
{
return "Unknown";
}
return name;
}
@ -80,9 +85,9 @@ public final class DescriptorUtils
* The binary-coded decimal to decode.
* @return The decoded binary-coded decimal.
*/
public static String decodeBCD(final int bcd)
public static String decodeBCD(final short bcd)
{
return String.format("%x.%02x", (bcd & 0xff00) >> 8, bcd & 0xff);
return String.format("%x.%02x", (bcd & 0xFF00) >> 8, bcd & 0x00FF);
}
/**
@ -94,25 +99,31 @@ public final class DescriptorUtils
*/
public static String dump(final ByteBuffer bytes)
{
final int columns = 16;
bytes.rewind();
final int columns = 16;
final StringBuilder builder = new StringBuilder();
int i = 0;
while (bytes.hasRemaining())
{
if (i % columns != 0)
if ((i % columns) != 0)
{
builder.append(' ');
}
else if (i >= columns)
{
builder.append(String.format("%n"));
}
builder.append(String.format("%02x", bytes.get()));
i++;
}
return builder.toString();
}
/**
* Dumps the specified USB device descriptor into a string and
* returns it.
* Dumps the specified USB device descriptor into a string and returns it.
*
* @param descriptor
* The USB device descriptor to dump.
@ -124,8 +135,7 @@ public final class DescriptorUtils
}
/**
* Dumps the specified USB device descriptor into a string and
* returns it.
* Dumps the specified USB device descriptor into a string and returns it.
*
* @param descriptor
* The USB device descriptor to dump.
@ -159,19 +169,19 @@ public final class DescriptorUtils
descriptor.bDescriptorType(),
decodeBCD(descriptor.bcdUSB()),
descriptor.bDeviceClass() & 0xff,
getUSBClassName(descriptor.bDeviceClass() & 0xff),
getUSBClassName(descriptor.bDeviceClass()),
descriptor.bDeviceSubClass() & 0xff,
descriptor.bDeviceProtocol() & 0xff,
descriptor.bMaxPacketSize0() & 0xff,
String.format("0x%04x", descriptor.idVendor() & 0xffff),
String.format("0x%04x", descriptor.idProduct() & 0xffff),
decodeBCD(descriptor.bcdDevice()),
descriptor.iManufacturer() & 0xff,
manufacturer == null ? "" : " " + manufacturer,
descriptor.iManufacturer() & 0xff,
(manufacturer == null) ? ("") : (" " + manufacturer),
descriptor.iProduct() & 0xff,
product == null ? "" : " " + product,
descriptor.iSerialNumber() & 0xff,
serial == null ? "" : " " + serial,
(product == null) ? ("") : (" " + product),
descriptor.iSerialNumber() & 0xff,
(serial == null) ? ("") : (" " + serial),
descriptor.bNumConfigurations() & 0xff);
}
@ -203,16 +213,16 @@ public final class DescriptorUtils
descriptor.bConfigurationValue() & 0xff,
descriptor.iConfiguration() & 0xff,
String.format("0x%02x", descriptor.bmAttributes() & 0xff),
(descriptor.bmAttributes() & 64) == 0 ? "(Bus Powered)" :
"Self Powered",
(descriptor.bmAttributes() & 32) == 0 ? "" :
String.format(" Remote Wakeup%n"),
((descriptor.bmAttributes() & 64) == 0) ? ("(Bus Powered)")
: ("Self Powered"),
((descriptor.bmAttributes() & 32) == 0) ? ("")
: String.format(" Remote Wakeup%n"),
(descriptor.bMaxPower() & 0xff) * 2);
}
/**
* Dumps the specified USB interface descriptor into a string and
* returns it.
* Dumps the specified USB interface descriptor into a string and returns
* it.
*
* @param descriptor
* The USB interface descriptor to dump.
@ -236,15 +246,14 @@ public final class DescriptorUtils
descriptor.bAlternateSetting() & 0xff,
descriptor.bNumEndpoints() & 0xff,
descriptor.bInterfaceClass() & 0xff,
getUSBClassName(descriptor.bInterfaceClass() & 0xff),
getUSBClassName(descriptor.bInterfaceClass()),
descriptor.bInterfaceSubClass() & 0xff,
descriptor.bInterfaceProtocol() & 0xff,
descriptor.iInterface() & 0xff);
}
/**
* Dumps the specified USB endpoint descriptor into a string and
* returns it.
* Dumps the specified USB endpoint descriptor into a string and returns it.
*
* @param descriptor
* The USB endpoint descriptor to dump.
@ -265,12 +274,12 @@ public final class DescriptorUtils
descriptor.bLength(),
descriptor.bDescriptorType(),
String.format("0x%02x", descriptor.bEndpointAddress() & 0xff),
descriptor.bEndpointAddress() & 0xf,
(descriptor.bEndpointAddress() & 0x80) == 0 ? "OUT" : "IN",
descriptor.bEndpointAddress() & 0xf,
((descriptor.bEndpointAddress() & 0x80) == 0) ? ("OUT") : ("IN"),
descriptor.bmAttributes() & 0xff,
getTransferTypeName(descriptor.bmAttributes() & 0xff),
getSynchTypeName(descriptor.bmAttributes() & 0xff),
getUsageTypeName(descriptor.bmAttributes() & 0xff),
getTransferTypeName(descriptor.bmAttributes()),
getSynchTypeName(descriptor.bmAttributes()),
getUsageTypeName(descriptor.bmAttributes()),
descriptor.wMaxPacketSize() & 0xffff,
descriptor.bInterval() & 0xff);
}
@ -283,18 +292,20 @@ public final class DescriptorUtils
* The endpoint attributes value.
* @return The transfer type name.
*/
public static String getTransferTypeName(final int bmAttributes)
public static String getTransferTypeName(final byte bmAttributes)
{
switch (bmAttributes & 3)
switch (bmAttributes & LibUsb.TRANSFER_TYPE_MASK)
{
case 1:
case LibUsb.TRANSFER_TYPE_CONTROL:
return "Control";
case LibUsb.TRANSFER_TYPE_ISOCHRONOUS:
return "Isochronous";
case 2:
case LibUsb.TRANSFER_TYPE_BULK:
return "Bulk";
case 3:
case LibUsb.TRANSFER_TYPE_INTERRUPT:
return "Interrupt";
default:
return "Control";
return "Unknown";
}
}
@ -306,18 +317,20 @@ public final class DescriptorUtils
* The endpoint attributes value.
* @return The synch type name.
*/
public static String getSynchTypeName(final int bmAttributes)
public static String getSynchTypeName(final byte bmAttributes)
{
switch ((bmAttributes >> 2) & 3)
switch ((bmAttributes & LibUsb.ISO_SYNC_TYPE_MASK) >> 2)
{
case 1:
case LibUsb.ISO_SYNC_TYPE_NONE:
return "None";
case LibUsb.ISO_SYNC_TYPE_ASYNC:
return "Asynchronous";
case 2:
case LibUsb.ISO_SYNC_TYPE_ADAPTIVE:
return "Adaptive";
case 3:
case LibUsb.ISO_SYNC_TYPE_SYNC:
return "Synchronous";
default:
return "None";
return "Unknown";
}
}
@ -328,18 +341,20 @@ public final class DescriptorUtils
* The endpoint attributes value.
* @return The usage type name.
*/
public static String getUsageTypeName(final int bmAttributes)
public static String getUsageTypeName(final byte bmAttributes)
{
switch ((bmAttributes >> 4) & 3)
switch ((bmAttributes & LibUsb.ISO_USAGE_TYPE_MASK) >> 4)
{
case 1:
case LibUsb.ISO_USAGE_TYPE_DATA:
return "Data";
case LibUsb.ISO_USAGE_TYPE_FEEDBACK:
return "Feedback";
case 2:
return "Explicit Feedback Data";
case 3:
case LibUsb.ISO_USAGE_TYPE_IMPLICIT:
return "Implicit Feedback Data";
case 3: // b11 is considered "Reserved" according to USB 3.0 spec.
return "Reserved";
default:
return "Data";
return "Unknown";
}
}

View File

@ -34,16 +34,16 @@ public class LibUSBDeviceTest
private Device device;
/** The device endpoint to test with. */
private int endpoint;
private byte endpoint;
/** The value of the active configuration. */
private int configValue;
private byte configValue;
/** The vendor ID of the device we test. */
private int vendorId;
private short vendorId;
/** The manufacturer ID of the device we test. */
private int productId;
private short productId;
/**
* Set up the test.
@ -51,19 +51,21 @@ public class LibUSBDeviceTest
@Before
public void setUp()
{
this.context = new Context();
LibUsb.init(this.context);
context = new Context();
LibUsb.init(context);
try
{
this.device = findTestDevice();
if (this.device == null)
device = findTestDevice();
if (device == null)
{
throw new IllegalStateException("Need at least one USB device "
+
"with at least one endpoint to execute this test");
}
}
catch (Throwable e)
{
this.device = null;
device = null;
}
}
@ -75,22 +77,29 @@ public class LibUSBDeviceTest
private Device findTestDevice()
{
DeviceList list = new DeviceList();
if (LibUsb.getDeviceList(this.context, list) <= 0) return null;
if (LibUsb.getDeviceList(context, list) <= 0)
{
return null;
}
try
{
for (Device device: list)
{
DeviceDescriptor descriptor = new DeviceDescriptor();
if (LibUsb.getDeviceDescriptor(device, descriptor) != 0)
{
continue;
this.vendorId = descriptor.idVendor();
this.productId = descriptor.idProduct();
}
vendorId = descriptor.idVendor();
productId = descriptor.idProduct();
ConfigDescriptor config = new ConfigDescriptor();
if (LibUsb.getActiveConfigDescriptor(device, config) < 0)
{
return null;
}
try
{
this.configValue = config.bConfigurationValue();
configValue = config.bConfigurationValue();
for (int j = 0; j < config.bNumInterfaces(); j++)
{
Interface iface = config.iface()[j];
@ -100,7 +109,7 @@ public class LibUSBDeviceTest
iface.altsetting()[k];
if (ifaceDescriptor.bNumEndpoints() > 1)
{
this.endpoint = ifaceDescriptor.endpoint()[0].
endpoint = ifaceDescriptor.endpoint()[0].
bEndpointAddress();
return LibUsb.refDevice(device);
}
@ -126,8 +135,14 @@ public class LibUSBDeviceTest
@After
public void tearDown()
{
if (this.device != null) LibUsb.unrefDevice(this.device);
if (this.context != null) LibUsb.exit(this.context);
if (device != null)
{
LibUsb.unrefDevice(device);
}
if (context != null)
{
LibUsb.exit(context);
}
}
/**
@ -137,8 +152,8 @@ public class LibUSBDeviceTest
public void testGetBusNumber()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assertTrue(LibUsb.getBusNumber(this.device) >= 0);
assumeNotNull(device);
assertTrue(LibUsb.getBusNumber(device) >= 0);
}
/**
@ -148,69 +163,126 @@ public class LibUSBDeviceTest
public void testGetPortNumber()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assertTrue(LibUsb.getPortNumber(this.device) >= 0);
assumeNotNull(device);
assertTrue(LibUsb.getPortNumber(device) >= 0);
}
/**
<<<<<<< HEAD
* Tests the {@link LibUsb#getPortNumbers(Device, byte[])} method.
=======
* Tests the {@link LibUsb#getPortPath(Context, Device, ByteBuffer)} method.
>>>>>>> origin/asyncio
*/
@Test
public void testGetPortNumbers()
{
assumeUsbTestsEnabled();
<<<<<<< HEAD
assumeNotNull(this.device);
byte[] path = new byte[8];
int result = LibUsb.getPortNumbers(this.device, path);
=======
assumeNotNull(device);
ByteBuffer path = ByteBuffer.allocateDirect(8);
int result = LibUsb.getPortPath(context, device, path);
>>>>>>> origin/asyncio
assertTrue(result > 0);
assertTrue(result <= path.length);
assertTrue(result <= path.capacity());
}
/**
<<<<<<< HEAD
* Tests the {@link LibUsb#getPortNumbers(Device, byte[])} method with
=======
* Tests the {@link LibUsb#getPortPath(Context, Device, ByteBuffer)} method with
>>>>>>> origin/asyncio
* 0-sized path buffer.
*/
@Test
public void testGetPortNumbersWithTooSmallBuffer()
{
assumeUsbTestsEnabled();
<<<<<<< HEAD
assumeNotNull(this.device);
byte[] path = new byte[0];
int result = LibUsb.getPortNumbers(this.device, path);
=======
assumeNotNull(device);
ByteBuffer path = ByteBuffer.allocateDirect(0);
int result = LibUsb.getPortPath(context, device, path);
>>>>>>> origin/asyncio
assertEquals(LibUsb.ERROR_OVERFLOW, result);
}
/**
<<<<<<< HEAD
* Tests the {@link LibUsb#getPortNumbers(Device, byte[])} method
=======
* Tests the {@link LibUsb#getPortPath(Context, Device, ByteBuffer)} method
>>>>>>> origin/asyncio
* without a device.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPortNumbersWithoutDevice()
{
assumeUsbTestsEnabled();
<<<<<<< HEAD
LibUsb.getPortNumbers(null, new byte[8]);
}
/**
* Tests the {@link LibUsb#getPortNumbers(Device, byte[])} method
=======
LibUsb.getPortPath(context, null, ByteBuffer.allocateDirect(8));
}
/**
* Tests the {@link LibUsb#getPortPath(Context, Device, ByteBuffer)} method
>>>>>>> origin/asyncio
* without a buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetPortNumbersWithoutBuffer()
{
assumeUsbTestsEnabled();
<<<<<<< HEAD
LibUsb.getPortNumbers(this.device, null);
}
/**
* Tests {@link LibUsb#getPortNumbers(Device, byte[])} method with
=======
LibUsb.getPortPath(context, device, null);
}
/**
* Tests {@link LibUsb#getPortPath(Context, Device, ByteBuffer)} method with
* uninitialized USB context.
*/
@Test(expected = IllegalStateException.class)
public void testGetPortPathWithUninitializedContext()
{
assumeUsbTestsEnabled();
assumeNotNull(device);
final Context context = new Context();
LibUsb.getPortPath(context, device, ByteBuffer.allocateDirect(16));
}
/**
* Tests {@link LibUsb#getPortPath(Context, Device, ByteBuffer)} method with
>>>>>>> origin/asyncio
* uninitialized device.
*/
@Test(expected = IllegalStateException.class)
public void testGetPortNumbersWithUninitializedDevice()
{
assumeUsbTestsEnabled();
<<<<<<< HEAD
LibUsb.getPortNumbers(new Device(), new byte[16]);
=======
LibUsb.getPortPath(context, new Device(), ByteBuffer.allocateDirect(16));
>>>>>>> origin/asyncio
}
/**
@ -220,17 +292,17 @@ public class LibUSBDeviceTest
public void testGetParent()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
DeviceList list = new DeviceList();
LibUsb.getDeviceList(this.context, list);
LibUsb.getDeviceList(context, list);
try
{
Device parent = LibUsb.getParent(this.device);
Device parent = LibUsb.getParent(device);
// We cannot test anything else here. Parent can be null if our
// test device is a root device. We just make sure that it can't
// be the device itself.
assertNotEquals(parent, this.device);
assertNotEquals(parent, device);
}
finally
{
@ -255,8 +327,8 @@ public class LibUSBDeviceTest
public void testGetDeviceAddress()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assertTrue(LibUsb.getDeviceAddress(this.device) >= 0);
assumeNotNull(device);
assertTrue(LibUsb.getDeviceAddress(device) >= 0);
}
/**
@ -277,9 +349,9 @@ public class LibUSBDeviceTest
public void testGetDeviceSpeed()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
int speed = LibUsb.getDeviceSpeed(this.device);
assertTrue(speed >= LibUsb.SPEED_UNKNOWN && speed <= LibUsb.SPEED_SUPER);
assumeNotNull(device);
int speed = LibUsb.getDeviceSpeed(device);
assertTrue((speed >= LibUsb.SPEED_UNKNOWN) && (speed <= LibUsb.SPEED_SUPER));
}
/**
@ -293,71 +365,71 @@ public class LibUSBDeviceTest
}
/**
* Tests the {@link LibUsb#getMaxPacketSize(Device, int)} method.
* Tests the {@link LibUsb#getMaxPacketSize(Device, byte)} method.
*/
@Test
public void testGetMaxPacketSizeWithInvalidEndpoint()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
assertEquals(LibUsb.ERROR_NOT_FOUND,
LibUsb.getMaxPacketSize(this.device, 0));
LibUsb.getMaxPacketSize(device, (byte) 0));
}
/**
* Tests the {@link LibUsb#getMaxPacketSize(Device, int)} method.
* Tests the {@link LibUsb#getMaxPacketSize(Device, byte)} method.
*/
@Test
public void testGetMaxPacketSize()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assertTrue(LibUsb.getMaxPacketSize(this.device, this.endpoint) > 0);
assumeNotNull(device);
assertTrue(LibUsb.getMaxPacketSize(device, endpoint) > 0);
}
/**
* Tests the {@link LibUsb#getMaxPacketSize(Device, int)} method without a
* Tests the {@link LibUsb#getMaxPacketSize(Device, byte)} method without a
* device.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetMaxPacketSizeWithoutDevice()
{
assumeUsbTestsEnabled();
LibUsb.getMaxPacketSize(null, 0);
LibUsb.getMaxPacketSize(null, (byte) 0);
}
/**
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, int)} method.
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, byte)} method.
*/
@Test
public void testGetMaxIsoPacketSizeWithInvalidEndpoint()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
assertEquals(LibUsb.ERROR_NOT_FOUND,
LibUsb.getMaxIsoPacketSize(this.device, 0));
LibUsb.getMaxIsoPacketSize(device, (byte) 0));
}
/**
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, int)} method.
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, byte)} method.
*/
@Test
public void testGetMaxIsoPacketSize()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assertTrue(LibUsb.getMaxIsoPacketSize(this.device, this.endpoint) > 0);
assumeNotNull(device);
assertTrue(LibUsb.getMaxIsoPacketSize(device, endpoint) > 0);
}
/**
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, int)} method without
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, byte)} method without
* a device.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetMaxIsoPacketSizeWithoutDevice()
{
assumeUsbTestsEnabled();
LibUsb.getMaxIsoPacketSize(null, 0);
LibUsb.getMaxIsoPacketSize(null, (byte) 0);
}
/**
@ -368,7 +440,7 @@ public class LibUSBDeviceTest
public void testRefUnRefDevice()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
Device device = LibUsb.refDevice(this.device);
try
{
@ -411,10 +483,10 @@ public class LibUSBDeviceTest
public void testOpenAndClose()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
DeviceHandle handle = new DeviceHandle();
int result = LibUsb.open(this.device, handle);
assertTrue(result == LibUsb.SUCCESS || result == LibUsb.ERROR_ACCESS);
int result = LibUsb.open(device, handle);
assertTrue((result == LibUsb.SUCCESS) || (result == LibUsb.ERROR_ACCESS));
if (result == LibUsb.SUCCESS)
{
LibUsb.close(handle);
@ -451,7 +523,7 @@ public class LibUSBDeviceTest
public void testOpenWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.open(this.device, null);
LibUsb.open(device, null);
}
/**
@ -473,9 +545,12 @@ public class LibUSBDeviceTest
public void testOpenDeviceWithVidPid()
{
assumeUsbTestsEnabled();
DeviceHandle handle = LibUsb.openDeviceWithVidPid(this.context,
this.vendorId, this.productId);
if (handle != null) LibUsb.close(handle);
DeviceHandle handle = LibUsb.openDeviceWithVidPid(context,
vendorId, productId);
if (handle != null)
{
LibUsb.close(handle);
}
}
/**
@ -557,14 +632,14 @@ public class LibUSBDeviceTest
}
/**
* Tests the {@link LibUsb#clearHalt(DeviceHandle, int)} method without a
* Tests the {@link LibUsb#clearHalt(DeviceHandle, byte)} method without a
* handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testClearHaltWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.clearHalt(null, 0);
LibUsb.clearHalt(null, (byte) 0);
}
/**
@ -630,7 +705,7 @@ public class LibUSBDeviceTest
public void testGetDeviceDescriptorWithoutDescriptor()
{
assumeUsbTestsEnabled();
LibUsb.getDeviceDescriptor(this.device, null);
LibUsb.getDeviceDescriptor(device, null);
}
/**
@ -645,16 +720,16 @@ public class LibUSBDeviceTest
public void testGetDeviceDescriptor()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
DeviceDescriptor desc = new DeviceDescriptor();
LibUsb.getDeviceDescriptor(this.device, desc);
LibUsb.getDeviceDescriptor(device, desc);
desc.bcdDevice();
desc.bcdUSB();
assertEquals(LibUsb.DT_DEVICE, desc.bDescriptorType());
desc.bDeviceClass();
desc.bDeviceProtocol();
desc.bDeviceSubClass();
assertEquals(desc.getData().limit(), desc.bLength());
assertTrue(desc.bLength() > 0);
desc.bMaxPacketSize0();
desc.bNumConfigurations();
}
@ -680,7 +755,7 @@ public class LibUSBDeviceTest
public void testGetActiveConfigDescriptorWithoutDescriptor()
{
assumeUsbTestsEnabled();
LibUsb.getActiveConfigDescriptor(this.device, null);
LibUsb.getActiveConfigDescriptor(device, null);
}
/**
@ -702,7 +777,9 @@ public class LibUSBDeviceTest
assertTrue(desc.wTotalLength() >= desc.bLength());
for (Interface iface: desc.iface())
{
validateInterface(iface);
}
}
/**
@ -773,9 +850,9 @@ public class LibUSBDeviceTest
public void testGetActiveConfigDescriptor()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
ConfigDescriptor desc = new ConfigDescriptor();
LibUsb.getActiveConfigDescriptor(this.device, desc);
LibUsb.getActiveConfigDescriptor(device, desc);
try
{
validateConfigDescriptor(desc);
@ -788,39 +865,39 @@ public class LibUSBDeviceTest
/**
* Tests the
* {@link LibUsb#getConfigDescriptor(Device, int, ConfigDescriptor)} method
* {@link LibUsb#getConfigDescriptor(Device, byte, ConfigDescriptor)} method
* without a device.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetConfigDescriptorWithoutDevice()
{
assumeUsbTestsEnabled();
LibUsb.getConfigDescriptor(null, 0, new ConfigDescriptor());
LibUsb.getConfigDescriptor(null, (byte) 0, new ConfigDescriptor());
}
/**
* Tests the
* {@link LibUsb#getConfigDescriptor(Device, int, ConfigDescriptor)} method
* {@link LibUsb#getConfigDescriptor(Device, byte, ConfigDescriptor)} method
* without a descriptor.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetConfigDescriptorWithoutDescriptor()
{
assumeUsbTestsEnabled();
LibUsb.getConfigDescriptor(this.device, 0, null);
LibUsb.getConfigDescriptor(device, (byte) 0, null);
}
/**
* Tests the
* {@link LibUsb#getConfigDescriptor(Device, int, ConfigDescriptor)} method.
* {@link LibUsb#getConfigDescriptor(Device, byte, ConfigDescriptor)} method.
*/
@Test
public void testGetConfigDescriptor()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
ConfigDescriptor desc = new ConfigDescriptor();
LibUsb.getConfigDescriptor(this.device, 0, desc);
LibUsb.getConfigDescriptor(device, (byte) 0, desc);
try
{
validateConfigDescriptor(desc);
@ -833,40 +910,40 @@ public class LibUSBDeviceTest
/**
* Tests the
* {@link LibUsb#getConfigDescriptorByValue(Device, int, ConfigDescriptor)}
* {@link LibUsb#getConfigDescriptorByValue(Device, byte, ConfigDescriptor)}
* method without a device.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetConfigDescriptorByValueWithoutDevice()
{
assumeUsbTestsEnabled();
LibUsb.getConfigDescriptorByValue(null, 0, new ConfigDescriptor());
LibUsb.getConfigDescriptorByValue(null, (byte) 0, new ConfigDescriptor());
}
/**
* Tests the
* {@link LibUsb#getConfigDescriptorByValue(Device, int, ConfigDescriptor)}
* {@link LibUsb#getConfigDescriptorByValue(Device, byte, ConfigDescriptor)}
* method without a descriptor.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetConfigDescriptorByValueWithoutDescriptor()
{
assumeUsbTestsEnabled();
LibUsb.getConfigDescriptorByValue(this.device, 0, null);
LibUsb.getConfigDescriptorByValue(device, (byte) 0, null);
}
/**
* Tests the
* {@link LibUsb#getConfigDescriptorByValue(Device, int, ConfigDescriptor)}
* {@link LibUsb#getConfigDescriptorByValue(Device, byte, ConfigDescriptor)}
* method.
*/
@Test
public void testGetConfigDescriptorByValue()
{
assumeUsbTestsEnabled();
assumeNotNull(this.device);
assumeNotNull(device);
ConfigDescriptor desc = new ConfigDescriptor();
LibUsb.getConfigDescriptorByValue(this.device, this.configValue, desc);
LibUsb.getConfigDescriptorByValue(device, configValue, desc);
try
{
validateConfigDescriptor(desc);
@ -900,214 +977,214 @@ public class LibUSBDeviceTest
/**
* Tests the
* {@link LibUsb#getStringDescriptorAscii(DeviceHandle, int, StringBuffer, int)}
* {@link LibUsb#getStringDescriptorAscii(DeviceHandle, byte, StringBuffer)}
* method without a handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetStringDescriptorAsciiWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.getStringDescriptorAscii(null, 0, new StringBuffer(), 0);
LibUsb.getStringDescriptorAscii(null, (byte) 0, new StringBuffer());
}
/**
* Tests the
* {@link LibUsb#getStringDescriptorAscii(DeviceHandle, int, StringBuffer, int)}
* {@link LibUsb#getStringDescriptorAscii(DeviceHandle, byte, StringBuffer)}
* method without a buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetStringDescriptorAsciiWithoutBuffer()
{
assumeUsbTestsEnabled();
LibUsb.getStringDescriptorAscii(new DeviceHandle(), 0, null, 0);
LibUsb.getStringDescriptorAscii(new DeviceHandle(), (byte) 0, null);
}
/**
* Tests the
* {@link LibUsb#getDescriptor(DeviceHandle, int, int, ByteBuffer)} method
* {@link LibUsb#getDescriptor(DeviceHandle, byte, byte, ByteBuffer)} method
* without a handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetDescriptorWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.getDescriptor(null, 0, 0, ByteBuffer.allocate(18));
LibUsb.getDescriptor(null, (byte) 0, (byte) 0, ByteBuffer.allocate(18));
}
/**
* Tests the
* {@link LibUsb#getDescriptor(DeviceHandle, int, int, ByteBuffer)} method
* {@link LibUsb#getDescriptor(DeviceHandle, byte, byte, ByteBuffer)} method
* without a buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetDescriptorWithoutBuffer()
{
assumeUsbTestsEnabled();
LibUsb.getDescriptor(new DeviceHandle(), 0, 0, null);
LibUsb.getDescriptor(new DeviceHandle(), (byte) 0, (byte) 0, null);
}
/**
* Tests the
* {@link LibUsb#getStringDescriptor(DeviceHandle, int, int, ByteBuffer)}
* {@link LibUsb#getStringDescriptor(DeviceHandle, byte, short, ByteBuffer)}
* method without a handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetStringDescriptorWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.getStringDescriptor(null, 0, 0, ByteBuffer.allocate(18));
LibUsb.getStringDescriptor(null, (byte) 0, (short) 0, ByteBuffer.allocate(18));
}
/**
* Tests the
* {@link LibUsb#getStringDescriptor(DeviceHandle, int, int, ByteBuffer)}
* {@link LibUsb#getStringDescriptor(DeviceHandle, byte, short, ByteBuffer)}
* method without a buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testGetStringDescriptorWithoutBuffer()
{
assumeUsbTestsEnabled();
LibUsb.getStringDescriptor(new DeviceHandle(), 0, 0, null);
LibUsb.getStringDescriptor(new DeviceHandle(), (byte) 0, (short) 0, null);
}
/**
* Tests the
* {@link LibUsb#controlTransfer(DeviceHandle, int, int, int, int, ByteBuffer, int)}
* {@link LibUsb#controlTransfer(DeviceHandle, byte, byte, short, short, ByteBuffer, long)}
* method without a handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testControlTransferWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.controlTransfer(null, 0, 0, 0, 0, ByteBuffer.allocate(0), 0);
LibUsb.controlTransfer(null, (byte) 0, (byte) 0, (short) 0, (short) 0, ByteBuffer.allocate(0), 0);
}
/**
* Tests the
* {@link LibUsb#controlTransfer(DeviceHandle, int, int, int, int, ByteBuffer, int)}
* {@link LibUsb#controlTransfer(DeviceHandle, byte, byte, short, short, ByteBuffer, long)}
* method without a buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testControlTransferWithoutBuffer()
{
assumeUsbTestsEnabled();
LibUsb.controlTransfer(new DeviceHandle(), 0, 0, 0, 0, null, 0);
LibUsb.controlTransfer(new DeviceHandle(), (byte) 0, (byte) 0, (short) 0, (short) 0, null, 0);
}
/**
* Tests the
* {@link LibUsb#controlTransfer(DeviceHandle, int, int, int, int, ByteBuffer, int)}
* {@link LibUsb#controlTransfer(DeviceHandle, byte, byte, short, short, ByteBuffer, long)}
* method with an indirect buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testControlTransferWithIndirectBuffer()
{
assumeUsbTestsEnabled();
LibUsb.controlTransfer(new DeviceHandle(), 0, 0, 0, 0,
LibUsb.controlTransfer(new DeviceHandle(), (byte) 0, (byte) 0, (short) 0, (short) 0,
ByteBuffer.allocate(0), 0);
}
/**
* Tests the
* {@link LibUsb#bulkTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#bulkTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method without a handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testBulkTransferWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.bulkTransfer(null, 0, ByteBuffer.allocate(0),
LibUsb.bulkTransfer(null, (byte) 0, ByteBuffer.allocate(0),
IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#bulkTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#bulkTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method without a data buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testBulkTransferWithoutDataBuffer()
{
assumeUsbTestsEnabled();
LibUsb.bulkTransfer(new DeviceHandle(), 0, null,
LibUsb.bulkTransfer(new DeviceHandle(), (byte) 0, null,
IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#bulkTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#bulkTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method with an indirect data buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testBulkTransferWithIndirectDataBuffer()
{
assumeUsbTestsEnabled();
LibUsb.bulkTransfer(new DeviceHandle(), 0, ByteBuffer.allocate(0),
LibUsb.bulkTransfer(new DeviceHandle(), (byte) 0, ByteBuffer.allocate(0),
IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#bulkTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#bulkTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method without a transferred buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testBulkTransferWithoutTransferredBuffer()
{
assumeUsbTestsEnabled();
LibUsb.bulkTransfer(new DeviceHandle(), 0, ByteBuffer.allocate(0),
LibUsb.bulkTransfer(new DeviceHandle(), (byte) 0, ByteBuffer.allocate(0),
null, 0);
}
/**
* Tests the
* {@link LibUsb#interruptTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#interruptTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method without a handle.
*/
@Test(expected = IllegalArgumentException.class)
public void testInterruptTransferWithoutHandle()
{
assumeUsbTestsEnabled();
LibUsb.interruptTransfer(null, 0, ByteBuffer.allocate(0),
LibUsb.interruptTransfer(null, (byte) 0, ByteBuffer.allocate(0),
IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#interruptTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#interruptTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method without a data buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testInterruptTransferWithoutDataBuffer()
{
assumeUsbTestsEnabled();
LibUsb.interruptTransfer(new DeviceHandle(), 0, null,
LibUsb.interruptTransfer(new DeviceHandle(), (byte) 0, null,
IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#interruptTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#interruptTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method with an indirect data buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testInterruptTransferWithIndirectDataBuffer()
{
assumeUsbTestsEnabled();
LibUsb.interruptTransfer(new DeviceHandle(), 0, ByteBuffer.allocate(0),
LibUsb.interruptTransfer(new DeviceHandle(), (byte) 0, ByteBuffer.allocate(0),
IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#interruptTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#interruptTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method without a transferred buffer.
*/
@Test(expected = IllegalArgumentException.class)
public void testInterruptTransferWithoutTransferredBuffer()
{
assumeUsbTestsEnabled();
LibUsb.interruptTransfer(new DeviceHandle(), 0, ByteBuffer.allocate(0),
LibUsb.interruptTransfer(new DeviceHandle(), (byte) 0, ByteBuffer.allocate(0),
null, 0);
}
@ -1120,9 +1197,9 @@ public class LibUSBDeviceTest
{
assumeUsbTestsEnabled();
DeviceList list = new DeviceList();
assertTrue(LibUsb.getDeviceList(this.context, list) >= 0);
assertTrue(LibUsb.getDeviceList(context, list) >= 0);
LibUsb.freeDeviceList(list, true);
try
{
LibUsb.freeDeviceList(list, true);
@ -1149,7 +1226,7 @@ public class LibUSBDeviceTest
{
assertTrue(LibUsb.getDeviceList(null, list) >= 0);
LibUsb.freeDeviceList(list, true);
try
{
LibUsb.freeDeviceList(list, true);

View File

@ -16,7 +16,7 @@ import org.junit.Before;
import org.junit.Test;
/**
* Tests the global-scope methods of the {@link LibUsb} class which need a
* Tests the global-scope methods of the {@link LibUsb} class which need a
* open USB context.
*
* @author Klaus Reimer (k@ailis.de)
@ -32,14 +32,14 @@ public class LibUSBGlobalTest
@Before
public void setUp()
{
this.context = new Context();
context = new Context();
try
{
LibUsb.init(this.context);
LibUsb.init(context);
}
catch (Throwable e)
{
this.context = null;
context = null;
}
}
@ -49,7 +49,10 @@ public class LibUSBGlobalTest
@After
public void tearDown()
{
if (this.context != null) LibUsb.exit(this.context);
if (context != null)
{
LibUsb.exit(context);
}
}
/**
@ -59,11 +62,11 @@ public class LibUSBGlobalTest
public void testSetDebug()
{
assumeUsbTestsEnabled();
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_DEBUG);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_INFO);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_WARNING);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_ERROR);
LibUsb.setDebug(this.context, LibUsb.LOG_LEVEL_NONE);
LibUsb.setDebug(context, LibUsb.LOG_LEVEL_DEBUG);
LibUsb.setDebug(context, LibUsb.LOG_LEVEL_INFO);
LibUsb.setDebug(context, LibUsb.LOG_LEVEL_WARNING);
LibUsb.setDebug(context, LibUsb.LOG_LEVEL_ERROR);
LibUsb.setDebug(context, LibUsb.LOG_LEVEL_NONE);
}
/**
@ -74,7 +77,7 @@ public class LibUSBGlobalTest
{
assumeUsbTestsEnabled();
final DeviceList list = new DeviceList();
final int result = LibUsb.getDeviceList(this.context, list);
final int result = LibUsb.getDeviceList(context, list);
assertTrue(
"At least one USB device must be present for the simple unit tests",
result > 0);
@ -98,7 +101,7 @@ public class LibUSBGlobalTest
public void testGetDeviceListWithoutList()
{
assumeUsbTestsEnabled();
LibUsb.getDeviceList(this.context, null);
LibUsb.getDeviceList(context, null);
}
/**
@ -109,7 +112,7 @@ public class LibUSBGlobalTest
public void testEndianConversion()
{
assumeUsbTestsEnabled();
assertEquals(0x1234, LibUsb.le16ToCpu(LibUsb.cpuToLe16(0x1234)));
assertEquals(0x1234, LibUsb.le16ToCpu(LibUsb.cpuToLe16((short) 0x1234)));
}
/**

View File

@ -16,10 +16,12 @@ import static org.junit.Assert.fail;
import java.io.FileDescriptor;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.nio.LongBuffer;
import org.junit.Test;
import de.ailis.usb4java.libusb.mocks.PollfdListenerMock;
import de.ailis.usb4java.utils.BufferUtils;
/**
* Tests the {@link LibUsb} class.
@ -57,9 +59,9 @@ public class LibUSBTest
assertEquals(0x07, LibUsb.REQUEST_SET_DESCRIPTOR);
assertEquals(0x08, LibUsb.REQUEST_GET_CONFIGURATION);
assertEquals(0x09, LibUsb.REQUEST_SET_CONFIGURATION);
assertEquals(0x0a, LibUsb.REQUEST_GET_INTERFACE);
assertEquals(0x0b, LibUsb.REQUEST_SET_INTERFACE);
assertEquals(0x0c, LibUsb.REQUEST_SYNCH_FRAME);
assertEquals(0x0A, LibUsb.REQUEST_GET_INTERFACE);
assertEquals(0x0B, LibUsb.REQUEST_SET_INTERFACE);
assertEquals(0x0C, LibUsb.REQUEST_SYNCH_FRAME);
assertEquals(0x30, LibUsb.REQUEST_SET_SEL);
assertEquals(0x31, LibUsb.SET_ISOCH_DELAY);
@ -106,14 +108,14 @@ public class LibUSBTest
assertEquals(8, LibUsb.CLASS_MASS_STORAGE);
assertEquals(9, LibUsb.CLASS_HUB);
assertEquals(10, LibUsb.CLASS_DATA);
assertEquals(0x0b, LibUsb.CLASS_SMART_CARD);
assertEquals(0x0d, LibUsb.CLASS_CONTENT_SECURITY);
assertEquals(0x0e, LibUsb.CLASS_VIDEO);
assertEquals(0x0f, LibUsb.CLASS_PERSONAL_HEALTHCARE);
assertEquals(0xdc, LibUsb.CLASS_DIAGNOSTIC_DEVICE);
assertEquals(0xe0, LibUsb.CLASS_WIRELESS);
assertEquals(0xfe, LibUsb.CLASS_APPLICATION);
assertEquals(0xff, LibUsb.CLASS_VENDOR_SPEC);
assertEquals(0x0B, LibUsb.CLASS_SMART_CARD);
assertEquals(0x0D, LibUsb.CLASS_CONTENT_SECURITY);
assertEquals(0x0E, LibUsb.CLASS_VIDEO);
assertEquals(0x0F, LibUsb.CLASS_PERSONAL_HEALTHCARE);
assertEquals((byte) 0xDC, LibUsb.CLASS_DIAGNOSTIC_DEVICE);
assertEquals((byte) 0xE0, LibUsb.CLASS_WIRELESS);
assertEquals((byte) 0xFE, LibUsb.CLASS_APPLICATION);
assertEquals((byte) 0xFF, LibUsb.CLASS_VENDOR_SPEC);
// Descriptor types
assertEquals(0x01, LibUsb.DT_DEVICE);
@ -125,10 +127,10 @@ public class LibUSBTest
assertEquals(0x22, LibUsb.DT_REPORT);
assertEquals(0x23, LibUsb.DT_PHYSICAL);
assertEquals(0x29, LibUsb.DT_HUB);
assertEquals(0x2a, LibUsb.DT_SUPERSPEED_HUB);
assertEquals(0x2A, LibUsb.DT_SUPERSPEED_HUB);
// Endpoint direction
assertEquals(0x80, LibUsb.ENDPOINT_IN);
assertEquals((byte) 0x80, LibUsb.ENDPOINT_IN);
assertEquals(0x00, LibUsb.ENDPOINT_OUT);
// Transfer types
@ -160,7 +162,7 @@ public class LibUSBTest
assertNotNull(version);
assertEquals(1, version.major());
assertEquals(0, version.minor());
assertTrue(version.micro() > 0 && version.micro() < 100);
assertTrue((version.micro() > 0) && (version.micro() < 100));
assertNotNull(version.rc());
assertTrue(version.toString().startsWith("1.0."));
}
@ -354,25 +356,25 @@ public class LibUSBTest
}
/**
* Tests the {@link LibUsb#getMaxPacketSize(Device, int)} method with
* Tests the {@link LibUsb#getMaxPacketSize(Device, byte)} method with
* uninitialized device.
*/
@Test(expected = IllegalStateException.class)
public void testMaxPacketSizeWithUninitializedDevice()
{
assumeUsbTestsEnabled();
LibUsb.getMaxPacketSize(new Device(), 0);
LibUsb.getMaxPacketSize(new Device(), (byte) 0);
}
/**
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, int)} method with
* Tests the {@link LibUsb#getMaxIsoPacketSize(Device, byte)} method with
* uninitialized device.
*/
@Test(expected = IllegalStateException.class)
public void testMaxIsoPacketSizeWithUninitializedDevice()
{
assumeUsbTestsEnabled();
LibUsb.getMaxIsoPacketSize(new Device(), 0);
LibUsb.getMaxIsoPacketSize(new Device(), (byte) 0);
}
/**
@ -486,14 +488,14 @@ public class LibUSBTest
}
/**
* Tests the {@link LibUsb#clearHalt(DeviceHandle, int)} method with
* Tests the {@link LibUsb#clearHalt(DeviceHandle, byte)} method with
* uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testClearHaltWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.clearHalt(new DeviceHandle(), 0);
LibUsb.clearHalt(new DeviceHandle(), (byte) 0);
}
/**
@ -575,15 +577,15 @@ public class LibUSBTest
/**
* Tests the
* {@link LibUsb#getStringDescriptorAscii(DeviceHandle, int, StringBuffer, int)}
* {@link LibUsb#getStringDescriptorAscii(DeviceHandle, byte, StringBuffer)}
* method with uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testGetStringDescriptorAsciiWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.getStringDescriptorAscii(new DeviceHandle(), 0,
new StringBuffer(), 0);
LibUsb.getStringDescriptorAscii(new DeviceHandle(), (byte) 0,
new StringBuffer());
}
/**
@ -600,26 +602,26 @@ public class LibUSBTest
/**
* Tests the
* {@link LibUsb#getConfigDescriptor(Device, int, ConfigDescriptor)} method
* {@link LibUsb#getConfigDescriptor(Device, byte, ConfigDescriptor)} method
* with uninitialized device.
*/
@Test(expected = IllegalStateException.class)
public void testGetConfigDescriptorWithUninitializedDevice()
{
assumeUsbTestsEnabled();
LibUsb.getConfigDescriptor(new Device(), 0, new ConfigDescriptor());
LibUsb.getConfigDescriptor(new Device(), (byte) 0, new ConfigDescriptor());
}
/**
* Tests the
* {@link LibUsb#getConfigDescriptorByValue(Device, int, ConfigDescriptor)}
* {@link LibUsb#getConfigDescriptorByValue(Device, byte, ConfigDescriptor)}
* method with uninitialized device.
*/
@Test(expected = IllegalStateException.class)
public void testGetConfigDescriptorByValueWithUninitializedDevice()
{
assumeUsbTestsEnabled();
LibUsb.getConfigDescriptorByValue(new Device(), 0,
LibUsb.getConfigDescriptorByValue(new Device(), (byte) 0,
new ConfigDescriptor());
}
@ -636,6 +638,7 @@ public class LibUSBTest
/**
* Tests the
<<<<<<< HEAD
* {@link LibUsb#getSsEndpointCompanionDescriptor(Context, EndpointDescriptor, SsEndpointCompanionDescriptor)}
* method with uninitialized endpoint.
*/
@ -890,65 +893,68 @@ public class LibUSBTest
/**
* Tests the
* {@link LibUsb#getDescriptor(DeviceHandle, int, int, ByteBuffer)} method
=======
* {@link LibUsb#getDescriptor(DeviceHandle, byte, byte, ByteBuffer)} method
>>>>>>> origin/asyncio
* with uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testGetDescriptorWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.getDescriptor(new DeviceHandle(), 0, 0,
LibUsb.getDescriptor(new DeviceHandle(), (byte) 0, (byte) 0,
ByteBuffer.allocateDirect(1));
}
/**
* Tests the
* {@link LibUsb#getStringDescriptor(DeviceHandle, int, int, ByteBuffer)}
* {@link LibUsb#getStringDescriptor(DeviceHandle, byte, short, ByteBuffer)}
* method with uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testGetStringDescriptorWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.getStringDescriptor(new DeviceHandle(), 0, 0,
LibUsb.getStringDescriptor(new DeviceHandle(), (byte) 0, (short) 0,
ByteBuffer.allocateDirect(1));
}
/**
* Tests the
* {@link LibUsb#controlTransfer(DeviceHandle, int, int, int, int, ByteBuffer, int)}
* {@link LibUsb#controlTransfer(DeviceHandle, byte, byte, short, short, ByteBuffer, long)}
* method with uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testControlTransferWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.controlTransfer(new DeviceHandle(), 0, 0, 0, 0,
LibUsb.controlTransfer(new DeviceHandle(), (byte) 0, (byte) 0, (short) 0, (short) 0,
ByteBuffer.allocateDirect(1), 0);
}
/**
* Tests the
* {@link LibUsb#bulkTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#bulkTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method with uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testBulkTransferWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.bulkTransfer(new DeviceHandle(), 0,
LibUsb.bulkTransfer(new DeviceHandle(), (byte) 0,
ByteBuffer.allocateDirect(1), IntBuffer.allocate(1), 0);
}
/**
* Tests the
* {@link LibUsb#interruptTransfer(DeviceHandle, int, ByteBuffer, IntBuffer, int)}
* {@link LibUsb#interruptTransfer(DeviceHandle, byte, ByteBuffer, IntBuffer, long)}
* method with uninitialized device handle.
*/
@Test(expected = IllegalStateException.class)
public void testInterruptTransferWithUninitializedHandle()
{
assumeUsbTestsEnabled();
LibUsb.interruptTransfer(new DeviceHandle(), 0,
LibUsb.interruptTransfer(new DeviceHandle(), (byte) 0,
ByteBuffer.allocateDirect(1), IntBuffer.allocate(1), 0);
}
@ -964,7 +970,7 @@ public class LibUSBTest
}
/**
* Tests {@link LibUsb#openDeviceWithVidPid(Context, int, int)} with
* Tests {@link LibUsb#openDeviceWithVidPid(Context, short, short)} with
* uninitialized USB context.
*/
@Test(expected = IllegalStateException.class)
@ -972,7 +978,7 @@ public class LibUSBTest
{
assumeUsbTestsEnabled();
final Context context = new Context();
LibUsb.openDeviceWithVidPid(context, 0, 0);
LibUsb.openDeviceWithVidPid(context, (short) 0, (short) 0);
}
/**
@ -1080,7 +1086,7 @@ public class LibUSBTest
{
assumeUsbTestsEnabled();
final Context context = new Context();
LibUsb.handleEventsTimeoutCompleted(context, 53, IntBuffer.allocate(1));
LibUsb.handleEventsTimeoutCompleted(context, 53, BufferUtils.allocateIntBuffer());
}
/**
@ -1116,7 +1122,7 @@ public class LibUSBTest
{
assumeUsbTestsEnabled();
final Context context = new Context();
LibUsb.handleEventsCompleted(context, IntBuffer.allocate(1));
LibUsb.handleEventsCompleted(context, BufferUtils.allocateIntBuffer());
}
/**
@ -1152,7 +1158,7 @@ public class LibUSBTest
{
assumeUsbTestsEnabled();
final Context context = new Context();
LibUsb.getNextTimeout(context, IntBuffer.allocate(1));
LibUsb.getNextTimeout(context, LongBuffer.allocate(1));
}
/**
@ -1164,7 +1170,7 @@ public class LibUSBTest
{
assumeUsbTestsEnabled();
final Context context = new Context();
LibUsb.setPollfdNotifiers(context);
LibUsb.setPollfdNotifiers(context, context.getPointer());
}
/**
@ -1194,7 +1200,7 @@ public class LibUSBTest
LibUsb.setPollfdNotifiers(context, listener, "test");
FileDescriptor fd = new FileDescriptor();
LibUsb.triggerPollfdAdded(fd, 53);
LibUsb.triggerPollfdAdded(fd, 53, context.getPointer());
assertEquals(53, listener.addedEvents);
assertSame(fd, listener.addedFd);
assertSame("test", listener.addedUserData);
@ -1204,7 +1210,7 @@ public class LibUSBTest
listener.reset();
fd = new FileDescriptor();
LibUsb.triggerPollfdRemoved(fd);
LibUsb.triggerPollfdRemoved(fd, context.getPointer());
assertEquals(0, listener.addedEvents);
assertNull(listener.addedFd);
assertNull(listener.addedUserData);
@ -1215,7 +1221,7 @@ public class LibUSBTest
listener.reset();
fd = new FileDescriptor();
LibUsb.triggerPollfdAdded(fd, 53);
LibUsb.triggerPollfdAdded(fd, 53, context.getPointer());
assertEquals(0, listener.addedEvents);
assertNull(listener.addedFd);
assertNull(listener.addedUserData);
@ -1225,7 +1231,7 @@ public class LibUSBTest
listener.reset();
fd = new FileDescriptor();
LibUsb.triggerPollfdRemoved(fd);
LibUsb.triggerPollfdRemoved(fd, context.getPointer());
assertEquals(0, listener.addedEvents);
assertNull(listener.addedFd);
assertNull(listener.addedUserData);

View File

@ -35,14 +35,14 @@ public class TransferTest
@Before
public void setUp()
{
this.context = new Context();
context = new Context();
try
{
LibUsb.init(this.context);
LibUsb.init(context);
}
catch (Throwable e)
{
this.context = null;
context = null;
}
}
@ -52,7 +52,10 @@ public class TransferTest
@After
public void tearDown()
{
if (this.context != null) LibUsb.exit(this.context);
if (context != null)
{
LibUsb.exit(context);
}
}
/**
@ -113,19 +116,19 @@ public class TransferTest
setPointer(handle, 1);
DeviceHandle handle2 = new DeviceHandle();
setPointer(handle2, 2);
assertNull(transfer.getDevHandle());
assertNull(transfer.devHandle());
transfer.setDevHandle(handle);
assertNotNull(transfer.getDevHandle());
assertNotSame(handle, transfer.getDevHandle());
assertNotEquals(handle2, transfer.getDevHandle());
assertEquals(handle, transfer.getDevHandle());
assertNotNull(transfer.devHandle());
assertNotSame(handle, transfer.devHandle());
assertNotEquals(handle2, transfer.devHandle());
assertEquals(handle, transfer.devHandle());
transfer.setDevHandle(null);
assertNull(transfer.getDevHandle());
assertNull(transfer.devHandle());
LibUsb.freeTransfer(transfer);
}
/**
* Tests the {@link Transfer#setFlags(int)} and {@link Transfer#getFlags()}
* Tests the {@link Transfer#setFlags(byte)} and {@link Transfer#getFlags()}
* methods.
*/
@Test
@ -133,16 +136,16 @@ public class TransferTest
{
assumeUsbTestsEnabled();
Transfer transfer = LibUsb.allocTransfer(0);
assertEquals(0, transfer.getFlags());
transfer.setFlags(1);
assertEquals(1, transfer.getFlags());
transfer.setFlags(0);
assertEquals(0, transfer.getFlags());
assertEquals(0, transfer.flags());
transfer.setFlags((byte) 1);
assertEquals(1, transfer.flags());
transfer.setFlags((byte) 0);
assertEquals(0, transfer.flags());
LibUsb.freeTransfer(transfer);
}
/**
* Tests the {@link Transfer#setEndpoint(int)} and
* Tests the {@link Transfer#setEndpoint(byte)} and
* {@link Transfer#getEndpoint()} methods.
*/
@Test
@ -150,16 +153,16 @@ public class TransferTest
{
assumeUsbTestsEnabled();
Transfer transfer = LibUsb.allocTransfer(0);
assertEquals(0, transfer.getEndpoint());
transfer.setEndpoint(1);
assertEquals(1, transfer.getEndpoint());
transfer.setEndpoint(0);
assertEquals(0, transfer.getEndpoint());
assertEquals(0, transfer.endpoint());
transfer.setEndpoint((byte) 1);
assertEquals(1, transfer.endpoint());
transfer.setEndpoint((byte) 0);
assertEquals(0, transfer.endpoint());
LibUsb.freeTransfer(transfer);
}
/**
* Tests the {@link Transfer#setType(int)} and {@link Transfer#getType()}
* Tests the {@link Transfer#setType(byte)} and {@link Transfer#getType()}
* methods.
*/
@Test
@ -167,11 +170,11 @@ public class TransferTest
{
assumeUsbTestsEnabled();
Transfer transfer = LibUsb.allocTransfer(0);
assertEquals(0, transfer.getType());
transfer.setType(1);
assertEquals(1, transfer.getType());
transfer.setType(0);
assertEquals(0, transfer.getType());
assertEquals(0, transfer.type());
transfer.setType((byte) 1);
assertEquals(1, transfer.type());
transfer.setType((byte) 0);
assertEquals(0, transfer.type());
LibUsb.freeTransfer(transfer);
}
@ -184,11 +187,11 @@ public class TransferTest
{
assumeUsbTestsEnabled();
Transfer transfer = LibUsb.allocTransfer(0);
assertEquals(0, transfer.getTimeout());
assertEquals(0, transfer.timeout());
transfer.setTimeout(1);
assertEquals(1, transfer.getTimeout());
assertEquals(1, transfer.timeout());
transfer.setTimeout(0);
assertEquals(0, transfer.getTimeout());
assertEquals(0, transfer.timeout());
LibUsb.freeTransfer(transfer);
}
@ -200,7 +203,7 @@ public class TransferTest
{
assumeUsbTestsEnabled();
Transfer transfer = LibUsb.allocTransfer(0);
assertEquals(0, transfer.getStatus());
assertEquals(0, transfer.status());
LibUsb.freeTransfer(transfer);
}
}

View File

@ -26,12 +26,12 @@ import de.ailis.usb4java.libusb.LibUsb;
public class DescriptorUtilsTest
{
/**
* Tests the {@link DescriptorUtils#decodeBCD(int)} method.
* Tests the {@link DescriptorUtils#decodeBCD(short)} method.
*/
@Test
public void testDecodeBCD()
{
assertEquals("10.20", DescriptorUtils.decodeBCD(0x1020));
assertEquals("10.20", DescriptorUtils.decodeBCD((short) 0x1020));
}
/**
@ -49,52 +49,52 @@ public class DescriptorUtilsTest
}
/**
* Tests the {@link DescriptorUtils#getSynchTypeName(int)} method.
* Tests the {@link DescriptorUtils#getSynchTypeName(byte)} method.
*/
@Test
public void testGetSynchTypeName()
{
assertEquals("None", DescriptorUtils.getSynchTypeName(0));
assertEquals("Asynchronous", DescriptorUtils.getSynchTypeName(4));
assertEquals("Adaptive", DescriptorUtils.getSynchTypeName(8));
assertEquals("Synchronous", DescriptorUtils.getSynchTypeName(12));
assertEquals("None", DescriptorUtils.getSynchTypeName((byte) 0));
assertEquals("Asynchronous", DescriptorUtils.getSynchTypeName((byte) 4));
assertEquals("Adaptive", DescriptorUtils.getSynchTypeName((byte) 8));
assertEquals("Synchronous", DescriptorUtils.getSynchTypeName((byte) 12));
}
/**
* Tests the {@link DescriptorUtils#getUsageTypeName(int)} method.
* Tests the {@link DescriptorUtils#getUsageTypeName(byte)} method.
*/
@Test
public void testGetUsageTypeName()
{
assertEquals("Data", DescriptorUtils.getUsageTypeName(0));
assertEquals("Feedback", DescriptorUtils.getUsageTypeName(16));
assertEquals("Explicit Feedback Data",
DescriptorUtils.getUsageTypeName(32));
assertEquals("Reserved", DescriptorUtils.getUsageTypeName(48));
assertEquals("Data", DescriptorUtils.getUsageTypeName((byte) 0));
assertEquals("Feedback", DescriptorUtils.getUsageTypeName((byte) 16));
assertEquals("Implicit Feedback Data",
DescriptorUtils.getUsageTypeName((byte) 32));
assertEquals("Reserved", DescriptorUtils.getUsageTypeName((byte) 48));
}
/**
* Tests the {@link DescriptorUtils#getTransferTypeName(int)} method.
* Tests the {@link DescriptorUtils#getTransferTypeName(byte)} method.
*/
@Test
public void testGetTransferTypeName()
{
assertEquals("Control", DescriptorUtils.getTransferTypeName(0));
assertEquals("Isochronous", DescriptorUtils.getTransferTypeName(1));
assertEquals("Bulk", DescriptorUtils.getTransferTypeName(2));
assertEquals("Interrupt", DescriptorUtils.getTransferTypeName(3));
assertEquals("Control", DescriptorUtils.getTransferTypeName((byte) 0));
assertEquals("Isochronous", DescriptorUtils.getTransferTypeName((byte) 1));
assertEquals("Bulk", DescriptorUtils.getTransferTypeName((byte) 2));
assertEquals("Interrupt", DescriptorUtils.getTransferTypeName((byte) 3));
}
/**
* Tests the {@link DescriptorUtils#getUSBClassName(int)} method.
* Tests the {@link DescriptorUtils#getUSBClassName(byte)} method.
*/
@Test
public void testGetUSBClassName()
{
assertEquals("Audio",
DescriptorUtils.getUSBClassName(LibUsb.CLASS_AUDIO));
assertEquals("Unknown", DescriptorUtils.getUSBClassName(0x1234));
assertEquals("Unknown", DescriptorUtils.getUSBClassName((byte) 0xF3));
}
/**
@ -178,7 +178,7 @@ public class DescriptorUtilsTest
(byte) 7, (byte) 8, (byte) 9, (byte) 10, (byte) 11, (byte) 12,
(byte) 13), "Manufacturer", "Product", "Serial"));
}
/**
* Tests the
* {@link DescriptorUtils#dump(javax.usb.UsbConfigurationDescriptor)}
@ -248,7 +248,7 @@ public class DescriptorUtilsTest
(byte) 1, (byte) 2, (byte) 3, (byte) 4, (byte) 5, (byte) 0x20,
(byte) 7)));
}
/**
* Tests the {@link DescriptorUtils#dump(javax.usb.UsbInterfaceDescriptor)}
* method.
@ -262,7 +262,7 @@ public class DescriptorUtilsTest
+ " bInterfaceNumber 2\n"
+ " bAlternateSetting 3\n"
+ " bNumEndpoints 4\n"
+ " bInterfaceClass 5 Unknown\n"
+ " bInterfaceClass 5 Physical\n"
+ " bInterfaceSubClass 6\n"
+ " bInterfaceProtocol 7\n"
+ " iInterface 8\n",
@ -312,7 +312,7 @@ public class DescriptorUtilsTest
DescriptorUtils.dump(new SimpleUsbEndpointDescriptor((byte) 0,
(byte) 1, (byte) 0x82, (byte) 3, (byte) 4, (byte) 5)));
}
/**
* Ensure constructor is private.
*